s3:registry: extract the reg_cachehook prototypes into their own header.
[Samba/gebeck_regimport.git] / source3 / utils / net_ads_gpo.c
blobe47efe768ed34c2b4263b82482a70d58c335cfb3
1 /*
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/>.
20 #include "includes.h"
21 #include "utils/net.h"
22 #include "../libgpo/gpo.h"
23 #include "libgpo/gpo_proto.h"
25 #ifdef HAVE_ADS
27 static int net_ads_gpo_refresh(struct net_context *c, int argc, const char **argv)
29 TALLOC_CTX *mem_ctx;
30 ADS_STRUCT *ads;
31 ADS_STATUS status;
32 const char *dn = NULL;
33 struct GROUP_POLICY_OBJECT *gpo_list = NULL;
34 struct GROUP_POLICY_OBJECT *read_list = NULL;
35 uint32 uac = 0;
36 uint32 flags = 0;
37 struct GROUP_POLICY_OBJECT *gpo;
38 NTSTATUS result;
39 struct nt_user_token *token = NULL;
41 if (argc < 1 || c->display_usage) {
42 d_printf("%s\n%s\n%s",
43 _("Usage:"),
44 _("net ads gpo refresh <username|machinename>"),
45 _(" Lists all GPOs assigned to an account and "
46 "downloads them\n"
47 " username\tUser to refresh GPOs for\n"
48 " machinename\tMachine to refresh GPOs for\n"));
49 return -1;
52 mem_ctx = talloc_init("net_ads_gpo_refresh");
53 if (mem_ctx == NULL) {
54 return -1;
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));
60 goto out;
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]);
66 goto out;
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"),
75 argv[0], dn);
77 d_printf(_("* fetching token "));
78 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
79 status = gp_get_machine_token(ads, mem_ctx, NULL, dn, &token);
80 } else {
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));
86 goto out;
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"),
94 ads_errstr(status));
95 goto out;
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),
102 NULL,
103 flags,
104 gpo_list))) {
105 d_printf(_("failed: %s\n"), nt_errstr(result));
106 goto out;
108 d_printf(_("finished\n"));
110 d_printf(_("* storing GPO list to registry "));
113 WERROR werr = gp_reg_state_store(mem_ctx, flags, dn,
114 token, gpo_list);
115 if (!W_ERROR_IS_OK(werr)) {
116 d_printf(_("failed: %s\n"), win_errstr(werr));
117 goto out;
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);
130 #if 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",
136 gpo->version,
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,
142 &unix_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);
150 #endif
154 d_printf(_("* re-reading GPO list from registry "));
157 WERROR werr = gp_reg_state_read(mem_ctx, flags,
158 &token->user_sids[0],
159 &read_list);
160 if (!W_ERROR_IS_OK(werr)) {
161 d_printf(_("failed: %s\n"), win_errstr(werr));
162 goto out;
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);
176 #if 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",
182 gpo->version,
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,
188 &unix_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);
196 #endif
200 out:
201 ads_destroy(&ads);
202 talloc_destroy(mem_ctx);
203 return 0;
206 static int net_ads_gpo_list_all(struct net_context *c, int argc, const char **argv)
208 ADS_STRUCT *ads;
209 ADS_STATUS status;
210 LDAPMessage *res = NULL;
211 int num_reply = 0;
212 LDAPMessage *msg = NULL;
213 struct GROUP_POLICY_OBJECT gpo;
214 TALLOC_CTX *mem_ctx;
215 char *dn;
216 const char *attrs[] = {
217 "versionNumber",
218 "flags",
219 "gPCFileSysPath",
220 "displayName",
221 "name",
222 "gPCMachineExtensionNames",
223 "gPCUserExtensionNames",
224 "ntSecurityDescriptor",
225 NULL
228 if (c->display_usage) {
229 d_printf( "%s\n"
230 "net ads gpo listall\n"
231 " %s\n",
232 _("Usage:"),
233 _("List all GPOs on the DC"));
234 return 0;
237 mem_ctx = talloc_init("net_ads_gpo_list_all");
238 if (mem_ctx == NULL) {
239 return -1;
242 status = ads_startup(c, false, &ads);
243 if (!ADS_ERR_OK(status)) {
244 goto out;
247 status = ads_do_search_all_sd_flags(ads, ads->config.bind_path,
248 LDAP_SCOPE_SUBTREE,
249 "(objectclass=groupPolicyContainer)",
250 attrs,
251 DACL_SECURITY_INFORMATION,
252 &res);
254 if (!ADS_ERR_OK(status)) {
255 d_printf(_("search failed: %s\n"), ads_errstr(status));
256 goto out;
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);
265 msg;
266 msg = ads_next_entry(ads, msg)) {
268 if ((dn = ads_get_dn(ads, mem_ctx, msg)) == NULL) {
269 goto out;
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"),
276 ads_errstr(status));
277 goto out;
280 dump_gpo(ads, mem_ctx, &gpo, 0);
283 out:
284 ads_msgfree(ads, res);
286 TALLOC_FREE(mem_ctx);
287 ads_destroy(&ads);
289 return 0;
292 static int net_ads_gpo_list(struct net_context *c, int argc, const char **argv)
294 ADS_STRUCT *ads;
295 ADS_STATUS status;
296 LDAPMessage *res = NULL;
297 TALLOC_CTX *mem_ctx;
298 const char *dn = NULL;
299 uint32 uac = 0;
300 uint32 flags = 0;
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",
306 _("Usage:"),
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"));
311 return -1;
314 mem_ctx = talloc_init("net_ads_gpo_list");
315 if (mem_ctx == NULL) {
316 goto out;
319 status = ads_startup(c, false, &ads);
320 if (!ADS_ERR_OK(status)) {
321 goto out;
324 status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
325 if (!ADS_ERR_OK(status)) {
326 goto out;
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"),
335 argv[0], dn);
337 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
338 status = gp_get_machine_token(ads, mem_ctx, NULL, dn, &token);
339 } else {
340 status = ads_get_sid_token(ads, mem_ctx, dn, &token);
343 if (!ADS_ERR_OK(status)) {
344 goto out;
347 status = ads_get_gpo_list(ads, mem_ctx, dn, flags, token, &gpo_list);
348 if (!ADS_ERR_OK(status)) {
349 goto out;
352 dump_gpo_list(ads, mem_ctx, gpo_list, 0);
354 out:
355 ads_msgfree(ads, res);
357 talloc_destroy(mem_ctx);
358 ads_destroy(&ads);
360 return 0;
363 static int net_ads_gpo_apply(struct net_context *c, int argc, const char **argv)
365 TALLOC_CTX *mem_ctx;
366 ADS_STRUCT *ads;
367 ADS_STATUS status;
368 const char *dn = NULL;
369 struct GROUP_POLICY_OBJECT *gpo_list;
370 uint32 uac = 0;
371 uint32 flags = 0;
372 struct nt_user_token *token = NULL;
373 const char *filter = NULL;
375 if (argc < 1 || c->display_usage) {
376 d_printf("Usage:\n"
377 "net ads gpo apply <username|machinename>\n"
378 " Apply GPOs for machine/user\n"
379 " username\tUsername to apply GPOs for\n"
380 " machinename\tMachine to apply GPOs for\n");
381 return -1;
384 mem_ctx = talloc_init("net_ads_gpo_apply");
385 if (mem_ctx == NULL) {
386 goto out;
389 if (argc >= 2) {
390 filter = cse_gpo_name_to_guid_string(argv[1]);
393 status = ads_startup(c, false, &ads);
394 /* filter = cse_gpo_name_to_guid_string("Security"); */
396 if (!ADS_ERR_OK(status)) {
397 d_printf("got: %s\n", ads_errstr(status));
398 goto out;
401 status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
402 if (!ADS_ERR_OK(status)) {
403 d_printf("failed to find samaccount for %s: %s\n",
404 argv[0], ads_errstr(status));
405 goto out;
408 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
409 flags |= GPO_LIST_FLAG_MACHINE;
412 if (c->opt_verbose) {
413 flags |= GPO_INFO_FLAG_VERBOSE;
416 d_printf("%s: '%s' has dn: '%s'\n",
417 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? "machine" : "user",
418 argv[0], dn);
420 if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
421 status = gp_get_machine_token(ads, mem_ctx, NULL, dn, &token);
422 } else {
423 status = ads_get_sid_token(ads, mem_ctx, dn, &token);
426 if (!ADS_ERR_OK(status)) {
427 goto out;
430 status = ads_get_gpo_list(ads, mem_ctx, dn, flags, token, &gpo_list);
431 if (!ADS_ERR_OK(status)) {
432 goto out;
435 status = gpo_process_gpo_list(ads, mem_ctx, token, gpo_list,
436 filter, flags);
437 if (!ADS_ERR_OK(status)) {
438 d_printf("failed to process gpo list: %s\n",
439 ads_errstr(status));
440 goto out;
443 out:
444 ads_destroy(&ads);
445 talloc_destroy(mem_ctx);
446 return 0;
449 static int net_ads_gpo_link_get(struct net_context *c, int argc, const char **argv)
451 ADS_STRUCT *ads;
452 ADS_STATUS status;
453 TALLOC_CTX *mem_ctx;
454 struct GP_LINK gp_link;
456 if (argc < 1 || c->display_usage) {
457 d_printf("%s\n%s\n%s",
458 _("Usage:"),
459 _("net ads gpo linkget <container>"),
460 _(" Lists gPLink of a containter\n"
461 " container\tContainer to get link for\n"));
462 return -1;
465 mem_ctx = talloc_init("add_gpo_link");
466 if (mem_ctx == NULL) {
467 return -1;
470 status = ads_startup(c, false, &ads);
471 if (!ADS_ERR_OK(status)) {
472 goto out;
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],
478 ads_errstr(status));
479 goto out;
482 dump_gplink(ads, mem_ctx, &gp_link);
484 out:
485 talloc_destroy(mem_ctx);
486 ads_destroy(&ads);
488 return 0;
491 static int net_ads_gpo_link_add(struct net_context *c, int argc, const char **argv)
493 ADS_STRUCT *ads;
494 ADS_STATUS status;
495 uint32 gpo_opt = 0;
496 TALLOC_CTX *mem_ctx;
498 if (argc < 2 || c->display_usage) {
499 d_printf("%s\n%s\n%s",
500 _("Usage:"),
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"));
507 return -1;
510 mem_ctx = talloc_init("add_gpo_link");
511 if (mem_ctx == NULL) {
512 return -1;
515 if (argc == 3) {
516 gpo_opt = atoi(argv[2]);
519 status = ads_startup(c, false, &ads);
520 if (!ADS_ERR_OK(status)) {
521 goto out;
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));
527 goto out;
530 out:
531 talloc_destroy(mem_ctx);
532 ads_destroy(&ads);
534 return 0;
537 #if 0 /* broken */
539 static int net_ads_gpo_link_delete(struct net_context *c, int argc, const char **argv)
541 ADS_STRUCT *ads;
542 ADS_STATUS status;
543 TALLOC_CTX *mem_ctx;
545 if (argc < 2 || c->display_usage) {
546 d_printf("Usage:\n"
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");
551 return -1;
554 mem_ctx = talloc_init("delete_gpo_link");
555 if (mem_ctx == NULL) {
556 return -1;
559 status = ads_startup(c, false, &ads);
560 if (!ADS_ERR_OK(status)) {
561 goto out;
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));
567 goto out;
570 out:
571 talloc_destroy(mem_ctx);
572 ads_destroy(&ads);
574 return 0;
577 #endif
579 static int net_ads_gpo_get_gpo(struct net_context *c, int argc, const char **argv)
581 ADS_STRUCT *ads;
582 ADS_STATUS status;
583 TALLOC_CTX *mem_ctx;
584 struct GROUP_POLICY_OBJECT gpo;
586 if (argc < 1 || c->display_usage) {
587 d_printf("%s\n%s\n%s",
588 _("Usage:"),
589 _("net ads gpo getgpo <gpo>"),
590 _(" List speciefied GPO\n"
591 " gpo\t\tGPO to list\n"));
592 return -1;
595 mem_ctx = talloc_init("ads_gpo_get_gpo");
596 if (mem_ctx == NULL) {
597 return -1;
600 status = ads_startup(c, false, &ads);
601 if (!ADS_ERR_OK(status)) {
602 goto out;
605 if (strnequal(argv[0], "CN={", strlen("CN={"))) {
606 status = ads_get_gpo(ads, mem_ctx, argv[0], NULL, NULL, &gpo);
607 } else {
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],
613 ads_errstr(status));
614 goto out;
617 dump_gpo(ads, mem_ctx, &gpo, 1);
619 out:
620 talloc_destroy(mem_ctx);
621 ads_destroy(&ads);
623 return 0;
626 int net_ads_gpo(struct net_context *c, int argc, const char **argv)
628 struct functable func[] = {
630 "apply",
631 net_ads_gpo_apply,
632 NET_TRANSPORT_ADS,
633 "Apply GPO to container",
634 "net ads gpo apply\n"
635 " Apply GPO to container"
638 "getgpo",
639 net_ads_gpo_get_gpo,
640 NET_TRANSPORT_ADS,
641 N_("List specified GPO"),
642 N_("net ads gpo getgpo\n"
643 " List specified GPO")
646 "linkadd",
647 net_ads_gpo_link_add,
648 NET_TRANSPORT_ADS,
649 N_("Link a container to a GPO"),
650 N_("net ads gpo linkadd\n"
651 " Link a container to a GPO")
653 #if 0
655 "linkdelete",
656 net_ads_gpo_link_delete,
657 NET_TRANSPORT_ADS,
658 "Delete GPO link from a container",
659 "net ads gpo linkdelete\n"
660 " Delete GPO link from a container"
662 #endif
664 "linkget",
665 net_ads_gpo_link_get,
666 NET_TRANSPORT_ADS,
667 N_("Lists gPLink of containter"),
668 N_("net ads gpo linkget\n"
669 " Lists gPLink of containter")
672 "list",
673 net_ads_gpo_list,
674 NET_TRANSPORT_ADS,
675 N_("Lists all GPOs for machine/user"),
676 N_("net ads gpo list\n"
677 " Lists all GPOs for machine/user")
680 "listall",
681 net_ads_gpo_list_all,
682 NET_TRANSPORT_ADS,
683 N_("Lists all GPOs on a DC"),
684 N_("net ads gpo listall\n"
685 " Lists all GPOs on a DC")
688 "refresh",
689 net_ads_gpo_refresh,
690 NET_TRANSPORT_ADS,
691 N_("Lists all GPOs assigned to an account and "
692 "downloads them"),
693 N_("net ads gpo refresh\n"
694 " Lists all GPOs assigned to an account and "
695 "downloads them")
697 {NULL, NULL, 0, NULL, NULL}
700 return net_run_function(c, argc, argv, "net ads gpo", func);
703 #endif /* HAVE_ADS */