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"
29 #include "lib/privileges.h"
35 static int net_sam_userset(struct net_context
*c
, int argc
, const char **argv
,
37 bool (*fn
)(struct samu
*, const char *,
38 enum pdb_value_state
))
40 struct samu
*sam_acct
= NULL
;
42 enum lsa_SidType type
;
43 const char *dom
, *name
;
46 if (argc
!= 2 || c
->display_usage
) {
47 d_fprintf(stderr
, "%s\n", _("Usage:"));
48 d_fprintf(stderr
, _("net sam set %s <user> <value>\n"),
53 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
54 &dom
, &name
, &sid
, &type
)) {
55 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
59 if (type
!= SID_NAME_USER
) {
60 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
61 sid_type_lookup(type
));
65 if ( !(sam_acct
= samu_new( NULL
)) ) {
66 d_fprintf(stderr
, _("Internal error\n"));
70 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
71 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
75 if (!fn(sam_acct
, argv
[1], PDB_CHANGED
)) {
76 d_fprintf(stderr
, _("Internal error\n"));
80 status
= pdb_update_sam_account(sam_acct
);
81 if (!NT_STATUS_IS_OK(status
)) {
82 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
83 argv
[0], nt_errstr(status
));
87 TALLOC_FREE(sam_acct
);
89 d_printf(_("Updated %s for %s\\%s to %s\n"), field
, dom
, name
, argv
[1]);
93 static int net_sam_set_fullname(struct net_context
*c
, int argc
,
96 return net_sam_userset(c
, argc
, argv
, "fullname",
100 static int net_sam_set_logonscript(struct net_context
*c
, int argc
,
103 return net_sam_userset(c
, argc
, argv
, "logonscript",
104 pdb_set_logon_script
);
107 static int net_sam_set_profilepath(struct net_context
*c
, int argc
,
110 return net_sam_userset(c
, argc
, argv
, "profilepath",
111 pdb_set_profile_path
);
114 static int net_sam_set_homedrive(struct net_context
*c
, int argc
,
117 return net_sam_userset(c
, argc
, argv
, "homedrive",
121 static int net_sam_set_homedir(struct net_context
*c
, int argc
,
124 return net_sam_userset(c
, argc
, argv
, "homedir",
128 static int net_sam_set_workstations(struct net_context
*c
, int argc
,
131 return net_sam_userset(c
, argc
, argv
, "workstations",
132 pdb_set_workstations
);
139 static int net_sam_set_userflag(struct net_context
*c
, int argc
,
140 const char **argv
, const char *field
,
143 struct samu
*sam_acct
= NULL
;
145 enum lsa_SidType type
;
146 const char *dom
, *name
;
150 if ((argc
!= 2) || c
->display_usage
||
151 (!strequal(argv
[1], "yes") &&
152 !strequal(argv
[1], "no"))) {
153 d_fprintf(stderr
, "%s\n", _("Usage:"));
154 d_fprintf(stderr
, _("net sam set %s <user> [yes|no]\n"),
159 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
160 &dom
, &name
, &sid
, &type
)) {
161 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
165 if (type
!= SID_NAME_USER
) {
166 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
167 sid_type_lookup(type
));
171 if ( !(sam_acct
= samu_new( NULL
)) ) {
172 d_fprintf(stderr
, _("Internal error\n"));
176 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
177 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
181 acct_flags
= pdb_get_acct_ctrl(sam_acct
);
183 if (strequal(argv
[1], "yes")) {
189 pdb_set_acct_ctrl(sam_acct
, acct_flags
, PDB_CHANGED
);
191 status
= pdb_update_sam_account(sam_acct
);
192 if (!NT_STATUS_IS_OK(status
)) {
193 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
194 argv
[0], nt_errstr(status
));
198 TALLOC_FREE(sam_acct
);
200 d_fprintf(stderr
, _("Updated flag %s for %s\\%s to %s\n"), field
, dom
,
205 static int net_sam_set_disabled(struct net_context
*c
, int argc
,
208 return net_sam_set_userflag(c
, argc
, argv
, "disabled", ACB_DISABLED
);
211 static int net_sam_set_pwnotreq(struct net_context
*c
, int argc
,
214 return net_sam_set_userflag(c
, argc
, argv
, "pwnotreq", ACB_PWNOTREQ
);
217 static int net_sam_set_autolock(struct net_context
*c
, int argc
,
220 return net_sam_set_userflag(c
, argc
, argv
, "autolock", ACB_AUTOLOCK
);
223 static int net_sam_set_pwnoexp(struct net_context
*c
, int argc
,
226 return net_sam_set_userflag(c
, argc
, argv
, "pwnoexp", ACB_PWNOEXP
);
230 * Set pass last change time, based on force pass change now
233 static int net_sam_set_pwdmustchangenow(struct net_context
*c
, int argc
,
236 struct samu
*sam_acct
= NULL
;
238 enum lsa_SidType type
;
239 const char *dom
, *name
;
242 if ((argc
!= 2) || c
->display_usage
||
243 (!strequal(argv
[1], "yes") &&
244 !strequal(argv
[1], "no"))) {
245 d_fprintf(stderr
, "%s\n%s",
247 _("net sam set pwdmustchangenow <user> [yes|no]\n"));
251 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
252 &dom
, &name
, &sid
, &type
)) {
253 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
257 if (type
!= SID_NAME_USER
) {
258 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
259 sid_type_lookup(type
));
263 if ( !(sam_acct
= samu_new( NULL
)) ) {
264 d_fprintf(stderr
, _("Internal error\n"));
268 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
269 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
273 if (strequal(argv
[1], "yes")) {
274 pdb_set_pass_last_set_time(sam_acct
, 0, PDB_CHANGED
);
276 pdb_set_pass_last_set_time(sam_acct
, time(NULL
), PDB_CHANGED
);
279 status
= pdb_update_sam_account(sam_acct
);
280 if (!NT_STATUS_IS_OK(status
)) {
281 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
282 argv
[0], nt_errstr(status
));
286 TALLOC_FREE(sam_acct
);
288 d_fprintf(stderr
, _("Updated 'user must change password at next logon' "
289 "for %s\\%s to %s\n"), dom
,
296 * Set a user's or a group's comment
299 static int net_sam_set_comment(struct net_context
*c
, int argc
,
304 enum lsa_SidType type
;
305 const char *dom
, *name
;
308 if (argc
!= 2 || c
->display_usage
) {
309 d_fprintf(stderr
, "%s\n%s",
311 _("net sam set comment <name> <comment>\n"));
315 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
316 &dom
, &name
, &sid
, &type
)) {
317 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
321 if (type
== SID_NAME_USER
) {
322 return net_sam_userset(c
, argc
, argv
, "comment",
326 if ((type
!= SID_NAME_DOM_GRP
) && (type
!= SID_NAME_ALIAS
) &&
327 (type
!= SID_NAME_WKN_GRP
)) {
328 d_fprintf(stderr
, _("%s is a %s, not a group\n"), argv
[0],
329 sid_type_lookup(type
));
333 if (!pdb_getgrsid(&map
, sid
)) {
334 d_fprintf(stderr
, _("Could not load group %s\n"), argv
[0]);
338 fstrcpy(map
.comment
, argv
[1]);
340 status
= pdb_update_group_mapping_entry(&map
);
342 if (!NT_STATUS_IS_OK(status
)) {
343 d_fprintf(stderr
, _("Updating group mapping entry failed with "
344 "%s\n"), nt_errstr(status
));
348 d_printf("Updated comment of group %s\\%s to %s\n", dom
, name
,
354 static int net_sam_set(struct net_context
*c
, int argc
, const char **argv
)
356 struct functable func
[] = {
361 N_("Change a user's home directory"),
362 N_("net sam set homedir\n"
363 " Change a user's home directory")
367 net_sam_set_profilepath
,
369 N_("Change a user's profile path"),
370 N_("net sam set profilepath\n"
371 " Change a user's profile path")
377 N_("Change a users or groups description"),
378 N_("net sam set comment\n"
379 " Change a users or groups description")
383 net_sam_set_fullname
,
385 N_("Change a user's full name"),
386 N_("net sam set fullname\n"
387 " Change a user's full name")
391 net_sam_set_logonscript
,
393 N_("Change a user's logon script"),
394 N_("net sam set logonscript\n"
395 " Change a user's logon script")
399 net_sam_set_homedrive
,
401 N_("Change a user's home drive"),
402 N_("net sam set homedrive\n"
403 " Change a user's home drive")
407 net_sam_set_workstations
,
409 N_("Change a user's allowed workstations"),
410 N_("net sam set workstations\n"
411 " Change a user's allowed workstations")
415 net_sam_set_disabled
,
417 N_("Disable/Enable a user"),
418 N_("net sam set disable\n"
419 " Disable/Enable a user")
423 net_sam_set_pwnotreq
,
425 N_("Disable/Enable the password not required flag"),
426 N_("net sam set pwnotreq\n"
427 " Disable/Enable the password not required flag")
431 net_sam_set_autolock
,
433 N_("Disable/Enable a user's lockout flag"),
434 N_("net sam set autolock\n"
435 " Disable/Enable a user's lockout flag")
441 N_("Disable/Enable whether a user's pw does not "
443 N_("net sam set pwnoexp\n"
444 " Disable/Enable whether a user's pw does not "
449 net_sam_set_pwdmustchangenow
,
451 N_("Force users password must change at next logon"),
452 N_("net sam set pwdmustchangenow\n"
453 " Force users password must change at next logon")
455 {NULL
, NULL
, 0, NULL
, NULL
}
458 return net_run_function(c
, argc
, argv
, "net sam set", func
);
462 * Manage account policies
465 static int net_sam_policy_set(struct net_context
*c
, int argc
, const char **argv
)
467 const char *account_policy
= NULL
;
469 uint32 old_value
= 0;
470 enum pdb_policy_type field
;
473 if (argc
!= 2 || c
->display_usage
) {
474 d_fprintf(stderr
, "%s\n%s",
476 _("net sam policy set \"<account policy>\" <value>\n"));
480 account_policy
= argv
[0];
481 field
= account_policy_name_to_typenum(account_policy
);
483 if (strequal(argv
[1], "forever") || strequal(argv
[1], "never")
484 || strequal(argv
[1], "off")) {
488 value
= strtoul(argv
[1], &endptr
, 10);
490 if ((endptr
== argv
[1]) || (endptr
[0] != '\0')) {
491 d_printf(_("Unable to set policy \"%s\"! Invalid value "
493 account_policy
, argv
[1]);
502 account_policy_names_list(talloc_tos(), &names
, &count
);
503 d_fprintf(stderr
, _("No account policy \"%s\"!\n\n"), argv
[0]);
504 d_fprintf(stderr
, _("Valid account policies are:\n"));
506 for (i
=0; i
<count
; i
++) {
507 d_fprintf(stderr
, "%s\n", names
[i
]);
515 if (!pdb_get_account_policy(field
, &old_value
)) {
516 d_fprintf(stderr
, _("Valid account policy, but unable to fetch "
519 d_printf(_("Account policy \"%s\" value was: %d\n"),
520 account_policy
, old_value
);
523 if (!pdb_set_account_policy(field
, value
)) {
524 d_fprintf(stderr
, _("Valid account policy, but unable to "
528 d_printf(_("Account policy \"%s\" value is now: %d\n"),
529 account_policy
, value
);
535 static int net_sam_policy_show(struct net_context
*c
, int argc
, const char **argv
)
537 const char *account_policy
= NULL
;
539 enum pdb_policy_type field
;
541 if (argc
!= 1 || c
->display_usage
) {
542 d_fprintf(stderr
, "%s\n%s",
544 _("net sam policy show \"<account policy>\"\n"));
548 account_policy
= argv
[0];
549 field
= account_policy_name_to_typenum(account_policy
);
555 account_policy_names_list(talloc_tos(), &names
, &count
);
556 d_fprintf(stderr
, _("No account policy by that name!\n"));
558 d_fprintf(stderr
, _("Valid account policies "
560 for (i
=0; i
<count
; i
++) {
561 d_fprintf(stderr
, "%s\n", names
[i
]);
568 if (!pdb_get_account_policy(field
, &old_value
)) {
569 fprintf(stderr
, _("Valid account policy, but unable to "
574 printf(_("Account policy \"%s\" description: %s\n"),
575 account_policy
, account_policy_get_desc(field
));
576 printf(_("Account policy \"%s\" value is: %d\n"), account_policy
,
581 static int net_sam_policy_list(struct net_context
*c
, int argc
, const char **argv
)
587 if (c
->display_usage
) {
589 "net sam policy list\n"
592 _("List account policies"));
596 account_policy_names_list(talloc_tos(), &names
, &count
);
598 d_fprintf(stderr
, _("Valid account policies "
600 for (i
= 0; i
< count
; i
++) {
601 d_fprintf(stderr
, "%s\n", names
[i
]);
608 static int net_sam_policy(struct net_context
*c
, int argc
, const char **argv
)
610 struct functable func
[] = {
615 N_("List account policies"),
616 N_("net sam policy list\n"
617 " List account policies")
623 N_("Show account policies"),
624 N_("net sam policy show\n"
625 " Show account policies")
631 N_("Change account policies"),
632 N_("net sam policy set\n"
633 " Change account policies")
635 {NULL
, NULL
, 0, NULL
, NULL
}
638 return net_run_function(c
, argc
, argv
, "net sam policy", func
);
641 static int net_sam_rights_list(struct net_context
*c
, int argc
,
644 enum sec_privilege privilege
;
646 if (argc
> 1 || c
->display_usage
) {
647 d_fprintf(stderr
, "%s\n%s",
649 _("net sam rights list [privilege name]\n"));
655 int num
= num_privileges_in_short_list();
657 for (i
=0; i
<num
; i
++) {
658 d_printf("%s\n", sec_privilege_name_from_index(i
));
663 privilege
= sec_privilege_id(argv
[0]);
665 if (privilege
!= SEC_PRIV_INVALID
) {
666 struct dom_sid
*sids
;
670 status
= privilege_enum_sids(privilege
, talloc_tos(),
672 if (!NT_STATUS_IS_OK(status
)) {
673 d_fprintf(stderr
, _("Could not list rights: %s\n"),
678 for (i
=0; i
<num_sids
; i
++) {
679 const char *dom
, *name
;
680 enum lsa_SidType type
;
682 if (lookup_sid(talloc_tos(), &sids
[i
], &dom
, &name
,
684 d_printf("%s\\%s\n", dom
, name
);
687 d_printf("%s\n", sid_string_tos(&sids
[i
]));
696 static int net_sam_rights_grant(struct net_context
*c
, int argc
,
700 enum lsa_SidType type
;
701 const char *dom
, *name
;
704 if (argc
< 2 || c
->display_usage
) {
705 d_fprintf(stderr
, "%s\n%s",
707 _("net sam rights grant <name> <rights> ...\n"));
711 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
712 &dom
, &name
, &sid
, &type
)) {
713 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
717 for (i
=1; i
< argc
; i
++) {
718 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
719 if (privilege
== SEC_PRIV_INVALID
) {
720 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
724 if (!grant_privilege_by_name(&sid
, argv
[i
])) {
725 d_fprintf(stderr
, _("Could not grant privilege\n"));
729 d_printf(_("Granted %s to %s\\%s\n"), argv
[i
], dom
, name
);
735 static int net_sam_rights_revoke(struct net_context
*c
, int argc
,
739 enum lsa_SidType type
;
740 const char *dom
, *name
;
743 if (argc
< 2 || c
->display_usage
) {
744 d_fprintf(stderr
, "%s\n%s",
746 _("net sam rights revoke <name> <rights>\n"));
750 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
751 &dom
, &name
, &sid
, &type
)) {
752 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
756 for (i
=1; i
< argc
; i
++) {
757 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
758 if (privilege
== SEC_PRIV_INVALID
) {
759 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
763 if (!revoke_privilege_by_name(&sid
, argv
[i
])) {
764 d_fprintf(stderr
, _("Could not revoke privilege\n"));
768 d_printf(_("Revoked %s from %s\\%s\n"), argv
[i
], dom
, name
);
774 static int net_sam_rights(struct net_context
*c
, int argc
, const char **argv
)
776 struct functable func
[] = {
781 N_("List possible user rights"),
782 N_("net sam rights list\n"
783 " List possible user rights")
787 net_sam_rights_grant
,
789 N_("Grant right(s)"),
790 N_("net sam rights grant\n"
795 net_sam_rights_revoke
,
797 N_("Revoke right(s)"),
798 N_("net sam rights revoke\n"
801 {NULL
, NULL
, 0, NULL
, NULL
}
803 return net_run_function(c
, argc
, argv
, "net sam rights", func
);
807 * Map a unix group to a domain group
810 static NTSTATUS
map_unix_group(const struct group
*grp
, GROUP_MAP
*pmap
)
814 const char *grpname
, *dom
, *name
;
817 if (pdb_getgrgid(&map
, grp
->gr_gid
)) {
818 return NT_STATUS_GROUP_EXISTS
;
821 map
.gid
= grp
->gr_gid
;
822 grpname
= grp
->gr_name
;
824 if (lookup_name(talloc_tos(), grpname
, LOOKUP_NAME_LOCAL
,
825 &dom
, &name
, NULL
, NULL
)) {
827 const char *tmp
= talloc_asprintf(
828 talloc_tos(), "Unix Group %s", grp
->gr_name
);
830 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
831 grpname
, dom
, name
, tmp
));
835 if (lookup_name(talloc_tos(), grpname
, LOOKUP_NAME_LOCAL
,
836 NULL
, NULL
, NULL
, NULL
)) {
837 DEBUG(3, ("\"%s\" exists, can't map it\n", grp
->gr_name
));
838 return NT_STATUS_GROUP_EXISTS
;
841 fstrcpy(map
.nt_name
, grpname
);
843 if (pdb_capabilities() & PDB_CAP_STORE_RIDS
) {
844 if (!pdb_new_rid(&rid
)) {
845 DEBUG(3, ("Could not get a new RID for %s\n",
847 return NT_STATUS_ACCESS_DENIED
;
850 rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
853 sid_compose(&map
.sid
, get_global_sam_sid(), rid
);
854 map
.sid_name_use
= SID_NAME_DOM_GRP
;
855 fstrcpy(map
.comment
, talloc_asprintf(talloc_tos(), "Unix Group %s",
858 status
= pdb_add_group_mapping_entry(&map
);
859 if (NT_STATUS_IS_OK(status
)) {
865 static int net_sam_mapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
871 if (argc
!= 1 || c
->display_usage
) {
872 d_fprintf(stderr
, "%s\n%s",
874 _("net sam mapunixgroup <name>\n"));
878 grp
= getgrnam(argv
[0]);
880 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
884 status
= map_unix_group(grp
, &map
);
886 if (!NT_STATUS_IS_OK(status
)) {
887 d_fprintf(stderr
, _("Mapping group %s failed with %s\n"),
888 argv
[0], nt_errstr(status
));
892 d_printf(_("Mapped unix group %s to SID %s\n"), argv
[0],
893 sid_string_tos(&map
.sid
));
899 * Remove a group mapping
902 static NTSTATUS
unmap_unix_group(const struct group
*grp
, GROUP_MAP
*pmap
)
906 struct dom_sid dom_sid
;
908 map
.gid
= grp
->gr_gid
;
909 grpname
= grp
->gr_name
;
911 if (!lookup_name(talloc_tos(), grpname
, LOOKUP_NAME_LOCAL
,
912 NULL
, NULL
, NULL
, NULL
)) {
913 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp
->gr_name
));
914 return NT_STATUS_NO_SUCH_GROUP
;
917 fstrcpy(map
.nt_name
, grpname
);
919 if (!pdb_gid_to_sid(map
.gid
, &dom_sid
)) {
920 return NT_STATUS_UNSUCCESSFUL
;
923 return pdb_delete_group_mapping_entry(dom_sid
);
926 static int net_sam_unmapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
932 if (argc
!= 1 || c
->display_usage
) {
933 d_fprintf(stderr
, "%s\n%s",
935 _("net sam unmapunixgroup <name>\n"));
939 grp
= getgrnam(argv
[0]);
941 d_fprintf(stderr
, _("Could not find mapping for group %s.\n"),
946 status
= unmap_unix_group(grp
, &map
);
948 if (!NT_STATUS_IS_OK(status
)) {
949 d_fprintf(stderr
, _("Unmapping group %s failed with %s.\n"),
950 argv
[0], nt_errstr(status
));
954 d_printf(_("Unmapped unix group %s.\n"), argv
[0]);
960 * Create a domain group
963 static int net_sam_createdomaingroup(struct net_context
*c
, int argc
,
969 if (argc
!= 1 || c
->display_usage
) {
970 d_fprintf(stderr
, "%s\n%s",
972 _("net sam createdomaingroup <name>\n"));
976 status
= pdb_create_dom_group(talloc_tos(), argv
[0], &rid
);
978 if (!NT_STATUS_IS_OK(status
)) {
979 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
980 argv
[0], nt_errstr(status
));
984 d_printf(_("Created domain group %s with RID %d\n"), argv
[0], rid
);
990 * Delete a domain group
993 static int net_sam_deletedomaingroup(struct net_context
*c
, int argc
,
998 enum lsa_SidType type
;
999 const char *dom
, *name
;
1002 if (argc
!= 1 || c
->display_usage
) {
1003 d_fprintf(stderr
, "%s\n%s",
1005 _("net sam deletelocalgroup <name>\n"));
1009 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1010 &dom
, &name
, &sid
, &type
)) {
1011 d_fprintf(stderr
, _("Could not find %s.\n"), argv
[0]);
1015 if (type
!= SID_NAME_DOM_GRP
) {
1016 d_fprintf(stderr
, _("%s is a %s, not a domain group.\n"),
1017 argv
[0], sid_type_lookup(type
));
1021 sid_peek_rid(&sid
, &rid
);
1023 status
= pdb_delete_dom_group(talloc_tos(), rid
);
1025 if (!NT_STATUS_IS_OK(status
)) {
1026 d_fprintf(stderr
,_("Deleting domain group %s failed with %s\n"),
1027 argv
[0], nt_errstr(status
));
1031 d_printf(_("Deleted domain group %s.\n"), argv
[0]);
1037 * Create a local group
1040 static int net_sam_createlocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1045 if (argc
!= 1 || c
->display_usage
) {
1046 d_fprintf(stderr
, "%s\n%s",
1048 _("net sam createlocalgroup <name>\n"));
1052 if (!winbind_ping()) {
1053 d_fprintf(stderr
, _("winbind seems not to run. "
1054 "createlocalgroup only works when winbind runs.\n"));
1058 status
= pdb_create_alias(argv
[0], &rid
);
1060 if (!NT_STATUS_IS_OK(status
)) {
1061 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1062 argv
[0], nt_errstr(status
));
1066 d_printf(_("Created local group %s with RID %d\n"), argv
[0], rid
);
1072 * Delete a local group
1075 static int net_sam_deletelocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1078 enum lsa_SidType type
;
1079 const char *dom
, *name
;
1082 if (argc
!= 1 || c
->display_usage
) {
1083 d_fprintf(stderr
, "%s\n%s",
1085 _("net sam deletelocalgroup <name>\n"));
1089 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1090 &dom
, &name
, &sid
, &type
)) {
1091 d_fprintf(stderr
,_("Could not find %s.\n"), argv
[0]);
1095 if (type
!= SID_NAME_ALIAS
) {
1096 d_fprintf(stderr
, _("%s is a %s, not a local group.\n"),argv
[0],
1097 sid_type_lookup(type
));
1101 status
= pdb_delete_alias(&sid
);
1103 if (!NT_STATUS_IS_OK(status
)) {
1104 d_fprintf(stderr
, _("Deleting local group %s failed with %s\n"),
1105 argv
[0], nt_errstr(status
));
1109 d_printf(_("Deleted local group %s.\n"), argv
[0]);
1115 * Create a builtin group
1118 static int net_sam_createbuiltingroup(struct net_context
*c
, int argc
, const char **argv
)
1122 enum lsa_SidType type
;
1126 if (argc
!= 1 || c
->display_usage
) {
1127 d_fprintf(stderr
, "%s\n%s",
1129 _("net sam createbuiltingroup <name>\n"));
1133 if (!winbind_ping()) {
1134 d_fprintf(stderr
, _("winbind seems not to run. "
1135 "createbuiltingroup only works when winbind "
1140 /* validate the name and get the group */
1142 fstrcpy( groupname
, "BUILTIN\\" );
1143 fstrcat( groupname
, argv
[0] );
1145 if ( !lookup_name(talloc_tos(), groupname
, LOOKUP_NAME_ALL
, NULL
,
1146 NULL
, &sid
, &type
)) {
1147 d_fprintf(stderr
, _("%s is not a BUILTIN group\n"), argv
[0]);
1151 if ( !sid_peek_rid( &sid
, &rid
) ) {
1152 d_fprintf(stderr
, _("Failed to get RID for %s\n"), argv
[0]);
1156 status
= pdb_create_builtin_alias( rid
);
1158 if (!NT_STATUS_IS_OK(status
)) {
1159 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1160 argv
[0], nt_errstr(status
));
1164 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv
[0], rid
);
1170 * Add a group member
1173 static int net_sam_addmem(struct net_context
*c
, int argc
, const char **argv
)
1175 const char *groupdomain
, *groupname
, *memberdomain
, *membername
;
1176 struct dom_sid group
, member
;
1177 enum lsa_SidType grouptype
, membertype
;
1180 if (argc
!= 2 || c
->display_usage
) {
1181 d_fprintf(stderr
, "%s\n%s",
1183 _("net sam addmem <group> <member>\n"));
1187 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1188 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1189 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1193 /* check to see if the member to be added is a name or a SID */
1195 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1196 &memberdomain
, &membername
, &member
, &membertype
))
1198 /* try it as a SID */
1200 if ( !string_to_sid( &member
, argv
[1] ) ) {
1201 d_fprintf(stderr
, _("Could not find member %s\n"),
1206 if ( !lookup_sid(talloc_tos(), &member
, &memberdomain
,
1207 &membername
, &membertype
) )
1209 d_fprintf(stderr
, _("Could not resolve SID %s\n"),
1215 if ((grouptype
== SID_NAME_ALIAS
) || (grouptype
== SID_NAME_WKN_GRP
)) {
1216 if ((membertype
!= SID_NAME_USER
) &&
1217 (membertype
!= SID_NAME_DOM_GRP
)) {
1218 d_fprintf(stderr
, _("%s is a local group, only users "
1219 "and domain groups can be added.\n"
1220 "%s is a %s\n"), argv
[0], argv
[1],
1221 sid_type_lookup(membertype
));
1224 status
= pdb_add_aliasmem(&group
, &member
);
1226 if (!NT_STATUS_IS_OK(status
)) {
1227 d_fprintf(stderr
, _("Adding local group member failed "
1228 "with %s\n"), nt_errstr(status
));
1231 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1232 uint32_t grouprid
, memberrid
;
1234 sid_peek_rid(&group
, &grouprid
);
1235 sid_peek_rid(&member
, &memberrid
);
1237 status
= pdb_add_groupmem(talloc_tos(), grouprid
, memberrid
);
1238 if (!NT_STATUS_IS_OK(status
)) {
1239 d_fprintf(stderr
, _("Adding domain group member failed "
1240 "with %s\n"), nt_errstr(status
));
1244 d_fprintf(stderr
, _("Can only add members to local groups so "
1245 "far, %s is a %s\n"), argv
[0],
1246 sid_type_lookup(grouptype
));
1250 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain
, membername
,
1251 groupdomain
, groupname
);
1257 * Delete a group member
1260 static int net_sam_delmem(struct net_context
*c
, int argc
, const char **argv
)
1262 const char *groupdomain
, *groupname
;
1263 const char *memberdomain
= NULL
;
1264 const char *membername
= NULL
;
1265 struct dom_sid group
, member
;
1266 enum lsa_SidType grouptype
;
1269 if (argc
!= 2 || c
->display_usage
) {
1270 d_fprintf(stderr
,"%s\n%s",
1272 _("net sam delmem <group> <member>\n"));
1276 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1277 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1278 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1282 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1283 &memberdomain
, &membername
, &member
, NULL
)) {
1284 if (!string_to_sid(&member
, argv
[1])) {
1285 d_fprintf(stderr
, _("Could not find member %s\n"),
1291 if ((grouptype
== SID_NAME_ALIAS
) ||
1292 (grouptype
== SID_NAME_WKN_GRP
)) {
1293 status
= pdb_del_aliasmem(&group
, &member
);
1295 if (!NT_STATUS_IS_OK(status
)) {
1296 d_fprintf(stderr
,_("Deleting local group member failed "
1297 "with %s\n"), nt_errstr(status
));
1300 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1301 uint32_t grouprid
, memberrid
;
1303 sid_peek_rid(&group
, &grouprid
);
1304 sid_peek_rid(&member
, &memberrid
);
1306 status
= pdb_del_groupmem(talloc_tos(), grouprid
, memberrid
);
1307 if (!NT_STATUS_IS_OK(status
)) {
1308 d_fprintf(stderr
, _("Deleting domain group member "
1309 "failed with %s\n"), nt_errstr(status
));
1313 d_fprintf(stderr
, _("Can only delete members from local groups "
1314 "so far, %s is a %s\n"), argv
[0],
1315 sid_type_lookup(grouptype
));
1319 if (membername
!= NULL
) {
1320 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1321 memberdomain
, membername
, groupdomain
, groupname
);
1323 d_printf(_("Deleted %s from %s\\%s\n"),
1324 sid_string_tos(&member
), groupdomain
, groupname
);
1331 * List group members
1334 static int net_sam_listmem(struct net_context
*c
, int argc
, const char **argv
)
1336 const char *groupdomain
, *groupname
;
1337 struct dom_sid group
;
1338 struct dom_sid
*members
= NULL
;
1339 size_t i
, num_members
= 0;
1340 enum lsa_SidType grouptype
;
1343 if (argc
!= 1 || c
->display_usage
) {
1344 d_fprintf(stderr
, "%s\n%s",
1346 _("net sam listmem <group>\n"));
1350 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1351 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1352 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1356 if ((grouptype
== SID_NAME_ALIAS
) ||
1357 (grouptype
== SID_NAME_WKN_GRP
)) {
1358 status
= pdb_enum_aliasmem(&group
, talloc_tos(), &members
,
1360 if (!NT_STATUS_IS_OK(status
)) {
1361 d_fprintf(stderr
, _("Listing group members failed with "
1362 "%s\n"), nt_errstr(status
));
1365 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1368 status
= pdb_enum_group_members(talloc_tos(), &group
,
1369 &rids
, &num_members
);
1370 if (!NT_STATUS_IS_OK(status
)) {
1371 d_fprintf(stderr
, _("Listing group members failed with "
1372 "%s\n"), nt_errstr(status
));
1376 members
= talloc_array(talloc_tos(), struct dom_sid
,
1378 if (members
== NULL
) {
1383 for (i
=0; i
<num_members
; i
++) {
1384 sid_compose(&members
[i
], get_global_sam_sid(),
1389 d_fprintf(stderr
,_("Can only list local group members so far.\n"
1390 "%s is a %s\n"), argv
[0], sid_type_lookup(grouptype
));
1394 d_printf(_("%s\\%s has %u members\n"), groupdomain
, groupname
,
1395 (unsigned int)num_members
);
1396 for (i
=0; i
<num_members
; i
++) {
1397 const char *dom
, *name
;
1398 if (lookup_sid(talloc_tos(), &members
[i
], &dom
, &name
, NULL
)) {
1399 d_printf(" %s\\%s\n", dom
, name
);
1401 d_printf(" %s\n", sid_string_tos(&members
[i
]));
1405 TALLOC_FREE(members
);
1413 static int net_sam_do_list(struct net_context
*c
, int argc
, const char **argv
,
1414 struct pdb_search
*search
, const char *what
)
1416 bool verbose
= (argc
== 1);
1418 if ((argc
> 1) || c
->display_usage
||
1419 ((argc
== 1) && !strequal(argv
[0], "verbose"))) {
1420 d_fprintf(stderr
, "%s\n", _("Usage:"));
1421 d_fprintf(stderr
, _("net sam list %s [verbose]\n"), what
);
1425 if (search
== NULL
) {
1426 d_fprintf(stderr
, _("Could not start search\n"));
1431 struct samr_displayentry entry
;
1432 if (!search
->next_entry(search
, &entry
)) {
1436 d_printf("%s:%d:%s\n",
1441 d_printf("%s\n", entry
.account_name
);
1445 TALLOC_FREE(search
);
1449 static int net_sam_list_users(struct net_context
*c
, int argc
,
1452 return net_sam_do_list(c
, argc
, argv
,
1453 pdb_search_users(talloc_tos(), ACB_NORMAL
),
1457 static int net_sam_list_groups(struct net_context
*c
, int argc
,
1460 return net_sam_do_list(c
, argc
, argv
, pdb_search_groups(talloc_tos()),
1464 static int net_sam_list_localgroups(struct net_context
*c
, int argc
,
1467 return net_sam_do_list(c
, argc
, argv
,
1468 pdb_search_aliases(talloc_tos(),
1469 get_global_sam_sid()),
1473 static int net_sam_list_builtin(struct net_context
*c
, int argc
,
1476 return net_sam_do_list(c
, argc
, argv
,
1477 pdb_search_aliases(talloc_tos(),
1478 &global_sid_Builtin
),
1482 static int net_sam_list_workstations(struct net_context
*c
, int argc
,
1485 return net_sam_do_list(c
, argc
, argv
,
1486 pdb_search_users(talloc_tos(), ACB_WSTRUST
),
1494 static int net_sam_list(struct net_context
*c
, int argc
, const char **argv
)
1496 struct functable func
[] = {
1500 NET_TRANSPORT_LOCAL
,
1501 N_("List SAM users"),
1502 N_("net sam list users\n"
1507 net_sam_list_groups
,
1508 NET_TRANSPORT_LOCAL
,
1509 N_("List SAM groups"),
1510 N_("net sam list groups\n"
1515 net_sam_list_localgroups
,
1516 NET_TRANSPORT_LOCAL
,
1517 N_("List SAM local groups"),
1518 N_("net sam list localgroups\n"
1519 " List SAM local groups")
1523 net_sam_list_builtin
,
1524 NET_TRANSPORT_LOCAL
,
1525 N_("List builtin groups"),
1526 N_("net sam list builtin\n"
1527 " List builtin groups")
1531 net_sam_list_workstations
,
1532 NET_TRANSPORT_LOCAL
,
1533 N_("List domain member workstations"),
1534 N_("net sam list workstations\n"
1535 " List domain member workstations")
1537 {NULL
, NULL
, 0, NULL
, NULL
}
1540 return net_run_function(c
, argc
, argv
, "net sam list", func
);
1544 * Show details of SAM entries
1547 static int net_sam_show(struct net_context
*c
, int argc
, const char **argv
)
1550 enum lsa_SidType type
;
1551 const char *dom
, *name
;
1553 if (argc
!= 1 || c
->display_usage
) {
1554 d_fprintf(stderr
, "%s\n%s",
1556 _("net sam show <name>\n"));
1560 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1561 &dom
, &name
, &sid
, &type
)) {
1562 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
1566 d_printf(_("%s\\%s is a %s with SID %s\n"), dom
, name
,
1567 sid_type_lookup(type
), sid_string_tos(&sid
));
1575 * Init an LDAP tree with default users and Groups
1576 * if ldapsam:editposix is enabled
1579 static int net_sam_provision(struct net_context
*c
, int argc
, const char **argv
)
1583 char *ldap_uri
= NULL
;
1585 struct smbldap_state
*ls
;
1587 struct dom_sid gsid
;
1588 gid_t domusers_gid
= -1;
1589 gid_t domadmins_gid
= -1;
1590 struct samu
*samuser
;
1592 bool is_ipa
= false;
1594 if (c
->display_usage
) {
1596 "net sam provision\n"
1599 _("Init an LDAP tree with default users/groups"));
1603 tc
= talloc_new(NULL
);
1605 d_fprintf(stderr
, _("Out of Memory!\n"));
1609 if ((ldap_bk
= talloc_strdup(tc
, lp_passdb_backend())) == NULL
) {
1610 d_fprintf(stderr
, _("talloc failed\n"));
1614 p
= strchr(ldap_bk
, ':');
1617 ldap_uri
= talloc_strdup(tc
, p
+1);
1618 trim_char(ldap_uri
, ' ', ' ');
1621 trim_char(ldap_bk
, ' ', ' ');
1623 if (strcmp(ldap_bk
, "IPA_ldapsam") == 0 ) {
1627 if (strcmp(ldap_bk
, "ldapsam") != 0 && !is_ipa
) {
1629 _("Provisioning works only with ldapsam backend\n"));
1633 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1634 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1636 d_fprintf(stderr
, _("Provisioning works only if ldapsam:trusted"
1637 " and ldapsam:editposix are enabled.\n"));
1641 if (!is_ipa
&& !winbind_ping()) {
1642 d_fprintf(stderr
, _("winbind seems not to run. Provisioning "
1643 "LDAP only works when winbind runs.\n"));
1647 if (!NT_STATUS_IS_OK(smbldap_init(tc
, NULL
, ldap_uri
, &ls
))) {
1648 d_fprintf(stderr
, _("Unable to connect to the LDAP server.\n"));
1652 d_printf(_("Checking for Domain Users group.\n"));
1654 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_USERS
);
1656 if (!pdb_getgrsid(&gmap
, gsid
)) {
1657 LDAPMod
**mods
= NULL
;
1665 d_printf(_("Adding the Domain Users group.\n"));
1667 /* lets allocate a new groupid for this group */
1671 if (!winbind_allocate_gid(&domusers_gid
)) {
1672 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1673 "create Domain Users group!\n"));
1678 uname
= talloc_strdup(tc
, "domusers");
1679 wname
= talloc_strdup(tc
, "Domain Users");
1680 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1681 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domusers_gid
);
1682 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1684 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1685 d_fprintf(stderr
, "Out of Memory!\n");
1689 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1690 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1692 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1693 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1694 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1696 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1697 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1698 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1699 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1700 sid_string_talloc(tc
, &gsid
));
1701 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1703 talloc_autofree_ldapmod(tc
, mods
);
1705 rc
= smbldap_add(ls
, dn
, mods
);
1707 if (rc
!= LDAP_SUCCESS
) {
1708 d_fprintf(stderr
, _("Failed to add Domain Users group "
1709 "to ldap directory\n"));
1713 if (!pdb_getgrsid(&gmap
, gsid
)) {
1714 d_fprintf(stderr
, _("Failed to read just "
1715 "created domain group.\n"));
1718 domusers_gid
= gmap
.gid
;
1722 domusers_gid
= gmap
.gid
;
1723 d_printf(_("found!\n"));
1728 d_printf(_("Checking for Domain Admins group.\n"));
1730 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_ADMINS
);
1732 if (!pdb_getgrsid(&gmap
, gsid
)) {
1733 LDAPMod
**mods
= NULL
;
1741 d_printf(_("Adding the Domain Admins group.\n"));
1743 /* lets allocate a new groupid for this group */
1745 domadmins_gid
= 999;
1747 if (!winbind_allocate_gid(&domadmins_gid
)) {
1748 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1749 "create Domain Admins group!\n"));
1754 uname
= talloc_strdup(tc
, "domadmins");
1755 wname
= talloc_strdup(tc
, "Domain Admins");
1756 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1757 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1758 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1760 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1761 d_fprintf(stderr
, _("Out of Memory!\n"));
1765 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1766 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1768 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1769 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1770 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1772 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1773 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1774 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1775 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1776 sid_string_talloc(tc
, &gsid
));
1777 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1779 talloc_autofree_ldapmod(tc
, mods
);
1781 rc
= smbldap_add(ls
, dn
, mods
);
1783 if (rc
!= LDAP_SUCCESS
) {
1784 d_fprintf(stderr
, _("Failed to add Domain Admins group "
1785 "to ldap directory\n"));
1789 if (!pdb_getgrsid(&gmap
, gsid
)) {
1790 d_fprintf(stderr
, _("Failed to read just "
1791 "created domain group.\n"));
1794 domadmins_gid
= gmap
.gid
;
1798 domadmins_gid
= gmap
.gid
;
1799 d_printf(_("found!\n"));
1804 d_printf(_("Check for Administrator account.\n"));
1806 samuser
= samu_new(tc
);
1808 d_fprintf(stderr
, _("Out of Memory!\n"));
1812 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1813 LDAPMod
**mods
= NULL
;
1825 d_printf(_("Adding the Administrator user.\n"));
1827 if (domadmins_gid
== -1) {
1829 _("Can't create Administrator user, Domain "
1830 "Admins group not available!\n"));
1837 if (!winbind_allocate_uid(&uid
)) {
1839 _("Unable to allocate a new uid to create "
1840 "the Administrator user!\n"));
1845 name
= talloc_strdup(tc
, "Administrator");
1846 dn
= talloc_asprintf(tc
, "uid=Administrator,%s", lp_ldap_user_suffix());
1847 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)uid
);
1848 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1849 dir
= talloc_sub_specified(tc
, lp_template_homedir(),
1851 get_global_sam_name(),
1852 uid
, domadmins_gid
);
1853 shell
= talloc_sub_specified(tc
, lp_template_shell(),
1855 get_global_sam_name(),
1856 uid
, domadmins_gid
);
1858 if (!name
|| !dn
|| !uidstr
|| !gidstr
|| !dir
|| !shell
) {
1859 d_fprintf(stderr
, _("Out of Memory!\n"));
1863 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR
);
1865 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1866 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1867 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1869 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
1870 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
1871 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
1872 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
1873 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
1874 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
1875 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", name
);
1876 princ
= talloc_asprintf(tc
, "%s@%s", name
, lp_realm());
1878 d_fprintf(stderr
, _("Out of Memory!\n"));
1881 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "krbPrincipalName", princ
);
1883 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", name
);
1884 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
1885 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
1886 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1887 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1888 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", dir
);
1889 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
1890 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
1891 sid_string_talloc(tc
, &sid
));
1892 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
1893 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
1894 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1896 talloc_autofree_ldapmod(tc
, mods
);
1898 rc
= smbldap_add(ls
, dn
, mods
);
1900 if (rc
!= LDAP_SUCCESS
) {
1901 d_fprintf(stderr
, _("Failed to add Administrator user "
1902 "to ldap directory\n"));
1906 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1907 d_fprintf(stderr
, _("Failed to read just "
1908 "created user.\n"));
1913 d_printf(_("found!\n"));
1916 d_printf(_("Checking for Guest user.\n"));
1918 samuser
= samu_new(tc
);
1920 d_fprintf(stderr
, _("Out of Memory!\n"));
1924 if (!pdb_getsampwnam(samuser
, lp_guestaccount())) {
1925 LDAPMod
**mods
= NULL
;
1932 d_printf(_("Adding the Guest user.\n"));
1934 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_GUEST
);
1936 pwd
= Get_Pwnam_alloc(tc
, lp_guestaccount());
1939 if (domusers_gid
== -1) {
1941 _("Can't create Guest user, Domain "
1942 "Users group not available!\n"));
1945 if ((pwd
= talloc(tc
, struct passwd
)) == NULL
) {
1946 d_fprintf(stderr
, _("talloc failed\n"));
1949 pwd
->pw_name
= talloc_strdup(pwd
, lp_guestaccount());
1954 if (!winbind_allocate_uid(&(pwd
->pw_uid
))) {
1956 _("Unable to allocate a new uid to "
1957 "create the Guest user!\n"));
1961 pwd
->pw_gid
= domusers_gid
;
1962 pwd
->pw_dir
= talloc_strdup(tc
, "/");
1963 pwd
->pw_shell
= talloc_strdup(tc
, "/bin/false");
1964 if (!pwd
->pw_dir
|| !pwd
->pw_shell
) {
1965 d_fprintf(stderr
, _("Out of Memory!\n"));
1970 dn
= talloc_asprintf(tc
, "uid=%s,%s", pwd
->pw_name
, lp_ldap_user_suffix ());
1971 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_uid
);
1972 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
1973 if (!dn
|| !uidstr
|| !gidstr
) {
1974 d_fprintf(stderr
, _("Out of Memory!\n"));
1978 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1979 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1980 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1982 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
1983 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
1984 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
1985 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
1986 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
1987 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
1988 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", pwd
->pw_name
);
1990 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", pwd
->pw_name
);
1991 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", pwd
->pw_name
);
1992 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", pwd
->pw_name
);
1993 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1994 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1995 if ((pwd
->pw_dir
!= NULL
) && (pwd
->pw_dir
[0] != '\0')) {
1996 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", pwd
->pw_dir
);
1998 if ((pwd
->pw_shell
!= NULL
) && (pwd
->pw_shell
[0] != '\0')) {
1999 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", pwd
->pw_shell
);
2001 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
2002 sid_string_talloc(tc
, &sid
));
2003 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
2004 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
2005 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
2007 talloc_autofree_ldapmod(tc
, mods
);
2009 rc
= smbldap_add(ls
, dn
, mods
);
2011 if (rc
!= LDAP_SUCCESS
) {
2012 d_fprintf(stderr
, _("Failed to add Guest user to "
2013 "ldap directory\n"));
2017 if (!pdb_getsampwnam(samuser
, lp_guestaccount())) {
2018 d_fprintf(stderr
, _("Failed to read just "
2019 "created user.\n"));
2024 d_printf(_("found!\n"));
2027 d_printf(_("Checking Guest's group.\n"));
2029 pwd
= Get_Pwnam_alloc(tc
, lp_guestaccount());
2032 _("Failed to find just created Guest account!\n"
2033 " Is nss properly configured?!\n"));
2037 if (pwd
->pw_gid
== domusers_gid
) {
2038 d_printf(_("found!\n"));
2042 if (!pdb_getgrgid(&gmap
, pwd
->pw_gid
)) {
2043 LDAPMod
**mods
= NULL
;
2051 d_printf(_("Adding the Domain Guests group.\n"));
2053 uname
= talloc_strdup(tc
, "domguests");
2054 wname
= talloc_strdup(tc
, "Domain Guests");
2055 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
2056 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2057 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
2059 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
2060 d_fprintf(stderr
, _("Out of Memory!\n"));
2064 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_GUESTS
);
2066 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
2067 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
2069 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
2070 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
2071 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
2073 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
2074 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
2075 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2076 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
2077 sid_string_talloc(tc
, &gsid
));
2078 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
2080 talloc_autofree_ldapmod(tc
, mods
);
2082 rc
= smbldap_add(ls
, dn
, mods
);
2084 if (rc
!= LDAP_SUCCESS
) {
2086 _("Failed to add Domain Guests group to ldap "
2090 d_printf(_("found!\n"));
2105 /***********************************************************
2106 migrated functionality from smbgroupedit
2107 **********************************************************/
2108 int net_sam(struct net_context
*c
, int argc
, const char **argv
)
2110 struct functable func
[] = {
2112 "createbuiltingroup",
2113 net_sam_createbuiltingroup
,
2114 NET_TRANSPORT_LOCAL
,
2115 N_("Create a new BUILTIN group"),
2116 N_("net sam createbuiltingroup\n"
2117 " Create a new BUILTIN group")
2121 net_sam_createlocalgroup
,
2122 NET_TRANSPORT_LOCAL
,
2123 N_("Create a new local group"),
2124 N_("net sam createlocalgroup\n"
2125 " Create a new local group")
2128 "createdomaingroup",
2129 net_sam_createdomaingroup
,
2130 NET_TRANSPORT_LOCAL
,
2131 N_("Create a new group"),
2132 N_("net sam createdomaingroup\n"
2133 " Create a new group")
2137 net_sam_deletelocalgroup
,
2138 NET_TRANSPORT_LOCAL
,
2139 N_("Delete an existing local group"),
2140 N_("net sam deletelocalgroup\n"
2141 " Delete an existing local group")
2144 "deletedomaingroup",
2145 net_sam_deletedomaingroup
,
2146 NET_TRANSPORT_LOCAL
,
2147 N_("Delete a domain group"),
2148 N_("net sam deletedomaingroup\n"
2153 net_sam_mapunixgroup
,
2154 NET_TRANSPORT_LOCAL
,
2155 N_("Map a unix group to a domain group"),
2156 N_("net sam mapunixgroup\n"
2157 " Map a unix group to a domain group")
2161 net_sam_unmapunixgroup
,
2162 NET_TRANSPORT_LOCAL
,
2163 N_("Remove a group mapping of an unix group to a "
2165 N_("net sam unmapunixgroup\n"
2166 " Remove a group mapping of an unix group to a "
2172 NET_TRANSPORT_LOCAL
,
2173 N_("Add a member to a group"),
2174 N_("net sam addmem\n"
2175 " Add a member to a group")
2180 NET_TRANSPORT_LOCAL
,
2181 N_("Delete a member from a group"),
2182 N_("net sam delmem\n"
2183 " Delete a member from a group")
2188 NET_TRANSPORT_LOCAL
,
2189 N_("List group members"),
2190 N_("net sam listmem\n"
2191 " List group members")
2196 NET_TRANSPORT_LOCAL
,
2197 N_("List users, groups and local groups"),
2199 " List users, groups and local groups")
2204 NET_TRANSPORT_LOCAL
,
2205 N_("Show details of a SAM entry"),
2207 " Show details of a SAM entry")
2212 NET_TRANSPORT_LOCAL
,
2213 N_("Set details of a SAM account"),
2215 " Set details of a SAM account")
2220 NET_TRANSPORT_LOCAL
,
2221 N_("Set account policies"),
2222 N_("net sam policy\n"
2223 " Set account policies")
2228 NET_TRANSPORT_LOCAL
,
2229 N_("Manipulate user privileges"),
2230 N_("net sam rights\n"
2231 " Manipulate user privileges")
2237 NET_TRANSPORT_LOCAL
,
2238 N_("Provision a clean user database"),
2239 N_("net sam privison\n"
2240 " Provision a clear user database")
2243 {NULL
, NULL
, 0, NULL
, NULL
}
2246 if (getuid() != 0) {
2247 d_fprintf(stderr
, _("You are not root, most things won't "
2251 return net_run_function(c
, argc
, argv
, "net sam", func
);