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));
381 printf("Account policy \"%s\" value was: %d\n", account_policy,
384 if (!pdb_set_account_policy(field, value)) {
385 d_fprintf(stderr, "Setting account policy %s to %u failed \n",
386 account_policy, value);
389 printf("Account policy \"%s\" value is now: %d\n", account_policy,
395 static int net_sam_policy_show(int argc, const char **argv)
397 const char *account_policy = NULL;
402 d_fprintf(stderr, "usage: net sam policy show"
403 " \"<account policy>\" \n");
407 account_policy = argv[0];
408 field = account_policy_name_to_fieldnum(account_policy);
411 char *apn = account_policy_names_list();
412 d_fprintf(stderr, "No account policy by that name!\n");
414 d_fprintf(stderr, "Valid account policies "
421 if (!pdb_get_account_policy(field, &old_value)) {
422 fprintf(stderr, "Valid account policy, but unable to "
427 printf("Account policy \"%s\" description: %s\n",
428 account_policy, account_policy_get_desc(field));
429 printf("Account policy \"%s\" value is: %d\n", account_policy,
434 static int net_sam_policy_list(int argc, const char **argv)
436 char *apn = account_policy_names_list();
438 d_fprintf(stderr, "Valid account policies "
439 "are:\n\n%s\n", apn);
445 static int net_sam_policy(int argc, const char **argv)
447 struct functable2 func[] = {
448 { "list", net_sam_policy_list,
449 "List account policies" },
450 { "show", net_sam_policy_show,
451 "Show account policies" },
452 { "set", net_sam_policy_set,
453 "Change account policies" },
457 return net_run_function2(argc, argv, "net sam policy", func);
461 * Map a unix group to a domain group
464 static int net_sam_mapunixgroup(int argc, const char **argv)
471 d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
475 grp = getgrnam(argv[0]);
477 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
481 status = map_unix_group(grp, &map);
483 if (!NT_STATUS_IS_OK(status)) {
484 d_fprintf(stderr, "Mapping group %s failed with %s\n",
485 argv[0], nt_errstr(status));
489 d_printf("Mapped unix group %s to SID %s\n", argv[0],
490 sid_string_static(&map.sid));
496 * Create a local group
499 static int net_sam_createlocalgroup(int argc, const char **argv)
505 d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
509 if (!winbind_ping()) {
510 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
511 "only works when winbind runs.\n");
515 status = pdb_create_alias(argv[0], &rid);
517 if (!NT_STATUS_IS_OK(status)) {
518 d_fprintf(stderr, "Creating %s failed with %s\n",
519 argv[0], nt_errstr(status));
523 d_printf("Created local group %s with RID %d\n", argv[0], rid);
529 * Create a local group
532 static int net_sam_createbuiltingroup(int argc, const char **argv)
536 enum lsa_SidType type;
541 d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
545 if (!winbind_ping()) {
546 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
547 "only works when winbind runs.\n");
551 /* validate the name and get the group */
553 fstrcpy( groupname, "BUILTIN\\" );
554 fstrcat( groupname, argv[0] );
556 if ( !lookup_name(tmp_talloc_ctx(), groupname, LOOKUP_NAME_ALL, NULL,
557 NULL, &sid, &type)) {
558 d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
562 if ( !sid_peek_rid( &sid, &rid ) ) {
563 d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
567 status = pdb_create_builtin_alias( rid );
569 if (!NT_STATUS_IS_OK(status)) {
570 d_fprintf(stderr, "Creating %s failed with %s\n",
571 argv[0], nt_errstr(status));
575 d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
584 static int net_sam_addmem(int argc, const char **argv)
586 const char *groupdomain, *groupname, *memberdomain, *membername;
587 DOM_SID group, member;
588 enum lsa_SidType grouptype, membertype;
592 d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
596 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
597 &groupdomain, &groupname, &group, &grouptype)) {
598 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
602 /* check to see if the member to be added is a name or a SID */
604 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
605 &memberdomain, &membername, &member, &membertype))
607 /* try it as a SID */
609 if ( !string_to_sid( &member, argv[1] ) ) {
610 d_fprintf(stderr, "Could not find member %s\n", argv[1]);
614 if ( !lookup_sid(tmp_talloc_ctx(), &member, &memberdomain,
615 &membername, &membertype) )
617 d_fprintf(stderr, "Could not resolve SID %s\n", argv[1]);
622 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
623 if ((membertype != SID_NAME_USER) &&
624 (membertype != SID_NAME_DOM_GRP)) {
625 d_fprintf(stderr, "%s is a local group, only users "
626 "and domain groups can be added.\n"
627 "%s is a %s\n", argv[0], argv[1],
628 sid_type_lookup(membertype));
631 status = pdb_add_aliasmem(&group, &member);
633 if (!NT_STATUS_IS_OK(status)) {
634 d_fprintf(stderr, "Adding local group member failed "
635 "with %s\n", nt_errstr(status));
639 d_fprintf(stderr, "Can only add members to local groups so "
640 "far, %s is a %s\n", argv[0],
641 sid_type_lookup(grouptype));
645 d_printf("Added %s\\%s to %s\\%s\n", memberdomain, membername,
646 groupdomain, groupname);
652 * Delete a group member
655 static int net_sam_delmem(int argc, const char **argv)
657 const char *groupdomain, *groupname;
658 const char *memberdomain = NULL;
659 const char *membername = NULL;
660 DOM_SID group, member;
661 enum lsa_SidType grouptype;
665 d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
669 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
670 &groupdomain, &groupname, &group, &grouptype)) {
671 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
675 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
676 &memberdomain, &membername, &member, NULL)) {
677 if (!string_to_sid(&member, argv[1])) {
678 d_fprintf(stderr, "Could not find member %s\n",
684 if ((grouptype == SID_NAME_ALIAS) ||
685 (grouptype == SID_NAME_WKN_GRP)) {
686 status = pdb_del_aliasmem(&group, &member);
688 if (!NT_STATUS_IS_OK(status)) {
689 d_fprintf(stderr, "Deleting local group member failed "
690 "with %s\n", nt_errstr(status));
694 d_fprintf(stderr, "Can only delete members from local groups "
695 "so far, %s is a %s\n", argv[0],
696 sid_type_lookup(grouptype));
700 if (membername != NULL) {
701 d_printf("Deleted %s\\%s from %s\\%s\n",
702 memberdomain, membername, groupdomain, groupname);
704 d_printf("Deleted %s from %s\\%s\n",
705 sid_string_static(&member), groupdomain, groupname);
715 static int net_sam_listmem(int argc, const char **argv)
717 const char *groupdomain, *groupname;
719 enum lsa_SidType grouptype;
723 d_fprintf(stderr, "usage: net sam listmem <group>\n");
727 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
728 &groupdomain, &groupname, &group, &grouptype)) {
729 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
733 if ((grouptype == SID_NAME_ALIAS) ||
734 (grouptype == SID_NAME_WKN_GRP)) {
735 DOM_SID *members = NULL;
736 size_t i, num_members = 0;
738 status = pdb_enum_aliasmem(&group, &members, &num_members);
740 if (!NT_STATUS_IS_OK(status)) {
741 d_fprintf(stderr, "Listing group members failed with "
742 "%s\n", nt_errstr(status));
746 d_printf("%s\\%s has %u members\n", groupdomain, groupname,
747 (unsigned int)num_members);
748 for (i=0; i<num_members; i++) {
749 const char *dom, *name;
750 if (lookup_sid(tmp_talloc_ctx(), &members[i],
751 &dom, &name, NULL)) {
752 d_printf(" %s\\%s\n", dom, name);
755 sid_string_static(&members[i]));
759 d_fprintf(stderr, "Can only list local group members so far.\n"
760 "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
770 static int net_sam_do_list(int argc, const char **argv,
771 struct pdb_search *search, const char *what)
773 BOOL verbose = (argc == 1);
776 ((argc == 1) && !strequal(argv[0], "verbose"))) {
777 d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
781 if (search == NULL) {
782 d_fprintf(stderr, "Could not start search\n");
787 struct samr_displayentry entry;
788 if (!search->next_entry(search, &entry)) {
792 d_printf("%s:%d:%s\n",
797 d_printf("%s\n", entry.account_name);
801 search->search_end(search);
805 static int net_sam_list_users(int argc, const char **argv)
807 return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
811 static int net_sam_list_groups(int argc, const char **argv)
813 return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
816 static int net_sam_list_localgroups(int argc, const char **argv)
818 return net_sam_do_list(argc, argv,
819 pdb_search_aliases(get_global_sam_sid()),
823 static int net_sam_list_builtin(int argc, const char **argv)
825 return net_sam_do_list(argc, argv,
826 pdb_search_aliases(&global_sid_Builtin),
830 static int net_sam_list_workstations(int argc, const char **argv)
832 return net_sam_do_list(argc, argv,
833 pdb_search_users(ACB_WSTRUST),
841 static int net_sam_list(int argc, const char **argv)
843 struct functable2 func[] = {
844 { "users", net_sam_list_users,
846 { "groups", net_sam_list_groups,
848 { "localgroups", net_sam_list_localgroups,
849 "List SAM local groups" },
850 { "builtin", net_sam_list_builtin,
851 "List builtin groups" },
852 { "workstations", net_sam_list_workstations,
853 "List domain member workstations" },
857 return net_run_function2(argc, argv, "net sam list", func);
861 * Show details of SAM entries
864 static int net_sam_show(int argc, const char **argv)
867 enum lsa_SidType type;
868 const char *dom, *name;
871 d_fprintf(stderr, "usage: net sam show <name>\n");
875 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
876 &dom, &name, &sid, &type)) {
877 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
881 d_printf("%s\\%s is a %s with SID %s\n", dom, name,
882 sid_type_lookup(type), sid_string_static(&sid));
890 * Init an LDAP tree with default users and Groups
891 * if ldapsam:editposix is enabled
894 static int net_sam_provision(int argc, const char **argv)
898 char *ldap_uri = NULL;
900 struct smbldap_state *ls;
903 gid_t domusers_gid = -1;
904 gid_t domadmins_gid = -1;
905 struct samu *samuser;
908 tc = talloc_new(NULL);
910 d_fprintf(stderr, "Out of Memory!\n");
914 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
915 d_fprintf(stderr, "talloc failed\n");
919 p = strchr(ldap_bk, ':');
922 ldap_uri = talloc_strdup(tc, p+1);
923 trim_char(ldap_uri, ' ', ' ');
926 trim_char(ldap_bk, ' ', ' ');
928 if (strcmp(ldap_bk, "ldapsam") != 0) {
929 d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
933 if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
934 !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
936 d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
937 " and ldapsam:editposix are enabled.\n");
941 if (!winbind_ping()) {
942 d_fprintf(stderr, "winbind seems not to run. Provisioning "
943 "LDAP only works when winbind runs.\n");
947 if (!NT_STATUS_IS_OK(smbldap_init(tc, ldap_uri, &ls))) {
948 d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
952 d_printf("Checking for Domain Users group.\n");
954 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
956 if (!pdb_getgrsid(&gmap, gsid)) {
957 LDAPMod **mods = NULL;
965 d_printf("Adding the Domain Users group.\n");
967 /* lets allocate a new groupid for this group */
968 if (!winbind_allocate_gid(&domusers_gid)) {
969 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
973 uname = talloc_strdup(tc, "domusers");
974 wname = talloc_strdup(tc, "Domain Users");
975 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
976 gidstr = talloc_asprintf(tc, "%d", domusers_gid);
977 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
979 if (!uname || !wname || !dn || !gidstr || !gtype) {
980 d_fprintf(stderr, "Out of Memory!\n");
984 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
985 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
986 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
987 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
988 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
989 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
990 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
992 talloc_autofree_ldapmod(tc, mods);
994 rc = smbldap_add(ls, dn, mods);
996 if (rc != LDAP_SUCCESS) {
997 d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
1000 d_printf("found!\n");
1005 d_printf("Checking for Domain Admins group.\n");
1007 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
1009 if (!pdb_getgrsid(&gmap, gsid)) {
1010 LDAPMod **mods = NULL;
1018 d_printf("Adding the Domain Admins group.\n");
1020 /* lets allocate a new groupid for this group */
1021 if (!winbind_allocate_gid(&domadmins_gid)) {
1022 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
1026 uname = talloc_strdup(tc, "domadmins");
1027 wname = talloc_strdup(tc, "Domain Admins");
1028 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1029 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1030 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1032 if (!uname || !wname || !dn || !gidstr || !gtype) {
1033 d_fprintf(stderr, "Out of Memory!\n");
1037 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1038 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1039 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1040 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1041 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1042 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1043 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1045 talloc_autofree_ldapmod(tc, mods);
1047 rc = smbldap_add(ls, dn, mods);
1049 if (rc != LDAP_SUCCESS) {
1050 d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
1053 d_printf("found!\n");
1058 d_printf("Check for Administrator account.\n");
1060 samuser = samu_new(tc);
1062 d_fprintf(stderr, "Out of Memory!\n");
1066 if (!pdb_getsampwnam(samuser, "Administrator")) {
1067 LDAPMod **mods = NULL;
1078 d_printf("Adding the Administrator user.\n");
1080 if (domadmins_gid == -1) {
1081 d_fprintf(stderr, "Can't create Administrtor user, Domain Admins group not available!\n");
1084 if (!winbind_allocate_uid(&uid)) {
1085 d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
1088 name = talloc_strdup(tc, "Administrator");
1089 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1090 uidstr = talloc_asprintf(tc, "%d", uid);
1091 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1092 dir = talloc_sub_specified(tc, lp_template_homedir(),
1094 get_global_sam_name(),
1095 uid, domadmins_gid);
1096 shell = talloc_sub_specified(tc, lp_template_shell(),
1098 get_global_sam_name(),
1099 uid, domadmins_gid);
1101 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1102 d_fprintf(stderr, "Out of Memory!\n");
1106 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1108 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1109 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1110 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1111 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1112 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1113 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1114 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1115 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1116 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1117 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1118 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1119 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1120 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1121 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1123 talloc_autofree_ldapmod(tc, mods);
1125 rc = smbldap_add(ls, dn, mods);
1127 if (rc != LDAP_SUCCESS) {
1128 d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
1131 d_printf("found!\n");
1134 d_printf("Checking for Guest user.\n");
1136 samuser = samu_new(tc);
1138 d_fprintf(stderr, "Out of Memory!\n");
1142 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1143 LDAPMod **mods = NULL;
1150 d_printf("Adding the Guest user.\n");
1152 pwd = getpwnam_alloc(tc, lp_guestaccount());
1155 if (domusers_gid == -1) {
1156 d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
1159 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1160 d_fprintf(stderr, "talloc failed\n");
1163 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1164 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1165 d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
1168 pwd->pw_gid = domusers_gid;
1169 pwd->pw_dir = talloc_strdup(tc, "/");
1170 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1171 if (!pwd->pw_dir || !pwd->pw_shell) {
1172 d_fprintf(stderr, "Out of Memory!\n");
1177 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1179 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1180 uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
1181 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1182 if (!dn || !uidstr || !gidstr) {
1183 d_fprintf(stderr, "Out of Memory!\n");
1187 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1188 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1189 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1190 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1191 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1192 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1193 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1194 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1195 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1196 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1197 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1198 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1199 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1200 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1202 talloc_autofree_ldapmod(tc, mods);
1204 rc = smbldap_add(ls, dn, mods);
1206 if (rc != LDAP_SUCCESS) {
1207 d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
1210 d_printf("found!\n");
1213 d_printf("Checking Guest's group.\n");
1215 pwd = getpwnam_alloc(NULL, lp_guestaccount());
1217 d_fprintf(stderr, "Failed to find just created Guest account!\n"
1218 " Is nssswitch properly configured?!\n");
1222 if (pwd->pw_gid == domusers_gid) {
1223 d_printf("found!\n");
1227 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1228 LDAPMod **mods = NULL;
1236 d_printf("Adding the Domain Guests group.\n");
1238 uname = talloc_strdup(tc, "domguests");
1239 wname = talloc_strdup(tc, "Domain Guests");
1240 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1241 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1242 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1244 if (!uname || !wname || !dn || !gidstr || !gtype) {
1245 d_fprintf(stderr, "Out of Memory!\n");
1249 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1251 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1252 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1253 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1254 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1255 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1256 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1257 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1259 talloc_autofree_ldapmod(tc, mods);
1261 rc = smbldap_add(ls, dn, mods);
1263 if (rc != LDAP_SUCCESS) {
1264 d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
1267 d_printf("found!\n");
1282 /***********************************************************
1283 migrated functionality from smbgroupedit
1284 **********************************************************/
1285 int net_sam(int argc, const char **argv)
1287 struct functable2 func[] = {
1288 { "createbuiltingroup", net_sam_createbuiltingroup,
1289 "Create a new BUILTIN group" },
1290 { "createlocalgroup", net_sam_createlocalgroup,
1291 "Create a new local group" },
1292 { "mapunixgroup", net_sam_mapunixgroup,
1293 "Map a unix group to a domain group" },
1294 { "addmem", net_sam_addmem,
1295 "Add a member to a group" },
1296 { "delmem", net_sam_delmem,
1297 "Delete a member from a group" },
1298 { "listmem", net_sam_listmem,
1299 "List group members" },
1300 { "list", net_sam_list,
1301 "List users, groups and local groups" },
1302 { "show", net_sam_show,
1303 "Show details of a SAM entry" },
1304 { "set", net_sam_set,
1305 "Set details of a SAM account" },
1306 { "policy", net_sam_policy,
1307 "Set account policies" },
1309 { "provision", net_sam_provision,
1310 "Provision a clean User Database" },
1312 { NULL, NULL, NULL }
1315 /* we shouldn't have silly checks like this */
1316 if (getuid() != 0) {
1317 d_fprintf(stderr, "You must be root to edit the SAM "
1322 return net_run_function2(argc, argv, "net sam", func);