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