c9c37a68c27a408e08cfc22f535bed2c7c46c84d
[gd/samba/.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  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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         fstring string_sid;
90         fstring group_type;
91         
92         decode_sid_name_use(group_type, map.sid_name_use);
93         sid_to_string(string_sid, &map.sid);
94                 
95         if (!long_list)
96                 d_printf("%s (%s) -> %s\n", map.nt_name, string_sid, gidtoname(map.gid));
97         else {
98                 d_printf("%s\n", map.nt_name);
99                 d_printf("\tSID       : %s\n", string_sid);
100                 d_printf("\tUnix group: %s\n", gidtoname(map.gid));
101                 d_printf("\tGroup type: %s\n", group_type);
102                 d_printf("\tComment   : %s\n", map.comment);
103         }
104
105 }
106 /*********************************************************
107  List the groups.
108 **********************************************************/
109 static int net_groupmap_list(int argc, const char **argv)
110 {
111         int entries;
112         BOOL long_list = False;
113         int i;
114         fstring ntgroup = "";
115         fstring sid_string = "";
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_printf("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_printf("must supply a SID\n");
133                                 return -1;
134                         }               
135                 }
136                 else {
137                         d_printf("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_printf("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(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
173         return 0;
174 }
175
176 /*********************************************************
177  Add a new group mapping entry
178 **********************************************************/
179
180 static int net_groupmap_add(int argc, const char **argv)
181 {
182         DOM_SID sid;
183         fstring ntgroup = "";
184         fstring unixgrp = "";
185         fstring string_sid = "";
186         fstring type = "";
187         fstring ntcomment = "";
188         enum SID_NAME_USE sid_type = SID_NAME_DOM_GRP;
189         uint32 rid = 0; 
190         gid_t gid;
191         int i;
192         
193         /* get the options */
194         for ( i=0; i<argc; i++ ) {
195                 if ( !StrnCaseCmp(argv[i], "rid", strlen("rid")) ) {
196                         rid = get_int_param(argv[i]);
197                         if ( rid < DOMAIN_GROUP_RID_ADMINS ) {
198                                 d_printf("RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
199                                 return -1;
200                         }
201                 }
202                 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
203                         fstrcpy( unixgrp, get_string_param( argv[i] ) );
204                         if ( !unixgrp[0] ) {
205                                 d_printf("must supply a name\n");
206                                 return -1;
207                         }               
208                 }
209                 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
210                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
211                         if ( !ntgroup[0] ) {
212                                 d_printf("must supply a name\n");
213                                 return -1;
214                         }               
215                 }
216                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
217                         fstrcpy( string_sid, get_string_param( argv[i] ) );
218                         if ( !string_sid[0] ) {
219                                 d_printf("must supply a SID\n");
220                                 return -1;
221                         }               
222                 }
223                 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
224                         fstrcpy( ntcomment, get_string_param( argv[i] ) );
225                         if ( !ntcomment[0] ) {
226                                 d_printf("must supply a comment string\n");
227                                 return -1;
228                         }                               
229                 }
230                 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) )  {
231                         fstrcpy( type, get_string_param( argv[i] ) );
232                         switch ( type[0] ) {
233                                 case 'b':
234                                 case 'B':
235                                         sid_type = SID_NAME_WKN_GRP;
236                                         break;
237                                 case 'd':
238                                 case 'D':
239                                         sid_type = SID_NAME_DOM_GRP;
240                                         break;
241                                 case 'l':
242                                 case 'L':
243                                         sid_type = SID_NAME_ALIAS;
244                                         break;
245                         }
246                 }
247                 else {
248                         d_printf("Bad option: %s\n", argv[i]);
249                         return -1;
250                 }
251         }
252
253         if ( !unixgrp[0] || (!rid && !string_sid[0]) ) {
254                 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
255                 return -1;
256         }
257         
258         /* append the rid to our own domain/machine SID if we don't have a full SID */
259         if ( !string_sid[0] ) {
260                 sid_copy(&sid, get_global_sam_sid());
261                 sid_append_rid(&sid, rid);
262                 sid_to_string(string_sid, &sid);
263         }
264
265         if (ntcomment[0])
266                 fstrcpy(ntcomment, "Local Unix group");
267                 
268         if ( (gid = nametogid(unixgrp)) == (gid_t)-1 ) {
269                 d_printf("Can't lookup UNIX group %s\n", ntgroup);
270                 return -1;
271         }
272         
273         if ( !ntgroup[0] )
274                 fstrcpy( ntgroup, unixgrp );
275                 
276         
277         if (!add_initial_entry(gid, string_sid, sid_type, ntgroup, ntcomment)) {
278                 d_printf("adding entry for group %s failed!\n", ntgroup);
279                 return -1;
280         }
281
282         d_printf("Successully added group %s to the mapping db\n", ntgroup);
283         return 0;
284 }
285
286 static int net_groupmap_modify(int argc, const char **argv)
287 {
288         DOM_SID sid;
289         GROUP_MAP map;
290         fstring ntcomment = "";
291         fstring type = "";
292         fstring ntgroup = "";
293         fstring unixgrp = "";
294         fstring sid_string = "";
295         enum SID_NAME_USE sid_type = SID_NAME_UNKNOWN;
296         int i;
297         gid_t gid;
298
299         /* get the options */
300         for ( i=0; i<argc; i++ ) {
301                 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
302                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
303                         if ( !ntgroup[0] ) {
304                                 d_printf("must supply a name\n");
305                                 return -1;
306                         }               
307                 }
308                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
309                         fstrcpy( sid_string, get_string_param( argv[i] ) );
310                         if ( !sid_string[0] ) {
311                                 d_printf("must supply a name\n");
312                                 return -1;
313                         }               
314                 }
315                 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
316                         fstrcpy( ntcomment, get_string_param( argv[i] ) );
317                         if ( !ntcomment[0] ) {
318                                 d_printf("must supply a comment string\n");
319                                 return -1;
320                         }                               
321                 }
322                 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
323                         fstrcpy( unixgrp, get_string_param( argv[i] ) );
324                         if ( !unixgrp[0] ) {
325                                 d_printf("must supply a group name\n");
326                                 return -1;
327                         }                               
328                 }
329                 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) )  {
330                         fstrcpy( type, get_string_param( argv[i] ) );
331                         switch ( type[0] ) {
332                                 case 'd':
333                                 case 'D':
334                                         sid_type = SID_NAME_DOM_GRP;
335                                         break;
336                                 case 'l':
337                                 case 'L':
338                                         sid_type = SID_NAME_ALIAS;
339                                         break;
340                         }
341                 }
342                 else {
343                         d_printf("Bad option: %s\n", argv[i]);
344                         return -1;
345                 }
346         }
347         
348         if ( !ntgroup[0] && !sid_string[0] ) {
349                 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
350                 return -1;
351         }
352
353         /* give preference to the SID; if both the ntgroup name and SID
354            are defined, use the SID and assume that the group name could be a 
355            new name */
356                 
357         if ( sid_string[0] ) {  
358                 if (!get_sid_from_input(&sid, sid_string)) {
359                         return -1;
360                 }
361         }
362         else {
363                 if (!get_sid_from_input(&sid, ntgroup)) {
364                         return -1;
365                 }
366         }       
367
368         /* Get the current mapping from the database */
369         if(!pdb_getgrsid(&map, sid)) {
370                 d_printf("Failure to local group SID in the database\n");
371                 return -1;
372         }
373         
374         /*
375          * Allow changing of group type only between domain and local
376          * We disallow changing Builtin groups !!! (SID problem)
377          */ 
378         if (sid_type != SID_NAME_UNKNOWN) { 
379                 if (map.sid_name_use == SID_NAME_WKN_GRP) {
380                         d_printf("You can only change between domain and local groups.\n");
381                         return -1;
382                 }
383                 
384                 map.sid_name_use=sid_type;
385         }
386
387         /* Change comment if new one */
388         if ( ntcomment[0] )
389                 fstrcpy( map.comment, ntcomment );
390                 
391         if ( ntgroup[0] )
392                 fstrcpy( map.nt_name, ntgroup );
393                 
394         if ( unixgrp[0] ) {
395                 gid = nametogid( unixgrp );
396                 if ( gid == -1 ) {
397                         d_printf("Unable to lookup UNIX group %s.  Make sure the group exists.\n",
398                                 unixgrp);
399                         return -1;
400                 }
401                 
402                 map.gid = gid;
403         }
404
405         if ( !pdb_update_group_mapping_entry(&map) ) {
406                 d_printf("Could not update group database\n");
407                 return -1;
408         }
409         
410         d_printf("Updated mapping entry for %s\n", ntgroup);
411
412         return 0;
413 }
414
415 static int net_groupmap_delete(int argc, const char **argv)
416 {
417         DOM_SID sid;
418         fstring ntgroup = "";
419         fstring sid_string = "";
420         int i;
421
422         /* get the options */
423         for ( i=0; i<argc; i++ ) {
424                 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
425                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
426                         if ( !ntgroup[0] ) {
427                                 d_printf("must supply a name\n");
428                                 return -1;
429                         }               
430                 }
431                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
432                         fstrcpy( sid_string, get_string_param( argv[i] ) );
433                         if ( !sid_string[0] ) {
434                                 d_printf("must supply a SID\n");
435                                 return -1;
436                         }               
437                 }
438                 else {
439                         d_printf("Bad option: %s\n", argv[i]);
440                         return -1;
441                 }
442         }
443         
444         if ( !ntgroup[0] && !sid_string[0]) {
445                 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
446                 return -1;
447         }
448         
449         /* give preference to the SID if we have that */
450         
451         if ( sid_string[0] )
452                 fstrcpy( ntgroup, sid_string );
453                 
454         if ( !get_sid_from_input(&sid, ntgroup) ) {
455                 d_printf("Unable to resolve group %s to a SID\n", ntgroup);
456                 return -1;
457         }
458
459         if ( !pdb_delete_group_mapping_entry(sid) ) {
460                 printf("Failed to removing group %s from the mapping db!\n", ntgroup);
461                 return -1;
462         }
463
464         d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);
465
466         return 0;
467 }
468
469 int net_help_groupmap(int argc, const char **argv)
470 {
471         d_printf("net groupmap add"\
472                 "\n  Create a new group mapping\n");
473         d_printf("net groupmap modify"\
474                 "\n  Update a group mapping\n");
475         d_printf("net groupmap delete"\
476                 "\n  Remove a group mapping\n");
477         d_printf("net groupmap list"\
478                 "\n  List current group map\n");
479         
480         return -1;
481 }
482
483
484 /***********************************************************
485  migrated functionality from smbgroupedit
486  **********************************************************/
487 int net_groupmap(int argc, const char **argv)
488 {
489         /* we shouldn't have silly checks like this */
490         if (getuid() != 0) {
491                 d_printf("You must be root to edit group mappings.\nExiting...\n");
492                 return -1;
493         }
494         
495         struct functable func[] = {
496                 {"add", net_groupmap_add},
497                 {"modify", net_groupmap_modify},
498                 {"delete", net_groupmap_delete},
499                 {"list", net_groupmap_list},
500                 {"help", net_help_groupmap},
501                 {NULL, NULL}
502         };
503
504         return net_run_function(argc, argv, func, net_help_groupmap);
505         if ( 0 == argc )
506                 return net_help_groupmap( argc, argv );
507
508         return net_help_groupmap( argc, argv );
509 }
510