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 SID_NAME_USE 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 SID_NAME_USE 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 a user's time field
212 static int net_sam_set_time(int argc, const char **argv, const char *field,
213 BOOL (*fn)(struct samu *, time_t,
214 enum pdb_value_state))
216 struct samu *sam_acct = NULL;
218 enum SID_NAME_USE type;
219 const char *dom, *name;
224 d_fprintf(stderr, "usage: net sam set %s <user> "
225 "[now|YYYY-MM-DD HH:MM]\n", field);
229 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
230 &dom, &name, &sid, &type)) {
231 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
235 if (type != SID_NAME_USER) {
236 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
237 sid_type_lookup(type));
241 if (strequal(argv[1], "now")) {
242 new_time = time(NULL);
247 end = strptime(argv[1], "%Y-%m-%d %H:%M", &tm);
248 new_time = mktime(&tm);
249 if ((end == NULL) || (*end != '\0') || (new_time == -1)) {
250 d_fprintf(stderr, "Could not parse time string %s\n",
257 if ( !(sam_acct = samu_new( NULL )) ) {
258 d_fprintf(stderr, "Internal error\n");
262 if (!pdb_getsampwsid(sam_acct, &sid)) {
263 d_fprintf(stderr, "Loading user %s failed\n", argv[0]);
267 if (!fn(sam_acct, new_time, PDB_CHANGED)) {
268 d_fprintf(stderr, "Internal error\n");
272 status = pdb_update_sam_account(sam_acct);
273 if (!NT_STATUS_IS_OK(status)) {
274 d_fprintf(stderr, "Updating sam account %s failed with %s\n",
275 argv[0], nt_errstr(status));
279 TALLOC_FREE(sam_acct);
281 d_printf("Updated %s for %s\\%s to %s\n", field, dom, name, argv[1]);
285 static int net_sam_set_pwdmustchange(int argc, const char **argv)
287 return net_sam_set_time(argc, argv, "pwdmustchange",
288 pdb_set_pass_must_change_time);
291 static int net_sam_set_pwdcanchange(int argc, const char **argv)
293 return net_sam_set_time(argc, argv, "pwdcanchange",
294 pdb_set_pass_can_change_time);
298 * Set a user's or a group's comment
301 static int net_sam_set_comment(int argc, const char **argv)
305 enum SID_NAME_USE type;
306 const char *dom, *name;
310 d_fprintf(stderr, "usage: net sam set comment <name> "
315 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
316 &dom, &name, &sid, &type)) {
317 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
321 if (type == SID_NAME_USER) {
322 return net_sam_userset(argc, argv, "comment",
326 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
327 (type != SID_NAME_WKN_GRP)) {
328 d_fprintf(stderr, "%s is a %s, not a group\n", argv[0],
329 sid_type_lookup(type));
333 if (!pdb_getgrsid(&map, sid)) {
334 d_fprintf(stderr, "Could not load group %s\n", argv[0]);
338 fstrcpy(map.comment, argv[1]);
340 status = pdb_update_group_mapping_entry(&map);
342 if (!NT_STATUS_IS_OK(status)) {
343 d_fprintf(stderr, "Updating group mapping entry failed with "
344 "%s\n", nt_errstr(status));
348 d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
354 static int net_sam_set(int argc, const char **argv)
356 struct functable2 func[] = {
357 { "homedir", net_sam_set_homedir,
358 "Change a user's home directory" },
359 { "profilepath", net_sam_set_profilepath,
360 "Change a user's profile path" },
361 { "comment", net_sam_set_comment,
362 "Change a users or groups description" },
363 { "fullname", net_sam_set_fullname,
364 "Change a user's full name" },
365 { "logonscript", net_sam_set_logonscript,
366 "Change a user's logon script" },
367 { "homedrive", net_sam_set_homedrive,
368 "Change a user's home drive" },
369 { "workstations", net_sam_set_workstations,
370 "Change a user's allowed workstations" },
371 { "disabled", net_sam_set_disabled,
372 "Disable/Enable a user" },
373 { "pwnotreq", net_sam_set_pwnotreq,
374 "Disable/Enable the password not required flag" },
375 { "autolock", net_sam_set_autolock,
376 "Disable/Enable a user's lockout flag" },
377 { "pwnoexp", net_sam_set_pwnoexp,
378 "Disable/Enable whether a user's pw does not expire" },
379 { "pwdmustchange", net_sam_set_pwdmustchange,
380 "Set a users password must change time" },
381 { "pwdcanchange", net_sam_set_pwdcanchange,
382 "Set a users password can change time" },
386 return net_run_function2(argc, argv, "net sam set", func);
390 * Map a unix group to a domain group
393 static int net_sam_mapunixgroup(int argc, const char **argv)
400 d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
404 grp = getgrnam(argv[0]);
406 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
410 status = map_unix_group(grp, &map);
412 if (!NT_STATUS_IS_OK(status)) {
413 d_fprintf(stderr, "Mapping group %s failed with %s\n",
414 argv[0], nt_errstr(status));
418 d_printf("Mapped unix group %s to SID %s\n", argv[0],
419 sid_string_static(&map.sid));
425 * Create a local group
428 static int net_sam_createlocalgroup(int argc, const char **argv)
434 d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
438 if (!winbind_ping()) {
439 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
440 "only works when winbind runs.\n");
444 status = pdb_create_alias(argv[0], &rid);
446 if (!NT_STATUS_IS_OK(status)) {
447 d_fprintf(stderr, "Creating %s failed with %s\n",
448 argv[0], nt_errstr(status));
452 d_printf("Created local group %s with RID %d\n", argv[0], rid);
458 * Create a local group
461 static int net_sam_createbuiltingroup(int argc, const char **argv)
466 enum SID_NAME_USE type;
471 d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
475 if (!winbind_ping()) {
476 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
477 "only works when winbind runs.\n");
481 if ( (ctx = talloc_init("net_sam_createbuiltingroup")) == NULL ) {
482 d_fprintf( stderr, "Memory allocation error\n");
486 /* validate the name and get the group */
488 fstrcpy( groupname, "BUILTIN\\" );
489 fstrcat( groupname, argv[0] );
491 if ( !lookup_name(ctx, groupname, LOOKUP_NAME_ALL, NULL, NULL, &sid, &type)) {
492 d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
496 if ( !sid_peek_rid( &sid, &rid ) ) {
497 d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
501 status = pdb_create_builtin_alias( rid );
503 if (!NT_STATUS_IS_OK(status)) {
504 d_fprintf(stderr, "Creating %s failed with %s\n",
505 argv[0], nt_errstr(status));
509 d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
518 static int net_sam_addmem(int argc, const char **argv)
520 const char *groupdomain, *groupname, *memberdomain, *membername;
521 DOM_SID group, member;
522 enum SID_NAME_USE grouptype, membertype;
526 d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
530 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
531 &groupdomain, &groupname, &group, &grouptype)) {
532 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
536 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
537 &memberdomain, &membername, &member, &membertype)) {
538 d_fprintf(stderr, "Could not find member %s\n", argv[1]);
542 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
543 if ((membertype != SID_NAME_USER) &&
544 (membertype != SID_NAME_DOM_GRP)) {
545 d_fprintf(stderr, "%s is a local group, only users "
546 "and domain groups can be added.\n"
547 "%s is a %s\n", argv[0], argv[1],
548 sid_type_lookup(membertype));
551 status = pdb_add_aliasmem(&group, &member);
553 if (!NT_STATUS_IS_OK(status)) {
554 d_fprintf(stderr, "Adding local group member failed "
555 "with %s\n", nt_errstr(status));
559 d_fprintf(stderr, "Can only add members to local groups so "
560 "far, %s is a %s\n", argv[0],
561 sid_type_lookup(grouptype));
565 d_printf("Added %s\\%s to %s\\%s\n",
566 memberdomain, membername, groupdomain, groupname);
572 * Delete a group member
575 static int net_sam_delmem(int argc, const char **argv)
577 const char *groupdomain, *groupname;
578 const char *memberdomain = NULL;
579 const char *membername = NULL;
580 DOM_SID group, member;
581 enum SID_NAME_USE grouptype;
585 d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
589 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
590 &groupdomain, &groupname, &group, &grouptype)) {
591 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
595 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
596 &memberdomain, &membername, &member, NULL)) {
597 if (!string_to_sid(&member, argv[1])) {
598 d_fprintf(stderr, "Could not find member %s\n",
604 if ((grouptype == SID_NAME_ALIAS) ||
605 (grouptype == SID_NAME_WKN_GRP)) {
606 status = pdb_del_aliasmem(&group, &member);
608 if (!NT_STATUS_IS_OK(status)) {
609 d_fprintf(stderr, "Deleting local group member failed "
610 "with %s\n", nt_errstr(status));
614 d_fprintf(stderr, "Can only delete members from local groups "
615 "so far, %s is a %s\n", argv[0],
616 sid_type_lookup(grouptype));
620 if (membername != NULL) {
621 d_printf("Deleted %s\\%s from %s\\%s\n",
622 memberdomain, membername, groupdomain, groupname);
624 d_printf("Deleted %s from %s\\%s\n",
625 sid_string_static(&member), groupdomain, groupname);
635 static int net_sam_listmem(int argc, const char **argv)
637 const char *groupdomain, *groupname;
639 enum SID_NAME_USE grouptype;
643 d_fprintf(stderr, "usage: net sam listmem <group>\n");
647 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
648 &groupdomain, &groupname, &group, &grouptype)) {
649 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
653 if ((grouptype == SID_NAME_ALIAS) ||
654 (grouptype == SID_NAME_WKN_GRP)) {
655 DOM_SID *members = NULL;
656 size_t i, num_members = 0;
658 status = pdb_enum_aliasmem(&group, &members, &num_members);
660 if (!NT_STATUS_IS_OK(status)) {
661 d_fprintf(stderr, "Listing group members failed with "
662 "%s\n", nt_errstr(status));
666 d_printf("%s\\%s has %d members\n", groupdomain, groupname,
668 for (i=0; i<num_members; i++) {
669 const char *dom, *name;
670 if (lookup_sid(tmp_talloc_ctx(), &members[i],
671 &dom, &name, NULL)) {
672 d_printf(" %s\\%s\n", dom, name);
675 sid_string_static(&members[i]));
679 d_fprintf(stderr, "Can only list local group members so far.\n"
680 "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
690 static int net_sam_do_list(int argc, const char **argv,
691 struct pdb_search *search, const char *what)
693 BOOL verbose = (argc == 1);
696 ((argc == 1) && !strequal(argv[0], "verbose"))) {
697 d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
701 if (search == NULL) {
702 d_fprintf(stderr, "Could not start search\n");
707 struct samr_displayentry entry;
708 if (!search->next_entry(search, &entry)) {
712 d_printf("%s:%d:%s\n",
717 d_printf("%s\n", entry.account_name);
721 search->search_end(search);
725 static int net_sam_list_users(int argc, const char **argv)
727 return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
731 static int net_sam_list_groups(int argc, const char **argv)
733 return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
736 static int net_sam_list_localgroups(int argc, const char **argv)
738 return net_sam_do_list(argc, argv,
739 pdb_search_aliases(get_global_sam_sid()),
743 static int net_sam_list_builtin(int argc, const char **argv)
745 return net_sam_do_list(argc, argv,
746 pdb_search_aliases(&global_sid_Builtin),
750 static int net_sam_list_workstations(int argc, const char **argv)
752 return net_sam_do_list(argc, argv,
753 pdb_search_users(ACB_WSTRUST),
761 static int net_sam_list(int argc, const char **argv)
763 struct functable2 func[] = {
764 { "users", net_sam_list_users,
766 { "groups", net_sam_list_groups,
768 { "localgroups", net_sam_list_localgroups,
769 "List SAM local groups" },
770 { "builtin", net_sam_list_builtin,
771 "List builtin groups" },
772 { "workstations", net_sam_list_workstations,
773 "List domain member workstations" },
777 return net_run_function2(argc, argv, "net sam list", func);
781 * Show details of SAM entries
784 static int net_sam_show(int argc, const char **argv)
787 enum SID_NAME_USE type;
788 const char *dom, *name;
791 d_fprintf(stderr, "usage: net sam show <name>\n");
795 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
796 &dom, &name, &sid, &type)) {
797 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
801 d_printf("%s\\%s is a %s with SID %s\n", dom, name,
802 sid_type_lookup(type), sid_string_static(&sid));
810 * Init an LDAP tree with default users and Groups
811 * if ldapsam:editposix is enabled
814 static int net_sam_provision(int argc, const char **argv)
818 char *ldap_uri = NULL;
820 struct smbldap_state *ls;
823 gid_t domusers_gid = -1;
824 gid_t domadmins_gid = -1;
825 struct samu *samuser;
828 tc = talloc_new(NULL);
830 d_fprintf(stderr, "Out of Memory!\n");
834 ldap_bk = talloc_strdup(tc, lp_passdb_backend());
835 p = strchr(ldap_bk, ':');
838 ldap_uri = talloc_strdup(tc, p+1);
839 trim_char(ldap_uri, ' ', ' ');
842 trim_char(ldap_bk, ' ', ' ');
844 if (strcmp(ldap_bk, "ldapsam") != 0) {
845 d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
849 if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
850 !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
852 d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
853 " and ldapsam:editposix are enabled.\n");
857 if (!winbind_ping()) {
858 d_fprintf(stderr, "winbind seems not to run. Provisioning "
859 "LDAP only works when winbind runs.\n");
863 if (!NT_STATUS_IS_OK(smbldap_init(tc, ldap_uri, &ls))) {
864 d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
868 d_printf("Checking for Domain Users group.\n");
870 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
872 if (!pdb_getgrsid(&gmap, gsid)) {
873 LDAPMod **mods = NULL;
881 d_printf("Adding the Domain Users group.\n");
883 /* lets allocate a new groupid for this group */
884 if (!winbind_allocate_gid(&domusers_gid)) {
885 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
889 uname = talloc_strdup(tc, "domusers");
890 wname = talloc_strdup(tc, "Domain Users");
891 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
892 gidstr = talloc_asprintf(tc, "%d", domusers_gid);
893 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
895 if (!uname || !wname || !dn || !gidstr || !gtype) {
896 d_fprintf(stderr, "Out of Memory!\n");
900 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
901 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
902 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
903 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
904 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
905 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
906 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
908 talloc_autofree_ldapmod(tc, mods);
910 rc = smbldap_add(ls, dn, mods);
912 if (rc != LDAP_SUCCESS) {
913 d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
916 d_printf("found!\n");
921 d_printf("Checking for Domain Admins group.\n");
923 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
925 if (!pdb_getgrsid(&gmap, gsid)) {
926 LDAPMod **mods = NULL;
934 d_printf("Adding the Domain Admins group.\n");
936 /* lets allocate a new groupid for this group */
937 if (!winbind_allocate_gid(&domadmins_gid)) {
938 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
942 uname = talloc_strdup(tc, "domadmins");
943 wname = talloc_strdup(tc, "Domain Admins");
944 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
945 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
946 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
948 if (!uname || !wname || !dn || !gidstr || !gtype) {
949 d_fprintf(stderr, "Out of Memory!\n");
953 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
954 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
955 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
956 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
957 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
958 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
959 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
961 talloc_autofree_ldapmod(tc, mods);
963 rc = smbldap_add(ls, dn, mods);
965 if (rc != LDAP_SUCCESS) {
966 d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
969 d_printf("found!\n");
974 d_printf("Check for Administrator account.\n");
976 samuser = samu_new(tc);
978 d_fprintf(stderr, "Out of Memory!\n");
982 if (!pdb_getsampwnam(samuser, "Administrator")) {
983 LDAPMod **mods = NULL;
994 d_printf("Adding the Administrator user.\n");
996 if (domadmins_gid == -1) {
997 d_fprintf(stderr, "Can't create Administrtor user, Domain Admins group not available!\n");
1000 if (!winbind_allocate_uid(&uid)) {
1001 d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
1004 name = talloc_strdup(tc, "Administrator");
1005 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1006 uidstr = talloc_asprintf(tc, "%d", uid);
1007 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1008 dir = talloc_sub_specified(tc, lp_template_homedir(),
1010 get_global_sam_name(),
1011 uid, domadmins_gid);
1012 shell = talloc_sub_specified(tc, lp_template_shell(),
1014 get_global_sam_name(),
1015 uid, domadmins_gid);
1017 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1018 d_fprintf(stderr, "Out of Memory!\n");
1022 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1024 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1025 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1026 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1027 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1028 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1029 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1030 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1031 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1032 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1033 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1034 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1035 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1036 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1037 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1039 talloc_autofree_ldapmod(tc, mods);
1041 rc = smbldap_add(ls, dn, mods);
1043 if (rc != LDAP_SUCCESS) {
1044 d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
1047 d_printf("found!\n");
1050 d_printf("Checking for Guest user.\n");
1052 samuser = samu_new(tc);
1054 d_fprintf(stderr, "Out of Memory!\n");
1058 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1059 LDAPMod **mods = NULL;
1066 d_printf("Adding the Guest user.\n");
1068 pwd = getpwnam_alloc(tc, lp_guestaccount());
1071 if (domusers_gid == -1) {
1072 d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
1075 pwd = talloc(tc, struct passwd);
1076 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1077 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1078 d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
1081 pwd->pw_gid = domusers_gid;
1082 pwd->pw_dir = talloc_strdup(tc, "/");
1083 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1084 if (!pwd->pw_dir || !pwd->pw_shell) {
1085 d_fprintf(stderr, "Out of Memory!\n");
1090 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1092 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1093 uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
1094 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1095 if (!dn || !uidstr || !gidstr) {
1096 d_fprintf(stderr, "Out of Memory!\n");
1100 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1101 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1102 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1103 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1104 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1105 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1106 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1107 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1108 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1109 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1110 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1111 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1112 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1113 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1115 talloc_autofree_ldapmod(tc, mods);
1117 rc = smbldap_add(ls, dn, mods);
1119 if (rc != LDAP_SUCCESS) {
1120 d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
1123 d_printf("found!\n");
1126 d_printf("Checking Guest's group.\n");
1128 pwd = getpwnam_alloc(NULL, lp_guestaccount());
1130 d_fprintf(stderr, "Failed to find just created Guest account!\n"
1131 " Is nssswitch properly configured?!\n");
1135 if (pwd->pw_gid == domusers_gid) {
1136 d_printf("found!\n");
1140 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1141 LDAPMod **mods = NULL;
1149 d_printf("Adding the Domain Guests group.\n");
1151 uname = talloc_strdup(tc, "domguests");
1152 wname = talloc_strdup(tc, "Domain Guests");
1153 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1154 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1155 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1157 if (!uname || !wname || !dn || !gidstr || !gtype) {
1158 d_fprintf(stderr, "Out of Memory!\n");
1162 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1164 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1165 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1166 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1167 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1168 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1169 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1170 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1172 talloc_autofree_ldapmod(tc, mods);
1174 rc = smbldap_add(ls, dn, mods);
1176 if (rc != LDAP_SUCCESS) {
1177 d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
1180 d_printf("found!\n");
1194 /***********************************************************
1195 migrated functionality from smbgroupedit
1196 **********************************************************/
1197 int net_sam(int argc, const char **argv)
1199 struct functable2 func[] = {
1200 { "createbuiltingroup", net_sam_createbuiltingroup,
1201 "Create a new BUILTIN group" },
1202 { "createlocalgroup", net_sam_createlocalgroup,
1203 "Create a new local group" },
1204 { "mapunixgroup", net_sam_mapunixgroup,
1205 "Map a unix group to a domain group" },
1206 { "addmem", net_sam_addmem,
1207 "Add a member to a group" },
1208 { "delmem", net_sam_delmem,
1209 "Delete a member from a group" },
1210 { "listmem", net_sam_listmem,
1211 "List group members" },
1212 { "list", net_sam_list,
1213 "List users, groups and local groups" },
1214 { "show", net_sam_show,
1215 "Show details of a SAM entry" },
1216 { "set", net_sam_set,
1217 "Set details of a SAM account" },
1219 { "provision", net_sam_provision,
1220 "Provision a clean User Database" },
1222 { NULL, NULL, NULL }
1225 /* we shouldn't have silly checks like this */
1226 if (getuid() != 0) {
1227 d_fprintf(stderr, "You must be root to edit the SAM "
1232 return net_run_function2(argc, argv, "net sam", func);