2 * Copyright (c) 2009 Rick Macklem, University of Guelph
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <sys/mount.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
39 #include <sys/ucred.h>
40 #include <sys/vnode.h>
43 #include <netinet/in.h>
45 #include <arpa/inet.h>
47 #include <nfs/nfssvc.h>
51 #include <fs/nfs/rpcv2.h>
52 #include <fs/nfs/nfsproto.h>
53 #include <fs/nfs/nfskpiport.h>
54 #include <fs/nfs/nfs.h>
69 * This program loads the password and group databases into the kernel
73 static void cleanup_term(int);
74 static void usage(void);
75 static void nfsuserdsrv(struct svc_req *, SVCXPRT *);
76 static bool_t xdr_getid(XDR *, caddr_t);
77 static bool_t xdr_getname(XDR *, caddr_t);
78 static bool_t xdr_retval(XDR *, caddr_t);
79 static int nfsbind_localhost(void);
82 #define MAXNFSUSERD 20
84 #define MAXUSERMAX 100000
86 #define DEFUSERMAX 200
87 #define DEFUSERTIMEOUT (1 * 60)
91 char name[MAXNAME + 1];
94 u_char *dnsname = "default.domain";
95 u_char *defaultuser = "nobody";
96 uid_t defaultuid = 65534;
97 u_char *defaultgroup = "nogroup";
98 gid_t defaultgid = 65533;
99 int verbose = 0, im_a_slave = 0, nfsuserdcnt = -1, forcestart = 0;
100 int defusertimeout = DEFUSERTIMEOUT, manage_gids = 0;
101 pid_t slaves[MAXNFSUSERD];
102 static struct sockaddr_storage fromip;
104 static struct in6_addr in6loopback = IN6ADDR_LOOPBACK_INIT;
108 main(int argc, char *argv[])
111 int error, fnd_dup, len, mustfreeai = 0, start_uidpos;
112 struct nfsd_idargs nid;
117 struct nfsuserd_args nargs;
119 char hostname[MAXHOSTNAMELEN + 1], *cp;
120 struct addrinfo *aip, hints;
121 static uid_t check_dups[MAXUSERMAX];
125 struct sockaddr_in *sin;
128 struct sockaddr_in6 *sin6;
132 if (modfind("nfscommon") < 0) {
133 /* Not present in kernel, try loading it */
134 if (kldload("nfscommon") < 0 ||
135 modfind("nfscommon") < 0)
136 errx(1, "Experimental nfs subsystem is not available");
140 * First, figure out what our domain name and Kerberos Realm
141 * seem to be. Command line args may override these later.
143 if (gethostname(hostname, MAXHOSTNAMELEN) == 0) {
144 if ((cp = strchr(hostname, '.')) != NULL &&
148 memset((void *)&hints, 0, sizeof (hints));
149 hints.ai_flags = AI_CANONNAME;
150 error = getaddrinfo(hostname, NULL, &hints, &aip);
152 if (aip->ai_canonname != NULL &&
153 (cp = strchr(aip->ai_canonname, '.')) != NULL
154 && *(cp + 1) != '\0') {
165 * See if this server handles IPv4 or IPv6 and set up the default
170 s = socket(PF_INET6, SOCK_DGRAM, 0);
172 fromip.ss_family = AF_INET6;
173 fromip.ss_len = sizeof(struct sockaddr_in6);
174 sin6 = (struct sockaddr_in6 *)&fromip;
175 sin6->sin6_addr = in6loopback;
181 s = socket(PF_INET, SOCK_DGRAM, 0);
183 fromip.ss_family = AF_INET;
184 fromip.ss_len = sizeof(struct sockaddr_in);
185 sin = (struct sockaddr_in *)&fromip;
186 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
192 err(1, "Can't create a inet/inet6 socket");
194 nid.nid_usermax = DEFUSERMAX;
195 nid.nid_usertimeout = defusertimeout;
200 if (!strcmp(*argv, "-domain")) {
205 strncpy(hostname, *argv, MAXHOSTNAMELEN);
206 hostname[MAXHOSTNAMELEN] = '\0';
208 } else if (!strcmp(*argv, "-verbose")) {
210 } else if (!strcmp(*argv, "-force")) {
212 } else if (!strcmp(*argv, "-manage-gids")) {
214 } else if (!strcmp(*argv, "-usermax")) {
220 if (i < MINUSERMAX || i > MAXUSERMAX) {
222 "usermax %d out of range %d<->%d\n", i,
223 MINUSERMAX, MAXUSERMAX);
227 } else if (!strcmp(*argv, "-usertimeout")) {
233 if (i < 0 || i > 100000) {
235 "usertimeout %d out of range 0<->100000\n",
239 nid.nid_usertimeout = defusertimeout = i * 60;
240 } else if (nfsuserdcnt == -1) {
241 nfsuserdcnt = atoi(*argv);
244 if (nfsuserdcnt > MAXNFSUSERD) {
245 warnx("nfsuserd count %d; reset to %d",
246 nfsuserdcnt, DEFNFSUSERD);
247 nfsuserdcnt = DEFNFSUSERD;
256 nfsuserdcnt = DEFNFSUSERD;
259 * Strip off leading and trailing '.'s in domain name and map
260 * alphabetics to lower case.
262 while (*dnsname == '.')
264 if (*dnsname == '\0')
265 errx(1, "Domain name all '.'");
266 len = strlen(dnsname);
267 cp = dnsname + len - 1;
273 for (i = 0; i < len; i++) {
274 if (!isascii(dnsname[i]))
275 errx(1, "Domain name has non-ascii char");
276 if (isupper(dnsname[i]))
277 dnsname[i] = tolower(dnsname[i]);
281 * If the nfsuserd died off ungracefully, this is necessary to
282 * get them to start again.
284 if (forcestart && nfssvc(NFSSVC_NFSUSERDDELPORT, NULL) < 0)
285 errx(1, "Can't do nfssvc() to delete the port");
289 "nfsuserd: domain=%s usermax=%d usertimeout=%d\n",
290 dnsname, nid.nid_usermax, nid.nid_usertimeout);
292 for (i = 0; i < nfsuserdcnt; i++)
293 slaves[i] = (pid_t)-1;
295 nargs.nuserd_family = fromip.ss_family;
297 * Set up the service port to accept requests via UDP from
298 * localhost (INADDR_LOOPBACK or IN6ADDR_LOOPBACK_INIT).
300 if ((sock = socket(nargs.nuserd_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
301 err(1, "cannot create udp socket");
304 * Not sure what this does, so I'll leave it here for now.
306 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
308 if ((udptransp = svcudp_create(sock)) == NULL)
309 err(1, "Can't set up socket");
312 * By not specifying a protocol, it is linked into the
313 * dispatch queue, but not registered with portmapper,
314 * which is just what I want.
316 if (!svc_register(udptransp, RPCPROG_NFSUSERD, RPCNFSUSERD_VERS,
318 err(1, "Can't register nfsuserd");
321 * Tell the kernel what my port# is.
323 nargs.nuserd_port = htons(udptransp->xp_port);
325 printf("portnum=0x%x\n", nargs.nuserd_port);
327 if (nfssvc(NFSSVC_NFSUSERDPORT | NFSSVC_NEWSTRUCT, &nargs) < 0) {
328 if (errno == EPERM) {
330 "Can't start nfsuserd when already running");
332 " If not running, use the -force option.\n");
334 fprintf(stderr, "Can't do nfssvc() to add port\n");
340 pwd = getpwnam(defaultuser);
342 nid.nid_uid = pwd->pw_uid;
344 nid.nid_uid = defaultuid;
345 grp = getgrnam(defaultgroup);
347 nid.nid_gid = grp->gr_gid;
349 nid.nid_gid = defaultgid;
350 nid.nid_name = dnsname;
351 nid.nid_namelen = strlen(nid.nid_name);
354 nid.nid_flag = NFSID_INITIALIZE;
356 printf("Initialize uid=%d gid=%d dns=%s\n", nid.nid_uid, nid.nid_gid,
359 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
361 errx(1, "Can't initialize nfs user/groups");
366 * Loop around adding all groups.
369 while (i < nid.nid_usermax && (grp = getgrent())) {
370 nid.nid_gid = grp->gr_gid;
371 nid.nid_name = grp->gr_name;
372 nid.nid_namelen = strlen(grp->gr_name);
375 nid.nid_flag = NFSID_ADDGID;
377 printf("add gid=%d name=%s\n", nid.nid_gid, nid.nid_name);
379 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
381 errx(1, "Can't add group %s", grp->gr_name);
388 * Loop around adding all users.
392 while (i < nid.nid_usermax && (pwd = getpwent())) {
395 * Yes, this is inefficient, but it is only done once when
396 * the daemon is started and will run in a fraction of a second
397 * for nid_usermax at 10000. If nid_usermax is cranked up to
398 * 100000, it will take several seconds, depending on the CPU.
400 for (j = 0; j < (i - start_uidpos); j++)
401 if (check_dups[j] == pwd->pw_uid) {
402 /* Found another entry for uid, so skip it */
408 check_dups[i - start_uidpos] = pwd->pw_uid;
409 nid.nid_uid = pwd->pw_uid;
410 nid.nid_name = pwd->pw_name;
411 nid.nid_namelen = strlen(pwd->pw_name);
412 if (manage_gids != 0) {
413 /* Get the group list for this user. */
415 if (getgrouplist(pwd->pw_name, pwd->pw_gid, grps,
417 syslog(LOG_ERR, "Group list too small");
418 nid.nid_ngroup = ngroup;
424 nid.nid_flag = NFSID_ADDUID;
426 printf("add uid=%d name=%s\n", nid.nid_uid, nid.nid_name);
428 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
430 errx(1, "Can't add user %s", pwd->pw_name);
437 * I should feel guilty for not calling this for all the above exit()
438 * upon error cases, but I don't.
447 * Temporarily block SIGUSR1 and SIGCHLD, so slaves[] can't
450 sigemptyset(&signew);
451 sigaddset(&signew, SIGUSR1);
452 sigaddset(&signew, SIGCHLD);
453 sigprocmask(SIG_BLOCK, &signew, NULL);
456 (void)signal(SIGHUP, SIG_IGN);
457 (void)signal(SIGINT, SIG_IGN);
458 (void)signal(SIGQUIT, SIG_IGN);
459 (void)signal(SIGTERM, SIG_IGN);
460 (void)signal(SIGUSR1, cleanup_term);
461 (void)signal(SIGCHLD, cleanup_term);
463 openlog("nfsuserd:", LOG_PID, LOG_DAEMON);
466 * Fork off the slave daemons that do the work. All the master
467 * does is kill them off and cleanup.
469 for (i = 0; i < nfsuserdcnt; i++) {
471 if (slaves[i] == 0) {
473 setproctitle("slave");
474 sigemptyset(&signew);
475 sigaddset(&signew, SIGUSR1);
476 sigprocmask(SIG_UNBLOCK, &signew, NULL);
482 syslog(LOG_ERR, "nfsuserd died: %m");
484 } else if (slaves[i] < 0) {
485 syslog(LOG_ERR, "fork: %m");
490 * Just wait for SIGUSR1 or a child to die and then...
491 * As the Governor of California would say, "Terminate them".
493 setproctitle("master");
494 sigemptyset(&signew);
500 * The nfsuserd rpc service
503 nfsuserdsrv(struct svc_req *rqstp, SVCXPRT *transp)
508 #if defined(INET) || defined(INET6)
513 struct nfsd_idargs nid;
517 struct sockaddr_in *fromsin, *sin;
520 struct sockaddr_in6 *fromsin6, *sin6;
521 char buf[INET6_ADDRSTRLEN];
525 * Only handle requests from localhost on a reserved port number.
526 * If the upcall is from a different address, call nfsbind_localhost()
527 * to check for a remapping of localhost, due to jails.
528 * (Since a reserved port # at localhost implies a client with
529 * local root, there won't be a security breach. This is about
530 * the only case I can think of where a reserved port # means
533 if (rqstp->rq_proc != NULLPROC) {
534 switch (fromip.ss_family) {
537 if (transp->xp_rtaddr.len < sizeof(*sin)) {
538 syslog(LOG_ERR, "xp_rtaddr too small");
539 svcerr_weakauth(transp);
542 sin = (struct sockaddr_in *)transp->xp_rtaddr.buf;
543 fromsin = (struct sockaddr_in *)&fromip;
544 sport = ntohs(sin->sin_port);
545 if (sport >= IPPORT_RESERVED) {
546 syslog(LOG_ERR, "not a reserved port#");
547 svcerr_weakauth(transp);
551 if (sin->sin_addr.s_addr != fromsin->sin_addr.s_addr)
552 ret = nfsbind_localhost();
553 if (ret == 0 || sin->sin_addr.s_addr !=
554 fromsin->sin_addr.s_addr) {
555 syslog(LOG_ERR, "bad from ip %s",
556 inet_ntoa(sin->sin_addr));
557 svcerr_weakauth(transp);
564 if (transp->xp_rtaddr.len < sizeof(*sin6)) {
565 syslog(LOG_ERR, "xp_rtaddr too small");
566 svcerr_weakauth(transp);
569 sin6 = (struct sockaddr_in6 *)transp->xp_rtaddr.buf;
570 fromsin6 = (struct sockaddr_in6 *)&fromip;
571 sport = ntohs(sin6->sin6_port);
572 if (sport >= IPV6PORT_RESERVED) {
573 syslog(LOG_ERR, "not a reserved port#");
574 svcerr_weakauth(transp);
578 if (!IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
579 &fromsin6->sin6_addr))
580 ret = nfsbind_localhost();
581 if (ret == 0 || !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
582 &fromsin6->sin6_addr)) {
583 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
584 INET6_ADDRSTRLEN) != NULL)
585 syslog(LOG_ERR, "bad from ip %s", buf);
587 syslog(LOG_ERR, "bad from ip6 addr");
588 svcerr_weakauth(transp);
595 switch (rqstp->rq_proc) {
597 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
598 syslog(LOG_ERR, "Can't send reply");
600 case RPCNFSUSERD_GETUID:
601 if (!svc_getargs(transp, (xdrproc_t)xdr_getid,
603 svcerr_decode(transp);
606 pwd = getpwuid((uid_t)info.id);
609 nid.nid_usertimeout = defusertimeout;
610 nid.nid_uid = pwd->pw_uid;
611 nid.nid_name = pwd->pw_name;
612 if (manage_gids != 0) {
613 /* Get the group list for this user. */
615 if (getgrouplist(pwd->pw_name, pwd->pw_gid,
617 syslog(LOG_ERR, "Group list too small");
618 nid.nid_ngroup = ngroup;
625 nid.nid_usertimeout = 5;
626 nid.nid_uid = (uid_t)info.id;
627 nid.nid_name = defaultuser;
631 nid.nid_namelen = strlen(nid.nid_name);
632 nid.nid_flag = NFSID_ADDUID;
633 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
636 syslog(LOG_ERR, "Can't add user %s\n", pwd->pw_name);
637 } else if (verbose) {
638 syslog(LOG_ERR,"Added uid=%d name=%s\n",
639 nid.nid_uid, nid.nid_name);
641 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
643 syslog(LOG_ERR, "Can't send reply");
645 case RPCNFSUSERD_GETGID:
646 if (!svc_getargs(transp, (xdrproc_t)xdr_getid,
648 svcerr_decode(transp);
651 grp = getgrgid((gid_t)info.id);
654 nid.nid_usertimeout = defusertimeout;
655 nid.nid_gid = grp->gr_gid;
656 nid.nid_name = grp->gr_name;
658 nid.nid_usertimeout = 5;
659 nid.nid_gid = (gid_t)info.id;
660 nid.nid_name = defaultgroup;
662 nid.nid_namelen = strlen(nid.nid_name);
665 nid.nid_flag = NFSID_ADDGID;
666 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
669 syslog(LOG_ERR, "Can't add group %s\n",
671 } else if (verbose) {
672 syslog(LOG_ERR,"Added gid=%d name=%s\n",
673 nid.nid_gid, nid.nid_name);
675 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
677 syslog(LOG_ERR, "Can't send reply");
679 case RPCNFSUSERD_GETUSER:
680 if (!svc_getargs(transp, (xdrproc_t)xdr_getname,
682 svcerr_decode(transp);
685 pwd = getpwnam(info.name);
688 nid.nid_usertimeout = defusertimeout;
689 nid.nid_uid = pwd->pw_uid;
690 nid.nid_name = pwd->pw_name;
692 nid.nid_usertimeout = 5;
693 nid.nid_uid = defaultuid;
694 nid.nid_name = info.name;
696 nid.nid_namelen = strlen(nid.nid_name);
699 nid.nid_flag = NFSID_ADDUSERNAME;
700 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
703 syslog(LOG_ERR, "Can't add user %s\n", pwd->pw_name);
704 } else if (verbose) {
705 syslog(LOG_ERR,"Added uid=%d name=%s\n",
706 nid.nid_uid, nid.nid_name);
708 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
710 syslog(LOG_ERR, "Can't send reply");
712 case RPCNFSUSERD_GETGROUP:
713 if (!svc_getargs(transp, (xdrproc_t)xdr_getname,
715 svcerr_decode(transp);
718 grp = getgrnam(info.name);
721 nid.nid_usertimeout = defusertimeout;
722 nid.nid_gid = grp->gr_gid;
723 nid.nid_name = grp->gr_name;
725 nid.nid_usertimeout = 5;
726 nid.nid_gid = defaultgid;
727 nid.nid_name = info.name;
729 nid.nid_namelen = strlen(nid.nid_name);
732 nid.nid_flag = NFSID_ADDGROUPNAME;
733 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
736 syslog(LOG_ERR, "Can't add group %s\n",
738 } else if (verbose) {
739 syslog(LOG_ERR,"Added gid=%d name=%s\n",
740 nid.nid_gid, nid.nid_name);
742 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
744 syslog(LOG_ERR, "Can't send reply");
747 svcerr_noproc(transp);
753 * Xdr routine to get an id number
756 xdr_getid(XDR *xdrsp, caddr_t cp)
758 struct info *ifp = (struct info *)cp;
760 return (xdr_long(xdrsp, &ifp->id));
764 * Xdr routine to get a user name
767 xdr_getname(XDR *xdrsp, caddr_t cp)
769 struct info *ifp = (struct info *)cp;
772 if (!xdr_long(xdrsp, &len))
776 if (!xdr_opaque(xdrsp, ifp->name, len))
778 ifp->name[len] = '\0';
783 * Xdr routine to return the value.
786 xdr_retval(XDR *xdrsp, caddr_t cp)
788 struct info *ifp = (struct info *)cp;
792 return (xdr_long(xdrsp, &val));
796 * cleanup_term() called via SIGUSR1.
799 cleanup_term(int signo __unused)
807 * Ok, so I'm the master.
808 * As the Governor of California might say, "Terminate them".
811 for (i = 0; i < nfsuserdcnt; i++) {
812 if (slaves[i] != (pid_t)-1) {
814 kill(slaves[i], SIGUSR1);
819 * and wait for them to die
821 for (i = 0; i < cnt; i++)
822 wait3(NULL, 0, NULL);
825 * Finally, get rid of the socket
827 if (nfssvc(NFSSVC_NFSUSERDDELPORT, NULL) < 0) {
828 syslog(LOG_ERR, "Can't do nfssvc() to delete the port\n");
835 * Get the IP address that the localhost address maps to.
836 * This is needed when jails map localhost to another IP address.
839 nfsbind_localhost(void)
842 struct sockaddr_in sin;
845 struct sockaddr_in6 sin6;
850 switch (fromip.ss_family) {
853 s = socket(PF_INET6, SOCK_DGRAM, 0);
856 memset(&sin6, 0, sizeof(sin6));
857 sin6.sin6_len = sizeof(sin6);
858 sin6.sin6_family = AF_INET6;
859 sin6.sin6_addr = in6loopback;
861 ret = bind(s, (struct sockaddr *)&sin6, sizeof(sin6));
870 s = socket(PF_INET, SOCK_DGRAM, 0);
873 memset(&sin, 0, sizeof(sin));
874 sin.sin_len = sizeof(sin);
875 sin.sin_family = AF_INET;
876 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
878 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
886 memset(&fromip, 0, sizeof(fromip));
887 slen = sizeof(fromip);
888 ret = getsockname(s, (struct sockaddr *)&fromip, &slen);
900 "usage: nfsuserd [-usermax cache_size] [-usertimeout minutes] [-verbose] [-manage-gids] [-domain domain_name] [n]");