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"
27 /*********************************************************
28 Figure out if the input was an NT group or a SID string.
30 **********************************************************/
31 static bool get_sid_from_input(DOM_SID *sid, char *input)
35 if (StrnCaseCmp( input, "S-", 2)) {
36 /* Perhaps its the NT group name? */
37 if (!pdb_getgrnam(&map, input)) {
38 printf("NT Group %s doesn't exist in mapping DB\n", input);
44 if (!string_to_sid(sid, input)) {
45 printf("converting sid %s from a string failed!\n", input);
52 /*********************************************************
53 Dump a GROUP_MAP entry to stdout (long or short listing)
54 **********************************************************/
56 static void print_map_entry ( GROUP_MAP map, bool long_list )
59 d_printf("%s (%s) -> %s\n", map.nt_name,
60 sid_string_tos(&map.sid), gidtoname(map.gid));
62 d_printf("%s\n", map.nt_name);
63 d_printf("\tSID : %s\n", sid_string_tos(&map.sid));
64 d_printf("\tUnix gid : %d\n", map.gid);
65 d_printf("\tUnix group: %s\n", gidtoname(map.gid));
66 d_printf("\tGroup type: %s\n",
67 sid_type_lookup(map.sid_name_use));
68 d_printf("\tComment : %s\n", map.comment);
72 /*********************************************************
74 **********************************************************/
75 static int net_groupmap_list(struct net_context *c, int argc, const char **argv)
78 bool long_list = false;
81 fstring sid_string = "";
82 const char list_usage_str[] = "net groupmap list [verbose] "
83 "[ntgroup=NT group] [sid=SID]\n"
84 " verbose\tPrint verbose list\n"
85 " ntgroup\tNT group to list\n"
86 " sid\tSID of group to list";
88 if (c->display_usage) {
89 d_printf("Usage:\n%s\n", list_usage_str);
93 if (c->opt_verbose || c->opt_long_list_entries)
97 for ( i=0; i<argc; i++ ) {
98 if ( !StrCaseCmp(argv[i], "verbose")) {
101 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
102 fstrcpy( ntgroup, get_string_param( argv[i] ) );
104 d_fprintf(stderr, "must supply a name\n");
108 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
109 fstrcpy( sid_string, get_string_param( argv[i] ) );
110 if ( !sid_string[0] ) {
111 d_fprintf(stderr, "must supply a SID\n");
116 d_fprintf(stderr, "Bad option: %s\n", argv[i]);
117 d_printf("Usage:\n%s\n", list_usage_str);
122 /* list a single group is given a name */
123 if ( ntgroup[0] || sid_string[0] ) {
128 fstrcpy( ntgroup, sid_string);
130 if (!get_sid_from_input(&sid, ntgroup)) {
134 /* Get the current mapping from the database */
135 if(!pdb_getgrsid(&map, sid)) {
136 d_fprintf(stderr, "Failure to local group SID in the database\n");
140 print_map_entry( map, long_list );
144 /* enumerate all group mappings */
145 if (!pdb_enum_group_mapping(NULL, SID_NAME_UNKNOWN, &map, &entries, ENUM_ALL_MAPPED))
148 for (i=0; i<entries; i++) {
149 print_map_entry( map[i], long_list );
158 /*********************************************************
159 Add a new group mapping entry
160 **********************************************************/
162 static int net_groupmap_add(struct net_context *c, int argc, const char **argv)
165 fstring ntgroup = "";
166 fstring unixgrp = "";
167 fstring string_sid = "";
169 fstring ntcomment = "";
170 enum lsa_SidType sid_type = SID_NAME_DOM_GRP;
176 const char *name_type;
177 const char add_usage_str[] = "net groupmap add {rid=<int>|sid=<string>}"
178 " unixgroup=<string> "
179 "[type=<domain|local|builtin>] "
180 "[ntgroup=<string>] [comment=<string>]";
184 /* Default is domain group. */
185 map.sid_name_use = SID_NAME_DOM_GRP;
186 name_type = "domain group";
188 if (c->display_usage) {
189 d_printf("Usage\n%s\n", add_usage_str);
193 /* get the options */
194 for ( i=0; i<argc; i++ ) {
195 if ( !StrnCaseCmp(argv[i], "rid", strlen("rid")) ) {
196 rid = get_int_param(argv[i]);
197 if ( rid < DOMAIN_GROUP_RID_ADMINS ) {
198 d_fprintf(stderr, "RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
202 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
203 fstrcpy( unixgrp, get_string_param( argv[i] ) );
205 d_fprintf(stderr, "must supply a name\n");
209 else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
210 fstrcpy( ntgroup, get_string_param( argv[i] ) );
212 d_fprintf(stderr, "must supply a name\n");
216 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
217 fstrcpy( string_sid, get_string_param( argv[i] ) );
218 if ( !string_sid[0] ) {
219 d_fprintf(stderr, "must supply a SID\n");
223 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
224 fstrcpy( ntcomment, get_string_param( argv[i] ) );
225 if ( !ntcomment[0] ) {
226 d_fprintf(stderr, "must supply a comment string\n");
230 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
231 fstrcpy( type, get_string_param( argv[i] ) );
235 sid_type = SID_NAME_WKN_GRP;
236 name_type = "wellknown group";
240 sid_type = SID_NAME_DOM_GRP;
241 name_type = "domain group";
245 sid_type = SID_NAME_ALIAS;
246 name_type = "alias (local) group";
249 d_fprintf(stderr, "unknown group type %s\n", type);
254 d_fprintf(stderr, "Bad option: %s\n", argv[i]);
260 d_printf("Usage:\n%s\n", add_usage_str);
264 if ( (gid = nametogid(unixgrp)) == (gid_t)-1 ) {
265 d_fprintf(stderr, "Can't lookup UNIX group %s\n", unixgrp);
270 if (pdb_getgrgid(&map, gid)) {
271 d_printf("Unix group %s already mapped to SID %s\n",
272 unixgrp, sid_string_tos(&map.sid));
277 if ( (rid == 0) && (string_sid[0] == '\0') ) {
278 d_printf("No rid or sid specified, choosing a RID\n");
279 if (pdb_rid_algorithm()) {
280 rid = algorithmic_pdb_gid_to_group_rid(gid);
282 if (!pdb_new_rid(&rid)) {
283 d_printf("Could not get new RID\n");
286 d_printf("Got RID %d\n", rid);
289 /* append the rid to our own domain/machine SID if we don't have a full SID */
290 if ( !string_sid[0] ) {
291 sid_copy(&sid, get_global_sam_sid());
292 sid_append_rid(&sid, rid);
293 sid_to_fstring(string_sid, &sid);
298 case SID_NAME_WKN_GRP:
299 fstrcpy(ntcomment, "Wellknown Unix group");
301 case SID_NAME_DOM_GRP:
302 fstrcpy(ntcomment, "Domain Unix group");
305 fstrcpy(ntcomment, "Local Unix group");
308 fstrcpy(ntcomment, "Unix group");
314 fstrcpy( ntgroup, unixgrp );
316 if (!NT_STATUS_IS_OK(add_initial_entry(gid, string_sid, sid_type, ntgroup, ntcomment))) {
317 d_fprintf(stderr, "adding entry for group %s failed!\n", ntgroup);
321 d_printf("Successfully added group %s to the mapping db as a %s\n",
326 static int net_groupmap_modify(struct net_context *c, int argc, const char **argv)
330 fstring ntcomment = "";
332 fstring ntgroup = "";
333 fstring unixgrp = "";
334 fstring sid_string = "";
335 enum lsa_SidType sid_type = SID_NAME_UNKNOWN;
338 const char modify_usage_str[] = "net groupmap modify "
339 "{ntgroup=<string>|sid=<SID>} "
340 "[comment=<string>] "
341 "[unixgroup=<string>] "
342 "[type=<domain|local>]";
344 if (c->display_usage) {
345 d_printf("Usage:\n%s\n", modify_usage_str);
349 /* get the options */
350 for ( i=0; i<argc; i++ ) {
351 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
352 fstrcpy( ntgroup, get_string_param( argv[i] ) );
354 d_fprintf(stderr, "must supply a name\n");
358 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
359 fstrcpy( sid_string, get_string_param( argv[i] ) );
360 if ( !sid_string[0] ) {
361 d_fprintf(stderr, "must supply a name\n");
365 else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
366 fstrcpy( ntcomment, get_string_param( argv[i] ) );
367 if ( !ntcomment[0] ) {
368 d_fprintf(stderr, "must supply a comment string\n");
372 else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
373 fstrcpy( unixgrp, get_string_param( argv[i] ) );
375 d_fprintf(stderr, "must supply a group name\n");
379 else if ( !StrnCaseCmp(argv[i], "type", strlen("type")) ) {
380 fstrcpy( type, get_string_param( argv[i] ) );
384 sid_type = SID_NAME_DOM_GRP;
388 sid_type = SID_NAME_ALIAS;
393 d_fprintf(stderr, "Bad option: %s\n", argv[i]);
398 if ( !ntgroup[0] && !sid_string[0] ) {
399 d_printf("Usage:\n%s\n", modify_usage_str);
403 /* give preference to the SID; if both the ntgroup name and SID
404 are defined, use the SID and assume that the group name could be a
407 if ( sid_string[0] ) {
408 if (!get_sid_from_input(&sid, sid_string)) {
413 if (!get_sid_from_input(&sid, ntgroup)) {
418 /* Get the current mapping from the database */
419 if(!pdb_getgrsid(&map, sid)) {
420 d_fprintf(stderr, "Failure to local group SID in the database\n");
425 * Allow changing of group type only between domain and local
426 * We disallow changing Builtin groups !!! (SID problem)
428 if (sid_type == SID_NAME_UNKNOWN) {
429 d_fprintf(stderr, "Can't map to an unknown group type.\n");
433 if (map.sid_name_use == SID_NAME_WKN_GRP) {
434 d_fprintf(stderr, "You can only change between domain and local groups.\n");
438 map.sid_name_use=sid_type;
440 /* Change comment if new one */
442 fstrcpy( map.comment, ntcomment );
445 fstrcpy( map.nt_name, ntgroup );
448 gid = nametogid( unixgrp );
450 d_fprintf(stderr, "Unable to lookup UNIX group %s. Make sure the group exists.\n",
458 if ( !NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map)) ) {
459 d_fprintf(stderr, "Could not update group database\n");
463 d_printf("Updated mapping entry for %s\n", map.nt_name);
468 static int net_groupmap_delete(struct net_context *c, int argc, const char **argv)
471 fstring ntgroup = "";
472 fstring sid_string = "";
474 const char delete_usage_str[] = "net groupmap delete "
475 "{ntgroup=<string>|sid=<SID>}";
477 if (c->display_usage) {
478 d_printf("Usage:\n%s\n", delete_usage_str);
482 /* get the options */
483 for ( i=0; i<argc; i++ ) {
484 if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
485 fstrcpy( ntgroup, get_string_param( argv[i] ) );
487 d_fprintf(stderr, "must supply a name\n");
491 else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
492 fstrcpy( sid_string, get_string_param( argv[i] ) );
493 if ( !sid_string[0] ) {
494 d_fprintf(stderr, "must supply a SID\n");
499 d_fprintf(stderr, "Bad option: %s\n", argv[i]);
504 if ( !ntgroup[0] && !sid_string[0]) {
505 d_printf("Usage:\n%s\n", delete_usage_str);
509 /* give preference to the SID if we have that */
512 fstrcpy( ntgroup, sid_string );
514 if ( !get_sid_from_input(&sid, ntgroup) ) {
515 d_fprintf(stderr, "Unable to resolve group %s to a SID\n", ntgroup);
519 if ( !NT_STATUS_IS_OK(pdb_delete_group_mapping_entry(sid)) ) {
520 d_fprintf(stderr, "Failed to removing group %s from the mapping db!\n", ntgroup);
524 d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);
529 static int net_groupmap_set(struct net_context *c, int argc, const char **argv)
531 const char *ntgroup = NULL;
532 struct group *grp = NULL;
534 bool have_map = false;
536 if ((argc < 1) || (argc > 2) || c->display_usage) {
537 d_printf("Usage: net groupmap set \"NT Group\" "
538 "[\"unix group\"] [-C \"comment\"] [-L] [-D]\n");
542 if ( c->opt_localgroup && c->opt_domaingroup ) {
543 d_printf("Can only specify -L or -D, not both\n");
550 grp = getgrnam(argv[1]);
553 d_fprintf(stderr, "Could not find unix group %s\n", argv[1]);
558 have_map = pdb_getgrnam(&map, ntgroup);
562 have_map = ( (strncmp(ntgroup, "S-", 2) == 0) &&
563 string_to_sid(&sid, ntgroup) &&
564 pdb_getgrsid(&map, sid) );
572 d_fprintf(stderr, "Could not find group mapping for %s\n",
577 map.gid = grp->gr_gid;
579 if (c->opt_rid == 0) {
580 if ( pdb_rid_algorithm() )
581 c->opt_rid = algorithmic_pdb_gid_to_group_rid(map.gid);
583 if ( !pdb_new_rid((uint32*)&c->opt_rid) ) {
584 d_fprintf( stderr, "Could not allocate new RID\n");
590 sid_copy(&map.sid, get_global_sam_sid());
591 sid_append_rid(&map.sid, c->opt_rid);
593 map.sid_name_use = SID_NAME_DOM_GRP;
594 fstrcpy(map.nt_name, ntgroup);
595 fstrcpy(map.comment, "");
597 if (!NT_STATUS_IS_OK(pdb_add_group_mapping_entry(&map))) {
598 d_fprintf(stderr, "Could not add mapping entry for %s\n",
604 /* Now we have a mapping entry, update that stuff */
606 if ( c->opt_localgroup || c->opt_domaingroup ) {
607 if (map.sid_name_use == SID_NAME_WKN_GRP) {
608 d_fprintf(stderr, "Can't change type of the BUILTIN group %s\n",
614 if (c->opt_localgroup)
615 map.sid_name_use = SID_NAME_ALIAS;
617 if (c->opt_domaingroup)
618 map.sid_name_use = SID_NAME_DOM_GRP;
620 /* The case (opt_domaingroup && opt_localgroup) was tested for above */
622 if (strlen(c->opt_comment) > 0)
623 fstrcpy(map.comment, c->opt_comment);
625 if (strlen(c->opt_newntname) > 0)
626 fstrcpy(map.nt_name, c->opt_newntname);
629 map.gid = grp->gr_gid;
631 if (!NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map))) {
632 d_fprintf(stderr, "Could not update group mapping for %s\n", ntgroup);
639 static int net_groupmap_cleanup(struct net_context *c, int argc, const char **argv)
641 GROUP_MAP *map = NULL;
644 if (c->display_usage) {
646 "net groupmap cleanup\n"
647 " Delete all group mappings\n");
651 if (!pdb_enum_group_mapping(NULL, SID_NAME_UNKNOWN, &map, &entries,
653 d_fprintf(stderr, "Could not list group mappings\n");
657 for (i=0; i<entries; i++) {
659 if (map[i].gid == -1)
660 printf("Group %s is not mapped\n", map[i].nt_name);
662 if (!sid_check_is_in_our_domain(&map[i].sid)) {
663 printf("Deleting mapping for NT Group %s, sid %s\n",
665 sid_string_tos(&map[i].sid));
666 pdb_delete_group_mapping_entry(map[i].sid);
675 static int net_groupmap_addmem(struct net_context *c, int argc, const char **argv)
677 DOM_SID alias, member;
681 !string_to_sid(&alias, argv[0]) ||
682 !string_to_sid(&member, argv[1]) ) {
683 d_printf("Usage: net groupmap addmem alias-sid member-sid\n");
687 if (!NT_STATUS_IS_OK(pdb_add_aliasmem(&alias, &member))) {
688 d_fprintf(stderr, "Could not add sid %s to alias %s\n",
696 static int net_groupmap_delmem(struct net_context *c, int argc, const char **argv)
698 DOM_SID alias, member;
702 !string_to_sid(&alias, argv[0]) ||
703 !string_to_sid(&member, argv[1]) ) {
704 d_printf("Usage: net groupmap delmem alias-sid member-sid\n");
708 if (!NT_STATUS_IS_OK(pdb_del_aliasmem(&alias, &member))) {
709 d_fprintf(stderr, "Could not delete sid %s from alias %s\n",
717 static int net_groupmap_listmem(struct net_context *c, int argc, const char **argv)
725 !string_to_sid(&alias, argv[0]) ) {
726 d_printf("Usage: net groupmap listmem alias-sid\n");
733 if (!NT_STATUS_IS_OK(pdb_enum_aliasmem(&alias, &members, &num))) {
734 d_fprintf(stderr, "Could not list members for sid %s\n", argv[0]);
738 for (i = 0; i < num; i++) {
739 printf("%s\n", sid_string_tos(&(members[i])));
742 TALLOC_FREE(members);
747 static bool print_alias_memberships(TALLOC_CTX *mem_ctx,
748 const DOM_SID *domain_sid,
749 const DOM_SID *member)
752 size_t i, num_alias_rids;
757 if (!NT_STATUS_IS_OK(pdb_enum_alias_memberships(
758 mem_ctx, domain_sid, member, 1,
759 &alias_rids, &num_alias_rids))) {
760 d_fprintf(stderr, "Could not list memberships for sid %s\n",
761 sid_string_tos(member));
765 for (i = 0; i < num_alias_rids; i++) {
767 sid_copy(&alias, domain_sid);
768 sid_append_rid(&alias, alias_rids[i]);
769 printf("%s\n", sid_string_tos(&alias));
775 static int net_groupmap_memberships(struct net_context *c, int argc, const char **argv)
778 DOM_SID *domain_sid, *builtin_sid, member;
782 !string_to_sid(&member, argv[0]) ) {
783 d_printf("Usage: net groupmap memberof sid\n");
787 mem_ctx = talloc_init("net_groupmap_memberships");
788 if (mem_ctx == NULL) {
789 d_fprintf(stderr, "talloc_init failed\n");
793 domain_sid = get_global_sam_sid();
794 builtin_sid = string_sid_talloc(mem_ctx, "S-1-5-32");
795 if ((domain_sid == NULL) || (builtin_sid == NULL)) {
796 d_fprintf(stderr, "Could not get domain sid\n");
800 if (!print_alias_memberships(mem_ctx, domain_sid, &member) ||
801 !print_alias_memberships(mem_ctx, builtin_sid, &member))
804 talloc_destroy(mem_ctx);
809 /***********************************************************
810 migrated functionality from smbgroupedit
811 **********************************************************/
812 int net_groupmap(struct net_context *c, int argc, const char **argv)
814 struct functable func[] = {
819 "Create a new group mapping",
821 " Create a new group mapping"
827 "Update a group mapping",
828 "net groupmap modify\n"
829 " Modify an existing group mapping"
835 "Remove a group mapping",
836 "net groupmap delete\n"
837 " Remove a group mapping"
845 " Set a group mapping"
849 net_groupmap_cleanup,
851 "Remove foreign group mapping entries",
852 "net groupmap cleanup\n"
853 " Remove foreign group mapping entries"
859 "Add a foreign alias member",
860 "net groupmap addmem\n"
861 " Add a foreign alias member"
867 "Delete foreign alias member",
868 "net groupmap delmem\n"
869 " Delete foreign alias member"
873 net_groupmap_listmem,
875 "List foreign group members",
876 "net groupmap listmem\n"
877 " List foreign alias members"
881 net_groupmap_memberships,
883 "List foreign group memberships",
884 "net groupmap memberships\n"
885 " List foreign group memberships"
891 "List current group map",
892 "net groupmap list\n"
893 " List current group map"
895 {NULL, NULL, 0, NULL, NULL}
898 /* we shouldn't have silly checks like this */
900 d_fprintf(stderr, "You must be root to edit group mappings.\n");
904 return net_run_function(c,argc, argv, "net groupmap", func);