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"
25 static int net_ads_gpo_refresh(struct net_context
*c
, int argc
, const char **argv
)
30 const char *dn
= NULL
;
31 struct GROUP_POLICY_OBJECT
*gpo_list
= NULL
;
32 struct GROUP_POLICY_OBJECT
*read_list
= NULL
;
35 struct GROUP_POLICY_OBJECT
*gpo
;
37 struct nt_user_token
*token
= NULL
;
39 if (argc
< 1 || c
->display_usage
) {
41 "net ads gpo refresh <username|machinename>\n"
42 " Lists all GPOs assigned to an account and "
44 " username\tUser to refresh GPOs for\n"
45 " machinename\tMachine to refresh GPOs for\n");
49 mem_ctx
= talloc_init("net_ads_gpo_refresh");
50 if (mem_ctx
== NULL
) {
54 status
= ads_startup(c
, false, &ads
);
55 if (!ADS_ERR_OK(status
)) {
56 d_printf("failed to connect AD server: %s\n", ads_errstr(status
));
60 status
= ads_find_samaccount(ads
, mem_ctx
, argv
[0], &uac
, &dn
);
61 if (!ADS_ERR_OK(status
)) {
62 d_printf("failed to find samaccount for %s\n", argv
[0]);
66 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
67 flags
|= GPO_LIST_FLAG_MACHINE
;
70 d_printf("\n%s: '%s' has dn: '%s'\n\n",
71 (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) ? "machine" : "user",
74 d_printf("* fetching token ");
75 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
76 status
= gp_get_machine_token(ads
, mem_ctx
, dn
, &token
);
78 status
= ads_get_sid_token(ads
, mem_ctx
, dn
, &token
);
81 if (!ADS_ERR_OK(status
)) {
82 d_printf("failed: %s\n", ads_errstr(status
));
85 d_printf("finished\n");
87 d_printf("* fetching GPO List ");
88 status
= ads_get_gpo_list(ads
, mem_ctx
, dn
, flags
, token
, &gpo_list
);
89 if (!ADS_ERR_OK(status
)) {
90 d_printf("failed: %s\n", ads_errstr(status
));
93 d_printf("finished\n");
95 d_printf("* refreshing Group Policy Data ");
96 if (!NT_STATUS_IS_OK(result
= check_refresh_gpo_list(ads
, mem_ctx
,
99 d_printf("failed: %s\n", nt_errstr(result
));
102 d_printf("finished\n");
104 d_printf("* storing GPO list to registry ");
107 WERROR werr
= gp_reg_state_store(mem_ctx
, flags
, dn
,
109 if (!W_ERROR_IS_OK(werr
)) {
110 d_printf("failed: %s\n", win_errstr(werr
));
115 d_printf("finished\n");
117 if (c
->opt_verbose
) {
119 d_printf("* dumping GPO list\n");
121 for (gpo
= gpo_list
; gpo
; gpo
= gpo
->next
) {
123 dump_gpo(ads
, mem_ctx
, gpo
, 0);
125 char *server
, *share
, *nt_path
, *unix_path
;
127 d_printf("--------------------------------------\n");
128 d_printf("Name:\t\t\t%s\n", gpo
->display_name
);
129 d_printf("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
131 GPO_VERSION_USER(gpo
->version
),
132 GPO_VERSION_MACHINE(gpo
->version
));
134 result
= gpo_explode_filesyspath(mem_ctx
, gpo
->file_sys_path
,
135 &server
, &share
, &nt_path
,
137 if (!NT_STATUS_IS_OK(result
)) {
138 d_printf("got: %s\n", nt_errstr(result
));
141 d_printf("GPO stored on server: %s, share: %s\n", server
, share
);
142 d_printf("\tremote path:\t%s\n", nt_path
);
143 d_printf("\tlocal path:\t%s\n", unix_path
);
148 d_printf("* re-reading GPO list from registry ");
151 WERROR werr
= gp_reg_state_read(mem_ctx
, flags
,
152 &token
->user_sids
[0],
154 if (!W_ERROR_IS_OK(werr
)) {
155 d_printf("failed: %s\n", win_errstr(werr
));
160 d_printf("finished\n");
162 if (c
->opt_verbose
) {
164 d_printf("* dumping GPO list from registry\n");
166 for (gpo
= read_list
; gpo
; gpo
= gpo
->next
) {
168 dump_gpo(ads
, mem_ctx
, gpo
, 0);
171 char *server
, *share
, *nt_path
, *unix_path
;
173 d_printf("--------------------------------------\n");
174 d_printf("Name:\t\t\t%s\n", gpo
->display_name
);
175 d_printf("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
177 GPO_VERSION_USER(gpo
->version
),
178 GPO_VERSION_MACHINE(gpo
->version
));
180 result
= gpo_explode_filesyspath(mem_ctx
, gpo
->file_sys_path
,
181 &server
, &share
, &nt_path
,
183 if (!NT_STATUS_IS_OK(result
)) {
184 d_printf("got: %s\n", nt_errstr(result
));
187 d_printf("GPO stored on server: %s, share: %s\n", server
, share
);
188 d_printf("\tremote path:\t%s\n", nt_path
);
189 d_printf("\tlocal path:\t%s\n", unix_path
);
196 talloc_destroy(mem_ctx
);
200 static int net_ads_gpo_list_all(struct net_context
*c
, int argc
, const char **argv
)
204 LDAPMessage
*res
= NULL
;
206 LDAPMessage
*msg
= NULL
;
207 struct GROUP_POLICY_OBJECT gpo
;
210 const char *attrs
[] = {
216 "gPCMachineExtensionNames",
217 "gPCUserExtensionNames",
218 "ntSecurityDescriptor",
222 if (c
->display_usage
) {
224 "net ads gpo listall\n"
225 " List all GPOs on the DC\n");
229 mem_ctx
= talloc_init("net_ads_gpo_list_all");
230 if (mem_ctx
== NULL
) {
234 status
= ads_startup(c
, false, &ads
);
235 if (!ADS_ERR_OK(status
)) {
239 status
= ads_do_search_all_sd_flags(ads
, ads
->config
.bind_path
,
241 "(objectclass=groupPolicyContainer)",
243 DACL_SECURITY_INFORMATION
,
246 if (!ADS_ERR_OK(status
)) {
247 d_printf("search failed: %s\n", ads_errstr(status
));
251 num_reply
= ads_count_replies(ads
, res
);
253 d_printf("Got %d replies\n\n", num_reply
);
255 /* dump the results */
256 for (msg
= ads_first_entry(ads
, res
);
258 msg
= ads_next_entry(ads
, msg
)) {
260 if ((dn
= ads_get_dn(ads
, mem_ctx
, msg
)) == NULL
) {
264 status
= ads_parse_gpo(ads
, mem_ctx
, msg
, dn
, &gpo
);
266 if (!ADS_ERR_OK(status
)) {
267 d_printf("ads_parse_gpo failed: %s\n",
272 dump_gpo(ads
, mem_ctx
, &gpo
, 0);
276 ads_msgfree(ads
, res
);
278 TALLOC_FREE(mem_ctx
);
284 static int net_ads_gpo_list(struct net_context
*c
, int argc
, const char **argv
)
288 LDAPMessage
*res
= NULL
;
290 const char *dn
= NULL
;
293 struct GROUP_POLICY_OBJECT
*gpo_list
;
294 struct nt_user_token
*token
= NULL
;
296 if (argc
< 1 || c
->display_usage
) {
298 "net ads gpo list <username|machinename>\n"
299 " Lists all GPOs for machine/user\n"
300 " username\tUser to list GPOs for\n"
301 " machinename\tMachine to list GPOs for\n");
305 mem_ctx
= talloc_init("net_ads_gpo_list");
306 if (mem_ctx
== NULL
) {
310 status
= ads_startup(c
, false, &ads
);
311 if (!ADS_ERR_OK(status
)) {
315 status
= ads_find_samaccount(ads
, mem_ctx
, argv
[0], &uac
, &dn
);
316 if (!ADS_ERR_OK(status
)) {
320 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
321 flags
|= GPO_LIST_FLAG_MACHINE
;
324 d_printf("%s: '%s' has dn: '%s'\n",
325 (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) ? "machine" : "user",
328 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
329 status
= gp_get_machine_token(ads
, mem_ctx
, dn
, &token
);
331 status
= ads_get_sid_token(ads
, mem_ctx
, dn
, &token
);
334 if (!ADS_ERR_OK(status
)) {
338 status
= ads_get_gpo_list(ads
, mem_ctx
, dn
, flags
, token
, &gpo_list
);
339 if (!ADS_ERR_OK(status
)) {
343 dump_gpo_list(ads
, mem_ctx
, gpo_list
, 0);
346 ads_msgfree(ads
, res
);
348 talloc_destroy(mem_ctx
);
355 static int net_ads_gpo_apply(struct net_context
*c
, int argc
, const char **argv
)
360 const char *dn
= NULL
;
361 struct GROUP_POLICY_OBJECT
*gpo_list
;
364 struct nt_user_token
*token
= NULL
;
365 const char *filter
= NULL
;
367 if (argc
< 1 || c
->display_usage
) {
369 "net ads gpo apply <username|machinename>\n"
370 " Apply GPOs for machine/user\n"
371 " username\tUsername to apply GPOs for\n"
372 " machinename\tMachine to apply GPOs for\n");
376 mem_ctx
= talloc_init("net_ads_gpo_apply");
377 if (mem_ctx
== NULL
) {
382 filter
= cse_gpo_name_to_guid_string(argv
[1]);
385 status
= ads_startup(c
, false, &ads
);
386 if (!ADS_ERR_OK(status
)) {
387 d_printf("got: %s\n", ads_errstr(status
));
391 status
= ads_find_samaccount(ads
, mem_ctx
, argv
[0], &uac
, &dn
);
392 if (!ADS_ERR_OK(status
)) {
393 d_printf("failed to find samaccount for %s: %s\n",
394 argv
[0], ads_errstr(status
));
398 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
399 flags
|= GPO_LIST_FLAG_MACHINE
;
403 flags
|= GPO_INFO_FLAG_VERBOSE
;
406 d_printf("%s: '%s' has dn: '%s'\n",
407 (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) ? "machine" : "user",
410 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
411 status
= gp_get_machine_token(ads
, mem_ctx
, dn
, &token
);
413 status
= ads_get_sid_token(ads
, mem_ctx
, dn
, &token
);
416 if (!ADS_ERR_OK(status
)) {
420 status
= ads_get_gpo_list(ads
, mem_ctx
, dn
, flags
, token
, &gpo_list
);
421 if (!ADS_ERR_OK(status
)) {
425 status
= gpo_process_gpo_list(ads
, mem_ctx
, token
, gpo_list
,
427 if (!ADS_ERR_OK(status
)) {
428 d_printf("failed to process gpo list: %s\n",
435 talloc_destroy(mem_ctx
);
440 static int net_ads_gpo_link_get(struct net_context
*c
, int argc
, const char **argv
)
445 struct GP_LINK gp_link
;
447 if (argc
< 1 || c
->display_usage
) {
449 "net ads gpo linkget <container>\n"
450 " Lists gPLink of a containter\n"
451 " container\tContainer to get link for\n");
455 mem_ctx
= talloc_init("add_gpo_link");
456 if (mem_ctx
== NULL
) {
460 status
= ads_startup(c
, false, &ads
);
461 if (!ADS_ERR_OK(status
)) {
465 status
= ads_get_gpo_link(ads
, mem_ctx
, argv
[0], &gp_link
);
466 if (!ADS_ERR_OK(status
)) {
467 d_printf("get link for %s failed: %s\n", argv
[0],
472 dump_gplink(ads
, mem_ctx
, &gp_link
);
475 talloc_destroy(mem_ctx
);
481 static int net_ads_gpo_link_add(struct net_context
*c
, int argc
, const char **argv
)
488 if (argc
< 2 || c
->display_usage
) {
490 "net ads gpo linkadd <linkdn> <gpodn> [options]\n"
491 " Link a container to a GPO\n"
492 " linkdn\tContainer to link to a GPO\n"
493 " gpodn\tGPO to link container to\n");
494 d_printf("note: DNs must be provided properly escaped.\n");
495 d_printf("See RFC 4514 for details\n");
499 mem_ctx
= talloc_init("add_gpo_link");
500 if (mem_ctx
== NULL
) {
505 gpo_opt
= atoi(argv
[2]);
508 status
= ads_startup(c
, false, &ads
);
509 if (!ADS_ERR_OK(status
)) {
513 status
= ads_add_gpo_link(ads
, mem_ctx
, argv
[0], argv
[1], gpo_opt
);
514 if (!ADS_ERR_OK(status
)) {
515 d_printf("link add failed: %s\n", ads_errstr(status
));
520 talloc_destroy(mem_ctx
);
528 static int net_ads_gpo_link_delete(struct net_context
*c
, int argc
, const char **argv
)
534 if (argc
< 2 || c
->display_usage
) {
536 "net ads gpo linkdelete <linkdn> <gpodn>\n"
537 " Delete a GPO link\n"
538 " <linkdn>\tContainer to delete GPO from\n"
539 " <gpodn>\tGPO to delete from container\n");
543 mem_ctx
= talloc_init("delete_gpo_link");
544 if (mem_ctx
== NULL
) {
548 status
= ads_startup(c
, false, &ads
);
549 if (!ADS_ERR_OK(status
)) {
553 status
= ads_delete_gpo_link(ads
, mem_ctx
, argv
[0], argv
[1]);
554 if (!ADS_ERR_OK(status
)) {
555 d_printf("delete link failed: %s\n", ads_errstr(status
));
560 talloc_destroy(mem_ctx
);
568 static int net_ads_gpo_get_gpo(struct net_context
*c
, int argc
, const char **argv
)
573 struct GROUP_POLICY_OBJECT gpo
;
575 if (argc
< 1 || c
->display_usage
) {
577 "net ads gpo getgpo <gpo>\n"
578 " List speciefied GPO\n"
579 " gpo\t\tGPO to list\n");
583 mem_ctx
= talloc_init("ads_gpo_get_gpo");
584 if (mem_ctx
== NULL
) {
588 status
= ads_startup(c
, false, &ads
);
589 if (!ADS_ERR_OK(status
)) {
593 if (strnequal(argv
[0], "CN={", strlen("CN={"))) {
594 status
= ads_get_gpo(ads
, mem_ctx
, argv
[0], NULL
, NULL
, &gpo
);
596 status
= ads_get_gpo(ads
, mem_ctx
, NULL
, argv
[0], NULL
, &gpo
);
599 if (!ADS_ERR_OK(status
)) {
600 d_printf("get gpo for [%s] failed: %s\n", argv
[0],
605 dump_gpo(ads
, mem_ctx
, &gpo
, 1);
608 talloc_destroy(mem_ctx
);
614 int net_ads_gpo(struct net_context
*c
, int argc
, const char **argv
)
616 struct functable func
[] = {
622 "Apply GPO to container",
623 "net ads gpo apply\n"
624 " Apply GPO to container"
631 "List specified GPO",
632 "net ads gpo getgpo\n"
633 " List specified GPO"
637 net_ads_gpo_link_add
,
639 "Link a container to a GPO",
640 "net ads gpo linkadd\n"
641 " Link a container to a GPO"
646 net_ads_gpo_link_delete
,
648 "Delete GPO link from a container",
649 "net ads gpo linkdelete\n"
650 " Delete GPO link from a container"
655 net_ads_gpo_link_get
,
657 "Lists gPLink of containter",
658 "net ads gpo linkget\n"
659 " Lists gPLink of containter"
665 "Lists all GPOs for machine/user",
667 " Lists all GPOs for machine/user"
671 net_ads_gpo_list_all
,
673 "Lists all GPOs on a DC",
674 "net ads gpo listall\n"
675 " Lists all GPOs on a DC"
681 "Lists all GPOs assigned to an account and downloads "
683 "net ads gpo refresh\n"
684 " Lists all GPOs assigned to an account and "
687 {NULL
, NULL
, 0, NULL
, NULL
}
690 return net_run_function(c
, argc
, argv
, "net ads gpo", func
);
693 #endif /* HAVE_ADS */