Re-structure Volker's patch to "Fix trans2findfirst for the large directory optimizat...
[Samba.git] / source / utils / net_sam.c
blobb5cd8e637ad867c1d929b20d03b1986c96ab707b
1 /*
2 * Unix SMB/CIFS implementation.
3 * Local SAM access routines
4 * Copyright (C) Volker Lendecke 2006
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "utils/net.h"
25 * Set a user's data
28 static int net_sam_userset(int argc, const char **argv, const char *field,
29 bool (*fn)(struct samu *, const char *,
30 enum pdb_value_state))
32 struct samu *sam_acct = NULL;
33 DOM_SID sid;
34 enum lsa_SidType type;
35 const char *dom, *name;
36 NTSTATUS status;
38 if (argc != 2) {
39 d_fprintf(stderr, "usage: net sam set %s <user> <value>\n",
40 field);
41 return -1;
44 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
45 &dom, &name, &sid, &type)) {
46 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
47 return -1;
50 if (type != SID_NAME_USER) {
51 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
52 sid_type_lookup(type));
53 return -1;
56 if ( !(sam_acct = samu_new( NULL )) ) {
57 d_fprintf(stderr, "Internal error\n");
58 return -1;
61 if (!pdb_getsampwsid(sam_acct, &sid)) {
62 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
63 return -1;
66 if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
67 d_fprintf(stderr, "Internal error\n");
68 return -1;
71 status = pdb_update_sam_account(sam_acct);
72 if (!NT_STATUS_IS_OK(status)) {
73 d_fprintf(stderr, "Updating sam account %s failed with %s\n",
74 argv[0], nt_errstr(status));
75 return -1;
78 TALLOC_FREE(sam_acct);
80 d_printf("Updated %s for %s\\%s to %s\n", field, dom, name, argv[1]);
81 return 0;
84 static int net_sam_set_fullname(int argc, const char **argv)
86 return net_sam_userset(argc, argv, "fullname",
87 pdb_set_fullname);
90 static int net_sam_set_logonscript(int argc, const char **argv)
92 return net_sam_userset(argc, argv, "logonscript",
93 pdb_set_logon_script);
96 static int net_sam_set_profilepath(int argc, const char **argv)
98 return net_sam_userset(argc, argv, "profilepath",
99 pdb_set_profile_path);
102 static int net_sam_set_homedrive(int argc, const char **argv)
104 return net_sam_userset(argc, argv, "homedrive",
105 pdb_set_dir_drive);
108 static int net_sam_set_homedir(int argc, const char **argv)
110 return net_sam_userset(argc, argv, "homedir",
111 pdb_set_homedir);
114 static int net_sam_set_workstations(int argc, const char **argv)
116 return net_sam_userset(argc, argv, "workstations",
117 pdb_set_workstations);
121 * Set account flags
124 static int net_sam_set_userflag(int argc, const char **argv, const char *field,
125 uint16 flag)
127 struct samu *sam_acct = NULL;
128 DOM_SID sid;
129 enum lsa_SidType type;
130 const char *dom, *name;
131 NTSTATUS status;
132 uint16 acct_flags;
134 if ((argc != 2) || (!strequal(argv[1], "yes") &&
135 !strequal(argv[1], "no"))) {
136 d_fprintf(stderr, "usage: net sam set %s <user> [yes|no]\n",
137 field);
138 return -1;
141 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
142 &dom, &name, &sid, &type)) {
143 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
144 return -1;
147 if (type != SID_NAME_USER) {
148 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
149 sid_type_lookup(type));
150 return -1;
153 if ( !(sam_acct = samu_new( NULL )) ) {
154 d_fprintf(stderr, "Internal error\n");
155 return -1;
158 if (!pdb_getsampwsid(sam_acct, &sid)) {
159 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
160 return -1;
163 acct_flags = pdb_get_acct_ctrl(sam_acct);
165 if (strequal(argv[1], "yes")) {
166 acct_flags |= flag;
167 } else {
168 acct_flags &= ~flag;
171 pdb_set_acct_ctrl(sam_acct, acct_flags, PDB_CHANGED);
173 status = pdb_update_sam_account(sam_acct);
174 if (!NT_STATUS_IS_OK(status)) {
175 d_fprintf(stderr, "Updating sam account %s failed with %s\n",
176 argv[0], nt_errstr(status));
177 return -1;
180 TALLOC_FREE(sam_acct);
182 d_fprintf(stderr, "Updated flag %s for %s\\%s to %s\n", field, dom,
183 name, argv[1]);
184 return 0;
187 static int net_sam_set_disabled(int argc, const char **argv)
189 return net_sam_set_userflag(argc, argv, "disabled", ACB_DISABLED);
192 static int net_sam_set_pwnotreq(int argc, const char **argv)
194 return net_sam_set_userflag(argc, argv, "pwnotreq", ACB_PWNOTREQ);
197 static int net_sam_set_autolock(int argc, const char **argv)
199 return net_sam_set_userflag(argc, argv, "autolock", ACB_AUTOLOCK);
202 static int net_sam_set_pwnoexp(int argc, const char **argv)
204 return net_sam_set_userflag(argc, argv, "pwnoexp", ACB_PWNOEXP);
208 * Set pass last change time, based on force pass change now
211 static int net_sam_set_pwdmustchangenow(int argc, const char **argv)
213 struct samu *sam_acct = NULL;
214 DOM_SID sid;
215 enum lsa_SidType type;
216 const char *dom, *name;
217 NTSTATUS status;
219 if ((argc != 2) || (!strequal(argv[1], "yes") &&
220 !strequal(argv[1], "no"))) {
221 d_fprintf(stderr, "usage: net sam set pwdmustchangenow <user> [yes|no]\n");
222 return -1;
225 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
226 &dom, &name, &sid, &type)) {
227 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
228 return -1;
231 if (type != SID_NAME_USER) {
232 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
233 sid_type_lookup(type));
234 return -1;
237 if ( !(sam_acct = samu_new( NULL )) ) {
238 d_fprintf(stderr, "Internal error\n");
239 return -1;
242 if (!pdb_getsampwsid(sam_acct, &sid)) {
243 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
244 return -1;
247 if (strequal(argv[1], "yes")) {
248 pdb_set_pass_last_set_time(sam_acct, 0, PDB_CHANGED);
249 } else {
250 pdb_set_pass_last_set_time(sam_acct, time(NULL), PDB_CHANGED);
253 status = pdb_update_sam_account(sam_acct);
254 if (!NT_STATUS_IS_OK(status)) {
255 d_fprintf(stderr, "Updating sam account %s failed with %s\n",
256 argv[0], nt_errstr(status));
257 return -1;
260 TALLOC_FREE(sam_acct);
262 d_fprintf(stderr, "Updated 'user must change password at next logon' for %s\\%s to %s\n", dom,
263 name, argv[1]);
264 return 0;
269 * Set a user's or a group's comment
272 static int net_sam_set_comment(int argc, const char **argv)
274 GROUP_MAP map;
275 DOM_SID sid;
276 enum lsa_SidType type;
277 const char *dom, *name;
278 NTSTATUS status;
280 if (argc != 2) {
281 d_fprintf(stderr, "usage: net sam set comment <name> "
282 "<comment>\n");
283 return -1;
286 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
287 &dom, &name, &sid, &type)) {
288 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
289 return -1;
292 if (type == SID_NAME_USER) {
293 return net_sam_userset(argc, argv, "comment",
294 pdb_set_acct_desc);
297 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
298 (type != SID_NAME_WKN_GRP)) {
299 d_fprintf(stderr, "%s is a %s, not a group\n", argv[0],
300 sid_type_lookup(type));
301 return -1;
304 if (!pdb_getgrsid(&map, sid)) {
305 d_fprintf(stderr, "Could not load group %s\n", argv[0]);
306 return -1;
309 fstrcpy(map.comment, argv[1]);
311 status = pdb_update_group_mapping_entry(&map);
313 if (!NT_STATUS_IS_OK(status)) {
314 d_fprintf(stderr, "Updating group mapping entry failed with "
315 "%s\n", nt_errstr(status));
316 return -1;
319 d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
320 argv[1]);
322 return 0;
325 static int net_sam_set(int argc, const char **argv)
327 struct functable2 func[] = {
328 { "homedir", net_sam_set_homedir,
329 "Change a user's home directory" },
330 { "profilepath", net_sam_set_profilepath,
331 "Change a user's profile path" },
332 { "comment", net_sam_set_comment,
333 "Change a users or groups description" },
334 { "fullname", net_sam_set_fullname,
335 "Change a user's full name" },
336 { "logonscript", net_sam_set_logonscript,
337 "Change a user's logon script" },
338 { "homedrive", net_sam_set_homedrive,
339 "Change a user's home drive" },
340 { "workstations", net_sam_set_workstations,
341 "Change a user's allowed workstations" },
342 { "disabled", net_sam_set_disabled,
343 "Disable/Enable a user" },
344 { "pwnotreq", net_sam_set_pwnotreq,
345 "Disable/Enable the password not required flag" },
346 { "autolock", net_sam_set_autolock,
347 "Disable/Enable a user's lockout flag" },
348 { "pwnoexp", net_sam_set_pwnoexp,
349 "Disable/Enable whether a user's pw does not expire" },
350 { "pwdmustchangenow", net_sam_set_pwdmustchangenow,
351 "Force users password must change at next logon" },
352 {NULL, NULL}
355 return net_run_function2(argc, argv, "net sam set", func);
359 * Manage account policies
362 static int net_sam_policy_set(int argc, const char **argv)
364 const char *account_policy = NULL;
365 uint32 value = 0;
366 uint32 old_value = 0;
367 int field;
368 char *endptr;
370 if (argc != 2) {
371 d_fprintf(stderr, "usage: net sam policy set "
372 "\"<account policy>\" <value> \n");
373 return -1;
376 account_policy = argv[0];
377 field = account_policy_name_to_fieldnum(account_policy);
379 if (strequal(argv[1], "forever") || strequal(argv[1], "never")
380 || strequal(argv[1], "off")) {
381 value = -1;
383 else {
384 value = strtoul(argv[1], &endptr, 10);
386 if ((endptr == argv[1]) || (endptr[0] != '\0')) {
387 d_printf("Unable to set policy \"%s\"! Invalid value "
388 "\"%s\".\n",
389 account_policy, argv[1]);
390 return -1;
394 if (field == 0) {
395 const char **names;
396 int i, count;
398 account_policy_names_list(&names, &count);
399 d_fprintf(stderr, "No account policy \"%s\"!\n\n", argv[0]);
400 d_fprintf(stderr, "Valid account policies are:\n");
402 for (i=0; i<count; i++) {
403 d_fprintf(stderr, "%s\n", names[i]);
406 SAFE_FREE(names);
407 return -1;
410 if (!pdb_get_account_policy(field, &old_value)) {
411 d_fprintf(stderr, "Valid account policy, but unable to fetch "
412 "value!\n");
413 } else {
414 d_printf("Account policy \"%s\" value was: %d\n", account_policy,
415 old_value);
418 if (!pdb_set_account_policy(field, value)) {
419 d_fprintf(stderr, "Valid account policy, but unable to "
420 "set value!\n");
421 return -1;
422 } else {
423 d_printf("Account policy \"%s\" value is now: %d\n", account_policy,
424 value);
427 return 0;
430 static int net_sam_policy_show(int argc, const char **argv)
432 const char *account_policy = NULL;
433 uint32 old_value;
434 int field;
436 if (argc != 1) {
437 d_fprintf(stderr, "usage: net sam policy show"
438 " \"<account policy>\" \n");
439 return -1;
442 account_policy = argv[0];
443 field = account_policy_name_to_fieldnum(account_policy);
445 if (field == 0) {
446 const char **names;
447 int count;
448 int i;
449 account_policy_names_list(&names, &count);
450 d_fprintf(stderr, "No account policy by that name!\n");
451 if (count != 0) {
452 d_fprintf(stderr, "Valid account policies "
453 "are:\n");
454 for (i=0; i<count; i++) {
455 d_fprintf(stderr, "%s\n", names[i]);
458 SAFE_FREE(names);
459 return -1;
462 if (!pdb_get_account_policy(field, &old_value)) {
463 fprintf(stderr, "Valid account policy, but unable to "
464 "fetch value!\n");
465 return -1;
468 printf("Account policy \"%s\" description: %s\n",
469 account_policy, account_policy_get_desc(field));
470 printf("Account policy \"%s\" value is: %d\n", account_policy,
471 old_value);
472 return 0;
475 static int net_sam_policy_list(int argc, const char **argv)
477 const char **names;
478 int count;
479 int i;
480 account_policy_names_list(&names, &count);
481 if (count != 0) {
482 d_fprintf(stderr, "Valid account policies "
483 "are:\n");
484 for (i = 0; i < count ; i++) {
485 d_fprintf(stderr, "%s\n", names[i]);
488 SAFE_FREE(names);
489 return -1;
492 static int net_sam_policy(int argc, const char **argv)
494 struct functable2 func[] = {
495 { "list", net_sam_policy_list,
496 "List account policies" },
497 { "show", net_sam_policy_show,
498 "Show account policies" },
499 { "set", net_sam_policy_set,
500 "Change account policies" },
501 {NULL, NULL}
504 return net_run_function2(argc, argv, "net sam policy", func);
507 extern PRIVS privs[];
509 static int net_sam_rights_list(int argc, const char **argv)
511 SE_PRIV mask;
513 if (argc > 1) {
514 d_fprintf(stderr, "usage: net sam rights list [privilege name]\n");
515 return -1;
518 if (argc == 0) {
519 int i;
520 int num = count_all_privileges();
522 for (i=0; i<num; i++) {
523 d_printf("%s\n", privs[i].name);
525 return 0;
528 if (se_priv_from_name(argv[0], &mask)) {
529 DOM_SID *sids;
530 int i, num_sids;
531 NTSTATUS status;
533 status = privilege_enum_sids(&mask, talloc_tos(),
534 &sids, &num_sids);
535 if (!NT_STATUS_IS_OK(status)) {
536 d_fprintf(stderr, "Could not list rights: %s\n",
537 nt_errstr(status));
538 return -1;
541 for (i=0; i<num_sids; i++) {
542 const char *dom, *name;
543 enum lsa_SidType type;
545 if (lookup_sid(talloc_tos(), &sids[i], &dom, &name,
546 &type)) {
547 d_printf("%s\\%s\n", dom, name);
549 else {
550 d_printf("%s\n", sid_string_tos(&sids[i]));
553 return 0;
556 return -1;
559 static int net_sam_rights_grant(int argc, const char **argv)
561 DOM_SID sid;
562 enum lsa_SidType type;
563 const char *dom, *name;
564 SE_PRIV mask;
566 if (argc != 2) {
567 d_fprintf(stderr, "usage: net sam rights grant <name> "
568 "<right>\n");
569 return -1;
572 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
573 &dom, &name, &sid, &type)) {
574 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
575 return -1;
578 if (!se_priv_from_name(argv[1], &mask)) {
579 d_fprintf(stderr, "%s unknown\n", argv[1]);
580 return -1;
583 if (!grant_privilege(&sid, &mask)) {
584 d_fprintf(stderr, "Could not grant privilege\n");
585 return -1;
588 d_printf("Granted %s to %s\\%s\n", argv[1], dom, name);
589 return 0;
592 static int net_sam_rights_revoke(int argc, const char **argv)
594 DOM_SID sid;
595 enum lsa_SidType type;
596 const char *dom, *name;
597 SE_PRIV mask;
599 if (argc != 2) {
600 d_fprintf(stderr, "usage: net sam rights revoke <name> "
601 "<right>\n");
602 return -1;
605 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
606 &dom, &name, &sid, &type)) {
607 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
608 return -1;
611 if (!se_priv_from_name(argv[1], &mask)) {
612 d_fprintf(stderr, "%s unknown\n", argv[1]);
613 return -1;
616 if (!revoke_privilege(&sid, &mask)) {
617 d_fprintf(stderr, "Could not revoke privilege\n");
618 return -1;
621 d_printf("Revoked %s from %s\\%s\n", argv[1], dom, name);
622 return 0;
625 static int net_sam_rights(int argc, const char **argv)
627 struct functable2 func[] = {
628 { "list", net_sam_rights_list,
629 "List possible user rights" },
630 { "grant", net_sam_rights_grant,
631 "Grant a right" },
632 { "revoke", net_sam_rights_revoke,
633 "Revoke a right" },
634 { NULL }
636 return net_run_function2(argc, argv, "net sam rights", func);
640 * Map a unix group to a domain group
643 static NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
645 NTSTATUS status;
646 GROUP_MAP map;
647 const char *grpname, *dom, *name;
648 uint32 rid;
650 if (pdb_getgrgid(&map, grp->gr_gid)) {
651 return NT_STATUS_GROUP_EXISTS;
654 map.gid = grp->gr_gid;
655 grpname = grp->gr_name;
657 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
658 &dom, &name, NULL, NULL)) {
660 const char *tmp = talloc_asprintf(
661 talloc_tos(), "Unix Group %s", grp->gr_name);
663 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
664 grpname, dom, name, tmp));
665 grpname = tmp;
668 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
669 NULL, NULL, NULL, NULL)) {
670 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
671 return NT_STATUS_GROUP_EXISTS;
674 fstrcpy(map.nt_name, grpname);
676 if (pdb_rid_algorithm()) {
677 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
678 } else {
679 if (!pdb_new_rid(&rid)) {
680 DEBUG(3, ("Could not get a new RID for %s\n",
681 grp->gr_name));
682 return NT_STATUS_ACCESS_DENIED;
686 sid_compose(&map.sid, get_global_sam_sid(), rid);
687 map.sid_name_use = SID_NAME_DOM_GRP;
688 fstrcpy(map.comment, talloc_asprintf(talloc_tos(), "Unix Group %s",
689 grp->gr_name));
691 status = pdb_add_group_mapping_entry(&map);
692 if (NT_STATUS_IS_OK(status)) {
693 *pmap = map;
695 return status;
698 static int net_sam_mapunixgroup(int argc, const char **argv)
700 NTSTATUS status;
701 GROUP_MAP map;
702 struct group *grp;
704 if (argc != 1) {
705 d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
706 return -1;
709 grp = getgrnam(argv[0]);
710 if (grp == NULL) {
711 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
712 return -1;
715 status = map_unix_group(grp, &map);
717 if (!NT_STATUS_IS_OK(status)) {
718 d_fprintf(stderr, "Mapping group %s failed with %s\n",
719 argv[0], nt_errstr(status));
720 return -1;
723 d_printf("Mapped unix group %s to SID %s\n", argv[0],
724 sid_string_tos(&map.sid));
726 return 0;
730 * Remove a group mapping
733 static NTSTATUS unmap_unix_group(const struct group *grp, GROUP_MAP *pmap)
735 NTSTATUS status;
736 GROUP_MAP map;
737 const char *grpname;
738 DOM_SID dom_sid;
740 map.gid = grp->gr_gid;
741 grpname = grp->gr_name;
743 if (!lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
744 NULL, NULL, NULL, NULL)) {
745 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp->gr_name));
746 return NT_STATUS_NO_SUCH_GROUP;
749 fstrcpy(map.nt_name, grpname);
751 if (!pdb_gid_to_sid(map.gid, &dom_sid)) {
752 return NT_STATUS_UNSUCCESSFUL;
755 status = pdb_delete_group_mapping_entry(dom_sid);
757 return status;
760 static int net_sam_unmapunixgroup(int argc, const char **argv)
762 NTSTATUS status;
763 GROUP_MAP map;
764 struct group *grp;
766 if (argc != 1) {
767 d_fprintf(stderr, "usage: net sam unmapunixgroup <name>\n");
768 return -1;
771 grp = getgrnam(argv[0]);
772 if (grp == NULL) {
773 d_fprintf(stderr, "Could not find mapping for group %s.\n", argv[0]);
774 return -1;
777 status = unmap_unix_group(grp, &map);
779 if (!NT_STATUS_IS_OK(status)) {
780 d_fprintf(stderr, "Unmapping group %s failed with %s.\n",
781 argv[0], nt_errstr(status));
782 return -1;
785 d_printf("Unmapped unix group %s.\n", argv[0]);
787 return 0;
791 * Create a local group
794 static int net_sam_createlocalgroup(int argc, const char **argv)
796 NTSTATUS status;
797 uint32 rid;
799 if (argc != 1) {
800 d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
801 return -1;
804 if (!winbind_ping()) {
805 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
806 "only works when winbind runs.\n");
807 return -1;
810 status = pdb_create_alias(argv[0], &rid);
812 if (!NT_STATUS_IS_OK(status)) {
813 d_fprintf(stderr, "Creating %s failed with %s\n",
814 argv[0], nt_errstr(status));
815 return -1;
818 d_printf("Created local group %s with RID %d\n", argv[0], rid);
820 return 0;
824 * Delete a local group
827 static int net_sam_deletelocalgroup(int argc, const char **argv)
829 DOM_SID sid;
830 enum lsa_SidType type;
831 const char *dom, *name;
832 NTSTATUS status;
834 if (argc != 1) {
835 d_fprintf(stderr, "usage: net sam deletelocalgroup <name>\n");
836 return -1;
839 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
840 &dom, &name, &sid, &type)) {
841 d_fprintf(stderr, "Could not find %s.\n", argv[0]);
842 return -1;
845 if (type != SID_NAME_ALIAS) {
846 d_fprintf(stderr, "%s is a %s, not a local group.\n", argv[0],
847 sid_type_lookup(type));
848 return -1;
851 status = pdb_delete_alias(&sid);
853 if (!NT_STATUS_IS_OK(status)) {
854 d_fprintf(stderr, "Deleting local group %s failed with %s\n",
855 argv[0], nt_errstr(status));
856 return -1;
859 d_printf("Deleted local group %s.\n", argv[0]);
861 return 0;
865 * Create a local group
868 static int net_sam_createbuiltingroup(int argc, const char **argv)
870 NTSTATUS status;
871 uint32 rid;
872 enum lsa_SidType type;
873 fstring groupname;
874 DOM_SID sid;
876 if (argc != 1) {
877 d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
878 return -1;
881 if (!winbind_ping()) {
882 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
883 "only works when winbind runs.\n");
884 return -1;
887 /* validate the name and get the group */
889 fstrcpy( groupname, "BUILTIN\\" );
890 fstrcat( groupname, argv[0] );
892 if ( !lookup_name(talloc_tos(), groupname, LOOKUP_NAME_ALL, NULL,
893 NULL, &sid, &type)) {
894 d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
895 return -1;
898 if ( !sid_peek_rid( &sid, &rid ) ) {
899 d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
900 return -1;
903 status = pdb_create_builtin_alias( rid );
905 if (!NT_STATUS_IS_OK(status)) {
906 d_fprintf(stderr, "Creating %s failed with %s\n",
907 argv[0], nt_errstr(status));
908 return -1;
911 d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
913 return 0;
917 * Add a group member
920 static int net_sam_addmem(int argc, const char **argv)
922 const char *groupdomain, *groupname, *memberdomain, *membername;
923 DOM_SID group, member;
924 enum lsa_SidType grouptype, membertype;
925 NTSTATUS status;
927 if (argc != 2) {
928 d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
929 return -1;
932 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
933 &groupdomain, &groupname, &group, &grouptype)) {
934 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
935 return -1;
938 /* check to see if the member to be added is a name or a SID */
940 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
941 &memberdomain, &membername, &member, &membertype))
943 /* try it as a SID */
945 if ( !string_to_sid( &member, argv[1] ) ) {
946 d_fprintf(stderr, "Could not find member %s\n", argv[1]);
947 return -1;
950 if ( !lookup_sid(talloc_tos(), &member, &memberdomain,
951 &membername, &membertype) )
953 d_fprintf(stderr, "Could not resolve SID %s\n", argv[1]);
954 return -1;
958 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
959 if ((membertype != SID_NAME_USER) &&
960 (membertype != SID_NAME_DOM_GRP)) {
961 d_fprintf(stderr, "%s is a local group, only users "
962 "and domain groups can be added.\n"
963 "%s is a %s\n", argv[0], argv[1],
964 sid_type_lookup(membertype));
965 return -1;
967 status = pdb_add_aliasmem(&group, &member);
969 if (!NT_STATUS_IS_OK(status)) {
970 d_fprintf(stderr, "Adding local group member failed "
971 "with %s\n", nt_errstr(status));
972 return -1;
974 } else {
975 d_fprintf(stderr, "Can only add members to local groups so "
976 "far, %s is a %s\n", argv[0],
977 sid_type_lookup(grouptype));
978 return -1;
981 d_printf("Added %s\\%s to %s\\%s\n", memberdomain, membername,
982 groupdomain, groupname);
984 return 0;
988 * Delete a group member
991 static int net_sam_delmem(int argc, const char **argv)
993 const char *groupdomain, *groupname;
994 const char *memberdomain = NULL;
995 const char *membername = NULL;
996 DOM_SID group, member;
997 enum lsa_SidType grouptype;
998 NTSTATUS status;
1000 if (argc != 2) {
1001 d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
1002 return -1;
1005 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1006 &groupdomain, &groupname, &group, &grouptype)) {
1007 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
1008 return -1;
1011 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1012 &memberdomain, &membername, &member, NULL)) {
1013 if (!string_to_sid(&member, argv[1])) {
1014 d_fprintf(stderr, "Could not find member %s\n",
1015 argv[1]);
1016 return -1;
1020 if ((grouptype == SID_NAME_ALIAS) ||
1021 (grouptype == SID_NAME_WKN_GRP)) {
1022 status = pdb_del_aliasmem(&group, &member);
1024 if (!NT_STATUS_IS_OK(status)) {
1025 d_fprintf(stderr, "Deleting local group member failed "
1026 "with %s\n", nt_errstr(status));
1027 return -1;
1029 } else {
1030 d_fprintf(stderr, "Can only delete members from local groups "
1031 "so far, %s is a %s\n", argv[0],
1032 sid_type_lookup(grouptype));
1033 return -1;
1036 if (membername != NULL) {
1037 d_printf("Deleted %s\\%s from %s\\%s\n",
1038 memberdomain, membername, groupdomain, groupname);
1039 } else {
1040 d_printf("Deleted %s from %s\\%s\n",
1041 sid_string_tos(&member), groupdomain, groupname);
1044 return 0;
1048 * List group members
1051 static int net_sam_listmem(int argc, const char **argv)
1053 const char *groupdomain, *groupname;
1054 DOM_SID group;
1055 enum lsa_SidType grouptype;
1056 NTSTATUS status;
1058 if (argc != 1) {
1059 d_fprintf(stderr, "usage: net sam listmem <group>\n");
1060 return -1;
1063 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1064 &groupdomain, &groupname, &group, &grouptype)) {
1065 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
1066 return -1;
1069 if ((grouptype == SID_NAME_ALIAS) ||
1070 (grouptype == SID_NAME_WKN_GRP)) {
1071 DOM_SID *members = NULL;
1072 size_t i, num_members = 0;
1074 status = pdb_enum_aliasmem(&group, &members, &num_members);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 d_fprintf(stderr, "Listing group members failed with "
1078 "%s\n", nt_errstr(status));
1079 return -1;
1082 d_printf("%s\\%s has %u members\n", groupdomain, groupname,
1083 (unsigned int)num_members);
1084 for (i=0; i<num_members; i++) {
1085 const char *dom, *name;
1086 if (lookup_sid(talloc_tos(), &members[i],
1087 &dom, &name, NULL)) {
1088 d_printf(" %s\\%s\n", dom, name);
1089 } else {
1090 d_printf(" %s\n", sid_string_tos(&members[i]));
1094 TALLOC_FREE(members);
1095 } else {
1096 d_fprintf(stderr, "Can only list local group members so far.\n"
1097 "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
1098 return -1;
1101 return 0;
1105 * Do the listing
1107 static int net_sam_do_list(int argc, const char **argv,
1108 struct pdb_search *search, const char *what)
1110 bool verbose = (argc == 1);
1112 if ((argc > 1) ||
1113 ((argc == 1) && !strequal(argv[0], "verbose"))) {
1114 d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
1115 return -1;
1118 if (search == NULL) {
1119 d_fprintf(stderr, "Could not start search\n");
1120 return -1;
1123 while (True) {
1124 struct samr_displayentry entry;
1125 if (!search->next_entry(search, &entry)) {
1126 break;
1128 if (verbose) {
1129 d_printf("%s:%d:%s\n",
1130 entry.account_name,
1131 entry.rid,
1132 entry.description);
1133 } else {
1134 d_printf("%s\n", entry.account_name);
1138 pdb_search_destroy(search);
1139 return 0;
1142 static int net_sam_list_users(int argc, const char **argv)
1144 return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
1145 "users");
1148 static int net_sam_list_groups(int argc, const char **argv)
1150 return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
1153 static int net_sam_list_localgroups(int argc, const char **argv)
1155 return net_sam_do_list(argc, argv,
1156 pdb_search_aliases(get_global_sam_sid()),
1157 "localgroups");
1160 static int net_sam_list_builtin(int argc, const char **argv)
1162 return net_sam_do_list(argc, argv,
1163 pdb_search_aliases(&global_sid_Builtin),
1164 "builtin");
1167 static int net_sam_list_workstations(int argc, const char **argv)
1169 return net_sam_do_list(argc, argv,
1170 pdb_search_users(ACB_WSTRUST),
1171 "workstations");
1175 * List stuff
1178 static int net_sam_list(int argc, const char **argv)
1180 struct functable2 func[] = {
1181 { "users", net_sam_list_users,
1182 "List SAM users" },
1183 { "groups", net_sam_list_groups,
1184 "List SAM groups" },
1185 { "localgroups", net_sam_list_localgroups,
1186 "List SAM local groups" },
1187 { "builtin", net_sam_list_builtin,
1188 "List builtin groups" },
1189 { "workstations", net_sam_list_workstations,
1190 "List domain member workstations" },
1191 {NULL, NULL}
1194 return net_run_function2(argc, argv, "net sam list", func);
1198 * Show details of SAM entries
1201 static int net_sam_show(int argc, const char **argv)
1203 DOM_SID sid;
1204 enum lsa_SidType type;
1205 const char *dom, *name;
1207 if (argc != 1) {
1208 d_fprintf(stderr, "usage: net sam show <name>\n");
1209 return -1;
1212 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1213 &dom, &name, &sid, &type)) {
1214 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
1215 return -1;
1218 d_printf("%s\\%s is a %s with SID %s\n", dom, name,
1219 sid_type_lookup(type), sid_string_tos(&sid));
1221 return 0;
1224 #ifdef HAVE_LDAP
1227 * Init an LDAP tree with default users and Groups
1228 * if ldapsam:editposix is enabled
1231 static int net_sam_provision(int argc, const char **argv)
1233 TALLOC_CTX *tc;
1234 char *ldap_bk;
1235 char *ldap_uri = NULL;
1236 char *p;
1237 struct smbldap_state *ls;
1238 GROUP_MAP gmap;
1239 DOM_SID gsid;
1240 gid_t domusers_gid = -1;
1241 gid_t domadmins_gid = -1;
1242 struct samu *samuser;
1243 struct passwd *pwd;
1245 tc = talloc_new(NULL);
1246 if (!tc) {
1247 d_fprintf(stderr, "Out of Memory!\n");
1248 return -1;
1251 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
1252 d_fprintf(stderr, "talloc failed\n");
1253 talloc_free(tc);
1254 return -1;
1256 p = strchr(ldap_bk, ':');
1257 if (p) {
1258 *p = 0;
1259 ldap_uri = talloc_strdup(tc, p+1);
1260 trim_char(ldap_uri, ' ', ' ');
1263 trim_char(ldap_bk, ' ', ' ');
1265 if (strcmp(ldap_bk, "ldapsam") != 0) {
1266 d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
1267 goto failed;
1270 if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
1271 !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
1273 d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
1274 " and ldapsam:editposix are enabled.\n");
1275 goto failed;
1278 if (!winbind_ping()) {
1279 d_fprintf(stderr, "winbind seems not to run. Provisioning "
1280 "LDAP only works when winbind runs.\n");
1281 goto failed;
1284 if (!NT_STATUS_IS_OK(smbldap_init(tc, NULL, ldap_uri, &ls))) {
1285 d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
1286 goto failed;
1289 d_printf("Checking for Domain Users group.\n");
1291 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
1293 if (!pdb_getgrsid(&gmap, gsid)) {
1294 LDAPMod **mods = NULL;
1295 char *dn;
1296 char *uname;
1297 char *wname;
1298 char *gidstr;
1299 char *gtype;
1300 int rc;
1302 d_printf("Adding the Domain Users group.\n");
1304 /* lets allocate a new groupid for this group */
1305 if (!winbind_allocate_gid(&domusers_gid)) {
1306 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
1307 goto domu_done;
1310 uname = talloc_strdup(tc, "domusers");
1311 wname = talloc_strdup(tc, "Domain Users");
1312 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1313 gidstr = talloc_asprintf(tc, "%d", domusers_gid);
1314 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1316 if (!uname || !wname || !dn || !gidstr || !gtype) {
1317 d_fprintf(stderr, "Out of Memory!\n");
1318 goto failed;
1321 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1322 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1323 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1324 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1325 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1326 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1327 sid_string_talloc(tc, &gsid));
1328 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1330 talloc_autofree_ldapmod(tc, mods);
1332 rc = smbldap_add(ls, dn, mods);
1334 if (rc != LDAP_SUCCESS) {
1335 d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
1337 } else {
1338 domusers_gid = gmap.gid;
1339 d_printf("found!\n");
1342 domu_done:
1344 d_printf("Checking for Domain Admins group.\n");
1346 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
1348 if (!pdb_getgrsid(&gmap, gsid)) {
1349 LDAPMod **mods = NULL;
1350 char *dn;
1351 char *uname;
1352 char *wname;
1353 char *gidstr;
1354 char *gtype;
1355 int rc;
1357 d_printf("Adding the Domain Admins group.\n");
1359 /* lets allocate a new groupid for this group */
1360 if (!winbind_allocate_gid(&domadmins_gid)) {
1361 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
1362 goto doma_done;
1365 uname = talloc_strdup(tc, "domadmins");
1366 wname = talloc_strdup(tc, "Domain Admins");
1367 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1368 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1369 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1371 if (!uname || !wname || !dn || !gidstr || !gtype) {
1372 d_fprintf(stderr, "Out of Memory!\n");
1373 goto failed;
1376 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1377 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1378 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1379 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1380 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1381 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1382 sid_string_talloc(tc, &gsid));
1383 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1385 talloc_autofree_ldapmod(tc, mods);
1387 rc = smbldap_add(ls, dn, mods);
1389 if (rc != LDAP_SUCCESS) {
1390 d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
1392 } else {
1393 domadmins_gid = gmap.gid;
1394 d_printf("found!\n");
1397 doma_done:
1399 d_printf("Check for Administrator account.\n");
1401 samuser = samu_new(tc);
1402 if (!samuser) {
1403 d_fprintf(stderr, "Out of Memory!\n");
1404 goto failed;
1407 if (!pdb_getsampwnam(samuser, "Administrator")) {
1408 LDAPMod **mods = NULL;
1409 DOM_SID sid;
1410 char *dn;
1411 char *name;
1412 char *uidstr;
1413 char *gidstr;
1414 char *shell;
1415 char *dir;
1416 uid_t uid;
1417 int rc;
1419 d_printf("Adding the Administrator user.\n");
1421 if (domadmins_gid == -1) {
1422 d_fprintf(stderr, "Can't create Administrator user, Domain Admins group not available!\n");
1423 goto done;
1425 if (!winbind_allocate_uid(&uid)) {
1426 d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
1427 goto done;
1429 name = talloc_strdup(tc, "Administrator");
1430 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1431 uidstr = talloc_asprintf(tc, "%d", uid);
1432 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1433 dir = talloc_sub_specified(tc, lp_template_homedir(),
1434 "Administrator",
1435 get_global_sam_name(),
1436 uid, domadmins_gid);
1437 shell = talloc_sub_specified(tc, lp_template_shell(),
1438 "Administrator",
1439 get_global_sam_name(),
1440 uid, domadmins_gid);
1442 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1443 d_fprintf(stderr, "Out of Memory!\n");
1444 goto failed;
1447 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1449 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1450 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1451 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1452 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1453 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1454 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1455 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1456 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1457 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1458 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1459 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1460 sid_string_talloc(tc, &sid));
1461 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1462 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1463 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1465 talloc_autofree_ldapmod(tc, mods);
1467 rc = smbldap_add(ls, dn, mods);
1469 if (rc != LDAP_SUCCESS) {
1470 d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
1472 } else {
1473 d_printf("found!\n");
1476 d_printf("Checking for Guest user.\n");
1478 samuser = samu_new(tc);
1479 if (!samuser) {
1480 d_fprintf(stderr, "Out of Memory!\n");
1481 goto failed;
1484 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1485 LDAPMod **mods = NULL;
1486 DOM_SID sid;
1487 char *dn;
1488 char *uidstr;
1489 char *gidstr;
1490 int rc;
1492 d_printf("Adding the Guest user.\n");
1494 pwd = getpwnam_alloc(tc, lp_guestaccount());
1496 if (!pwd) {
1497 if (domusers_gid == -1) {
1498 d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
1499 goto done;
1501 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1502 d_fprintf(stderr, "talloc failed\n");
1503 goto done;
1505 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1506 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1507 d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
1508 goto done;
1510 pwd->pw_gid = domusers_gid;
1511 pwd->pw_dir = talloc_strdup(tc, "/");
1512 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1513 if (!pwd->pw_dir || !pwd->pw_shell) {
1514 d_fprintf(stderr, "Out of Memory!\n");
1515 goto failed;
1519 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1521 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1522 uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
1523 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1524 if (!dn || !uidstr || !gidstr) {
1525 d_fprintf(stderr, "Out of Memory!\n");
1526 goto failed;
1529 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1530 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1531 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1532 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1533 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1534 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1535 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1536 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1537 if ((pwd->pw_dir != NULL) && (pwd->pw_dir[0] != '\0')) {
1538 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1540 if ((pwd->pw_shell != NULL) && (pwd->pw_shell[0] != '\0')) {
1541 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1543 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1544 sid_string_talloc(tc, &sid));
1545 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1546 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1547 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1549 talloc_autofree_ldapmod(tc, mods);
1551 rc = smbldap_add(ls, dn, mods);
1553 if (rc != LDAP_SUCCESS) {
1554 d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
1556 } else {
1557 d_printf("found!\n");
1560 d_printf("Checking Guest's group.\n");
1562 pwd = getpwnam_alloc(NULL, lp_guestaccount());
1563 if (!pwd) {
1564 d_fprintf(stderr, "Failed to find just created Guest account!\n"
1565 " Is nss properly configured?!\n");
1566 goto failed;
1569 if (pwd->pw_gid == domusers_gid) {
1570 d_printf("found!\n");
1571 goto done;
1574 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1575 LDAPMod **mods = NULL;
1576 char *dn;
1577 char *uname;
1578 char *wname;
1579 char *gidstr;
1580 char *gtype;
1581 int rc;
1583 d_printf("Adding the Domain Guests group.\n");
1585 uname = talloc_strdup(tc, "domguests");
1586 wname = talloc_strdup(tc, "Domain Guests");
1587 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1588 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1589 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1591 if (!uname || !wname || !dn || !gidstr || !gtype) {
1592 d_fprintf(stderr, "Out of Memory!\n");
1593 goto failed;
1596 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1598 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1599 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1600 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1601 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1602 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1603 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1604 sid_string_talloc(tc, &gsid));
1605 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1607 talloc_autofree_ldapmod(tc, mods);
1609 rc = smbldap_add(ls, dn, mods);
1611 if (rc != LDAP_SUCCESS) {
1612 d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
1614 } else {
1615 d_printf("found!\n");
1619 done:
1620 talloc_free(tc);
1621 return 0;
1623 failed:
1624 talloc_free(tc);
1625 return -1;
1628 #endif
1630 /***********************************************************
1631 migrated functionality from smbgroupedit
1632 **********************************************************/
1633 int net_sam(int argc, const char **argv)
1635 struct functable2 func[] = {
1636 { "createbuiltingroup", net_sam_createbuiltingroup,
1637 "Create a new BUILTIN group" },
1638 { "createlocalgroup", net_sam_createlocalgroup,
1639 "Create a new local group" },
1640 { "deletelocalgroup", net_sam_deletelocalgroup,
1641 "Delete an existing local group" },
1642 { "mapunixgroup", net_sam_mapunixgroup,
1643 "Map a unix group to a domain group" },
1644 { "unmapunixgroup", net_sam_unmapunixgroup,
1645 "Remove a group mapping of an unix group to a domain group" },
1646 { "addmem", net_sam_addmem,
1647 "Add a member to a group" },
1648 { "delmem", net_sam_delmem,
1649 "Delete a member from a group" },
1650 { "listmem", net_sam_listmem,
1651 "List group members" },
1652 { "list", net_sam_list,
1653 "List users, groups and local groups" },
1654 { "show", net_sam_show,
1655 "Show details of a SAM entry" },
1656 { "set", net_sam_set,
1657 "Set details of a SAM account" },
1658 { "policy", net_sam_policy,
1659 "Set account policies" },
1660 { "rights", net_sam_rights,
1661 "Manipulate user privileges" },
1662 #ifdef HAVE_LDAP
1663 { "provision", net_sam_provision,
1664 "Provision a clean User Database" },
1665 #endif
1666 { NULL, NULL, NULL }
1669 if (getuid() != 0) {
1670 d_fprintf(stderr, "You are not root, most things won't "
1671 "work\n");
1674 return net_run_function2(argc, argv, "net sam", func);