2 * Unix SMB/CIFS implementation.
3 * Local SAM access routines
4 * Copyright (C) Volker Lendecke 2006
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "system/passwd.h"
23 #include "utils/net.h"
24 #include "../librpc/gen_ndr/samr.h"
26 #include "../libcli/security/security.h"
27 #include "lib/winbind_util.h"
29 #include "lib/privileges.h"
35 static int net_sam_userset(struct net_context
*c
, int argc
, const char **argv
,
37 bool (*fn
)(struct samu
*, const char *,
38 enum pdb_value_state
))
40 struct samu
*sam_acct
= NULL
;
42 enum lsa_SidType type
;
43 const char *dom
, *name
;
46 if (argc
!= 2 || c
->display_usage
) {
47 d_fprintf(stderr
, "%s\n", _("Usage:"));
48 d_fprintf(stderr
, _("net sam set %s <user> <value>\n"),
53 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
54 &dom
, &name
, &sid
, &type
)) {
55 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
59 if (type
!= SID_NAME_USER
) {
60 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
61 sid_type_lookup(type
));
65 if ( !(sam_acct
= samu_new( NULL
)) ) {
66 d_fprintf(stderr
, _("Internal error\n"));
70 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
71 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
75 if (!fn(sam_acct
, argv
[1], PDB_CHANGED
)) {
76 d_fprintf(stderr
, _("Internal error\n"));
80 status
= pdb_update_sam_account(sam_acct
);
81 if (!NT_STATUS_IS_OK(status
)) {
82 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
83 argv
[0], nt_errstr(status
));
87 TALLOC_FREE(sam_acct
);
89 d_printf(_("Updated %s for %s\\%s to %s\n"), field
, dom
, name
, argv
[1]);
93 static int net_sam_set_fullname(struct net_context
*c
, int argc
,
96 return net_sam_userset(c
, argc
, argv
, "fullname",
100 static int net_sam_set_logonscript(struct net_context
*c
, int argc
,
103 return net_sam_userset(c
, argc
, argv
, "logonscript",
104 pdb_set_logon_script
);
107 static int net_sam_set_profilepath(struct net_context
*c
, int argc
,
110 return net_sam_userset(c
, argc
, argv
, "profilepath",
111 pdb_set_profile_path
);
114 static int net_sam_set_homedrive(struct net_context
*c
, int argc
,
117 return net_sam_userset(c
, argc
, argv
, "homedrive",
121 static int net_sam_set_homedir(struct net_context
*c
, int argc
,
124 return net_sam_userset(c
, argc
, argv
, "homedir",
128 static int net_sam_set_workstations(struct net_context
*c
, int argc
,
131 return net_sam_userset(c
, argc
, argv
, "workstations",
132 pdb_set_workstations
);
139 static int net_sam_set_userflag(struct net_context
*c
, int argc
,
140 const char **argv
, const char *field
,
143 struct samu
*sam_acct
= NULL
;
145 enum lsa_SidType type
;
146 const char *dom
, *name
;
150 if ((argc
!= 2) || c
->display_usage
||
151 (!strequal(argv
[1], "yes") &&
152 !strequal(argv
[1], "no"))) {
153 d_fprintf(stderr
, "%s\n", _("Usage:"));
154 d_fprintf(stderr
, _("net sam set %s <user> [yes|no]\n"),
159 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
160 &dom
, &name
, &sid
, &type
)) {
161 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
165 if (type
!= SID_NAME_USER
) {
166 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
167 sid_type_lookup(type
));
171 if ( !(sam_acct
= samu_new( NULL
)) ) {
172 d_fprintf(stderr
, _("Internal error\n"));
176 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
177 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
181 acct_flags
= pdb_get_acct_ctrl(sam_acct
);
183 if (strequal(argv
[1], "yes")) {
189 pdb_set_acct_ctrl(sam_acct
, acct_flags
, PDB_CHANGED
);
191 status
= pdb_update_sam_account(sam_acct
);
192 if (!NT_STATUS_IS_OK(status
)) {
193 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
194 argv
[0], nt_errstr(status
));
198 TALLOC_FREE(sam_acct
);
200 d_fprintf(stderr
, _("Updated flag %s for %s\\%s to %s\n"), field
, dom
,
205 static int net_sam_set_disabled(struct net_context
*c
, int argc
,
208 return net_sam_set_userflag(c
, argc
, argv
, "disabled", ACB_DISABLED
);
211 static int net_sam_set_pwnotreq(struct net_context
*c
, int argc
,
214 return net_sam_set_userflag(c
, argc
, argv
, "pwnotreq", ACB_PWNOTREQ
);
217 static int net_sam_set_autolock(struct net_context
*c
, int argc
,
220 return net_sam_set_userflag(c
, argc
, argv
, "autolock", ACB_AUTOLOCK
);
223 static int net_sam_set_pwnoexp(struct net_context
*c
, int argc
,
226 return net_sam_set_userflag(c
, argc
, argv
, "pwnoexp", ACB_PWNOEXP
);
230 * Set pass last change time, based on force pass change now
233 static int net_sam_set_pwdmustchangenow(struct net_context
*c
, int argc
,
236 struct samu
*sam_acct
= NULL
;
238 enum lsa_SidType type
;
239 const char *dom
, *name
;
242 if ((argc
!= 2) || c
->display_usage
||
243 (!strequal(argv
[1], "yes") &&
244 !strequal(argv
[1], "no"))) {
245 d_fprintf(stderr
, "%s\n%s",
247 _("net sam set pwdmustchangenow <user> [yes|no]\n"));
251 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
252 &dom
, &name
, &sid
, &type
)) {
253 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
257 if (type
!= SID_NAME_USER
) {
258 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
259 sid_type_lookup(type
));
263 if ( !(sam_acct
= samu_new( NULL
)) ) {
264 d_fprintf(stderr
, _("Internal error\n"));
268 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
269 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
273 if (strequal(argv
[1], "yes")) {
274 pdb_set_pass_last_set_time(sam_acct
, 0, PDB_CHANGED
);
276 pdb_set_pass_last_set_time(sam_acct
, time(NULL
), PDB_CHANGED
);
279 status
= pdb_update_sam_account(sam_acct
);
280 if (!NT_STATUS_IS_OK(status
)) {
281 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
282 argv
[0], nt_errstr(status
));
286 TALLOC_FREE(sam_acct
);
288 d_fprintf(stderr
, _("Updated 'user must change password at next logon' "
289 "for %s\\%s to %s\n"), dom
,
296 * Set a user's or a group's comment
299 static int net_sam_set_comment(struct net_context
*c
, int argc
,
304 enum lsa_SidType type
;
305 const char *dom
, *name
;
308 if (argc
!= 2 || c
->display_usage
) {
309 d_fprintf(stderr
, "%s\n%s",
311 _("net sam set comment <name> <comment>\n"));
315 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
316 &dom
, &name
, &sid
, &type
)) {
317 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
321 if (type
== SID_NAME_USER
) {
322 return net_sam_userset(c
, argc
, argv
, "comment",
326 if ((type
!= SID_NAME_DOM_GRP
) && (type
!= SID_NAME_ALIAS
) &&
327 (type
!= SID_NAME_WKN_GRP
)) {
328 d_fprintf(stderr
, _("%s is a %s, not a group\n"), argv
[0],
329 sid_type_lookup(type
));
333 if (!pdb_getgrsid(&map
, sid
)) {
334 d_fprintf(stderr
, _("Could not load group %s\n"), argv
[0]);
338 fstrcpy(map
.comment
, argv
[1]);
340 status
= pdb_update_group_mapping_entry(&map
);
342 if (!NT_STATUS_IS_OK(status
)) {
343 d_fprintf(stderr
, _("Updating group mapping entry failed with "
344 "%s\n"), nt_errstr(status
));
348 d_printf("Updated comment of group %s\\%s to %s\n", dom
, name
,
354 static int net_sam_set(struct net_context
*c
, int argc
, const char **argv
)
356 struct functable func
[] = {
361 N_("Change a user's home directory"),
362 N_("net sam set homedir\n"
363 " Change a user's home directory")
367 net_sam_set_profilepath
,
369 N_("Change a user's profile path"),
370 N_("net sam set profilepath\n"
371 " Change a user's profile path")
377 N_("Change a users or groups description"),
378 N_("net sam set comment\n"
379 " Change a users or groups description")
383 net_sam_set_fullname
,
385 N_("Change a user's full name"),
386 N_("net sam set fullname\n"
387 " Change a user's full name")
391 net_sam_set_logonscript
,
393 N_("Change a user's logon script"),
394 N_("net sam set logonscript\n"
395 " Change a user's logon script")
399 net_sam_set_homedrive
,
401 N_("Change a user's home drive"),
402 N_("net sam set homedrive\n"
403 " Change a user's home drive")
407 net_sam_set_workstations
,
409 N_("Change a user's allowed workstations"),
410 N_("net sam set workstations\n"
411 " Change a user's allowed workstations")
415 net_sam_set_disabled
,
417 N_("Disable/Enable a user"),
418 N_("net sam set disable\n"
419 " Disable/Enable a user")
423 net_sam_set_pwnotreq
,
425 N_("Disable/Enable the password not required flag"),
426 N_("net sam set pwnotreq\n"
427 " Disable/Enable the password not required flag")
431 net_sam_set_autolock
,
433 N_("Disable/Enable a user's lockout flag"),
434 N_("net sam set autolock\n"
435 " Disable/Enable a user's lockout flag")
441 N_("Disable/Enable whether a user's pw does not "
443 N_("net sam set pwnoexp\n"
444 " Disable/Enable whether a user's pw does not "
449 net_sam_set_pwdmustchangenow
,
451 N_("Force users password must change at next logon"),
452 N_("net sam set pwdmustchangenow\n"
453 " Force users password must change at next logon")
455 {NULL
, NULL
, 0, NULL
, NULL
}
458 return net_run_function(c
, argc
, argv
, "net sam set", func
);
462 * Manage account policies
465 static int net_sam_policy_set(struct net_context
*c
, int argc
, const char **argv
)
467 const char *account_policy
= NULL
;
469 uint32 old_value
= 0;
470 enum pdb_policy_type field
;
473 if (argc
!= 2 || c
->display_usage
) {
474 d_fprintf(stderr
, "%s\n%s",
476 _("net sam policy set \"<account policy>\" <value>\n"));
480 account_policy
= argv
[0];
481 field
= account_policy_name_to_typenum(account_policy
);
483 if (strequal(argv
[1], "forever") || strequal(argv
[1], "never")
484 || strequal(argv
[1], "off")) {
488 value
= strtoul(argv
[1], &endptr
, 10);
490 if ((endptr
== argv
[1]) || (endptr
[0] != '\0')) {
491 d_printf(_("Unable to set policy \"%s\"! Invalid value "
493 account_policy
, argv
[1]);
502 account_policy_names_list(&names
, &count
);
503 d_fprintf(stderr
, _("No account policy \"%s\"!\n\n"), argv
[0]);
504 d_fprintf(stderr
, _("Valid account policies are:\n"));
506 for (i
=0; i
<count
; i
++) {
507 d_fprintf(stderr
, "%s\n", names
[i
]);
514 if (!pdb_get_account_policy(field
, &old_value
)) {
515 d_fprintf(stderr
, _("Valid account policy, but unable to fetch "
518 d_printf(_("Account policy \"%s\" value was: %d\n"),
519 account_policy
, old_value
);
522 if (!pdb_set_account_policy(field
, value
)) {
523 d_fprintf(stderr
, _("Valid account policy, but unable to "
527 d_printf(_("Account policy \"%s\" value is now: %d\n"),
528 account_policy
, value
);
534 static int net_sam_policy_show(struct net_context
*c
, int argc
, const char **argv
)
536 const char *account_policy
= NULL
;
538 enum pdb_policy_type field
;
540 if (argc
!= 1 || c
->display_usage
) {
541 d_fprintf(stderr
, "%s\n%s",
543 _("net sam policy show \"<account policy>\"\n"));
547 account_policy
= argv
[0];
548 field
= account_policy_name_to_typenum(account_policy
);
554 account_policy_names_list(&names
, &count
);
555 d_fprintf(stderr
, _("No account policy by that name!\n"));
557 d_fprintf(stderr
, _("Valid account policies "
559 for (i
=0; i
<count
; i
++) {
560 d_fprintf(stderr
, "%s\n", names
[i
]);
567 if (!pdb_get_account_policy(field
, &old_value
)) {
568 fprintf(stderr
, _("Valid account policy, but unable to "
573 printf(_("Account policy \"%s\" description: %s\n"),
574 account_policy
, account_policy_get_desc(field
));
575 printf(_("Account policy \"%s\" value is: %d\n"), account_policy
,
580 static int net_sam_policy_list(struct net_context
*c
, int argc
, const char **argv
)
586 if (c
->display_usage
) {
588 "net sam policy list\n"
591 _("List account policies"));
595 account_policy_names_list(&names
, &count
);
597 d_fprintf(stderr
, _("Valid account policies "
599 for (i
= 0; i
< count
; i
++) {
600 d_fprintf(stderr
, "%s\n", names
[i
]);
607 static int net_sam_policy(struct net_context
*c
, int argc
, const char **argv
)
609 struct functable func
[] = {
614 N_("List account policies"),
615 N_("net sam policy list\n"
616 " List account policies")
622 N_("Show account policies"),
623 N_("net sam policy show\n"
624 " Show account policies")
630 N_("Change account policies"),
631 N_("net sam policy set\n"
632 " Change account policies")
634 {NULL
, NULL
, 0, NULL
, NULL
}
637 return net_run_function(c
, argc
, argv
, "net sam policy", func
);
640 static int net_sam_rights_list(struct net_context
*c
, int argc
,
643 enum sec_privilege privilege
;
645 if (argc
> 1 || c
->display_usage
) {
646 d_fprintf(stderr
, "%s\n%s",
648 _("net sam rights list [privilege name]\n"));
654 int num
= num_privileges_in_short_list();
656 for (i
=0; i
<num
; i
++) {
657 d_printf("%s\n", sec_privilege_name_from_index(i
));
662 privilege
= sec_privilege_id(argv
[0]);
664 if (privilege
!= SEC_PRIV_INVALID
) {
665 struct dom_sid
*sids
;
669 status
= privilege_enum_sids(privilege
, talloc_tos(),
671 if (!NT_STATUS_IS_OK(status
)) {
672 d_fprintf(stderr
, _("Could not list rights: %s\n"),
677 for (i
=0; i
<num_sids
; i
++) {
678 const char *dom
, *name
;
679 enum lsa_SidType type
;
681 if (lookup_sid(talloc_tos(), &sids
[i
], &dom
, &name
,
683 d_printf("%s\\%s\n", dom
, name
);
686 d_printf("%s\n", sid_string_tos(&sids
[i
]));
695 static int net_sam_rights_grant(struct net_context
*c
, int argc
,
699 enum lsa_SidType type
;
700 const char *dom
, *name
;
703 if (argc
< 2 || c
->display_usage
) {
704 d_fprintf(stderr
, "%s\n%s",
706 _("net sam rights grant <name> <rights> ...\n"));
710 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
711 &dom
, &name
, &sid
, &type
)) {
712 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
716 for (i
=1; i
< argc
; i
++) {
717 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
718 if (privilege
== SEC_PRIV_INVALID
) {
719 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
723 if (!grant_privilege_by_name(&sid
, argv
[i
])) {
724 d_fprintf(stderr
, _("Could not grant privilege\n"));
728 d_printf(_("Granted %s to %s\\%s\n"), argv
[i
], dom
, name
);
734 static int net_sam_rights_revoke(struct net_context
*c
, int argc
,
738 enum lsa_SidType type
;
739 const char *dom
, *name
;
742 if (argc
< 2 || c
->display_usage
) {
743 d_fprintf(stderr
, "%s\n%s",
745 _("net sam rights revoke <name> <rights>\n"));
749 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
750 &dom
, &name
, &sid
, &type
)) {
751 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
755 for (i
=1; i
< argc
; i
++) {
756 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
757 if (privilege
== SEC_PRIV_INVALID
) {
758 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
762 if (!revoke_privilege_by_name(&sid
, argv
[i
])) {
763 d_fprintf(stderr
, _("Could not revoke privilege\n"));
767 d_printf(_("Revoked %s from %s\\%s\n"), argv
[i
], dom
, name
);
773 static int net_sam_rights(struct net_context
*c
, int argc
, const char **argv
)
775 struct functable func
[] = {
780 N_("List possible user rights"),
781 N_("net sam rights list\n"
782 " List possible user rights")
786 net_sam_rights_grant
,
788 N_("Grant right(s)"),
789 N_("net sam rights grant\n"
794 net_sam_rights_revoke
,
796 N_("Revoke right(s)"),
797 N_("net sam rights revoke\n"
800 {NULL
, NULL
, 0, NULL
, NULL
}
802 return net_run_function(c
, argc
, argv
, "net sam rights", func
);
806 * Map a unix group to a domain group
809 static NTSTATUS
map_unix_group(const struct group
*grp
, GROUP_MAP
*pmap
)
813 const char *grpname
, *dom
, *name
;
816 if (pdb_getgrgid(&map
, grp
->gr_gid
)) {
817 return NT_STATUS_GROUP_EXISTS
;
820 map
.gid
= grp
->gr_gid
;
821 grpname
= grp
->gr_name
;
823 if (lookup_name(talloc_tos(), grpname
, LOOKUP_NAME_LOCAL
,
824 &dom
, &name
, NULL
, NULL
)) {
826 const char *tmp
= talloc_asprintf(
827 talloc_tos(), "Unix Group %s", grp
->gr_name
);
829 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
830 grpname
, dom
, name
, tmp
));
834 if (lookup_name(talloc_tos(), grpname
, LOOKUP_NAME_LOCAL
,
835 NULL
, NULL
, NULL
, NULL
)) {
836 DEBUG(3, ("\"%s\" exists, can't map it\n", grp
->gr_name
));
837 return NT_STATUS_GROUP_EXISTS
;
840 fstrcpy(map
.nt_name
, grpname
);
842 if (pdb_capabilities() & PDB_CAP_STORE_RIDS
) {
843 if (!pdb_new_rid(&rid
)) {
844 DEBUG(3, ("Could not get a new RID for %s\n",
846 return NT_STATUS_ACCESS_DENIED
;
849 rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
852 sid_compose(&map
.sid
, get_global_sam_sid(), rid
);
853 map
.sid_name_use
= SID_NAME_DOM_GRP
;
854 fstrcpy(map
.comment
, talloc_asprintf(talloc_tos(), "Unix Group %s",
857 status
= pdb_add_group_mapping_entry(&map
);
858 if (NT_STATUS_IS_OK(status
)) {
864 static int net_sam_mapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
870 if (argc
!= 1 || c
->display_usage
) {
871 d_fprintf(stderr
, "%s\n%s",
873 _("net sam mapunixgroup <name>\n"));
877 grp
= getgrnam(argv
[0]);
879 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
883 status
= map_unix_group(grp
, &map
);
885 if (!NT_STATUS_IS_OK(status
)) {
886 d_fprintf(stderr
, _("Mapping group %s failed with %s\n"),
887 argv
[0], nt_errstr(status
));
891 d_printf(_("Mapped unix group %s to SID %s\n"), argv
[0],
892 sid_string_tos(&map
.sid
));
898 * Remove a group mapping
901 static NTSTATUS
unmap_unix_group(const struct group
*grp
, GROUP_MAP
*pmap
)
905 struct dom_sid dom_sid
;
907 map
.gid
= grp
->gr_gid
;
908 grpname
= grp
->gr_name
;
910 if (!lookup_name(talloc_tos(), grpname
, LOOKUP_NAME_LOCAL
,
911 NULL
, NULL
, NULL
, NULL
)) {
912 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp
->gr_name
));
913 return NT_STATUS_NO_SUCH_GROUP
;
916 fstrcpy(map
.nt_name
, grpname
);
918 if (!pdb_gid_to_sid(map
.gid
, &dom_sid
)) {
919 return NT_STATUS_UNSUCCESSFUL
;
922 return pdb_delete_group_mapping_entry(dom_sid
);
925 static int net_sam_unmapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
931 if (argc
!= 1 || c
->display_usage
) {
932 d_fprintf(stderr
, "%s\n%s",
934 _("net sam unmapunixgroup <name>\n"));
938 grp
= getgrnam(argv
[0]);
940 d_fprintf(stderr
, _("Could not find mapping for group %s.\n"),
945 status
= unmap_unix_group(grp
, &map
);
947 if (!NT_STATUS_IS_OK(status
)) {
948 d_fprintf(stderr
, _("Unmapping group %s failed with %s.\n"),
949 argv
[0], nt_errstr(status
));
953 d_printf(_("Unmapped unix group %s.\n"), argv
[0]);
959 * Create a domain group
962 static int net_sam_createdomaingroup(struct net_context
*c
, int argc
,
968 if (argc
!= 1 || c
->display_usage
) {
969 d_fprintf(stderr
, "%s\n%s",
971 _("net sam createdomaingroup <name>\n"));
975 status
= pdb_create_dom_group(talloc_tos(), argv
[0], &rid
);
977 if (!NT_STATUS_IS_OK(status
)) {
978 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
979 argv
[0], nt_errstr(status
));
983 d_printf(_("Created domain group %s with RID %d\n"), argv
[0], rid
);
989 * Delete a domain group
992 static int net_sam_deletedomaingroup(struct net_context
*c
, int argc
,
997 enum lsa_SidType type
;
998 const char *dom
, *name
;
1001 if (argc
!= 1 || c
->display_usage
) {
1002 d_fprintf(stderr
, "%s\n%s",
1004 _("net sam deletelocalgroup <name>\n"));
1008 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1009 &dom
, &name
, &sid
, &type
)) {
1010 d_fprintf(stderr
, _("Could not find %s.\n"), argv
[0]);
1014 if (type
!= SID_NAME_DOM_GRP
) {
1015 d_fprintf(stderr
, _("%s is a %s, not a domain group.\n"),
1016 argv
[0], sid_type_lookup(type
));
1020 sid_peek_rid(&sid
, &rid
);
1022 status
= pdb_delete_dom_group(talloc_tos(), rid
);
1024 if (!NT_STATUS_IS_OK(status
)) {
1025 d_fprintf(stderr
,_("Deleting domain group %s failed with %s\n"),
1026 argv
[0], nt_errstr(status
));
1030 d_printf(_("Deleted domain group %s.\n"), argv
[0]);
1036 * Create a local group
1039 static int net_sam_createlocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1044 if (argc
!= 1 || c
->display_usage
) {
1045 d_fprintf(stderr
, "%s\n%s",
1047 _("net sam createlocalgroup <name>\n"));
1051 if (!winbind_ping()) {
1052 d_fprintf(stderr
, _("winbind seems not to run. "
1053 "createlocalgroup only works when winbind runs.\n"));
1057 status
= pdb_create_alias(argv
[0], &rid
);
1059 if (!NT_STATUS_IS_OK(status
)) {
1060 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1061 argv
[0], nt_errstr(status
));
1065 d_printf(_("Created local group %s with RID %d\n"), argv
[0], rid
);
1071 * Delete a local group
1074 static int net_sam_deletelocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1077 enum lsa_SidType type
;
1078 const char *dom
, *name
;
1081 if (argc
!= 1 || c
->display_usage
) {
1082 d_fprintf(stderr
, "%s\n%s",
1084 _("net sam deletelocalgroup <name>\n"));
1088 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1089 &dom
, &name
, &sid
, &type
)) {
1090 d_fprintf(stderr
,_("Could not find %s.\n"), argv
[0]);
1094 if (type
!= SID_NAME_ALIAS
) {
1095 d_fprintf(stderr
, _("%s is a %s, not a local group.\n"),argv
[0],
1096 sid_type_lookup(type
));
1100 status
= pdb_delete_alias(&sid
);
1102 if (!NT_STATUS_IS_OK(status
)) {
1103 d_fprintf(stderr
, _("Deleting local group %s failed with %s\n"),
1104 argv
[0], nt_errstr(status
));
1108 d_printf(_("Deleted local group %s.\n"), argv
[0]);
1114 * Create a builtin group
1117 static int net_sam_createbuiltingroup(struct net_context
*c
, int argc
, const char **argv
)
1121 enum lsa_SidType type
;
1125 if (argc
!= 1 || c
->display_usage
) {
1126 d_fprintf(stderr
, "%s\n%s",
1128 _("net sam createbuiltingroup <name>\n"));
1132 if (!winbind_ping()) {
1133 d_fprintf(stderr
, _("winbind seems not to run. "
1134 "createbuiltingroup only works when winbind "
1139 /* validate the name and get the group */
1141 fstrcpy( groupname
, "BUILTIN\\" );
1142 fstrcat( groupname
, argv
[0] );
1144 if ( !lookup_name(talloc_tos(), groupname
, LOOKUP_NAME_ALL
, NULL
,
1145 NULL
, &sid
, &type
)) {
1146 d_fprintf(stderr
, _("%s is not a BUILTIN group\n"), argv
[0]);
1150 if ( !sid_peek_rid( &sid
, &rid
) ) {
1151 d_fprintf(stderr
, _("Failed to get RID for %s\n"), argv
[0]);
1155 status
= pdb_create_builtin_alias( rid
);
1157 if (!NT_STATUS_IS_OK(status
)) {
1158 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1159 argv
[0], nt_errstr(status
));
1163 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv
[0], rid
);
1169 * Add a group member
1172 static int net_sam_addmem(struct net_context
*c
, int argc
, const char **argv
)
1174 const char *groupdomain
, *groupname
, *memberdomain
, *membername
;
1175 struct dom_sid group
, member
;
1176 enum lsa_SidType grouptype
, membertype
;
1179 if (argc
!= 2 || c
->display_usage
) {
1180 d_fprintf(stderr
, "%s\n%s",
1182 _("net sam addmem <group> <member>\n"));
1186 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1187 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1188 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1192 /* check to see if the member to be added is a name or a SID */
1194 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1195 &memberdomain
, &membername
, &member
, &membertype
))
1197 /* try it as a SID */
1199 if ( !string_to_sid( &member
, argv
[1] ) ) {
1200 d_fprintf(stderr
, _("Could not find member %s\n"),
1205 if ( !lookup_sid(talloc_tos(), &member
, &memberdomain
,
1206 &membername
, &membertype
) )
1208 d_fprintf(stderr
, _("Could not resolve SID %s\n"),
1214 if ((grouptype
== SID_NAME_ALIAS
) || (grouptype
== SID_NAME_WKN_GRP
)) {
1215 if ((membertype
!= SID_NAME_USER
) &&
1216 (membertype
!= SID_NAME_DOM_GRP
)) {
1217 d_fprintf(stderr
, _("%s is a local group, only users "
1218 "and domain groups can be added.\n"
1219 "%s is a %s\n"), argv
[0], argv
[1],
1220 sid_type_lookup(membertype
));
1223 status
= pdb_add_aliasmem(&group
, &member
);
1225 if (!NT_STATUS_IS_OK(status
)) {
1226 d_fprintf(stderr
, _("Adding local group member failed "
1227 "with %s\n"), nt_errstr(status
));
1230 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1231 uint32_t grouprid
, memberrid
;
1233 sid_peek_rid(&group
, &grouprid
);
1234 sid_peek_rid(&member
, &memberrid
);
1236 status
= pdb_add_groupmem(talloc_tos(), grouprid
, memberrid
);
1237 if (!NT_STATUS_IS_OK(status
)) {
1238 d_fprintf(stderr
, _("Adding domain group member failed "
1239 "with %s\n"), nt_errstr(status
));
1243 d_fprintf(stderr
, _("Can only add members to local groups so "
1244 "far, %s is a %s\n"), argv
[0],
1245 sid_type_lookup(grouptype
));
1249 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain
, membername
,
1250 groupdomain
, groupname
);
1256 * Delete a group member
1259 static int net_sam_delmem(struct net_context
*c
, int argc
, const char **argv
)
1261 const char *groupdomain
, *groupname
;
1262 const char *memberdomain
= NULL
;
1263 const char *membername
= NULL
;
1264 struct dom_sid group
, member
;
1265 enum lsa_SidType grouptype
;
1268 if (argc
!= 2 || c
->display_usage
) {
1269 d_fprintf(stderr
,"%s\n%s",
1271 _("net sam delmem <group> <member>\n"));
1275 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1276 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1277 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1281 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1282 &memberdomain
, &membername
, &member
, NULL
)) {
1283 if (!string_to_sid(&member
, argv
[1])) {
1284 d_fprintf(stderr
, _("Could not find member %s\n"),
1290 if ((grouptype
== SID_NAME_ALIAS
) ||
1291 (grouptype
== SID_NAME_WKN_GRP
)) {
1292 status
= pdb_del_aliasmem(&group
, &member
);
1294 if (!NT_STATUS_IS_OK(status
)) {
1295 d_fprintf(stderr
,_("Deleting local group member failed "
1296 "with %s\n"), nt_errstr(status
));
1299 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1300 uint32_t grouprid
, memberrid
;
1302 sid_peek_rid(&group
, &grouprid
);
1303 sid_peek_rid(&member
, &memberrid
);
1305 status
= pdb_del_groupmem(talloc_tos(), grouprid
, memberrid
);
1306 if (!NT_STATUS_IS_OK(status
)) {
1307 d_fprintf(stderr
, _("Deleting domain group member "
1308 "failed with %s\n"), nt_errstr(status
));
1312 d_fprintf(stderr
, _("Can only delete members from local groups "
1313 "so far, %s is a %s\n"), argv
[0],
1314 sid_type_lookup(grouptype
));
1318 if (membername
!= NULL
) {
1319 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1320 memberdomain
, membername
, groupdomain
, groupname
);
1322 d_printf(_("Deleted %s from %s\\%s\n"),
1323 sid_string_tos(&member
), groupdomain
, groupname
);
1330 * List group members
1333 static int net_sam_listmem(struct net_context
*c
, int argc
, const char **argv
)
1335 const char *groupdomain
, *groupname
;
1336 struct dom_sid group
;
1337 struct dom_sid
*members
= NULL
;
1338 size_t i
, num_members
= 0;
1339 enum lsa_SidType grouptype
;
1342 if (argc
!= 1 || c
->display_usage
) {
1343 d_fprintf(stderr
, "%s\n%s",
1345 _("net sam listmem <group>\n"));
1349 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1350 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1351 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1355 if ((grouptype
== SID_NAME_ALIAS
) ||
1356 (grouptype
== SID_NAME_WKN_GRP
)) {
1357 status
= pdb_enum_aliasmem(&group
, talloc_tos(), &members
,
1359 if (!NT_STATUS_IS_OK(status
)) {
1360 d_fprintf(stderr
, _("Listing group members failed with "
1361 "%s\n"), nt_errstr(status
));
1364 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1367 status
= pdb_enum_group_members(talloc_tos(), &group
,
1368 &rids
, &num_members
);
1369 if (!NT_STATUS_IS_OK(status
)) {
1370 d_fprintf(stderr
, _("Listing group members failed with "
1371 "%s\n"), nt_errstr(status
));
1375 members
= talloc_array(talloc_tos(), struct dom_sid
,
1377 if (members
== NULL
) {
1382 for (i
=0; i
<num_members
; i
++) {
1383 sid_compose(&members
[i
], get_global_sam_sid(),
1388 d_fprintf(stderr
,_("Can only list local group members so far.\n"
1389 "%s is a %s\n"), argv
[0], sid_type_lookup(grouptype
));
1393 d_printf(_("%s\\%s has %u members\n"), groupdomain
, groupname
,
1394 (unsigned int)num_members
);
1395 for (i
=0; i
<num_members
; i
++) {
1396 const char *dom
, *name
;
1397 if (lookup_sid(talloc_tos(), &members
[i
], &dom
, &name
, NULL
)) {
1398 d_printf(" %s\\%s\n", dom
, name
);
1400 d_printf(" %s\n", sid_string_tos(&members
[i
]));
1404 TALLOC_FREE(members
);
1412 static int net_sam_do_list(struct net_context
*c
, int argc
, const char **argv
,
1413 struct pdb_search
*search
, const char *what
)
1415 bool verbose
= (argc
== 1);
1417 if ((argc
> 1) || c
->display_usage
||
1418 ((argc
== 1) && !strequal(argv
[0], "verbose"))) {
1419 d_fprintf(stderr
, "%s\n", _("Usage:"));
1420 d_fprintf(stderr
, _("net sam list %s [verbose]\n"), what
);
1424 if (search
== NULL
) {
1425 d_fprintf(stderr
, _("Could not start search\n"));
1430 struct samr_displayentry entry
;
1431 if (!search
->next_entry(search
, &entry
)) {
1435 d_printf("%s:%d:%s\n",
1440 d_printf("%s\n", entry
.account_name
);
1444 TALLOC_FREE(search
);
1448 static int net_sam_list_users(struct net_context
*c
, int argc
,
1451 return net_sam_do_list(c
, argc
, argv
,
1452 pdb_search_users(talloc_tos(), ACB_NORMAL
),
1456 static int net_sam_list_groups(struct net_context
*c
, int argc
,
1459 return net_sam_do_list(c
, argc
, argv
, pdb_search_groups(talloc_tos()),
1463 static int net_sam_list_localgroups(struct net_context
*c
, int argc
,
1466 return net_sam_do_list(c
, argc
, argv
,
1467 pdb_search_aliases(talloc_tos(),
1468 get_global_sam_sid()),
1472 static int net_sam_list_builtin(struct net_context
*c
, int argc
,
1475 return net_sam_do_list(c
, argc
, argv
,
1476 pdb_search_aliases(talloc_tos(),
1477 &global_sid_Builtin
),
1481 static int net_sam_list_workstations(struct net_context
*c
, int argc
,
1484 return net_sam_do_list(c
, argc
, argv
,
1485 pdb_search_users(talloc_tos(), ACB_WSTRUST
),
1493 static int net_sam_list(struct net_context
*c
, int argc
, const char **argv
)
1495 struct functable func
[] = {
1499 NET_TRANSPORT_LOCAL
,
1500 N_("List SAM users"),
1501 N_("net sam list users\n"
1506 net_sam_list_groups
,
1507 NET_TRANSPORT_LOCAL
,
1508 N_("List SAM groups"),
1509 N_("net sam list groups\n"
1514 net_sam_list_localgroups
,
1515 NET_TRANSPORT_LOCAL
,
1516 N_("List SAM local groups"),
1517 N_("net sam list localgroups\n"
1518 " List SAM local groups")
1522 net_sam_list_builtin
,
1523 NET_TRANSPORT_LOCAL
,
1524 N_("List builtin groups"),
1525 N_("net sam list builtin\n"
1526 " List builtin groups")
1530 net_sam_list_workstations
,
1531 NET_TRANSPORT_LOCAL
,
1532 N_("List domain member workstations"),
1533 N_("net sam list workstations\n"
1534 " List domain member workstations")
1536 {NULL
, NULL
, 0, NULL
, NULL
}
1539 return net_run_function(c
, argc
, argv
, "net sam list", func
);
1543 * Show details of SAM entries
1546 static int net_sam_show(struct net_context
*c
, int argc
, const char **argv
)
1549 enum lsa_SidType type
;
1550 const char *dom
, *name
;
1552 if (argc
!= 1 || c
->display_usage
) {
1553 d_fprintf(stderr
, "%s\n%s",
1555 _("net sam show <name>\n"));
1559 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1560 &dom
, &name
, &sid
, &type
)) {
1561 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
1565 d_printf(_("%s\\%s is a %s with SID %s\n"), dom
, name
,
1566 sid_type_lookup(type
), sid_string_tos(&sid
));
1574 * Init an LDAP tree with default users and Groups
1575 * if ldapsam:editposix is enabled
1578 static int net_sam_provision(struct net_context
*c
, int argc
, const char **argv
)
1582 char *ldap_uri
= NULL
;
1584 struct smbldap_state
*ls
;
1586 struct dom_sid gsid
;
1587 gid_t domusers_gid
= -1;
1588 gid_t domadmins_gid
= -1;
1589 struct samu
*samuser
;
1591 bool is_ipa
= false;
1593 if (c
->display_usage
) {
1595 "net sam provision\n"
1598 _("Init an LDAP tree with default users/groups"));
1602 tc
= talloc_new(NULL
);
1604 d_fprintf(stderr
, _("Out of Memory!\n"));
1608 if ((ldap_bk
= talloc_strdup(tc
, lp_passdb_backend())) == NULL
) {
1609 d_fprintf(stderr
, _("talloc failed\n"));
1613 p
= strchr(ldap_bk
, ':');
1616 ldap_uri
= talloc_strdup(tc
, p
+1);
1617 trim_char(ldap_uri
, ' ', ' ');
1620 trim_char(ldap_bk
, ' ', ' ');
1622 if (strcmp(ldap_bk
, "IPA_ldapsam") == 0 ) {
1626 if (strcmp(ldap_bk
, "ldapsam") != 0 && !is_ipa
) {
1628 _("Provisioning works only with ldapsam backend\n"));
1632 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1633 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1635 d_fprintf(stderr
, _("Provisioning works only if ldapsam:trusted"
1636 " and ldapsam:editposix are enabled.\n"));
1640 if (!is_ipa
&& !winbind_ping()) {
1641 d_fprintf(stderr
, _("winbind seems not to run. Provisioning "
1642 "LDAP only works when winbind runs.\n"));
1646 if (!NT_STATUS_IS_OK(smbldap_init(tc
, NULL
, ldap_uri
, &ls
))) {
1647 d_fprintf(stderr
, _("Unable to connect to the LDAP server.\n"));
1651 d_printf(_("Checking for Domain Users group.\n"));
1653 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_USERS
);
1655 if (!pdb_getgrsid(&gmap
, gsid
)) {
1656 LDAPMod
**mods
= NULL
;
1664 d_printf(_("Adding the Domain Users group.\n"));
1666 /* lets allocate a new groupid for this group */
1670 if (!winbind_allocate_gid(&domusers_gid
)) {
1671 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1672 "create Domain Users group!\n"));
1677 uname
= talloc_strdup(tc
, "domusers");
1678 wname
= talloc_strdup(tc
, "Domain Users");
1679 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1680 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domusers_gid
);
1681 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1683 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1684 d_fprintf(stderr
, "Out of Memory!\n");
1688 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1689 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1691 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1692 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1693 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1695 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1696 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1697 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1698 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1699 sid_string_talloc(tc
, &gsid
));
1700 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1702 talloc_autofree_ldapmod(tc
, mods
);
1704 rc
= smbldap_add(ls
, dn
, mods
);
1706 if (rc
!= LDAP_SUCCESS
) {
1707 d_fprintf(stderr
, _("Failed to add Domain Users group "
1708 "to ldap directory\n"));
1712 if (!pdb_getgrsid(&gmap
, gsid
)) {
1713 d_fprintf(stderr
, _("Failed to read just "
1714 "created domain group.\n"));
1717 domusers_gid
= gmap
.gid
;
1721 domusers_gid
= gmap
.gid
;
1722 d_printf(_("found!\n"));
1727 d_printf(_("Checking for Domain Admins group.\n"));
1729 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_ADMINS
);
1731 if (!pdb_getgrsid(&gmap
, gsid
)) {
1732 LDAPMod
**mods
= NULL
;
1740 d_printf(_("Adding the Domain Admins group.\n"));
1742 /* lets allocate a new groupid for this group */
1744 domadmins_gid
= 999;
1746 if (!winbind_allocate_gid(&domadmins_gid
)) {
1747 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1748 "create Domain Admins group!\n"));
1753 uname
= talloc_strdup(tc
, "domadmins");
1754 wname
= talloc_strdup(tc
, "Domain Admins");
1755 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1756 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1757 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1759 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1760 d_fprintf(stderr
, _("Out of Memory!\n"));
1764 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1765 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1767 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1768 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1769 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1771 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1772 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1773 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1774 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1775 sid_string_talloc(tc
, &gsid
));
1776 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1778 talloc_autofree_ldapmod(tc
, mods
);
1780 rc
= smbldap_add(ls
, dn
, mods
);
1782 if (rc
!= LDAP_SUCCESS
) {
1783 d_fprintf(stderr
, _("Failed to add Domain Admins group "
1784 "to ldap directory\n"));
1788 if (!pdb_getgrsid(&gmap
, gsid
)) {
1789 d_fprintf(stderr
, _("Failed to read just "
1790 "created domain group.\n"));
1793 domadmins_gid
= gmap
.gid
;
1797 domadmins_gid
= gmap
.gid
;
1798 d_printf(_("found!\n"));
1803 d_printf(_("Check for Administrator account.\n"));
1805 samuser
= samu_new(tc
);
1807 d_fprintf(stderr
, _("Out of Memory!\n"));
1811 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1812 LDAPMod
**mods
= NULL
;
1824 d_printf(_("Adding the Administrator user.\n"));
1826 if (domadmins_gid
== -1) {
1828 _("Can't create Administrator user, Domain "
1829 "Admins group not available!\n"));
1836 if (!winbind_allocate_uid(&uid
)) {
1838 _("Unable to allocate a new uid to create "
1839 "the Administrator user!\n"));
1844 name
= talloc_strdup(tc
, "Administrator");
1845 dn
= talloc_asprintf(tc
, "uid=Administrator,%s", lp_ldap_user_suffix());
1846 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)uid
);
1847 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1848 dir
= talloc_sub_specified(tc
, lp_template_homedir(),
1850 get_global_sam_name(),
1851 uid
, domadmins_gid
);
1852 shell
= talloc_sub_specified(tc
, lp_template_shell(),
1854 get_global_sam_name(),
1855 uid
, domadmins_gid
);
1857 if (!name
|| !dn
|| !uidstr
|| !gidstr
|| !dir
|| !shell
) {
1858 d_fprintf(stderr
, _("Out of Memory!\n"));
1862 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR
);
1864 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1865 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1866 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1868 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
1869 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
1870 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
1871 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
1872 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
1873 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
1874 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", name
);
1875 princ
= talloc_asprintf(tc
, "%s@%s", name
, lp_realm());
1877 d_fprintf(stderr
, _("Out of Memory!\n"));
1880 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "krbPrincipalName", princ
);
1882 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", name
);
1883 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
1884 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
1885 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1886 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1887 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", dir
);
1888 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
1889 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
1890 sid_string_talloc(tc
, &sid
));
1891 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
1892 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
1893 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1895 talloc_autofree_ldapmod(tc
, mods
);
1897 rc
= smbldap_add(ls
, dn
, mods
);
1899 if (rc
!= LDAP_SUCCESS
) {
1900 d_fprintf(stderr
, _("Failed to add Administrator user "
1901 "to ldap directory\n"));
1905 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1906 d_fprintf(stderr
, _("Failed to read just "
1907 "created user.\n"));
1912 d_printf(_("found!\n"));
1915 d_printf(_("Checking for Guest user.\n"));
1917 samuser
= samu_new(tc
);
1919 d_fprintf(stderr
, _("Out of Memory!\n"));
1923 if (!pdb_getsampwnam(samuser
, lp_guestaccount())) {
1924 LDAPMod
**mods
= NULL
;
1931 d_printf(_("Adding the Guest user.\n"));
1933 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_GUEST
);
1935 pwd
= Get_Pwnam_alloc(tc
, lp_guestaccount());
1938 if (domusers_gid
== -1) {
1940 _("Can't create Guest user, Domain "
1941 "Users group not available!\n"));
1944 if ((pwd
= talloc(tc
, struct passwd
)) == NULL
) {
1945 d_fprintf(stderr
, _("talloc failed\n"));
1948 pwd
->pw_name
= talloc_strdup(pwd
, lp_guestaccount());
1953 if (!winbind_allocate_uid(&(pwd
->pw_uid
))) {
1955 _("Unable to allocate a new uid to "
1956 "create the Guest user!\n"));
1960 pwd
->pw_gid
= domusers_gid
;
1961 pwd
->pw_dir
= talloc_strdup(tc
, "/");
1962 pwd
->pw_shell
= talloc_strdup(tc
, "/bin/false");
1963 if (!pwd
->pw_dir
|| !pwd
->pw_shell
) {
1964 d_fprintf(stderr
, _("Out of Memory!\n"));
1969 dn
= talloc_asprintf(tc
, "uid=%s,%s", pwd
->pw_name
, lp_ldap_user_suffix ());
1970 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_uid
);
1971 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
1972 if (!dn
|| !uidstr
|| !gidstr
) {
1973 d_fprintf(stderr
, _("Out of Memory!\n"));
1977 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1978 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1979 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1981 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
1982 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
1983 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
1984 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
1985 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
1986 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
1987 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", pwd
->pw_name
);
1989 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", pwd
->pw_name
);
1990 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", pwd
->pw_name
);
1991 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", pwd
->pw_name
);
1992 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1993 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1994 if ((pwd
->pw_dir
!= NULL
) && (pwd
->pw_dir
[0] != '\0')) {
1995 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", pwd
->pw_dir
);
1997 if ((pwd
->pw_shell
!= NULL
) && (pwd
->pw_shell
[0] != '\0')) {
1998 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", pwd
->pw_shell
);
2000 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
2001 sid_string_talloc(tc
, &sid
));
2002 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
2003 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
2004 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
2006 talloc_autofree_ldapmod(tc
, mods
);
2008 rc
= smbldap_add(ls
, dn
, mods
);
2010 if (rc
!= LDAP_SUCCESS
) {
2011 d_fprintf(stderr
, _("Failed to add Guest user to "
2012 "ldap directory\n"));
2016 if (!pdb_getsampwnam(samuser
, lp_guestaccount())) {
2017 d_fprintf(stderr
, _("Failed to read just "
2018 "created user.\n"));
2023 d_printf(_("found!\n"));
2026 d_printf(_("Checking Guest's group.\n"));
2028 pwd
= Get_Pwnam_alloc(tc
, lp_guestaccount());
2031 _("Failed to find just created Guest account!\n"
2032 " Is nss properly configured?!\n"));
2036 if (pwd
->pw_gid
== domusers_gid
) {
2037 d_printf(_("found!\n"));
2041 if (!pdb_getgrgid(&gmap
, pwd
->pw_gid
)) {
2042 LDAPMod
**mods
= NULL
;
2050 d_printf(_("Adding the Domain Guests group.\n"));
2052 uname
= talloc_strdup(tc
, "domguests");
2053 wname
= talloc_strdup(tc
, "Domain Guests");
2054 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
2055 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2056 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
2058 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
2059 d_fprintf(stderr
, _("Out of Memory!\n"));
2063 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_GUESTS
);
2065 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
2066 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
2068 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
2069 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
2070 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
2072 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
2073 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
2074 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2075 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
2076 sid_string_talloc(tc
, &gsid
));
2077 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
2079 talloc_autofree_ldapmod(tc
, mods
);
2081 rc
= smbldap_add(ls
, dn
, mods
);
2083 if (rc
!= LDAP_SUCCESS
) {
2085 _("Failed to add Domain Guests group to ldap "
2089 d_printf(_("found!\n"));
2104 /***********************************************************
2105 migrated functionality from smbgroupedit
2106 **********************************************************/
2107 int net_sam(struct net_context
*c
, int argc
, const char **argv
)
2109 struct functable func
[] = {
2111 "createbuiltingroup",
2112 net_sam_createbuiltingroup
,
2113 NET_TRANSPORT_LOCAL
,
2114 N_("Create a new BUILTIN group"),
2115 N_("net sam createbuiltingroup\n"
2116 " Create a new BUILTIN group")
2120 net_sam_createlocalgroup
,
2121 NET_TRANSPORT_LOCAL
,
2122 N_("Create a new local group"),
2123 N_("net sam createlocalgroup\n"
2124 " Create a new local group")
2127 "createdomaingroup",
2128 net_sam_createdomaingroup
,
2129 NET_TRANSPORT_LOCAL
,
2130 N_("Create a new group"),
2131 N_("net sam createdomaingroup\n"
2132 " Create a new group")
2136 net_sam_deletelocalgroup
,
2137 NET_TRANSPORT_LOCAL
,
2138 N_("Delete an existing local group"),
2139 N_("net sam deletelocalgroup\n"
2140 " Delete an existing local group")
2143 "deletedomaingroup",
2144 net_sam_deletedomaingroup
,
2145 NET_TRANSPORT_LOCAL
,
2146 N_("Delete a domain group"),
2147 N_("net sam deletedomaingroup\n"
2152 net_sam_mapunixgroup
,
2153 NET_TRANSPORT_LOCAL
,
2154 N_("Map a unix group to a domain group"),
2155 N_("net sam mapunixgroup\n"
2156 " Map a unix group to a domain group")
2160 net_sam_unmapunixgroup
,
2161 NET_TRANSPORT_LOCAL
,
2162 N_("Remove a group mapping of an unix group to a "
2164 N_("net sam unmapunixgroup\n"
2165 " Remove a group mapping of an unix group to a "
2171 NET_TRANSPORT_LOCAL
,
2172 N_("Add a member to a group"),
2173 N_("net sam addmem\n"
2174 " Add a member to a group")
2179 NET_TRANSPORT_LOCAL
,
2180 N_("Delete a member from a group"),
2181 N_("net sam delmem\n"
2182 " Delete a member from a group")
2187 NET_TRANSPORT_LOCAL
,
2188 N_("List group members"),
2189 N_("net sam listmem\n"
2190 " List group members")
2195 NET_TRANSPORT_LOCAL
,
2196 N_("List users, groups and local groups"),
2198 " List users, groups and local groups")
2203 NET_TRANSPORT_LOCAL
,
2204 N_("Show details of a SAM entry"),
2206 " Show details of a SAM entry")
2211 NET_TRANSPORT_LOCAL
,
2212 N_("Set details of a SAM account"),
2214 " Set details of a SAM account")
2219 NET_TRANSPORT_LOCAL
,
2220 N_("Set account policies"),
2221 N_("net sam policy\n"
2222 " Set account policies")
2227 NET_TRANSPORT_LOCAL
,
2228 N_("Manipulate user privileges"),
2229 N_("net sam rights\n"
2230 " Manipulate user privileges")
2236 NET_TRANSPORT_LOCAL
,
2237 N_("Provision a clean user database"),
2238 N_("net sam privison\n"
2239 " Provision a clear user database")
2242 {NULL
, NULL
, 0, NULL
, NULL
}
2245 if (getuid() != 0) {
2246 d_fprintf(stderr
, _("You are not root, most things won't "
2250 return net_run_function(c
, argc
, argv
, "net sam", func
);