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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/passwd.h"
23 #include "utils/net.h"
24 #include "../librpc/gen_ndr/samr.h"
26 #include "../libcli/security/security.h"
27 #include "lib/winbind_util.h"
33 static int net_sam_userset(struct net_context *c, int argc, const char **argv,
35 bool (*fn)(struct samu *, const char *,
36 enum pdb_value_state))
38 struct samu *sam_acct = NULL;
40 enum lsa_SidType type;
41 const char *dom, *name;
44 if (argc != 2 || c->display_usage) {
45 d_fprintf(stderr, "%s\n", _("Usage:"));
46 d_fprintf(stderr, _("net sam set %s <user> <value>\n"),
51 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
52 &dom, &name, &sid, &type)) {
53 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
57 if (type != SID_NAME_USER) {
58 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
59 sid_type_lookup(type));
63 if ( !(sam_acct = samu_new( NULL )) ) {
64 d_fprintf(stderr, _("Internal error\n"));
68 if (!pdb_getsampwsid(sam_acct, &sid)) {
69 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
73 if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
74 d_fprintf(stderr, _("Internal error\n"));
78 status = pdb_update_sam_account(sam_acct);
79 if (!NT_STATUS_IS_OK(status)) {
80 d_fprintf(stderr, _("Updating sam account %s failed with %s\n"),
81 argv[0], nt_errstr(status));
85 TALLOC_FREE(sam_acct);
87 d_printf(_("Updated %s for %s\\%s to %s\n"), field, dom, name, argv[1]);
91 static int net_sam_set_fullname(struct net_context *c, int argc,
94 return net_sam_userset(c, argc, argv, "fullname",
98 static int net_sam_set_logonscript(struct net_context *c, int argc,
101 return net_sam_userset(c, argc, argv, "logonscript",
102 pdb_set_logon_script);
105 static int net_sam_set_profilepath(struct net_context *c, int argc,
108 return net_sam_userset(c, argc, argv, "profilepath",
109 pdb_set_profile_path);
112 static int net_sam_set_homedrive(struct net_context *c, int argc,
115 return net_sam_userset(c, argc, argv, "homedrive",
119 static int net_sam_set_homedir(struct net_context *c, int argc,
122 return net_sam_userset(c, argc, argv, "homedir",
126 static int net_sam_set_workstations(struct net_context *c, int argc,
129 return net_sam_userset(c, argc, argv, "workstations",
130 pdb_set_workstations);
137 static int net_sam_set_userflag(struct net_context *c, int argc,
138 const char **argv, const char *field,
141 struct samu *sam_acct = NULL;
143 enum lsa_SidType type;
144 const char *dom, *name;
148 if ((argc != 2) || c->display_usage ||
149 (!strequal(argv[1], "yes") &&
150 !strequal(argv[1], "no"))) {
151 d_fprintf(stderr, "%s\n", _("Usage:"));
152 d_fprintf(stderr, _("net sam set %s <user> [yes|no]\n"),
157 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
158 &dom, &name, &sid, &type)) {
159 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
163 if (type != SID_NAME_USER) {
164 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
165 sid_type_lookup(type));
169 if ( !(sam_acct = samu_new( NULL )) ) {
170 d_fprintf(stderr, _("Internal error\n"));
174 if (!pdb_getsampwsid(sam_acct, &sid)) {
175 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
179 acct_flags = pdb_get_acct_ctrl(sam_acct);
181 if (strequal(argv[1], "yes")) {
187 pdb_set_acct_ctrl(sam_acct, acct_flags, PDB_CHANGED);
189 status = pdb_update_sam_account(sam_acct);
190 if (!NT_STATUS_IS_OK(status)) {
191 d_fprintf(stderr, _("Updating sam account %s failed with %s\n"),
192 argv[0], nt_errstr(status));
196 TALLOC_FREE(sam_acct);
198 d_fprintf(stderr, _("Updated flag %s for %s\\%s to %s\n"), field, dom,
203 static int net_sam_set_disabled(struct net_context *c, int argc,
206 return net_sam_set_userflag(c, argc, argv, "disabled", ACB_DISABLED);
209 static int net_sam_set_pwnotreq(struct net_context *c, int argc,
212 return net_sam_set_userflag(c, argc, argv, "pwnotreq", ACB_PWNOTREQ);
215 static int net_sam_set_autolock(struct net_context *c, int argc,
218 return net_sam_set_userflag(c, argc, argv, "autolock", ACB_AUTOLOCK);
221 static int net_sam_set_pwnoexp(struct net_context *c, int argc,
224 return net_sam_set_userflag(c, argc, argv, "pwnoexp", ACB_PWNOEXP);
228 * Set pass last change time, based on force pass change now
231 static int net_sam_set_pwdmustchangenow(struct net_context *c, int argc,
234 struct samu *sam_acct = NULL;
236 enum lsa_SidType type;
237 const char *dom, *name;
240 if ((argc != 2) || c->display_usage ||
241 (!strequal(argv[1], "yes") &&
242 !strequal(argv[1], "no"))) {
243 d_fprintf(stderr, "%s\n%s",
245 _("net sam set pwdmustchangenow <user> [yes|no]\n"));
249 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
250 &dom, &name, &sid, &type)) {
251 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
255 if (type != SID_NAME_USER) {
256 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
257 sid_type_lookup(type));
261 if ( !(sam_acct = samu_new( NULL )) ) {
262 d_fprintf(stderr, _("Internal error\n"));
266 if (!pdb_getsampwsid(sam_acct, &sid)) {
267 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
271 if (strequal(argv[1], "yes")) {
272 pdb_set_pass_last_set_time(sam_acct, 0, PDB_CHANGED);
274 pdb_set_pass_last_set_time(sam_acct, time(NULL), PDB_CHANGED);
277 status = pdb_update_sam_account(sam_acct);
278 if (!NT_STATUS_IS_OK(status)) {
279 d_fprintf(stderr, _("Updating sam account %s failed with %s\n"),
280 argv[0], nt_errstr(status));
284 TALLOC_FREE(sam_acct);
286 d_fprintf(stderr, _("Updated 'user must change password at next logon' "
287 "for %s\\%s to %s\n"), dom,
294 * Set a user's or a group's comment
297 static int net_sam_set_comment(struct net_context *c, int argc,
302 enum lsa_SidType type;
303 const char *dom, *name;
306 if (argc != 2 || c->display_usage) {
307 d_fprintf(stderr, "%s\n%s",
309 _("net sam set comment <name> <comment>\n"));
313 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
314 &dom, &name, &sid, &type)) {
315 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
319 if (type == SID_NAME_USER) {
320 return net_sam_userset(c, argc, argv, "comment",
324 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
325 (type != SID_NAME_WKN_GRP)) {
326 d_fprintf(stderr, _("%s is a %s, not a group\n"), argv[0],
327 sid_type_lookup(type));
331 if (!pdb_getgrsid(&map, sid)) {
332 d_fprintf(stderr, _("Could not load group %s\n"), argv[0]);
336 fstrcpy(map.comment, argv[1]);
338 status = pdb_update_group_mapping_entry(&map);
340 if (!NT_STATUS_IS_OK(status)) {
341 d_fprintf(stderr, _("Updating group mapping entry failed with "
342 "%s\n"), nt_errstr(status));
346 d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
352 static int net_sam_set(struct net_context *c, int argc, const char **argv)
354 struct functable func[] = {
359 N_("Change a user's home directory"),
360 N_("net sam set homedir\n"
361 " Change a user's home directory")
365 net_sam_set_profilepath,
367 N_("Change a user's profile path"),
368 N_("net sam set profilepath\n"
369 " Change a user's profile path")
375 N_("Change a users or groups description"),
376 N_("net sam set comment\n"
377 " Change a users or groups description")
381 net_sam_set_fullname,
383 N_("Change a user's full name"),
384 N_("net sam set fullname\n"
385 " Change a user's full name")
389 net_sam_set_logonscript,
391 N_("Change a user's logon script"),
392 N_("net sam set logonscript\n"
393 " Change a user's logon script")
397 net_sam_set_homedrive,
399 N_("Change a user's home drive"),
400 N_("net sam set homedrive\n"
401 " Change a user's home drive")
405 net_sam_set_workstations,
407 N_("Change a user's allowed workstations"),
408 N_("net sam set workstations\n"
409 " Change a user's allowed workstations")
413 net_sam_set_disabled,
415 N_("Disable/Enable a user"),
416 N_("net sam set disable\n"
417 " Disable/Enable a user")
421 net_sam_set_pwnotreq,
423 N_("Disable/Enable the password not required flag"),
424 N_("net sam set pwnotreq\n"
425 " Disable/Enable the password not required flag")
429 net_sam_set_autolock,
431 N_("Disable/Enable a user's lockout flag"),
432 N_("net sam set autolock\n"
433 " Disable/Enable a user's lockout flag")
439 N_("Disable/Enable whether a user's pw does not "
441 N_("net sam set pwnoexp\n"
442 " Disable/Enable whether a user's pw does not "
447 net_sam_set_pwdmustchangenow,
449 N_("Force users password must change at next logon"),
450 N_("net sam set pwdmustchangenow\n"
451 " Force users password must change at next logon")
453 {NULL, NULL, 0, NULL, NULL}
456 return net_run_function(c, argc, argv, "net sam set", func);
460 * Manage account policies
463 static int net_sam_policy_set(struct net_context *c, int argc, const char **argv)
465 const char *account_policy = NULL;
467 uint32 old_value = 0;
468 enum pdb_policy_type field;
471 if (argc != 2 || c->display_usage) {
472 d_fprintf(stderr, "%s\n%s",
474 _("net sam policy set \"<account policy>\" <value>\n"));
478 account_policy = argv[0];
479 field = account_policy_name_to_typenum(account_policy);
481 if (strequal(argv[1], "forever") || strequal(argv[1], "never")
482 || strequal(argv[1], "off")) {
486 value = strtoul(argv[1], &endptr, 10);
488 if ((endptr == argv[1]) || (endptr[0] != '\0')) {
489 d_printf(_("Unable to set policy \"%s\"! Invalid value "
491 account_policy, argv[1]);
500 account_policy_names_list(&names, &count);
501 d_fprintf(stderr, _("No account policy \"%s\"!\n\n"), argv[0]);
502 d_fprintf(stderr, _("Valid account policies are:\n"));
504 for (i=0; i<count; i++) {
505 d_fprintf(stderr, "%s\n", names[i]);
512 if (!pdb_get_account_policy(field, &old_value)) {
513 d_fprintf(stderr, _("Valid account policy, but unable to fetch "
516 d_printf(_("Account policy \"%s\" value was: %d\n"),
517 account_policy, old_value);
520 if (!pdb_set_account_policy(field, value)) {
521 d_fprintf(stderr, _("Valid account policy, but unable to "
525 d_printf(_("Account policy \"%s\" value is now: %d\n"),
526 account_policy, value);
532 static int net_sam_policy_show(struct net_context *c, int argc, const char **argv)
534 const char *account_policy = NULL;
536 enum pdb_policy_type field;
538 if (argc != 1 || c->display_usage) {
539 d_fprintf(stderr, "%s\n%s",
541 _("net sam policy show \"<account policy>\"\n"));
545 account_policy = argv[0];
546 field = account_policy_name_to_typenum(account_policy);
552 account_policy_names_list(&names, &count);
553 d_fprintf(stderr, _("No account policy by that name!\n"));
555 d_fprintf(stderr, _("Valid account policies "
557 for (i=0; i<count; i++) {
558 d_fprintf(stderr, "%s\n", names[i]);
565 if (!pdb_get_account_policy(field, &old_value)) {
566 fprintf(stderr, _("Valid account policy, but unable to "
571 printf(_("Account policy \"%s\" description: %s\n"),
572 account_policy, account_policy_get_desc(field));
573 printf(_("Account policy \"%s\" value is: %d\n"), account_policy,
578 static int net_sam_policy_list(struct net_context *c, int argc, const char **argv)
584 if (c->display_usage) {
586 "net sam policy list\n"
589 _("List account policies"));
593 account_policy_names_list(&names, &count);
595 d_fprintf(stderr, _("Valid account policies "
597 for (i = 0; i < count ; i++) {
598 d_fprintf(stderr, "%s\n", names[i]);
605 static int net_sam_policy(struct net_context *c, int argc, const char **argv)
607 struct functable func[] = {
612 N_("List account policies"),
613 N_("net sam policy list\n"
614 " List account policies")
620 N_("Show account policies"),
621 N_("net sam policy show\n"
622 " Show account policies")
628 N_("Change account policies"),
629 N_("net sam policy set\n"
630 " Change account policies")
632 {NULL, NULL, 0, NULL, NULL}
635 return net_run_function(c, argc, argv, "net sam policy", func);
638 static int net_sam_rights_list(struct net_context *c, int argc,
641 enum sec_privilege privilege;
643 if (argc > 1 || c->display_usage) {
644 d_fprintf(stderr, "%s\n%s",
646 _("net sam rights list [privilege name]\n"));
652 int num = num_privileges_in_short_list();
654 for (i=0; i<num; i++) {
655 d_printf("%s\n", sec_privilege_name_from_index(i));
660 privilege = sec_privilege_id(argv[0]);
662 if (privilege != SEC_PRIV_INVALID) {
663 struct dom_sid *sids;
667 status = privilege_enum_sids(privilege, talloc_tos(),
669 if (!NT_STATUS_IS_OK(status)) {
670 d_fprintf(stderr, _("Could not list rights: %s\n"),
675 for (i=0; i<num_sids; i++) {
676 const char *dom, *name;
677 enum lsa_SidType type;
679 if (lookup_sid(talloc_tos(), &sids[i], &dom, &name,
681 d_printf("%s\\%s\n", dom, name);
684 d_printf("%s\n", sid_string_tos(&sids[i]));
693 static int net_sam_rights_grant(struct net_context *c, int argc,
697 enum lsa_SidType type;
698 const char *dom, *name;
701 if (argc < 2 || c->display_usage) {
702 d_fprintf(stderr, "%s\n%s",
704 _("net sam rights grant <name> <rights> ...\n"));
708 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
709 &dom, &name, &sid, &type)) {
710 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
714 for (i=1; i < argc; i++) {
715 enum sec_privilege privilege = sec_privilege_id(argv[i]);
716 if (privilege == SEC_PRIV_INVALID) {
717 d_fprintf(stderr, _("%s unknown\n"), argv[i]);
721 if (!grant_privilege_by_name(&sid, argv[i])) {
722 d_fprintf(stderr, _("Could not grant privilege\n"));
726 d_printf(_("Granted %s to %s\\%s\n"), argv[i], dom, name);
732 static int net_sam_rights_revoke(struct net_context *c, int argc,
736 enum lsa_SidType type;
737 const char *dom, *name;
740 if (argc < 2 || c->display_usage) {
741 d_fprintf(stderr, "%s\n%s",
743 _("net sam rights revoke <name> <rights>\n"));
747 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
748 &dom, &name, &sid, &type)) {
749 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
753 for (i=1; i < argc; i++) {
754 enum sec_privilege privilege = sec_privilege_id(argv[i]);
755 if (privilege == SEC_PRIV_INVALID) {
756 d_fprintf(stderr, _("%s unknown\n"), argv[i]);
760 if (!revoke_privilege_by_name(&sid, argv[i])) {
761 d_fprintf(stderr, _("Could not revoke privilege\n"));
765 d_printf(_("Revoked %s from %s\\%s\n"), argv[i], dom, name);
771 static int net_sam_rights(struct net_context *c, int argc, const char **argv)
773 struct functable func[] = {
778 N_("List possible user rights"),
779 N_("net sam rights list\n"
780 " List possible user rights")
784 net_sam_rights_grant,
786 N_("Grant right(s)"),
787 N_("net sam rights grant\n"
792 net_sam_rights_revoke,
794 N_("Revoke right(s)"),
795 N_("net sam rights revoke\n"
798 {NULL, NULL, 0, NULL, NULL}
800 return net_run_function(c, argc, argv, "net sam rights", func);
804 * Map a unix group to a domain group
807 static NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
811 const char *grpname, *dom, *name;
814 if (pdb_getgrgid(&map, grp->gr_gid)) {
815 return NT_STATUS_GROUP_EXISTS;
818 map.gid = grp->gr_gid;
819 grpname = grp->gr_name;
821 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
822 &dom, &name, NULL, NULL)) {
824 const char *tmp = talloc_asprintf(
825 talloc_tos(), "Unix Group %s", grp->gr_name);
827 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
828 grpname, dom, name, tmp));
832 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
833 NULL, NULL, NULL, NULL)) {
834 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
835 return NT_STATUS_GROUP_EXISTS;
838 fstrcpy(map.nt_name, grpname);
840 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
841 if (!pdb_new_rid(&rid)) {
842 DEBUG(3, ("Could not get a new RID for %s\n",
844 return NT_STATUS_ACCESS_DENIED;
847 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
850 sid_compose(&map.sid, get_global_sam_sid(), rid);
851 map.sid_name_use = SID_NAME_DOM_GRP;
852 fstrcpy(map.comment, talloc_asprintf(talloc_tos(), "Unix Group %s",
855 status = pdb_add_group_mapping_entry(&map);
856 if (NT_STATUS_IS_OK(status)) {
862 static int net_sam_mapunixgroup(struct net_context *c, int argc, const char **argv)
868 if (argc != 1 || c->display_usage) {
869 d_fprintf(stderr, "%s\n%s",
871 _("net sam mapunixgroup <name>\n"));
875 grp = getgrnam(argv[0]);
877 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
881 status = map_unix_group(grp, &map);
883 if (!NT_STATUS_IS_OK(status)) {
884 d_fprintf(stderr, _("Mapping group %s failed with %s\n"),
885 argv[0], nt_errstr(status));
889 d_printf(_("Mapped unix group %s to SID %s\n"), argv[0],
890 sid_string_tos(&map.sid));
896 * Remove a group mapping
899 static NTSTATUS unmap_unix_group(const struct group *grp, GROUP_MAP *pmap)
903 struct dom_sid dom_sid;
905 map.gid = grp->gr_gid;
906 grpname = grp->gr_name;
908 if (!lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
909 NULL, NULL, NULL, NULL)) {
910 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp->gr_name));
911 return NT_STATUS_NO_SUCH_GROUP;
914 fstrcpy(map.nt_name, grpname);
916 if (!pdb_gid_to_sid(map.gid, &dom_sid)) {
917 return NT_STATUS_UNSUCCESSFUL;
920 return pdb_delete_group_mapping_entry(dom_sid);
923 static int net_sam_unmapunixgroup(struct net_context *c, int argc, const char **argv)
929 if (argc != 1 || c->display_usage) {
930 d_fprintf(stderr, "%s\n%s",
932 _("net sam unmapunixgroup <name>\n"));
936 grp = getgrnam(argv[0]);
938 d_fprintf(stderr, _("Could not find mapping for group %s.\n"),
943 status = unmap_unix_group(grp, &map);
945 if (!NT_STATUS_IS_OK(status)) {
946 d_fprintf(stderr, _("Unmapping group %s failed with %s.\n"),
947 argv[0], nt_errstr(status));
951 d_printf(_("Unmapped unix group %s.\n"), argv[0]);
957 * Create a domain group
960 static int net_sam_createdomaingroup(struct net_context *c, int argc,
966 if (argc != 1 || c->display_usage) {
967 d_fprintf(stderr, "%s\n%s",
969 _("net sam createdomaingroup <name>\n"));
973 status = pdb_create_dom_group(talloc_tos(), argv[0], &rid);
975 if (!NT_STATUS_IS_OK(status)) {
976 d_fprintf(stderr, _("Creating %s failed with %s\n"),
977 argv[0], nt_errstr(status));
981 d_printf(_("Created domain group %s with RID %d\n"), argv[0], rid);
987 * Delete a domain group
990 static int net_sam_deletedomaingroup(struct net_context *c, int argc,
995 enum lsa_SidType type;
996 const char *dom, *name;
999 if (argc != 1 || c->display_usage) {
1000 d_fprintf(stderr, "%s\n%s",
1002 _("net sam deletelocalgroup <name>\n"));
1006 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1007 &dom, &name, &sid, &type)) {
1008 d_fprintf(stderr, _("Could not find %s.\n"), argv[0]);
1012 if (type != SID_NAME_DOM_GRP) {
1013 d_fprintf(stderr, _("%s is a %s, not a domain group.\n"),
1014 argv[0], sid_type_lookup(type));
1018 sid_peek_rid(&sid, &rid);
1020 status = pdb_delete_dom_group(talloc_tos(), rid);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 d_fprintf(stderr,_("Deleting domain group %s failed with %s\n"),
1024 argv[0], nt_errstr(status));
1028 d_printf(_("Deleted domain group %s.\n"), argv[0]);
1034 * Create a local group
1037 static int net_sam_createlocalgroup(struct net_context *c, int argc, const char **argv)
1042 if (argc != 1 || c->display_usage) {
1043 d_fprintf(stderr, "%s\n%s",
1045 _("net sam createlocalgroup <name>\n"));
1049 if (!winbind_ping()) {
1050 d_fprintf(stderr, _("winbind seems not to run. "
1051 "createlocalgroup only works when winbind runs.\n"));
1055 status = pdb_create_alias(argv[0], &rid);
1057 if (!NT_STATUS_IS_OK(status)) {
1058 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1059 argv[0], nt_errstr(status));
1063 d_printf(_("Created local group %s with RID %d\n"), argv[0], rid);
1069 * Delete a local group
1072 static int net_sam_deletelocalgroup(struct net_context *c, int argc, const char **argv)
1075 enum lsa_SidType type;
1076 const char *dom, *name;
1079 if (argc != 1 || c->display_usage) {
1080 d_fprintf(stderr, "%s\n%s",
1082 _("net sam deletelocalgroup <name>\n"));
1086 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1087 &dom, &name, &sid, &type)) {
1088 d_fprintf(stderr,_("Could not find %s.\n"), argv[0]);
1092 if (type != SID_NAME_ALIAS) {
1093 d_fprintf(stderr, _("%s is a %s, not a local group.\n"),argv[0],
1094 sid_type_lookup(type));
1098 status = pdb_delete_alias(&sid);
1100 if (!NT_STATUS_IS_OK(status)) {
1101 d_fprintf(stderr, _("Deleting local group %s failed with %s\n"),
1102 argv[0], nt_errstr(status));
1106 d_printf(_("Deleted local group %s.\n"), argv[0]);
1112 * Create a builtin group
1115 static int net_sam_createbuiltingroup(struct net_context *c, int argc, const char **argv)
1119 enum lsa_SidType type;
1123 if (argc != 1 || c->display_usage) {
1124 d_fprintf(stderr, "%s\n%s",
1126 _("net sam createbuiltingroup <name>\n"));
1130 if (!winbind_ping()) {
1131 d_fprintf(stderr, _("winbind seems not to run. "
1132 "createbuiltingroup only works when winbind "
1137 /* validate the name and get the group */
1139 fstrcpy( groupname, "BUILTIN\\" );
1140 fstrcat( groupname, argv[0] );
1142 if ( !lookup_name(talloc_tos(), groupname, LOOKUP_NAME_ALL, NULL,
1143 NULL, &sid, &type)) {
1144 d_fprintf(stderr, _("%s is not a BUILTIN group\n"), argv[0]);
1148 if ( !sid_peek_rid( &sid, &rid ) ) {
1149 d_fprintf(stderr, _("Failed to get RID for %s\n"), argv[0]);
1153 status = pdb_create_builtin_alias( rid );
1155 if (!NT_STATUS_IS_OK(status)) {
1156 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1157 argv[0], nt_errstr(status));
1161 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv[0], rid);
1167 * Add a group member
1170 static int net_sam_addmem(struct net_context *c, int argc, const char **argv)
1172 const char *groupdomain, *groupname, *memberdomain, *membername;
1173 struct dom_sid group, member;
1174 enum lsa_SidType grouptype, membertype;
1177 if (argc != 2 || c->display_usage) {
1178 d_fprintf(stderr, "%s\n%s",
1180 _("net sam addmem <group> <member>\n"));
1184 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1185 &groupdomain, &groupname, &group, &grouptype)) {
1186 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1190 /* check to see if the member to be added is a name or a SID */
1192 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1193 &memberdomain, &membername, &member, &membertype))
1195 /* try it as a SID */
1197 if ( !string_to_sid( &member, argv[1] ) ) {
1198 d_fprintf(stderr, _("Could not find member %s\n"),
1203 if ( !lookup_sid(talloc_tos(), &member, &memberdomain,
1204 &membername, &membertype) )
1206 d_fprintf(stderr, _("Could not resolve SID %s\n"),
1212 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
1213 if ((membertype != SID_NAME_USER) &&
1214 (membertype != SID_NAME_DOM_GRP)) {
1215 d_fprintf(stderr, _("%s is a local group, only users "
1216 "and domain groups can be added.\n"
1217 "%s is a %s\n"), argv[0], argv[1],
1218 sid_type_lookup(membertype));
1221 status = pdb_add_aliasmem(&group, &member);
1223 if (!NT_STATUS_IS_OK(status)) {
1224 d_fprintf(stderr, _("Adding local group member failed "
1225 "with %s\n"), nt_errstr(status));
1228 } else if (grouptype == SID_NAME_DOM_GRP) {
1229 uint32_t grouprid, memberrid;
1231 sid_peek_rid(&group, &grouprid);
1232 sid_peek_rid(&member, &memberrid);
1234 status = pdb_add_groupmem(talloc_tos(), grouprid, memberrid);
1235 if (!NT_STATUS_IS_OK(status)) {
1236 d_fprintf(stderr, _("Adding domain group member failed "
1237 "with %s\n"), nt_errstr(status));
1241 d_fprintf(stderr, _("Can only add members to local groups so "
1242 "far, %s is a %s\n"), argv[0],
1243 sid_type_lookup(grouptype));
1247 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain, membername,
1248 groupdomain, groupname);
1254 * Delete a group member
1257 static int net_sam_delmem(struct net_context *c, int argc, const char **argv)
1259 const char *groupdomain, *groupname;
1260 const char *memberdomain = NULL;
1261 const char *membername = NULL;
1262 struct dom_sid group, member;
1263 enum lsa_SidType grouptype;
1266 if (argc != 2 || c->display_usage) {
1267 d_fprintf(stderr,"%s\n%s",
1269 _("net sam delmem <group> <member>\n"));
1273 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1274 &groupdomain, &groupname, &group, &grouptype)) {
1275 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1279 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1280 &memberdomain, &membername, &member, NULL)) {
1281 if (!string_to_sid(&member, argv[1])) {
1282 d_fprintf(stderr, _("Could not find member %s\n"),
1288 if ((grouptype == SID_NAME_ALIAS) ||
1289 (grouptype == SID_NAME_WKN_GRP)) {
1290 status = pdb_del_aliasmem(&group, &member);
1292 if (!NT_STATUS_IS_OK(status)) {
1293 d_fprintf(stderr,_("Deleting local group member failed "
1294 "with %s\n"), nt_errstr(status));
1297 } else if (grouptype == SID_NAME_DOM_GRP) {
1298 uint32_t grouprid, memberrid;
1300 sid_peek_rid(&group, &grouprid);
1301 sid_peek_rid(&member, &memberrid);
1303 status = pdb_del_groupmem(talloc_tos(), grouprid, memberrid);
1304 if (!NT_STATUS_IS_OK(status)) {
1305 d_fprintf(stderr, _("Deleting domain group member "
1306 "failed with %s\n"), nt_errstr(status));
1310 d_fprintf(stderr, _("Can only delete members from local groups "
1311 "so far, %s is a %s\n"), argv[0],
1312 sid_type_lookup(grouptype));
1316 if (membername != NULL) {
1317 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1318 memberdomain, membername, groupdomain, groupname);
1320 d_printf(_("Deleted %s from %s\\%s\n"),
1321 sid_string_tos(&member), groupdomain, groupname);
1328 * List group members
1331 static int net_sam_listmem(struct net_context *c, int argc, const char **argv)
1333 const char *groupdomain, *groupname;
1334 struct dom_sid group;
1335 struct dom_sid *members = NULL;
1336 size_t i, num_members = 0;
1337 enum lsa_SidType grouptype;
1340 if (argc != 1 || c->display_usage) {
1341 d_fprintf(stderr, "%s\n%s",
1343 _("net sam listmem <group>\n"));
1347 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1348 &groupdomain, &groupname, &group, &grouptype)) {
1349 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1353 if ((grouptype == SID_NAME_ALIAS) ||
1354 (grouptype == SID_NAME_WKN_GRP)) {
1355 status = pdb_enum_aliasmem(&group, talloc_tos(), &members,
1357 if (!NT_STATUS_IS_OK(status)) {
1358 d_fprintf(stderr, _("Listing group members failed with "
1359 "%s\n"), nt_errstr(status));
1362 } else if (grouptype == SID_NAME_DOM_GRP) {
1365 status = pdb_enum_group_members(talloc_tos(), &group,
1366 &rids, &num_members);
1367 if (!NT_STATUS_IS_OK(status)) {
1368 d_fprintf(stderr, _("Listing group members failed with "
1369 "%s\n"), nt_errstr(status));
1373 members = talloc_array(talloc_tos(), struct dom_sid,
1375 if (members == NULL) {
1380 for (i=0; i<num_members; i++) {
1381 sid_compose(&members[i], get_global_sam_sid(),
1386 d_fprintf(stderr,_("Can only list local group members so far.\n"
1387 "%s is a %s\n"), argv[0], sid_type_lookup(grouptype));
1391 d_printf(_("%s\\%s has %u members\n"), groupdomain, groupname,
1392 (unsigned int)num_members);
1393 for (i=0; i<num_members; i++) {
1394 const char *dom, *name;
1395 if (lookup_sid(talloc_tos(), &members[i], &dom, &name, NULL)) {
1396 d_printf(" %s\\%s\n", dom, name);
1398 d_printf(" %s\n", sid_string_tos(&members[i]));
1402 TALLOC_FREE(members);
1410 static int net_sam_do_list(struct net_context *c, int argc, const char **argv,
1411 struct pdb_search *search, const char *what)
1413 bool verbose = (argc == 1);
1415 if ((argc > 1) || c->display_usage ||
1416 ((argc == 1) && !strequal(argv[0], "verbose"))) {
1417 d_fprintf(stderr, "%s\n", _("Usage:"));
1418 d_fprintf(stderr, _("net sam list %s [verbose]\n"), what);
1422 if (search == NULL) {
1423 d_fprintf(stderr, _("Could not start search\n"));
1428 struct samr_displayentry entry;
1429 if (!search->next_entry(search, &entry)) {
1433 d_printf("%s:%d:%s\n",
1438 d_printf("%s\n", entry.account_name);
1442 TALLOC_FREE(search);
1446 static int net_sam_list_users(struct net_context *c, int argc,
1449 return net_sam_do_list(c, argc, argv,
1450 pdb_search_users(talloc_tos(), ACB_NORMAL),
1454 static int net_sam_list_groups(struct net_context *c, int argc,
1457 return net_sam_do_list(c, argc, argv, pdb_search_groups(talloc_tos()),
1461 static int net_sam_list_localgroups(struct net_context *c, int argc,
1464 return net_sam_do_list(c, argc, argv,
1465 pdb_search_aliases(talloc_tos(),
1466 get_global_sam_sid()),
1470 static int net_sam_list_builtin(struct net_context *c, int argc,
1473 return net_sam_do_list(c, argc, argv,
1474 pdb_search_aliases(talloc_tos(),
1475 &global_sid_Builtin),
1479 static int net_sam_list_workstations(struct net_context *c, int argc,
1482 return net_sam_do_list(c, argc, argv,
1483 pdb_search_users(talloc_tos(), ACB_WSTRUST),
1491 static int net_sam_list(struct net_context *c, int argc, const char **argv)
1493 struct functable func[] = {
1497 NET_TRANSPORT_LOCAL,
1498 N_("List SAM users"),
1499 N_("net sam list users\n"
1504 net_sam_list_groups,
1505 NET_TRANSPORT_LOCAL,
1506 N_("List SAM groups"),
1507 N_("net sam list groups\n"
1512 net_sam_list_localgroups,
1513 NET_TRANSPORT_LOCAL,
1514 N_("List SAM local groups"),
1515 N_("net sam list localgroups\n"
1516 " List SAM local groups")
1520 net_sam_list_builtin,
1521 NET_TRANSPORT_LOCAL,
1522 N_("List builtin groups"),
1523 N_("net sam list builtin\n"
1524 " List builtin groups")
1528 net_sam_list_workstations,
1529 NET_TRANSPORT_LOCAL,
1530 N_("List domain member workstations"),
1531 N_("net sam list workstations\n"
1532 " List domain member workstations")
1534 {NULL, NULL, 0, NULL, NULL}
1537 return net_run_function(c, argc, argv, "net sam list", func);
1541 * Show details of SAM entries
1544 static int net_sam_show(struct net_context *c, int argc, const char **argv)
1547 enum lsa_SidType type;
1548 const char *dom, *name;
1550 if (argc != 1 || c->display_usage) {
1551 d_fprintf(stderr, "%s\n%s",
1553 _("net sam show <name>\n"));
1557 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1558 &dom, &name, &sid, &type)) {
1559 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
1563 d_printf(_("%s\\%s is a %s with SID %s\n"), dom, name,
1564 sid_type_lookup(type), sid_string_tos(&sid));
1572 * Init an LDAP tree with default users and Groups
1573 * if ldapsam:editposix is enabled
1576 static int net_sam_provision(struct net_context *c, int argc, const char **argv)
1580 char *ldap_uri = NULL;
1582 struct smbldap_state *ls;
1584 struct dom_sid gsid;
1585 gid_t domusers_gid = -1;
1586 gid_t domadmins_gid = -1;
1587 struct samu *samuser;
1590 if (c->display_usage) {
1592 "net sam provision\n"
1595 _("Init an LDAP tree with default users/groups"));
1599 tc = talloc_new(NULL);
1601 d_fprintf(stderr, _("Out of Memory!\n"));
1605 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
1606 d_fprintf(stderr, _("talloc failed\n"));
1610 p = strchr(ldap_bk, ':');
1613 ldap_uri = talloc_strdup(tc, p+1);
1614 trim_char(ldap_uri, ' ', ' ');
1617 trim_char(ldap_bk, ' ', ' ');
1619 if (strcmp(ldap_bk, "ldapsam") != 0) {
1621 _("Provisioning works only with ldapsam backend\n"));
1625 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1626 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1628 d_fprintf(stderr, _("Provisioning works only if ldapsam:trusted"
1629 " and ldapsam:editposix are enabled.\n"));
1633 if (!winbind_ping()) {
1634 d_fprintf(stderr, _("winbind seems not to run. Provisioning "
1635 "LDAP only works when winbind runs.\n"));
1639 if (!NT_STATUS_IS_OK(smbldap_init(tc, NULL, ldap_uri, &ls))) {
1640 d_fprintf(stderr, _("Unable to connect to the LDAP server.\n"));
1644 d_printf(_("Checking for Domain Users group.\n"));
1646 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_USERS);
1648 if (!pdb_getgrsid(&gmap, gsid)) {
1649 LDAPMod **mods = NULL;
1657 d_printf(_("Adding the Domain Users group.\n"));
1659 /* lets allocate a new groupid for this group */
1660 if (!winbind_allocate_gid(&domusers_gid)) {
1661 d_fprintf(stderr, _("Unable to allocate a new gid to "
1662 "create Domain Users group!\n"));
1666 uname = talloc_strdup(tc, "domusers");
1667 wname = talloc_strdup(tc, "Domain Users");
1668 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1669 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domusers_gid);
1670 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1672 if (!uname || !wname || !dn || !gidstr || !gtype) {
1673 d_fprintf(stderr, "Out of Memory!\n");
1677 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1678 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1679 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1680 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1681 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1682 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1683 sid_string_talloc(tc, &gsid));
1684 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1686 talloc_autofree_ldapmod(tc, mods);
1688 rc = smbldap_add(ls, dn, mods);
1690 if (rc != LDAP_SUCCESS) {
1691 d_fprintf(stderr, _("Failed to add Domain Users group "
1692 "to ldap directory\n"));
1695 domusers_gid = gmap.gid;
1696 d_printf(_("found!\n"));
1701 d_printf(_("Checking for Domain Admins group.\n"));
1703 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_ADMINS);
1705 if (!pdb_getgrsid(&gmap, gsid)) {
1706 LDAPMod **mods = NULL;
1714 d_printf(_("Adding the Domain Admins group.\n"));
1716 /* lets allocate a new groupid for this group */
1717 if (!winbind_allocate_gid(&domadmins_gid)) {
1718 d_fprintf(stderr, _("Unable to allocate a new gid to "
1719 "create Domain Admins group!\n"));
1723 uname = talloc_strdup(tc, "domadmins");
1724 wname = talloc_strdup(tc, "Domain Admins");
1725 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1726 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1727 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1729 if (!uname || !wname || !dn || !gidstr || !gtype) {
1730 d_fprintf(stderr, _("Out of Memory!\n"));
1734 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1735 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1736 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1737 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1738 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1739 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1740 sid_string_talloc(tc, &gsid));
1741 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1743 talloc_autofree_ldapmod(tc, mods);
1745 rc = smbldap_add(ls, dn, mods);
1747 if (rc != LDAP_SUCCESS) {
1748 d_fprintf(stderr, _("Failed to add Domain Admins group "
1749 "to ldap directory\n"));
1752 domadmins_gid = gmap.gid;
1753 d_printf(_("found!\n"));
1758 d_printf(_("Check for Administrator account.\n"));
1760 samuser = samu_new(tc);
1762 d_fprintf(stderr, _("Out of Memory!\n"));
1766 if (!pdb_getsampwnam(samuser, "Administrator")) {
1767 LDAPMod **mods = NULL;
1778 d_printf(_("Adding the Administrator user.\n"));
1780 if (domadmins_gid == -1) {
1782 _("Can't create Administrator user, Domain "
1783 "Admins group not available!\n"));
1786 if (!winbind_allocate_uid(&uid)) {
1788 _("Unable to allocate a new uid to create "
1789 "the Administrator user!\n"));
1793 name = talloc_strdup(tc, "Administrator");
1794 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1795 uidstr = talloc_asprintf(tc, "%u", (unsigned int)uid);
1796 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1797 dir = talloc_sub_specified(tc, lp_template_homedir(),
1799 get_global_sam_name(),
1800 uid, domadmins_gid);
1801 shell = talloc_sub_specified(tc, lp_template_shell(),
1803 get_global_sam_name(),
1804 uid, domadmins_gid);
1806 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1807 d_fprintf(stderr, _("Out of Memory!\n"));
1811 sid_compose(&sid, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR);
1813 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1814 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1815 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1816 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1817 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1818 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1819 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1820 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1821 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1822 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1823 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1824 sid_string_talloc(tc, &sid));
1825 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1826 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1827 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1829 talloc_autofree_ldapmod(tc, mods);
1831 rc = smbldap_add(ls, dn, mods);
1833 if (rc != LDAP_SUCCESS) {
1834 d_fprintf(stderr, _("Failed to add Administrator user "
1835 "to ldap directory\n"));
1838 d_printf(_("found!\n"));
1841 d_printf(_("Checking for Guest user.\n"));
1843 samuser = samu_new(tc);
1845 d_fprintf(stderr, _("Out of Memory!\n"));
1849 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1850 LDAPMod **mods = NULL;
1857 d_printf(_("Adding the Guest user.\n"));
1859 sid_compose(&sid, get_global_sam_sid(), DOMAIN_RID_GUEST);
1861 pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
1864 if (domusers_gid == -1) {
1866 _("Can't create Guest user, Domain "
1867 "Users group not available!\n"));
1870 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1871 d_fprintf(stderr, _("talloc failed\n"));
1874 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1875 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1877 _("Unable to allocate a new uid to "
1878 "create the Guest user!\n"));
1881 pwd->pw_gid = domusers_gid;
1882 pwd->pw_dir = talloc_strdup(tc, "/");
1883 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1884 if (!pwd->pw_dir || !pwd->pw_shell) {
1885 d_fprintf(stderr, _("Out of Memory!\n"));
1890 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1891 uidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_uid);
1892 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
1893 if (!dn || !uidstr || !gidstr) {
1894 d_fprintf(stderr, _("Out of Memory!\n"));
1898 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1899 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1900 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1901 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1902 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1903 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1904 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1905 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1906 if ((pwd->pw_dir != NULL) && (pwd->pw_dir[0] != '\0')) {
1907 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1909 if ((pwd->pw_shell != NULL) && (pwd->pw_shell[0] != '\0')) {
1910 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1912 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1913 sid_string_talloc(tc, &sid));
1914 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1915 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1916 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1918 talloc_autofree_ldapmod(tc, mods);
1920 rc = smbldap_add(ls, dn, mods);
1922 if (rc != LDAP_SUCCESS) {
1923 d_fprintf(stderr, _("Failed to add Guest user to "
1924 "ldap directory\n"));
1927 d_printf(_("found!\n"));
1930 d_printf(_("Checking Guest's group.\n"));
1932 pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
1935 _("Failed to find just created Guest account!\n"
1936 " Is nss properly configured?!\n"));
1940 if (pwd->pw_gid == domusers_gid) {
1941 d_printf(_("found!\n"));
1945 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1946 LDAPMod **mods = NULL;
1954 d_printf(_("Adding the Domain Guests group.\n"));
1956 uname = talloc_strdup(tc, "domguests");
1957 wname = talloc_strdup(tc, "Domain Guests");
1958 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1959 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
1960 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1962 if (!uname || !wname || !dn || !gidstr || !gtype) {
1963 d_fprintf(stderr, _("Out of Memory!\n"));
1967 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_GUESTS);
1969 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1970 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1971 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1972 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1973 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1974 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1975 sid_string_talloc(tc, &gsid));
1976 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1978 talloc_autofree_ldapmod(tc, mods);
1980 rc = smbldap_add(ls, dn, mods);
1982 if (rc != LDAP_SUCCESS) {
1984 _("Failed to add Domain Guests group to ldap "
1988 d_printf(_("found!\n"));
2003 /***********************************************************
2004 migrated functionality from smbgroupedit
2005 **********************************************************/
2006 int net_sam(struct net_context *c, int argc, const char **argv)
2008 struct functable func[] = {
2010 "createbuiltingroup",
2011 net_sam_createbuiltingroup,
2012 NET_TRANSPORT_LOCAL,
2013 N_("Create a new BUILTIN group"),
2014 N_("net sam createbuiltingroup\n"
2015 " Create a new BUILTIN group")
2019 net_sam_createlocalgroup,
2020 NET_TRANSPORT_LOCAL,
2021 N_("Create a new local group"),
2022 N_("net sam createlocalgroup\n"
2023 " Create a new local group")
2026 "createdomaingroup",
2027 net_sam_createdomaingroup,
2028 NET_TRANSPORT_LOCAL,
2029 N_("Create a new group"),
2030 N_("net sam createdomaingroup\n"
2031 " Create a new group")
2035 net_sam_deletelocalgroup,
2036 NET_TRANSPORT_LOCAL,
2037 N_("Delete an existing local group"),
2038 N_("net sam deletelocalgroup\n"
2039 " Delete an existing local group")
2042 "deletedomaingroup",
2043 net_sam_deletedomaingroup,
2044 NET_TRANSPORT_LOCAL,
2045 N_("Delete a domain group"),
2046 N_("net sam deletedomaingroup\n"
2051 net_sam_mapunixgroup,
2052 NET_TRANSPORT_LOCAL,
2053 N_("Map a unix group to a domain group"),
2054 N_("net sam mapunixgroup\n"
2055 " Map a unix group to a domain group")
2059 net_sam_unmapunixgroup,
2060 NET_TRANSPORT_LOCAL,
2061 N_("Remove a group mapping of an unix group to a "
2063 N_("net sam unmapunixgroup\n"
2064 " Remove a group mapping of an unix group to a "
2070 NET_TRANSPORT_LOCAL,
2071 N_("Add a member to a group"),
2072 N_("net sam addmem\n"
2073 " Add a member to a group")
2078 NET_TRANSPORT_LOCAL,
2079 N_("Delete a member from a group"),
2080 N_("net sam delmem\n"
2081 " Delete a member from a group")
2086 NET_TRANSPORT_LOCAL,
2087 N_("List group members"),
2088 N_("net sam listmem\n"
2089 " List group members")
2094 NET_TRANSPORT_LOCAL,
2095 N_("List users, groups and local groups"),
2097 " List users, groups and local groups")
2102 NET_TRANSPORT_LOCAL,
2103 N_("Show details of a SAM entry"),
2105 " Show details of a SAM entry")
2110 NET_TRANSPORT_LOCAL,
2111 N_("Set details of a SAM account"),
2113 " Set details of a SAM account")
2118 NET_TRANSPORT_LOCAL,
2119 N_("Set account policies"),
2120 N_("net sam policy\n"
2121 " Set account policies")
2126 NET_TRANSPORT_LOCAL,
2127 N_("Manipulate user privileges"),
2128 N_("net sam rights\n"
2129 " Manipulate user privileges")
2135 NET_TRANSPORT_LOCAL,
2136 N_("Provision a clean user database"),
2137 N_("net sam privison\n"
2138 " Provision a clear user database")
2141 {NULL, NULL, 0, NULL, NULL}
2144 if (getuid() != 0) {
2145 d_fprintf(stderr, _("You are not root, most things won't "
2149 return net_run_function(c, argc, argv, "net sam", func);