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
)
902 map
.gid
= grp
->gr_gid
;
903 grpname
= grp
->gr_name
;
905 if (!lookup_name(talloc_tos(), grpname
, LOOKUP_NAME_LOCAL
,
906 NULL
, NULL
, NULL
, NULL
)) {
907 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp
->gr_name
));
908 return NT_STATUS_NO_SUCH_GROUP
;
911 fstrcpy(map
.nt_name
, grpname
);
913 if (!pdb_gid_to_sid(map
.gid
, &dom_sid
)) {
914 return NT_STATUS_UNSUCCESSFUL
;
917 status
= pdb_delete_group_mapping_entry(dom_sid
);
922 static int net_sam_unmapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
928 if (argc
!= 1 || c
->display_usage
) {
929 d_fprintf(stderr
, "%s\n%s",
931 _("net sam unmapunixgroup <name>\n"));
935 grp
= getgrnam(argv
[0]);
937 d_fprintf(stderr
, _("Could not find mapping for group %s.\n"),
942 status
= unmap_unix_group(grp
, &map
);
944 if (!NT_STATUS_IS_OK(status
)) {
945 d_fprintf(stderr
, _("Unmapping group %s failed with %s.\n"),
946 argv
[0], nt_errstr(status
));
950 d_printf(_("Unmapped unix group %s.\n"), argv
[0]);
956 * Create a domain group
959 static int net_sam_createdomaingroup(struct net_context
*c
, int argc
,
965 if (argc
!= 1 || c
->display_usage
) {
966 d_fprintf(stderr
, "%s\n%s",
968 _("net sam createdomaingroup <name>\n"));
972 status
= pdb_create_dom_group(talloc_tos(), argv
[0], &rid
);
974 if (!NT_STATUS_IS_OK(status
)) {
975 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
976 argv
[0], nt_errstr(status
));
980 d_printf(_("Created domain group %s with RID %d\n"), argv
[0], rid
);
986 * Delete a domain group
989 static int net_sam_deletedomaingroup(struct net_context
*c
, int argc
,
994 enum lsa_SidType type
;
995 const char *dom
, *name
;
998 if (argc
!= 1 || c
->display_usage
) {
999 d_fprintf(stderr
, "%s\n%s",
1001 _("net sam deletelocalgroup <name>\n"));
1005 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1006 &dom
, &name
, &sid
, &type
)) {
1007 d_fprintf(stderr
, _("Could not find %s.\n"), argv
[0]);
1011 if (type
!= SID_NAME_DOM_GRP
) {
1012 d_fprintf(stderr
, _("%s is a %s, not a domain group.\n"),
1013 argv
[0], sid_type_lookup(type
));
1017 sid_peek_rid(&sid
, &rid
);
1019 status
= pdb_delete_dom_group(talloc_tos(), rid
);
1021 if (!NT_STATUS_IS_OK(status
)) {
1022 d_fprintf(stderr
,_("Deleting domain group %s failed with %s\n"),
1023 argv
[0], nt_errstr(status
));
1027 d_printf(_("Deleted domain group %s.\n"), argv
[0]);
1033 * Create a local group
1036 static int net_sam_createlocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1041 if (argc
!= 1 || c
->display_usage
) {
1042 d_fprintf(stderr
, "%s\n%s",
1044 _("net sam createlocalgroup <name>\n"));
1048 if (!winbind_ping()) {
1049 d_fprintf(stderr
, _("winbind seems not to run. "
1050 "createlocalgroup only works when winbind runs.\n"));
1054 status
= pdb_create_alias(argv
[0], &rid
);
1056 if (!NT_STATUS_IS_OK(status
)) {
1057 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1058 argv
[0], nt_errstr(status
));
1062 d_printf(_("Created local group %s with RID %d\n"), argv
[0], rid
);
1068 * Delete a local group
1071 static int net_sam_deletelocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1074 enum lsa_SidType type
;
1075 const char *dom
, *name
;
1078 if (argc
!= 1 || c
->display_usage
) {
1079 d_fprintf(stderr
, "%s\n%s",
1081 _("net sam deletelocalgroup <name>\n"));
1085 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1086 &dom
, &name
, &sid
, &type
)) {
1087 d_fprintf(stderr
,_("Could not find %s.\n"), argv
[0]);
1091 if (type
!= SID_NAME_ALIAS
) {
1092 d_fprintf(stderr
, _("%s is a %s, not a local group.\n"),argv
[0],
1093 sid_type_lookup(type
));
1097 status
= pdb_delete_alias(&sid
);
1099 if (!NT_STATUS_IS_OK(status
)) {
1100 d_fprintf(stderr
, _("Deleting local group %s failed with %s\n"),
1101 argv
[0], nt_errstr(status
));
1105 d_printf(_("Deleted local group %s.\n"), argv
[0]);
1111 * Create a builtin group
1114 static int net_sam_createbuiltingroup(struct net_context
*c
, int argc
, const char **argv
)
1118 enum lsa_SidType type
;
1122 if (argc
!= 1 || c
->display_usage
) {
1123 d_fprintf(stderr
, "%s\n%s",
1125 _("net sam createbuiltingroup <name>\n"));
1129 if (!winbind_ping()) {
1130 d_fprintf(stderr
, _("winbind seems not to run. "
1131 "createbuiltingroup only works when winbind "
1136 /* validate the name and get the group */
1138 fstrcpy( groupname
, "BUILTIN\\" );
1139 fstrcat( groupname
, argv
[0] );
1141 if ( !lookup_name(talloc_tos(), groupname
, LOOKUP_NAME_ALL
, NULL
,
1142 NULL
, &sid
, &type
)) {
1143 d_fprintf(stderr
, _("%s is not a BUILTIN group\n"), argv
[0]);
1147 if ( !sid_peek_rid( &sid
, &rid
) ) {
1148 d_fprintf(stderr
, _("Failed to get RID for %s\n"), argv
[0]);
1152 status
= pdb_create_builtin_alias( rid
);
1154 if (!NT_STATUS_IS_OK(status
)) {
1155 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1156 argv
[0], nt_errstr(status
));
1160 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv
[0], rid
);
1166 * Add a group member
1169 static int net_sam_addmem(struct net_context
*c
, int argc
, const char **argv
)
1171 const char *groupdomain
, *groupname
, *memberdomain
, *membername
;
1172 DOM_SID group
, member
;
1173 enum lsa_SidType grouptype
, membertype
;
1176 if (argc
!= 2 || c
->display_usage
) {
1177 d_fprintf(stderr
, "%s\n%s",
1179 _("net sam addmem <group> <member>\n"));
1183 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1184 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1185 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1189 /* check to see if the member to be added is a name or a SID */
1191 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1192 &memberdomain
, &membername
, &member
, &membertype
))
1194 /* try it as a SID */
1196 if ( !string_to_sid( &member
, argv
[1] ) ) {
1197 d_fprintf(stderr
, _("Could not find member %s\n"),
1202 if ( !lookup_sid(talloc_tos(), &member
, &memberdomain
,
1203 &membername
, &membertype
) )
1205 d_fprintf(stderr
, _("Could not resolve SID %s\n"),
1211 if ((grouptype
== SID_NAME_ALIAS
) || (grouptype
== SID_NAME_WKN_GRP
)) {
1212 if ((membertype
!= SID_NAME_USER
) &&
1213 (membertype
!= SID_NAME_DOM_GRP
)) {
1214 d_fprintf(stderr
, _("%s is a local group, only users "
1215 "and domain groups can be added.\n"
1216 "%s is a %s\n"), argv
[0], argv
[1],
1217 sid_type_lookup(membertype
));
1220 status
= pdb_add_aliasmem(&group
, &member
);
1222 if (!NT_STATUS_IS_OK(status
)) {
1223 d_fprintf(stderr
, _("Adding local group member failed "
1224 "with %s\n"), nt_errstr(status
));
1227 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1228 uint32_t grouprid
, memberrid
;
1230 sid_peek_rid(&group
, &grouprid
);
1231 sid_peek_rid(&member
, &memberrid
);
1233 status
= pdb_add_groupmem(talloc_tos(), grouprid
, memberrid
);
1234 if (!NT_STATUS_IS_OK(status
)) {
1235 d_fprintf(stderr
, _("Adding domain group member failed "
1236 "with %s\n"), nt_errstr(status
));
1240 d_fprintf(stderr
, _("Can only add members to local groups so "
1241 "far, %s is a %s\n"), argv
[0],
1242 sid_type_lookup(grouptype
));
1246 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain
, membername
,
1247 groupdomain
, groupname
);
1253 * Delete a group member
1256 static int net_sam_delmem(struct net_context
*c
, int argc
, const char **argv
)
1258 const char *groupdomain
, *groupname
;
1259 const char *memberdomain
= NULL
;
1260 const char *membername
= NULL
;
1261 DOM_SID group
, member
;
1262 enum lsa_SidType grouptype
;
1265 if (argc
!= 2 || c
->display_usage
) {
1266 d_fprintf(stderr
,"%s\n%s",
1268 _("net sam delmem <group> <member>\n"));
1272 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1273 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1274 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1278 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1279 &memberdomain
, &membername
, &member
, NULL
)) {
1280 if (!string_to_sid(&member
, argv
[1])) {
1281 d_fprintf(stderr
, _("Could not find member %s\n"),
1287 if ((grouptype
== SID_NAME_ALIAS
) ||
1288 (grouptype
== SID_NAME_WKN_GRP
)) {
1289 status
= pdb_del_aliasmem(&group
, &member
);
1291 if (!NT_STATUS_IS_OK(status
)) {
1292 d_fprintf(stderr
,_("Deleting local group member failed "
1293 "with %s\n"), nt_errstr(status
));
1296 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1297 uint32_t grouprid
, memberrid
;
1299 sid_peek_rid(&group
, &grouprid
);
1300 sid_peek_rid(&member
, &memberrid
);
1302 status
= pdb_del_groupmem(talloc_tos(), grouprid
, memberrid
);
1303 if (!NT_STATUS_IS_OK(status
)) {
1304 d_fprintf(stderr
, _("Deleting domain group member "
1305 "failed with %s\n"), nt_errstr(status
));
1309 d_fprintf(stderr
, _("Can only delete members from local groups "
1310 "so far, %s is a %s\n"), argv
[0],
1311 sid_type_lookup(grouptype
));
1315 if (membername
!= NULL
) {
1316 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1317 memberdomain
, membername
, groupdomain
, groupname
);
1319 d_printf(_("Deleted %s from %s\\%s\n"),
1320 sid_string_tos(&member
), groupdomain
, groupname
);
1327 * List group members
1330 static int net_sam_listmem(struct net_context
*c
, int argc
, const char **argv
)
1332 const char *groupdomain
, *groupname
;
1334 DOM_SID
*members
= NULL
;
1335 size_t i
, num_members
= 0;
1336 enum lsa_SidType grouptype
;
1339 if (argc
!= 1 || c
->display_usage
) {
1340 d_fprintf(stderr
, "%s\n%s",
1342 _("net sam listmem <group>\n"));
1346 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1347 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1348 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1352 if ((grouptype
== SID_NAME_ALIAS
) ||
1353 (grouptype
== SID_NAME_WKN_GRP
)) {
1354 status
= pdb_enum_aliasmem(&group
, talloc_tos(), &members
,
1356 if (!NT_STATUS_IS_OK(status
)) {
1357 d_fprintf(stderr
, _("Listing group members failed with "
1358 "%s\n"), nt_errstr(status
));
1361 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1364 status
= pdb_enum_group_members(talloc_tos(), &group
,
1365 &rids
, &num_members
);
1366 if (!NT_STATUS_IS_OK(status
)) {
1367 d_fprintf(stderr
, _("Listing group members failed with "
1368 "%s\n"), nt_errstr(status
));
1372 members
= talloc_array(talloc_tos(), struct dom_sid
,
1374 if (members
== NULL
) {
1379 for (i
=0; i
<num_members
; i
++) {
1380 sid_compose(&members
[i
], get_global_sam_sid(),
1385 d_fprintf(stderr
,_("Can only list local group members so far.\n"
1386 "%s is a %s\n"), argv
[0], sid_type_lookup(grouptype
));
1390 d_printf(_("%s\\%s has %u members\n"), groupdomain
, groupname
,
1391 (unsigned int)num_members
);
1392 for (i
=0; i
<num_members
; i
++) {
1393 const char *dom
, *name
;
1394 if (lookup_sid(talloc_tos(), &members
[i
], &dom
, &name
, NULL
)) {
1395 d_printf(" %s\\%s\n", dom
, name
);
1397 d_printf(" %s\n", sid_string_tos(&members
[i
]));
1401 TALLOC_FREE(members
);
1409 static int net_sam_do_list(struct net_context
*c
, int argc
, const char **argv
,
1410 struct pdb_search
*search
, const char *what
)
1412 bool verbose
= (argc
== 1);
1414 if ((argc
> 1) || c
->display_usage
||
1415 ((argc
== 1) && !strequal(argv
[0], "verbose"))) {
1416 d_fprintf(stderr
, "%s\n", _("Usage:"));
1417 d_fprintf(stderr
, _("net sam list %s [verbose]\n"), what
);
1421 if (search
== NULL
) {
1422 d_fprintf(stderr
, _("Could not start search\n"));
1427 struct samr_displayentry entry
;
1428 if (!search
->next_entry(search
, &entry
)) {
1432 d_printf("%s:%d:%s\n",
1437 d_printf("%s\n", entry
.account_name
);
1441 TALLOC_FREE(search
);
1445 static int net_sam_list_users(struct net_context
*c
, int argc
,
1448 return net_sam_do_list(c
, argc
, argv
,
1449 pdb_search_users(talloc_tos(), ACB_NORMAL
),
1453 static int net_sam_list_groups(struct net_context
*c
, int argc
,
1456 return net_sam_do_list(c
, argc
, argv
, pdb_search_groups(talloc_tos()),
1460 static int net_sam_list_localgroups(struct net_context
*c
, int argc
,
1463 return net_sam_do_list(c
, argc
, argv
,
1464 pdb_search_aliases(talloc_tos(),
1465 get_global_sam_sid()),
1469 static int net_sam_list_builtin(struct net_context
*c
, int argc
,
1472 return net_sam_do_list(c
, argc
, argv
,
1473 pdb_search_aliases(talloc_tos(),
1474 &global_sid_Builtin
),
1478 static int net_sam_list_workstations(struct net_context
*c
, int argc
,
1481 return net_sam_do_list(c
, argc
, argv
,
1482 pdb_search_users(talloc_tos(), ACB_WSTRUST
),
1490 static int net_sam_list(struct net_context
*c
, int argc
, const char **argv
)
1492 struct functable func
[] = {
1496 NET_TRANSPORT_LOCAL
,
1497 N_("List SAM users"),
1498 N_("net sam list users\n"
1503 net_sam_list_groups
,
1504 NET_TRANSPORT_LOCAL
,
1505 N_("List SAM groups"),
1506 N_("net sam list groups\n"
1511 net_sam_list_localgroups
,
1512 NET_TRANSPORT_LOCAL
,
1513 N_("List SAM local groups"),
1514 N_("net sam list localgroups\n"
1515 " List SAM local groups")
1519 net_sam_list_builtin
,
1520 NET_TRANSPORT_LOCAL
,
1521 N_("List builtin groups"),
1522 N_("net sam list builtin\n"
1523 " List builtin groups")
1527 net_sam_list_workstations
,
1528 NET_TRANSPORT_LOCAL
,
1529 N_("List domain member workstations"),
1530 N_("net sam list workstations\n"
1531 " List domain member workstations")
1533 {NULL
, NULL
, 0, NULL
, NULL
}
1536 return net_run_function(c
, argc
, argv
, "net sam list", func
);
1540 * Show details of SAM entries
1543 static int net_sam_show(struct net_context
*c
, int argc
, const char **argv
)
1546 enum lsa_SidType type
;
1547 const char *dom
, *name
;
1549 if (argc
!= 1 || c
->display_usage
) {
1550 d_fprintf(stderr
, "%s\n%s",
1552 _("net sam show <name>\n"));
1556 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1557 &dom
, &name
, &sid
, &type
)) {
1558 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
1562 d_printf(_("%s\\%s is a %s with SID %s\n"), dom
, name
,
1563 sid_type_lookup(type
), sid_string_tos(&sid
));
1571 * Init an LDAP tree with default users and Groups
1572 * if ldapsam:editposix is enabled
1575 static int net_sam_provision(struct net_context
*c
, int argc
, const char **argv
)
1579 char *ldap_uri
= NULL
;
1581 struct smbldap_state
*ls
;
1584 gid_t domusers_gid
= -1;
1585 gid_t domadmins_gid
= -1;
1586 struct samu
*samuser
;
1589 if (c
->display_usage
) {
1591 "net sam provision\n"
1594 _("Init an LDAP tree with default users/groups"));
1598 tc
= talloc_new(NULL
);
1600 d_fprintf(stderr
, _("Out of Memory!\n"));
1604 if ((ldap_bk
= talloc_strdup(tc
, lp_passdb_backend())) == NULL
) {
1605 d_fprintf(stderr
, _("talloc failed\n"));
1609 p
= strchr(ldap_bk
, ':');
1612 ldap_uri
= talloc_strdup(tc
, p
+1);
1613 trim_char(ldap_uri
, ' ', ' ');
1616 trim_char(ldap_bk
, ' ', ' ');
1618 if (strcmp(ldap_bk
, "ldapsam") != 0) {
1620 _("Provisioning works only with ldapsam backend\n"));
1624 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1625 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1627 d_fprintf(stderr
, _("Provisioning works only if ldapsam:trusted"
1628 " and ldapsam:editposix are enabled.\n"));
1632 if (!winbind_ping()) {
1633 d_fprintf(stderr
, _("winbind seems not to run. Provisioning "
1634 "LDAP only works when winbind runs.\n"));
1638 if (!NT_STATUS_IS_OK(smbldap_init(tc
, NULL
, ldap_uri
, &ls
))) {
1639 d_fprintf(stderr
, _("Unable to connect to the LDAP server.\n"));
1643 d_printf(_("Checking for Domain Users group.\n"));
1645 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS
);
1647 if (!pdb_getgrsid(&gmap
, gsid
)) {
1648 LDAPMod
**mods
= NULL
;
1656 d_printf(_("Adding the Domain Users group.\n"));
1658 /* lets allocate a new groupid for this group */
1659 if (!winbind_allocate_gid(&domusers_gid
)) {
1660 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1661 "create Domain Users group!\n"));
1665 uname
= talloc_strdup(tc
, "domusers");
1666 wname
= talloc_strdup(tc
, "Domain Users");
1667 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1668 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domusers_gid
);
1669 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1671 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1672 d_fprintf(stderr
, "Out of Memory!\n");
1676 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1677 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1678 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1679 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1680 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1681 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1682 sid_string_talloc(tc
, &gsid
));
1683 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1685 talloc_autofree_ldapmod(tc
, mods
);
1687 rc
= smbldap_add(ls
, dn
, mods
);
1689 if (rc
!= LDAP_SUCCESS
) {
1690 d_fprintf(stderr
, _("Failed to add Domain Users group "
1691 "to ldap directory\n"));
1694 domusers_gid
= gmap
.gid
;
1695 d_printf(_("found!\n"));
1700 d_printf(_("Checking for Domain Admins group.\n"));
1702 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS
);
1704 if (!pdb_getgrsid(&gmap
, gsid
)) {
1705 LDAPMod
**mods
= NULL
;
1713 d_printf(_("Adding the Domain Admins group.\n"));
1715 /* lets allocate a new groupid for this group */
1716 if (!winbind_allocate_gid(&domadmins_gid
)) {
1717 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1718 "create Domain Admins group!\n"));
1722 uname
= talloc_strdup(tc
, "domadmins");
1723 wname
= talloc_strdup(tc
, "Domain Admins");
1724 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1725 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1726 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1728 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1729 d_fprintf(stderr
, _("Out of Memory!\n"));
1733 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1734 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1735 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1736 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1737 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1738 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1739 sid_string_talloc(tc
, &gsid
));
1740 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1742 talloc_autofree_ldapmod(tc
, mods
);
1744 rc
= smbldap_add(ls
, dn
, mods
);
1746 if (rc
!= LDAP_SUCCESS
) {
1747 d_fprintf(stderr
, _("Failed to add Domain Admins group "
1748 "to ldap directory\n"));
1751 domadmins_gid
= gmap
.gid
;
1752 d_printf(_("found!\n"));
1757 d_printf(_("Check for Administrator account.\n"));
1759 samuser
= samu_new(tc
);
1761 d_fprintf(stderr
, _("Out of Memory!\n"));
1765 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1766 LDAPMod
**mods
= NULL
;
1777 d_printf(_("Adding the Administrator user.\n"));
1779 if (domadmins_gid
== -1) {
1781 _("Can't create Administrator user, Domain "
1782 "Admins group not available!\n"));
1785 if (!winbind_allocate_uid(&uid
)) {
1787 _("Unable to allocate a new uid to create "
1788 "the Administrator user!\n"));
1791 name
= talloc_strdup(tc
, "Administrator");
1792 dn
= talloc_asprintf(tc
, "uid=Administrator,%s", lp_ldap_user_suffix());
1793 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)uid
);
1794 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1795 dir
= talloc_sub_specified(tc
, lp_template_homedir(),
1797 get_global_sam_name(),
1798 uid
, domadmins_gid
);
1799 shell
= talloc_sub_specified(tc
, lp_template_shell(),
1801 get_global_sam_name(),
1802 uid
, domadmins_gid
);
1804 if (!name
|| !dn
|| !uidstr
|| !gidstr
|| !dir
|| !shell
) {
1805 d_fprintf(stderr
, _("Out of Memory!\n"));
1809 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN
);
1811 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1812 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1813 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1814 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", name
);
1815 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
1816 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
1817 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1818 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1819 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", dir
);
1820 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
1821 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
1822 sid_string_talloc(tc
, &sid
));
1823 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
1824 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
1825 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1827 talloc_autofree_ldapmod(tc
, mods
);
1829 rc
= smbldap_add(ls
, dn
, mods
);
1831 if (rc
!= LDAP_SUCCESS
) {
1832 d_fprintf(stderr
, _("Failed to add Administrator user "
1833 "to ldap directory\n"));
1836 d_printf(_("found!\n"));
1839 d_printf(_("Checking for Guest user.\n"));
1841 samuser
= samu_new(tc
);
1843 d_fprintf(stderr
, _("Out of Memory!\n"));
1847 if (!pdb_getsampwnam(samuser
, lp_guestaccount())) {
1848 LDAPMod
**mods
= NULL
;
1855 d_printf(_("Adding the Guest user.\n"));
1857 pwd
= Get_Pwnam_alloc(tc
, lp_guestaccount());
1860 if (domusers_gid
== -1) {
1862 _("Can't create Guest user, Domain "
1863 "Users group not available!\n"));
1866 if ((pwd
= talloc(tc
, struct passwd
)) == NULL
) {
1867 d_fprintf(stderr
, _("talloc failed\n"));
1870 pwd
->pw_name
= talloc_strdup(pwd
, lp_guestaccount());
1871 if (!winbind_allocate_uid(&(pwd
->pw_uid
))) {
1873 _("Unable to allocate a new uid to "
1874 "create the Guest user!\n"));
1877 pwd
->pw_gid
= domusers_gid
;
1878 pwd
->pw_dir
= talloc_strdup(tc
, "/");
1879 pwd
->pw_shell
= talloc_strdup(tc
, "/bin/false");
1880 if (!pwd
->pw_dir
|| !pwd
->pw_shell
) {
1881 d_fprintf(stderr
, _("Out of Memory!\n"));
1886 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_USER_RID_GUEST
);
1888 dn
= talloc_asprintf(tc
, "uid=%s,%s", pwd
->pw_name
, lp_ldap_user_suffix ());
1889 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_uid
);
1890 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
1891 if (!dn
|| !uidstr
|| !gidstr
) {
1892 d_fprintf(stderr
, _("Out of Memory!\n"));
1896 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1897 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1898 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1899 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", pwd
->pw_name
);
1900 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", pwd
->pw_name
);
1901 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", pwd
->pw_name
);
1902 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1903 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1904 if ((pwd
->pw_dir
!= NULL
) && (pwd
->pw_dir
[0] != '\0')) {
1905 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", pwd
->pw_dir
);
1907 if ((pwd
->pw_shell
!= NULL
) && (pwd
->pw_shell
[0] != '\0')) {
1908 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", pwd
->pw_shell
);
1910 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
1911 sid_string_talloc(tc
, &sid
));
1912 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
1913 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
1914 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1916 talloc_autofree_ldapmod(tc
, mods
);
1918 rc
= smbldap_add(ls
, dn
, mods
);
1920 if (rc
!= LDAP_SUCCESS
) {
1921 d_fprintf(stderr
, _("Failed to add Guest user to "
1922 "ldap directory\n"));
1925 d_printf(_("found!\n"));
1928 d_printf(_("Checking Guest's group.\n"));
1930 pwd
= Get_Pwnam_alloc(talloc_autofree_context(), lp_guestaccount());
1933 _("Failed to find just created Guest account!\n"
1934 " Is nss properly configured?!\n"));
1938 if (pwd
->pw_gid
== domusers_gid
) {
1939 d_printf(_("found!\n"));
1943 if (!pdb_getgrgid(&gmap
, pwd
->pw_gid
)) {
1944 LDAPMod
**mods
= NULL
;
1952 d_printf(_("Adding the Domain Guests group.\n"));
1954 uname
= talloc_strdup(tc
, "domguests");
1955 wname
= talloc_strdup(tc
, "Domain Guests");
1956 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1957 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
1958 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1960 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1961 d_fprintf(stderr
, _("Out of Memory!\n"));
1965 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS
);
1967 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1968 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1969 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1970 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1971 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1972 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1973 sid_string_talloc(tc
, &gsid
));
1974 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1976 talloc_autofree_ldapmod(tc
, mods
);
1978 rc
= smbldap_add(ls
, dn
, mods
);
1980 if (rc
!= LDAP_SUCCESS
) {
1982 _("Failed to add Domain Guests group to ldap "
1986 d_printf(_("found!\n"));
2001 /***********************************************************
2002 migrated functionality from smbgroupedit
2003 **********************************************************/
2004 int net_sam(struct net_context
*c
, int argc
, const char **argv
)
2006 struct functable func
[] = {
2008 "createbuiltingroup",
2009 net_sam_createbuiltingroup
,
2010 NET_TRANSPORT_LOCAL
,
2011 N_("Create a new BUILTIN group"),
2012 N_("net sam createbuiltingroup\n"
2013 " Create a new BUILTIN group")
2017 net_sam_createlocalgroup
,
2018 NET_TRANSPORT_LOCAL
,
2019 N_("Create a new local group"),
2020 N_("net sam createlocalgroup\n"
2021 " Create a new local group")
2024 "createdomaingroup",
2025 net_sam_createdomaingroup
,
2026 NET_TRANSPORT_LOCAL
,
2027 N_("Create a new group"),
2028 N_("net sam createdomaingroup\n"
2029 " Create a new group")
2033 net_sam_deletelocalgroup
,
2034 NET_TRANSPORT_LOCAL
,
2035 N_("Delete an existing local group"),
2036 N_("net sam deletelocalgroup\n"
2037 " Delete an existing local group")
2040 "deletedomaingroup",
2041 net_sam_deletedomaingroup
,
2042 NET_TRANSPORT_LOCAL
,
2043 N_("Delete a domain group"),
2044 N_("net sam deletedomaingroup\n"
2049 net_sam_mapunixgroup
,
2050 NET_TRANSPORT_LOCAL
,
2051 N_("Map a unix group to a domain group"),
2052 N_("net sam mapunixgroup\n"
2053 " Map a unix group to a domain group")
2057 net_sam_unmapunixgroup
,
2058 NET_TRANSPORT_LOCAL
,
2059 N_("Remove a group mapping of an unix group to a "
2061 N_("net sam unmapunixgroup\n"
2062 " Remove a group mapping of an unix group to a "
2068 NET_TRANSPORT_LOCAL
,
2069 N_("Add a member to a group"),
2070 N_("net sam addmem\n"
2071 " Add a member to a group")
2076 NET_TRANSPORT_LOCAL
,
2077 N_("Delete a member from a group"),
2078 N_("net sam delmem\n"
2079 " Delete a member from a group")
2084 NET_TRANSPORT_LOCAL
,
2085 N_("List group members"),
2086 N_("net sam listmem\n"
2087 " List group members")
2092 NET_TRANSPORT_LOCAL
,
2093 N_("List users, groups and local groups"),
2095 " List users, groups and local groups")
2100 NET_TRANSPORT_LOCAL
,
2101 N_("Show details of a SAM entry"),
2103 " Show details of a SAM entry")
2108 NET_TRANSPORT_LOCAL
,
2109 N_("Set details of a SAM account"),
2111 " Set details of a SAM account")
2116 NET_TRANSPORT_LOCAL
,
2117 N_("Set account policies"),
2118 N_("net sam policy\n"
2119 " Set account policies")
2124 NET_TRANSPORT_LOCAL
,
2125 N_("Manipulate user privileges"),
2126 N_("net sam rights\n"
2127 " Manipulate user privileges")
2133 NET_TRANSPORT_LOCAL
,
2134 N_("Provision a clean user database"),
2135 N_("net sam privison\n"
2136 " Provision a clear user database")
2139 {NULL
, NULL
, 0, NULL
, NULL
}
2142 if (getuid() != 0) {
2143 d_fprintf(stderr
, _("You are not root, most things won't "
2147 return net_run_function(c
, argc
, argv
, "net sam", func
);