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 old_value
= 0;
485 enum pdb_policy_type field
;
488 if (argc
!= 2 || c
->display_usage
) {
489 d_fprintf(stderr
, "%s\n%s",
491 _("net sam policy set \"<account policy>\" <value>\n"));
495 account_policy
= argv
[0];
496 field
= account_policy_name_to_typenum(account_policy
);
498 if (strequal(argv
[1], "forever") || strequal(argv
[1], "never")
499 || strequal(argv
[1], "off")) {
503 value
= strtoul(argv
[1], &endptr
, 10);
505 if ((endptr
== argv
[1]) || (endptr
[0] != '\0')) {
506 d_printf(_("Unable to set policy \"%s\"! Invalid value "
508 account_policy
, argv
[1]);
517 account_policy_names_list(talloc_tos(), &names
, &count
);
518 d_fprintf(stderr
, _("No account policy \"%s\"!\n\n"), argv
[0]);
519 d_fprintf(stderr
, _("Valid account policies are:\n"));
521 for (i
=0; i
<count
; i
++) {
522 d_fprintf(stderr
, "%s\n", names
[i
]);
530 if (!pdb_get_account_policy(field
, &old_value
)) {
531 d_fprintf(stderr
, _("Valid account policy, but unable to fetch "
534 d_printf(_("Account policy \"%s\" value was: %d\n"),
535 account_policy
, old_value
);
538 if (!pdb_set_account_policy(field
, value
)) {
539 d_fprintf(stderr
, _("Valid account policy, but unable to "
543 d_printf(_("Account policy \"%s\" value is now: %d\n"),
544 account_policy
, value
);
550 static int net_sam_policy_show(struct net_context
*c
, int argc
, const char **argv
)
552 const char *account_policy
= NULL
;
554 enum pdb_policy_type field
;
556 if (argc
!= 1 || c
->display_usage
) {
557 d_fprintf(stderr
, "%s\n%s",
559 _("net sam policy show \"<account policy>\"\n"));
563 account_policy
= argv
[0];
564 field
= account_policy_name_to_typenum(account_policy
);
570 account_policy_names_list(talloc_tos(), &names
, &count
);
571 d_fprintf(stderr
, _("No account policy by that name!\n"));
573 d_fprintf(stderr
, _("Valid account policies "
575 for (i
=0; i
<count
; i
++) {
576 d_fprintf(stderr
, "%s\n", names
[i
]);
583 if (!pdb_get_account_policy(field
, &old_value
)) {
584 fprintf(stderr
, _("Valid account policy, but unable to "
589 printf(_("Account policy \"%s\" description: %s\n"),
590 account_policy
, account_policy_get_desc(field
));
591 printf(_("Account policy \"%s\" value is: %d\n"), account_policy
,
596 static int net_sam_policy_list(struct net_context
*c
, int argc
, const char **argv
)
602 if (c
->display_usage
) {
604 "net sam policy list\n"
607 _("List account policies"));
611 account_policy_names_list(talloc_tos(), &names
, &count
);
613 d_fprintf(stderr
, _("Valid account policies "
615 for (i
= 0; i
< count
; i
++) {
616 d_fprintf(stderr
, "%s\n", names
[i
]);
623 static int net_sam_policy(struct net_context
*c
, int argc
, const char **argv
)
625 struct functable func
[] = {
630 N_("List account policies"),
631 N_("net sam policy list\n"
632 " List account policies")
638 N_("Show account policies"),
639 N_("net sam policy show\n"
640 " Show account policies")
646 N_("Change account policies"),
647 N_("net sam policy set\n"
648 " Change account policies")
650 {NULL
, NULL
, 0, NULL
, NULL
}
653 return net_run_function(c
, argc
, argv
, "net sam policy", func
);
656 static int net_sam_rights_list(struct net_context
*c
, int argc
,
659 enum sec_privilege privilege
;
661 if (argc
> 1 || c
->display_usage
) {
662 d_fprintf(stderr
, "%s\n%s",
664 _("net sam rights list [privilege name]\n"));
670 int num
= num_privileges_in_short_list();
672 for (i
=0; i
<num
; i
++) {
673 d_printf("%s\n", sec_privilege_name_from_index(i
));
678 privilege
= sec_privilege_id(argv
[0]);
680 if (privilege
!= SEC_PRIV_INVALID
) {
681 struct dom_sid
*sids
;
685 status
= privilege_enum_sids(privilege
, talloc_tos(),
687 if (!NT_STATUS_IS_OK(status
)) {
688 d_fprintf(stderr
, _("Could not list rights: %s\n"),
693 for (i
=0; i
<num_sids
; i
++) {
694 const char *dom
, *name
;
695 enum lsa_SidType type
;
697 if (lookup_sid(talloc_tos(), &sids
[i
], &dom
, &name
,
699 d_printf("%s\\%s\n", dom
, name
);
702 d_printf("%s\n", sid_string_tos(&sids
[i
]));
711 static int net_sam_rights_grant(struct net_context
*c
, int argc
,
715 enum lsa_SidType type
;
716 const char *dom
, *name
;
719 if (argc
< 2 || c
->display_usage
) {
720 d_fprintf(stderr
, "%s\n%s",
722 _("net sam rights grant <name> <rights> ...\n"));
726 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
727 &dom
, &name
, &sid
, &type
)) {
728 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
732 for (i
=1; i
< argc
; i
++) {
733 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
734 if (privilege
== SEC_PRIV_INVALID
) {
735 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
739 if (!grant_privilege_by_name(&sid
, argv
[i
])) {
740 d_fprintf(stderr
, _("Could not grant privilege\n"));
744 d_printf(_("Granted %s to %s\\%s\n"), argv
[i
], dom
, name
);
750 static int net_sam_rights_revoke(struct net_context
*c
, int argc
,
754 enum lsa_SidType type
;
755 const char *dom
, *name
;
758 if (argc
< 2 || c
->display_usage
) {
759 d_fprintf(stderr
, "%s\n%s",
761 _("net sam rights revoke <name> <rights>\n"));
765 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
766 &dom
, &name
, &sid
, &type
)) {
767 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
771 for (i
=1; i
< argc
; i
++) {
772 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
773 if (privilege
== SEC_PRIV_INVALID
) {
774 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
778 if (!revoke_privilege_by_name(&sid
, argv
[i
])) {
779 d_fprintf(stderr
, _("Could not revoke privilege\n"));
783 d_printf(_("Revoked %s from %s\\%s\n"), argv
[i
], dom
, name
);
789 static int net_sam_rights(struct net_context
*c
, int argc
, const char **argv
)
791 struct functable func
[] = {
796 N_("List possible user rights"),
797 N_("net sam rights list\n"
798 " List possible user rights")
802 net_sam_rights_grant
,
804 N_("Grant right(s)"),
805 N_("net sam rights grant\n"
810 net_sam_rights_revoke
,
812 N_("Revoke right(s)"),
813 N_("net sam rights revoke\n"
816 {NULL
, NULL
, 0, NULL
, NULL
}
818 return net_run_function(c
, argc
, argv
, "net sam rights", func
);
822 * Map a unix group to a domain group
825 static NTSTATUS
map_unix_group(const struct group
*grp
, GROUP_MAP
*map
)
827 const char *dom
, *name
;
830 if (pdb_getgrgid(map
, grp
->gr_gid
)) {
831 return NT_STATUS_GROUP_EXISTS
;
834 map
->gid
= grp
->gr_gid
;
836 if (lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
837 &dom
, &name
, NULL
, NULL
)) {
839 map
->nt_name
= talloc_asprintf(map
, "Unix Group %s",
842 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
843 grp
->gr_name
, dom
, name
, map
->nt_name
));
846 if (lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
847 NULL
, NULL
, NULL
, NULL
)) {
848 DEBUG(3, ("\"%s\" exists, can't map it\n", grp
->gr_name
));
849 return NT_STATUS_GROUP_EXISTS
;
852 if (pdb_capabilities() & PDB_CAP_STORE_RIDS
) {
853 if (!pdb_new_rid(&rid
)) {
854 DEBUG(3, ("Could not get a new RID for %s\n",
856 return NT_STATUS_ACCESS_DENIED
;
859 rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
862 sid_compose(&map
->sid
, get_global_sam_sid(), rid
);
863 map
->sid_name_use
= SID_NAME_DOM_GRP
;
864 map
->comment
= talloc_asprintf(map
, "Unix Group %s", grp
->gr_name
);
866 return pdb_add_group_mapping_entry(map
);
869 static int net_sam_mapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
875 if (argc
!= 1 || c
->display_usage
) {
876 d_fprintf(stderr
, "%s\n%s",
878 _("net sam mapunixgroup <name>\n"));
882 grp
= getgrnam(argv
[0]);
884 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
888 map
= talloc_zero(talloc_tos(), GROUP_MAP
);
890 d_fprintf(stderr
, _("Out of memory!\n"));
894 status
= map_unix_group(grp
, map
);
896 if (!NT_STATUS_IS_OK(status
)) {
897 d_fprintf(stderr
, _("Mapping group %s failed with %s\n"),
898 argv
[0], nt_errstr(status
));
902 d_printf(_("Mapped unix group %s to SID %s\n"), argv
[0],
903 sid_string_tos(&map
->sid
));
910 * Remove a group mapping
913 static NTSTATUS
unmap_unix_group(const struct group
*grp
)
915 struct dom_sid dom_sid
;
918 if (!lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
919 NULL
, NULL
, NULL
, NULL
)) {
920 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp
->gr_name
));
921 return NT_STATUS_NO_SUCH_GROUP
;
925 id
.type
= ID_TYPE_GID
;
926 if (!pdb_id_to_sid(&id
, &dom_sid
)) {
927 return NT_STATUS_UNSUCCESSFUL
;
930 return pdb_delete_group_mapping_entry(dom_sid
);
933 static int net_sam_unmapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
938 if (argc
!= 1 || c
->display_usage
) {
939 d_fprintf(stderr
, "%s\n%s",
941 _("net sam unmapunixgroup <name>\n"));
945 grp
= getgrnam(argv
[0]);
947 d_fprintf(stderr
, _("Could not find mapping for group %s.\n"),
952 status
= unmap_unix_group(grp
);
954 if (!NT_STATUS_IS_OK(status
)) {
955 d_fprintf(stderr
, _("Unmapping group %s failed with %s.\n"),
956 argv
[0], nt_errstr(status
));
960 d_printf(_("Unmapped unix group %s.\n"), argv
[0]);
966 * Create a domain group
969 static int net_sam_createdomaingroup(struct net_context
*c
, int argc
,
975 if (argc
!= 1 || c
->display_usage
) {
976 d_fprintf(stderr
, "%s\n%s",
978 _("net sam createdomaingroup <name>\n"));
982 status
= pdb_create_dom_group(talloc_tos(), argv
[0], &rid
);
984 if (!NT_STATUS_IS_OK(status
)) {
985 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
986 argv
[0], nt_errstr(status
));
990 d_printf(_("Created domain group %s with RID %d\n"), argv
[0], rid
);
996 * Delete a domain group
999 static int net_sam_deletedomaingroup(struct net_context
*c
, int argc
,
1004 enum lsa_SidType type
;
1005 const char *dom
, *name
;
1008 if (argc
!= 1 || c
->display_usage
) {
1009 d_fprintf(stderr
, "%s\n%s",
1011 _("net sam deletelocalgroup <name>\n"));
1015 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1016 &dom
, &name
, &sid
, &type
)) {
1017 d_fprintf(stderr
, _("Could not find %s.\n"), argv
[0]);
1021 if (type
!= SID_NAME_DOM_GRP
) {
1022 d_fprintf(stderr
, _("%s is a %s, not a domain group.\n"),
1023 argv
[0], sid_type_lookup(type
));
1027 sid_peek_rid(&sid
, &rid
);
1029 status
= pdb_delete_dom_group(talloc_tos(), rid
);
1031 if (!NT_STATUS_IS_OK(status
)) {
1032 d_fprintf(stderr
,_("Deleting domain group %s failed with %s\n"),
1033 argv
[0], nt_errstr(status
));
1037 d_printf(_("Deleted domain group %s.\n"), argv
[0]);
1043 * Create a local group
1046 static int net_sam_createlocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1051 if (argc
!= 1 || c
->display_usage
) {
1052 d_fprintf(stderr
, "%s\n%s",
1054 _("net sam createlocalgroup <name>\n"));
1058 if (!winbind_ping()) {
1059 d_fprintf(stderr
, _("winbind seems not to run. "
1060 "createlocalgroup only works when winbind runs.\n"));
1064 status
= pdb_create_alias(argv
[0], &rid
);
1066 if (!NT_STATUS_IS_OK(status
)) {
1067 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1068 argv
[0], nt_errstr(status
));
1072 d_printf(_("Created local group %s with RID %d\n"), argv
[0], rid
);
1078 * Delete a local group
1081 static int net_sam_deletelocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1084 enum lsa_SidType type
;
1085 const char *dom
, *name
;
1088 if (argc
!= 1 || c
->display_usage
) {
1089 d_fprintf(stderr
, "%s\n%s",
1091 _("net sam deletelocalgroup <name>\n"));
1095 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1096 &dom
, &name
, &sid
, &type
)) {
1097 d_fprintf(stderr
,_("Could not find %s.\n"), argv
[0]);
1101 if (type
!= SID_NAME_ALIAS
) {
1102 d_fprintf(stderr
, _("%s is a %s, not a local group.\n"),argv
[0],
1103 sid_type_lookup(type
));
1107 status
= pdb_delete_alias(&sid
);
1109 if (!NT_STATUS_IS_OK(status
)) {
1110 d_fprintf(stderr
, _("Deleting local group %s failed with %s\n"),
1111 argv
[0], nt_errstr(status
));
1115 d_printf(_("Deleted local group %s.\n"), argv
[0]);
1121 * Create a builtin group
1124 static int net_sam_createbuiltingroup(struct net_context
*c
, int argc
, const char **argv
)
1128 enum lsa_SidType type
;
1132 if (argc
!= 1 || c
->display_usage
) {
1133 d_fprintf(stderr
, "%s\n%s",
1135 _("net sam createbuiltingroup <name>\n"));
1139 if (!winbind_ping()) {
1140 d_fprintf(stderr
, _("winbind seems not to run. "
1141 "createbuiltingroup only works when winbind "
1146 /* validate the name and get the group */
1148 fstrcpy( groupname
, "BUILTIN\\" );
1149 fstrcat( groupname
, argv
[0] );
1151 if ( !lookup_name(talloc_tos(), groupname
, LOOKUP_NAME_ALL
, NULL
,
1152 NULL
, &sid
, &type
)) {
1153 d_fprintf(stderr
, _("%s is not a BUILTIN group\n"), argv
[0]);
1157 if ( !sid_peek_rid( &sid
, &rid
) ) {
1158 d_fprintf(stderr
, _("Failed to get RID for %s\n"), argv
[0]);
1162 status
= pdb_create_builtin(rid
);
1164 if (!NT_STATUS_IS_OK(status
)) {
1165 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1166 argv
[0], nt_errstr(status
));
1170 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv
[0], rid
);
1176 * Add a group member
1179 static int net_sam_addmem(struct net_context
*c
, int argc
, const char **argv
)
1181 const char *groupdomain
, *groupname
, *memberdomain
, *membername
;
1182 struct dom_sid group
, member
;
1183 enum lsa_SidType grouptype
, membertype
;
1186 if (argc
!= 2 || c
->display_usage
) {
1187 d_fprintf(stderr
, "%s\n%s",
1189 _("net sam addmem <group> <member>\n"));
1193 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1194 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1195 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1199 /* check to see if the member to be added is a name or a SID */
1201 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1202 &memberdomain
, &membername
, &member
, &membertype
))
1204 /* try it as a SID */
1206 if ( !string_to_sid( &member
, argv
[1] ) ) {
1207 d_fprintf(stderr
, _("Could not find member %s\n"),
1212 if ( !lookup_sid(talloc_tos(), &member
, &memberdomain
,
1213 &membername
, &membertype
) )
1215 d_fprintf(stderr
, _("Could not resolve SID %s\n"),
1221 if ((grouptype
== SID_NAME_ALIAS
) || (grouptype
== SID_NAME_WKN_GRP
)) {
1222 if ((membertype
!= SID_NAME_USER
) &&
1223 (membertype
!= SID_NAME_DOM_GRP
)) {
1224 d_fprintf(stderr
, _("%s is a local group, only users "
1225 "and domain groups can be added.\n"
1226 "%s is a %s\n"), argv
[0], argv
[1],
1227 sid_type_lookup(membertype
));
1230 status
= pdb_add_aliasmem(&group
, &member
);
1232 if (!NT_STATUS_IS_OK(status
)) {
1233 d_fprintf(stderr
, _("Adding local group member failed "
1234 "with %s\n"), nt_errstr(status
));
1237 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1238 uint32_t grouprid
, memberrid
;
1240 sid_peek_rid(&group
, &grouprid
);
1241 sid_peek_rid(&member
, &memberrid
);
1243 status
= pdb_add_groupmem(talloc_tos(), grouprid
, memberrid
);
1244 if (!NT_STATUS_IS_OK(status
)) {
1245 d_fprintf(stderr
, _("Adding domain group member failed "
1246 "with %s\n"), nt_errstr(status
));
1250 d_fprintf(stderr
, _("Can only add members to local groups so "
1251 "far, %s is a %s\n"), argv
[0],
1252 sid_type_lookup(grouptype
));
1256 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain
, membername
,
1257 groupdomain
, groupname
);
1263 * Delete a group member
1266 static int net_sam_delmem(struct net_context
*c
, int argc
, const char **argv
)
1268 const char *groupdomain
, *groupname
;
1269 const char *memberdomain
= NULL
;
1270 const char *membername
= NULL
;
1271 struct dom_sid group
, member
;
1272 enum lsa_SidType grouptype
;
1275 if (argc
!= 2 || c
->display_usage
) {
1276 d_fprintf(stderr
,"%s\n%s",
1278 _("net sam delmem <group> <member>\n"));
1282 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1283 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1284 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1288 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1289 &memberdomain
, &membername
, &member
, NULL
)) {
1290 if (!string_to_sid(&member
, argv
[1])) {
1291 d_fprintf(stderr
, _("Could not find member %s\n"),
1297 if ((grouptype
== SID_NAME_ALIAS
) ||
1298 (grouptype
== SID_NAME_WKN_GRP
)) {
1299 status
= pdb_del_aliasmem(&group
, &member
);
1301 if (!NT_STATUS_IS_OK(status
)) {
1302 d_fprintf(stderr
,_("Deleting local group member failed "
1303 "with %s\n"), nt_errstr(status
));
1306 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1307 uint32_t grouprid
, memberrid
;
1309 sid_peek_rid(&group
, &grouprid
);
1310 sid_peek_rid(&member
, &memberrid
);
1312 status
= pdb_del_groupmem(talloc_tos(), grouprid
, memberrid
);
1313 if (!NT_STATUS_IS_OK(status
)) {
1314 d_fprintf(stderr
, _("Deleting domain group member "
1315 "failed with %s\n"), nt_errstr(status
));
1319 d_fprintf(stderr
, _("Can only delete members from local groups "
1320 "so far, %s is a %s\n"), argv
[0],
1321 sid_type_lookup(grouptype
));
1325 if (membername
!= NULL
) {
1326 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1327 memberdomain
, membername
, groupdomain
, groupname
);
1329 d_printf(_("Deleted %s from %s\\%s\n"),
1330 sid_string_tos(&member
), groupdomain
, groupname
);
1337 * List group members
1340 static int net_sam_listmem(struct net_context
*c
, int argc
, const char **argv
)
1342 const char *groupdomain
, *groupname
;
1343 struct dom_sid group
;
1344 struct dom_sid
*members
= NULL
;
1345 size_t i
, num_members
= 0;
1346 enum lsa_SidType grouptype
;
1349 if (argc
!= 1 || c
->display_usage
) {
1350 d_fprintf(stderr
, "%s\n%s",
1352 _("net sam listmem <group>\n"));
1356 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1357 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1358 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1362 if ((grouptype
== SID_NAME_ALIAS
) ||
1363 (grouptype
== SID_NAME_WKN_GRP
)) {
1364 status
= pdb_enum_aliasmem(&group
, talloc_tos(), &members
,
1366 if (!NT_STATUS_IS_OK(status
)) {
1367 d_fprintf(stderr
, _("Listing group members failed with "
1368 "%s\n"), nt_errstr(status
));
1371 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1374 status
= pdb_enum_group_members(talloc_tos(), &group
,
1375 &rids
, &num_members
);
1376 if (!NT_STATUS_IS_OK(status
)) {
1377 d_fprintf(stderr
, _("Listing group members failed with "
1378 "%s\n"), nt_errstr(status
));
1382 members
= talloc_array(talloc_tos(), struct dom_sid
,
1384 if (members
== NULL
) {
1389 for (i
=0; i
<num_members
; i
++) {
1390 sid_compose(&members
[i
], get_global_sam_sid(),
1395 d_fprintf(stderr
,_("Can only list local group members so far.\n"
1396 "%s is a %s\n"), argv
[0], sid_type_lookup(grouptype
));
1400 d_printf(_("%s\\%s has %u members\n"), groupdomain
, groupname
,
1401 (unsigned int)num_members
);
1402 for (i
=0; i
<num_members
; i
++) {
1403 const char *dom
, *name
;
1404 if (lookup_sid(talloc_tos(), &members
[i
], &dom
, &name
, NULL
)) {
1405 d_printf(" %s\\%s\n", dom
, name
);
1407 d_printf(" %s\n", sid_string_tos(&members
[i
]));
1411 TALLOC_FREE(members
);
1419 static int net_sam_do_list(struct net_context
*c
, int argc
, const char **argv
,
1420 struct pdb_search
*search
, const char *what
)
1422 bool verbose
= (argc
== 1);
1424 if ((argc
> 1) || c
->display_usage
||
1425 ((argc
== 1) && !strequal(argv
[0], "verbose"))) {
1426 d_fprintf(stderr
, "%s\n", _("Usage:"));
1427 d_fprintf(stderr
, _("net sam list %s [verbose]\n"), what
);
1431 if (search
== NULL
) {
1432 d_fprintf(stderr
, _("Could not start search\n"));
1437 struct samr_displayentry entry
;
1438 if (!search
->next_entry(search
, &entry
)) {
1442 d_printf("%s:%d:%s\n",
1447 d_printf("%s\n", entry
.account_name
);
1451 TALLOC_FREE(search
);
1455 static int net_sam_list_users(struct net_context
*c
, int argc
,
1458 return net_sam_do_list(c
, argc
, argv
,
1459 pdb_search_users(talloc_tos(), ACB_NORMAL
),
1463 static int net_sam_list_groups(struct net_context
*c
, int argc
,
1466 return net_sam_do_list(c
, argc
, argv
, pdb_search_groups(talloc_tos()),
1470 static int net_sam_list_localgroups(struct net_context
*c
, int argc
,
1473 return net_sam_do_list(c
, argc
, argv
,
1474 pdb_search_aliases(talloc_tos(),
1475 get_global_sam_sid()),
1479 static int net_sam_list_builtin(struct net_context
*c
, int argc
,
1482 return net_sam_do_list(c
, argc
, argv
,
1483 pdb_search_aliases(talloc_tos(),
1484 &global_sid_Builtin
),
1488 static int net_sam_list_workstations(struct net_context
*c
, int argc
,
1491 return net_sam_do_list(c
, argc
, argv
,
1492 pdb_search_users(talloc_tos(), ACB_WSTRUST
),
1500 static int net_sam_list(struct net_context
*c
, int argc
, const char **argv
)
1502 struct functable func
[] = {
1506 NET_TRANSPORT_LOCAL
,
1507 N_("List SAM users"),
1508 N_("net sam list users\n"
1513 net_sam_list_groups
,
1514 NET_TRANSPORT_LOCAL
,
1515 N_("List SAM groups"),
1516 N_("net sam list groups\n"
1521 net_sam_list_localgroups
,
1522 NET_TRANSPORT_LOCAL
,
1523 N_("List SAM local groups"),
1524 N_("net sam list localgroups\n"
1525 " List SAM local groups")
1529 net_sam_list_builtin
,
1530 NET_TRANSPORT_LOCAL
,
1531 N_("List builtin groups"),
1532 N_("net sam list builtin\n"
1533 " List builtin groups")
1537 net_sam_list_workstations
,
1538 NET_TRANSPORT_LOCAL
,
1539 N_("List domain member workstations"),
1540 N_("net sam list workstations\n"
1541 " List domain member workstations")
1543 {NULL
, NULL
, 0, NULL
, NULL
}
1546 return net_run_function(c
, argc
, argv
, "net sam list", func
);
1550 * Show details of SAM entries
1553 static int net_sam_show(struct net_context
*c
, int argc
, const char **argv
)
1556 enum lsa_SidType type
;
1557 const char *dom
, *name
;
1559 if (argc
!= 1 || c
->display_usage
) {
1560 d_fprintf(stderr
, "%s\n%s",
1562 _("net sam show <name>\n"));
1566 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1567 &dom
, &name
, &sid
, &type
)) {
1568 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
1572 d_printf(_("%s\\%s is a %s with SID %s\n"), dom
, name
,
1573 sid_type_lookup(type
), sid_string_tos(&sid
));
1581 * Init an LDAP tree with default users and Groups
1582 * if ldapsam:editposix is enabled
1585 static int net_sam_provision(struct net_context
*c
, int argc
, const char **argv
)
1589 char *ldap_uri
= NULL
;
1591 struct smbldap_state
*state
= NULL
;
1592 GROUP_MAP
*gmap
= NULL
;
1593 struct dom_sid gsid
;
1594 gid_t domusers_gid
= -1;
1595 gid_t domadmins_gid
= -1;
1596 struct samu
*samuser
;
1598 bool is_ipa
= false;
1599 char *bind_dn
= NULL
;
1600 char *bind_secret
= NULL
;
1603 if (c
->display_usage
) {
1605 "net sam provision\n"
1608 _("Init an LDAP tree with default users/groups"));
1612 tc
= talloc_new(NULL
);
1614 d_fprintf(stderr
, _("Out of Memory!\n"));
1618 if ((ldap_bk
= talloc_strdup(tc
, lp_passdb_backend())) == NULL
) {
1619 d_fprintf(stderr
, _("talloc failed\n"));
1623 p
= strchr(ldap_bk
, ':');
1626 ldap_uri
= talloc_strdup(tc
, p
+1);
1627 trim_char(ldap_uri
, ' ', ' ');
1630 trim_char(ldap_bk
, ' ', ' ');
1632 if (strcmp(ldap_bk
, "IPA_ldapsam") == 0 ) {
1636 if (strcmp(ldap_bk
, "ldapsam") != 0 && !is_ipa
) {
1638 _("Provisioning works only with ldapsam backend\n"));
1642 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1643 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1645 d_fprintf(stderr
, _("Provisioning works only if ldapsam:trusted"
1646 " and ldapsam:editposix are enabled.\n"));
1650 if (!is_ipa
&& !winbind_ping()) {
1651 d_fprintf(stderr
, _("winbind seems not to run. Provisioning "
1652 "LDAP only works when winbind runs.\n"));
1656 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
1657 d_fprintf(stderr
, _("Failed to retrieve LDAP password from secrets.tdb\n"));
1661 status
= smbldap_init(tc
, NULL
, ldap_uri
, false, bind_dn
, bind_secret
, &state
);
1663 memset(bind_secret
, '\0', strlen(bind_secret
));
1664 SAFE_FREE(bind_secret
);
1667 if (!NT_STATUS_IS_OK(status
)) {
1668 d_fprintf(stderr
, _("Unable to connect to the LDAP server.\n"));
1672 d_printf(_("Checking for Domain Users group.\n"));
1674 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_USERS
);
1676 gmap
= talloc_zero(tc
, GROUP_MAP
);
1678 d_printf(_("Out of memory!\n"));
1682 if (!pdb_getgrsid(gmap
, gsid
)) {
1683 LDAPMod
**mods
= NULL
;
1691 d_printf(_("Adding the Domain Users group.\n"));
1693 /* lets allocate a new groupid for this group */
1697 if (!winbind_allocate_gid(&domusers_gid
)) {
1698 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1699 "create Domain Users group!\n"));
1704 uname
= talloc_strdup(tc
, "domusers");
1705 wname
= talloc_strdup(tc
, "Domain Users");
1706 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domusers",
1707 lp_ldap_group_suffix(talloc_tos()));
1708 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domusers_gid
);
1709 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1711 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1712 d_fprintf(stderr
, "Out of Memory!\n");
1716 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1717 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1719 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1720 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1721 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1723 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1724 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1725 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1726 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1727 sid_string_talloc(tc
, &gsid
));
1728 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1730 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1732 rc
= smbldap_add(state
, dn
, mods
);
1734 if (rc
!= LDAP_SUCCESS
) {
1735 d_fprintf(stderr
, _("Failed to add Domain Users group "
1736 "to ldap directory\n"));
1740 if (!pdb_getgrsid(gmap
, gsid
)) {
1741 d_fprintf(stderr
, _("Failed to read just "
1742 "created domain group.\n"));
1745 domusers_gid
= gmap
->gid
;
1749 domusers_gid
= gmap
->gid
;
1750 d_printf(_("found!\n"));
1755 d_printf(_("Checking for Domain Admins group.\n"));
1757 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_ADMINS
);
1759 if (!pdb_getgrsid(gmap
, gsid
)) {
1760 LDAPMod
**mods
= NULL
;
1768 d_printf(_("Adding the Domain Admins group.\n"));
1770 /* lets allocate a new groupid for this group */
1772 domadmins_gid
= 999;
1774 if (!winbind_allocate_gid(&domadmins_gid
)) {
1775 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1776 "create Domain Admins group!\n"));
1781 uname
= talloc_strdup(tc
, "domadmins");
1782 wname
= talloc_strdup(tc
, "Domain Admins");
1783 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domadmins",
1784 lp_ldap_group_suffix(talloc_tos()));
1785 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1786 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1788 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1789 d_fprintf(stderr
, _("Out of Memory!\n"));
1793 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1794 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1796 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1797 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1798 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1800 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1801 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1802 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1803 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1804 sid_string_talloc(tc
, &gsid
));
1805 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1807 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1809 rc
= smbldap_add(state
, dn
, mods
);
1811 if (rc
!= LDAP_SUCCESS
) {
1812 d_fprintf(stderr
, _("Failed to add Domain Admins group "
1813 "to ldap directory\n"));
1817 if (!pdb_getgrsid(gmap
, gsid
)) {
1818 d_fprintf(stderr
, _("Failed to read just "
1819 "created domain group.\n"));
1822 domadmins_gid
= gmap
->gid
;
1826 domadmins_gid
= gmap
->gid
;
1827 d_printf(_("found!\n"));
1832 d_printf(_("Check for Administrator account.\n"));
1834 samuser
= samu_new(tc
);
1836 d_fprintf(stderr
, _("Out of Memory!\n"));
1840 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1841 LDAPMod
**mods
= NULL
;
1853 d_printf(_("Adding the Administrator user.\n"));
1855 if (domadmins_gid
== -1) {
1857 _("Can't create Administrator user, Domain "
1858 "Admins group not available!\n"));
1865 if (!winbind_allocate_uid(&uid
)) {
1867 _("Unable to allocate a new uid to create "
1868 "the Administrator user!\n"));
1873 name
= talloc_strdup(tc
, "Administrator");
1874 dn
= talloc_asprintf(tc
, "uid=Administrator,%s",
1875 lp_ldap_user_suffix(talloc_tos()));
1876 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)uid
);
1877 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1878 dir
= talloc_sub_specified(tc
, lp_template_homedir(),
1881 get_global_sam_name(),
1882 uid
, domadmins_gid
);
1883 shell
= talloc_sub_specified(tc
, lp_template_shell(),
1886 get_global_sam_name(),
1887 uid
, domadmins_gid
);
1889 if (!name
|| !dn
|| !uidstr
|| !gidstr
|| !dir
|| !shell
) {
1890 d_fprintf(stderr
, _("Out of Memory!\n"));
1894 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR
);
1896 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1897 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1898 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1900 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
1901 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
1902 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
1903 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
1904 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
1905 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
1906 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", name
);
1907 princ
= talloc_asprintf(tc
, "%s@%s", name
, lp_realm());
1909 d_fprintf(stderr
, _("Out of Memory!\n"));
1912 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "krbPrincipalName", princ
);
1914 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", name
);
1915 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
1916 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
1917 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1918 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1919 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", dir
);
1920 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
1921 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
1922 sid_string_talloc(tc
, &sid
));
1923 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
1924 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
1925 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1927 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1929 rc
= smbldap_add(state
, dn
, mods
);
1931 if (rc
!= LDAP_SUCCESS
) {
1932 d_fprintf(stderr
, _("Failed to add Administrator user "
1933 "to ldap directory\n"));
1937 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1938 d_fprintf(stderr
, _("Failed to read just "
1939 "created user.\n"));
1944 d_printf(_("found!\n"));
1947 d_printf(_("Checking for Guest user.\n"));
1949 samuser
= samu_new(tc
);
1951 d_fprintf(stderr
, _("Out of Memory!\n"));
1955 if (!pdb_getsampwnam(samuser
, lp_guest_account())) {
1956 LDAPMod
**mods
= NULL
;
1963 d_printf(_("Adding the Guest user.\n"));
1965 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_GUEST
);
1967 pwd
= Get_Pwnam_alloc(tc
, lp_guest_account());
1970 if (domusers_gid
== -1) {
1972 _("Can't create Guest user, Domain "
1973 "Users group not available!\n"));
1976 if ((pwd
= talloc(tc
, struct passwd
)) == NULL
) {
1977 d_fprintf(stderr
, _("talloc failed\n"));
1980 pwd
->pw_name
= talloc_strdup(pwd
, lp_guest_account());
1985 if (!winbind_allocate_uid(&(pwd
->pw_uid
))) {
1987 _("Unable to allocate a new uid to "
1988 "create the Guest user!\n"));
1992 pwd
->pw_gid
= domusers_gid
;
1993 pwd
->pw_dir
= talloc_strdup(tc
, "/");
1994 pwd
->pw_shell
= talloc_strdup(tc
, "/bin/false");
1995 if (!pwd
->pw_dir
|| !pwd
->pw_shell
) {
1996 d_fprintf(stderr
, _("Out of Memory!\n"));
2001 dn
= talloc_asprintf(tc
, "uid=%s,%s", pwd
->pw_name
,
2002 lp_ldap_user_suffix (talloc_tos()));
2003 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_uid
);
2004 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2005 if (!dn
|| !uidstr
|| !gidstr
) {
2006 d_fprintf(stderr
, _("Out of Memory!\n"));
2010 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
2011 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
2012 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
2014 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
2015 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
2016 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
2017 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
2018 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
2019 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
2020 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", pwd
->pw_name
);
2022 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", pwd
->pw_name
);
2023 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", pwd
->pw_name
);
2024 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", pwd
->pw_name
);
2025 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
2026 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2027 if ((pwd
->pw_dir
!= NULL
) && (pwd
->pw_dir
[0] != '\0')) {
2028 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", pwd
->pw_dir
);
2030 if ((pwd
->pw_shell
!= NULL
) && (pwd
->pw_shell
[0] != '\0')) {
2031 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", pwd
->pw_shell
);
2033 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
2034 sid_string_talloc(tc
, &sid
));
2035 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
2036 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
2037 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
2039 smbldap_talloc_autofree_ldapmod(tc
, mods
);
2041 rc
= smbldap_add(state
, dn
, mods
);
2043 if (rc
!= LDAP_SUCCESS
) {
2044 d_fprintf(stderr
, _("Failed to add Guest user to "
2045 "ldap directory\n"));
2049 if (!pdb_getsampwnam(samuser
, lp_guest_account())) {
2050 d_fprintf(stderr
, _("Failed to read just "
2051 "created user.\n"));
2056 d_printf(_("found!\n"));
2059 d_printf(_("Checking Guest's group.\n"));
2061 pwd
= Get_Pwnam_alloc(tc
, lp_guest_account());
2064 _("Failed to find just created Guest account!\n"
2065 " Is nss properly configured?!\n"));
2069 if (pwd
->pw_gid
== domusers_gid
) {
2070 d_printf(_("found!\n"));
2074 if (!pdb_getgrgid(gmap
, pwd
->pw_gid
)) {
2075 LDAPMod
**mods
= NULL
;
2083 d_printf(_("Adding the Domain Guests group.\n"));
2085 uname
= talloc_strdup(tc
, "domguests");
2086 wname
= talloc_strdup(tc
, "Domain Guests");
2087 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domguests",
2088 lp_ldap_group_suffix(talloc_tos()));
2089 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2090 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
2092 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
2093 d_fprintf(stderr
, _("Out of Memory!\n"));
2097 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_GUESTS
);
2099 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
2100 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
2102 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
2103 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
2104 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
2106 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
2107 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
2108 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2109 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
2110 sid_string_talloc(tc
, &gsid
));
2111 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
2113 smbldap_talloc_autofree_ldapmod(tc
, mods
);
2115 rc
= smbldap_add(state
, dn
, mods
);
2117 if (rc
!= LDAP_SUCCESS
) {
2119 _("Failed to add Domain Guests group to ldap "
2123 d_printf(_("found!\n"));
2138 /***********************************************************
2139 migrated functionality from smbgroupedit
2140 **********************************************************/
2141 int net_sam(struct net_context
*c
, int argc
, const char **argv
)
2143 struct functable func
[] = {
2145 "createbuiltingroup",
2146 net_sam_createbuiltingroup
,
2147 NET_TRANSPORT_LOCAL
,
2148 N_("Create a new BUILTIN group"),
2149 N_("net sam createbuiltingroup\n"
2150 " Create a new BUILTIN group")
2154 net_sam_createlocalgroup
,
2155 NET_TRANSPORT_LOCAL
,
2156 N_("Create a new local group"),
2157 N_("net sam createlocalgroup\n"
2158 " Create a new local group")
2161 "createdomaingroup",
2162 net_sam_createdomaingroup
,
2163 NET_TRANSPORT_LOCAL
,
2164 N_("Create a new group"),
2165 N_("net sam createdomaingroup\n"
2166 " Create a new group")
2170 net_sam_deletelocalgroup
,
2171 NET_TRANSPORT_LOCAL
,
2172 N_("Delete an existing local group"),
2173 N_("net sam deletelocalgroup\n"
2174 " Delete an existing local group")
2177 "deletedomaingroup",
2178 net_sam_deletedomaingroup
,
2179 NET_TRANSPORT_LOCAL
,
2180 N_("Delete a domain group"),
2181 N_("net sam deletedomaingroup\n"
2186 net_sam_mapunixgroup
,
2187 NET_TRANSPORT_LOCAL
,
2188 N_("Map a unix group to a domain group"),
2189 N_("net sam mapunixgroup\n"
2190 " Map a unix group to a domain group")
2194 net_sam_unmapunixgroup
,
2195 NET_TRANSPORT_LOCAL
,
2196 N_("Remove a group mapping of an unix group to a "
2198 N_("net sam unmapunixgroup\n"
2199 " Remove a group mapping of an unix group to a "
2205 NET_TRANSPORT_LOCAL
,
2206 N_("Add a member to a group"),
2207 N_("net sam addmem\n"
2208 " Add a member to a group")
2213 NET_TRANSPORT_LOCAL
,
2214 N_("Delete a member from a group"),
2215 N_("net sam delmem\n"
2216 " Delete a member from a group")
2221 NET_TRANSPORT_LOCAL
,
2222 N_("List group members"),
2223 N_("net sam listmem\n"
2224 " List group members")
2229 NET_TRANSPORT_LOCAL
,
2230 N_("List users, groups and local groups"),
2232 " List users, groups and local groups")
2237 NET_TRANSPORT_LOCAL
,
2238 N_("Show details of a SAM entry"),
2240 " Show details of a SAM entry")
2245 NET_TRANSPORT_LOCAL
,
2246 N_("Set details of a SAM account"),
2248 " Set details of a SAM account")
2253 NET_TRANSPORT_LOCAL
,
2254 N_("Set account policies"),
2255 N_("net sam policy\n"
2256 " Set account policies")
2261 NET_TRANSPORT_LOCAL
,
2262 N_("Manipulate user privileges"),
2263 N_("net sam rights\n"
2264 " Manipulate user privileges")
2270 NET_TRANSPORT_LOCAL
,
2271 N_("Provision a clean user database"),
2272 N_("net sam privison\n"
2273 " Provision a clear user database")
2276 {NULL
, NULL
, 0, NULL
, NULL
}
2279 if (getuid() != 0) {
2280 d_fprintf(stderr
, _("You are not root, most things won't "
2284 return net_run_function(c
, argc
, argv
, "net sam", func
);