nfsd.c revision 1.59 1 1.59 christos /* $NetBSD: nfsd.c,v 1.59 2012/08/13 08:19:11 christos Exp $ */
2 1.16 cgd
3 1.1 cgd /*
4 1.11 mycroft * Copyright (c) 1989, 1993, 1994
5 1.11 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.43 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.23 lukem #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.54 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 1.54 lukem The Regents of the University of California. All rights reserved.");
39 1.35 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.16 cgd #if 0
43 1.18 fvdl static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95";
44 1.16 cgd #else
45 1.59 christos __RCSID("$NetBSD: nfsd.c,v 1.59 2012/08/13 08:19:11 christos Exp $");
46 1.16 cgd #endif
47 1.18 fvdl #endif /* not lint */
48 1.1 cgd
49 1.11 mycroft #include <sys/param.h>
50 1.1 cgd #include <sys/ioctl.h>
51 1.1 cgd #include <sys/stat.h>
52 1.1 cgd #include <sys/wait.h>
53 1.11 mycroft #include <sys/uio.h>
54 1.11 mycroft #include <sys/ucred.h>
55 1.1 cgd #include <sys/mount.h>
56 1.1 cgd #include <sys/socket.h>
57 1.1 cgd #include <sys/socketvar.h>
58 1.39 itojun #include <poll.h>
59 1.11 mycroft
60 1.1 cgd #include <rpc/rpc.h>
61 1.1 cgd #include <rpc/pmap_clnt.h>
62 1.1 cgd #include <rpc/pmap_prot.h>
63 1.11 mycroft
64 1.1 cgd #include <nfs/rpcv2.h>
65 1.18 fvdl #include <nfs/nfsproto.h>
66 1.11 mycroft #include <nfs/nfs.h>
67 1.11 mycroft
68 1.11 mycroft #include <err.h>
69 1.11 mycroft #include <errno.h>
70 1.11 mycroft #include <fcntl.h>
71 1.11 mycroft #include <grp.h>
72 1.11 mycroft #include <pwd.h>
73 1.50 yamt #include <pthread.h>
74 1.11 mycroft #include <signal.h>
75 1.11 mycroft #include <stdio.h>
76 1.11 mycroft #include <stdlib.h>
77 1.25 perry #include <string.h>
78 1.19 mycroft #include <syslog.h>
79 1.11 mycroft #include <unistd.h>
80 1.32 fvdl #include <netdb.h>
81 1.1 cgd
82 1.1 cgd /* Global defs */
83 1.1 cgd #ifdef DEBUG
84 1.55 pooka #define syslog(e, s, args...) \
85 1.55 pooka do { \
86 1.55 pooka fprintf(stderr,(s), ## args); \
87 1.55 pooka fprintf(stderr, "\n"); \
88 1.55 pooka } while (/*CONSTCOND*/0)
89 1.58 joerg static int debug = 1;
90 1.1 cgd #else
91 1.58 joerg static int debug = 0;
92 1.1 cgd #endif
93 1.11 mycroft
94 1.58 joerg static void nonfs(int);
95 1.58 joerg __dead static void usage(void);
96 1.1 cgd
97 1.50 yamt static void *
98 1.56 pooka worker(void *dummy)
99 1.50 yamt {
100 1.50 yamt struct nfsd_srvargs nsd;
101 1.50 yamt int nfssvc_flag;
102 1.50 yamt
103 1.51 yamt pthread_setname_np(pthread_self(), "slave", NULL);
104 1.50 yamt nfssvc_flag = NFSSVC_NFSD;
105 1.50 yamt memset(&nsd, 0, sizeof(nsd));
106 1.50 yamt while (nfssvc(nfssvc_flag, &nsd) < 0) {
107 1.50 yamt if (errno != ENEEDAUTH) {
108 1.50 yamt syslog(LOG_ERR, "nfssvc: %m");
109 1.50 yamt exit(1);
110 1.50 yamt }
111 1.50 yamt nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
112 1.50 yamt }
113 1.50 yamt
114 1.50 yamt return NULL;
115 1.50 yamt }
116 1.50 yamt
117 1.59 christos struct conf {
118 1.59 christos struct addrinfo *ai;
119 1.59 christos struct netconfig *nc;
120 1.59 christos struct netbuf nb;
121 1.59 christos struct pollfd pfd;
122 1.59 christos };
123 1.59 christos
124 1.59 christos #define NFS_UDP4 0
125 1.59 christos #define NFS_TCP4 1
126 1.59 christos #define NFS_UDP6 2
127 1.59 christos #define NFS_TCP6 3
128 1.59 christos
129 1.59 christos static int cfg_family[] = { PF_INET, PF_INET, PF_INET6, PF_INET6 };
130 1.59 christos static const char *cfg_netconf[] = { "udp", "tcp", "udp6", "tcp6" };
131 1.59 christos static int cfg_socktype[] = {
132 1.59 christos SOCK_DGRAM, SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM };
133 1.59 christos static int cfg_protocol[] = {
134 1.59 christos IPPROTO_UDP, IPPROTO_TCP, IPPROTO_UDP, IPPROTO_TCP };
135 1.59 christos
136 1.59 christos static int
137 1.59 christos tryconf(struct conf *cfg, int t, int reregister)
138 1.59 christos {
139 1.59 christos struct addrinfo hints;
140 1.59 christos int ecode;
141 1.59 christos
142 1.59 christos memset(cfg, 0, sizeof(cfg));
143 1.59 christos memset(&hints, 0, sizeof hints);
144 1.59 christos hints.ai_flags = AI_PASSIVE;
145 1.59 christos hints.ai_family = cfg_family[t];
146 1.59 christos hints.ai_socktype = cfg_socktype[t];
147 1.59 christos hints.ai_protocol = cfg_protocol[t];
148 1.59 christos
149 1.59 christos ecode = getaddrinfo(NULL, "nfs", &hints, &cfg->ai);
150 1.59 christos if (ecode != 0) {
151 1.59 christos syslog(LOG_ERR, "getaddrinfo %s: %s", cfg_netconf[t],
152 1.59 christos gai_strerror(ecode));
153 1.59 christos return -1;
154 1.59 christos }
155 1.59 christos
156 1.59 christos cfg->nc = getnetconfigent(cfg_netconf[t]);
157 1.59 christos
158 1.59 christos if (cfg->nc == NULL) {
159 1.59 christos syslog(LOG_ERR, "getnetconfigent %s failed: %m",
160 1.59 christos cfg_netconf[t]);
161 1.59 christos return -1;
162 1.59 christos }
163 1.59 christos
164 1.59 christos cfg->nb.buf = cfg->ai->ai_addr;
165 1.59 christos cfg->nb.len = cfg->nb.maxlen = cfg->ai->ai_addrlen;
166 1.59 christos if (reregister)
167 1.59 christos if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb)) {
168 1.59 christos syslog(LOG_ERR, "rpcb_set %s failed", cfg_netconf[t]);
169 1.59 christos freenetconfigent(cfg->nc);
170 1.59 christos cfg->nc = NULL;
171 1.59 christos return -1;
172 1.59 christos }
173 1.59 christos return 0;
174 1.59 christos }
175 1.59 christos
176 1.59 christos static int
177 1.59 christos setupsock(struct conf *cfg, struct pollfd *set, int p)
178 1.59 christos {
179 1.59 christos int sock;
180 1.59 christos struct nfsd_args nfsdargs;
181 1.59 christos struct addrinfo *ai = cfg->ai;
182 1.59 christos int on = 1;
183 1.59 christos
184 1.59 christos sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
185 1.59 christos
186 1.59 christos if (sock == -1) {
187 1.59 christos syslog(LOG_ERR, "can't create %s socket: %m", cfg_netconf[p]);
188 1.59 christos return -1;
189 1.59 christos }
190 1.59 christos if (cfg_family[p] == PF_INET6) {
191 1.59 christos if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on,
192 1.59 christos sizeof(on)) == -1) {
193 1.59 christos syslog(LOG_ERR, "can't set v6-only binding for %s "
194 1.59 christos "socket: %m", cfg_netconf[p]);
195 1.59 christos goto out;
196 1.59 christos }
197 1.59 christos }
198 1.59 christos
199 1.59 christos if (cfg_protocol[p] == IPPROTO_TCP) {
200 1.59 christos if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
201 1.59 christos sizeof(on)) == -1) {
202 1.59 christos syslog(LOG_ERR, "setsockopt SO_REUSEADDR for %s: %m",
203 1.59 christos cfg_netconf[p]);
204 1.59 christos goto out;
205 1.59 christos }
206 1.59 christos }
207 1.59 christos
208 1.59 christos if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
209 1.59 christos syslog(LOG_ERR, "can't bind %s addr: %m", cfg_netconf[p]);
210 1.59 christos goto out;
211 1.59 christos }
212 1.59 christos
213 1.59 christos if (cfg_protocol[p] == IPPROTO_TCP) {
214 1.59 christos if (listen(sock, 5) == -1) {
215 1.59 christos syslog(LOG_ERR, "listen failed");
216 1.59 christos goto out;
217 1.59 christos }
218 1.59 christos }
219 1.59 christos
220 1.59 christos if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb) ||
221 1.59 christos !rpcb_set(RPCPROG_NFS, 3, cfg->nc, &cfg->nb)) {
222 1.59 christos syslog(LOG_ERR, "can't register with %s portmap",
223 1.59 christos cfg_netconf[p]);
224 1.59 christos goto out;
225 1.59 christos }
226 1.59 christos
227 1.59 christos
228 1.59 christos if (cfg_protocol[p] == IPPROTO_TCP)
229 1.59 christos set->fd = sock;
230 1.59 christos else {
231 1.59 christos nfsdargs.sock = sock;
232 1.59 christos nfsdargs.name = NULL;
233 1.59 christos nfsdargs.namelen = 0;
234 1.59 christos if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
235 1.59 christos syslog(LOG_ERR, "can't add %s socket", cfg_netconf[p]);
236 1.59 christos goto out;
237 1.59 christos }
238 1.59 christos (void)close(sock);
239 1.59 christos }
240 1.59 christos return 0;
241 1.59 christos out:
242 1.59 christos (void)close(sock);
243 1.59 christos return -1;
244 1.59 christos }
245 1.59 christos
246 1.1 cgd /*
247 1.1 cgd * Nfs server daemon mostly just a user context for nfssvc()
248 1.11 mycroft *
249 1.1 cgd * 1 - do file descriptor and signal cleanup
250 1.50 yamt * 2 - create the nfsd thread(s)
251 1.11 mycroft * 3 - create server socket(s)
252 1.11 mycroft * 4 - register socket with portmap
253 1.11 mycroft *
254 1.28 lukem * For connectionless protocols, just pass the socket into the kernel via
255 1.11 mycroft * nfssvc().
256 1.11 mycroft * For connection based sockets, loop doing accepts. When you get a new
257 1.28 lukem * socket from accept, pass the msgsock into the kernel via nfssvc().
258 1.1 cgd * The arguments are:
259 1.11 mycroft * -r - reregister with portmapper
260 1.59 christos * -t - support only tcp nfs clients
261 1.59 christos * -u - support only udp nfs clients
262 1.59 christos * -n num how many threads to create.
263 1.59 christos * -4 - use only ipv4
264 1.59 christos * -6 - use only ipv6
265 1.1 cgd */
266 1.11 mycroft int
267 1.58 joerg main(int argc, char *argv[])
268 1.1 cgd {
269 1.59 christos struct conf cfg[4];
270 1.59 christos struct pollfd set[__arraycount(cfg)];
271 1.59 christos int ch, connect_type_cnt;
272 1.59 christos size_t i, nfsdcnt;
273 1.59 christos int reregister;
274 1.59 christos int tcpflag, udpflag;
275 1.59 christos int ip6flag, ip4flag;
276 1.59 christos int s, compat;
277 1.11 mycroft
278 1.11 mycroft #define DEFNFSDCNT 4
279 1.11 mycroft nfsdcnt = DEFNFSDCNT;
280 1.59 christos compat = reregister = 0;
281 1.59 christos tcpflag = udpflag = 1;
282 1.59 christos ip6flag = ip4flag = 1;
283 1.59 christos #define GETOPT "46n:rtu"
284 1.59 christos #define USAGE "[-46rtu] [-n num_servers]"
285 1.30 thorpej while ((ch = getopt(argc, argv, GETOPT)) != -1) {
286 1.11 mycroft switch (ch) {
287 1.32 fvdl case '6':
288 1.32 fvdl ip6flag = 1;
289 1.59 christos ip4flag = 0;
290 1.32 fvdl s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
291 1.32 fvdl if (s < 0 && (errno == EPROTONOSUPPORT ||
292 1.32 fvdl errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
293 1.32 fvdl ip6flag = 0;
294 1.32 fvdl else
295 1.32 fvdl close(s);
296 1.32 fvdl break;
297 1.59 christos case '4':
298 1.59 christos ip6flag = 0;
299 1.59 christos ip4flag = 1;
300 1.59 christos s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
301 1.59 christos if (s < 0 && (errno == EPROTONOSUPPORT ||
302 1.59 christos errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
303 1.59 christos ip4flag = 0;
304 1.59 christos else
305 1.59 christos close(s);
306 1.59 christos break;
307 1.11 mycroft case 'n':
308 1.11 mycroft nfsdcnt = atoi(optarg);
309 1.53 yamt if (nfsdcnt < 1) {
310 1.59 christos warnx("nfsd count %zu; reset to %d", nfsdcnt,
311 1.59 christos DEFNFSDCNT);
312 1.11 mycroft nfsdcnt = DEFNFSDCNT;
313 1.11 mycroft }
314 1.11 mycroft break;
315 1.1 cgd case 'r':
316 1.11 mycroft reregister = 1;
317 1.1 cgd break;
318 1.1 cgd case 't':
319 1.59 christos compat |= 2;
320 1.11 mycroft tcpflag = 1;
321 1.59 christos udpflag = 0;
322 1.1 cgd break;
323 1.1 cgd case 'u':
324 1.59 christos compat |= 1;
325 1.59 christos tcpflag = 0;
326 1.11 mycroft udpflag = 1;
327 1.11 mycroft break;
328 1.1 cgd default:
329 1.1 cgd case '?':
330 1.1 cgd usage();
331 1.59 christos }
332 1.30 thorpej }
333 1.11 mycroft argv += optind;
334 1.11 mycroft argc -= optind;
335 1.1 cgd
336 1.59 christos if (compat == 3) {
337 1.59 christos warnx("Old -tu options detected; enabling both udp and tcp.");
338 1.59 christos warnx("This is the default behavior now and you can remove");
339 1.59 christos warnx("all options.");
340 1.59 christos tcpflag = udpflag = 1;
341 1.59 christos if (ip6flag == 1 && ip4flag == 0)
342 1.59 christos ip4flag = 1;
343 1.1 cgd }
344 1.31 soren
345 1.1 cgd if (debug == 0) {
346 1.1 cgd daemon(0, 0);
347 1.11 mycroft (void)signal(SIGHUP, SIG_IGN);
348 1.11 mycroft (void)signal(SIGINT, SIG_IGN);
349 1.11 mycroft (void)signal(SIGQUIT, SIG_IGN);
350 1.11 mycroft (void)signal(SIGSYS, nonfs);
351 1.1 cgd }
352 1.11 mycroft
353 1.59 christos openlog("nfsd", LOG_PID, LOG_DAEMON);
354 1.32 fvdl
355 1.59 christos for (i = 0; i < __arraycount(cfg); i++) {
356 1.59 christos if (ip4flag == 0 && cfg_family[i] == PF_INET)
357 1.59 christos continue;
358 1.59 christos if (ip6flag == 0 && cfg_family[i] == PF_INET6)
359 1.59 christos continue;
360 1.59 christos if (tcpflag == 0 && cfg_protocol[i] == IPPROTO_TCP)
361 1.59 christos continue;
362 1.59 christos if (udpflag == 0 && cfg_protocol[i] == IPPROTO_UDP)
363 1.59 christos continue;
364 1.59 christos tryconf(&cfg[i], i, reregister);
365 1.8 cgd }
366 1.32 fvdl
367 1.11 mycroft for (i = 0; i < nfsdcnt; i++) {
368 1.50 yamt pthread_t t;
369 1.50 yamt int error;
370 1.50 yamt
371 1.56 pooka error = pthread_create(&t, NULL, worker, NULL);
372 1.50 yamt if (error) {
373 1.50 yamt errno = error;
374 1.50 yamt syslog(LOG_ERR, "pthread_create: %m");
375 1.32 fvdl exit(1);
376 1.32 fvdl }
377 1.11 mycroft }
378 1.11 mycroft
379 1.11 mycroft connect_type_cnt = 0;
380 1.59 christos for (i = 0; i < __arraycount(cfg); i++) {
381 1.59 christos set[i].fd = -1;
382 1.59 christos set[i].events = POLLIN;
383 1.59 christos set[i].revents = 0;
384 1.59 christos
385 1.59 christos if (cfg[i].nc == NULL)
386 1.59 christos continue;
387 1.59 christos
388 1.59 christos setupsock(&cfg[i], &set[i], i);
389 1.59 christos if (set[i].fd != -1)
390 1.59 christos connect_type_cnt++;
391 1.32 fvdl
392 1.59 christos }
393 1.11 mycroft
394 1.11 mycroft if (connect_type_cnt == 0)
395 1.11 mycroft exit(0);
396 1.11 mycroft
397 1.51 yamt pthread_setname_np(pthread_self(), "master", NULL);
398 1.11 mycroft
399 1.11 mycroft /*
400 1.11 mycroft * Loop forever accepting connections and passing the sockets
401 1.11 mycroft * into the kernel for the mounts.
402 1.11 mycroft */
403 1.11 mycroft for (;;) {
404 1.59 christos if (poll(set, __arraycount(set), INFTIM) == -1) {
405 1.49 elad syslog(LOG_ERR, "poll failed: %m");
406 1.41 mycroft exit(1);
407 1.11 mycroft }
408 1.38 mycroft
409 1.59 christos for (i = 0; i < __arraycount(set); i++) {
410 1.59 christos struct nfsd_args nfsdargs;
411 1.59 christos struct sockaddr_storage ss;
412 1.59 christos socklen_t len;
413 1.59 christos int msgsock;
414 1.59 christos int on = 1;
415 1.59 christos
416 1.59 christos if ((set[i].revents & POLLIN) == 0)
417 1.59 christos continue;
418 1.59 christos len = sizeof(ss);
419 1.59 christos if ((msgsock = accept(set[i].fd,
420 1.59 christos (struct sockaddr *)&ss, &len)) == -1) {
421 1.59 christos int serrno = errno;
422 1.41 mycroft syslog(LOG_ERR, "accept failed: %m");
423 1.57 christos if (serrno == EINTR || serrno == ECONNABORTED)
424 1.57 christos continue;
425 1.1 cgd exit(1);
426 1.1 cgd }
427 1.59 christos if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, &on,
428 1.59 christos sizeof(on)) == -1)
429 1.11 mycroft syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
430 1.11 mycroft nfsdargs.sock = msgsock;
431 1.59 christos nfsdargs.name = (void *)&ss;
432 1.11 mycroft nfsdargs.namelen = len;
433 1.11 mycroft nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
434 1.11 mycroft (void)close(msgsock);
435 1.1 cgd }
436 1.1 cgd }
437 1.1 cgd }
438 1.1 cgd
439 1.58 joerg static void
440 1.58 joerg usage(void)
441 1.1 cgd {
442 1.59 christos (void)fprintf(stderr, "Usage: %s %s\n", getprogname(), USAGE);
443 1.1 cgd exit(1);
444 1.1 cgd }
445 1.1 cgd
446 1.58 joerg static void
447 1.58 joerg nonfs(int signo)
448 1.1 cgd {
449 1.11 mycroft syslog(LOG_ERR, "missing system call: NFS not available.");
450 1.1 cgd }
451