Author: gd
Date: 2007-07-09 16:03:00 +0000 (Mon, 09 Jul 2007)
New Revision: 23772

WebSVN: 
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=23772

Log:
Add ads_find_samaccount() helper function.

Guenther

Modified:
   branches/SAMBA_3_0/source/libads/ldap.c
   branches/SAMBA_3_0/source/utils/net_ads_gpo.c
   branches/SAMBA_3_0_26/source/libads/ldap.c
   branches/SAMBA_3_0_26/source/utils/net_ads_gpo.c


Changeset:
Modified: branches/SAMBA_3_0/source/libads/ldap.c
===================================================================
--- branches/SAMBA_3_0/source/libads/ldap.c     2007-07-09 15:53:08 UTC (rev 
23771)
+++ branches/SAMBA_3_0/source/libads/ldap.c     2007-07-09 16:03:00 UTC (rev 
23772)
@@ -3272,4 +3272,64 @@
        return ADS_ERROR_LDAP(LDAP_SUCCESS);
 }
 
+ADS_STATUS ads_find_samaccount(ADS_STRUCT *ads,
+                              TALLOC_CTX *mem_ctx,
+                              const char *samaccountname,
+                              uint32 *uac_ret,
+                              const char **dn_ret)
+{
+       ADS_STATUS status;
+       const char *attrs[] = { "userAccountControl", NULL };
+       const char *filter;
+       LDAPMessage *res = NULL;
+       char *dn = NULL;
+       uint32 uac = 0;
+
+       filter = talloc_asprintf(mem_ctx, 
"(&(objectclass=user)(sAMAccountName=%s))",
+               samaccountname);
+       if (filter == NULL) {
+               goto out;
+       }
+
+       status = ads_do_search_all(ads, ads->config.bind_path,
+                                  LDAP_SCOPE_SUBTREE,
+                                  filter, attrs, &res);
+       
+       if (!ADS_ERR_OK(status)) {
+               goto out;
+       }
+
+       if (ads_count_replies(ads, res) != 1) {
+               printf("no result\n");
+               goto out;
+       }
+
+       dn = ads_get_dn(ads, res);
+       if (dn == NULL) {
+               status = ADS_ERROR(LDAP_NO_MEMORY);
+               goto out;
+       }
+
+       if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
+               status = ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
+               goto out;
+       }
+
+       if (uac_ret) {
+               *uac_ret = uac;
+       }
+
+       if (dn_ret) {
+               *dn_ret = talloc_strdup(mem_ctx, dn);
+               if (!*dn_ret) {
+                       status = ADS_ERROR(LDAP_NO_MEMORY);
+                       goto out;
+               }
+       }
+ out:
+       ads_memfree(ads, dn);
+       ads_msgfree(ads, res);
+
+       return status;
+}
 #endif

Modified: branches/SAMBA_3_0/source/utils/net_ads_gpo.c
===================================================================
--- branches/SAMBA_3_0/source/utils/net_ads_gpo.c       2007-07-09 15:53:08 UTC 
(rev 23771)
+++ branches/SAMBA_3_0/source/utils/net_ads_gpo.c       2007-07-09 16:03:00 UTC 
(rev 23772)
@@ -46,10 +46,7 @@
        TALLOC_CTX *mem_ctx;
        ADS_STRUCT *ads;
        ADS_STATUS status;
-       const char *attrs[] = { "userAccountControl", NULL };
-       LDAPMessage *res = NULL;
-       const char *filter;
-       char *dn = NULL;
+       const char *dn = NULL;
        struct GROUP_POLICY_OBJECT *gpo_list = NULL;
        uint32 uac = 0;
        uint32 flags = 0;
@@ -66,38 +63,17 @@
                return -1;
        }
 
-       filter = talloc_asprintf(mem_ctx, 
"(&(objectclass=user)(sAMAccountName=%s))", argv[0]);
-       if (filter == NULL) {
-               goto out;
-       }
-
        status = ads_startup(False, &ads);
        if (!ADS_ERR_OK(status)) {
                goto out;
        }
 
-       status = ads_do_search_all(ads, ads->config.bind_path,
-                                  LDAP_SCOPE_SUBTREE,
-                                  filter, attrs, &res);
-       
+       status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
        if (!ADS_ERR_OK(status)) {
+               printf("failed to find samaccount for %s\n", argv[0]);
                goto out;
        }
 
-       if (ads_count_replies(ads, res) != 1) {
-               printf("no result\n");
-               goto out;
-       }
-
-       dn = ads_get_dn(ads, res);
-       if (dn == NULL) {
-               goto out;
-       }
-
-       if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
-               goto out;
-       }
-
        if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
                flags |= GPO_LIST_FLAG_MACHINE;
        }
