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 "passdb/pdb_ldap_util.h"
30 #include "passdb/pdb_ldap_schema.h"
31 #include "lib/privileges.h"
39 static int net_sam_userset(struct net_context
*c
, int argc
, const char **argv
,
41 bool (*fn
)(struct samu
*, const char *,
42 enum pdb_value_state
))
44 struct samu
*sam_acct
= NULL
;
46 enum lsa_SidType type
;
47 const char *dom
, *name
;
50 if (argc
!= 2 || c
->display_usage
) {
51 d_fprintf(stderr
, "%s\n", _("Usage:"));
52 d_fprintf(stderr
, _("net sam set %s <user> <value>\n"),
57 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
58 &dom
, &name
, &sid
, &type
)) {
59 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
63 if (type
!= SID_NAME_USER
) {
64 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
65 sid_type_lookup(type
));
69 if ( !(sam_acct
= samu_new( NULL
)) ) {
70 d_fprintf(stderr
, _("Internal error\n"));
74 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
75 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
79 if (!fn(sam_acct
, argv
[1], PDB_CHANGED
)) {
80 d_fprintf(stderr
, _("Internal error\n"));
84 status
= pdb_update_sam_account(sam_acct
);
85 if (!NT_STATUS_IS_OK(status
)) {
86 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
87 argv
[0], nt_errstr(status
));
91 TALLOC_FREE(sam_acct
);
93 d_printf(_("Updated %s for %s\\%s to %s\n"), field
, dom
, name
, argv
[1]);
97 static int net_sam_set_fullname(struct net_context
*c
, int argc
,
100 return net_sam_userset(c
, argc
, argv
, "fullname",
104 static int net_sam_set_logonscript(struct net_context
*c
, int argc
,
107 return net_sam_userset(c
, argc
, argv
, "logonscript",
108 pdb_set_logon_script
);
111 static int net_sam_set_profilepath(struct net_context
*c
, int argc
,
114 return net_sam_userset(c
, argc
, argv
, "profilepath",
115 pdb_set_profile_path
);
118 static int net_sam_set_homedrive(struct net_context
*c
, int argc
,
121 return net_sam_userset(c
, argc
, argv
, "homedrive",
125 static int net_sam_set_homedir(struct net_context
*c
, int argc
,
128 return net_sam_userset(c
, argc
, argv
, "homedir",
132 static int net_sam_set_workstations(struct net_context
*c
, int argc
,
135 return net_sam_userset(c
, argc
, argv
, "workstations",
136 pdb_set_workstations
);
143 static int net_sam_set_userflag(struct net_context
*c
, int argc
,
144 const char **argv
, const char *field
,
147 struct samu
*sam_acct
= NULL
;
149 enum lsa_SidType type
;
150 const char *dom
, *name
;
154 if ((argc
!= 2) || c
->display_usage
||
155 (!strequal(argv
[1], "yes") &&
156 !strequal(argv
[1], "no"))) {
157 d_fprintf(stderr
, "%s\n", _("Usage:"));
158 d_fprintf(stderr
, _("net sam set %s <user> [yes|no]\n"),
163 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
164 &dom
, &name
, &sid
, &type
)) {
165 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
169 if (type
!= SID_NAME_USER
) {
170 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
171 sid_type_lookup(type
));
175 if ( !(sam_acct
= samu_new( NULL
)) ) {
176 d_fprintf(stderr
, _("Internal error\n"));
180 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
181 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
185 acct_flags
= pdb_get_acct_ctrl(sam_acct
);
187 if (strequal(argv
[1], "yes")) {
193 pdb_set_acct_ctrl(sam_acct
, acct_flags
, PDB_CHANGED
);
195 status
= pdb_update_sam_account(sam_acct
);
196 if (!NT_STATUS_IS_OK(status
)) {
197 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
198 argv
[0], nt_errstr(status
));
202 TALLOC_FREE(sam_acct
);
204 d_fprintf(stderr
, _("Updated flag %s for %s\\%s to %s\n"), field
, dom
,
209 static int net_sam_set_disabled(struct net_context
*c
, int argc
,
212 return net_sam_set_userflag(c
, argc
, argv
, "disabled", ACB_DISABLED
);
215 static int net_sam_set_pwnotreq(struct net_context
*c
, int argc
,
218 return net_sam_set_userflag(c
, argc
, argv
, "pwnotreq", ACB_PWNOTREQ
);
221 static int net_sam_set_autolock(struct net_context
*c
, int argc
,
224 return net_sam_set_userflag(c
, argc
, argv
, "autolock", ACB_AUTOLOCK
);
227 static int net_sam_set_pwnoexp(struct net_context
*c
, int argc
,
230 return net_sam_set_userflag(c
, argc
, argv
, "pwnoexp", ACB_PWNOEXP
);
234 * Set pass last change time, based on force pass change now
237 static int net_sam_set_pwdmustchangenow(struct net_context
*c
, int argc
,
240 struct samu
*sam_acct
= NULL
;
242 enum lsa_SidType type
;
243 const char *dom
, *name
;
246 if ((argc
!= 2) || c
->display_usage
||
247 (!strequal(argv
[1], "yes") &&
248 !strequal(argv
[1], "no"))) {
249 d_fprintf(stderr
, "%s\n%s",
251 _("net sam set pwdmustchangenow <user> [yes|no]\n"));
255 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
256 &dom
, &name
, &sid
, &type
)) {
257 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
261 if (type
!= SID_NAME_USER
) {
262 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
263 sid_type_lookup(type
));
267 if ( !(sam_acct
= samu_new( NULL
)) ) {
268 d_fprintf(stderr
, _("Internal error\n"));
272 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
273 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
277 if (strequal(argv
[1], "yes")) {
278 pdb_set_pass_last_set_time(sam_acct
, 0, PDB_CHANGED
);
280 pdb_set_pass_last_set_time(sam_acct
, time(NULL
), PDB_CHANGED
);
283 status
= pdb_update_sam_account(sam_acct
);
284 if (!NT_STATUS_IS_OK(status
)) {
285 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
286 argv
[0], nt_errstr(status
));
290 TALLOC_FREE(sam_acct
);
292 d_fprintf(stderr
, _("Updated 'user must change password at next logon' "
293 "for %s\\%s to %s\n"), dom
,
300 * Set a user's or a group's comment
303 static int net_sam_set_comment(struct net_context
*c
, int argc
,
308 enum lsa_SidType type
;
309 const char *dom
, *name
;
312 if (argc
!= 2 || c
->display_usage
) {
313 d_fprintf(stderr
, "%s\n%s",
315 _("net sam set comment <name> <comment>\n"));
319 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
320 &dom
, &name
, &sid
, &type
)) {
321 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
325 if (type
== SID_NAME_USER
) {
326 return net_sam_userset(c
, argc
, argv
, "comment",
330 if ((type
!= SID_NAME_DOM_GRP
) && (type
!= SID_NAME_ALIAS
) &&
331 (type
!= SID_NAME_WKN_GRP
)) {
332 d_fprintf(stderr
, _("%s is a %s, not a group\n"), argv
[0],
333 sid_type_lookup(type
));
337 map
= talloc_zero(talloc_tos(), GROUP_MAP
);
339 d_fprintf(stderr
, _("Out of memory!\n"));
343 if (!pdb_getgrsid(map
, sid
)) {
344 d_fprintf(stderr
, _("Could not load group %s\n"), argv
[0]);
348 map
->comment
= talloc_strdup(map
, argv
[1]);
350 d_fprintf(stderr
, _("Out of memory!\n"));
354 status
= pdb_update_group_mapping_entry(map
);
356 if (!NT_STATUS_IS_OK(status
)) {
357 d_fprintf(stderr
, _("Updating group mapping entry failed with "
358 "%s\n"), nt_errstr(status
));
362 d_printf("Updated comment of group %s\\%s to %s\n", dom
, name
,
369 static int net_sam_set(struct net_context
*c
, int argc
, const char **argv
)
371 struct functable func
[] = {
376 N_("Change a user's home directory"),
377 N_("net sam set homedir\n"
378 " Change a user's home directory")
382 net_sam_set_profilepath
,
384 N_("Change a user's profile path"),
385 N_("net sam set profilepath\n"
386 " Change a user's profile path")
392 N_("Change a users or groups description"),
393 N_("net sam set comment\n"
394 " Change a users or groups description")
398 net_sam_set_fullname
,
400 N_("Change a user's full name"),
401 N_("net sam set fullname\n"
402 " Change a user's full name")
406 net_sam_set_logonscript
,
408 N_("Change a user's logon script"),
409 N_("net sam set logonscript\n"
410 " Change a user's logon script")
414 net_sam_set_homedrive
,
416 N_("Change a user's home drive"),
417 N_("net sam set homedrive\n"
418 " Change a user's home drive")
422 net_sam_set_workstations
,
424 N_("Change a user's allowed workstations"),
425 N_("net sam set workstations\n"
426 " Change a user's allowed workstations")
430 net_sam_set_disabled
,
432 N_("Disable/Enable a user"),
433 N_("net sam set disable\n"
434 " Disable/Enable a user")
438 net_sam_set_pwnotreq
,
440 N_("Disable/Enable the password not required flag"),
441 N_("net sam set pwnotreq\n"
442 " Disable/Enable the password not required flag")
446 net_sam_set_autolock
,
448 N_("Disable/Enable a user's lockout flag"),
449 N_("net sam set autolock\n"
450 " Disable/Enable a user's lockout flag")
456 N_("Disable/Enable whether a user's pw does not "
458 N_("net sam set pwnoexp\n"
459 " Disable/Enable whether a user's pw does not "
464 net_sam_set_pwdmustchangenow
,
466 N_("Force users password must change at next logon"),
467 N_("net sam set pwdmustchangenow\n"
468 " Force users password must change at next logon")
470 {NULL
, NULL
, 0, NULL
, NULL
}
473 return net_run_function(c
, argc
, argv
, "net sam set", func
);
477 * Manage account policies
480 static int net_sam_policy_set(struct net_context
*c
, int argc
, const char **argv
)
482 const char *account_policy
= NULL
;
484 uint32_t old_value
= 0;
485 enum pdb_policy_type field
;
489 if (argc
!= 2 || c
->display_usage
) {
490 d_fprintf(stderr
, "%s\n%s",
492 _("net sam policy set \"<account policy>\" <value>\n"));
496 account_policy
= argv
[0];
497 field
= account_policy_name_to_typenum(account_policy
);
499 if (strequal(argv
[1], "forever") || strequal(argv
[1], "never")
500 || strequal(argv
[1], "off")) {
504 value
= strtoul_err(argv
[1], &endptr
, 10, &err
);
506 if (err
!= 0 || *endptr
!= '\0') {
507 d_printf(_("Unable to set policy \"%s\"! Invalid value "
509 account_policy
, argv
[1]);
518 account_policy_names_list(talloc_tos(), &names
, &count
);
519 d_fprintf(stderr
, _("No account policy \"%s\"!\n\n"), argv
[0]);
520 d_fprintf(stderr
, _("Valid account policies are:\n"));
522 for (i
=0; i
<count
; i
++) {
523 d_fprintf(stderr
, "%s\n", names
[i
]);
531 if (!pdb_get_account_policy(field
, &old_value
)) {
532 d_fprintf(stderr
, _("Valid account policy, but unable to fetch "
535 d_printf(_("Account policy \"%s\" value was: %d\n"),
536 account_policy
, old_value
);
539 if (!pdb_set_account_policy(field
, value
)) {
540 d_fprintf(stderr
, _("Valid account policy, but unable to "
544 d_printf(_("Account policy \"%s\" value is now: %d\n"),
545 account_policy
, value
);
551 static int net_sam_policy_show(struct net_context
*c
, int argc
, const char **argv
)
553 const char *account_policy
= NULL
;
555 enum pdb_policy_type field
;
557 if (argc
!= 1 || c
->display_usage
) {
558 d_fprintf(stderr
, "%s\n%s",
560 _("net sam policy show \"<account policy>\"\n"));
564 account_policy
= argv
[0];
565 field
= account_policy_name_to_typenum(account_policy
);
571 account_policy_names_list(talloc_tos(), &names
, &count
);
572 d_fprintf(stderr
, _("No account policy by that name!\n"));
574 d_fprintf(stderr
, _("Valid account policies "
576 for (i
=0; i
<count
; i
++) {
577 d_fprintf(stderr
, "%s\n", names
[i
]);
584 if (!pdb_get_account_policy(field
, &old_value
)) {
585 fprintf(stderr
, _("Valid account policy, but unable to "
590 printf(_("Account policy \"%s\" description: %s\n"),
591 account_policy
, account_policy_get_desc(field
));
592 printf(_("Account policy \"%s\" value is: %d\n"), account_policy
,
597 static int net_sam_policy_list(struct net_context
*c
, int argc
, const char **argv
)
603 if (c
->display_usage
) {
605 "net sam policy list\n"
608 _("List account policies"));
612 account_policy_names_list(talloc_tos(), &names
, &count
);
614 d_fprintf(stderr
, _("Valid account policies "
616 for (i
= 0; i
< count
; i
++) {
617 d_fprintf(stderr
, "%s\n", names
[i
]);
624 static int net_sam_policy(struct net_context
*c
, int argc
, const char **argv
)
626 struct functable func
[] = {
631 N_("List account policies"),
632 N_("net sam policy list\n"
633 " List account policies")
639 N_("Show account policies"),
640 N_("net sam policy show\n"
641 " Show account policies")
647 N_("Change account policies"),
648 N_("net sam policy set\n"
649 " Change account policies")
651 {NULL
, NULL
, 0, NULL
, NULL
}
654 return net_run_function(c
, argc
, argv
, "net sam policy", func
);
657 static int net_sam_rights_list(struct net_context
*c
, int argc
,
660 enum sec_privilege privilege
;
662 if (argc
> 1 || c
->display_usage
) {
663 d_fprintf(stderr
, "%s\n%s",
665 _("net sam rights list [privilege name]\n"));
671 int num
= num_privileges_in_short_list();
673 for (i
=0; i
<num
; i
++) {
674 d_printf("%s\n", sec_privilege_name_from_index(i
));
679 privilege
= sec_privilege_id(argv
[0]);
681 if (privilege
!= SEC_PRIV_INVALID
) {
682 struct dom_sid
*sids
;
686 status
= privilege_enum_sids(privilege
, talloc_tos(),
688 if (!NT_STATUS_IS_OK(status
)) {
689 d_fprintf(stderr
, _("Could not list rights: %s\n"),
694 for (i
=0; i
<num_sids
; i
++) {
695 const char *dom
, *name
;
696 enum lsa_SidType type
;
697 struct dom_sid_buf buf
;
699 if (lookup_sid(talloc_tos(), &sids
[i
], &dom
, &name
,
701 d_printf("%s\\%s\n", dom
, name
);
705 dom_sid_str_buf(&sids
[i
], &buf
));
714 static int net_sam_rights_grant(struct net_context
*c
, int argc
,
718 enum lsa_SidType type
;
719 const char *dom
, *name
;
722 if (argc
< 2 || c
->display_usage
) {
723 d_fprintf(stderr
, "%s\n%s",
725 _("net sam rights grant <name> <rights> ...\n"));
729 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
730 &dom
, &name
, &sid
, &type
)) {
731 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
735 for (i
=1; i
< argc
; i
++) {
736 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
737 if (privilege
== SEC_PRIV_INVALID
) {
738 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
742 if (!grant_privilege_by_name(&sid
, argv
[i
])) {
743 d_fprintf(stderr
, _("Could not grant privilege\n"));
747 d_printf(_("Granted %s to %s\\%s\n"), argv
[i
], dom
, name
);
753 static int net_sam_rights_revoke(struct net_context
*c
, int argc
,
757 enum lsa_SidType type
;
758 const char *dom
, *name
;
761 if (argc
< 2 || c
->display_usage
) {
762 d_fprintf(stderr
, "%s\n%s",
764 _("net sam rights revoke <name> <rights>\n"));
768 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
769 &dom
, &name
, &sid
, &type
)) {
770 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
774 for (i
=1; i
< argc
; i
++) {
775 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
776 if (privilege
== SEC_PRIV_INVALID
) {
777 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
781 if (!revoke_privilege_by_name(&sid
, argv
[i
])) {
782 d_fprintf(stderr
, _("Could not revoke privilege\n"));
786 d_printf(_("Revoked %s from %s\\%s\n"), argv
[i
], dom
, name
);
792 static int net_sam_rights(struct net_context
*c
, int argc
, const char **argv
)
794 struct functable func
[] = {
799 N_("List possible user rights"),
800 N_("net sam rights list\n"
801 " List possible user rights")
805 net_sam_rights_grant
,
807 N_("Grant right(s)"),
808 N_("net sam rights grant\n"
813 net_sam_rights_revoke
,
815 N_("Revoke right(s)"),
816 N_("net sam rights revoke\n"
819 {NULL
, NULL
, 0, NULL
, NULL
}
821 return net_run_function(c
, argc
, argv
, "net sam rights", func
);
825 * Map a unix group to a domain group
828 static NTSTATUS
map_unix_group(const struct group
*grp
, GROUP_MAP
*map
)
830 const char *dom
, *name
;
833 if (pdb_getgrgid(map
, grp
->gr_gid
)) {
834 return NT_STATUS_GROUP_EXISTS
;
837 map
->gid
= grp
->gr_gid
;
839 if (lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
840 &dom
, &name
, NULL
, NULL
)) {
842 map
->nt_name
= talloc_asprintf(map
, "Unix Group %s",
845 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
846 grp
->gr_name
, dom
, name
, map
->nt_name
));
849 if (lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
850 NULL
, NULL
, NULL
, NULL
)) {
851 DEBUG(3, ("\"%s\" exists, can't map it\n", grp
->gr_name
));
852 return NT_STATUS_GROUP_EXISTS
;
855 if (pdb_capabilities() & PDB_CAP_STORE_RIDS
) {
856 if (!pdb_new_rid(&rid
)) {
857 DEBUG(3, ("Could not get a new RID for %s\n",
859 return NT_STATUS_ACCESS_DENIED
;
862 rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
865 sid_compose(&map
->sid
, get_global_sam_sid(), rid
);
866 map
->sid_name_use
= SID_NAME_DOM_GRP
;
867 map
->comment
= talloc_asprintf(map
, "Unix Group %s", grp
->gr_name
);
869 return pdb_add_group_mapping_entry(map
);
872 static int net_sam_mapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
877 struct dom_sid_buf buf
;
879 if (argc
!= 1 || c
->display_usage
) {
880 d_fprintf(stderr
, "%s\n%s",
882 _("net sam mapunixgroup <name>\n"));
886 grp
= getgrnam(argv
[0]);
888 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
892 map
= talloc_zero(talloc_tos(), GROUP_MAP
);
894 d_fprintf(stderr
, _("Out of memory!\n"));
898 status
= map_unix_group(grp
, map
);
900 if (!NT_STATUS_IS_OK(status
)) {
901 d_fprintf(stderr
, _("Mapping group %s failed with %s\n"),
902 argv
[0], nt_errstr(status
));
906 d_printf(_("Mapped unix group %s to SID %s\n"), argv
[0],
907 dom_sid_str_buf(&map
->sid
, &buf
));
914 * Remove a group mapping
917 static NTSTATUS
unmap_unix_group(const struct group
*grp
)
919 struct dom_sid dom_sid
;
922 if (!lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
923 NULL
, NULL
, NULL
, NULL
)) {
924 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp
->gr_name
));
925 return NT_STATUS_NO_SUCH_GROUP
;
929 id
.type
= ID_TYPE_GID
;
930 if (!pdb_id_to_sid(&id
, &dom_sid
)) {
931 return NT_STATUS_UNSUCCESSFUL
;
934 return pdb_delete_group_mapping_entry(dom_sid
);
937 static int net_sam_unmapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
942 if (argc
!= 1 || c
->display_usage
) {
943 d_fprintf(stderr
, "%s\n%s",
945 _("net sam unmapunixgroup <name>\n"));
949 grp
= getgrnam(argv
[0]);
951 d_fprintf(stderr
, _("Could not find mapping for group %s.\n"),
956 status
= unmap_unix_group(grp
);
958 if (!NT_STATUS_IS_OK(status
)) {
959 d_fprintf(stderr
, _("Unmapping group %s failed with %s.\n"),
960 argv
[0], nt_errstr(status
));
964 d_printf(_("Unmapped unix group %s.\n"), argv
[0]);
970 * Create a domain group
973 static int net_sam_createdomaingroup(struct net_context
*c
, int argc
,
979 if (argc
!= 1 || c
->display_usage
) {
980 d_fprintf(stderr
, "%s\n%s",
982 _("net sam createdomaingroup <name>\n"));
986 status
= pdb_create_dom_group(talloc_tos(), argv
[0], &rid
);
988 if (!NT_STATUS_IS_OK(status
)) {
989 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
990 argv
[0], nt_errstr(status
));
994 d_printf(_("Created domain group %s with RID %d\n"), argv
[0], rid
);
1000 * Delete a domain group
1003 static int net_sam_deletedomaingroup(struct net_context
*c
, int argc
,
1008 enum lsa_SidType type
;
1009 const char *dom
, *name
;
1012 if (argc
!= 1 || c
->display_usage
) {
1013 d_fprintf(stderr
, "%s\n%s",
1015 _("net sam deletelocalgroup <name>\n"));
1019 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1020 &dom
, &name
, &sid
, &type
)) {
1021 d_fprintf(stderr
, _("Could not find %s.\n"), argv
[0]);
1025 if (type
!= SID_NAME_DOM_GRP
) {
1026 d_fprintf(stderr
, _("%s is a %s, not a domain group.\n"),
1027 argv
[0], sid_type_lookup(type
));
1031 sid_peek_rid(&sid
, &rid
);
1033 status
= pdb_delete_dom_group(talloc_tos(), rid
);
1035 if (!NT_STATUS_IS_OK(status
)) {
1036 d_fprintf(stderr
,_("Deleting domain group %s failed with %s\n"),
1037 argv
[0], nt_errstr(status
));
1041 d_printf(_("Deleted domain group %s.\n"), argv
[0]);
1047 * Create a local group
1050 static int net_sam_createlocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1055 if (argc
!= 1 || c
->display_usage
) {
1056 d_fprintf(stderr
, "%s\n%s",
1058 _("net sam createlocalgroup <name>\n"));
1062 if (!winbind_ping()) {
1063 d_fprintf(stderr
, _("winbind seems not to run. "
1064 "createlocalgroup only works when winbind runs.\n"));
1068 status
= pdb_create_alias(argv
[0], &rid
);
1070 if (!NT_STATUS_IS_OK(status
)) {
1071 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1072 argv
[0], nt_errstr(status
));
1076 d_printf(_("Created local group %s with RID %d\n"), argv
[0], rid
);
1082 * Delete a local group
1085 static int net_sam_deletelocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1088 enum lsa_SidType type
;
1089 const char *dom
, *name
;
1092 if (argc
!= 1 || c
->display_usage
) {
1093 d_fprintf(stderr
, "%s\n%s",
1095 _("net sam deletelocalgroup <name>\n"));
1099 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1100 &dom
, &name
, &sid
, &type
)) {
1101 d_fprintf(stderr
,_("Could not find %s.\n"), argv
[0]);
1105 if (type
!= SID_NAME_ALIAS
) {
1106 d_fprintf(stderr
, _("%s is a %s, not a local group.\n"),argv
[0],
1107 sid_type_lookup(type
));
1111 status
= pdb_delete_alias(&sid
);
1113 if (!NT_STATUS_IS_OK(status
)) {
1114 d_fprintf(stderr
, _("Deleting local group %s failed with %s\n"),
1115 argv
[0], nt_errstr(status
));
1119 d_printf(_("Deleted local group %s.\n"), argv
[0]);
1125 * Create a builtin group
1128 static int net_sam_createbuiltingroup(struct net_context
*c
, int argc
, const char **argv
)
1132 enum lsa_SidType type
;
1136 if (argc
!= 1 || c
->display_usage
) {
1137 d_fprintf(stderr
, "%s\n%s",
1139 _("net sam createbuiltingroup <name>\n"));
1143 if (!winbind_ping()) {
1144 d_fprintf(stderr
, _("winbind seems not to run. "
1145 "createbuiltingroup only works when winbind "
1150 /* validate the name and get the group */
1152 fstrcpy( groupname
, "BUILTIN\\" );
1153 fstrcat( groupname
, argv
[0] );
1155 if ( !lookup_name(talloc_tos(), groupname
, LOOKUP_NAME_ALL
, NULL
,
1156 NULL
, &sid
, &type
)) {
1157 d_fprintf(stderr
, _("%s is not a BUILTIN group\n"), argv
[0]);
1161 if ( !sid_peek_rid( &sid
, &rid
) ) {
1162 d_fprintf(stderr
, _("Failed to get RID for %s\n"), argv
[0]);
1166 status
= pdb_create_builtin(rid
);
1168 if (!NT_STATUS_IS_OK(status
)) {
1169 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1170 argv
[0], nt_errstr(status
));
1174 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv
[0], rid
);
1180 * Add a group member
1183 static int net_sam_addmem(struct net_context
*c
, int argc
, const char **argv
)
1185 const char *groupdomain
, *groupname
, *memberdomain
, *membername
;
1186 struct dom_sid group
, member
;
1187 enum lsa_SidType grouptype
, membertype
;
1190 if (argc
!= 2 || c
->display_usage
) {
1191 d_fprintf(stderr
, "%s\n%s",
1193 _("net sam addmem <group> <member>\n"));
1197 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1198 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1199 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1203 /* check to see if the member to be added is a name or a SID */
1205 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1206 &memberdomain
, &membername
, &member
, &membertype
))
1208 /* try it as a SID */
1210 if ( !string_to_sid( &member
, argv
[1] ) ) {
1211 d_fprintf(stderr
, _("Could not find member %s\n"),
1216 if ( !lookup_sid(talloc_tos(), &member
, &memberdomain
,
1217 &membername
, &membertype
) )
1219 d_fprintf(stderr
, _("Could not resolve SID %s\n"),
1225 if ((grouptype
== SID_NAME_ALIAS
) || (grouptype
== SID_NAME_WKN_GRP
)) {
1226 if ((membertype
!= SID_NAME_USER
) &&
1227 (membertype
!= SID_NAME_ALIAS
) &&
1228 (membertype
!= SID_NAME_DOM_GRP
)) {
1229 d_fprintf(stderr
, _("Can't add %s: only users, domain "
1230 "groups and domain local groups "
1231 "can be added. %s is a %s\n"),
1233 sid_type_lookup(membertype
));
1236 status
= pdb_add_aliasmem(&group
, &member
);
1238 if (!NT_STATUS_IS_OK(status
)) {
1239 d_fprintf(stderr
, _("Adding local group member failed "
1240 "with %s\n"), nt_errstr(status
));
1243 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1244 uint32_t grouprid
, memberrid
;
1246 sid_peek_rid(&group
, &grouprid
);
1247 sid_peek_rid(&member
, &memberrid
);
1249 status
= pdb_add_groupmem(talloc_tos(), grouprid
, memberrid
);
1250 if (!NT_STATUS_IS_OK(status
)) {
1251 d_fprintf(stderr
, _("Adding domain group member failed "
1252 "with %s\n"), nt_errstr(status
));
1256 d_fprintf(stderr
, _("Can only add members to local groups so "
1257 "far, %s is a %s\n"), argv
[0],
1258 sid_type_lookup(grouptype
));
1262 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain
, membername
,
1263 groupdomain
, groupname
);
1269 * Delete a group member
1272 static int net_sam_delmem(struct net_context
*c
, int argc
, const char **argv
)
1274 const char *groupdomain
, *groupname
;
1275 const char *memberdomain
= NULL
;
1276 const char *membername
= NULL
;
1277 struct dom_sid group
, member
;
1278 enum lsa_SidType grouptype
;
1281 if (argc
!= 2 || c
->display_usage
) {
1282 d_fprintf(stderr
,"%s\n%s",
1284 _("net sam delmem <group> <member>\n"));
1288 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1289 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1290 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1294 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1295 &memberdomain
, &membername
, &member
, NULL
)) {
1296 if (!string_to_sid(&member
, argv
[1])) {
1297 d_fprintf(stderr
, _("Could not find member %s\n"),
1303 if ((grouptype
== SID_NAME_ALIAS
) ||
1304 (grouptype
== SID_NAME_WKN_GRP
)) {
1305 status
= pdb_del_aliasmem(&group
, &member
);
1307 if (!NT_STATUS_IS_OK(status
)) {
1308 d_fprintf(stderr
,_("Deleting local group member failed "
1309 "with %s\n"), nt_errstr(status
));
1312 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1313 uint32_t grouprid
, memberrid
;
1315 sid_peek_rid(&group
, &grouprid
);
1316 sid_peek_rid(&member
, &memberrid
);
1318 status
= pdb_del_groupmem(talloc_tos(), grouprid
, memberrid
);
1319 if (!NT_STATUS_IS_OK(status
)) {
1320 d_fprintf(stderr
, _("Deleting domain group member "
1321 "failed with %s\n"), nt_errstr(status
));
1325 d_fprintf(stderr
, _("Can only delete members from local groups "
1326 "so far, %s is a %s\n"), argv
[0],
1327 sid_type_lookup(grouptype
));
1331 if (membername
!= NULL
) {
1332 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1333 memberdomain
, membername
, groupdomain
, groupname
);
1335 struct dom_sid_buf buf
;
1336 d_printf(_("Deleted %s from %s\\%s\n"),
1337 dom_sid_str_buf(&member
, &buf
),
1346 * List group members
1349 static int net_sam_listmem(struct net_context
*c
, int argc
, const char **argv
)
1351 const char *groupdomain
, *groupname
;
1352 struct dom_sid group
;
1353 struct dom_sid
*members
= NULL
;
1354 size_t i
, num_members
= 0;
1355 enum lsa_SidType grouptype
;
1358 if (argc
!= 1 || c
->display_usage
) {
1359 d_fprintf(stderr
, "%s\n%s",
1361 _("net sam listmem <group>\n"));
1365 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1366 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1367 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1371 if ((grouptype
== SID_NAME_ALIAS
) ||
1372 (grouptype
== SID_NAME_WKN_GRP
)) {
1373 status
= pdb_enum_aliasmem(&group
, talloc_tos(), &members
,
1375 if (!NT_STATUS_IS_OK(status
)) {
1376 d_fprintf(stderr
, _("Listing group members failed with "
1377 "%s\n"), nt_errstr(status
));
1380 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1383 status
= pdb_enum_group_members(talloc_tos(), &group
,
1384 &rids
, &num_members
);
1385 if (!NT_STATUS_IS_OK(status
)) {
1386 d_fprintf(stderr
, _("Listing group members failed with "
1387 "%s\n"), nt_errstr(status
));
1391 members
= talloc_array(talloc_tos(), struct dom_sid
,
1393 if (members
== NULL
) {
1398 for (i
=0; i
<num_members
; i
++) {
1399 sid_compose(&members
[i
], get_global_sam_sid(),
1404 d_fprintf(stderr
,_("Can only list local group members so far.\n"
1405 "%s is a %s\n"), argv
[0], sid_type_lookup(grouptype
));
1409 d_printf(_("%s\\%s has %u members\n"), groupdomain
, groupname
,
1410 (unsigned int)num_members
);
1411 for (i
=0; i
<num_members
; i
++) {
1412 const char *dom
, *name
;
1413 if (lookup_sid(talloc_tos(), &members
[i
], &dom
, &name
, NULL
)) {
1414 d_printf(" %s\\%s\n", dom
, name
);
1416 struct dom_sid_buf buf
;
1418 dom_sid_str_buf(&members
[i
], &buf
));
1422 TALLOC_FREE(members
);
1430 static int net_sam_do_list(struct net_context
*c
, int argc
, const char **argv
,
1431 struct pdb_search
*search
, const char *what
)
1433 bool verbose
= (argc
== 1);
1435 if ((argc
> 1) || c
->display_usage
||
1436 ((argc
== 1) && !strequal(argv
[0], "verbose"))) {
1437 d_fprintf(stderr
, "%s\n", _("Usage:"));
1438 d_fprintf(stderr
, _("net sam list %s [verbose]\n"), what
);
1442 if (search
== NULL
) {
1443 d_fprintf(stderr
, _("Could not start search\n"));
1448 struct samr_displayentry entry
;
1449 if (!search
->next_entry(search
, &entry
)) {
1453 d_printf("%s:%d:%s\n",
1458 d_printf("%s\n", entry
.account_name
);
1462 TALLOC_FREE(search
);
1466 static int net_sam_list_users(struct net_context
*c
, int argc
,
1469 return net_sam_do_list(c
, argc
, argv
,
1470 pdb_search_users(talloc_tos(), ACB_NORMAL
),
1474 static int net_sam_list_groups(struct net_context
*c
, int argc
,
1477 return net_sam_do_list(c
, argc
, argv
, pdb_search_groups(talloc_tos()),
1481 static int net_sam_list_localgroups(struct net_context
*c
, int argc
,
1484 return net_sam_do_list(c
, argc
, argv
,
1485 pdb_search_aliases(talloc_tos(),
1486 get_global_sam_sid()),
1490 static int net_sam_list_builtin(struct net_context
*c
, int argc
,
1493 return net_sam_do_list(c
, argc
, argv
,
1494 pdb_search_aliases(talloc_tos(),
1495 &global_sid_Builtin
),
1499 static int net_sam_list_workstations(struct net_context
*c
, int argc
,
1502 return net_sam_do_list(c
, argc
, argv
,
1503 pdb_search_users(talloc_tos(), ACB_WSTRUST
),
1511 static int net_sam_list(struct net_context
*c
, int argc
, const char **argv
)
1513 struct functable func
[] = {
1517 NET_TRANSPORT_LOCAL
,
1518 N_("List SAM users"),
1519 N_("net sam list users\n"
1524 net_sam_list_groups
,
1525 NET_TRANSPORT_LOCAL
,
1526 N_("List SAM groups"),
1527 N_("net sam list groups\n"
1532 net_sam_list_localgroups
,
1533 NET_TRANSPORT_LOCAL
,
1534 N_("List SAM local groups"),
1535 N_("net sam list localgroups\n"
1536 " List SAM local groups")
1540 net_sam_list_builtin
,
1541 NET_TRANSPORT_LOCAL
,
1542 N_("List builtin groups"),
1543 N_("net sam list builtin\n"
1544 " List builtin groups")
1548 net_sam_list_workstations
,
1549 NET_TRANSPORT_LOCAL
,
1550 N_("List domain member workstations"),
1551 N_("net sam list workstations\n"
1552 " List domain member workstations")
1554 {NULL
, NULL
, 0, NULL
, NULL
}
1557 return net_run_function(c
, argc
, argv
, "net sam list", func
);
1561 * Show details of SAM entries
1564 static int net_sam_show(struct net_context
*c
, int argc
, const char **argv
)
1567 struct dom_sid_buf buf
;
1568 enum lsa_SidType type
;
1569 const char *dom
, *name
;
1571 if (argc
!= 1 || c
->display_usage
) {
1572 d_fprintf(stderr
, "%s\n%s",
1574 _("net sam show <name>\n"));
1578 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1579 &dom
, &name
, &sid
, &type
)) {
1580 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
1584 d_printf(_("%s\\%s is a %s with SID %s\n"), dom
, name
,
1585 sid_type_lookup(type
), dom_sid_str_buf(&sid
, &buf
));
1593 * Init an LDAP tree with default users and Groups
1594 * if ldapsam:editposix is enabled
1597 static int net_sam_provision(struct net_context
*c
, int argc
, const char **argv
)
1601 char *ldap_uri
= NULL
;
1603 struct smbldap_state
*state
= NULL
;
1604 GROUP_MAP
*gmap
= NULL
;
1605 struct dom_sid gsid
;
1606 gid_t domusers_gid
= -1;
1607 gid_t domadmins_gid
= -1;
1608 struct samu
*samuser
;
1610 bool is_ipa
= false;
1611 char *bind_dn
= NULL
;
1612 char *bind_secret
= NULL
;
1615 if (c
->display_usage
) {
1617 "net sam provision\n"
1620 _("Init an LDAP tree with default users/groups"));
1624 tc
= talloc_new(NULL
);
1626 d_fprintf(stderr
, _("Out of Memory!\n"));
1630 if ((ldap_bk
= talloc_strdup(tc
, lp_passdb_backend())) == NULL
) {
1631 d_fprintf(stderr
, _("talloc failed\n"));
1635 p
= strchr(ldap_bk
, ':');
1638 ldap_uri
= talloc_strdup(tc
, p
+1);
1639 trim_char(ldap_uri
, ' ', ' ');
1642 trim_char(ldap_bk
, ' ', ' ');
1644 if (strcmp(ldap_bk
, "IPA_ldapsam") == 0 ) {
1648 if (strcmp(ldap_bk
, "ldapsam") != 0 && !is_ipa
) {
1650 _("Provisioning works only with ldapsam backend\n"));
1654 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1655 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1657 d_fprintf(stderr
, _("Provisioning works only if ldapsam:trusted"
1658 " and ldapsam:editposix are enabled.\n"));
1662 if (!is_ipa
&& !winbind_ping()) {
1663 d_fprintf(stderr
, _("winbind seems not to run. Provisioning "
1664 "LDAP only works when winbind runs.\n"));
1668 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
1669 d_fprintf(stderr
, _("Failed to retrieve LDAP password from secrets.tdb\n"));
1673 status
= smbldap_init(tc
, NULL
, ldap_uri
, false, bind_dn
, bind_secret
, &state
);
1675 memset(bind_secret
, '\0', strlen(bind_secret
));
1676 SAFE_FREE(bind_secret
);
1679 if (!NT_STATUS_IS_OK(status
)) {
1680 d_fprintf(stderr
, _("Unable to connect to the LDAP server.\n"));
1684 d_printf(_("Checking for Domain Users group.\n"));
1686 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_USERS
);
1688 gmap
= talloc_zero(tc
, GROUP_MAP
);
1690 d_printf(_("Out of memory!\n"));
1694 if (!pdb_getgrsid(gmap
, gsid
)) {
1695 struct dom_sid_buf gsid_str
;
1696 LDAPMod
**mods
= NULL
;
1704 d_printf(_("Adding the Domain Users group.\n"));
1706 /* lets allocate a new groupid for this group */
1710 if (!winbind_allocate_gid(&domusers_gid
)) {
1711 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1712 "create Domain Users group!\n"));
1717 uname
= talloc_strdup(tc
, "domusers");
1718 wname
= talloc_strdup(tc
, "Domain Users");
1719 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domusers",
1720 lp_ldap_group_suffix(talloc_tos()));
1721 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domusers_gid
);
1722 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1724 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1725 d_fprintf(stderr
, "Out of Memory!\n");
1729 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1730 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1732 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1733 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1734 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1736 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1737 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1738 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1739 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1740 dom_sid_str_buf(&gsid
, &gsid_str
));
1741 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1743 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1745 rc
= smbldap_add(state
, dn
, mods
);
1747 if (rc
!= LDAP_SUCCESS
) {
1748 d_fprintf(stderr
, _("Failed to add Domain Users group "
1749 "to ldap directory\n"));
1753 if (!pdb_getgrsid(gmap
, gsid
)) {
1754 d_fprintf(stderr
, _("Failed to read just "
1755 "created domain group.\n"));
1758 domusers_gid
= gmap
->gid
;
1762 domusers_gid
= gmap
->gid
;
1763 d_printf(_("found!\n"));
1768 d_printf(_("Checking for Domain Admins group.\n"));
1770 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_ADMINS
);
1772 if (!pdb_getgrsid(gmap
, gsid
)) {
1773 struct dom_sid_buf gsid_str
;
1774 LDAPMod
**mods
= NULL
;
1782 d_printf(_("Adding the Domain Admins group.\n"));
1784 /* lets allocate a new groupid for this group */
1786 domadmins_gid
= 999;
1788 if (!winbind_allocate_gid(&domadmins_gid
)) {
1789 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1790 "create Domain Admins group!\n"));
1795 uname
= talloc_strdup(tc
, "domadmins");
1796 wname
= talloc_strdup(tc
, "Domain Admins");
1797 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domadmins",
1798 lp_ldap_group_suffix(talloc_tos()));
1799 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1800 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1802 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1803 d_fprintf(stderr
, _("Out of Memory!\n"));
1807 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1808 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1810 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1811 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1812 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1814 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1815 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1816 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1817 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1818 dom_sid_str_buf(&gsid
, &gsid_str
));
1819 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1821 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1823 rc
= smbldap_add(state
, dn
, mods
);
1825 if (rc
!= LDAP_SUCCESS
) {
1826 d_fprintf(stderr
, _("Failed to add Domain Admins group "
1827 "to ldap directory\n"));
1831 if (!pdb_getgrsid(gmap
, gsid
)) {
1832 d_fprintf(stderr
, _("Failed to read just "
1833 "created domain group.\n"));
1836 domadmins_gid
= gmap
->gid
;
1840 domadmins_gid
= gmap
->gid
;
1841 d_printf(_("found!\n"));
1846 d_printf(_("Check for Administrator account.\n"));
1848 samuser
= samu_new(tc
);
1850 d_fprintf(stderr
, _("Out of Memory!\n"));
1854 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1855 LDAPMod
**mods
= NULL
;
1857 struct dom_sid_buf sid_str
;
1868 d_printf(_("Adding the Administrator user.\n"));
1870 if (domadmins_gid
== -1) {
1872 _("Can't create Administrator user, Domain "
1873 "Admins group not available!\n"));
1880 if (!winbind_allocate_uid(&uid
)) {
1882 _("Unable to allocate a new uid to create "
1883 "the Administrator user!\n"));
1888 name
= talloc_strdup(tc
, "Administrator");
1889 dn
= talloc_asprintf(tc
, "uid=Administrator,%s",
1890 lp_ldap_user_suffix(talloc_tos()));
1891 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)uid
);
1892 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1893 dir
= talloc_sub_specified(tc
, lp_template_homedir(),
1896 get_global_sam_name(),
1897 uid
, domadmins_gid
);
1898 shell
= talloc_sub_specified(tc
, lp_template_shell(),
1901 get_global_sam_name(),
1902 uid
, domadmins_gid
);
1904 if (!name
|| !dn
|| !uidstr
|| !gidstr
|| !dir
|| !shell
) {
1905 d_fprintf(stderr
, _("Out of Memory!\n"));
1909 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR
);
1911 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1912 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1913 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1915 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
1916 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
1917 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
1918 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
1919 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
1920 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
1921 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", name
);
1922 princ
= talloc_asprintf(tc
, "%s@%s", name
, lp_realm());
1924 d_fprintf(stderr
, _("Out of Memory!\n"));
1927 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "krbPrincipalName", princ
);
1929 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", name
);
1930 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
1931 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
1932 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1933 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1934 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", dir
);
1935 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
1936 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
1937 dom_sid_str_buf(&sid
, &sid_str
));
1938 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
1939 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
1940 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1942 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1944 rc
= smbldap_add(state
, dn
, mods
);
1946 if (rc
!= LDAP_SUCCESS
) {
1947 d_fprintf(stderr
, _("Failed to add Administrator user "
1948 "to ldap directory\n"));
1952 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1953 d_fprintf(stderr
, _("Failed to read just "
1954 "created user.\n"));
1959 d_printf(_("found!\n"));
1962 d_printf(_("Checking for Guest user.\n"));
1964 samuser
= samu_new(tc
);
1966 d_fprintf(stderr
, _("Out of Memory!\n"));
1970 if (!pdb_getsampwnam(samuser
, lp_guest_account())) {
1971 LDAPMod
**mods
= NULL
;
1973 struct dom_sid_buf sid_str
;
1979 d_printf(_("Adding the Guest user.\n"));
1981 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_GUEST
);
1983 pwd
= Get_Pwnam_alloc(tc
, lp_guest_account());
1986 if (domusers_gid
== -1) {
1988 _("Can't create Guest user, Domain "
1989 "Users group not available!\n"));
1992 if ((pwd
= talloc(tc
, struct passwd
)) == NULL
) {
1993 d_fprintf(stderr
, _("talloc failed\n"));
1996 pwd
->pw_name
= talloc_strdup(pwd
, lp_guest_account());
2001 if (!winbind_allocate_uid(&(pwd
->pw_uid
))) {
2003 _("Unable to allocate a new uid to "
2004 "create the Guest user!\n"));
2008 pwd
->pw_gid
= domusers_gid
;
2009 pwd
->pw_dir
= talloc_strdup(tc
, "/");
2010 pwd
->pw_shell
= talloc_strdup(tc
, "/bin/false");
2011 if (!pwd
->pw_dir
|| !pwd
->pw_shell
) {
2012 d_fprintf(stderr
, _("Out of Memory!\n"));
2017 dn
= talloc_asprintf(tc
, "uid=%s,%s", pwd
->pw_name
,
2018 lp_ldap_user_suffix (talloc_tos()));
2019 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_uid
);
2020 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2021 if (!dn
|| !uidstr
|| !gidstr
) {
2022 d_fprintf(stderr
, _("Out of Memory!\n"));
2026 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
2027 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
2028 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
2030 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
2031 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
2032 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
2033 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
2034 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
2035 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
2036 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", pwd
->pw_name
);
2038 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", pwd
->pw_name
);
2039 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", pwd
->pw_name
);
2040 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", pwd
->pw_name
);
2041 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
2042 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2043 if ((pwd
->pw_dir
!= NULL
) && (pwd
->pw_dir
[0] != '\0')) {
2044 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", pwd
->pw_dir
);
2046 if ((pwd
->pw_shell
!= NULL
) && (pwd
->pw_shell
[0] != '\0')) {
2047 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", pwd
->pw_shell
);
2049 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
2050 dom_sid_str_buf(&sid
, &sid_str
));
2051 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
2052 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
2053 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
2055 smbldap_talloc_autofree_ldapmod(tc
, mods
);
2057 rc
= smbldap_add(state
, dn
, mods
);
2059 if (rc
!= LDAP_SUCCESS
) {
2060 d_fprintf(stderr
, _("Failed to add Guest user to "
2061 "ldap directory\n"));
2065 if (!pdb_getsampwnam(samuser
, lp_guest_account())) {
2066 d_fprintf(stderr
, _("Failed to read just "
2067 "created user.\n"));
2072 d_printf(_("found!\n"));
2075 d_printf(_("Checking Guest's group.\n"));
2077 pwd
= Get_Pwnam_alloc(tc
, lp_guest_account());
2080 _("Failed to find just created Guest account!\n"
2081 " Is nss properly configured?!\n"));
2085 if (pwd
->pw_gid
== domusers_gid
) {
2086 d_printf(_("found!\n"));
2090 if (!pdb_getgrgid(gmap
, pwd
->pw_gid
)) {
2091 struct dom_sid_buf gsid_str
;
2092 LDAPMod
**mods
= NULL
;
2100 d_printf(_("Adding the Domain Guests group.\n"));
2102 uname
= talloc_strdup(tc
, "domguests");
2103 wname
= talloc_strdup(tc
, "Domain Guests");
2104 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domguests",
2105 lp_ldap_group_suffix(talloc_tos()));
2106 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2107 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
2109 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
2110 d_fprintf(stderr
, _("Out of Memory!\n"));
2114 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_GUESTS
);
2116 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
2117 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
2119 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
2120 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
2121 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
2123 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
2124 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
2125 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2126 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
2127 dom_sid_str_buf(&gsid
, &gsid_str
));
2128 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
2130 smbldap_talloc_autofree_ldapmod(tc
, mods
);
2132 rc
= smbldap_add(state
, dn
, mods
);
2134 if (rc
!= LDAP_SUCCESS
) {
2136 _("Failed to add Domain Guests group to ldap "
2140 d_printf(_("found!\n"));
2155 /***********************************************************
2156 migrated functionality from smbgroupedit
2157 **********************************************************/
2158 int net_sam(struct net_context
*c
, int argc
, const char **argv
)
2160 struct functable func
[] = {
2162 "createbuiltingroup",
2163 net_sam_createbuiltingroup
,
2164 NET_TRANSPORT_LOCAL
,
2165 N_("Create a new BUILTIN group"),
2166 N_("net sam createbuiltingroup\n"
2167 " Create a new BUILTIN group")
2171 net_sam_createlocalgroup
,
2172 NET_TRANSPORT_LOCAL
,
2173 N_("Create a new local group"),
2174 N_("net sam createlocalgroup\n"
2175 " Create a new local group")
2178 "createdomaingroup",
2179 net_sam_createdomaingroup
,
2180 NET_TRANSPORT_LOCAL
,
2181 N_("Create a new group"),
2182 N_("net sam createdomaingroup\n"
2183 " Create a new group")
2187 net_sam_deletelocalgroup
,
2188 NET_TRANSPORT_LOCAL
,
2189 N_("Delete an existing local group"),
2190 N_("net sam deletelocalgroup\n"
2191 " Delete an existing local group")
2194 "deletedomaingroup",
2195 net_sam_deletedomaingroup
,
2196 NET_TRANSPORT_LOCAL
,
2197 N_("Delete a domain group"),
2198 N_("net sam deletedomaingroup\n"
2203 net_sam_mapunixgroup
,
2204 NET_TRANSPORT_LOCAL
,
2205 N_("Map a unix group to a domain group"),
2206 N_("net sam mapunixgroup\n"
2207 " Map a unix group to a domain group")
2211 net_sam_unmapunixgroup
,
2212 NET_TRANSPORT_LOCAL
,
2213 N_("Remove a group mapping of an unix group to a "
2215 N_("net sam unmapunixgroup\n"
2216 " Remove a group mapping of an unix group to a "
2222 NET_TRANSPORT_LOCAL
,
2223 N_("Add a member to a group"),
2224 N_("net sam addmem\n"
2225 " Add a member to a group")
2230 NET_TRANSPORT_LOCAL
,
2231 N_("Delete a member from a group"),
2232 N_("net sam delmem\n"
2233 " Delete a member from a group")
2238 NET_TRANSPORT_LOCAL
,
2239 N_("List group members"),
2240 N_("net sam listmem\n"
2241 " List group members")
2246 NET_TRANSPORT_LOCAL
,
2247 N_("List users, groups and local groups"),
2249 " List users, groups and local groups")
2254 NET_TRANSPORT_LOCAL
,
2255 N_("Show details of a SAM entry"),
2257 " Show details of a SAM entry")
2262 NET_TRANSPORT_LOCAL
,
2263 N_("Set details of a SAM account"),
2265 " Set details of a SAM account")
2270 NET_TRANSPORT_LOCAL
,
2271 N_("Set account policies"),
2272 N_("net sam policy\n"
2273 " Set account policies")
2278 NET_TRANSPORT_LOCAL
,
2279 N_("Manipulate user privileges"),
2280 N_("net sam rights\n"
2281 " Manipulate user privileges")
2287 NET_TRANSPORT_LOCAL
,
2288 N_("Provision a clean user database"),
2289 N_("net sam privison\n"
2290 " Provision a clear user database")
2293 {NULL
, NULL
, 0, NULL
, NULL
}
2296 if (getuid() != 0) {
2297 d_fprintf(stderr
, _("You are not root, most things won't "
2301 return net_run_function(c
, argc
, argv
, "net sam", func
);