2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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
35 static const char copyright
[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
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.
47 * Based on the trivial file transfer protocol server.
49 * The original version included many modifications by Jim Guyton
53 #include <sys/param.h>
54 #include <sys/ioctl.h>
56 #include <sys/socket.h>
57 #include <sys/types.h>
60 #include <netinet/in.h>
62 #include <arpa/inet.h>
77 #include <libkern/OSByteOrder.h>
79 #include "kdumpsubs.h"
84 int rexmtval
= TIMEOUT
;
85 int maxtimeout
= 25 * TIMEOUT
;
87 #define PKTSIZE SEGSIZE+6
89 char buf
[MAXIMUM_KDP_PKTSIZE
];
90 char ackbuf
[MAXIMUM_KDP_PKTSIZE
];
91 struct sockaddr_in from
;
94 void kdump
__P((struct kdumphdr
*, int));
97 * Null-terminated directory prefix list for absolute pathname requests and
98 * search list for relative pathname requests.
100 * MAXDIRS should be at least as large as the number of arguments that
101 * inetd allows (currently 20).
104 static struct dirlist
{
108 static int suppress_naks
;
109 static int logging
= 1;
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
;
118 #define KDP_FEATURE_MASK_STRING "features"
119 enum {KDP_FEATURE_LARGE_CRASHDUMPS
= 1, KDP_FEATURE_LARGE_PKT_SIZE
= 2};
121 uint32_t kdp_crashdump_feature_mask
;
122 uint32_t kdp_feature_large_crashdumps
, kdp_feature_large_packets
;
129 register struct kdumphdr
*tp
;
132 struct sockaddr_in sin
;
133 char *chroot_dir
= NULL
;
134 struct passwd
*nobody
;
135 char *chuser
= "nobody";
137 openlog("kdumpd", LOG_PID
| LOG_NDELAY
, LOG_FTP
);
138 while ((ch
= getopt(argc
, argv
, "cClns:u:")) != -1) {
159 syslog(LOG_WARNING
, "ignoring unknown option -%c", ch
);
164 struct dirlist
*dirp
;
166 /* Get list of directory prefixes. Skip relative pathnames. */
167 for (dirp
= dirs
; optind
< argc
&& dirp
< &dirs
[MAXDIRS
];
169 if (argv
[optind
][0] == '/') {
170 dirp
->name
= argv
[optind
];
171 dirp
->len
= strlen(dirp
->name
);
176 else if (chroot_dir
) {
180 if (ipchroot
&& chroot_dir
== NULL
) {
181 syslog(LOG_ERR
, "-c requires -s");
186 if (ioctl(0, FIONBIO
, &on
) < 0) {
187 syslog(LOG_ERR
, "ioctl(FIONBIO): %m");
190 fromlen
= sizeof (from
);
191 n
= recvfrom(0, buf
, sizeof (buf
), 0,
192 (struct sockaddr
*)&from
, &fromlen
);
194 syslog(LOG_ERR
, "recvfrom: %m");
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.
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.
216 for (i
= 1; i
< 20; i
++) {
221 * flush out to most recently sent request.
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.
231 i
= recvfrom(0, buf
, sizeof (buf
), 0,
232 (struct sockaddr
*)&from
, &j
);
242 syslog(LOG_ERR
, "fork: %m");
244 } else if (pid
!= 0) {
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.
256 char tempchroot
[MAXPATHLEN
];
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
;
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
);
273 if (chroot(chroot_dir
)) {
274 syslog(LOG_ERR
, "chroot: %s: %m", chroot_dir
);
278 setuid(nobody
->pw_uid
);
281 if (0 != chdir(dirs
->name
))
282 syslog(LOG_ERR
, "chdir%s: %m", dirs
->name
);
284 from
.sin_family
= AF_INET
;
288 peer
= socket(AF_INET
, SOCK_DGRAM
, 0);
290 syslog(LOG_ERR
, "socket: %m");
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");
299 if (connect(peer
, (struct sockaddr
*)&from
, sizeof(from
)) < 0) {
300 syslog(LOG_ERR
, "connect: %m");
303 tp
= (struct kdumphdr
*)buf
;
304 tp
->th_opcode
= ntohs(tp
->th_opcode
);
305 if (tp
->th_opcode
== WRQ
)
311 int validate_access
__P((char **, int));
313 void recvfile
__P((struct formats
*));
317 int (*f_validate
) __P((char **, int));
319 void (*f_recv
) __P((struct formats
*));
322 { "netascii", validate_access
, recvfile
, 1 },
323 { "octet", validate_access
, recvfile
, 0 },
328 * Handle initial connection protocol.
336 int first
= 1, ecode
;
337 register struct formats
*pf
;
338 char *filename
, *mode
= NULL
;
340 filename
= cp
= tp
->th_stuff
;
342 while (cp
< buf
+ size
) {
356 for (cp
= mode
; *cp
; 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
;
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
);
370 syslog(KDUMPD_DEBUG_LEVEL
, "Received feature mask %s:0x%x", cp
, kdp_crashdump_feature_mask
);
372 syslog(KDUMPD_DEBUG_LEVEL
, "Unable to locate feature mask, mode: %s", mode
);
374 for (pf
= formats
; pf
->f_mode
; pf
++)
375 if (strcmp(pf
->f_mode
, mode
) == 0)
377 if (pf
->f_mode
== 0) {
381 ecode
= (*pf
->f_validate
)(&filename
, tp
->th_opcode
);
383 syslog(KDUMPD_DEBUG_LEVEL
, "%s: %s request for %s: %s", verifyhost(&from
),
384 tp
->th_opcode
== WRQ
? "write" : "read",
385 filename
, errtomsg(ecode
));
389 * Avoid storms of naks to a RRQ broadcast for a relative
390 * bootfile pathname from a diskless Sun.
392 if (suppress_naks
&& *filename
!= '/' && ecode
== ENOTFOUND
)
397 if (tp
->th_opcode
== WRQ
)
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
417 validate_access(char **filep
, int mode
)
421 char *filename
= *filep
;
422 static char pathname
[MAXPATHLEN
];
424 if (strstr(filename
, "/") || strstr(filename
, ".."))
427 snprintf(pathname
, sizeof(pathname
), "./%s", filename
);
429 if (0 == stat(pathname
, &stbuf
))
436 fd
= open(filename
, O_RDWR
|O_CREAT
|O_TRUNC
, S_IRUSR
| S_IWUSR
);
439 return (errno
+ 100);
441 file
= fdopen(fd
, (mode
== RRQ
)? "r":"w");
445 if (fchmod(fd
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
) < 0)
459 if (timeout
>= maxtimeout
)
461 longjmp(timeoutbuf
, 2);
463 longjmp(timeoutbuf
, 1);
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;
485 signal(SIGALRM
, timer
);
487 ap
= (struct kdumphdr
*)ackbuf
;
490 send_seek_ack
: timeout
= 0;
492 ap
->th_opcode
= htons((u_short
)ACK
| ((kdp_feature_large_crashdumps
| kdp_feature_large_packets
) << 8));
494 ap
->th_opcode
= htons((u_short
)ACK
);
495 ap
->th_block
= htonl((unsigned int)block
);
497 jmpval
= setjmp(timeoutbuf
);
500 syslog (LOG_ERR
, "Timing out and flushing file to disk");
504 if (send(peer
, ackbuf
, 6 , 0) != 6) {
505 syslog(LOG_ERR
, "write: %m");
508 write_behind(file
, pf
->f_convert
);
511 n
= recv(peer
, dp
, kdp_crashdump_pkt_size
, 0);
513 if (n
< 0) { /* really? */
514 syslog(LOG_ERR
, "read: %m");
517 dp
->th_opcode
= ntohs((u_short
)dp
->th_opcode
);
518 dp
->th_block
= ntohl((unsigned int)dp
->th_block
);
520 syslog(KDUMPD_DEBUG_LEVEL
, "Received packet type %u, block %u\n", (unsigned)dp
->th_opcode
, (unsigned)dp
->th_block
);
523 if (dp
->th_opcode
== ERROR
)
526 if (dp
->th_opcode
== KDP_EOF
)
528 syslog (LOG_ERR
, "Received last panic dump packet");
531 if (dp
->th_opcode
== KDP_SEEK
)
533 if (dp
->th_block
== block
)
535 off_t crashdump_offset
= 0;
536 unsigned int tempoff
= 0;
538 if (kdp_feature_large_crashdumps
) {
539 crashdump_offset
= OSSwapBigToHostInt64((*(uint64_t *)dp
->th_data
));
542 bcopy (dp
->th_data
, &tempoff
, sizeof(unsigned int));
543 crashdump_offset
= ntohl(tempoff
);
547 syslog(KDUMPD_DEBUG_LEVEL
, "Seeking to offset 0x%llx\n", crashdump_offset
);
550 lseek(fileno (file
), crashdump_offset
, SEEK_SET
);
552 syslog (LOG_ERR
, "lseek: %m");
556 (void) synchnet(peer
);
557 if (dp
->th_block
== (block
-1))
559 syslog (LOG_DAEMON
|LOG_ERR
, "Retransmitting seek ack - current block %u, received block %u", block
, dp
->th_block
);
560 goto send_ack
; /* rexmit */
564 if (dp
->th_opcode
== DATA
) {
565 if (dp
->th_block
== block
) {
568 /* Re-synchronize with the other side */
569 (void) synchnet(peer
);
570 if (dp
->th_block
== (block
-1))
572 syslog (LOG_DAEMON
|LOG_ERR
, "Retransmitting ack - current block %u, received block %u", block
, dp
->th_block
);
573 goto send_ack
; /* rexmit */
576 syslog (LOG_DAEMON
|LOG_ERR
, "Not retransmitting ack - current block %u, received block %u", block
, dp
->th_block
);
580 syslog(KDUMPD_DEBUG_LEVEL
, "Writing block sized %u, current offset 0x%llx\n", n
- 6, ftello(file
));
582 size
= writeit(file
, &dp
, n
- 6, pf
->f_convert
);
583 if (size
!= (n
-6)) { /* ahem */
584 if (size
< 0) nak(errno
+ 100);
588 } while (dp
->th_opcode
!= KDP_EOF
);
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);
595 write_behind(file
, pf
->f_convert
);
596 (void) fclose(file
); /* close data file */
597 syslog (LOG_ERR
, "file closed, sending final ACK\n");
599 signal(SIGALRM
, justquit
); /* just quit on timeout */
601 n
= recv(peer
, buf
, sizeof (buf
), 0); /* normally times out and quits */
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 */
612 /* update if needed, when adding new errmsgs */
613 #define MAXERRMSGLEN 40
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" },
635 register struct errmsg
*pe
;
638 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
639 if (pe
->e_code
== error
)
641 snprintf(buf
, sizeof(buf
), "error %d", error
);
646 * Send a nak packet (error message).
647 * Error code passed in is one of the
648 * standard KDUMP codes, or a UNIX errno
655 register struct kdumphdr
*tp
;
657 register struct errmsg
*pe
;
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
)
665 if (pe
->e_code
< 0) {
666 pe
->e_msg
= strerror(error
- 100);
667 tp
->th_code
= EUNDEF
; /* set 'undef' errorcode */
669 if (strlen(pe
->e_msg
) > MAXERRMSGLEN
) {
670 syslog(LOG_ERR
, "nak: error msg too long");
674 strlcpy(tp
->th_msg
, pe
->e_msg
, MAXERRMSGLEN
);
675 length
= strlen(pe
->e_msg
);
676 tp
->th_msg
[length
] = '\0';
678 if (send(peer
, buf
, length
, 0) != length
)
679 syslog(LOG_ERR
, "nak: %m");
686 struct sockaddr_in
*fromp
;
690 hp
= gethostbyaddr((char *)&fromp
->sin_addr
, sizeof(fromp
->sin_addr
),
695 return inet_ntoa(fromp
->sin_addr
);