4 * Userland daemon for idmap.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
40 #include <sys/socket.h>
44 #include "nfs_idmap.h"
65 #endif /* HAVE_CONFIG_H */
72 #define PIPEFS_DIR "/var/lib/nfs/rpc_pipefs/"
76 #define NFSD_DIR "/proc/net/rpc"
79 #ifndef NFS4NOBODY_USER
80 #define NFS4NOBODY_USER "nobody"
83 #ifndef NFS4NOBODY_GROUP
84 #define NFS4NOBODY_GROUP "nobody"
88 #define CONF_SAVE(w, f) do { \
100 char ic_path[PATH_MAX];
102 struct event ic_event;
105 TAILQ_ENTRY(idmap_client) ic_next;
108 TAILQ_HEAD(idmap_clientq, idmap_client);
110 static void dirscancb(int, short, void *);
111 static void clntscancb(int, short, void *);
112 static int nfsopen(struct idmap_client *);
113 static void nfscb(int, short, void *);
114 static void nfsdcb(int, short, void *);
115 static int validateascii(char *, u_int32_t);
116 static int addfield(char **, ssize_t *, char *);
117 static int getfield(char **, char *, size_t);
119 static void imconv(struct idmap_client *, struct idmap_msg *);
120 static void idtonameres(struct idmap_msg *);
121 static void nametoidres(struct idmap_msg *);
123 static int nfsdopen(char *);
124 static int nfsdopenone(struct idmap_client *, short, char *);
126 size_t strlcat(char *, const char *, size_t);
127 size_t strlcpy(char *, const char *, size_t);
128 ssize_t atomicio(ssize_t (*)(), int, void *, size_t);
129 int daemon(int, int);
131 static int verbose = 0;
132 static char domain[512];
133 static char pipefsdir[PATH_MAX];
134 static char *nobodyuser, *nobodygroup;
135 static uid_t nobodyuid;
136 static gid_t nobodygid;
137 static struct idmap_client nfsd_ic[2];
143 main(int argc, char **argv)
145 int fd = 0, opt, fg = 0, nfsdret = -1;
146 struct idmap_clientq icq;
147 struct event rootdirev, clntdirev;
148 struct event initialize;
152 char *xpipefsdir = NULL;
153 char *xdomain = NULL;
154 int serverstart = 1, clientstart = 1;
156 conf_path = _PATH_IDMAPDCONF;
157 nobodyuser = NFS4NOBODY_USER;
158 nobodygroup = NFS4NOBODY_GROUP;
159 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
161 #define GETOPTSTR "vfd:p:U:G:c:CS"
162 opterr=0; /* Turn off error messages */
163 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
167 if (strchr(GETOPTSTR, optopt))
168 errx(1, "'-%c' option requires an argument.", optopt);
170 errx(1, "'-%c' is an invalid argument.", optopt);
175 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
176 warn("Skipping configuration file \"%s\"", conf_path);
179 verbose = conf_get_num("General", "Verbosity", 0);
180 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
181 CONF_SAVE(xdomain, conf_get_str("General", "Domain"));
182 if (xpipefsdir != NULL)
183 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
185 strlcpy(domain, xdomain, sizeof(domain));
186 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
187 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
190 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
199 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
204 errx(1, "the -d, -U, and -G options have been removed;"
205 " please use the configuration file instead.");
216 if (!serverstart && !clientstart)
217 errx(1, "it is illegal to specify both -C and -S");
219 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
221 if (domain[0] == '\0') {
225 if (gethostname(hname, sizeof(hname)) == -1)
226 errx(1, "Error getting hostname");
228 if ((he = gethostbyname(hname)) == NULL)
229 errx(1, "Error resolving hostname: %s", hname);
231 if ((c = strchr(he->h_name, '.')) == NULL || *++c == '\0')
232 errx(1, "Error resolving domain, "
233 "please use the -d switch");
235 strlcpy(domain, c, sizeof(domain));
238 if ((pw = getpwnam(nobodyuser)) == NULL)
239 errx(1, "Could not find user \"%s\"", nobodyuser);
240 nobodyuid = pw->pw_uid;
242 if ((gr = getgrnam(nobodygroup)) == NULL)
243 errx(1, "Could not find group \"%s\"", nobodygroup);
244 nobodygid = gr->gr_gid;
246 if (strlen(domain) == 0)
247 errx(1, "Invalid domain; please specify with -d switch");
250 warnx("Using domain \"%s\"", domain);
258 nfsdret = nfsdopen(NFSD_DIR);
261 struct timeval now = {
266 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
267 err(1, "open(%s)", pipefsdir);
269 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
270 err(1, "fcntl(%s)", pipefsdir);
271 if (fcntl(fd, F_NOTIFY,
272 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1)
273 err(1, "fcntl(%s)", pipefsdir);
277 /* These events are persistent */
278 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
279 signal_add(&rootdirev, NULL);
280 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
281 signal_add(&clntdirev, NULL);
283 /* Fetch current state */
284 /* (Delay till start of event_dispatch to avoid possibly losing
285 * a SIGUSR1 between here and the call to event_dispatch().) */
286 evtimer_set(&initialize, dirscancb, &icq);
287 evtimer_add(&initialize, &now);
290 if (nfsdret != 0 && fd == 0)
291 errx(1, "Neither NFS client nor NFSd found");
293 if (event_dispatch() < 0)
294 errx(1, "event_dispatch: returns errno %d (%s)", errno, strerror(errno));
300 dirscancb(int fd, short which, void *data)
303 struct dirent **ents;
304 struct idmap_client *ic;
306 struct idmap_clientq *icq = data;
308 nent = scandir(pipefsdir, &ents, NULL, alphasort);
310 warn("scandir(%s)", pipefsdir);
314 for (i = 0; i < nent; i++) {
315 if (ents[i]->d_reclen > 4 &&
316 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
317 TAILQ_FOREACH(ic, icq, ic_next)
318 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
323 if ((ic = calloc(1, sizeof(*ic))) == NULL)
325 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
326 sizeof(ic->ic_clid));
328 snprintf(path, sizeof(path), "%s/%s",
329 pipefsdir, ents[i]->d_name);
331 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
332 warn("open(%s)", path);
337 strlcat(path, "/idmap", sizeof(path));
338 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
341 warnx("New client: %s", ic->ic_clid);
343 if (nfsopen(ic) == -1) {
349 ic->ic_id = "Client";
351 TAILQ_INSERT_TAIL(icq, ic, ic_next);
358 TAILQ_FOREACH(ic, icq, ic_next) {
359 if (!ic->ic_scanned) {
360 event_del(&ic->ic_event);
363 TAILQ_REMOVE(icq, ic, ic_next);
365 warnx("Stale client: %s", ic->ic_clid);
366 warnx("\t-> closed %s", ic->ic_path);
376 clntscancb(int fd, short which, void *data)
378 struct idmap_clientq *icq = data;
379 struct idmap_client *ic;
381 TAILQ_FOREACH(ic, icq, ic_next)
382 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
384 TAILQ_REMOVE(icq, ic, ic_next);
390 nfsdcb(int fd, short which, void *data)
392 struct idmap_client *ic = data;
394 u_char buf[IDMAP_MAXMSGSZ + 1];
396 char *bp, typebuf[IDMAP_MAXMSGSZ],
397 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
399 if (which != EV_READ)
402 if ((len = read(ic->ic_fd, buf, sizeof(buf))) == -1) {
403 warnx("nfsdcb: read(%s) failed: errno %d (%s)",
404 ic->ic_path, errno, strerror(errno));
408 /* Get rid of newline and terminate buffer*/
412 memset(&im, 0, sizeof(im));
414 /* Authentication name -- ignored for now*/
415 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
416 warnx("nfsdcb: bad authentication name in upcall\n");
419 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
420 warnx("nfsdcb: bad type in upcall\n");
424 warnx("nfsdcb: authbuf=%s authtype=%s", authbuf, typebuf);
426 im.im_type = strcmp(typebuf, "user") == 0 ?
427 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
429 switch (ic->ic_which) {
431 im.im_conv = IDMAP_CONV_NAMETOID;
432 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
433 warnx("nfsdcb: bad name in upcall\n");
438 im.im_conv = IDMAP_CONV_IDTONAME;
439 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
440 warnx("nfsdcb: bad id in upcall\n");
443 if ((im.im_id = strtoul(buf1, (char **)NULL, 10)) == ULONG_MAX &&
445 warnx("nfsdcb: id '%s' too big!\n", buf1);
451 warnx("Unknown which type %d", ic->ic_which);
461 /* Authentication name */
462 addfield(&bp, &bsiz, authbuf);
464 switch (ic->ic_which) {
467 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
468 addfield(&bp, &bsiz, p);
470 addfield(&bp, &bsiz, im.im_name);
471 #define NFSD_EXPIRY 300 /* seconds */
473 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
474 addfield(&bp, &bsiz, buf1);
476 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
477 addfield(&bp, &bsiz, buf1);
479 //if (bsiz == sizeof(buf)) /* XXX */
486 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
487 addfield(&bp, &bsiz, p);
489 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
490 addfield(&bp, &bsiz, buf1);
492 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
493 addfield(&bp, &bsiz, buf1);
495 addfield(&bp, &bsiz, im.im_name);
501 warnx("Unknown which type %d", ic->ic_which);
505 bsiz = sizeof(buf) - bsiz;
507 if (atomicio(write, ic->ic_fd, buf, bsiz) != bsiz)
508 warnx("nfsdcb: write(%s) failed: errno %d (%s)",
509 ic->ic_path, errno, strerror(errno));
512 event_add(&ic->ic_event, NULL);
516 imconv(struct idmap_client *ic, struct idmap_msg *im)
518 switch (im->im_conv) {
519 case IDMAP_CONV_IDTONAME:
522 warnx("%s %s: (%s) id \"%d\" -> name \"%s\"",
523 ic->ic_id, ic->ic_clid,
524 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
525 im->im_id, im->im_name);
527 case IDMAP_CONV_NAMETOID:
528 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
529 im->im_status |= IDMAP_STATUS_INVALIDMSG;
534 warnx("%s %s: (%s) name \"%s\" -> id \"%d\"",
535 ic->ic_id, ic->ic_clid,
536 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
537 im->im_name, im->im_id);
540 warnx("Invalid conversion type (%d) in message", im->im_conv);
541 im->im_status |= IDMAP_STATUS_INVALIDMSG;
547 nfscb(int fd, short which, void *data)
549 struct idmap_client *ic = data;
552 if (which != EV_READ)
555 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
557 warn("read(%s)", ic->ic_path);
565 if (atomicio(write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
566 warn("write(%s)", ic->ic_path);
568 event_add(&ic->ic_event, NULL);
574 return ((nfsdopenone(&nfsd_ic[0], IC_NAMEID, path) == 0 &&
575 nfsdopenone(&nfsd_ic[1], IC_IDNAME, path) == 0) ? 0 : -1);
579 nfsdopenone(struct idmap_client *ic, short which, char *path)
583 whichstr = which == IC_IDNAME ? "idtoname" : "nametoid";
584 snprintf(ic->ic_path, sizeof(ic->ic_path),
585 "%s/nfs4.%s/channel", path, whichstr);
586 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
588 warnx("Opening %s failed: errno %d (%s)",
589 ic->ic_path, errno, strerror(errno));
593 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
594 event_add(&ic->ic_event, NULL);
596 ic->ic_which = which;
597 ic->ic_id = "Server";
598 strlcpy(ic->ic_clid, domain, sizeof(ic->ic_clid));
601 warnx("Opened %s", ic->ic_path);
607 nfsopen(struct idmap_client *ic)
609 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
612 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
613 fcntl(ic->ic_dirfd, F_NOTIFY,
614 DN_CREATE | DN_DELETE | DN_MULTISHOT);
617 warn("open(%s)", ic->ic_path);
621 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
622 event_add(&ic->ic_event, NULL);
623 fcntl(ic->ic_dirfd, F_SETSIG, 0);
624 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
626 warnx("Opened %s", ic->ic_path);
632 static int write_name(char *dest, char *localname, char *domain, size_t len)
634 if (strlen(localname) + 1 + strlen(domain) + 1 > len) {
635 return -ENOMEM; /* XXX: Is there an -ETOOLONG? */
637 strcpy(dest, localname);
639 strcat(dest, domain);
644 idtonameres(struct idmap_msg *im)
648 switch (im->im_type) {
649 case IDMAP_TYPE_USER:
650 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
651 sizeof(im->im_name));
653 write_name(im->im_name, nobodyuser, domain,
654 sizeof(im->im_name));
656 case IDMAP_TYPE_GROUP:
657 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
658 sizeof(im->im_name));
660 write_name(im->im_name, nobodygroup, domain,
661 sizeof(im->im_name));
664 /* XXX Hack? would rather return failure instead of writing nobody
665 * as above, but kernel seems not to deal well with that as of
667 im->im_status = IDMAP_STATUS_SUCCESS;
671 nametoidres(struct idmap_msg *im)
675 switch (im->im_type) {
676 case IDMAP_TYPE_USER:
677 ret = nfs4_name_to_uid(im->im_name, &im->im_id);
679 im->im_id = nobodyuid;
681 case IDMAP_TYPE_GROUP:
682 ret = nfs4_name_to_gid(im->im_name, &im->im_id);
684 im->im_id = nobodygid;
687 /* XXX Hack? would rather return failure instead of writing nobody
688 * as above, but kernel seems not to deal well with that as of
690 im->im_status = IDMAP_STATUS_SUCCESS;
694 validateascii(char *string, u_int32_t len)
698 for (i = 0; i < len; i++) {
699 if (string[i] == '\0')
702 if (string[i] & 0x80)
706 if (string[i] != '\0')
713 addfield(char **bpp, ssize_t *bsizp, char *fld)
716 ssize_t bsiz = *bsizp;
718 while ((ch = *fld++) != '\0' && bsiz > 0) {
725 bp += snprintf(bp, bsiz, "\\%03o", ch);
736 if (bsiz < 1 || ch != '\0')
749 getfield(char **bpp, char *fld, size_t fldsz)
754 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
757 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
760 while (*bp != '\0' && fldsz > 1) {
762 if ((n = sscanf(bp, "\\%03o", &val)) != 1)