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 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 int serverstart = 1, clientstart = 1;
157 conf_path = _PATH_IDMAPDCONF;
158 nobodyuser = NFS4NOBODY_USER;
159 nobodygroup = NFS4NOBODY_GROUP;
160 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
162 #define GETOPTSTR "vfd:p:U:G:c:CS"
163 opterr=0; /* Turn off error messages */
164 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
168 if (strchr(GETOPTSTR, optopt))
169 errx(1, "'-%c' option requires an argument.", optopt);
171 errx(1, "'-%c' is an invalid argument.", optopt);
176 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
177 warn("Skipping configuration file \"%s\"", conf_path);
180 verbose = conf_get_num("General", "Verbosity", 0);
181 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
182 if (xpipefsdir != NULL)
183 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
184 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
185 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
186 nfs4_init_name_mapping(conf_path);
189 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
198 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
203 errx(1, "the -d, -U, and -G options have been removed;"
204 " please use the configuration file instead.");
215 if (!serverstart && !clientstart)
216 errx(1, "it is illegal to specify both -C and -S");
218 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
220 if ((pw = getpwnam(nobodyuser)) == NULL)
221 errx(1, "Could not find user \"%s\"", nobodyuser);
222 nobodyuid = pw->pw_uid;
224 if ((gr = getgrnam(nobodygroup)) == NULL)
225 errx(1, "Could not find group \"%s\"", nobodygroup);
226 nobodygid = gr->gr_gid;
234 nfsdret = nfsdopen(NFSD_DIR);
237 struct timeval now = {
242 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
243 err(1, "open(%s)", pipefsdir);
245 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
246 err(1, "fcntl(%s)", pipefsdir);
247 if (fcntl(fd, F_NOTIFY,
248 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1)
249 err(1, "fcntl(%s)", pipefsdir);
253 /* These events are persistent */
254 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
255 signal_add(&rootdirev, NULL);
256 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
257 signal_add(&clntdirev, NULL);
258 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
259 signal_add(&svrdirev, NULL);
261 /* Fetch current state */
262 /* (Delay till start of event_dispatch to avoid possibly losing
263 * a SIGUSR1 between here and the call to event_dispatch().) */
264 evtimer_set(&initialize, dirscancb, &icq);
265 evtimer_add(&initialize, &now);
268 if (nfsdret != 0 && fd == 0)
269 errx(1, "Neither NFS client nor NFSd found");
273 if (event_dispatch() < 0)
274 errx(1, "event_dispatch: returns errno %d (%s)", errno, strerror(errno));
280 dirscancb(int fd, short which, void *data)
283 struct dirent **ents;
284 struct idmap_client *ic;
286 struct idmap_clientq *icq = data;
288 nent = scandir(pipefsdir, &ents, NULL, alphasort);
290 warn("scandir(%s)", pipefsdir);
294 for (i = 0; i < nent; i++) {
295 if (ents[i]->d_reclen > 4 &&
296 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
297 TAILQ_FOREACH(ic, icq, ic_next)
298 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
303 if ((ic = calloc(1, sizeof(*ic))) == NULL)
305 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
306 sizeof(ic->ic_clid));
308 snprintf(path, sizeof(path), "%s/%s",
309 pipefsdir, ents[i]->d_name);
311 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
312 warn("open(%s)", path);
317 strlcat(path, "/idmap", sizeof(path));
318 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
321 warnx("New client: %s", ic->ic_clid);
323 if (nfsopen(ic) == -1) {
329 ic->ic_id = "Client";
331 TAILQ_INSERT_TAIL(icq, ic, ic_next);
338 TAILQ_FOREACH(ic, icq, ic_next) {
339 if (!ic->ic_scanned) {
340 event_del(&ic->ic_event);
343 TAILQ_REMOVE(icq, ic, ic_next);
345 warnx("Stale client: %s", ic->ic_clid);
346 warnx("\t-> closed %s", ic->ic_path);
356 svrreopen(int fd, short which, void *data)
362 clntscancb(int fd, short which, void *data)
364 struct idmap_clientq *icq = data;
365 struct idmap_client *ic;
367 TAILQ_FOREACH(ic, icq, ic_next)
368 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
370 TAILQ_REMOVE(icq, ic, ic_next);
376 nfsdcb(int fd, short which, void *data)
378 struct idmap_client *ic = data;
380 u_char buf[IDMAP_MAXMSGSZ + 1];
382 char *bp, typebuf[IDMAP_MAXMSGSZ],
383 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
385 if (which != EV_READ)
388 if ((len = read(ic->ic_fd, buf, sizeof(buf))) == -1) {
389 warnx("nfsdcb: read(%s) failed: errno %d (%s)",
390 ic->ic_path, errno, strerror(errno));
394 /* Get rid of newline and terminate buffer*/
398 memset(&im, 0, sizeof(im));
400 /* Authentication name -- ignored for now*/
401 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
402 warnx("nfsdcb: bad authentication name in upcall\n");
405 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
406 warnx("nfsdcb: bad type in upcall\n");
410 warnx("nfsdcb: authbuf=%s authtype=%s", authbuf, typebuf);
412 im.im_type = strcmp(typebuf, "user") == 0 ?
413 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
415 switch (ic->ic_which) {
417 im.im_conv = IDMAP_CONV_NAMETOID;
418 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
419 warnx("nfsdcb: bad name in upcall\n");
424 im.im_conv = IDMAP_CONV_IDTONAME;
425 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
426 warnx("nfsdcb: bad id in upcall\n");
429 if ((im.im_id = strtoul(buf1, (char **)NULL, 10)) == ULONG_MAX &&
431 warnx("nfsdcb: id '%s' too big!\n", buf1);
437 warnx("Unknown which type %d", ic->ic_which);
447 /* Authentication name */
448 addfield(&bp, &bsiz, authbuf);
450 switch (ic->ic_which) {
453 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
454 addfield(&bp, &bsiz, p);
456 addfield(&bp, &bsiz, im.im_name);
457 #define NFSD_EXPIRY 300 /* seconds */
459 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
460 addfield(&bp, &bsiz, buf1);
462 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
463 addfield(&bp, &bsiz, buf1);
465 //if (bsiz == sizeof(buf)) /* XXX */
472 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
473 addfield(&bp, &bsiz, p);
475 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
476 addfield(&bp, &bsiz, buf1);
478 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
479 addfield(&bp, &bsiz, buf1);
481 addfield(&bp, &bsiz, im.im_name);
487 warnx("Unknown which type %d", ic->ic_which);
491 bsiz = sizeof(buf) - bsiz;
493 if (atomicio(write, ic->ic_fd, buf, bsiz) != bsiz)
494 warnx("nfsdcb: write(%s) failed: errno %d (%s)",
495 ic->ic_path, errno, strerror(errno));
498 event_add(&ic->ic_event, NULL);
502 imconv(struct idmap_client *ic, struct idmap_msg *im)
504 switch (im->im_conv) {
505 case IDMAP_CONV_IDTONAME:
508 warnx("%s %s: (%s) id \"%d\" -> name \"%s\"",
509 ic->ic_id, ic->ic_clid,
510 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
511 im->im_id, im->im_name);
513 case IDMAP_CONV_NAMETOID:
514 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
515 im->im_status |= IDMAP_STATUS_INVALIDMSG;
520 warnx("%s %s: (%s) name \"%s\" -> id \"%d\"",
521 ic->ic_id, ic->ic_clid,
522 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
523 im->im_name, im->im_id);
526 warnx("Invalid conversion type (%d) in message", im->im_conv);
527 im->im_status |= IDMAP_STATUS_INVALIDMSG;
533 nfscb(int fd, short which, void *data)
535 struct idmap_client *ic = data;
538 if (which != EV_READ)
541 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
543 warn("read(%s)", ic->ic_path);
551 if (atomicio(write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
552 warn("write(%s)", ic->ic_path);
554 event_add(&ic->ic_event, NULL);
558 nfsdreopen_one(struct idmap_client *ic)
563 warnx("ReOpening %s", ic->ic_path);
564 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
567 ic->ic_event.ev_fd = ic->ic_fd = fd;
568 if ((ic->ic_event.ev_flags & EVLIST_INIT) == 0) {
569 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
570 event_add(&ic->ic_event, NULL);
573 warnx("nfsdreopen: Opening '%s' failed: errno %d (%s)",
574 ic->ic_path, errno, strerror(errno));
579 * Note: nfsdreopen assumes nfsdopen has already been called
584 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
585 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
592 return ((nfsdopenone(&nfsd_ic[0], IC_NAMEID, path) == 0 &&
593 nfsdopenone(&nfsd_ic[1], IC_IDNAME, path) == 0) ? 0 : -1);
597 nfsdopenone(struct idmap_client *ic, short which, char *path)
601 whichstr = which == IC_IDNAME ? "idtoname" : "nametoid";
602 snprintf(ic->ic_path, sizeof(ic->ic_path),
603 "%s/nfs4.%s/channel", path, whichstr);
604 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
606 warnx("Opening %s failed: errno %d (%s)",
607 ic->ic_path, errno, strerror(errno));
611 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
612 event_add(&ic->ic_event, NULL);
614 ic->ic_which = which;
615 ic->ic_id = "Server";
616 strlcpy(ic->ic_clid, "Server", strlen("Server"));
619 warnx("Opened %s", ic->ic_path);
625 nfsopen(struct idmap_client *ic)
627 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
630 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
631 fcntl(ic->ic_dirfd, F_NOTIFY,
632 DN_CREATE | DN_DELETE | DN_MULTISHOT);
635 warn("open(%s)", ic->ic_path);
639 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
640 event_add(&ic->ic_event, NULL);
641 fcntl(ic->ic_dirfd, F_SETSIG, 0);
642 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
644 warnx("Opened %s", ic->ic_path);
650 static int write_name(char *dest, char *localname, char *domain, size_t len)
652 if (strlen(localname) + 1 + strlen(domain) + 1 > len) {
653 return -ENOMEM; /* XXX: Is there an -ETOOLONG? */
655 strcpy(dest, localname);
657 strcat(dest, domain);
662 idtonameres(struct idmap_msg *im)
664 char domain[NFS4_MAX_DOMAIN_LEN];
667 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
668 switch (im->im_type) {
669 case IDMAP_TYPE_USER:
670 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
671 sizeof(im->im_name));
673 write_name(im->im_name, nobodyuser, domain,
674 sizeof(im->im_name));
676 case IDMAP_TYPE_GROUP:
677 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
678 sizeof(im->im_name));
680 write_name(im->im_name, nobodygroup, domain,
681 sizeof(im->im_name));
684 /* XXX Hack? would rather return failure instead of writing nobody
685 * as above, but kernel seems not to deal well with that as of
687 im->im_status = IDMAP_STATUS_SUCCESS;
691 nametoidres(struct idmap_msg *im)
695 switch (im->im_type) {
696 case IDMAP_TYPE_USER:
697 ret = nfs4_name_to_uid(im->im_name, &im->im_id);
699 im->im_id = nobodyuid;
701 case IDMAP_TYPE_GROUP:
702 ret = nfs4_name_to_gid(im->im_name, &im->im_id);
704 im->im_id = nobodygid;
707 /* XXX Hack? would rather return failure instead of writing nobody
708 * as above, but kernel seems not to deal well with that as of
710 im->im_status = IDMAP_STATUS_SUCCESS;
714 validateascii(char *string, u_int32_t len)
718 for (i = 0; i < len; i++) {
719 if (string[i] == '\0')
722 if (string[i] & 0x80)
726 if (string[i] != '\0')
733 addfield(char **bpp, ssize_t *bsizp, char *fld)
736 ssize_t bsiz = *bsizp;
738 while ((ch = *fld++) != '\0' && bsiz > 0) {
745 bp += snprintf(bp, bsiz, "\\%03o", ch);
756 if (bsiz < 1 || ch != '\0')
769 getfield(char **bpp, char *fld, size_t fldsz)
774 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
777 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
780 while (*bp != '\0' && fldsz > 1) {
782 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
802 * mydaemon creates a pipe between the partent and child
803 * process. The parent process will wait until the
804 * child dies or writes a '1' on the pipe signaling
805 * that it started successfully.
807 int pipefds[2] = { -1, -1};
810 mydaemon(int nochdir, int noclose)
812 int pid, status, tempfd, fdmax, filedes;
814 if (pipe(pipefds) < 0)
815 err(1, "mydaemon: pipe() failed: errno %d (%s)\n", errno, strerror(errno));
817 if ((pid = fork ()) < 0)
818 err(1, "mydaemon: fork() failed: errno %d (%s)\n", errno, strerror(errno));
822 * Parent. Wait for status from child.
825 if (read(pipefds[0], &status, 1) != 1)
833 if (chdir ("/") == -1)
834 err(1, "mydaemon: chdir() failed: errno %d (%s)\n", errno, strerror(errno));
837 while (pipefds[1] <= 2) {
838 pipefds[1] = dup(pipefds[1]);
840 err(1, "mydaemon: dup() failed: errno %d (%s)\n", errno, strerror(errno));
844 tempfd = open("/dev/null", O_RDWR);
845 close(0); dup2(tempfd, 0);
846 close(1); dup2(tempfd, 1);
847 close(2); dup2(tempfd, 2);
848 fdmax = sysconf (_SC_OPEN_MAX);
849 for (filedes = 3; filedes < fdmax; filedes++)
850 if (filedes != pipefds[1])
861 if (pipefds[1] > 0) {
862 write(pipefds[1], &status, 1);