Fix up a bunch of problems in editreg.c
[Samba.git] / source3 / utils / net_groupmap.c
blobf4cd8c13a6f294b12f41b687772771a2a2ba015e
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.
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.
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.
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.
24 #include "includes.h"
25 #include "../utils/net.h"
28 /*********************************************************
29 utility function to parse an integer parameter from
30 "parameter = value"
31 **********************************************************/
32 static uint32 get_int_param( const char* param )
34 char *p;
36 p = strchr( param, '=' );
37 if ( !p )
38 return 0;
40 return atoi(p+1);
43 /*********************************************************
44 utility function to parse an integer parameter from
45 "parameter = value"
46 **********************************************************/
47 static char* get_string_param( const char* param )
49 char *p;
51 p = strchr( param, '=' );
52 if ( !p )
53 return NULL;
55 return (p+1);
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)
64 GROUP_MAP map;
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;
74 } else {
75 if (!string_to_sid(sid, input)) {
76 printf("converting sid %s from a string failed!\n", input);
77 return False;
80 return True;
83 /*********************************************************
84 Dump a GROUP_MAP entry to stdout (long or short listing)
85 **********************************************************/
87 static void print_map_entry ( GROUP_MAP map, BOOL long_list )
89 fstring string_sid;
90 fstring group_type;
91 fstring priv_text;
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);
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);
109 /*********************************************************
110 List the groups.
111 **********************************************************/
112 int net_groupmap_list(int argc, const char **argv)
114 int entries;
115 BOOL long_list = False;
116 int i;
117 fstring ntgroup = "";
118 fstring sid_string = "";
120 /* get the options */
121 for ( i=0; i<argc; i++ ) {
122 if ( !StrCaseCmp(argv[i], "verbose")) {
123 long_list = True;
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;
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;
139 else {
140 d_printf("Bad option: %s\n", argv[i]);
141 return -1;
145 /* list a single group is given a name */
146 if ( ntgroup[0] || sid_string[0] ) {
147 DOM_SID sid;
148 GROUP_MAP map;
150 if ( sid_string[0] )
151 fstrcpy( ntgroup, sid_string);
153 if (!get_sid_from_input(&sid, ntgroup)) {
154 return -1;
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;
163 print_map_entry( map, long_list );
164 free_privilege(&(map.priv_set));
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;
172 for (i=0; i<entries; i++) {
173 print_map_entry( map[i], long_list );
174 free_privilege(&(map[i].priv_set));
178 return 0;
181 /*********************************************************
182 Add a new group mapping entry
183 **********************************************************/
185 int net_groupmap_add(int argc, const char **argv)
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;
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;
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;
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;
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;
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;
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;
253 else {
254 d_printf("Bad option: %s\n", argv[i]);
255 return -1;
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;
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);
271 if (ntcomment[0])
272 fstrcpy(ntcomment, "Local Unix group");
274 if ( !(gid = nametogid(unixgrp)) ) {
275 d_printf("Can't lookup UNIX group %s\n", ntgroup);
276 return -1;
279 if ( !ntgroup[0] )
280 fstrcpy( ntgroup, unixgrp );
283 init_privilege(&se_priv);
284 #if 0
285 if (privilege!=NULL)
286 convert_priv_from_text(&se_priv, privilege);
287 #endif
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;
295 free_privilege(&se_priv);
297 d_printf("Successully added group %s to the mapping db\n", ntgroup);
298 return 0;
301 int net_groupmap_modify(int argc, const char **argv)
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;
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;
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;
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;
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;
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;
357 else {
358 d_printf("Bad option: %s\n", argv[i]);
359 return -1;
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;
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 */
372 if ( sid_string[0] ) {
373 if (!get_sid_from_input(&sid, sid_string)) {
374 return -1;
377 else {
378 if (!get_sid_from_input(&sid, ntgroup)) {
379 return -1;
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;
390 * Allow changing of group type only between domain and local
391 * We disallow changing Builtin groups !!! (SID problem)
393 if ( sid_type != SID_NAME_UNKNOWN )
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;
400 map.sid_name_use=sid_type;
403 /* Change comment if new one */
404 if ( ntcomment[0] )
405 fstrcpy( map.comment, ntcomment );
407 if ( ntgroup[0] )
408 fstrcpy( map.nt_name, ntgroup );
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;
418 map.gid = gid;
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
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;
433 free_privilege(&map.priv_set);
435 d_printf("Updated mapping entry for %s\n", ntgroup);
437 return 0;
440 int net_groupmap_delete(int argc, const char **argv)
442 DOM_SID sid;
443 fstring ntgroup = "";
444 fstring sid_string = "";
445 int i;
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;
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;
463 else {
464 d_printf("Bad option: %s\n", argv[i]);
465 return -1;
469 if ( !ntgroup[0] && !sid_string[0]) {
470 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
471 return -1;
474 /* give preference to the SID if we have that */
476 if ( sid_string[0] )
477 fstrcpy( ntgroup, sid_string );
479 if ( !get_sid_from_input(&sid, ntgroup) ) {
480 d_printf("Unable to resolve group %s to a SID\n", ntgroup);
481 return -1;
484 if ( !pdb_delete_group_mapping_entry(sid) ) {
485 printf("Failed to removing group %s from the mapping db!\n", ntgroup);
486 return -1;
489 d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);
491 return 0;