r9304: BUG 3001: don't use C style comments in configure.in (thanks Jason)
[Samba/nascimento.git] / source3 / utils / net_groupmap.c
blobb08673b2bb49b6c6062478654ed1cedecac74c3b
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 * Copyright (C) Volker Lendecke 2004
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
26 #include "utils/net.h"
29 /*********************************************************
30 utility function to parse an integer parameter from
31 "parameter = value"
32 **********************************************************/
33 static uint32 get_int_param( const char* param )
35 char *p;
37 p = strchr( param, '=' );
38 if ( !p )
39 return 0;
41 return atoi(p+1);
44 /*********************************************************
45 utility function to parse an integer parameter from
46 "parameter = value"
47 **********************************************************/
48 static char* get_string_param( const char* param )
50 char *p;
52 p = strchr( param, '=' );
53 if ( !p )
54 return NULL;
56 return (p+1);
59 /*********************************************************
60 Figure out if the input was an NT group or a SID string.
61 Return the SID.
62 **********************************************************/
63 static BOOL get_sid_from_input(DOM_SID *sid, char *input)
65 GROUP_MAP map;
67 if (StrnCaseCmp( input, "S-", 2)) {
68 /* Perhaps its the NT group name? */
69 if (!pdb_getgrnam(&map, input)) {
70 printf("NT Group %s doesn't exist in mapping DB\n", input);
71 return False;
72 } else {
73 *sid = map.sid;
75 } else {
76 if (!string_to_sid(sid, input)) {
77 printf("converting sid %s from a string failed!\n", input);
78 return False;
81 return True;
84 /*********************************************************
85 Dump a GROUP_MAP entry to stdout (long or short listing)
86 **********************************************************/
88 static void print_map_entry ( GROUP_MAP map, BOOL long_list )
90 fstring string_sid;
91 fstring group_type;
93 decode_sid_name_use(group_type, map.sid_name_use);
94 sid_to_string(string_sid, &map.sid);
96 if (!long_list)
97 d_printf("%s (%s) -> %s\n", map.nt_name, string_sid, gidtoname(map.gid));
98 else {
99 d_printf("%s\n", map.nt_name);
100 d_printf("\tSID : %s\n", string_sid);
101 d_printf("\tUnix group: %s\n", gidtoname(map.gid));
102 d_printf("\tGroup type: %s\n", group_type);
103 d_printf("\tComment : %s\n", map.comment);
107 /*********************************************************
108 List the groups.
109 **********************************************************/
110 static int net_groupmap_list(int argc, const char **argv)
112 int entries;
113 BOOL long_list = False;
114 int i;
115 fstring ntgroup = "";
116 fstring sid_string = "";
118 if (opt_verbose || opt_long_list_entries)
119 long_list = True;
121 /* get the options */
122 for ( i=0; i<argc; i++ ) {
123 if ( !StrCaseCmp(argv[i], "verbose")) {
124 long_list = True;
126 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
127 fstrcpy( ntgroup, get_string_param( argv[i] ) );
128 if ( !ntgroup[0] ) {
129 d_printf("must supply a name\n");
130 return -1;
133 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
134 fstrcpy( sid_string, get_string_param( argv[i] ) );
135 if ( !sid_string[0] ) {
136 d_printf("must supply a SID\n");
137 return -1;
140 else {
141 d_printf("Bad option: %s\n", argv[i]);
142 return -1;
146 /* list a single group is given a name */
147 if ( ntgroup[0] || sid_string[0] ) {
148 DOM_SID sid;
149 GROUP_MAP map;
151 if ( sid_string[0] )
152 fstrcpy( ntgroup, sid_string);
154 if (!get_sid_from_input(&sid, ntgroup)) {
155 return -1;
158 /* Get the current mapping from the database */
159 if(!pdb_getgrsid(&map, sid)) {
160 d_printf("Failure to local group SID in the database\n");
161 return -1;
164 print_map_entry( map, long_list );
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))
170 return -1;
172 for (i=0; i<entries; i++) {
173 print_map_entry( map[i], long_list );
176 SAFE_FREE(map);
179 return 0;
182 /*********************************************************
183 Add a new group mapping entry
184 **********************************************************/
186 static int net_groupmap_add(int argc, const char **argv)
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] ) {
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 if ( (gid = nametogid(unixgrp)) == (gid_t)-1 ) {
265 d_printf("Can't lookup UNIX group %s\n", unixgrp);
266 return -1;
269 if ( (rid == 0) && (string_sid[0] == '\0') ) {
270 d_printf("No rid or sid specified, choosing algorithmic mapping\n");
271 rid = pdb_gid_to_group_rid(gid);
274 /* append the rid to our own domain/machine SID if we don't have a full SID */
275 if ( !string_sid[0] ) {
276 sid_copy(&sid, get_global_sam_sid());
277 sid_append_rid(&sid, rid);
278 sid_to_string(string_sid, &sid);
281 if (!ntcomment[0]) {
282 switch (sid_type) {
283 case SID_NAME_WKN_GRP:
284 fstrcpy(ntcomment, "Wellknown Unix group");
285 break;
286 case SID_NAME_DOM_GRP:
287 fstrcpy(ntcomment, "Domain Unix group");
288 break;
289 case SID_NAME_ALIAS:
290 fstrcpy(ntcomment, "Local Unix group");
291 break;
292 default:
293 fstrcpy(ntcomment, "Unix group");
294 break;
298 if (!ntgroup[0] )
299 fstrcpy( ntgroup, unixgrp );
302 if (!add_initial_entry(gid, string_sid, sid_type, ntgroup, ntcomment)) {
303 d_printf("adding entry for group %s failed!\n", ntgroup);
304 return -1;
307 d_printf("Successfully added group %s to the mapping db\n", ntgroup);
308 return 0;
311 static int net_groupmap_modify(int argc, const char **argv)
313 DOM_SID sid;
314 GROUP_MAP map;
315 fstring ntcomment = "";
316 fstring type = "";
317 fstring ntgroup = "";
318 fstring unixgrp = "";
319 fstring sid_string = "";
320 enum SID_NAME_USE sid_type = SID_NAME_UNKNOWN;
321 int i;
322 gid_t gid;
324 /* get the options */
325 for ( i=0; i<argc; i++ ) {
326 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
327 fstrcpy( ntgroup, get_string_param( argv[i] ) );
328 if ( !ntgroup[0] ) {
329 d_printf("must supply a name\n");
330 return -1;
333 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
334 fstrcpy( sid_string, get_string_param( argv[i] ) );
335 if ( !sid_string[0] ) {
336 d_printf("must supply a name\n");
337 return -1;
340 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
341 fstrcpy( ntcomment, get_string_param( argv[i] ) );
342 if ( !ntcomment[0] ) {
343 d_printf("must supply a comment string\n");
344 return -1;
347 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
348 fstrcpy( unixgrp, get_string_param( argv[i] ) );
349 if ( !unixgrp[0] ) {
350 d_printf("must supply a group name\n");
351 return -1;
354 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
355 fstrcpy( type, get_string_param( argv[i] ) );
356 switch ( type[0] ) {
357 case 'd':
358 case 'D':
359 sid_type = SID_NAME_DOM_GRP;
360 break;
361 case 'l':
362 case 'L':
363 sid_type = SID_NAME_ALIAS;
364 break;
367 else {
368 d_printf("Bad option: %s\n", argv[i]);
369 return -1;
373 if ( !ntgroup[0] && !sid_string[0] ) {
374 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
375 return -1;
378 /* give preference to the SID; if both the ntgroup name and SID
379 are defined, use the SID and assume that the group name could be a
380 new name */
382 if ( sid_string[0] ) {
383 if (!get_sid_from_input(&sid, sid_string)) {
384 return -1;
387 else {
388 if (!get_sid_from_input(&sid, ntgroup)) {
389 return -1;
393 /* Get the current mapping from the database */
394 if(!pdb_getgrsid(&map, sid)) {
395 d_printf("Failure to local group SID in the database\n");
396 return -1;
400 * Allow changing of group type only between domain and local
401 * We disallow changing Builtin groups !!! (SID problem)
403 if (sid_type != SID_NAME_UNKNOWN) {
404 if (map.sid_name_use == SID_NAME_WKN_GRP) {
405 d_printf("You can only change between domain and local groups.\n");
406 return -1;
409 map.sid_name_use=sid_type;
412 /* Change comment if new one */
413 if ( ntcomment[0] )
414 fstrcpy( map.comment, ntcomment );
416 if ( ntgroup[0] )
417 fstrcpy( map.nt_name, ntgroup );
419 if ( unixgrp[0] ) {
420 gid = nametogid( unixgrp );
421 if ( gid == -1 ) {
422 d_printf("Unable to lookup UNIX group %s. Make sure the group exists.\n",
423 unixgrp);
424 return -1;
427 map.gid = gid;
430 if ( !pdb_update_group_mapping_entry(&map) ) {
431 d_printf("Could not update group database\n");
432 return -1;
435 d_printf("Updated mapping entry for %s\n", map.nt_name);
437 return 0;
440 static 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;
494 static int net_groupmap_set(int argc, const char **argv)
496 const char *ntgroup = NULL;
497 struct group *grp = NULL;
498 GROUP_MAP map;
499 BOOL have_map = False;
501 if ((argc < 1) || (argc > 2)) {
502 d_printf("Usage: net groupmap set \"NT Group\" "
503 "[\"unix group\"] [-C \"comment\"] [-L] [-D]\n");
504 return -1;
507 if ( opt_localgroup && opt_domaingroup ) {
508 d_printf("Can only specify -L or -D, not both\n");
509 return -1;
512 ntgroup = argv[0];
514 if (argc == 2) {
515 grp = getgrnam(argv[1]);
517 if (grp == NULL) {
518 d_printf("Could not find unix group %s\n", argv[1]);
519 return -1;
523 have_map = pdb_getgrnam(&map, ntgroup);
525 if (!have_map) {
526 DOM_SID sid;
527 have_map = ( (strncmp(ntgroup, "S-", 2) == 0) &&
528 string_to_sid(&sid, ntgroup) &&
529 pdb_getgrsid(&map, sid) );
532 if (!have_map) {
534 /* Ok, add it */
536 if (grp == NULL) {
537 d_printf("Could not find group mapping for %s\n",
538 ntgroup);
539 return -1;
542 map.gid = grp->gr_gid;
544 if (opt_rid == 0) {
545 opt_rid = pdb_gid_to_group_rid(map.gid);
548 sid_copy(&map.sid, get_global_sam_sid());
549 sid_append_rid(&map.sid, opt_rid);
551 map.sid_name_use = SID_NAME_DOM_GRP;
552 fstrcpy(map.nt_name, ntgroup);
553 fstrcpy(map.comment, "");
555 if (!pdb_add_group_mapping_entry(&map)) {
556 d_printf("Could not add mapping entry for %s\n",
557 ntgroup);
558 return -1;
562 /* Now we have a mapping entry, update that stuff */
564 if ( opt_localgroup || opt_domaingroup ) {
565 if (map.sid_name_use == SID_NAME_WKN_GRP) {
566 d_printf("Can't change type of the BUILTIN group %s\n",
567 map.nt_name);
568 return -1;
572 if (opt_localgroup)
573 map.sid_name_use = SID_NAME_ALIAS;
575 if (opt_domaingroup)
576 map.sid_name_use = SID_NAME_DOM_GRP;
578 /* The case (opt_domaingroup && opt_localgroup) was tested for above */
580 if (strlen(opt_comment) > 0)
581 fstrcpy(map.comment, opt_comment);
583 if (strlen(opt_newntname) > 0)
584 fstrcpy(map.nt_name, opt_newntname);
586 if (grp != NULL)
587 map.gid = grp->gr_gid;
589 if (!pdb_update_group_mapping_entry(&map)) {
590 d_printf("Could not update group mapping for %s\n", ntgroup);
591 return -1;
594 return 0;
597 static int net_groupmap_cleanup(int argc, const char **argv)
599 GROUP_MAP *map = NULL;
600 int i, entries;
602 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &entries,
603 ENUM_ALL_MAPPED)) {
604 d_printf("Could not list group mappings\n");
605 return -1;
608 for (i=0; i<entries; i++) {
610 if (map[i].sid_name_use == SID_NAME_WKN_GRP)
611 continue;
613 if (map[i].gid == -1)
614 printf("Group %s is not mapped\n", map[i].nt_name);
616 if (!sid_check_is_in_our_domain(&map[i].sid)) {
617 printf("Deleting mapping for NT Group %s, sid %s\n",
618 map[i].nt_name,
619 sid_string_static(&map[i].sid));
620 pdb_delete_group_mapping_entry(map[i].sid);
624 SAFE_FREE(map);
626 return 0;
629 static int net_groupmap_addmem(int argc, const char **argv)
631 DOM_SID alias, member;
633 if ( (argc != 2) ||
634 !string_to_sid(&alias, argv[0]) ||
635 !string_to_sid(&member, argv[1]) ) {
636 d_printf("Usage: net groupmap addmem alias-sid member-sid\n");
637 return -1;
640 if (!pdb_add_aliasmem(&alias, &member)) {
641 d_printf("Could not add sid %s to alias %s\n",
642 argv[1], argv[0]);
643 return -1;
646 return 0;
649 static int net_groupmap_delmem(int argc, const char **argv)
651 DOM_SID alias, member;
653 if ( (argc != 2) ||
654 !string_to_sid(&alias, argv[0]) ||
655 !string_to_sid(&member, argv[1]) ) {
656 d_printf("Usage: net groupmap delmem alias-sid member-sid\n");
657 return -1;
660 if (!pdb_del_aliasmem(&alias, &member)) {
661 d_printf("Could not delete sid %s from alias %s\n",
662 argv[1], argv[0]);
663 return -1;
666 return 0;
669 static int net_groupmap_listmem(int argc, const char **argv)
671 DOM_SID alias;
672 DOM_SID *members;
673 int i, num;
675 if ( (argc != 1) ||
676 !string_to_sid(&alias, argv[0]) ) {
677 d_printf("Usage: net groupmap listmem alias-sid\n");
678 return -1;
681 if (!pdb_enum_aliasmem(&alias, &members, &num)) {
682 d_printf("Could not list members for sid %s\n", argv[0]);
683 return -1;
686 for (i = 0; i < num; i++) {
687 printf("%s\n", sid_string_static(&(members[i])));
690 SAFE_FREE(members);
692 return 0;
695 static BOOL print_alias_memberships(TALLOC_CTX *mem_ctx,
696 const DOM_SID *domain_sid,
697 const DOM_SID *member)
699 uint32 *alias_rids;
700 int i, num_alias_rids;
702 alias_rids = NULL;
703 num_alias_rids = 0;
705 if (!pdb_enum_alias_memberships(mem_ctx, domain_sid, member, 1,
706 &alias_rids, &num_alias_rids)) {
707 d_printf("Could not list memberships for sid %s\n",
708 sid_string_static(member));
709 return False;
712 for (i = 0; i < num_alias_rids; i++) {
713 DOM_SID alias;
714 sid_copy(&alias, domain_sid);
715 sid_append_rid(&alias, alias_rids[i]);
716 printf("%s\n", sid_string_static(&alias));
719 return True;
722 static int net_groupmap_memberships(int argc, const char **argv)
724 TALLOC_CTX *mem_ctx;
725 DOM_SID *domain_sid, *builtin_sid, member;
727 if ( (argc != 1) ||
728 !string_to_sid(&member, argv[0]) ) {
729 d_printf("Usage: net groupmap memberof sid\n");
730 return -1;
733 mem_ctx = talloc_init("net_groupmap_memberships");
734 if (mem_ctx == NULL) {
735 d_printf("talloc_init failed\n");
736 return -1;
739 domain_sid = get_global_sam_sid();
740 builtin_sid = string_sid_talloc(mem_ctx, "S-1-5-32");
741 if ((domain_sid == NULL) || (builtin_sid == NULL)) {
742 d_printf("Could not get domain sid\n");
743 return -1;
746 if (!print_alias_memberships(mem_ctx, domain_sid, &member) ||
747 !print_alias_memberships(mem_ctx, builtin_sid, &member))
748 return -1;
750 talloc_destroy(mem_ctx);
752 return 0;
755 int net_help_groupmap(int argc, const char **argv)
757 d_printf("net groupmap add"\
758 "\n Create a new group mapping\n");
759 d_printf("net groupmap modify"\
760 "\n Update a group mapping\n");
761 d_printf("net groupmap delete"\
762 "\n Remove a group mapping\n");
763 d_printf("net groupmap addmem"\
764 "\n Add a foreign alias member\n");
765 d_printf("net groupmap delmem"\
766 "\n Delete a foreign alias member\n");
767 d_printf("net groupmap listmem"\
768 "\n List foreign group members\n");
769 d_printf("net groupmap memberships"\
770 "\n List foreign group memberships\n");
771 d_printf("net groupmap list"\
772 "\n List current group map\n");
773 d_printf("net groupmap set"\
774 "\n Set group mapping\n");
775 d_printf("net groupmap cleanup"\
776 "\n Remove foreign group mapping entries\n");
778 return -1;
782 /***********************************************************
783 migrated functionality from smbgroupedit
784 **********************************************************/
785 int net_groupmap(int argc, const char **argv)
787 struct functable func[] = {
788 {"add", net_groupmap_add},
789 {"modify", net_groupmap_modify},
790 {"delete", net_groupmap_delete},
791 {"set", net_groupmap_set},
792 {"cleanup", net_groupmap_cleanup},
793 {"addmem", net_groupmap_addmem},
794 {"delmem", net_groupmap_delmem},
795 {"listmem", net_groupmap_listmem},
796 {"memberships", net_groupmap_memberships},
797 {"list", net_groupmap_list},
798 {"help", net_help_groupmap},
799 {NULL, NULL}
802 /* we shouldn't have silly checks like this */
803 if (getuid() != 0) {
804 d_printf("You must be root to edit group mappings.\nExiting...\n");
805 return -1;
808 if ( argc )
809 return net_run_function(argc, argv, func, net_help_groupmap);
811 return net_help_groupmap( argc, argv );