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 2 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "utils/net.h"
29 static int net_sam_userset(int argc, const char **argv, const char *field,
30 BOOL (*fn)(struct samu *, const char *,
31 enum pdb_value_state))
33 struct samu *sam_acct = NULL;
35 enum lsa_SidType type;
36 const char *dom, *name;
40 d_fprintf(stderr, "usage: net sam set %s <user> <value>\n",
45 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
46 &dom, &name, &sid, &type)) {
47 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
51 if (type != SID_NAME_USER) {
52 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
53 sid_type_lookup(type));
57 if ( !(sam_acct = samu_new( NULL )) ) {
58 d_fprintf(stderr, "Internal error\n");
62 if (!pdb_getsampwsid(sam_acct, &sid)) {
63 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
67 if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
68 d_fprintf(stderr, "Internal error\n");
72 status = pdb_update_sam_account(sam_acct);
73 if (!NT_STATUS_IS_OK(status)) {
74 d_fprintf(stderr, "Updating sam account %s failed with %s\n",
75 argv[0], nt_errstr(status));
79 TALLOC_FREE(sam_acct);
81 d_printf("Updated %s for %s\\%s to %s\n", field, dom, name, argv[1]);
85 static int net_sam_set_fullname(int argc, const char **argv)
87 return net_sam_userset(argc, argv, "fullname",
91 static int net_sam_set_logonscript(int argc, const char **argv)
93 return net_sam_userset(argc, argv, "logonscript",
94 pdb_set_logon_script);
97 static int net_sam_set_profilepath(int argc, const char **argv)
99 return net_sam_userset(argc, argv, "profilepath",
100 pdb_set_profile_path);
103 static int net_sam_set_homedrive(int argc, const char **argv)
105 return net_sam_userset(argc, argv, "homedrive",
109 static int net_sam_set_homedir(int argc, const char **argv)
111 return net_sam_userset(argc, argv, "homedir",
115 static int net_sam_set_workstations(int argc, const char **argv)
117 return net_sam_userset(argc, argv, "workstations",
118 pdb_set_workstations);
125 static int net_sam_set_userflag(int argc, const char **argv, const char *field,
128 struct samu *sam_acct = NULL;
130 enum lsa_SidType type;
131 const char *dom, *name;
135 if ((argc != 2) || (!strequal(argv[1], "yes") &&
136 !strequal(argv[1], "no"))) {
137 d_fprintf(stderr, "usage: net sam set %s <user> [yes|no]\n",
142 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
143 &dom, &name, &sid, &type)) {
144 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
148 if (type != SID_NAME_USER) {
149 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
150 sid_type_lookup(type));
154 if ( !(sam_acct = samu_new( NULL )) ) {
155 d_fprintf(stderr, "Internal error\n");
159 if (!pdb_getsampwsid(sam_acct, &sid)) {
160 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
164 acct_flags = pdb_get_acct_ctrl(sam_acct);
166 if (strequal(argv[1], "yes")) {
172 pdb_set_acct_ctrl(sam_acct, acct_flags, PDB_CHANGED);
174 status = pdb_update_sam_account(sam_acct);
175 if (!NT_STATUS_IS_OK(status)) {
176 d_fprintf(stderr, "Updating sam account %s failed with %s\n",
177 argv[0], nt_errstr(status));
181 TALLOC_FREE(sam_acct);
183 d_fprintf(stderr, "Updated flag %s for %s\\%s to %s\n", field, dom,
188 static int net_sam_set_disabled(int argc, const char **argv)
190 return net_sam_set_userflag(argc, argv, "disabled", ACB_DISABLED);
193 static int net_sam_set_pwnotreq(int argc, const char **argv)
195 return net_sam_set_userflag(argc, argv, "pwnotreq", ACB_PWNOTREQ);
198 static int net_sam_set_autolock(int argc, const char **argv)
200 return net_sam_set_userflag(argc, argv, "autolock", ACB_AUTOLOCK);
203 static int net_sam_set_pwnoexp(int argc, const char **argv)
205 return net_sam_set_userflag(argc, argv, "pwnoexp", ACB_PWNOEXP);
209 * Set pass last change time, based on force pass change now
212 static int net_sam_set_pwdmustchangenow(int argc, const char **argv)
214 struct samu *sam_acct = NULL;
216 enum lsa_SidType type;
217 const char *dom, *name;
220 if ((argc != 2) || (!strequal(argv[1], "yes") &&
221 !strequal(argv[1], "no"))) {
222 d_fprintf(stderr, "usage: net sam set pwdmustchangenow <user> [yes|no]\n");
226 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
227 &dom, &name, &sid, &type)) {
228 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
232 if (type != SID_NAME_USER) {
233 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
234 sid_type_lookup(type));
238 if ( !(sam_acct = samu_new( NULL )) ) {
239 d_fprintf(stderr, "Internal error\n");
243 if (!pdb_getsampwsid(sam_acct, &sid)) {
244 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
248 if (strequal(argv[1], "yes")) {
249 pdb_set_pass_last_set_time(sam_acct, 0, PDB_CHANGED);
251 pdb_set_pass_last_set_time(sam_acct, time(NULL), PDB_CHANGED);
254 status = pdb_update_sam_account(sam_acct);
255 if (!NT_STATUS_IS_OK(status)) {
256 d_fprintf(stderr, "Updating sam account %s failed with %s\n",
257 argv[0], nt_errstr(status));
261 TALLOC_FREE(sam_acct);
263 d_fprintf(stderr, "Updated 'user must change password at next logon' for %s\\%s to %s\n", dom,
270 * Set a user's or a group's comment
273 static int net_sam_set_comment(int argc, const char **argv)
277 enum lsa_SidType type;
278 const char *dom, *name;
282 d_fprintf(stderr, "usage: net sam set comment <name> "
287 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
288 &dom, &name, &sid, &type)) {
289 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
293 if (type == SID_NAME_USER) {
294 return net_sam_userset(argc, argv, "comment",
298 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
299 (type != SID_NAME_WKN_GRP)) {
300 d_fprintf(stderr, "%s is a %s, not a group\n", argv[0],
301 sid_type_lookup(type));
305 if (!pdb_getgrsid(&map, sid)) {
306 d_fprintf(stderr, "Could not load group %s\n", argv[0]);
310 fstrcpy(map.comment, argv[1]);
312 status = pdb_update_group_mapping_entry(&map);
314 if (!NT_STATUS_IS_OK(status)) {
315 d_fprintf(stderr, "Updating group mapping entry failed with "
316 "%s\n", nt_errstr(status));
320 d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
326 static int net_sam_set(int argc, const char **argv)
328 struct functable2 func[] = {
329 { "homedir", net_sam_set_homedir,
330 "Change a user's home directory" },
331 { "profilepath", net_sam_set_profilepath,
332 "Change a user's profile path" },
333 { "comment", net_sam_set_comment,
334 "Change a users or groups description" },
335 { "fullname", net_sam_set_fullname,
336 "Change a user's full name" },
337 { "logonscript", net_sam_set_logonscript,
338 "Change a user's logon script" },
339 { "homedrive", net_sam_set_homedrive,
340 "Change a user's home drive" },
341 { "workstations", net_sam_set_workstations,
342 "Change a user's allowed workstations" },
343 { "disabled", net_sam_set_disabled,
344 "Disable/Enable a user" },
345 { "pwnotreq", net_sam_set_pwnotreq,
346 "Disable/Enable the password not required flag" },
347 { "autolock", net_sam_set_autolock,
348 "Disable/Enable a user's lockout flag" },
349 { "pwnoexp", net_sam_set_pwnoexp,
350 "Disable/Enable whether a user's pw does not expire" },
351 { "pwdmustchangenow", net_sam_set_pwdmustchangenow,
352 "Force users password must change at next logon" },
356 return net_run_function2(argc, argv, "net sam set", func);
360 * Manage account policies
363 static int net_sam_policy_set(int argc, const char **argv)
365 const char *account_policy = NULL;
366 uint32 value, old_value;
371 d_fprintf(stderr, "usage: net sam policy set "
372 "\"<account policy>\" <value> \n");
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")) {
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 "
389 account_policy, argv[1]);
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]);
410 if (!pdb_get_account_policy(field, &old_value)) {
411 d_fprintf(stderr, "Valid account policy, but unable to fetch "
415 if (!pdb_set_account_policy(field, value)) {
416 d_fprintf(stderr, "Valid account policy, but unable to "
421 d_printf("Account policy \"%s\" value was: %d\n", account_policy,
424 d_printf("Account policy \"%s\" value is now: %d\n", account_policy,
429 static int net_sam_policy_show(int argc, const char **argv)
431 const char *account_policy = NULL;
436 d_fprintf(stderr, "usage: net sam policy show"
437 " \"<account policy>\" \n");
441 account_policy = argv[0];
442 field = account_policy_name_to_fieldnum(account_policy);
448 account_policy_names_list(&names, &count);
449 d_fprintf(stderr, "No account policy by that name!\n");
451 d_fprintf(stderr, "Valid account policies "
453 for (i=0; i<count; i++) {
454 d_fprintf(stderr, "%s\n", names[i]);
461 if (!pdb_get_account_policy(field, &old_value)) {
462 fprintf(stderr, "Valid account policy, but unable to "
467 printf("Account policy \"%s\" description: %s\n",
468 account_policy, account_policy_get_desc(field));
469 printf("Account policy \"%s\" value is: %d\n", account_policy,
474 static int net_sam_policy_list(int argc, const char **argv)
479 account_policy_names_list(&names, &count);
481 d_fprintf(stderr, "Valid account policies "
483 for (i = 0; i < count ; i++) {
484 d_fprintf(stderr, "%s\n", names[i]);
491 static int net_sam_policy(int argc, const char **argv)
493 struct functable2 func[] = {
494 { "list", net_sam_policy_list,
495 "List account policies" },
496 { "show", net_sam_policy_show,
497 "Show account policies" },
498 { "set", net_sam_policy_set,
499 "Change account policies" },
503 return net_run_function2(argc, argv, "net sam policy", func);
507 * Map a unix group to a domain group
510 static int net_sam_mapunixgroup(int argc, const char **argv)
517 d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
521 grp = getgrnam(argv[0]);
523 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
527 status = map_unix_group(grp, &map);
529 if (!NT_STATUS_IS_OK(status)) {
530 d_fprintf(stderr, "Mapping group %s failed with %s\n",
531 argv[0], nt_errstr(status));
535 d_printf("Mapped unix group %s to SID %s\n", argv[0],
536 sid_string_static(&map.sid));
542 * Create a local group
545 static int net_sam_createlocalgroup(int argc, const char **argv)
551 d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
555 if (!winbind_ping()) {
556 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
557 "only works when winbind runs.\n");
561 status = pdb_create_alias(argv[0], &rid);
563 if (!NT_STATUS_IS_OK(status)) {
564 d_fprintf(stderr, "Creating %s failed with %s\n",
565 argv[0], nt_errstr(status));
569 d_printf("Created local group %s with RID %d\n", argv[0], rid);
575 * Delete a local group
578 static int net_sam_deletelocalgroup(int argc, const char **argv)
581 enum lsa_SidType type;
582 const char *dom, *name;
586 d_fprintf(stderr, "usage: net sam deletelocalgroup <name>\n");
590 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
591 &dom, &name, &sid, &type)) {
592 d_fprintf(stderr, "Could not find name %s.\n", argv[0]);
596 if (type != SID_NAME_ALIAS) {
597 d_fprintf(stderr, "%s is a %s, not a local group.\n", argv[0],
598 sid_type_lookup(type));
602 ret = pdb_delete_alias(&sid);
605 d_fprintf(stderr, "Could not delete local group %s.\n", argv[0]);
609 d_printf("Deleted local group %s.\n", argv[0]);
615 * Create a local group
618 static int net_sam_createbuiltingroup(int argc, const char **argv)
622 enum lsa_SidType type;
627 d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
631 if (!winbind_ping()) {
632 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
633 "only works when winbind runs.\n");
637 /* validate the name and get the group */
639 fstrcpy( groupname, "BUILTIN\\" );
640 fstrcat( groupname, argv[0] );
642 if ( !lookup_name(tmp_talloc_ctx(), groupname, LOOKUP_NAME_ALL, NULL,
643 NULL, &sid, &type)) {
644 d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
648 if ( !sid_peek_rid( &sid, &rid ) ) {
649 d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
653 status = pdb_create_builtin_alias( rid );
655 if (!NT_STATUS_IS_OK(status)) {
656 d_fprintf(stderr, "Creating %s failed with %s\n",
657 argv[0], nt_errstr(status));
661 d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
670 static int net_sam_addmem(int argc, const char **argv)
672 const char *groupdomain, *groupname, *memberdomain, *membername;
673 DOM_SID group, member;
674 enum lsa_SidType grouptype, membertype;
678 d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
682 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
683 &groupdomain, &groupname, &group, &grouptype)) {
684 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
688 /* check to see if the member to be added is a name or a SID */
690 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
691 &memberdomain, &membername, &member, &membertype))
693 /* try it as a SID */
695 if ( !string_to_sid( &member, argv[1] ) ) {
696 d_fprintf(stderr, "Could not find member %s\n", argv[1]);
700 if ( !lookup_sid(tmp_talloc_ctx(), &member, &memberdomain,
701 &membername, &membertype) )
703 d_fprintf(stderr, "Could not resolve SID %s\n", argv[1]);
708 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
709 if ((membertype != SID_NAME_USER) &&
710 (membertype != SID_NAME_DOM_GRP)) {
711 d_fprintf(stderr, "%s is a local group, only users "
712 "and domain groups can be added.\n"
713 "%s is a %s\n", argv[0], argv[1],
714 sid_type_lookup(membertype));
717 status = pdb_add_aliasmem(&group, &member);
719 if (!NT_STATUS_IS_OK(status)) {
720 d_fprintf(stderr, "Adding local group member failed "
721 "with %s\n", nt_errstr(status));
725 d_fprintf(stderr, "Can only add members to local groups so "
726 "far, %s is a %s\n", argv[0],
727 sid_type_lookup(grouptype));
731 d_printf("Added %s\\%s to %s\\%s\n", memberdomain, membername,
732 groupdomain, groupname);
738 * Delete a group member
741 static int net_sam_delmem(int argc, const char **argv)
743 const char *groupdomain, *groupname;
744 const char *memberdomain = NULL;
745 const char *membername = NULL;
746 DOM_SID group, member;
747 enum lsa_SidType grouptype;
751 d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
755 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
756 &groupdomain, &groupname, &group, &grouptype)) {
757 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
761 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
762 &memberdomain, &membername, &member, NULL)) {
763 if (!string_to_sid(&member, argv[1])) {
764 d_fprintf(stderr, "Could not find member %s\n",
770 if ((grouptype == SID_NAME_ALIAS) ||
771 (grouptype == SID_NAME_WKN_GRP)) {
772 status = pdb_del_aliasmem(&group, &member);
774 if (!NT_STATUS_IS_OK(status)) {
775 d_fprintf(stderr, "Deleting local group member failed "
776 "with %s\n", nt_errstr(status));
780 d_fprintf(stderr, "Can only delete members from local groups "
781 "so far, %s is a %s\n", argv[0],
782 sid_type_lookup(grouptype));
786 if (membername != NULL) {
787 d_printf("Deleted %s\\%s from %s\\%s\n",
788 memberdomain, membername, groupdomain, groupname);
790 d_printf("Deleted %s from %s\\%s\n",
791 sid_string_static(&member), groupdomain, groupname);
801 static int net_sam_listmem(int argc, const char **argv)
803 const char *groupdomain, *groupname;
805 enum lsa_SidType grouptype;
809 d_fprintf(stderr, "usage: net sam listmem <group>\n");
813 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
814 &groupdomain, &groupname, &group, &grouptype)) {
815 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
819 if ((grouptype == SID_NAME_ALIAS) ||
820 (grouptype == SID_NAME_WKN_GRP)) {
821 DOM_SID *members = NULL;
822 size_t i, num_members = 0;
824 status = pdb_enum_aliasmem(&group, &members, &num_members);
826 if (!NT_STATUS_IS_OK(status)) {
827 d_fprintf(stderr, "Listing group members failed with "
828 "%s\n", nt_errstr(status));
832 d_printf("%s\\%s has %u members\n", groupdomain, groupname,
833 (unsigned int)num_members);
834 for (i=0; i<num_members; i++) {
835 const char *dom, *name;
836 if (lookup_sid(tmp_talloc_ctx(), &members[i],
837 &dom, &name, NULL)) {
838 d_printf(" %s\\%s\n", dom, name);
841 sid_string_static(&members[i]));
845 d_fprintf(stderr, "Can only list local group members so far.\n"
846 "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
856 static int net_sam_do_list(int argc, const char **argv,
857 struct pdb_search *search, const char *what)
859 BOOL verbose = (argc == 1);
862 ((argc == 1) && !strequal(argv[0], "verbose"))) {
863 d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
867 if (search == NULL) {
868 d_fprintf(stderr, "Could not start search\n");
873 struct samr_displayentry entry;
874 if (!search->next_entry(search, &entry)) {
878 d_printf("%s:%d:%s\n",
883 d_printf("%s\n", entry.account_name);
887 search->search_end(search);
891 static int net_sam_list_users(int argc, const char **argv)
893 return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
897 static int net_sam_list_groups(int argc, const char **argv)
899 return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
902 static int net_sam_list_localgroups(int argc, const char **argv)
904 return net_sam_do_list(argc, argv,
905 pdb_search_aliases(get_global_sam_sid()),
909 static int net_sam_list_builtin(int argc, const char **argv)
911 return net_sam_do_list(argc, argv,
912 pdb_search_aliases(&global_sid_Builtin),
916 static int net_sam_list_workstations(int argc, const char **argv)
918 return net_sam_do_list(argc, argv,
919 pdb_search_users(ACB_WSTRUST),
927 static int net_sam_list(int argc, const char **argv)
929 struct functable2 func[] = {
930 { "users", net_sam_list_users,
932 { "groups", net_sam_list_groups,
934 { "localgroups", net_sam_list_localgroups,
935 "List SAM local groups" },
936 { "builtin", net_sam_list_builtin,
937 "List builtin groups" },
938 { "workstations", net_sam_list_workstations,
939 "List domain member workstations" },
943 return net_run_function2(argc, argv, "net sam list", func);
947 * Show details of SAM entries
950 static int net_sam_show(int argc, const char **argv)
953 enum lsa_SidType type;
954 const char *dom, *name;
957 d_fprintf(stderr, "usage: net sam show <name>\n");
961 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
962 &dom, &name, &sid, &type)) {
963 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
967 d_printf("%s\\%s is a %s with SID %s\n", dom, name,
968 sid_type_lookup(type), sid_string_static(&sid));
976 * Init an LDAP tree with default users and Groups
977 * if ldapsam:editposix is enabled
980 static int net_sam_provision(int argc, const char **argv)
984 char *ldap_uri = NULL;
986 struct smbldap_state *ls;
989 gid_t domusers_gid = -1;
990 gid_t domadmins_gid = -1;
991 struct samu *samuser;
994 tc = talloc_new(NULL);
996 d_fprintf(stderr, "Out of Memory!\n");
1000 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
1001 d_fprintf(stderr, "talloc failed\n");
1005 p = strchr(ldap_bk, ':');
1008 ldap_uri = talloc_strdup(tc, p+1);
1009 trim_char(ldap_uri, ' ', ' ');
1012 trim_char(ldap_bk, ' ', ' ');
1014 if (strcmp(ldap_bk, "ldapsam") != 0) {
1015 d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
1019 if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
1020 !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
1022 d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
1023 " and ldapsam:editposix are enabled.\n");
1027 if (!winbind_ping()) {
1028 d_fprintf(stderr, "winbind seems not to run. Provisioning "
1029 "LDAP only works when winbind runs.\n");
1033 if (!NT_STATUS_IS_OK(smbldap_init(tc, NULL, ldap_uri, &ls))) {
1034 d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
1038 d_printf("Checking for Domain Users group.\n");
1040 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
1042 if (!pdb_getgrsid(&gmap, gsid)) {
1043 LDAPMod **mods = NULL;
1051 d_printf("Adding the Domain Users group.\n");
1053 /* lets allocate a new groupid for this group */
1054 if (!winbind_allocate_gid(&domusers_gid)) {
1055 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
1059 uname = talloc_strdup(tc, "domusers");
1060 wname = talloc_strdup(tc, "Domain Users");
1061 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1062 gidstr = talloc_asprintf(tc, "%d", domusers_gid);
1063 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1065 if (!uname || !wname || !dn || !gidstr || !gtype) {
1066 d_fprintf(stderr, "Out of Memory!\n");
1070 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1071 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1072 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1073 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1074 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1075 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1076 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1078 talloc_autofree_ldapmod(tc, mods);
1080 rc = smbldap_add(ls, dn, mods);
1082 if (rc != LDAP_SUCCESS) {
1083 d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
1086 domusers_gid = gmap.gid;
1087 d_printf("found!\n");
1092 d_printf("Checking for Domain Admins group.\n");
1094 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
1096 if (!pdb_getgrsid(&gmap, gsid)) {
1097 LDAPMod **mods = NULL;
1105 d_printf("Adding the Domain Admins group.\n");
1107 /* lets allocate a new groupid for this group */
1108 if (!winbind_allocate_gid(&domadmins_gid)) {
1109 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
1113 uname = talloc_strdup(tc, "domadmins");
1114 wname = talloc_strdup(tc, "Domain Admins");
1115 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1116 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1117 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1119 if (!uname || !wname || !dn || !gidstr || !gtype) {
1120 d_fprintf(stderr, "Out of Memory!\n");
1124 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1125 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1126 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1127 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1128 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1129 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1130 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1132 talloc_autofree_ldapmod(tc, mods);
1134 rc = smbldap_add(ls, dn, mods);
1136 if (rc != LDAP_SUCCESS) {
1137 d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
1140 domadmins_gid = gmap.gid;
1141 d_printf("found!\n");
1146 d_printf("Check for Administrator account.\n");
1148 samuser = samu_new(tc);
1150 d_fprintf(stderr, "Out of Memory!\n");
1154 if (!pdb_getsampwnam(samuser, "Administrator")) {
1155 LDAPMod **mods = NULL;
1166 d_printf("Adding the Administrator user.\n");
1168 if (domadmins_gid == -1) {
1169 d_fprintf(stderr, "Can't create Administrator user, Domain Admins group not available!\n");
1172 if (!winbind_allocate_uid(&uid)) {
1173 d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
1176 name = talloc_strdup(tc, "Administrator");
1177 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1178 uidstr = talloc_asprintf(tc, "%d", uid);
1179 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1180 dir = talloc_sub_specified(tc, lp_template_homedir(),
1182 get_global_sam_name(),
1183 uid, domadmins_gid);
1184 shell = talloc_sub_specified(tc, lp_template_shell(),
1186 get_global_sam_name(),
1187 uid, domadmins_gid);
1189 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1190 d_fprintf(stderr, "Out of Memory!\n");
1194 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1196 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1197 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1198 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1199 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1200 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1201 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1202 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1203 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1204 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1205 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1206 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1207 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1208 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1209 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1211 talloc_autofree_ldapmod(tc, mods);
1213 rc = smbldap_add(ls, dn, mods);
1215 if (rc != LDAP_SUCCESS) {
1216 d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
1219 d_printf("found!\n");
1222 d_printf("Checking for Guest user.\n");
1224 samuser = samu_new(tc);
1226 d_fprintf(stderr, "Out of Memory!\n");
1230 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1231 LDAPMod **mods = NULL;
1238 d_printf("Adding the Guest user.\n");
1240 pwd = getpwnam_alloc(tc, lp_guestaccount());
1243 if (domusers_gid == -1) {
1244 d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
1247 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1248 d_fprintf(stderr, "talloc failed\n");
1251 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1252 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1253 d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
1256 pwd->pw_gid = domusers_gid;
1257 pwd->pw_dir = talloc_strdup(tc, "/");
1258 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1259 if (!pwd->pw_dir || !pwd->pw_shell) {
1260 d_fprintf(stderr, "Out of Memory!\n");
1265 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1267 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1268 uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
1269 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1270 if (!dn || !uidstr || !gidstr) {
1271 d_fprintf(stderr, "Out of Memory!\n");
1275 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1276 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1277 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1278 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1279 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1280 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1281 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1282 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1283 if ((pwd->pw_dir != NULL) && (pwd->pw_dir[0] != '\0')) {
1284 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1286 if ((pwd->pw_shell != NULL) && (pwd->pw_shell[0] != '\0')) {
1287 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1289 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1290 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1291 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1292 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1294 talloc_autofree_ldapmod(tc, mods);
1296 rc = smbldap_add(ls, dn, mods);
1298 if (rc != LDAP_SUCCESS) {
1299 d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
1302 d_printf("found!\n");
1305 d_printf("Checking Guest's group.\n");
1307 pwd = getpwnam_alloc(NULL, lp_guestaccount());
1309 d_fprintf(stderr, "Failed to find just created Guest account!\n"
1310 " Is nss properly configured?!\n");
1314 if (pwd->pw_gid == domusers_gid) {
1315 d_printf("found!\n");
1319 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1320 LDAPMod **mods = NULL;
1328 d_printf("Adding the Domain Guests group.\n");
1330 uname = talloc_strdup(tc, "domguests");
1331 wname = talloc_strdup(tc, "Domain Guests");
1332 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1333 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1334 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1336 if (!uname || !wname || !dn || !gidstr || !gtype) {
1337 d_fprintf(stderr, "Out of Memory!\n");
1341 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1343 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1344 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1345 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1346 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1347 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1348 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1349 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1351 talloc_autofree_ldapmod(tc, mods);
1353 rc = smbldap_add(ls, dn, mods);
1355 if (rc != LDAP_SUCCESS) {
1356 d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
1359 d_printf("found!\n");
1374 /***********************************************************
1375 migrated functionality from smbgroupedit
1376 **********************************************************/
1377 int net_sam(int argc, const char **argv)
1379 struct functable2 func[] = {
1380 { "createbuiltingroup", net_sam_createbuiltingroup,
1381 "Create a new BUILTIN group" },
1382 { "createlocalgroup", net_sam_createlocalgroup,
1383 "Create a new local group" },
1384 { "deletelocalgroup", net_sam_deletelocalgroup,
1385 "Delete an existing local group" },
1386 { "mapunixgroup", net_sam_mapunixgroup,
1387 "Map a unix group to a domain group" },
1388 { "addmem", net_sam_addmem,
1389 "Add a member to a group" },
1390 { "delmem", net_sam_delmem,
1391 "Delete a member from a group" },
1392 { "listmem", net_sam_listmem,
1393 "List group members" },
1394 { "list", net_sam_list,
1395 "List users, groups and local groups" },
1396 { "show", net_sam_show,
1397 "Show details of a SAM entry" },
1398 { "set", net_sam_set,
1399 "Set details of a SAM account" },
1400 { "policy", net_sam_policy,
1401 "Set account policies" },
1403 { "provision", net_sam_provision,
1404 "Provision a clean User Database" },
1406 { NULL, NULL, NULL }
1409 /* we shouldn't have silly checks like this */
1410 if (getuid() != 0) {
1411 d_fprintf(stderr, "You must be root to edit the SAM "
1416 return net_run_function2(argc, argv, "net sam", func);