r4868: Add "net rpc user RENAME"-command.
[Samba/nascimento.git] / source3 / utils / net_groupmap.c
blobc6391a65feb77bef5724f0d15ef8650f5fcba80c
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)) {
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;
92 decode_sid_name_use(group_type, map.sid_name_use);
93 sid_to_string(string_sid, &map.sid);
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);
106 /*********************************************************
107 List the groups.
108 **********************************************************/
109 static int net_groupmap_list(int argc, const char **argv)
111 int entries;
112 BOOL long_list = False;
113 int i;
114 fstring ntgroup = "";
115 fstring sid_string = "";
117 if (opt_verbose || opt_long_list_entries)
118 long_list = True;
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)) {
159 d_printf("Failure to local group SID in the database\n");
160 return -1;
163 print_map_entry( map, long_list );
165 else {
166 GROUP_MAP *map=NULL;
167 /* enumerate all group mappings */
168 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &entries, ENUM_ALL_MAPPED))
169 return -1;
171 for (i=0; i<entries; i++) {
172 print_map_entry( map[i], long_list );
175 SAFE_FREE(map);
178 return 0;
181 /*********************************************************
182 Add a new group mapping entry
183 **********************************************************/
185 static int net_groupmap_add(int argc, const char **argv)
187 DOM_SID sid;
188 fstring ntgroup = "";
189 fstring unixgrp = "";
190 fstring string_sid = "";
191 fstring type = "";
192 fstring ntcomment = "";
193 enum SID_NAME_USE sid_type = SID_NAME_DOM_GRP;
194 uint32 rid = 0;
195 gid_t gid;
196 int i;
198 /* get the options */
199 for ( i=0; i<argc; i++ ) {
200 if ( !StrnCaseCmp(argv[i], "rid", strlen("rid")) ) {
201 rid = get_int_param(argv[i]);
202 if ( rid < DOMAIN_GROUP_RID_ADMINS ) {
203 d_printf("RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
204 return -1;
207 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
208 fstrcpy( unixgrp, get_string_param( argv[i] ) );
209 if ( !unixgrp[0] ) {
210 d_printf("must supply a name\n");
211 return -1;
214 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
215 fstrcpy( ntgroup, get_string_param( argv[i] ) );
216 if ( !ntgroup[0] ) {
217 d_printf("must supply a name\n");
218 return -1;
221 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
222 fstrcpy( string_sid, get_string_param( argv[i] ) );
223 if ( !string_sid[0] ) {
224 d_printf("must supply a SID\n");
225 return -1;
228 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
229 fstrcpy( ntcomment, get_string_param( argv[i] ) );
230 if ( !ntcomment[0] ) {
231 d_printf("must supply a comment string\n");
232 return -1;
235 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
236 fstrcpy( type, get_string_param( argv[i] ) );
237 switch ( type[0] ) {
238 case 'b':
239 case 'B':
240 sid_type = SID_NAME_WKN_GRP;
241 break;
242 case 'd':
243 case 'D':
244 sid_type = SID_NAME_DOM_GRP;
245 break;
246 case 'l':
247 case 'L':
248 sid_type = SID_NAME_ALIAS;
249 break;
252 else {
253 d_printf("Bad option: %s\n", argv[i]);
254 return -1;
258 if ( !unixgrp[0] ) {
259 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
260 return -1;
263 if ( (gid = nametogid(unixgrp)) == (gid_t)-1 ) {
264 d_printf("Can't lookup UNIX group %s\n", unixgrp);
265 return -1;
268 if ( (rid == 0) && (string_sid[0] == '\0') ) {
269 d_printf("No rid or sid specified, choosing algorithmic mapping\n");
270 rid = pdb_gid_to_group_rid(gid);
273 /* append the rid to our own domain/machine SID if we don't have a full SID */
274 if ( !string_sid[0] ) {
275 sid_copy(&sid, get_global_sam_sid());
276 sid_append_rid(&sid, rid);
277 sid_to_string(string_sid, &sid);
280 if (!ntcomment[0])
281 fstrcpy(ntcomment, "Local Unix group");
283 if (!ntgroup[0] )
284 fstrcpy( ntgroup, unixgrp );
287 if (!add_initial_entry(gid, string_sid, sid_type, ntgroup, ntcomment)) {
288 d_printf("adding entry for group %s failed!\n", ntgroup);
289 return -1;
292 d_printf("Successully added group %s to the mapping db\n", ntgroup);
293 return 0;
296 static int net_groupmap_modify(int argc, const char **argv)
298 DOM_SID sid;
299 GROUP_MAP map;
300 fstring ntcomment = "";
301 fstring type = "";
302 fstring ntgroup = "";
303 fstring unixgrp = "";
304 fstring sid_string = "";
305 enum SID_NAME_USE sid_type = SID_NAME_UNKNOWN;
306 int i;
307 gid_t gid;
309 /* get the options */
310 for ( i=0; i<argc; i++ ) {
311 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
312 fstrcpy( ntgroup, get_string_param( argv[i] ) );
313 if ( !ntgroup[0] ) {
314 d_printf("must supply a name\n");
315 return -1;
318 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
319 fstrcpy( sid_string, get_string_param( argv[i] ) );
320 if ( !sid_string[0] ) {
321 d_printf("must supply a name\n");
322 return -1;
325 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
326 fstrcpy( ntcomment, get_string_param( argv[i] ) );
327 if ( !ntcomment[0] ) {
328 d_printf("must supply a comment string\n");
329 return -1;
332 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
333 fstrcpy( unixgrp, get_string_param( argv[i] ) );
334 if ( !unixgrp[0] ) {
335 d_printf("must supply a group name\n");
336 return -1;
339 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
340 fstrcpy( type, get_string_param( argv[i] ) );
341 switch ( type[0] ) {
342 case 'd':
343 case 'D':
344 sid_type = SID_NAME_DOM_GRP;
345 break;
346 case 'l':
347 case 'L':
348 sid_type = SID_NAME_ALIAS;
349 break;
352 else {
353 d_printf("Bad option: %s\n", argv[i]);
354 return -1;
358 if ( !ntgroup[0] && !sid_string[0] ) {
359 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
360 return -1;
363 /* give preference to the SID; if both the ntgroup name and SID
364 are defined, use the SID and assume that the group name could be a
365 new name */
367 if ( sid_string[0] ) {
368 if (!get_sid_from_input(&sid, sid_string)) {
369 return -1;
372 else {
373 if (!get_sid_from_input(&sid, ntgroup)) {
374 return -1;
378 /* Get the current mapping from the database */
379 if(!pdb_getgrsid(&map, sid)) {
380 d_printf("Failure to local group SID in the database\n");
381 return -1;
385 * Allow changing of group type only between domain and local
386 * We disallow changing Builtin groups !!! (SID problem)
388 if (sid_type != SID_NAME_UNKNOWN) {
389 if (map.sid_name_use == SID_NAME_WKN_GRP) {
390 d_printf("You can only change between domain and local groups.\n");
391 return -1;
394 map.sid_name_use=sid_type;
397 /* Change comment if new one */
398 if ( ntcomment[0] )
399 fstrcpy( map.comment, ntcomment );
401 if ( ntgroup[0] )
402 fstrcpy( map.nt_name, ntgroup );
404 if ( unixgrp[0] ) {
405 gid = nametogid( unixgrp );
406 if ( gid == -1 ) {
407 d_printf("Unable to lookup UNIX group %s. Make sure the group exists.\n",
408 unixgrp);
409 return -1;
412 map.gid = gid;
415 if ( !pdb_update_group_mapping_entry(&map) ) {
416 d_printf("Could not update group database\n");
417 return -1;
420 d_printf("Updated mapping entry for %s\n", map.nt_name);
422 return 0;
425 static int net_groupmap_delete(int argc, const char **argv)
427 DOM_SID sid;
428 fstring ntgroup = "";
429 fstring sid_string = "";
430 int i;
432 /* get the options */
433 for ( i=0; i<argc; i++ ) {
434 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
435 fstrcpy( ntgroup, get_string_param( argv[i] ) );
436 if ( !ntgroup[0] ) {
437 d_printf("must supply a name\n");
438 return -1;
441 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
442 fstrcpy( sid_string, get_string_param( argv[i] ) );
443 if ( !sid_string[0] ) {
444 d_printf("must supply a SID\n");
445 return -1;
448 else {
449 d_printf("Bad option: %s\n", argv[i]);
450 return -1;
454 if ( !ntgroup[0] && !sid_string[0]) {
455 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
456 return -1;
459 /* give preference to the SID if we have that */
461 if ( sid_string[0] )
462 fstrcpy( ntgroup, sid_string );
464 if ( !get_sid_from_input(&sid, ntgroup) ) {
465 d_printf("Unable to resolve group %s to a SID\n", ntgroup);
466 return -1;
469 if ( !pdb_delete_group_mapping_entry(sid) ) {
470 printf("Failed to removing group %s from the mapping db!\n", ntgroup);
471 return -1;
474 d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);
476 return 0;
479 static int net_groupmap_set(int argc, const char **argv)
481 const char *ntgroup = NULL;
482 struct group *grp = NULL;
483 GROUP_MAP map;
484 BOOL have_map = False;
486 if ((argc < 1) || (argc > 2)) {
487 d_printf("Usage: net groupmap set \"NT Group\" "
488 "[\"unix group\"] [-C \"comment\"] [-L] [-D]\n");
489 return -1;
492 if ( opt_localgroup && opt_domaingroup ) {
493 d_printf("Can only specify -L or -D, not both\n");
494 return -1;
497 ntgroup = argv[0];
499 if (argc == 2) {
500 grp = getgrnam(argv[1]);
502 if (grp == NULL) {
503 d_printf("Could not find unix group %s\n", argv[1]);
504 return -1;
508 have_map = pdb_getgrnam(&map, ntgroup);
510 if (!have_map) {
511 DOM_SID sid;
512 have_map = ( (strncmp(ntgroup, "S-", 2) == 0) &&
513 string_to_sid(&sid, ntgroup) &&
514 pdb_getgrsid(&map, sid) );
517 if (!have_map) {
519 /* Ok, add it */
521 if (grp == NULL) {
522 d_printf("Could not find group mapping for %s\n",
523 ntgroup);
524 return -1;
527 map.gid = grp->gr_gid;
529 if (opt_rid == 0) {
530 opt_rid = pdb_gid_to_group_rid(map.gid);
533 sid_copy(&map.sid, get_global_sam_sid());
534 sid_append_rid(&map.sid, opt_rid);
536 map.sid_name_use = SID_NAME_DOM_GRP;
537 fstrcpy(map.nt_name, ntgroup);
538 fstrcpy(map.comment, "");
540 if (!pdb_add_group_mapping_entry(&map)) {
541 d_printf("Could not add mapping entry for %s\n",
542 ntgroup);
543 return -1;
547 /* Now we have a mapping entry, update that stuff */
549 if ( opt_localgroup || opt_domaingroup ) {
550 if (map.sid_name_use == SID_NAME_WKN_GRP) {
551 d_printf("Can't change type of the BUILTIN group %s\n",
552 map.nt_name);
553 return -1;
557 if (opt_localgroup)
558 map.sid_name_use = SID_NAME_ALIAS;
560 if (opt_domaingroup)
561 map.sid_name_use = SID_NAME_DOM_GRP;
563 /* The case (opt_domaingroup && opt_localgroup) was tested for above */
565 if (strlen(opt_comment) > 0)
566 fstrcpy(map.comment, opt_comment);
568 if (strlen(opt_newntname) > 0)
569 fstrcpy(map.nt_name, opt_newntname);
571 if (grp != NULL)
572 map.gid = grp->gr_gid;
574 if (!pdb_update_group_mapping_entry(&map)) {
575 d_printf("Could not update group mapping for %s\n", ntgroup);
576 return -1;
579 return 0;
582 static int net_groupmap_cleanup(int argc, const char **argv)
584 GROUP_MAP *map = NULL;
585 int i, entries;
587 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &entries,
588 ENUM_ALL_MAPPED)) {
589 d_printf("Could not list group mappings\n");
590 return -1;
593 for (i=0; i<entries; i++) {
595 if (map[i].sid_name_use == SID_NAME_WKN_GRP)
596 continue;
598 if (map[i].gid == -1)
599 printf("Group %s is not mapped\n", map[i].nt_name);
601 if (!sid_check_is_in_our_domain(&map[i].sid)) {
602 printf("Deleting mapping for NT Group %s, sid %s\n",
603 map[i].nt_name,
604 sid_string_static(&map[i].sid));
605 pdb_delete_group_mapping_entry(map[i].sid);
609 SAFE_FREE(map);
611 return 0;
614 static int net_groupmap_addmem(int argc, const char **argv)
616 DOM_SID alias, member;
618 if ( (argc != 2) ||
619 !string_to_sid(&alias, argv[0]) ||
620 !string_to_sid(&member, argv[1]) ) {
621 d_printf("Usage: net groupmap addmem alias-sid member-sid\n");
622 return -1;
625 if (!pdb_add_aliasmem(&alias, &member)) {
626 d_printf("Could not add sid %s to alias %s\n",
627 argv[1], argv[0]);
628 return -1;
631 return 0;
634 static int net_groupmap_delmem(int argc, const char **argv)
636 DOM_SID alias, member;
638 if ( (argc != 2) ||
639 !string_to_sid(&alias, argv[0]) ||
640 !string_to_sid(&member, argv[1]) ) {
641 d_printf("Usage: net groupmap delmem alias-sid member-sid\n");
642 return -1;
645 if (!pdb_del_aliasmem(&alias, &member)) {
646 d_printf("Could not delete sid %s from alias %s\n",
647 argv[1], argv[0]);
648 return -1;
651 return 0;
654 static int net_groupmap_listmem(int argc, const char **argv)
656 DOM_SID alias;
657 DOM_SID *members;
658 int i, num;
659 NTSTATUS result;
661 if ( (argc != 1) ||
662 !string_to_sid(&alias, argv[0]) ) {
663 d_printf("Usage: net groupmap listmem alias-sid\n");
664 return -1;
667 if (!pdb_enum_aliasmem(&alias, &members, &num)) {
668 d_printf("Could not list members for sid %s: %s\n",
669 argv[0], nt_errstr(result));
670 return -1;
673 for (i = 0; i < num; i++) {
674 printf("%s\n", sid_string_static(&(members[i])));
677 SAFE_FREE(members);
679 return 0;
682 static int net_groupmap_memberships(int argc, const char **argv)
684 DOM_SID member;
685 DOM_SID *aliases;
686 int i, num;
687 NTSTATUS result;
689 if ( (argc != 1) ||
690 !string_to_sid(&member, argv[0]) ) {
691 d_printf("Usage: net groupmap memberof sid\n");
692 return -1;
695 if (!pdb_enum_alias_memberships(&member, 1, &aliases, &num)) {
696 d_printf("Could not list memberships for sid %s: %s\n",
697 argv[0], nt_errstr(result));
698 return -1;
701 for (i = 0; i < num; i++) {
702 printf("%s\n", sid_string_static(&(aliases[i])));
705 SAFE_FREE(aliases);
707 return 0;
710 int net_help_groupmap(int argc, const char **argv)
712 d_printf("net groupmap add"\
713 "\n Create a new group mapping\n");
714 d_printf("net groupmap modify"\
715 "\n Update a group mapping\n");
716 d_printf("net groupmap delete"\
717 "\n Remove a group mapping\n");
718 d_printf("net groupmap addmem"\
719 "\n Add a foreign alias member\n");
720 d_printf("net groupmap delmem"\
721 "\n Delete a foreign alias member\n");
722 d_printf("net groupmap listmem"\
723 "\n List foreign group members\n");
724 d_printf("net groupmap memberships"\
725 "\n List foreign group memberships\n");
726 d_printf("net groupmap list"\
727 "\n List current group map\n");
728 d_printf("net groupmap set"\
729 "\n Set group mapping\n");
730 d_printf("net groupmap cleanup"\
731 "\n Remove foreign group mapping entries\n");
733 return -1;
737 /***********************************************************
738 migrated functionality from smbgroupedit
739 **********************************************************/
740 int net_groupmap(int argc, const char **argv)
742 struct functable func[] = {
743 {"add", net_groupmap_add},
744 {"modify", net_groupmap_modify},
745 {"delete", net_groupmap_delete},
746 {"set", net_groupmap_set},
747 {"cleanup", net_groupmap_cleanup},
748 {"addmem", net_groupmap_addmem},
749 {"delmem", net_groupmap_delmem},
750 {"listmem", net_groupmap_listmem},
751 {"memberships", net_groupmap_memberships},
752 {"list", net_groupmap_list},
753 {"help", net_help_groupmap},
754 {NULL, NULL}
757 /* we shouldn't have silly checks like this */
758 if (getuid() != 0) {
759 d_printf("You must be root to edit group mappings.\nExiting...\n");
760 return -1;
763 if ( argc )
764 return net_run_function(argc, argv, func, net_help_groupmap);
766 return net_help_groupmap( argc, argv );