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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
|
/*
* Unix SMB/CIFS implementation.
* Local SAM access routines
* Copyright (C) Volker Lendecke 2006
*
* 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"
/*
* Set a user's data
*/
static int net_sam_userset(int argc, const char **argv, const char *field,
BOOL (*fn)(struct samu *, const char *,
enum pdb_value_state))
{
struct samu *sam_acct = NULL;
DOM_SID sid;
enum lsa_SidType type;
const char *dom, *name;
NTSTATUS status;
if (argc != 2) {
d_fprintf(stderr, "usage: net sam set %s <user> <value>\n",
field);
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
&dom, &name, &sid, &type)) {
d_fprintf(stderr, "Could not find name %s\n", argv[0]);
return -1;
}
if (type != SID_NAME_USER) {
d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
sid_type_lookup(type));
return -1;
}
if ( !(sam_acct = samu_new( NULL )) ) {
d_fprintf(stderr, "Internal error\n");
return -1;
}
if (!pdb_getsampwsid(sam_acct, &sid)) {
d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
return -1;
}
if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
d_fprintf(stderr, "Internal error\n");
return -1;
}
status = pdb_update_sam_account(sam_acct);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Updating sam account %s failed with %s\n",
argv[0], nt_errstr(status));
return -1;
}
TALLOC_FREE(sam_acct);
d_printf("Updated %s for %s\\%s to %s\n", field, dom, name, argv[1]);
return 0;
}
static int net_sam_set_fullname(int argc, const char **argv)
{
return net_sam_userset(argc, argv, "fullname",
pdb_set_fullname);
}
static int net_sam_set_logonscript(int argc, const char **argv)
{
return net_sam_userset(argc, argv, "logonscript",
pdb_set_logon_script);
}
static int net_sam_set_profilepath(int argc, const char **argv)
{
return net_sam_userset(argc, argv, "profilepath",
pdb_set_profile_path);
}
static int net_sam_set_homedrive(int argc, const char **argv)
{
return net_sam_userset(argc, argv, "homedrive",
pdb_set_dir_drive);
}
static int net_sam_set_homedir(int argc, const char **argv)
{
return net_sam_userset(argc, argv, "homedir",
pdb_set_homedir);
}
static int net_sam_set_workstations(int argc, const char **argv)
{
return net_sam_userset(argc, argv, "workstations",
pdb_set_workstations);
}
/*
* Set account flags
*/
static int net_sam_set_userflag(int argc, const char **argv, const char *field,
uint16 flag)
{
struct samu *sam_acct = NULL;
DOM_SID sid;
enum lsa_SidType type;
const char *dom, *name;
NTSTATUS status;
uint16 acct_flags;
if ((argc != 2) || (!strequal(argv[1], "yes") &&
!strequal(argv[1], "no"))) {
d_fprintf(stderr, "usage: net sam set %s <user> [yes|no]\n",
field);
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
&dom, &name, &sid, &type)) {
d_fprintf(stderr, "Could not find name %s\n", argv[0]);
return -1;
}
if (type != SID_NAME_USER) {
d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
sid_type_lookup(type));
return -1;
}
if ( !(sam_acct = samu_new( NULL )) ) {
d_fprintf(stderr, "Internal error\n");
return -1;
}
if (!pdb_getsampwsid(sam_acct, &sid)) {
d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
return -1;
}
acct_flags = pdb_get_acct_ctrl(sam_acct);
if (strequal(argv[1], "yes")) {
acct_flags |= flag;
} else {
acct_flags &= ~flag;
}
pdb_set_acct_ctrl(sam_acct, acct_flags, PDB_CHANGED);
status = pdb_update_sam_account(sam_acct);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Updating sam account %s failed with %s\n",
argv[0], nt_errstr(status));
return -1;
}
TALLOC_FREE(sam_acct);
d_fprintf(stderr, "Updated flag %s for %s\\%s to %s\n", field, dom,
name, argv[1]);
return 0;
}
static int net_sam_set_disabled(int argc, const char **argv)
{
return net_sam_set_userflag(argc, argv, "disabled", ACB_DISABLED);
}
static int net_sam_set_pwnotreq(int argc, const char **argv)
{
return net_sam_set_userflag(argc, argv, "pwnotreq", ACB_PWNOTREQ);
}
static int net_sam_set_autolock(int argc, const char **argv)
{
return net_sam_set_userflag(argc, argv, "autolock", ACB_AUTOLOCK);
}
static int net_sam_set_pwnoexp(int argc, const char **argv)
{
return net_sam_set_userflag(argc, argv, "pwnoexp", ACB_PWNOEXP);
}
/*
* Set pass last change time, based on force pass change now
*/
static int net_sam_set_pwdmustchangenow(int argc, const char **argv)
{
struct samu *sam_acct = NULL;
DOM_SID sid;
enum lsa_SidType type;
const char *dom, *name;
NTSTATUS status;
if ((argc != 2) || (!strequal(argv[1], "yes") &&
!strequal(argv[1], "no"))) {
d_fprintf(stderr, "usage: net sam set pwdmustchangenow <user> [yes|no]\n");
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
&dom, &name, &sid, &type)) {
d_fprintf(stderr, "Could not find name %s\n", argv[0]);
return -1;
}
if (type != SID_NAME_USER) {
d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
sid_type_lookup(type));
return -1;
}
if ( !(sam_acct = samu_new( NULL )) ) {
d_fprintf(stderr, "Internal error\n");
return -1;
}
if (!pdb_getsampwsid(sam_acct, &sid)) {
d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
return -1;
}
if (strequal(argv[1], "yes")) {
pdb_set_pass_last_set_time(sam_acct, 0, PDB_CHANGED);
} else {
pdb_set_pass_last_set_time(sam_acct, time(NULL), PDB_CHANGED);
}
status = pdb_update_sam_account(sam_acct);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Updating sam account %s failed with %s\n",
argv[0], nt_errstr(status));
return -1;
}
TALLOC_FREE(sam_acct);
d_fprintf(stderr, "Updated 'user must change password at next logon' for %s\\%s to %s\n", dom,
name, argv[1]);
return 0;
}
/*
* Set a user's or a group's comment
*/
static int net_sam_set_comment(int argc, const char **argv)
{
GROUP_MAP map;
DOM_SID sid;
enum lsa_SidType type;
const char *dom, *name;
NTSTATUS status;
if (argc != 2) {
d_fprintf(stderr, "usage: net sam set comment <name> "
"<comment>\n");
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
&dom, &name, &sid, &type)) {
d_fprintf(stderr, "Could not find name %s\n", argv[0]);
return -1;
}
if (type == SID_NAME_USER) {
return net_sam_userset(argc, argv, "comment",
pdb_set_acct_desc);
}
if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
(type != SID_NAME_WKN_GRP)) {
d_fprintf(stderr, "%s is a %s, not a group\n", argv[0],
sid_type_lookup(type));
return -1;
}
if (!pdb_getgrsid(&map, sid)) {
d_fprintf(stderr, "Could not load group %s\n", argv[0]);
return -1;
}
fstrcpy(map.comment, argv[1]);
status = pdb_update_group_mapping_entry(&map);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Updating group mapping entry failed with "
"%s\n", nt_errstr(status));
return -1;
}
d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
argv[1]);
return 0;
}
static int net_sam_set(int argc, const char **argv)
{
struct functable2 func[] = {
{ "homedir", net_sam_set_homedir,
"Change a user's home directory" },
{ "profilepath", net_sam_set_profilepath,
"Change a user's profile path" },
{ "comment", net_sam_set_comment,
"Change a users or groups description" },
{ "fullname", net_sam_set_fullname,
"Change a user's full name" },
{ "logonscript", net_sam_set_logonscript,
"Change a user's logon script" },
{ "homedrive", net_sam_set_homedrive,
"Change a user's home drive" },
{ "workstations", net_sam_set_workstations,
"Change a user's allowed workstations" },
{ "disabled", net_sam_set_disabled,
"Disable/Enable a user" },
{ "pwnotreq", net_sam_set_pwnotreq,
"Disable/Enable the password not required flag" },
{ "autolock", net_sam_set_autolock,
"Disable/Enable a user's lockout flag" },
{ "pwnoexp", net_sam_set_pwnoexp,
"Disable/Enable whether a user's pw does not expire" },
{ "pwdmustchangenow", net_sam_set_pwdmustchangenow,
"Force users password must change at next logon" },
{NULL, NULL}
};
return net_run_function2(argc, argv, "net sam set", func);
}
/*
* Manage account policies
*/
static int net_sam_policy_set(int argc, const char **argv)
{
const char *account_policy = NULL;
uint32 value, old_value;
int field;
if (argc != 2) {
d_fprintf(stderr, "usage: net sam policy set"
"\"<account policy>\" <value> \n");
return -1;
}
value = strtoul(argv[1], NULL, 10);
account_policy = argv[0];
field = account_policy_name_to_fieldnum(account_policy);
printf("Account policy \"%s\" description: %s\n", account_policy,
account_policy_get_desc(field));
if (!pdb_get_account_policy(field, &old_value)) {
fprintf(stderr, "Valid account policy, but unable to "
"fetch value!\n");
return -1;
}
printf("Account policy \"%s\" value was: %d\n", account_policy,
old_value);
if (!pdb_set_account_policy(field, value)) {
d_fprintf(stderr, "Setting account policy %s to %u failed \n",
account_policy, value);
}
printf("Account policy \"%s\" value is now: %d\n", account_policy,
value);
return 0;
}
static int net_sam_policy_show(int argc, const char **argv)
{
const char *account_policy = NULL;
uint32 old_value;
int field;
if (argc != 1) {
d_fprintf(stderr, "usage: net sam policy show"
" \"<account policy>\" \n");
return -1;
}
account_policy = argv[0];
field = account_policy_name_to_fieldnum(account_policy);
if (field == 0) {
char *apn = account_policy_names_list();
d_fprintf(stderr, "No account policy by that name!\n");
if (apn) {
d_fprintf(stderr, "Valid account policies "
"are:\n%s\n", apn);
}
SAFE_FREE(apn);
return -1;
}
if (!pdb_get_account_policy(field, &old_value)) {
fprintf(stderr, "Valid account policy, but unable to "
"fetch value!\n");
return -1;
}
printf("Account policy \"%s\" description: %s\n",
account_policy, account_policy_get_desc(field));
printf("Account policy \"%s\" value is: %d\n", account_policy,
old_value);
return 0;
}
static int net_sam_policy_list(int argc, const char **argv)
{
char *apn = account_policy_names_list();
if (apn) {
d_fprintf(stderr, "Valid account policies "
"are:\n\n%s\n", apn);
}
SAFE_FREE(apn);
return -1;
}
static int net_sam_policy(int argc, const char **argv)
{
struct functable2 func[] = {
{ "list", net_sam_policy_list,
"List account policies" },
{ "show", net_sam_policy_show,
"Show account policies" },
{ "set", net_sam_policy_set,
"Change account policies" },
{NULL, NULL}
};
return net_run_function2(argc, argv, "net sam policy", func);
}
/*
* Map a unix group to a domain group
*/
static int net_sam_mapunixgroup(int argc, const char **argv)
{
NTSTATUS status;
GROUP_MAP map;
struct group *grp;
if (argc != 1) {
d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
return -1;
}
grp = getgrnam(argv[0]);
if (grp == NULL) {
d_fprintf(stderr, "Could not find group %s\n", argv[0]);
return -1;
}
status = map_unix_group(grp, &map);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Mapping group %s failed with %s\n",
argv[0], nt_errstr(status));
return -1;
}
d_printf("Mapped unix group %s to SID %s\n", argv[0],
sid_string_static(&map.sid));
return 0;
}
/*
* Create a local group
*/
static int net_sam_createlocalgroup(int argc, const char **argv)
{
NTSTATUS status;
uint32 rid;
if (argc != 1) {
d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
return -1;
}
if (!winbind_ping()) {
d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
"only works when winbind runs.\n");
return -1;
}
status = pdb_create_alias(argv[0], &rid);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Creating %s failed with %s\n",
argv[0], nt_errstr(status));
return -1;
}
d_printf("Created local group %s with RID %d\n", argv[0], rid);
return 0;
}
/*
* Create a local group
*/
static int net_sam_createbuiltingroup(int argc, const char **argv)
{
NTSTATUS status;
uint32 rid;
enum lsa_SidType type;
fstring groupname;
DOM_SID sid;
if (argc != 1) {
d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
return -1;
}
if (!winbind_ping()) {
d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
"only works when winbind runs.\n");
return -1;
}
/* validate the name and get the group */
fstrcpy( groupname, "BUILTIN\\" );
fstrcat( groupname, argv[0] );
if ( !lookup_name(tmp_talloc_ctx(), groupname, LOOKUP_NAME_ALL, NULL,
NULL, &sid, &type)) {
d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
return -1;
}
if ( !sid_peek_rid( &sid, &rid ) ) {
d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
return -1;
}
status = pdb_create_builtin_alias( rid );
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Creating %s failed with %s\n",
argv[0], nt_errstr(status));
return -1;
}
d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
return 0;
}
/*
* Add a group member
*/
static int net_sam_addmem(int argc, const char **argv)
{
const char *groupdomain, *groupname, *memberdomain, *membername;
DOM_SID group, member;
enum lsa_SidType grouptype, membertype;
NTSTATUS status;
if (argc != 2) {
d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
&groupdomain, &groupname, &group, &grouptype)) {
d_fprintf(stderr, "Could not find group %s\n", argv[0]);
return -1;
}
/* check to see if the member to be added is a name or a SID */
if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
&memberdomain, &membername, &member, &membertype))
{
/* try it as a SID */
if ( !string_to_sid( &member, argv[1] ) ) {
d_fprintf(stderr, "Could not find member %s\n", argv[1]);
return -1;
}
if ( !lookup_sid(tmp_talloc_ctx(), &member, &memberdomain,
&membername, &membertype) )
{
d_fprintf(stderr, "Could not resolve SID %s\n", argv[1]);
return -1;
}
}
if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
if ((membertype != SID_NAME_USER) &&
(membertype != SID_NAME_DOM_GRP)) {
d_fprintf(stderr, "%s is a local group, only users "
"and domain groups can be added.\n"
"%s is a %s\n", argv[0], argv[1],
sid_type_lookup(membertype));
return -1;
}
status = pdb_add_aliasmem(&group, &member);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Adding local group member failed "
"with %s\n", nt_errstr(status));
return -1;
}
} else {
d_fprintf(stderr, "Can only add members to local groups so "
"far, %s is a %s\n", argv[0],
sid_type_lookup(grouptype));
return -1;
}
d_printf("Added %s\\%s to %s\\%s\n", memberdomain, membername,
groupdomain, groupname);
return 0;
}
/*
* Delete a group member
*/
static int net_sam_delmem(int argc, const char **argv)
{
const char *groupdomain, *groupname;
const char *memberdomain = NULL;
const char *membername = NULL;
DOM_SID group, member;
enum lsa_SidType grouptype;
NTSTATUS status;
if (argc != 2) {
d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
&groupdomain, &groupname, &group, &grouptype)) {
d_fprintf(stderr, "Could not find group %s\n", argv[0]);
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
&memberdomain, &membername, &member, NULL)) {
if (!string_to_sid(&member, argv[1])) {
d_fprintf(stderr, "Could not find member %s\n",
argv[1]);
return -1;
}
}
if ((grouptype == SID_NAME_ALIAS) ||
(grouptype == SID_NAME_WKN_GRP)) {
status = pdb_del_aliasmem(&group, &member);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Deleting local group member failed "
"with %s\n", nt_errstr(status));
return -1;
}
} else {
d_fprintf(stderr, "Can only delete members from local groups "
"so far, %s is a %s\n", argv[0],
sid_type_lookup(grouptype));
return -1;
}
if (membername != NULL) {
d_printf("Deleted %s\\%s from %s\\%s\n",
memberdomain, membername, groupdomain, groupname);
} else {
d_printf("Deleted %s from %s\\%s\n",
sid_string_static(&member), groupdomain, groupname);
}
return 0;
}
/*
* List group members
*/
static int net_sam_listmem(int argc, const char **argv)
{
const char *groupdomain, *groupname;
DOM_SID group;
enum lsa_SidType grouptype;
NTSTATUS status;
if (argc != 1) {
d_fprintf(stderr, "usage: net sam listmem <group>\n");
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
&groupdomain, &groupname, &group, &grouptype)) {
d_fprintf(stderr, "Could not find group %s\n", argv[0]);
return -1;
}
if ((grouptype == SID_NAME_ALIAS) ||
(grouptype == SID_NAME_WKN_GRP)) {
DOM_SID *members = NULL;
size_t i, num_members = 0;
status = pdb_enum_aliasmem(&group, &members, &num_members);
if (!NT_STATUS_IS_OK(status)) {
d_fprintf(stderr, "Listing group members failed with "
"%s\n", nt_errstr(status));
return -1;
}
d_printf("%s\\%s has %u members\n", groupdomain, groupname,
(unsigned int)num_members);
for (i=0; i<num_members; i++) {
const char *dom, *name;
if (lookup_sid(tmp_talloc_ctx(), &members[i],
&dom, &name, NULL)) {
d_printf(" %s\\%s\n", dom, name);
} else {
d_printf(" %s\n",
sid_string_static(&members[i]));
}
}
} else {
d_fprintf(stderr, "Can only list local group members so far.\n"
"%s is a %s\n", argv[0], sid_type_lookup(grouptype));
return -1;
}
return 0;
}
/*
* Do the listing
*/
static int net_sam_do_list(int argc, const char **argv,
struct pdb_search *search, const char *what)
{
BOOL verbose = (argc == 1);
if ((argc > 1) ||
((argc == 1) && !strequal(argv[0], "verbose"))) {
d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
return -1;
}
if (search == NULL) {
d_fprintf(stderr, "Could not start search\n");
return -1;
}
while (True) {
struct samr_displayentry entry;
if (!search->next_entry(search, &entry)) {
break;
}
if (verbose) {
d_printf("%s:%d:%s\n",
entry.account_name,
entry.rid,
entry.description);
} else {
d_printf("%s\n", entry.account_name);
}
}
search->search_end(search);
return 0;
}
static int net_sam_list_users(int argc, const char **argv)
{
return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
"users");
}
static int net_sam_list_groups(int argc, const char **argv)
{
return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
}
static int net_sam_list_localgroups(int argc, const char **argv)
{
return net_sam_do_list(argc, argv,
pdb_search_aliases(get_global_sam_sid()),
"localgroups");
}
static int net_sam_list_builtin(int argc, const char **argv)
{
return net_sam_do_list(argc, argv,
pdb_search_aliases(&global_sid_Builtin),
"builtin");
}
static int net_sam_list_workstations(int argc, const char **argv)
{
return net_sam_do_list(argc, argv,
pdb_search_users(ACB_WSTRUST),
"workstations");
}
/*
* List stuff
*/
static int net_sam_list(int argc, const char **argv)
{
struct functable2 func[] = {
{ "users", net_sam_list_users,
"List SAM users" },
{ "groups", net_sam_list_groups,
"List SAM groups" },
{ "localgroups", net_sam_list_localgroups,
"List SAM local groups" },
{ "builtin", net_sam_list_builtin,
"List builtin groups" },
{ "workstations", net_sam_list_workstations,
"List domain member workstations" },
{NULL, NULL}
};
return net_run_function2(argc, argv, "net sam list", func);
}
/*
* Show details of SAM entries
*/
static int net_sam_show(int argc, const char **argv)
{
DOM_SID sid;
enum lsa_SidType type;
const char *dom, *name;
if (argc != 1) {
d_fprintf(stderr, "usage: net sam show <name>\n");
return -1;
}
if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
&dom, &name, &sid, &type)) {
d_fprintf(stderr, "Could not find name %s\n", argv[0]);
return -1;
}
d_printf("%s\\%s is a %s with SID %s\n", dom, name,
sid_type_lookup(type), sid_string_static(&sid));
return 0;
}
#ifdef HAVE_LDAP
/*
* Init an LDAP tree with default users and Groups
* if ldapsam:editposix is enabled
*/
static int net_sam_provision(int argc, const char **argv)
{
TALLOC_CTX *tc;
char *ldap_bk;
char *ldap_uri = NULL;
char *p;
struct smbldap_state *ls;
GROUP_MAP gmap;
DOM_SID gsid;
gid_t domusers_gid = -1;
gid_t domadmins_gid = -1;
struct samu *samuser;
struct passwd *pwd;
tc = talloc_new(NULL);
if (!tc) {
d_fprintf(stderr, "Out of Memory!\n");
return -1;
}
if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
d_fprintf(stderr, "talloc failed\n");
talloc_free(tc);
return -1;
}
p = strchr(ldap_bk, ':');
if (p) {
*p = 0;
ldap_uri = talloc_strdup(tc, p+1);
trim_char(ldap_uri, ' ', ' ');
}
trim_char(ldap_bk, ' ', ' ');
if (strcmp(ldap_bk, "ldapsam") != 0) {
d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
goto failed;
}
if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
!lp_parm_bool(-1, "ldapsam", "editposix", False)) {
d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
" and ldapsam:editposix are enabled.\n");
goto failed;
}
if (!winbind_ping()) {
d_fprintf(stderr, "winbind seems not to run. Provisioning "
"LDAP only works when winbind runs.\n");
goto failed;
}
if (!NT_STATUS_IS_OK(smbldap_init(tc, ldap_uri, &ls))) {
d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
goto failed;
}
d_printf("Checking for Domain Users group.\n");
sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
if (!pdb_getgrsid(&gmap, gsid)) {
LDAPMod **mods = NULL;
char *dn;
char *uname;
char *wname;
char *gidstr;
char *gtype;
int rc;
d_printf("Adding the Domain Users group.\n");
/* lets allocate a new groupid for this group */
if (!winbind_allocate_gid(&domusers_gid)) {
d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
goto domu_done;
}
uname = talloc_strdup(tc, "domusers");
wname = talloc_strdup(tc, "Domain Users");
dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
gidstr = talloc_asprintf(tc, "%d", domusers_gid);
gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
if (!uname || !wname || !dn || !gidstr || !gtype) {
d_fprintf(stderr, "Out of Memory!\n");
goto failed;
}
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
talloc_autofree_ldapmod(tc, mods);
rc = smbldap_add(ls, dn, mods);
if (rc != LDAP_SUCCESS) {
d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
}
} else {
d_printf("found!\n");
}
domu_done:
d_printf("Checking for Domain Admins group.\n");
sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
if (!pdb_getgrsid(&gmap, gsid)) {
LDAPMod **mods = NULL;
char *dn;
char *uname;
char *wname;
char *gidstr;
char *gtype;
int rc;
d_printf("Adding the Domain Admins group.\n");
/* lets allocate a new groupid for this group */
if (!winbind_allocate_gid(&domadmins_gid)) {
d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
goto doma_done;
}
uname = talloc_strdup(tc, "domadmins");
wname = talloc_strdup(tc, "Domain Admins");
dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
if (!uname || !wname || !dn || !gidstr || !gtype) {
d_fprintf(stderr, "Out of Memory!\n");
goto failed;
}
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
talloc_autofree_ldapmod(tc, mods);
rc = smbldap_add(ls, dn, mods);
if (rc != LDAP_SUCCESS) {
d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
}
} else {
d_printf("found!\n");
}
doma_done:
d_printf("Check for Administrator account.\n");
samuser = samu_new(tc);
if (!samuser) {
d_fprintf(stderr, "Out of Memory!\n");
goto failed;
}
if (!pdb_getsampwnam(samuser, "Administrator")) {
LDAPMod **mods = NULL;
DOM_SID sid;
char *dn;
char *name;
char *uidstr;
char *gidstr;
char *shell;
char *dir;
uid_t uid;
int rc;
d_printf("Adding the Administrator user.\n");
if (domadmins_gid == -1) {
d_fprintf(stderr, "Can't create Administrtor user, Domain Admins group not available!\n");
goto done;
}
if (!winbind_allocate_uid(&uid)) {
d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
goto done;
}
name = talloc_strdup(tc, "Administrator");
dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
uidstr = talloc_asprintf(tc, "%d", uid);
gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
dir = talloc_sub_specified(tc, lp_template_homedir(),
"Administrator",
get_global_sam_name(),
uid, domadmins_gid);
shell = talloc_sub_specified(tc, lp_template_shell(),
"Administrator",
get_global_sam_name(),
uid, domadmins_gid);
if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
d_fprintf(stderr, "Out of Memory!\n");
goto failed;
}
sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
NEW_PW_FORMAT_SPACE_PADDED_LEN));
talloc_autofree_ldapmod(tc, mods);
rc = smbldap_add(ls, dn, mods);
if (rc != LDAP_SUCCESS) {
d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
}
} else {
d_printf("found!\n");
}
d_printf("Checking for Guest user.\n");
samuser = samu_new(tc);
if (!samuser) {
d_fprintf(stderr, "Out of Memory!\n");
goto failed;
}
if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
LDAPMod **mods = NULL;
DOM_SID sid;
char *dn;
char *uidstr;
char *gidstr;
int rc;
d_printf("Adding the Guest user.\n");
pwd = getpwnam_alloc(tc, lp_guestaccount());
if (!pwd) {
if (domusers_gid == -1) {
d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
goto done;
}
if ((pwd = talloc(tc, struct passwd)) == NULL) {
d_fprintf(stderr, "talloc failed\n");
goto done;
}
pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
if (!winbind_allocate_uid(&(pwd->pw_uid))) {
d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
goto done;
}
pwd->pw_gid = domusers_gid;
pwd->pw_dir = talloc_strdup(tc, "/");
pwd->pw_shell = talloc_strdup(tc, "/bin/false");
if (!pwd->pw_dir || !pwd->pw_shell) {
d_fprintf(stderr, "Out of Memory!\n");
goto failed;
}
}
sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
if (!dn || !uidstr || !gidstr) {
d_fprintf(stderr, "Out of Memory!\n");
goto failed;
}
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
NEW_PW_FORMAT_SPACE_PADDED_LEN));
talloc_autofree_ldapmod(tc, mods);
rc = smbldap_add(ls, dn, mods);
if (rc != LDAP_SUCCESS) {
d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
}
} else {
d_printf("found!\n");
}
d_printf("Checking Guest's group.\n");
pwd = getpwnam_alloc(NULL, lp_guestaccount());
if (!pwd) {
d_fprintf(stderr, "Failed to find just created Guest account!\n"
" Is nssswitch properly configured?!\n");
goto failed;
}
if (pwd->pw_gid == domusers_gid) {
d_printf("found!\n");
goto done;
}
if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
LDAPMod **mods = NULL;
char *dn;
char *uname;
char *wname;
char *gidstr;
char *gtype;
int rc;
d_printf("Adding the Domain Guests group.\n");
uname = talloc_strdup(tc, "domguests");
wname = talloc_strdup(tc, "Domain Guests");
dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
if (!uname || !wname || !dn || !gidstr || !gtype) {
d_fprintf(stderr, "Out of Memory!\n");
goto failed;
}
sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
talloc_autofree_ldapmod(tc, mods);
rc = smbldap_add(ls, dn, mods);
if (rc != LDAP_SUCCESS) {
d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
}
} else {
d_printf("found!\n");
}
done:
talloc_free(tc);
return 0;
failed:
talloc_free(tc);
return -1;
}
#endif
/***********************************************************
migrated functionality from smbgroupedit
**********************************************************/
int net_sam(int argc, const char **argv)
{
struct functable2 func[] = {
{ "createbuiltingroup", net_sam_createbuiltingroup,
"Create a new BUILTIN group" },
{ "createlocalgroup", net_sam_createlocalgroup,
"Create a new local group" },
{ "mapunixgroup", net_sam_mapunixgroup,
"Map a unix group to a domain group" },
{ "addmem", net_sam_addmem,
"Add a member to a group" },
{ "delmem", net_sam_delmem,
"Delete a member from a group" },
{ "listmem", net_sam_listmem,
"List group members" },
{ "list", net_sam_list,
"List users, groups and local groups" },
{ "show", net_sam_show,
"Show details of a SAM entry" },
{ "set", net_sam_set,
"Set details of a SAM account" },
{ "policy", net_sam_policy,
"Set account policies" },
#ifdef HAVE_LDAP
{ "provision", net_sam_provision,
"Provision a clean User Database" },
#endif
{ NULL, NULL, NULL }
};
/* we shouldn't have silly checks like this */
if (getuid() != 0) {
d_fprintf(stderr, "You must be root to edit the SAM "
"directly.\n");
return -1;
}
return net_run_function2(argc, argv, "net sam", func);
}
|