r23772: Add ads_find_samaccount() helper function.
[jra/samba/.git] / source / utils / net_ads_gpo.c
index 1865aee3d41be1f18e16f9b380025bb4388fd8f3..be5406af17d1e6a111f14d8e7f3d71da4d89228b 100644 (file)
@@ -46,11 +46,8 @@ static int net_ads_gpo_refresh(int argc, const char **argv)
        TALLOC_CTX *mem_ctx;
        ADS_STRUCT *ads;
        ADS_STATUS status;
-       const char *attrs[] = { "userAccountControl", NULL };
-       LDAPMessage *res = NULL;
-       const char *filter;
-       char *dn = NULL;
-       struct GROUP_POLICY_OBJECT *gpo_list;
+       const char *dn = NULL;
+       struct GROUP_POLICY_OBJECT *gpo_list = NULL;
        uint32 uac = 0;
        uint32 flags = 0;
        struct GROUP_POLICY_OBJECT *gpo;
@@ -66,35 +63,14 @@ static int net_ads_gpo_refresh(int argc, const char **argv)
                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)) {
-               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)) {
+               printf("failed to find samaccount for %s\n", argv[0]);
                goto out;
        }
 
@@ -127,8 +103,8 @@ static int net_ads_gpo_refresh(int argc, const char **argv)
                        GPO_VERSION_USER(gpo->version),
                        GPO_VERSION_MACHINE(gpo->version));
 
-               result = ads_gpo_explode_filesyspath(ads, mem_ctx, gpo->file_sys_path,
-                                                    &server, &share, &nt_path, &unix_path);
+               result = gpo_explode_filesyspath(mem_ctx, gpo->file_sys_path,
+                                                &server, &share, &nt_path, &unix_path);
                if (!NT_STATUS_IS_OK(result)) {
                        printf("got: %s\n", nt_errstr(result));
                }
@@ -139,9 +115,6 @@ static int net_ads_gpo_refresh(int argc, const char **argv)
        }
 
  out:
-       ads_memfree(ads, dn);
-       ads_msgfree(ads, res);
-
        ads_destroy(&ads);
        talloc_destroy(mem_ctx);
        return 0;
@@ -225,10 +198,7 @@ static int net_ads_gpo_apply(int argc, const char **argv)
        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 @@ static int net_ads_gpo_apply(int argc, const char **argv)
                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 @@ static int net_ads_gpo_apply(int argc, const char **argv)
        }
 
        /* 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;
@@ -351,6 +296,7 @@ static int net_ads_gpo_add_link(int argc, const char **argv)
 
        if (argc < 2) {
                printf("usage: net ads gpo addlink <linkdn> <gpodn> [options]\n");
+               printf("note: DNs must be provided properly escaped.\n      See RFC 4514 for details\n");
                return -1;
        }