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>
59 #include <netinet/in.h>
61 #include <arpa/inet.h>
75 #include <libkern/OSByteOrder.h>
77 #include "kdumpsubs.h"
82 int rexmtval
= TIMEOUT
;
83 int maxtimeout
= 10*TIMEOUT
;
85 #define PKTSIZE SEGSIZE+6
88 struct sockaddr_in from
;
91 void kdump
__P((struct kdumphdr
*, int));
94 * Null-terminated directory prefix list for absolute pathname requests and
95 * search list for relative pathname requests.
97 * MAXDIRS should be at least as large as the number of arguments that
98 * inetd allows (currently 20).
101 static struct dirlist
{
105 static int suppress_naks
;
106 static int logging
= 1;
109 static char *errtomsg
__P((int));
110 static void nak
__P((int));
111 static char * __P(verifyhost(struct sockaddr_in
*));
113 #define KDP_FEATURE_MASK_STRING "features"
114 enum {KDP_FEATURE_LARGE_CRASHDUMPS
= 1};
115 uint32_t kdp_crashdump_feature_mask
;
116 uint32_t kdp_feature_large_crashdumps
;
123 register struct kdumphdr
*tp
;
126 struct sockaddr_in sin
;
127 char *chroot_dir
= NULL
;
128 struct passwd
*nobody
;
129 char *chuser
= "nobody";
131 openlog("kdumpd", LOG_PID
| LOG_NDELAY
, LOG_FTP
);
132 while ((ch
= getopt(argc
, argv
, "cClns:u:")) != -1) {
153 syslog(LOG_WARNING
, "ignoring unknown option -%c", ch
);
158 struct dirlist
*dirp
;
160 /* Get list of directory prefixes. Skip relative pathnames. */
161 for (dirp
= dirs
; optind
< argc
&& dirp
< &dirs
[MAXDIRS
];
163 if (argv
[optind
][0] == '/') {
164 dirp
->name
= argv
[optind
];
165 dirp
->len
= strlen(dirp
->name
);
170 else if (chroot_dir
) {
174 if (ipchroot
&& chroot_dir
== NULL
) {
175 syslog(LOG_ERR
, "-c requires -s");
180 if (ioctl(0, FIONBIO
, &on
) < 0) {
181 syslog(LOG_ERR
, "ioctl(FIONBIO): %m");
184 fromlen
= sizeof (from
);
185 n
= recvfrom(0, buf
, sizeof (buf
), 0,
186 (struct sockaddr
*)&from
, &fromlen
);
188 syslog(LOG_ERR
, "recvfrom: %m");
192 * Now that we have read the message out of the UDP
193 * socket, we fork and exit. Thus, inetd will go back
194 * to listening to the kdump port, and the next request
195 * to come in will start up a new instance of kdumpd.
197 * We do this so that inetd can run kdumpd in "wait" mode.
198 * The problem with kdumpd running in "nowait" mode is that
199 * inetd may get one or more successful "selects" on the
200 * kdump port before we do our receive, so more than one
201 * instance of kdumpd may be started up. Worse, if kdumpd
202 * breaks before doing the above "recvfrom", inetd would
203 * spawn endless instances, clogging the system.
210 for (i
= 1; i
< 20; i
++) {
215 * flush out to most recently sent request.
217 * This may drop some requests, but those
218 * will be resent by the clients when
219 * they timeout. The positive effect of
220 * this flush is to (try to) prevent more
221 * than one kdumpd being started up to service
222 * a single request from a single client.
225 i
= recvfrom(0, buf
, sizeof (buf
), 0,
226 (struct sockaddr
*)&from
, &j
);
236 syslog(LOG_ERR
, "fork: %m");
238 } else if (pid
!= 0) {
244 * Since we exit here, we should do that only after the above
245 * recvfrom to keep inetd from constantly forking should there
246 * be a problem. See the above comment about system clogging.
250 char tempchroot
[MAXPATHLEN
];
255 tempaddr
= inet_ntoa(from
.sin_addr
);
256 snprintf(tempchroot
, sizeof(tempchroot
), "%s/%s", chroot_dir
, tempaddr
);
257 statret
= stat(tempchroot
, &sb
);
258 if ((sb
.st_mode
& S_IFDIR
) &&
259 (statret
== 0 || (statret
== -1 && ipchroot
== 1)))
260 chroot_dir
= tempchroot
;
262 /* Must get this before chroot because /etc might go away */
263 if ((nobody
= getpwnam(chuser
)) == NULL
) {
264 syslog(LOG_ERR
, "%s: no such user", chuser
);
267 if (chroot(chroot_dir
)) {
268 syslog(LOG_ERR
, "chroot: %s: %m", chroot_dir
);
272 setuid(nobody
->pw_uid
);
275 if (0 != chdir(dirs
->name
))
276 syslog(LOG_ERR
, "chdir%s: %m", dirs
->name
);
278 from
.sin_family
= AF_INET
;
282 peer
= socket(AF_INET
, SOCK_DGRAM
, 0);
284 syslog(LOG_ERR
, "socket: %m");
287 memset(&sin
, 0, sizeof(sin
));
288 sin
.sin_family
= AF_INET
;
289 if (bind(peer
, (struct sockaddr
*)&sin
, sizeof (sin
)) < 0) {
290 syslog(LOG_ERR
, "bind: %m");
293 if (connect(peer
, (struct sockaddr
*)&from
, sizeof(from
)) < 0) {
294 syslog(LOG_ERR
, "connect: %m");
297 tp
= (struct kdumphdr
*)buf
;
298 tp
->th_opcode
= ntohs(tp
->th_opcode
);
299 if (tp
->th_opcode
== WRQ
)
305 int validate_access
__P((char **, int));
307 void recvfile
__P((struct formats
*));
311 int (*f_validate
) __P((char **, int));
313 void (*f_recv
) __P((struct formats
*));
316 { "netascii", validate_access
, recvfile
, 1 },
317 { "octet", validate_access
, recvfile
, 0 },
322 * Handle initial connection protocol.
330 int first
= 1, ecode
;
331 register struct formats
*pf
;
332 char *filename
, *mode
= NULL
;
334 filename
= cp
= tp
->th_stuff
;
336 while (cp
< buf
+ size
) {
350 for (cp
= mode
; *cp
; cp
++)
355 if (strncmp(KDP_FEATURE_MASK_STRING
, cp
, sizeof(KDP_FEATURE_MASK_STRING
)) == 0) {
356 kdp_crashdump_feature_mask
= ntohl(*(uint32_t *) (cp
+ sizeof(KDP_FEATURE_MASK_STRING
)));
357 kdp_feature_large_crashdumps
= kdp_crashdump_feature_mask
& KDP_FEATURE_LARGE_CRASHDUMPS
;
358 syslog(LOG_INFO
, "Received feature mask %s:%hx", cp
, kdp_crashdump_feature_mask
);
361 for (pf
= formats
; pf
->f_mode
; pf
++)
362 if (strcmp(pf
->f_mode
, mode
) == 0)
364 if (pf
->f_mode
== 0) {
368 ecode
= (*pf
->f_validate
)(&filename
, tp
->th_opcode
);
370 syslog(LOG_INFO
, "%s: %s request for %s: %s", verifyhost(&from
),
371 tp
->th_opcode
== WRQ
? "write" : "read",
372 filename
, errtomsg(ecode
));
376 * Avoid storms of naks to a RRQ broadcast for a relative
377 * bootfile pathname from a diskless Sun.
379 if (suppress_naks
&& *filename
!= '/' && ecode
== ENOTFOUND
)
384 if (tp
->th_opcode
== WRQ
)
394 * Validate file access. We only allow storage of files that do not already
395 * exist, and that do not include directory specifiers in their pathnames.
396 * This is because kernel coredump filenames should always be of the form
397 * "core-version-IP as dotted quad-random string" as in :
398 * core-custom-17.202.40.204-a75b4eec
399 * The file is written to the directory supplied as the first argument
404 validate_access(char **filep
, int mode
)
408 char *filename
= *filep
;
409 static char pathname
[MAXPATHLEN
];
411 if (strstr(filename
, "/") || strstr(filename
, ".."))
414 snprintf(pathname
, sizeof(pathname
), "./%s", filename
);
416 if (0 == stat(pathname
, &stbuf
))
423 fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
);
426 return (errno
+ 100);
428 file
= fdopen(fd
, (mode
== RRQ
)? "r":"w");
432 if (fchmod(fd
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
) < 0)
446 if (timeout
>= maxtimeout
)
448 longjmp(timeoutbuf
, 2);
450 longjmp(timeoutbuf
, 1);
467 struct kdumphdr
*dp
, *w_init();
468 register struct kdumphdr
*ap
; /* ack buffer */
469 register int n
, size
;
470 volatile unsigned int block
;
471 volatile unsigned int jmpval
= 0;
473 signal(SIGALRM
, timer
);
475 ap
= (struct kdumphdr
*)ackbuf
;
478 send_seek_ack
: timeout
= 0;
480 ap
->th_opcode
= htons((u_short
)ACK
| (kdp_feature_large_crashdumps
<< 8));
482 ap
->th_opcode
= htons((u_short
)ACK
);
483 ap
->th_block
= htonl((unsigned int)block
);
485 jmpval
= setjmp(timeoutbuf
);
488 syslog (LOG_ERR
, "Timing out and flushing file to disk");
492 if (send(peer
, ackbuf
, 6 , 0) != 6) {
493 syslog(LOG_ERR
, "write: %m");
496 write_behind(file
, pf
->f_convert
);
499 n
= recv(peer
, dp
, PKTSIZE
, 0);
501 if (n
< 0) { /* really? */
502 syslog(LOG_ERR
, "read: %m");
505 dp
->th_opcode
= ntohs((u_short
)dp
->th_opcode
);
506 dp
->th_block
= ntohl((unsigned int)dp
->th_block
);
507 if (dp
->th_opcode
== ERROR
)
509 if (dp
->th_opcode
== KDP_EOF
)
511 syslog (LOG_ERR
, "Received last panic dump packet");
514 if (dp
->th_opcode
== KDP_SEEK
)
516 if (dp
->th_block
== block
)
518 off_t crashdump_offset
= 0;
519 unsigned int tempoff
= 0;
521 if (kdp_feature_large_crashdumps
) {
522 crashdump_offset
= OSSwapBigToHostInt64((*(uint64_t *)dp
->th_data
));
524 syslog(LOG_INFO
, "Large offset");
528 bcopy (dp
->th_data
, &tempoff
, sizeof(unsigned int));
529 crashdump_offset
= ntohl(tempoff
);
532 syslog(LOG_INFO
, "Seeking to offset 0x%llx\n", crashdump_offset
);
535 lseek(fileno (file
), crashdump_offset
, SEEK_SET
);
537 syslog (LOG_ERR
, "lseek: %m");
541 (void) synchnet(peer
);
542 if (dp
->th_block
== (block
-1))
544 syslog (LOG_DAEMON
|LOG_ERR
, "Retransmitting seek ack - current block %hu, received block %hu", block
, dp
->th_block
);
545 goto send_ack
; /* rexmit */
549 if (dp
->th_opcode
== DATA
) {
550 if (dp
->th_block
== block
) {
553 /* Re-synchronize with the other side */
554 (void) synchnet(peer
);
555 if (dp
->th_block
== (block
-1))
557 syslog (LOG_DAEMON
|LOG_ERR
, "Retransmitting ack - current block %hu, received block %hu", block
, dp
->th_block
);
558 goto send_ack
; /* rexmit */
561 syslog (LOG_DAEMON
|LOG_ERR
, "Not retransmitting ack - current block %hu, received block %hu", block
, dp
->th_block
);
565 size
= writeit(file
, &dp
, n
- 6, pf
->f_convert
);
566 if (size
!= (n
-6)) { /* ahem */
567 if (size
< 0) nak(errno
+ 100);
571 } while (dp
->th_opcode
!= KDP_EOF
);
574 ap
->th_opcode
= htons((u_short
)ACK
); /* send the "final" ack */
575 ap
->th_block
= htonl((unsigned int) (block
));
576 (void) send(peer
, ackbuf
, 6, 0);
578 write_behind(file
, pf
->f_convert
);
579 (void) fclose(file
); /* close data file */
580 syslog (LOG_ERR
, "file closed, sending final ACK\n");
582 signal(SIGALRM
, justquit
); /* just quit on timeout */
584 n
= recv(peer
, buf
, sizeof (buf
), 0); /* normally times out and quits */
586 if (n
>= 6 && /* if read some data */
587 dp
->th_opcode
== DATA
&& /* and got a data block */
588 block
== dp
->th_block
) { /* then my last ack was lost */
589 (void) send(peer
, ackbuf
, 6, 0); /* resend final ack */
599 { EUNDEF
, "Undefined error code" },
600 { ENOTFOUND
, "File not found" },
601 { EACCESS
, "Access violation" },
602 { ENOSPACE
, "Disk full or allocation exceeded" },
603 { EBADOP
, "Illegal KDUMP operation" },
604 { EBADID
, "Unknown transfer ID" },
605 { EEXISTS
, "File already exists" },
606 { ENOUSER
, "No such user" },
615 register struct errmsg
*pe
;
618 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
619 if (pe
->e_code
== error
)
621 snprintf(buf
, sizeof(buf
), "error %d", error
);
626 * Send a nak packet (error message).
627 * Error code passed in is one of the
628 * standard KDUMP codes, or a UNIX errno
635 register struct kdumphdr
*tp
;
637 register struct errmsg
*pe
;
639 tp
= (struct kdumphdr
*)buf
;
640 tp
->th_opcode
= htons((u_short
)ERROR
);
641 tp
->th_code
= htons((unsigned int)error
);
642 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
643 if (pe
->e_code
== error
)
645 if (pe
->e_code
< 0) {
646 pe
->e_msg
= strerror(error
- 100);
647 tp
->th_code
= EUNDEF
; /* set 'undef' errorcode */
649 strcpy(tp
->th_msg
, pe
->e_msg
);
650 length
= strlen(pe
->e_msg
);
651 tp
->th_msg
[length
] = '\0';
653 if (send(peer
, buf
, length
, 0) != length
)
654 syslog(LOG_ERR
, "nak: %m");
659 struct sockaddr_in
*fromp
;
663 hp
= gethostbyaddr((char *)&fromp
->sin_addr
, sizeof(fromp
->sin_addr
),
668 return inet_ntoa(fromp
->sin_addr
);