]>
git.saurik.com Git - apple/network_cmds.git/blob - kdumpd.tproj/kdumpd.c
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>
76 #include "kdumpsubs.h"
81 int rexmtval
= TIMEOUT
;
82 int maxtimeout
= 10*TIMEOUT
;
84 #define PKTSIZE SEGSIZE+6
87 struct sockaddr_in from
;
90 void kdump
__P((struct kdumphdr
*, int));
93 * Null-terminated directory prefix list for absolute pathname requests and
94 * search list for relative pathname requests.
96 * MAXDIRS should be at least as large as the number of arguments that
97 * inetd allows (currently 20).
100 static struct dirlist
{
104 static int suppress_naks
;
105 static int logging
= 1;
108 static char *errtomsg
__P((int));
109 static void nak
__P((int));
110 static char * __P(verifyhost(struct sockaddr_in
*));
117 register struct kdumphdr
*tp
;
120 struct sockaddr_in sin
;
121 char *chroot_dir
= NULL
;
122 struct passwd
*nobody
;
123 char *chuser
= "nobody";
125 openlog("kdumpd", LOG_PID
| LOG_NDELAY
, LOG_FTP
);
126 while ((ch
= getopt(argc
, argv
, "cClns:u:")) != -1) {
147 syslog(LOG_WARNING
, "ignoring unknown option -%c", ch
);
152 struct dirlist
*dirp
;
154 /* Get list of directory prefixes. Skip relative pathnames. */
155 for (dirp
= dirs
; optind
< argc
&& dirp
< &dirs
[MAXDIRS
];
157 if (argv
[optind
][0] == '/') {
158 dirp
->name
= argv
[optind
];
159 dirp
->len
= strlen(dirp
->name
);
164 else if (chroot_dir
) {
168 if (ipchroot
&& chroot_dir
== NULL
) {
169 syslog(LOG_ERR
, "-c requires -s");
174 if (ioctl(0, FIONBIO
, &on
) < 0) {
175 syslog(LOG_ERR
, "ioctl(FIONBIO): %m");
178 fromlen
= sizeof (from
);
179 n
= recvfrom(0, buf
, sizeof (buf
), 0,
180 (struct sockaddr
*)&from
, &fromlen
);
182 syslog(LOG_ERR
, "recvfrom: %m");
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.
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.
203 for (i
= 1; i
< 20; i
++) {
208 * flush out to most recently sent request.
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.
218 i
= recvfrom(0, buf
, sizeof (buf
), 0,
219 (struct sockaddr
*)&from
, &j
);
229 syslog(LOG_ERR
, "fork: %m");
231 } else if (pid
!= 0) {
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.
243 char tempchroot
[MAXPATHLEN
];
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
;
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
);
260 if (chroot(chroot_dir
)) {
261 syslog(LOG_ERR
, "chroot: %s: %m", chroot_dir
);
265 setuid(nobody
->pw_uid
);
268 if (0 != chdir(dirs
->name
))
269 syslog(LOG_ERR
, "chdir%s: %m", dirs
->name
);
271 from
.sin_family
= AF_INET
;
275 peer
= socket(AF_INET
, SOCK_DGRAM
, 0);
277 syslog(LOG_ERR
, "socket: %m");
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");
286 if (connect(peer
, (struct sockaddr
*)&from
, sizeof(from
)) < 0) {
287 syslog(LOG_ERR
, "connect: %m");
290 tp
= (struct kdumphdr
*)buf
;
291 tp
->th_opcode
= ntohs(tp
->th_opcode
);
292 if (tp
->th_opcode
== WRQ
)
298 int validate_access
__P((char **, int));
300 void recvfile
__P((struct formats
*));
304 int (*f_validate
) __P((char **, int));
306 void (*f_recv
) __P((struct formats
*));
309 { "netascii", validate_access
, recvfile
, 1 },
310 { "octet", validate_access
, recvfile
, 0 },
315 * Handle initial connection protocol.
323 int first
= 1, ecode
;
324 register struct formats
*pf
;
325 char *filename
, *mode
= NULL
;
327 filename
= cp
= tp
->th_stuff
;
329 while (cp
< buf
+ size
) {
343 for (cp
= mode
; *cp
; cp
++)
346 for (pf
= formats
; pf
->f_mode
; pf
++)
347 if (strcmp(pf
->f_mode
, mode
) == 0)
349 if (pf
->f_mode
== 0) {
353 ecode
= (*pf
->f_validate
)(&filename
, tp
->th_opcode
);
355 syslog(LOG_INFO
, "%s: %s request for %s: %s", verifyhost(&from
),
356 tp
->th_opcode
== WRQ
? "write" : "read",
357 filename
, errtomsg(ecode
));
361 * Avoid storms of naks to a RRQ broadcast for a relative
362 * bootfile pathname from a diskless Sun.
364 if (suppress_naks
&& *filename
!= '/' && ecode
== ENOTFOUND
)
369 if (tp
->th_opcode
== WRQ
)
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
389 validate_access(char **filep
, int mode
)
393 char *filename
= *filep
;
394 static char pathname
[MAXPATHLEN
];
396 if (strstr(filename
, "/") || strstr(filename
, ".."))
399 snprintf(pathname
, sizeof(pathname
), "./%s", filename
);
401 if (0 == stat(pathname
, &stbuf
))
408 fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
);
411 return (errno
+ 100);
413 file
= fdopen(fd
, (mode
== RRQ
)? "r":"w");
417 if (fchmod(fd
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
) < 0)
431 if (timeout
>= maxtimeout
)
433 longjmp(timeoutbuf
, 2);
435 longjmp(timeoutbuf
, 1);
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;
458 signal(SIGALRM
, timer
);
460 ap
= (struct kdumphdr
*)ackbuf
;
463 send_seek_ack
: timeout
= 0;
464 ap
->th_opcode
= htons((u_short
)ACK
);
465 ap
->th_block
= htonl((unsigned int)block
);
467 jmpval
= setjmp(timeoutbuf
);
470 syslog (LOG_ERR
, "Timing out and flushing file to disk");
474 if (send(peer
, ackbuf
, 6 , 0) != 6) {
475 syslog(LOG_ERR
, "write: %m");
478 write_behind(file
, pf
->f_convert
);
481 n
= recv(peer
, dp
, PKTSIZE
, 0);
483 if (n
< 0) { /* really? */
484 syslog(LOG_ERR
, "read: %m");
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
)
491 if (dp
->th_opcode
== KDP_EOF
)
493 syslog (LOG_ERR
, "Received last panic dump packet");
496 if (dp
->th_opcode
== KDP_SEEK
)
498 if (dp
->th_block
== block
)
500 unsigned int tempoff
= 0;
501 bcopy (dp
->th_data
, &tempoff
, sizeof(unsigned int));
502 lseek (fileno (file
), ntohl(tempoff
), SEEK_SET
);
504 syslog (LOG_ERR
, "lseek: %m");
508 (void) synchnet(peer
);
509 if (dp
->th_block
== (block
-1))
511 syslog (LOG_DAEMON
|LOG_ERR
, "Retransmitting seek ack - current block %hu, received block %hu", block
, dp
->th_block
);
512 goto send_ack
; /* rexmit */
516 if (dp
->th_opcode
== DATA
) {
517 if (dp
->th_block
== block
) {
520 /* Re-synchronize with the other side */
521 (void) synchnet(peer
);
522 if (dp
->th_block
== (block
-1))
524 syslog (LOG_DAEMON
|LOG_ERR
, "Retransmitting ack - current block %hu, received block %hu", block
, dp
->th_block
);
525 goto send_ack
; /* rexmit */
528 syslog (LOG_DAEMON
|LOG_ERR
, "Not retransmitting ack - current block %hu, received block %hu", block
, dp
->th_block
);
532 size
= writeit(file
, &dp
, n
- 6, pf
->f_convert
);
533 if (size
!= (n
-6)) { /* ahem */
534 if (size
< 0) nak(errno
+ 100);
538 } while (dp
->th_opcode
!= KDP_EOF
);
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);
545 write_behind(file
, pf
->f_convert
);
546 (void) fclose(file
); /* close data file */
547 syslog (LOG_ERR
, "file closed, sending final ACK\n");
549 signal(SIGALRM
, justquit
); /* just quit on timeout */
551 n
= recv(peer
, buf
, sizeof (buf
), 0); /* normally times out and quits */
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 */
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" },
582 register struct errmsg
*pe
;
585 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
586 if (pe
->e_code
== error
)
588 snprintf(buf
, sizeof(buf
), "error %d", error
);
593 * Send a nak packet (error message).
594 * Error code passed in is one of the
595 * standard KDUMP codes, or a UNIX errno
602 register struct kdumphdr
*tp
;
604 register struct errmsg
*pe
;
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
)
612 if (pe
->e_code
< 0) {
613 pe
->e_msg
= strerror(error
- 100);
614 tp
->th_code
= EUNDEF
; /* set 'undef' errorcode */
616 strcpy(tp
->th_msg
, pe
->e_msg
);
617 length
= strlen(pe
->e_msg
);
618 tp
->th_msg
[length
] = '\0';
620 if (send(peer
, buf
, length
, 0) != length
)
621 syslog(LOG_ERR
, "nak: %m");
626 struct sockaddr_in
*fromp
;
630 hp
= gethostbyaddr((char *)&fromp
->sin_addr
, sizeof(fromp
->sin_addr
),
635 return inet_ntoa(fromp
->sin_addr
);