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 <nfs/nfssvc.h>
47 #include <fs/nfs/rpcv2.h>
48 #include <fs/nfs/nfsproto.h>
49 #include <fs/nfs/nfskpiport.h>
50 #include <fs/nfs/nfs.h>
65 * This program loads the password and group databases into the kernel
69 static void cleanup_term(int);
70 static void usage(void);
71 static void nfsuserdsrv(struct svc_req *, SVCXPRT *);
72 static bool_t xdr_getid(XDR *, caddr_t);
73 static bool_t xdr_getname(XDR *, caddr_t);
74 static bool_t xdr_retval(XDR *, caddr_t);
77 #define MAXNFSUSERD 20
79 #define MAXUSERMAX 100000
81 #define DEFUSERMAX 200
82 #define DEFUSERTIMEOUT (1 * 60)
86 char name[MAXNAME + 1];
89 u_char *dnsname = "default.domain";
90 u_char *defaultuser = "nobody";
91 uid_t defaultuid = (uid_t)32767;
92 u_char *defaultgroup = "nogroup";
93 gid_t defaultgid = (gid_t)32767;
94 int verbose = 0, im_a_slave = 0, nfsuserdcnt = -1, forcestart = 0;
95 int defusertimeout = DEFUSERTIMEOUT;
96 pid_t slaves[MAXNFSUSERD];
99 main(int argc, char *argv[])
102 int error, fnd_dup, len, mustfreeai = 0, start_uidpos;
103 struct nfsd_idargs nid;
110 char hostname[MAXHOSTNAMELEN + 1], *cp;
111 struct addrinfo *aip, hints;
112 static uid_t check_dups[MAXUSERMAX];
114 if (modfind("nfscommon") < 0) {
115 /* Not present in kernel, try loading it */
116 if (kldload("nfscommon") < 0 ||
117 modfind("nfscommon") < 0)
118 errx(1, "Experimental nfs subsystem is not available");
122 * First, figure out what our domain name and Kerberos Realm
123 * seem to be. Command line args may override these later.
125 if (gethostname(hostname, MAXHOSTNAMELEN) == 0) {
126 if ((cp = strchr(hostname, '.')) != NULL &&
130 memset((void *)&hints, 0, sizeof (hints));
131 hints.ai_flags = AI_CANONNAME;
132 error = getaddrinfo(hostname, NULL, &hints, &aip);
134 if (aip->ai_canonname != NULL &&
135 (cp = strchr(aip->ai_canonname, '.')) != NULL
136 && *(cp + 1) != '\0') {
145 nid.nid_usermax = DEFUSERMAX;
146 nid.nid_usertimeout = defusertimeout;
151 if (!strcmp(*argv, "-domain")) {
156 strncpy(hostname, *argv, MAXHOSTNAMELEN);
157 hostname[MAXHOSTNAMELEN] = '\0';
159 } else if (!strcmp(*argv, "-verbose")) {
161 } else if (!strcmp(*argv, "-force")) {
163 } else if (!strcmp(*argv, "-usermax")) {
169 if (i < MINUSERMAX || i > MAXUSERMAX) {
171 "usermax %d out of range %d<->%d\n", i,
172 MINUSERMAX, MAXUSERMAX);
176 } else if (!strcmp(*argv, "-usertimeout")) {
182 if (i < 0 || i > 100000) {
184 "usertimeout %d out of range 0<->100000\n",
188 nid.nid_usertimeout = defusertimeout = i * 60;
189 } else if (nfsuserdcnt == -1) {
190 nfsuserdcnt = atoi(*argv);
193 if (nfsuserdcnt > MAXNFSUSERD) {
194 warnx("nfsuserd count %d; reset to %d",
195 nfsuserdcnt, DEFNFSUSERD);
196 nfsuserdcnt = DEFNFSUSERD;
205 nfsuserdcnt = DEFNFSUSERD;
208 * Strip off leading and trailing '.'s in domain name and map
209 * alphabetics to lower case.
211 while (*dnsname == '.')
213 if (*dnsname == '\0')
214 errx(1, "Domain name all '.'");
215 len = strlen(dnsname);
216 cp = dnsname + len - 1;
222 for (i = 0; i < len; i++) {
223 if (!isascii(dnsname[i]))
224 errx(1, "Domain name has non-ascii char");
225 if (isupper(dnsname[i]))
226 dnsname[i] = tolower(dnsname[i]);
230 * If the nfsuserd died off ungracefully, this is necessary to
231 * get them to start again.
233 if (forcestart && nfssvc(NFSSVC_NFSUSERDDELPORT, NULL) < 0)
234 errx(1, "Can't do nfssvc() to delete the port");
238 "nfsuserd: domain=%s usermax=%d usertimeout=%d\n",
239 dnsname, nid.nid_usermax, nid.nid_usertimeout);
241 for (i = 0; i < nfsuserdcnt; i++)
242 slaves[i] = (pid_t)-1;
245 * Set up the service port to accept requests via UDP from
246 * localhost (127.0.0.1).
248 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
249 err(1, "cannot create udp socket");
252 * Not sure what this does, so I'll leave it here for now.
254 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
256 if ((udptransp = svcudp_create(sock)) == NULL)
257 err(1, "Can't set up socket");
260 * By not specifying a protocol, it is linked into the
261 * dispatch queue, but not registered with portmapper,
262 * which is just what I want.
264 if (!svc_register(udptransp, RPCPROG_NFSUSERD, RPCNFSUSERD_VERS,
266 err(1, "Can't register nfsuserd");
269 * Tell the kernel what my port# is.
271 portnum = htons(udptransp->xp_port);
273 printf("portnum=0x%x\n", portnum);
275 if (nfssvc(NFSSVC_NFSUSERDPORT, (caddr_t)&portnum) < 0) {
276 if (errno == EPERM) {
278 "Can't start nfsuserd when already running");
280 " If not running, use the -force option.\n");
282 fprintf(stderr, "Can't do nfssvc() to add port\n");
288 pwd = getpwnam(defaultuser);
290 nid.nid_uid = pwd->pw_uid;
292 nid.nid_uid = defaultuid;
293 grp = getgrnam(defaultgroup);
295 nid.nid_gid = grp->gr_gid;
297 nid.nid_gid = defaultgid;
298 nid.nid_name = dnsname;
299 nid.nid_namelen = strlen(nid.nid_name);
300 nid.nid_flag = NFSID_INITIALIZE;
302 printf("Initialize uid=%d gid=%d dns=%s\n", nid.nid_uid, nid.nid_gid,
305 error = nfssvc(NFSSVC_IDNAME, &nid);
307 errx(1, "Can't initialize nfs user/groups");
312 * Loop around adding all groups.
315 while (i < nid.nid_usermax && (grp = getgrent())) {
316 nid.nid_gid = grp->gr_gid;
317 nid.nid_name = grp->gr_name;
318 nid.nid_namelen = strlen(grp->gr_name);
319 nid.nid_flag = NFSID_ADDGID;
321 printf("add gid=%d name=%s\n", nid.nid_gid, nid.nid_name);
323 error = nfssvc(NFSSVC_IDNAME, &nid);
325 errx(1, "Can't add group %s", grp->gr_name);
331 * Loop around adding all users.
335 while (i < nid.nid_usermax && (pwd = getpwent())) {
338 * Yes, this is inefficient, but it is only done once when
339 * the daemon is started and will run in a fraction of a second
340 * for nid_usermax at 10000. If nid_usermax is cranked up to
341 * 100000, it will take several seconds, depending on the CPU.
343 for (j = 0; j < (i - start_uidpos); j++)
344 if (check_dups[j] == pwd->pw_uid) {
345 /* Found another entry for uid, so skip it */
351 check_dups[i - start_uidpos] = pwd->pw_uid;
352 nid.nid_uid = pwd->pw_uid;
353 nid.nid_name = pwd->pw_name;
354 nid.nid_namelen = strlen(pwd->pw_name);
355 nid.nid_flag = NFSID_ADDUID;
357 printf("add uid=%d name=%s\n", nid.nid_uid, nid.nid_name);
359 error = nfssvc(NFSSVC_IDNAME, &nid);
361 errx(1, "Can't add user %s", pwd->pw_name);
367 * I should feel guilty for not calling this for all the above exit()
368 * upon error cases, but I don't.
377 * Temporarily block SIGUSR1 and SIGCHLD, so slaves[] can't
380 sigemptyset(&signew);
381 sigaddset(&signew, SIGUSR1);
382 sigaddset(&signew, SIGCHLD);
383 sigprocmask(SIG_BLOCK, &signew, NULL);
386 (void)signal(SIGHUP, SIG_IGN);
387 (void)signal(SIGINT, SIG_IGN);
388 (void)signal(SIGQUIT, SIG_IGN);
389 (void)signal(SIGTERM, SIG_IGN);
390 (void)signal(SIGUSR1, cleanup_term);
391 (void)signal(SIGCHLD, cleanup_term);
393 openlog("nfsuserd:", LOG_PID, LOG_DAEMON);
396 * Fork off the slave daemons that do the work. All the master
397 * does is kill them off and cleanup.
399 for (i = 0; i < nfsuserdcnt; i++) {
401 if (slaves[i] == 0) {
403 setproctitle("slave");
404 sigemptyset(&signew);
405 sigaddset(&signew, SIGUSR1);
406 sigprocmask(SIG_UNBLOCK, &signew, NULL);
412 syslog(LOG_ERR, "nfsuserd died: %m");
414 } else if (slaves[i] < 0) {
415 syslog(LOG_ERR, "fork: %m");
420 * Just wait for SIGUSR1 or a child to die and then...
421 * As the Governor of California would say, "Terminate them".
423 setproctitle("master");
424 sigemptyset(&signew);
430 * The nfsuserd rpc service
433 nfsuserdsrv(struct svc_req *rqstp, SVCXPRT *transp)
440 struct nfsd_idargs nid;
444 * Only handle requests from 127.0.0.1 on a reserved port number.
445 * (Since a reserved port # at localhost implies a client with
446 * local root, there won't be a security breach. This is about
447 * the only case I can think of where a reserved port # means
450 sport = ntohs(transp->xp_raddr.sin_port);
451 saddr = ntohl(transp->xp_raddr.sin_addr.s_addr);
452 if ((rqstp->rq_proc != NULLPROC && sport >= IPPORT_RESERVED) ||
453 saddr != 0x7f000001) {
454 syslog(LOG_ERR, "req from ip=0x%x port=%d\n", saddr, sport);
455 svcerr_weakauth(transp);
458 switch (rqstp->rq_proc) {
460 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
461 syslog(LOG_ERR, "Can't send reply");
463 case RPCNFSUSERD_GETUID:
464 if (!svc_getargs(transp, (xdrproc_t)xdr_getid,
466 svcerr_decode(transp);
469 pwd = getpwuid((uid_t)info.id);
472 nid.nid_usertimeout = defusertimeout;
473 nid.nid_uid = pwd->pw_uid;
474 nid.nid_name = pwd->pw_name;
476 nid.nid_usertimeout = 5;
477 nid.nid_uid = (uid_t)info.id;
478 nid.nid_name = defaultuser;
480 nid.nid_namelen = strlen(nid.nid_name);
481 nid.nid_flag = NFSID_ADDUID;
482 error = nfssvc(NFSSVC_IDNAME, &nid);
485 syslog(LOG_ERR, "Can't add user %s\n", pwd->pw_name);
486 } else if (verbose) {
487 syslog(LOG_ERR,"Added uid=%d name=%s\n",
488 nid.nid_uid, nid.nid_name);
490 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
492 syslog(LOG_ERR, "Can't send reply");
494 case RPCNFSUSERD_GETGID:
495 if (!svc_getargs(transp, (xdrproc_t)xdr_getid,
497 svcerr_decode(transp);
500 grp = getgrgid((gid_t)info.id);
503 nid.nid_usertimeout = defusertimeout;
504 nid.nid_gid = grp->gr_gid;
505 nid.nid_name = grp->gr_name;
507 nid.nid_usertimeout = 5;
508 nid.nid_gid = (gid_t)info.id;
509 nid.nid_name = defaultgroup;
511 nid.nid_namelen = strlen(nid.nid_name);
512 nid.nid_flag = NFSID_ADDGID;
513 error = nfssvc(NFSSVC_IDNAME, &nid);
516 syslog(LOG_ERR, "Can't add group %s\n",
518 } else if (verbose) {
519 syslog(LOG_ERR,"Added gid=%d name=%s\n",
520 nid.nid_gid, nid.nid_name);
522 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
524 syslog(LOG_ERR, "Can't send reply");
526 case RPCNFSUSERD_GETUSER:
527 if (!svc_getargs(transp, (xdrproc_t)xdr_getname,
529 svcerr_decode(transp);
532 pwd = getpwnam(info.name);
535 nid.nid_usertimeout = defusertimeout;
536 nid.nid_uid = pwd->pw_uid;
537 nid.nid_name = pwd->pw_name;
539 nid.nid_usertimeout = 5;
540 nid.nid_uid = defaultuid;
541 nid.nid_name = info.name;
543 nid.nid_namelen = strlen(nid.nid_name);
544 nid.nid_flag = NFSID_ADDUSERNAME;
545 error = nfssvc(NFSSVC_IDNAME, &nid);
548 syslog(LOG_ERR, "Can't add user %s\n", pwd->pw_name);
549 } else if (verbose) {
550 syslog(LOG_ERR,"Added uid=%d name=%s\n",
551 nid.nid_uid, nid.nid_name);
553 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
555 syslog(LOG_ERR, "Can't send reply");
557 case RPCNFSUSERD_GETGROUP:
558 if (!svc_getargs(transp, (xdrproc_t)xdr_getname,
560 svcerr_decode(transp);
563 grp = getgrnam(info.name);
566 nid.nid_usertimeout = defusertimeout;
567 nid.nid_gid = grp->gr_gid;
568 nid.nid_name = grp->gr_name;
570 nid.nid_usertimeout = 5;
571 nid.nid_gid = defaultgid;
572 nid.nid_name = info.name;
574 nid.nid_namelen = strlen(nid.nid_name);
575 nid.nid_flag = NFSID_ADDGROUPNAME;
576 error = nfssvc(NFSSVC_IDNAME, &nid);
579 syslog(LOG_ERR, "Can't add group %s\n",
581 } else if (verbose) {
582 syslog(LOG_ERR,"Added gid=%d name=%s\n",
583 nid.nid_gid, nid.nid_name);
585 if (!svc_sendreply(transp, (xdrproc_t)xdr_retval,
587 syslog(LOG_ERR, "Can't send reply");
590 svcerr_noproc(transp);
596 * Xdr routine to get an id number
599 xdr_getid(XDR *xdrsp, caddr_t cp)
601 struct info *ifp = (struct info *)cp;
603 return (xdr_long(xdrsp, &ifp->id));
607 * Xdr routine to get a user name
610 xdr_getname(XDR *xdrsp, caddr_t cp)
612 struct info *ifp = (struct info *)cp;
615 if (!xdr_long(xdrsp, &len))
619 if (!xdr_opaque(xdrsp, ifp->name, len))
621 ifp->name[len] = '\0';
626 * Xdr routine to return the value.
629 xdr_retval(XDR *xdrsp, caddr_t cp)
631 struct info *ifp = (struct info *)cp;
635 return (xdr_long(xdrsp, &val));
639 * cleanup_term() called via SIGUSR1.
642 cleanup_term(int signo __unused)
650 * Ok, so I'm the master.
651 * As the Governor of California might say, "Terminate them".
654 for (i = 0; i < nfsuserdcnt; i++) {
655 if (slaves[i] != (pid_t)-1) {
657 kill(slaves[i], SIGUSR1);
662 * and wait for them to die
664 for (i = 0; i < cnt; i++)
665 wait3(NULL, 0, NULL);
668 * Finally, get rid of the socket
670 if (nfssvc(NFSSVC_NFSUSERDDELPORT, NULL) < 0) {
671 syslog(LOG_ERR, "Can't do nfssvc() to delete the port\n");
682 "usage: nfsuserd [-usermax cache_size] [-usertimeout minutes] [-verbose] [-domain domain_name] [n]");