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"
23 #include "../libgpo/gpo.h"
24 #include "libgpo/gpo_proto.h"
25 #include "../libds/common/flags.h"
29 static int net_ads_gpo_refresh(struct net_context
*c
, int argc
, const char **argv
)
34 const char *dn
= NULL
;
35 struct GROUP_POLICY_OBJECT
*gpo_list
= NULL
;
36 struct GROUP_POLICY_OBJECT
*read_list
= NULL
;
39 struct GROUP_POLICY_OBJECT
*gpo
;
41 struct security_token
*token
= NULL
;
43 if (argc
< 1 || c
->display_usage
) {
44 d_printf("%s\n%s\n%s",
46 _("net ads gpo refresh <username|machinename>"),
47 _(" Lists all GPOs assigned to an account and "
49 " username\tUser to refresh GPOs for\n"
50 " machinename\tMachine to refresh GPOs for\n"));
54 mem_ctx
= talloc_init("net_ads_gpo_refresh");
55 if (mem_ctx
== NULL
) {
59 status
= ads_startup(c
, false, &ads
);
60 if (!ADS_ERR_OK(status
)) {
61 d_printf(_("failed to connect AD server: %s\n"), ads_errstr(status
));
65 status
= ads_find_samaccount(ads
, mem_ctx
, argv
[0], &uac
, &dn
);
66 if (!ADS_ERR_OK(status
)) {
67 d_printf(_("failed to find samaccount for %s\n"), argv
[0]);
71 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
72 flags
|= GPO_LIST_FLAG_MACHINE
;
75 d_printf(_("\n%s: '%s' has dn: '%s'\n\n"),
76 (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) ? _("machine") : _("user"),
79 d_printf(_("* fetching token "));
80 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
81 status
= gp_get_machine_token(ads
, mem_ctx
, dn
, &token
);
83 status
= ads_get_sid_token(ads
, mem_ctx
, dn
, &token
);
86 if (!ADS_ERR_OK(status
)) {
87 d_printf(_("failed: %s\n"), ads_errstr(status
));
90 d_printf(_("finished\n"));
92 d_printf(_("* fetching GPO List "));
93 status
= ads_get_gpo_list(ads
, mem_ctx
, dn
, flags
, token
, &gpo_list
);
94 if (!ADS_ERR_OK(status
)) {
95 d_printf(_("failed: %s\n"),
99 d_printf(_("finished\n"));
101 d_printf(_("* Refreshing Group Policy Data "));
102 if (!NT_STATUS_IS_OK(result
= check_refresh_gpo_list(ads
, mem_ctx
,
103 cache_path(GPO_CACHE_DIR
),
106 d_printf(_("failed: %s\n"), nt_errstr(result
));
109 d_printf(_("finished\n"));
111 d_printf(_("* storing GPO list to registry "));
114 WERROR werr
= gp_reg_state_store(mem_ctx
, flags
, dn
,
116 if (!W_ERROR_IS_OK(werr
)) {
117 d_printf(_("failed: %s\n"), win_errstr(werr
));
122 d_printf(_("finished\n"));
124 if (c
->opt_verbose
) {
126 d_printf(_("* dumping GPO list\n"));
128 for (gpo
= gpo_list
; gpo
; gpo
= gpo
->next
) {
132 char *server
, *share
, *nt_path
, *unix_path
;
134 d_printf("--------------------------------------\n");
135 d_printf("Name:\t\t\t%s\n", gpo
->display_name
);
136 d_printf("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
138 GPO_VERSION_USER(gpo
->version
),
139 GPO_VERSION_MACHINE(gpo
->version
));
141 result
= gpo_explode_filesyspath(mem_ctx
, gpo
->file_sys_path
,
142 &server
, &share
, &nt_path
,
144 if (!NT_STATUS_IS_OK(result
)) {
145 d_printf("got: %s\n", nt_errstr(result
));
148 d_printf("GPO stored on server: %s, share: %s\n", server
, share
);
149 d_printf("\tremote path:\t%s\n", nt_path
);
150 d_printf("\tlocal path:\t%s\n", unix_path
);
155 d_printf(_("* re-reading GPO list from registry "));
158 WERROR werr
= gp_reg_state_read(mem_ctx
, flags
,
161 if (!W_ERROR_IS_OK(werr
)) {
162 d_printf(_("failed: %s\n"), win_errstr(werr
));
167 d_printf(_("finished\n"));
169 if (c
->opt_verbose
) {
171 d_printf(_("* dumping GPO list from registry\n"));
173 for (gpo
= read_list
; gpo
; gpo
= gpo
->next
) {
178 char *server
, *share
, *nt_path
, *unix_path
;
180 d_printf("--------------------------------------\n");
181 d_printf("Name:\t\t\t%s\n", gpo
->display_name
);
182 d_printf("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
184 GPO_VERSION_USER(gpo
->version
),
185 GPO_VERSION_MACHINE(gpo
->version
));
187 result
= gpo_explode_filesyspath(mem_ctx
, gpo
->file_sys_path
,
188 &server
, &share
, &nt_path
,
190 if (!NT_STATUS_IS_OK(result
)) {
191 d_printf("got: %s\n", nt_errstr(result
));
194 d_printf("GPO stored on server: %s, share: %s\n", server
, share
);
195 d_printf("\tremote path:\t%s\n", nt_path
);
196 d_printf("\tlocal path:\t%s\n", unix_path
);
203 talloc_destroy(mem_ctx
);
207 static int net_ads_gpo_list_all(struct net_context
*c
, int argc
, const char **argv
)
211 LDAPMessage
*res
= NULL
;
213 LDAPMessage
*msg
= NULL
;
214 struct GROUP_POLICY_OBJECT gpo
;
217 const char *attrs
[] = {
223 "gPCMachineExtensionNames",
224 "gPCUserExtensionNames",
225 "ntSecurityDescriptor",
229 if (c
->display_usage
) {
231 "net ads gpo listall\n"
234 _("List all GPOs on the DC"));
238 mem_ctx
= talloc_init("net_ads_gpo_list_all");
239 if (mem_ctx
== NULL
) {
243 status
= ads_startup(c
, false, &ads
);
244 if (!ADS_ERR_OK(status
)) {
248 status
= ads_do_search_all_sd_flags(ads
, ads
->config
.bind_path
,
250 "(objectclass=groupPolicyContainer)",
255 if (!ADS_ERR_OK(status
)) {
256 d_printf(_("search failed: %s\n"), ads_errstr(status
));
260 num_reply
= ads_count_replies(ads
, res
);
262 d_printf(_("Got %d replies\n\n"), num_reply
);
264 /* dump the results */
265 for (msg
= ads_first_entry(ads
, res
);
267 msg
= ads_next_entry(ads
, msg
)) {
269 if ((dn
= ads_get_dn(ads
, mem_ctx
, msg
)) == NULL
) {
273 status
= ads_parse_gpo(ads
, mem_ctx
, msg
, dn
, &gpo
);
275 if (!ADS_ERR_OK(status
)) {
276 d_printf(_("ads_parse_gpo failed: %s\n"),
285 ads_msgfree(ads
, res
);
287 TALLOC_FREE(mem_ctx
);
293 static int net_ads_gpo_list(struct net_context
*c
, int argc
, const char **argv
)
295 ADS_STRUCT
*ads
= NULL
;
297 LDAPMessage
*res
= NULL
;
299 const char *dn
= NULL
;
302 struct GROUP_POLICY_OBJECT
*gpo_list
;
303 struct security_token
*token
= NULL
;
305 if (argc
< 1 || c
->display_usage
) {
306 d_printf("%s\n%s\n%s",
308 _("net ads gpo list <username|machinename>"),
309 _(" Lists all GPOs for machine/user\n"
310 " username\tUser to list GPOs for\n"
311 " machinename\tMachine to list GPOs for\n"));
315 mem_ctx
= talloc_init("net_ads_gpo_list");
316 if (mem_ctx
== NULL
) {
320 status
= ads_startup(c
, false, &ads
);
321 if (!ADS_ERR_OK(status
)) {
325 status
= ads_find_samaccount(ads
, mem_ctx
, argv
[0], &uac
, &dn
);
326 if (!ADS_ERR_OK(status
)) {
330 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
331 flags
|= GPO_LIST_FLAG_MACHINE
;
334 d_printf(_("%s: '%s' has dn: '%s'\n"),
335 (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) ? _("machine") : _("user"),
338 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
339 status
= gp_get_machine_token(ads
, mem_ctx
, dn
, &token
);
341 status
= ads_get_sid_token(ads
, mem_ctx
, dn
, &token
);
344 if (!ADS_ERR_OK(status
)) {
348 status
= ads_get_gpo_list(ads
, mem_ctx
, dn
, flags
, token
, &gpo_list
);
349 if (!ADS_ERR_OK(status
)) {
353 dump_gpo_list(gpo_list
, 0);
356 ads_msgfree(ads
, res
);
358 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 security_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 /* filter = cse_gpo_name_to_guid_string("Security"); */
397 if (!ADS_ERR_OK(status
)) {
398 d_printf("got: %s\n", ads_errstr(status
));
402 status
= ads_find_samaccount(ads
, mem_ctx
, argv
[0], &uac
, &dn
);
403 if (!ADS_ERR_OK(status
)) {
404 d_printf("failed to find samaccount for %s: %s\n",
405 argv
[0], ads_errstr(status
));
409 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
410 flags
|= GPO_LIST_FLAG_MACHINE
;
413 if (c
->opt_verbose
) {
414 flags
|= GPO_INFO_FLAG_VERBOSE
;
417 d_printf("%s: '%s' has dn: '%s'\n",
418 (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) ? "machine" : "user",
421 if (uac
& UF_WORKSTATION_TRUST_ACCOUNT
) {
422 status
= gp_get_machine_token(ads
, mem_ctx
, dn
, &token
);
424 status
= ads_get_sid_token(ads
, mem_ctx
, dn
, &token
);
427 if (!ADS_ERR_OK(status
)) {
431 status
= ads_get_gpo_list(ads
, mem_ctx
, dn
, flags
, token
, &gpo_list
);
432 if (!ADS_ERR_OK(status
)) {
436 status
= ADS_ERROR_NT(gpo_process_gpo_list(mem_ctx
, token
, NULL
, gpo_list
,
438 if (!ADS_ERR_OK(status
)) {
439 d_printf("failed to process gpo list: %s\n",
446 talloc_destroy(mem_ctx
);
450 static int net_ads_gpo_link_get(struct net_context
*c
, int argc
, const char **argv
)
455 struct GP_LINK gp_link
;
457 if (argc
< 1 || c
->display_usage
) {
458 d_printf("%s\n%s\n%s",
460 _("net ads gpo linkget <container>"),
461 _(" Lists gPLink of a containter\n"
462 " container\tContainer to get link for\n"));
466 mem_ctx
= talloc_init("add_gpo_link");
467 if (mem_ctx
== NULL
) {
471 status
= ads_startup(c
, false, &ads
);
472 if (!ADS_ERR_OK(status
)) {
476 status
= ads_get_gpo_link(ads
, mem_ctx
, argv
[0], &gp_link
);
477 if (!ADS_ERR_OK(status
)) {
478 d_printf(_("get link for %s failed: %s\n"), argv
[0],
483 dump_gplink(&gp_link
);
486 talloc_destroy(mem_ctx
);
492 static int net_ads_gpo_link_add(struct net_context
*c
, int argc
, const char **argv
)
499 if (argc
< 2 || c
->display_usage
) {
500 d_printf("%s\n%s\n%s",
502 _("net ads gpo linkadd <linkdn> <gpodn> [options]"),
503 _(" Link a container to a GPO\n"
504 " linkdn\tContainer to link to a GPO\n"
505 " gpodn\tGPO to link container to\n"));
506 d_printf(_("note: DNs must be provided properly escaped.\n"
507 "See RFC 4514 for details\n"));
511 mem_ctx
= talloc_init("add_gpo_link");
512 if (mem_ctx
== NULL
) {
517 gpo_opt
= atoi(argv
[2]);
520 status
= ads_startup(c
, false, &ads
);
521 if (!ADS_ERR_OK(status
)) {
525 status
= ads_add_gpo_link(ads
, mem_ctx
, argv
[0], argv
[1], gpo_opt
);
526 if (!ADS_ERR_OK(status
)) {
527 d_printf(_("link add failed: %s\n"), ads_errstr(status
));
532 talloc_destroy(mem_ctx
);
540 static int net_ads_gpo_link_delete(struct net_context
*c
, int argc
, const char **argv
)
546 if (argc
< 2 || c
->display_usage
) {
548 "net ads gpo linkdelete <linkdn> <gpodn>\n"
549 " Delete a GPO link\n"
550 " <linkdn>\tContainer to delete GPO from\n"
551 " <gpodn>\tGPO to delete from container\n");
555 mem_ctx
= talloc_init("delete_gpo_link");
556 if (mem_ctx
== NULL
) {
560 status
= ads_startup(c
, false, &ads
);
561 if (!ADS_ERR_OK(status
)) {
565 status
= ads_delete_gpo_link(ads
, mem_ctx
, argv
[0], argv
[1]);
566 if (!ADS_ERR_OK(status
)) {
567 d_printf("delete link failed: %s\n", ads_errstr(status
));
572 talloc_destroy(mem_ctx
);
580 static int net_ads_gpo_get_gpo(struct net_context
*c
, int argc
, const char **argv
)
585 struct GROUP_POLICY_OBJECT gpo
;
587 if (argc
< 1 || c
->display_usage
) {
588 d_printf("%s\n%s\n%s",
590 _("net ads gpo getgpo <gpo>"),
591 _(" List speciefied GPO\n"
592 " gpo\t\tGPO to list\n"));
596 mem_ctx
= talloc_init("ads_gpo_get_gpo");
597 if (mem_ctx
== NULL
) {
601 status
= ads_startup(c
, false, &ads
);
602 if (!ADS_ERR_OK(status
)) {
606 if (strnequal(argv
[0], "CN={", strlen("CN={"))) {
607 status
= ads_get_gpo(ads
, mem_ctx
, argv
[0], NULL
, NULL
, &gpo
);
609 status
= ads_get_gpo(ads
, mem_ctx
, NULL
, argv
[0], NULL
, &gpo
);
612 if (!ADS_ERR_OK(status
)) {
613 d_printf(_("get gpo for [%s] failed: %s\n"), argv
[0],
621 talloc_destroy(mem_ctx
);
627 int net_ads_gpo(struct net_context
*c
, int argc
, const char **argv
)
629 struct functable func
[] = {
634 "Apply GPO to container",
635 "net ads gpo apply\n"
636 " Apply GPO to container"
642 N_("List specified GPO"),
643 N_("net ads gpo getgpo\n"
644 " List specified GPO")
648 net_ads_gpo_link_add
,
650 N_("Link a container to a GPO"),
651 N_("net ads gpo linkadd\n"
652 " Link a container to a GPO")
657 net_ads_gpo_link_delete
,
659 "Delete GPO link from a container",
660 "net ads gpo linkdelete\n"
661 " Delete GPO link from a container"
666 net_ads_gpo_link_get
,
668 N_("Lists gPLink of containter"),
669 N_("net ads gpo linkget\n"
670 " Lists gPLink of containter")
676 N_("Lists all GPOs for machine/user"),
677 N_("net ads gpo list\n"
678 " Lists all GPOs for machine/user")
682 net_ads_gpo_list_all
,
684 N_("Lists all GPOs on a DC"),
685 N_("net ads gpo listall\n"
686 " Lists all GPOs on a DC")
692 N_("Lists all GPOs assigned to an account and "
694 N_("net ads gpo refresh\n"
695 " Lists all GPOs assigned to an account and "
698 {NULL
, NULL
, 0, NULL
, NULL
}
701 return net_run_function(c
, argc
, argv
, "net ads gpo", func
);
704 #endif /* HAVE_ADS */