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 3 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, see <http://www.gnu.org/licenses/>.
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
)
90 d_printf("%s (%s) -> %s\n", map
.nt_name
,
91 sid_string_static(&map
.sid
), gidtoname(map
.gid
));
93 d_printf("%s\n", map
.nt_name
);
94 d_printf("\tSID : %s\n", sid_string_static(&map
.sid
));
95 d_printf("\tUnix gid : %d\n", map
.gid
);
96 d_printf("\tUnix group: %s\n", gidtoname(map
.gid
));
97 d_printf("\tGroup type: %s\n",
98 sid_type_lookup(map
.sid_name_use
));
99 d_printf("\tComment : %s\n", map
.comment
);
103 /*********************************************************
105 **********************************************************/
106 static int net_groupmap_list(int argc
, const char **argv
)
109 BOOL long_list
= False
;
111 fstring ntgroup
= "";
112 fstring sid_string
= "";
114 if (opt_verbose
|| opt_long_list_entries
)
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_fprintf(stderr
, "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_fprintf(stderr
, "must supply a SID\n");
137 d_fprintf(stderr
, "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_fprintf(stderr
, "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(NULL
, 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 lsa_SidType sid_type
= SID_NAME_DOM_GRP
;
196 const char *name_type
;
200 /* Default is domain group. */
201 map
.sid_name_use
= SID_NAME_DOM_GRP
;
202 name_type
= "domain group";
204 /* get the options */
205 for ( i
=0; i
<argc
; i
++ ) {
206 if ( !StrnCaseCmp(argv
[i
], "rid", strlen("rid")) ) {
207 rid
= get_int_param(argv
[i
]);
208 if ( rid
< DOMAIN_GROUP_RID_ADMINS
) {
209 d_fprintf(stderr
, "RID must be greater than %d\n", (uint32
)DOMAIN_GROUP_RID_ADMINS
-1);
213 else if ( !StrnCaseCmp(argv
[i
], "unixgroup", strlen("unixgroup")) ) {
214 fstrcpy( unixgrp
, get_string_param( argv
[i
] ) );
216 d_fprintf(stderr
, "must supply a name\n");
220 else if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
221 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
223 d_fprintf(stderr
, "must supply a name\n");
227 else if ( !StrnCaseCmp(argv
[i
], "sid", strlen("sid")) ) {
228 fstrcpy( string_sid
, get_string_param( argv
[i
] ) );
229 if ( !string_sid
[0] ) {
230 d_fprintf(stderr
, "must supply a SID\n");
234 else if ( !StrnCaseCmp(argv
[i
], "comment", strlen("comment")) ) {
235 fstrcpy( ntcomment
, get_string_param( argv
[i
] ) );
236 if ( !ntcomment
[0] ) {
237 d_fprintf(stderr
, "must supply a comment string\n");
241 else if ( !StrnCaseCmp(argv
[i
], "type", strlen("type")) ) {
242 fstrcpy( type
, get_string_param( argv
[i
] ) );
246 sid_type
= SID_NAME_WKN_GRP
;
247 name_type
= "wellknown group";
251 sid_type
= SID_NAME_DOM_GRP
;
252 name_type
= "domain group";
256 sid_type
= SID_NAME_ALIAS
;
257 name_type
= "alias (local) group";
260 d_fprintf(stderr
, "unknown group type %s\n", type
);
265 d_fprintf(stderr
, "Bad option: %s\n", argv
[i
]);
271 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
275 if ( (gid
= nametogid(unixgrp
)) == (gid_t
)-1 ) {
276 d_fprintf(stderr
, "Can't lookup UNIX group %s\n", unixgrp
);
281 if (pdb_getgrgid(&map
, gid
)) {
282 d_printf("Unix group %s already mapped to SID %s\n",
283 unixgrp
, sid_string_static(&map
.sid
));
288 if ( (rid
== 0) && (string_sid
[0] == '\0') ) {
289 d_printf("No rid or sid specified, choosing a RID\n");
290 if (pdb_rid_algorithm()) {
291 rid
= algorithmic_pdb_gid_to_group_rid(gid
);
293 if (!pdb_new_rid(&rid
)) {
294 d_printf("Could not get new RID\n");
297 d_printf("Got RID %d\n", rid
);
300 /* append the rid to our own domain/machine SID if we don't have a full SID */
301 if ( !string_sid
[0] ) {
302 sid_copy(&sid
, get_global_sam_sid());
303 sid_append_rid(&sid
, rid
);
304 sid_to_string(string_sid
, &sid
);
309 case SID_NAME_WKN_GRP
:
310 fstrcpy(ntcomment
, "Wellknown Unix group");
312 case SID_NAME_DOM_GRP
:
313 fstrcpy(ntcomment
, "Domain Unix group");
316 fstrcpy(ntcomment
, "Local Unix group");
319 fstrcpy(ntcomment
, "Unix group");
325 fstrcpy( ntgroup
, unixgrp
);
328 if (!NT_STATUS_IS_OK(add_initial_entry(gid
, string_sid
, sid_type
, ntgroup
, ntcomment
))) {
329 d_fprintf(stderr
, "adding entry for group %s failed!\n", ntgroup
);
333 d_printf("Successfully added group %s to the mapping db as a %s\n",
338 static int net_groupmap_modify(int argc
, const char **argv
)
342 fstring ntcomment
= "";
344 fstring ntgroup
= "";
345 fstring unixgrp
= "";
346 fstring sid_string
= "";
347 enum lsa_SidType sid_type
= SID_NAME_UNKNOWN
;
351 /* get the options */
352 for ( i
=0; i
<argc
; i
++ ) {
353 if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
354 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
356 d_fprintf(stderr
, "must supply a name\n");
360 else if ( !StrnCaseCmp(argv
[i
], "sid", strlen("sid")) ) {
361 fstrcpy( sid_string
, get_string_param( argv
[i
] ) );
362 if ( !sid_string
[0] ) {
363 d_fprintf(stderr
, "must supply a name\n");
367 else if ( !StrnCaseCmp(argv
[i
], "comment", strlen("comment")) ) {
368 fstrcpy( ntcomment
, get_string_param( argv
[i
] ) );
369 if ( !ntcomment
[0] ) {
370 d_fprintf(stderr
, "must supply a comment string\n");
374 else if ( !StrnCaseCmp(argv
[i
], "unixgroup", strlen("unixgroup")) ) {
375 fstrcpy( unixgrp
, get_string_param( argv
[i
] ) );
377 d_fprintf(stderr
, "must supply a group name\n");
381 else if ( !StrnCaseCmp(argv
[i
], "type", strlen("type")) ) {
382 fstrcpy( type
, get_string_param( argv
[i
] ) );
386 sid_type
= SID_NAME_DOM_GRP
;
390 sid_type
= SID_NAME_ALIAS
;
395 d_fprintf(stderr
, "Bad option: %s\n", argv
[i
]);
400 if ( !ntgroup
[0] && !sid_string
[0] ) {
401 d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
405 /* give preference to the SID; if both the ntgroup name and SID
406 are defined, use the SID and assume that the group name could be a
409 if ( sid_string
[0] ) {
410 if (!get_sid_from_input(&sid
, sid_string
)) {
415 if (!get_sid_from_input(&sid
, ntgroup
)) {
420 /* Get the current mapping from the database */
421 if(!pdb_getgrsid(&map
, sid
)) {
422 d_fprintf(stderr
, "Failure to local group SID in the database\n");
427 * Allow changing of group type only between domain and local
428 * We disallow changing Builtin groups !!! (SID problem)
430 if (sid_type
== SID_NAME_UNKNOWN
) {
431 d_fprintf(stderr
, "Can't map to an unknown group type.\n");
435 if (map
.sid_name_use
== SID_NAME_WKN_GRP
) {
436 d_fprintf(stderr
, "You can only change between domain and local groups.\n");
440 map
.sid_name_use
=sid_type
;
442 /* Change comment if new one */
444 fstrcpy( map
.comment
, ntcomment
);
447 fstrcpy( map
.nt_name
, ntgroup
);
450 gid
= nametogid( unixgrp
);
452 d_fprintf(stderr
, "Unable to lookup UNIX group %s. Make sure the group exists.\n",
460 if ( !NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map
)) ) {
461 d_fprintf(stderr
, "Could not update group database\n");
465 d_printf("Updated mapping entry for %s\n", map
.nt_name
);
470 static int net_groupmap_delete(int argc
, const char **argv
)
473 fstring ntgroup
= "";
474 fstring sid_string
= "";
477 /* get the options */
478 for ( i
=0; i
<argc
; i
++ ) {
479 if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
480 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
482 d_fprintf(stderr
, "must supply a name\n");
486 else if ( !StrnCaseCmp(argv
[i
], "sid", strlen("sid")) ) {
487 fstrcpy( sid_string
, get_string_param( argv
[i
] ) );
488 if ( !sid_string
[0] ) {
489 d_fprintf(stderr
, "must supply a SID\n");
494 d_fprintf(stderr
, "Bad option: %s\n", argv
[i
]);
499 if ( !ntgroup
[0] && !sid_string
[0]) {
500 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
504 /* give preference to the SID if we have that */
507 fstrcpy( ntgroup
, sid_string
);
509 if ( !get_sid_from_input(&sid
, ntgroup
) ) {
510 d_fprintf(stderr
, "Unable to resolve group %s to a SID\n", ntgroup
);
514 if ( !NT_STATUS_IS_OK(pdb_delete_group_mapping_entry(sid
)) ) {
515 d_fprintf(stderr
, "Failed to removing group %s from the mapping db!\n", ntgroup
);
519 d_printf("Sucessfully removed %s from the mapping db\n", ntgroup
);
524 static int net_groupmap_set(int argc
, const char **argv
)
526 const char *ntgroup
= NULL
;
527 struct group
*grp
= NULL
;
529 BOOL have_map
= False
;
531 if ((argc
< 1) || (argc
> 2)) {
532 d_printf("Usage: net groupmap set \"NT Group\" "
533 "[\"unix group\"] [-C \"comment\"] [-L] [-D]\n");
537 if ( opt_localgroup
&& opt_domaingroup
) {
538 d_printf("Can only specify -L or -D, not both\n");
545 grp
= getgrnam(argv
[1]);
548 d_fprintf(stderr
, "Could not find unix group %s\n", argv
[1]);
553 have_map
= pdb_getgrnam(&map
, ntgroup
);
557 have_map
= ( (strncmp(ntgroup
, "S-", 2) == 0) &&
558 string_to_sid(&sid
, ntgroup
) &&
559 pdb_getgrsid(&map
, sid
) );
567 d_fprintf(stderr
, "Could not find group mapping for %s\n",
572 map
.gid
= grp
->gr_gid
;
575 if ( pdb_rid_algorithm() )
576 opt_rid
= algorithmic_pdb_gid_to_group_rid(map
.gid
);
578 if ( !pdb_new_rid((uint32
*)&opt_rid
) ) {
579 d_fprintf( stderr
, "Could not allocate new RID\n");
585 sid_copy(&map
.sid
, get_global_sam_sid());
586 sid_append_rid(&map
.sid
, opt_rid
);
588 map
.sid_name_use
= SID_NAME_DOM_GRP
;
589 fstrcpy(map
.nt_name
, ntgroup
);
590 fstrcpy(map
.comment
, "");
592 if (!NT_STATUS_IS_OK(pdb_add_group_mapping_entry(&map
))) {
593 d_fprintf(stderr
, "Could not add mapping entry for %s\n",
599 /* Now we have a mapping entry, update that stuff */
601 if ( opt_localgroup
|| opt_domaingroup
) {
602 if (map
.sid_name_use
== SID_NAME_WKN_GRP
) {
603 d_fprintf(stderr
, "Can't change type of the BUILTIN group %s\n",
610 map
.sid_name_use
= SID_NAME_ALIAS
;
613 map
.sid_name_use
= SID_NAME_DOM_GRP
;
615 /* The case (opt_domaingroup && opt_localgroup) was tested for above */
617 if (strlen(opt_comment
) > 0)
618 fstrcpy(map
.comment
, opt_comment
);
620 if (strlen(opt_newntname
) > 0)
621 fstrcpy(map
.nt_name
, opt_newntname
);
624 map
.gid
= grp
->gr_gid
;
626 if (!NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map
))) {
627 d_fprintf(stderr
, "Could not update group mapping for %s\n", ntgroup
);
634 static int net_groupmap_cleanup(int argc
, const char **argv
)
636 GROUP_MAP
*map
= NULL
;
639 if (!pdb_enum_group_mapping(NULL
, SID_NAME_UNKNOWN
, &map
, &entries
,
641 d_fprintf(stderr
, "Could not list group mappings\n");
645 for (i
=0; i
<entries
; i
++) {
647 if (map
[i
].gid
== -1)
648 printf("Group %s is not mapped\n", map
[i
].nt_name
);
650 if (!sid_check_is_in_our_domain(&map
[i
].sid
)) {
651 printf("Deleting mapping for NT Group %s, sid %s\n",
653 sid_string_static(&map
[i
].sid
));
654 pdb_delete_group_mapping_entry(map
[i
].sid
);
663 static int net_groupmap_addmem(int argc
, const char **argv
)
665 DOM_SID alias
, member
;
668 !string_to_sid(&alias
, argv
[0]) ||
669 !string_to_sid(&member
, argv
[1]) ) {
670 d_printf("Usage: net groupmap addmem alias-sid member-sid\n");
674 if (!NT_STATUS_IS_OK(pdb_add_aliasmem(&alias
, &member
))) {
675 d_fprintf(stderr
, "Could not add sid %s to alias %s\n",
683 static int net_groupmap_delmem(int argc
, const char **argv
)
685 DOM_SID alias
, member
;
688 !string_to_sid(&alias
, argv
[0]) ||
689 !string_to_sid(&member
, argv
[1]) ) {
690 d_printf("Usage: net groupmap delmem alias-sid member-sid\n");
694 if (!NT_STATUS_IS_OK(pdb_del_aliasmem(&alias
, &member
))) {
695 d_fprintf(stderr
, "Could not delete sid %s from alias %s\n",
703 static int net_groupmap_listmem(int argc
, const char **argv
)
710 !string_to_sid(&alias
, argv
[0]) ) {
711 d_printf("Usage: net groupmap listmem alias-sid\n");
718 if (!NT_STATUS_IS_OK(pdb_enum_aliasmem(&alias
, &members
, &num
))) {
719 d_fprintf(stderr
, "Could not list members for sid %s\n", argv
[0]);
723 for (i
= 0; i
< num
; i
++) {
724 printf("%s\n", sid_string_static(&(members
[i
])));
727 TALLOC_FREE(members
);
732 static BOOL
print_alias_memberships(TALLOC_CTX
*mem_ctx
,
733 const DOM_SID
*domain_sid
,
734 const DOM_SID
*member
)
737 size_t i
, num_alias_rids
;
742 if (!NT_STATUS_IS_OK(pdb_enum_alias_memberships(
743 mem_ctx
, domain_sid
, member
, 1,
744 &alias_rids
, &num_alias_rids
))) {
745 d_fprintf(stderr
, "Could not list memberships for sid %s\n",
746 sid_string_static(member
));
750 for (i
= 0; i
< num_alias_rids
; i
++) {
752 sid_copy(&alias
, domain_sid
);
753 sid_append_rid(&alias
, alias_rids
[i
]);
754 printf("%s\n", sid_string_static(&alias
));
760 static int net_groupmap_memberships(int argc
, const char **argv
)
763 DOM_SID
*domain_sid
, *builtin_sid
, member
;
766 !string_to_sid(&member
, argv
[0]) ) {
767 d_printf("Usage: net groupmap memberof sid\n");
771 mem_ctx
= talloc_init("net_groupmap_memberships");
772 if (mem_ctx
== NULL
) {
773 d_fprintf(stderr
, "talloc_init failed\n");
777 domain_sid
= get_global_sam_sid();
778 builtin_sid
= string_sid_talloc(mem_ctx
, "S-1-5-32");
779 if ((domain_sid
== NULL
) || (builtin_sid
== NULL
)) {
780 d_fprintf(stderr
, "Could not get domain sid\n");
784 if (!print_alias_memberships(mem_ctx
, domain_sid
, &member
) ||
785 !print_alias_memberships(mem_ctx
, builtin_sid
, &member
))
788 talloc_destroy(mem_ctx
);
793 int net_help_groupmap(int argc
, const char **argv
)
795 d_printf("net groupmap add"\
796 "\n Create a new group mapping\n");
797 d_printf("net groupmap modify"\
798 "\n Update a group mapping\n");
799 d_printf("net groupmap delete"\
800 "\n Remove a group mapping\n");
801 d_printf("net groupmap addmem"\
802 "\n Add a foreign alias member\n");
803 d_printf("net groupmap delmem"\
804 "\n Delete a foreign alias member\n");
805 d_printf("net groupmap listmem"\
806 "\n List foreign group members\n");
807 d_printf("net groupmap memberships"\
808 "\n List foreign group memberships\n");
809 d_printf("net groupmap list"\
810 "\n List current group map\n");
811 d_printf("net groupmap set"\
812 "\n Set group mapping\n");
813 d_printf("net groupmap cleanup"\
814 "\n Remove foreign group mapping entries\n");
820 /***********************************************************
821 migrated functionality from smbgroupedit
822 **********************************************************/
823 int net_groupmap(int argc
, const char **argv
)
825 struct functable func
[] = {
826 {"add", net_groupmap_add
},
827 {"modify", net_groupmap_modify
},
828 {"delete", net_groupmap_delete
},
829 {"set", net_groupmap_set
},
830 {"cleanup", net_groupmap_cleanup
},
831 {"addmem", net_groupmap_addmem
},
832 {"delmem", net_groupmap_delmem
},
833 {"listmem", net_groupmap_listmem
},
834 {"memberships", net_groupmap_memberships
},
835 {"list", net_groupmap_list
},
836 {"help", net_help_groupmap
},
840 /* we shouldn't have silly checks like this */
842 d_fprintf(stderr
, "You must be root to edit group mappings.\n");
847 return net_run_function(argc
, argv
, func
, net_help_groupmap
);
849 return net_help_groupmap( argc
, argv
);