summaryrefslogtreecommitdiffstats
path: root/sbin/idmapd/idmapd.c
blob: fa3a082d3ddb52a3c1a7e5dbe85cf8b59e85141c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* $FreeBSD$ */
/* $Id: idmapd.c,v 1.5 2003/11/05 14:58:58 rees Exp $ */

/*
 * copyright (c) 2003
 * the regents of the university of michigan
 * all rights reserved
 * 
 * permission is granted to use, copy, create derivative works and redistribute
 * this software and such derivative works for any purpose, so long as the name
 * of the university of michigan is not used in any advertising or publicity
 * pertaining to the use or distribution of this software without specific,
 * written prior authorization.  if the above copyright notice or any other
 * identification of the university of michigan is included in any copy of any
 * portion of this software, then the disclaimer below must also be included.
 * 
 * this software is provided as is, without representation from the university
 * of michigan as to its fitness for any purpose, and without warranty by the
 * university of michigan of any kind, either express or implied, including
 * without limitation the implied warranties of merchantability and fitness for
 * a particular purpose. the regents of the university of michigan shall not be
 * liable for any damages, including special, indirect, incidental, or
 * consequential damages, with respect to any claim arising out of or in
 * connection with the use of the software, even if it has been or is hereafter
 * advised of the possibility of such damages.
 */

/* XXX ignores the domain of received names. */

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/queue.h>

#include <nfs4client/nfs4_dev.h>
#include <nfs4client/nfs4_idmap.h>

#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>

#define DEV_PATH "/dev/nfs4"

#define DOMAIN	"@FreeBSD.org"
#define BADUSER	"nobody"
#define BADGROUP "nogroup"
#define BADUID	65534
#define BADGID	65533

struct idmap_e {
	struct nfs4dev_msg msg;
	TAILQ_ENTRY(idmap_e) next;
};

int fd, verbose;
char *domain = DOMAIN;

TAILQ_HEAD(, idmap_e) upcall_q;

#define add_idmap_e(E) do {	\
  	assert(E != NULL);	\
	TAILQ_INSERT_TAIL(&upcall_q, E, next); \
} while(0)

#define remove_idmap_e(E) do {	\
  	assert(E != NULL && !TAILQ_EMPTY(&upcall_q));	\
	E = TAILQ_FIRST(&upcall_q);	\
	TAILQ_REMOVE(&upcall_q, E, next); \
} while(0)

#define get_idmap_e(E) do {	\
  	if ((E = (struct idmap_e *) malloc(sizeof(struct idmap_e))) == NULL) {\
		fprintf(stderr, "get_idmap_e(): error in malloc\n");\
	} } while(0)

#define put_idmap_e(E) free(E)

/* from marius */
int
validateascii(char *string, u_int32_t len)
{
	int i;

	for (i = 0; i < len; i++) {
		if (string[i] == '\0')
			break;
		if (string[i] & 0x80)
			return (-1);
	}

	if (string[i] != '\0')
		return (-1);
	return (i + 1);
}

char *
idmap_prune_domain(struct idmap_msg * m)
{
  	size_t i;
	size_t len;
	char * ret = NULL;

	if (m == NULL)
		return NULL;

	len = m->id_namelen;

	if (validateascii(m->id_name, len) < 0) {
		fprintf(stderr, "msg has invalid ascii\n");
		return NULL;
	}

	for (i=0; i < len && m->id_name[i] != '@' ; i++);

	ret = (char *)malloc(i+1);
	if (ret == NULL)
	  return NULL;

	bcopy(m->id_name, ret, i);
	ret[i] = '\0';

	return ret;
}

int
idmap_add_domain(struct idmap_msg * m, char * name)
{
  	size_t len, nlen;

	if (m == NULL || name == NULL)
		return -1;

	len = strlen(name);

	nlen = len + strlen(domain);

	if (nlen > IDMAP_MAXNAMELEN)
	  	return -1;

	bcopy(name, &m->id_name[0], len);
	bcopy(domain, &m->id_name[len], strlen(domain));

	m->id_name[nlen] = '\0';
	m->id_namelen = nlen;

	return 0;
}

int
idmap_name(struct idmap_msg * m, char *name)
{
	if (m == NULL || name == NULL || m->id_namelen != 0)
		return -1;

	if (idmap_add_domain(m, name))
		return -1;

	return 0;
}

int
idmap_id(struct idmap_msg * m, ident_t id)
{
	if (m == NULL || m->id_namelen == 0) {
		fprintf(stderr, "idmap_id: bad msg\n");
	  	return -1;
	}

	switch(m->id_type) {
	case IDMAP_TYPE_UID:
		m->id_id.uid = id.uid;
		break;
	case IDMAP_TYPE_GID:
		m->id_id.gid = id.gid;
		break;
	default:
		return -1;
		break;
	};

	return 0;
}

