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