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 if (opt_verbose
|| opt_long_list_entries
)
120 /* get the options */
121 for ( i
=0; i
<argc
; i
++ ) {
122 if ( !StrCaseCmp(argv
[i
], "verbose")) {
125 else if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
126 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
128 d_printf("must supply a name\n");
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");
140 d_printf("Bad option: %s\n", argv
[i
]);
145 /* list a single group is given a name */
146 if ( ntgroup
[0] || sid_string
[0] ) {
151 fstrcpy( ntgroup
, sid_string
);
153 if (!get_sid_from_input(&sid
, ntgroup
)) {
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");
163 print_map_entry( map
, long_list
);
167 /* enumerate all group mappings */
168 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN
, &map
, &entries
, ENUM_ALL_MAPPED
))
171 for (i
=0; i
<entries
; i
++) {
172 print_map_entry( map
[i
], long_list
);
181 /*********************************************************
182 Add a new group mapping entry
183 **********************************************************/
185 static int net_groupmap_add(int argc
, const char **argv
)
188 fstring ntgroup
= "";
189 fstring unixgrp
= "";
190 fstring string_sid
= "";
192 fstring ntcomment
= "";
193 enum SID_NAME_USE sid_type
= SID_NAME_DOM_GRP
;
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);
207 else if ( !StrnCaseCmp(argv
[i
], "unixgroup", strlen("unixgroup")) ) {
208 fstrcpy( unixgrp
, get_string_param( argv
[i
] ) );
210 d_printf("must supply a name\n");
214 else if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
215 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
217 d_printf("must supply a name\n");
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");
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");
235 else if ( !StrnCaseCmp(argv
[i
], "type", strlen("type")) ) {
236 fstrcpy( type
, get_string_param( argv
[i
] ) );
240 sid_type
= SID_NAME_WKN_GRP
;
244 sid_type
= SID_NAME_DOM_GRP
;
248 sid_type
= SID_NAME_ALIAS
;
253 d_printf("Bad option: %s\n", argv
[i
]);
259 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
263 if ( (gid
= nametogid(unixgrp
)) == (gid_t
)-1 ) {
264 d_printf("Can't lookup UNIX group %s\n", unixgrp
);
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
);
282 case SID_NAME_WKN_GRP
:
283 fstrcpy(ntcomment
, "Wellknown Unix group");
285 case SID_NAME_DOM_GRP
:
286 fstrcpy(ntcomment
, "Domain Unix group");
289 fstrcpy(ntcomment
, "Local Unix group");
292 fstrcpy(ntcomment
, "Unix group");
298 fstrcpy( ntgroup
, unixgrp
);
301 if (!add_initial_entry(gid
, string_sid
, sid_type
, ntgroup
, ntcomment
)) {
302 d_printf("adding entry for group %s failed!\n", ntgroup
);
306 d_printf("Successfully added group %s to the mapping db\n", ntgroup
);
310 static int net_groupmap_modify(int argc
, const char **argv
)
314 fstring ntcomment
= "";
316 fstring ntgroup
= "";
317 fstring unixgrp
= "";
318 fstring sid_string
= "";
319 enum SID_NAME_USE sid_type
= SID_NAME_UNKNOWN
;
323 /* get the options */
324 for ( i
=0; i
<argc
; i
++ ) {
325 if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
326 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
328 d_printf("must supply a name\n");
332 else if ( !StrnCaseCmp(argv
[i
], "sid", strlen("sid")) ) {
333 fstrcpy( sid_string
, get_string_param( argv
[i
] ) );
334 if ( !sid_string
[0] ) {
335 d_printf("must supply a name\n");
339 else if ( !StrnCaseCmp(argv
[i
], "comment", strlen("comment")) ) {
340 fstrcpy( ntcomment
, get_string_param( argv
[i
] ) );
341 if ( !ntcomment
[0] ) {
342 d_printf("must supply a comment string\n");
346 else if ( !StrnCaseCmp(argv
[i
], "unixgroup", strlen("unixgroup")) ) {
347 fstrcpy( unixgrp
, get_string_param( argv
[i
] ) );
349 d_printf("must supply a group name\n");
353 else if ( !StrnCaseCmp(argv
[i
], "type", strlen("type")) ) {
354 fstrcpy( type
, get_string_param( argv
[i
] ) );
358 sid_type
= SID_NAME_DOM_GRP
;
362 sid_type
= SID_NAME_ALIAS
;
367 d_printf("Bad option: %s\n", argv
[i
]);
372 if ( !ntgroup
[0] && !sid_string
[0] ) {
373 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
377 /* give preference to the SID; if both the ntgroup name and SID
378 are defined, use the SID and assume that the group name could be a
381 if ( sid_string
[0] ) {
382 if (!get_sid_from_input(&sid
, sid_string
)) {
387 if (!get_sid_from_input(&sid
, ntgroup
)) {
392 /* Get the current mapping from the database */
393 if(!pdb_getgrsid(&map
, sid
)) {
394 d_printf("Failure to local group SID in the database\n");
399 * Allow changing of group type only between domain and local
400 * We disallow changing Builtin groups !!! (SID problem)
402 if (sid_type
!= SID_NAME_UNKNOWN
) {
403 if (map
.sid_name_use
== SID_NAME_WKN_GRP
) {
404 d_printf("You can only change between domain and local groups.\n");
408 map
.sid_name_use
=sid_type
;
411 /* Change comment if new one */
413 fstrcpy( map
.comment
, ntcomment
);
416 fstrcpy( map
.nt_name
, ntgroup
);
419 gid
= nametogid( unixgrp
);
421 d_printf("Unable to lookup UNIX group %s. Make sure the group exists.\n",
429 if ( !pdb_update_group_mapping_entry(&map
) ) {
430 d_printf("Could not update group database\n");
434 d_printf("Updated mapping entry for %s\n", map
.nt_name
);
439 static int net_groupmap_delete(int argc
, const char **argv
)
442 fstring ntgroup
= "";
443 fstring sid_string
= "";
446 /* get the options */
447 for ( i
=0; i
<argc
; i
++ ) {
448 if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
449 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
451 d_printf("must supply a name\n");
455 else if ( !StrnCaseCmp(argv
[i
], "sid", strlen("sid")) ) {
456 fstrcpy( sid_string
, get_string_param( argv
[i
] ) );
457 if ( !sid_string
[0] ) {
458 d_printf("must supply a SID\n");
463 d_printf("Bad option: %s\n", argv
[i
]);
468 if ( !ntgroup
[0] && !sid_string
[0]) {
469 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
473 /* give preference to the SID if we have that */
476 fstrcpy( ntgroup
, sid_string
);
478 if ( !get_sid_from_input(&sid
, ntgroup
) ) {
479 d_printf("Unable to resolve group %s to a SID\n", ntgroup
);
483 if ( !pdb_delete_group_mapping_entry(sid
) ) {
484 printf("Failed to removing group %s from the mapping db!\n", ntgroup
);
488 d_printf("Sucessfully removed %s from the mapping db\n", ntgroup
);
493 static int net_groupmap_set(int argc
, const char **argv
)
495 const char *ntgroup
= NULL
;
496 struct group
*grp
= NULL
;
498 BOOL have_map
= False
;
500 if ((argc
< 1) || (argc
> 2)) {
501 d_printf("Usage: net groupmap set \"NT Group\" "
502 "[\"unix group\"] [-C \"comment\"] [-L] [-D]\n");
506 if ( opt_localgroup
&& opt_domaingroup
) {
507 d_printf("Can only specify -L or -D, not both\n");
514 grp
= getgrnam(argv
[1]);
517 d_printf("Could not find unix group %s\n", argv
[1]);
522 have_map
= pdb_getgrnam(&map
, ntgroup
);
526 have_map
= ( (strncmp(ntgroup
, "S-", 2) == 0) &&
527 string_to_sid(&sid
, ntgroup
) &&
528 pdb_getgrsid(&map
, sid
) );
536 d_printf("Could not find group mapping for %s\n",
541 map
.gid
= grp
->gr_gid
;
544 opt_rid
= pdb_gid_to_group_rid(map
.gid
);
547 sid_copy(&map
.sid
, get_global_sam_sid());
548 sid_append_rid(&map
.sid
, opt_rid
);
550 map
.sid_name_use
= SID_NAME_DOM_GRP
;
551 fstrcpy(map
.nt_name
, ntgroup
);
552 fstrcpy(map
.comment
, "");
554 if (!pdb_add_group_mapping_entry(&map
)) {
555 d_printf("Could not add mapping entry for %s\n",
561 /* Now we have a mapping entry, update that stuff */
563 if ( opt_localgroup
|| opt_domaingroup
) {
564 if (map
.sid_name_use
== SID_NAME_WKN_GRP
) {
565 d_printf("Can't change type of the BUILTIN group %s\n",
572 map
.sid_name_use
= SID_NAME_ALIAS
;
575 map
.sid_name_use
= SID_NAME_DOM_GRP
;
577 /* The case (opt_domaingroup && opt_localgroup) was tested for above */
579 if (strlen(opt_comment
) > 0)
580 fstrcpy(map
.comment
, opt_comment
);
582 if (strlen(opt_newntname
) > 0)
583 fstrcpy(map
.nt_name
, opt_newntname
);
586 map
.gid
= grp
->gr_gid
;
588 if (!pdb_update_group_mapping_entry(&map
)) {
589 d_printf("Could not update group mapping for %s\n", ntgroup
);
596 static int net_groupmap_cleanup(int argc
, const char **argv
)
598 GROUP_MAP
*map
= NULL
;
601 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN
, &map
, &entries
,
603 d_printf("Could not list group mappings\n");
607 for (i
=0; i
<entries
; i
++) {
609 if (map
[i
].sid_name_use
== SID_NAME_WKN_GRP
)
612 if (map
[i
].gid
== -1)
613 printf("Group %s is not mapped\n", map
[i
].nt_name
);
615 if (!sid_check_is_in_our_domain(&map
[i
].sid
)) {
616 printf("Deleting mapping for NT Group %s, sid %s\n",
618 sid_string_static(&map
[i
].sid
));
619 pdb_delete_group_mapping_entry(map
[i
].sid
);
628 static int net_groupmap_addmem(int argc
, const char **argv
)
630 DOM_SID alias
, member
;
633 !string_to_sid(&alias
, argv
[0]) ||
634 !string_to_sid(&member
, argv
[1]) ) {
635 d_printf("Usage: net groupmap addmem alias-sid member-sid\n");
639 if (!pdb_add_aliasmem(&alias
, &member
)) {
640 d_printf("Could not add sid %s to alias %s\n",
648 static int net_groupmap_delmem(int argc
, const char **argv
)
650 DOM_SID alias
, member
;
653 !string_to_sid(&alias
, argv
[0]) ||
654 !string_to_sid(&member
, argv
[1]) ) {
655 d_printf("Usage: net groupmap delmem alias-sid member-sid\n");
659 if (!pdb_del_aliasmem(&alias
, &member
)) {
660 d_printf("Could not delete sid %s from alias %s\n",
668 static int net_groupmap_listmem(int argc
, const char **argv
)
675 !string_to_sid(&alias
, argv
[0]) ) {
676 d_printf("Usage: net groupmap listmem alias-sid\n");
680 if (!pdb_enum_aliasmem(&alias
, &members
, &num
)) {
681 d_printf("Could not list members for sid %s\n", argv
[0]);
685 for (i
= 0; i
< num
; i
++) {
686 printf("%s\n", sid_string_static(&(members
[i
])));
694 static BOOL
print_alias_memberships(TALLOC_CTX
*mem_ctx
,
695 const DOM_SID
*domain_sid
,
696 const DOM_SID
*member
)
699 int i
, num_alias_rids
;
704 if (!pdb_enum_alias_memberships(mem_ctx
, domain_sid
, member
, 1,
705 &alias_rids
, &num_alias_rids
)) {
706 d_printf("Could not list memberships for sid %s\n",
707 sid_string_static(member
));
711 for (i
= 0; i
< num_alias_rids
; i
++) {
713 sid_copy(&alias
, domain_sid
);
714 sid_append_rid(&alias
, alias_rids
[i
]);
715 printf("%s\n", sid_string_static(&alias
));
721 static int net_groupmap_memberships(int argc
, const char **argv
)
724 DOM_SID
*domain_sid
, *builtin_sid
, member
;
727 !string_to_sid(&member
, argv
[0]) ) {
728 d_printf("Usage: net groupmap memberof sid\n");
732 mem_ctx
= talloc_init("net_groupmap_memberships");
733 if (mem_ctx
== NULL
) {
734 d_printf("talloc_init failed\n");
738 domain_sid
= get_global_sam_sid();
739 builtin_sid
= string_sid_talloc(mem_ctx
, "S-1-5-32");
740 if ((domain_sid
== NULL
) || (builtin_sid
== NULL
)) {
741 d_printf("Could not get domain sid\n");
745 if (!print_alias_memberships(mem_ctx
, domain_sid
, &member
) ||
746 !print_alias_memberships(mem_ctx
, builtin_sid
, &member
))
749 talloc_destroy(mem_ctx
);
754 int net_help_groupmap(int argc
, const char **argv
)
756 d_printf("net groupmap add"\
757 "\n Create a new group mapping\n");
758 d_printf("net groupmap modify"\
759 "\n Update a group mapping\n");
760 d_printf("net groupmap delete"\
761 "\n Remove a group mapping\n");
762 d_printf("net groupmap addmem"\
763 "\n Add a foreign alias member\n");
764 d_printf("net groupmap delmem"\
765 "\n Delete a foreign alias member\n");
766 d_printf("net groupmap listmem"\
767 "\n List foreign group members\n");
768 d_printf("net groupmap memberships"\
769 "\n List foreign group memberships\n");
770 d_printf("net groupmap list"\
771 "\n List current group map\n");
772 d_printf("net groupmap set"\
773 "\n Set group mapping\n");
774 d_printf("net groupmap cleanup"\
775 "\n Remove foreign group mapping entries\n");
781 /***********************************************************
782 migrated functionality from smbgroupedit
783 **********************************************************/
784 int net_groupmap(int argc
, const char **argv
)
786 struct functable func
[] = {
787 {"add", net_groupmap_add
},
788 {"modify", net_groupmap_modify
},
789 {"delete", net_groupmap_delete
},
790 {"set", net_groupmap_set
},
791 {"cleanup", net_groupmap_cleanup
},
792 {"addmem", net_groupmap_addmem
},
793 {"delmem", net_groupmap_delmem
},
794 {"listmem", net_groupmap_listmem
},
795 {"memberships", net_groupmap_memberships
},
796 {"list", net_groupmap_list
},
797 {"help", net_help_groupmap
},
801 /* we shouldn't have silly checks like this */
803 d_printf("You must be root to edit group mappings.\nExiting...\n");
808 return net_run_function(argc
, argv
, func
, net_help_groupmap
);
810 return net_help_groupmap( argc
, argv
);