1/*
2 * Copyright (c) 2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#include <stdint.h>
24#include <stdio.h>
25#include <strings.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <err.h>
29#include <sys/types.h>
30#include <sys/mount.h>
31#include <sys/ucred.h>
32#include <sys/socket.h>
33#include <sys/kauth.h>
34#include <nfs/rpcv2.h>
35#include <nfs/nfs.h>
36#include <uuid/uuid.h>
37
38extern int nfsclnt(int, void *);
39
40void
41Usage()
42{
43	errx(1, "Usage: %s {-g gid | -u uid} | [-G] name", getprogname());
44}
45
46void
47print_map(struct nfs_testmapid *map)
48{
49	uuid_string_t guidstr;
50
51	if (map->ntm_name2id) {
52		printf("%s maps to %s id %d\n", map->ntm_name,
53		       map->ntm_grpflag ? "group" : "user",
54		       map->ntm_id);
55	} else {
56		printf("%s id %d maps to %s\n",
57		       map->ntm_grpflag ? "group" : "user",
58		       map->ntm_id, map->ntm_name);
59	}
60	uuid_unparse(map->ntm_guid.g_guid, guidstr);
61	printf("\tmapping done through guid %s\n", guidstr);
62}
63
64int
65main(int argc, char *argv[])
66{
67	int opt;
68	char *eptr = NULL;
69	struct nfs_testmapid map;
70	int id2name = 0;
71	int error;
72
73	memset(&map, 0, sizeof (map));
74
75	while ((opt = getopt(argc, argv, "u:g:G")) != -1) {
76		switch (opt) {
77		case 'g': map.ntm_grpflag = 1;
78		case 'u': map.ntm_id = (uint32_t)strtoul(optarg, &eptr, 0);
79			if (*eptr)
80				errx(1, "%s is not a valid uid/gid", optarg);
81			if (map.ntm_name2id)
82				Usage();
83			id2name = 1;
84			break;
85		case 'G': map.ntm_grpflag = 1;
86			if (id2name)
87				Usage();
88			map.ntm_name2id = 1;
89			break;
90		default:
91			Usage();
92			break;
93		}
94	}
95	argc -= optind;
96	argv += optind;
97
98	if (!id2name) {
99		if (argc != 1)
100			Usage();
101		memcpy(map.ntm_name, *argv, strlen(*argv));
102		map.ntm_name2id = 1;
103	} else if (argc != 0)
104		Usage();
105
106	error = nfsclnt(NFSCLNT_TESTIDMAP, &map);
107	print_map(&map);
108
109	if (error)
110		err(1, "nfsclnt failed");
111	return (0);
112}
113
114
115