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"
67 #endif /* HAVE_CONFIG_H */
74 #define PIPEFS_DIR "/var/lib/nfs/rpc_pipefs/"
78 #define NFSD_DIR "/proc/net/rpc"
81 #ifndef NFS4NOBODY_USER
82 #define NFS4NOBODY_USER "nobody"
85 #ifndef NFS4NOBODY_GROUP
86 #define NFS4NOBODY_GROUP "nobody"
90 #define CONF_SAVE(w, f) do { \
97 #define IC_IDNAME_CHAN NFSD_DIR "/nfs4.idtoname/channel"
98 #define IC_IDNAME_FLUSH NFSD_DIR "/nfs4.idtoname/flush"
101 #define IC_NAMEID_CHAN NFSD_DIR "/nfs4.nametoid/channel"
102 #define IC_NAMEID_FLUSH NFSD_DIR "/nfs4.nametoid/flush"
104 struct idmap_client {
108 char ic_path[PATH_MAX];
112 struct event ic_event;
113 TAILQ_ENTRY(idmap_client) ic_next;
115 static struct idmap_client nfsd_ic[2] = {
116 {IC_IDNAME, "Server", "", IC_IDNAME_CHAN, -1, -1, 0},
117 {IC_NAMEID, "Server", "", IC_NAMEID_CHAN, -1, -1, 0},
120 TAILQ_HEAD(idmap_clientq, idmap_client);
122 static void dirscancb(int, short, void *);
123 static void clntscancb(int, short, void *);
124 static void svrreopen(int, short, void *);
125 static int nfsopen(struct idmap_client *);
126 static void nfscb(int, short, void *);
127 static void nfsdcb(int, short, void *);
128 static int validateascii(char *, u_int32_t);
129 static int addfield(char **, ssize_t *, char *);
130 static int getfield(char **, char *, size_t);
132 static void imconv(struct idmap_client *, struct idmap_msg *);
133 static void idtonameres(struct idmap_msg *);
134 static void nametoidres(struct idmap_msg *);
136 static int nfsdopen();
137 static int nfsdopenone(struct idmap_client *);
138 static void nfsdreopen(void);
140 size_t strlcat(char *, const char *, size_t);
141 size_t strlcpy(char *, const char *, size_t);
142 ssize_t atomicio(ssize_t (*)(), int, void *, size_t);
143 void mydaemon(int, int);
144 void release_parent();
146 static int verbose = 0;
147 static char pipefsdir[PATH_MAX];
148 static char *nobodyuser, *nobodygroup;
149 static uid_t nobodyuid;
150 static gid_t nobodygid;
156 flush_nfsd_cache(char *path, time_t now)
161 sprintf(stime, "%ld\n", now);
162 fd = open(path, O_RDWR);
165 write(fd, stime, strlen(stime));
171 flush_nfsd_idmap_cache(void)
173 time_t now = time(NULL);
176 ret = flush_nfsd_cache(IC_IDNAME_FLUSH, now);
179 ret = flush_nfsd_cache(IC_NAMEID_FLUSH, now);
184 msg_format(char *rtnbuff, int rtnbuffsize, int errval,
185 const char *fmt, va_list args)
190 vsnprintf(buff, sizeof(buff), fmt, args);
192 if ((n = strlen(buff)) > 0 && buff[n-1] == '\n')
195 snprintf(rtnbuff, rtnbuffsize, "%s: %s", buff, strerror(errval));
199 idmapd_warn(const char *fmt, ...)
201 int errval = errno; /* save this! */
206 msg_format(buff, sizeof(buff), errval, fmt, args);
209 syslog(LOG_WARNING, "%s", buff);
213 idmapd_warnx(const char *fmt, ...)
218 vsyslog(LOG_WARNING, fmt, args);
223 idmapd_err(int eval, const char *fmt, ...)
225 int errval = errno; /* save this! */
230 msg_format(buff, sizeof(buff), errval, fmt, args);
233 syslog(LOG_ERR, "%s", buff);
238 idmapd_errx(int eval, const char *fmt, ...)
243 vsyslog(LOG_ERR, fmt, args);
249 main(int argc, char **argv)
251 int fd = 0, opt, fg = 0, nfsdret = -1;
252 struct idmap_clientq icq;
253 struct event rootdirev, clntdirev, svrdirev;
254 struct event initialize;
258 char *xpipefsdir = NULL;
259 int serverstart = 1, clientstart = 1;
263 conf_path = _PATH_IDMAPDCONF;
264 nobodyuser = NFS4NOBODY_USER;
265 nobodygroup = NFS4NOBODY_GROUP;
266 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
268 if ((progname = strrchr(argv[0], '/')))
272 openlog(progname, LOG_PID, LOG_DAEMON);
274 #define GETOPTSTR "vfd:p:U:G:c:CS"
275 opterr=0; /* Turn off error messages */
276 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
280 if (strchr(GETOPTSTR, optopt))
281 errx(1, "'-%c' option requires an argument.", optopt);
283 errx(1, "'-%c' is an invalid argument.", optopt);
288 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
289 warn("Skipping configuration file \"%s\"", conf_path);
292 verbose = conf_get_num("General", "Verbosity", 0);
293 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
294 if (xpipefsdir != NULL)
295 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
296 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
297 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
298 nfs4_init_name_mapping(conf_path);
301 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
310 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
315 errx(1, "the -d, -U, and -G options have been removed;"
316 " please use the configuration file instead.");
327 if (!serverstart && !clientstart)
328 errx(1, "it is illegal to specify both -C and -S");
330 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
332 if ((pw = getpwnam(nobodyuser)) == NULL)
333 errx(1, "Could not find user \"%s\"", nobodyuser);
334 nobodyuid = pw->pw_uid;
336 if ((gr = getgrnam(nobodygroup)) == NULL)
337 errx(1, "Could not find group \"%s\"", nobodygroup);
338 nobodygid = gr->gr_gid;
346 nfsdret = nfsdopen();
348 ret = flush_nfsd_idmap_cache();
351 "main: Failed to flush nfsd idmap cache\n");
356 struct timeval now = {
361 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
362 idmapd_err(1, "main: open(%s)", pipefsdir);
364 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
365 idmapd_err(1, "main: fcntl(%s)", pipefsdir);
367 if (fcntl(fd, F_NOTIFY,
368 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1)
369 idmapd_err(1, "main: fcntl(%s)", pipefsdir);
373 /* These events are persistent */
374 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
375 signal_add(&rootdirev, NULL);
376 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
377 signal_add(&clntdirev, NULL);
378 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
379 signal_add(&svrdirev, NULL);
381 /* Fetch current state */
382 /* (Delay till start of event_dispatch to avoid possibly losing
383 * a SIGUSR1 between here and the call to event_dispatch().) */
384 evtimer_set(&initialize, dirscancb, &icq);
385 evtimer_add(&initialize, &now);
388 if (nfsdret != 0 && fd == 0)
389 idmapd_errx(1, "main: Neither NFS client nor NFSd found");
393 if (event_dispatch() < 0)
394 idmapd_errx(1, "main: event_dispatch returns errno %d (%s)",
395 errno, strerror(errno));
401 dirscancb(int fd, short which, void *data)
404 struct dirent **ents;
405 struct idmap_client *ic;
407 struct idmap_clientq *icq = data;
409 nent = scandir(pipefsdir, &ents, NULL, alphasort);
411 idmapd_warn("dirscancb: scandir(%s)", pipefsdir);
415 for (i = 0; i < nent; i++) {
416 if (ents[i]->d_reclen > 4 &&
417 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
418 TAILQ_FOREACH(ic, icq, ic_next)
419 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
424 if ((ic = calloc(1, sizeof(*ic))) == NULL)
426 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
427 sizeof(ic->ic_clid));
429 snprintf(path, sizeof(path), "%s/%s",
430 pipefsdir, ents[i]->d_name);
432 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
433 idmapd_warn("dirscancb: open(%s)", path);
438 strlcat(path, "/idmap", sizeof(path));
439 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
442 idmapd_warnx("New client: %s", ic->ic_clid);
444 if (nfsopen(ic) == -1) {
450 ic->ic_id = "Client";
452 TAILQ_INSERT_TAIL(icq, ic, ic_next);
459 TAILQ_FOREACH(ic, icq, ic_next) {
460 if (!ic->ic_scanned) {
461 event_del(&ic->ic_event);
464 TAILQ_REMOVE(icq, ic, ic_next);
466 idmapd_warnx("Stale client: %s", ic->ic_clid);
467 idmapd_warnx("\t-> closed %s", ic->ic_path);
477 svrreopen(int fd, short which, void *data)
483 clntscancb(int fd, short which, void *data)
485 struct idmap_clientq *icq = data;
486 struct idmap_client *ic;
488 TAILQ_FOREACH(ic, icq, ic_next)
489 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
491 TAILQ_REMOVE(icq, ic, ic_next);
497 nfsdcb(int fd, short which, void *data)
499 struct idmap_client *ic = data;
501 u_char buf[IDMAP_MAXMSGSZ + 1];
503 char *bp, typebuf[IDMAP_MAXMSGSZ],
504 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
507 if (which != EV_READ)
510 if ((len = read(ic->ic_fd, buf, sizeof(buf))) == -1) {
511 idmapd_warnx("nfsdcb: read(%s) failed: errno %d (%s)",
512 ic->ic_path, errno, strerror(errno));
516 /* Get rid of newline and terminate buffer*/
520 memset(&im, 0, sizeof(im));
522 /* Authentication name -- ignored for now*/
523 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
524 idmapd_warnx("nfsdcb: bad authentication name in upcall\n");
527 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
528 idmapd_warnx("nfsdcb: bad type in upcall\n");
532 idmapd_warnx("nfsdcb: authbuf=%s authtype=%s",
535 im.im_type = strcmp(typebuf, "user") == 0 ?
536 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
538 switch (ic->ic_which) {
540 im.im_conv = IDMAP_CONV_NAMETOID;
541 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
542 idmapd_warnx("nfsdcb: bad name in upcall\n");
547 im.im_conv = IDMAP_CONV_IDTONAME;
548 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
549 idmapd_warnx("nfsdcb: bad id in upcall\n");
552 tmp = strtoul(buf1, (char **)NULL, 10);
553 im.im_id = (u_int32_t)tmp;
554 if ((tmp == ULONG_MAX && errno == ERANGE)
555 || (unsigned long)im.im_id != tmp) {
556 idmapd_warnx("nfsdcb: id '%s' too big!\n", buf1);
561 idmapd_warnx("nfsdcb: Unknown which type %d", ic->ic_which);
571 /* Authentication name */
572 addfield(&bp, &bsiz, authbuf);
574 switch (ic->ic_which) {
577 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
578 addfield(&bp, &bsiz, p);
580 addfield(&bp, &bsiz, im.im_name);
581 #define NFSD_EXPIRY 300 /* seconds */
583 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
584 addfield(&bp, &bsiz, buf1);
586 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
587 addfield(&bp, &bsiz, buf1);
589 //if (bsiz == sizeof(buf)) /* XXX */
596 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
597 addfield(&bp, &bsiz, p);
599 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
600 addfield(&bp, &bsiz, buf1);
602 snprintf(buf1, sizeof(buf1), "%lu", time(NULL) + NFSD_EXPIRY);
603 addfield(&bp, &bsiz, buf1);
605 addfield(&bp, &bsiz, im.im_name);
611 idmapd_warnx("nfsdcb: Unknown which type %d", ic->ic_which);
615 bsiz = sizeof(buf) - bsiz;
617 if (atomicio(write, ic->ic_fd, buf, bsiz) != bsiz)
618 idmapd_warnx("nfsdcb: write(%s) failed: errno %d (%s)",
619 ic->ic_path, errno, strerror(errno));
622 event_add(&ic->ic_event, NULL);
626 imconv(struct idmap_client *ic, struct idmap_msg *im)
628 switch (im->im_conv) {
629 case IDMAP_CONV_IDTONAME:
632 idmapd_warnx("%s %s: (%s) id \"%d\" -> name \"%s\"",
633 ic->ic_id, ic->ic_clid,
634 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
635 im->im_id, im->im_name);
637 case IDMAP_CONV_NAMETOID:
638 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
639 im->im_status |= IDMAP_STATUS_INVALIDMSG;
644 idmapd_warnx("%s %s: (%s) name \"%s\" -> id \"%d\"",
645 ic->ic_id, ic->ic_clid,
646 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
647 im->im_name, im->im_id);
650 idmapd_warnx("imconv: Invalid conversion type (%d) in message",
652 im->im_status |= IDMAP_STATUS_INVALIDMSG;
658 nfscb(int fd, short which, void *data)
660 struct idmap_client *ic = data;
663 if (which != EV_READ)
666 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
668 idmapd_warn("nfscb: read(%s)", ic->ic_path);
676 if (atomicio(write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
677 idmapd_warn("nfscb: write(%s)", ic->ic_path);
679 event_add(&ic->ic_event, NULL);
683 nfsdreopen_one(struct idmap_client *ic)
688 idmapd_warnx("ReOpening %s", ic->ic_path);
690 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
693 if ((ic->ic_event.ev_flags & EVLIST_INIT))
694 event_del(&ic->ic_event);
696 ic->ic_event.ev_fd = ic->ic_fd = fd;
697 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
698 event_add(&ic->ic_event, NULL);
700 idmapd_warnx("nfsdreopen: Opening '%s' failed: errno %d (%s)",
701 ic->ic_path, errno, strerror(errno));
708 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
709 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
716 return ((nfsdopenone(&nfsd_ic[IC_NAMEID]) == 0 &&
717 nfsdopenone(&nfsd_ic[IC_IDNAME]) == 0) ? 0 : -1);
721 nfsdopenone(struct idmap_client *ic)
723 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
725 idmapd_warnx("nfsdopenone: Opening %s failed: "
727 ic->ic_path, errno, strerror(errno));
731 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
732 event_add(&ic->ic_event, NULL);
735 idmapd_warnx("Opened %s", ic->ic_path);
741 nfsopen(struct idmap_client *ic)
743 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
746 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
747 fcntl(ic->ic_dirfd, F_NOTIFY,
748 DN_CREATE | DN_DELETE | DN_MULTISHOT);
751 idmapd_warn("nfsopen: open(%s)", ic->ic_path);
755 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
756 event_add(&ic->ic_event, NULL);
757 fcntl(ic->ic_dirfd, F_SETSIG, 0);
758 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
760 idmapd_warnx("Opened %s", ic->ic_path);
767 idtonameres(struct idmap_msg *im)
769 char domain[NFS4_MAX_DOMAIN_LEN];
772 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
773 switch (im->im_type) {
774 case IDMAP_TYPE_USER:
775 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
776 sizeof(im->im_name));
778 if (strlen(nobodyuser) < sizeof(im->im_name))
779 strcpy(im->im_name, nobodyuser);
781 strcpy(im->im_name, NFS4NOBODY_USER);
784 case IDMAP_TYPE_GROUP:
785 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
786 sizeof(im->im_name));
788 if (strlen(nobodygroup) < sizeof(im->im_name))
789 strcpy(im->im_name, nobodygroup);
791 strcpy(im->im_name, NFS4NOBODY_GROUP);
796 im->im_status = IDMAP_STATUS_SUCCESS;
800 nametoidres(struct idmap_msg *im)
806 /* XXX: nobody fallbacks shouldn't always happen:
807 * server id -> name should be OK
808 * client name -> id should be OK
809 * but not otherwise */
810 /* XXX: move nobody stuff to library calls
811 * (nfs4_get_nobody_user(domain), nfs4_get_nobody_group(domain)) */
812 /* XXX: should make this call higher up in the call chain (so we'd
813 * have a chance on looking up server/whatever. */
814 switch (im->im_type) {
815 case IDMAP_TYPE_USER:
816 ret = nfs4_name_to_uid(im->im_name, &uid);
817 im->im_id = (u_int32_t) uid;
819 im->im_id = nobodyuid;
821 case IDMAP_TYPE_GROUP:
822 ret = nfs4_name_to_gid(im->im_name, &gid);
823 im->im_id = (u_int32_t) gid;
825 im->im_id = nobodygid;
829 im->im_status = IDMAP_STATUS_SUCCESS;
833 validateascii(char *string, u_int32_t len)
837 for (i = 0; i < len; i++) {
838 if (string[i] == '\0')
841 if (string[i] & 0x80)
845 if (string[i] != '\0')
852 addfield(char **bpp, ssize_t *bsizp, char *fld)
855 ssize_t bsiz = *bsizp;
857 while ((ch = *fld++) != '\0' && bsiz > 0) {
864 bp += snprintf(bp, bsiz, "\\%03o", ch);
875 if (bsiz < 1 || ch != '\0')
888 getfield(char **bpp, char *fld, size_t fldsz)
893 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
896 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
899 while (*bp != '\0' && fldsz > 1) {
901 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
921 * mydaemon creates a pipe between the partent and child
922 * process. The parent process will wait until the
923 * child dies or writes a '1' on the pipe signaling
924 * that it started successfully.
926 int pipefds[2] = { -1, -1};
929 mydaemon(int nochdir, int noclose)
931 int pid, status, tempfd;
933 if (pipe(pipefds) < 0)
934 err(1, "mydaemon: pipe() failed: errno %d", errno);
936 if ((pid = fork ()) < 0)
937 err(1, "mydaemon: fork() failed: errno %d", errno);
941 * Parent. Wait for status from child.
944 if (read(pipefds[0], &status, 1) != 1)
952 if (chdir ("/") == -1)
953 err(1, "mydaemon: chdir() failed: errno %d", errno);
956 while (pipefds[1] <= 2) {
957 pipefds[1] = dup(pipefds[1]);
959 err(1, "mydaemon: dup() failed: errno %d", errno);
963 tempfd = open("/dev/null", O_RDWR);
977 if (pipefds[1] > 0) {
978 write(pipefds[1], &status, 1);