s3-libgpo: move group policy protos to where they belong.
[ira/wip.git] / source3 / utils / net_ads_gpo.c
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)
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "utils/net.h"
22 #include "../libgpo/gpo.h"
23 #include "libgpo/gpo_proto.h"
24
25 #ifdef HAVE_ADS
26
27 static int net_ads_gpo_refresh(struct net_context *c, int argc, const char **argv)
28 {
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;
40
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;
50         }
51
52         mem_ctx = talloc_init("net_ads_gpo_refresh");
53         if (mem_ctx == NULL) {
54                 return -1;
55         }
56
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;
61         }
62
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;
67         }
68
69         if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
70                 flags |= GPO_LIST_FLAG_MACHINE;
71         }
72
73         d_printf(_("\n%s: '%s' has dn: '%s'\n\n"),
74                 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? _("machine") : _("user"),
75                 argv[0], dn);
76
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);
82         }
83
84         if (!ADS_ERR_OK(status)) {
85                 d_printf(_("failed: %s\n"), ads_errstr(status));
86                 goto out;
87         }
88         d_printf(_("finished\n"));
89
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;
96         }
97         d_printf(_("finished\n"));
98
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;
107         }
108         d_printf(_("finished\n"));
109
110         d_printf(_("* storing GPO list to registry "));
111
112         {
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;
118                 }
119         }
120
121         d_printf(_("finished\n"));
122
123         if (c->opt_verbose) {
124
125                 d_printf(_("* dumping GPO list\n"));
126
127                 for (gpo = gpo_list; gpo; gpo = gpo->next) {
128
129                         dump_gpo(ads, mem_ctx, gpo, 0);
130 #if 0
131                 char *server, *share, *nt_path, *unix_path;
132
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));
139
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));
145                 }
146
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
151                 }
152         }
153
154         d_printf(_("* re-reading GPO list from registry "));
155
156         {
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;
163                 }
164         }
165
166         d_printf(_("finished\n"));
167
168         if (c->opt_verbose) {
169
170                 d_printf(_("* dumping GPO list from registry\n"));
171
172                 for (gpo = read_list; gpo; gpo = gpo->next) {
173
174                         dump_gpo(ads, mem_ctx, gpo, 0);
175
176 #if 0
177                 char *server, *share, *nt_path, *unix_path;
178
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));
185
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));
191                 }
192
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
197                 }
198         }
199
200  out:
201         ads_destroy(&ads);
202         talloc_destroy(mem_ctx);
203         return 0;
204 }
205
206 static int net_ads_gpo_list_all(struct net_context *c, int argc, const char **argv)
207 {
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
226         };
227
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;
235         }
236
237         mem_ctx = talloc_init("net_ads_gpo_list_all");
238         if (mem_ctx == NULL) {
239                 return -1;
240         }
241
242         status = ads_startup(c, false, &ads);
243         if (!ADS_ERR_OK(status)) {
244                 goto out;
245         }
246
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);
253
254         if (!ADS_ERR_OK(status)) {
255                 d_printf(_("search failed: %s\n"), ads_errstr(status));
256                 goto out;
257         }
258
259         num_reply = ads_count_replies(ads, res);
260
261         d_printf(_("Got %d replies\n\n"), num_reply);
262
263         /* dump the results */
264         for (msg = ads_first_entry(ads, res);
265              msg;
266              msg = ads_next_entry(ads, msg)) {
267
268                 if ((dn = ads_get_dn(ads, mem_ctx, msg)) == NULL) {
269                         goto out;
270                 }
271
272                 status = ads_parse_gpo(ads, mem_ctx, msg, dn, &gpo);
273
274                 if (!ADS_ERR_OK(status)) {
275                         d_printf(_("ads_parse_gpo failed: %s\n"),
276                                 ads_errstr(status));
277                         goto out;
278                 }
279
280                 dump_gpo(ads, mem_ctx, &gpo, 0);
281         }
282
283 out:
284         ads_msgfree(ads, res);
285
286         TALLOC_FREE(mem_ctx);
287         ads_destroy(&ads);
288
289         return 0;
290 }
291
292 static int net_ads_gpo_list(struct net_context *c, int argc, const char **argv)
293 {
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;
303
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;
312         }
313
314         mem_ctx = talloc_init("net_ads_gpo_list");
315         if (mem_ctx == NULL) {
316                 goto out;
317         }
318
319         status = ads_startup(c, false, &ads);
320         if (!ADS_ERR_OK(status)) {
321                 goto out;
322         }
323
324         status = ads_find_samaccount(ads, mem_ctx, argv[0], &uac, &dn);
325         if (!ADS_ERR_OK(status)) {
326                 goto out;
327         }
328
329         if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
330                 flags |= GPO_LIST_FLAG_MACHINE;
331         }
332
333         d_printf(_("%s: '%s' has dn: '%s'\n"),
334                 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? _("machine") : _("user"),
335                 argv[0], dn);
336
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);
341         }
342
343         if (!ADS_ERR_OK(status)) {
344                 goto out;
345         }
346
347         status = ads_get_gpo_list(ads, mem_ctx, dn, flags, token, &gpo_list);
348         if (!ADS_ERR_OK(status)) {
349                 goto out;
350         }
351
352         dump_gpo_list(ads, mem_ctx, gpo_list, 0);
353
354 out:
355         ads_msgfree(ads, res);
356
357         talloc_destroy(mem_ctx);
358         ads_destroy(&ads);
359
360         return 0;
361 }
362
363 #if 0
364 static int net_ads_gpo_apply(struct net_context *c, int argc, const char **argv)
365 {
366         TALLOC_CTX *mem_ctx;
367         ADS_STRUCT *ads;
368         ADS_STATUS status;
369         const char *dn = NULL;
370         struct GROUP_POLICY_OBJECT *gpo_list;
371         uint32 uac = 0;
372         uint32 flags = 0;
373         struct nt_user_token *token = NULL;
374         const char *filter = NULL;
375
376         if (argc < 1 || c->display_usage) {
377                 d_printf("Usage:\n"
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");
382                 return -1;
383         }
384
385         mem_ctx = talloc_init("net_ads_gpo_apply");
386         if (mem_ctx == NULL) {
387                 goto out;
388         }
389
390         if (argc >= 2) {
391                 filter = cse_gpo_name_to_guid_string(argv[1]);
392         }
393
394         status = ads_startup(c, false, &ads);
395         if (!ADS_ERR_OK(status)) {
396                 d_printf("got: %s\n", ads_errstr(status));
397                 goto out;
398         }
399
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));
404                 goto out;
405         }
406
407         if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
408                 flags |= GPO_LIST_FLAG_MACHINE;
409         }
410
411         if (c->opt_verbose) {
412                 flags |= GPO_INFO_FLAG_VERBOSE;
413         }
414
415         d_printf("%s: '%s' has dn: '%s'\n",
416                 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? "machine" : "user",
417                 argv[0], dn);
418
419         if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
420                 status = gp_get_machine_token(ads, mem_ctx, NULL, dn, &token);
421         } else {
422                 status = ads_get_sid_token(ads, mem_ctx, dn, &token);
423         }
424
425         if (!ADS_ERR_OK(status)) {
426                 goto out;
427         }
428
429         status = ads_get_gpo_list(ads, mem_ctx, dn, flags, token, &gpo_list);
430         if (!ADS_ERR_OK(status)) {
431                 goto out;
432         }
433
434         status = gpo_process_gpo_list(ads, mem_ctx, token, gpo_list,
435                                       filter, flags);
436         if (!ADS_ERR_OK(status)) {
437                 d_printf("failed to process gpo list: %s\n",
438                         ads_errstr(status));
439                 goto out;
440         }
441
442 out:
443         ads_destroy(&ads);
444         talloc_destroy(mem_ctx);
445         return 0;
446 }
447 #endif
448
449 static int net_ads_gpo_link_get(struct net_context *c, int argc, const char **argv)
450 {
451         ADS_STRUCT *ads;
452         ADS_STATUS status;
453         TALLOC_CTX *mem_ctx;
454         struct GP_LINK gp_link;
455
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;
463         }
464
465         mem_ctx = talloc_init("add_gpo_link");
466         if (mem_ctx == NULL) {
467                 return -1;
468         }
469
470         status = ads_startup(c, false, &ads);
471         if (!ADS_ERR_OK(status)) {
472                 goto out;
473         }
474
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;
480         }
481
482         dump_gplink(ads, mem_ctx, &gp_link);
483
484 out:
485         talloc_destroy(mem_ctx);
486         ads_destroy(&ads);
487
488         return 0;
489 }
490
491 static int net_ads_gpo_link_add(struct net_context *c, int argc, const char **argv)
492 {
493         ADS_STRUCT *ads;
494         ADS_STATUS status;
495         uint32 gpo_opt = 0;
496         TALLOC_CTX *mem_ctx;
497
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;
508         }
509
510         mem_ctx = talloc_init("add_gpo_link");
511         if (mem_ctx == NULL) {
512                 return -1;
513         }
514
515         if (argc == 3) {
516                 gpo_opt = atoi(argv[2]);
517         }
518
519         status = ads_startup(c, false, &ads);
520         if (!ADS_ERR_OK(status)) {
521                 goto out;
522         }
523
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;
528         }
529
530 out:
531         talloc_destroy(mem_ctx);
532         ads_destroy(&ads);
533
534         return 0;
535 }
536
537 #if 0 /* broken */
538
539 static int net_ads_gpo_link_delete(struct net_context *c, int argc, const char **argv)
540 {
541         ADS_STRUCT *ads;
542         ADS_STATUS status;
543         TALLOC_CTX *mem_ctx;
544
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;
552         }
553
554         mem_ctx = talloc_init("delete_gpo_link");
555         if (mem_ctx == NULL) {
556                 return -1;
557         }
558
559         status = ads_startup(c, false, &ads);
560         if (!ADS_ERR_OK(status)) {
561                 goto out;
562         }
563
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;
568         }
569
570 out:
571         talloc_destroy(mem_ctx);
572         ads_destroy(&ads);
573
574         return 0;
575 }
576
577 #endif
578
579 static int net_ads_gpo_get_gpo(struct net_context *c, int argc, const char **argv)
580 {
581         ADS_STRUCT *ads;
582         ADS_STATUS status;
583         TALLOC_CTX *mem_ctx;
584         struct GROUP_POLICY_OBJECT gpo;
585
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;
593         }
594
595         mem_ctx = talloc_init("ads_gpo_get_gpo");
596         if (mem_ctx == NULL) {
597                 return -1;
598         }
599
600         status = ads_startup(c, false, &ads);
601         if (!ADS_ERR_OK(status)) {
602                 goto out;
603         }
604
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);
609         }
610
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;
615         }
616
617         dump_gpo(ads, mem_ctx, &gpo, 1);
618
619 out:
620         talloc_destroy(mem_ctx);
621         ads_destroy(&ads);
622
623         return 0;
624 }
625
626 int net_ads_gpo(struct net_context *c, int argc, const char **argv)
627 {
628         struct functable func[] = {
629 #if 0
630                 {
631                         "apply",
632                         net_ads_gpo_apply,
633                         NET_TRANSPORT_ADS,
634                         "Apply GPO to container",
635                         "net ads gpo apply\n"
636                         "    Apply GPO to container"
637                 },
638 #endif
639                 {
640                         "getgpo",
641                         net_ads_gpo_get_gpo,
642                         NET_TRANSPORT_ADS,
643                         N_("List specified GPO"),
644                         N_("net ads gpo getgpo\n"
645                            "    List specified GPO")
646                 },
647                 {
648                         "linkadd",
649                         net_ads_gpo_link_add,
650                         NET_TRANSPORT_ADS,
651                         N_("Link a container to a GPO"),
652                         N_("net ads gpo linkadd\n"
653                            "    Link a container to a GPO")
654                 },
655 #if 0
656                 {
657                         "linkdelete",
658                         net_ads_gpo_link_delete,
659                         NET_TRANSPORT_ADS,
660                         "Delete GPO link from a container",
661                         "net ads gpo linkdelete\n"
662                         "    Delete GPO link from a container"
663                 },
664 #endif
665                 {
666                         "linkget",
667                         net_ads_gpo_link_get,
668                         NET_TRANSPORT_ADS,
669                         N_("Lists gPLink of containter"),
670                         N_("net ads gpo linkget\n"
671                            "    Lists gPLink of containter")
672                 },
673                 {
674                         "list",
675                         net_ads_gpo_list,
676                         NET_TRANSPORT_ADS,
677                         N_("Lists all GPOs for machine/user"),
678                         N_("net ads gpo list\n"
679                            "    Lists all GPOs for machine/user")
680                 },
681                 {
682                         "listall",
683                         net_ads_gpo_list_all,
684                         NET_TRANSPORT_ADS,
685                         N_("Lists all GPOs on a DC"),
686                         N_("net ads gpo listall\n"
687                            "    Lists all GPOs on a DC")
688                 },
689                 {
690                         "refresh",
691                         net_ads_gpo_refresh,
692                         NET_TRANSPORT_ADS,
693                         N_("Lists all GPOs assigned to an account and "
694                            "downloads them"),
695                         N_("net ads gpo refresh\n"
696                            "    Lists all GPOs assigned to an account and "
697                            "downloads them")
698                 },
699                 {NULL, NULL, 0, NULL, NULL}
700         };
701
702         return net_run_function(c, argc, argv, "net ads gpo", func);
703 }
704
705 #endif /* HAVE_ADS */