f4cd8c13a6f294b12f41b687772771a2a2ba015e
[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, MAPPING_WITHOUT_PRIV)) {
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         fstring priv_text;
92         
93         decode_sid_name_use(group_type, map.sid_name_use);
94         sid_to_string(string_sid, &map.sid);
95         convert_priv_to_text(&(map.priv_set), priv_text);
96                 
97         if (!long_list)
98                 d_printf("%s (%s) -> %s\n", map.nt_name, string_sid, gidtoname(map.gid));
99         else {
100                 d_printf("%s\n", map.nt_name);
101                 d_printf("\tSID       : %s\n", string_sid);
102                 d_printf("\tUnix group: %s\n", gidtoname(map.gid));
103                 d_printf("\tGroup type: %s\n", group_type);
104                 d_printf("\tComment   : %s\n", map.comment);
105                 d_printf("\tPrivilege : %s\n\n", priv_text);
106         }
107
108 }
109 /*********************************************************
110  List the groups.
111 **********************************************************/
112 int net_groupmap_list(int argc, const char **argv)
113 {
114         int entries;
115         BOOL long_list = False;
116         int i;
117         fstring ntgroup = "";
118         fstring sid_string = "";
119         
120         /* get the options */
121         for ( i=0; i<argc; i++ ) {
122                 if ( !StrCaseCmp(argv[i], "verbose")) {
123                         long_list = True;
124                 }
125                 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
126                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
127                         if ( !ntgroup[0] ) {
128                                 d_printf("must supply a name\n");
129                                 return -1;
130                         }               
131                 }
132                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
133                         fstrcpy( sid_string, get_string_param( argv[i] ) );
134                         if ( !sid_string[0] ) {
135                                 d_printf("must supply a SID\n");
136                                 return -1;
137                         }               
138                 }
139                 else {
140                         d_printf("Bad option: %s\n", argv[i]);
141                         return -1;
142                 }
143         }
144
145         /* list a single group is given a name */
146         if ( ntgroup[0] || sid_string[0] ) {
147                 DOM_SID sid;
148                 GROUP_MAP map;
149                 
150                 if ( sid_string[0] )
151                         fstrcpy( ntgroup, sid_string);
152                         
153                 if (!get_sid_from_input(&sid, ntgroup)) {
154                         return -1;
155                 }
156
157                 /* Get the current mapping from the database */
158                 if(!pdb_getgrsid(&map, sid, MAPPING_WITH_PRIV)) {
159                         d_printf("Failure to local group SID in the database\n");
160                         return -1;
161                 }
162         
163                 print_map_entry( map, long_list );
164                 free_privilege(&(map.priv_set));        
165         }
166         else {
167                 GROUP_MAP *map=NULL;
168                 /* enumerate all group mappings */
169                 if ( !pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &entries, ENUM_ALL_MAPPED, MAPPING_WITH_PRIV) )
170                         return -1;
171         
172                 for (i=0; i<entries; i++) {
173                         print_map_entry( map[i], long_list );
174                         free_privilege(&(map[i].priv_set));
175                 }
176         }
177
178         return 0;
179 }
180
181 /*********************************************************
182  Add a new group mapping entry
183 **********************************************************/
184
185 int net_groupmap_add(int argc, const char **argv)
186 {
187         PRIVILEGE_SET se_priv;
188         DOM_SID sid;
189         fstring ntgroup = "";
190         fstring unixgrp = "";
191         fstring string_sid = "";
192         fstring type = "";
193         fstring ntcomment = "";
194         enum SID_NAME_USE sid_type = SID_NAME_DOM_GRP;
195         uint32 rid = 0; 
196         gid_t gid;
197         int i;
198         
199         /* get the options */
200         for ( i=0; i<argc; i++ ) {
201                 if ( !StrnCaseCmp(argv[i], "rid", strlen("rid")) ) {
202                         rid = get_int_param(argv[i]);
203                         if ( rid < DOMAIN_GROUP_RID_ADMINS ) {
204                                 d_printf("RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
205                                 return -1;
206                         }
207                 }
208                 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
209                         fstrcpy( unixgrp, get_string_param( argv[i] ) );
210                         if ( !unixgrp[0] ) {
211                                 d_printf("must supply a name\n");
212                                 return -1;
213                         }               
214                 }
215                 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
216                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
217                         if ( !ntgroup[0] ) {
218                                 d_printf("must supply a name\n");
219                                 return -1;
220                         }               
221                 }
222                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
223                         fstrcpy( string_sid, get_string_param( argv[i] ) );
224                         if ( !string_sid[0] ) {
225                                 d_printf("must supply a SID\n");
226                                 return -1;
227                         }               
228                 }
229                 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
230                         fstrcpy( ntcomment, get_string_param( argv[i] ) );
231                         if ( !ntcomment[0] ) {
232                                 d_printf("must supply a comment string\n");
233                                 return -1;
234                         }                               
235                 }
236                 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) )  {
237                         fstrcpy( type, get_string_param( argv[i] ) );
238                         switch ( type[0] ) {
239                                 case 'b':
240                                 case 'B':
241                                         sid_type = SID_NAME_WKN_GRP;
242                                         break;
243                                 case 'd':
244                                 case 'D':
245                                         sid_type = SID_NAME_DOM_GRP;
246                                         break;
247                                 case 'l':
248                                 case 'L':
249                                         sid_type = SID_NAME_ALIAS;
250                                         break;
251                         }
252                 }
253                 else {
254                         d_printf("Bad option: %s\n", argv[i]);
255                         return -1;
256                 }
257         }
258
259         if ( !unixgrp[0] || (!rid && !string_sid[0]) ) {
260                 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
261                 return -1;
262         }
263         
264         /* append the rid to our own domain/machine SID if we don't have a full SID */
265         if ( !string_sid[0] ) {
266                 sid_copy(&sid, get_global_sam_sid());
267                 sid_append_rid(&sid, rid);
268                 sid_to_string(string_sid, &sid);
269         }
270
271         if (ntcomment[0])
272                 fstrcpy(ntcomment, "Local Unix group");
273                 
274         if ( !(gid = nametogid(unixgrp)) ) {
275                 d_printf("Can't lookup UNIX group %s\n", ntgroup);
276                 return -1;
277         }
278         
279         if ( !ntgroup[0] )
280                 fstrcpy( ntgroup, unixgrp );
281                 
282         
283         init_privilege(&se_priv);
284 #if 0
285         if (privilege!=NULL)
286                 convert_priv_from_text(&se_priv, privilege);
287 #endif
288
289         if (!add_initial_entry(gid, string_sid, sid_type, ntgroup,
290                               ntcomment, se_priv, PR_ACCESS_FROM_NETWORK) ) {
291                 d_printf("adding entry for group %s failed!\n", ntgroup);
292                 return -1;
293         }
294
295         free_privilege(&se_priv);
296                 
297         d_printf("Successully added group %s to the mapping db\n", ntgroup);
298         return 0;
299 }
300
301 int net_groupmap_modify(int argc, const char **argv)
302 {
303         DOM_SID sid;
304         GROUP_MAP map;
305         fstring ntcomment = "";
306         fstring type = "";
307         fstring ntgroup = "";
308         fstring unixgrp = "";
309         fstring sid_string = "";
310         enum SID_NAME_USE sid_type = SID_NAME_UNKNOWN;
311         int i;
312         gid_t gid;
313
314         /* get the options */
315         for ( i=0; i<argc; i++ ) {
316                 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
317                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
318                         if ( !ntgroup[0] ) {
319                                 d_printf("must supply a name\n");
320                                 return -1;
321                         }               
322                 }
323                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
324                         fstrcpy( sid_string, get_string_param( argv[i] ) );
325                         if ( !sid_string[0] ) {
326                                 d_printf("must supply a name\n");
327                                 return -1;
328                         }               
329                 }
330                 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
331                         fstrcpy( ntcomment, get_string_param( argv[i] ) );
332                         if ( !ntcomment[0] ) {
333                                 d_printf("must supply a comment string\n");
334                                 return -1;
335                         }                               
336                 }
337                 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
338                         fstrcpy( unixgrp, get_string_param( argv[i] ) );
339                         if ( !unixgrp[0] ) {
340                                 d_printf("must supply a group name\n");
341                                 return -1;
342                         }                               
343                 }
344                 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) )  {
345                         fstrcpy( type, get_string_param( argv[i] ) );
346                         switch ( type[0] ) {
347                                 case 'd':
348                                 case 'D':
349                                         sid_type = SID_NAME_DOM_GRP;
350                                         break;
351                                 case 'l':
352                                 case 'L':
353                                         sid_type = SID_NAME_ALIAS;
354                                         break;
355                         }
356                 }
357                 else {
358                         d_printf("Bad option: %s\n", argv[i]);
359                         return -1;
360                 }
361         }
362         
363         if ( !ntgroup[0] && !sid_string[0] ) {
364                 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
365                 return -1;
366         }
367
368         /* give preference to the SID; if both the ntgroup name and SID
369            are defined, use the SID and assume that the group name could be a 
370            new name */
371                 
372         if ( sid_string[0] ) {  
373                 if (!get_sid_from_input(&sid, sid_string)) {
374                         return -1;
375                 }
376         }
377         else {
378                 if (!get_sid_from_input(&sid, ntgroup)) {
379                         return -1;
380                 }
381         }       
382
383         /* Get the current mapping from the database */
384         if(!pdb_getgrsid(&map, sid, MAPPING_WITH_PRIV)) {
385                 d_printf("Failure to local group SID in the database\n");
386                 return -1;
387         }
388         
389         /*
390          * Allow changing of group type only between domain and local
391          * We disallow changing Builtin groups !!! (SID problem)
392          */ 
393         if ( sid_type != SID_NAME_UNKNOWN ) 
394         { 
395                 if ( map.sid_name_use == SID_NAME_WKN_GRP ) {
396                         d_printf("You can only change between domain and local groups.\n");
397                         return -1;
398                 }
399                 
400                 map.sid_name_use=sid_type;
401         }
402
403         /* Change comment if new one */
404         if ( ntcomment[0] )
405                 fstrcpy( map.comment, ntcomment );
406                 
407         if ( ntgroup[0] )
408                 fstrcpy( map.nt_name, ntgroup );
409                 
410         if ( unixgrp[0] ) {
411                 gid = nametogid( unixgrp );
412                 if ( gid == -1 ) {
413                         d_printf("Unable to lookup UNIX group %s.  Make sure the group exists.\n",
414                                 unixgrp);
415                         return -1;
416                 }
417                 
418                 map.gid = gid;
419         }
420
421 #if 0
422         /* Change the privilege if new one */
423         if (privilege!=NULL)
424                 convert_priv_from_text(&map.priv_set, privilege);
425 #endif
426
427         if ( !pdb_update_group_mapping_entry(&map) ) {
428                 d_printf("Could not update group database\n");
429                 free_privilege(&map.priv_set);
430                 return -1;
431         }
432         
433         free_privilege(&map.priv_set);
434         
435         d_printf("Updated mapping entry for %s\n", ntgroup);
436
437         return 0;
438 }
439
440 int net_groupmap_delete(int argc, const char **argv)
441 {
442         DOM_SID sid;
443         fstring ntgroup = "";
444         fstring sid_string = "";
445         int i;
446
447         /* get the options */
448         for ( i=0; i<argc; i++ ) {
449                 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
450                         fstrcpy( ntgroup, get_string_param( argv[i] ) );
451                         if ( !ntgroup[0] ) {
452                                 d_printf("must supply a name\n");
453                                 return -1;
454                         }               
455                 }
456                 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
457                         fstrcpy( sid_string, get_string_param( argv[i] ) );
458                         if ( !sid_string[0] ) {
459                                 d_printf("must supply a SID\n");
460                                 return -1;
461                         }               
462                 }
463                 else {
464                         d_printf("Bad option: %s\n", argv[i]);
465                         return -1;
466                 }
467         }
468         
469         if ( !ntgroup[0] && !sid_string[0]) {
470                 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
471                 return -1;
472         }
473         
474         /* give preference to the SID if we have that */
475         
476         if ( sid_string[0] )
477                 fstrcpy( ntgroup, sid_string );
478                 
479         if ( !get_sid_from_input(&sid, ntgroup) ) {
480                 d_printf("Unable to resolve group %s to a SID\n", ntgroup);
481                 return -1;
482         }
483
484         if ( !pdb_delete_group_mapping_entry(sid) ) {
485                 printf("Failed to removing group %s from the mapping db!\n", ntgroup);
486                 return -1;
487         }
488
489         d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);
490
491         return 0;
492 }
493