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;
370 d_fprintf(stderr, "usage: net sam policy set"
371 "\"<account policy>\" <value> \n");
375 value = strtoul(argv[1], NULL, 10);
376 account_policy = argv[0];
377 field = account_policy_name_to_fieldnum(account_policy);
379 printf("Account policy \"%s\" description: %s\n", account_policy,
380 account_policy_get_desc(field));
382 if (!pdb_get_account_policy(field, &old_value)) {
383 fprintf(stderr, "Valid account policy, but unable to "
388 printf("Account policy \"%s\" value was: %d\n", account_policy,
391 if (!pdb_set_account_policy(field, value)) {
392 d_fprintf(stderr, "Setting account policy %s to %u failed \n",
393 account_policy, value);
396 printf("Account policy \"%s\" value is now: %d\n", account_policy,
402 static int net_sam_policy_show(int argc, const char **argv)
404 const char *account_policy = NULL;
409 d_fprintf(stderr, "usage: net sam policy show"
410 " \"<account policy>\" \n");
414 account_policy = argv[0];
415 field = account_policy_name_to_fieldnum(account_policy);
418 char *apn = account_policy_names_list();
419 d_fprintf(stderr, "No account policy by that name!\n");
421 d_fprintf(stderr, "Valid account policies "
428 if (!pdb_get_account_policy(field, &old_value)) {
429 fprintf(stderr, "Valid account policy, but unable to "
434 printf("Account policy \"%s\" description: %s\n",
435 account_policy, account_policy_get_desc(field));
436 printf("Account policy \"%s\" value is: %d\n", account_policy,
441 static int net_sam_policy_list(int argc, const char **argv)
443 char *apn = account_policy_names_list();
445 d_fprintf(stderr, "Valid account policies "
446 "are:\n\n%s\n", apn);
452 static int net_sam_policy(int argc, const char **argv)
454 struct functable2 func[] = {
455 { "list", net_sam_policy_list,
456 "List account policies" },
457 { "show", net_sam_policy_show,
458 "Show account policies" },
459 { "set", net_sam_policy_set,
460 "Change account policies" },
464 return net_run_function2(argc, argv, "net sam policy", func);
468 * Map a unix group to a domain group
471 static int net_sam_mapunixgroup(int argc, const char **argv)
478 d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
482 grp = getgrnam(argv[0]);
484 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
488 status = map_unix_group(grp, &map);
490 if (!NT_STATUS_IS_OK(status)) {
491 d_fprintf(stderr, "Mapping group %s failed with %s\n",
492 argv[0], nt_errstr(status));
496 d_printf("Mapped unix group %s to SID %s\n", argv[0],
497 sid_string_static(&map.sid));
503 * Create a local group
506 static int net_sam_createlocalgroup(int argc, const char **argv)
512 d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
516 if (!winbind_ping()) {
517 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
518 "only works when winbind runs.\n");
522 status = pdb_create_alias(argv[0], &rid);
524 if (!NT_STATUS_IS_OK(status)) {
525 d_fprintf(stderr, "Creating %s failed with %s\n",
526 argv[0], nt_errstr(status));
530 d_printf("Created local group %s with RID %d\n", argv[0], rid);
536 * Create a local group
539 static int net_sam_createbuiltingroup(int argc, const char **argv)
543 enum lsa_SidType type;
548 d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
552 if (!winbind_ping()) {
553 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
554 "only works when winbind runs.\n");
558 /* validate the name and get the group */
560 fstrcpy( groupname, "BUILTIN\\" );
561 fstrcat( groupname, argv[0] );
563 if ( !lookup_name(tmp_talloc_ctx(), groupname, LOOKUP_NAME_ALL, NULL,
564 NULL, &sid, &type)) {
565 d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
569 if ( !sid_peek_rid( &sid, &rid ) ) {
570 d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
574 status = pdb_create_builtin_alias( rid );
576 if (!NT_STATUS_IS_OK(status)) {
577 d_fprintf(stderr, "Creating %s failed with %s\n",
578 argv[0], nt_errstr(status));
582 d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
591 static int net_sam_addmem(int argc, const char **argv)
593 const char *groupdomain, *groupname, *memberdomain, *membername;
594 DOM_SID group, member;
595 enum lsa_SidType grouptype, membertype;
599 d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
603 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
604 &groupdomain, &groupname, &group, &grouptype)) {
605 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
609 /* check to see if the member to be added is a name or a SID */
611 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
612 &memberdomain, &membername, &member, &membertype))
614 /* try it as a SID */
616 if ( !string_to_sid( &member, argv[1] ) ) {
617 d_fprintf(stderr, "Could not find member %s\n", argv[1]);
621 if ( !lookup_sid(tmp_talloc_ctx(), &member, &memberdomain,
622 &membername, &membertype) )
624 d_fprintf(stderr, "Could not resolve SID %s\n", argv[1]);
629 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
630 if ((membertype != SID_NAME_USER) &&
631 (membertype != SID_NAME_DOM_GRP)) {
632 d_fprintf(stderr, "%s is a local group, only users "
633 "and domain groups can be added.\n"
634 "%s is a %s\n", argv[0], argv[1],
635 sid_type_lookup(membertype));
638 status = pdb_add_aliasmem(&group, &member);
640 if (!NT_STATUS_IS_OK(status)) {
641 d_fprintf(stderr, "Adding local group member failed "
642 "with %s\n", nt_errstr(status));
646 d_fprintf(stderr, "Can only add members to local groups so "
647 "far, %s is a %s\n", argv[0],
648 sid_type_lookup(grouptype));
652 d_printf("Added %s\\%s to %s\\%s\n", memberdomain, membername,
653 groupdomain, groupname);
659 * Delete a group member
662 static int net_sam_delmem(int argc, const char **argv)
664 const char *groupdomain, *groupname;
665 const char *memberdomain = NULL;
666 const char *membername = NULL;
667 DOM_SID group, member;
668 enum lsa_SidType grouptype;
672 d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
676 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
677 &groupdomain, &groupname, &group, &grouptype)) {
678 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
682 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
683 &memberdomain, &membername, &member, NULL)) {
684 if (!string_to_sid(&member, argv[1])) {
685 d_fprintf(stderr, "Could not find member %s\n",
691 if ((grouptype == SID_NAME_ALIAS) ||
692 (grouptype == SID_NAME_WKN_GRP)) {
693 status = pdb_del_aliasmem(&group, &member);
695 if (!NT_STATUS_IS_OK(status)) {
696 d_fprintf(stderr, "Deleting local group member failed "
697 "with %s\n", nt_errstr(status));
701 d_fprintf(stderr, "Can only delete members from local groups "
702 "so far, %s is a %s\n", argv[0],
703 sid_type_lookup(grouptype));
707 if (membername != NULL) {
708 d_printf("Deleted %s\\%s from %s\\%s\n",
709 memberdomain, membername, groupdomain, groupname);
711 d_printf("Deleted %s from %s\\%s\n",
712 sid_string_static(&member), groupdomain, groupname);
722 static int net_sam_listmem(int argc, const char **argv)
724 const char *groupdomain, *groupname;
726 enum lsa_SidType grouptype;
730 d_fprintf(stderr, "usage: net sam listmem <group>\n");
734 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
735 &groupdomain, &groupname, &group, &grouptype)) {
736 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
740 if ((grouptype == SID_NAME_ALIAS) ||
741 (grouptype == SID_NAME_WKN_GRP)) {
742 DOM_SID *members = NULL;
743 size_t i, num_members = 0;
745 status = pdb_enum_aliasmem(&group, &members, &num_members);
747 if (!NT_STATUS_IS_OK(status)) {
748 d_fprintf(stderr, "Listing group members failed with "
749 "%s\n", nt_errstr(status));
753 d_printf("%s\\%s has %u members\n", groupdomain, groupname,
754 (unsigned int)num_members);
755 for (i=0; i<num_members; i++) {
756 const char *dom, *name;
757 if (lookup_sid(tmp_talloc_ctx(), &members[i],
758 &dom, &name, NULL)) {
759 d_printf(" %s\\%s\n", dom, name);
762 sid_string_static(&members[i]));
766 d_fprintf(stderr, "Can only list local group members so far.\n"
767 "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
777 static int net_sam_do_list(int argc, const char **argv,
778 struct pdb_search *search, const char *what)
780 BOOL verbose = (argc == 1);
783 ((argc == 1) && !strequal(argv[0], "verbose"))) {
784 d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
788 if (search == NULL) {
789 d_fprintf(stderr, "Could not start search\n");
794 struct samr_displayentry entry;
795 if (!search->next_entry(search, &entry)) {
799 d_printf("%s:%d:%s\n",
804 d_printf("%s\n", entry.account_name);
808 search->search_end(search);
812 static int net_sam_list_users(int argc, const char **argv)
814 return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
818 static int net_sam_list_groups(int argc, const char **argv)
820 return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
823 static int net_sam_list_localgroups(int argc, const char **argv)
825 return net_sam_do_list(argc, argv,
826 pdb_search_aliases(get_global_sam_sid()),
830 static int net_sam_list_builtin(int argc, const char **argv)
832 return net_sam_do_list(argc, argv,
833 pdb_search_aliases(&global_sid_Builtin),
837 static int net_sam_list_workstations(int argc, const char **argv)
839 return net_sam_do_list(argc, argv,
840 pdb_search_users(ACB_WSTRUST),
848 static int net_sam_list(int argc, const char **argv)
850 struct functable2 func[] = {
851 { "users", net_sam_list_users,
853 { "groups", net_sam_list_groups,
855 { "localgroups", net_sam_list_localgroups,
856 "List SAM local groups" },
857 { "builtin", net_sam_list_builtin,
858 "List builtin groups" },
859 { "workstations", net_sam_list_workstations,
860 "List domain member workstations" },
864 return net_run_function2(argc, argv, "net sam list", func);
868 * Show details of SAM entries
871 static int net_sam_show(int argc, const char **argv)
874 enum lsa_SidType type;
875 const char *dom, *name;
878 d_fprintf(stderr, "usage: net sam show <name>\n");
882 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
883 &dom, &name, &sid, &type)) {
884 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
888 d_printf("%s\\%s is a %s with SID %s\n", dom, name,
889 sid_type_lookup(type), sid_string_static(&sid));
897 * Init an LDAP tree with default users and Groups
898 * if ldapsam:editposix is enabled
901 static int net_sam_provision(int argc, const char **argv)
905 char *ldap_uri = NULL;
907 struct smbldap_state *ls;
910 gid_t domusers_gid = -1;
911 gid_t domadmins_gid = -1;
912 struct samu *samuser;
915 tc = talloc_new(NULL);
917 d_fprintf(stderr, "Out of Memory!\n");
921 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
922 d_fprintf(stderr, "talloc failed\n");
926 p = strchr(ldap_bk, ':');
929 ldap_uri = talloc_strdup(tc, p+1);
930 trim_char(ldap_uri, ' ', ' ');
933 trim_char(ldap_bk, ' ', ' ');
935 if (strcmp(ldap_bk, "ldapsam") != 0) {
936 d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
940 if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
941 !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
943 d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
944 " and ldapsam:editposix are enabled.\n");
948 if (!winbind_ping()) {
949 d_fprintf(stderr, "winbind seems not to run. Provisioning "
950 "LDAP only works when winbind runs.\n");
954 if (!NT_STATUS_IS_OK(smbldap_init(tc, ldap_uri, &ls))) {
955 d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
959 d_printf("Checking for Domain Users group.\n");
961 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
963 if (!pdb_getgrsid(&gmap, gsid)) {
964 LDAPMod **mods = NULL;
972 d_printf("Adding the Domain Users group.\n");
974 /* lets allocate a new groupid for this group */
975 if (!winbind_allocate_gid(&domusers_gid)) {
976 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
980 uname = talloc_strdup(tc, "domusers");
981 wname = talloc_strdup(tc, "Domain Users");
982 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
983 gidstr = talloc_asprintf(tc, "%d", domusers_gid);
984 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
986 if (!uname || !wname || !dn || !gidstr || !gtype) {
987 d_fprintf(stderr, "Out of Memory!\n");
991 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
992 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
993 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
994 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
995 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
996 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
997 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
999 talloc_autofree_ldapmod(tc, mods);
1001 rc = smbldap_add(ls, dn, mods);
1003 if (rc != LDAP_SUCCESS) {
1004 d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
1007 d_printf("found!\n");
1012 d_printf("Checking for Domain Admins group.\n");
1014 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
1016 if (!pdb_getgrsid(&gmap, gsid)) {
1017 LDAPMod **mods = NULL;
1025 d_printf("Adding the Domain Admins group.\n");
1027 /* lets allocate a new groupid for this group */
1028 if (!winbind_allocate_gid(&domadmins_gid)) {
1029 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
1033 uname = talloc_strdup(tc, "domadmins");
1034 wname = talloc_strdup(tc, "Domain Admins");
1035 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1036 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1037 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1039 if (!uname || !wname || !dn || !gidstr || !gtype) {
1040 d_fprintf(stderr, "Out of Memory!\n");
1044 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1045 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1046 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1047 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1048 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1049 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1050 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1052 talloc_autofree_ldapmod(tc, mods);
1054 rc = smbldap_add(ls, dn, mods);
1056 if (rc != LDAP_SUCCESS) {
1057 d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
1060 d_printf("found!\n");
1065 d_printf("Check for Administrator account.\n");
1067 samuser = samu_new(tc);
1069 d_fprintf(stderr, "Out of Memory!\n");
1073 if (!pdb_getsampwnam(samuser, "Administrator")) {
1074 LDAPMod **mods = NULL;
1085 d_printf("Adding the Administrator user.\n");
1087 if (domadmins_gid == -1) {
1088 d_fprintf(stderr, "Can't create Administrtor user, Domain Admins group not available!\n");
1091 if (!winbind_allocate_uid(&uid)) {
1092 d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
1095 name = talloc_strdup(tc, "Administrator");
1096 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1097 uidstr = talloc_asprintf(tc, "%d", uid);
1098 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1099 dir = talloc_sub_specified(tc, lp_template_homedir(),
1101 get_global_sam_name(),
1102 uid, domadmins_gid);
1103 shell = talloc_sub_specified(tc, lp_template_shell(),
1105 get_global_sam_name(),
1106 uid, domadmins_gid);
1108 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1109 d_fprintf(stderr, "Out of Memory!\n");
1113 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1115 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1116 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1117 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1118 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1119 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1120 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1121 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1122 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1123 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1124 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1125 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1126 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1127 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1128 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1130 talloc_autofree_ldapmod(tc, mods);
1132 rc = smbldap_add(ls, dn, mods);
1134 if (rc != LDAP_SUCCESS) {
1135 d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
1138 d_printf("found!\n");
1141 d_printf("Checking for Guest user.\n");
1143 samuser = samu_new(tc);
1145 d_fprintf(stderr, "Out of Memory!\n");
1149 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1150 LDAPMod **mods = NULL;
1157 d_printf("Adding the Guest user.\n");
1159 pwd = getpwnam_alloc(tc, lp_guestaccount());
1162 if (domusers_gid == -1) {
1163 d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
1166 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1167 d_fprintf(stderr, "talloc failed\n");
1170 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1171 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1172 d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
1175 pwd->pw_gid = domusers_gid;
1176 pwd->pw_dir = talloc_strdup(tc, "/");
1177 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1178 if (!pwd->pw_dir || !pwd->pw_shell) {
1179 d_fprintf(stderr, "Out of Memory!\n");
1184 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1186 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1187 uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
1188 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1189 if (!dn || !uidstr || !gidstr) {
1190 d_fprintf(stderr, "Out of Memory!\n");
1194 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1195 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1196 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1197 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1198 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1199 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1200 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1201 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1202 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1203 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1204 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1205 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1206 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1207 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1209 talloc_autofree_ldapmod(tc, mods);
1211 rc = smbldap_add(ls, dn, mods);
1213 if (rc != LDAP_SUCCESS) {
1214 d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
1217 d_printf("found!\n");
1220 d_printf("Checking Guest's group.\n");
1222 pwd = getpwnam_alloc(NULL, lp_guestaccount());
1224 d_fprintf(stderr, "Failed to find just created Guest account!\n"
1225 " Is nssswitch properly configured?!\n");
1229 if (pwd->pw_gid == domusers_gid) {
1230 d_printf("found!\n");
1234 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1235 LDAPMod **mods = NULL;
1243 d_printf("Adding the Domain Guests group.\n");
1245 uname = talloc_strdup(tc, "domguests");
1246 wname = talloc_strdup(tc, "Domain Guests");
1247 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1248 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1249 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1251 if (!uname || !wname || !dn || !gidstr || !gtype) {
1252 d_fprintf(stderr, "Out of Memory!\n");
1256 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1258 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1259 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1260 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1261 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1262 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1263 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1264 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1266 talloc_autofree_ldapmod(tc, mods);
1268 rc = smbldap_add(ls, dn, mods);
1270 if (rc != LDAP_SUCCESS) {
1271 d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
1274 d_printf("found!\n");
1289 /***********************************************************
1290 migrated functionality from smbgroupedit
1291 **********************************************************/
1292 int net_sam(int argc, const char **argv)
1294 struct functable2 func[] = {
1295 { "createbuiltingroup", net_sam_createbuiltingroup,
1296 "Create a new BUILTIN group" },
1297 { "createlocalgroup", net_sam_createlocalgroup,
1298 "Create a new local group" },
1299 { "mapunixgroup", net_sam_mapunixgroup,
1300 "Map a unix group to a domain group" },
1301 { "addmem", net_sam_addmem,
1302 "Add a member to a group" },
1303 { "delmem", net_sam_delmem,
1304 "Delete a member from a group" },
1305 { "listmem", net_sam_listmem,
1306 "List group members" },
1307 { "list", net_sam_list,
1308 "List users, groups and local groups" },
1309 { "show", net_sam_show,
1310 "Show details of a SAM entry" },
1311 { "set", net_sam_set,
1312 "Set details of a SAM account" },
1313 { "policy", net_sam_policy,
1314 "Set account policies" },
1316 { "provision", net_sam_provision,
1317 "Provision a clean User Database" },
1319 { NULL, NULL, NULL }
1322 /* we shouldn't have silly checks like this */
1323 if (getuid() != 0) {
1324 d_fprintf(stderr, "You must be root to edit the SAM "
1329 return net_run_function2(argc, argv, "net sam", func);