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/>.
22 #include "utils/net.h"
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;
34 enum lsa_SidType type;
35 const char *dom, *name;
39 d_fprintf(stderr, "usage: net sam set %s <user> <value>\n",
44 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
45 &dom, &name, &sid, &type)) {
46 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
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));
56 if ( !(sam_acct = samu_new( NULL )) ) {
57 d_fprintf(stderr, "Internal error\n");
61 if (!pdb_getsampwsid(sam_acct, &sid)) {
62 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
66 if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
67 d_fprintf(stderr, "Internal error\n");
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));
78 TALLOC_FREE(sam_acct);
80 d_printf("Updated %s for %s\\%s to %s\n", field, dom, name, argv[1]);
84 static int net_sam_set_fullname(int argc, const char **argv)
86 return net_sam_userset(argc, argv, "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",
108 static int net_sam_set_homedir(int argc, const char **argv)
110 return net_sam_userset(argc, argv, "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);
124 static int net_sam_set_userflag(int argc, const char **argv, const char *field,
127 struct samu *sam_acct = NULL;
129 enum lsa_SidType type;
130 const char *dom, *name;
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",
141 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
142 &dom, &name, &sid, &type)) {
143 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
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));
153 if ( !(sam_acct = samu_new( NULL )) ) {
154 d_fprintf(stderr, "Internal error\n");
158 if (!pdb_getsampwsid(sam_acct, &sid)) {
159 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
163 acct_flags = pdb_get_acct_ctrl(sam_acct);
165 if (strequal(argv[1], "yes")) {
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));
180 TALLOC_FREE(sam_acct);
182 d_fprintf(stderr, "Updated flag %s for %s\\%s to %s\n", field, dom,
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;
215 enum lsa_SidType type;
216 const char *dom, *name;
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");
225 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
226 &dom, &name, &sid, &type)) {
227 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
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));
237 if ( !(sam_acct = samu_new( NULL )) ) {
238 d_fprintf(stderr, "Internal error\n");
242 if (!pdb_getsampwsid(sam_acct, &sid)) {
243 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
247 if (strequal(argv[1], "yes")) {
248 pdb_set_pass_last_set_time(sam_acct, 0, PDB_CHANGED);
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));
260 TALLOC_FREE(sam_acct);
262 d_fprintf(stderr, "Updated 'user must change password at next logon' for %s\\%s to %s\n", dom,
269 * Set a user's or a group's comment
272 static int net_sam_set_comment(int argc, const char **argv)
276 enum lsa_SidType type;
277 const char *dom, *name;
281 d_fprintf(stderr, "usage: net sam set comment <name> "
286 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
287 &dom, &name, &sid, &type)) {
288 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
292 if (type == SID_NAME_USER) {
293 return net_sam_userset(argc, argv, "comment",
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));
304 if (!pdb_getgrsid(&map, sid)) {
305 d_fprintf(stderr, "Could not load group %s\n", argv[0]);
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));
319 d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
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" },
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, old_value;
370 d_fprintf(stderr, "usage: net sam policy set "
371 "\"<account policy>\" <value> \n");
375 account_policy = argv[0];
376 field = account_policy_name_to_fieldnum(account_policy);
378 if (strequal(argv[1], "forever") || strequal(argv[1], "never")
379 || strequal(argv[1], "off")) {
383 value = strtoul(argv[1], &endptr, 10);
385 if ((endptr == argv[1]) || (endptr[0] != '\0')) {
386 d_printf("Unable to set policy \"%s\"! Invalid value "
388 account_policy, argv[1]);
397 account_policy_names_list(&names, &count);
398 d_fprintf(stderr, "No account policy \"%s\"!\n\n", argv[0]);
399 d_fprintf(stderr, "Valid account policies are:\n");
401 for (i=0; i<count; i++) {
402 d_fprintf(stderr, "%s\n", names[i]);
409 if (!pdb_get_account_policy(field, &old_value)) {
410 d_fprintf(stderr, "Valid account policy, but unable to fetch "
414 if (!pdb_set_account_policy(field, value)) {
415 d_fprintf(stderr, "Valid account policy, but unable to "
420 d_printf("Account policy \"%s\" value was: %d\n", account_policy,
423 d_printf("Account policy \"%s\" value is now: %d\n", account_policy,
428 static int net_sam_policy_show(int argc, const char **argv)
430 const char *account_policy = NULL;
435 d_fprintf(stderr, "usage: net sam policy show"
436 " \"<account policy>\" \n");
440 account_policy = argv[0];
441 field = account_policy_name_to_fieldnum(account_policy);
447 account_policy_names_list(&names, &count);
448 d_fprintf(stderr, "No account policy by that name!\n");
450 d_fprintf(stderr, "Valid account policies "
452 for (i=0; i<count; i++) {
453 d_fprintf(stderr, "%s\n", names[i]);
460 if (!pdb_get_account_policy(field, &old_value)) {
461 fprintf(stderr, "Valid account policy, but unable to "
466 printf("Account policy \"%s\" description: %s\n",
467 account_policy, account_policy_get_desc(field));
468 printf("Account policy \"%s\" value is: %d\n", account_policy,
473 static int net_sam_policy_list(int argc, const char **argv)
478 account_policy_names_list(&names, &count);
480 d_fprintf(stderr, "Valid account policies "
482 for (i = 0; i < count ; i++) {
483 d_fprintf(stderr, "%s\n", names[i]);
490 static int net_sam_policy(int argc, const char **argv)
492 struct functable2 func[] = {
493 { "list", net_sam_policy_list,
494 "List account policies" },
495 { "show", net_sam_policy_show,
496 "Show account policies" },
497 { "set", net_sam_policy_set,
498 "Change account policies" },
502 return net_run_function2(argc, argv, "net sam policy", func);
506 * Map a unix group to a domain group
509 static NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
513 const char *grpname, *dom, *name;
516 if (pdb_getgrgid(&map, grp->gr_gid)) {
517 return NT_STATUS_GROUP_EXISTS;
520 map.gid = grp->gr_gid;
521 grpname = grp->gr_name;
523 if (lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
524 &dom, &name, NULL, NULL)) {
526 const char *tmp = talloc_asprintf(
527 tmp_talloc_ctx(), "Unix Group %s", grp->gr_name);
529 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
530 grpname, dom, name, tmp));
534 if (lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
535 NULL, NULL, NULL, NULL)) {
536 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
537 return NT_STATUS_GROUP_EXISTS;
540 fstrcpy(map.nt_name, grpname);
542 if (pdb_rid_algorithm()) {
543 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
545 if (!pdb_new_rid(&rid)) {
546 DEBUG(3, ("Could not get a new RID for %s\n",
548 return NT_STATUS_ACCESS_DENIED;
552 sid_compose(&map.sid, get_global_sam_sid(), rid);
553 map.sid_name_use = SID_NAME_DOM_GRP;
554 fstrcpy(map.comment, talloc_asprintf(tmp_talloc_ctx(), "Unix Group %s",
557 status = pdb_add_group_mapping_entry(&map);
558 if (NT_STATUS_IS_OK(status)) {
564 static int net_sam_mapunixgroup(int argc, const char **argv)
571 d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
575 grp = getgrnam(argv[0]);
577 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
581 status = map_unix_group(grp, &map);
583 if (!NT_STATUS_IS_OK(status)) {
584 d_fprintf(stderr, "Mapping group %s failed with %s\n",
585 argv[0], nt_errstr(status));
589 d_printf("Mapped unix group %s to SID %s\n", argv[0],
590 sid_string_static(&map.sid));
596 * Remove a group mapping
599 static NTSTATUS unmap_unix_group(const struct group *grp, GROUP_MAP *pmap)
606 map.gid = grp->gr_gid;
607 grpname = grp->gr_name;
609 if (!lookup_name(tmp_talloc_ctx(), grpname, LOOKUP_NAME_ISOLATED,
610 NULL, NULL, NULL, NULL)) {
611 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp->gr_name));
612 return NT_STATUS_NO_SUCH_GROUP;
615 fstrcpy(map.nt_name, grpname);
617 if (!pdb_gid_to_sid(map.gid, &dom_sid)) {
618 return NT_STATUS_UNSUCCESSFUL;
621 status = pdb_delete_group_mapping_entry(dom_sid);
626 static int net_sam_unmapunixgroup(int argc, const char **argv)
633 d_fprintf(stderr, "usage: net sam unmapunixgroup <name>\n");
637 grp = getgrnam(argv[0]);
639 d_fprintf(stderr, "Could not find mapping for group %s.\n", argv[0]);
643 status = unmap_unix_group(grp, &map);
645 if (!NT_STATUS_IS_OK(status)) {
646 d_fprintf(stderr, "Unmapping group %s failed with %s.\n",
647 argv[0], nt_errstr(status));
651 d_printf("Unmapped unix group %s.\n", argv[0]);
657 * Create a local group
660 static int net_sam_createlocalgroup(int argc, const char **argv)
666 d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
670 if (!winbind_ping()) {
671 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
672 "only works when winbind runs.\n");
676 status = pdb_create_alias(argv[0], &rid);
678 if (!NT_STATUS_IS_OK(status)) {
679 d_fprintf(stderr, "Creating %s failed with %s\n",
680 argv[0], nt_errstr(status));
684 d_printf("Created local group %s with RID %d\n", argv[0], rid);
690 * Delete a local group
693 static int net_sam_deletelocalgroup(int argc, const char **argv)
696 enum lsa_SidType type;
697 const char *dom, *name;
701 d_fprintf(stderr, "usage: net sam deletelocalgroup <name>\n");
705 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
706 &dom, &name, &sid, &type)) {
707 d_fprintf(stderr, "Could not find %s.\n", argv[0]);
711 if (type != SID_NAME_ALIAS) {
712 d_fprintf(stderr, "%s is a %s, not a local group.\n", argv[0],
713 sid_type_lookup(type));
717 status = pdb_delete_alias(&sid);
719 if (!NT_STATUS_IS_OK(status)) {
720 d_fprintf(stderr, "Deleting local group %s failed with %s\n",
721 argv[0], nt_errstr(status));
725 d_printf("Deleted local group %s.\n", argv[0]);
731 * Create a local group
734 static int net_sam_createbuiltingroup(int argc, const char **argv)
738 enum lsa_SidType type;
743 d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
747 if (!winbind_ping()) {
748 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
749 "only works when winbind runs.\n");
753 /* validate the name and get the group */
755 fstrcpy( groupname, "BUILTIN\\" );
756 fstrcat( groupname, argv[0] );
758 if ( !lookup_name(tmp_talloc_ctx(), groupname, LOOKUP_NAME_ALL, NULL,
759 NULL, &sid, &type)) {
760 d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
764 if ( !sid_peek_rid( &sid, &rid ) ) {
765 d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
769 status = pdb_create_builtin_alias( rid );
771 if (!NT_STATUS_IS_OK(status)) {
772 d_fprintf(stderr, "Creating %s failed with %s\n",
773 argv[0], nt_errstr(status));
777 d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
786 static int net_sam_addmem(int argc, const char **argv)
788 const char *groupdomain, *groupname, *memberdomain, *membername;
789 DOM_SID group, member;
790 enum lsa_SidType grouptype, membertype;
794 d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
798 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
799 &groupdomain, &groupname, &group, &grouptype)) {
800 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
804 /* check to see if the member to be added is a name or a SID */
806 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
807 &memberdomain, &membername, &member, &membertype))
809 /* try it as a SID */
811 if ( !string_to_sid( &member, argv[1] ) ) {
812 d_fprintf(stderr, "Could not find member %s\n", argv[1]);
816 if ( !lookup_sid(tmp_talloc_ctx(), &member, &memberdomain,
817 &membername, &membertype) )
819 d_fprintf(stderr, "Could not resolve SID %s\n", argv[1]);
824 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
825 if ((membertype != SID_NAME_USER) &&
826 (membertype != SID_NAME_DOM_GRP)) {
827 d_fprintf(stderr, "%s is a local group, only users "
828 "and domain groups can be added.\n"
829 "%s is a %s\n", argv[0], argv[1],
830 sid_type_lookup(membertype));
833 status = pdb_add_aliasmem(&group, &member);
835 if (!NT_STATUS_IS_OK(status)) {
836 d_fprintf(stderr, "Adding local group member failed "
837 "with %s\n", nt_errstr(status));
841 d_fprintf(stderr, "Can only add members to local groups so "
842 "far, %s is a %s\n", argv[0],
843 sid_type_lookup(grouptype));
847 d_printf("Added %s\\%s to %s\\%s\n", memberdomain, membername,
848 groupdomain, groupname);
854 * Delete a group member
857 static int net_sam_delmem(int argc, const char **argv)
859 const char *groupdomain, *groupname;
860 const char *memberdomain = NULL;
861 const char *membername = NULL;
862 DOM_SID group, member;
863 enum lsa_SidType grouptype;
867 d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
871 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
872 &groupdomain, &groupname, &group, &grouptype)) {
873 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
877 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
878 &memberdomain, &membername, &member, NULL)) {
879 if (!string_to_sid(&member, argv[1])) {
880 d_fprintf(stderr, "Could not find member %s\n",
886 if ((grouptype == SID_NAME_ALIAS) ||
887 (grouptype == SID_NAME_WKN_GRP)) {
888 status = pdb_del_aliasmem(&group, &member);
890 if (!NT_STATUS_IS_OK(status)) {
891 d_fprintf(stderr, "Deleting local group member failed "
892 "with %s\n", nt_errstr(status));
896 d_fprintf(stderr, "Can only delete members from local groups "
897 "so far, %s is a %s\n", argv[0],
898 sid_type_lookup(grouptype));
902 if (membername != NULL) {
903 d_printf("Deleted %s\\%s from %s\\%s\n",
904 memberdomain, membername, groupdomain, groupname);
906 d_printf("Deleted %s from %s\\%s\n",
907 sid_string_static(&member), groupdomain, groupname);
917 static int net_sam_listmem(int argc, const char **argv)
919 const char *groupdomain, *groupname;
921 enum lsa_SidType grouptype;
925 d_fprintf(stderr, "usage: net sam listmem <group>\n");
929 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
930 &groupdomain, &groupname, &group, &grouptype)) {
931 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
935 if ((grouptype == SID_NAME_ALIAS) ||
936 (grouptype == SID_NAME_WKN_GRP)) {
937 DOM_SID *members = NULL;
938 size_t i, num_members = 0;
940 status = pdb_enum_aliasmem(&group, &members, &num_members);
942 if (!NT_STATUS_IS_OK(status)) {
943 d_fprintf(stderr, "Listing group members failed with "
944 "%s\n", nt_errstr(status));
948 d_printf("%s\\%s has %u members\n", groupdomain, groupname,
949 (unsigned int)num_members);
950 for (i=0; i<num_members; i++) {
951 const char *dom, *name;
952 if (lookup_sid(tmp_talloc_ctx(), &members[i],
953 &dom, &name, NULL)) {
954 d_printf(" %s\\%s\n", dom, name);
957 sid_string_static(&members[i]));
961 TALLOC_FREE(members);
963 d_fprintf(stderr, "Can only list local group members so far.\n"
964 "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
974 static int net_sam_do_list(int argc, const char **argv,
975 struct pdb_search *search, const char *what)
977 BOOL verbose = (argc == 1);
980 ((argc == 1) && !strequal(argv[0], "verbose"))) {
981 d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
985 if (search == NULL) {
986 d_fprintf(stderr, "Could not start search\n");
991 struct samr_displayentry entry;
992 if (!search->next_entry(search, &entry)) {
996 d_printf("%s:%d:%s\n",
1001 d_printf("%s\n", entry.account_name);
1005 search->search_end(search);
1009 static int net_sam_list_users(int argc, const char **argv)
1011 return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
1015 static int net_sam_list_groups(int argc, const char **argv)
1017 return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
1020 static int net_sam_list_localgroups(int argc, const char **argv)
1022 return net_sam_do_list(argc, argv,
1023 pdb_search_aliases(get_global_sam_sid()),
1027 static int net_sam_list_builtin(int argc, const char **argv)
1029 return net_sam_do_list(argc, argv,
1030 pdb_search_aliases(&global_sid_Builtin),
1034 static int net_sam_list_workstations(int argc, const char **argv)
1036 return net_sam_do_list(argc, argv,
1037 pdb_search_users(ACB_WSTRUST),
1045 static int net_sam_list(int argc, const char **argv)
1047 struct functable2 func[] = {
1048 { "users", net_sam_list_users,
1050 { "groups", net_sam_list_groups,
1051 "List SAM groups" },
1052 { "localgroups", net_sam_list_localgroups,
1053 "List SAM local groups" },
1054 { "builtin", net_sam_list_builtin,
1055 "List builtin groups" },
1056 { "workstations", net_sam_list_workstations,
1057 "List domain member workstations" },
1061 return net_run_function2(argc, argv, "net sam list", func);
1065 * Show details of SAM entries
1068 static int net_sam_show(int argc, const char **argv)
1071 enum lsa_SidType type;
1072 const char *dom, *name;
1075 d_fprintf(stderr, "usage: net sam show <name>\n");
1079 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
1080 &dom, &name, &sid, &type)) {
1081 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
1085 d_printf("%s\\%s is a %s with SID %s\n", dom, name,
1086 sid_type_lookup(type), sid_string_static(&sid));
1094 * Init an LDAP tree with default users and Groups
1095 * if ldapsam:editposix is enabled
1098 static int net_sam_provision(int argc, const char **argv)
1102 char *ldap_uri = NULL;
1104 struct smbldap_state *ls;
1107 gid_t domusers_gid = -1;
1108 gid_t domadmins_gid = -1;
1109 struct samu *samuser;
1112 tc = talloc_new(NULL);
1114 d_fprintf(stderr, "Out of Memory!\n");
1118 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
1119 d_fprintf(stderr, "talloc failed\n");
1123 p = strchr(ldap_bk, ':');
1126 ldap_uri = talloc_strdup(tc, p+1);
1127 trim_char(ldap_uri, ' ', ' ');
1130 trim_char(ldap_bk, ' ', ' ');
1132 if (strcmp(ldap_bk, "ldapsam") != 0) {
1133 d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
1137 if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
1138 !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
1140 d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
1141 " and ldapsam:editposix are enabled.\n");
1145 if (!winbind_ping()) {
1146 d_fprintf(stderr, "winbind seems not to run. Provisioning "
1147 "LDAP only works when winbind runs.\n");
1151 if (!NT_STATUS_IS_OK(smbldap_init(tc, NULL, ldap_uri, &ls))) {
1152 d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
1156 d_printf("Checking for Domain Users group.\n");
1158 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
1160 if (!pdb_getgrsid(&gmap, gsid)) {
1161 LDAPMod **mods = NULL;
1169 d_printf("Adding the Domain Users group.\n");
1171 /* lets allocate a new groupid for this group */
1172 if (!winbind_allocate_gid(&domusers_gid)) {
1173 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
1177 uname = talloc_strdup(tc, "domusers");
1178 wname = talloc_strdup(tc, "Domain Users");
1179 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1180 gidstr = talloc_asprintf(tc, "%d", domusers_gid);
1181 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1183 if (!uname || !wname || !dn || !gidstr || !gtype) {
1184 d_fprintf(stderr, "Out of Memory!\n");
1188 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1189 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1190 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1191 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1192 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1193 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1194 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1196 talloc_autofree_ldapmod(tc, mods);
1198 rc = smbldap_add(ls, dn, mods);
1200 if (rc != LDAP_SUCCESS) {
1201 d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
1204 domusers_gid = gmap.gid;
1205 d_printf("found!\n");
1210 d_printf("Checking for Domain Admins group.\n");
1212 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
1214 if (!pdb_getgrsid(&gmap, gsid)) {
1215 LDAPMod **mods = NULL;
1223 d_printf("Adding the Domain Admins group.\n");
1225 /* lets allocate a new groupid for this group */
1226 if (!winbind_allocate_gid(&domadmins_gid)) {
1227 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
1231 uname = talloc_strdup(tc, "domadmins");
1232 wname = talloc_strdup(tc, "Domain Admins");
1233 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1234 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1235 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1237 if (!uname || !wname || !dn || !gidstr || !gtype) {
1238 d_fprintf(stderr, "Out of Memory!\n");
1242 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1243 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1244 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1245 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1246 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1247 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1248 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1250 talloc_autofree_ldapmod(tc, mods);
1252 rc = smbldap_add(ls, dn, mods);
1254 if (rc != LDAP_SUCCESS) {
1255 d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
1258 domadmins_gid = gmap.gid;
1259 d_printf("found!\n");
1264 d_printf("Check for Administrator account.\n");
1266 samuser = samu_new(tc);
1268 d_fprintf(stderr, "Out of Memory!\n");
1272 if (!pdb_getsampwnam(samuser, "Administrator")) {
1273 LDAPMod **mods = NULL;
1284 d_printf("Adding the Administrator user.\n");
1286 if (domadmins_gid == -1) {
1287 d_fprintf(stderr, "Can't create Administrator user, Domain Admins group not available!\n");
1290 if (!winbind_allocate_uid(&uid)) {
1291 d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
1294 name = talloc_strdup(tc, "Administrator");
1295 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1296 uidstr = talloc_asprintf(tc, "%d", uid);
1297 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1298 dir = talloc_sub_specified(tc, lp_template_homedir(),
1300 get_global_sam_name(),
1301 uid, domadmins_gid);
1302 shell = talloc_sub_specified(tc, lp_template_shell(),
1304 get_global_sam_name(),
1305 uid, domadmins_gid);
1307 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1308 d_fprintf(stderr, "Out of Memory!\n");
1312 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1314 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1315 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1316 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1317 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1318 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1319 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1320 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1321 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1322 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1323 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1324 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1325 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1326 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1327 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1329 talloc_autofree_ldapmod(tc, mods);
1331 rc = smbldap_add(ls, dn, mods);
1333 if (rc != LDAP_SUCCESS) {
1334 d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
1337 d_printf("found!\n");
1340 d_printf("Checking for Guest user.\n");
1342 samuser = samu_new(tc);
1344 d_fprintf(stderr, "Out of Memory!\n");
1348 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1349 LDAPMod **mods = NULL;
1356 d_printf("Adding the Guest user.\n");
1358 pwd = getpwnam_alloc(tc, lp_guestaccount());
1361 if (domusers_gid == -1) {
1362 d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
1365 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1366 d_fprintf(stderr, "talloc failed\n");
1369 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1370 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1371 d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
1374 pwd->pw_gid = domusers_gid;
1375 pwd->pw_dir = talloc_strdup(tc, "/");
1376 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1377 if (!pwd->pw_dir || !pwd->pw_shell) {
1378 d_fprintf(stderr, "Out of Memory!\n");
1383 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1385 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1386 uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
1387 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1388 if (!dn || !uidstr || !gidstr) {
1389 d_fprintf(stderr, "Out of Memory!\n");
1393 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1394 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1395 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1396 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1397 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1398 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1399 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1400 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1401 if ((pwd->pw_dir != NULL) && (pwd->pw_dir[0] != '\0')) {
1402 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1404 if ((pwd->pw_shell != NULL) && (pwd->pw_shell[0] != '\0')) {
1405 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1407 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1408 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1409 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1410 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1412 talloc_autofree_ldapmod(tc, mods);
1414 rc = smbldap_add(ls, dn, mods);
1416 if (rc != LDAP_SUCCESS) {
1417 d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
1420 d_printf("found!\n");
1423 d_printf("Checking Guest's group.\n");
1425 pwd = getpwnam_alloc(NULL, lp_guestaccount());
1427 d_fprintf(stderr, "Failed to find just created Guest account!\n"
1428 " Is nss properly configured?!\n");
1432 if (pwd->pw_gid == domusers_gid) {
1433 d_printf("found!\n");
1437 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1438 LDAPMod **mods = NULL;
1446 d_printf("Adding the Domain Guests group.\n");
1448 uname = talloc_strdup(tc, "domguests");
1449 wname = talloc_strdup(tc, "Domain Guests");
1450 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1451 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1452 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1454 if (!uname || !wname || !dn || !gidstr || !gtype) {
1455 d_fprintf(stderr, "Out of Memory!\n");
1459 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1461 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1462 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1463 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1464 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1465 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1466 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1467 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1469 talloc_autofree_ldapmod(tc, mods);
1471 rc = smbldap_add(ls, dn, mods);
1473 if (rc != LDAP_SUCCESS) {
1474 d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
1477 d_printf("found!\n");
1492 /***********************************************************
1493 migrated functionality from smbgroupedit
1494 **********************************************************/
1495 int net_sam(int argc, const char **argv)
1497 struct functable2 func[] = {
1498 { "createbuiltingroup", net_sam_createbuiltingroup,
1499 "Create a new BUILTIN group" },
1500 { "createlocalgroup", net_sam_createlocalgroup,
1501 "Create a new local group" },
1502 { "deletelocalgroup", net_sam_deletelocalgroup,
1503 "Delete an existing local group" },
1504 { "mapunixgroup", net_sam_mapunixgroup,
1505 "Map a unix group to a domain group" },
1506 { "unmapunixgroup", net_sam_unmapunixgroup,
1507 "Remove a group mapping of an unix group to a domain group" },
1508 { "addmem", net_sam_addmem,
1509 "Add a member to a group" },
1510 { "delmem", net_sam_delmem,
1511 "Delete a member from a group" },
1512 { "listmem", net_sam_listmem,
1513 "List group members" },
1514 { "list", net_sam_list,
1515 "List users, groups and local groups" },
1516 { "show", net_sam_show,
1517 "Show details of a SAM entry" },
1518 { "set", net_sam_set,
1519 "Set details of a SAM account" },
1520 { "policy", net_sam_policy,
1521 "Set account policies" },
1523 { "provision", net_sam_provision,
1524 "Provision a clean User Database" },
1526 { NULL, NULL, NULL }
1529 /* we shouldn't have silly checks like this */
1530 if (getuid() != 0) {
1531 d_fprintf(stderr, "You must be root to edit the SAM "
1536 return net_run_function2(argc, argv, "net sam", func);