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
34 #include <sys/cdefs.h>
37 __unused
static const char copyright
[] =
38 "@(#) Copyright (c) 1983, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
42 /* Mac OS X kernel core dump server, based on the BSD trivial file
43 * transfer protocol server (FreeBSD distribution), with several
44 * modifications. This server is *not* compatible with tftp, as the
45 * protocol has changed considerably.
49 * Based on the trivial file transfer protocol server.
51 * The original version included many modifications by Jim Guyton
55 #include <sys/param.h>
56 #include <sys/ioctl.h>
58 #include <sys/socket.h>
59 #include <sys/types.h>
62 #include <netinet/in.h>
64 #include <arpa/inet.h>
79 #include <libkern/OSByteOrder.h>
81 #include "kdumpsubs.h"
86 int rexmtval
= TIMEOUT
;
87 int maxtimeout
= 25 * TIMEOUT
;
89 #define PKTSIZE SEGSIZE+6
91 char buf
[MAXIMUM_KDP_PKTSIZE
];
92 char ackbuf
[MAXIMUM_KDP_PKTSIZE
];
93 struct sockaddr_in from
;
96 void kdump
__P((struct kdumphdr
*, int));
99 * Null-terminated directory prefix list for absolute pathname requests and
100 * search list for relative pathname requests.
102 * MAXDIRS should be at least as large as the number of arguments that
103 * inetd allows (currently 20).
106 static struct dirlist
{
110 static int suppress_naks
;
111 static int logging
= 1;
114 static char *errtomsg
__P((int));
115 static void nak
__P((int));
116 static char * __P(verifyhost(struct sockaddr_in
*));
117 uint32_t kdp_crashdump_pkt_size
= (SEGSIZE
+ (sizeof(struct kdumphdr
)));
118 uint32_t kdp_crashdump_seg_size
= SEGSIZE
;
120 #define KDP_FEATURE_MASK_STRING "features"
121 enum {KDP_FEATURE_LARGE_CRASHDUMPS
= 1, KDP_FEATURE_LARGE_PKT_SIZE
= 2};
123 uint32_t kdp_crashdump_feature_mask
;
124 uint32_t kdp_feature_large_crashdumps
, kdp_feature_large_packets
;
131 register struct kdumphdr
*tp
;
134 struct sockaddr_in sin
;
135 char *chroot_dir
= NULL
;
136 struct passwd
*nobody
;
137 char *chuser
= "nobody";
139 openlog("kdumpd", LOG_PID
| LOG_NDELAY
, LOG_FTP
);
140 while ((ch
= getopt(argc
, argv
, "cClns:u:")) != -1) {
161 syslog(LOG_WARNING
, "ignoring unknown option -%c", ch
);
166 struct dirlist
*dirp
;
168 /* Get list of directory prefixes. Skip relative pathnames. */
169 for (dirp
= dirs
; optind
< argc
&& dirp
< &dirs
[MAXDIRS
];
171 if (argv
[optind
][0] == '/') {
172 dirp
->name
= argv
[optind
];
173 dirp
->len
= strlen(dirp
->name
);
178 else if (chroot_dir
) {
182 if (ipchroot
&& chroot_dir
== NULL
) {
183 syslog(LOG_ERR
, "-c requires -s");
188 if (ioctl(0, FIONBIO
, &on
) < 0) {
189 syslog(LOG_ERR
, "ioctl(FIONBIO): %m");
192 fromlen
= sizeof (from
);
193 n
= recvfrom(0, buf
, sizeof (buf
), 0,
194 (struct sockaddr
*)&from
, &fromlen
);
196 syslog(LOG_ERR
, "recvfrom: %m");
200 * Now that we have read the message out of the UDP
201 * socket, we fork and exit. Thus, inetd will go back
202 * to listening to the kdump port, and the next request
203 * to come in will start up a new instance of kdumpd.
205 * We do this so that inetd can run kdumpd in "wait" mode.
206 * The problem with kdumpd running in "nowait" mode is that
207 * inetd may get one or more successful "selects" on the
208 * kdump port before we do our receive, so more than one
209 * instance of kdumpd may be started up. Worse, if kdumpd
210 * breaks before doing the above "recvfrom", inetd would
211 * spawn endless instances, clogging the system.
218 for (i
= 1; i
< 20; i
++) {
223 * flush out to most recently sent request.
225 * This may drop some requests, but those
226 * will be resent by the clients when
227 * they timeout. The positive effect of
228 * this flush is to (try to) prevent more
229 * than one kdumpd being started up to service
230 * a single request from a single client.
233 i
= recvfrom(0, buf
, sizeof (buf
), 0,
234 (struct sockaddr
*)&from
, &j
);
244 syslog(LOG_ERR
, "fork: %m");
246 } else if (pid
!= 0) {
252 * Since we exit here, we should do that only after the above
253 * recvfrom to keep inetd from constantly forking should there
254 * be a problem. See the above comment about system clogging.
258 char tempchroot
[MAXPATHLEN
];
263 tempaddr
= inet_ntoa(from
.sin_addr
);
264 snprintf(tempchroot
, sizeof(tempchroot
), "%s/%s", chroot_dir
, tempaddr
);
265 statret
= stat(tempchroot
, &sb
);
266 if (((sb
.st_mode
& S_IFMT
) == S_IFDIR
) &&
267 (statret
== 0 || (statret
== -1 && ipchroot
== 1)))
268 chroot_dir
= tempchroot
;
270 /* Must get this before chroot because /etc might go away */
271 if ((nobody
= getpwnam(chuser
)) == NULL
) {
272 syslog(LOG_ERR
, "%s: no such user", chuser
);
275 if (chroot(chroot_dir
)) {
276 syslog(LOG_ERR
, "chroot: %s: %m", chroot_dir
);
280 setuid(nobody
->pw_uid
);
283 if (0 != chdir(dirs
->name
))
284 syslog(LOG_ERR
, "chdir%s: %m", dirs
->name
);
286 from
.sin_family
= AF_INET
;
290 peer
= socket(AF_INET
, SOCK_DGRAM
, 0);
292 syslog(LOG_ERR
, "socket: %m");
295 memset(&sin
, 0, sizeof(sin
));
296 sin
.sin_family
= AF_INET
;
297 if (bind(peer
, (struct sockaddr
*)&sin
, sizeof (sin
)) < 0) {
298 syslog(LOG_ERR
, "bind: %m");
301 if (connect(peer
, (struct sockaddr
*)&from
, sizeof(from
)) < 0) {
302 syslog(LOG_ERR
, "connect: %m");
305 tp
= (struct kdumphdr
*)buf
;
306 tp
->th_opcode
= ntohs(tp
->th_opcode
);
307 if (tp
->th_opcode
== WRQ
)
313 int validate_access
__P((char **, int));
315 void recvfile
__P((struct formats
*));
319 int (*f_validate
) __P((char **, int));
321 void (*f_recv
) __P((struct formats
*));
324 { "netascii", validate_access
, recvfile
, 1 },
325 { "octet", validate_access
, recvfile
, 0 },
330 * Handle initial connection protocol.
338 int first
= 1, ecode
;
339 register struct formats
*pf
;
340 char *filename
, *mode
= NULL
;
342 filename
= cp
= tp
->th_stuff
;
344 while (cp
< buf
+ size
) {
358 for (cp
= mode
; *cp
; cp
++)
363 if (strncmp(KDP_FEATURE_MASK_STRING
, cp
, sizeof(KDP_FEATURE_MASK_STRING
)) == 0) {
364 kdp_crashdump_feature_mask
= ntohl(*(uint32_t *) (cp
+ sizeof(KDP_FEATURE_MASK_STRING
)));
365 kdp_feature_large_crashdumps
= kdp_crashdump_feature_mask
& KDP_FEATURE_LARGE_CRASHDUMPS
;
366 kdp_feature_large_packets
= kdp_crashdump_feature_mask
& KDP_FEATURE_LARGE_PKT_SIZE
;
368 if (kdp_feature_large_packets
) {
369 kdp_crashdump_pkt_size
= KDP_LARGE_CRASHDUMP_PKT_SIZE
;
370 kdp_crashdump_seg_size
= kdp_crashdump_pkt_size
- sizeof(struct kdumphdr
);
372 syslog(KDUMPD_DEBUG_LEVEL
, "Received feature mask %s:0x%x", cp
, kdp_crashdump_feature_mask
);
374 syslog(KDUMPD_DEBUG_LEVEL
, "Unable to locate feature mask, mode: %s", mode
);
376 for (pf
= formats
; pf
->f_mode
; pf
++)
377 if (strcmp(pf
->f_mode
, mode
) == 0)
379 if (pf
->f_mode
== 0) {
383 ecode
= (*pf
->f_validate
)(&filename
, tp
->th_opcode
);
385 syslog(KDUMPD_DEBUG_LEVEL
, "%s: %s request for %s: %s", verifyhost(&from
),
386 tp
->th_opcode
== WRQ
? "write" : "read",
387 filename
, errtomsg(ecode
));
391 * Avoid storms of naks to a RRQ broadcast for a relative
392 * bootfile pathname from a diskless Sun.
394 if (suppress_naks
&& *filename
!= '/' && ecode
== ENOTFOUND
)
399 if (tp
->th_opcode
== WRQ
)
409 * Validate file access. We only allow storage of files that do not already
410 * exist, and that do not include directory specifiers in their pathnames.
411 * This is because kernel coredump filenames should always be of the form
412 * "core-version-IP as dotted quad-random string" as in :
413 * core-custom-17.202.40.204-a75b4eec
414 * The file is written to the directory supplied as the first argument
419 validate_access(char **filep
, int mode
)
423 char *filename
= *filep
;
424 static char pathname
[MAXPATHLEN
];
426 if (strstr(filename
, "/") || strstr(filename
, ".."))
429 snprintf(pathname
, sizeof(pathname
), "./%s", filename
);
431 if (0 == stat(pathname
, &stbuf
))
438 fd
= open(filename
, O_RDWR
|O_CREAT
|O_TRUNC
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
441 return (errno
+ 100);
443 file
= fdopen(fd
, (mode
== RRQ
)? "r":"w");
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
);