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"
34 #include "lib/util/smb_strtox.h"
35 #include "lib/util/string_wrappers.h"
36 #include "source3/lib/substitute.h"
42 static int net_sam_userset(struct net_context
*c
, int argc
, const char **argv
,
44 bool (*fn
)(struct samu
*, const char *,
45 enum pdb_value_state
))
47 struct samu
*sam_acct
= NULL
;
49 enum lsa_SidType type
;
50 const char *dom
, *name
;
53 if (argc
!= 2 || c
->display_usage
) {
54 d_fprintf(stderr
, "%s\n", _("Usage:"));
55 d_fprintf(stderr
, _("net sam set %s <user> <value>\n"),
60 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
61 &dom
, &name
, &sid
, &type
)) {
62 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
66 if (type
!= SID_NAME_USER
) {
67 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
68 sid_type_lookup(type
));
72 if ( !(sam_acct
= samu_new( NULL
)) ) {
73 d_fprintf(stderr
, _("Internal error\n"));
77 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
78 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
82 if (!fn(sam_acct
, argv
[1], PDB_CHANGED
)) {
83 d_fprintf(stderr
, _("Internal error\n"));
87 status
= pdb_update_sam_account(sam_acct
);
88 if (!NT_STATUS_IS_OK(status
)) {
89 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
90 argv
[0], nt_errstr(status
));
94 TALLOC_FREE(sam_acct
);
96 d_printf(_("Updated %s for %s\\%s to %s\n"), field
, dom
, name
, argv
[1]);
100 static int net_sam_set_fullname(struct net_context
*c
, int argc
,
103 return net_sam_userset(c
, argc
, argv
, "fullname",
107 static int net_sam_set_logonscript(struct net_context
*c
, int argc
,
110 return net_sam_userset(c
, argc
, argv
, "logonscript",
111 pdb_set_logon_script
);
114 static int net_sam_set_profilepath(struct net_context
*c
, int argc
,
117 return net_sam_userset(c
, argc
, argv
, "profilepath",
118 pdb_set_profile_path
);
121 static int net_sam_set_homedrive(struct net_context
*c
, int argc
,
124 return net_sam_userset(c
, argc
, argv
, "homedrive",
128 static int net_sam_set_homedir(struct net_context
*c
, int argc
,
131 return net_sam_userset(c
, argc
, argv
, "homedir",
135 static int net_sam_set_workstations(struct net_context
*c
, int argc
,
138 return net_sam_userset(c
, argc
, argv
, "workstations",
139 pdb_set_workstations
);
146 static int net_sam_set_userflag(struct net_context
*c
, int argc
,
147 const char **argv
, const char *field
,
150 struct samu
*sam_acct
= NULL
;
152 enum lsa_SidType type
;
153 const char *dom
, *name
;
157 if ((argc
!= 2) || c
->display_usage
||
158 (!strequal(argv
[1], "yes") &&
159 !strequal(argv
[1], "no"))) {
160 d_fprintf(stderr
, "%s\n", _("Usage:"));
161 d_fprintf(stderr
, _("net sam set %s <user> [yes|no]\n"),
166 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
167 &dom
, &name
, &sid
, &type
)) {
168 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
172 if (type
!= SID_NAME_USER
) {
173 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
174 sid_type_lookup(type
));
178 if ( !(sam_acct
= samu_new( NULL
)) ) {
179 d_fprintf(stderr
, _("Internal error\n"));
183 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
184 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
188 acct_flags
= pdb_get_acct_ctrl(sam_acct
);
190 if (strequal(argv
[1], "yes")) {
196 pdb_set_acct_ctrl(sam_acct
, acct_flags
, PDB_CHANGED
);
198 status
= pdb_update_sam_account(sam_acct
);
199 if (!NT_STATUS_IS_OK(status
)) {
200 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
201 argv
[0], nt_errstr(status
));
205 TALLOC_FREE(sam_acct
);
207 d_fprintf(stderr
, _("Updated flag %s for %s\\%s to %s\n"), field
, dom
,
212 static int net_sam_set_disabled(struct net_context
*c
, int argc
,
215 return net_sam_set_userflag(c
, argc
, argv
, "disabled", ACB_DISABLED
);
218 static int net_sam_set_pwnotreq(struct net_context
*c
, int argc
,
221 return net_sam_set_userflag(c
, argc
, argv
, "pwnotreq", ACB_PWNOTREQ
);
224 static int net_sam_set_autolock(struct net_context
*c
, int argc
,
227 return net_sam_set_userflag(c
, argc
, argv
, "autolock", ACB_AUTOLOCK
);
230 static int net_sam_set_pwnoexp(struct net_context
*c
, int argc
,
233 return net_sam_set_userflag(c
, argc
, argv
, "pwnoexp", ACB_PWNOEXP
);
237 * Set pass last change time, based on force pass change now
240 static int net_sam_set_pwdmustchangenow(struct net_context
*c
, int argc
,
243 struct samu
*sam_acct
= NULL
;
245 enum lsa_SidType type
;
246 const char *dom
, *name
;
249 if ((argc
!= 2) || c
->display_usage
||
250 (!strequal(argv
[1], "yes") &&
251 !strequal(argv
[1], "no"))) {
252 d_fprintf(stderr
, "%s\n%s",
254 _("net sam set pwdmustchangenow <user> [yes|no]\n"));
258 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
259 &dom
, &name
, &sid
, &type
)) {
260 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
264 if (type
!= SID_NAME_USER
) {
265 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
266 sid_type_lookup(type
));
270 if ( !(sam_acct
= samu_new( NULL
)) ) {
271 d_fprintf(stderr
, _("Internal error\n"));
275 if (!pdb_getsampwsid(sam_acct
, &sid
)) {
276 d_fprintf(stderr
, _("Loading user %s failed\n"), argv
[0]);
280 if (strequal(argv
[1], "yes")) {
281 pdb_set_pass_last_set_time(sam_acct
, 0, PDB_CHANGED
);
283 pdb_set_pass_last_set_time(sam_acct
, time(NULL
), PDB_CHANGED
);
286 status
= pdb_update_sam_account(sam_acct
);
287 if (!NT_STATUS_IS_OK(status
)) {
288 d_fprintf(stderr
, _("Updating sam account %s failed with %s\n"),
289 argv
[0], nt_errstr(status
));
293 TALLOC_FREE(sam_acct
);
295 d_fprintf(stderr
, _("Updated 'user must change password at next logon' "
296 "for %s\\%s to %s\n"), dom
,
303 * Set a user's or a group's comment
306 static int net_sam_set_comment(struct net_context
*c
, int argc
,
311 enum lsa_SidType type
;
312 const char *dom
, *name
;
315 if (argc
!= 2 || c
->display_usage
) {
316 d_fprintf(stderr
, "%s\n%s",
318 _("net sam set comment <name> <comment>\n"));
322 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
323 &dom
, &name
, &sid
, &type
)) {
324 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
328 if (type
== SID_NAME_USER
) {
329 return net_sam_userset(c
, argc
, argv
, "comment",
333 if ((type
!= SID_NAME_DOM_GRP
) && (type
!= SID_NAME_ALIAS
) &&
334 (type
!= SID_NAME_WKN_GRP
)) {
335 d_fprintf(stderr
, _("%s is a %s, not a group\n"), argv
[0],
336 sid_type_lookup(type
));
340 map
= talloc_zero(talloc_tos(), GROUP_MAP
);
342 d_fprintf(stderr
, _("Out of memory!\n"));
346 if (!pdb_getgrsid(map
, sid
)) {
347 d_fprintf(stderr
, _("Could not load group %s\n"), argv
[0]);
351 map
->comment
= talloc_strdup(map
, argv
[1]);
353 d_fprintf(stderr
, _("Out of memory!\n"));
357 status
= pdb_update_group_mapping_entry(map
);
359 if (!NT_STATUS_IS_OK(status
)) {
360 d_fprintf(stderr
, _("Updating group mapping entry failed with "
361 "%s\n"), nt_errstr(status
));
365 d_printf("Updated comment of group %s\\%s to %s\n", dom
, name
,
372 static int net_sam_set(struct net_context
*c
, int argc
, const char **argv
)
374 struct functable func
[] = {
379 N_("Change a user's home directory"),
380 N_("net sam set homedir\n"
381 " Change a user's home directory")
385 net_sam_set_profilepath
,
387 N_("Change a user's profile path"),
388 N_("net sam set profilepath\n"
389 " Change a user's profile path")
395 N_("Change a users or groups description"),
396 N_("net sam set comment\n"
397 " Change a users or groups description")
401 net_sam_set_fullname
,
403 N_("Change a user's full name"),
404 N_("net sam set fullname\n"
405 " Change a user's full name")
409 net_sam_set_logonscript
,
411 N_("Change a user's logon script"),
412 N_("net sam set logonscript\n"
413 " Change a user's logon script")
417 net_sam_set_homedrive
,
419 N_("Change a user's home drive"),
420 N_("net sam set homedrive\n"
421 " Change a user's home drive")
425 net_sam_set_workstations
,
427 N_("Change a user's allowed workstations"),
428 N_("net sam set workstations\n"
429 " Change a user's allowed workstations")
433 net_sam_set_disabled
,
435 N_("Disable/Enable a user"),
436 N_("net sam set disable\n"
437 " Disable/Enable a user")
441 net_sam_set_pwnotreq
,
443 N_("Disable/Enable the password not required flag"),
444 N_("net sam set pwnotreq\n"
445 " Disable/Enable the password not required flag")
449 net_sam_set_autolock
,
451 N_("Disable/Enable a user's lockout flag"),
452 N_("net sam set autolock\n"
453 " Disable/Enable a user's lockout flag")
459 N_("Disable/Enable whether a user's pw does not "
461 N_("net sam set pwnoexp\n"
462 " Disable/Enable whether a user's pw does not "
467 net_sam_set_pwdmustchangenow
,
469 N_("Force users password must change at next logon"),
470 N_("net sam set pwdmustchangenow\n"
471 " Force users password must change at next logon")
473 {NULL
, NULL
, 0, NULL
, NULL
}
476 return net_run_function(c
, argc
, argv
, "net sam set", func
);
480 * Manage account policies
483 static int net_sam_policy_set(struct net_context
*c
, int argc
, const char **argv
)
485 const char *account_policy
= NULL
;
487 uint32_t old_value
= 0;
488 enum pdb_policy_type field
;
491 if (argc
!= 2 || c
->display_usage
) {
492 d_fprintf(stderr
, "%s\n%s",
494 _("net sam policy set \"<account policy>\" <value>\n"));
498 account_policy
= argv
[0];
499 field
= account_policy_name_to_typenum(account_policy
);
501 if (strequal(argv
[1], "forever") || strequal(argv
[1], "never")
502 || strequal(argv
[1], "off")) {
506 value
= smb_strtoul(argv
[1],
510 SMB_STR_FULL_STR_CONV
);
513 d_printf(_("Unable to set policy \"%s\"! Invalid value "
515 account_policy
, argv
[1]);
524 account_policy_names_list(talloc_tos(), &names
, &count
);
525 d_fprintf(stderr
, _("No account policy \"%s\"!\n\n"), argv
[0]);
526 d_fprintf(stderr
, _("Valid account policies are:\n"));
528 for (i
=0; i
<count
; i
++) {
529 d_fprintf(stderr
, "%s\n", names
[i
]);
537 if (!pdb_get_account_policy(field
, &old_value
)) {
538 d_fprintf(stderr
, _("Valid account policy, but unable to fetch "
541 d_printf(_("Account policy \"%s\" value was: %d\n"),
542 account_policy
, old_value
);
545 if (!pdb_set_account_policy(field
, value
)) {
546 d_fprintf(stderr
, _("Valid account policy, but unable to "
550 d_printf(_("Account policy \"%s\" value is now: %d\n"),
551 account_policy
, value
);
557 static int net_sam_policy_show(struct net_context
*c
, int argc
, const char **argv
)
559 const char *account_policy
= NULL
;
561 enum pdb_policy_type field
;
563 if (argc
!= 1 || c
->display_usage
) {
564 d_fprintf(stderr
, "%s\n%s",
566 _("net sam policy show \"<account policy>\"\n"));
570 account_policy
= argv
[0];
571 field
= account_policy_name_to_typenum(account_policy
);
577 account_policy_names_list(talloc_tos(), &names
, &count
);
578 d_fprintf(stderr
, _("No account policy by that name!\n"));
580 d_fprintf(stderr
, _("Valid account policies "
582 for (i
=0; i
<count
; i
++) {
583 d_fprintf(stderr
, "%s\n", names
[i
]);
590 if (!pdb_get_account_policy(field
, &old_value
)) {
591 fprintf(stderr
, _("Valid account policy, but unable to "
596 printf(_("Account policy \"%s\" description: %s\n"),
597 account_policy
, account_policy_get_desc(field
));
598 printf(_("Account policy \"%s\" value is: %d\n"), account_policy
,
603 static int net_sam_policy_list(struct net_context
*c
, int argc
, const char **argv
)
609 if (c
->display_usage
) {
611 "net sam policy list\n"
614 _("List account policies"));
618 account_policy_names_list(talloc_tos(), &names
, &count
);
620 d_fprintf(stderr
, _("Valid account policies "
622 for (i
= 0; i
< count
; i
++) {
623 d_fprintf(stderr
, "%s\n", names
[i
]);
630 static int net_sam_policy(struct net_context
*c
, int argc
, const char **argv
)
632 struct functable func
[] = {
637 N_("List account policies"),
638 N_("net sam policy list\n"
639 " List account policies")
645 N_("Show account policies"),
646 N_("net sam policy show\n"
647 " Show account policies")
653 N_("Change account policies"),
654 N_("net sam policy set\n"
655 " Change account policies")
657 {NULL
, NULL
, 0, NULL
, NULL
}
660 return net_run_function(c
, argc
, argv
, "net sam policy", func
);
663 static int net_sam_rights_list(struct net_context
*c
, int argc
,
666 enum sec_privilege privilege
;
668 if (argc
> 1 || c
->display_usage
) {
669 d_fprintf(stderr
, "%s\n%s",
671 _("net sam rights list [privilege name]\n"));
677 int num
= num_privileges_in_short_list();
679 for (i
=0; i
<num
; i
++) {
680 d_printf("%s\n", sec_privilege_name_from_index(i
));
685 privilege
= sec_privilege_id(argv
[0]);
687 if (privilege
!= SEC_PRIV_INVALID
) {
688 struct dom_sid
*sids
;
692 status
= privilege_enum_sids(privilege
, talloc_tos(),
694 if (!NT_STATUS_IS_OK(status
)) {
695 d_fprintf(stderr
, _("Could not list rights: %s\n"),
700 for (i
=0; i
<num_sids
; i
++) {
701 const char *dom
, *name
;
702 enum lsa_SidType type
;
703 struct dom_sid_buf buf
;
705 if (lookup_sid(talloc_tos(), &sids
[i
], &dom
, &name
,
707 d_printf("%s\\%s\n", dom
, name
);
711 dom_sid_str_buf(&sids
[i
], &buf
));
720 static int net_sam_rights_grant(struct net_context
*c
, int argc
,
724 enum lsa_SidType type
;
725 const char *dom
, *name
;
728 if (argc
< 2 || c
->display_usage
) {
729 d_fprintf(stderr
, "%s\n%s",
731 _("net sam rights grant <name> <rights> ...\n"));
735 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
736 &dom
, &name
, &sid
, &type
)) {
737 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
741 for (i
=1; i
< argc
; i
++) {
742 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
743 if (privilege
== SEC_PRIV_INVALID
) {
744 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
748 if (!grant_privilege_by_name(&sid
, argv
[i
])) {
749 d_fprintf(stderr
, _("Could not grant privilege\n"));
753 d_printf(_("Granted %s to %s\\%s\n"), argv
[i
], dom
, name
);
759 static int net_sam_rights_revoke(struct net_context
*c
, int argc
,
763 enum lsa_SidType type
;
764 const char *dom
, *name
;
767 if (argc
< 2 || c
->display_usage
) {
768 d_fprintf(stderr
, "%s\n%s",
770 _("net sam rights revoke <name> <rights>\n"));
774 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
775 &dom
, &name
, &sid
, &type
)) {
776 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
780 for (i
=1; i
< argc
; i
++) {
781 enum sec_privilege privilege
= sec_privilege_id(argv
[i
]);
782 if (privilege
== SEC_PRIV_INVALID
) {
783 d_fprintf(stderr
, _("%s unknown\n"), argv
[i
]);
787 if (!revoke_privilege_by_name(&sid
, argv
[i
])) {
788 d_fprintf(stderr
, _("Could not revoke privilege\n"));
792 d_printf(_("Revoked %s from %s\\%s\n"), argv
[i
], dom
, name
);
798 static int net_sam_rights(struct net_context
*c
, int argc
, const char **argv
)
800 struct functable func
[] = {
805 N_("List possible user rights"),
806 N_("net sam rights list\n"
807 " List possible user rights")
811 net_sam_rights_grant
,
813 N_("Grant right(s)"),
814 N_("net sam rights grant\n"
819 net_sam_rights_revoke
,
821 N_("Revoke right(s)"),
822 N_("net sam rights revoke\n"
825 {NULL
, NULL
, 0, NULL
, NULL
}
827 return net_run_function(c
, argc
, argv
, "net sam rights", func
);
831 * Map a unix group to a domain group
834 static NTSTATUS
map_unix_group(const struct group
*grp
, GROUP_MAP
*map
)
836 const char *dom
, *name
;
839 if (pdb_getgrgid(map
, grp
->gr_gid
)) {
840 return NT_STATUS_GROUP_EXISTS
;
843 map
->gid
= grp
->gr_gid
;
845 if (lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
846 &dom
, &name
, NULL
, NULL
)) {
848 map
->nt_name
= talloc_asprintf(map
, "Unix Group %s",
851 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
852 grp
->gr_name
, dom
, name
, map
->nt_name
));
855 if (lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
856 NULL
, NULL
, NULL
, NULL
)) {
857 DEBUG(3, ("\"%s\" exists, can't map it\n", grp
->gr_name
));
858 return NT_STATUS_GROUP_EXISTS
;
861 if (pdb_capabilities() & PDB_CAP_STORE_RIDS
) {
862 if (!pdb_new_rid(&rid
)) {
863 DEBUG(3, ("Could not get a new RID for %s\n",
865 return NT_STATUS_ACCESS_DENIED
;
868 rid
= algorithmic_pdb_gid_to_group_rid( grp
->gr_gid
);
871 sid_compose(&map
->sid
, get_global_sam_sid(), rid
);
872 map
->sid_name_use
= SID_NAME_DOM_GRP
;
873 map
->comment
= talloc_asprintf(map
, "Unix Group %s", grp
->gr_name
);
875 return pdb_add_group_mapping_entry(map
);
878 static int net_sam_mapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
883 struct dom_sid_buf buf
;
885 if (argc
!= 1 || c
->display_usage
) {
886 d_fprintf(stderr
, "%s\n%s",
888 _("net sam mapunixgroup <name>\n"));
892 grp
= getgrnam(argv
[0]);
894 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
898 map
= talloc_zero(talloc_tos(), GROUP_MAP
);
900 d_fprintf(stderr
, _("Out of memory!\n"));
904 status
= map_unix_group(grp
, map
);
906 if (!NT_STATUS_IS_OK(status
)) {
907 d_fprintf(stderr
, _("Mapping group %s failed with %s\n"),
908 argv
[0], nt_errstr(status
));
912 d_printf(_("Mapped unix group %s to SID %s\n"), argv
[0],
913 dom_sid_str_buf(&map
->sid
, &buf
));
920 * Remove a group mapping
923 static NTSTATUS
unmap_unix_group(const struct group
*grp
)
925 struct dom_sid dom_sid
;
928 if (!lookup_name(talloc_tos(), grp
->gr_name
, LOOKUP_NAME_LOCAL
,
929 NULL
, NULL
, NULL
, NULL
)) {
930 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp
->gr_name
));
931 return NT_STATUS_NO_SUCH_GROUP
;
935 id
.type
= ID_TYPE_GID
;
936 if (!pdb_id_to_sid(&id
, &dom_sid
)) {
937 return NT_STATUS_UNSUCCESSFUL
;
940 return pdb_delete_group_mapping_entry(dom_sid
);
943 static int net_sam_unmapunixgroup(struct net_context
*c
, int argc
, const char **argv
)
948 if (argc
!= 1 || c
->display_usage
) {
949 d_fprintf(stderr
, "%s\n%s",
951 _("net sam unmapunixgroup <name>\n"));
955 grp
= getgrnam(argv
[0]);
957 d_fprintf(stderr
, _("Could not find mapping for group %s.\n"),
962 status
= unmap_unix_group(grp
);
964 if (!NT_STATUS_IS_OK(status
)) {
965 d_fprintf(stderr
, _("Unmapping group %s failed with %s.\n"),
966 argv
[0], nt_errstr(status
));
970 d_printf(_("Unmapped unix group %s.\n"), argv
[0]);
976 * Create a domain group
979 static int net_sam_createdomaingroup(struct net_context
*c
, int argc
,
985 if (argc
!= 1 || c
->display_usage
) {
986 d_fprintf(stderr
, "%s\n%s",
988 _("net sam createdomaingroup <name>\n"));
992 status
= pdb_create_dom_group(talloc_tos(), argv
[0], &rid
);
994 if (!NT_STATUS_IS_OK(status
)) {
995 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
996 argv
[0], nt_errstr(status
));
1000 d_printf(_("Created domain group %s with RID %d\n"), argv
[0], rid
);
1006 * Delete a domain group
1009 static int net_sam_deletedomaingroup(struct net_context
*c
, int argc
,
1014 enum lsa_SidType type
;
1015 const char *dom
, *name
;
1018 if (argc
!= 1 || c
->display_usage
) {
1019 d_fprintf(stderr
, "%s\n%s",
1021 _("net sam deletelocalgroup <name>\n"));
1025 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1026 &dom
, &name
, &sid
, &type
)) {
1027 d_fprintf(stderr
, _("Could not find %s.\n"), argv
[0]);
1031 if (type
!= SID_NAME_DOM_GRP
) {
1032 d_fprintf(stderr
, _("%s is a %s, not a domain group.\n"),
1033 argv
[0], sid_type_lookup(type
));
1037 sid_peek_rid(&sid
, &rid
);
1039 status
= pdb_delete_dom_group(talloc_tos(), rid
);
1041 if (!NT_STATUS_IS_OK(status
)) {
1042 d_fprintf(stderr
,_("Deleting domain group %s failed with %s\n"),
1043 argv
[0], nt_errstr(status
));
1047 d_printf(_("Deleted domain group %s.\n"), argv
[0]);
1053 * Create a local group
1056 static int net_sam_createlocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1061 if (argc
!= 1 || c
->display_usage
) {
1062 d_fprintf(stderr
, "%s\n%s",
1064 _("net sam createlocalgroup <name>\n"));
1068 if (!winbind_ping()) {
1069 d_fprintf(stderr
, _("winbind seems not to run. "
1070 "createlocalgroup only works when winbind runs.\n"));
1074 status
= pdb_create_alias(argv
[0], &rid
);
1076 if (!NT_STATUS_IS_OK(status
)) {
1077 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1078 argv
[0], nt_errstr(status
));
1082 d_printf(_("Created local group %s with RID %d\n"), argv
[0], rid
);
1088 * Delete a local group
1091 static int net_sam_deletelocalgroup(struct net_context
*c
, int argc
, const char **argv
)
1094 enum lsa_SidType type
;
1095 const char *dom
, *name
;
1098 if (argc
!= 1 || c
->display_usage
) {
1099 d_fprintf(stderr
, "%s\n%s",
1101 _("net sam deletelocalgroup <name>\n"));
1105 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1106 &dom
, &name
, &sid
, &type
)) {
1107 d_fprintf(stderr
,_("Could not find %s.\n"), argv
[0]);
1111 if (type
!= SID_NAME_ALIAS
) {
1112 d_fprintf(stderr
, _("%s is a %s, not a local group.\n"),argv
[0],
1113 sid_type_lookup(type
));
1117 status
= pdb_delete_alias(&sid
);
1119 if (!NT_STATUS_IS_OK(status
)) {
1120 d_fprintf(stderr
, _("Deleting local group %s failed with %s\n"),
1121 argv
[0], nt_errstr(status
));
1125 d_printf(_("Deleted local group %s.\n"), argv
[0]);
1131 * Create a builtin group
1134 static int net_sam_createbuiltingroup(struct net_context
*c
, int argc
, const char **argv
)
1138 enum lsa_SidType type
;
1142 if (argc
!= 1 || c
->display_usage
) {
1143 d_fprintf(stderr
, "%s\n%s",
1145 _("net sam createbuiltingroup <name>\n"));
1149 if (!winbind_ping()) {
1150 d_fprintf(stderr
, _("winbind seems not to run. "
1151 "createbuiltingroup only works when winbind "
1156 /* validate the name and get the group */
1158 fstrcpy( groupname
, "BUILTIN\\" );
1159 fstrcat( groupname
, argv
[0] );
1161 if ( !lookup_name(talloc_tos(), groupname
, LOOKUP_NAME_ALL
, NULL
,
1162 NULL
, &sid
, &type
)) {
1163 d_fprintf(stderr
, _("%s is not a BUILTIN group\n"), argv
[0]);
1167 if ( !sid_peek_rid( &sid
, &rid
) ) {
1168 d_fprintf(stderr
, _("Failed to get RID for %s\n"), argv
[0]);
1172 status
= pdb_create_builtin(rid
);
1174 if (!NT_STATUS_IS_OK(status
)) {
1175 d_fprintf(stderr
, _("Creating %s failed with %s\n"),
1176 argv
[0], nt_errstr(status
));
1180 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv
[0], rid
);
1186 * Add a group member
1189 static int net_sam_addmem(struct net_context
*c
, int argc
, const char **argv
)
1191 const char *groupdomain
, *groupname
, *memberdomain
, *membername
;
1192 struct dom_sid group
, member
;
1193 enum lsa_SidType grouptype
, membertype
;
1196 if (argc
!= 2 || c
->display_usage
) {
1197 d_fprintf(stderr
, "%s\n%s",
1199 _("net sam addmem <group> <member>\n"));
1203 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1204 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1205 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1209 /* check to see if the member to be added is a name or a SID */
1211 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1212 &memberdomain
, &membername
, &member
, &membertype
))
1214 /* try it as a SID */
1216 if ( !string_to_sid( &member
, argv
[1] ) ) {
1217 d_fprintf(stderr
, _("Could not find member %s\n"),
1222 if ( !lookup_sid(talloc_tos(), &member
, &memberdomain
,
1223 &membername
, &membertype
) )
1225 d_fprintf(stderr
, _("Could not resolve SID %s\n"),
1231 if ((grouptype
== SID_NAME_ALIAS
) || (grouptype
== SID_NAME_WKN_GRP
)) {
1232 if ((membertype
!= SID_NAME_USER
) &&
1233 (membertype
!= SID_NAME_ALIAS
) &&
1234 (membertype
!= SID_NAME_DOM_GRP
)) {
1235 d_fprintf(stderr
, _("Can't add %s: only users, domain "
1236 "groups and domain local groups "
1237 "can be added. %s is a %s\n"),
1239 sid_type_lookup(membertype
));
1242 status
= pdb_add_aliasmem(&group
, &member
);
1244 if (!NT_STATUS_IS_OK(status
)) {
1245 d_fprintf(stderr
, _("Adding local group member failed "
1246 "with %s\n"), nt_errstr(status
));
1249 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1250 uint32_t grouprid
, memberrid
;
1252 sid_peek_rid(&group
, &grouprid
);
1253 sid_peek_rid(&member
, &memberrid
);
1255 status
= pdb_add_groupmem(talloc_tos(), grouprid
, memberrid
);
1256 if (!NT_STATUS_IS_OK(status
)) {
1257 d_fprintf(stderr
, _("Adding domain group member failed "
1258 "with %s\n"), nt_errstr(status
));
1262 d_fprintf(stderr
, _("Can only add members to local groups so "
1263 "far, %s is a %s\n"), argv
[0],
1264 sid_type_lookup(grouptype
));
1268 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain
, membername
,
1269 groupdomain
, groupname
);
1275 * Delete a group member
1278 static int net_sam_delmem(struct net_context
*c
, int argc
, const char **argv
)
1280 const char *groupdomain
, *groupname
;
1281 const char *memberdomain
= NULL
;
1282 const char *membername
= NULL
;
1283 struct dom_sid group
, member
;
1284 enum lsa_SidType grouptype
;
1287 if (argc
!= 2 || c
->display_usage
) {
1288 d_fprintf(stderr
,"%s\n%s",
1290 _("net sam delmem <group> <member>\n"));
1294 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1295 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1296 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1300 if (!lookup_name(talloc_tos(), argv
[1], LOOKUP_NAME_LOCAL
,
1301 &memberdomain
, &membername
, &member
, NULL
)) {
1302 if (!string_to_sid(&member
, argv
[1])) {
1303 d_fprintf(stderr
, _("Could not find member %s\n"),
1309 if ((grouptype
== SID_NAME_ALIAS
) ||
1310 (grouptype
== SID_NAME_WKN_GRP
)) {
1311 status
= pdb_del_aliasmem(&group
, &member
);
1313 if (!NT_STATUS_IS_OK(status
)) {
1314 d_fprintf(stderr
,_("Deleting local group member failed "
1315 "with %s\n"), nt_errstr(status
));
1318 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1319 uint32_t grouprid
, memberrid
;
1321 sid_peek_rid(&group
, &grouprid
);
1322 sid_peek_rid(&member
, &memberrid
);
1324 status
= pdb_del_groupmem(talloc_tos(), grouprid
, memberrid
);
1325 if (!NT_STATUS_IS_OK(status
)) {
1326 d_fprintf(stderr
, _("Deleting domain group member "
1327 "failed with %s\n"), nt_errstr(status
));
1331 d_fprintf(stderr
, _("Can only delete members from local groups "
1332 "so far, %s is a %s\n"), argv
[0],
1333 sid_type_lookup(grouptype
));
1337 if (membername
!= NULL
) {
1338 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1339 memberdomain
, membername
, groupdomain
, groupname
);
1341 struct dom_sid_buf buf
;
1342 d_printf(_("Deleted %s from %s\\%s\n"),
1343 dom_sid_str_buf(&member
, &buf
),
1352 * List group members
1355 static int net_sam_listmem(struct net_context
*c
, int argc
, const char **argv
)
1357 const char *groupdomain
, *groupname
;
1358 struct dom_sid group
;
1359 struct dom_sid
*members
= NULL
;
1360 size_t i
, num_members
= 0;
1361 enum lsa_SidType grouptype
;
1364 if (argc
!= 1 || c
->display_usage
) {
1365 d_fprintf(stderr
, "%s\n%s",
1367 _("net sam listmem <group>\n"));
1371 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1372 &groupdomain
, &groupname
, &group
, &grouptype
)) {
1373 d_fprintf(stderr
, _("Could not find group %s\n"), argv
[0]);
1377 if ((grouptype
== SID_NAME_ALIAS
) ||
1378 (grouptype
== SID_NAME_WKN_GRP
)) {
1379 status
= pdb_enum_aliasmem(&group
, talloc_tos(), &members
,
1381 if (!NT_STATUS_IS_OK(status
)) {
1382 d_fprintf(stderr
, _("Listing group members failed with "
1383 "%s\n"), nt_errstr(status
));
1386 } else if (grouptype
== SID_NAME_DOM_GRP
) {
1389 status
= pdb_enum_group_members(talloc_tos(), &group
,
1390 &rids
, &num_members
);
1391 if (!NT_STATUS_IS_OK(status
)) {
1392 d_fprintf(stderr
, _("Listing group members failed with "
1393 "%s\n"), nt_errstr(status
));
1397 members
= talloc_array(talloc_tos(), struct dom_sid
,
1399 if (members
== NULL
) {
1404 for (i
=0; i
<num_members
; i
++) {
1405 sid_compose(&members
[i
], get_global_sam_sid(),
1410 d_fprintf(stderr
,_("Can only list local group members so far.\n"
1411 "%s is a %s\n"), argv
[0], sid_type_lookup(grouptype
));
1415 d_printf(_("%s\\%s has %u members\n"), groupdomain
, groupname
,
1416 (unsigned int)num_members
);
1417 for (i
=0; i
<num_members
; i
++) {
1418 const char *dom
, *name
;
1419 if (lookup_sid(talloc_tos(), &members
[i
], &dom
, &name
, NULL
)) {
1420 d_printf(" %s\\%s\n", dom
, name
);
1422 struct dom_sid_buf buf
;
1424 dom_sid_str_buf(&members
[i
], &buf
));
1428 TALLOC_FREE(members
);
1436 static int net_sam_do_list(struct net_context
*c
, int argc
, const char **argv
,
1437 struct pdb_search
*search
, const char *what
)
1439 bool verbose
= (argc
== 1);
1441 if ((argc
> 1) || c
->display_usage
||
1442 ((argc
== 1) && !strequal(argv
[0], "verbose"))) {
1443 d_fprintf(stderr
, "%s\n", _("Usage:"));
1444 d_fprintf(stderr
, _("net sam list %s [verbose]\n"), what
);
1448 if (search
== NULL
) {
1449 d_fprintf(stderr
, _("Could not start search\n"));
1454 struct samr_displayentry entry
;
1455 if (!search
->next_entry(search
, &entry
)) {
1459 d_printf("%s:%d:%s\n",
1464 d_printf("%s\n", entry
.account_name
);
1468 TALLOC_FREE(search
);
1472 static int net_sam_list_users(struct net_context
*c
, int argc
,
1475 return net_sam_do_list(c
, argc
, argv
,
1476 pdb_search_users(talloc_tos(), ACB_NORMAL
),
1480 static int net_sam_list_groups(struct net_context
*c
, int argc
,
1483 return net_sam_do_list(c
, argc
, argv
, pdb_search_groups(talloc_tos()),
1487 static int net_sam_list_localgroups(struct net_context
*c
, int argc
,
1490 return net_sam_do_list(c
, argc
, argv
,
1491 pdb_search_aliases(talloc_tos(),
1492 get_global_sam_sid()),
1496 static int net_sam_list_builtin(struct net_context
*c
, int argc
,
1499 return net_sam_do_list(c
, argc
, argv
,
1500 pdb_search_aliases(talloc_tos(),
1501 &global_sid_Builtin
),
1505 static int net_sam_list_workstations(struct net_context
*c
, int argc
,
1508 return net_sam_do_list(c
, argc
, argv
,
1509 pdb_search_users(talloc_tos(), ACB_WSTRUST
),
1517 static int net_sam_list(struct net_context
*c
, int argc
, const char **argv
)
1519 struct functable func
[] = {
1523 NET_TRANSPORT_LOCAL
,
1524 N_("List SAM users"),
1525 N_("net sam list users\n"
1530 net_sam_list_groups
,
1531 NET_TRANSPORT_LOCAL
,
1532 N_("List SAM groups"),
1533 N_("net sam list groups\n"
1538 net_sam_list_localgroups
,
1539 NET_TRANSPORT_LOCAL
,
1540 N_("List SAM local groups"),
1541 N_("net sam list localgroups\n"
1542 " List SAM local groups")
1546 net_sam_list_builtin
,
1547 NET_TRANSPORT_LOCAL
,
1548 N_("List builtin groups"),
1549 N_("net sam list builtin\n"
1550 " List builtin groups")
1554 net_sam_list_workstations
,
1555 NET_TRANSPORT_LOCAL
,
1556 N_("List domain member workstations"),
1557 N_("net sam list workstations\n"
1558 " List domain member workstations")
1560 {NULL
, NULL
, 0, NULL
, NULL
}
1563 return net_run_function(c
, argc
, argv
, "net sam list", func
);
1567 * Show details of SAM entries
1570 static int net_sam_show(struct net_context
*c
, int argc
, const char **argv
)
1573 struct dom_sid_buf buf
;
1574 enum lsa_SidType type
;
1575 const char *dom
, *name
;
1577 if (argc
!= 1 || c
->display_usage
) {
1578 d_fprintf(stderr
, "%s\n%s",
1580 _("net sam show <name>\n"));
1584 if (!lookup_name(talloc_tos(), argv
[0], LOOKUP_NAME_LOCAL
,
1585 &dom
, &name
, &sid
, &type
)) {
1586 d_fprintf(stderr
, _("Could not find name %s\n"), argv
[0]);
1590 d_printf(_("%s\\%s is a %s with SID %s\n"), dom
, name
,
1591 sid_type_lookup(type
), dom_sid_str_buf(&sid
, &buf
));
1599 * Init an LDAP tree with default users and Groups
1600 * if ldapsam:editposix is enabled
1603 static int net_sam_provision(struct net_context
*c
, int argc
, const char **argv
)
1607 char *ldap_uri
= NULL
;
1609 struct smbldap_state
*state
= NULL
;
1610 GROUP_MAP
*gmap
= NULL
;
1611 struct dom_sid gsid
;
1612 gid_t domusers_gid
= -1;
1613 gid_t domadmins_gid
= -1;
1614 struct samu
*samuser
;
1616 bool is_ipa
= false;
1617 char *bind_dn
= NULL
;
1618 char *bind_secret
= NULL
;
1621 if (c
->display_usage
) {
1623 "net sam provision\n"
1626 _("Init an LDAP tree with default users/groups"));
1630 tc
= talloc_new(NULL
);
1632 d_fprintf(stderr
, _("Out of Memory!\n"));
1636 if ((ldap_bk
= talloc_strdup(tc
, lp_passdb_backend())) == NULL
) {
1637 d_fprintf(stderr
, _("talloc failed\n"));
1641 p
= strchr(ldap_bk
, ':');
1644 ldap_uri
= talloc_strdup(tc
, p
+1);
1645 trim_char(ldap_uri
, ' ', ' ');
1648 trim_char(ldap_bk
, ' ', ' ');
1650 if (strcmp(ldap_bk
, "IPA_ldapsam") == 0 ) {
1654 if (strcmp(ldap_bk
, "ldapsam") != 0 && !is_ipa
) {
1656 _("Provisioning works only with ldapsam backend\n"));
1660 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1661 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1663 d_fprintf(stderr
, _("Provisioning works only if ldapsam:trusted"
1664 " and ldapsam:editposix are enabled.\n"));
1668 if (!is_ipa
&& !winbind_ping()) {
1669 d_fprintf(stderr
, _("winbind seems not to run. Provisioning "
1670 "LDAP only works when winbind runs.\n"));
1674 if (!fetch_ldap_pw(&bind_dn
, &bind_secret
)) {
1675 d_fprintf(stderr
, _("Failed to retrieve LDAP password from secrets.tdb\n"));
1679 status
= smbldap_init(tc
, NULL
, ldap_uri
, false, bind_dn
, bind_secret
, &state
);
1681 BURN_FREE_STR(bind_secret
);
1684 if (!NT_STATUS_IS_OK(status
)) {
1685 d_fprintf(stderr
, _("Unable to connect to the LDAP server.\n"));
1689 d_printf(_("Checking for Domain Users group.\n"));
1691 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_USERS
);
1693 gmap
= talloc_zero(tc
, GROUP_MAP
);
1695 d_printf(_("Out of memory!\n"));
1699 if (!pdb_getgrsid(gmap
, gsid
)) {
1700 struct dom_sid_buf gsid_str
;
1701 LDAPMod
**mods
= NULL
;
1709 d_printf(_("Adding the Domain Users group.\n"));
1711 /* lets allocate a new groupid for this group */
1715 if (!winbind_allocate_gid(&domusers_gid
)) {
1716 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1717 "create Domain Users group!\n"));
1722 uname
= talloc_strdup(tc
, "domusers");
1723 wname
= talloc_strdup(tc
, "Domain Users");
1724 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domusers",
1725 lp_ldap_group_suffix(talloc_tos()));
1726 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domusers_gid
);
1727 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1729 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1730 d_fprintf(stderr
, "Out of Memory!\n");
1734 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1735 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1737 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1738 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1739 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1741 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1742 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1743 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1744 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1745 dom_sid_str_buf(&gsid
, &gsid_str
));
1746 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1748 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1750 rc
= smbldap_add(state
, dn
, mods
);
1752 if (rc
!= LDAP_SUCCESS
) {
1753 d_fprintf(stderr
, _("Failed to add Domain Users group "
1754 "to ldap directory\n"));
1758 if (!pdb_getgrsid(gmap
, gsid
)) {
1759 d_fprintf(stderr
, _("Failed to read just "
1760 "created domain group.\n"));
1763 domusers_gid
= gmap
->gid
;
1767 domusers_gid
= gmap
->gid
;
1768 d_printf(_("found!\n"));
1773 d_printf(_("Checking for Domain Admins group.\n"));
1775 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_ADMINS
);
1777 if (!pdb_getgrsid(gmap
, gsid
)) {
1778 struct dom_sid_buf gsid_str
;
1779 LDAPMod
**mods
= NULL
;
1787 d_printf(_("Adding the Domain Admins group.\n"));
1789 /* lets allocate a new groupid for this group */
1791 domadmins_gid
= 999;
1793 if (!winbind_allocate_gid(&domadmins_gid
)) {
1794 d_fprintf(stderr
, _("Unable to allocate a new gid to "
1795 "create Domain Admins group!\n"));
1800 uname
= talloc_strdup(tc
, "domadmins");
1801 wname
= talloc_strdup(tc
, "Domain Admins");
1802 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domadmins",
1803 lp_ldap_group_suffix(talloc_tos()));
1804 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1805 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
1807 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
1808 d_fprintf(stderr
, _("Out of Memory!\n"));
1812 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
1813 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
1815 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
1816 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
1817 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
1819 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
1820 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
1821 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1822 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
1823 dom_sid_str_buf(&gsid
, &gsid_str
));
1824 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
1826 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1828 rc
= smbldap_add(state
, dn
, mods
);
1830 if (rc
!= LDAP_SUCCESS
) {
1831 d_fprintf(stderr
, _("Failed to add Domain Admins group "
1832 "to ldap directory\n"));
1836 if (!pdb_getgrsid(gmap
, gsid
)) {
1837 d_fprintf(stderr
, _("Failed to read just "
1838 "created domain group.\n"));
1841 domadmins_gid
= gmap
->gid
;
1845 domadmins_gid
= gmap
->gid
;
1846 d_printf(_("found!\n"));
1851 d_printf(_("Check for Administrator account.\n"));
1853 samuser
= samu_new(tc
);
1855 d_fprintf(stderr
, _("Out of Memory!\n"));
1859 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1860 LDAPMod
**mods
= NULL
;
1862 struct dom_sid_buf sid_str
;
1873 d_printf(_("Adding the Administrator user.\n"));
1875 if (domadmins_gid
== -1) {
1877 _("Can't create Administrator user, Domain "
1878 "Admins group not available!\n"));
1885 if (!winbind_allocate_uid(&uid
)) {
1887 _("Unable to allocate a new uid to create "
1888 "the Administrator user!\n"));
1893 name
= talloc_strdup(tc
, "Administrator");
1894 dn
= talloc_asprintf(tc
, "uid=Administrator,%s",
1895 lp_ldap_user_suffix(talloc_tos()));
1896 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)uid
);
1897 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)domadmins_gid
);
1898 dir
= talloc_sub_specified(tc
, lp_template_homedir(),
1901 get_global_sam_name(),
1902 uid
, domadmins_gid
);
1903 shell
= talloc_sub_specified(tc
, lp_template_shell(),
1906 get_global_sam_name(),
1907 uid
, domadmins_gid
);
1909 if (!name
|| !dn
|| !uidstr
|| !gidstr
|| !dir
|| !shell
) {
1910 d_fprintf(stderr
, _("Out of Memory!\n"));
1914 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR
);
1916 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
1917 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
1918 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
1920 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
1921 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
1922 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
1923 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
1924 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
1925 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
1926 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", name
);
1927 princ
= talloc_asprintf(tc
, "%s@%s", name
, lp_realm());
1929 d_fprintf(stderr
, _("Out of Memory!\n"));
1932 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "krbPrincipalName", princ
);
1934 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", name
);
1935 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", name
);
1936 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", name
);
1937 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
1938 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
1939 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", dir
);
1940 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", shell
);
1941 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
1942 dom_sid_str_buf(&sid
, &sid_str
));
1943 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
1944 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
1945 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
1947 smbldap_talloc_autofree_ldapmod(tc
, mods
);
1949 rc
= smbldap_add(state
, dn
, mods
);
1951 if (rc
!= LDAP_SUCCESS
) {
1952 d_fprintf(stderr
, _("Failed to add Administrator user "
1953 "to ldap directory\n"));
1957 if (!pdb_getsampwnam(samuser
, "Administrator")) {
1958 d_fprintf(stderr
, _("Failed to read just "
1959 "created user.\n"));
1964 d_printf(_("found!\n"));
1967 d_printf(_("Checking for Guest user.\n"));
1969 samuser
= samu_new(tc
);
1971 d_fprintf(stderr
, _("Out of Memory!\n"));
1975 if (!pdb_getsampwnam(samuser
, lp_guest_account())) {
1976 LDAPMod
**mods
= NULL
;
1978 struct dom_sid_buf sid_str
;
1984 d_printf(_("Adding the Guest user.\n"));
1986 sid_compose(&sid
, get_global_sam_sid(), DOMAIN_RID_GUEST
);
1988 pwd
= Get_Pwnam_alloc(tc
, lp_guest_account());
1991 if (domusers_gid
== -1) {
1993 _("Can't create Guest user, Domain "
1994 "Users group not available!\n"));
1997 if ((pwd
= talloc(tc
, struct passwd
)) == NULL
) {
1998 d_fprintf(stderr
, _("talloc failed\n"));
2001 pwd
->pw_name
= talloc_strdup(pwd
, lp_guest_account());
2006 if (!winbind_allocate_uid(&(pwd
->pw_uid
))) {
2008 _("Unable to allocate a new uid to "
2009 "create the Guest user!\n"));
2013 pwd
->pw_gid
= domusers_gid
;
2014 pwd
->pw_dir
= talloc_strdup(tc
, "/");
2015 pwd
->pw_shell
= talloc_strdup(tc
, "/bin/false");
2016 if (!pwd
->pw_dir
|| !pwd
->pw_shell
) {
2017 d_fprintf(stderr
, _("Out of Memory!\n"));
2022 dn
= talloc_asprintf(tc
, "uid=%s,%s", pwd
->pw_name
,
2023 lp_ldap_user_suffix (talloc_tos()));
2024 uidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_uid
);
2025 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2026 if (!dn
|| !uidstr
|| !gidstr
) {
2027 d_fprintf(stderr
, _("Out of Memory!\n"));
2031 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_ACCOUNT
);
2032 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXACCOUNT
);
2033 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT
);
2035 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "person");
2036 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "organizationalperson");
2037 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetorgperson");
2038 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "inetuser");
2039 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbprincipalaux");
2040 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "krbticketpolicyaux");
2041 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sn", pwd
->pw_name
);
2043 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uid", pwd
->pw_name
);
2044 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", pwd
->pw_name
);
2045 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", pwd
->pw_name
);
2046 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "uidNumber", uidstr
);
2047 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2048 if ((pwd
->pw_dir
!= NULL
) && (pwd
->pw_dir
[0] != '\0')) {
2049 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "homeDirectory", pwd
->pw_dir
);
2051 if ((pwd
->pw_shell
!= NULL
) && (pwd
->pw_shell
[0] != '\0')) {
2052 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "loginShell", pwd
->pw_shell
);
2054 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSID",
2055 dom_sid_str_buf(&sid
, &sid_str
));
2056 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaAcctFlags",
2057 pdb_encode_acct_ctrl(ACB_NORMAL
|ACB_DISABLED
,
2058 NEW_PW_FORMAT_SPACE_PADDED_LEN
));
2060 smbldap_talloc_autofree_ldapmod(tc
, mods
);
2062 rc
= smbldap_add(state
, dn
, mods
);
2064 if (rc
!= LDAP_SUCCESS
) {
2065 d_fprintf(stderr
, _("Failed to add Guest user to "
2066 "ldap directory\n"));
2070 if (!pdb_getsampwnam(samuser
, lp_guest_account())) {
2071 d_fprintf(stderr
, _("Failed to read just "
2072 "created user.\n"));
2077 d_printf(_("found!\n"));
2080 d_printf(_("Checking Guest's group.\n"));
2082 pwd
= Get_Pwnam_alloc(tc
, lp_guest_account());
2085 _("Failed to find just created Guest account!\n"
2086 " Is nss properly configured?!\n"));
2090 if (pwd
->pw_gid
== domusers_gid
) {
2091 d_printf(_("found!\n"));
2095 if (!pdb_getgrgid(gmap
, pwd
->pw_gid
)) {
2096 struct dom_sid_buf gsid_str
;
2097 LDAPMod
**mods
= NULL
;
2105 d_printf(_("Adding the Domain Guests group.\n"));
2107 uname
= talloc_strdup(tc
, "domguests");
2108 wname
= talloc_strdup(tc
, "Domain Guests");
2109 dn
= talloc_asprintf(tc
, "cn=%s,%s", "domguests",
2110 lp_ldap_group_suffix(talloc_tos()));
2111 gidstr
= talloc_asprintf(tc
, "%u", (unsigned int)pwd
->pw_gid
);
2112 gtype
= talloc_asprintf(tc
, "%d", SID_NAME_DOM_GRP
);
2114 if (!uname
|| !wname
|| !dn
|| !gidstr
|| !gtype
) {
2115 d_fprintf(stderr
, _("Out of Memory!\n"));
2119 sid_compose(&gsid
, get_global_sam_sid(), DOMAIN_RID_GUESTS
);
2121 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_POSIXGROUP
);
2122 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", LDAP_OBJ_GROUPMAP
);
2124 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "groupofnames");
2125 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "nestedgroup");
2126 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "objectClass", "ipausergroup");
2128 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "cn", uname
);
2129 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "displayName", wname
);
2130 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "gidNumber", gidstr
);
2131 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaSid",
2132 dom_sid_str_buf(&gsid
, &gsid_str
));
2133 smbldap_set_mod(&mods
, LDAP_MOD_ADD
, "sambaGroupType", gtype
);
2135 smbldap_talloc_autofree_ldapmod(tc
, mods
);
2137 rc
= smbldap_add(state
, dn
, mods
);
2139 if (rc
!= LDAP_SUCCESS
) {
2141 _("Failed to add Domain Guests group to ldap "
2145 d_printf(_("found!\n"));
2160 /***********************************************************
2161 migrated functionality from smbgroupedit
2162 **********************************************************/
2163 int net_sam(struct net_context
*c
, int argc
, const char **argv
)
2165 struct functable func
[] = {
2167 "createbuiltingroup",
2168 net_sam_createbuiltingroup
,
2169 NET_TRANSPORT_LOCAL
,
2170 N_("Create a new BUILTIN group"),
2171 N_("net sam createbuiltingroup\n"
2172 " Create a new BUILTIN group")
2176 net_sam_createlocalgroup
,
2177 NET_TRANSPORT_LOCAL
,
2178 N_("Create a new local group"),
2179 N_("net sam createlocalgroup\n"
2180 " Create a new local group")
2183 "createdomaingroup",
2184 net_sam_createdomaingroup
,
2185 NET_TRANSPORT_LOCAL
,
2186 N_("Create a new group"),
2187 N_("net sam createdomaingroup\n"
2188 " Create a new group")
2192 net_sam_deletelocalgroup
,
2193 NET_TRANSPORT_LOCAL
,
2194 N_("Delete an existing local group"),
2195 N_("net sam deletelocalgroup\n"
2196 " Delete an existing local group")
2199 "deletedomaingroup",
2200 net_sam_deletedomaingroup
,
2201 NET_TRANSPORT_LOCAL
,
2202 N_("Delete a domain group"),
2203 N_("net sam deletedomaingroup\n"
2208 net_sam_mapunixgroup
,
2209 NET_TRANSPORT_LOCAL
,
2210 N_("Map a unix group to a domain group"),
2211 N_("net sam mapunixgroup\n"
2212 " Map a unix group to a domain group")
2216 net_sam_unmapunixgroup
,
2217 NET_TRANSPORT_LOCAL
,
2218 N_("Remove a group mapping of an unix group to a "
2220 N_("net sam unmapunixgroup\n"
2221 " Remove a group mapping of an unix group to a "
2227 NET_TRANSPORT_LOCAL
,
2228 N_("Add a member to a group"),
2229 N_("net sam addmem\n"
2230 " Add a member to a group")
2235 NET_TRANSPORT_LOCAL
,
2236 N_("Delete a member from a group"),
2237 N_("net sam delmem\n"
2238 " Delete a member from a group")
2243 NET_TRANSPORT_LOCAL
,
2244 N_("List group members"),
2245 N_("net sam listmem\n"
2246 " List group members")
2251 NET_TRANSPORT_LOCAL
,
2252 N_("List users, groups and local groups"),
2254 " List users, groups and local groups")
2259 NET_TRANSPORT_LOCAL
,
2260 N_("Show details of a SAM entry"),
2262 " Show details of a SAM entry")
2267 NET_TRANSPORT_LOCAL
,
2268 N_("Set details of a SAM account"),
2270 " Set details of a SAM account")
2275 NET_TRANSPORT_LOCAL
,
2276 N_("Set account policies"),
2277 N_("net sam policy\n"
2278 " Set account policies")
2283 NET_TRANSPORT_LOCAL
,
2284 N_("Manipulate user privileges"),
2285 N_("net sam rights\n"
2286 " Manipulate user privileges")
2292 NET_TRANSPORT_LOCAL
,
2293 N_("Provision a clean user database"),
2294 N_("net sam privison\n"
2295 " Provision a clear user database")
2298 {NULL
, NULL
, 0, NULL
, NULL
}
2301 if (getuid() != 0) {
2302 d_fprintf(stderr
, _("You are not root, most things won't "
2306 return net_run_function(c
, argc
, argv
, "net sam", func
);