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.
25 #include "../utils/net.h"
28 /*********************************************************
29 utility function to parse an integer parameter from
31 **********************************************************/
32 static uint32 get_int_param( const char* param )
36 p = strchr( param, '=' );
43 /*********************************************************
44 utility function to parse an integer parameter from
46 **********************************************************/
47 static char* get_string_param( const char* param )
51 p = strchr( param, '=' );
58 /*********************************************************
59 Figure out if the input was an NT group or a SID string.
61 **********************************************************/
62 static BOOL get_sid_from_input(DOM_SID *sid, char *input)
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);
75 if (!string_to_sid(sid, input)) {
76 printf("converting sid %s from a string failed!\n", input);
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 )
92 decode_sid_name_use(group_type, map.sid_name_use);
93 sid_to_string(string_sid, &map.sid);
96 d_printf("%s (%s) -> %s\n", map.nt_name, string_sid, gidtoname(map.gid));
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);
106 /*********************************************************
108 **********************************************************/
109 static int net_groupmap_list(int argc, const char **argv)
112 BOOL long_list = False;
114 fstring ntgroup = "";
115 fstring sid_string = "";
117 /* get the options */
118 for ( i=0; i<argc; i++ ) {
119 if ( !StrCaseCmp(argv[i], "verbose")) {
122 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
123 fstrcpy( ntgroup, get_string_param( argv[i] ) );
125 d_printf("must supply a name\n");
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");
137 d_printf("Bad option: %s\n", argv[i]);
142 /* list a single group is given a name */
143 if ( ntgroup[0] || sid_string[0] ) {
148 fstrcpy( ntgroup, sid_string);
150 if (!get_sid_from_input(&sid, ntgroup)) {
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");
160 print_map_entry( map, long_list );
164 /* enumerate all group mappings */
165 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &entries, ENUM_ALL_MAPPED))
168 for (i=0; i<entries; i++) {
169 print_map_entry( map[i], long_list );
178 /*********************************************************
179 Add a new group mapping entry
180 **********************************************************/
182 static int net_groupmap_add(int argc, const char **argv)
185 fstring ntgroup = "";
186 fstring unixgrp = "";
187 fstring string_sid = "";
189 fstring ntcomment = "";
190 enum SID_NAME_USE sid_type = SID_NAME_DOM_GRP;
195 /* get the options */
196 for ( i=0; i<argc; i++ ) {
197 if ( !StrnCaseCmp(argv[i], "rid", strlen("rid")) ) {
198 rid = get_int_param(argv[i]);
199 if ( rid < DOMAIN_GROUP_RID_ADMINS ) {
200 d_printf("RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
204 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
205 fstrcpy( unixgrp, get_string_param( argv[i] ) );
207 d_printf("must supply a name\n");
211 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
212 fstrcpy( ntgroup, get_string_param( argv[i] ) );
214 d_printf("must supply a name\n");
218 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
219 fstrcpy( string_sid, get_string_param( argv[i] ) );
220 if ( !string_sid[0] ) {
221 d_printf("must supply a SID\n");
225 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
226 fstrcpy( ntcomment, get_string_param( argv[i] ) );
227 if ( !ntcomment[0] ) {
228 d_printf("must supply a comment string\n");
232 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
233 fstrcpy( type, get_string_param( argv[i] ) );
237 sid_type = SID_NAME_WKN_GRP;
241 sid_type = SID_NAME_DOM_GRP;
245 sid_type = SID_NAME_ALIAS;
250 d_printf("Bad option: %s\n", argv[i]);
255 if ( !unixgrp[0] || (!rid && !string_sid[0]) ) {
256 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
260 /* append the rid to our own domain/machine SID if we don't have a full SID */
261 if ( !string_sid[0] ) {
262 sid_copy(&sid, get_global_sam_sid());
263 sid_append_rid(&sid, rid);
264 sid_to_string(string_sid, &sid);
268 fstrcpy(ntcomment, "Local Unix group");
270 if ( (gid = nametogid(unixgrp)) == (gid_t)-1 ) {
271 d_printf("Can't lookup UNIX group %s\n", ntgroup);
276 fstrcpy( ntgroup, unixgrp );
279 if (!add_initial_entry(gid, string_sid, sid_type, ntgroup, ntcomment)) {
280 d_printf("adding entry for group %s failed!\n", ntgroup);
284 d_printf("Successully added group %s to the mapping db\n", ntgroup);
288 static int net_groupmap_modify(int argc, const char **argv)
292 fstring ntcomment = "";
294 fstring ntgroup = "";
295 fstring unixgrp = "";
296 fstring sid_string = "";
297 enum SID_NAME_USE sid_type = SID_NAME_UNKNOWN;
301 /* get the options */
302 for ( i=0; i<argc; i++ ) {
303 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
304 fstrcpy( ntgroup, get_string_param( argv[i] ) );
306 d_printf("must supply a name\n");
310 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
311 fstrcpy( sid_string, get_string_param( argv[i] ) );
312 if ( !sid_string[0] ) {
313 d_printf("must supply a name\n");
317 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
318 fstrcpy( ntcomment, get_string_param( argv[i] ) );
319 if ( !ntcomment[0] ) {
320 d_printf("must supply a comment string\n");
324 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
325 fstrcpy( unixgrp, get_string_param( argv[i] ) );
327 d_printf("must supply a group name\n");
331 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
332 fstrcpy( type, get_string_param( argv[i] ) );
336 sid_type = SID_NAME_DOM_GRP;
340 sid_type = SID_NAME_ALIAS;
345 d_printf("Bad option: %s\n", argv[i]);
350 if ( !ntgroup[0] && !sid_string[0] ) {
351 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
355 /* give preference to the SID; if both the ntgroup name and SID
356 are defined, use the SID and assume that the group name could be a
359 if ( sid_string[0] ) {
360 if (!get_sid_from_input(&sid, sid_string)) {
365 if (!get_sid_from_input(&sid, ntgroup)) {
370 /* Get the current mapping from the database */
371 if(!pdb_getgrsid(&map, sid)) {
372 d_printf("Failure to local group SID in the database\n");
377 * Allow changing of group type only between domain and local
378 * We disallow changing Builtin groups !!! (SID problem)
380 if (sid_type != SID_NAME_UNKNOWN) {
381 if (map.sid_name_use == SID_NAME_WKN_GRP) {
382 d_printf("You can only change between domain and local groups.\n");
386 map.sid_name_use=sid_type;
389 /* Change comment if new one */
391 fstrcpy( map.comment, ntcomment );
394 fstrcpy( map.nt_name, ntgroup );
397 gid = nametogid( unixgrp );
399 d_printf("Unable to lookup UNIX group %s. Make sure the group exists.\n",
407 if ( !pdb_update_group_mapping_entry(&map) ) {
408 d_printf("Could not update group database\n");
412 d_printf("Updated mapping entry for %s\n", ntgroup);
417 static int net_groupmap_delete(int argc, const char **argv)
420 fstring ntgroup = "";
421 fstring sid_string = "";
424 /* get the options */
425 for ( i=0; i<argc; i++ ) {
426 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
427 fstrcpy( ntgroup, get_string_param( argv[i] ) );
429 d_printf("must supply a name\n");
433 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
434 fstrcpy( sid_string, get_string_param( argv[i] ) );
435 if ( !sid_string[0] ) {
436 d_printf("must supply a SID\n");
441 d_printf("Bad option: %s\n", argv[i]);
446 if ( !ntgroup[0] && !sid_string[0]) {
447 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
451 /* give preference to the SID if we have that */
454 fstrcpy( ntgroup, sid_string );
456 if ( !get_sid_from_input(&sid, ntgroup) ) {
457 d_printf("Unable to resolve group %s to a SID\n", ntgroup);
461 if ( !pdb_delete_group_mapping_entry(sid) ) {
462 printf("Failed to removing group %s from the mapping db!\n", ntgroup);
466 d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);
471 int net_help_groupmap(int argc, const char **argv)
473 d_printf("net groupmap add"\
474 "\n Create a new group mapping\n");
475 d_printf("net groupmap modify"\
476 "\n Update a group mapping\n");
477 d_printf("net groupmap delete"\
478 "\n Remove a group mapping\n");
479 d_printf("net groupmap list"\
480 "\n List current group map\n");
486 /***********************************************************
487 migrated functionality from smbgroupedit
488 **********************************************************/
489 int net_groupmap(int argc, const char **argv)
491 struct functable func[] = {
492 {"add", net_groupmap_add},
493 {"modify", net_groupmap_modify},
494 {"delete", net_groupmap_delete},
495 {"list", net_groupmap_list},
496 {"help", net_help_groupmap},
500 /* we shouldn't have silly checks like this */
502 d_printf("You must be root to edit group mappings.\nExiting...\n");
506 return net_run_function(argc, argv, func, net_help_groupmap);
508 return net_help_groupmap( argc, argv );
510 return net_help_groupmap( argc, argv );