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 * Change account policies
363 static int net_sam_policy(int argc, const char **argv)
366 const char *account_policy = NULL;
367 uint32 value, old_value;
370 if ((argc < 1) || (argc > 2)) {
371 d_fprintf(stderr, "usage: net sam policy \"<account policy>\" "
372 "-> show current value\n");
373 d_fprintf(stderr, "usage: net sam policy \"<account policy>\" "
374 "<value> -> set a new value\n");
378 account_policy = argv[0];
379 field = account_policy_name_to_fieldnum(account_policy);
382 char *apn = account_policy_names_list();
383 d_fprintf(stderr, "No account policy by that name!\n");
385 d_fprintf(stderr, "Valid account policies "
392 if (!pdb_get_account_policy(field, &old_value)) {
393 fprintf(stderr, "Valid account policy, but unable to "
400 * Just read the value
403 printf("Account policy \"%s\" description: %s\n",
404 account_policy, account_policy_get_desc(field));
405 printf("Account policy \"%s\" value is: %d\n", account_policy,
411 * Here we know we have 2 args, so set it
414 value = strtoul(argv[1], NULL, 10);
416 printf("Account policy \"%s\" description: %s\n", account_policy,
417 account_policy_get_desc(field));
418 printf("Account policy \"%s\" value was: %d\n", account_policy,
421 if (!pdb_set_account_policy(field, value)) {
422 d_fprintf(stderr, "Setting account policy %s to %u failed \n",
423 account_policy, value);
426 printf("Account policy \"%s\" value is now: %d\n", account_policy,
434 * Map a unix group to a domain group
437 static int net_sam_mapunixgroup(int argc, const char **argv)
444 d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
448 grp = getgrnam(argv[0]);
450 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
454 status = map_unix_group(grp, &map);
456 if (!NT_STATUS_IS_OK(status)) {
457 d_fprintf(stderr, "Mapping group %s failed with %s\n",
458 argv[0], nt_errstr(status));
462 d_printf("Mapped unix group %s to SID %s\n", argv[0],
463 sid_string_static(&map.sid));
469 * Create a local group
472 static int net_sam_createlocalgroup(int argc, const char **argv)
478 d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
482 if (!winbind_ping()) {
483 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
484 "only works when winbind runs.\n");
488 status = pdb_create_alias(argv[0], &rid);
490 if (!NT_STATUS_IS_OK(status)) {
491 d_fprintf(stderr, "Creating %s failed with %s\n",
492 argv[0], nt_errstr(status));
496 d_printf("Created local group %s with RID %d\n", argv[0], rid);
502 * Create a local group
505 static int net_sam_createbuiltingroup(int argc, const char **argv)
509 enum lsa_SidType type;
514 d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
518 if (!winbind_ping()) {
519 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
520 "only works when winbind runs.\n");
524 /* validate the name and get the group */
526 fstrcpy( groupname, "BUILTIN\\" );
527 fstrcat( groupname, argv[0] );
529 if ( !lookup_name(tmp_talloc_ctx(), groupname, LOOKUP_NAME_ALL, NULL,
530 NULL, &sid, &type)) {
531 d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
535 if ( !sid_peek_rid( &sid, &rid ) ) {
536 d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
540 status = pdb_create_builtin_alias( rid );
542 if (!NT_STATUS_IS_OK(status)) {
543 d_fprintf(stderr, "Creating %s failed with %s\n",
544 argv[0], nt_errstr(status));
548 d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
557 static int net_sam_addmem(int argc, const char **argv)
559 const char *groupdomain, *groupname, *memberdomain, *membername;
560 DOM_SID group, member;
561 enum lsa_SidType grouptype, membertype;
565 d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
569 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
570 &groupdomain, &groupname, &group, &grouptype)) {
571 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
575 /* check to see if the member to be added is a name or a SID */
577 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
578 &memberdomain, &membername, &member, &membertype))
580 /* try it as a SID */
582 if ( !string_to_sid( &member, argv[1] ) ) {
583 d_fprintf(stderr, "Could not find member %s\n", argv[1]);
587 if ( !lookup_sid(tmp_talloc_ctx(), &member, &memberdomain,
588 &membername, &membertype) )
590 d_fprintf(stderr, "Could not resolve SID %s\n", argv[1]);
595 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
596 if ((membertype != SID_NAME_USER) &&
597 (membertype != SID_NAME_DOM_GRP)) {
598 d_fprintf(stderr, "%s is a local group, only users "
599 "and domain groups can be added.\n"
600 "%s is a %s\n", argv[0], argv[1],
601 sid_type_lookup(membertype));
604 status = pdb_add_aliasmem(&group, &member);
606 if (!NT_STATUS_IS_OK(status)) {
607 d_fprintf(stderr, "Adding local group member failed "
608 "with %s\n", nt_errstr(status));
612 d_fprintf(stderr, "Can only add members to local groups so "
613 "far, %s is a %s\n", argv[0],
614 sid_type_lookup(grouptype));
618 d_printf("Added %s\\%s to %s\\%s\n", memberdomain, membername,
619 groupdomain, groupname);
625 * Delete a group member
628 static int net_sam_delmem(int argc, const char **argv)
630 const char *groupdomain, *groupname;
631 const char *memberdomain = NULL;
632 const char *membername = NULL;
633 DOM_SID group, member;
634 enum lsa_SidType grouptype;
638 d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
642 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
643 &groupdomain, &groupname, &group, &grouptype)) {
644 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
648 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
649 &memberdomain, &membername, &member, NULL)) {
650 if (!string_to_sid(&member, argv[1])) {
651 d_fprintf(stderr, "Could not find member %s\n",
657 if ((grouptype == SID_NAME_ALIAS) ||
658 (grouptype == SID_NAME_WKN_GRP)) {
659 status = pdb_del_aliasmem(&group, &member);
661 if (!NT_STATUS_IS_OK(status)) {
662 d_fprintf(stderr, "Deleting local group member failed "
663 "with %s\n", nt_errstr(status));
667 d_fprintf(stderr, "Can only delete members from local groups "
668 "so far, %s is a %s\n", argv[0],
669 sid_type_lookup(grouptype));
673 if (membername != NULL) {
674 d_printf("Deleted %s\\%s from %s\\%s\n",
675 memberdomain, membername, groupdomain, groupname);
677 d_printf("Deleted %s from %s\\%s\n",
678 sid_string_static(&member), groupdomain, groupname);
688 static int net_sam_listmem(int argc, const char **argv)
690 const char *groupdomain, *groupname;
692 enum lsa_SidType grouptype;
696 d_fprintf(stderr, "usage: net sam listmem <group>\n");
700 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
701 &groupdomain, &groupname, &group, &grouptype)) {
702 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
706 if ((grouptype == SID_NAME_ALIAS) ||
707 (grouptype == SID_NAME_WKN_GRP)) {
708 DOM_SID *members = NULL;
709 size_t i, num_members = 0;
711 status = pdb_enum_aliasmem(&group, &members, &num_members);
713 if (!NT_STATUS_IS_OK(status)) {
714 d_fprintf(stderr, "Listing group members failed with "
715 "%s\n", nt_errstr(status));
719 d_printf("%s\\%s has %u members\n", groupdomain, groupname,
720 (unsigned int)num_members);
721 for (i=0; i<num_members; i++) {
722 const char *dom, *name;
723 if (lookup_sid(tmp_talloc_ctx(), &members[i],
724 &dom, &name, NULL)) {
725 d_printf(" %s\\%s\n", dom, name);
728 sid_string_static(&members[i]));
732 d_fprintf(stderr, "Can only list local group members so far.\n"
733 "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
743 static int net_sam_do_list(int argc, const char **argv,
744 struct pdb_search *search, const char *what)
746 BOOL verbose = (argc == 1);
749 ((argc == 1) && !strequal(argv[0], "verbose"))) {
750 d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
754 if (search == NULL) {
755 d_fprintf(stderr, "Could not start search\n");
760 struct samr_displayentry entry;
761 if (!search->next_entry(search, &entry)) {
765 d_printf("%s:%d:%s\n",
770 d_printf("%s\n", entry.account_name);
774 search->search_end(search);
778 static int net_sam_list_users(int argc, const char **argv)
780 return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
784 static int net_sam_list_groups(int argc, const char **argv)
786 return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
789 static int net_sam_list_localgroups(int argc, const char **argv)
791 return net_sam_do_list(argc, argv,
792 pdb_search_aliases(get_global_sam_sid()),
796 static int net_sam_list_builtin(int argc, const char **argv)
798 return net_sam_do_list(argc, argv,
799 pdb_search_aliases(&global_sid_Builtin),
803 static int net_sam_list_workstations(int argc, const char **argv)
805 return net_sam_do_list(argc, argv,
806 pdb_search_users(ACB_WSTRUST),
814 static int net_sam_list(int argc, const char **argv)
816 struct functable2 func[] = {
817 { "users", net_sam_list_users,
819 { "groups", net_sam_list_groups,
821 { "localgroups", net_sam_list_localgroups,
822 "List SAM local groups" },
823 { "builtin", net_sam_list_builtin,
824 "List builtin groups" },
825 { "workstations", net_sam_list_workstations,
826 "List domain member workstations" },
830 return net_run_function2(argc, argv, "net sam list", func);
834 * Show details of SAM entries
837 static int net_sam_show(int argc, const char **argv)
840 enum lsa_SidType type;
841 const char *dom, *name;
844 d_fprintf(stderr, "usage: net sam show <name>\n");
848 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
849 &dom, &name, &sid, &type)) {
850 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
854 d_printf("%s\\%s is a %s with SID %s\n", dom, name,
855 sid_type_lookup(type), sid_string_static(&sid));
863 * Init an LDAP tree with default users and Groups
864 * if ldapsam:editposix is enabled
867 static int net_sam_provision(int argc, const char **argv)
871 char *ldap_uri = NULL;
873 struct smbldap_state *ls;
876 gid_t domusers_gid = -1;
877 gid_t domadmins_gid = -1;
878 struct samu *samuser;
881 tc = talloc_new(NULL);
883 d_fprintf(stderr, "Out of Memory!\n");
887 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
888 d_fprintf(stderr, "talloc failed\n");
892 p = strchr(ldap_bk, ':');
895 ldap_uri = talloc_strdup(tc, p+1);
896 trim_char(ldap_uri, ' ', ' ');
899 trim_char(ldap_bk, ' ', ' ');
901 if (strcmp(ldap_bk, "ldapsam") != 0) {
902 d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
906 if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
907 !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
909 d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
910 " and ldapsam:editposix are enabled.\n");
914 if (!winbind_ping()) {
915 d_fprintf(stderr, "winbind seems not to run. Provisioning "
916 "LDAP only works when winbind runs.\n");
920 if (!NT_STATUS_IS_OK(smbldap_init(tc, ldap_uri, &ls))) {
921 d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
925 d_printf("Checking for Domain Users group.\n");
927 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
929 if (!pdb_getgrsid(&gmap, gsid)) {
930 LDAPMod **mods = NULL;
938 d_printf("Adding the Domain Users group.\n");
940 /* lets allocate a new groupid for this group */
941 if (!winbind_allocate_gid(&domusers_gid)) {
942 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
946 uname = talloc_strdup(tc, "domusers");
947 wname = talloc_strdup(tc, "Domain Users");
948 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
949 gidstr = talloc_asprintf(tc, "%d", domusers_gid);
950 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
952 if (!uname || !wname || !dn || !gidstr || !gtype) {
953 d_fprintf(stderr, "Out of Memory!\n");
957 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
958 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
959 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
960 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
961 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
962 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
963 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
965 talloc_autofree_ldapmod(tc, mods);
967 rc = smbldap_add(ls, dn, mods);
969 if (rc != LDAP_SUCCESS) {
970 d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
973 d_printf("found!\n");
978 d_printf("Checking for Domain Admins group.\n");
980 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
982 if (!pdb_getgrsid(&gmap, gsid)) {
983 LDAPMod **mods = NULL;
991 d_printf("Adding the Domain Admins group.\n");
993 /* lets allocate a new groupid for this group */
994 if (!winbind_allocate_gid(&domadmins_gid)) {
995 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
999 uname = talloc_strdup(tc, "domadmins");
1000 wname = talloc_strdup(tc, "Domain Admins");
1001 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1002 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1003 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1005 if (!uname || !wname || !dn || !gidstr || !gtype) {
1006 d_fprintf(stderr, "Out of Memory!\n");
1010 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1011 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1012 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1013 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1014 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1015 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1016 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1018 talloc_autofree_ldapmod(tc, mods);
1020 rc = smbldap_add(ls, dn, mods);
1022 if (rc != LDAP_SUCCESS) {
1023 d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
1026 d_printf("found!\n");
1031 d_printf("Check for Administrator account.\n");
1033 samuser = samu_new(tc);
1035 d_fprintf(stderr, "Out of Memory!\n");
1039 if (!pdb_getsampwnam(samuser, "Administrator")) {
1040 LDAPMod **mods = NULL;
1051 d_printf("Adding the Administrator user.\n");
1053 if (domadmins_gid == -1) {
1054 d_fprintf(stderr, "Can't create Administrtor user, Domain Admins group not available!\n");
1057 if (!winbind_allocate_uid(&uid)) {
1058 d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
1061 name = talloc_strdup(tc, "Administrator");
1062 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1063 uidstr = talloc_asprintf(tc, "%d", uid);
1064 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1065 dir = talloc_sub_specified(tc, lp_template_homedir(),
1067 get_global_sam_name(),
1068 uid, domadmins_gid);
1069 shell = talloc_sub_specified(tc, lp_template_shell(),
1071 get_global_sam_name(),
1072 uid, domadmins_gid);
1074 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1075 d_fprintf(stderr, "Out of Memory!\n");
1079 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1081 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1082 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1083 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1084 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1085 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1086 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1087 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1088 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1089 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1090 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1091 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1092 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1093 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1094 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1096 talloc_autofree_ldapmod(tc, mods);
1098 rc = smbldap_add(ls, dn, mods);
1100 if (rc != LDAP_SUCCESS) {
1101 d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
1104 d_printf("found!\n");
1107 d_printf("Checking for Guest user.\n");
1109 samuser = samu_new(tc);
1111 d_fprintf(stderr, "Out of Memory!\n");
1115 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1116 LDAPMod **mods = NULL;
1123 d_printf("Adding the Guest user.\n");
1125 pwd = getpwnam_alloc(tc, lp_guestaccount());
1128 if (domusers_gid == -1) {
1129 d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
1132 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1133 d_fprintf(stderr, "talloc failed\n");
1136 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1137 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1138 d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
1141 pwd->pw_gid = domusers_gid;
1142 pwd->pw_dir = talloc_strdup(tc, "/");
1143 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1144 if (!pwd->pw_dir || !pwd->pw_shell) {
1145 d_fprintf(stderr, "Out of Memory!\n");
1150 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1152 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1153 uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
1154 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1155 if (!dn || !uidstr || !gidstr) {
1156 d_fprintf(stderr, "Out of Memory!\n");
1160 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1161 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1162 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1163 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1164 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1165 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1166 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1167 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1168 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1169 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1170 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1171 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1172 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1173 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1175 talloc_autofree_ldapmod(tc, mods);
1177 rc = smbldap_add(ls, dn, mods);
1179 if (rc != LDAP_SUCCESS) {
1180 d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
1183 d_printf("found!\n");
1186 d_printf("Checking Guest's group.\n");
1188 pwd = getpwnam_alloc(NULL, lp_guestaccount());
1190 d_fprintf(stderr, "Failed to find just created Guest account!\n"
1191 " Is nssswitch properly configured?!\n");
1195 if (pwd->pw_gid == domusers_gid) {
1196 d_printf("found!\n");
1200 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1201 LDAPMod **mods = NULL;
1209 d_printf("Adding the Domain Guests group.\n");
1211 uname = talloc_strdup(tc, "domguests");
1212 wname = talloc_strdup(tc, "Domain Guests");
1213 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1214 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1215 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1217 if (!uname || !wname || !dn || !gidstr || !gtype) {
1218 d_fprintf(stderr, "Out of Memory!\n");
1222 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1224 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1225 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1226 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1227 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1228 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1229 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1230 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1232 talloc_autofree_ldapmod(tc, mods);
1234 rc = smbldap_add(ls, dn, mods);
1236 if (rc != LDAP_SUCCESS) {
1237 d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
1240 d_printf("found!\n");
1255 /***********************************************************
1256 migrated functionality from smbgroupedit
1257 **********************************************************/
1258 int net_sam(int argc, const char **argv)
1260 struct functable2 func[] = {
1261 { "createbuiltingroup", net_sam_createbuiltingroup,
1262 "Create a new BUILTIN group" },
1263 { "createlocalgroup", net_sam_createlocalgroup,
1264 "Create a new local group" },
1265 { "mapunixgroup", net_sam_mapunixgroup,
1266 "Map a unix group to a domain group" },
1267 { "addmem", net_sam_addmem,
1268 "Add a member to a group" },
1269 { "delmem", net_sam_delmem,
1270 "Delete a member from a group" },
1271 { "listmem", net_sam_listmem,
1272 "List group members" },
1273 { "list", net_sam_list,
1274 "List users, groups and local groups" },
1275 { "show", net_sam_show,
1276 "Show details of a SAM entry" },
1277 { "set", net_sam_set,
1278 "Set details of a SAM account" },
1279 { "policy", net_sam_policy,
1280 "Set account policies" },
1282 { "provision", net_sam_provision,
1283 "Provision a clean User Database" },
1285 { NULL, NULL, NULL }
1288 /* we shouldn't have silly checks like this */
1289 if (getuid() != 0) {
1290 d_fprintf(stderr, "You must be root to edit the SAM "
1295 return net_run_function2(argc, argv, "net sam", func);