r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[nivanova/samba-autobuild/.git] / source3 / utils / net_groupmap.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-2000,
5  *  Copyright (C) Jean François Micouleau      1998-2001.
6  *  Copyright (C) Gerald Carter                2003,
7  *  Copyright (C) Volker Lendecke              2004
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "includes.h"
25 #include "utils/net.h"
26
27
28 /*********************************************************
29  utility function to parse an integer parameter from 
30  "parameter = value"
31 **********************************************************/
32 static uint32 get_int_param( const char* param )
33 {
34         char *p;
35         
36         p = strchr( param, '=' );
37         if ( !p )
38                 return 0;
39                 
40         return atoi(p+1);
41 }
42
43 /*********************************************************
44  utility function to parse an integer parameter from 
45  "parameter = value"
46 **********************************************************/
47 static char* get_string_param( const char* param )
48 {
49         char *p;
50         
51         p = strchr( param, '=' );
52         if ( !p )
53                 return NULL;
54                 
55         return (p+1);
56 }
57
58 /*********************************************************
59  Figure out if the input was an NT group or a SID string.
60  Return the SID.
61 **********************************************************/
62 static BOOL get_sid_from_input(DOM_SID *sid, char *input)
63 {
64         GROUP_MAP map;
65
66         if (StrnCaseCmp( input, "S-", 2)) {
67                 /* Perhaps its the NT group name? */
68                 if (!pdb_getgrnam(&map, input)) {
69                         printf("NT Group %s doesn't exist in mapping DB\n", input);
70                         return False;
71                 } else {
72                         *sid = map.sid;
73                 }
74         } else {
75                 if (!string_to_sid(sid, input)) {
76                         printf("converting sid %s from a string failed!\n", input);
77                         return False;
78                 }
79         }
80         return True;
81 }
82
83 /*********************************************************
84  Dump a GROUP_MAP entry to stdout (long or short listing)
85 **********************************************************/
86
87 static void print_map_entry ( GROUP_MAP map, BOOL long_list )
88 {
89         if (!long_list)
90                 d_printf("%s (%s) -> %s\n", map.nt_name,
91                          sid_string_static(&map.sid), gidtoname(map.gid));
92         else {
93                 d_printf("%s\n", map.nt_name);
94                 d_printf("\tSID       : %s\n", sid_string_static(&map.sid));
95                 d_printf("\tUnix gid  : %d\n", map.gid);
96                 d_printf("\tUnix group: %s\n", gidtoname(map.gid));
97                 d_printf("\tGroup type: %s\n",
98                          sid_type_lookup(map.sid_name_use));
99                 d_printf("\tComment   : %s\n", map.comment);
100         }
101
102 }
103 /*********************************************************
104  List the groups.
105 **********************************************************/
106 static int net_groupmap_list(int argc, const char **argv)
107 {
108         size_t entries;
109         BOOL long_list = False;
110         size_t i;
111         fstring ntgroup = "";
112         fstring sid_string = "";
113
114         if (opt_verbose || opt_long_list_entries)
115                 long_list = True;
116         
117         /* get the options */
118         for ( i=0; i<argc; i++ ) {
119                 if ( !StrCaseCmp(argv[i], "verbose")) {
120                         long_list = True;
121                 }
122                 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
123                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
124                         if ( !ntgroup[0] ) {
125                                 d_fprintf(stderr, "must supply a name\n");
126                                 return -1;
127                         }               
128                 }
129                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
130                         fstrcpy( sid_string, get_string_param( argv[i] ) );
131                         if ( !sid_string[0] ) {
132                                 d_fprintf(stderr, "must supply a SID\n");
133                                 return -1;
134                         }               
135                 }
136                 else {
137                         d_fprintf(stderr, "Bad option: %s\n", argv[i]);
138                         return -1;
139                 }
140         }
141
142         /* list a single group is given a name */
143         if ( ntgroup[0] || sid_string[0] ) {
144                 DOM_SID sid;
145                 GROUP_MAP map;
146                 
147                 if ( sid_string[0] )
148                         fstrcpy( ntgroup, sid_string);
149                         
150                 if (!get_sid_from_input(&sid, ntgroup)) {
151                         return -1;
152                 }
153
154                 /* Get the current mapping from the database */
155                 if(!pdb_getgrsid(&map, sid)) {
156                         d_fprintf(stderr, "Failure to local group SID in the database\n");
157                         return -1;
158                 }
159         
160                 print_map_entry( map, long_list );
161         }
162         else {
163                 GROUP_MAP *map=NULL;
164                 /* enumerate all group mappings */
165                 if (!pdb_enum_group_mapping(NULL, SID_NAME_UNKNOWN, &map, &entries, ENUM_ALL_MAPPED))
166                         return -1;
167         
168                 for (i=0; i<entries; i++) {
169                         print_map_entry( map[i], long_list );
170                 }
171
172                 SAFE_FREE(map);
173         }
174
175         return 0;
176 }
177
178 /*********************************************************
179  Add a new group mapping entry
180 **********************************************************/
181
182 static int net_groupmap_add(int argc, const char **argv)
183 {
184         DOM_SID sid;
185         fstring ntgroup = "";
186         fstring unixgrp = "";
187         fstring string_sid = "";
188         fstring type = "";
189         fstring ntcomment = "";
190         enum lsa_SidType sid_type = SID_NAME_DOM_GRP;
191         uint32 rid = 0; 
192         gid_t gid;
193         int i;
194         GROUP_MAP map;
195         
196         const char *name_type;
197
198         ZERO_STRUCT(map);
199
200         /* Default is domain group. */
201         map.sid_name_use = SID_NAME_DOM_GRP;
202         name_type = "domain group";
203
204         /* get the options */
205         for ( i=0; i<argc; i++ ) {
206                 if ( !StrnCaseCmp(argv[i], "rid", strlen("rid")) ) {
207                         rid = get_int_param(argv[i]);
208                         if ( rid < DOMAIN_GROUP_RID_ADMINS ) {
209                                 d_fprintf(stderr, "RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
210                                 return -1;
211                         }
212                 }
213                 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
214                         fstrcpy( unixgrp, get_string_param( argv[i] ) );
215                         if ( !unixgrp[0] ) {
216                                 d_fprintf(stderr, "must supply a name\n");
217                                 return -1;
218                         }               
219                 }
220                 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
221                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
222                         if ( !ntgroup[0] ) {
223                                 d_fprintf(stderr, "must supply a name\n");
224                                 return -1;
225                         }               
226                 }
227                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
228                         fstrcpy( string_sid, get_string_param( argv[i] ) );
229                         if ( !string_sid[0] ) {
230                                 d_fprintf(stderr, "must supply a SID\n");
231                                 return -1;
232                         }               
233                 }
234                 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
235                         fstrcpy( ntcomment, get_string_param( argv[i] ) );
236                         if ( !ntcomment[0] ) {
237                                 d_fprintf(stderr, "must supply a comment string\n");
238                                 return -1;
239                         }                               
240                 }
241                 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) )  {
242                         fstrcpy( type, get_string_param( argv[i] ) );
243                         switch ( type[0] ) {
244                                 case 'b':
245                                 case 'B':
246                                         sid_type = SID_NAME_WKN_GRP;
247                                         name_type = "wellknown group";
248                                         break;
249                                 case 'd':
250                                 case 'D':
251                                         sid_type = SID_NAME_DOM_GRP;
252                                         name_type = "domain group";
253                                         break;
254                                 case 'l':
255                                 case 'L':
256                                         sid_type = SID_NAME_ALIAS;
257                                         name_type = "alias (local) group";
258                                         break;
259                                 default:
260                                         d_fprintf(stderr, "unknown group type %s\n", type);
261                                         return -1;
262                         }
263                 }
264                 else {
265                         d_fprintf(stderr, "Bad option: %s\n", argv[i]);
266                         return -1;
267                 }
268         }
269
270         if ( !unixgrp[0] ) {
271                 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
272                 return -1;
273         }
274         
275         if ( (gid = nametogid(unixgrp)) == (gid_t)-1 ) {
276                 d_fprintf(stderr, "Can't lookup UNIX group %s\n", unixgrp);
277                 return -1;
278         }
279
280         {
281                 if (pdb_getgrgid(&map, gid)) {
282                         d_printf("Unix group %s already mapped to SID %s\n",
283                                  unixgrp, sid_string_static(&map.sid));
284                         return -1;
285                 }
286         }
287         
288         if ( (rid == 0) && (string_sid[0] == '\0') ) {
289                 d_printf("No rid or sid specified, choosing a RID\n");
290                 if (pdb_rid_algorithm()) {
291                         rid = algorithmic_pdb_gid_to_group_rid(gid);
292                 } else {
293                         if (!pdb_new_rid(&rid)) {
294                                 d_printf("Could not get new RID\n");
295                         }
296                 }
297                 d_printf("Got RID %d\n", rid);
298         }
299
300         /* append the rid to our own domain/machine SID if we don't have a full SID */
301         if ( !string_sid[0] ) {
302                 sid_copy(&sid, get_global_sam_sid());
303                 sid_append_rid(&sid, rid);
304                 sid_to_string(string_sid, &sid);
305         }
306
307         if (!ntcomment[0]) {
308                 switch (sid_type) {
309                 case SID_NAME_WKN_GRP:
310                         fstrcpy(ntcomment, "Wellknown Unix group");
311                         break;
312                 case SID_NAME_DOM_GRP:
313                         fstrcpy(ntcomment, "Domain Unix group");
314                         break;
315                 case SID_NAME_ALIAS:
316                         fstrcpy(ntcomment, "Local Unix group");
317                         break;
318                 default:
319                         fstrcpy(ntcomment, "Unix group");
320                         break;
321                 }
322         }
323                 
324         if (!ntgroup[0] )
325                 fstrcpy( ntgroup, unixgrp );
326                 
327         
328         if (!NT_STATUS_IS_OK(add_initial_entry(gid, string_sid, sid_type, ntgroup, ntcomment))) {
329                 d_fprintf(stderr, "adding entry for group %s failed!\n", ntgroup);
330                 return -1;
331         }
332
333         d_printf("Successfully added group %s to the mapping db as a %s\n",
334                  ntgroup, name_type);
335         return 0;
336 }
337
338 static int net_groupmap_modify(int argc, const char **argv)
339 {
340         DOM_SID sid;
341         GROUP_MAP map;
342         fstring ntcomment = "";
343         fstring type = "";
344         fstring ntgroup = "";
345         fstring unixgrp = "";
346         fstring sid_string = "";
347         enum lsa_SidType sid_type = SID_NAME_UNKNOWN;
348         int i;
349         gid_t gid;
350
351         /* get the options */
352         for ( i=0; i<argc; i++ ) {
353                 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
354                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
355                         if ( !ntgroup[0] ) {
356                                 d_fprintf(stderr, "must supply a name\n");
357                                 return -1;
358                         }               
359                 }
360                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
361                         fstrcpy( sid_string, get_string_param( argv[i] ) );
362                         if ( !sid_string[0] ) {
363                                 d_fprintf(stderr, "must supply a name\n");
364                                 return -1;
365                         }               
366                 }
367                 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
368                         fstrcpy( ntcomment, get_string_param( argv[i] ) );
369                         if ( !ntcomment[0] ) {
370                                 d_fprintf(stderr, "must supply a comment string\n");
371                                 return -1;
372                         }                               
373                 }
374                 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
375                         fstrcpy( unixgrp, get_string_param( argv[i] ) );
376                         if ( !unixgrp[0] ) {
377                                 d_fprintf(stderr, "must supply a group name\n");
378                                 return -1;
379                         }                               
380                 }
381                 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) )  {
382                         fstrcpy( type, get_string_param( argv[i] ) );
383                         switch ( type[0] ) {
384                                 case 'd':
385                                 case 'D':
386                                         sid_type = SID_NAME_DOM_GRP;
387                                         break;
388                                 case 'l':
389                                 case 'L':
390                                         sid_type = SID_NAME_ALIAS;
391                                         break;
392                         }
393                 }
394                 else {
395                         d_fprintf(stderr, "Bad option: %s\n", argv[i]);
396                         return -1;
397                 }
398         }
399         
400         if ( !ntgroup[0] && !sid_string[0] ) {
401                 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
402                 return -1;
403         }
404
405         /* give preference to the SID; if both the ntgroup name and SID
406            are defined, use the SID and assume that the group name could be a 
407            new name */
408                 
409         if ( sid_string[0] ) {  
410                 if (!get_sid_from_input(&sid, sid_string)) {
411                         return -1;
412                 }
413         }
414         else {
415                 if (!get_sid_from_input(&sid, ntgroup)) {
416                         return -1;
417                 }
418         }       
419
420         /* Get the current mapping from the database */
421         if(!pdb_getgrsid(&map, sid)) {
422                 d_fprintf(stderr, "Failure to local group SID in the database\n");
423                 return -1;
424         }
425         
426         /*
427          * Allow changing of group type only between domain and local
428          * We disallow changing Builtin groups !!! (SID problem)
429          */ 
430         if (sid_type == SID_NAME_UNKNOWN) {
431                 d_fprintf(stderr, "Can't map to an unknown group type.\n");
432                 return -1;
433         }
434
435         if (map.sid_name_use == SID_NAME_WKN_GRP) {
436                 d_fprintf(stderr, "You can only change between domain and local groups.\n");
437                 return -1;
438         }
439
440         map.sid_name_use=sid_type;
441
442         /* Change comment if new one */
443         if ( ntcomment[0] )
444                 fstrcpy( map.comment, ntcomment );
445                 
446         if ( ntgroup[0] )
447                 fstrcpy( map.nt_name, ntgroup );
448                 
449         if ( unixgrp[0] ) {
450                 gid = nametogid( unixgrp );
451                 if ( gid == -1 ) {
452                         d_fprintf(stderr, "Unable to lookup UNIX group %s.  Make sure the group exists.\n",
453                                 unixgrp);
454                         return -1;
455                 }
456                 
457                 map.gid = gid;
458         }
459
460         if ( !NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map)) ) {
461                 d_fprintf(stderr, "Could not update group database\n");
462                 return -1;
463         }
464         
465         d_printf("Updated mapping entry for %s\n", map.nt_name);
466
467         return 0;
468 }
469
470 static int net_groupmap_delete(int argc, const char **argv)
471 {
472         DOM_SID sid;
473         fstring ntgroup = "";
474         fstring sid_string = "";
475         int i;
476
477         /* get the options */
478         for ( i=0; i<argc; i++ ) {
479                 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
480                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
481                         if ( !ntgroup[0] ) {
482                                 d_fprintf(stderr, "must supply a name\n");
483                                 return -1;
484                         }               
485                 }
486                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
487                         fstrcpy( sid_string, get_string_param( argv[i] ) );
488                         if ( !sid_string[0] ) {
489                                 d_fprintf(stderr, "must supply a SID\n");
490                                 return -1;
491                         }               
492                 }
493                 else {
494                         d_fprintf(stderr, "Bad option: %s\n", argv[i]);
495                         return -1;
496                 }
497         }
498         
499         if ( !ntgroup[0] && !sid_string[0]) {
500                 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
501                 return -1;
502         }
503         
504         /* give preference to the SID if we have that */
505         
506         if ( sid_string[0] )
507                 fstrcpy( ntgroup, sid_string );
508                 
509         if ( !get_sid_from_input(&sid, ntgroup) ) {
510                 d_fprintf(stderr, "Unable to resolve group %s to a SID\n", ntgroup);
511                 return -1;
512         }
513
514         if ( !NT_STATUS_IS_OK(pdb_delete_group_mapping_entry(sid)) ) {
515                 d_fprintf(stderr, "Failed to removing group %s from the mapping db!\n", ntgroup);
516                 return -1;
517         }
518
519         d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);
520
521         return 0;
522 }
523
524 static int net_groupmap_set(int argc, const char **argv)
525 {
526         const char *ntgroup = NULL;
527         struct group *grp = NULL;
528         GROUP_MAP map;
529         BOOL have_map = False;
530
531         if ((argc < 1) || (argc > 2)) {
532                 d_printf("Usage: net groupmap set \"NT Group\" "
533                          "[\"unix group\"] [-C \"comment\"] [-L] [-D]\n");
534                 return -1;
535         }
536
537         if ( opt_localgroup && opt_domaingroup ) {
538                 d_printf("Can only specify -L or -D, not both\n");
539                 return -1;
540         }
541
542         ntgroup = argv[0];
543
544         if (argc == 2) {
545                 grp = getgrnam(argv[1]);
546
547                 if (grp == NULL) {
548                         d_fprintf(stderr, "Could not find unix group %s\n", argv[1]);
549                         return -1;
550                 }
551         }
552
553         have_map = pdb_getgrnam(&map, ntgroup);
554
555         if (!have_map) {
556                 DOM_SID sid;
557                 have_map = ( (strncmp(ntgroup, "S-", 2) == 0) &&
558                              string_to_sid(&sid, ntgroup) &&
559                              pdb_getgrsid(&map, sid) );
560         }
561
562         if (!have_map) {
563
564                 /* Ok, add it */
565
566                 if (grp == NULL) {
567                         d_fprintf(stderr, "Could not find group mapping for %s\n",
568                                  ntgroup);
569                         return -1;
570                 }
571
572                 map.gid = grp->gr_gid;
573
574                 if (opt_rid == 0) {
575                         if ( pdb_rid_algorithm() )
576                                 opt_rid = algorithmic_pdb_gid_to_group_rid(map.gid);
577                         else {
578                                 if ( !pdb_new_rid((uint32*)&opt_rid) ) {
579                                         d_fprintf( stderr, "Could not allocate new RID\n");
580                                         return -1;
581                                 }
582                         }
583                 }
584
585                 sid_copy(&map.sid, get_global_sam_sid());
586                 sid_append_rid(&map.sid, opt_rid);
587
588                 map.sid_name_use = SID_NAME_DOM_GRP;
589                 fstrcpy(map.nt_name, ntgroup);
590                 fstrcpy(map.comment, "");
591
592                 if (!NT_STATUS_IS_OK(pdb_add_group_mapping_entry(&map))) {
593                         d_fprintf(stderr, "Could not add mapping entry for %s\n",
594                                  ntgroup);
595                         return -1;
596                 }
597         }
598
599         /* Now we have a mapping entry, update that stuff */
600
601         if ( opt_localgroup || opt_domaingroup ) {
602                 if (map.sid_name_use == SID_NAME_WKN_GRP) {
603                         d_fprintf(stderr, "Can't change type of the BUILTIN group %s\n",
604                                  map.nt_name);
605                         return -1;
606                 }
607         }
608
609         if (opt_localgroup)
610                 map.sid_name_use = SID_NAME_ALIAS;
611
612         if (opt_domaingroup)
613                 map.sid_name_use = SID_NAME_DOM_GRP;
614
615         /* The case (opt_domaingroup && opt_localgroup) was tested for above */
616
617         if (strlen(opt_comment) > 0)
618                 fstrcpy(map.comment, opt_comment);
619
620         if (strlen(opt_newntname) > 0)
621                 fstrcpy(map.nt_name, opt_newntname);
622
623         if (grp != NULL)
624                 map.gid = grp->gr_gid;
625
626         if (!NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map))) {
627                 d_fprintf(stderr, "Could not update group mapping for %s\n", ntgroup);
628                 return -1;
629         }
630
631         return 0;
632 }
633
634 static int net_groupmap_cleanup(int argc, const char **argv)
635 {
636         GROUP_MAP *map = NULL;
637         size_t i, entries;
638
639         if (!pdb_enum_group_mapping(NULL, SID_NAME_UNKNOWN, &map, &entries,
640                                     ENUM_ALL_MAPPED)) {
641                 d_fprintf(stderr, "Could not list group mappings\n");
642                 return -1;
643         }
644
645         for (i=0; i<entries; i++) {
646
647                 if (map[i].gid == -1)
648                         printf("Group %s is not mapped\n", map[i].nt_name);
649
650                 if (!sid_check_is_in_our_domain(&map[i].sid)) {
651                         printf("Deleting mapping for NT Group %s, sid %s\n",
652                                map[i].nt_name,
653                                sid_string_static(&map[i].sid));
654                         pdb_delete_group_mapping_entry(map[i].sid);
655                 }
656         }
657
658         SAFE_FREE(map);
659
660         return 0;
661 }
662
663 static int net_groupmap_addmem(int argc, const char **argv)
664 {
665         DOM_SID alias, member;
666
667         if ( (argc != 2) || 
668              !string_to_sid(&alias, argv[0]) ||
669              !string_to_sid(&member, argv[1]) ) {
670                 d_printf("Usage: net groupmap addmem alias-sid member-sid\n");
671                 return -1;
672         }
673
674         if (!NT_STATUS_IS_OK(pdb_add_aliasmem(&alias, &member))) {
675                 d_fprintf(stderr, "Could not add sid %s to alias %s\n",
676                          argv[1], argv[0]);
677                 return -1;
678         }
679
680         return 0;
681 }
682
683 static int net_groupmap_delmem(int argc, const char **argv)
684 {
685         DOM_SID alias, member;
686
687         if ( (argc != 2) || 
688              !string_to_sid(&alias, argv[0]) ||
689              !string_to_sid(&member, argv[1]) ) {
690                 d_printf("Usage: net groupmap delmem alias-sid member-sid\n");
691                 return -1;
692         }
693
694         if (!NT_STATUS_IS_OK(pdb_del_aliasmem(&alias, &member))) {
695                 d_fprintf(stderr, "Could not delete sid %s from alias %s\n",
696                          argv[1], argv[0]);
697                 return -1;
698         }
699
700         return 0;
701 }
702
703 static int net_groupmap_listmem(int argc, const char **argv)
704 {
705         DOM_SID alias;
706         DOM_SID *members;
707         size_t i, num;
708
709         if ( (argc != 1) || 
710              !string_to_sid(&alias, argv[0]) ) {
711                 d_printf("Usage: net groupmap listmem alias-sid\n");
712                 return -1;
713         }
714
715         members = NULL;
716         num = 0;
717
718         if (!NT_STATUS_IS_OK(pdb_enum_aliasmem(&alias, &members, &num))) {
719                 d_fprintf(stderr, "Could not list members for sid %s\n", argv[0]);
720                 return -1;
721         }
722
723         for (i = 0; i < num; i++) {
724                 printf("%s\n", sid_string_static(&(members[i])));
725         }
726
727         TALLOC_FREE(members);
728
729         return 0;
730 }
731
732 static BOOL print_alias_memberships(TALLOC_CTX *mem_ctx,
733                                     const DOM_SID *domain_sid,
734                                     const DOM_SID *member)
735 {
736         uint32 *alias_rids;
737         size_t i, num_alias_rids;
738
739         alias_rids = NULL;
740         num_alias_rids = 0;
741
742         if (!NT_STATUS_IS_OK(pdb_enum_alias_memberships(
743                                      mem_ctx, domain_sid, member, 1,
744                                      &alias_rids, &num_alias_rids))) {
745                 d_fprintf(stderr, "Could not list memberships for sid %s\n",
746                          sid_string_static(member));
747                 return False;
748         }
749
750         for (i = 0; i < num_alias_rids; i++) {
751                 DOM_SID alias;
752                 sid_copy(&alias, domain_sid);
753                 sid_append_rid(&alias, alias_rids[i]);
754                 printf("%s\n", sid_string_static(&alias));
755         }
756
757         return True;
758 }
759
760 static int net_groupmap_memberships(int argc, const char **argv)
761 {
762         TALLOC_CTX *mem_ctx;
763         DOM_SID *domain_sid, *builtin_sid, member;
764
765         if ( (argc != 1) || 
766              !string_to_sid(&member, argv[0]) ) {
767                 d_printf("Usage: net groupmap memberof sid\n");
768                 return -1;
769         }
770
771         mem_ctx = talloc_init("net_groupmap_memberships");
772         if (mem_ctx == NULL) {
773                 d_fprintf(stderr, "talloc_init failed\n");
774                 return -1;
775         }
776
777         domain_sid = get_global_sam_sid();
778         builtin_sid = string_sid_talloc(mem_ctx, "S-1-5-32");
779         if ((domain_sid == NULL) || (builtin_sid == NULL)) {
780                 d_fprintf(stderr, "Could not get domain sid\n");
781                 return -1;
782         }
783
784         if (!print_alias_memberships(mem_ctx, domain_sid, &member) ||
785             !print_alias_memberships(mem_ctx, builtin_sid, &member))
786                 return -1;
787
788         talloc_destroy(mem_ctx);
789
790         return 0;
791 }
792
793 int net_help_groupmap(int argc, const char **argv)
794 {
795         d_printf("net groupmap add"\
796                 "\n  Create a new group mapping\n");
797         d_printf("net groupmap modify"\
798                 "\n  Update a group mapping\n");
799         d_printf("net groupmap delete"\
800                 "\n  Remove a group mapping\n");
801         d_printf("net groupmap addmem"\
802                  "\n  Add a foreign alias member\n");
803         d_printf("net groupmap delmem"\
804                  "\n  Delete a foreign alias member\n");
805         d_printf("net groupmap listmem"\
806                  "\n  List foreign group members\n");
807         d_printf("net groupmap memberships"\
808                  "\n  List foreign group memberships\n");
809         d_printf("net groupmap list"\
810                 "\n  List current group map\n");
811         d_printf("net groupmap set"\
812                 "\n  Set group mapping\n");
813         d_printf("net groupmap cleanup"\
814                 "\n  Remove foreign group mapping entries\n");
815         
816         return -1;
817 }
818
819
820 /***********************************************************
821  migrated functionality from smbgroupedit
822  **********************************************************/
823 int net_groupmap(int argc, const char **argv)
824 {
825         struct functable func[] = {
826                 {"add", net_groupmap_add},
827                 {"modify", net_groupmap_modify},
828                 {"delete", net_groupmap_delete},
829                 {"set", net_groupmap_set},
830                 {"cleanup", net_groupmap_cleanup},
831                 {"addmem", net_groupmap_addmem},
832                 {"delmem", net_groupmap_delmem},
833                 {"listmem", net_groupmap_listmem},
834                 {"memberships", net_groupmap_memberships},
835                 {"list", net_groupmap_list},
836                 {"help", net_help_groupmap},
837                 {NULL, NULL}
838         };
839
840         /* we shouldn't have silly checks like this */
841         if (getuid() != 0) {
842                 d_fprintf(stderr, "You must be root to edit group mappings.\n");
843                 return -1;
844         }
845         
846         if ( argc )
847                 return net_run_function(argc, argv, func, net_help_groupmap);
848
849         return net_help_groupmap( argc, argv );
850 }
851