s3-lsasd: Listen on parent messages
[Samba/bjacke.git] / source3 / utils / net_sam.c
blob467e441f60f37cd6c0626a0b986c669be6d564af
1 /*
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/>.
21 #include "includes.h"
22 #include "system/passwd.h"
23 #include "utils/net.h"
24 #include "../librpc/gen_ndr/samr.h"
25 #include "smbldap.h"
26 #include "../libcli/security/security.h"
27 #include "lib/winbind_util.h"
28 #include "passdb.h"
29 #include "lib/privileges.h"
32 * Set a user's data
35 static int net_sam_userset(struct net_context *c, int argc, const char **argv,
36 const char *field,
37 bool (*fn)(struct samu *, const char *,
38 enum pdb_value_state))
40 struct samu *sam_acct = NULL;
41 struct dom_sid sid;
42 enum lsa_SidType type;
43 const char *dom, *name;
44 NTSTATUS status;
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"),
49 field);
50 return -1;
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]);
56 return -1;
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));
62 return -1;
65 if ( !(sam_acct = samu_new( NULL )) ) {
66 d_fprintf(stderr, _("Internal error\n"));
67 return -1;
70 if (!pdb_getsampwsid(sam_acct, &sid)) {
71 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
72 return -1;
75 if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
76 d_fprintf(stderr, _("Internal error\n"));
77 return -1;
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));
84 return -1;
87 TALLOC_FREE(sam_acct);
89 d_printf(_("Updated %s for %s\\%s to %s\n"), field, dom, name, argv[1]);
90 return 0;
93 static int net_sam_set_fullname(struct net_context *c, int argc,
94 const char **argv)
96 return net_sam_userset(c, argc, argv, "fullname",
97 pdb_set_fullname);
100 static int net_sam_set_logonscript(struct net_context *c, int argc,
101 const char **argv)
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,
108 const char **argv)
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,
115 const char **argv)
117 return net_sam_userset(c, argc, argv, "homedrive",
118 pdb_set_dir_drive);
121 static int net_sam_set_homedir(struct net_context *c, int argc,
122 const char **argv)
124 return net_sam_userset(c, argc, argv, "homedir",
125 pdb_set_homedir);
128 static int net_sam_set_workstations(struct net_context *c, int argc,
129 const char **argv)
131 return net_sam_userset(c, argc, argv, "workstations",
132 pdb_set_workstations);
136 * Set account flags
139 static int net_sam_set_userflag(struct net_context *c, int argc,
140 const char **argv, const char *field,
141 uint16 flag)
143 struct samu *sam_acct = NULL;
144 struct dom_sid sid;
145 enum lsa_SidType type;
146 const char *dom, *name;
147 NTSTATUS status;
148 uint32_t acct_flags;
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"),
155 field);
156 return -1;
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]);
162 return -1;
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));
168 return -1;
171 if ( !(sam_acct = samu_new( NULL )) ) {
172 d_fprintf(stderr, _("Internal error\n"));
173 return -1;
176 if (!pdb_getsampwsid(sam_acct, &sid)) {
177 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
178 return -1;
181 acct_flags = pdb_get_acct_ctrl(sam_acct);
183 if (strequal(argv[1], "yes")) {
184 acct_flags |= flag;
185 } else {
186 acct_flags &= ~flag;
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));
195 return -1;
198 TALLOC_FREE(sam_acct);
200 d_fprintf(stderr, _("Updated flag %s for %s\\%s to %s\n"), field, dom,
201 name, argv[1]);
202 return 0;
205 static int net_sam_set_disabled(struct net_context *c, int argc,
206 const char **argv)
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,
212 const char **argv)
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,
218 const char **argv)
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,
224 const char **argv)
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,
234 const char **argv)
236 struct samu *sam_acct = NULL;
237 struct dom_sid sid;
238 enum lsa_SidType type;
239 const char *dom, *name;
240 NTSTATUS status;
242 if ((argc != 2) || c->display_usage ||
243 (!strequal(argv[1], "yes") &&
244 !strequal(argv[1], "no"))) {
245 d_fprintf(stderr, "%s\n%s",
246 _("Usage:"),
247 _("net sam set pwdmustchangenow <user> [yes|no]\n"));
248 return -1;
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]);
254 return -1;
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));
260 return -1;
263 if ( !(sam_acct = samu_new( NULL )) ) {
264 d_fprintf(stderr, _("Internal error\n"));
265 return -1;
268 if (!pdb_getsampwsid(sam_acct, &sid)) {
269 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
270 return -1;
273 if (strequal(argv[1], "yes")) {
274 pdb_set_pass_last_set_time(sam_acct, 0, PDB_CHANGED);
275 } else {
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));
283 return -1;
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,
290 name, argv[1]);
291 return 0;
296 * Set a user's or a group's comment
299 static int net_sam_set_comment(struct net_context *c, int argc,
300 const char **argv)
302 GROUP_MAP map;
303 struct dom_sid sid;
304 enum lsa_SidType type;
305 const char *dom, *name;
306 NTSTATUS status;
308 if (argc != 2 || c->display_usage) {
309 d_fprintf(stderr, "%s\n%s",
310 _("Usage:"),
311 _("net sam set comment <name> <comment>\n"));
312 return -1;
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]);
318 return -1;
321 if (type == SID_NAME_USER) {
322 return net_sam_userset(c, argc, argv, "comment",
323 pdb_set_acct_desc);
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));
330 return -1;
333 if (!pdb_getgrsid(&map, sid)) {
334 d_fprintf(stderr, _("Could not load group %s\n"), argv[0]);
335 return -1;
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));
345 return -1;
348 d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
349 argv[1]);
351 return 0;
354 static int net_sam_set(struct net_context *c, int argc, const char **argv)
356 struct functable func[] = {
358 "homedir",
359 net_sam_set_homedir,
360 NET_TRANSPORT_LOCAL,
361 N_("Change a user's home directory"),
362 N_("net sam set homedir\n"
363 " Change a user's home directory")
366 "profilepath",
367 net_sam_set_profilepath,
368 NET_TRANSPORT_LOCAL,
369 N_("Change a user's profile path"),
370 N_("net sam set profilepath\n"
371 " Change a user's profile path")
374 "comment",
375 net_sam_set_comment,
376 NET_TRANSPORT_LOCAL,
377 N_("Change a users or groups description"),
378 N_("net sam set comment\n"
379 " Change a users or groups description")
382 "fullname",
383 net_sam_set_fullname,
384 NET_TRANSPORT_LOCAL,
385 N_("Change a user's full name"),
386 N_("net sam set fullname\n"
387 " Change a user's full name")
390 "logonscript",
391 net_sam_set_logonscript,
392 NET_TRANSPORT_LOCAL,
393 N_("Change a user's logon script"),
394 N_("net sam set logonscript\n"
395 " Change a user's logon script")
398 "homedrive",
399 net_sam_set_homedrive,
400 NET_TRANSPORT_LOCAL,
401 N_("Change a user's home drive"),
402 N_("net sam set homedrive\n"
403 " Change a user's home drive")
406 "workstations",
407 net_sam_set_workstations,
408 NET_TRANSPORT_LOCAL,
409 N_("Change a user's allowed workstations"),
410 N_("net sam set workstations\n"
411 " Change a user's allowed workstations")
414 "disabled",
415 net_sam_set_disabled,
416 NET_TRANSPORT_LOCAL,
417 N_("Disable/Enable a user"),
418 N_("net sam set disable\n"
419 " Disable/Enable a user")
422 "pwnotreq",
423 net_sam_set_pwnotreq,
424 NET_TRANSPORT_LOCAL,
425 N_("Disable/Enable the password not required flag"),
426 N_("net sam set pwnotreq\n"
427 " Disable/Enable the password not required flag")
430 "autolock",
431 net_sam_set_autolock,
432 NET_TRANSPORT_LOCAL,
433 N_("Disable/Enable a user's lockout flag"),
434 N_("net sam set autolock\n"
435 " Disable/Enable a user's lockout flag")
438 "pwnoexp",
439 net_sam_set_pwnoexp,
440 NET_TRANSPORT_LOCAL,
441 N_("Disable/Enable whether a user's pw does not "
442 "expire"),
443 N_("net sam set pwnoexp\n"
444 " Disable/Enable whether a user's pw does not "
445 "expire")
448 "pwdmustchangenow",
449 net_sam_set_pwdmustchangenow,
450 NET_TRANSPORT_LOCAL,
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;
468 uint32 value = 0;
469 uint32 old_value = 0;
470 enum pdb_policy_type field;
471 char *endptr;
473 if (argc != 2 || c->display_usage) {
474 d_fprintf(stderr, "%s\n%s",
475 _("Usage:"),
476 _("net sam policy set \"<account policy>\" <value>\n"));
477 return -1;
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")) {
485 value = -1;
487 else {
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 "
492 "\"%s\".\n"),
493 account_policy, argv[1]);
494 return -1;
498 if (field == 0) {
499 const char **names;
500 int i, count;
502 account_policy_names_list(talloc_tos(), &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]);
510 TALLOC_FREE(names);
512 return -1;
515 if (!pdb_get_account_policy(field, &old_value)) {
516 d_fprintf(stderr, _("Valid account policy, but unable to fetch "
517 "value!\n"));
518 } else {
519 d_printf(_("Account policy \"%s\" value was: %d\n"),
520 account_policy, old_value);
523 if (!pdb_set_account_policy(field, value)) {
524 d_fprintf(stderr, _("Valid account policy, but unable to "
525 "set value!\n"));
526 return -1;
527 } else {
528 d_printf(_("Account policy \"%s\" value is now: %d\n"),
529 account_policy, value);
532 return 0;
535 static int net_sam_policy_show(struct net_context *c, int argc, const char **argv)
537 const char *account_policy = NULL;
538 uint32 old_value;
539 enum pdb_policy_type field;
541 if (argc != 1 || c->display_usage) {
542 d_fprintf(stderr, "%s\n%s",
543 _("Usage:"),
544 _("net sam policy show \"<account policy>\"\n"));
545 return -1;
548 account_policy = argv[0];
549 field = account_policy_name_to_typenum(account_policy);
551 if (field == 0) {
552 const char **names;
553 int count;
554 int i;
555 account_policy_names_list(talloc_tos(), &names, &count);
556 d_fprintf(stderr, _("No account policy by that name!\n"));
557 if (count != 0) {
558 d_fprintf(stderr, _("Valid account policies "
559 "are:\n"));
560 for (i=0; i<count; i++) {
561 d_fprintf(stderr, "%s\n", names[i]);
564 TALLOC_FREE(names);
565 return -1;
568 if (!pdb_get_account_policy(field, &old_value)) {
569 fprintf(stderr, _("Valid account policy, but unable to "
570 "fetch value!\n"));
571 return -1;
574 printf(_("Account policy \"%s\" description: %s\n"),
575 account_policy, account_policy_get_desc(field));
576 printf(_("Account policy \"%s\" value is: %d\n"), account_policy,
577 old_value);
578 return 0;
581 static int net_sam_policy_list(struct net_context *c, int argc, const char **argv)
583 const char **names;
584 int count;
585 int i;
587 if (c->display_usage) {
588 d_printf( "%s\n"
589 "net sam policy list\n"
590 " %s\n",
591 _("Usage:"),
592 _("List account policies"));
593 return 0;
596 account_policy_names_list(talloc_tos(), &names, &count);
597 if (count != 0) {
598 d_fprintf(stderr, _("Valid account policies "
599 "are:\n"));
600 for (i = 0; i < count ; i++) {
601 d_fprintf(stderr, "%s\n", names[i]);
604 TALLOC_FREE(names);
605 return -1;
608 static int net_sam_policy(struct net_context *c, int argc, const char **argv)
610 struct functable func[] = {
612 "list",
613 net_sam_policy_list,
614 NET_TRANSPORT_LOCAL,
615 N_("List account policies"),
616 N_("net sam policy list\n"
617 " List account policies")
620 "show",
621 net_sam_policy_show,
622 NET_TRANSPORT_LOCAL,
623 N_("Show account policies"),
624 N_("net sam policy show\n"
625 " Show account policies")
628 "set",
629 net_sam_policy_set,
630 NET_TRANSPORT_LOCAL,
631 N_("Change account policies"),
632 N_("net sam policy set\n"
633 " Change account policies")
635 {NULL, NULL, 0, NULL, NULL}
638 return net_run_function(c, argc, argv, "net sam policy", func);
641 static int net_sam_rights_list(struct net_context *c, int argc,
642 const char **argv)
644 enum sec_privilege privilege;
646 if (argc > 1 || c->display_usage) {
647 d_fprintf(stderr, "%s\n%s",
648 _("Usage:"),
649 _("net sam rights list [privilege name]\n"));
650 return -1;
653 if (argc == 0) {
654 int i;
655 int num = num_privileges_in_short_list();
657 for (i=0; i<num; i++) {
658 d_printf("%s\n", sec_privilege_name_from_index(i));
660 return 0;
663 privilege = sec_privilege_id(argv[0]);
665 if (privilege != SEC_PRIV_INVALID) {
666 struct dom_sid *sids;
667 int i, num_sids;
668 NTSTATUS status;
670 status = privilege_enum_sids(privilege, talloc_tos(),
671 &sids, &num_sids);
672 if (!NT_STATUS_IS_OK(status)) {
673 d_fprintf(stderr, _("Could not list rights: %s\n"),
674 nt_errstr(status));
675 return -1;
678 for (i=0; i<num_sids; i++) {
679 const char *dom, *name;
680 enum lsa_SidType type;
682 if (lookup_sid(talloc_tos(), &sids[i], &dom, &name,
683 &type)) {
684 d_printf("%s\\%s\n", dom, name);
686 else {
687 d_printf("%s\n", sid_string_tos(&sids[i]));
690 return 0;
693 return -1;
696 static int net_sam_rights_grant(struct net_context *c, int argc,
697 const char **argv)
699 struct dom_sid sid;
700 enum lsa_SidType type;
701 const char *dom, *name;
702 int i;
704 if (argc < 2 || c->display_usage) {
705 d_fprintf(stderr, "%s\n%s",
706 _("Usage:"),
707 _("net sam rights grant <name> <rights> ...\n"));
708 return -1;
711 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
712 &dom, &name, &sid, &type)) {
713 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
714 return -1;
717 for (i=1; i < argc; i++) {
718 enum sec_privilege privilege = sec_privilege_id(argv[i]);
719 if (privilege == SEC_PRIV_INVALID) {
720 d_fprintf(stderr, _("%s unknown\n"), argv[i]);
721 return -1;
724 if (!grant_privilege_by_name(&sid, argv[i])) {
725 d_fprintf(stderr, _("Could not grant privilege\n"));
726 return -1;
729 d_printf(_("Granted %s to %s\\%s\n"), argv[i], dom, name);
732 return 0;
735 static int net_sam_rights_revoke(struct net_context *c, int argc,
736 const char **argv)
738 struct dom_sid sid;
739 enum lsa_SidType type;
740 const char *dom, *name;
741 int i;
743 if (argc < 2 || c->display_usage) {
744 d_fprintf(stderr, "%s\n%s",
745 _("Usage:"),
746 _("net sam rights revoke <name> <rights>\n"));
747 return -1;
750 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
751 &dom, &name, &sid, &type)) {
752 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
753 return -1;
756 for (i=1; i < argc; i++) {
757 enum sec_privilege privilege = sec_privilege_id(argv[i]);
758 if (privilege == SEC_PRIV_INVALID) {
759 d_fprintf(stderr, _("%s unknown\n"), argv[i]);
760 return -1;
763 if (!revoke_privilege_by_name(&sid, argv[i])) {
764 d_fprintf(stderr, _("Could not revoke privilege\n"));
765 return -1;
768 d_printf(_("Revoked %s from %s\\%s\n"), argv[i], dom, name);
771 return 0;
774 static int net_sam_rights(struct net_context *c, int argc, const char **argv)
776 struct functable func[] = {
778 "list",
779 net_sam_rights_list,
780 NET_TRANSPORT_LOCAL,
781 N_("List possible user rights"),
782 N_("net sam rights list\n"
783 " List possible user rights")
786 "grant",
787 net_sam_rights_grant,
788 NET_TRANSPORT_LOCAL,
789 N_("Grant right(s)"),
790 N_("net sam rights grant\n"
791 " Grant right(s)")
794 "revoke",
795 net_sam_rights_revoke,
796 NET_TRANSPORT_LOCAL,
797 N_("Revoke right(s)"),
798 N_("net sam rights revoke\n"
799 " Revoke right(s)")
801 {NULL, NULL, 0, NULL, NULL}
803 return net_run_function(c, argc, argv, "net sam rights", func);
807 * Map a unix group to a domain group
810 static NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
812 NTSTATUS status;
813 GROUP_MAP map;
814 const char *grpname, *dom, *name;
815 uint32 rid;
817 if (pdb_getgrgid(&map, grp->gr_gid)) {
818 return NT_STATUS_GROUP_EXISTS;
821 map.gid = grp->gr_gid;
822 grpname = grp->gr_name;
824 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
825 &dom, &name, NULL, NULL)) {
827 const char *tmp = talloc_asprintf(
828 talloc_tos(), "Unix Group %s", grp->gr_name);
830 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
831 grpname, dom, name, tmp));
832 grpname = tmp;
835 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
836 NULL, NULL, NULL, NULL)) {
837 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
838 return NT_STATUS_GROUP_EXISTS;
841 fstrcpy(map.nt_name, grpname);
843 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
844 if (!pdb_new_rid(&rid)) {
845 DEBUG(3, ("Could not get a new RID for %s\n",
846 grp->gr_name));
847 return NT_STATUS_ACCESS_DENIED;
849 } else {
850 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
853 sid_compose(&map.sid, get_global_sam_sid(), rid);
854 map.sid_name_use = SID_NAME_DOM_GRP;
855 fstrcpy(map.comment, talloc_asprintf(talloc_tos(), "Unix Group %s",
856 grp->gr_name));
858 status = pdb_add_group_mapping_entry(&map);
859 if (NT_STATUS_IS_OK(status)) {
860 *pmap = map;
862 return status;
865 static int net_sam_mapunixgroup(struct net_context *c, int argc, const char **argv)
867 NTSTATUS status;
868 GROUP_MAP map;
869 struct group *grp;
871 if (argc != 1 || c->display_usage) {
872 d_fprintf(stderr, "%s\n%s",
873 _("Usage:"),
874 _("net sam mapunixgroup <name>\n"));
875 return -1;
878 grp = getgrnam(argv[0]);
879 if (grp == NULL) {
880 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
881 return -1;
884 status = map_unix_group(grp, &map);
886 if (!NT_STATUS_IS_OK(status)) {
887 d_fprintf(stderr, _("Mapping group %s failed with %s\n"),
888 argv[0], nt_errstr(status));
889 return -1;
892 d_printf(_("Mapped unix group %s to SID %s\n"), argv[0],
893 sid_string_tos(&map.sid));
895 return 0;
899 * Remove a group mapping
902 static NTSTATUS unmap_unix_group(const struct group *grp, GROUP_MAP *pmap)
904 GROUP_MAP map;
905 const char *grpname;
906 struct dom_sid dom_sid;
908 map.gid = grp->gr_gid;
909 grpname = grp->gr_name;
911 if (!lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
912 NULL, NULL, NULL, NULL)) {
913 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp->gr_name));
914 return NT_STATUS_NO_SUCH_GROUP;
917 fstrcpy(map.nt_name, grpname);
919 if (!pdb_gid_to_sid(map.gid, &dom_sid)) {
920 return NT_STATUS_UNSUCCESSFUL;
923 return pdb_delete_group_mapping_entry(dom_sid);
926 static int net_sam_unmapunixgroup(struct net_context *c, int argc, const char **argv)
928 NTSTATUS status;
929 GROUP_MAP map;
930 struct group *grp;
932 if (argc != 1 || c->display_usage) {
933 d_fprintf(stderr, "%s\n%s",
934 _("Usage:"),
935 _("net sam unmapunixgroup <name>\n"));
936 return -1;
939 grp = getgrnam(argv[0]);
940 if (grp == NULL) {
941 d_fprintf(stderr, _("Could not find mapping for group %s.\n"),
942 argv[0]);
943 return -1;
946 status = unmap_unix_group(grp, &map);
948 if (!NT_STATUS_IS_OK(status)) {
949 d_fprintf(stderr, _("Unmapping group %s failed with %s.\n"),
950 argv[0], nt_errstr(status));
951 return -1;
954 d_printf(_("Unmapped unix group %s.\n"), argv[0]);
956 return 0;
960 * Create a domain group
963 static int net_sam_createdomaingroup(struct net_context *c, int argc,
964 const char **argv)
966 NTSTATUS status;
967 uint32 rid;
969 if (argc != 1 || c->display_usage) {
970 d_fprintf(stderr, "%s\n%s",
971 _("Usage:"),
972 _("net sam createdomaingroup <name>\n"));
973 return -1;
976 status = pdb_create_dom_group(talloc_tos(), argv[0], &rid);
978 if (!NT_STATUS_IS_OK(status)) {
979 d_fprintf(stderr, _("Creating %s failed with %s\n"),
980 argv[0], nt_errstr(status));
981 return -1;
984 d_printf(_("Created domain group %s with RID %d\n"), argv[0], rid);
986 return 0;
990 * Delete a domain group
993 static int net_sam_deletedomaingroup(struct net_context *c, int argc,
994 const char **argv)
996 struct dom_sid sid;
997 uint32_t rid;
998 enum lsa_SidType type;
999 const char *dom, *name;
1000 NTSTATUS status;
1002 if (argc != 1 || c->display_usage) {
1003 d_fprintf(stderr, "%s\n%s",
1004 _("Usage:"),
1005 _("net sam deletelocalgroup <name>\n"));
1006 return -1;
1009 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1010 &dom, &name, &sid, &type)) {
1011 d_fprintf(stderr, _("Could not find %s.\n"), argv[0]);
1012 return -1;
1015 if (type != SID_NAME_DOM_GRP) {
1016 d_fprintf(stderr, _("%s is a %s, not a domain group.\n"),
1017 argv[0], sid_type_lookup(type));
1018 return -1;
1021 sid_peek_rid(&sid, &rid);
1023 status = pdb_delete_dom_group(talloc_tos(), rid);
1025 if (!NT_STATUS_IS_OK(status)) {
1026 d_fprintf(stderr,_("Deleting domain group %s failed with %s\n"),
1027 argv[0], nt_errstr(status));
1028 return -1;
1031 d_printf(_("Deleted domain group %s.\n"), argv[0]);
1033 return 0;
1037 * Create a local group
1040 static int net_sam_createlocalgroup(struct net_context *c, int argc, const char **argv)
1042 NTSTATUS status;
1043 uint32 rid;
1045 if (argc != 1 || c->display_usage) {
1046 d_fprintf(stderr, "%s\n%s",
1047 _("Usage:"),
1048 _("net sam createlocalgroup <name>\n"));
1049 return -1;
1052 if (!winbind_ping()) {
1053 d_fprintf(stderr, _("winbind seems not to run. "
1054 "createlocalgroup only works when winbind runs.\n"));
1055 return -1;
1058 status = pdb_create_alias(argv[0], &rid);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1062 argv[0], nt_errstr(status));
1063 return -1;
1066 d_printf(_("Created local group %s with RID %d\n"), argv[0], rid);
1068 return 0;
1072 * Delete a local group
1075 static int net_sam_deletelocalgroup(struct net_context *c, int argc, const char **argv)
1077 struct dom_sid sid;
1078 enum lsa_SidType type;
1079 const char *dom, *name;
1080 NTSTATUS status;
1082 if (argc != 1 || c->display_usage) {
1083 d_fprintf(stderr, "%s\n%s",
1084 _("Usage:"),
1085 _("net sam deletelocalgroup <name>\n"));
1086 return -1;
1089 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1090 &dom, &name, &sid, &type)) {
1091 d_fprintf(stderr,_("Could not find %s.\n"), argv[0]);
1092 return -1;
1095 if (type != SID_NAME_ALIAS) {
1096 d_fprintf(stderr, _("%s is a %s, not a local group.\n"),argv[0],
1097 sid_type_lookup(type));
1098 return -1;
1101 status = pdb_delete_alias(&sid);
1103 if (!NT_STATUS_IS_OK(status)) {
1104 d_fprintf(stderr, _("Deleting local group %s failed with %s\n"),
1105 argv[0], nt_errstr(status));
1106 return -1;
1109 d_printf(_("Deleted local group %s.\n"), argv[0]);
1111 return 0;
1115 * Create a builtin group
1118 static int net_sam_createbuiltingroup(struct net_context *c, int argc, const char **argv)
1120 NTSTATUS status;
1121 uint32 rid;
1122 enum lsa_SidType type;
1123 fstring groupname;
1124 struct dom_sid sid;
1126 if (argc != 1 || c->display_usage) {
1127 d_fprintf(stderr, "%s\n%s",
1128 _("Usage:"),
1129 _("net sam createbuiltingroup <name>\n"));
1130 return -1;
1133 if (!winbind_ping()) {
1134 d_fprintf(stderr, _("winbind seems not to run. "
1135 "createbuiltingroup only works when winbind "
1136 "runs.\n"));
1137 return -1;
1140 /* validate the name and get the group */
1142 fstrcpy( groupname, "BUILTIN\\" );
1143 fstrcat( groupname, argv[0] );
1145 if ( !lookup_name(talloc_tos(), groupname, LOOKUP_NAME_ALL, NULL,
1146 NULL, &sid, &type)) {
1147 d_fprintf(stderr, _("%s is not a BUILTIN group\n"), argv[0]);
1148 return -1;
1151 if ( !sid_peek_rid( &sid, &rid ) ) {
1152 d_fprintf(stderr, _("Failed to get RID for %s\n"), argv[0]);
1153 return -1;
1156 status = pdb_create_builtin_alias( rid );
1158 if (!NT_STATUS_IS_OK(status)) {
1159 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1160 argv[0], nt_errstr(status));
1161 return -1;
1164 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv[0], rid);
1166 return 0;
1170 * Add a group member
1173 static int net_sam_addmem(struct net_context *c, int argc, const char **argv)
1175 const char *groupdomain, *groupname, *memberdomain, *membername;
1176 struct dom_sid group, member;
1177 enum lsa_SidType grouptype, membertype;
1178 NTSTATUS status;
1180 if (argc != 2 || c->display_usage) {
1181 d_fprintf(stderr, "%s\n%s",
1182 _("Usage:"),
1183 _("net sam addmem <group> <member>\n"));
1184 return -1;
1187 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1188 &groupdomain, &groupname, &group, &grouptype)) {
1189 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1190 return -1;
1193 /* check to see if the member to be added is a name or a SID */
1195 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1196 &memberdomain, &membername, &member, &membertype))
1198 /* try it as a SID */
1200 if ( !string_to_sid( &member, argv[1] ) ) {
1201 d_fprintf(stderr, _("Could not find member %s\n"),
1202 argv[1]);
1203 return -1;
1206 if ( !lookup_sid(talloc_tos(), &member, &memberdomain,
1207 &membername, &membertype) )
1209 d_fprintf(stderr, _("Could not resolve SID %s\n"),
1210 argv[1]);
1211 return -1;
1215 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
1216 if ((membertype != SID_NAME_USER) &&
1217 (membertype != SID_NAME_DOM_GRP)) {
1218 d_fprintf(stderr, _("%s is a local group, only users "
1219 "and domain groups can be added.\n"
1220 "%s is a %s\n"), argv[0], argv[1],
1221 sid_type_lookup(membertype));
1222 return -1;
1224 status = pdb_add_aliasmem(&group, &member);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 d_fprintf(stderr, _("Adding local group member failed "
1228 "with %s\n"), nt_errstr(status));
1229 return -1;
1231 } else if (grouptype == SID_NAME_DOM_GRP) {
1232 uint32_t grouprid, memberrid;
1234 sid_peek_rid(&group, &grouprid);
1235 sid_peek_rid(&member, &memberrid);
1237 status = pdb_add_groupmem(talloc_tos(), grouprid, memberrid);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 d_fprintf(stderr, _("Adding domain group member failed "
1240 "with %s\n"), nt_errstr(status));
1241 return -1;
1243 } else {
1244 d_fprintf(stderr, _("Can only add members to local groups so "
1245 "far, %s is a %s\n"), argv[0],
1246 sid_type_lookup(grouptype));
1247 return -1;
1250 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain, membername,
1251 groupdomain, groupname);
1253 return 0;
1257 * Delete a group member
1260 static int net_sam_delmem(struct net_context *c, int argc, const char **argv)
1262 const char *groupdomain, *groupname;
1263 const char *memberdomain = NULL;
1264 const char *membername = NULL;
1265 struct dom_sid group, member;
1266 enum lsa_SidType grouptype;
1267 NTSTATUS status;
1269 if (argc != 2 || c->display_usage) {
1270 d_fprintf(stderr,"%s\n%s",
1271 _("Usage:"),
1272 _("net sam delmem <group> <member>\n"));
1273 return -1;
1276 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1277 &groupdomain, &groupname, &group, &grouptype)) {
1278 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1279 return -1;
1282 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1283 &memberdomain, &membername, &member, NULL)) {
1284 if (!string_to_sid(&member, argv[1])) {
1285 d_fprintf(stderr, _("Could not find member %s\n"),
1286 argv[1]);
1287 return -1;
1291 if ((grouptype == SID_NAME_ALIAS) ||
1292 (grouptype == SID_NAME_WKN_GRP)) {
1293 status = pdb_del_aliasmem(&group, &member);
1295 if (!NT_STATUS_IS_OK(status)) {
1296 d_fprintf(stderr,_("Deleting local group member failed "
1297 "with %s\n"), nt_errstr(status));
1298 return -1;
1300 } else if (grouptype == SID_NAME_DOM_GRP) {
1301 uint32_t grouprid, memberrid;
1303 sid_peek_rid(&group, &grouprid);
1304 sid_peek_rid(&member, &memberrid);
1306 status = pdb_del_groupmem(talloc_tos(), grouprid, memberrid);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 d_fprintf(stderr, _("Deleting domain group member "
1309 "failed with %s\n"), nt_errstr(status));
1310 return -1;
1312 } else {
1313 d_fprintf(stderr, _("Can only delete members from local groups "
1314 "so far, %s is a %s\n"), argv[0],
1315 sid_type_lookup(grouptype));
1316 return -1;
1319 if (membername != NULL) {
1320 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1321 memberdomain, membername, groupdomain, groupname);
1322 } else {
1323 d_printf(_("Deleted %s from %s\\%s\n"),
1324 sid_string_tos(&member), groupdomain, groupname);
1327 return 0;
1331 * List group members
1334 static int net_sam_listmem(struct net_context *c, int argc, const char **argv)
1336 const char *groupdomain, *groupname;
1337 struct dom_sid group;
1338 struct dom_sid *members = NULL;
1339 size_t i, num_members = 0;
1340 enum lsa_SidType grouptype;
1341 NTSTATUS status;
1343 if (argc != 1 || c->display_usage) {
1344 d_fprintf(stderr, "%s\n%s",
1345 _("Usage:"),
1346 _("net sam listmem <group>\n"));
1347 return -1;
1350 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1351 &groupdomain, &groupname, &group, &grouptype)) {
1352 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1353 return -1;
1356 if ((grouptype == SID_NAME_ALIAS) ||
1357 (grouptype == SID_NAME_WKN_GRP)) {
1358 status = pdb_enum_aliasmem(&group, talloc_tos(), &members,
1359 &num_members);
1360 if (!NT_STATUS_IS_OK(status)) {
1361 d_fprintf(stderr, _("Listing group members failed with "
1362 "%s\n"), nt_errstr(status));
1363 return -1;
1365 } else if (grouptype == SID_NAME_DOM_GRP) {
1366 uint32_t *rids;
1368 status = pdb_enum_group_members(talloc_tos(), &group,
1369 &rids, &num_members);
1370 if (!NT_STATUS_IS_OK(status)) {
1371 d_fprintf(stderr, _("Listing group members failed with "
1372 "%s\n"), nt_errstr(status));
1373 return -1;
1376 members = talloc_array(talloc_tos(), struct dom_sid,
1377 num_members);
1378 if (members == NULL) {
1379 TALLOC_FREE(rids);
1380 return -1;
1383 for (i=0; i<num_members; i++) {
1384 sid_compose(&members[i], get_global_sam_sid(),
1385 rids[i]);
1387 TALLOC_FREE(rids);
1388 } else {
1389 d_fprintf(stderr,_("Can only list local group members so far.\n"
1390 "%s is a %s\n"), argv[0], sid_type_lookup(grouptype));
1391 return -1;
1394 d_printf(_("%s\\%s has %u members\n"), groupdomain, groupname,
1395 (unsigned int)num_members);
1396 for (i=0; i<num_members; i++) {
1397 const char *dom, *name;
1398 if (lookup_sid(talloc_tos(), &members[i], &dom, &name, NULL)) {
1399 d_printf(" %s\\%s\n", dom, name);
1400 } else {
1401 d_printf(" %s\n", sid_string_tos(&members[i]));
1405 TALLOC_FREE(members);
1407 return 0;
1411 * Do the listing
1413 static int net_sam_do_list(struct net_context *c, int argc, const char **argv,
1414 struct pdb_search *search, const char *what)
1416 bool verbose = (argc == 1);
1418 if ((argc > 1) || c->display_usage ||
1419 ((argc == 1) && !strequal(argv[0], "verbose"))) {
1420 d_fprintf(stderr, "%s\n", _("Usage:"));
1421 d_fprintf(stderr, _("net sam list %s [verbose]\n"), what);
1422 return -1;
1425 if (search == NULL) {
1426 d_fprintf(stderr, _("Could not start search\n"));
1427 return -1;
1430 while (true) {
1431 struct samr_displayentry entry;
1432 if (!search->next_entry(search, &entry)) {
1433 break;
1435 if (verbose) {
1436 d_printf("%s:%d:%s\n",
1437 entry.account_name,
1438 entry.rid,
1439 entry.description);
1440 } else {
1441 d_printf("%s\n", entry.account_name);
1445 TALLOC_FREE(search);
1446 return 0;
1449 static int net_sam_list_users(struct net_context *c, int argc,
1450 const char **argv)
1452 return net_sam_do_list(c, argc, argv,
1453 pdb_search_users(talloc_tos(), ACB_NORMAL),
1454 "users");
1457 static int net_sam_list_groups(struct net_context *c, int argc,
1458 const char **argv)
1460 return net_sam_do_list(c, argc, argv, pdb_search_groups(talloc_tos()),
1461 "groups");
1464 static int net_sam_list_localgroups(struct net_context *c, int argc,
1465 const char **argv)
1467 return net_sam_do_list(c, argc, argv,
1468 pdb_search_aliases(talloc_tos(),
1469 get_global_sam_sid()),
1470 "localgroups");
1473 static int net_sam_list_builtin(struct net_context *c, int argc,
1474 const char **argv)
1476 return net_sam_do_list(c, argc, argv,
1477 pdb_search_aliases(talloc_tos(),
1478 &global_sid_Builtin),
1479 "builtin");
1482 static int net_sam_list_workstations(struct net_context *c, int argc,
1483 const char **argv)
1485 return net_sam_do_list(c, argc, argv,
1486 pdb_search_users(talloc_tos(), ACB_WSTRUST),
1487 "workstations");
1491 * List stuff
1494 static int net_sam_list(struct net_context *c, int argc, const char **argv)
1496 struct functable func[] = {
1498 "users",
1499 net_sam_list_users,
1500 NET_TRANSPORT_LOCAL,
1501 N_("List SAM users"),
1502 N_("net sam list users\n"
1503 " List SAM users")
1506 "groups",
1507 net_sam_list_groups,
1508 NET_TRANSPORT_LOCAL,
1509 N_("List SAM groups"),
1510 N_("net sam list groups\n"
1511 " List SAM groups")
1514 "localgroups",
1515 net_sam_list_localgroups,
1516 NET_TRANSPORT_LOCAL,
1517 N_("List SAM local groups"),
1518 N_("net sam list localgroups\n"
1519 " List SAM local groups")
1522 "builtin",
1523 net_sam_list_builtin,
1524 NET_TRANSPORT_LOCAL,
1525 N_("List builtin groups"),
1526 N_("net sam list builtin\n"
1527 " List builtin groups")
1530 "workstations",
1531 net_sam_list_workstations,
1532 NET_TRANSPORT_LOCAL,
1533 N_("List domain member workstations"),
1534 N_("net sam list workstations\n"
1535 " List domain member workstations")
1537 {NULL, NULL, 0, NULL, NULL}
1540 return net_run_function(c, argc, argv, "net sam list", func);
1544 * Show details of SAM entries
1547 static int net_sam_show(struct net_context *c, int argc, const char **argv)
1549 struct dom_sid sid;
1550 enum lsa_SidType type;
1551 const char *dom, *name;
1553 if (argc != 1 || c->display_usage) {
1554 d_fprintf(stderr, "%s\n%s",
1555 _("Usage:"),
1556 _("net sam show <name>\n"));
1557 return -1;
1560 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1561 &dom, &name, &sid, &type)) {
1562 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
1563 return -1;
1566 d_printf(_("%s\\%s is a %s with SID %s\n"), dom, name,
1567 sid_type_lookup(type), sid_string_tos(&sid));
1569 return 0;
1572 #ifdef HAVE_LDAP
1575 * Init an LDAP tree with default users and Groups
1576 * if ldapsam:editposix is enabled
1579 static int net_sam_provision(struct net_context *c, int argc, const char **argv)
1581 TALLOC_CTX *tc;
1582 char *ldap_bk;
1583 char *ldap_uri = NULL;
1584 char *p;
1585 struct smbldap_state *ls;
1586 GROUP_MAP gmap;
1587 struct dom_sid gsid;
1588 gid_t domusers_gid = -1;
1589 gid_t domadmins_gid = -1;
1590 struct samu *samuser;
1591 struct passwd *pwd;
1592 bool is_ipa = false;
1594 if (c->display_usage) {
1595 d_printf( "%s\n"
1596 "net sam provision\n"
1597 " %s\n",
1598 _("Usage:"),
1599 _("Init an LDAP tree with default users/groups"));
1600 return 0;
1603 tc = talloc_new(NULL);
1604 if (!tc) {
1605 d_fprintf(stderr, _("Out of Memory!\n"));
1606 return -1;
1609 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
1610 d_fprintf(stderr, _("talloc failed\n"));
1611 talloc_free(tc);
1612 return -1;
1614 p = strchr(ldap_bk, ':');
1615 if (p) {
1616 *p = 0;
1617 ldap_uri = talloc_strdup(tc, p+1);
1618 trim_char(ldap_uri, ' ', ' ');
1621 trim_char(ldap_bk, ' ', ' ');
1623 if (strcmp(ldap_bk, "IPA_ldapsam") == 0 ) {
1624 is_ipa = true;
1627 if (strcmp(ldap_bk, "ldapsam") != 0 && !is_ipa ) {
1628 d_fprintf(stderr,
1629 _("Provisioning works only with ldapsam backend\n"));
1630 goto failed;
1633 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1634 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1636 d_fprintf(stderr, _("Provisioning works only if ldapsam:trusted"
1637 " and ldapsam:editposix are enabled.\n"));
1638 goto failed;
1641 if (!is_ipa && !winbind_ping()) {
1642 d_fprintf(stderr, _("winbind seems not to run. Provisioning "
1643 "LDAP only works when winbind runs.\n"));
1644 goto failed;
1647 if (!NT_STATUS_IS_OK(smbldap_init(tc, NULL, ldap_uri, &ls))) {
1648 d_fprintf(stderr, _("Unable to connect to the LDAP server.\n"));
1649 goto failed;
1652 d_printf(_("Checking for Domain Users group.\n"));
1654 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_USERS);
1656 if (!pdb_getgrsid(&gmap, gsid)) {
1657 LDAPMod **mods = NULL;
1658 char *dn;
1659 char *uname;
1660 char *wname;
1661 char *gidstr;
1662 char *gtype;
1663 int rc;
1665 d_printf(_("Adding the Domain Users group.\n"));
1667 /* lets allocate a new groupid for this group */
1668 if (is_ipa) {
1669 domusers_gid = 999;
1670 } else {
1671 if (!winbind_allocate_gid(&domusers_gid)) {
1672 d_fprintf(stderr, _("Unable to allocate a new gid to "
1673 "create Domain Users group!\n"));
1674 goto domu_done;
1678 uname = talloc_strdup(tc, "domusers");
1679 wname = talloc_strdup(tc, "Domain Users");
1680 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1681 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domusers_gid);
1682 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1684 if (!uname || !wname || !dn || !gidstr || !gtype) {
1685 d_fprintf(stderr, "Out of Memory!\n");
1686 goto failed;
1689 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1690 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1691 if (is_ipa) {
1692 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "groupofnames");
1693 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "nestedgroup");
1694 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "ipausergroup");
1696 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1697 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1698 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1699 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1700 sid_string_talloc(tc, &gsid));
1701 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1703 talloc_autofree_ldapmod(tc, mods);
1705 rc = smbldap_add(ls, dn, mods);
1707 if (rc != LDAP_SUCCESS) {
1708 d_fprintf(stderr, _("Failed to add Domain Users group "
1709 "to ldap directory\n"));
1712 if (is_ipa) {
1713 if (!pdb_getgrsid(&gmap, gsid)) {
1714 d_fprintf(stderr, _("Failed to read just "
1715 "created domain group.\n"));
1716 goto failed;
1717 } else {
1718 domusers_gid = gmap.gid;
1721 } else {
1722 domusers_gid = gmap.gid;
1723 d_printf(_("found!\n"));
1726 domu_done:
1728 d_printf(_("Checking for Domain Admins group.\n"));
1730 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_ADMINS);
1732 if (!pdb_getgrsid(&gmap, gsid)) {
1733 LDAPMod **mods = NULL;
1734 char *dn;
1735 char *uname;
1736 char *wname;
1737 char *gidstr;
1738 char *gtype;
1739 int rc;
1741 d_printf(_("Adding the Domain Admins group.\n"));
1743 /* lets allocate a new groupid for this group */
1744 if (is_ipa) {
1745 domadmins_gid = 999;
1746 } else {
1747 if (!winbind_allocate_gid(&domadmins_gid)) {
1748 d_fprintf(stderr, _("Unable to allocate a new gid to "
1749 "create Domain Admins group!\n"));
1750 goto doma_done;
1754 uname = talloc_strdup(tc, "domadmins");
1755 wname = talloc_strdup(tc, "Domain Admins");
1756 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1757 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1758 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1760 if (!uname || !wname || !dn || !gidstr || !gtype) {
1761 d_fprintf(stderr, _("Out of Memory!\n"));
1762 goto failed;
1765 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1766 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1767 if (is_ipa) {
1768 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "groupofnames");
1769 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "nestedgroup");
1770 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "ipausergroup");
1772 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1773 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1774 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1775 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1776 sid_string_talloc(tc, &gsid));
1777 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1779 talloc_autofree_ldapmod(tc, mods);
1781 rc = smbldap_add(ls, dn, mods);
1783 if (rc != LDAP_SUCCESS) {
1784 d_fprintf(stderr, _("Failed to add Domain Admins group "
1785 "to ldap directory\n"));
1788 if (is_ipa) {
1789 if (!pdb_getgrsid(&gmap, gsid)) {
1790 d_fprintf(stderr, _("Failed to read just "
1791 "created domain group.\n"));
1792 goto failed;
1793 } else {
1794 domadmins_gid = gmap.gid;
1797 } else {
1798 domadmins_gid = gmap.gid;
1799 d_printf(_("found!\n"));
1802 doma_done:
1804 d_printf(_("Check for Administrator account.\n"));
1806 samuser = samu_new(tc);
1807 if (!samuser) {
1808 d_fprintf(stderr, _("Out of Memory!\n"));
1809 goto failed;
1812 if (!pdb_getsampwnam(samuser, "Administrator")) {
1813 LDAPMod **mods = NULL;
1814 struct dom_sid sid;
1815 char *dn;
1816 char *name;
1817 char *uidstr;
1818 char *gidstr;
1819 char *shell;
1820 char *dir;
1821 char *princ;
1822 uid_t uid;
1823 int rc;
1825 d_printf(_("Adding the Administrator user.\n"));
1827 if (domadmins_gid == -1) {
1828 d_fprintf(stderr,
1829 _("Can't create Administrator user, Domain "
1830 "Admins group not available!\n"));
1831 goto done;
1834 if (is_ipa) {
1835 uid = 999;
1836 } else {
1837 if (!winbind_allocate_uid(&uid)) {
1838 d_fprintf(stderr,
1839 _("Unable to allocate a new uid to create "
1840 "the Administrator user!\n"));
1841 goto done;
1845 name = talloc_strdup(tc, "Administrator");
1846 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1847 uidstr = talloc_asprintf(tc, "%u", (unsigned int)uid);
1848 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1849 dir = talloc_sub_specified(tc, lp_template_homedir(),
1850 "Administrator",
1851 get_global_sam_name(),
1852 uid, domadmins_gid);
1853 shell = talloc_sub_specified(tc, lp_template_shell(),
1854 "Administrator",
1855 get_global_sam_name(),
1856 uid, domadmins_gid);
1858 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1859 d_fprintf(stderr, _("Out of Memory!\n"));
1860 goto failed;
1863 sid_compose(&sid, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR);
1865 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1866 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1867 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1868 if (is_ipa) {
1869 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "person");
1870 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "organizationalperson");
1871 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "inetorgperson");
1872 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "inetuser");
1873 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "krbprincipalaux");
1874 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "krbticketpolicyaux");
1875 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sn", name);
1876 princ = talloc_asprintf(tc, "%s@%s", name, lp_realm());
1877 if (!princ) {
1878 d_fprintf(stderr, _("Out of Memory!\n"));
1879 goto failed;
1881 smbldap_set_mod(&mods, LDAP_MOD_ADD, "krbPrincipalName", princ);
1883 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1884 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1885 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1886 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1887 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1888 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1889 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1890 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1891 sid_string_talloc(tc, &sid));
1892 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1893 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1894 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1896 talloc_autofree_ldapmod(tc, mods);
1898 rc = smbldap_add(ls, dn, mods);
1900 if (rc != LDAP_SUCCESS) {
1901 d_fprintf(stderr, _("Failed to add Administrator user "
1902 "to ldap directory\n"));
1905 if (is_ipa) {
1906 if (!pdb_getsampwnam(samuser, "Administrator")) {
1907 d_fprintf(stderr, _("Failed to read just "
1908 "created user.\n"));
1909 goto failed;
1912 } else {
1913 d_printf(_("found!\n"));
1916 d_printf(_("Checking for Guest user.\n"));
1918 samuser = samu_new(tc);
1919 if (!samuser) {
1920 d_fprintf(stderr, _("Out of Memory!\n"));
1921 goto failed;
1924 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1925 LDAPMod **mods = NULL;
1926 struct dom_sid sid;
1927 char *dn;
1928 char *uidstr;
1929 char *gidstr;
1930 int rc;
1932 d_printf(_("Adding the Guest user.\n"));
1934 sid_compose(&sid, get_global_sam_sid(), DOMAIN_RID_GUEST);
1936 pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
1938 if (!pwd) {
1939 if (domusers_gid == -1) {
1940 d_fprintf(stderr,
1941 _("Can't create Guest user, Domain "
1942 "Users group not available!\n"));
1943 goto done;
1945 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1946 d_fprintf(stderr, _("talloc failed\n"));
1947 goto done;
1949 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1951 if (is_ipa) {
1952 pwd->pw_uid = 999;
1953 } else {
1954 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1955 d_fprintf(stderr,
1956 _("Unable to allocate a new uid to "
1957 "create the Guest user!\n"));
1958 goto done;
1961 pwd->pw_gid = domusers_gid;
1962 pwd->pw_dir = talloc_strdup(tc, "/");
1963 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1964 if (!pwd->pw_dir || !pwd->pw_shell) {
1965 d_fprintf(stderr, _("Out of Memory!\n"));
1966 goto failed;
1970 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1971 uidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_uid);
1972 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
1973 if (!dn || !uidstr || !gidstr) {
1974 d_fprintf(stderr, _("Out of Memory!\n"));
1975 goto failed;
1978 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1979 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1980 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1981 if (is_ipa) {
1982 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "person");
1983 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "organizationalperson");
1984 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "inetorgperson");
1985 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "inetuser");
1986 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "krbprincipalaux");
1987 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "krbticketpolicyaux");
1988 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sn", pwd->pw_name);
1990 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1991 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1992 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1993 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1994 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1995 if ((pwd->pw_dir != NULL) && (pwd->pw_dir[0] != '\0')) {
1996 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1998 if ((pwd->pw_shell != NULL) && (pwd->pw_shell[0] != '\0')) {
1999 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
2001 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
2002 sid_string_talloc(tc, &sid));
2003 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
2004 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
2005 NEW_PW_FORMAT_SPACE_PADDED_LEN));
2007 talloc_autofree_ldapmod(tc, mods);
2009 rc = smbldap_add(ls, dn, mods);
2011 if (rc != LDAP_SUCCESS) {
2012 d_fprintf(stderr, _("Failed to add Guest user to "
2013 "ldap directory\n"));
2016 if (is_ipa) {
2017 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
2018 d_fprintf(stderr, _("Failed to read just "
2019 "created user.\n"));
2020 goto failed;
2023 } else {
2024 d_printf(_("found!\n"));
2027 d_printf(_("Checking Guest's group.\n"));
2029 pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
2030 if (!pwd) {
2031 d_fprintf(stderr,
2032 _("Failed to find just created Guest account!\n"
2033 " Is nss properly configured?!\n"));
2034 goto failed;
2037 if (pwd->pw_gid == domusers_gid) {
2038 d_printf(_("found!\n"));
2039 goto done;
2042 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
2043 LDAPMod **mods = NULL;
2044 char *dn;
2045 char *uname;
2046 char *wname;
2047 char *gidstr;
2048 char *gtype;
2049 int rc;
2051 d_printf(_("Adding the Domain Guests group.\n"));
2053 uname = talloc_strdup(tc, "domguests");
2054 wname = talloc_strdup(tc, "Domain Guests");
2055 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
2056 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
2057 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
2059 if (!uname || !wname || !dn || !gidstr || !gtype) {
2060 d_fprintf(stderr, _("Out of Memory!\n"));
2061 goto failed;
2064 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_GUESTS);
2066 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
2067 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
2068 if (is_ipa) {
2069 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "groupofnames");
2070 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "nestedgroup");
2071 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "ipausergroup");
2073 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
2074 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
2075 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
2076 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
2077 sid_string_talloc(tc, &gsid));
2078 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
2080 talloc_autofree_ldapmod(tc, mods);
2082 rc = smbldap_add(ls, dn, mods);
2084 if (rc != LDAP_SUCCESS) {
2085 d_fprintf(stderr,
2086 _("Failed to add Domain Guests group to ldap "
2087 "directory\n"));
2089 } else {
2090 d_printf(_("found!\n"));
2094 done:
2095 talloc_free(tc);
2096 return 0;
2098 failed:
2099 talloc_free(tc);
2100 return -1;
2103 #endif
2105 /***********************************************************
2106 migrated functionality from smbgroupedit
2107 **********************************************************/
2108 int net_sam(struct net_context *c, int argc, const char **argv)
2110 struct functable func[] = {
2112 "createbuiltingroup",
2113 net_sam_createbuiltingroup,
2114 NET_TRANSPORT_LOCAL,
2115 N_("Create a new BUILTIN group"),
2116 N_("net sam createbuiltingroup\n"
2117 " Create a new BUILTIN group")
2120 "createlocalgroup",
2121 net_sam_createlocalgroup,
2122 NET_TRANSPORT_LOCAL,
2123 N_("Create a new local group"),
2124 N_("net sam createlocalgroup\n"
2125 " Create a new local group")
2128 "createdomaingroup",
2129 net_sam_createdomaingroup,
2130 NET_TRANSPORT_LOCAL,
2131 N_("Create a new group"),
2132 N_("net sam createdomaingroup\n"
2133 " Create a new group")
2136 "deletelocalgroup",
2137 net_sam_deletelocalgroup,
2138 NET_TRANSPORT_LOCAL,
2139 N_("Delete an existing local group"),
2140 N_("net sam deletelocalgroup\n"
2141 " Delete an existing local group")
2144 "deletedomaingroup",
2145 net_sam_deletedomaingroup,
2146 NET_TRANSPORT_LOCAL,
2147 N_("Delete a domain group"),
2148 N_("net sam deletedomaingroup\n"
2149 " Delete a group")
2152 "mapunixgroup",
2153 net_sam_mapunixgroup,
2154 NET_TRANSPORT_LOCAL,
2155 N_("Map a unix group to a domain group"),
2156 N_("net sam mapunixgroup\n"
2157 " Map a unix group to a domain group")
2160 "unmapunixgroup",
2161 net_sam_unmapunixgroup,
2162 NET_TRANSPORT_LOCAL,
2163 N_("Remove a group mapping of an unix group to a "
2164 "domain group"),
2165 N_("net sam unmapunixgroup\n"
2166 " Remove a group mapping of an unix group to a "
2167 "domain group")
2170 "addmem",
2171 net_sam_addmem,
2172 NET_TRANSPORT_LOCAL,
2173 N_("Add a member to a group"),
2174 N_("net sam addmem\n"
2175 " Add a member to a group")
2178 "delmem",
2179 net_sam_delmem,
2180 NET_TRANSPORT_LOCAL,
2181 N_("Delete a member from a group"),
2182 N_("net sam delmem\n"
2183 " Delete a member from a group")
2186 "listmem",
2187 net_sam_listmem,
2188 NET_TRANSPORT_LOCAL,
2189 N_("List group members"),
2190 N_("net sam listmem\n"
2191 " List group members")
2194 "list",
2195 net_sam_list,
2196 NET_TRANSPORT_LOCAL,
2197 N_("List users, groups and local groups"),
2198 N_("net sam list\n"
2199 " List users, groups and local groups")
2202 "show",
2203 net_sam_show,
2204 NET_TRANSPORT_LOCAL,
2205 N_("Show details of a SAM entry"),
2206 N_("net sam show\n"
2207 " Show details of a SAM entry")
2210 "set",
2211 net_sam_set,
2212 NET_TRANSPORT_LOCAL,
2213 N_("Set details of a SAM account"),
2214 N_("net sam set\n"
2215 " Set details of a SAM account")
2218 "policy",
2219 net_sam_policy,
2220 NET_TRANSPORT_LOCAL,
2221 N_("Set account policies"),
2222 N_("net sam policy\n"
2223 " Set account policies")
2226 "rights",
2227 net_sam_rights,
2228 NET_TRANSPORT_LOCAL,
2229 N_("Manipulate user privileges"),
2230 N_("net sam rights\n"
2231 " Manipulate user privileges")
2233 #ifdef HAVE_LDAP
2235 "provision",
2236 net_sam_provision,
2237 NET_TRANSPORT_LOCAL,
2238 N_("Provision a clean user database"),
2239 N_("net sam privison\n"
2240 " Provision a clear user database")
2242 #endif
2243 {NULL, NULL, 0, NULL, NULL}
2246 if (getuid() != 0) {
2247 d_fprintf(stderr, _("You are not root, most things won't "
2248 "work\n"));
2251 return net_run_function(c, argc, argv, "net sam", func);