nfsd.c revision 1.9 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Rick Macklem at The University of Guelph.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1 cgd char copyright[] =
39 1.1 cgd "@(#) Copyright (c) 1989 Regents of the University of California.\n\
40 1.1 cgd All rights reserved.\n";
41 1.1 cgd #endif not lint
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.7 mycroft /*static char sccsid[] = "from: @(#)nfsd.c 5.10 (Berkeley) 4/24/91";*/
45 1.9 deraadt static char rcsid[] = "$Id: nfsd.c,v 1.9 1993/12/05 13:41:07 deraadt Exp $";
46 1.1 cgd #endif not lint
47 1.1 cgd
48 1.1 cgd #include <sys/types.h>
49 1.1 cgd #include <sys/signal.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.1 cgd #include <sys/mount.h>
54 1.1 cgd #include <sys/socket.h>
55 1.1 cgd #include <sys/socketvar.h>
56 1.1 cgd #include <stdio.h>
57 1.1 cgd #include <syslog.h>
58 1.1 cgd #include <fcntl.h>
59 1.1 cgd #include <string.h>
60 1.1 cgd #include <netdb.h>
61 1.1 cgd #include <rpc/rpc.h>
62 1.1 cgd #include <rpc/pmap_clnt.h>
63 1.1 cgd #include <rpc/pmap_prot.h>
64 1.1 cgd #include <nfs/rpcv2.h>
65 1.1 cgd #include <nfs/nfsv2.h>
66 1.1 cgd
67 1.6 cgd #include <machine/vmparam.h> /* these are for PS_STRINGS */
68 1.6 cgd #include <sys/exec.h>
69 1.6 cgd
70 1.1 cgd /* Global defs */
71 1.1 cgd #ifdef DEBUG
72 1.1 cgd #define syslog(e, s) fprintf(stderr,(s))
73 1.1 cgd int debug = 1;
74 1.1 cgd #else
75 1.1 cgd int debug = 0;
76 1.1 cgd #endif
77 1.1 cgd struct hadr {
78 1.1 cgd u_long ha_sad;
79 1.1 cgd struct hadr *ha_next;
80 1.1 cgd };
81 1.1 cgd struct hadr hphead;
82 1.1 cgd char **Argv = NULL; /* pointer to argument vector */
83 1.1 cgd char *LastArg = NULL; /* end of argv */
84 1.4 glass void reapchild(),not_nfsserver();;
85 1.1 cgd
86 1.1 cgd /*
87 1.1 cgd * Nfs server daemon mostly just a user context for nfssvc()
88 1.1 cgd * 1 - do file descriptor and signal cleanup
89 1.1 cgd * 2 - create server socket
90 1.1 cgd * 3 - register socket with portmap
91 1.1 cgd * For SOCK_DGRAM, just fork children and send them into the kernel
92 1.1 cgd * by calling nfssvc()
93 1.1 cgd * For connection based sockets, loop doing accepts. When you get a new socket
94 1.1 cgd * from accept, fork a child that drops into the kernel via. nfssvc.
95 1.1 cgd * This child will return from nfssvc when the connection is closed, so
96 1.1 cgd * just shutdown() and exit().
97 1.1 cgd * The arguments are:
98 1.1 cgd * -t - support tcp nfs clients
99 1.1 cgd * -u - support udp nfs clients
100 1.1 cgd */
101 1.1 cgd main(argc, argv, envp)
102 1.1 cgd int argc;
103 1.1 cgd char *argv[], *envp[];
104 1.1 cgd {
105 1.1 cgd register int i;
106 1.1 cgd register char *cp, *cp2;
107 1.1 cgd register struct hadr *hp;
108 1.1 cgd int udpcnt, sock, msgsock, tcpflag = 0, udpflag = 0, ret, len;
109 1.1 cgd int reregister = 0;
110 1.1 cgd char opt;
111 1.1 cgd extern int optind;
112 1.1 cgd extern char *optarg;
113 1.1 cgd struct sockaddr_in saddr, msk, mtch, peername;
114 1.1 cgd
115 1.9 deraadt bzero((char *)&saddr, sizeof saddr);
116 1.9 deraadt bzero((char *)&msk, sizeof msk);
117 1.9 deraadt bzero((char *)&mtch, sizeof mtch);
118 1.1 cgd
119 1.1 cgd /*
120 1.1 cgd * Save start and extent of argv for setproctitle.
121 1.1 cgd */
122 1.1 cgd
123 1.1 cgd Argv = argv;
124 1.1 cgd if (envp == 0 || *envp == 0)
125 1.1 cgd envp = argv;
126 1.1 cgd while (*envp)
127 1.1 cgd envp++;
128 1.1 cgd LastArg = envp[-1] + strlen(envp[-1]);
129 1.1 cgd while ((opt = getopt(argc, argv, "rt:u:")) != EOF)
130 1.1 cgd switch (opt) {
131 1.1 cgd case 'r':
132 1.1 cgd reregister++;
133 1.1 cgd break;
134 1.1 cgd case 't':
135 1.1 cgd tcpflag++;
136 1.1 cgd if (cp = index(optarg, ',')) {
137 1.1 cgd *cp++ = '\0';
138 1.1 cgd msk.sin_addr.s_addr = inet_addr(optarg);
139 1.1 cgd if (msk.sin_addr.s_addr == -1)
140 1.1 cgd usage();
141 1.1 cgd if (cp2 = index(cp, ','))
142 1.1 cgd *cp2++ = '\0';
143 1.1 cgd mtch.sin_addr.s_addr = inet_addr(cp);
144 1.1 cgd if (mtch.sin_addr.s_addr == -1)
145 1.1 cgd usage();
146 1.1 cgd cp = cp2;
147 1.1 cgd hphead.ha_next = (struct hadr *)0;
148 1.1 cgd while (cp) {
149 1.1 cgd if (cp2 = index(cp, ','))
150 1.1 cgd *cp2++ = '\0';
151 1.1 cgd hp = (struct hadr *)
152 1.1 cgd malloc(sizeof (struct hadr));
153 1.1 cgd hp->ha_sad = inet_addr(cp);
154 1.1 cgd if (hp->ha_sad == -1)
155 1.1 cgd usage();
156 1.1 cgd hp->ha_next = hphead.ha_next;
157 1.1 cgd hphead.ha_next = hp;
158 1.1 cgd cp = cp2;
159 1.1 cgd }
160 1.1 cgd } else
161 1.1 cgd usage();
162 1.1 cgd break;
163 1.1 cgd case 'u':
164 1.1 cgd udpflag++;
165 1.1 cgd if (cp = index(optarg, ',')) {
166 1.1 cgd *cp++ = '\0';
167 1.1 cgd msk.sin_addr.s_addr = inet_addr(optarg);
168 1.1 cgd if (msk.sin_addr.s_addr == -1)
169 1.1 cgd usage();
170 1.1 cgd if (cp2 = index(cp, ','))
171 1.1 cgd *cp2++ = '\0';
172 1.1 cgd mtch.sin_addr.s_addr = inet_addr(cp);
173 1.1 cgd if (mtch.sin_addr.s_addr == -1)
174 1.1 cgd usage();
175 1.1 cgd if (cp2)
176 1.1 cgd udpcnt = atoi(cp2);
177 1.1 cgd if (udpcnt < 1 || udpcnt > 20)
178 1.1 cgd udpcnt = 1;
179 1.1 cgd } else
180 1.1 cgd usage();
181 1.1 cgd break;
182 1.1 cgd default:
183 1.1 cgd case '?':
184 1.1 cgd usage();
185 1.1 cgd }
186 1.1 cgd
187 1.1 cgd /*
188 1.1 cgd * Default, if neither UDP nor TCP is specified,
189 1.1 cgd * is to support UDP only; a numeric argument indicates
190 1.1 cgd * the number of server daemons to run.
191 1.1 cgd */
192 1.1 cgd if (udpflag == 0 && tcpflag == 0) {
193 1.1 cgd if (argc > 1)
194 1.1 cgd udpcnt = atoi(*++argv);
195 1.1 cgd if (udpcnt < 1 || udpcnt > 20)
196 1.1 cgd udpcnt = 1;
197 1.1 cgd msk.sin_addr.s_addr = mtch.sin_addr.s_addr = 0;
198 1.1 cgd udpflag++;
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd if (debug == 0) {
202 1.1 cgd daemon(0, 0);
203 1.1 cgd signal(SIGINT, SIG_IGN);
204 1.1 cgd signal(SIGQUIT, SIG_IGN);
205 1.1 cgd signal(SIGTERM, SIG_IGN);
206 1.1 cgd signal(SIGHUP, SIG_IGN);
207 1.1 cgd }
208 1.1 cgd signal(SIGCHLD, reapchild);
209 1.4 glass signal(SIGSYS, not_nfsserver);
210 1.8 cgd if (nfssvc(-1, NULL, 0, NULL, 0) >= 0) {
211 1.8 cgd syslog(LOG_ERR, "bad arguments didn't cause error");
212 1.8 cgd exit(1);
213 1.8 cgd }
214 1.1 cgd if (reregister) {
215 1.1 cgd if (udpflag && !pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP,
216 1.1 cgd NFS_PORT)) {
217 1.1 cgd fprintf(stderr,
218 1.1 cgd "Can't register with portmap for UDP\n");
219 1.1 cgd exit(1);
220 1.1 cgd }
221 1.1 cgd if (tcpflag && !pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP,
222 1.1 cgd NFS_PORT)) {
223 1.1 cgd fprintf(stderr,
224 1.1 cgd "Can't register with portmap for TCP\n");
225 1.1 cgd exit(1);
226 1.1 cgd }
227 1.1 cgd exit(0);
228 1.1 cgd }
229 1.1 cgd openlog("nfsd:", LOG_PID, LOG_DAEMON);
230 1.1 cgd #ifdef notdef
231 1.1 cgd /* why? unregisters both protocols even if we restart only one */
232 1.1 cgd pmap_unset(RPCPROG_NFS, NFS_VER2);
233 1.1 cgd #endif
234 1.1 cgd if (udpflag) {
235 1.1 cgd if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
236 1.1 cgd syslog(LOG_ERR, "Can't create socket");
237 1.1 cgd exit(1);
238 1.1 cgd }
239 1.1 cgd saddr.sin_family = AF_INET;
240 1.1 cgd saddr.sin_addr.s_addr = INADDR_ANY;
241 1.1 cgd saddr.sin_port = htons(NFS_PORT);
242 1.1 cgd if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
243 1.1 cgd syslog(LOG_ERR, "Can't bind addr");
244 1.1 cgd exit(1);
245 1.1 cgd }
246 1.1 cgd if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
247 1.1 cgd syslog(LOG_ERR, "Can't register with portmap");
248 1.1 cgd exit(1);
249 1.1 cgd }
250 1.1 cgd
251 1.1 cgd /*
252 1.1 cgd * Send the nfs datagram servers
253 1.1 cgd * right down into the kernel
254 1.1 cgd */
255 1.1 cgd for (i = 0; i < udpcnt; i++)
256 1.1 cgd if (fork() == 0) {
257 1.1 cgd setproctitle("nfsd-udp",
258 1.1 cgd (struct sockaddr_in *)NULL);
259 1.1 cgd ret = nfssvc(sock, &msk, sizeof(msk),
260 1.1 cgd &mtch, sizeof(mtch));
261 1.1 cgd if (ret < 0)
262 1.1 cgd syslog(LOG_ERR, "nfssvc() failed %m");
263 1.1 cgd exit(1);
264 1.1 cgd }
265 1.1 cgd close(sock);
266 1.1 cgd }
267 1.1 cgd
268 1.1 cgd /*
269 1.1 cgd * Now set up the master STREAM server waiting for tcp connections.
270 1.1 cgd */
271 1.1 cgd if (tcpflag) {
272 1.1 cgd int on = 1;
273 1.1 cgd
274 1.1 cgd if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
275 1.1 cgd syslog(LOG_ERR, "Can't create socket");
276 1.1 cgd exit(1);
277 1.1 cgd }
278 1.1 cgd if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
279 1.1 cgd (char *) &on, sizeof(on)) < 0)
280 1.1 cgd syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
281 1.1 cgd saddr.sin_family = AF_INET;
282 1.1 cgd saddr.sin_addr.s_addr = INADDR_ANY;
283 1.1 cgd saddr.sin_port = htons(NFS_PORT);
284 1.1 cgd if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
285 1.1 cgd syslog(LOG_ERR, "Can't bind addr");
286 1.1 cgd exit(1);
287 1.1 cgd }
288 1.1 cgd if (listen(sock, 5) < 0) {
289 1.1 cgd syslog(LOG_ERR, "Listen failed");
290 1.1 cgd exit(1);
291 1.1 cgd }
292 1.1 cgd if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
293 1.1 cgd syslog(LOG_ERR, "Can't register with portmap");
294 1.1 cgd exit(1);
295 1.1 cgd }
296 1.1 cgd setproctitle("nfsd-listen", (struct sockaddr_in *)NULL);
297 1.1 cgd /*
298 1.1 cgd * Loop forever accepting connections and sending the children
299 1.1 cgd * into the kernel to service the mounts.
300 1.1 cgd */
301 1.1 cgd for (;;) {
302 1.1 cgd len = sizeof(peername);
303 1.1 cgd if ((msgsock = accept(sock,
304 1.1 cgd (struct sockaddr *)&peername, &len)) < 0) {
305 1.1 cgd syslog(LOG_ERR, "Accept failed: %m");
306 1.1 cgd exit(1);
307 1.1 cgd }
308 1.1 cgd if ((peername.sin_addr.s_addr & msk.sin_addr.s_addr) !=
309 1.1 cgd mtch.sin_addr.s_addr) {
310 1.1 cgd hp = hphead.ha_next;
311 1.1 cgd while (hp) {
312 1.1 cgd if (peername.sin_addr.s_addr ==
313 1.1 cgd hp->ha_sad)
314 1.1 cgd break;
315 1.1 cgd hp = hp->ha_next;
316 1.1 cgd }
317 1.1 cgd if (hp == NULL) {
318 1.1 cgd shutdown(msgsock, 2);
319 1.1 cgd close(msgsock);
320 1.1 cgd continue;
321 1.1 cgd }
322 1.1 cgd }
323 1.1 cgd if (fork() == 0) {
324 1.1 cgd close(sock);
325 1.1 cgd setproctitle("nfsd-tcp", &peername);
326 1.1 cgd if (setsockopt(msgsock, SOL_SOCKET,
327 1.1 cgd SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0)
328 1.1 cgd syslog(LOG_ERR,
329 1.1 cgd "setsockopt SO_KEEPALIVE: %m");
330 1.1 cgd ret = nfssvc(msgsock, &msk, sizeof(msk),
331 1.1 cgd &mtch, sizeof(mtch));
332 1.1 cgd shutdown(msgsock, 2);
333 1.1 cgd if (ret < 0)
334 1.1 cgd syslog(LOG_NOTICE,
335 1.1 cgd "Nfssvc STREAM Failed");
336 1.1 cgd exit(1);
337 1.1 cgd }
338 1.1 cgd close(msgsock);
339 1.1 cgd }
340 1.1 cgd }
341 1.1 cgd }
342 1.1 cgd
343 1.1 cgd usage()
344 1.1 cgd {
345 1.1 cgd fprintf(stderr, "nfsd [-t msk,mtch[,addrs]] [-u msk,mtch,numprocs]\n");
346 1.1 cgd exit(1);
347 1.1 cgd }
348 1.1 cgd
349 1.1 cgd void
350 1.1 cgd reapchild()
351 1.1 cgd {
352 1.1 cgd
353 1.1 cgd while (wait3((int *) NULL, WNOHANG, (struct rusage *) NULL))
354 1.1 cgd ;
355 1.1 cgd }
356 1.1 cgd
357 1.1 cgd setproctitle(a, sin)
358 1.1 cgd char *a;
359 1.1 cgd struct sockaddr_in *sin;
360 1.1 cgd {
361 1.1 cgd register char *cp;
362 1.6 cgd static char buf[80];
363 1.1 cgd
364 1.1 cgd if (sin)
365 1.1 cgd (void) sprintf(buf, "%s [%s]", a, inet_ntoa(sin->sin_addr));
366 1.1 cgd else
367 1.1 cgd (void) sprintf(buf, "%s", a);
368 1.6 cgd PS_STRINGS->ps_nargvstr = 1;
369 1.6 cgd PS_STRINGS->ps_argvstr = buf;
370 1.1 cgd }
371 1.4 glass
372 1.4 glass void not_nfsserver()
373 1.4 glass {
374 1.8 cgd syslog(LOG_ERR, "not configured as NFS server");
375 1.8 cgd exit(1);
376 1.4 glass }
377