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
;
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_ALIAS
) &&
1224 (membertype
!= SID_NAME_DOM_GRP
)) {
1225 d_fprintf(stderr
, _("Can't add %s: only users, domain "
1226 "groups and domain local groups "
1227 "can be added. %s is a %s\n"),
1229 sid_type_lookup(membertype
));
1232 status
= pdb_add_aliasmem(&group
, &member
);
1234 if (!NT_STATUS_IS_OK(status
)) {
1235 d_fprintf(stderr
, _("Adding local group member failed "
1236 "with %s\n"), nt_errstr(status
));
1239 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1240 uint32_t grouprid
, memberrid
;
1242 sid_peek_rid(&group
, &grouprid
);
1243 sid_peek_rid(&member
, &memberrid
);
1245 status
= pdb_add_groupmem(talloc_tos(), grouprid
, memberrid
);
1246 if (!NT_STATUS_IS_OK(status
)) {
1247 d_fprintf(stderr
, _("Adding domain group member failed "
1248 "with %s\n"), nt_errstr(status
));
1252 d_fprintf(stderr
, _("Can only add members to local groups so "
1253 "far, %s is a %s\n"), argv
[0],
1254 sid_type_lookup(grouptype
));
1258 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain
, membername
,
1259 groupdomain
, groupname
);
1265 * Delete a group member
1268 static int net_sam_delmem(struct net_context
*c
, int argc
, const char **argv
)
1270 const char *groupdomain
, *groupname
;
1271 const char *memberdomain
= NULL
;
1272 const char *membername
= NULL
;
1273 struct dom_sid group
, member
;
1274 enum lsa_SidType grouptype
;
1277 if (argc
!= 2 || c
->display_usage
) {
1278 d_fprintf(stderr
,"%s\n%s",
1280 _("net sam delmem <group> <member>\n"));
1284 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1285 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1286 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1290 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1291 &memberdomain
, &membername
, &member
, NULL
)) {
1292 if (!string_to_sid(&member
, argv
[1])) {
1293 d_fprintf(stderr
, _("Could not find member %s\n"),
1299 if ((grouptype
== SID_NAME_ALIAS
) ||
1300 (grouptype
== SID_NAME_WKN_GRP
)) {
1301 status
= pdb_del_aliasmem(&group
, &member
);
1303 if (!NT_STATUS_IS_OK(status
)) {
1304 d_fprintf(stderr
,_("Deleting local group member failed "
1305 "with %s\n"), nt_errstr(status
));
1308 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1309 uint32_t grouprid
, memberrid
;
1311 sid_peek_rid(&group
, &grouprid
);
1312 sid_peek_rid(&member
, &memberrid
);
1314 status
= pdb_del_groupmem(talloc_tos(), grouprid
, memberrid
);
1315 if (!NT_STATUS_IS_OK(status
)) {
1316 d_fprintf(stderr
, _("Deleting domain group member "
1317 "failed with %s\n"), nt_errstr(status
));
1321 d_fprintf(stderr
, _("Can only delete members from local groups "
1322 "so far, %s is a %s\n"), argv
[0],
1323 sid_type_lookup(grouptype
));
1327 if (membername
!= NULL
) {
1328 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1329 memberdomain
, membername
, groupdomain
, groupname
);
1331 d_printf(_("Deleted %s from %s\\%s\n"),
1332 sid_string_tos(&member
), groupdomain
, groupname
);
1339 * List group members
1342 static int net_sam_listmem(struct net_context
*c
, int argc
, const char **argv
)
1344 const char *groupdomain
, *groupname
;
1345 struct dom_sid group
;
1346 struct dom_sid
*members
= NULL
;
1347 size_t i
, num_members
= 0;
1348 enum lsa_SidType grouptype
;
1351 if (argc
!= 1 || c
->display_usage
) {
1352 d_fprintf(stderr
, "%s\n%s",
1354 _("net sam listmem <group>\n"));
1358 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1359 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1360 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1364 if ((grouptype
== SID_NAME_ALIAS
) ||
1365 (grouptype
== SID_NAME_WKN_GRP
)) {
1366 status
= pdb_enum_aliasmem(&group
, talloc_tos(), &members
,
1368 if (!NT_STATUS_IS_OK(status
)) {
1369 d_fprintf(stderr
, _("Listing group members failed with "
1370 "%s\n"), nt_errstr(status
));
1373 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1376 status
= pdb_enum_group_members(talloc_tos(), &group
,
1377 &rids
, &num_members
);
1378 if (!NT_STATUS_IS_OK(status
)) {
1379 d_fprintf(stderr
, _("Listing group members failed with "
1380 "%s\n"), nt_errstr(status
));
1384 members
= talloc_array(talloc_tos(), struct dom_sid
,
1386 if (members
== NULL
) {
1391 for (i
=0; i
<num_members
; i
++) {
1392 sid_compose(&members
[i
], get_global_sam_sid(),
1397 d_fprintf(stderr
,_("Can only list local group members so far.\n"
1398 "%s is a %s\n"), argv
[0], sid_type_lookup(grouptype
));
1402 d_printf(_("%s\\%s has %u members\n"), groupdomain
, groupname
,
1403 (unsigned int)num_members
);
1404 for (i
=0; i
<num_members
; i
++) {
1405 const char *dom
, *name
;
1406 if (lookup_sid(talloc_tos(), &members
[i
], &dom
, &name
, NULL
)) {
1407 d_printf(" %s\\%s\n", dom
, name
);
1409 d_printf(" %s\n", sid_string_tos(&members
[i
]));
1413 TALLOC_FREE(members
);
1421 static int net_sam_do_list(struct net_context
*c
, int argc
, const char **argv
,
1422 struct pdb_search
*search
, const char *what
)
1424 bool verbose
= (argc
== 1);
1426 if ((argc
> 1) || c
->display_usage
||
1427 ((argc
== 1) && !strequal(argv
[0], "verbose"))) {
1428 d_fprintf(stderr
, "%s\n", _("Usage:"));
1429 d_fprintf(stderr
, _("net sam list %s [verbose]\n"), what
);
1433 if (search
== NULL
) {
1434 d_fprintf(stderr
, _("Could not start search\n"));
1439 struct samr_displayentry entry
;
1440 if (!search
->next_entry(search
, &entry
)) {
1444 d_printf("%s:%d:%s\n",
1449 d_printf("%s\n", entry
.account_name
);
1453 TALLOC_FREE(search
);
1457 static int net_sam_list_users(struct net_context
*c
, int argc
,
1460 return net_sam_do_list(c
, argc
, argv
,
1461 pdb_search_users(talloc_tos(), ACB_NORMAL
),
1465 static int net_sam_list_groups(struct net_context
*c
, int argc
,
1468 return net_sam_do_list(c
, argc
, argv
, pdb_search_groups(talloc_tos()),
1472 static int net_sam_list_localgroups(struct net_context
*c
, int argc
,
1475 return net_sam_do_list(c
, argc
, argv
,
1476 pdb_search_aliases(talloc_tos(),
1477 get_global_sam_sid()),
1481 static int net_sam_list_builtin(struct net_context
*c
, int argc
,
1484 return net_sam_do_list(c
, argc
, argv
,
1485 pdb_search_aliases(talloc_tos(),
1486 &global_sid_Builtin
),
1490 static int net_sam_list_workstations(struct net_context
*c
, int argc
,
1493 return net_sam_do_list(c
, argc
, argv
,
1494 pdb_search_users(talloc_tos(), ACB_WSTRUST
),
1502 static int net_sam_list(struct net_context
*c
, int argc
, const char **argv
)
1504 struct functable func
[] = {
1508 NET_TRANSPORT_LOCAL
,
1509 N_("List SAM users"),
1510 N_("net sam list users\n"
1515 net_sam_list_groups
,
1516 NET_TRANSPORT_LOCAL
,
1517 N_("List SAM groups"),
1518 N_("net sam list groups\n"
1523 net_sam_list_localgroups
,
1524 NET_TRANSPORT_LOCAL
,
1525 N_("List SAM local groups"),
1526 N_("net sam list localgroups\n"
1527 " List SAM local groups")
1531 net_sam_list_builtin
,
1532 NET_TRANSPORT_LOCAL
,
1533 N_("List builtin groups"),
1534 N_("net sam list builtin\n"
1535 " List builtin groups")
1539 net_sam_list_workstations
,
1540 NET_TRANSPORT_LOCAL
,
1541 N_("List domain member workstations"),
1542 N_("net sam list workstations\n"
1543 " List domain member workstations")
1545 {NULL
, NULL
, 0, NULL
, NULL
}
1548 return net_run_function(c
, argc
, argv
, "net sam list", func
);
1552 * Show details of SAM entries
1555 static int net_sam_show(struct net_context
*c
, int argc
, const char **argv
)
1558 enum lsa_SidType type
;
1559 const char *dom
, *name
;
1561 if (argc
!= 1 || c
->display_usage
) {
1562 d_fprintf(stderr
, "%s\n%s",
1564 _("net sam show <name>\n"));
1568 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1569 &dom
, &name
, &sid
, &type
)) {
1570 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
1574 d_printf(_("%s\\%s is a %s with SID %s\n"), dom
, name
,
1575 sid_type_lookup(type
), sid_string_tos(&sid
));
1583 * Init an LDAP tree with default users and Groups
1584 * if ldapsam:editposix is enabled
1587 static int net_sam_provision(struct net_context
*c
, int argc
, const char **argv
)
1591 char *ldap_uri
= NULL
;
1593 struct smbldap_state
*state
= NULL
;
1594 GROUP_MAP
*gmap
= NULL
;
1595 struct dom_sid gsid
;
1596 gid_t domusers_gid
= -1;
1597 gid_t domadmins_gid
= -1;
1598 struct samu
*samuser
;
1600 bool is_ipa
= false;
1601 char *bind_dn
= NULL
;
1602 char *bind_secret
= NULL
;
1605 if (c
->display_usage
) {
1607 "net sam provision\n"
1610 _("Init an LDAP tree with default users/groups"));
1614 tc
= talloc_new(NULL
);
1616 d_fprintf(stderr
, _("Out of Memory!\n"));
1620 if ((ldap_bk
= talloc_strdup(tc
, lp_passdb_backend())) == NULL
) {
1621 d_fprintf(stderr
, _("talloc failed\n"));
1625 p
= strchr(ldap_bk
, ':');
1628 ldap_uri
= talloc_strdup(tc
, p
+1);
1629 trim_char(ldap_uri
, ' ', ' ');
1632 trim_char(ldap_bk
, ' ', ' ');
1634 if (strcmp(ldap_bk
, "IPA_ldapsam") == 0 ) {
1638 if (strcmp(ldap_bk
, "ldapsam") != 0 && !is_ipa
) {
1640 _("Provisioning works only with ldapsam backend\n"));
1644 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1645 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1647 d_fprintf(stderr
, _("Provisioning works only if ldapsam:trusted"
1648 " and ldapsam:editposix are enabled.\n"));
1652 if (!is_ipa
&& !winbind_ping()) {
1653 d_fprintf(stderr
, _("winbind seems not to run. Provisioning "
1654 "LDAP only works when winbind runs.\n"));
1658 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
1659 d_fprintf(stderr
, _("Failed to retrieve LDAP password from secrets.tdb\n"));
1663 status
= smbldap_init(tc
, NULL
, ldap_uri
, false, bind_dn
, bind_secret
, &state
);
1665 memset(bind_secret
, '\0', strlen(bind_secret
));
1666 SAFE_FREE(bind_secret
);
1669 if (!NT_STATUS_IS_OK(status
)) {
1670 d_fprintf(stderr
, _("Unable to connect to the LDAP server.\n"));
1674 d_printf(_("Checking for Domain Users group.\n"));
1676 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_USERS
);
1678 gmap
= talloc_zero(tc
, GROUP_MAP
);
1680 d_printf(_("Out of memory!\n"));
1684 if (!pdb_getgrsid(gmap
, gsid
)) {
1685 LDAPMod
**mods
= NULL
;
1693 d_printf(_("Adding the Domain Users group.\n"));
1695 /* lets allocate a new groupid for this group */
1699 if (!winbind_allocate_gid(&domusers_gid
)) {
1700 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1701 "create Domain Users group!\n"));
1706 uname
= talloc_strdup(tc
, "domusers");
1707 wname
= talloc_strdup(tc
, "Domain Users");
1708 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domusers",
1709 lp_ldap_group_suffix(talloc_tos()));
1710 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domusers_gid
);
1711 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1713 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1714 d_fprintf(stderr
, "Out of Memory!\n");
1718 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1719 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1721 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1722 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1723 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1725 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1726 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1727 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1728 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1729 sid_string_talloc(tc
, &gsid
));
1730 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1732 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1734 rc
= smbldap_add(state
, dn
, mods
);
1736 if (rc
!= LDAP_SUCCESS
) {
1737 d_fprintf(stderr
, _("Failed to add Domain Users group "
1738 "to ldap directory\n"));
1742 if (!pdb_getgrsid(gmap
, gsid
)) {
1743 d_fprintf(stderr
, _("Failed to read just "
1744 "created domain group.\n"));
1747 domusers_gid
= gmap
->gid
;
1751 domusers_gid
= gmap
->gid
;
1752 d_printf(_("found!\n"));
1757 d_printf(_("Checking for Domain Admins group.\n"));
1759 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_ADMINS
);
1761 if (!pdb_getgrsid(gmap
, gsid
)) {
1762 LDAPMod
**mods
= NULL
;
1770 d_printf(_("Adding the Domain Admins group.\n"));
1772 /* lets allocate a new groupid for this group */
1774 domadmins_gid
= 999;
1776 if (!winbind_allocate_gid(&domadmins_gid
)) {
1777 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1778 "create Domain Admins group!\n"));
1783 uname
= talloc_strdup(tc
, "domadmins");
1784 wname
= talloc_strdup(tc
, "Domain Admins");
1785 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domadmins",
1786 lp_ldap_group_suffix(talloc_tos()));
1787 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1788 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1790 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1791 d_fprintf(stderr
, _("Out of Memory!\n"));
1795 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1796 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1798 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1799 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1800 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1802 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1803 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1804 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1805 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1806 sid_string_talloc(tc
, &gsid
));
1807 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1809 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1811 rc
= smbldap_add(state
, dn
, mods
);
1813 if (rc
!= LDAP_SUCCESS
) {
1814 d_fprintf(stderr
, _("Failed to add Domain Admins group "
1815 "to ldap directory\n"));
1819 if (!pdb_getgrsid(gmap
, gsid
)) {
1820 d_fprintf(stderr
, _("Failed to read just "
1821 "created domain group.\n"));
1824 domadmins_gid
= gmap
->gid
;
1828 domadmins_gid
= gmap
->gid
;
1829 d_printf(_("found!\n"));
1834 d_printf(_("Check for Administrator account.\n"));
1836 samuser
= samu_new(tc
);
1838 d_fprintf(stderr
, _("Out of Memory!\n"));
1842 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1843 LDAPMod
**mods
= NULL
;
1855 d_printf(_("Adding the Administrator user.\n"));
1857 if (domadmins_gid
== -1) {
1859 _("Can't create Administrator user, Domain "
1860 "Admins group not available!\n"));
1867 if (!winbind_allocate_uid(&uid
)) {
1869 _("Unable to allocate a new uid to create "
1870 "the Administrator user!\n"));
1875 name
= talloc_strdup(tc
, "Administrator");
1876 dn
= talloc_asprintf(tc
, "uid=Administrator,%s",
1877 lp_ldap_user_suffix(talloc_tos()));
1878 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)uid
);
1879 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1880 dir
= talloc_sub_specified(tc
, lp_template_homedir(),
1883 get_global_sam_name(),
1884 uid
, domadmins_gid
);
1885 shell
= talloc_sub_specified(tc
, lp_template_shell(),
1888 get_global_sam_name(),
1889 uid
, domadmins_gid
);
1891 if (!name
|| !dn
|| !uidstr
|| !gidstr
|| !dir
|| !shell
) {
1892 d_fprintf(stderr
, _("Out of Memory!\n"));
1896 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR
);
1898 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1899 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1900 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1902 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
1903 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
1904 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
1905 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
1906 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
1907 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
1908 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", name
);
1909 princ
= talloc_asprintf(tc
, "%s@%s", name
, lp_realm());
1911 d_fprintf(stderr
, _("Out of Memory!\n"));
1914 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "krbPrincipalName", princ
);
1916 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", name
);
1917 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
1918 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
1919 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1920 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1921 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", dir
);
1922 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
1923 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
1924 sid_string_talloc(tc
, &sid
));
1925 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
1926 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
1927 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1929 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1931 rc
= smbldap_add(state
, dn
, mods
);
1933 if (rc
!= LDAP_SUCCESS
) {
1934 d_fprintf(stderr
, _("Failed to add Administrator user "
1935 "to ldap directory\n"));
1939 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1940 d_fprintf(stderr
, _("Failed to read just "
1941 "created user.\n"));
1946 d_printf(_("found!\n"));
1949 d_printf(_("Checking for Guest user.\n"));
1951 samuser
= samu_new(tc
);
1953 d_fprintf(stderr
, _("Out of Memory!\n"));
1957 if (!pdb_getsampwnam(samuser
, lp_guest_account())) {
1958 LDAPMod
**mods
= NULL
;
1965 d_printf(_("Adding the Guest user.\n"));
1967 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_GUEST
);
1969 pwd
= Get_Pwnam_alloc(tc
, lp_guest_account());
1972 if (domusers_gid
== -1) {
1974 _("Can't create Guest user, Domain "
1975 "Users group not available!\n"));
1978 if ((pwd
= talloc(tc
, struct passwd
)) == NULL
) {
1979 d_fprintf(stderr
, _("talloc failed\n"));
1982 pwd
->pw_name
= talloc_strdup(pwd
, lp_guest_account());
1987 if (!winbind_allocate_uid(&(pwd
->pw_uid
))) {
1989 _("Unable to allocate a new uid to "
1990 "create the Guest user!\n"));
1994 pwd
->pw_gid
= domusers_gid
;
1995 pwd
->pw_dir
= talloc_strdup(tc
, "/");
1996 pwd
->pw_shell
= talloc_strdup(tc
, "/bin/false");
1997 if (!pwd
->pw_dir
|| !pwd
->pw_shell
) {
1998 d_fprintf(stderr
, _("Out of Memory!\n"));
2003 dn
= talloc_asprintf(tc
, "uid=%s,%s", pwd
->pw_name
,
2004 lp_ldap_user_suffix (talloc_tos()));
2005 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_uid
);
2006 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2007 if (!dn
|| !uidstr
|| !gidstr
) {
2008 d_fprintf(stderr
, _("Out of Memory!\n"));
2012 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
2013 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
2014 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
2016 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
2017 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
2018 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
2019 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
2020 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
2021 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
2022 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", pwd
->pw_name
);
2024 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", pwd
->pw_name
);
2025 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", pwd
->pw_name
);
2026 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", pwd
->pw_name
);
2027 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
2028 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2029 if ((pwd
->pw_dir
!= NULL
) && (pwd
->pw_dir
[0] != '\0')) {
2030 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", pwd
->pw_dir
);
2032 if ((pwd
->pw_shell
!= NULL
) && (pwd
->pw_shell
[0] != '\0')) {
2033 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", pwd
->pw_shell
);
2035 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
2036 sid_string_talloc(tc
, &sid
));
2037 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
2038 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
2039 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
2041 smbldap_talloc_autofree_ldapmod(tc
, mods
);
2043 rc
= smbldap_add(state
, dn
, mods
);
2045 if (rc
!= LDAP_SUCCESS
) {
2046 d_fprintf(stderr
, _("Failed to add Guest user to "
2047 "ldap directory\n"));
2051 if (!pdb_getsampwnam(samuser
, lp_guest_account())) {
2052 d_fprintf(stderr
, _("Failed to read just "
2053 "created user.\n"));
2058 d_printf(_("found!\n"));
2061 d_printf(_("Checking Guest's group.\n"));
2063 pwd
= Get_Pwnam_alloc(tc
, lp_guest_account());
2066 _("Failed to find just created Guest account!\n"
2067 " Is nss properly configured?!\n"));
2071 if (pwd
->pw_gid
== domusers_gid
) {
2072 d_printf(_("found!\n"));
2076 if (!pdb_getgrgid(gmap
, pwd
->pw_gid
)) {
2077 LDAPMod
**mods
= NULL
;
2085 d_printf(_("Adding the Domain Guests group.\n"));
2087 uname
= talloc_strdup(tc
, "domguests");
2088 wname
= talloc_strdup(tc
, "Domain Guests");
2089 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domguests",
2090 lp_ldap_group_suffix(talloc_tos()));
2091 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2092 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
2094 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
2095 d_fprintf(stderr
, _("Out of Memory!\n"));
2099 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_GUESTS
);
2101 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
2102 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
2104 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
2105 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
2106 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
2108 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
2109 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
2110 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2111 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
2112 sid_string_talloc(tc
, &gsid
));
2113 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
2115 smbldap_talloc_autofree_ldapmod(tc
, mods
);
2117 rc
= smbldap_add(state
, dn
, mods
);
2119 if (rc
!= LDAP_SUCCESS
) {
2121 _("Failed to add Domain Guests group to ldap "
2125 d_printf(_("found!\n"));
2140 /***********************************************************
2141 migrated functionality from smbgroupedit
2142 **********************************************************/
2143 int net_sam(struct net_context
*c
, int argc
, const char **argv
)
2145 struct functable func
[] = {
2147 "createbuiltingroup",
2148 net_sam_createbuiltingroup
,
2149 NET_TRANSPORT_LOCAL
,
2150 N_("Create a new BUILTIN group"),
2151 N_("net sam createbuiltingroup\n"
2152 " Create a new BUILTIN group")
2156 net_sam_createlocalgroup
,
2157 NET_TRANSPORT_LOCAL
,
2158 N_("Create a new local group"),
2159 N_("net sam createlocalgroup\n"
2160 " Create a new local group")
2163 "createdomaingroup",
2164 net_sam_createdomaingroup
,
2165 NET_TRANSPORT_LOCAL
,
2166 N_("Create a new group"),
2167 N_("net sam createdomaingroup\n"
2168 " Create a new group")
2172 net_sam_deletelocalgroup
,
2173 NET_TRANSPORT_LOCAL
,
2174 N_("Delete an existing local group"),
2175 N_("net sam deletelocalgroup\n"
2176 " Delete an existing local group")
2179 "deletedomaingroup",
2180 net_sam_deletedomaingroup
,
2181 NET_TRANSPORT_LOCAL
,
2182 N_("Delete a domain group"),
2183 N_("net sam deletedomaingroup\n"
2188 net_sam_mapunixgroup
,
2189 NET_TRANSPORT_LOCAL
,
2190 N_("Map a unix group to a domain group"),
2191 N_("net sam mapunixgroup\n"
2192 " Map a unix group to a domain group")
2196 net_sam_unmapunixgroup
,
2197 NET_TRANSPORT_LOCAL
,
2198 N_("Remove a group mapping of an unix group to a "
2200 N_("net sam unmapunixgroup\n"
2201 " Remove a group mapping of an unix group to a "
2207 NET_TRANSPORT_LOCAL
,
2208 N_("Add a member to a group"),
2209 N_("net sam addmem\n"
2210 " Add a member to a group")
2215 NET_TRANSPORT_LOCAL
,
2216 N_("Delete a member from a group"),
2217 N_("net sam delmem\n"
2218 " Delete a member from a group")
2223 NET_TRANSPORT_LOCAL
,
2224 N_("List group members"),
2225 N_("net sam listmem\n"
2226 " List group members")
2231 NET_TRANSPORT_LOCAL
,
2232 N_("List users, groups and local groups"),
2234 " List users, groups and local groups")
2239 NET_TRANSPORT_LOCAL
,
2240 N_("Show details of a SAM entry"),
2242 " Show details of a SAM entry")
2247 NET_TRANSPORT_LOCAL
,
2248 N_("Set details of a SAM account"),
2250 " Set details of a SAM account")
2255 NET_TRANSPORT_LOCAL
,
2256 N_("Set account policies"),
2257 N_("net sam policy\n"
2258 " Set account policies")
2263 NET_TRANSPORT_LOCAL
,
2264 N_("Manipulate user privileges"),
2265 N_("net sam rights\n"
2266 " Manipulate user privileges")
2272 NET_TRANSPORT_LOCAL
,
2273 N_("Provision a clean user database"),
2274 N_("net sam privison\n"
2275 " Provision a clear user database")
2278 {NULL
, NULL
, 0, NULL
, NULL
}
2281 if (getuid() != 0) {
2282 d_fprintf(stderr
, _("You are not root, most things won't "
2286 return net_run_function(c
, argc
, argv
, "net sam", func
);