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 void mydaemon(int, int);
132 void release_parent();
134 static int verbose = 0;
135 static char domain[512];
136 static char pipefsdir[PATH_MAX];
137 static char *nobodyuser, *nobodygroup;
138 static uid_t nobodyuid;
139 static gid_t nobodygid;
140 static struct idmap_client nfsd_ic[2];
146 main(int argc, char **argv)
148 int fd = 0, opt, fg = 0, nfsdret = -1;
149 struct idmap_clientq icq;
150 struct event rootdirev, clntdirev, svrdirev;
151 struct event initialize;
155 char *xpipefsdir = NULL;
156 char *xdomain = NULL;
157 int serverstart = 1, clientstart = 1;
159 conf_path = _PATH_IDMAPDCONF;
160 nobodyuser = NFS4NOBODY_USER;
161 nobodygroup = NFS4NOBODY_GROUP;
162 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
164 #define GETOPTSTR "vfd:p:U:G:c:CS"
165 opterr=0; /* Turn off error messages */
166 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
170 if (strchr(GETOPTSTR, optopt))
171 errx(1, "'-%c' option requires an argument.", optopt);
173 errx(1, "'-%c' is an invalid argument.", optopt);
178 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
179 warn("Skipping configuration file \"%s\"", conf_path);
182 verbose = conf_get_num("General", "Verbosity", 0);
183 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
184 CONF_SAVE(xdomain, conf_get_str("General", "Domain"));
185 if (xpipefsdir != NULL)
186 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
188 strlcpy(domain, xdomain, sizeof(domain));
189 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
190 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
193 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
202 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
207 errx(1, "the -d, -U, and -G options have been removed;"
208 " please use the configuration file instead.");
219 if (!serverstart && !clientstart)
220 errx(1, "it is illegal to specify both -C and -S");
222 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
224 if (domain[0] == '\0') {
228 if (gethostname(hname, sizeof(hname)) == -1)
229 errx(1, "Error getting hostname");
231 if ((he = gethostbyname(hname)) == NULL)
232 errx(1, "Error resolving hostname: %s", hname);
234 if ((c = strchr(he->h_name, '.')) == NULL || *++c == '\0')
235 errx(1, "Error resolving domain, "
236 "please use the -d switch");
238 strlcpy(domain, c, sizeof(domain));
241 if ((pw = getpwnam(nobodyuser)) == NULL)
242 errx(1, "Could not find user \"%s\"", nobodyuser);
243 nobodyuid = pw->pw_uid;
245 if ((gr = getgrnam(nobodygroup)) == NULL)
246 errx(1, "Could not find group \"%s\"", nobodygroup);
247 nobodygid = gr->gr_gid;
249 if (strlen(domain) == 0)
250 errx(1, "Invalid domain; please specify with -d switch");
253 warnx("Using domain \"%s\"", domain);
261 nfsdret = nfsdopen(NFSD_DIR);
264 struct timeval now = {
269 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
270 err(1, "open(%s)", pipefsdir);
272 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
273 err(1, "fcntl(%s)", pipefsdir);
274 if (fcntl(fd, F_NOTIFY,
275 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1)
276 err(1, "fcntl(%s)", pipefsdir);
280 /* These events are persistent */
281 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
282 signal_add(&rootdirev, NULL);
283 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
284 signal_add(&clntdirev, NULL);
285 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
286 signal_add(&svrdirev, NULL);
288 /* Fetch current state */
289 /* (Delay till start of event_dispatch to avoid possibly losing
290 * a SIGUSR1 between here and the call to event_dispatch().) */
291 evtimer_set(&initialize, dirscancb, &icq);
292 evtimer_add(&initialize, &now);
295 if (nfsdret != 0 && fd == 0)
296 errx(1, "Neither NFS client nor NFSd found");
300 if (event_dispatch() < 0)
301 errx(1, "event_dispatch: returns errno %d (%s)", errno, strerror(errno));
307 dirscancb(int fd, short which, void *data)
310 struct dirent **ents;
311 struct idmap_client *ic;
313 struct idmap_clientq *icq = data;
315 nent = scandir(pipefsdir, &ents, NULL, alphasort);
317 warn("scandir(%s)", pipefsdir);
321 for (i = 0; i < nent; i++) {
322 if (ents[i]->d_reclen > 4 &&
323 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
324 TAILQ_FOREACH(ic, icq, ic_next)
325 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
330 if ((ic = calloc(1, sizeof(*ic))) == NULL)
332 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
333 sizeof(ic->ic_clid));
335 snprintf(path, sizeof(path), "%s/%s",
336 pipefsdir, ents[i]->d_name);
338 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
339 warn("open(%s)", path);
344 strlcat(path, "/idmap", sizeof(path));
345 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
348 warnx("New client: %s", ic->ic_clid);
350 if (nfsopen(ic) == -1) {
356 ic->ic_id = "Client";
358 TAILQ_INSERT_TAIL(icq, ic, ic_next);
365 TAILQ_FOREACH(ic, icq, ic_next) {
366 if (!ic->ic_scanned) {
367 event_del(&ic->ic_event);
370 TAILQ_REMOVE(icq, ic, ic_next);
372 warnx("Stale client: %s", ic->ic_clid);
373 warnx("\t-> closed %s", ic->ic_path);
383 svrreopen(int fd, short which, void *data)
389 clntscancb(int fd, short which, void *data)
391 struct idmap_clientq *icq = data;
392 struct idmap_client *ic;
394 TAILQ_FOREACH(ic, icq, ic_next)
395 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
397 TAILQ_REMOVE(icq, ic, ic_next);
403 nfsdcb(int fd, short which, void *data)
405 struct idmap_client *ic = data;
407 u_char buf[IDMAP_MAXMSGSZ + 1];
409 char *bp, typebuf[IDMAP_MAXMSGSZ],
410 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
412 if (which != EV_READ)
415 if ((len = read(ic->ic_fd, buf, sizeof(buf))) == -1) {
416 warnx("nfsdcb: read(%s) failed: errno %d (%s)",
417 ic->ic_path, errno, strerror(errno));
421 /* Get rid of newline and terminate buffer*/
425 memset(&im, 0, sizeof(im));
427 /* Authentication name -- ignored for now*/
428 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
429 warnx("nfsdcb: bad authentication name in upcall\n");
432 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
433 warnx("nfsdcb: bad type in upcall\n");
437 warnx("nfsdcb: authbuf=%s authtype=%s", authbuf, typebuf);
439 im.im_type = strcmp(typebuf, "user") == 0 ?
440 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
442 switch (ic->ic_which) {
444 im.im_conv = IDMAP_CONV_NAMETOID;
445 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
446 warnx("nfsdcb: bad name in upcall\n");
451 im.im_conv = IDMAP_CONV_IDTONAME;
452 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
453 warnx("nfsdcb: bad id in upcall\n");
456 if ((im.im_id = strtoul(buf1, (char **)NULL, 10)) == ULONG_MAX &&
458 warnx("nfsdcb: id '%s' too big!\n", buf1);
464 warnx("Unknown which type %d", ic->ic_which);
474 /* Authentication name */
475 addfield(&bp, &bsiz, authbuf);
477 switch (ic->ic_which) {
480 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
481 addfield(&bp, &bsiz, p);
483 addfield(&bp, &bsiz, im.im_name);
484 #define NFSD_EXPIRY 300 /* seconds */
486 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
487 addfield(&bp, &bsiz, buf1);
489 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
490 addfield(&bp, &bsiz, buf1);
492 //if (bsiz == sizeof(buf)) /* XXX */
499 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
500 addfield(&bp, &bsiz, p);
502 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
503 addfield(&bp, &bsiz, buf1);
505 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
506 addfield(&bp, &bsiz, buf1);
508 addfield(&bp, &bsiz, im.im_name);
514 warnx("Unknown which type %d", ic->ic_which);
518 bsiz = sizeof(buf) - bsiz;
520 if (atomicio(write, ic->ic_fd, buf, bsiz) != bsiz)
521 warnx("nfsdcb: write(%s) failed: errno %d (%s)",
522 ic->ic_path, errno, strerror(errno));
525 event_add(&ic->ic_event, NULL);
529 imconv(struct idmap_client *ic, struct idmap_msg *im)
531 switch (im->im_conv) {
532 case IDMAP_CONV_IDTONAME:
535 warnx("%s %s: (%s) id \"%d\" -> name \"%s\"",
536 ic->ic_id, ic->ic_clid,
537 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
538 im->im_id, im->im_name);
540 case IDMAP_CONV_NAMETOID:
541 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
542 im->im_status |= IDMAP_STATUS_INVALIDMSG;
547 warnx("%s %s: (%s) name \"%s\" -> id \"%d\"",
548 ic->ic_id, ic->ic_clid,
549 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
550 im->im_name, im->im_id);
553 warnx("Invalid conversion type (%d) in message", im->im_conv);
554 im->im_status |= IDMAP_STATUS_INVALIDMSG;
560 nfscb(int fd, short which, void *data)
562 struct idmap_client *ic = data;
565 if (which != EV_READ)
568 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
570 warn("read(%s)", ic->ic_path);
578 if (atomicio(write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
579 warn("write(%s)", ic->ic_path);
581 event_add(&ic->ic_event, NULL);
585 nfsdreopen_one(struct idmap_client *ic)
590 warnx("ReOpening %s", ic->ic_path);
591 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
594 ic->ic_event.ev_fd = ic->ic_fd = fd;
595 if ((ic->ic_event.ev_flags & EVLIST_INIT) == 0) {
596 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
597 event_add(&ic->ic_event, NULL);
600 warnx("nfsdreopen: Opening '%s' failed: errno %d (%s)",
601 ic->ic_path, errno, strerror(errno));
606 * Note: nfsdreopen assumes nfsdopen has already been called
611 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
612 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
619 return ((nfsdopenone(&nfsd_ic[0], IC_NAMEID, path) == 0 &&
620 nfsdopenone(&nfsd_ic[1], IC_IDNAME, path) == 0) ? 0 : -1);
624 nfsdopenone(struct idmap_client *ic, short which, char *path)
628 whichstr = which == IC_IDNAME ? "idtoname" : "nametoid";
629 snprintf(ic->ic_path, sizeof(ic->ic_path),
630 "%s/nfs4.%s/channel", path, whichstr);
631 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
633 warnx("Opening %s failed: errno %d (%s)",
634 ic->ic_path, errno, strerror(errno));
638 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
639 event_add(&ic->ic_event, NULL);
641 ic->ic_which = which;
642 ic->ic_id = "Server";
643 strlcpy(ic->ic_clid, domain, sizeof(ic->ic_clid));
646 warnx("Opened %s", ic->ic_path);
652 nfsopen(struct idmap_client *ic)
654 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
657 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
658 fcntl(ic->ic_dirfd, F_NOTIFY,
659 DN_CREATE | DN_DELETE | DN_MULTISHOT);
662 warn("open(%s)", ic->ic_path);
666 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
667 event_add(&ic->ic_event, NULL);
668 fcntl(ic->ic_dirfd, F_SETSIG, 0);
669 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
671 warnx("Opened %s", ic->ic_path);
677 static int write_name(char *dest, char *localname, char *domain, size_t len)
679 if (strlen(localname) + 1 + strlen(domain) + 1 > len) {
680 return -ENOMEM; /* XXX: Is there an -ETOOLONG? */
682 strcpy(dest, localname);
684 strcat(dest, domain);
689 idtonameres(struct idmap_msg *im)
693 switch (im->im_type) {
694 case IDMAP_TYPE_USER:
695 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
696 sizeof(im->im_name));
698 write_name(im->im_name, nobodyuser, domain,
699 sizeof(im->im_name));
701 case IDMAP_TYPE_GROUP:
702 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
703 sizeof(im->im_name));
705 write_name(im->im_name, nobodygroup, domain,
706 sizeof(im->im_name));
709 /* XXX Hack? would rather return failure instead of writing nobody
710 * as above, but kernel seems not to deal well with that as of
712 im->im_status = IDMAP_STATUS_SUCCESS;
716 nametoidres(struct idmap_msg *im)
720 switch (im->im_type) {
721 case IDMAP_TYPE_USER:
722 ret = nfs4_name_to_uid(im->im_name, &im->im_id);
724 im->im_id = nobodyuid;
726 case IDMAP_TYPE_GROUP:
727 ret = nfs4_name_to_gid(im->im_name, &im->im_id);
729 im->im_id = nobodygid;
732 /* XXX Hack? would rather return failure instead of writing nobody
733 * as above, but kernel seems not to deal well with that as of
735 im->im_status = IDMAP_STATUS_SUCCESS;
739 validateascii(char *string, u_int32_t len)
743 for (i = 0; i < len; i++) {
744 if (string[i] == '\0')
747 if (string[i] & 0x80)
751 if (string[i] != '\0')
758 addfield(char **bpp, ssize_t *bsizp, char *fld)
761 ssize_t bsiz = *bsizp;
763 while ((ch = *fld++) != '\0' && bsiz > 0) {
770 bp += snprintf(bp, bsiz, "\\%03o", ch);
781 if (bsiz < 1 || ch != '\0')
794 getfield(char **bpp, char *fld, size_t fldsz)
799 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
802 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
805 while (*bp != '\0' && fldsz > 1) {
807 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
827 * mydaemon creates a pipe between the partent and child
828 * process. The parent process will wait until the
829 * child dies or writes a '1' on the pipe signaling
830 * that it started successfully.
832 int pipefds[2] = { -1, -1};
835 mydaemon(int nochdir, int noclose)
837 int pid, status, tempfd, fdmax, filedes;
839 if (pipe(pipefds) < 0)
840 err(1, "mydaemon: pipe() failed: errno %d (%s)\n", errno, strerror(errno));
842 if ((pid = fork ()) < 0)
843 err(1, "mydaemon: fork() failed: errno %d (%s)\n", errno, strerror(errno));
847 * Parent. Wait for status from child.
850 if (read(pipefds[0], &status, 1) != 1)
858 if (chdir ("/") == -1)
859 err(1, "mydaemon: chdir() failed: errno %d (%s)\n", errno, strerror(errno));
862 while (pipefds[1] <= 2) {
863 pipefds[1] = dup(pipefds[1]);
865 err(1, "mydaemon: dup() failed: errno %d (%s)\n", errno, strerror(errno));
869 tempfd = open("/dev/null", O_RDWR);
870 close(0); dup2(tempfd, 0);
871 close(1); dup2(tempfd, 1);
872 close(2); dup2(tempfd, 2);
873 fdmax = sysconf (_SC_OPEN_MAX);
874 for (filedes = 3; filedes < fdmax; filedes++)
875 if (filedes != pipefds[1])
886 if (pipefds[1] > 0) {
887 write(pipefds[1], &status, 1);