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 void svrreopen(int, short, void *);
113 static int nfsopen(struct idmap_client *);
114 static void nfscb(int, short, void *);
115 static void nfsdcb(int, short, void *);
116 static int validateascii(char *, u_int32_t);
117 static int addfield(char **, ssize_t *, char *);
118 static int getfield(char **, char *, size_t);
120 static void imconv(struct idmap_client *, struct idmap_msg *);
121 static void idtonameres(struct idmap_msg *);
122 static void nametoidres(struct idmap_msg *);
124 static int nfsdopen(char *);
125 static int nfsdopenone(struct idmap_client *, short, char *);
126 static void nfsdreopen(void);
128 size_t strlcat(char *, const char *, size_t);
129 size_t strlcpy(char *, const char *, size_t);
130 ssize_t atomicio(ssize_t (*)(), int, void *, size_t);
131 int daemon(int, int);
133 static int verbose = 0;
134 static char domain[512];
135 static char pipefsdir[PATH_MAX];
136 static char *nobodyuser, *nobodygroup;
137 static uid_t nobodyuid;
138 static gid_t nobodygid;
139 static struct idmap_client nfsd_ic[2];
145 main(int argc, char **argv)
147 int fd = 0, opt, fg = 0, nfsdret = -1;
148 struct idmap_clientq icq;
149 struct event rootdirev, clntdirev, svrdirev;
150 struct event initialize;
154 char *xpipefsdir = NULL;
155 char *xdomain = NULL;
156 int serverstart = 1, clientstart = 1;
158 conf_path = _PATH_IDMAPDCONF;
159 nobodyuser = NFS4NOBODY_USER;
160 nobodygroup = NFS4NOBODY_GROUP;
161 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
163 #define GETOPTSTR "vfd:p:U:G:c:CS"
164 opterr=0; /* Turn off error messages */
165 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
169 if (strchr(GETOPTSTR, optopt))
170 errx(1, "'-%c' option requires an argument.", optopt);
172 errx(1, "'-%c' is an invalid argument.", optopt);
177 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
178 warn("Skipping configuration file \"%s\"", conf_path);
181 verbose = conf_get_num("General", "Verbosity", 0);
182 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
183 CONF_SAVE(xdomain, conf_get_str("General", "Domain"));
184 if (xpipefsdir != NULL)
185 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
187 strlcpy(domain, xdomain, sizeof(domain));
188 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
189 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
192 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
201 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
206 errx(1, "the -d, -U, and -G options have been removed;"
207 " please use the configuration file instead.");
218 if (!serverstart && !clientstart)
219 errx(1, "it is illegal to specify both -C and -S");
221 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
223 if (domain[0] == '\0') {
227 if (gethostname(hname, sizeof(hname)) == -1)
228 errx(1, "Error getting hostname");
230 if ((he = gethostbyname(hname)) == NULL)
231 errx(1, "Error resolving hostname: %s", hname);
233 if ((c = strchr(he->h_name, '.')) == NULL || *++c == '\0')
234 errx(1, "Error resolving domain, "
235 "please use the -d switch");
237 strlcpy(domain, c, sizeof(domain));
240 if ((pw = getpwnam(nobodyuser)) == NULL)
241 errx(1, "Could not find user \"%s\"", nobodyuser);
242 nobodyuid = pw->pw_uid;
244 if ((gr = getgrnam(nobodygroup)) == NULL)
245 errx(1, "Could not find group \"%s\"", nobodygroup);
246 nobodygid = gr->gr_gid;
248 if (strlen(domain) == 0)
249 errx(1, "Invalid domain; please specify with -d switch");
252 warnx("Using domain \"%s\"", domain);
260 nfsdret = nfsdopen(NFSD_DIR);
263 struct timeval now = {
268 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
269 err(1, "open(%s)", pipefsdir);
271 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
272 err(1, "fcntl(%s)", pipefsdir);
273 if (fcntl(fd, F_NOTIFY,
274 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1)
275 err(1, "fcntl(%s)", pipefsdir);
279 /* These events are persistent */
280 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
281 signal_add(&rootdirev, NULL);
282 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
283 signal_add(&clntdirev, NULL);
284 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
285 signal_add(&svrdirev, NULL);
287 /* Fetch current state */
288 /* (Delay till start of event_dispatch to avoid possibly losing
289 * a SIGUSR1 between here and the call to event_dispatch().) */
290 evtimer_set(&initialize, dirscancb, &icq);
291 evtimer_add(&initialize, &now);
294 if (nfsdret != 0 && fd == 0)
295 errx(1, "Neither NFS client nor NFSd found");
297 if (event_dispatch() < 0)
298 errx(1, "event_dispatch: returns errno %d (%s)", errno, strerror(errno));
304 dirscancb(int fd, short which, void *data)
307 struct dirent **ents;
308 struct idmap_client *ic;
310 struct idmap_clientq *icq = data;
312 nent = scandir(pipefsdir, &ents, NULL, alphasort);
314 warn("scandir(%s)", pipefsdir);
318 for (i = 0; i < nent; i++) {
319 if (ents[i]->d_reclen > 4 &&
320 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
321 TAILQ_FOREACH(ic, icq, ic_next)
322 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
327 if ((ic = calloc(1, sizeof(*ic))) == NULL)
329 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
330 sizeof(ic->ic_clid));
332 snprintf(path, sizeof(path), "%s/%s",
333 pipefsdir, ents[i]->d_name);
335 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
336 warn("open(%s)", path);
341 strlcat(path, "/idmap", sizeof(path));
342 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
345 warnx("New client: %s", ic->ic_clid);
347 if (nfsopen(ic) == -1) {
353 ic->ic_id = "Client";
355 TAILQ_INSERT_TAIL(icq, ic, ic_next);
362 TAILQ_FOREACH(ic, icq, ic_next) {
363 if (!ic->ic_scanned) {
364 event_del(&ic->ic_event);
367 TAILQ_REMOVE(icq, ic, ic_next);
369 warnx("Stale client: %s", ic->ic_clid);
370 warnx("\t-> closed %s", ic->ic_path);
380 svrreopen(int fd, short which, void *data)
386 clntscancb(int fd, short which, void *data)
388 struct idmap_clientq *icq = data;
389 struct idmap_client *ic;
391 TAILQ_FOREACH(ic, icq, ic_next)
392 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
394 TAILQ_REMOVE(icq, ic, ic_next);
400 nfsdcb(int fd, short which, void *data)
402 struct idmap_client *ic = data;
404 u_char buf[IDMAP_MAXMSGSZ + 1];
406 char *bp, typebuf[IDMAP_MAXMSGSZ],
407 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
409 if (which != EV_READ)
412 if ((len = read(ic->ic_fd, buf, sizeof(buf))) == -1) {
413 warnx("nfsdcb: read(%s) failed: errno %d (%s)",
414 ic->ic_path, errno, strerror(errno));
418 /* Get rid of newline and terminate buffer*/
422 memset(&im, 0, sizeof(im));
424 /* Authentication name -- ignored for now*/
425 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
426 warnx("nfsdcb: bad authentication name in upcall\n");
429 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
430 warnx("nfsdcb: bad type in upcall\n");
434 warnx("nfsdcb: authbuf=%s authtype=%s", authbuf, typebuf);
436 im.im_type = strcmp(typebuf, "user") == 0 ?
437 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
439 switch (ic->ic_which) {
441 im.im_conv = IDMAP_CONV_NAMETOID;
442 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
443 warnx("nfsdcb: bad name in upcall\n");
448 im.im_conv = IDMAP_CONV_IDTONAME;
449 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
450 warnx("nfsdcb: bad id in upcall\n");
453 if ((im.im_id = strtoul(buf1, (char **)NULL, 10)) == ULONG_MAX &&
455 warnx("nfsdcb: id '%s' too big!\n", buf1);
461 warnx("Unknown which type %d", ic->ic_which);
471 /* Authentication name */
472 addfield(&bp, &bsiz, authbuf);
474 switch (ic->ic_which) {
477 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
478 addfield(&bp, &bsiz, p);
480 addfield(&bp, &bsiz, im.im_name);
481 #define NFSD_EXPIRY 300 /* seconds */
483 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
484 addfield(&bp, &bsiz, buf1);
486 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
487 addfield(&bp, &bsiz, buf1);
489 //if (bsiz == sizeof(buf)) /* XXX */
496 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
497 addfield(&bp, &bsiz, p);
499 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
500 addfield(&bp, &bsiz, buf1);
502 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
503 addfield(&bp, &bsiz, buf1);
505 addfield(&bp, &bsiz, im.im_name);
511 warnx("Unknown which type %d", ic->ic_which);
515 bsiz = sizeof(buf) - bsiz;
517 if (atomicio(write, ic->ic_fd, buf, bsiz) != bsiz)
518 warnx("nfsdcb: write(%s) failed: errno %d (%s)",
519 ic->ic_path, errno, strerror(errno));
522 event_add(&ic->ic_event, NULL);
526 imconv(struct idmap_client *ic, struct idmap_msg *im)
528 switch (im->im_conv) {
529 case IDMAP_CONV_IDTONAME:
532 warnx("%s %s: (%s) id \"%d\" -> name \"%s\"",
533 ic->ic_id, ic->ic_clid,
534 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
535 im->im_id, im->im_name);
537 case IDMAP_CONV_NAMETOID:
538 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
539 im->im_status |= IDMAP_STATUS_INVALIDMSG;
544 warnx("%s %s: (%s) name \"%s\" -> id \"%d\"",
545 ic->ic_id, ic->ic_clid,
546 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
547 im->im_name, im->im_id);
550 warnx("Invalid conversion type (%d) in message", im->im_conv);
551 im->im_status |= IDMAP_STATUS_INVALIDMSG;
557 nfscb(int fd, short which, void *data)
559 struct idmap_client *ic = data;
562 if (which != EV_READ)
565 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
567 warn("read(%s)", ic->ic_path);
575 if (atomicio(write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
576 warn("write(%s)", ic->ic_path);
578 event_add(&ic->ic_event, NULL);
582 nfsdreopen_one(struct idmap_client *ic)
587 warnx("ReOpening %s", ic->ic_path);
588 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
591 ic->ic_event.ev_fd = ic->ic_fd = fd;
592 if ((ic->ic_event.ev_flags & EVLIST_INIT) == 0) {
593 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
594 event_add(&ic->ic_event, NULL);
597 warnx("nfsdreopen: Opening '%s' failed: errno %d (%s)",
598 ic->ic_path, errno, strerror(errno));
603 * Note: nfsdreopen assumes nfsdopen has already been called
608 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
609 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
616 return ((nfsdopenone(&nfsd_ic[0], IC_NAMEID, path) == 0 &&
617 nfsdopenone(&nfsd_ic[1], IC_IDNAME, path) == 0) ? 0 : -1);
621 nfsdopenone(struct idmap_client *ic, short which, char *path)
625 whichstr = which == IC_IDNAME ? "idtoname" : "nametoid";
626 snprintf(ic->ic_path, sizeof(ic->ic_path),
627 "%s/nfs4.%s/channel", path, whichstr);
628 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
630 warnx("Opening %s failed: errno %d (%s)",
631 ic->ic_path, errno, strerror(errno));
635 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
636 event_add(&ic->ic_event, NULL);
638 ic->ic_which = which;
639 ic->ic_id = "Server";
640 strlcpy(ic->ic_clid, domain, sizeof(ic->ic_clid));
643 warnx("Opened %s", ic->ic_path);
649 nfsopen(struct idmap_client *ic)
651 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
654 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
655 fcntl(ic->ic_dirfd, F_NOTIFY,
656 DN_CREATE | DN_DELETE | DN_MULTISHOT);
659 warn("open(%s)", ic->ic_path);
663 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
664 event_add(&ic->ic_event, NULL);
665 fcntl(ic->ic_dirfd, F_SETSIG, 0);
666 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
668 warnx("Opened %s", ic->ic_path);
674 static int write_name(char *dest, char *localname, char *domain, size_t len)
676 if (strlen(localname) + 1 + strlen(domain) + 1 > len) {
677 return -ENOMEM; /* XXX: Is there an -ETOOLONG? */
679 strcpy(dest, localname);
681 strcat(dest, domain);
686 idtonameres(struct idmap_msg *im)
690 switch (im->im_type) {
691 case IDMAP_TYPE_USER:
692 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
693 sizeof(im->im_name));
695 write_name(im->im_name, nobodyuser, domain,
696 sizeof(im->im_name));
698 case IDMAP_TYPE_GROUP:
699 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
700 sizeof(im->im_name));
702 write_name(im->im_name, nobodygroup, domain,
703 sizeof(im->im_name));
706 /* XXX Hack? would rather return failure instead of writing nobody
707 * as above, but kernel seems not to deal well with that as of
709 im->im_status = IDMAP_STATUS_SUCCESS;
713 nametoidres(struct idmap_msg *im)
717 switch (im->im_type) {
718 case IDMAP_TYPE_USER:
719 ret = nfs4_name_to_uid(im->im_name, &im->im_id);
721 im->im_id = nobodyuid;
723 case IDMAP_TYPE_GROUP:
724 ret = nfs4_name_to_gid(im->im_name, &im->im_id);
726 im->im_id = nobodygid;
729 /* XXX Hack? would rather return failure instead of writing nobody
730 * as above, but kernel seems not to deal well with that as of
732 im->im_status = IDMAP_STATUS_SUCCESS;
736 validateascii(char *string, u_int32_t len)
740 for (i = 0; i < len; i++) {
741 if (string[i] == '\0')
744 if (string[i] & 0x80)
748 if (string[i] != '\0')
755 addfield(char **bpp, ssize_t *bsizp, char *fld)
758 ssize_t bsiz = *bsizp;
760 while ((ch = *fld++) != '\0' && bsiz > 0) {
767 bp += snprintf(bp, bsiz, "\\%03o", ch);
778 if (bsiz < 1 || ch != '\0')
791 getfield(char **bpp, char *fld, size_t fldsz)
796 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
799 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
802 while (*bp != '\0' && fldsz > 1) {
804 if ((n = sscanf(bp, "\\%03o", &val)) != 1)