4754fb0f34327ba106d212a8faaaa69a74c8f5eb
[idra/samba.git] / source3 / utils / net_sam.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Local SAM access routines
4  *  Copyright (C) Volker Lendecke 2006
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20
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"
30
31 /*
32  * Set a user's data
33  */
34
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))
39 {
40         struct samu *sam_acct = NULL;
41         struct dom_sid sid;
42         enum lsa_SidType type;
43         const char *dom, *name;
44         NTSTATUS status;
45
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;
51         }
52
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;
57         }
58
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;
63         }
64
65         if ( !(sam_acct = samu_new( NULL )) ) {
66                 d_fprintf(stderr, _("Internal error\n"));
67                 return -1;
68         }
69
70         if (!pdb_getsampwsid(sam_acct, &sid)) {
71                 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
72                 return -1;
73         }
74
75         if (!fn(sam_acct, argv[1], PDB_CHANGED)) {
76                 d_fprintf(stderr, _("Internal error\n"));
77                 return -1;
78         }
79
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;
85         }
86
87         TALLOC_FREE(sam_acct);
88
89         d_printf(_("Updated %s for %s\\%s to %s\n"), field, dom, name, argv[1]);
90         return 0;
91 }
92
93 static int net_sam_set_fullname(struct net_context *c, int argc,
94                                 const char **argv)
95 {
96         return net_sam_userset(c, argc, argv, "fullname",
97                                pdb_set_fullname);
98 }
99
100 static int net_sam_set_logonscript(struct net_context *c, int argc,
101                                    const char **argv)
102 {
103         return net_sam_userset(c, argc, argv, "logonscript",
104                                pdb_set_logon_script);
105 }
106
107 static int net_sam_set_profilepath(struct net_context *c, int argc,
108                                    const char **argv)
109 {
110         return net_sam_userset(c, argc, argv, "profilepath",
111                                pdb_set_profile_path);
112 }
113
114 static int net_sam_set_homedrive(struct net_context *c, int argc,
115                                  const char **argv)
116 {
117         return net_sam_userset(c, argc, argv, "homedrive",
118                                pdb_set_dir_drive);
119 }
120
121 static int net_sam_set_homedir(struct net_context *c, int argc,
122                                const char **argv)
123 {
124         return net_sam_userset(c, argc, argv, "homedir",
125                                pdb_set_homedir);
126 }
127
128 static int net_sam_set_workstations(struct net_context *c, int argc,
129                                     const char **argv)
130 {
131         return net_sam_userset(c, argc, argv, "workstations",
132                                pdb_set_workstations);
133 }
134
135 /*
136  * Set account flags
137  */
138
139 static int net_sam_set_userflag(struct net_context *c, int argc,
140                                 const char **argv, const char *field,
141                                 uint16 flag)
142 {
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;
149
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;
157         }
158
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;
163         }
164
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;
169         }
170
171         if ( !(sam_acct = samu_new( NULL )) ) {
172                 d_fprintf(stderr, _("Internal error\n"));
173                 return -1;
174         }
175
176         if (!pdb_getsampwsid(sam_acct, &sid)) {
177                 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
178                 return -1;
179         }
180
181         acct_flags = pdb_get_acct_ctrl(sam_acct);
182
183         if (strequal(argv[1], "yes")) {
184                 acct_flags |= flag;
185         } else {
186                 acct_flags &= ~flag;
187         }
188
189         pdb_set_acct_ctrl(sam_acct, acct_flags, PDB_CHANGED);
190
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;
196         }
197
198         TALLOC_FREE(sam_acct);
199
200         d_fprintf(stderr, _("Updated flag %s for %s\\%s to %s\n"), field, dom,
201                   name, argv[1]);
202         return 0;
203 }
204
205 static int net_sam_set_disabled(struct net_context *c, int argc,
206                                 const char **argv)
207 {
208         return net_sam_set_userflag(c, argc, argv, "disabled", ACB_DISABLED);
209 }
210
211 static int net_sam_set_pwnotreq(struct net_context *c, int argc,
212                                 const char **argv)
213 {
214         return net_sam_set_userflag(c, argc, argv, "pwnotreq", ACB_PWNOTREQ);
215 }
216
217 static int net_sam_set_autolock(struct net_context *c, int argc,
218                                 const char **argv)
219 {
220         return net_sam_set_userflag(c, argc, argv, "autolock", ACB_AUTOLOCK);
221 }
222
223 static int net_sam_set_pwnoexp(struct net_context *c, int argc,
224                                const char **argv)
225 {
226         return net_sam_set_userflag(c, argc, argv, "pwnoexp", ACB_PWNOEXP);
227 }
228
229 /*
230  * Set pass last change time, based on force pass change now
231  */
232
233 static int net_sam_set_pwdmustchangenow(struct net_context *c, int argc,
234                                         const char **argv)
235 {
236         struct samu *sam_acct = NULL;
237         struct dom_sid sid;
238         enum lsa_SidType type;
239         const char *dom, *name;
240         NTSTATUS status;
241
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;
249         }
250
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;
255         }
256
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;
261         }
262
263         if ( !(sam_acct = samu_new( NULL )) ) {
264                 d_fprintf(stderr, _("Internal error\n"));
265                 return -1;
266         }
267
268         if (!pdb_getsampwsid(sam_acct, &sid)) {
269                 d_fprintf(stderr, _("Loading user %s failed\n"), argv[0]);
270                 return -1;
271         }
272
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);
277         }
278
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;
284         }
285
286         TALLOC_FREE(sam_acct);
287
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;
292 }
293
294
295 /*
296  * Set a user's or a group's comment
297  */
298
299 static int net_sam_set_comment(struct net_context *c, int argc,
300                                const char **argv)
301 {
302         GROUP_MAP map;
303         struct dom_sid sid;
304         enum lsa_SidType type;
305         const char *dom, *name;
306         NTSTATUS status;
307
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;
313         }
314
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;
319         }
320
321         if (type == SID_NAME_USER) {
322                 return net_sam_userset(c, argc, argv, "comment",
323                                        pdb_set_acct_desc);
324         }
325
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;
331         }
332
333         if (!pdb_getgrsid(&map, sid)) {
334                 d_fprintf(stderr, _("Could not load group %s\n"), argv[0]);
335                 return -1;
336         }
337
338         fstrcpy(map.comment, argv[1]);
339
340         status = pdb_update_group_mapping_entry(&map);
341
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;
346         }
347
348         d_printf("Updated comment of group %s\\%s to %s\n", dom, name,
349                  argv[1]);
350
351         return 0;
352 }
353
354 static int net_sam_set(struct net_context *c, int argc, const char **argv)
355 {
356         struct functable func[] = {
357                 {
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")
364                 },
365                 {
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")
372                 },
373                 {
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")
380                 },
381                 {
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")
388                 },
389                 {
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")
396                 },
397                 {
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")
404                 },
405                 {
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")
412                 },
413                 {
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")
420                 },
421                 {
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")
428                 },
429                 {
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")
436                 },
437                 {
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")
446                 },
447                 {
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")
454                 },
455                 {NULL, NULL, 0, NULL, NULL}
456         };
457
458         return net_run_function(c, argc, argv, "net sam set", func);
459 }
460
461 /*
462  * Manage account policies
463  */
464
465 static int net_sam_policy_set(struct net_context *c, int argc, const char **argv)
466 {
467         const char *account_policy = NULL;
468         uint32 value = 0;
469         uint32 old_value = 0;
470         enum pdb_policy_type field;
471         char *endptr;
472
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;
478         }
479
480         account_policy = argv[0];
481         field = account_policy_name_to_typenum(account_policy);
482
483         if (strequal(argv[1], "forever") || strequal(argv[1], "never")
484             || strequal(argv[1], "off")) {
485                 value = -1;
486         }
487         else {
488                 value = strtoul(argv[1], &endptr, 10);
489
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;
495                 }
496         }
497
498         if (field == 0) {
499                 const char **names;
500                 int i, count;
501
502                 account_policy_names_list(&names, &count);
503                 d_fprintf(stderr, _("No account policy \"%s\"!\n\n"), argv[0]);
504                 d_fprintf(stderr, _("Valid account policies are:\n"));
505
506                 for (i=0; i<count; i++) {
507                         d_fprintf(stderr, "%s\n", names[i]);
508                 }
509
510                 SAFE_FREE(names);
511                 return -1;
512         }
513
514         if (!pdb_get_account_policy(field, &old_value)) {
515                 d_fprintf(stderr, _("Valid account policy, but unable to fetch "
516                           "value!\n"));
517         } else {
518                 d_printf(_("Account policy \"%s\" value was: %d\n"),
519                         account_policy, old_value);
520         }
521
522         if (!pdb_set_account_policy(field, value)) {
523                 d_fprintf(stderr, _("Valid account policy, but unable to "
524                           "set value!\n"));
525                 return -1;
526         } else {
527                 d_printf(_("Account policy \"%s\" value is now: %d\n"),
528                         account_policy, value);
529         }
530
531         return 0;
532 }
533
534 static int net_sam_policy_show(struct net_context *c, int argc, const char **argv)
535 {
536         const char *account_policy = NULL;
537         uint32 old_value;
538         enum pdb_policy_type field;
539
540         if (argc != 1 || c->display_usage) {
541                 d_fprintf(stderr, "%s\n%s",
542                           _("Usage:"),
543                           _("net sam policy show \"<account policy>\"\n"));
544                 return -1;
545         }
546
547         account_policy = argv[0];
548         field = account_policy_name_to_typenum(account_policy);
549
550         if (field == 0) {
551                 const char **names;
552                 int count;
553                 int i;
554                 account_policy_names_list(&names, &count);
555                 d_fprintf(stderr, _("No account policy by that name!\n"));
556                 if (count != 0) {
557                         d_fprintf(stderr, _("Valid account policies "
558                                   "are:\n"));
559                         for (i=0; i<count; i++) {
560                                 d_fprintf(stderr, "%s\n", names[i]);
561                         }
562                 }
563                 SAFE_FREE(names);
564                 return -1;
565         }
566
567         if (!pdb_get_account_policy(field, &old_value)) {
568                 fprintf(stderr, _("Valid account policy, but unable to "
569                         "fetch value!\n"));
570                 return -1;
571         }
572
573         printf(_("Account policy \"%s\" description: %s\n"),
574                account_policy, account_policy_get_desc(field));
575         printf(_("Account policy \"%s\" value is: %d\n"), account_policy,
576                old_value);
577         return 0;
578 }
579
580 static int net_sam_policy_list(struct net_context *c, int argc, const char **argv)
581 {
582         const char **names;
583         int count;
584         int i;
585
586         if (c->display_usage) {
587                 d_printf(  "%s\n"
588                            "net sam policy list\n"
589                            "    %s\n",
590                          _("Usage:"),
591                          _("List account policies"));
592                 return 0;
593         }
594
595         account_policy_names_list(&names, &count);
596         if (count != 0) {
597                 d_fprintf(stderr, _("Valid account policies "
598                           "are:\n"));
599                 for (i = 0; i < count ; i++) {
600                         d_fprintf(stderr, "%s\n", names[i]);
601                 }
602         }
603         SAFE_FREE(names);
604         return -1;
605 }
606
607 static int net_sam_policy(struct net_context *c, int argc, const char **argv)
608 {
609         struct functable func[] = {
610                 {
611                         "list",
612                         net_sam_policy_list,
613                         NET_TRANSPORT_LOCAL,
614                         N_("List account policies"),
615                         N_("net sam policy list\n"
616                            "    List account policies")
617                 },
618                 {
619                         "show",
620                         net_sam_policy_show,
621                         NET_TRANSPORT_LOCAL,
622                         N_("Show account policies"),
623                         N_("net sam policy show\n"
624                            "    Show account policies")
625                 },
626                 {
627                         "set",
628                         net_sam_policy_set,
629                         NET_TRANSPORT_LOCAL,
630                         N_("Change account policies"),
631                         N_("net sam policy set\n"
632                            "    Change account policies")
633                 },
634                 {NULL, NULL, 0, NULL, NULL}
635         };
636
637         return net_run_function(c, argc, argv, "net sam policy", func);
638 }
639
640 static int net_sam_rights_list(struct net_context *c, int argc,
641                                const char **argv)
642 {
643         enum sec_privilege privilege;
644
645         if (argc > 1 || c->display_usage) {
646                 d_fprintf(stderr, "%s\n%s",
647                           _("Usage:"),
648                           _("net sam rights list [privilege name]\n"));
649                 return -1;
650         }
651
652         if (argc == 0) {
653                 int i;
654                 int num = num_privileges_in_short_list();
655
656                 for (i=0; i<num; i++) {
657                         d_printf("%s\n", sec_privilege_name_from_index(i));
658                 }
659                 return 0;
660         }
661
662         privilege = sec_privilege_id(argv[0]);
663
664         if (privilege != SEC_PRIV_INVALID) {
665                 struct dom_sid *sids;
666                 int i, num_sids;
667                 NTSTATUS status;
668
669                 status = privilege_enum_sids(privilege, talloc_tos(),
670                                              &sids, &num_sids);
671                 if (!NT_STATUS_IS_OK(status)) {
672                         d_fprintf(stderr, _("Could not list rights: %s\n"),
673                                   nt_errstr(status));
674                         return -1;
675                 }
676
677                 for (i=0; i<num_sids; i++) {
678                         const char *dom, *name;
679                         enum lsa_SidType type;
680
681                         if (lookup_sid(talloc_tos(), &sids[i], &dom, &name,
682                                        &type)) {
683                                 d_printf("%s\\%s\n", dom, name);
684                         }
685                         else {
686                                 d_printf("%s\n", sid_string_tos(&sids[i]));
687                         }
688                 }
689                 return 0;
690         }
691
692         return -1;
693 }
694
695 static int net_sam_rights_grant(struct net_context *c, int argc,
696                                 const char **argv)
697 {
698         struct dom_sid sid;
699         enum lsa_SidType type;
700         const char *dom, *name;
701         int i;
702
703         if (argc < 2 || c->display_usage) {
704                 d_fprintf(stderr, "%s\n%s",
705                           _("Usage:"),
706                           _("net sam rights grant <name> <rights> ...\n"));
707                 return -1;
708         }
709
710         if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
711                         &dom, &name, &sid, &type)) {
712                 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
713                 return -1;
714         }
715
716         for (i=1; i < argc; i++) {
717                 enum sec_privilege privilege = sec_privilege_id(argv[i]);
718                 if (privilege == SEC_PRIV_INVALID) {
719                         d_fprintf(stderr, _("%s unknown\n"), argv[i]);
720                         return -1;
721                 }
722
723                 if (!grant_privilege_by_name(&sid, argv[i])) {
724                         d_fprintf(stderr, _("Could not grant privilege\n"));
725                         return -1;
726                 }
727
728                 d_printf(_("Granted %s to %s\\%s\n"), argv[i], dom, name);
729         }
730
731         return 0;
732 }
733
734 static int net_sam_rights_revoke(struct net_context *c, int argc,
735                                 const char **argv)
736 {
737         struct dom_sid sid;
738         enum lsa_SidType type;
739         const char *dom, *name;
740         int i;
741
742         if (argc < 2 || c->display_usage) {
743                 d_fprintf(stderr, "%s\n%s",
744                           _("Usage:"),
745                           _("net sam rights revoke <name> <rights>\n"));
746                 return -1;
747         }
748
749         if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
750                         &dom, &name, &sid, &type)) {
751                 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
752                 return -1;
753         }
754
755         for (i=1; i < argc; i++) {
756                 enum sec_privilege privilege = sec_privilege_id(argv[i]);
757                 if (privilege == SEC_PRIV_INVALID) {
758                         d_fprintf(stderr, _("%s unknown\n"), argv[i]);
759                         return -1;
760                 }
761
762                 if (!revoke_privilege_by_name(&sid, argv[i])) {
763                         d_fprintf(stderr, _("Could not revoke privilege\n"));
764                         return -1;
765                 }
766
767                 d_printf(_("Revoked %s from %s\\%s\n"), argv[i], dom, name);
768         }
769
770         return 0;
771 }
772
773 static int net_sam_rights(struct net_context *c, int argc, const char **argv)
774 {
775         struct functable func[] = {
776                 {
777                         "list",
778                         net_sam_rights_list,
779                         NET_TRANSPORT_LOCAL,
780                         N_("List possible user rights"),
781                         N_("net sam rights list\n"
782                            "    List possible user rights")
783                 },
784                 {
785                         "grant",
786                         net_sam_rights_grant,
787                         NET_TRANSPORT_LOCAL,
788                         N_("Grant right(s)"),
789                         N_("net sam rights grant\n"
790                            "    Grant right(s)")
791                 },
792                 {
793                         "revoke",
794                         net_sam_rights_revoke,
795                         NET_TRANSPORT_LOCAL,
796                         N_("Revoke right(s)"),
797                         N_("net sam rights revoke\n"
798                            "    Revoke right(s)")
799                 },
800                 {NULL, NULL, 0, NULL, NULL}
801         };
802         return net_run_function(c, argc, argv, "net sam rights", func);
803 }
804
805 /*
806  * Map a unix group to a domain group
807  */
808
809 static NTSTATUS map_unix_group(const struct group *grp, GROUP_MAP *pmap)
810 {
811         NTSTATUS status;
812         GROUP_MAP map;
813         const char *grpname, *dom, *name;
814         uint32 rid;
815
816         if (pdb_getgrgid(&map, grp->gr_gid)) {
817                 return NT_STATUS_GROUP_EXISTS;
818         }
819
820         map.gid = grp->gr_gid;
821         grpname = grp->gr_name;
822
823         if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
824                         &dom, &name, NULL, NULL)) {
825
826                 const char *tmp = talloc_asprintf(
827                         talloc_tos(), "Unix Group %s", grp->gr_name);
828
829                 DEBUG(5, ("%s exists as %s\\%s, retrying as \"%s\"\n",
830                           grpname, dom, name, tmp));
831                 grpname = tmp;
832         }
833
834         if (lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
835                         NULL, NULL, NULL, NULL)) {
836                 DEBUG(3, ("\"%s\" exists, can't map it\n", grp->gr_name));
837                 return NT_STATUS_GROUP_EXISTS;
838         }
839
840         fstrcpy(map.nt_name, grpname);
841
842         if (pdb_capabilities() & PDB_CAP_STORE_RIDS) {
843                 if (!pdb_new_rid(&rid)) {
844                         DEBUG(3, ("Could not get a new RID for %s\n",
845                                   grp->gr_name));
846                         return NT_STATUS_ACCESS_DENIED;
847                 }
848         } else {
849                 rid = algorithmic_pdb_gid_to_group_rid( grp->gr_gid );
850         }
851
852         sid_compose(&map.sid, get_global_sam_sid(), rid);
853         map.sid_name_use = SID_NAME_DOM_GRP;
854         fstrcpy(map.comment, talloc_asprintf(talloc_tos(), "Unix Group %s",
855                                              grp->gr_name));
856
857         status = pdb_add_group_mapping_entry(&map);
858         if (NT_STATUS_IS_OK(status)) {
859                 *pmap = map;
860         }
861         return status;
862 }
863
864 static int net_sam_mapunixgroup(struct net_context *c, int argc, const char **argv)
865 {
866         NTSTATUS status;
867         GROUP_MAP map;
868         struct group *grp;
869
870         if (argc != 1 || c->display_usage) {
871                 d_fprintf(stderr, "%s\n%s",
872                           _("Usage:"),
873                           _("net sam mapunixgroup <name>\n"));
874                 return -1;
875         }
876
877         grp = getgrnam(argv[0]);
878         if (grp == NULL) {
879                 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
880                 return -1;
881         }
882
883         status = map_unix_group(grp, &map);
884
885         if (!NT_STATUS_IS_OK(status)) {
886                 d_fprintf(stderr, _("Mapping group %s failed with %s\n"),
887                           argv[0], nt_errstr(status));
888                 return -1;
889         }
890
891         d_printf(_("Mapped unix group %s to SID %s\n"), argv[0],
892                  sid_string_tos(&map.sid));
893
894         return 0;
895 }
896
897 /*
898  * Remove a group mapping
899  */
900
901 static NTSTATUS unmap_unix_group(const struct group *grp, GROUP_MAP *pmap)
902 {
903         GROUP_MAP map;
904         const char *grpname;
905         struct dom_sid dom_sid;
906
907         map.gid = grp->gr_gid;
908         grpname = grp->gr_name;
909
910         if (!lookup_name(talloc_tos(), grpname, LOOKUP_NAME_LOCAL,
911                         NULL, NULL, NULL, NULL)) {
912                 DEBUG(3, ("\"%s\" does not exist, can't unmap it\n", grp->gr_name));
913                 return NT_STATUS_NO_SUCH_GROUP;
914         }
915
916         fstrcpy(map.nt_name, grpname);
917
918         if (!pdb_gid_to_sid(map.gid, &dom_sid)) {
919                 return NT_STATUS_UNSUCCESSFUL;
920         }
921
922         return pdb_delete_group_mapping_entry(dom_sid);
923 }
924
925 static int net_sam_unmapunixgroup(struct net_context *c, int argc, const char **argv)
926 {
927         NTSTATUS status;
928         GROUP_MAP map;
929         struct group *grp;
930
931         if (argc != 1 || c->display_usage) {
932                 d_fprintf(stderr, "%s\n%s",
933                           _("Usage:"),
934                           _("net sam unmapunixgroup <name>\n"));
935                 return -1;
936         }
937
938         grp = getgrnam(argv[0]);
939         if (grp == NULL) {
940                 d_fprintf(stderr, _("Could not find mapping for group %s.\n"),
941                           argv[0]);
942                 return -1;
943         }
944
945         status = unmap_unix_group(grp, &map);
946
947         if (!NT_STATUS_IS_OK(status)) {
948                 d_fprintf(stderr, _("Unmapping group %s failed with %s.\n"),
949                           argv[0], nt_errstr(status));
950                 return -1;
951         }
952
953         d_printf(_("Unmapped unix group %s.\n"), argv[0]);
954
955         return 0;
956 }
957
958 /*
959  * Create a domain group
960  */
961
962 static int net_sam_createdomaingroup(struct net_context *c, int argc,
963                                      const char **argv)
964 {
965         NTSTATUS status;
966         uint32 rid;
967
968         if (argc != 1 || c->display_usage) {
969                 d_fprintf(stderr, "%s\n%s",
970                           _("Usage:"),
971                           _("net sam createdomaingroup <name>\n"));
972                 return -1;
973         }
974
975         status = pdb_create_dom_group(talloc_tos(), argv[0], &rid);
976
977         if (!NT_STATUS_IS_OK(status)) {
978                 d_fprintf(stderr, _("Creating %s failed with %s\n"),
979                           argv[0], nt_errstr(status));
980                 return -1;
981         }
982
983         d_printf(_("Created domain group %s with RID %d\n"), argv[0], rid);
984
985         return 0;
986 }
987
988 /*
989  * Delete a domain group
990  */
991
992 static int net_sam_deletedomaingroup(struct net_context *c, int argc,
993                                      const char **argv)
994 {
995         struct dom_sid sid;
996         uint32_t rid;
997         enum lsa_SidType type;
998         const char *dom, *name;
999         NTSTATUS status;
1000
1001         if (argc != 1 || c->display_usage) {
1002                 d_fprintf(stderr, "%s\n%s",
1003                           _("Usage:"),
1004                           _("net sam deletelocalgroup <name>\n"));
1005                 return -1;
1006         }
1007
1008         if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1009                          &dom, &name, &sid, &type)) {
1010                 d_fprintf(stderr, _("Could not find %s.\n"), argv[0]);
1011                 return -1;
1012         }
1013
1014         if (type != SID_NAME_DOM_GRP) {
1015                 d_fprintf(stderr, _("%s is a %s, not a domain group.\n"),
1016                           argv[0], sid_type_lookup(type));
1017                 return -1;
1018         }
1019
1020         sid_peek_rid(&sid, &rid);
1021
1022         status = pdb_delete_dom_group(talloc_tos(), rid);
1023
1024         if (!NT_STATUS_IS_OK(status)) {
1025                 d_fprintf(stderr,_("Deleting domain group %s failed with %s\n"),
1026                           argv[0], nt_errstr(status));
1027                 return -1;
1028         }
1029
1030         d_printf(_("Deleted domain group %s.\n"), argv[0]);
1031
1032         return 0;
1033 }
1034
1035 /*
1036  * Create a local group
1037  */
1038
1039 static int net_sam_createlocalgroup(struct net_context *c, int argc, const char **argv)
1040 {
1041         NTSTATUS status;
1042         uint32 rid;
1043
1044         if (argc != 1 || c->display_usage) {
1045                 d_fprintf(stderr, "%s\n%s",
1046                           _("Usage:"),
1047                           _("net sam createlocalgroup <name>\n"));
1048                 return -1;
1049         }
1050
1051         if (!winbind_ping()) {
1052                 d_fprintf(stderr, _("winbind seems not to run. "
1053                           "createlocalgroup only works when winbind runs.\n"));
1054                 return -1;
1055         }
1056
1057         status = pdb_create_alias(argv[0], &rid);
1058
1059         if (!NT_STATUS_IS_OK(status)) {
1060                 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1061                           argv[0], nt_errstr(status));
1062                 return -1;
1063         }
1064
1065         d_printf(_("Created local group %s with RID %d\n"), argv[0], rid);
1066
1067         return 0;
1068 }
1069
1070 /*
1071  * Delete a local group
1072  */
1073
1074 static int net_sam_deletelocalgroup(struct net_context *c, int argc, const char **argv)
1075 {
1076         struct dom_sid sid;
1077         enum lsa_SidType type;
1078         const char *dom, *name;
1079         NTSTATUS status;
1080
1081         if (argc != 1 || c->display_usage) {
1082                 d_fprintf(stderr, "%s\n%s",
1083                           _("Usage:"),
1084                           _("net sam deletelocalgroup <name>\n"));
1085                 return -1;
1086         }
1087
1088         if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1089                          &dom, &name, &sid, &type)) {
1090                 d_fprintf(stderr,_("Could not find %s.\n"), argv[0]);
1091                 return -1;
1092         }
1093
1094         if (type != SID_NAME_ALIAS) {
1095                 d_fprintf(stderr, _("%s is a %s, not a local group.\n"),argv[0],
1096                           sid_type_lookup(type));
1097                 return -1;
1098         }
1099
1100         status = pdb_delete_alias(&sid);
1101
1102         if (!NT_STATUS_IS_OK(status)) {
1103                 d_fprintf(stderr, _("Deleting local group %s failed with %s\n"),
1104                           argv[0], nt_errstr(status));
1105                 return -1;
1106         }
1107
1108         d_printf(_("Deleted local group %s.\n"), argv[0]);
1109
1110         return 0;
1111 }
1112
1113 /*
1114  * Create a builtin group
1115  */
1116
1117 static int net_sam_createbuiltingroup(struct net_context *c, int argc, const char **argv)
1118 {
1119         NTSTATUS status;
1120         uint32 rid;
1121         enum lsa_SidType type;
1122         fstring groupname;
1123         struct dom_sid sid;
1124
1125         if (argc != 1 || c->display_usage) {
1126                 d_fprintf(stderr, "%s\n%s",
1127                           _("Usage:"),
1128                           _("net sam createbuiltingroup <name>\n"));
1129                 return -1;
1130         }
1131
1132         if (!winbind_ping()) {
1133                 d_fprintf(stderr, _("winbind seems not to run. "
1134                           "createbuiltingroup only works when winbind "
1135                           "runs.\n"));
1136                 return -1;
1137         }
1138
1139         /* validate the name and get the group */
1140
1141         fstrcpy( groupname, "BUILTIN\\" );
1142         fstrcat( groupname, argv[0] );
1143
1144         if ( !lookup_name(talloc_tos(), groupname, LOOKUP_NAME_ALL, NULL,
1145                           NULL, &sid, &type)) {
1146                 d_fprintf(stderr, _("%s is not a BUILTIN group\n"), argv[0]);
1147                 return -1;
1148         }
1149
1150         if ( !sid_peek_rid( &sid, &rid ) ) {
1151                 d_fprintf(stderr, _("Failed to get RID for %s\n"), argv[0]);
1152                 return -1;
1153         }
1154
1155         status = pdb_create_builtin_alias( rid );
1156
1157         if (!NT_STATUS_IS_OK(status)) {
1158                 d_fprintf(stderr, _("Creating %s failed with %s\n"),
1159                           argv[0], nt_errstr(status));
1160                 return -1;
1161         }
1162
1163         d_printf(_("Created BUILTIN group %s with RID %d\n"), argv[0], rid);
1164
1165         return 0;
1166 }
1167
1168 /*
1169  * Add a group member
1170  */
1171
1172 static int net_sam_addmem(struct net_context *c, int argc, const char **argv)
1173 {
1174         const char *groupdomain, *groupname, *memberdomain, *membername;
1175         struct dom_sid group, member;
1176         enum lsa_SidType grouptype, membertype;
1177         NTSTATUS status;
1178
1179         if (argc != 2 || c->display_usage) {
1180                 d_fprintf(stderr, "%s\n%s",
1181                           _("Usage:"),
1182                           _("net sam addmem <group> <member>\n"));
1183                 return -1;
1184         }
1185
1186         if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1187                          &groupdomain, &groupname, &group, &grouptype)) {
1188                 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1189                 return -1;
1190         }
1191
1192         /* check to see if the member to be added is a name or a SID */
1193
1194         if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1195                          &memberdomain, &membername, &member, &membertype))
1196         {
1197                 /* try it as a SID */
1198
1199                 if ( !string_to_sid( &member, argv[1] ) ) {
1200                         d_fprintf(stderr, _("Could not find member %s\n"),
1201                                   argv[1]);
1202                         return -1;
1203                 }
1204
1205                 if ( !lookup_sid(talloc_tos(), &member, &memberdomain,
1206                         &membername, &membertype) )
1207                 {
1208                         d_fprintf(stderr, _("Could not resolve SID %s\n"),
1209                                   argv[1]);
1210                         return -1;
1211                 }
1212         }
1213
1214         if ((grouptype == SID_NAME_ALIAS) || (grouptype == SID_NAME_WKN_GRP)) {
1215                 if ((membertype != SID_NAME_USER) &&
1216                     (membertype != SID_NAME_DOM_GRP)) {
1217                         d_fprintf(stderr, _("%s is a local group, only users "
1218                                   "and domain groups can be added.\n"
1219                                   "%s is a %s\n"), argv[0], argv[1],
1220                                   sid_type_lookup(membertype));
1221                         return -1;
1222                 }
1223                 status = pdb_add_aliasmem(&group, &member);
1224
1225                 if (!NT_STATUS_IS_OK(status)) {
1226                         d_fprintf(stderr, _("Adding local group member failed "
1227                                   "with %s\n"), nt_errstr(status));
1228                         return -1;
1229                 }
1230         } else if (grouptype == SID_NAME_DOM_GRP) {
1231                 uint32_t grouprid, memberrid;
1232
1233                 sid_peek_rid(&group, &grouprid);
1234                 sid_peek_rid(&member, &memberrid);
1235
1236                 status = pdb_add_groupmem(talloc_tos(), grouprid, memberrid);
1237                 if (!NT_STATUS_IS_OK(status)) {
1238                         d_fprintf(stderr, _("Adding domain group member failed "
1239                                   "with %s\n"), nt_errstr(status));
1240                         return -1;
1241                 }
1242         } else {
1243                 d_fprintf(stderr, _("Can only add members to local groups so "
1244                           "far, %s is a %s\n"), argv[0],
1245                           sid_type_lookup(grouptype));
1246                 return -1;
1247         }
1248
1249         d_printf(_("Added %s\\%s to %s\\%s\n"), memberdomain, membername,
1250                 groupdomain, groupname);
1251
1252         return 0;
1253 }
1254
1255 /*
1256  * Delete a group member
1257  */
1258
1259 static int net_sam_delmem(struct net_context *c, int argc, const char **argv)
1260 {
1261         const char *groupdomain, *groupname;
1262         const char *memberdomain = NULL;
1263         const char *membername = NULL;
1264         struct dom_sid group, member;
1265         enum lsa_SidType grouptype;
1266         NTSTATUS status;
1267
1268         if (argc != 2 || c->display_usage) {
1269                 d_fprintf(stderr,"%s\n%s",
1270                           _("Usage:"),
1271                           _("net sam delmem <group> <member>\n"));
1272                 return -1;
1273         }
1274
1275         if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1276                          &groupdomain, &groupname, &group, &grouptype)) {
1277                 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1278                 return -1;
1279         }
1280
1281         if (!lookup_name(talloc_tos(), argv[1], LOOKUP_NAME_LOCAL,
1282                          &memberdomain, &membername, &member, NULL)) {
1283                 if (!string_to_sid(&member, argv[1])) {
1284                         d_fprintf(stderr, _("Could not find member %s\n"),
1285                                   argv[1]);
1286                         return -1;
1287                 }
1288         }
1289
1290         if ((grouptype == SID_NAME_ALIAS) ||
1291             (grouptype == SID_NAME_WKN_GRP)) {
1292                 status = pdb_del_aliasmem(&group, &member);
1293
1294                 if (!NT_STATUS_IS_OK(status)) {
1295                         d_fprintf(stderr,_("Deleting local group member failed "
1296                                   "with %s\n"), nt_errstr(status));
1297                         return -1;
1298                 }
1299         } else if (grouptype == SID_NAME_DOM_GRP) {
1300                 uint32_t grouprid, memberrid;
1301
1302                 sid_peek_rid(&group, &grouprid);
1303                 sid_peek_rid(&member, &memberrid);
1304
1305                 status = pdb_del_groupmem(talloc_tos(), grouprid, memberrid);
1306                 if (!NT_STATUS_IS_OK(status)) {
1307                         d_fprintf(stderr, _("Deleting domain group member "
1308                                   "failed with %s\n"), nt_errstr(status));
1309                         return -1;
1310                 }
1311         } else {
1312                 d_fprintf(stderr, _("Can only delete members from local groups "
1313                           "so far, %s is a %s\n"), argv[0],
1314                           sid_type_lookup(grouptype));
1315                 return -1;
1316         }
1317
1318         if (membername != NULL) {
1319                 d_printf(_("Deleted %s\\%s from %s\\%s\n"),
1320                          memberdomain, membername, groupdomain, groupname);
1321         } else {
1322                 d_printf(_("Deleted %s from %s\\%s\n"),
1323                          sid_string_tos(&member), groupdomain, groupname);
1324         }
1325
1326         return 0;
1327 }
1328
1329 /*
1330  * List group members
1331  */
1332
1333 static int net_sam_listmem(struct net_context *c, int argc, const char **argv)
1334 {
1335         const char *groupdomain, *groupname;
1336         struct dom_sid group;
1337         struct dom_sid *members = NULL;
1338         size_t i, num_members = 0;
1339         enum lsa_SidType grouptype;
1340         NTSTATUS status;
1341
1342         if (argc != 1 || c->display_usage) {
1343                 d_fprintf(stderr, "%s\n%s",
1344                           _("Usage:"),
1345                           _("net sam listmem <group>\n"));
1346                 return -1;
1347         }
1348
1349         if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1350                          &groupdomain, &groupname, &group, &grouptype)) {
1351                 d_fprintf(stderr, _("Could not find group %s\n"), argv[0]);
1352                 return -1;
1353         }
1354
1355         if ((grouptype == SID_NAME_ALIAS) ||
1356             (grouptype == SID_NAME_WKN_GRP)) {
1357                 status = pdb_enum_aliasmem(&group, talloc_tos(), &members,
1358                                            &num_members);
1359                 if (!NT_STATUS_IS_OK(status)) {
1360                         d_fprintf(stderr, _("Listing group members failed with "
1361                                   "%s\n"), nt_errstr(status));
1362                         return -1;
1363                 }
1364         } else if (grouptype == SID_NAME_DOM_GRP) {
1365                 uint32_t *rids;
1366
1367                 status = pdb_enum_group_members(talloc_tos(), &group,
1368                                                 &rids, &num_members);
1369                 if (!NT_STATUS_IS_OK(status)) {
1370                         d_fprintf(stderr, _("Listing group members failed with "
1371                                   "%s\n"), nt_errstr(status));
1372                         return -1;
1373                 }
1374
1375                 members = talloc_array(talloc_tos(), struct dom_sid,
1376                                        num_members);
1377                 if (members == NULL) {
1378                         TALLOC_FREE(rids);
1379                         return -1;
1380                 }
1381
1382                 for (i=0; i<num_members; i++) {
1383                         sid_compose(&members[i], get_global_sam_sid(),
1384                                     rids[i]);
1385                 }
1386                 TALLOC_FREE(rids);
1387         } else {
1388                 d_fprintf(stderr,_("Can only list local group members so far.\n"
1389                           "%s is a %s\n"), argv[0], sid_type_lookup(grouptype));
1390                 return -1;
1391         }
1392
1393         d_printf(_("%s\\%s has %u members\n"), groupdomain, groupname,
1394                  (unsigned int)num_members);
1395         for (i=0; i<num_members; i++) {
1396                 const char *dom, *name;
1397                 if (lookup_sid(talloc_tos(), &members[i], &dom, &name, NULL)) {
1398                         d_printf(" %s\\%s\n", dom, name);
1399                 } else {
1400                         d_printf(" %s\n", sid_string_tos(&members[i]));
1401                 }
1402         }
1403
1404                 TALLOC_FREE(members);
1405
1406         return 0;
1407 }
1408
1409 /*
1410  * Do the listing
1411  */
1412 static int net_sam_do_list(struct net_context *c, int argc, const char **argv,
1413                            struct pdb_search *search, const char *what)
1414 {
1415         bool verbose = (argc == 1);
1416
1417         if ((argc > 1) || c->display_usage ||
1418             ((argc == 1) && !strequal(argv[0], "verbose"))) {
1419                 d_fprintf(stderr, "%s\n", _("Usage:"));
1420                 d_fprintf(stderr, _("net sam list %s [verbose]\n"), what);
1421                 return -1;
1422         }
1423
1424         if (search == NULL) {
1425                 d_fprintf(stderr, _("Could not start search\n"));
1426                 return -1;
1427         }
1428
1429         while (true) {
1430                 struct samr_displayentry entry;
1431                 if (!search->next_entry(search, &entry)) {
1432                         break;
1433                 }
1434                 if (verbose) {
1435                         d_printf("%s:%d:%s\n",
1436                                  entry.account_name,
1437                                  entry.rid,
1438                                  entry.description);
1439                 } else {
1440                         d_printf("%s\n", entry.account_name);
1441                 }
1442         }
1443
1444         TALLOC_FREE(search);
1445         return 0;
1446 }
1447
1448 static int net_sam_list_users(struct net_context *c, int argc,
1449                               const char **argv)
1450 {
1451         return net_sam_do_list(c, argc, argv,
1452                                pdb_search_users(talloc_tos(), ACB_NORMAL),
1453                                "users");
1454 }
1455
1456 static int net_sam_list_groups(struct net_context *c, int argc,
1457                                const char **argv)
1458 {
1459         return net_sam_do_list(c, argc, argv, pdb_search_groups(talloc_tos()),
1460                                "groups");
1461 }
1462
1463 static int net_sam_list_localgroups(struct net_context *c, int argc,
1464                                     const char **argv)
1465 {
1466         return net_sam_do_list(c, argc, argv,
1467                                pdb_search_aliases(talloc_tos(),
1468                                                   get_global_sam_sid()),
1469                                "localgroups");
1470 }
1471
1472 static int net_sam_list_builtin(struct net_context *c, int argc,
1473                                 const char **argv)
1474 {
1475         return net_sam_do_list(c, argc, argv,
1476                                pdb_search_aliases(talloc_tos(),
1477                                                   &global_sid_Builtin),
1478                                "builtin");
1479 }
1480
1481 static int net_sam_list_workstations(struct net_context *c, int argc,
1482                                      const char **argv)
1483 {
1484         return net_sam_do_list(c, argc, argv,
1485                                pdb_search_users(talloc_tos(), ACB_WSTRUST),
1486                                "workstations");
1487 }
1488
1489 /*
1490  * List stuff
1491  */
1492
1493 static int net_sam_list(struct net_context *c, int argc, const char **argv)
1494 {
1495         struct functable func[] = {
1496                 {
1497                         "users",
1498                         net_sam_list_users,
1499                         NET_TRANSPORT_LOCAL,
1500                         N_("List SAM users"),
1501                         N_("net sam list users\n"
1502                            "    List SAM users")
1503                 },
1504                 {
1505                         "groups",
1506                         net_sam_list_groups,
1507                         NET_TRANSPORT_LOCAL,
1508                         N_("List SAM groups"),
1509                         N_("net sam list groups\n"
1510                            "    List SAM groups")
1511                 },
1512                 {
1513                         "localgroups",
1514                         net_sam_list_localgroups,
1515                         NET_TRANSPORT_LOCAL,
1516                         N_("List SAM local groups"),
1517                         N_("net sam list localgroups\n"
1518                            "    List SAM local groups")
1519                 },
1520                 {
1521                         "builtin",
1522                         net_sam_list_builtin,
1523                         NET_TRANSPORT_LOCAL,
1524                         N_("List builtin groups"),
1525                         N_("net sam list builtin\n"
1526                            "    List builtin groups")
1527                 },
1528                 {
1529                         "workstations",
1530                         net_sam_list_workstations,
1531                         NET_TRANSPORT_LOCAL,
1532                         N_("List domain member workstations"),
1533                         N_("net sam list workstations\n"
1534                            "    List domain member workstations")
1535                 },
1536                 {NULL, NULL, 0, NULL, NULL}
1537         };
1538
1539         return net_run_function(c, argc, argv, "net sam list", func);
1540 }
1541
1542 /*
1543  * Show details of SAM entries
1544  */
1545
1546 static int net_sam_show(struct net_context *c, int argc, const char **argv)
1547 {
1548         struct dom_sid sid;
1549         enum lsa_SidType type;
1550         const char *dom, *name;
1551
1552         if (argc != 1 || c->display_usage) {
1553                 d_fprintf(stderr, "%s\n%s",
1554                           _("Usage:"),
1555                           _("net sam show <name>\n"));
1556                 return -1;
1557         }
1558
1559         if (!lookup_name(talloc_tos(), argv[0], LOOKUP_NAME_LOCAL,
1560                          &dom, &name, &sid, &type)) {
1561                 d_fprintf(stderr, _("Could not find name %s\n"), argv[0]);
1562                 return -1;
1563         }
1564
1565         d_printf(_("%s\\%s is a %s with SID %s\n"), dom, name,
1566                  sid_type_lookup(type), sid_string_tos(&sid));
1567
1568         return 0;
1569 }
1570
1571 #ifdef HAVE_LDAP
1572
1573 /*
1574  * Init an LDAP tree with default users and Groups
1575  * if ldapsam:editposix is enabled
1576  */
1577
1578 static int net_sam_provision(struct net_context *c, int argc, const char **argv)
1579 {
1580         TALLOC_CTX *tc;
1581         char *ldap_bk;
1582         char *ldap_uri = NULL;
1583         char *p;
1584         struct smbldap_state *ls;
1585         GROUP_MAP gmap;
1586         struct dom_sid gsid;
1587         gid_t domusers_gid = -1;
1588         gid_t domadmins_gid = -1;
1589         struct samu *samuser;
1590         struct passwd *pwd;
1591         bool is_ipa = false;
1592
1593         if (c->display_usage) {
1594                 d_printf(  "%s\n"
1595                            "net sam provision\n"
1596                             "    %s\n",
1597                           _("Usage:"),
1598                           _("Init an LDAP tree with default users/groups"));
1599                 return 0;
1600         }
1601
1602         tc = talloc_new(NULL);
1603         if (!tc) {
1604                 d_fprintf(stderr, _("Out of Memory!\n"));
1605                 return -1;
1606         }
1607
1608         if ((ldap_bk = talloc_strdup(tc, lp_passdb_backend())) == NULL) {
1609                 d_fprintf(stderr, _("talloc failed\n"));
1610                 talloc_free(tc);
1611                 return -1;
1612         }
1613         p = strchr(ldap_bk, ':');
1614         if (p) {
1615                 *p = 0;
1616                 ldap_uri = talloc_strdup(tc, p+1);
1617                 trim_char(ldap_uri, ' ', ' ');
1618         }
1619
1620         trim_char(ldap_bk, ' ', ' ');
1621
1622         if (strcmp(ldap_bk, "IPA_ldapsam") == 0 ) {
1623                 is_ipa = true;
1624         }
1625
1626         if (strcmp(ldap_bk, "ldapsam") != 0 && !is_ipa ) {
1627                 d_fprintf(stderr,
1628                           _("Provisioning works only with ldapsam backend\n"));
1629                 goto failed;
1630         }
1631
1632         if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
1633             !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
1634
1635                 d_fprintf(stderr, _("Provisioning works only if ldapsam:trusted"
1636                                     " and ldapsam:editposix are enabled.\n"));
1637                 goto failed;
1638         }
1639
1640         if (!is_ipa && !winbind_ping()) {
1641                 d_fprintf(stderr, _("winbind seems not to run. Provisioning "
1642                             "LDAP only works when winbind runs.\n"));
1643                 goto failed;
1644         }
1645
1646         if (!NT_STATUS_IS_OK(smbldap_init(tc, NULL, ldap_uri, &ls))) {
1647                 d_fprintf(stderr, _("Unable to connect to the LDAP server.\n"));
1648                 goto failed;
1649         }
1650
1651         d_printf(_("Checking for Domain Users group.\n"));
1652
1653         sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_USERS);
1654
1655         if (!pdb_getgrsid(&gmap, gsid)) {
1656                 LDAPMod **mods = NULL;
1657                 char *dn;
1658                 char *uname;
1659                 char *wname;
1660                 char *gidstr;
1661                 char *gtype;
1662                 int rc;
1663
1664                 d_printf(_("Adding the Domain Users group.\n"));
1665
1666                 /* lets allocate a new groupid for this group */
1667                 if (is_ipa) {
1668                         domusers_gid = 999;
1669                 } else {
1670                         if (!winbind_allocate_gid(&domusers_gid)) {
1671                                 d_fprintf(stderr, _("Unable to allocate a new gid to "
1672                                                     "create Domain Users group!\n"));
1673                                 goto domu_done;
1674                         }
1675                 }
1676
1677                 uname = talloc_strdup(tc, "domusers");
1678                 wname = talloc_strdup(tc, "Domain Users");
1679                 dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
1680                 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domusers_gid);
1681                 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1682
1683                 if (!uname || !wname || !dn || !gidstr || !gtype) {
1684                         d_fprintf(stderr, "Out of Memory!\n");
1685                         goto failed;
1686                 }
1687
1688                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1689                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1690                 if (is_ipa) {
1691                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "groupofnames");
1692                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "nestedgroup");
1693                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "ipausergroup");
1694                 }
1695                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1696                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1697                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1698                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1699                                 sid_string_talloc(tc, &gsid));
1700                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1701
1702                 talloc_autofree_ldapmod(tc, mods);
1703
1704                 rc = smbldap_add(ls, dn, mods);
1705
1706                 if (rc != LDAP_SUCCESS) {
1707                         d_fprintf(stderr, _("Failed to add Domain Users group "
1708                                             "to ldap directory\n"));
1709                 }
1710
1711                 if (is_ipa) {
1712                         if (!pdb_getgrsid(&gmap, gsid)) {
1713                                 d_fprintf(stderr, _("Failed to read just "
1714                                                     "created domain group.\n"));
1715                                 goto failed;
1716                         } else {
1717                                 domusers_gid = gmap.gid;
1718                         }
1719                 }
1720         } else {
1721                 domusers_gid = gmap.gid;
1722                 d_printf(_("found!\n"));
1723         }
1724
1725 domu_done:
1726
1727         d_printf(_("Checking for Domain Admins group.\n"));
1728
1729         sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_ADMINS);
1730
1731         if (!pdb_getgrsid(&gmap, gsid)) {
1732                 LDAPMod **mods = NULL;
1733                 char *dn;
1734                 char *uname;
1735                 char *wname;
1736                 char *gidstr;
1737                 char *gtype;
1738                 int rc;
1739
1740                 d_printf(_("Adding the Domain Admins group.\n"));
1741
1742                 /* lets allocate a new groupid for this group */
1743                 if (is_ipa) {
1744                         domadmins_gid = 999;
1745                 } else {
1746                         if (!winbind_allocate_gid(&domadmins_gid)) {
1747                                 d_fprintf(stderr, _("Unable to allocate a new gid to "
1748                                                     "create Domain Admins group!\n"));
1749                                 goto doma_done;
1750                         }
1751                 }
1752
1753                 uname = talloc_strdup(tc, "domadmins");
1754                 wname = talloc_strdup(tc, "Domain Admins");
1755                 dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
1756                 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1757                 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
1758
1759                 if (!uname || !wname || !dn || !gidstr || !gtype) {
1760                         d_fprintf(stderr, _("Out of Memory!\n"));
1761                         goto failed;
1762                 }
1763
1764                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
1765                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
1766                 if (is_ipa) {
1767                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "groupofnames");
1768                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "nestedgroup");
1769                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "ipausergroup");
1770                 }
1771                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
1772                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
1773                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1774                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
1775                                 sid_string_talloc(tc, &gsid));
1776                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
1777
1778                 talloc_autofree_ldapmod(tc, mods);
1779
1780                 rc = smbldap_add(ls, dn, mods);
1781
1782                 if (rc != LDAP_SUCCESS) {
1783                         d_fprintf(stderr, _("Failed to add Domain Admins group "
1784                                             "to ldap directory\n"));
1785                 }
1786
1787                 if (is_ipa) {
1788                         if (!pdb_getgrsid(&gmap, gsid)) {
1789                                 d_fprintf(stderr, _("Failed to read just "
1790                                                     "created domain group.\n"));
1791                                 goto failed;
1792                         } else {
1793                                 domadmins_gid = gmap.gid;
1794                         }
1795                 }
1796         } else {
1797                 domadmins_gid = gmap.gid;
1798                 d_printf(_("found!\n"));
1799         }
1800
1801 doma_done:
1802
1803         d_printf(_("Check for Administrator account.\n"));
1804
1805         samuser = samu_new(tc);
1806         if (!samuser) {
1807                 d_fprintf(stderr, _("Out of Memory!\n"));
1808                 goto failed;
1809         }
1810
1811         if (!pdb_getsampwnam(samuser, "Administrator")) {
1812                 LDAPMod **mods = NULL;
1813                 struct dom_sid sid;
1814                 char *dn;
1815                 char *name;
1816                 char *uidstr;
1817                 char *gidstr;
1818                 char *shell;
1819                 char *dir;
1820                 char *princ;
1821                 uid_t uid;
1822                 int rc;
1823
1824                 d_printf(_("Adding the Administrator user.\n"));
1825
1826                 if (domadmins_gid == -1) {
1827                         d_fprintf(stderr,
1828                                   _("Can't create Administrator user, Domain "
1829                                     "Admins group not available!\n"));
1830                         goto done;
1831                 }
1832
1833                 if (is_ipa) {
1834                         uid = 999;
1835                 } else {
1836                         if (!winbind_allocate_uid(&uid)) {
1837                                 d_fprintf(stderr,
1838                                           _("Unable to allocate a new uid to create "
1839                                             "the Administrator user!\n"));
1840                                 goto done;
1841                         }
1842                 }
1843
1844                 name = talloc_strdup(tc, "Administrator");
1845                 dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
1846                 uidstr = talloc_asprintf(tc, "%u", (unsigned int)uid);
1847                 gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
1848                 dir = talloc_sub_specified(tc, lp_template_homedir(),
1849                                                 "Administrator",
1850                                                 get_global_sam_name(),
1851                                                 uid, domadmins_gid);
1852                 shell = talloc_sub_specified(tc, lp_template_shell(),
1853                                                 "Administrator",
1854                                                 get_global_sam_name(),
1855                                                 uid, domadmins_gid);
1856
1857                 if (!name || !dn || !uidstr || !gidstr || !dir || !shell) {
1858                         d_fprintf(stderr, _("Out of Memory!\n"));
1859                         goto failed;
1860                 }
1861
1862                 sid_compose(&sid, get_global_sam_sid(), DOMAIN_RID_ADMINISTRATOR);
1863
1864                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1865                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1866                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1867                 if (is_ipa) {
1868                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "person");
1869                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "organizationalperson");
1870                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "inetorgperson");
1871                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "inetuser");
1872                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "krbprincipalaux");
1873                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "krbticketpolicyaux");
1874                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "sn", name);
1875                         princ = talloc_asprintf(tc, "%s@%s", name, lp_realm());
1876                         if (!princ) {
1877                                 d_fprintf(stderr, _("Out of Memory!\n"));
1878                                 goto failed;
1879                         }
1880                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "krbPrincipalName", princ);
1881                 }
1882                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", name);
1883                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", name);
1884                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
1885                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1886                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1887                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", dir);
1888                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", shell);
1889                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
1890                                 sid_string_talloc(tc, &sid));
1891                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
1892                                 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
1893                                 NEW_PW_FORMAT_SPACE_PADDED_LEN));
1894
1895                 talloc_autofree_ldapmod(tc, mods);
1896
1897                 rc = smbldap_add(ls, dn, mods);
1898
1899                 if (rc != LDAP_SUCCESS) {
1900                         d_fprintf(stderr, _("Failed to add Administrator user "
1901                                             "to ldap directory\n"));
1902                 }
1903
1904                 if (is_ipa) {
1905                         if (!pdb_getsampwnam(samuser, "Administrator")) {
1906                                 d_fprintf(stderr, _("Failed to read just "
1907                                                     "created user.\n"));
1908                                 goto failed;
1909                         }
1910                 }
1911         } else {
1912                 d_printf(_("found!\n"));
1913         }
1914
1915         d_printf(_("Checking for Guest user.\n"));
1916
1917         samuser = samu_new(tc);
1918         if (!samuser) {
1919                 d_fprintf(stderr, _("Out of Memory!\n"));
1920                 goto failed;
1921         }
1922
1923         if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
1924                 LDAPMod **mods = NULL;
1925                 struct dom_sid sid;
1926                 char *dn;
1927                 char *uidstr;
1928                 char *gidstr;
1929                 int rc;
1930
1931                 d_printf(_("Adding the Guest user.\n"));
1932
1933                 sid_compose(&sid, get_global_sam_sid(), DOMAIN_RID_GUEST);
1934
1935                 pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
1936
1937                 if (!pwd) {
1938                         if (domusers_gid == -1) {
1939                                 d_fprintf(stderr,
1940                                           _("Can't create Guest user, Domain "
1941                                             "Users group not available!\n"));
1942                                 goto done;
1943                         }
1944                         if ((pwd = talloc(tc, struct passwd)) == NULL) {
1945                                 d_fprintf(stderr, _("talloc failed\n"));
1946                                 goto done;
1947                         }
1948                         pwd->pw_name = talloc_strdup(pwd, lp_guestaccount());
1949
1950                         if (is_ipa) {
1951                                 pwd->pw_uid = 999;
1952                         } else {
1953                                 if (!winbind_allocate_uid(&(pwd->pw_uid))) {
1954                                         d_fprintf(stderr,
1955                                                   _("Unable to allocate a new uid to "
1956                                                     "create the Guest user!\n"));
1957                                         goto done;
1958                                 }
1959                         }
1960                         pwd->pw_gid = domusers_gid;
1961                         pwd->pw_dir = talloc_strdup(tc, "/");
1962                         pwd->pw_shell = talloc_strdup(tc, "/bin/false");
1963                         if (!pwd->pw_dir || !pwd->pw_shell) {
1964                                 d_fprintf(stderr, _("Out of Memory!\n"));
1965                                 goto failed;
1966                         }
1967                 }
1968
1969                 dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
1970                 uidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_uid);
1971                 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
1972                 if (!dn || !uidstr || !gidstr) {
1973                         d_fprintf(stderr, _("Out of Memory!\n"));
1974                         goto failed;
1975                 }
1976
1977                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_ACCOUNT);
1978                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXACCOUNT);
1979                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SAMBASAMACCOUNT);
1980                 if (is_ipa) {
1981                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "person");
1982                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "organizationalperson");
1983                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "inetorgperson");
1984                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "inetuser");
1985                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "krbprincipalaux");
1986                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "krbticketpolicyaux");
1987                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "sn", pwd->pw_name);
1988                 }
1989                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uid", pwd->pw_name);
1990                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", pwd->pw_name);
1991                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", pwd->pw_name);
1992                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "uidNumber", uidstr);
1993                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
1994                 if ((pwd->pw_dir != NULL) && (pwd->pw_dir[0] != '\0')) {
1995                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "homeDirectory", pwd->pw_dir);
1996                 }
1997                 if ((pwd->pw_shell != NULL) && (pwd->pw_shell[0] != '\0')) {
1998                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "loginShell", pwd->pw_shell);
1999                 }
2000                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSID",
2001                                 sid_string_talloc(tc, &sid));
2002                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaAcctFlags",
2003                                 pdb_encode_acct_ctrl(ACB_NORMAL|ACB_DISABLED,
2004                                 NEW_PW_FORMAT_SPACE_PADDED_LEN));
2005
2006                 talloc_autofree_ldapmod(tc, mods);
2007
2008                 rc = smbldap_add(ls, dn, mods);
2009
2010                 if (rc != LDAP_SUCCESS) {
2011                         d_fprintf(stderr, _("Failed to add Guest user to "
2012                                             "ldap directory\n"));
2013                 }
2014
2015                 if (is_ipa) {
2016                         if (!pdb_getsampwnam(samuser, lp_guestaccount())) {
2017                                 d_fprintf(stderr, _("Failed to read just "
2018                                                     "created user.\n"));
2019                                 goto failed;
2020                         }
2021                 }
2022         } else {
2023                 d_printf(_("found!\n"));
2024         }
2025
2026         d_printf(_("Checking Guest's group.\n"));
2027
2028         pwd = Get_Pwnam_alloc(tc, lp_guestaccount());
2029         if (!pwd) {
2030                 d_fprintf(stderr,
2031                           _("Failed to find just created Guest account!\n"
2032                             "   Is nss properly configured?!\n"));
2033                 goto failed;
2034         }
2035
2036         if (pwd->pw_gid == domusers_gid) {
2037                 d_printf(_("found!\n"));
2038                 goto done;
2039         }
2040
2041         if (!pdb_getgrgid(&gmap, pwd->pw_gid)) {
2042                 LDAPMod **mods = NULL;
2043                 char *dn;
2044                 char *uname;
2045                 char *wname;
2046                 char *gidstr;
2047                 char *gtype;
2048                 int rc;
2049
2050                 d_printf(_("Adding the Domain Guests group.\n"));
2051
2052                 uname = talloc_strdup(tc, "domguests");
2053                 wname = talloc_strdup(tc, "Domain Guests");
2054                 dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
2055                 gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
2056                 gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
2057
2058                 if (!uname || !wname || !dn || !gidstr || !gtype) {
2059                         d_fprintf(stderr, _("Out of Memory!\n"));
2060                         goto failed;
2061                 }
2062
2063                 sid_compose(&gsid, get_global_sam_sid(), DOMAIN_RID_GUESTS);
2064
2065                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_POSIXGROUP);
2066                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
2067                 if (is_ipa) {
2068                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "groupofnames");
2069                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "nestedgroup");
2070                         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", "ipausergroup");
2071                 }
2072                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "cn", uname);
2073                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", wname);
2074                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
2075                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid",
2076                                 sid_string_talloc(tc, &gsid));
2077                 smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", gtype);
2078
2079                 talloc_autofree_ldapmod(tc, mods);
2080
2081                 rc = smbldap_add(ls, dn, mods);
2082
2083                 if (rc != LDAP_SUCCESS) {
2084                         d_fprintf(stderr,
2085                                   _("Failed to add Domain Guests group to ldap "
2086                                     "directory\n"));
2087                 }
2088         } else {
2089                 d_printf(_("found!\n"));
2090         }
2091
2092
2093 done:
2094         talloc_free(tc);
2095         return 0;
2096
2097 failed:
2098         talloc_free(tc);
2099         return -1;
2100 }
2101
2102 #endif
2103
2104 /***********************************************************
2105  migrated functionality from smbgroupedit
2106  **********************************************************/
2107 int net_sam(struct net_context *c, int argc, const char **argv)
2108 {
2109         struct functable func[] = {
2110                 {
2111                         "createbuiltingroup",
2112                         net_sam_createbuiltingroup,
2113                         NET_TRANSPORT_LOCAL,
2114                         N_("Create a new BUILTIN group"),
2115                         N_("net sam createbuiltingroup\n"
2116                            "    Create a new BUILTIN group")
2117                 },
2118                 {
2119                         "createlocalgroup",
2120                         net_sam_createlocalgroup,
2121                         NET_TRANSPORT_LOCAL,
2122                         N_("Create a new local group"),
2123                         N_("net sam createlocalgroup\n"
2124                            "    Create a new local group")
2125                 },
2126                 {
2127                         "createdomaingroup",
2128                         net_sam_createdomaingroup,
2129                         NET_TRANSPORT_LOCAL,
2130                         N_("Create a new group"),
2131                         N_("net sam createdomaingroup\n"
2132                            "    Create a new group")
2133                 },
2134                 {
2135                         "deletelocalgroup",
2136                         net_sam_deletelocalgroup,
2137                         NET_TRANSPORT_LOCAL,
2138                         N_("Delete an existing local group"),
2139                         N_("net sam deletelocalgroup\n"
2140                            "    Delete an existing local group")
2141                 },
2142                 {
2143                         "deletedomaingroup",
2144                         net_sam_deletedomaingroup,
2145                         NET_TRANSPORT_LOCAL,
2146                         N_("Delete a domain group"),
2147                         N_("net sam deletedomaingroup\n"
2148                            "    Delete a group")
2149                 },
2150                 {
2151                         "mapunixgroup",
2152                         net_sam_mapunixgroup,
2153                         NET_TRANSPORT_LOCAL,
2154                         N_("Map a unix group to a domain group"),
2155                         N_("net sam mapunixgroup\n"
2156                            "    Map a unix group to a domain group")
2157                 },
2158                 {
2159                         "unmapunixgroup",
2160                         net_sam_unmapunixgroup,
2161                         NET_TRANSPORT_LOCAL,
2162                         N_("Remove a group mapping of an unix group to a "
2163                            "domain group"),
2164                         N_("net sam unmapunixgroup\n"
2165                            "    Remove a group mapping of an unix group to a "
2166                            "domain group")
2167                 },
2168                 {
2169                         "addmem",
2170                         net_sam_addmem,
2171                         NET_TRANSPORT_LOCAL,
2172                         N_("Add a member to a group"),
2173                         N_("net sam addmem\n"
2174                         "    Add a member to a group")
2175                 },
2176                 {
2177                         "delmem",
2178                         net_sam_delmem,
2179                         NET_TRANSPORT_LOCAL,
2180                         N_("Delete a member from a group"),
2181                         N_("net sam delmem\n"
2182                            "    Delete a member from a group")
2183                 },
2184                 {
2185                         "listmem",
2186                         net_sam_listmem,
2187                         NET_TRANSPORT_LOCAL,
2188                         N_("List group members"),
2189                         N_("net sam listmem\n"
2190                            "    List group members")
2191                 },
2192                 {
2193                         "list",
2194                         net_sam_list,
2195                         NET_TRANSPORT_LOCAL,
2196                         N_("List users, groups and local groups"),
2197                         N_("net sam list\n"
2198                            "    List users, groups and local groups")
2199                 },
2200                 {
2201                         "show",
2202                         net_sam_show,
2203                         NET_TRANSPORT_LOCAL,
2204                         N_("Show details of a SAM entry"),
2205                         N_("net sam show\n"
2206                            "    Show details of a SAM entry")
2207                 },
2208                 {
2209                         "set",
2210                         net_sam_set,
2211                         NET_TRANSPORT_LOCAL,
2212                         N_("Set details of a SAM account"),
2213                         N_("net sam set\n"
2214                            "    Set details of a SAM account")
2215                 },
2216                 {
2217                         "policy",
2218                         net_sam_policy,
2219                         NET_TRANSPORT_LOCAL,
2220                         N_("Set account policies"),
2221                         N_("net sam policy\n"
2222                            "    Set account policies")
2223                 },
2224                 {
2225                         "rights",
2226                         net_sam_rights,
2227                         NET_TRANSPORT_LOCAL,
2228                         N_("Manipulate user privileges"),
2229                         N_("net sam rights\n"
2230                            "    Manipulate user privileges")
2231                 },
2232 #ifdef HAVE_LDAP
2233                 {
2234                         "provision",
2235                         net_sam_provision,
2236                         NET_TRANSPORT_LOCAL,
2237                         N_("Provision a clean user database"),
2238                         N_("net sam privison\n"
2239                            "    Provision a clear user database")
2240                 },
2241 #endif
2242                 {NULL, NULL, 0, NULL, NULL}
2243         };
2244
2245         if (getuid() != 0) {
2246                 d_fprintf(stderr, _("You are not root, most things won't "
2247                           "work\n"));
2248         }
2249
2250         return net_run_function(c, argc, argv, "net sam", func);
2251 }
2252