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, MAPPING_WITHOUT_PRIV)) {
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 )
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);
98 d_printf("%s (%s) -> %s\n", map.nt_name, string_sid, gidtoname(map.gid));
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 /*********************************************************
111 **********************************************************/
112 int net_groupmap_list(int argc, const char **argv)
115 BOOL long_list = False;
117 fstring ntgroup = "";
119 /* get the options */
120 for ( i=0; i<argc; i++ ) {
121 if ( !StrCaseCmp(argv[i], "verbose")) {
124 else if ( !StrnCaseCmp(argv[i], "name", strlen("name")) ) {
125 fstrcpy( ntgroup, get_string_param( argv[i] ) );
127 d_printf("must supply a name\n");
132 d_printf("Bad option: %s\n", argv[i]);
137 /* list a single group is given a name */
142 if (!get_sid_from_input(&sid, ntgroup)) {
146 /* Get the current mapping from the database */
147 if(!pdb_getgrsid(&map, sid, MAPPING_WITH_PRIV)) {
148 d_printf("Failure to local group SID in the database\n");
152 print_map_entry( map, long_list );
153 free_privilege(&(map.priv_set));
157 /* enumerate all group mappings */
158 if ( !pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &entries, ENUM_ALL_MAPPED, MAPPING_WITH_PRIV) )
161 for (i=0; i<entries; i++) {
162 print_map_entry( map[i], long_list );
163 free_privilege(&(map[i].priv_set));
170 /*********************************************************
171 Add a new group mapping entry
172 **********************************************************/
174 int net_groupmap_add(int argc, const char **argv)
176 PRIVILEGE_SET se_priv;
178 fstring ntgroup = "";
179 fstring unixgrp = "";
180 fstring string_sid = "";
182 fstring ntcomment = "";
183 enum SID_NAME_USE sid_type = SID_NAME_DOM_GRP;
188 /* get the options */
189 for ( i=0; i<argc; i++ ) {
190 if ( !StrnCaseCmp(argv[i], "rid", strlen("rid")) ) {
191 rid = get_int_param(argv[i]);
192 if ( rid < DOMAIN_GROUP_RID_ADMINS ) {
193 d_printf("RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
197 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
198 fstrcpy( unixgrp, get_string_param( argv[i] ) );
200 d_printf("must supply a name\n");
204 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
205 fstrcpy( ntgroup, get_string_param( argv[i] ) );
207 d_printf("must supply a name\n");
211 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
212 fstrcpy( string_sid, get_string_param( argv[i] ) );
213 if ( !string_sid[0] ) {
214 d_printf("must supply a SID\n");
218 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
219 fstrcpy( ntcomment, get_string_param( argv[i] ) );
220 if ( !ntcomment[0] ) {
221 d_printf("must supply a comment string\n");
225 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
226 fstrcpy( type, get_string_param( argv[i] ) );
230 sid_type = SID_NAME_WKN_GRP;
234 sid_type = SID_NAME_DOM_GRP;
238 sid_type = SID_NAME_ALIAS;
243 d_printf("Bad option: %s\n", argv[i]);
248 if ( !unixgrp[0] || (!rid && !string_sid[0]) ) {
249 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
253 /* append the rid to our own domain/machine SID if we don't have a full SID */
254 if ( !string_sid[0] ) {
255 sid_copy(&sid, get_global_sam_sid());
256 sid_append_rid(&sid, rid);
257 sid_to_string(string_sid, &sid);
261 fstrcpy(ntcomment, "Local Unix group");
263 if ( !(gid = nametogid(unixgrp)) ) {
264 d_printf("Can't lookup UNIX group %s\n", ntgroup);
269 fstrcpy( ntgroup, unixgrp );
272 init_privilege(&se_priv);
275 convert_priv_from_text(&se_priv, privilege);
278 if (!add_initial_entry(gid, string_sid, sid_type, ntgroup,
279 ntcomment, se_priv, PR_ACCESS_FROM_NETWORK) ) {
280 d_printf("adding entry for group %s failed!\n", ntgroup);
284 free_privilege(&se_priv);
286 d_printf("Successully added group %s to the mapping db\n", ntgroup);
290 int net_groupmap_modify(int argc, const char **argv)
294 fstring ntcomment = "";
296 fstring ntgroup = "";
297 fstring unixgrp = "";
298 fstring sid_string = "";
299 enum SID_NAME_USE sid_type = SID_NAME_UNKNOWN;
303 /* get the options */
304 for ( i=0; i<argc; i++ ) {
305 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
306 fstrcpy( ntgroup, get_string_param( argv[i] ) );
308 d_printf("must supply a name\n");
312 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
313 fstrcpy( sid_string, get_string_param( argv[i] ) );
314 if ( !sid_string[0] ) {
315 d_printf("must supply a name\n");
319 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
320 fstrcpy( ntcomment, get_string_param( argv[i] ) );
321 if ( !ntcomment[0] ) {
322 d_printf("must supply a comment string\n");
326 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
327 fstrcpy( unixgrp, get_string_param( argv[i] ) );
329 d_printf("must supply a group name\n");
333 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
334 fstrcpy( type, get_string_param( argv[i] ) );
338 sid_type = SID_NAME_DOM_GRP;
342 sid_type = SID_NAME_ALIAS;
347 d_printf("Bad option: %s\n", argv[i]);
352 if ( !ntgroup[0] && !sid_string[0] ) {
353 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
357 /* give preference to the SID; if both the ntgroup name and SID
358 are defined, use the SID and assume that the group name could be a
361 if ( sid_string[0] ) {
362 if (!get_sid_from_input(&sid, sid_string)) {
367 if (!get_sid_from_input(&sid, ntgroup)) {
372 /* Get the current mapping from the database */
373 if(!pdb_getgrsid(&map, sid, MAPPING_WITH_PRIV)) {
374 d_printf("Failure to local group SID in the database\n");
379 * Allow changing of group type only between domain and local
380 * We disallow changing Builtin groups !!! (SID problem)
382 if ( sid_type != SID_NAME_UNKNOWN )
384 if ( map.sid_name_use == SID_NAME_WKN_GRP ) {
385 d_printf("You can only change between domain and local groups.\n");
389 map.sid_name_use=sid_type;
392 /* Change comment if new one */
394 fstrcpy( map.comment, ntcomment );
397 fstrcpy( map.nt_name, ntgroup );
400 gid = nametogid( unixgrp );
402 d_printf("Unable to lookup UNIX group %s. Make sure the group exists.\n",
411 /* Change the privilege if new one */
413 convert_priv_from_text(&map.priv_set, privilege);
416 if ( !pdb_update_group_mapping_entry(&map) ) {
417 d_printf("Could not update group database\n");
418 free_privilege(&map.priv_set);
422 free_privilege(&map.priv_set);
424 d_printf("Updated mapping entry for %s\n", ntgroup);
429 int net_groupmap_delete(int argc, const char **argv)
432 fstring ntgroup = "";
433 fstring sid_string = "";
436 /* get the options */
437 for ( i=0; i<argc; i++ ) {
438 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
439 fstrcpy( ntgroup, get_string_param( argv[i] ) );
441 d_printf("must supply a name\n");
445 if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
446 fstrcpy( sid_string, get_string_param( argv[i] ) );
447 if ( !sid_string[0] ) {
448 d_printf("must supply a SID\n");
453 d_printf("Bad option: %s\n", argv[i]);
458 if ( !ntgroup[0] && !sid_string[0]) {
459 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
463 /* give preference to the SID if we have that */
466 fstrcpy( ntgroup, sid_string );
468 if ( !get_sid_from_input(&sid, ntgroup) ) {
469 d_printf("Unable to resolve group %s to a SID\n", ntgroup);
473 if ( !pdb_delete_group_mapping_entry(sid) ) {
474 printf("Failed to removing group %s from the mapping db!\n", ntgroup);
478 d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);