r18987: Add some usage prints for "net ads gpo".
[samba.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 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
19 */
20
21 #include "includes.h"
22 #include "utils/net.h"
23
24 #ifdef HAVE_ADS
25
26 static int net_ads_gpo_usage(int argc, const char **argv)
27 {
28         d_printf(
29                 "net ads gpo <COMMAND>\n"\
30 "<COMMAND> can be either:\n"\
31 "  ADDLINK      Link a container to a GPO\n"\
32 "  APPLY        Apply all GPOs\n"\
33 "  DELETELINK   Delete a gPLink from a container\n"\
34 "  EFFECTIVE    Lists all GPOs assigned to a machine\n"\
35 "  GETGPO       Lists specified GPO\n"\
36 "  GETLINK      Lists gPLink of a containter\n"\
37 "  HELP         Prints this help message\n"\
38 "  LIST         Lists all GPOs\n"\
39 "\n"
40                 );
41         return -1;
42 }
43
44 static int net_ads_gpo_effective(int argc, const char **argv)
45 {
46         TALLOC_CTX *mem_ctx;
47         ADS_STRUCT *ads;
48         ADS_STATUS status;
49         const char *attrs[] = { "userAccountControl", NULL };
50         LDAPMessage *res = NULL;
51         const char *filter;
52         char *dn = NULL;
53         struct GROUP_POLICY_OBJECT *gpo_list;
54         uint32 uac = 0;
55         uint32 flags = 0;
56         struct GROUP_POLICY_OBJECT *gpo;
57         NTSTATUS result;
58         
59         if (argc < 1) {
60                 printf("usage: net ads gpo effective <username|machinename>\n");
61                 return -1;
62         }
63
64         mem_ctx = talloc_init("net_ads_gpo_effective");
65         if (mem_ctx == NULL) {
66                 return -1;
67         }
68
69         filter = talloc_asprintf(mem_ctx, "(&(objectclass=user)(sAMAccountName=%s))", argv[0]);
70         if (filter == NULL) {
71                 goto out;
72         }
73
74         status = ads_startup(False, &ads);
75         if (!ADS_ERR_OK(status)) {
76                 goto out;
77         }
78
79         status = ads_do_search_all(ads, ads->config.bind_path,
80                                    LDAP_SCOPE_SUBTREE,
81                                    filter, attrs, &res);
82         
83         if (!ADS_ERR_OK(status)) {
84                 goto out;
85         }
86
87         if (ads_count_replies(ads, res) != 1) {
88                 printf("no result\n");
89                 goto out;
90         }
91
92         dn = ads_get_dn(ads, res);
93         if (dn == NULL) {
94                 goto out;
95         }
96
97         if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
98                 goto out;
99         }
100
101         if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
102                 flags |= GPO_LIST_FLAG_MACHINE;
103         }
104
105         printf("\n%s: '%s' has dn: '%s'\n\n", 
106                 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? "machine" : "user", 
107                 argv[0], dn);
108
109         status = ads_get_gpo_list(ads, mem_ctx, dn, flags, &gpo_list);
110         if (!ADS_ERR_OK(status)) {
111                 goto out;
112         }
113
114         for (gpo = gpo_list; gpo; gpo = gpo->next) {
115
116                 char *server, *share, *nt_path, *unix_path;
117
118                 printf("--------------------------------------\n");
119                 printf("Name:\t\t\t%s\n", gpo->display_name);
120                 printf("LDAP GPO version:\t%d (user: %d, machine: %d)\n",
121                         gpo->version,
122                         GPO_VERSION_USER(gpo->version),
123                         GPO_VERSION_MACHINE(gpo->version));
124
125                 result = ads_gpo_explode_filesyspath(ads, mem_ctx, gpo->file_sys_path,
126                                                      &server, &share, &nt_path, &unix_path);
127                 if (!NT_STATUS_IS_OK(result)) {
128                         printf("got: %s\n", nt_errstr(result));
129                 }
130
131                 printf("GPO stored on server: %s, share: %s\n", server, share);
132                 printf("\tremote path:\t%s\n", nt_path);
133                 printf("\tlocal path:\t%s\n", unix_path);
134         }
135
136  out:
137         ads_memfree(ads, dn);
138         ads_msgfree(ads, res);
139
140         ads_destroy(&ads);
141         talloc_destroy(mem_ctx);
142         return 0;
143 }
144
145 static int net_ads_gpo_list(int argc, const char **argv)
146 {
147         ADS_STRUCT *ads;
148         ADS_STATUS status;
149         LDAPMessage *res = NULL;
150         int num_reply = 0;
151         LDAPMessage *msg = NULL;
152         struct GROUP_POLICY_OBJECT gpo;
153         TALLOC_CTX *mem_ctx;
154         char *dn;
155         const char *attrs[] = {
156                 "versionNumber",
157                 "flags",
158                 "gPCFileSysPath",
159                 "displayName",
160                 "name",
161                 "gPCMachineExtensionNames",
162                 "gPCUserExtensionNames",
163                 NULL
164         };
165
166         mem_ctx = talloc_init("net_ads_gpo_list");
167         if (mem_ctx == NULL) {
168                 return -1;
169         }
170
171         status = ads_startup(False, &ads);
172         if (!ADS_ERR_OK(status)) {
173                 goto out;
174         }
175
176         status = ads_do_search_all(ads, ads->config.bind_path,
177                                    LDAP_SCOPE_SUBTREE,
178                                    "(objectclass=groupPolicyContainer)", attrs, &res);
179         if (!ADS_ERR_OK(status)) {
180                 d_printf("search failed: %s\n", ads_errstr(status));
181                 goto out;
182         }       
183
184         num_reply = ads_count_replies(ads, res);
185         
186         d_printf("Got %d replies\n\n", num_reply);
187
188         /* dump the results */
189         for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
190
191                 if ((dn = ads_get_dn(ads, msg)) == NULL) {
192                         goto out;
193                 }
194
195                 status = ads_parse_gpo(ads, mem_ctx, msg, dn, &gpo);
196
197                 if (!ADS_ERR_OK(status)) {
198                         d_printf("parse failed: %s\n", ads_errstr(status));
199                         ads_memfree(ads, dn);
200                         goto out;
201                 }       
202
203                 dump_gpo(mem_ctx, &gpo);
204                 ads_memfree(ads, dn);
205         }
206
207 out:
208         ads_msgfree(ads, res);
209
210         talloc_destroy(mem_ctx);
211         ads_destroy(&ads);
212         
213         return 0;
214 }
215
216 static int net_ads_gpo_apply(int argc, const char **argv)
217 {
218         TALLOC_CTX *mem_ctx;
219         ADS_STRUCT *ads;
220         ADS_STATUS status;
221         const char *attrs[] = {"distinguishedName", "userAccountControl", NULL};
222         LDAPMessage *res = NULL;
223         const char *filter;
224         char *dn = NULL;
225         struct GROUP_POLICY_OBJECT *gpo_list;
226         uint32 uac = 0;
227         uint32 flags = 0;
228         
229         if (argc < 1) {
230                 printf("usage: net ads gpo apply <username|machinename>\n");
231                 return -1;
232         }
233
234         mem_ctx = talloc_init("net_ads_gpo_apply");
235         if (mem_ctx == NULL) {
236                 goto out;
237         }
238
239         filter = talloc_asprintf(mem_ctx, "(&(objectclass=user)(sAMAccountName=%s))", argv[0]);
240         if (filter == NULL) {
241                 goto out;
242         }
243
244         status = ads_startup(False, &ads);
245         if (!ADS_ERR_OK(status)) {
246                 goto out;
247         }
248
249         status = ads_do_search_all(ads, ads->config.bind_path,
250                                    LDAP_SCOPE_SUBTREE,
251                                    filter, attrs, &res);
252         
253         if (!ADS_ERR_OK(status)) {
254                 goto out;
255         }
256
257         if (ads_count_replies(ads, res) != 1) {
258                 printf("no result\n");
259                 goto out;
260         }
261
262         dn = ads_get_dn(ads, res);
263         if (dn == NULL) {
264                 goto out;
265         }
266
267         if (!ads_pull_uint32(ads, res, "userAccountControl", &uac)) {
268                 goto out;
269         }
270
271         if (uac & UF_WORKSTATION_TRUST_ACCOUNT) {
272                 flags |= GPO_LIST_FLAG_MACHINE;
273         }
274
275         printf("%s: '%s' has dn: '%s'\n", 
276                 (uac & UF_WORKSTATION_TRUST_ACCOUNT) ? "machine" : "user", 
277                 argv[0], dn);
278
279         status = ads_get_gpo_list(ads, mem_ctx, dn, flags, &gpo_list);
280         if (!ADS_ERR_OK(status)) {
281                 goto out;
282         }
283
284         /* FIXME: allow to process just a single extension */
285         status = gpo_process_gpo_list(ads, mem_ctx, &gpo_list, NULL, flags); 
286         if (!ADS_ERR_OK(status)) {
287                 goto out;
288         }
289
290 out:
291         ads_memfree(ads, dn);
292         ads_msgfree(ads, res);
293
294         ads_destroy(&ads);
295         talloc_destroy(mem_ctx);
296         return 0;
297 }
298
299
300 static int net_ads_gpo_get_link(int argc, const char **argv)
301 {
302         ADS_STRUCT *ads;
303         ADS_STATUS status;
304         TALLOC_CTX *mem_ctx;
305         struct GP_LINK gp_link;
306
307         if (argc < 1) {
308                 printf("usage: net ads gpo getlink <linkname>\n");
309                 return -1;
310         }
311
312         mem_ctx = talloc_init("add_gpo_link");
313         if (mem_ctx == NULL) {
314                 return -1;
315         }
316
317         status = ads_startup(False, &ads);
318         if (!ADS_ERR_OK(status)) {
319                 goto out;
320         }
321
322         status = ads_get_gpo_link(ads, mem_ctx, argv[0], &gp_link);
323         if (!ADS_ERR_OK(status)) {
324                 d_printf("get link for %s failed: %s\n", argv[0], ads_errstr(status));
325                 goto out;
326         }       
327
328         dump_gplink(ads, mem_ctx, &gp_link);
329
330 out:
331         talloc_destroy(mem_ctx);
332         ads_destroy(&ads);
333
334         return 0;
335 }
336
337 static int net_ads_gpo_add_link(int argc, const char **argv)
338 {
339         ADS_STRUCT *ads;
340         ADS_STATUS status;
341         uint32 gpo_opt = 0;
342         TALLOC_CTX *mem_ctx;
343
344         if (argc < 2) {
345                 printf("usage: net ads gpo addlink <linkdn> <gpodn> [options]\n");
346                 return -1;
347         }
348
349         mem_ctx = talloc_init("add_gpo_link");
350         if (mem_ctx == NULL) {
351                 return -1;
352         }
353
354         if (argc == 3) {
355                 gpo_opt = atoi(argv[2]);
356         }
357
358         status = ads_startup(False, &ads);
359         if (!ADS_ERR_OK(status)) {
360                 goto out;
361         }
362
363         status = ads_add_gpo_link(ads, mem_ctx, argv[0], argv[1], gpo_opt);
364         if (!ADS_ERR_OK(status)) {
365                 d_printf("add link failed: %s\n", ads_errstr(status));
366                 goto out;
367         }
368
369 out:
370         talloc_destroy(mem_ctx);
371         ads_destroy(&ads);
372
373         return 0;
374 }
375
376 static int net_ads_gpo_delete_link(int argc, const char **argv)
377 {
378         ADS_STRUCT *ads;
379         ADS_STATUS status;
380         TALLOC_CTX *mem_ctx;
381
382         if (argc < 2) {
383                 return -1;
384         }
385
386         mem_ctx = talloc_init("delete_gpo_link");
387         if (mem_ctx == NULL) {
388                 return -1;
389         }
390
391         status = ads_startup(False, &ads);
392         if (!ADS_ERR_OK(status)) {
393                 goto out;
394         }
395
396         status = ads_delete_gpo_link(ads, mem_ctx, argv[0], argv[1]);
397         if (!ADS_ERR_OK(status)) {
398                 d_printf("delete link failed: %s\n", ads_errstr(status));
399                 goto out;
400         }       
401
402 out:
403         talloc_destroy(mem_ctx);
404         ads_destroy(&ads);
405
406         return 0;
407 }
408
409 static int net_ads_gpo_get_gpo(int argc, const char **argv)
410 {
411         ADS_STRUCT *ads;
412         ADS_STATUS status;
413         TALLOC_CTX *mem_ctx;
414         struct GROUP_POLICY_OBJECT gpo;
415         uint32 sysvol_gpt_version;
416         char *display_name;
417
418         if (argc < 1) {
419                 return -1;
420         }
421
422         mem_ctx = talloc_init("add_gpo_get_gpo");
423         if (mem_ctx == NULL) {
424                 return -1;
425         }
426
427         status = ads_startup(False, &ads);
428         if (!ADS_ERR_OK(status)) {
429                 goto out;
430         }
431
432         if (strnequal(argv[0], "CN={", strlen("CN={"))) {
433                 status = ads_get_gpo(ads, mem_ctx, argv[0], NULL, NULL, &gpo);
434         } else {
435                 status = ads_get_gpo(ads, mem_ctx, NULL, argv[0], NULL, &gpo);
436         }
437
438         if (!ADS_ERR_OK(status)) {
439                 d_printf("get gpo for [%s] failed: %s\n", argv[0], ads_errstr(status));
440                 goto out;
441         }       
442
443         dump_gpo(mem_ctx, &gpo);
444
445         status = ADS_ERROR_NT(ads_gpo_get_sysvol_gpt_version(ads, mem_ctx, 
446                                                              gpo.file_sys_path, 
447                                                              &sysvol_gpt_version, 
448                                                              &display_name)); 
449         if (!ADS_ERR_OK(status)) {
450                 goto out;
451         }
452
453         printf("sysvol GPT version: %d\n", sysvol_gpt_version);
454
455 out:
456         talloc_destroy(mem_ctx);
457         ads_destroy(&ads);
458
459         return 0;
460 }
461
462 int net_ads_gpo(int argc, const char **argv)
463 {
464         struct functable func[] = {
465                 {"LIST", net_ads_gpo_list},
466                 {"EFFECTIVE", net_ads_gpo_effective},
467                 {"ADDLINK", net_ads_gpo_add_link},
468                 {"DELETELINK", net_ads_gpo_delete_link},
469                 {"GETLINK", net_ads_gpo_get_link},
470                 {"GETGPO", net_ads_gpo_get_gpo},
471                 {"HELP", net_ads_gpo_usage},
472                 {"APPLY", net_ads_gpo_apply},
473                 {NULL, NULL}
474         };
475
476         return net_run_function(argc, argv, func, net_ads_gpo_usage);
477 }
478
479 #endif /* HAVE_ADS */