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.
26 #include "utils/net.h"
29 /*********************************************************
30 utility function to parse an integer parameter from
32 **********************************************************/
33 static uint32
get_int_param( const char* param
)
37 p
= strchr( param
, '=' );
44 /*********************************************************
45 utility function to parse an integer parameter from
47 **********************************************************/
48 static char* get_string_param( const char* param
)
52 p
= strchr( param
, '=' );
59 /*********************************************************
60 Figure out if the input was an NT group or a SID string.
62 **********************************************************/
63 static BOOL
get_sid_from_input(DOM_SID
*sid
, char *input
)
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
);
76 if (!string_to_sid(sid
, input
)) {
77 printf("converting sid %s from a string failed!\n", input
);
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
)
93 decode_sid_name_use(group_type
, map
.sid_name_use
);
94 sid_to_string(string_sid
, &map
.sid
);
97 d_printf("%s (%s) -> %s\n", map
.nt_name
, string_sid
, gidtoname(map
.gid
));
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 /*********************************************************
109 **********************************************************/
110 static int net_groupmap_list(int argc
, const char **argv
)
113 BOOL long_list
= False
;
115 fstring ntgroup
= "";
116 fstring sid_string
= "";
118 if (opt_verbose
|| opt_long_list_entries
)
121 /* get the options */
122 for ( i
=0; i
<argc
; i
++ ) {
123 if ( !StrCaseCmp(argv
[i
], "verbose")) {
126 else if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
127 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
129 d_printf("must supply a name\n");
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");
141 d_printf("Bad option: %s\n", argv
[i
]);
146 /* list a single group is given a name */
147 if ( ntgroup
[0] || sid_string
[0] ) {
152 fstrcpy( ntgroup
, sid_string
);
154 if (!get_sid_from_input(&sid
, ntgroup
)) {
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");
164 print_map_entry( map
, long_list
);
168 /* enumerate all group mappings */
169 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN
, &map
, &entries
, ENUM_ALL_MAPPED
))
172 for (i
=0; i
<entries
; i
++) {
173 print_map_entry( map
[i
], long_list
);
182 /*********************************************************
183 Add a new group mapping entry
184 **********************************************************/
186 static int net_groupmap_add(int argc
, const char **argv
)
189 fstring ntgroup
= "";
190 fstring unixgrp
= "";
191 fstring string_sid
= "";
193 fstring ntcomment
= "";
194 enum SID_NAME_USE sid_type
= SID_NAME_DOM_GRP
;
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);
208 else if ( !StrnCaseCmp(argv
[i
], "unixgroup", strlen("unixgroup")) ) {
209 fstrcpy( unixgrp
, get_string_param( argv
[i
] ) );
211 d_printf("must supply a name\n");
215 else if ( !StrnCaseCmp(argv
[i
], "ntgroup", strlen("ntgroup")) ) {
216 fstrcpy( ntgroup
, get_string_param( argv
[i
] ) );
218 d_printf("must supply a name\n");
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");
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");
236 else if ( !StrnCaseCmp(argv
[i
], "type", strlen("type")) ) {
237 fstrcpy( type
, get_string_param( argv
[i
] ) );
241 sid_type
= SID_NAME_WKN_GRP
;
245 sid_type
= SID_NAME_DOM_GRP
;
249 sid_type
= SID_NAME_ALIAS
;
254 d_printf("Bad option: %s\n", argv
[i
]);
260 d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
264 if ( (gid
= nametogid(unixgrp
)) == (gid_t
)-1 ) {
265 d_printf("Can't lookup UNIX group %s\n", unixgrp
);
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
);
283 case SID_NAME_WKN_GRP
:
284 fstrcpy(ntcomment
, "Wellknown Unix group");
286 case SID_NAME_DOM_GRP
:
287 fstrcpy(ntcomment
, "Domain Unix group");
290 fstrcpy(ntcomment
, "Local Unix group");
293 fstrcpy(ntcomment
, "Unix group");
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
);
307 d_printf("Successfully added group %s to the mapping db\n", ntgroup
);
311 static int net_groupmap_modify(int argc
, const char **argv
)
315 fstring ntcomment
= "";
317 fstring ntgroup
= "";
318 fstring unixgrp
= "";
319 fstring sid_string
= "";
320 enum SID_NAME_USE sid_type
= SID_NAME_UNKNOWN
;
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
] ) );
329 d_printf("must supply a name\n");
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");
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");
347 else if ( !StrnCaseCmp(argv
[i
], "unixgroup", strlen("unixgroup")) ) {
348 fstrcpy( unixgrp
, get_string_param( argv
[i
] ) );
350 d_printf("must supply a group name\n");
354 else if ( !StrnCaseCmp(argv
[i
], "type", strlen("type")) ) {
355 fstrcpy( type
, get_string_param( argv
[i
] ) );
359 sid_type
= SID_NAME_DOM_GRP
;
363 sid_type
= SID_NAME_ALIAS
;
368 d_printf("Bad option: %s\n", argv
[i
]);
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");
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
382 if ( sid_string
[0] ) {
383 if (!get_sid_from_input(&sid
, sid_string
)) {
388 if (!get_sid_from_input(&sid
, ntgroup
)) {
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");
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");
409 map
.sid_name_use
=sid_type
;
412 /* Change comment if new one */
414 fstrcpy( map
.comment
, ntcomment
);
417 fstrcpy( map
.nt_name
, ntgroup
);
420 gid
= nametogid( unixgrp
);
422 d_printf("Unable to lookup UNIX group %s. Make sure the group exists.\n",
430 if ( !pdb_update_group_mapping_entry(&map
) ) {
431 d_printf("Could not update group database\n");
435 d_printf("Updated mapping entry for %s\n", map
.nt_name
);
440 static int net_groupmap_delete(int argc
, const char **argv
)
443 fstring ntgroup
= "";
444 fstring sid_string
= "";
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
] ) );
452 d_printf("must supply a name\n");
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");
464 d_printf("Bad option: %s\n", argv
[i
]);
469 if ( !ntgroup
[0] && !sid_string
[0]) {
470 d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
474 /* give preference to the SID if we have that */
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
);
484 if ( !pdb_delete_group_mapping_entry(sid
) ) {
485 printf("Failed to removing group %s from the mapping db!\n", ntgroup
);
489 d_printf("Sucessfully removed %s from the mapping db\n", ntgroup
);
494 static int net_groupmap_set(int argc
, const char **argv
)
496 const char *ntgroup
= NULL
;
497 struct group
*grp
= NULL
;
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");
507 if ( opt_localgroup
&& opt_domaingroup
) {
508 d_printf("Can only specify -L or -D, not both\n");
515 grp
= getgrnam(argv
[1]);
518 d_printf("Could not find unix group %s\n", argv
[1]);
523 have_map
= pdb_getgrnam(&map
, ntgroup
);
527 have_map
= ( (strncmp(ntgroup
, "S-", 2) == 0) &&
528 string_to_sid(&sid
, ntgroup
) &&
529 pdb_getgrsid(&map
, sid
) );
537 d_printf("Could not find group mapping for %s\n",
542 map
.gid
= grp
->gr_gid
;
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",
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",
573 map
.sid_name_use
= SID_NAME_ALIAS
;
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
);
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
);
597 static int net_groupmap_cleanup(int argc
, const char **argv
)
599 GROUP_MAP
*map
= NULL
;
602 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN
, &map
, &entries
,
604 d_printf("Could not list group mappings\n");
608 for (i
=0; i
<entries
; i
++) {
610 if (map
[i
].sid_name_use
== SID_NAME_WKN_GRP
)
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",
619 sid_string_static(&map
[i
].sid
));
620 pdb_delete_group_mapping_entry(map
[i
].sid
);
629 static int net_groupmap_addmem(int argc
, const char **argv
)
631 DOM_SID alias
, member
;
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");
640 if (!pdb_add_aliasmem(&alias
, &member
)) {
641 d_printf("Could not add sid %s to alias %s\n",
649 static int net_groupmap_delmem(int argc
, const char **argv
)
651 DOM_SID alias
, member
;
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");
660 if (!pdb_del_aliasmem(&alias
, &member
)) {
661 d_printf("Could not delete sid %s from alias %s\n",
669 static int net_groupmap_listmem(int argc
, const char **argv
)
676 !string_to_sid(&alias
, argv
[0]) ) {
677 d_printf("Usage: net groupmap listmem alias-sid\n");
681 if (!pdb_enum_aliasmem(&alias
, &members
, &num
)) {
682 d_printf("Could not list members for sid %s\n", argv
[0]);
686 for (i
= 0; i
< num
; i
++) {
687 printf("%s\n", sid_string_static(&(members
[i
])));
695 static BOOL
print_alias_memberships(TALLOC_CTX
*mem_ctx
,
696 const DOM_SID
*domain_sid
,
697 const DOM_SID
*member
)
700 int i
, num_alias_rids
;
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
));
712 for (i
= 0; i
< num_alias_rids
; i
++) {
714 sid_copy(&alias
, domain_sid
);
715 sid_append_rid(&alias
, alias_rids
[i
]);
716 printf("%s\n", sid_string_static(&alias
));
722 static int net_groupmap_memberships(int argc
, const char **argv
)
725 DOM_SID
*domain_sid
, *builtin_sid
, member
;
728 !string_to_sid(&member
, argv
[0]) ) {
729 d_printf("Usage: net groupmap memberof sid\n");
733 mem_ctx
= talloc_init("net_groupmap_memberships");
734 if (mem_ctx
== NULL
) {
735 d_printf("talloc_init failed\n");
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");
746 if (!print_alias_memberships(mem_ctx
, domain_sid
, &member
) ||
747 !print_alias_memberships(mem_ctx
, builtin_sid
, &member
))
750 talloc_destroy(mem_ctx
);
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");
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
},
802 /* we shouldn't have silly checks like this */
804 d_printf("You must be root to edit group mappings.\nExiting...\n");
809 return net_run_function(argc
, argv
, func
, net_help_groupmap
);
811 return net_help_groupmap( argc
, argv
);