int
idmap_service(struct idmap_e * e)
{
  	struct idmap_msg * m;
	struct passwd * pwd;
	struct group * grp;
	ident_t id;
	char * name;

	if (e == NULL) {
	  	fprintf(stderr, "bad entry\n");
		return -1;
	}

	if (e->msg.msg_vers != NFS4DEV_VERSION) {
		fprintf(stderr, "kernel/userland version mismatch! %d/%d\n",
		    e->msg.msg_vers, NFS4DEV_VERSION);
		return -1;
	}

	if (e->msg.msg_type != NFS4DEV_TYPE_IDMAP) {
  		fprintf(stderr, "bad type!\n");
		return -1;
	}

	if (e->msg.msg_len != sizeof(struct idmap_msg)) {
		fprintf(stderr, "bad message length: %zu/%zu\n", e->msg.msg_len,
			sizeof(struct idmap_msg));
		return -1;
	}

	if (verbose)
		printf("servicing msg xid: %x\n", e->msg.msg_xid);


	m = (struct idmap_msg *)e->msg.msg_data;

	if (m->id_namelen != 0 && m->id_namelen != strlen(m->id_name)) {
		fprintf(stderr, "bad name length in idmap_msg\n");
		return -1;
	}

	switch (m->id_type) {
	case IDMAP_TYPE_UID:
		if (m->id_namelen == 0) {
			/* id to name */
		  	pwd = getpwuid(m->id_id.uid);

			if (pwd == NULL) {
				fprintf(stderr, "unknown uid %d!\n",
					(uint32_t)m->id_id.uid);
				name = BADUSER;
			} else
			  	name = pwd->pw_name;

			if (idmap_name(m, name))
				return -1;

		} else {
		  	/* name to id */
		  	name = idmap_prune_domain(m);
			if (name == NULL)
				return -1;

			pwd = getpwnam(name);

			if (pwd == NULL) {
				fprintf(stderr, "unknown username %s!\n", name);

				id.uid = (uid_t)BADUID;
			} else
			  	id.uid = pwd->pw_uid;

			free(name);

			if (idmap_id(m, id))
				return -1;
		}
		break;
	case IDMAP_TYPE_GID:
		if (m->id_namelen == 0) {
			/* id to name */
		  	grp = getgrgid(m->id_id.gid);

			if (grp == NULL) {
				fprintf(stderr, "unknown gid %d!\n",
					(uint32_t)m->id_id.gid);
				name = BADGROUP;
			} else
			  	name = grp->gr_name;

			if (idmap_name(m, name))
				return -1;
		} else {
		  	/* name to id */
		  	name = idmap_prune_domain(m);
			if (name == NULL)
				return -1;

			grp = getgrnam(name);

			if (grp == NULL) {
				fprintf(stderr, "unknown groupname %s!\n", name);

				id.gid = (gid_t)BADGID;
			} else
			  	id.gid = grp->gr_gid;

			free(name);

			if (idmap_id(m, id))
				return -1;
		}
		break;
	default:
		fprintf(stderr, "bad idmap type: %d\n", m->id_type);
		return -1;
		break;
	}

	return 0;
}

int
main(int argc, char ** argv)
{
	int error = 0;
	struct idmap_e * entry;
	fd_set read_fds, write_fds;
	int maxfd;
	int ret, ch;

	while ((ch = getopt(argc, argv, "d:v")) != -1) {
		switch (ch) {
		case 'd':
			domain = optarg;
			break;
		case 'v':
			verbose = 1;
			break;
		default:
			fprintf(stderr, "usage: %s [-v] [-d domain]\n", argv[0]);
			exit(1);
			break;
		}
	}


	TAILQ_INIT(&upcall_q);

	fd = open(DEV_PATH, O_RDWR, S_IRUSR | S_IWUSR);

	if (fd < 0) {
		perror(DEV_PATH);
		exit(1);
	}

	if (!verbose)
		daemon(0,0);

	maxfd = fd;
	for (;;) {
	  	struct timeval timo = {1, 0};
		do {
			FD_ZERO(&read_fds);
			FD_ZERO(&write_fds);

			FD_SET(fd, &read_fds);
			FD_SET(fd, &write_fds);

			ret = select(maxfd+1, &read_fds, &write_fds, NULL, &timo);
		} while (ret < 0 && errno == EINTR);

		if (ret <= 0) {
			if (ret != 0)
				perror("select");
			continue;
		}


		if (FD_ISSET(fd, &read_fds)) {
			for (;;) {
				get_idmap_e(entry);

				error = ioctl(fd, NFS4DEVIOCGET, &entry->msg);

				if (error == -1) {
					if (errno != EAGAIN)
	  					perror("get ioctl:");
					put_idmap_e(entry);
					break;
				}

				switch (entry->msg.msg_type ) {
				case NFS4DEV_TYPE_IDMAP:
					if (idmap_service(entry))
						entry->msg.msg_error = EIO;
				break;
				default:
					fprintf(stderr, "unknown nfs4dev_msg type\n");
					entry->msg.msg_error = EIO;
				break;
				}

				add_idmap_e(entry);
			}
		}

		if (FD_ISSET(fd, &write_fds)) {
			while (!TAILQ_EMPTY(&upcall_q)) {
				remove_idmap_e(entry);

				error = ioctl(fd, NFS4DEVIOCPUT, &entry->msg);

				if (error == -1) {
				  	if (errno != EAGAIN)
	  					perror("put ioctl");
					break;
				}
				put_idmap_e(entry);
			}
		}
	}

	/* never reached */
	exit(1);
}
OpenPOWER on IntegriCloud