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