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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
/*
Samba Unix/Linux SMB client library
net ads commands for Group Policy
Copyright (C) 2005-2008 Guenther Deschner
Copyright (C) 2009 Wilco Baan Hofman
Based on Guenther's work in net_ads_gpo.h (samba 3)
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "utils/net/net.h"
#include "lib/ldb/include/ldb.h"
#include "auth/auth.h"
#include "param/param.h"
#include "libcli/security/sddl.h"
#include "dsdb/samdb/samdb.h"
#include "dsdb/common/util.h"
#include "lib/policy/policy.h"
static int net_gpo_list_all_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo listall [options]\n");
d_printf("For a list of available options, please type net gpo listall --help\n");
return 0;
}
static int net_gpo_list_all(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
struct gp_object **gpo;
const char **gpo_flags;
unsigned int i, j;
NTSTATUS rv;
rv = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
rv = gp_list_all_gpos(gp_ctx, &gpo);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to list all GPO's: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
for (i = 0; gpo[i] != NULL; i++) {
gp_get_gpo_flags(gp_ctx, gpo[i]->flags, &gpo_flags);
d_printf("GPO : %s\n", gpo[i]->name);
d_printf("display name : %s\n", gpo[i]->display_name);
d_printf("path : %s\n", gpo[i]->file_sys_path);
d_printf("dn : %s\n", gpo[i]->dn);
d_printf("version : %d\n", gpo[i]->version);
if (gpo_flags[0] == NULL) {
d_printf("flags : NONE\n");
} else {
d_printf("flags : %s\n", gpo_flags[0]);
for (j = 1; gpo_flags[j] != NULL; j++) {
d_printf(" %s\n", gpo_flags[i]);
}
}
d_printf("\n");
talloc_free(gpo_flags);
}
talloc_free(gp_ctx);
return 0;
}
static int net_gpo_get_gpo_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo show <dn> [options]\n");
d_printf("For a list of available options, please type net gpo show --help\n");
return 0;
}
static int net_gpo_get_gpo(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
struct gp_object *gpo;
const char **gpo_flags;
char *sddl;
int i;
NTSTATUS rv;
if (argc != 1) {
return net_gpo_get_gpo_usage(ctx, argc, argv);
}
rv = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
rv = gp_get_gpo_info(gp_ctx, argv[0], &gpo);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to get GPO: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
gp_get_gpo_flags(gp_ctx, gpo->flags, &gpo_flags);
d_printf("GPO : %s\n", gpo->name);
d_printf("display name : %s\n", gpo->display_name);
d_printf("path : %s\n", gpo->file_sys_path);
d_printf("dn : %s\n", gpo->dn);
d_printf("version : %d\n", gpo->version);
if (gpo_flags[0] == NULL) {
d_printf("flags : NONE\n");
} else {
d_printf("flags : %s\n", gpo_flags[0]);
for (i = 1; gpo_flags[i] != NULL; i++) {
d_printf(" %s\n", gpo_flags[i]);
}
}
sddl = sddl_encode(gp_ctx, gpo->security_descriptor, samdb_domain_sid(gp_ctx->ldb_ctx));
if (sddl != NULL) {
d_printf("ACL : %s\n", sddl);
}
d_printf("\n");
talloc_free(gp_ctx);
return 0;
}
static int net_gpo_link_get_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo getlink <dn> [options]\n");
d_printf("For a list of available options, please type net gpo getlink --help\n");
return 0;
}
static int net_gpo_link_get(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
struct gp_link **links;
NTSTATUS rv;
unsigned int i,j;
const char **options;
if (argc != 1) {
return net_gpo_link_get_usage(ctx, argc, argv);
}
rv = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
rv = gp_get_gplinks(gp_ctx, argv[0], &links);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to get gplinks: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
for (i = 0; links[i] != NULL; i++) {
gp_get_gplink_options(gp_ctx, links[i]->options, &options);
d_printf("GPO DN : %s\n", links[i]->dn);
if (options[0] == NULL) {
d_printf("Options : NONE\n");
} else {
d_printf("Options : %s\n", options[0]);
for (j = 1; options[j] != NULL; j++) {
d_printf(" : %s\n", options[j]);
}
}
d_printf("\n");
talloc_free(options);
}
talloc_free(gp_ctx);
return 0;
}
static int net_gpo_list_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo list <username> [options]\n");
d_printf("For a list of available options, please type net gpo list --help\n");
return 0;
}
static int net_gpo_list(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
struct ldb_result *result;
struct auth_serversupplied_info *server_info;
struct auth_session_info *session_info;
DATA_BLOB dummy = { NULL, 0 };
const char **gpos;
NTSTATUS status;
int rv;
unsigned int i;
struct auth_context *auth_context;
if (argc != 1) {
return net_gpo_list_usage(ctx, argc, argv);
}
status = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
/* Find the user in the directory. We need extended DN's for group expansion
* in authsam_make_server_info */
rv = dsdb_search(gp_ctx->ldb_ctx,
gp_ctx,
&result,
ldb_get_default_basedn(gp_ctx->ldb_ctx),
LDB_SCOPE_SUBTREE,
NULL,
DSDB_SEARCH_SHOW_EXTENDED_DN,
"(&(objectClass=user)(sAMAccountName=%s))", argv[0]);
if (rv != LDB_SUCCESS) {
DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv),ldb_errstring(gp_ctx->ldb_ctx)));
talloc_free(gp_ctx);
return 1;
}
/* We expect exactly one record */
if (result->count != 1) {
DEBUG(0, ("Could not find SAM account with name %s\n", argv[0]));
talloc_free(gp_ctx);
return 1;
}
/* We need the server info, as this will contain the groups of this
* user, needed for a token */
status = authsam_make_server_info(gp_ctx,
gp_ctx->ldb_ctx,
lp_netbios_name(gp_ctx->lp_ctx),
lp_sam_name(gp_ctx->lp_ctx),
ldb_get_default_basedn(gp_ctx->ldb_ctx),
result->msgs[0],
dummy,
dummy,
&server_info);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to make server information: %s\n", get_friendly_nt_error_msg(status)));
talloc_free(gp_ctx);
return 1;
}
/* We do now need an auth context to create a session */
status = auth_context_create_from_ldb(gp_ctx, gp_ctx->ldb_ctx, &auth_context);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to get an auth context: %s\n", get_friendly_nt_error_msg(status)));
talloc_free(gp_ctx);
return 1;
}
/* The session info will contain the security token for this user */
status = auth_generate_session_info(gp_ctx, auth_context, server_info, 0, &session_info);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to generate session information: %s\n", get_friendly_nt_error_msg(status)));
talloc_free(gp_ctx);
return 1;
}
status = gp_list_gpos(gp_ctx, session_info->security_token, &gpos);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to list gpos for user %s: %s\n", argv[0],
get_friendly_nt_error_msg(status)));
talloc_free(gp_ctx);
return 1;
}
d_printf("GPO's for user %s:\n", argv[0]);
for (i = 0; gpos[i] != NULL; i++) {
d_printf("\t%s\n", gpos[i]);
}
talloc_free(gp_ctx);
return 0;
}
static int net_gpo_link_set_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo setlink <container> <gpo> ['disable'] ['enforce'] [options]\n");
d_printf("For a list of available options, please type net gpo setlink --help\n");
return 0;
}
static int net_gpo_link_set(struct net_context *ctx, int argc, const char **argv)
{
struct gp_link *gplink = talloc_zero(ctx, struct gp_link);
struct gp_context *gp_ctx;
unsigned int i;
NTSTATUS status;
if (argc < 2) {
return net_gpo_link_set_usage(ctx, argc, argv);
}
if (argc >= 3) {
for (i = 2; i < argc; i++) {
if (strcmp(argv[i], "disable") == 0) {
gplink->options |= GPLINK_OPT_DISABLE;
}
if (strcmp(argv[i], "enforce") == 0) {
gplink->options |= GPLINK_OPT_ENFORCE;
}
}
}
gplink->dn = argv[1];
status = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
status = gp_set_gplink(gp_ctx, argv[0], gplink);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to set GPO link on container: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
d_printf("Set link on container.\nCurrent Group Policy links:\n");
/* Display current links */
net_gpo_link_get(ctx, 1, argv);
talloc_free(gp_ctx);
return 0;
}
static int net_gpo_link_del_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo dellink <container> <gpo> [options]\n");
d_printf("For a list of available options, please type net gpo dellink --help\n");
return 0;
}
static int net_gpo_link_del(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
NTSTATUS status;
if (argc != 2) {
return net_gpo_link_del_usage(ctx, argc, argv);
}
status = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
status = gp_del_gplink(gp_ctx, argv[0], argv[1]);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to delete gplink: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
d_printf("Deleted gplink.\nCurrent Group Policy links:\n\n");
/* Display current links */
net_gpo_link_get(ctx, 1, argv);
talloc_free(gp_ctx);
return 0;
}
static int net_gpo_inheritance_get_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo getinheritance <container> [options]\n");
d_printf("For a list of available options, please type net gpo getinheritance --help\n");
return 0;
}
static int net_gpo_inheritance_get(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
enum gpo_inheritance inheritance;
NTSTATUS status;
if (argc != 1) {
return net_gpo_inheritance_get_usage(ctx, argc, argv);
}
status = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
status = gp_get_inheritance(gp_ctx, argv[0], &inheritance);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to set GPO link on container: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
if (inheritance == GPO_BLOCK_INHERITANCE) {
d_printf("container has GPO_BLOCK_INHERITANCE\n");
} else {
d_printf("container has GPO_INHERIT\n");
}
talloc_free(gp_ctx);
return 0;
}
static int net_gpo_inheritance_set_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo setinheritance <container> <\"block\"|\"inherit\"> [options]\n");
d_printf("For a list of available options, please type net gpo setinheritance --help\n");
return 0;
}
static int net_gpo_inheritance_set(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
enum gpo_inheritance inheritance;
NTSTATUS status;
if (argc != 2) {
return net_gpo_inheritance_set_usage(ctx, argc, argv);
}
if (strcmp(argv[1], "inherit") == 0) {
inheritance = GPO_INHERIT;
} else if (strcmp(argv[1], "block") == 0) {
inheritance = GPO_BLOCK_INHERITANCE;
} else {
return net_gpo_inheritance_set_usage(ctx, argc, argv);
}
status = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
status = gp_set_inheritance(gp_ctx, argv[0], inheritance);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0, ("Failed to set GPO link on container: %s\n", get_friendly_nt_error_msg(status)));
return 1;
}
/* Display current links */
net_gpo_inheritance_get(ctx, 1, argv);
return 0;
}
static int net_gpo_fetch_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo fetch <container> [options]\n");
d_printf("For a list of available options, please type net gpo fetch --help\n");
return 0;
}
static int net_gpo_fetch(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
struct gp_object *gpo;
const char *path;
NTSTATUS rv;
if (argc != 1) {
return net_gpo_fetch_usage(ctx, argc, argv);
}
rv = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
rv = gp_get_gpo_info(gp_ctx, argv[0], &gpo);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to get GPO: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
rv = gp_fetch_gpt(gp_ctx, gpo, &path);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to fetch GPO: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
d_printf("%s\n", path);
return 0;
}
static int net_gpo_create_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo create <displayname> [options]\n");
d_printf("For a list of available options, please type net gpo create --help\n");
return 0;
}
static int net_gpo_create(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
struct gp_object *gpo;
NTSTATUS rv;
if (argc != 1) {
return net_gpo_create_usage(ctx, argc, argv);
}
rv = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
rv = gp_create_gpo(gp_ctx, argv[0], &gpo);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to create GPO: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
return 0;
}
static int net_gpo_set_acl_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo setacl <dn> <sddl> [options]\n");
d_printf("For a list of available options, please type net gpo setacl --help\n");
return 0;
}
static int net_gpo_set_acl(struct net_context *ctx, int argc, const char **argv)
{
struct gp_context *gp_ctx;
struct security_descriptor *sd;
NTSTATUS rv;
if (argc != 2) {
return net_gpo_set_acl_usage(ctx, argc, argv);
}
rv = gp_init(ctx, ctx->lp_ctx, ctx->credentials, ctx->event_ctx, &gp_ctx);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to connect to DC's LDAP: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
/* Convert sddl to security descriptor */
sd = sddl_decode(gp_ctx, argv[1], samdb_domain_sid(gp_ctx->ldb_ctx));
if (sd == NULL) {
DEBUG(0, ("Invalid SDDL\n"));
return 1;
}
rv = gp_set_acl(gp_ctx, argv[0], sd);
if (!NT_STATUS_IS_OK(rv)) {
DEBUG(0, ("Failed to set ACL on GPO: %s\n", get_friendly_nt_error_msg(rv)));
return 1;
}
talloc_free(gp_ctx);
return 0;
}
static const struct net_functable net_gpo_functable[] = {
{ "listall", "List all GPO's on a DC\n", net_gpo_list_all, net_gpo_list_all_usage },
{ "list", "List all active GPO's for a machine/user\n", net_gpo_list, net_gpo_list_usage },
{ "show", "Show information for a GPO\n", net_gpo_get_gpo, net_gpo_get_gpo_usage },
{ "getlink", "List gPLink of container\n", net_gpo_link_get, net_gpo_link_get_usage },
{ "setlink", "Link a GPO to a container\n", net_gpo_link_set, net_gpo_link_set_usage },
{ "dellink", "Delete GPO link from a container\n", net_gpo_link_del, net_gpo_link_del_usage },
{ "getinheritance", "Get inheritance flag from a container\n", net_gpo_inheritance_get, net_gpo_inheritance_get_usage },
{ "setinheritance", "Set inheritance flag on a container\n", net_gpo_inheritance_set, net_gpo_inheritance_set_usage },
{ "fetch", "Download a GPO\n", net_gpo_fetch, net_gpo_fetch_usage },
{ "create", "Create a GPO\n", net_gpo_create, net_gpo_create_usage },
{ "setacl", "Set ACL on a GPO\n", net_gpo_set_acl, net_gpo_set_acl_usage },
{ NULL, NULL }
};
int net_gpo_usage(struct net_context *ctx, int argc, const char **argv)
{
d_printf("Syntax: net gpo <command> [options]\n");
d_printf("For available commands, please type net gpo help\n");
return 0;
}
int net_gpo(struct net_context *ctx, int argc, const char **argv)
{
return net_run_function(ctx, argc, argv, net_gpo_functable, net_gpo_usage);
}
|