2 Samba Unix/Linux SMB client library
3 net ads commands for Group Policy
4 Copyright (C) 2005-2008 Guenther Deschner (gd@samba.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "utils/net.h"
22 #include "../libgpo/gpo.h"
23 #include "libgpo/gpo_proto.h"
27 static int net_ads_gpo_refresh(struct net_context *c, int argc, const char **argv)
32 const char *dn = NULL;
33 struct GROUP_POLICY_OBJECT *gpo_list = NULL;
34 struct GROUP_POLICY_OBJECT *read_list = NULL;
37 struct GROUP_POLICY_OBJECT *gpo;
39 struct nt_user_token *token = NULL;
41 if (argc < 1 || c->display_usage) {
42 d_printf("%s\n%s\n%s",
44 _("net ads gpo refresh <username|machinename>"),
45 _(" Lists all GPOs assigned to an account and "
47 " username\tUser to refresh GPOs for\n"
48 " machinename\tMachine to refresh GPOs for\n"));
52 mem_ctx = talloc_init("net_ads_gpo_refresh");
53 if (mem_ctx == NULL) {
57 status = ads_startup(c, false, &ads);
58 if (!ADS_ERR_OK(status)) {
59 d_printf(_("failed to connect AD server: %s\n"), ads_errstr(status));
63 status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
64 if (!ADS_ERR_OK(status)) {
65 d_printf(_("failed to find samaccount for %s\n"), argv[0]);
69 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
70 flags |= GPO_LIST_FLAG_MACHINE;
73 d_printf(_("\n%s: '%s' has dn: '%s'\n\n"),
74 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? _("machine") : _("user"),
77 d_printf(_("* fetching token "));
78 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
79 status = gp_get_machine_token(ads, mem_ctx, NULL, dn, &token);
81 status = ads_get_sid_token(ads, mem_ctx, dn, &token);
84 if (!ADS_ERR_OK(status)) {
85 d_printf(_("failed: %s\n"), ads_errstr(status));
88 d_printf(_("finished\n"));
90 d_printf(_("* fetching GPO List "));
91 status = ads_get_gpo_list(ads, mem_ctx, dn, flags, token, &gpo_list);
92 if (!ADS_ERR_OK(status)) {
93 d_printf(_("failed: %s\n"),
97 d_printf(_("finished\n"));
99 d_printf(_("* Refreshing Group Policy Data "));
100 if (!NT_STATUS_IS_OK(result = check_refresh_gpo_list(ads, mem_ctx,
101 cache_path(GPO_CACHE_DIR),
105 d_printf(_("failed: %s\n"), nt_errstr(result));
108 d_printf(_("finished\n"));
110 d_printf(_("* storing GPO list to registry "));
113 WERROR werr = gp_reg_state_store(mem_ctx, flags, dn,
115 if (!W_ERROR_IS_OK(werr)) {
116 d_printf(_("failed: %s\n"), win_errstr(werr));
121 d_printf(_("finished\n"));
123 if (c->opt_verbose) {
125 d_printf(_("* dumping GPO list\n"));
127 for (gpo = gpo_list; gpo; gpo = gpo->next) {
129 dump_gpo(ads, mem_ctx, gpo, 0);
131 char *server, *share, *nt_path, *unix_path;
133 d_printf("--------------------------------------\n");
134 d_printf("Name:\t\t\t%s\n", gpo->display_name);
135 d_printf("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
137 GPO_VERSION_USER(gpo->version),
138 GPO_VERSION_MACHINE(gpo->version));
140 result = gpo_explode_filesyspath(mem_ctx, gpo->file_sys_path,
141 &server, &share, &nt_path,
143 if (!NT_STATUS_IS_OK(result)) {
144 d_printf("got: %s\n", nt_errstr(result));
147 d_printf("GPO stored on server: %s, share: %s\n", server, share);
148 d_printf("\tremote path:\t%s\n", nt_path);
149 d_printf("\tlocal path:\t%s\n", unix_path);
154 d_printf(_("* re-reading GPO list from registry "));
157 WERROR werr = gp_reg_state_read(mem_ctx, flags,
158 &token->user_sids[0],
160 if (!W_ERROR_IS_OK(werr)) {
161 d_printf(_("failed: %s\n"), win_errstr(werr));
166 d_printf(_("finished\n"));
168 if (c->opt_verbose) {
170 d_printf(_("* dumping GPO list from registry\n"));
172 for (gpo = read_list; gpo; gpo = gpo->next) {
174 dump_gpo(ads, mem_ctx, gpo, 0);
177 char *server, *share, *nt_path, *unix_path;
179 d_printf("--------------------------------------\n");
180 d_printf("Name:\t\t\t%s\n", gpo->display_name);
181 d_printf("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
183 GPO_VERSION_USER(gpo->version),
184 GPO_VERSION_MACHINE(gpo->version));
186 result = gpo_explode_filesyspath(mem_ctx, gpo->file_sys_path,
187 &server, &share, &nt_path,
189 if (!NT_STATUS_IS_OK(result)) {
190 d_printf("got: %s\n", nt_errstr(result));
193 d_printf("GPO stored on server: %s, share: %s\n", server, share);
194 d_printf("\tremote path:\t%s\n", nt_path);
195 d_printf("\tlocal path:\t%s\n", unix_path);
202 talloc_destroy(mem_ctx);
206 static int net_ads_gpo_list_all(struct net_context *c, int argc, const char **argv)
210 LDAPMessage *res = NULL;
212 LDAPMessage *msg = NULL;
213 struct GROUP_POLICY_OBJECT gpo;
216 const char *attrs[] = {
222 "gPCMachineExtensionNames",
223 "gPCUserExtensionNames",
224 "ntSecurityDescriptor",
228 if (c->display_usage) {
230 "net ads gpo listall\n"
233 _("List all GPOs on the DC"));
237 mem_ctx = talloc_init("net_ads_gpo_list_all");
238 if (mem_ctx == NULL) {
242 status = ads_startup(c, false, &ads);
243 if (!ADS_ERR_OK(status)) {
247 status = ads_do_search_all_sd_flags(ads, ads->config.bind_path,
249 "(objectclass=groupPolicyContainer)",
251 DACL_SECURITY_INFORMATION,
254 if (!ADS_ERR_OK(status)) {
255 d_printf(_("search failed: %s\n"), ads_errstr(status));
259 num_reply = ads_count_replies(ads, res);
261 d_printf(_("Got %d replies\n\n"), num_reply);
263 /* dump the results */
264 for (msg = ads_first_entry(ads, res);
266 msg = ads_next_entry(ads, msg)) {
268 if ((dn = ads_get_dn(ads, mem_ctx, msg)) == NULL) {
272 status = ads_parse_gpo(ads, mem_ctx, msg, dn, &gpo);
274 if (!ADS_ERR_OK(status)) {
275 d_printf(_("ads_parse_gpo failed: %s\n"),
280 dump_gpo(ads, mem_ctx, &gpo, 0);
284 ads_msgfree(ads, res);
286 TALLOC_FREE(mem_ctx);
292 static int net_ads_gpo_list(struct net_context *c, int argc, const char **argv)
296 LDAPMessage *res = NULL;
298 const char *dn = NULL;
301 struct GROUP_POLICY_OBJECT *gpo_list;
302 struct nt_user_token *token = NULL;
304 if (argc < 1 || c->display_usage) {
305 d_printf("%s\n%s\n%s",
307 _("net ads gpo list <username|machinename>"),
308 _(" Lists all GPOs for machine/user\n"
309 " username\tUser to list GPOs for\n"
310 " machinename\tMachine to list GPOs for\n"));
314 mem_ctx = talloc_init("net_ads_gpo_list");
315 if (mem_ctx == NULL) {
319 status = ads_startup(c, false, &ads);
320 if (!ADS_ERR_OK(status)) {
324 status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
325 if (!ADS_ERR_OK(status)) {
329 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
330 flags |= GPO_LIST_FLAG_MACHINE;
333 d_printf(_("%s: '%s' has dn: '%s'\n"),
334 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? _("machine") : _("user"),
337 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
338 status = gp_get_machine_token(ads, mem_ctx, NULL, dn, &token);
340 status = ads_get_sid_token(ads, mem_ctx, dn, &token);
343 if (!ADS_ERR_OK(status)) {
347 status = ads_get_gpo_list(ads, mem_ctx, dn, flags, token, &gpo_list);
348 if (!ADS_ERR_OK(status)) {
352 dump_gpo_list(ads, mem_ctx, gpo_list, 0);
355 ads_msgfree(ads, res);
357 talloc_destroy(mem_ctx);
364 static int net_ads_gpo_apply(struct net_context *c, int argc, const char **argv)
369 const char *dn = NULL;
370 struct GROUP_POLICY_OBJECT *gpo_list;
373 struct nt_user_token *token = NULL;
374 const char *filter = NULL;
376 if (argc < 1 || c->display_usage) {
378 "net ads gpo apply <username|machinename>\n"
379 " Apply GPOs for machine/user\n"
380 " username\tUsername to apply GPOs for\n"
381 " machinename\tMachine to apply GPOs for\n");
385 mem_ctx = talloc_init("net_ads_gpo_apply");
386 if (mem_ctx == NULL) {
391 filter = cse_gpo_name_to_guid_string(argv[1]);
394 status = ads_startup(c, false, &ads);
395 if (!ADS_ERR_OK(status)) {
396 d_printf("got: %s\n", ads_errstr(status));
400 status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
401 if (!ADS_ERR_OK(status)) {
402 d_printf("failed to find samaccount for %s: %s\n",
403 argv[0], ads_errstr(status));
407 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
408 flags |= GPO_LIST_FLAG_MACHINE;
411 if (c->opt_verbose) {
412 flags |= GPO_INFO_FLAG_VERBOSE;
415 d_printf("%s: '%s' has dn: '%s'\n",
416 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? "machine" : "user",
419 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
420 status = gp_get_machine_token(ads, mem_ctx, NULL, dn, &token);
422 status = ads_get_sid_token(ads, mem_ctx, dn, &token);
425 if (!ADS_ERR_OK(status)) {
429 status = ads_get_gpo_list(ads, mem_ctx, dn, flags, token, &gpo_list);
430 if (!ADS_ERR_OK(status)) {
434 status = gpo_process_gpo_list(ads, mem_ctx, token, gpo_list,
436 if (!ADS_ERR_OK(status)) {
437 d_printf("failed to process gpo list: %s\n",
444 talloc_destroy(mem_ctx);
449 static int net_ads_gpo_link_get(struct net_context *c, int argc, const char **argv)
454 struct GP_LINK gp_link;
456 if (argc < 1 || c->display_usage) {
457 d_printf("%s\n%s\n%s",
459 _("net ads gpo linkget <container>"),
460 _(" Lists gPLink of a containter\n"
461 " container\tContainer to get link for\n"));
465 mem_ctx = talloc_init("add_gpo_link");
466 if (mem_ctx == NULL) {
470 status = ads_startup(c, false, &ads);
471 if (!ADS_ERR_OK(status)) {
475 status = ads_get_gpo_link(ads, mem_ctx, argv[0], &gp_link);
476 if (!ADS_ERR_OK(status)) {
477 d_printf(_("get link for %s failed: %s\n"), argv[0],
482 dump_gplink(ads, mem_ctx, &gp_link);
485 talloc_destroy(mem_ctx);
491 static int net_ads_gpo_link_add(struct net_context *c, int argc, const char **argv)
498 if (argc < 2 || c->display_usage) {
499 d_printf("%s\n%s\n%s",
501 _("net ads gpo linkadd <linkdn> <gpodn> [options]"),
502 _(" Link a container to a GPO\n"
503 " linkdn\tContainer to link to a GPO\n"
504 " gpodn\tGPO to link container to\n"));
505 d_printf(_("note: DNs must be provided properly escaped.\n"
506 "See RFC 4514 for details\n"));
510 mem_ctx = talloc_init("add_gpo_link");
511 if (mem_ctx == NULL) {
516 gpo_opt = atoi(argv[2]);
519 status = ads_startup(c, false, &ads);
520 if (!ADS_ERR_OK(status)) {
524 status = ads_add_gpo_link(ads, mem_ctx, argv[0], argv[1], gpo_opt);
525 if (!ADS_ERR_OK(status)) {
526 d_printf(_("link add failed: %s\n"), ads_errstr(status));
531 talloc_destroy(mem_ctx);
539 static int net_ads_gpo_link_delete(struct net_context *c, int argc, const char **argv)
545 if (argc < 2 || c->display_usage) {
547 "net ads gpo linkdelete <linkdn> <gpodn>\n"
548 " Delete a GPO link\n"
549 " <linkdn>\tContainer to delete GPO from\n"
550 " <gpodn>\tGPO to delete from container\n");
554 mem_ctx = talloc_init("delete_gpo_link");
555 if (mem_ctx == NULL) {
559 status = ads_startup(c, false, &ads);
560 if (!ADS_ERR_OK(status)) {
564 status = ads_delete_gpo_link(ads, mem_ctx, argv[0], argv[1]);
565 if (!ADS_ERR_OK(status)) {
566 d_printf("delete link failed: %s\n", ads_errstr(status));
571 talloc_destroy(mem_ctx);
579 static int net_ads_gpo_get_gpo(struct net_context *c, int argc, const char **argv)
584 struct GROUP_POLICY_OBJECT gpo;
586 if (argc < 1 || c->display_usage) {
587 d_printf("%s\n%s\n%s",
589 _("net ads gpo getgpo <gpo>"),
590 _(" List speciefied GPO\n"
591 " gpo\t\tGPO to list\n"));
595 mem_ctx = talloc_init("ads_gpo_get_gpo");
596 if (mem_ctx == NULL) {
600 status = ads_startup(c, false, &ads);
601 if (!ADS_ERR_OK(status)) {
605 if (strnequal(argv[0], "CN={", strlen("CN={"))) {
606 status = ads_get_gpo(ads, mem_ctx, argv[0], NULL, NULL, &gpo);
608 status = ads_get_gpo(ads, mem_ctx, NULL, argv[0], NULL, &gpo);
611 if (!ADS_ERR_OK(status)) {
612 d_printf(_("get gpo for [%s] failed: %s\n"), argv[0],
617 dump_gpo(ads, mem_ctx, &gpo, 1);
620 talloc_destroy(mem_ctx);
626 int net_ads_gpo(struct net_context *c, int argc, const char **argv)
628 struct functable func[] = {
634 "Apply GPO to container",
635 "net ads gpo apply\n"
636 " Apply GPO to container"
643 N_("List specified GPO"),
644 N_("net ads gpo getgpo\n"
645 " List specified GPO")
649 net_ads_gpo_link_add,
651 N_("Link a container to a GPO"),
652 N_("net ads gpo linkadd\n"
653 " Link a container to a GPO")
658 net_ads_gpo_link_delete,
660 "Delete GPO link from a container",
661 "net ads gpo linkdelete\n"
662 " Delete GPO link from a container"
667 net_ads_gpo_link_get,
669 N_("Lists gPLink of containter"),
670 N_("net ads gpo linkget\n"
671 " Lists gPLink of containter")
677 N_("Lists all GPOs for machine/user"),
678 N_("net ads gpo list\n"
679 " Lists all GPOs for machine/user")
683 net_ads_gpo_list_all,
685 N_("Lists all GPOs on a DC"),
686 N_("net ads gpo listall\n"
687 " Lists all GPOs on a DC")
693 N_("Lists all GPOs assigned to an account and "
695 N_("net ads gpo refresh\n"
696 " Lists all GPOs assigned to an account and "
699 {NULL, NULL, 0, NULL, NULL}
702 return net_run_function(c, argc, argv, "net ads gpo", func);
705 #endif /* HAVE_ADS */