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);
387 * Loop around adding all users.
391 while (i < nid.nid_usermax && (pwd = getpwent())) {
394 * Yes, this is inefficient, but it is only done once when
395 * the daemon is started and will run in a fraction of a second
396 * for nid_usermax at 10000. If nid_usermax is cranked up to
397 * 100000, it will take several seconds, depending on the CPU.
399 for (j = 0; j < (i - start_uidpos); j++)
400 if (check_dups[j] == pwd->pw_uid) {
401 /* Found another entry for uid, so skip it */
407 check_dups[i - start_uidpos] = pwd->pw_uid;
408 nid.nid_uid = pwd->pw_uid;
409 nid.nid_name = pwd->pw_name;
410 nid.nid_namelen = strlen(pwd->pw_name);
411 if (manage_gids != 0) {
412 /* Get the group list for this user. */
414 if (getgrouplist(pwd->pw_name, pwd->pw_gid, grps,
416 syslog(LOG_ERR, "Group list too small");
417 nid.nid_ngroup = ngroup;
423 nid.nid_flag = NFSID_ADDUID;
425 printf("add uid=%d name=%s\n", nid.nid_uid, nid.nid_name);
427 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
429 errx(1, "Can't add user %s", pwd->pw_name);
435 * I should feel guilty for not calling this for all the above exit()
436 * upon error cases, but I don't.
445 * Temporarily block SIGUSR1 and SIGCHLD, so slaves[] can't
448 sigemptyset(&signew);
449 sigaddset(&signew, SIGUSR1);
450 sigaddset(&signew, SIGCHLD);
451 sigprocmask(SIG_BLOCK, &signew, NULL);
454 (void)signal(SIGHUP, SIG_IGN);
455 (void)signal(SIGINT, SIG_IGN);
456 (void)signal(SIGQUIT, SIG_IGN);
457 (void)signal(SIGTERM, SIG_IGN);
458 (void)signal(SIGUSR1, cleanup_term);
459 (void)signal(SIGCHLD, cleanup_term);
461 openlog("nfsuserd:", LOG_PID, LOG_DAEMON);
464 * Fork off the slave daemons that do the work. All the master
465 * does is kill them off and cleanup.
467 for (i = 0; i < nfsuserdcnt; i++) {
469 if (slaves[i] == 0) {
471 setproctitle("slave");
472 sigemptyset(&signew);
473 sigaddset(&signew, SIGUSR1);
474 sigprocmask(SIG_UNBLOCK, &signew, NULL);
480 syslog(LOG_ERR, "nfsuserd died: %m");
482 } else if (slaves[i] < 0) {
483 syslog(LOG_ERR, "fork: %m");
488 * Just wait for SIGUSR1 or a child to die and then...
489 * As the Governor of California would say, "Terminate them".
491 setproctitle("master");
492 sigemptyset(&signew);
498 * The nfsuserd rpc service
501 nfsuserdsrv(struct svc_req *rqstp, SVCXPRT *transp)
506 #if defined(INET) || defined(INET6)
511 struct nfsd_idargs nid;
515 struct sockaddr_in *fromsin, *sin;
518 struct sockaddr_in6 *fromsin6, *sin6;
519 char buf[INET6_ADDRSTRLEN];
523 * Only handle requests from localhost on a reserved port number.
524 * If the upcall is from a different address, call nfsbind_localhost()
525 * to check for a remapping of localhost, due to jails.
526 * (Since a reserved port # at localhost implies a client with
527 * local root, there won't be a security breach. This is about
528 * the only case I can think of where a reserved port # means
531 if (rqstp->rq_proc != NULLPROC) {
532 switch (fromip.ss_family) {
535 if (transp->xp_rtaddr.len < sizeof(*sin)) {
536 syslog(LOG_ERR, "xp_rtaddr too small");
537 svcerr_weakauth(transp);
540 sin = (struct sockaddr_in *)transp->xp_rtaddr.buf;
541 fromsin = (struct sockaddr_in *)&fromip;
542 sport = ntohs(sin->sin_port);
543 if (sport >= IPPORT_RESERVED) {
544 syslog(LOG_ERR, "not a reserved port#");
545 svcerr_weakauth(transp);
549 if (sin->sin_addr.s_addr != fromsin->sin_addr.s_addr)
550 ret = nfsbind_localhost();
551 if (ret == 0 || sin->sin_addr.s_addr !=
552 fromsin->sin_addr.s_addr) {
553 syslog(LOG_ERR, "bad from ip %s",
554 inet_ntoa(sin->sin_addr));
555 svcerr_weakauth(transp);
562 if (transp->xp_rtaddr.len < sizeof(*sin6)) {
563 syslog(LOG_ERR, "xp_rtaddr too small");
564 svcerr_weakauth(transp);
567 sin6 = (struct sockaddr_in6 *)transp->xp_rtaddr.buf;
568 fromsin6 = (struct sockaddr_in6 *)&fromip;
569 sport = ntohs(sin6->sin6_port);
570 if (sport >= IPV6PORT_RESERVED) {
571 syslog(LOG_ERR, "not a reserved port#");
572 svcerr_weakauth(transp);
576 if (!IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
577 &fromsin6->sin6_addr))
578 ret = nfsbind_localhost();
579 if (ret == 0 || !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
580 &fromsin6->sin6_addr)) {
581 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
582 INET6_ADDRSTRLEN) != NULL)
583 syslog(LOG_ERR, "bad from ip %s", buf);
585 syslog(LOG_ERR, "bad from ip6 addr");
586 svcerr_weakauth(transp);
593 switch (rqstp->rq_proc) {
595 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
596 syslog(LOG_ERR, "Can't send reply");
598 case RPCNFSUSERD_GETUID:
599 if (!svc_getargs(transp, (xdrproc_t)xdr_getid,
601 svcerr_decode(transp);
604 pwd = getpwuid((uid_t)info.id);
607 nid.nid_usertimeout = defusertimeout;
608 nid.nid_uid = pwd->pw_uid;
609 nid.nid_name = pwd->pw_name;
610 if (manage_gids != 0) {
611 /* Get the group list for this user. */
613 if (getgrouplist(pwd->pw_name, pwd->pw_gid,
615 syslog(LOG_ERR, "Group list too small");
616 nid.nid_ngroup = ngroup;
623 nid.nid_usertimeout = 5;
624 nid.nid_uid = (uid_t)info.id;
625 nid.nid_name = defaultuser;
629 nid.nid_namelen = strlen(nid.nid_name);
630 nid.nid_flag = NFSID_ADDUID;
631 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
634 syslog(LOG_ERR, "Can't add user %s\n", pwd->pw_name);
635 } else if (verbose) {
636 syslog(LOG_ERR,"Added uid=%d name=%s\n",
637 nid.nid_uid, nid.nid_name);
639 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
641 syslog(LOG_ERR, "Can't send reply");
643 case RPCNFSUSERD_GETGID:
644 if (!svc_getargs(transp, (xdrproc_t)xdr_getid,
646 svcerr_decode(transp);
649 grp = getgrgid((gid_t)info.id);
652 nid.nid_usertimeout = defusertimeout;
653 nid.nid_gid = grp->gr_gid;
654 nid.nid_name = grp->gr_name;
656 nid.nid_usertimeout = 5;
657 nid.nid_gid = (gid_t)info.id;
658 nid.nid_name = defaultgroup;
660 nid.nid_namelen = strlen(nid.nid_name);
663 nid.nid_flag = NFSID_ADDGID;
664 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
667 syslog(LOG_ERR, "Can't add group %s\n",
669 } else if (verbose) {
670 syslog(LOG_ERR,"Added gid=%d name=%s\n",
671 nid.nid_gid, nid.nid_name);
673 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
675 syslog(LOG_ERR, "Can't send reply");
677 case RPCNFSUSERD_GETUSER:
678 if (!svc_getargs(transp, (xdrproc_t)xdr_getname,
680 svcerr_decode(transp);
683 pwd = getpwnam(info.name);
686 nid.nid_usertimeout = defusertimeout;
687 nid.nid_uid = pwd->pw_uid;
688 nid.nid_name = pwd->pw_name;
690 nid.nid_usertimeout = 5;
691 nid.nid_uid = defaultuid;
692 nid.nid_name = info.name;
694 nid.nid_namelen = strlen(nid.nid_name);
697 nid.nid_flag = NFSID_ADDUSERNAME;
698 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
701 syslog(LOG_ERR, "Can't add user %s\n", pwd->pw_name);
702 } else if (verbose) {
703 syslog(LOG_ERR,"Added uid=%d name=%s\n",
704 nid.nid_uid, nid.nid_name);
706 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
708 syslog(LOG_ERR, "Can't send reply");
710 case RPCNFSUSERD_GETGROUP:
711 if (!svc_getargs(transp, (xdrproc_t)xdr_getname,
713 svcerr_decode(transp);
716 grp = getgrnam(info.name);
719 nid.nid_usertimeout = defusertimeout;
720 nid.nid_gid = grp->gr_gid;
721 nid.nid_name = grp->gr_name;
723 nid.nid_usertimeout = 5;
724 nid.nid_gid = defaultgid;
725 nid.nid_name = info.name;
727 nid.nid_namelen = strlen(nid.nid_name);
730 nid.nid_flag = NFSID_ADDGROUPNAME;
731 error = nfssvc(NFSSVC_IDNAME | NFSSVC_NEWSTRUCT, &nid);
734 syslog(LOG_ERR, "Can't add group %s\n",
736 } else if (verbose) {
737 syslog(LOG_ERR,"Added gid=%d name=%s\n",
738 nid.nid_gid, nid.nid_name);
740 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
742 syslog(LOG_ERR, "Can't send reply");
745 svcerr_noproc(transp);
751 * Xdr routine to get an id number
754 xdr_getid(XDR *xdrsp, caddr_t cp)
756 struct info *ifp = (struct info *)cp;
758 return (xdr_long(xdrsp, &ifp->id));
762 * Xdr routine to get a user name
765 xdr_getname(XDR *xdrsp, caddr_t cp)
767 struct info *ifp = (struct info *)cp;
770 if (!xdr_long(xdrsp, &len))
774 if (!xdr_opaque(xdrsp, ifp->name, len))
776 ifp->name[len] = '\0';
781 * Xdr routine to return the value.
784 xdr_retval(XDR *xdrsp, caddr_t cp)
786 struct info *ifp = (struct info *)cp;
790 return (xdr_long(xdrsp, &val));
794 * cleanup_term() called via SIGUSR1.
797 cleanup_term(int signo __unused)
805 * Ok, so I'm the master.
806 * As the Governor of California might say, "Terminate them".
809 for (i = 0; i < nfsuserdcnt; i++) {
810 if (slaves[i] != (pid_t)-1) {
812 kill(slaves[i], SIGUSR1);
817 * and wait for them to die
819 for (i = 0; i < cnt; i++)
820 wait3(NULL, 0, NULL);
823 * Finally, get rid of the socket
825 if (nfssvc(NFSSVC_NFSUSERDDELPORT, NULL) < 0) {
826 syslog(LOG_ERR, "Can't do nfssvc() to delete the port\n");
833 * Get the IP address that the localhost address maps to.
834 * This is needed when jails map localhost to another IP address.
837 nfsbind_localhost(void)
840 struct sockaddr_in sin;
843 struct sockaddr_in6 sin6;
848 switch (fromip.ss_family) {
851 s = socket(PF_INET6, SOCK_DGRAM, 0);
854 memset(&sin6, 0, sizeof(sin6));
855 sin6.sin6_len = sizeof(sin6);
856 sin6.sin6_family = AF_INET6;
857 sin6.sin6_addr = in6loopback;
859 ret = bind(s, (struct sockaddr *)&sin6, sizeof(sin6));
868 s = socket(PF_INET, SOCK_DGRAM, 0);
871 memset(&sin, 0, sizeof(sin));
872 sin.sin_len = sizeof(sin);
873 sin.sin_family = AF_INET;
874 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
876 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
884 memset(&fromip, 0, sizeof(fromip));
885 slen = sizeof(fromip);
886 ret = getsockname(s, (struct sockaddr *)&fromip, &slen);
898 "usage: nfsuserd [-usermax cache_size] [-usertimeout minutes] [-verbose] [-manage-gids] [-domain domain_name] [n]");