]> git.saurik.com Git - apple/network_cmds.git/blob - kdumpd.tproj/kdumpd.c
network_cmds-356.9.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 #include <sys/mman.h>
59
60 #include <netinet/in.h>
61 #include "kdump.h"
62 #include <arpa/inet.h>
63
64 #include <stdint.h>
65 #include <ctype.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <netdb.h>
69 #include <pwd.h>
70 #include <setjmp.h>
71 #include <signal.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <syslog.h>
76 #include <unistd.h>
77 #include <libkern/OSByteOrder.h>
78
79 #include "kdumpsubs.h"
80
81 #define TIMEOUT 2
82
83 int peer;
84 int rexmtval = TIMEOUT;
85 int maxtimeout = 25 * TIMEOUT;
86
87 #define PKTSIZE SEGSIZE+6
88
89 char buf[MAXIMUM_KDP_PKTSIZE];
90 char ackbuf[MAXIMUM_KDP_PKTSIZE];
91 struct sockaddr_in from;
92 socklen_t fromlen;
93
94 void kdump __P((struct kdumphdr *, int));
95
96 /*
97 * Null-terminated directory prefix list for absolute pathname requests and
98 * search list for relative pathname requests.
99 *
100 * MAXDIRS should be at least as large as the number of arguments that
101 * inetd allows (currently 20).
102 */
103 #define MAXDIRS 20
104 static struct dirlist {
105 char *name;
106 int len;
107 } dirs[MAXDIRS+1];
108 static int suppress_naks;
109 static int logging = 1;
110 static int ipchroot;
111
112 static char *errtomsg __P((int));
113 static void nak __P((int));
114 static char * __P(verifyhost(struct sockaddr_in *));
115 uint32_t kdp_crashdump_pkt_size = (SEGSIZE + (sizeof(struct kdumphdr)));
116 uint32_t kdp_crashdump_seg_size = SEGSIZE;
117
118 #define KDP_FEATURE_MASK_STRING "features"
119 enum {KDP_FEATURE_LARGE_CRASHDUMPS = 1, KDP_FEATURE_LARGE_PKT_SIZE = 2};
120
121 uint32_t kdp_crashdump_feature_mask;
122 uint32_t kdp_feature_large_crashdumps, kdp_feature_large_packets;
123
124 int
125 main(argc, argv)
126 int argc;
127 char *argv[];
128 {
129 register struct kdumphdr *tp;
130 register int n;
131 int ch, on;
132 struct sockaddr_in sin;
133 char *chroot_dir = NULL;
134 struct passwd *nobody;
135 char *chuser = "nobody";
136
137 openlog("kdumpd", LOG_PID | LOG_NDELAY, LOG_FTP);
138 while ((ch = getopt(argc, argv, "cClns:u:")) != -1) {
139 switch (ch) {
140 case 'c':
141 ipchroot = 1;
142 break;
143 case 'C':
144 ipchroot = 2;
145 break;
146 case 'l':
147 logging = 1;
148 break;
149 case 'n':
150 suppress_naks = 1;
151 break;
152 case 's':
153 chroot_dir = optarg;
154 break;
155 case 'u':
156 chuser = optarg;
157 break;
158 default:
159 syslog(LOG_WARNING, "ignoring unknown option -%c", ch);
160 }
161 }
162
163 if (optind < argc) {
164 struct dirlist *dirp;
165
166 /* Get list of directory prefixes. Skip relative pathnames. */
167 for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
168 optind++) {
169 if (argv[optind][0] == '/') {
170 dirp->name = argv[optind];
171 dirp->len = strlen(dirp->name);
172 dirp++;
173 }
174 }
175 }
176 else if (chroot_dir) {
177 dirs->name = "/";
178 dirs->len = 1;
179 }
180 if (ipchroot && chroot_dir == NULL) {
181 syslog(LOG_ERR, "-c requires -s");
182 exit(1);
183 }
184
185 on = 1;
186 if (ioctl(0, FIONBIO, &on) < 0) {
187 syslog(LOG_ERR, "ioctl(FIONBIO): %m");
188 exit(1);
189 }
190 fromlen = sizeof (from);
191 n = recvfrom(0, buf, sizeof (buf), 0,
192 (struct sockaddr *)&from, &fromlen);
193 if (n < 0) {
194 syslog(LOG_ERR, "recvfrom: %m");
195 exit(1);
196 }
197 /*
198 * Now that we have read the message out of the UDP
199 * socket, we fork and exit. Thus, inetd will go back
200 * to listening to the kdump port, and the next request
201 * to come in will start up a new instance of kdumpd.
202 *
203 * We do this so that inetd can run kdumpd in "wait" mode.
204 * The problem with kdumpd running in "nowait" mode is that
205 * inetd may get one or more successful "selects" on the
206 * kdump port before we do our receive, so more than one
207 * instance of kdumpd may be started up. Worse, if kdumpd
208 * breaks before doing the above "recvfrom", inetd would
209 * spawn endless instances, clogging the system.
210 */
211 {
212 int pid;
213 int i;
214 socklen_t j;
215
216 for (i = 1; i < 20; i++) {
217 pid = fork();
218 if (pid < 0) {
219 sleep(i);
220 /*
221 * flush out to most recently sent request.
222 *
223 * This may drop some requests, but those
224 * will be resent by the clients when
225 * they timeout. The positive effect of
226 * this flush is to (try to) prevent more
227 * than one kdumpd being started up to service
228 * a single request from a single client.
229 */
230 j = sizeof from;
231 i = recvfrom(0, buf, sizeof (buf), 0,
232 (struct sockaddr *)&from, &j);
233 if (i > 0) {
234 n = i;
235 fromlen = j;
236 }
237 } else {
238 break;
239 }
240 }
241 if (pid < 0) {
242 syslog(LOG_ERR, "fork: %m");
243 exit(1);
244 } else if (pid != 0) {
245 exit(0);
246 }
247 }
248
249 /*
250 * Since we exit here, we should do that only after the above
251 * recvfrom to keep inetd from constantly forking should there
252 * be a problem. See the above comment about system clogging.
253 */
254 if (chroot_dir) {
255 if (ipchroot) {
256 char tempchroot[MAXPATHLEN];
257 char *tempaddr;
258 struct stat sb;
259 int statret;
260
261 tempaddr = inet_ntoa(from.sin_addr);
262 snprintf(tempchroot, sizeof(tempchroot), "%s/%s", chroot_dir, tempaddr);
263 statret = stat(tempchroot, &sb);
264 if ((sb.st_mode & S_IFDIR) &&
265 (statret == 0 || (statret == -1 && ipchroot == 1)))
266 chroot_dir = tempchroot;
267 }
268 /* Must get this before chroot because /etc might go away */
269 if ((nobody = getpwnam(chuser)) == NULL) {
270 syslog(LOG_ERR, "%s: no such user", chuser);
271 exit(1);
272 }
273 if (chroot(chroot_dir)) {
274 syslog(LOG_ERR, "chroot: %s: %m", chroot_dir);
275 exit(1);
276 }
277 chdir( "/" );
278 setuid(nobody->pw_uid);
279 }
280 else
281 if (0 != chdir(dirs->name))
282 syslog(LOG_ERR, "chdir%s: %m", dirs->name);
283
284 from.sin_family = AF_INET;
285 alarm(0);
286 close(0);
287 close(1);
288 peer = socket(AF_INET, SOCK_DGRAM, 0);
289 if (peer < 0) {
290 syslog(LOG_ERR, "socket: %m");
291 exit(1);
292 }
293 memset(&sin, 0, sizeof(sin));
294 sin.sin_family = AF_INET;
295 if (bind(peer, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
296 syslog(LOG_ERR, "bind: %m");
297 exit(1);
298 }
299 if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
300 syslog(LOG_ERR, "connect: %m");
301 exit(1);
302 }
303 tp = (struct kdumphdr *)buf;
304 tp->th_opcode = ntohs(tp->th_opcode);
305 if (tp->th_opcode == WRQ)
306 kdump(tp, n);
307 exit(1);
308 }
309
310 struct formats;
311 int validate_access __P((char **, int));
312
313 void recvfile __P((struct formats *));
314
315 struct formats {
316 char *f_mode;
317 int (*f_validate) __P((char **, int));
318
319 void (*f_recv) __P((struct formats *));
320 int f_convert;
321 } formats[] = {
322 { "netascii", validate_access, recvfile, 1 },
323 { "octet", validate_access, recvfile, 0 },
324 { 0 }
325 };
326
327 /*
328 * Handle initial connection protocol.
329 */
330 void
331 kdump(tp, size)
332 struct kdumphdr *tp;
333 int size;
334 {
335 register char *cp;
336 int first = 1, ecode;
337 register struct formats *pf;
338 char *filename, *mode = NULL;
339
340 filename = cp = tp->th_stuff;
341 again:
342 while (cp < buf + size) {
343 if (*cp == '\0')
344 break;
345 cp++;
346 }
347 if (*cp != '\0') {
348 nak(EBADOP);
349 exit(1);
350 }
351 if (first) {
352 mode = ++cp;
353 first = 0;
354 goto again;
355 }
356 for (cp = mode; *cp; cp++)
357 if (isupper(*cp))
358 *cp = tolower(*cp);
359
360 cp++;
361 if (strncmp(KDP_FEATURE_MASK_STRING, cp, sizeof(KDP_FEATURE_MASK_STRING)) == 0) {
362 kdp_crashdump_feature_mask = ntohl(*(uint32_t *) (cp + sizeof(KDP_FEATURE_MASK_STRING)));
363 kdp_feature_large_crashdumps = kdp_crashdump_feature_mask & KDP_FEATURE_LARGE_CRASHDUMPS;
364 kdp_feature_large_packets = kdp_crashdump_feature_mask & KDP_FEATURE_LARGE_PKT_SIZE;
365
366 if (kdp_feature_large_packets) {
367 kdp_crashdump_pkt_size = KDP_LARGE_CRASHDUMP_PKT_SIZE;
368 kdp_crashdump_seg_size = kdp_crashdump_pkt_size - sizeof(struct kdumphdr);
369 }
370 syslog(KDUMPD_DEBUG_LEVEL, "Received feature mask %s:0x%x", cp, kdp_crashdump_feature_mask);
371 } else
372 syslog(KDUMPD_DEBUG_LEVEL, "Unable to locate feature mask, mode: %s", mode);
373
374 for (pf = formats; pf->f_mode; pf++)
375 if (strcmp(pf->f_mode, mode) == 0)
376 break;
377 if (pf->f_mode == 0) {
378 nak(EBADOP);
379 exit(1);
380 }
381 ecode = (*pf->f_validate)(&filename, tp->th_opcode);
382 if (logging) {
383 syslog(KDUMPD_DEBUG_LEVEL, "%s: %s request for %s: %s", verifyhost(&from),
384 tp->th_opcode == WRQ ? "write" : "read",
385 filename, errtomsg(ecode));
386 }
387 if (ecode) {
388 /*
389 * Avoid storms of naks to a RRQ broadcast for a relative
390 * bootfile pathname from a diskless Sun.
391 */
392 if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
393 exit(0);
394 nak(ecode);
395 exit(1);
396 }
397 if (tp->th_opcode == WRQ)
398 (*pf->f_recv)(pf);
399
400 exit(0);
401 }
402
403
404 FILE *file;
405
406 /*
407 * Validate file access. We only allow storage of files that do not already
408 * exist, and that do not include directory specifiers in their pathnames.
409 * This is because kernel coredump filenames should always be of the form
410 * "core-version-IP as dotted quad-random string" as in :
411 * core-custom-17.202.40.204-a75b4eec
412 * The file is written to the directory supplied as the first argument
413 * in inetd.conf
414 */
415
416 int
417 validate_access(char **filep, int mode)
418 {
419 struct stat stbuf;
420 int fd;
421 char *filename = *filep;
422 static char pathname[MAXPATHLEN];
423
424 if (strstr(filename, "/") || strstr(filename, ".."))
425 return (EACCESS);
426
427 snprintf(pathname, sizeof(pathname), "./%s", filename);
428
429 if (0 == stat(pathname, &stbuf))
430 return (EEXIST);
431
432 if (errno != ENOENT)
433 return (errno);
434
435
436 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC , S_IRUSR | S_IWUSR);
437
438 if (fd < 0)
439 return (errno + 100);
440
441 file = fdopen(fd, (mode == RRQ)? "r":"w");
442 if (file == NULL) {
443 return errno+100;
444 }
445 if (fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) < 0)
446 return errno+100;
447
448 return (0);
449 }
450
451 int timeout;
452 jmp_buf timeoutbuf;
453
454 void
455 timer()
456 {
457
458 timeout += rexmtval;
459 if (timeout >= maxtimeout)
460 {
461 longjmp(timeoutbuf, 2);
462 }
463 longjmp(timeoutbuf, 1);
464 }
465
466 void
467 justquit()
468 {
469 exit(0);
470 }
471
472 /*
473 * Receive a file.
474 */
475 void
476 recvfile(pf)
477 struct formats *pf;
478 {
479 struct kdumphdr *dp, *w_init();
480 register struct kdumphdr *ap; /* ack buffer */
481 register int n, size;
482 volatile unsigned int block;
483 volatile unsigned int jmpval = 0;
484
485 signal(SIGALRM, timer);
486 dp = w_init();
487 ap = (struct kdumphdr *)ackbuf;
488 block = 0;
489 do {
490 send_seek_ack: timeout = 0;
491 if (block == 0)
492 ap->th_opcode = htons((u_short)ACK | ((kdp_feature_large_crashdumps | kdp_feature_large_packets) << 8));
493 else
494 ap->th_opcode = htons((u_short)ACK);
495 ap->th_block = htonl((unsigned int)block);
496 block++;
497 jmpval = setjmp(timeoutbuf);
498 if (2 == jmpval)
499 {
500 syslog (LOG_ERR, "Timing out and flushing file to disk");
501 goto flushfile;
502 }
503 send_ack:
504 if (send(peer, ackbuf, 6 , 0) != 6) {
505 syslog(LOG_ERR, "write: %m");
506 goto abort;
507 }
508 write_behind(file, pf->f_convert);
509 for ( ; ; ) {
510 alarm(rexmtval);
511 n = recv(peer, dp, kdp_crashdump_pkt_size, 0);
512 alarm(0);
513 if (n < 0) { /* really? */
514 syslog(LOG_ERR, "read: %m");
515 goto abort;
516 }
517 dp->th_opcode = ntohs((u_short)dp->th_opcode);
518 dp->th_block = ntohl((unsigned int)dp->th_block);
519 #if DEBUG
520 syslog(KDUMPD_DEBUG_LEVEL, "Received packet type %u, block %u\n", (unsigned)dp->th_opcode, (unsigned)dp->th_block);
521 #endif
522
523 if (dp->th_opcode == ERROR)
524 goto abort;
525
526 if (dp->th_opcode == KDP_EOF)
527 {
528 syslog (LOG_ERR, "Received last panic dump packet");
529 goto final_ack;
530 }
531 if (dp->th_opcode == KDP_SEEK)
532 {
533 if (dp->th_block == block)
534 {
535 off_t crashdump_offset = 0;
536 unsigned int tempoff = 0;
537
538 if (kdp_feature_large_crashdumps) {
539 crashdump_offset = OSSwapBigToHostInt64((*(uint64_t *)dp->th_data));
540 }
541 else {
542 bcopy (dp->th_data, &tempoff, sizeof(unsigned int));
543 crashdump_offset = ntohl(tempoff);
544 }
545
546 #if DEBUG
547 syslog(KDUMPD_DEBUG_LEVEL, "Seeking to offset 0x%llx\n", crashdump_offset);
548 #endif
549 errno = 0;
550 lseek(fileno (file), crashdump_offset, SEEK_SET);
551 if (errno)
552 syslog (LOG_ERR, "lseek: %m");
553
554 goto send_seek_ack;
555 }
556 (void) synchnet(peer);
557 if (dp->th_block == (block-1))
558 {
559 syslog (LOG_DAEMON|LOG_ERR, "Retransmitting seek ack - current block %u, received block %u", block, dp->th_block);
560 goto send_ack; /* rexmit */
561 }
562 }
563
564 if (dp->th_opcode == DATA) {
565 if (dp->th_block == block) {
566 break; /* normal */
567 }
568 /* Re-synchronize with the other side */
569 (void) synchnet(peer);
570 if (dp->th_block == (block-1))
571 {
572 syslog (LOG_DAEMON|LOG_ERR, "Retransmitting ack - current block %u, received block %u", block, dp->th_block);
573 goto send_ack; /* rexmit */
574 }
575 else
576 syslog (LOG_DAEMON|LOG_ERR, "Not retransmitting ack - current block %u, received block %u", block, dp->th_block);
577 }
578 }
579 #if DEBUG
580 syslog(KDUMPD_DEBUG_LEVEL, "Writing block sized %u, current offset 0x%llx\n", n - 6, ftello(file));
581 #endif
582 size = writeit(file, &dp, n - 6, pf->f_convert);
583 if (size != (n-6)) { /* ahem */
584 if (size < 0) nak(errno + 100);
585 else nak(ENOSPACE);
586 goto abort;
587 }
588 } while (dp->th_opcode != KDP_EOF);
589
590 final_ack:
591 ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */
592 ap->th_block = htonl((unsigned int) (block));
593 (void) send(peer, ackbuf, 6, 0);
594 flushfile:
595 write_behind(file, pf->f_convert);
596 (void) fclose(file); /* close data file */
597 syslog (LOG_ERR, "file closed, sending final ACK\n");
598
599 signal(SIGALRM, justquit); /* just quit on timeout */
600 alarm(rexmtval);
601 n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
602 alarm(0);
603 if (n >= 6 && /* if read some data */
604 dp->th_opcode == DATA && /* and got a data block */
605 block == dp->th_block) { /* then my last ack was lost */
606 (void) send(peer, ackbuf, 6, 0); /* resend final ack */
607 }
608 abort:
609 return;
610 }
611
612 /* update if needed, when adding new errmsgs */
613 #define MAXERRMSGLEN 40
614
615 struct errmsg {
616 int e_code;
617 char *e_msg;
618 } errmsgs[] = {
619 { EUNDEF, "Undefined error code" },
620 { ENOTFOUND, "File not found" },
621 { EACCESS, "Access violation" },
622 { ENOSPACE, "Disk full or allocation exceeded" },
623 { EBADOP, "Illegal KDUMP operation" },
624 { EBADID, "Unknown transfer ID" },
625 { EEXISTS, "File already exists" },
626 { ENOUSER, "No such user" },
627 { -1, 0 }
628 };
629
630 static char *
631 errtomsg(error)
632 int error;
633 {
634 static char buf[20];
635 register struct errmsg *pe;
636 if (error == 0)
637 return "success";
638 for (pe = errmsgs; pe->e_code >= 0; pe++)
639 if (pe->e_code == error)
640 return pe->e_msg;
641 snprintf(buf, sizeof(buf), "error %d", error);
642 return buf;
643 }
644
645 /*
646 * Send a nak packet (error message).
647 * Error code passed in is one of the
648 * standard KDUMP codes, or a UNIX errno
649 * offset by 100.
650 */
651 static void
652 nak(error)
653 int error;
654 {
655 register struct kdumphdr *tp;
656 int length;
657 register struct errmsg *pe;
658
659 tp = (struct kdumphdr *)buf;
660 tp->th_opcode = htons((u_short)ERROR);
661 tp->th_code = htons((unsigned int)error);
662 for (pe = errmsgs; pe->e_code >= 0; pe++)
663 if (pe->e_code == error)
664 break;
665 if (pe->e_code < 0) {
666 pe->e_msg = strerror(error - 100);
667 tp->th_code = EUNDEF; /* set 'undef' errorcode */
668 }
669 if (strlen(pe->e_msg) > MAXERRMSGLEN) {
670 syslog(LOG_ERR, "nak: error msg too long");
671 return;
672 }
673
674 strlcpy(tp->th_msg, pe->e_msg, MAXERRMSGLEN);
675 length = strlen(pe->e_msg);
676 tp->th_msg[length] = '\0';
677 length += 5;
678 if (send(peer, buf, length, 0) != length)
679 syslog(LOG_ERR, "nak: %m");
680
681 return;
682 }
683
684 static char *
685 verifyhost(fromp)
686 struct sockaddr_in *fromp;
687 {
688 struct hostent *hp;
689
690 hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof(fromp->sin_addr),
691 fromp->sin_family);
692 if(hp)
693 return hp->h_name;
694 else
695 return inet_ntoa(fromp->sin_addr);
696 }