]> git.saurik.com Git - apple/network_cmds.git/blob - kdumpd.tproj/kdumpd.c
network_cmds-245.19.tar.gz
[apple/network_cmds.git] / kdumpd.tproj / kdumpd.c
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 /* Mac OS X kernel core dump server, based on the BSD trivial file
41 * transfer protocol server (FreeBSD distribution), with several
42 * modifications. This server is *not* compatible with tftp, as the
43 * protocol has changed considerably.
44 */
45
46 /*
47 * Based on the trivial file transfer protocol server.
48 *
49 * The original version included many modifications by Jim Guyton
50 * <guyton@rand-unix>.
51 */
52
53 #include <sys/param.h>
54 #include <sys/ioctl.h>
55 #include <sys/stat.h>
56 #include <sys/socket.h>
57 #include <sys/types.h>
58
59 #include <netinet/in.h>
60 #include "kdump.h"
61 #include <arpa/inet.h>
62
63 #include <ctype.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <netdb.h>
67 #include <pwd.h>
68 #include <setjmp.h>
69 #include <signal.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <syslog.h>
74 #include <unistd.h>
75
76 #include "kdumpsubs.h"
77
78 #define TIMEOUT 5
79
80 int peer;
81 int rexmtval = TIMEOUT;
82 int maxtimeout = 10*TIMEOUT;
83
84 #define PKTSIZE SEGSIZE+6
85 char buf[PKTSIZE];
86 char ackbuf[PKTSIZE];
87 struct sockaddr_in from;
88 int fromlen;
89
90 void kdump __P((struct kdumphdr *, int));
91
92 /*
93 * Null-terminated directory prefix list for absolute pathname requests and
94 * search list for relative pathname requests.
95 *
96 * MAXDIRS should be at least as large as the number of arguments that
97 * inetd allows (currently 20).
98 */
99 #define MAXDIRS 20
100 static struct dirlist {
101 char *name;
102 int len;
103 } dirs[MAXDIRS+1];
104 static int suppress_naks;
105 static int logging = 1;
106 static int ipchroot;
107
108 static char *errtomsg __P((int));
109 static void nak __P((int));
110 static char * __P(verifyhost(struct sockaddr_in *));
111
112 int
113 main(argc, argv)
114 int argc;
115 char *argv[];
116 {
117 register struct kdumphdr *tp;
118 register int n;
119 int ch, on;
120 struct sockaddr_in sin;
121 char *chroot_dir = NULL;
122 struct passwd *nobody;
123 char *chuser = "nobody";
124
125 openlog("kdumpd", LOG_PID | LOG_NDELAY, LOG_FTP);
126 while ((ch = getopt(argc, argv, "cClns:u:")) != -1) {
127 switch (ch) {
128 case 'c':
129 ipchroot = 1;
130 break;
131 case 'C':
132 ipchroot = 2;
133 break;
134 case 'l':
135 logging = 1;
136 break;
137 case 'n':
138 suppress_naks = 1;
139 break;
140 case 's':
141 chroot_dir = optarg;
142 break;
143 case 'u':
144 chuser = optarg;
145 break;
146 default:
147 syslog(LOG_WARNING, "ignoring unknown option -%c", ch);
148 }
149 }
150
151 if (optind < argc) {
152 struct dirlist *dirp;
153
154 /* Get list of directory prefixes. Skip relative pathnames. */
155 for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
156 optind++) {
157 if (argv[optind][0] == '/') {
158 dirp->name = argv[optind];
159 dirp->len = strlen(dirp->name);
160 dirp++;
161 }
162 }
163 }
164 else if (chroot_dir) {
165 dirs->name = "/";
166 dirs->len = 1;
167 }
168 if (ipchroot && chroot_dir == NULL) {
169 syslog(LOG_ERR, "-c requires -s");
170 exit(1);
171 }
172
173 on = 1;
174 if (ioctl(0, FIONBIO, &on) < 0) {
175 syslog(LOG_ERR, "ioctl(FIONBIO): %m");
176 exit(1);
177 }
178 fromlen = sizeof (from);
179 n = recvfrom(0, buf, sizeof (buf), 0,
180 (struct sockaddr *)&from, &fromlen);
181 if (n < 0) {
182 syslog(LOG_ERR, "recvfrom: %m");
183 exit(1);
184 }
185 /*
186 * Now that we have read the message out of the UDP
187 * socket, we fork and exit. Thus, inetd will go back
188 * to listening to the kdump port, and the next request
189 * to come in will start up a new instance of kdumpd.
190 *
191 * We do this so that inetd can run kdumpd in "wait" mode.
192 * The problem with kdumpd running in "nowait" mode is that
193 * inetd may get one or more successful "selects" on the
194 * kdump port before we do our receive, so more than one
195 * instance of kdumpd may be started up. Worse, if kdumpd
196 * breaks before doing the above "recvfrom", inetd would
197 * spawn endless instances, clogging the system.
198 */
199 {
200 int pid;
201 int i, j;
202
203 for (i = 1; i < 20; i++) {
204 pid = fork();
205 if (pid < 0) {
206 sleep(i);
207 /*
208 * flush out to most recently sent request.
209 *
210 * This may drop some requests, but those
211 * will be resent by the clients when
212 * they timeout. The positive effect of
213 * this flush is to (try to) prevent more
214 * than one kdumpd being started up to service
215 * a single request from a single client.
216 */
217 j = sizeof from;
218 i = recvfrom(0, buf, sizeof (buf), 0,
219 (struct sockaddr *)&from, &j);
220 if (i > 0) {
221 n = i;
222 fromlen = j;
223 }
224 } else {
225 break;
226 }
227 }
228 if (pid < 0) {
229 syslog(LOG_ERR, "fork: %m");
230 exit(1);
231 } else if (pid != 0) {
232 exit(0);
233 }
234 }
235
236 /*
237 * Since we exit here, we should do that only after the above
238 * recvfrom to keep inetd from constantly forking should there
239 * be a problem. See the above comment about system clogging.
240 */
241 if (chroot_dir) {
242 if (ipchroot) {
243 char tempchroot[MAXPATHLEN];
244 char *tempaddr;
245 struct stat sb;
246 int statret;
247
248 tempaddr = inet_ntoa(from.sin_addr);
249 snprintf(tempchroot, sizeof(tempchroot), "%s/%s", chroot_dir, tempaddr);
250 statret = stat(tempchroot, &sb);
251 if ((sb.st_mode & S_IFDIR) &&
252 (statret == 0 || (statret == -1 && ipchroot == 1)))
253 chroot_dir = tempchroot;
254 }
255 /* Must get this before chroot because /etc might go away */
256 if ((nobody = getpwnam(chuser)) == NULL) {
257 syslog(LOG_ERR, "%s: no such user", chuser);
258 exit(1);
259 }
260 if (chroot(chroot_dir)) {
261 syslog(LOG_ERR, "chroot: %s: %m", chroot_dir);
262 exit(1);
263 }
264 chdir( "/" );
265 setuid(nobody->pw_uid);
266 }
267 else
268 if (0 != chdir(dirs->name))
269 syslog(LOG_ERR, "chdir%s: %m", dirs->name);
270
271 from.sin_family = AF_INET;
272 alarm(0);
273 close(0);
274 close(1);
275 peer = socket(AF_INET, SOCK_DGRAM, 0);
276 if (peer < 0) {
277 syslog(LOG_ERR, "socket: %m");
278 exit(1);
279 }
280 memset(&sin, 0, sizeof(sin));
281 sin.sin_family = AF_INET;
282 if (bind(peer, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
283 syslog(LOG_ERR, "bind: %m");
284 exit(1);
285 }
286 if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
287 syslog(LOG_ERR, "connect: %m");
288 exit(1);
289 }
290 tp = (struct kdumphdr *)buf;
291 tp->th_opcode = ntohs(tp->th_opcode);
292 if (tp->th_opcode == WRQ)
293 kdump(tp, n);
294 exit(1);
295 }
296
297 struct formats;
298 int validate_access __P((char **, int));
299
300 void recvfile __P((struct formats *));
301
302 struct formats {
303 char *f_mode;
304 int (*f_validate) __P((char **, int));
305
306 void (*f_recv) __P((struct formats *));
307 int f_convert;
308 } formats[] = {
309 { "netascii", validate_access, recvfile, 1 },
310 { "octet", validate_access, recvfile, 0 },
311 { 0 }
312 };
313
314 /*
315 * Handle initial connection protocol.
316 */
317 void
318 kdump(tp, size)
319 struct kdumphdr *tp;
320 int size;
321 {
322 register char *cp;
323 int first = 1, ecode;
324 register struct formats *pf;
325 char *filename, *mode = NULL;
326
327 filename = cp = tp->th_stuff;
328 again:
329 while (cp < buf + size) {
330 if (*cp == '\0')
331 break;
332 cp++;
333 }
334 if (*cp != '\0') {
335 nak(EBADOP);
336 exit(1);
337 }
338 if (first) {
339 mode = ++cp;
340 first = 0;
341 goto again;
342 }
343 for (cp = mode; *cp; cp++)
344 if (isupper(*cp))
345 *cp = tolower(*cp);
346 for (pf = formats; pf->f_mode; pf++)
347 if (strcmp(pf->f_mode, mode) == 0)
348 break;
349 if (pf->f_mode == 0) {
350 nak(EBADOP);
351 exit(1);
352 }
353 ecode = (*pf->f_validate)(&filename, tp->th_opcode);
354 if (logging) {
355 syslog(LOG_INFO, "%s: %s request for %s: %s", verifyhost(&from),
356 tp->th_opcode == WRQ ? "write" : "read",
357 filename, errtomsg(ecode));
358 }
359 if (ecode) {
360 /*
361 * Avoid storms of naks to a RRQ broadcast for a relative
362 * bootfile pathname from a diskless Sun.
363 */
364 if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
365 exit(0);
366 nak(ecode);
367 exit(1);
368 }
369 if (tp->th_opcode == WRQ)
370 (*pf->f_recv)(pf);
371
372 exit(0);
373 }
374
375
376 FILE *file;
377
378 /*
379 * Validate file access. We only allow storage of files that do not already
380 * exist, and that do not include directory specifiers in their pathnames.
381 * This is because kernel coredump filenames should always be of the form
382 * "core-version-IP as dotted quad-random string" as in :
383 * core-custom-17.202.40.204-a75b4eec
384 * The file is written to the directory supplied as the first argument
385 * in inetd.conf
386 */
387
388 int
389 validate_access(char **filep, int mode)
390 {
391 struct stat stbuf;
392 int fd;
393 char *filename = *filep;
394 static char pathname[MAXPATHLEN];
395
396 if (strstr(filename, "/") || strstr(filename, ".."))
397 return (EACCESS);
398
399 snprintf(pathname, sizeof(pathname), "./%s", filename);
400
401 if (0 == stat(pathname, &stbuf))
402 return (EEXIST);
403
404 if (errno != ENOENT)
405 return (errno);
406
407
408 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC);
409
410 if (fd < 0)
411 return (errno + 100);
412
413 file = fdopen(fd, (mode == RRQ)? "r":"w");
414 if (file == NULL) {
415 return errno+100;
416 }
417 if (fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) < 0)
418 return errno+100;
419
420 return (0);
421 }
422
423 int timeout;
424 jmp_buf timeoutbuf;
425
426 void
427 timer()
428 {
429
430 timeout += rexmtval;
431 if (timeout >= maxtimeout)
432 {
433 longjmp(timeoutbuf, 2);
434 }
435 longjmp(timeoutbuf, 1);
436 }
437
438 void
439 justquit()
440 {
441 exit(0);
442 }
443
444
445 /*
446 * Receive a file.
447 */
448 void
449 recvfile(pf)
450 struct formats *pf;
451 {
452 struct kdumphdr *dp, *w_init();
453 register struct kdumphdr *ap; /* ack buffer */
454 register int n, size;
455 volatile unsigned int block;
456 volatile unsigned int jmpval = 0;
457
458 signal(SIGALRM, timer);
459 dp = w_init();
460 ap = (struct kdumphdr *)ackbuf;
461 block = 0;
462 do {
463 send_seek_ack: timeout = 0;
464 ap->th_opcode = htons((u_short)ACK);
465 ap->th_block = htonl((unsigned int)block);
466 block++;
467 jmpval = setjmp(timeoutbuf);
468 if (2 == jmpval)
469 {
470 syslog (LOG_ERR, "Timing out and flushing file to disk");
471 goto flushfile;
472 }
473 send_ack:
474 if (send(peer, ackbuf, 6 , 0) != 6) {
475 syslog(LOG_ERR, "write: %m");
476 goto abort;
477 }
478 write_behind(file, pf->f_convert);
479 for ( ; ; ) {
480 alarm(rexmtval);
481 n = recv(peer, dp, PKTSIZE, 0);
482 alarm(0);
483 if (n < 0) { /* really? */
484 syslog(LOG_ERR, "read: %m");
485 goto abort;
486 }
487 dp->th_opcode = ntohs((u_short)dp->th_opcode);
488 dp->th_block = ntohl((unsigned int)dp->th_block);
489 if (dp->th_opcode == ERROR)
490 goto abort;
491 if (dp->th_opcode == KDP_EOF)
492 {
493 syslog (LOG_ERR, "Received last panic dump packet");
494 goto final_ack;
495 }
496 if (dp->th_opcode == KDP_SEEK)
497 {
498 if (dp->th_block == block)
499 {
500 unsigned int tempoff = 0;
501 bcopy (dp->th_data, &tempoff, sizeof(unsigned int));
502 lseek (fileno (file), ntohl(tempoff), SEEK_SET);
503 if (errno)
504 syslog (LOG_ERR, "lseek: %m");
505
506 goto send_seek_ack;
507 }
508 (void) synchnet(peer);
509 if (dp->th_block == (block-1))
510 {
511 syslog (LOG_DAEMON|LOG_ERR, "Retransmitting seek ack - current block %hu, received block %hu", block, dp->th_block);
512 goto send_ack; /* rexmit */
513 }
514 }
515
516 if (dp->th_opcode == DATA) {
517 if (dp->th_block == block) {
518 break; /* normal */
519 }
520 /* Re-synchronize with the other side */
521 (void) synchnet(peer);
522 if (dp->th_block == (block-1))
523 {
524 syslog (LOG_DAEMON|LOG_ERR, "Retransmitting ack - current block %hu, received block %hu", block, dp->th_block);
525 goto send_ack; /* rexmit */
526 }
527 else
528 syslog (LOG_DAEMON|LOG_ERR, "Not retransmitting ack - current block %hu, received block %hu", block, dp->th_block);
529 }
530 }
531
532 size = writeit(file, &dp, n - 6, pf->f_convert);
533 if (size != (n-6)) { /* ahem */
534 if (size < 0) nak(errno + 100);
535 else nak(ENOSPACE);
536 goto abort;
537 }
538 } while (dp->th_opcode != KDP_EOF);
539
540 final_ack:
541 ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */
542 ap->th_block = htonl((unsigned int) (block));
543 (void) send(peer, ackbuf, 6, 0);
544 flushfile:
545 write_behind(file, pf->f_convert);
546 (void) fclose(file); /* close data file */
547 syslog (LOG_ERR, "file closed, sending final ACK\n");
548
549 signal(SIGALRM, justquit); /* just quit on timeout */
550 alarm(rexmtval);
551 n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
552 alarm(0);
553 if (n >= 6 && /* if read some data */
554 dp->th_opcode == DATA && /* and got a data block */
555 block == dp->th_block) { /* then my last ack was lost */
556 (void) send(peer, ackbuf, 6, 0); /* resend final ack */
557 }
558 abort:
559 return;
560 }
561
562 struct errmsg {
563 int e_code;
564 char *e_msg;
565 } errmsgs[] = {
566 { EUNDEF, "Undefined error code" },
567 { ENOTFOUND, "File not found" },
568 { EACCESS, "Access violation" },
569 { ENOSPACE, "Disk full or allocation exceeded" },
570 { EBADOP, "Illegal KDUMP operation" },
571 { EBADID, "Unknown transfer ID" },
572 { EEXISTS, "File already exists" },
573 { ENOUSER, "No such user" },
574 { -1, 0 }
575 };
576
577 static char *
578 errtomsg(error)
579 int error;
580 {
581 static char buf[20];
582 register struct errmsg *pe;
583 if (error == 0)
584 return "success";
585 for (pe = errmsgs; pe->e_code >= 0; pe++)
586 if (pe->e_code == error)
587 return pe->e_msg;
588 snprintf(buf, sizeof(buf), "error %d", error);
589 return buf;
590 }
591
592 /*
593 * Send a nak packet (error message).
594 * Error code passed in is one of the
595 * standard KDUMP codes, or a UNIX errno
596 * offset by 100.
597 */
598 static void
599 nak(error)
600 int error;
601 {
602 register struct kdumphdr *tp;
603 int length;
604 register struct errmsg *pe;
605
606 tp = (struct kdumphdr *)buf;
607 tp->th_opcode = htons((u_short)ERROR);
608 tp->th_code = htons((unsigned int)error);
609 for (pe = errmsgs; pe->e_code >= 0; pe++)
610 if (pe->e_code == error)
611 break;
612 if (pe->e_code < 0) {
613 pe->e_msg = strerror(error - 100);
614 tp->th_code = EUNDEF; /* set 'undef' errorcode */
615 }
616 strcpy(tp->th_msg, pe->e_msg);
617 length = strlen(pe->e_msg);
618 tp->th_msg[length] = '\0';
619 length += 5;
620 if (send(peer, buf, length, 0) != length)
621 syslog(LOG_ERR, "nak: %m");
622 }
623
624 static char *
625 verifyhost(fromp)
626 struct sockaddr_in *fromp;
627 {
628 struct hostent *hp;
629
630 hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof(fromp->sin_addr),
631 fromp->sin_family);
632 if(hp)
633 return hp->h_name;
634 else
635 return inet_ntoa(fromp->sin_addr);
636 }