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