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 "utils/net.h"
28 static int net_sam_userset(struct net_context *c, int argc, const char **argv,
30 bool (*fn)(struct samu *, const char *,
31 enum pdb_value_state))
33 struct samu *sam_acct = NULL;
35 enum lsa_SidType type;
36 const char *dom, *name;
39 if (argc != 2 || c->display_usage) {
40 d_fprintf(stderr, "%s\n", _("Usage:"));
41 d_fprintf(stderr, _("net sam set %s <user> <value>\n"),
46 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
47 &dom, &name, &sid, &type)) {
48 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
52 if (type != SID_NAME_USER) {
53 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
54 sid_type_lookup(type));
58 if ( !(sam_acct = samu_new( NULL )) ) {
59 d_fprintf(stderr, _("Internal error\n"));
63 if (!pdb_getsampwsid(sam_acct, &sid)) {
64 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
68 if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
69 d_fprintf(stderr, _("Internal error\n"));
73 status = pdb_update_sam_account(sam_acct);
74 if (!NT_STATUS_IS_OK(status)) {
75 d_fprintf(stderr, _("Updating sam account %s failed with %s\n"),
76 argv[0], nt_errstr(status));
80 TALLOC_FREE(sam_acct);
82 d_printf(_("Updated %s for %s\\%s to %s\n"), field, dom, name, argv[1]);
86 static int net_sam_set_fullname(struct net_context *c, int argc,
89 return net_sam_userset(c, argc, argv, "fullname",
93 static int net_sam_set_logonscript(struct net_context *c, int argc,
96 return net_sam_userset(c, argc, argv, "logonscript",
97 pdb_set_logon_script);
100 static int net_sam_set_profilepath(struct net_context *c, int argc,
103 return net_sam_userset(c, argc, argv, "profilepath",
104 pdb_set_profile_path);
107 static int net_sam_set_homedrive(struct net_context *c, int argc,
110 return net_sam_userset(c, argc, argv, "homedrive",
114 static int net_sam_set_homedir(struct net_context *c, int argc,
117 return net_sam_userset(c, argc, argv, "homedir",
121 static int net_sam_set_workstations(struct net_context *c, int argc,
124 return net_sam_userset(c, argc, argv, "workstations",
125 pdb_set_workstations);
132 static int net_sam_set_userflag(struct net_context *c, int argc,
133 const char **argv, const char *field,
136 struct samu *sam_acct = NULL;
138 enum lsa_SidType type;
139 const char *dom, *name;
143 if ((argc != 2) || c->display_usage ||
144 (!strequal(argv[1], "yes") &&
145 !strequal(argv[1], "no"))) {
146 d_fprintf(stderr, "%s\n", _("Usage:"));
147 d_fprintf(stderr, _("net sam set %s <user> [yes|no]\n"),
152 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
153 &dom, &name, &sid, &type)) {
154 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
158 if (type != SID_NAME_USER) {
159 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
160 sid_type_lookup(type));
164 if ( !(sam_acct = samu_new( NULL )) ) {
165 d_fprintf(stderr, _("Internal error\n"));
169 if (!pdb_getsampwsid(sam_acct, &sid)) {
170 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
174 acct_flags = pdb_get_acct_ctrl(sam_acct);
176 if (strequal(argv[1], "yes")) {
182 pdb_set_acct_ctrl(sam_acct, acct_flags, PDB_CHANGED);
184 status = pdb_update_sam_account(sam_acct);
185 if (!NT_STATUS_IS_OK(status)) {
186 d_fprintf(stderr, _("Updating sam account %s failed with %s\n"),
187 argv[0], nt_errstr(status));
191 TALLOC_FREE(sam_acct);
193 d_fprintf(stderr, _("Updated flag %s for %s\\%s to %s\n"), field, dom,
198 static int net_sam_set_disabled(struct net_context *c, int argc,
201 return net_sam_set_userflag(c, argc, argv, "disabled", ACB_DISABLED);
204 static int net_sam_set_pwnotreq(struct net_context *c, int argc,
207 return net_sam_set_userflag(c, argc, argv, "pwnotreq", ACB_PWNOTREQ);
210 static int net_sam_set_autolock(struct net_context *c, int argc,
213 return net_sam_set_userflag(c, argc, argv, "autolock", ACB_AUTOLOCK);
216 static int net_sam_set_pwnoexp(struct net_context *c, int argc,
219 return net_sam_set_userflag(c, argc, argv, "pwnoexp", ACB_PWNOEXP);
223 * Set pass last change time, based on force pass change now
226 static int net_sam_set_pwdmustchangenow(struct net_context *c, int argc,
229 struct samu *sam_acct = NULL;
231 enum lsa_SidType type;
232 const char *dom, *name;
235 if ((argc != 2) || c->display_usage ||
236 (!strequal(argv[1], "yes") &&
237 !strequal(argv[1], "no"))) {
238 d_fprintf(stderr, "%s\n%s",
240 _("net sam set pwdmustchangenow <user> [yes|no]\n"));
244 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
245 &dom, &name, &sid, &type)) {
246 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
250 if (type != SID_NAME_USER) {
251 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
252 sid_type_lookup(type));
256 if ( !(sam_acct = samu_new( NULL )) ) {
257 d_fprintf(stderr, _("Internal error\n"));
261 if (!pdb_getsampwsid(sam_acct, &sid)) {
262 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
266 if (strequal(argv[1], "yes")) {
267 pdb_set_pass_last_set_time(sam_acct, 0, PDB_CHANGED);
269 pdb_set_pass_last_set_time(sam_acct, time(NULL), PDB_CHANGED);
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_fprintf(stderr, _("Updated 'user must change password at next logon' "
282 "for %s\\%s to %s\n"), dom,
289 * Set a user's or a group's comment
292 static int net_sam_set_comment(struct net_context *c, int argc,
297 enum lsa_SidType type;
298 const char *dom, *name;
301 if (argc != 2 || c->display_usage) {
302 d_fprintf(stderr, "%s\n%s",
304 _("net sam set comment <name> <comment>\n"));
308 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
309 &dom, &name, &sid, &type)) {
310 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
314 if (type == SID_NAME_USER) {
315 return net_sam_userset(c, argc, argv, "comment",
319 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
320 (type != SID_NAME_WKN_GRP)) {
321 d_fprintf(stderr, _("%s is a %s, not a group\n"), argv[0],
322 sid_type_lookup(type));
326 if (!pdb_getgrsid(&map, sid)) {
327 d_fprintf(stderr, _("Could not load group %s\n"), argv[0]);
331 fstrcpy(map.comment, argv[1]);
333 status = pdb_update_group_mapping_entry(&map);
335 if (!NT_STATUS_IS_OK(status)) {
336 d_fprintf(stderr, _("Updating group mapping entry failed with "
337 "%s\n"), nt_errstr(status));
341 d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
347 static int net_sam_set(struct net_context *c, int argc, const char **argv)
349 struct functable func[] = {
354 N_("Change a user's home directory"),
355 N_("net sam set homedir\n"
356 " Change a user's home directory")
360 net_sam_set_profilepath,
362 N_("Change a user's profile path"),
363 N_("net sam set profilepath\n"
364 " Change a user's profile path")
370 N_("Change a users or groups description"),
371 N_("net sam set comment\n"
372 " Change a users or groups description")
376 net_sam_set_fullname,
378 N_("Change a user's full name"),
379 N_("net sam set fullname\n"
380 " Change a user's full name")
384 net_sam_set_logonscript,
386 N_("Change a user's logon script"),
387 N_("net sam set logonscript\n"
388 " Change a user's logon script")
392 net_sam_set_homedrive,
394 N_("Change a user's home drive"),
395 N_("net sam set homedrive\n"
396 " Change a user's home drive")
400 net_sam_set_workstations,
402 N_("Change a user's allowed workstations"),
403 N_("net sam set workstations\n"
404 " Change a user's allowed workstations")
408 net_sam_set_disabled,
410 N_("Disable/Enable a user"),
411 N_("net sam set disable\n"
412 " Disable/Enable a user")
416 net_sam_set_pwnotreq,
418 N_("Disable/Enable the password not required flag"),
419 N_("net sam set pwnotreq\n"
420 " Disable/Enable the password not required flag")
424 net_sam_set_autolock,
426 N_("Disable/Enable a user's lockout flag"),
427 N_("net sam set autolock\n"
428 " Disable/Enable a user's lockout flag")
434 N_("Disable/Enable whether a user's pw does not "
436 N_("net sam set pwnoexp\n"
437 " Disable/Enable whether a user's pw does not "
442 net_sam_set_pwdmustchangenow,
444 N_("Force users password must change at next logon"),
445 N_("net sam set pwdmustchangenow\n"
446 " Force users password must change at next logon")
448 {NULL, NULL, 0, NULL, NULL}
451 return net_run_function(c, argc, argv, "net sam set", func);
455 * Manage account policies
458 static int net_sam_policy_set(struct net_context *c, int argc, const char **argv)
460 const char *account_policy = NULL;
462 uint32 old_value = 0;
463 enum pdb_policy_type field;
466 if (argc != 2 || c->display_usage) {
467 d_fprintf(stderr, "%s\n%s",
469 _("net sam policy set \"<account policy>\" <value>\n"));
473 account_policy = argv[0];
474 field = account_policy_name_to_typenum(account_policy);
476 if (strequal(argv[1], "forever") || strequal(argv[1], "never")
477 || strequal(argv[1], "off")) {
481 value = strtoul(argv[1], &endptr, 10);
483 if ((endptr == argv[1]) || (endptr[0] != '\0')) {
484 d_printf(_("Unable to set policy \"%s\"! Invalid value "
486 account_policy, argv[1]);
495 account_policy_names_list(&names, &count);
496 d_fprintf(stderr, _("No account policy \"%s\"!\n\n"), argv[0]);
497 d_fprintf(stderr, _("Valid account policies are:\n"));
499 for (i=0; i<count; i++) {
500 d_fprintf(stderr, "%s\n", names[i]);
507 if (!pdb_get_account_policy(field, &old_value)) {
508 d_fprintf(stderr, _("Valid account policy, but unable to fetch "
511 d_printf(_("Account policy \"%s\" value was: %d\n"),
512 account_policy, old_value);
515 if (!pdb_set_account_policy(field, value)) {
516 d_fprintf(stderr, _("Valid account policy, but unable to "
520 d_printf(_("Account policy \"%s\" value is now: %d\n"),
521 account_policy, value);
527 static int net_sam_policy_show(struct net_context *c, int argc, const char **argv)
529 const char *account_policy = NULL;
531 enum pdb_policy_type field;
533 if (argc != 1 || c->display_usage) {
534 d_fprintf(stderr, "%s\n%s",
536 _("net sam policy show \"<account policy>\"\n"));
540 account_policy = argv[0];
541 field = account_policy_name_to_typenum(account_policy);
547 account_policy_names_list(&names, &count);
548 d_fprintf(stderr, _("No account policy by that name!\n"));
550 d_fprintf(stderr, _("Valid account policies "
552 for (i=0; i<count; i++) {
553 d_fprintf(stderr, "%s\n", names[i]);
560 if (!pdb_get_account_policy(field, &old_value)) {
561 fprintf(stderr, _("Valid account policy, but unable to "
566 printf(_("Account policy \"%s\" description: %s\n"),
567 account_policy, account_policy_get_desc(field));
568 printf(_("Account policy \"%s\" value is: %d\n"), account_policy,
573 static int net_sam_policy_list(struct net_context *c, int argc, const char **argv)
579 if (c->display_usage) {
581 "net sam policy list\n"
584 _("List account policies"));
588 account_policy_names_list(&names, &count);
590 d_fprintf(stderr, _("Valid account policies "
592 for (i = 0; i < count ; i++) {
593 d_fprintf(stderr, "%s\n", names[i]);
600 static int net_sam_policy(struct net_context *c, int argc, const char **argv)
602 struct functable func[] = {
607 N_("List account policies"),
608 N_("net sam policy list\n"
609 " List account policies")
615 N_("Show account policies"),
616 N_("net sam policy show\n"
617 " Show account policies")
623 N_("Change account policies"),
624 N_("net sam policy set\n"
625 " Change account policies")
627 {NULL, NULL, 0, NULL, NULL}
630 return net_run_function(c, argc, argv, "net sam policy", func);
633 extern PRIVS privs[];
635 static int net_sam_rights_list(struct net_context *c, int argc,
640 if (argc > 1 || c->display_usage) {
641 d_fprintf(stderr, "%s\n%s",
643 _("net sam rights list [privilege name]\n"));
649 int num = count_all_privileges();
651 for (i=0; i<num; i++) {
652 d_printf("%s\n", privs[i].name);
657 if (se_priv_from_name(argv[0], &mask)) {
662 status = privilege_enum_sids(&mask, talloc_tos(),
664 if (!NT_STATUS_IS_OK(status)) {
665 d_fprintf(stderr, _("Could not list rights: %s\n"),
670 for (i=0; i<num_sids; i++) {
671 const char *dom, *name;
672 enum lsa_SidType type;
674 if (lookup_sid(talloc_tos(), &sids[i], &dom, &name,
676 d_printf("%s\\%s\n", dom, name);
679 d_printf("%s\n", sid_string_tos(&sids[i]));
688 static int net_sam_rights_grant(struct net_context *c, int argc,
692 enum lsa_SidType type;
693 const char *dom, *name;
697 if (argc < 2 || c->display_usage) {
698 d_fprintf(stderr, "%s\n%s",
700 _("net sam rights grant <name> <rights> ...\n"));
704 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
705 &dom, &name, &sid, &type)) {
706 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
710 for (i=1; i < argc; i++) {
711 if (!se_priv_from_name(argv[i], &mask)) {
712 d_fprintf(stderr, _("%s unknown\n"), argv[i]);
716 if (!grant_privilege(&sid, &mask)) {
717 d_fprintf(stderr, _("Could not grant privilege\n"));
721 d_printf(_("Granted %s to %s\\%s\n"), argv[i], dom, name);
727 static int net_sam_rights_revoke(struct net_context *c, int argc,
731 enum lsa_SidType type;
732 const char *dom, *name;
736 if (argc < 2 || c->display_usage) {
737 d_fprintf(stderr, "%s\n%s",
739 _("net sam rights revoke <name> <rights>\n"));
743 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
744 &dom, &name, &sid, &type)) {
745 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
749 for (i=1; i < argc; i++) {
751 if (!se_priv_from_name(argv[i], &mask)) {
752 d_fprintf(stderr, _("%s unknown\n"), argv[i]);
756 if (!revoke_privilege(&sid, &mask)) {
757 d_fprintf(stderr, _("Could not revoke privilege\n"));
761 d_printf(_("Revoked %s from %s\\%s\n"), argv[i], dom, name);
767 static int net_sam_rights(struct net_context *c, int argc, const char **argv)
769 struct functable func[] = {
774 N_("List possible user rights"),
775 N_("net sam rights list\n"
776 " List possible user rights")
780 net_sam_rights_grant,
782 N_("Grant right(s)"),
783 N_("net sam rights grant\n"
788 net_sam_rights_revoke,
790 N_("Revoke right(s)"),
791 N_("net sam rights revoke\n"
794 {NULL, NULL, 0, NULL, NULL}
796 return net_run_function(c, argc, argv, "net sam rights", func);
800 * Map a unix group to a domain group
803 static NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
807 const char *grpname, *dom, *name;
810 if (pdb_getgrgid(&map, grp->gr_gid)) {
811 return NT_STATUS_GROUP_EXISTS;
814 map.gid = grp->gr_gid;
815 grpname = grp->gr_name;
817 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
818 &dom, &name, NULL, NULL)) {
820 const char *tmp = talloc_asprintf(
821 talloc_tos(), "Unix Group %s", grp->gr_name);
823 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
824 grpname, dom, name, tmp));
828 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
829 NULL, NULL, NULL, NULL)) {
830 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
831 return NT_STATUS_GROUP_EXISTS;
834 fstrcpy(map.nt_name, grpname);
836 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
837 if (!pdb_new_rid(&rid)) {
838 DEBUG(3, ("Could not get a new RID for %s\n",
840 return NT_STATUS_ACCESS_DENIED;
843 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
846 sid_compose(&map.sid, get_global_sam_sid(), rid);
847 map.sid_name_use = SID_NAME_DOM_GRP;
848 fstrcpy(map.comment, talloc_asprintf(talloc_tos(), "Unix Group %s",
851 status = pdb_add_group_mapping_entry(&map);
852 if (NT_STATUS_IS_OK(status)) {
858 static int net_sam_mapunixgroup(struct net_context *c, int argc, const char **argv)
864 if (argc != 1 || c->display_usage) {
865 d_fprintf(stderr, "%s\n%s",
867 _("net sam mapunixgroup <name>\n"));
871 grp = getgrnam(argv[0]);
873 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
877 status = map_unix_group(grp, &map);
879 if (!NT_STATUS_IS_OK(status)) {
880 d_fprintf(stderr, _("Mapping group %s failed with %s\n"),
881 argv[0], nt_errstr(status));
885 d_printf(_("Mapped unix group %s to SID %s\n"), argv[0],
886 sid_string_tos(&map.sid));
892 * Remove a group mapping
895 static NTSTATUS unmap_unix_group(const struct group *grp, GROUP_MAP *pmap)
901 map.gid = grp->gr_gid;
902 grpname = grp->gr_name;
904 if (!lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
905 NULL, NULL, NULL, NULL)) {
906 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp->gr_name));
907 return NT_STATUS_NO_SUCH_GROUP;
910 fstrcpy(map.nt_name, grpname);
912 if (!pdb_gid_to_sid(map.gid, &dom_sid)) {
913 return NT_STATUS_UNSUCCESSFUL;
916 return pdb_delete_group_mapping_entry(dom_sid);
919 static int net_sam_unmapunixgroup(struct net_context *c, int argc, const char **argv)
925 if (argc != 1 || c->display_usage) {
926 d_fprintf(stderr, "%s\n%s",
928 _("net sam unmapunixgroup <name>\n"));
932 grp = getgrnam(argv[0]);
934 d_fprintf(stderr, _("Could not find mapping for group %s.\n"),
939 status = unmap_unix_group(grp, &map);
941 if (!NT_STATUS_IS_OK(status)) {
942 d_fprintf(stderr, _("Unmapping group %s failed with %s.\n"),
943 argv[0], nt_errstr(status));
947 d_printf(_("Unmapped unix group %s.\n"), argv[0]);
953 * Create a domain group
956 static int net_sam_createdomaingroup(struct net_context *c, int argc,
962 if (argc != 1 || c->display_usage) {
963 d_fprintf(stderr, "%s\n%s",
965 _("net sam createdomaingroup <name>\n"));
969 status = pdb_create_dom_group(talloc_tos(), argv[0], &rid);
971 if (!NT_STATUS_IS_OK(status)) {
972 d_fprintf(stderr, _("Creating %s failed with %s\n"),
973 argv[0], nt_errstr(status));
977 d_printf(_("Created domain group %s with RID %d\n"), argv[0], rid);
983 * Delete a domain group
986 static int net_sam_deletedomaingroup(struct net_context *c, int argc,
991 enum lsa_SidType type;
992 const char *dom, *name;
995 if (argc != 1 || c->display_usage) {
996 d_fprintf(stderr, "%s\n%s",
998 _("net sam deletelocalgroup <name>\n"));
1002 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1003 &dom, &name, &sid, &type)) {
1004 d_fprintf(stderr, _("Could not find %s.\n"), argv[0]);
1008 if (type != SID_NAME_DOM_GRP) {
1009 d_fprintf(stderr, _("%s is a %s, not a domain group.\n"),
1010 argv[0], sid_type_lookup(type));
1014 sid_peek_rid(&sid, &rid);
1016 status = pdb_delete_dom_group(talloc_tos(), rid);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 d_fprintf(stderr,_("Deleting domain group %s failed with %s\n"),
1020 argv[0], nt_errstr(status));
1024 d_printf(_("Deleted domain group %s.\n"), argv[0]);
1030 * Create a local group
1033 static int net_sam_createlocalgroup(struct net_context *c, int argc, const char **argv)
1038 if (argc != 1 || c->display_usage) {
1039 d_fprintf(stderr, "%s\n%s",
1041 _("net sam createlocalgroup <name>\n"));
1045 if (!winbind_ping()) {
1046 d_fprintf(stderr, _("winbind seems not to run. "
1047 "createlocalgroup only works when winbind runs.\n"));
1051 status = pdb_create_alias(argv[0], &rid);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1055 argv[0], nt_errstr(status));
1059 d_printf(_("Created local group %s with RID %d\n"), argv[0], rid);
1065 * Delete a local group
1068 static int net_sam_deletelocalgroup(struct net_context *c, int argc, const char **argv)
1071 enum lsa_SidType type;
1072 const char *dom, *name;
1075 if (argc != 1 || c->display_usage) {
1076 d_fprintf(stderr, "%s\n%s",
1078 _("net sam deletelocalgroup <name>\n"));
1082 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1083 &dom, &name, &sid, &type)) {
1084 d_fprintf(stderr,_("Could not find %s.\n"), argv[0]);
1088 if (type != SID_NAME_ALIAS) {
1089 d_fprintf(stderr, _("%s is a %s, not a local group.\n"),argv[0],
1090 sid_type_lookup(type));
1094 status = pdb_delete_alias(&sid);
1096 if (!NT_STATUS_IS_OK(status)) {
1097 d_fprintf(stderr, _("Deleting local group %s failed with %s\n"),
1098 argv[0], nt_errstr(status));
1102 d_printf(_("Deleted local group %s.\n"), argv[0]);
1108 * Create a builtin group
1111 static int net_sam_createbuiltingroup(struct net_context *c, int argc, const char **argv)
1115 enum lsa_SidType type;
1119 if (argc != 1 || c->display_usage) {
1120 d_fprintf(stderr, "%s\n%s",
1122 _("net sam createbuiltingroup <name>\n"));
1126 if (!winbind_ping()) {
1127 d_fprintf(stderr, _("winbind seems not to run. "
1128 "createbuiltingroup only works when winbind "
1133 /* validate the name and get the group */
1135 fstrcpy( groupname, "BUILTIN\\" );
1136 fstrcat( groupname, argv[0] );
1138 if ( !lookup_name(talloc_tos(), groupname, LOOKUP_NAME_ALL, NULL,
1139 NULL, &sid, &type)) {
1140 d_fprintf(stderr, _("%s is not a BUILTIN group\n"), argv[0]);
1144 if ( !sid_peek_rid( &sid, &rid ) ) {
1145 d_fprintf(stderr, _("Failed to get RID for %s\n"), argv[0]);
1149 status = pdb_create_builtin_alias( rid );
1151 if (!NT_STATUS_IS_OK(status)) {
1152 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1153 argv[0], nt_errstr(status));
1157 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv[0], rid);
1163 * Add a group member
1166 static int net_sam_addmem(struct net_context *c, int argc, const char **argv)
1168 const char *groupdomain, *groupname, *memberdomain, *membername;
1169 DOM_SID group, member;
1170 enum lsa_SidType grouptype, membertype;
1173 if (argc != 2 || c->display_usage) {
1174 d_fprintf(stderr, "%s\n%s",
1176 _("net sam addmem <group> <member>\n"));
1180 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1181 &groupdomain, &groupname, &group, &grouptype)) {
1182 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1186 /* check to see if the member to be added is a name or a SID */
1188 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1189 &memberdomain, &membername, &member, &membertype))
1191 /* try it as a SID */
1193 if ( !string_to_sid( &member, argv[1] ) ) {
1194 d_fprintf(stderr, _("Could not find member %s\n"),
1199 if ( !lookup_sid(talloc_tos(), &member, &memberdomain,
1200 &membername, &membertype) )
1202 d_fprintf(stderr, _("Could not resolve SID %s\n"),
1208 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
1209 if ((membertype != SID_NAME_USER) &&
1210 (membertype != SID_NAME_DOM_GRP)) {
1211 d_fprintf(stderr, _("%s is a local group, only users "
1212 "and domain groups can be added.\n"
1213 "%s is a %s\n"), argv[0], argv[1],
1214 sid_type_lookup(membertype));
1217 status = pdb_add_aliasmem(&group, &member);
1219 if (!NT_STATUS_IS_OK(status)) {
1220 d_fprintf(stderr, _("Adding local group member failed "
1221 "with %s\n"), nt_errstr(status));
1224 } else if (grouptype == SID_NAME_DOM_GRP) {
1225 uint32_t grouprid, memberrid;
1227 sid_peek_rid(&group, &grouprid);
1228 sid_peek_rid(&member, &memberrid);
1230 status = pdb_add_groupmem(talloc_tos(), grouprid, memberrid);
1231 if (!NT_STATUS_IS_OK(status)) {
1232 d_fprintf(stderr, _("Adding domain group member failed "
1233 "with %s\n"), nt_errstr(status));
1237 d_fprintf(stderr, _("Can only add members to local groups so "
1238 "far, %s is a %s\n"), argv[0],
1239 sid_type_lookup(grouptype));
1243 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain, membername,
1244 groupdomain, groupname);
1250 * Delete a group member
1253 static int net_sam_delmem(struct net_context *c, int argc, const char **argv)
1255 const char *groupdomain, *groupname;
1256 const char *memberdomain = NULL;
1257 const char *membername = NULL;
1258 DOM_SID group, member;
1259 enum lsa_SidType grouptype;
1262 if (argc != 2 || c->display_usage) {
1263 d_fprintf(stderr,"%s\n%s",
1265 _("net sam delmem <group> <member>\n"));
1269 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1270 &groupdomain, &groupname, &group, &grouptype)) {
1271 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1275 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1276 &memberdomain, &membername, &member, NULL)) {
1277 if (!string_to_sid(&member, argv[1])) {
1278 d_fprintf(stderr, _("Could not find member %s\n"),
1284 if ((grouptype == SID_NAME_ALIAS) ||
1285 (grouptype == SID_NAME_WKN_GRP)) {
1286 status = pdb_del_aliasmem(&group, &member);
1288 if (!NT_STATUS_IS_OK(status)) {
1289 d_fprintf(stderr,_("Deleting local group member failed "
1290 "with %s\n"), nt_errstr(status));
1293 } else if (grouptype == SID_NAME_DOM_GRP) {
1294 uint32_t grouprid, memberrid;
1296 sid_peek_rid(&group, &grouprid);
1297 sid_peek_rid(&member, &memberrid);
1299 status = pdb_del_groupmem(talloc_tos(), grouprid, memberrid);
1300 if (!NT_STATUS_IS_OK(status)) {
1301 d_fprintf(stderr, _("Deleting domain group member "
1302 "failed with %s\n"), nt_errstr(status));
1306 d_fprintf(stderr, _("Can only delete members from local groups "
1307 "so far, %s is a %s\n"), argv[0],
1308 sid_type_lookup(grouptype));
1312 if (membername != NULL) {
1313 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1314 memberdomain, membername, groupdomain, groupname);
1316 d_printf(_("Deleted %s from %s\\%s\n"),
1317 sid_string_tos(&member), groupdomain, groupname);
1324 * List group members
1327 static int net_sam_listmem(struct net_context *c, int argc, const char **argv)
1329 const char *groupdomain, *groupname;
1331 DOM_SID *members = NULL;
1332 size_t i, num_members = 0;
1333 enum lsa_SidType grouptype;
1336 if (argc != 1 || c->display_usage) {
1337 d_fprintf(stderr, "%s\n%s",
1339 _("net sam listmem <group>\n"));
1343 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1344 &groupdomain, &groupname, &group, &grouptype)) {
1345 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1349 if ((grouptype == SID_NAME_ALIAS) ||
1350 (grouptype == SID_NAME_WKN_GRP)) {
1351 status = pdb_enum_aliasmem(&group, talloc_tos(), &members,
1353 if (!NT_STATUS_IS_OK(status)) {
1354 d_fprintf(stderr, _("Listing group members failed with "
1355 "%s\n"), nt_errstr(status));
1358 } else if (grouptype == SID_NAME_DOM_GRP) {
1361 status = pdb_enum_group_members(talloc_tos(), &group,
1362 &rids, &num_members);
1363 if (!NT_STATUS_IS_OK(status)) {
1364 d_fprintf(stderr, _("Listing group members failed with "
1365 "%s\n"), nt_errstr(status));
1369 members = talloc_array(talloc_tos(), struct dom_sid,
1371 if (members == NULL) {
1376 for (i=0; i<num_members; i++) {
1377 sid_compose(&members[i], get_global_sam_sid(),
1382 d_fprintf(stderr,_("Can only list local group members so far.\n"
1383 "%s is a %s\n"), argv[0], sid_type_lookup(grouptype));
1387 d_printf(_("%s\\%s has %u members\n"), groupdomain, groupname,
1388 (unsigned int)num_members);
1389 for (i=0; i<num_members; i++) {
1390 const char *dom, *name;
1391 if (lookup_sid(talloc_tos(), &members[i], &dom, &name, NULL)) {
1392 d_printf(" %s\\%s\n", dom, name);
1394 d_printf(" %s\n", sid_string_tos(&members[i]));
1398 TALLOC_FREE(members);
1406 static int net_sam_do_list(struct net_context *c, int argc, const char **argv,
1407 struct pdb_search *search, const char *what)
1409 bool verbose = (argc == 1);
1411 if ((argc > 1) || c->display_usage ||
1412 ((argc == 1) && !strequal(argv[0], "verbose"))) {
1413 d_fprintf(stderr, "%s\n", _("Usage:"));
1414 d_fprintf(stderr, _("net sam list %s [verbose]\n"), what);
1418 if (search == NULL) {
1419 d_fprintf(stderr, _("Could not start search\n"));
1424 struct samr_displayentry entry;
1425 if (!search->next_entry(search, &entry)) {
1429 d_printf("%s:%d:%s\n",
1434 d_printf("%s\n", entry.account_name);
1438 TALLOC_FREE(search);
1442 static int net_sam_list_users(struct net_context *c, int argc,
1445 return net_sam_do_list(c, argc, argv,
1446 pdb_search_users(talloc_tos(), ACB_NORMAL),
1450 static int net_sam_list_groups(struct net_context *c, int argc,
1453 return net_sam_do_list(c, argc, argv, pdb_search_groups(talloc_tos()),
1457 static int net_sam_list_localgroups(struct net_context *c, int argc,
1460 return net_sam_do_list(c, argc, argv,
1461 pdb_search_aliases(talloc_tos(),
1462 get_global_sam_sid()),
1466 static int net_sam_list_builtin(struct net_context *c, int argc,
1469 return net_sam_do_list(c, argc, argv,
1470 pdb_search_aliases(talloc_tos(),
1471 &global_sid_Builtin),
1475 static int net_sam_list_workstations(struct net_context *c, int argc,
1478 return net_sam_do_list(c, argc, argv,
1479 pdb_search_users(talloc_tos(), ACB_WSTRUST),
1487 static int net_sam_list(struct net_context *c, int argc, const char **argv)
1489 struct functable func[] = {
1493 NET_TRANSPORT_LOCAL,
1494 N_("List SAM users"),
1495 N_("net sam list users\n"
1500 net_sam_list_groups,
1501 NET_TRANSPORT_LOCAL,
1502 N_("List SAM groups"),
1503 N_("net sam list groups\n"
1508 net_sam_list_localgroups,
1509 NET_TRANSPORT_LOCAL,
1510 N_("List SAM local groups"),
1511 N_("net sam list localgroups\n"
1512 " List SAM local groups")
1516 net_sam_list_builtin,
1517 NET_TRANSPORT_LOCAL,
1518 N_("List builtin groups"),
1519 N_("net sam list builtin\n"
1520 " List builtin groups")
1524 net_sam_list_workstations,
1525 NET_TRANSPORT_LOCAL,
1526 N_("List domain member workstations"),
1527 N_("net sam list workstations\n"
1528 " List domain member workstations")
1530 {NULL, NULL, 0, NULL, NULL}
1533 return net_run_function(c, argc, argv, "net sam list", func);
1537 * Show details of SAM entries
1540 static int net_sam_show(struct net_context *c, int argc, const char **argv)
1543 enum lsa_SidType type;
1544 const char *dom, *name;
1546 if (argc != 1 || c->display_usage) {
1547 d_fprintf(stderr, "%s\n%s",
1549 _("net sam show <name>\n"));
1553 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1554 &dom, &name, &sid, &type)) {
1555 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
1559 d_printf(_("%s\\%s is a %s with SID %s\n"), dom, name,
1560 sid_type_lookup(type), sid_string_tos(&sid));
1568 * Init an LDAP tree with default users and Groups
1569 * if ldapsam:editposix is enabled
1572 static int net_sam_provision(struct net_context *c, int argc, const char **argv)
1576 char *ldap_uri = NULL;
1578 struct smbldap_state *ls;
1581 gid_t domusers_gid = -1;
1582 gid_t domadmins_gid = -1;
1583 struct samu *samuser;
1586 if (c->display_usage) {
1588 "net sam provision\n"
1591 _("Init an LDAP tree with default users/groups"));
1595 tc = talloc_new(NULL);
1597 d_fprintf(stderr, _("Out of Memory!\n"));
1601 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
1602 d_fprintf(stderr, _("talloc failed\n"));
1606 p = strchr(ldap_bk, ':');
1609 ldap_uri = talloc_strdup(tc, p+1);
1610 trim_char(ldap_uri, ' ', ' ');
1613 trim_char(ldap_bk, ' ', ' ');
1615 if (strcmp(ldap_bk, "ldapsam") != 0) {
1617 _("Provisioning works only with ldapsam backend\n"));
1621 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1622 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1624 d_fprintf(stderr, _("Provisioning works only if ldapsam:trusted"
1625 " and ldapsam:editposix are enabled.\n"));
1629 if (!winbind_ping()) {
1630 d_fprintf(stderr, _("winbind seems not to run. Provisioning "
1631 "LDAP only works when winbind runs.\n"));
1635 if (!NT_STATUS_IS_OK(smbldap_init(tc, NULL, ldap_uri, &ls))) {
1636 d_fprintf(stderr, _("Unable to connect to the LDAP server.\n"));
1640 d_printf(_("Checking for Domain Users group.\n"));
1642 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
1644 if (!pdb_getgrsid(&gmap, gsid)) {
1645 LDAPMod **mods = NULL;
1653 d_printf(_("Adding the Domain Users group.\n"));
1655 /* lets allocate a new groupid for this group */
1656 if (!winbind_allocate_gid(&domusers_gid)) {
1657 d_fprintf(stderr, _("Unable to allocate a new gid to "
1658 "create Domain Users group!\n"));
1662 uname = talloc_strdup(tc, "domusers");
1663 wname = talloc_strdup(tc, "Domain Users");
1664 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1665 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domusers_gid);
1666 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1668 if (!uname || !wname || !dn || !gidstr || !gtype) {
1669 d_fprintf(stderr, "Out of Memory!\n");
1673 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1674 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1675 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1676 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1677 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1678 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1679 sid_string_talloc(tc, &gsid));
1680 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1682 talloc_autofree_ldapmod(tc, mods);
1684 rc = smbldap_add(ls, dn, mods);
1686 if (rc != LDAP_SUCCESS) {
1687 d_fprintf(stderr, _("Failed to add Domain Users group "
1688 "to ldap directory\n"));
1691 domusers_gid = gmap.gid;
1692 d_printf(_("found!\n"));
1697 d_printf(_("Checking for Domain Admins group.\n"));
1699 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
1701 if (!pdb_getgrsid(&gmap, gsid)) {
1702 LDAPMod **mods = NULL;
1710 d_printf(_("Adding the Domain Admins group.\n"));
1712 /* lets allocate a new groupid for this group */
1713 if (!winbind_allocate_gid(&domadmins_gid)) {
1714 d_fprintf(stderr, _("Unable to allocate a new gid to "
1715 "create Domain Admins group!\n"));
1719 uname = talloc_strdup(tc, "domadmins");
1720 wname = talloc_strdup(tc, "Domain Admins");
1721 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1722 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1723 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1725 if (!uname || !wname || !dn || !gidstr || !gtype) {
1726 d_fprintf(stderr, _("Out of Memory!\n"));
1730 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1731 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1732 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1733 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1734 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1735 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1736 sid_string_talloc(tc, &gsid));
1737 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1739 talloc_autofree_ldapmod(tc, mods);
1741 rc = smbldap_add(ls, dn, mods);
1743 if (rc != LDAP_SUCCESS) {
1744 d_fprintf(stderr, _("Failed to add Domain Admins group "
1745 "to ldap directory\n"));
1748 domadmins_gid = gmap.gid;
1749 d_printf(_("found!\n"));
1754 d_printf(_("Check for Administrator account.\n"));
1756 samuser = samu_new(tc);
1758 d_fprintf(stderr, _("Out of Memory!\n"));
1762 if (!pdb_getsampwnam(samuser, "Administrator")) {
1763 LDAPMod **mods = NULL;
1774 d_printf(_("Adding the Administrator user.\n"));
1776 if (domadmins_gid == -1) {
1778 _("Can't create Administrator user, Domain "
1779 "Admins group not available!\n"));
1782 if (!winbind_allocate_uid(&uid)) {
1784 _("Unable to allocate a new uid to create "
1785 "the Administrator user!\n"));
1788 name = talloc_strdup(tc, "Administrator");
1789 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1790 uidstr = talloc_asprintf(tc, "%u", (unsigned int)uid);
1791 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1792 dir = talloc_sub_specified(tc, lp_template_homedir(),
1794 get_global_sam_name(),
1795 uid, domadmins_gid);
1796 shell = talloc_sub_specified(tc, lp_template_shell(),
1798 get_global_sam_name(),
1799 uid, domadmins_gid);
1801 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1802 d_fprintf(stderr, _("Out of Memory!\n"));
1806 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1808 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1809 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1810 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1811 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1812 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1813 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1814 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1815 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1816 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1817 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1818 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1819 sid_string_talloc(tc, &sid));
1820 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1821 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1822 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1824 talloc_autofree_ldapmod(tc, mods);
1826 rc = smbldap_add(ls, dn, mods);
1828 if (rc != LDAP_SUCCESS) {
1829 d_fprintf(stderr, _("Failed to add Administrator user "
1830 "to ldap directory\n"));
1833 d_printf(_("found!\n"));
1836 d_printf(_("Checking for Guest user.\n"));
1838 samuser = samu_new(tc);
1840 d_fprintf(stderr, _("Out of Memory!\n"));
1844 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1845 LDAPMod **mods = NULL;
1852 d_printf(_("Adding the Guest user.\n"));
1854 pwd = getpwnam_alloc(tc, lp_guestaccount());
1857 if (domusers_gid == -1) {
1859 _("Can't create Guest user, Domain "
1860 "Users group not available!\n"));
1863 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1864 d_fprintf(stderr, _("talloc failed\n"));
1867 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1868 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1870 _("Unable to allocate a new uid to "
1871 "create the Guest user!\n"));
1874 pwd->pw_gid = domusers_gid;
1875 pwd->pw_dir = talloc_strdup(tc, "/");
1876 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1877 if (!pwd->pw_dir || !pwd->pw_shell) {
1878 d_fprintf(stderr, _("Out of Memory!\n"));
1883 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1885 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1886 uidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_uid);
1887 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
1888 if (!dn || !uidstr || !gidstr) {
1889 d_fprintf(stderr, _("Out of Memory!\n"));
1893 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1894 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1895 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1896 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1897 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1898 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1899 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1900 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1901 if ((pwd->pw_dir != NULL) && (pwd->pw_dir[0] != '\0')) {
1902 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1904 if ((pwd->pw_shell != NULL) && (pwd->pw_shell[0] != '\0')) {
1905 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1907 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1908 sid_string_talloc(tc, &sid));
1909 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1910 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1911 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1913 talloc_autofree_ldapmod(tc, mods);
1915 rc = smbldap_add(ls, dn, mods);
1917 if (rc != LDAP_SUCCESS) {
1918 d_fprintf(stderr, _("Failed to add Guest user to "
1919 "ldap directory\n"));
1922 d_printf(_("found!\n"));
1925 d_printf(_("Checking Guest's group.\n"));
1927 pwd = getpwnam_alloc(talloc_autofree_context(), lp_guestaccount());
1930 _("Failed to find just created Guest account!\n"
1931 " Is nss properly configured?!\n"));
1935 if (pwd->pw_gid == domusers_gid) {
1936 d_printf(_("found!\n"));
1940 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1941 LDAPMod **mods = NULL;
1949 d_printf(_("Adding the Domain Guests group.\n"));
1951 uname = talloc_strdup(tc, "domguests");
1952 wname = talloc_strdup(tc, "Domain Guests");
1953 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1954 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
1955 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1957 if (!uname || !wname || !dn || !gidstr || !gtype) {
1958 d_fprintf(stderr, _("Out of Memory!\n"));
1962 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1964 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1965 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1966 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1967 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1968 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1969 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1970 sid_string_talloc(tc, &gsid));
1971 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1973 talloc_autofree_ldapmod(tc, mods);
1975 rc = smbldap_add(ls, dn, mods);
1977 if (rc != LDAP_SUCCESS) {
1979 _("Failed to add Domain Guests group to ldap "
1983 d_printf(_("found!\n"));
1998 /***********************************************************
1999 migrated functionality from smbgroupedit
2000 **********************************************************/
2001 int net_sam(struct net_context *c, int argc, const char **argv)
2003 struct functable func[] = {
2005 "createbuiltingroup",
2006 net_sam_createbuiltingroup,
2007 NET_TRANSPORT_LOCAL,
2008 N_("Create a new BUILTIN group"),
2009 N_("net sam createbuiltingroup\n"
2010 " Create a new BUILTIN group")
2014 net_sam_createlocalgroup,
2015 NET_TRANSPORT_LOCAL,
2016 N_("Create a new local group"),
2017 N_("net sam createlocalgroup\n"
2018 " Create a new local group")
2021 "createdomaingroup",
2022 net_sam_createdomaingroup,
2023 NET_TRANSPORT_LOCAL,
2024 N_("Create a new group"),
2025 N_("net sam createdomaingroup\n"
2026 " Create a new group")
2030 net_sam_deletelocalgroup,
2031 NET_TRANSPORT_LOCAL,
2032 N_("Delete an existing local group"),
2033 N_("net sam deletelocalgroup\n"
2034 " Delete an existing local group")
2037 "deletedomaingroup",
2038 net_sam_deletedomaingroup,
2039 NET_TRANSPORT_LOCAL,
2040 N_("Delete a domain group"),
2041 N_("net sam deletedomaingroup\n"
2046 net_sam_mapunixgroup,
2047 NET_TRANSPORT_LOCAL,
2048 N_("Map a unix group to a domain group"),
2049 N_("net sam mapunixgroup\n"
2050 " Map a unix group to a domain group")
2054 net_sam_unmapunixgroup,
2055 NET_TRANSPORT_LOCAL,
2056 N_("Remove a group mapping of an unix group to a "
2058 N_("net sam unmapunixgroup\n"
2059 " Remove a group mapping of an unix group to a "
2065 NET_TRANSPORT_LOCAL,
2066 N_("Add a member to a group"),
2067 N_("net sam addmem\n"
2068 " Add a member to a group")
2073 NET_TRANSPORT_LOCAL,
2074 N_("Delete a member from a group"),
2075 N_("net sam delmem\n"
2076 " Delete a member from a group")
2081 NET_TRANSPORT_LOCAL,
2082 N_("List group members"),
2083 N_("net sam listmem\n"
2084 " List group members")
2089 NET_TRANSPORT_LOCAL,
2090 N_("List users, groups and local groups"),
2092 " List users, groups and local groups")
2097 NET_TRANSPORT_LOCAL,
2098 N_("Show details of a SAM entry"),
2100 " Show details of a SAM entry")
2105 NET_TRANSPORT_LOCAL,
2106 N_("Set details of a SAM account"),
2108 " Set details of a SAM account")
2113 NET_TRANSPORT_LOCAL,
2114 N_("Set account policies"),
2115 N_("net sam policy\n"
2116 " Set account policies")
2121 NET_TRANSPORT_LOCAL,
2122 N_("Manipulate user privileges"),
2123 N_("net sam rights\n"
2124 " Manipulate user privileges")
2130 NET_TRANSPORT_LOCAL,
2131 N_("Provision a clean user database"),
2132 N_("net sam privison\n"
2133 " Provision a clear user database")
2136 {NULL, NULL, 0, NULL, NULL}
2139 if (getuid() != 0) {
2140 d_fprintf(stderr, _("You are not root, most things won't "
2144 return net_run_function(c, argc, argv, "net sam", func);