@@ -139,9 +115,6 @@
        }
 
  out:
-       ads_memfree(ads, dn);
-       ads_msgfree(ads, res);
-
        ads_destroy(&ads);
        talloc_destroy(mem_ctx);
        return 0;
@@ -225,10 +198,7 @@
        TALLOC_CTX *mem_ctx;
        ADS_STRUCT *ads;
        ADS_STATUS status;
-       const char *attrs[] = {"distinguishedName", "userAccountControl", NULL};
-       LDAPMessage *res = NULL;
-       const char *filter;
-       char *dn = NULL;
+       const char *dn = NULL;
        struct GROUP_POLICY_OBJECT *gpo_list;
        uint32 uac = 0;
        uint32 flags = 0;
@@ -243,38 +213,16 @@
                goto out;
        }
 
-       filter = talloc_asprintf(mem_ctx, 
"(&(objectclass=user)(sAMAccountName=%s))", argv[0]);
-       if (filter == NULL) {
-               goto out;
-       }
-
        status = ads_startup(False, &ads);
        if (!ADS_ERR_OK(status)) {
                goto out;
        }
 
-       status = ads_do_search_all(ads, ads->config.bind_path,
-                                  LDAP_SCOPE_SUBTREE,
-                                  filter, attrs, &res);
-       
+       status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
        if (!ADS_ERR_OK(status)) {
                goto out;
        }
 
-       if (ads_count_replies(ads, res) != 1) {
-               printf("no result\n");
-               goto out;
-       }
-
-       dn = ads_get_dn(ads, res);
-       if (dn == NULL) {
-               goto out;
-       }
-
-       if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
-               goto out;
-       }
-
        if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
                flags |= GPO_LIST_FLAG_MACHINE;
        }
@@ -289,15 +237,12 @@
        }
 
        /* FIXME: allow to process just a single extension */
-       status = gpo_process_gpo_list(ads, mem_ctx, &gpo_list, NULL, flags); 
+       status = gpo_process_gpo_list(ads, mem_ctx, gpo_list, NULL, flags); 
        if (!ADS_ERR_OK(status)) {
                goto out;
        }
 
 out:
-       ads_memfree(ads, dn);
-       ads_msgfree(ads, res);
-
        ads_destroy(&ads);
        talloc_destroy(mem_ctx);
        return 0;

Modified: branches/SAMBA_3_0_26/source/libads/ldap.c
===================================================================
--- branches/SAMBA_3_0_26/source/libads/ldap.c  2007-07-09 15:53:08 UTC (rev 
23771)
+++ branches/SAMBA_3_0_26/source/libads/ldap.c  2007-07-09 16:03:00 UTC (rev 
23772)
@@ -3272,4 +3272,64 @@
        return ADS_ERROR_LDAP(LDAP_SUCCESS);
 }
 
+ADS_STATUS ads_find_samaccount(ADS_STRUCT *ads,
+                              TALLOC_CTX *mem_ctx,
+                              const char *samaccountname,
+                              uint32 *uac_ret,
+                              const char **dn_ret)
+{
+       ADS_STATUS status;
+       const char *attrs[] = { "userAccountControl", NULL };
+       const char *filter;
+       LDAPMessage *res = NULL;
+       char *dn = NULL;
+       uint32 uac = 0;
+
+       filter = talloc_asprintf(mem_ctx, 
"(&(objectclass=user)(sAMAccountName=%s))",
+               samaccountname);
+       if (filter == NULL) {
+               goto out;
+       }
+
+       status = ads_do_search_all(ads, ads->config.bind_path,
+                                  LDAP_SCOPE_SUBTREE,
+                                  filter, attrs, &res);
+       
+       if (!ADS_ERR_OK(status)) {
+               goto out;
+       }
+
+       if (ads_count_replies(ads, res) != 1) {
+               printf("no result\n");
+               goto out;
+       }
+
+       dn = ads_get_dn(ads, res);
+       if (dn == NULL) {
+               status = ADS_ERROR(LDAP_NO_MEMORY);
+               goto out;
+       }
+
+       if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
+               status = ADS_ERROR(LDAP_NO_SUCH_ATTRIBUTE);
+               goto out;
+       }
+
+       if (uac_ret) {
+               *uac_ret = uac;
+       }
+
+       if (dn_ret) {
+               *dn_ret = talloc_strdup(mem_ctx, dn);
+               if (!*dn_ret) {
+                       status = ADS_ERROR(LDAP_NO_MEMORY);
+                       goto out;
+               }
+       }
+ out:
+       ads_memfree(ads, dn);
+       ads_msgfree(ads, res);
+
+       return status;
+}
 #endif

