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)
465 enum SID_NAME_USE type;
470 d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
474 if (!winbind_ping()) {
475 d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
476 "only works when winbind runs.\n");
480 /* validate the name and get the group */
482 fstrcpy( groupname, "BUILTIN\\" );
483 fstrcat( groupname, argv[0] );
485 if ( !lookup_name(tmp_talloc_ctx(), groupname, LOOKUP_NAME_ALL, NULL,
486 NULL, &sid, &type)) {
487 d_fprintf(stderr, "%s is not a BUILTIN group\n", argv[0]);
491 if ( !sid_peek_rid( &sid, &rid ) ) {
492 d_fprintf(stderr, "Failed to get RID for %s\n", argv[0]);
496 status = pdb_create_builtin_alias( rid );
498 if (!NT_STATUS_IS_OK(status)) {
499 d_fprintf(stderr, "Creating %s failed with %s\n",
500 argv[0], nt_errstr(status));
504 d_printf("Created BUILTIN group %s with RID %d\n", argv[0], rid);
513 static int net_sam_addmem(int argc, const char **argv)
515 const char *groupdomain, *groupname, *memberdomain, *membername;
516 DOM_SID group, member;
517 enum SID_NAME_USE grouptype, membertype;
521 d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
525 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
526 &groupdomain, &groupname, &group, &grouptype)) {
527 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
531 /* check to see if the member to be added is a name or a SID */
533 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
534 &memberdomain, &membername, &member, &membertype))
536 /* try it as a SID */
538 if ( !string_to_sid( &member, argv[1] ) ) {
539 d_fprintf(stderr, "Could not find member %s\n", argv[1]);
543 if ( !lookup_sid(tmp_talloc_ctx(), &member, &memberdomain,
544 &membername, &membertype) )
546 d_fprintf(stderr, "Could not resolve SID %s\n", argv[1]);
551 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
552 if ((membertype != SID_NAME_USER) &&
553 (membertype != SID_NAME_DOM_GRP)) {
554 d_fprintf(stderr, "%s is a local group, only users "
555 "and domain groups can be added.\n"
556 "%s is a %s\n", argv[0], argv[1],
557 sid_type_lookup(membertype));
560 status = pdb_add_aliasmem(&group, &member);
562 if (!NT_STATUS_IS_OK(status)) {
563 d_fprintf(stderr, "Adding local group member failed "
564 "with %s\n", nt_errstr(status));
568 d_fprintf(stderr, "Can only add members to local groups so "
569 "far, %s is a %s\n", argv[0],
570 sid_type_lookup(grouptype));
574 d_printf("Added %s\\%s to %s\\%s\n", memberdomain, membername,
575 groupdomain, groupname);
581 * Delete a group member
584 static int net_sam_delmem(int argc, const char **argv)
586 const char *groupdomain, *groupname;
587 const char *memberdomain = NULL;
588 const char *membername = NULL;
589 DOM_SID group, member;
590 enum SID_NAME_USE grouptype;
594 d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
598 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
599 &groupdomain, &groupname, &group, &grouptype)) {
600 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
604 if (!lookup_name(tmp_talloc_ctx(), argv[1], LOOKUP_NAME_ISOLATED,
605 &memberdomain, &membername, &member, NULL)) {
606 if (!string_to_sid(&member, argv[1])) {
607 d_fprintf(stderr, "Could not find member %s\n",
613 if ((grouptype == SID_NAME_ALIAS) ||
614 (grouptype == SID_NAME_WKN_GRP)) {
615 status = pdb_del_aliasmem(&group, &member);
617 if (!NT_STATUS_IS_OK(status)) {
618 d_fprintf(stderr, "Deleting local group member failed "
619 "with %s\n", nt_errstr(status));
623 d_fprintf(stderr, "Can only delete members from local groups "
624 "so far, %s is a %s\n", argv[0],
625 sid_type_lookup(grouptype));
629 if (membername != NULL) {
630 d_printf("Deleted %s\\%s from %s\\%s\n",
631 memberdomain, membername, groupdomain, groupname);
633 d_printf("Deleted %s from %s\\%s\n",
634 sid_string_static(&member), groupdomain, groupname);
644 static int net_sam_listmem(int argc, const char **argv)
646 const char *groupdomain, *groupname;
648 enum SID_NAME_USE grouptype;
652 d_fprintf(stderr, "usage: net sam listmem <group>\n");
656 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
657 &groupdomain, &groupname, &group, &grouptype)) {
658 d_fprintf(stderr, "Could not find group %s\n", argv[0]);
662 if ((grouptype == SID_NAME_ALIAS) ||
663 (grouptype == SID_NAME_WKN_GRP)) {
664 DOM_SID *members = NULL;
665 size_t i, num_members = 0;
667 status = pdb_enum_aliasmem(&group, &members, &num_members);
669 if (!NT_STATUS_IS_OK(status)) {
670 d_fprintf(stderr, "Listing group members failed with "
671 "%s\n", nt_errstr(status));
675 d_printf("%s\\%s has %d members\n", groupdomain, groupname,
677 for (i=0; i<num_members; i++) {
678 const char *dom, *name;
679 if (lookup_sid(tmp_talloc_ctx(), &members[i],
680 &dom, &name, NULL)) {
681 d_printf(" %s\\%s\n", dom, name);
684 sid_string_static(&members[i]));
688 d_fprintf(stderr, "Can only list local group members so far.\n"
689 "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
699 static int net_sam_do_list(int argc, const char **argv,
700 struct pdb_search *search, const char *what)
702 BOOL verbose = (argc == 1);
705 ((argc == 1) && !strequal(argv[0], "verbose"))) {
706 d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
710 if (search == NULL) {
711 d_fprintf(stderr, "Could not start search\n");
716 struct samr_displayentry entry;
717 if (!search->next_entry(search, &entry)) {
721 d_printf("%s:%d:%s\n",
726 d_printf("%s\n", entry.account_name);
730 search->search_end(search);
734 static int net_sam_list_users(int argc, const char **argv)
736 return net_sam_do_list(argc, argv, pdb_search_users(ACB_NORMAL),
740 static int net_sam_list_groups(int argc, const char **argv)
742 return net_sam_do_list(argc, argv, pdb_search_groups(), "groups");
745 static int net_sam_list_localgroups(int argc, const char **argv)
747 return net_sam_do_list(argc, argv,
748 pdb_search_aliases(get_global_sam_sid()),
752 static int net_sam_list_builtin(int argc, const char **argv)
754 return net_sam_do_list(argc, argv,
755 pdb_search_aliases(&global_sid_Builtin),
759 static int net_sam_list_workstations(int argc, const char **argv)
761 return net_sam_do_list(argc, argv,
762 pdb_search_users(ACB_WSTRUST),
770 static int net_sam_list(int argc, const char **argv)
772 struct functable2 func[] = {
773 { "users", net_sam_list_users,
775 { "groups", net_sam_list_groups,
777 { "localgroups", net_sam_list_localgroups,
778 "List SAM local groups" },
779 { "builtin", net_sam_list_builtin,
780 "List builtin groups" },
781 { "workstations", net_sam_list_workstations,
782 "List domain member workstations" },
786 return net_run_function2(argc, argv, "net sam list", func);
790 * Show details of SAM entries
793 static int net_sam_show(int argc, const char **argv)
796 enum SID_NAME_USE type;
797 const char *dom, *name;
800 d_fprintf(stderr, "usage: net sam show <name>\n");
804 if (!lookup_name(tmp_talloc_ctx(), argv[0], LOOKUP_NAME_ISOLATED,
805 &dom, &name, &sid, &type)) {
806 d_fprintf(stderr, "Could not find name %s\n", argv[0]);
810 d_printf("%s\\%s is a %s with SID %s\n", dom, name,
811 sid_type_lookup(type), sid_string_static(&sid));
819 * Init an LDAP tree with default users and Groups
820 * if ldapsam:editposix is enabled
823 static int net_sam_provision(int argc, const char **argv)
827 char *ldap_uri = NULL;
829 struct smbldap_state *ls;
832 gid_t domusers_gid = -1;
833 gid_t domadmins_gid = -1;
834 struct samu *samuser;
837 tc = talloc_new(NULL);
839 d_fprintf(stderr, "Out of Memory!\n");
843 ldap_bk = talloc_strdup(tc, lp_passdb_backend());
844 p = strchr(ldap_bk, ':');
847 ldap_uri = talloc_strdup(tc, p+1);
848 trim_char(ldap_uri, ' ', ' ');
851 trim_char(ldap_bk, ' ', ' ');
853 if (strcmp(ldap_bk, "ldapsam") != 0) {
854 d_fprintf(stderr, "Provisioning works only with ldapsam backend\n");
858 if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
859 !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
861 d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
862 " and ldapsam:editposix are enabled.\n");
866 if (!winbind_ping()) {
867 d_fprintf(stderr, "winbind seems not to run. Provisioning "
868 "LDAP only works when winbind runs.\n");
872 if (!NT_STATUS_IS_OK(smbldap_init(tc, ldap_uri, &ls))) {
873 d_fprintf(stderr, "Unable to connect to the LDAP server.\n");
877 d_printf("Checking for Domain Users group.\n");
879 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
881 if (!pdb_getgrsid(&gmap, gsid)) {
882 LDAPMod **mods = NULL;
890 d_printf("Adding the Domain Users group.\n");
892 /* lets allocate a new groupid for this group */
893 if (!winbind_allocate_gid(&domusers_gid)) {
894 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Users group!\n");
898 uname = talloc_strdup(tc, "domusers");
899 wname = talloc_strdup(tc, "Domain Users");
900 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
901 gidstr = talloc_asprintf(tc, "%d", domusers_gid);
902 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
904 if (!uname || !wname || !dn || !gidstr || !gtype) {
905 d_fprintf(stderr, "Out of Memory!\n");
909 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
910 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
911 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
912 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
913 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
914 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
915 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
917 talloc_autofree_ldapmod(tc, mods);
919 rc = smbldap_add(ls, dn, mods);
921 if (rc != LDAP_SUCCESS) {
922 d_fprintf(stderr, "Failed to add Domain Users group to ldap directory\n");
925 d_printf("found!\n");
930 d_printf("Checking for Domain Admins group.\n");
932 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
934 if (!pdb_getgrsid(&gmap, gsid)) {
935 LDAPMod **mods = NULL;
943 d_printf("Adding the Domain Admins group.\n");
945 /* lets allocate a new groupid for this group */
946 if (!winbind_allocate_gid(&domadmins_gid)) {
947 d_fprintf(stderr, "Unable to allocate a new gid to create Domain Admins group!\n");
951 uname = talloc_strdup(tc, "domadmins");
952 wname = talloc_strdup(tc, "Domain Admins");
953 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
954 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
955 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
957 if (!uname || !wname || !dn || !gidstr || !gtype) {
958 d_fprintf(stderr, "Out of Memory!\n");
962 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
963 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
964 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
965 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
966 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
967 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
968 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
970 talloc_autofree_ldapmod(tc, mods);
972 rc = smbldap_add(ls, dn, mods);
974 if (rc != LDAP_SUCCESS) {
975 d_fprintf(stderr, "Failed to add Domain Admins group to ldap directory\n");
978 d_printf("found!\n");
983 d_printf("Check for Administrator account.\n");
985 samuser = samu_new(tc);
987 d_fprintf(stderr, "Out of Memory!\n");
991 if (!pdb_getsampwnam(samuser, "Administrator")) {
992 LDAPMod **mods = NULL;
1003 d_printf("Adding the Administrator user.\n");
1005 if (domadmins_gid == -1) {
1006 d_fprintf(stderr, "Can't create Administrtor user, Domain Admins group not available!\n");
1009 if (!winbind_allocate_uid(&uid)) {
1010 d_fprintf(stderr, "Unable to allocate a new uid to create the Administrator user!\n");
1013 name = talloc_strdup(tc, "Administrator");
1014 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1015 uidstr = talloc_asprintf(tc, "%d", uid);
1016 gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
1017 dir = talloc_sub_specified(tc, lp_template_homedir(),
1019 get_global_sam_name(),
1020 uid, domadmins_gid);
1021 shell = talloc_sub_specified(tc, lp_template_shell(),
1023 get_global_sam_name(),
1024 uid, domadmins_gid);
1026 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1027 d_fprintf(stderr, "Out of Memory!\n");
1031 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1033 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1034 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1035 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1036 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1037 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1038 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1039 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1040 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1041 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1042 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1043 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1044 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1045 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1046 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1048 talloc_autofree_ldapmod(tc, mods);
1050 rc = smbldap_add(ls, dn, mods);
1052 if (rc != LDAP_SUCCESS) {
1053 d_fprintf(stderr, "Failed to add Administrator user to ldap directory\n");
1056 d_printf("found!\n");
1059 d_printf("Checking for Guest user.\n");
1061 samuser = samu_new(tc);
1063 d_fprintf(stderr, "Out of Memory!\n");
1067 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1068 LDAPMod **mods = NULL;
1075 d_printf("Adding the Guest user.\n");
1077 pwd = getpwnam_alloc(tc, lp_guestaccount());
1080 if (domusers_gid == -1) {
1081 d_fprintf(stderr, "Can't create Guest user, Domain Users group not available!\n");
1084 pwd = talloc(tc, struct passwd);
1085 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1086 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1087 d_fprintf(stderr, "Unable to allocate a new uid to create the Guest user!\n");
1090 pwd->pw_gid = domusers_gid;
1091 pwd->pw_dir = talloc_strdup(tc, "/");
1092 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1093 if (!pwd->pw_dir || !pwd->pw_shell) {
1094 d_fprintf(stderr, "Out of Memory!\n");
1099 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1101 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1102 uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
1103 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1104 if (!dn || !uidstr || !gidstr) {
1105 d_fprintf(stderr, "Out of Memory!\n");
1109 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1110 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1111 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1112 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1113 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1114 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1115 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1116 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1117 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1118 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1119 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID", sid_string_static(&sid));
1120 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1121 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1122 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1124 talloc_autofree_ldapmod(tc, mods);
1126 rc = smbldap_add(ls, dn, mods);
1128 if (rc != LDAP_SUCCESS) {
1129 d_fprintf(stderr, "Failed to add Guest user to ldap directory\n");
1132 d_printf("found!\n");
1135 d_printf("Checking Guest's group.\n");
1137 pwd = getpwnam_alloc(NULL, lp_guestaccount());
1139 d_fprintf(stderr, "Failed to find just created Guest account!\n"
1140 " Is nssswitch properly configured?!\n");
1144 if (pwd->pw_gid == domusers_gid) {
1145 d_printf("found!\n");
1149 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1150 LDAPMod **mods = NULL;
1158 d_printf("Adding the Domain Guests group.\n");
1160 uname = talloc_strdup(tc, "domguests");
1161 wname = talloc_strdup(tc, "Domain Guests");
1162 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1163 gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
1164 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1166 if (!uname || !wname || !dn || !gidstr || !gtype) {
1167 d_fprintf(stderr, "Out of Memory!\n");
1171 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1173 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_POSIXGROUP);
1174 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1175 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1176 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1177 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1178 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", sid_string_static(&gsid));
1179 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1181 talloc_autofree_ldapmod(tc, mods);
1183 rc = smbldap_add(ls, dn, mods);
1185 if (rc != LDAP_SUCCESS) {
1186 d_fprintf(stderr, "Failed to add Domain Guests group to ldap directory\n");
1189 d_printf("found!\n");
1204 /***********************************************************
1205 migrated functionality from smbgroupedit
1206 **********************************************************/
1207 int net_sam(int argc, const char **argv)
1209 struct functable2 func[] = {
1210 { "createbuiltingroup", net_sam_createbuiltingroup,
1211 "Create a new BUILTIN group" },
1212 { "createlocalgroup", net_sam_createlocalgroup,
1213 "Create a new local group" },
1214 { "mapunixgroup", net_sam_mapunixgroup,
1215 "Map a unix group to a domain group" },
1216 { "addmem", net_sam_addmem,
1217 "Add a member to a group" },
1218 { "delmem", net_sam_delmem,
1219 "Delete a member from a group" },
1220 { "listmem", net_sam_listmem,
1221 "List group members" },
1222 { "list", net_sam_list,
1223 "List users, groups and local groups" },
1224 { "show", net_sam_show,
1225 "Show details of a SAM entry" },
1226 { "set", net_sam_set,
1227 "Set details of a SAM account" },
1229 { "provision", net_sam_provision,
1230 "Provision a clean User Database" },
1232 { NULL, NULL, NULL }
1235 /* we shouldn't have silly checks like this */
1236 if (getuid() != 0) {
1237 d_fprintf(stderr, "You must be root to edit the SAM "
1242 return net_run_function2(argc, argv, "net sam", func);