s3: Fix uninitialized memory read in talloc_free()
[Samba.git] / source3 / utils / net_sam.c
blob5456abba98d37264955c4fbc2565d2f4be974ddc
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 "utils/net.h"
25 * Set a user's data
28 static int net_sam_userset(struct net_context *c, int argc, const char **argv,
29 const char *field,
30 bool (*fn)(struct samu *, const char *,
31 enum pdb_value_state))
33 struct samu *sam_acct = NULL;
34 DOM_SID sid;
35 enum lsa_SidType type;
36 const char *dom, *name;
37 NTSTATUS status;
39 if (argc != 2 || c->display_usage) {
40 d_fprintf(stderr, "%s\n", _("Usage:"));
41 d_fprintf(stderr, _("net sam set %s <user> <value>\n"),
42 field);
43 return -1;
46 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
47 &dom, &name, &sid, &type)) {
48 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
49 return -1;
52 if (type != SID_NAME_USER) {
53 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
54 sid_type_lookup(type));
55 return -1;
58 if ( !(sam_acct = samu_new( NULL )) ) {
59 d_fprintf(stderr, _("Internal error\n"));
60 return -1;
63 if (!pdb_getsampwsid(sam_acct, &sid)) {
64 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
65 return -1;
68 if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
69 d_fprintf(stderr, _("Internal error\n"));
70 return -1;
73 status = pdb_update_sam_account(sam_acct);
74 if (!NT_STATUS_IS_OK(status)) {
75 d_fprintf(stderr, _("Updating sam account %s failed with %s\n"),
76 argv[0], nt_errstr(status));
77 return -1;
80 TALLOC_FREE(sam_acct);
82 d_printf(_("Updated %s for %s\\%s to %s\n"), field, dom, name, argv[1]);
83 return 0;
86 static int net_sam_set_fullname(struct net_context *c, int argc,
87 const char **argv)
89 return net_sam_userset(c, argc, argv, "fullname",
90 pdb_set_fullname);
93 static int net_sam_set_logonscript(struct net_context *c, int argc,
94 const char **argv)
96 return net_sam_userset(c, argc, argv, "logonscript",
97 pdb_set_logon_script);
100 static int net_sam_set_profilepath(struct net_context *c, int argc,
101 const char **argv)
103 return net_sam_userset(c, argc, argv, "profilepath",
104 pdb_set_profile_path);
107 static int net_sam_set_homedrive(struct net_context *c, int argc,
108 const char **argv)
110 return net_sam_userset(c, argc, argv, "homedrive",
111 pdb_set_dir_drive);
114 static int net_sam_set_homedir(struct net_context *c, int argc,
115 const char **argv)
117 return net_sam_userset(c, argc, argv, "homedir",
118 pdb_set_homedir);
121 static int net_sam_set_workstations(struct net_context *c, int argc,
122 const char **argv)
124 return net_sam_userset(c, argc, argv, "workstations",
125 pdb_set_workstations);
129 * Set account flags
132 static int net_sam_set_userflag(struct net_context *c, int argc,
133 const char **argv, const char *field,
134 uint16 flag)
136 struct samu *sam_acct = NULL;
137 DOM_SID sid;
138 enum lsa_SidType type;
139 const char *dom, *name;
140 NTSTATUS status;
141 uint32_t acct_flags;
143 if ((argc != 2) || c->display_usage ||
144 (!strequal(argv[1], "yes") &&
145 !strequal(argv[1], "no"))) {
146 d_fprintf(stderr, "%s\n", _("Usage:"));
147 d_fprintf(stderr, _("net sam set %s <user> [yes|no]\n"),
148 field);
149 return -1;
152 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
153 &dom, &name, &sid, &type)) {
154 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
155 return -1;
158 if (type != SID_NAME_USER) {
159 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
160 sid_type_lookup(type));
161 return -1;
164 if ( !(sam_acct = samu_new( NULL )) ) {
165 d_fprintf(stderr, _("Internal error\n"));
166 return -1;
169 if (!pdb_getsampwsid(sam_acct, &sid)) {
170 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
171 return -1;
174 acct_flags = pdb_get_acct_ctrl(sam_acct);
176 if (strequal(argv[1], "yes")) {
177 acct_flags |= flag;
178 } else {
179 acct_flags &= ~flag;
182 pdb_set_acct_ctrl(sam_acct, acct_flags, PDB_CHANGED);
184 status = pdb_update_sam_account(sam_acct);
185 if (!NT_STATUS_IS_OK(status)) {
186 d_fprintf(stderr, _("Updating sam account %s failed with %s\n"),
187 argv[0], nt_errstr(status));
188 return -1;
191 TALLOC_FREE(sam_acct);
193 d_fprintf(stderr, _("Updated flag %s for %s\\%s to %s\n"), field, dom,
194 name, argv[1]);
195 return 0;
198 static int net_sam_set_disabled(struct net_context *c, int argc,
199 const char **argv)
201 return net_sam_set_userflag(c, argc, argv, "disabled", ACB_DISABLED);
204 static int net_sam_set_pwnotreq(struct net_context *c, int argc,
205 const char **argv)
207 return net_sam_set_userflag(c, argc, argv, "pwnotreq", ACB_PWNOTREQ);
210 static int net_sam_set_autolock(struct net_context *c, int argc,
211 const char **argv)
213 return net_sam_set_userflag(c, argc, argv, "autolock", ACB_AUTOLOCK);
216 static int net_sam_set_pwnoexp(struct net_context *c, int argc,
217 const char **argv)
219 return net_sam_set_userflag(c, argc, argv, "pwnoexp", ACB_PWNOEXP);
223 * Set pass last change time, based on force pass change now
226 static int net_sam_set_pwdmustchangenow(struct net_context *c, int argc,
227 const char **argv)
229 struct samu *sam_acct = NULL;
230 DOM_SID sid;
231 enum lsa_SidType type;
232 const char *dom, *name;
233 NTSTATUS status;
235 if ((argc != 2) || c->display_usage ||
236 (!strequal(argv[1], "yes") &&
237 !strequal(argv[1], "no"))) {
238 d_fprintf(stderr, "%s\n%s",
239 _("Usage:"),
240 _("net sam set pwdmustchangenow <user> [yes|no]\n"));
241 return -1;
244 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
245 &dom, &name, &sid, &type)) {
246 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
247 return -1;
250 if (type != SID_NAME_USER) {
251 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
252 sid_type_lookup(type));
253 return -1;
256 if ( !(sam_acct = samu_new( NULL )) ) {
257 d_fprintf(stderr, _("Internal error\n"));
258 return -1;
261 if (!pdb_getsampwsid(sam_acct, &sid)) {
262 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
263 return -1;
266 if (strequal(argv[1], "yes")) {
267 pdb_set_pass_last_set_time(sam_acct, 0, PDB_CHANGED);
268 } else {
269 pdb_set_pass_last_set_time(sam_acct, time(NULL), PDB_CHANGED);
272 status = pdb_update_sam_account(sam_acct);
273 if (!NT_STATUS_IS_OK(status)) {
274 d_fprintf(stderr, _("Updating sam account %s failed with %s\n"),
275 argv[0], nt_errstr(status));
276 return -1;
279 TALLOC_FREE(sam_acct);
281 d_fprintf(stderr, _("Updated 'user must change password at next logon' "
282 "for %s\\%s to %s\n"), dom,
283 name, argv[1]);
284 return 0;
289 * Set a user's or a group's comment
292 static int net_sam_set_comment(struct net_context *c, int argc,
293 const char **argv)
295 GROUP_MAP map;
296 DOM_SID sid;
297 enum lsa_SidType type;
298 const char *dom, *name;
299 NTSTATUS status;
301 if (argc != 2 || c->display_usage) {
302 d_fprintf(stderr, "%s\n%s",
303 _("Usage:"),
304 _("net sam set comment <name> <comment>\n"));
305 return -1;
308 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
309 &dom, &name, &sid, &type)) {
310 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
311 return -1;
314 if (type == SID_NAME_USER) {
315 return net_sam_userset(c, argc, argv, "comment",
316 pdb_set_acct_desc);
319 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
320 (type != SID_NAME_WKN_GRP)) {
321 d_fprintf(stderr, _("%s is a %s, not a group\n"), argv[0],
322 sid_type_lookup(type));
323 return -1;
326 if (!pdb_getgrsid(&map, sid)) {
327 d_fprintf(stderr, _("Could not load group %s\n"), argv[0]);
328 return -1;
331 fstrcpy(map.comment, argv[1]);
333 status = pdb_update_group_mapping_entry(&map);
335 if (!NT_STATUS_IS_OK(status)) {
336 d_fprintf(stderr, _("Updating group mapping entry failed with "
337 "%s\n"), nt_errstr(status));
338 return -1;
341 d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
342 argv[1]);
344 return 0;
347 static int net_sam_set(struct net_context *c, int argc, const char **argv)
349 struct functable func[] = {
351 "homedir",
352 net_sam_set_homedir,
353 NET_TRANSPORT_LOCAL,
354 N_("Change a user's home directory"),
355 N_("net sam set homedir\n"
356 " Change a user's home directory")
359 "profilepath",
360 net_sam_set_profilepath,
361 NET_TRANSPORT_LOCAL,
362 N_("Change a user's profile path"),
363 N_("net sam set profilepath\n"
364 " Change a user's profile path")
367 "comment",
368 net_sam_set_comment,
369 NET_TRANSPORT_LOCAL,
370 N_("Change a users or groups description"),
371 N_("net sam set comment\n"
372 " Change a users or groups description")
375 "fullname",
376 net_sam_set_fullname,
377 NET_TRANSPORT_LOCAL,
378 N_("Change a user's full name"),
379 N_("net sam set fullname\n"
380 " Change a user's full name")
383 "logonscript",
384 net_sam_set_logonscript,
385 NET_TRANSPORT_LOCAL,
386 N_("Change a user's logon script"),
387 N_("net sam set logonscript\n"
388 " Change a user's logon script")
391 "homedrive",
392 net_sam_set_homedrive,
393 NET_TRANSPORT_LOCAL,
394 N_("Change a user's home drive"),
395 N_("net sam set homedrive\n"
396 " Change a user's home drive")
399 "workstations",
400 net_sam_set_workstations,
401 NET_TRANSPORT_LOCAL,
402 N_("Change a user's allowed workstations"),
403 N_("net sam set workstations\n"
404 " Change a user's allowed workstations")
407 "disabled",
408 net_sam_set_disabled,
409 NET_TRANSPORT_LOCAL,
410 N_("Disable/Enable a user"),
411 N_("net sam set disable\n"
412 " Disable/Enable a user")
415 "pwnotreq",
416 net_sam_set_pwnotreq,
417 NET_TRANSPORT_LOCAL,
418 N_("Disable/Enable the password not required flag"),
419 N_("net sam set pwnotreq\n"
420 " Disable/Enable the password not required flag")
423 "autolock",
424 net_sam_set_autolock,
425 NET_TRANSPORT_LOCAL,
426 N_("Disable/Enable a user's lockout flag"),
427 N_("net sam set autolock\n"
428 " Disable/Enable a user's lockout flag")
431 "pwnoexp",
432 net_sam_set_pwnoexp,
433 NET_TRANSPORT_LOCAL,
434 N_("Disable/Enable whether a user's pw does not "
435 "expire"),
436 N_("net sam set pwnoexp\n"
437 " Disable/Enable whether a user's pw does not "
438 "expire")
441 "pwdmustchangenow",
442 net_sam_set_pwdmustchangenow,
443 NET_TRANSPORT_LOCAL,
444 N_("Force users password must change at next logon"),
445 N_("net sam set pwdmustchangenow\n"
446 " Force users password must change at next logon")
448 {NULL, NULL, 0, NULL, NULL}
451 return net_run_function(c, argc, argv, "net sam set", func);
455 * Manage account policies
458 static int net_sam_policy_set(struct net_context *c, int argc, const char **argv)
460 const char *account_policy = NULL;
461 uint32 value = 0;
462 uint32 old_value = 0;
463 enum pdb_policy_type field;
464 char *endptr;
466 if (argc != 2 || c->display_usage) {
467 d_fprintf(stderr, "%s\n%s",
468 _("Usage:"),
469 _("net sam policy set \"<account policy>\" <value>\n"));
470 return -1;
473 account_policy = argv[0];
474 field = account_policy_name_to_typenum(account_policy);
476 if (strequal(argv[1], "forever") || strequal(argv[1], "never")
477 || strequal(argv[1], "off")) {
478 value = -1;
480 else {
481 value = strtoul(argv[1], &endptr, 10);
483 if ((endptr == argv[1]) || (endptr[0] != '\0')) {
484 d_printf(_("Unable to set policy \"%s\"! Invalid value "
485 "\"%s\".\n"),
486 account_policy, argv[1]);
487 return -1;
491 if (field == 0) {
492 const char **names;
493 int i, count;
495 account_policy_names_list(&names, &count);
496 d_fprintf(stderr, _("No account policy \"%s\"!\n\n"), argv[0]);
497 d_fprintf(stderr, _("Valid account policies are:\n"));
499 for (i=0; i<count; i++) {
500 d_fprintf(stderr, "%s\n", names[i]);
503 SAFE_FREE(names);
504 return -1;
507 if (!pdb_get_account_policy(field, &old_value)) {
508 d_fprintf(stderr, _("Valid account policy, but unable to fetch "
509 "value!\n"));
510 } else {
511 d_printf(_("Account policy \"%s\" value was: %d\n"),
512 account_policy, old_value);
515 if (!pdb_set_account_policy(field, value)) {
516 d_fprintf(stderr, _("Valid account policy, but unable to "
517 "set value!\n"));
518 return -1;
519 } else {
520 d_printf(_("Account policy \"%s\" value is now: %d\n"),
521 account_policy, value);
524 return 0;
527 static int net_sam_policy_show(struct net_context *c, int argc, const char **argv)
529 const char *account_policy = NULL;
530 uint32 old_value;
531 enum pdb_policy_type field;
533 if (argc != 1 || c->display_usage) {
534 d_fprintf(stderr, "%s\n%s",
535 _("Usage:"),
536 _("net sam policy show \"<account policy>\"\n"));
537 return -1;
540 account_policy = argv[0];
541 field = account_policy_name_to_typenum(account_policy);
543 if (field == 0) {
544 const char **names;
545 int count;
546 int i;
547 account_policy_names_list(&names, &count);
548 d_fprintf(stderr, _("No account policy by that name!\n"));
549 if (count != 0) {
550 d_fprintf(stderr, _("Valid account policies "
551 "are:\n"));
552 for (i=0; i<count; i++) {
553 d_fprintf(stderr, "%s\n", names[i]);
556 SAFE_FREE(names);
557 return -1;
560 if (!pdb_get_account_policy(field, &old_value)) {
561 fprintf(stderr, _("Valid account policy, but unable to "
562 "fetch value!\n"));
563 return -1;
566 printf(_("Account policy \"%s\" description: %s\n"),
567 account_policy, account_policy_get_desc(field));
568 printf(_("Account policy \"%s\" value is: %d\n"), account_policy,
569 old_value);
570 return 0;
573 static int net_sam_policy_list(struct net_context *c, int argc, const char **argv)
575 const char **names;
576 int count;
577 int i;
579 if (c->display_usage) {
580 d_printf( "%s\n"
581 "net sam policy list\n"
582 " %s\n",
583 _("Usage:"),
584 _("List account policies"));
585 return 0;
588 account_policy_names_list(&names, &count);
589 if (count != 0) {
590 d_fprintf(stderr, _("Valid account policies "
591 "are:\n"));
592 for (i = 0; i < count ; i++) {
593 d_fprintf(stderr, "%s\n", names[i]);
596 SAFE_FREE(names);
597 return -1;
600 static int net_sam_policy(struct net_context *c, int argc, const char **argv)
602 struct functable func[] = {
604 "list",
605 net_sam_policy_list,
606 NET_TRANSPORT_LOCAL,
607 N_("List account policies"),
608 N_("net sam policy list\n"
609 " List account policies")
612 "show",
613 net_sam_policy_show,
614 NET_TRANSPORT_LOCAL,
615 N_("Show account policies"),
616 N_("net sam policy show\n"
617 " Show account policies")
620 "set",
621 net_sam_policy_set,
622 NET_TRANSPORT_LOCAL,
623 N_("Change account policies"),
624 N_("net sam policy set\n"
625 " Change account policies")
627 {NULL, NULL, 0, NULL, NULL}
630 return net_run_function(c, argc, argv, "net sam policy", func);
633 extern PRIVS privs[];
635 static int net_sam_rights_list(struct net_context *c, int argc,
636 const char **argv)
638 SE_PRIV mask;
640 if (argc > 1 || c->display_usage) {
641 d_fprintf(stderr, "%s\n%s",
642 _("Usage:"),
643 _("net sam rights list [privilege name]\n"));
644 return -1;
647 if (argc == 0) {
648 int i;
649 int num = count_all_privileges();
651 for (i=0; i<num; i++) {
652 d_printf("%s\n", privs[i].name);
654 return 0;
657 if (se_priv_from_name(argv[0], &mask)) {
658 DOM_SID *sids;
659 int i, num_sids;
660 NTSTATUS status;
662 status = privilege_enum_sids(&mask, talloc_tos(),
663 &sids, &num_sids);
664 if (!NT_STATUS_IS_OK(status)) {
665 d_fprintf(stderr, _("Could not list rights: %s\n"),
666 nt_errstr(status));
667 return -1;
670 for (i=0; i<num_sids; i++) {
671 const char *dom, *name;
672 enum lsa_SidType type;
674 if (lookup_sid(talloc_tos(), &sids[i], &dom, &name,
675 &type)) {
676 d_printf("%s\\%s\n", dom, name);
678 else {
679 d_printf("%s\n", sid_string_tos(&sids[i]));
682 return 0;
685 return -1;
688 static int net_sam_rights_grant(struct net_context *c, int argc,
689 const char **argv)
691 DOM_SID sid;
692 enum lsa_SidType type;
693 const char *dom, *name;
694 SE_PRIV mask;
695 int i;
697 if (argc < 2 || c->display_usage) {
698 d_fprintf(stderr, "%s\n%s",
699 _("Usage:"),
700 _("net sam rights grant <name> <rights> ...\n"));
701 return -1;
704 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
705 &dom, &name, &sid, &type)) {
706 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
707 return -1;
710 for (i=1; i < argc; i++) {
711 if (!se_priv_from_name(argv[i], &mask)) {
712 d_fprintf(stderr, _("%s unknown\n"), argv[i]);
713 return -1;
716 if (!grant_privilege(&sid, &mask)) {
717 d_fprintf(stderr, _("Could not grant privilege\n"));
718 return -1;
721 d_printf(_("Granted %s to %s\\%s\n"), argv[i], dom, name);
724 return 0;
727 static int net_sam_rights_revoke(struct net_context *c, int argc,
728 const char **argv)
730 DOM_SID sid;
731 enum lsa_SidType type;
732 const char *dom, *name;
733 SE_PRIV mask;
734 int i;
736 if (argc < 2 || c->display_usage) {
737 d_fprintf(stderr, "%s\n%s",
738 _("Usage:"),
739 _("net sam rights revoke <name> <rights>\n"));
740 return -1;
743 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
744 &dom, &name, &sid, &type)) {
745 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
746 return -1;
749 for (i=1; i < argc; i++) {
751 if (!se_priv_from_name(argv[i], &mask)) {
752 d_fprintf(stderr, _("%s unknown\n"), argv[i]);
753 return -1;
756 if (!revoke_privilege(&sid, &mask)) {
757 d_fprintf(stderr, _("Could not revoke privilege\n"));
758 return -1;
761 d_printf(_("Revoked %s from %s\\%s\n"), argv[i], dom, name);
764 return 0;
767 static int net_sam_rights(struct net_context *c, int argc, const char **argv)
769 struct functable func[] = {
771 "list",
772 net_sam_rights_list,
773 NET_TRANSPORT_LOCAL,
774 N_("List possible user rights"),
775 N_("net sam rights list\n"
776 " List possible user rights")
779 "grant",
780 net_sam_rights_grant,
781 NET_TRANSPORT_LOCAL,
782 N_("Grant right(s)"),
783 N_("net sam rights grant\n"
784 " Grant right(s)")
787 "revoke",
788 net_sam_rights_revoke,
789 NET_TRANSPORT_LOCAL,
790 N_("Revoke right(s)"),
791 N_("net sam rights revoke\n"
792 " Revoke right(s)")
794 {NULL, NULL, 0, NULL, NULL}
796 return net_run_function(c, argc, argv, "net sam rights", func);
800 * Map a unix group to a domain group
803 static NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
805 NTSTATUS status;
806 GROUP_MAP map;
807 const char *grpname, *dom, *name;
808 uint32 rid;
810 if (pdb_getgrgid(&map, grp->gr_gid)) {
811 return NT_STATUS_GROUP_EXISTS;
814 map.gid = grp->gr_gid;
815 grpname = grp->gr_name;
817 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
818 &dom, &name, NULL, NULL)) {
820 const char *tmp = talloc_asprintf(
821 talloc_tos(), "Unix Group %s", grp->gr_name);
823 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
824 grpname, dom, name, tmp));
825 grpname = tmp;
828 if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
829 NULL, NULL, NULL, NULL)) {
830 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
831 return NT_STATUS_GROUP_EXISTS;
834 fstrcpy(map.nt_name, grpname);
836 if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
837 if (!pdb_new_rid(&rid)) {
838 DEBUG(3, ("Could not get a new RID for %s\n",
839 grp->gr_name));
840 return NT_STATUS_ACCESS_DENIED;
842 } else {
843 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
846 sid_compose(&map.sid, get_global_sam_sid(), rid);
847 map.sid_name_use = SID_NAME_DOM_GRP;
848 fstrcpy(map.comment, talloc_asprintf(talloc_tos(), "Unix Group %s",
849 grp->gr_name));
851 status = pdb_add_group_mapping_entry(&map);
852 if (NT_STATUS_IS_OK(status)) {
853 *pmap = map;
855 return status;
858 static int net_sam_mapunixgroup(struct net_context *c, int argc, const char **argv)
860 NTSTATUS status;
861 GROUP_MAP map;
862 struct group *grp;
864 if (argc != 1 || c->display_usage) {
865 d_fprintf(stderr, "%s\n%s",
866 _("Usage:"),
867 _("net sam mapunixgroup <name>\n"));
868 return -1;
871 grp = getgrnam(argv[0]);
872 if (grp == NULL) {
873 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
874 return -1;
877 status = map_unix_group(grp, &map);
879 if (!NT_STATUS_IS_OK(status)) {
880 d_fprintf(stderr, _("Mapping group %s failed with %s\n"),
881 argv[0], nt_errstr(status));
882 return -1;
885 d_printf(_("Mapped unix group %s to SID %s\n"), argv[0],
886 sid_string_tos(&map.sid));
888 return 0;
892 * Remove a group mapping
895 static NTSTATUS unmap_unix_group(const struct group *grp, GROUP_MAP *pmap)
897 NTSTATUS status;
898 GROUP_MAP map;
899 const char *grpname;
900 DOM_SID dom_sid;
902 map.gid = grp->gr_gid;
903 grpname = grp->gr_name;
905 if (!lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
906 NULL, NULL, NULL, NULL)) {
907 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp->gr_name));
908 return NT_STATUS_NO_SUCH_GROUP;
911 fstrcpy(map.nt_name, grpname);
913 if (!pdb_gid_to_sid(map.gid, &dom_sid)) {
914 return NT_STATUS_UNSUCCESSFUL;
917 status = pdb_delete_group_mapping_entry(dom_sid);
919 return status;
922 static int net_sam_unmapunixgroup(struct net_context *c, int argc, const char **argv)
924 NTSTATUS status;
925 GROUP_MAP map;
926 struct group *grp;
928 if (argc != 1 || c->display_usage) {
929 d_fprintf(stderr, "%s\n%s",
930 _("Usage:"),
931 _("net sam unmapunixgroup <name>\n"));
932 return -1;
935 grp = getgrnam(argv[0]);
936 if (grp == NULL) {
937 d_fprintf(stderr, _("Could not find mapping for group %s.\n"),
938 argv[0]);
939 return -1;
942 status = unmap_unix_group(grp, &map);
944 if (!NT_STATUS_IS_OK(status)) {
945 d_fprintf(stderr, _("Unmapping group %s failed with %s.\n"),
946 argv[0], nt_errstr(status));
947 return -1;
950 d_printf(_("Unmapped unix group %s.\n"), argv[0]);
952 return 0;
956 * Create a domain group
959 static int net_sam_createdomaingroup(struct net_context *c, int argc,
960 const char **argv)
962 NTSTATUS status;
963 uint32 rid;
965 if (argc != 1 || c->display_usage) {
966 d_fprintf(stderr, "%s\n%s",
967 _("Usage:"),
968 _("net sam createdomaingroup <name>\n"));
969 return -1;
972 status = pdb_create_dom_group(talloc_tos(), argv[0], &rid);
974 if (!NT_STATUS_IS_OK(status)) {
975 d_fprintf(stderr, _("Creating %s failed with %s\n"),
976 argv[0], nt_errstr(status));
977 return -1;
980 d_printf(_("Created domain group %s with RID %d\n"), argv[0], rid);
982 return 0;
986 * Delete a domain group
989 static int net_sam_deletedomaingroup(struct net_context *c, int argc,
990 const char **argv)
992 DOM_SID sid;
993 uint32_t rid;
994 enum lsa_SidType type;
995 const char *dom, *name;
996 NTSTATUS status;
998 if (argc != 1 || c->display_usage) {
999 d_fprintf(stderr, "%s\n%s",
1000 _("Usage:"),
1001 _("net sam deletelocalgroup <name>\n"));
1002 return -1;
1005 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1006 &dom, &name, &sid, &type)) {
1007 d_fprintf(stderr, _("Could not find %s.\n"), argv[0]);
1008 return -1;
1011 if (type != SID_NAME_DOM_GRP) {
1012 d_fprintf(stderr, _("%s is a %s, not a domain group.\n"),
1013 argv[0], sid_type_lookup(type));
1014 return -1;
1017 sid_peek_rid(&sid, &rid);
1019 status = pdb_delete_dom_group(talloc_tos(), rid);
1021 if (!NT_STATUS_IS_OK(status)) {
1022 d_fprintf(stderr,_("Deleting domain group %s failed with %s\n"),
1023 argv[0], nt_errstr(status));
1024 return -1;
1027 d_printf(_("Deleted domain group %s.\n"), argv[0]);
1029 return 0;
1033 * Create a local group
1036 static int net_sam_createlocalgroup(struct net_context *c, int argc, const char **argv)
1038 NTSTATUS status;
1039 uint32 rid;
1041 if (argc != 1 || c->display_usage) {
1042 d_fprintf(stderr, "%s\n%s",
1043 _("Usage:"),
1044 _("net sam createlocalgroup <name>\n"));
1045 return -1;
1048 if (!winbind_ping()) {
1049 d_fprintf(stderr, _("winbind seems not to run. "
1050 "createlocalgroup only works when winbind runs.\n"));
1051 return -1;
1054 status = pdb_create_alias(argv[0], &rid);
1056 if (!NT_STATUS_IS_OK(status)) {
1057 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1058 argv[0], nt_errstr(status));
1059 return -1;
1062 d_printf(_("Created local group %s with RID %d\n"), argv[0], rid);
1064 return 0;
1068 * Delete a local group
1071 static int net_sam_deletelocalgroup(struct net_context *c, int argc, const char **argv)
1073 DOM_SID sid;
1074 enum lsa_SidType type;
1075 const char *dom, *name;
1076 NTSTATUS status;
1078 if (argc != 1 || c->display_usage) {
1079 d_fprintf(stderr, "%s\n%s",
1080 _("Usage:"),
1081 _("net sam deletelocalgroup <name>\n"));
1082 return -1;
1085 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1086 &dom, &name, &sid, &type)) {
1087 d_fprintf(stderr,_("Could not find %s.\n"), argv[0]);
1088 return -1;
1091 if (type != SID_NAME_ALIAS) {
1092 d_fprintf(stderr, _("%s is a %s, not a local group.\n"),argv[0],
1093 sid_type_lookup(type));
1094 return -1;
1097 status = pdb_delete_alias(&sid);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 d_fprintf(stderr, _("Deleting local group %s failed with %s\n"),
1101 argv[0], nt_errstr(status));
1102 return -1;
1105 d_printf(_("Deleted local group %s.\n"), argv[0]);
1107 return 0;
1111 * Create a builtin group
1114 static int net_sam_createbuiltingroup(struct net_context *c, int argc, const char **argv)
1116 NTSTATUS status;
1117 uint32 rid;
1118 enum lsa_SidType type;
1119 fstring groupname;
1120 DOM_SID sid;
1122 if (argc != 1 || c->display_usage) {
1123 d_fprintf(stderr, "%s\n%s",
1124 _("Usage:"),
1125 _("net sam createbuiltingroup <name>\n"));
1126 return -1;
1129 if (!winbind_ping()) {
1130 d_fprintf(stderr, _("winbind seems not to run. "
1131 "createbuiltingroup only works when winbind "
1132 "runs.\n"));
1133 return -1;
1136 /* validate the name and get the group */
1138 fstrcpy( groupname, "BUILTIN\\" );
1139 fstrcat( groupname, argv[0] );
1141 if ( !lookup_name(talloc_tos(), groupname, LOOKUP_NAME_ALL, NULL,
1142 NULL, &sid, &type)) {
1143 d_fprintf(stderr, _("%s is not a BUILTIN group\n"), argv[0]);
1144 return -1;
1147 if ( !sid_peek_rid( &sid, &rid ) ) {
1148 d_fprintf(stderr, _("Failed to get RID for %s\n"), argv[0]);
1149 return -1;
1152 status = pdb_create_builtin_alias( rid );
1154 if (!NT_STATUS_IS_OK(status)) {
1155 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1156 argv[0], nt_errstr(status));
1157 return -1;
1160 d_printf(_("Created BUILTIN group %s with RID %d\n"), argv[0], rid);
1162 return 0;
1166 * Add a group member
1169 static int net_sam_addmem(struct net_context *c, int argc, const char **argv)
1171 const char *groupdomain, *groupname, *memberdomain, *membername;
1172 DOM_SID group, member;
1173 enum lsa_SidType grouptype, membertype;
1174 NTSTATUS status;
1176 if (argc != 2 || c->display_usage) {
1177 d_fprintf(stderr, "%s\n%s",
1178 _("Usage:"),
1179 _("net sam addmem <group> <member>\n"));
1180 return -1;
1183 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1184 &groupdomain, &groupname, &group, &grouptype)) {
1185 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1186 return -1;
1189 /* check to see if the member to be added is a name or a SID */
1191 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1192 &memberdomain, &membername, &member, &membertype))
1194 /* try it as a SID */
1196 if ( !string_to_sid( &member, argv[1] ) ) {
1197 d_fprintf(stderr, _("Could not find member %s\n"),
1198 argv[1]);
1199 return -1;
1202 if ( !lookup_sid(talloc_tos(), &member, &memberdomain,
1203 &membername, &membertype) )
1205 d_fprintf(stderr, _("Could not resolve SID %s\n"),
1206 argv[1]);
1207 return -1;
1211 if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
1212 if ((membertype != SID_NAME_USER) &&
1213 (membertype != SID_NAME_DOM_GRP)) {
1214 d_fprintf(stderr, _("%s is a local group, only users "
1215 "and domain groups can be added.\n"
1216 "%s is a %s\n"), argv[0], argv[1],
1217 sid_type_lookup(membertype));
1218 return -1;
1220 status = pdb_add_aliasmem(&group, &member);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 d_fprintf(stderr, _("Adding local group member failed "
1224 "with %s\n"), nt_errstr(status));
1225 return -1;
1227 } else if (grouptype == SID_NAME_DOM_GRP) {
1228 uint32_t grouprid, memberrid;
1230 sid_peek_rid(&group, &grouprid);
1231 sid_peek_rid(&member, &memberrid);
1233 status = pdb_add_groupmem(talloc_tos(), grouprid, memberrid);
1234 if (!NT_STATUS_IS_OK(status)) {
1235 d_fprintf(stderr, _("Adding domain group member failed "
1236 "with %s\n"), nt_errstr(status));
1237 return -1;
1239 } else {
1240 d_fprintf(stderr, _("Can only add members to local groups so "
1241 "far, %s is a %s\n"), argv[0],
1242 sid_type_lookup(grouptype));
1243 return -1;
1246 d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain, membername,
1247 groupdomain, groupname);
1249 return 0;
1253 * Delete a group member
1256 static int net_sam_delmem(struct net_context *c, int argc, const char **argv)
1258 const char *groupdomain, *groupname;
1259 const char *memberdomain = NULL;
1260 const char *membername = NULL;
1261 DOM_SID group, member;
1262 enum lsa_SidType grouptype;
1263 NTSTATUS status;
1265 if (argc != 2 || c->display_usage) {
1266 d_fprintf(stderr,"%s\n%s",
1267 _("Usage:"),
1268 _("net sam delmem <group> <member>\n"));
1269 return -1;
1272 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1273 &groupdomain, &groupname, &group, &grouptype)) {
1274 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1275 return -1;
1278 if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1279 &memberdomain, &membername, &member, NULL)) {
1280 if (!string_to_sid(&member, argv[1])) {
1281 d_fprintf(stderr, _("Could not find member %s\n"),
1282 argv[1]);
1283 return -1;
1287 if ((grouptype == SID_NAME_ALIAS) ||
1288 (grouptype == SID_NAME_WKN_GRP)) {
1289 status = pdb_del_aliasmem(&group, &member);
1291 if (!NT_STATUS_IS_OK(status)) {
1292 d_fprintf(stderr,_("Deleting local group member failed "
1293 "with %s\n"), nt_errstr(status));
1294 return -1;
1296 } else if (grouptype == SID_NAME_DOM_GRP) {
1297 uint32_t grouprid, memberrid;
1299 sid_peek_rid(&group, &grouprid);
1300 sid_peek_rid(&member, &memberrid);
1302 status = pdb_del_groupmem(talloc_tos(), grouprid, memberrid);
1303 if (!NT_STATUS_IS_OK(status)) {
1304 d_fprintf(stderr, _("Deleting domain group member "
1305 "failed with %s\n"), nt_errstr(status));
1306 return -1;
1308 } else {
1309 d_fprintf(stderr, _("Can only delete members from local groups "
1310 "so far, %s is a %s\n"), argv[0],
1311 sid_type_lookup(grouptype));
1312 return -1;
1315 if (membername != NULL) {
1316 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1317 memberdomain, membername, groupdomain, groupname);
1318 } else {
1319 d_printf(_("Deleted %s from %s\\%s\n"),
1320 sid_string_tos(&member), groupdomain, groupname);
1323 return 0;
1327 * List group members
1330 static int net_sam_listmem(struct net_context *c, int argc, const char **argv)
1332 const char *groupdomain, *groupname;
1333 DOM_SID group;
1334 DOM_SID *members = NULL;
1335 size_t i, num_members = 0;
1336 enum lsa_SidType grouptype;
1337 NTSTATUS status;
1339 if (argc != 1 || c->display_usage) {
1340 d_fprintf(stderr, "%s\n%s",
1341 _("Usage:"),
1342 _("net sam listmem <group>\n"));
1343 return -1;
1346 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1347 &groupdomain, &groupname, &group, &grouptype)) {
1348 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1349 return -1;
1352 if ((grouptype == SID_NAME_ALIAS) ||
1353 (grouptype == SID_NAME_WKN_GRP)) {
1354 status = pdb_enum_aliasmem(&group, talloc_tos(), &members,
1355 &num_members);
1356 if (!NT_STATUS_IS_OK(status)) {
1357 d_fprintf(stderr, _("Listing group members failed with "
1358 "%s\n"), nt_errstr(status));
1359 return -1;
1361 } else if (grouptype == SID_NAME_DOM_GRP) {
1362 uint32_t *rids;
1364 status = pdb_enum_group_members(talloc_tos(), &group,
1365 &rids, &num_members);
1366 if (!NT_STATUS_IS_OK(status)) {
1367 d_fprintf(stderr, _("Listing group members failed with "
1368 "%s\n"), nt_errstr(status));
1369 return -1;
1372 members = talloc_array(talloc_tos(), struct dom_sid,
1373 num_members);
1374 if (members == NULL) {
1375 TALLOC_FREE(rids);
1376 return -1;
1379 for (i=0; i<num_members; i++) {
1380 sid_compose(&members[i], get_global_sam_sid(),
1381 rids[i]);
1383 TALLOC_FREE(rids);
1384 } else {
1385 d_fprintf(stderr,_("Can only list local group members so far.\n"
1386 "%s is a %s\n"), argv[0], sid_type_lookup(grouptype));
1387 return -1;
1390 d_printf(_("%s\\%s has %u members\n"), groupdomain, groupname,
1391 (unsigned int)num_members);
1392 for (i=0; i<num_members; i++) {
1393 const char *dom, *name;
1394 if (lookup_sid(talloc_tos(), &members[i], &dom, &name, NULL)) {
1395 d_printf(" %s\\%s\n", dom, name);
1396 } else {
1397 d_printf(" %s\n", sid_string_tos(&members[i]));
1401 TALLOC_FREE(members);
1403 return 0;
1407 * Do the listing
1409 static int net_sam_do_list(struct net_context *c, int argc, const char **argv,
1410 struct pdb_search *search, const char *what)
1412 bool verbose = (argc == 1);
1414 if ((argc > 1) || c->display_usage ||
1415 ((argc == 1) && !strequal(argv[0], "verbose"))) {
1416 d_fprintf(stderr, "%s\n", _("Usage:"));
1417 d_fprintf(stderr, _("net sam list %s [verbose]\n"), what);
1418 return -1;
1421 if (search == NULL) {
1422 d_fprintf(stderr, _("Could not start search\n"));
1423 return -1;
1426 while (true) {
1427 struct samr_displayentry entry;
1428 if (!search->next_entry(search, &entry)) {
1429 break;
1431 if (verbose) {
1432 d_printf("%s:%d:%s\n",
1433 entry.account_name,
1434 entry.rid,
1435 entry.description);
1436 } else {
1437 d_printf("%s\n", entry.account_name);
1441 TALLOC_FREE(search);
1442 return 0;
1445 static int net_sam_list_users(struct net_context *c, int argc,
1446 const char **argv)
1448 return net_sam_do_list(c, argc, argv,
1449 pdb_search_users(talloc_tos(), ACB_NORMAL),
1450 "users");
1453 static int net_sam_list_groups(struct net_context *c, int argc,
1454 const char **argv)
1456 return net_sam_do_list(c, argc, argv, pdb_search_groups(talloc_tos()),
1457 "groups");
1460 static int net_sam_list_localgroups(struct net_context *c, int argc,
1461 const char **argv)
1463 return net_sam_do_list(c, argc, argv,
1464 pdb_search_aliases(talloc_tos(),
1465 get_global_sam_sid()),
1466 "localgroups");
1469 static int net_sam_list_builtin(struct net_context *c, int argc,
1470 const char **argv)
1472 return net_sam_do_list(c, argc, argv,
1473 pdb_search_aliases(talloc_tos(),
1474 &global_sid_Builtin),
1475 "builtin");
1478 static int net_sam_list_workstations(struct net_context *c, int argc,
1479 const char **argv)
1481 return net_sam_do_list(c, argc, argv,
1482 pdb_search_users(talloc_tos(), ACB_WSTRUST),
1483 "workstations");
1487 * List stuff
1490 static int net_sam_list(struct net_context *c, int argc, const char **argv)
1492 struct functable func[] = {
1494 "users",
1495 net_sam_list_users,
1496 NET_TRANSPORT_LOCAL,
1497 N_("List SAM users"),
1498 N_("net sam list users\n"
1499 " List SAM users")
1502 "groups",
1503 net_sam_list_groups,
1504 NET_TRANSPORT_LOCAL,
1505 N_("List SAM groups"),
1506 N_("net sam list groups\n"
1507 " List SAM groups")
1510 "localgroups",
1511 net_sam_list_localgroups,
1512 NET_TRANSPORT_LOCAL,
1513 N_("List SAM local groups"),
1514 N_("net sam list localgroups\n"
1515 " List SAM local groups")
1518 "builtin",
1519 net_sam_list_builtin,
1520 NET_TRANSPORT_LOCAL,
1521 N_("List builtin groups"),
1522 N_("net sam list builtin\n"
1523 " List builtin groups")
1526 "workstations",
1527 net_sam_list_workstations,
1528 NET_TRANSPORT_LOCAL,
1529 N_("List domain member workstations"),
1530 N_("net sam list workstations\n"
1531 " List domain member workstations")
1533 {NULL, NULL, 0, NULL, NULL}
1536 return net_run_function(c, argc, argv, "net sam list", func);
1540 * Show details of SAM entries
1543 static int net_sam_show(struct net_context *c, int argc, const char **argv)
1545 DOM_SID sid;
1546 enum lsa_SidType type;
1547 const char *dom, *name;
1549 if (argc != 1 || c->display_usage) {
1550 d_fprintf(stderr, "%s\n%s",
1551 _("Usage:"),
1552 _("net sam show <name>\n"));
1553 return -1;
1556 if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1557 &dom, &name, &sid, &type)) {
1558 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
1559 return -1;
1562 d_printf(_("%s\\%s is a %s with SID %s\n"), dom, name,
1563 sid_type_lookup(type), sid_string_tos(&sid));
1565 return 0;
1568 #ifdef HAVE_LDAP
1571 * Init an LDAP tree with default users and Groups
1572 * if ldapsam:editposix is enabled
1575 static int net_sam_provision(struct net_context *c, int argc, const char **argv)
1577 TALLOC_CTX *tc;
1578 char *ldap_bk;
1579 char *ldap_uri = NULL;
1580 char *p;
1581 struct smbldap_state *ls;
1582 GROUP_MAP gmap;
1583 DOM_SID gsid;
1584 gid_t domusers_gid = -1;
1585 gid_t domadmins_gid = -1;
1586 struct samu *samuser;
1587 struct passwd *pwd;
1589 if (c->display_usage) {
1590 d_printf( "%s\n"
1591 "net sam provision\n"
1592 " %s\n",
1593 _("Usage:"),
1594 _("Init an LDAP tree with default users/groups"));
1595 return 0;
1598 tc = talloc_new(NULL);
1599 if (!tc) {
1600 d_fprintf(stderr, _("Out of Memory!\n"));
1601 return -1;
1604 if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
1605 d_fprintf(stderr, _("talloc failed\n"));
1606 talloc_free(tc);
1607 return -1;
1609 p = strchr(ldap_bk, ':');
1610 if (p) {
1611 *p = 0;
1612 ldap_uri = talloc_strdup(tc, p+1);
1613 trim_char(ldap_uri, ' ', ' ');
1616 trim_char(ldap_bk, ' ', ' ');
1618 if (strcmp(ldap_bk, "ldapsam") != 0) {
1619 d_fprintf(stderr,
1620 _("Provisioning works only with ldapsam backend\n"));
1621 goto failed;
1624 if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1625 !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1627 d_fprintf(stderr, _("Provisioning works only if ldapsam:trusted"
1628 " and ldapsam:editposix are enabled.\n"));
1629 goto failed;
1632 if (!winbind_ping()) {
1633 d_fprintf(stderr, _("winbind seems not to run. Provisioning "
1634 "LDAP only works when winbind runs.\n"));
1635 goto failed;
1638 if (!NT_STATUS_IS_OK(smbldap_init(tc, NULL, ldap_uri, &ls))) {
1639 d_fprintf(stderr, _("Unable to connect to the LDAP server.\n"));
1640 goto failed;
1643 d_printf(_("Checking for Domain Users group.\n"));
1645 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_USERS);
1647 if (!pdb_getgrsid(&gmap, gsid)) {
1648 LDAPMod **mods = NULL;
1649 char *dn;
1650 char *uname;
1651 char *wname;
1652 char *gidstr;
1653 char *gtype;
1654 int rc;
1656 d_printf(_("Adding the Domain Users group.\n"));
1658 /* lets allocate a new groupid for this group */
1659 if (!winbind_allocate_gid(&domusers_gid)) {
1660 d_fprintf(stderr, _("Unable to allocate a new gid to "
1661 "create Domain Users group!\n"));
1662 goto domu_done;
1665 uname = talloc_strdup(tc, "domusers");
1666 wname = talloc_strdup(tc, "Domain Users");
1667 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1668 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domusers_gid);
1669 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1671 if (!uname || !wname || !dn || !gidstr || !gtype) {
1672 d_fprintf(stderr, "Out of Memory!\n");
1673 goto failed;
1676 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1677 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1678 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1679 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1680 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1681 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1682 sid_string_talloc(tc, &gsid));
1683 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1685 talloc_autofree_ldapmod(tc, mods);
1687 rc = smbldap_add(ls, dn, mods);
1689 if (rc != LDAP_SUCCESS) {
1690 d_fprintf(stderr, _("Failed to add Domain Users group "
1691 "to ldap directory\n"));
1693 } else {
1694 domusers_gid = gmap.gid;
1695 d_printf(_("found!\n"));
1698 domu_done:
1700 d_printf(_("Checking for Domain Admins group.\n"));
1702 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_ADMINS);
1704 if (!pdb_getgrsid(&gmap, gsid)) {
1705 LDAPMod **mods = NULL;
1706 char *dn;
1707 char *uname;
1708 char *wname;
1709 char *gidstr;
1710 char *gtype;
1711 int rc;
1713 d_printf(_("Adding the Domain Admins group.\n"));
1715 /* lets allocate a new groupid for this group */
1716 if (!winbind_allocate_gid(&domadmins_gid)) {
1717 d_fprintf(stderr, _("Unable to allocate a new gid to "
1718 "create Domain Admins group!\n"));
1719 goto doma_done;
1722 uname = talloc_strdup(tc, "domadmins");
1723 wname = talloc_strdup(tc, "Domain Admins");
1724 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1725 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1726 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1728 if (!uname || !wname || !dn || !gidstr || !gtype) {
1729 d_fprintf(stderr, _("Out of Memory!\n"));
1730 goto failed;
1733 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1734 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1735 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1736 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1737 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1738 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1739 sid_string_talloc(tc, &gsid));
1740 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1742 talloc_autofree_ldapmod(tc, mods);
1744 rc = smbldap_add(ls, dn, mods);
1746 if (rc != LDAP_SUCCESS) {
1747 d_fprintf(stderr, _("Failed to add Domain Admins group "
1748 "to ldap directory\n"));
1750 } else {
1751 domadmins_gid = gmap.gid;
1752 d_printf(_("found!\n"));
1755 doma_done:
1757 d_printf(_("Check for Administrator account.\n"));
1759 samuser = samu_new(tc);
1760 if (!samuser) {
1761 d_fprintf(stderr, _("Out of Memory!\n"));
1762 goto failed;
1765 if (!pdb_getsampwnam(samuser, "Administrator")) {
1766 LDAPMod **mods = NULL;
1767 DOM_SID sid;
1768 char *dn;
1769 char *name;
1770 char *uidstr;
1771 char *gidstr;
1772 char *shell;
1773 char *dir;
1774 uid_t uid;
1775 int rc;
1777 d_printf(_("Adding the Administrator user.\n"));
1779 if (domadmins_gid == -1) {
1780 d_fprintf(stderr,
1781 _("Can't create Administrator user, Domain "
1782 "Admins group not available!\n"));
1783 goto done;
1785 if (!winbind_allocate_uid(&uid)) {
1786 d_fprintf(stderr,
1787 _("Unable to allocate a new uid to create "
1788 "the Administrator user!\n"));
1789 goto done;
1791 name = talloc_strdup(tc, "Administrator");
1792 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1793 uidstr = talloc_asprintf(tc, "%u", (unsigned int)uid);
1794 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1795 dir = talloc_sub_specified(tc, lp_template_homedir(),
1796 "Administrator",
1797 get_global_sam_name(),
1798 uid, domadmins_gid);
1799 shell = talloc_sub_specified(tc, lp_template_shell(),
1800 "Administrator",
1801 get_global_sam_name(),
1802 uid, domadmins_gid);
1804 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1805 d_fprintf(stderr, _("Out of Memory!\n"));
1806 goto failed;
1809 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_ADMIN);
1811 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1812 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1813 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1814 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1815 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1816 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1817 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1818 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1819 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1820 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1821 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1822 sid_string_talloc(tc, &sid));
1823 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1824 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1825 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1827 talloc_autofree_ldapmod(tc, mods);
1829 rc = smbldap_add(ls, dn, mods);
1831 if (rc != LDAP_SUCCESS) {
1832 d_fprintf(stderr, _("Failed to add Administrator user "
1833 "to ldap directory\n"));
1835 } else {
1836 d_printf(_("found!\n"));
1839 d_printf(_("Checking for Guest user.\n"));
1841 samuser = samu_new(tc);
1842 if (!samuser) {
1843 d_fprintf(stderr, _("Out of Memory!\n"));
1844 goto failed;
1847 if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1848 LDAPMod **mods = NULL;
1849 DOM_SID sid;
1850 char *dn;
1851 char *uidstr;
1852 char *gidstr;
1853 int rc;
1855 d_printf(_("Adding the Guest user.\n"));
1857 pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
1859 if (!pwd) {
1860 if (domusers_gid == -1) {
1861 d_fprintf(stderr,
1862 _("Can't create Guest user, Domain "
1863 "Users group not available!\n"));
1864 goto done;
1866 if ((pwd = talloc(tc, struct passwd)) == NULL) {
1867 d_fprintf(stderr, _("talloc failed\n"));
1868 goto done;
1870 pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1871 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1872 d_fprintf(stderr,
1873 _("Unable to allocate a new uid to "
1874 "create the Guest user!\n"));
1875 goto done;
1877 pwd->pw_gid = domusers_gid;
1878 pwd->pw_dir = talloc_strdup(tc, "/");
1879 pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1880 if (!pwd->pw_dir || !pwd->pw_shell) {
1881 d_fprintf(stderr, _("Out of Memory!\n"));
1882 goto failed;
1886 sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
1888 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1889 uidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_uid);
1890 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
1891 if (!dn || !uidstr || !gidstr) {
1892 d_fprintf(stderr, _("Out of Memory!\n"));
1893 goto failed;
1896 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1897 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1898 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1899 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1900 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1901 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1902 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1903 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1904 if ((pwd->pw_dir != NULL) && (pwd->pw_dir[0] != '\0')) {
1905 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1907 if ((pwd->pw_shell != NULL) && (pwd->pw_shell[0] != '\0')) {
1908 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1910 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1911 sid_string_talloc(tc, &sid));
1912 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1913 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1914 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1916 talloc_autofree_ldapmod(tc, mods);
1918 rc = smbldap_add(ls, dn, mods);
1920 if (rc != LDAP_SUCCESS) {
1921 d_fprintf(stderr, _("Failed to add Guest user to "
1922 "ldap directory\n"));
1924 } else {
1925 d_printf(_("found!\n"));
1928 d_printf(_("Checking Guest's group.\n"));
1930 pwd = Get_Pwnam_alloc(talloc_autofree_context(), lp_guestaccount());
1931 if (!pwd) {
1932 d_fprintf(stderr,
1933 _("Failed to find just created Guest account!\n"
1934 " Is nss properly configured?!\n"));
1935 goto failed;
1938 if (pwd->pw_gid == domusers_gid) {
1939 d_printf(_("found!\n"));
1940 goto done;
1943 if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
1944 LDAPMod **mods = NULL;
1945 char *dn;
1946 char *uname;
1947 char *wname;
1948 char *gidstr;
1949 char *gtype;
1950 int rc;
1952 d_printf(_("Adding the Domain Guests group.\n"));
1954 uname = talloc_strdup(tc, "domguests");
1955 wname = talloc_strdup(tc, "Domain Guests");
1956 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
1957 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
1958 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1960 if (!uname || !wname || !dn || !gidstr || !gtype) {
1961 d_fprintf(stderr, _("Out of Memory!\n"));
1962 goto failed;
1965 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_GROUP_RID_GUESTS);
1967 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1968 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1969 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1970 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1971 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1972 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1973 sid_string_talloc(tc, &gsid));
1974 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1976 talloc_autofree_ldapmod(tc, mods);
1978 rc = smbldap_add(ls, dn, mods);
1980 if (rc != LDAP_SUCCESS) {
1981 d_fprintf(stderr,
1982 _("Failed to add Domain Guests group to ldap "
1983 "directory\n"));
1985 } else {
1986 d_printf(_("found!\n"));
1990 done:
1991 talloc_free(tc);
1992 return 0;
1994 failed:
1995 talloc_free(tc);
1996 return -1;
1999 #endif
2001 /***********************************************************
2002 migrated functionality from smbgroupedit
2003 **********************************************************/
2004 int net_sam(struct net_context *c, int argc, const char **argv)
2006 struct functable func[] = {
2008 "createbuiltingroup",
2009 net_sam_createbuiltingroup,
2010 NET_TRANSPORT_LOCAL,
2011 N_("Create a new BUILTIN group"),
2012 N_("net sam createbuiltingroup\n"
2013 " Create a new BUILTIN group")
2016 "createlocalgroup",
2017 net_sam_createlocalgroup,
2018 NET_TRANSPORT_LOCAL,
2019 N_("Create a new local group"),
2020 N_("net sam createlocalgroup\n"
2021 " Create a new local group")
2024 "createdomaingroup",
2025 net_sam_createdomaingroup,
2026 NET_TRANSPORT_LOCAL,
2027 N_("Create a new group"),
2028 N_("net sam createdomaingroup\n"
2029 " Create a new group")
2032 "deletelocalgroup",
2033 net_sam_deletelocalgroup,
2034 NET_TRANSPORT_LOCAL,
2035 N_("Delete an existing local group"),
2036 N_("net sam deletelocalgroup\n"
2037 " Delete an existing local group")
2040 "deletedomaingroup",
2041 net_sam_deletedomaingroup,
2042 NET_TRANSPORT_LOCAL,
2043 N_("Delete a domain group"),
2044 N_("net sam deletedomaingroup\n"
2045 " Delete a group")
2048 "mapunixgroup",
2049 net_sam_mapunixgroup,
2050 NET_TRANSPORT_LOCAL,
2051 N_("Map a unix group to a domain group"),
2052 N_("net sam mapunixgroup\n"
2053 " Map a unix group to a domain group")
2056 "unmapunixgroup",
2057 net_sam_unmapunixgroup,
2058 NET_TRANSPORT_LOCAL,
2059 N_("Remove a group mapping of an unix group to a "
2060 "domain group"),
2061 N_("net sam unmapunixgroup\n"
2062 " Remove a group mapping of an unix group to a "
2063 "domain group")
2066 "addmem",
2067 net_sam_addmem,
2068 NET_TRANSPORT_LOCAL,
2069 N_("Add a member to a group"),
2070 N_("net sam addmem\n"
2071 " Add a member to a group")
2074 "delmem",
2075 net_sam_delmem,
2076 NET_TRANSPORT_LOCAL,
2077 N_("Delete a member from a group"),
2078 N_("net sam delmem\n"
2079 " Delete a member from a group")
2082 "listmem",
2083 net_sam_listmem,
2084 NET_TRANSPORT_LOCAL,
2085 N_("List group members"),
2086 N_("net sam listmem\n"
2087 " List group members")
2090 "list",
2091 net_sam_list,
2092 NET_TRANSPORT_LOCAL,
2093 N_("List users, groups and local groups"),
2094 N_("net sam list\n"
2095 " List users, groups and local groups")
2098 "show",
2099 net_sam_show,
2100 NET_TRANSPORT_LOCAL,
2101 N_("Show details of a SAM entry"),
2102 N_("net sam show\n"
2103 " Show details of a SAM entry")
2106 "set",
2107 net_sam_set,
2108 NET_TRANSPORT_LOCAL,
2109 N_("Set details of a SAM account"),
2110 N_("net sam set\n"
2111 " Set details of a SAM account")
2114 "policy",
2115 net_sam_policy,
2116 NET_TRANSPORT_LOCAL,
2117 N_("Set account policies"),
2118 N_("net sam policy\n"
2119 " Set account policies")
2122 "rights",
2123 net_sam_rights,
2124 NET_TRANSPORT_LOCAL,
2125 N_("Manipulate user privileges"),
2126 N_("net sam rights\n"
2127 " Manipulate user privileges")
2129 #ifdef HAVE_LDAP
2131 "provision",
2132 net_sam_provision,
2133 NET_TRANSPORT_LOCAL,
2134 N_("Provision a clean user database"),
2135 N_("net sam privison\n"
2136 " Provision a clear user database")
2138 #endif
2139 {NULL, NULL, 0, NULL, NULL}
2142 if (getuid() != 0) {
2143 d_fprintf(stderr, _("You are not root, most things won't "
2144 "work\n"));
2147 return net_run_function(c, argc, argv, "net sam", func);