Modified: branches/SAMBA_3_0_26/source/utils/net_ads_gpo.c
===================================================================
--- branches/SAMBA_3_0_26/source/utils/net_ads_gpo.c    2007-07-09 15:53:08 UTC 
(rev 23771)
+++ branches/SAMBA_3_0_26/source/utils/net_ads_gpo.c    2007-07-09 16:03:00 UTC 
(rev 23772)
@@ -46,10 +46,7 @@
        TALLOC_CTX *mem_ctx;
        ADS_STRUCT *ads;
        ADS_STATUS status;
-       const char *attrs[] = { "userAccountControl", NULL };
-       LDAPMessage *res = NULL;
-       const char *filter;
-       char *dn = NULL;
+       const char *dn = NULL;
        struct GROUP_POLICY_OBJECT *gpo_list = NULL;
        uint32 uac = 0;
        uint32 flags = 0;
@@ -66,38 +63,17 @@
                return -1;
        }
 
-       filter = talloc_asprintf(mem_ctx, 
"(&(objectclass=user)(sAMAccountName=%s))", argv[0]);
-       if (filter == NULL) {
-               goto out;
-       }
-
        status = ads_startup(False, &ads);
        if (!ADS_ERR_OK(status)) {
                goto out;
        }
 
-       status = ads_do_search_all(ads, ads->config.bind_path,
-                                  LDAP_SCOPE_SUBTREE,
-                                  filter, attrs, &res);
-       
+       status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
        if (!ADS_ERR_OK(status)) {
+               printf("failed to find samaccount for %s\n", argv[0]);
                goto out;
        }
 
-       if (ads_count_replies(ads, res) != 1) {
-               printf("no result\n");
-               goto out;
-       }
-
-       dn = ads_get_dn(ads, res);
-       if (dn == NULL) {
-               goto out;
-       }
-
-       if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
-               goto out;
-       }
-
        if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
                flags |= GPO_LIST_FLAG_MACHINE;
        }
@@ -139,9 +115,6 @@
        }
 
  out:
-       ads_memfree(ads, dn);
-       ads_msgfree(ads, res);
-
        ads_destroy(&ads);
        talloc_destroy(mem_ctx);
        return 0;
@@ -225,10 +198,7 @@
        TALLOC_CTX *mem_ctx;
        ADS_STRUCT *ads;
        ADS_STATUS status;
-       const char *attrs[] = {"distinguishedName", "userAccountControl", NULL};
-       LDAPMessage *res = NULL;
-       const char *filter;
-       char *dn = NULL;
+       const char *dn = NULL;
        struct GROUP_POLICY_OBJECT *gpo_list;
        uint32 uac = 0;
        uint32 flags = 0;
@@ -243,38 +213,16 @@
                goto out;
        }
 
-       filter = talloc_asprintf(mem_ctx, 
"(&(objectclass=user)(sAMAccountName=%s))", argv[0]);
-       if (filter == NULL) {
-               goto out;
-       }
-
        status = ads_startup(False, &ads);
        if (!ADS_ERR_OK(status)) {
                goto out;
        }
 
-       status = ads_do_search_all(ads, ads->config.bind_path,
-                                  LDAP_SCOPE_SUBTREE,
-                                  filter, attrs, &res);
-       
+       status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
        if (!ADS_ERR_OK(status)) {
                goto out;
        }
 
-       if (ads_count_replies(ads, res) != 1) {
-               printf("no result\n");
-               goto out;
-       }
-
-       dn = ads_get_dn(ads, res);
-       if (dn == NULL) {
-               goto out;
-       }
-
-       if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
-               goto out;
-       }
-
        if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
                flags |= GPO_LIST_FLAG_MACHINE;
        }
@@ -289,15 +237,12 @@
        }
 
        /* FIXME: allow to process just a single extension */
-       status = gpo_process_gpo_list(ads, mem_ctx, &gpo_list, NULL, flags); 
+       status = gpo_process_gpo_list(ads, mem_ctx, gpo_list, NULL, flags); 
        if (!ADS_ERR_OK(status)) {
                goto out;
        }
 
 out:
-       ads_memfree(ads, dn);
-       ads_msgfree(ads, res);
-
        ads_destroy(&ads);
        talloc_destroy(mem_ctx);
        return 0;

Reply via email to