1 /* $NetBSD: tftpd.c,v 1.18 1999/07/12 20:17:09 itojun Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #if 0 41 static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93"; 42 #else 43 __RCSID("$NetBSD: tftpd.c,v 1.18 1999/07/12 20:17:09 itojun Exp $"); 44 #endif 45 #endif /* not lint */ 46 47 /* 48 * Trivial file transfer protocol server. 49 * 50 * This version includes many modifications by Jim Guyton 51 * <guyton@rand-unix>. 52 */ 53 54 #include <sys/param.h> 55 #include <sys/ioctl.h> 56 #include <sys/stat.h> 57 #include <sys/socket.h> 58 59 #include <signal.h> 60 #include <fcntl.h> 61 62 #include <netinet/in.h> 63 #include <arpa/tftp.h> 64 #include <arpa/inet.h> 65 66 #include <ctype.h> 67 #include <errno.h> 68 #include <fcntl.h> 69 #include <grp.h> 70 #include <netdb.h> 71 #include <pwd.h> 72 #include <setjmp.h> 73 #include <signal.h> 74 #include <stdio.h> 75 #include <stdlib.h> 76 #include <string.h> 77 #include <syslog.h> 78 #include <unistd.h> 79 80 #include "tftpsubs.h" 81 82 #define DEFAULTUSER "nobody" 83 84 #define TIMEOUT 5 85 86 extern char *__progname; 87 int peer; 88 int rexmtval = TIMEOUT; 89 int maxtimeout = 5*TIMEOUT; 90 91 #define PKTSIZE SEGSIZE+4 92 char buf[PKTSIZE]; 93 char ackbuf[PKTSIZE]; 94 struct sockaddr_storage from; 95 int fromlen; 96 97 /* 98 * Null-terminated directory prefix list for absolute pathname requests and 99 * search list for relative pathname requests. 100 * 101 * MAXDIRS should be at least as large as the number of arguments that 102 * inetd allows (currently 20). 103 */ 104 #define MAXDIRS 20 105 static struct dirlist { 106 char *name; 107 int len; 108 } dirs[MAXDIRS+1]; 109 static int suppress_naks; 110 static int logging; 111 static int secure; 112 static char *securedir; 113 114 struct formats; 115 116 static void tftp __P((struct tftphdr *, int)); 117 static const char *errtomsg __P((int)); 118 static void nak __P((int)); 119 static char *verifyhost __P((struct sockaddr *)); 120 static void usage __P((void)); 121 void timer __P((int)); 122 void sendfile __P((struct formats *)); 123 void recvfile __P((struct formats *)); 124 void justquit __P((int)); 125 int validate_access __P((char **, int)); 126 int main __P((int, char **)); 127 128 struct formats { 129 char *f_mode; 130 int (*f_validate) __P((char **, int)); 131 void (*f_send) __P((struct formats *)); 132 void (*f_recv) __P((struct formats *)); 133 int f_convert; 134 } formats[] = { 135 { "netascii", validate_access, sendfile, recvfile, 1 }, 136 { "octet", validate_access, sendfile, recvfile, 0 }, 137 { 0 } 138 }; 139 140 static void 141 usage() 142 { 143 syslog(LOG_ERR, 144 "Usage: %s [-ln] [-u user] [-g group] [-s directory] [directory ...]", 145 __progname); 146 exit(1); 147 } 148 149 int 150 main(argc, argv) 151 int argc; 152 char **argv; 153 { 154 struct passwd *pwent; 155 struct group *grent; 156 struct tftphdr *tp; 157 int n = 0; 158 int ch, on; 159 int fd = 0; 160 struct sockaddr_storage me; 161 int len; 162 char *tgtuser, *tgtgroup, *ep; 163 uid_t curuid, tgtuid; 164 gid_t curgid, tgtgid; 165 long nid; 166 167 openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON); 168 tgtuser = DEFAULTUSER; 169 tgtgroup = NULL; 170 curuid = getuid(); 171 curgid = getgid(); 172 173 while ((ch = getopt(argc, argv, "g:lns:u:")) != -1) 174 switch (ch) { 175 176 case 'g': 177 tgtgroup = optarg; 178 break; 179 180 case 'l': 181 logging = 1; 182 break; 183 184 case 'n': 185 suppress_naks = 1; 186 break; 187 188 case 's': 189 secure = 1; 190 securedir = optarg; 191 break; 192 193 case 'u': 194 tgtuser = optarg; 195 break; 196 197 default: 198 usage(); 199 break; 200 } 201 202 if (optind < argc) { 203 struct dirlist *dirp; 204 205 /* Get list of directory prefixes. Skip relative pathnames. */ 206 for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS]; 207 optind++) { 208 if (argv[optind][0] == '/') { 209 dirp->name = argv[optind]; 210 dirp->len = strlen(dirp->name); 211 dirp++; 212 } 213 } 214 } 215 216 if (*tgtuser == '\0' || (tgtgroup != NULL && *tgtgroup == '\0')) 217 usage(); 218 219 nid = (strtol(tgtuser, &ep, 10)); 220 if (*ep == '\0') { 221 if (nid > UID_MAX) { 222 syslog(LOG_ERR, "uid %ld is too large", nid); 223 exit(1); 224 } 225 pwent = getpwuid((uid_t)nid); 226 } else 227 pwent = getpwnam(tgtuser); 228 if (pwent == NULL) { 229 syslog(LOG_ERR, "unknown user `%s'", tgtuser); 230 exit(1); 231 } 232 tgtuid = pwent->pw_uid; 233 tgtgid = pwent->pw_gid; 234 235 if (tgtgroup != NULL) { 236 nid = (strtol(tgtgroup, &ep, 10)); 237 if (*ep == '\0') { 238 if (nid > GID_MAX) { 239 syslog(LOG_ERR, "gid %ld is too large", nid); 240 exit(1); 241 } 242 grent = getgrgid((gid_t)nid); 243 } else 244 grent = getgrnam(tgtgroup); 245 if (grent != NULL) 246 tgtgid = grent->gr_gid; 247 else { 248 syslog(LOG_ERR, "unknown group `%s'", tgtgroup); 249 exit(1); 250 } 251 } 252 253 if (secure) { 254 if (chdir(securedir) < 0) { 255 syslog(LOG_ERR, "chdir %s: %m", securedir); 256 exit(1); 257 } 258 if (chroot(".")) { 259 syslog(LOG_ERR, "chroot: %m"); 260 exit(1); 261 } 262 } 263 264 syslog(LOG_DEBUG, "running as user `%s' (%d), group `%s' (%d)", 265 tgtuser, tgtuid, tgtgroup ? tgtgroup : "(unspecified)" , tgtgid); 266 if (curgid != tgtgid) { 267 if (setgid(tgtgid)) { 268 syslog(LOG_ERR, "setgid to %d: %m", (int)tgtgid); 269 exit(1); 270 } 271 if (setgroups(0, NULL)) { 272 syslog(LOG_ERR, "setgroups: %m"); 273 exit(1); 274 } 275 } 276 277 if (curuid != tgtuid) { 278 if (setuid(tgtuid)) { 279 syslog(LOG_ERR, "setuid to %d: %m", (int)tgtuid); 280 exit(1); 281 } 282 } 283 284 on = 1; 285 if (ioctl(fd, FIONBIO, &on) < 0) { 286 syslog(LOG_ERR, "ioctl(FIONBIO): %m"); 287 exit(1); 288 } 289 fromlen = sizeof (from); 290 n = recvfrom(fd, buf, sizeof (buf), 0, 291 (struct sockaddr *)&from, &fromlen); 292 if (n < 0) { 293 syslog(LOG_ERR, "recvfrom: %m"); 294 exit(1); 295 } 296 /* 297 * Now that we have read the message out of the UDP 298 * socket, we fork and exit. Thus, inetd will go back 299 * to listening to the tftp port, and the next request 300 * to come in will start up a new instance of tftpd. 301 * 302 * We do this so that inetd can run tftpd in "wait" mode. 303 * The problem with tftpd running in "nowait" mode is that 304 * inetd may get one or more successful "selects" on the 305 * tftp port before we do our receive, so more than one 306 * instance of tftpd may be started up. Worse, if tftpd 307 * break before doing the above "recvfrom", inetd would 308 * spawn endless instances, clogging the system. 309 */ 310 { 311 int pid; 312 int i, j; 313 314 for (i = 1; i < 20; i++) { 315 pid = fork(); 316 if (pid < 0) { 317 sleep(i); 318 /* 319 * flush out to most recently sent request. 320 * 321 * This may drop some request, but those 322 * will be resent by the clients when 323 * they timeout. The positive effect of 324 * this flush is to (try to) prevent more 325 * than one tftpd being started up to service 326 * a single request from a single client. 327 */ 328 j = sizeof from; 329 i = recvfrom(fd, buf, sizeof (buf), 0, 330 (struct sockaddr *)&from, &j); 331 if (i > 0) { 332 n = i; 333 fromlen = j; 334 } 335 } else { 336 break; 337 } 338 } 339 if (pid < 0) { 340 syslog(LOG_ERR, "fork: %m"); 341 exit(1); 342 } else if (pid != 0) { 343 exit(0); 344 } 345 } 346 347 /* 348 * remember what address this was sent to, so we can respond on the 349 * same interface 350 */ 351 len = sizeof(me); 352 if (getsockname(fd, (struct sockaddr *)&me, &len) == 0) { 353 switch (me.ss_family) { 354 case AF_INET: 355 ((struct sockaddr_in *)&me)->sin_port = 0; 356 break; 357 case AF_INET6: 358 ((struct sockaddr_in6 *)&me)->sin6_port = 0; 359 break; 360 default: 361 /* unsupported */ 362 break; 363 } 364 } else { 365 memset(&me, 0, sizeof(me)); 366 me.ss_family = from.ss_family; 367 me.ss_len = from.ss_len; 368 } 369 370 alarm(0); 371 close(fd); 372 close(1); 373 peer = socket(from.ss_family, SOCK_DGRAM, 0); 374 if (peer < 0) { 375 syslog(LOG_ERR, "socket: %m"); 376 exit(1); 377 } 378 if (bind(peer, (struct sockaddr *)&me, me.ss_len) < 0) { 379 syslog(LOG_ERR, "bind: %m"); 380 exit(1); 381 } 382 if (connect(peer, (struct sockaddr *)&from, from.ss_len) < 0) { 383 syslog(LOG_ERR, "connect: %m"); 384 exit(1); 385 } 386 tp = (struct tftphdr *)buf; 387 tp->th_opcode = ntohs(tp->th_opcode); 388 if (tp->th_opcode == RRQ || tp->th_opcode == WRQ) 389 tftp(tp, n); 390 exit(1); 391 } 392 393 /* 394 * Handle initial connection protocol. 395 */ 396 static void 397 tftp(tp, size) 398 struct tftphdr *tp; 399 int size; 400 { 401 char *cp; 402 int first = 1, ecode; 403 struct formats *pf; 404 char *filename, *mode = NULL; /* XXX gcc */ 405 406 filename = cp = tp->th_stuff; 407 again: 408 while (cp < buf + size) { 409 if (*cp == '\0') 410 break; 411 cp++; 412 } 413 if (*cp != '\0') { 414 nak(EBADOP); 415 exit(1); 416 } 417 if (first) { 418 mode = ++cp; 419 first = 0; 420 goto again; 421 } 422 for (cp = mode; *cp; cp++) 423 if (isupper(*cp)) 424 *cp = tolower(*cp); 425 for (pf = formats; pf->f_mode; pf++) 426 if (strcmp(pf->f_mode, mode) == 0) 427 break; 428 if (pf->f_mode == 0) { 429 nak(EBADOP); 430 exit(1); 431 } 432 ecode = (*pf->f_validate)(&filename, tp->th_opcode); 433 if (logging) { 434 syslog(LOG_INFO, "%s: %s request for %s: %s", 435 verifyhost((struct sockaddr *)&from), 436 tp->th_opcode == WRQ ? "write" : "read", 437 filename, errtomsg(ecode)); 438 } 439 if (ecode) { 440 /* 441 * Avoid storms of naks to a RRQ broadcast for a relative 442 * bootfile pathname from a diskless Sun. 443 */ 444 if (suppress_naks && *filename != '/' && ecode == ENOTFOUND) 445 exit(0); 446 nak(ecode); 447 exit(1); 448 } 449 if (tp->th_opcode == WRQ) 450 (*pf->f_recv)(pf); 451 else 452 (*pf->f_send)(pf); 453 exit(0); 454 } 455 456 457 FILE *file; 458 459 /* 460 * Validate file access. Since we 461 * have no uid or gid, for now require 462 * file to exist and be publicly 463 * readable/writable. 464 * If we were invoked with arguments 465 * from inetd then the file must also be 466 * in one of the given directory prefixes. 467 */ 468 int 469 validate_access(filep, mode) 470 char **filep; 471 int mode; 472 { 473 struct stat stbuf; 474 int fd; 475 struct dirlist *dirp; 476 static char pathname[MAXPATHLEN]; 477 char *filename = *filep; 478 479 /* 480 * Prevent tricksters from getting around the directory restrictions 481 */ 482 if (strstr(filename, "/../")) 483 return (EACCESS); 484 485 if (*filename == '/') { 486 /* 487 * Allow the request if it's in one of the approved locations. 488 * Special case: check the null prefix ("/") by looking 489 * for length = 1 and relying on the arg. processing that 490 * it's a /. 491 */ 492 for (dirp = dirs; dirp->name != NULL; dirp++) { 493 if (dirp->len == 1 || 494 (!strncmp(filename, dirp->name, dirp->len) && 495 filename[dirp->len] == '/')) 496 break; 497 } 498 /* If directory list is empty, allow access to any file */ 499 if (dirp->name == NULL && dirp != dirs) 500 return (EACCESS); 501 if (stat(filename, &stbuf) < 0) 502 return (errno == ENOENT ? ENOTFOUND : EACCESS); 503 if (!S_ISREG(stbuf.st_mode)) 504 return (ENOTFOUND); 505 if (mode == RRQ) { 506 if ((stbuf.st_mode & S_IROTH) == 0) 507 return (EACCESS); 508 } else { 509 if ((stbuf.st_mode & S_IWOTH) == 0) 510 return (EACCESS); 511 } 512 } else { 513 /* 514 * Relative file name: search the approved locations for it. 515 */ 516 517 if (!strncmp(filename, "../", 3)) 518 return (EACCESS); 519 520 /* 521 * Find the first file that exists in any of the directories, 522 * check access on it. 523 */ 524 if (dirs[0].name != NULL) { 525 for (dirp = dirs; dirp->name != NULL; dirp++) { 526 snprintf(pathname, sizeof pathname, "%s/%s", 527 dirp->name, filename); 528 if (stat(pathname, &stbuf) == 0 && 529 (stbuf.st_mode & S_IFMT) == S_IFREG) { 530 break; 531 } 532 } 533 if (dirp->name == NULL) 534 return (ENOTFOUND); 535 if (mode == RRQ && !(stbuf.st_mode & S_IROTH)) 536 return (EACCESS); 537 if (mode == WRQ && !(stbuf.st_mode & S_IWOTH)) 538 return (EACCESS); 539 *filep = filename = pathname; 540 } else { 541 /* 542 * If there's no directory list, take our cue from the 543 * absolute file request check above (*filename == '/'), 544 * and allow access to anything. 545 */ 546 if (stat(filename, &stbuf) < 0) 547 return (errno == ENOENT ? ENOTFOUND : EACCESS); 548 if (!S_ISREG(stbuf.st_mode)) 549 return (ENOTFOUND); 550 if (mode == RRQ) { 551 if ((stbuf.st_mode & S_IROTH) == 0) 552 return (EACCESS); 553 } else { 554 if ((stbuf.st_mode & S_IWOTH) == 0) 555 return (EACCESS); 556 } 557 *filep = filename; 558 } 559 } 560 fd = open(filename, mode == RRQ ? O_RDONLY : O_WRONLY | O_TRUNC); 561 if (fd < 0) 562 return (errno + 100); 563 file = fdopen(fd, (mode == RRQ)? "r":"w"); 564 if (file == NULL) { 565 close(fd); 566 return errno+100; 567 } 568 return (0); 569 } 570 571 int timeout; 572 jmp_buf timeoutbuf; 573 574 void 575 timer(dummy) 576 int dummy; 577 { 578 579 timeout += rexmtval; 580 if (timeout >= maxtimeout) 581 exit(1); 582 longjmp(timeoutbuf, 1); 583 } 584 585 /* 586 * Send the requested file. 587 */ 588 void 589 sendfile(pf) 590 struct formats *pf; 591 { 592 struct tftphdr *dp; 593 struct tftphdr *ap; /* ack packet */ 594 int size, n; 595 volatile int block; 596 597 signal(SIGALRM, timer); 598 dp = r_init(); 599 ap = (struct tftphdr *)ackbuf; 600 block = 1; 601 do { 602 size = readit(file, &dp, pf->f_convert); 603 if (size < 0) { 604 nak(errno + 100); 605 goto abort; 606 } 607 dp->th_opcode = htons((u_short)DATA); 608 dp->th_block = htons((u_short)block); 609 timeout = 0; 610 (void)setjmp(timeoutbuf); 611 612 send_data: 613 if (send(peer, dp, size + 4, 0) != size + 4) { 614 syslog(LOG_ERR, "tftpd: write: %m"); 615 goto abort; 616 } 617 read_ahead(file, pf->f_convert); 618 for ( ; ; ) { 619 alarm(rexmtval); /* read the ack */ 620 n = recv(peer, ackbuf, sizeof (ackbuf), 0); 621 alarm(0); 622 if (n < 0) { 623 syslog(LOG_ERR, "tftpd: read: %m"); 624 goto abort; 625 } 626 ap->th_opcode = ntohs((u_short)ap->th_opcode); 627 ap->th_block = ntohs((u_short)ap->th_block); 628 629 if (ap->th_opcode == ERROR) 630 goto abort; 631 632 if (ap->th_opcode == ACK) { 633 if (ap->th_block == block) 634 break; 635 /* Re-synchronize with the other side */ 636 (void) synchnet(peer); 637 if (ap->th_block == (block -1)) 638 goto send_data; 639 } 640 641 } 642 block++; 643 } while (size == SEGSIZE); 644 abort: 645 (void) fclose(file); 646 } 647 648 void 649 justquit(dummy) 650 int dummy; 651 { 652 exit(0); 653 } 654 655 /* 656 * Receive a file. 657 */ 658 void 659 recvfile(pf) 660 struct formats *pf; 661 { 662 struct tftphdr *dp; 663 struct tftphdr *ap; /* ack buffer */ 664 int n, size; 665 volatile int block; 666 667 signal(SIGALRM, timer); 668 dp = w_init(); 669 ap = (struct tftphdr *)ackbuf; 670 block = 0; 671 do { 672 timeout = 0; 673 ap->th_opcode = htons((u_short)ACK); 674 ap->th_block = htons((u_short)block); 675 block++; 676 (void) setjmp(timeoutbuf); 677 send_ack: 678 if (send(peer, ackbuf, 4, 0) != 4) { 679 syslog(LOG_ERR, "tftpd: write: %m"); 680 goto abort; 681 } 682 write_behind(file, pf->f_convert); 683 for ( ; ; ) { 684 alarm(rexmtval); 685 n = recv(peer, dp, PKTSIZE, 0); 686 alarm(0); 687 if (n < 0) { /* really? */ 688 syslog(LOG_ERR, "tftpd: read: %m"); 689 goto abort; 690 } 691 dp->th_opcode = ntohs((u_short)dp->th_opcode); 692 dp->th_block = ntohs((u_short)dp->th_block); 693 if (dp->th_opcode == ERROR) 694 goto abort; 695 if (dp->th_opcode == DATA) { 696 if (dp->th_block == block) { 697 break; /* normal */ 698 } 699 /* Re-synchronize with the other side */ 700 (void) synchnet(peer); 701 if (dp->th_block == (block-1)) 702 goto send_ack; /* rexmit */ 703 } 704 } 705 /* size = write(file, dp->th_data, n - 4); */ 706 size = writeit(file, &dp, n - 4, pf->f_convert); 707 if (size != (n-4)) { /* ahem */ 708 if (size < 0) nak(errno + 100); 709 else nak(ENOSPACE); 710 goto abort; 711 } 712 } while (size == SEGSIZE); 713 write_behind(file, pf->f_convert); 714 (void) fclose(file); /* close data file */ 715 716 ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */ 717 ap->th_block = htons((u_short)(block)); 718 (void) send(peer, ackbuf, 4, 0); 719 720 signal(SIGALRM, justquit); /* just quit on timeout */ 721 alarm(rexmtval); 722 n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */ 723 alarm(0); 724 if (n >= 4 && /* if read some data */ 725 dp->th_opcode == DATA && /* and got a data block */ 726 block == dp->th_block) { /* then my last ack was lost */ 727 (void) send(peer, ackbuf, 4, 0); /* resend final ack */ 728 } 729 abort: 730 return; 731 } 732 733 const struct errmsg { 734 int e_code; 735 const char *e_msg; 736 } errmsgs[] = { 737 { EUNDEF, "Undefined error code" }, 738 { ENOTFOUND, "File not found" }, 739 { EACCESS, "Access violation" }, 740 { ENOSPACE, "Disk full or allocation exceeded" }, 741 { EBADOP, "Illegal TFTP operation" }, 742 { EBADID, "Unknown transfer ID" }, 743 { EEXISTS, "File already exists" }, 744 { ENOUSER, "No such user" }, 745 { -1, 0 } 746 }; 747 748 static const char * 749 errtomsg(error) 750 int error; 751 { 752 static char buf[20]; 753 const struct errmsg *pe; 754 755 if (error == 0) 756 return "success"; 757 for (pe = errmsgs; pe->e_code >= 0; pe++) 758 if (pe->e_code == error) 759 return pe->e_msg; 760 sprintf(buf, "error %d", error); 761 return buf; 762 } 763 764 /* 765 * Send a nak packet (error message). 766 * Error code passed in is one of the 767 * standard TFTP codes, or a UNIX errno 768 * offset by 100. 769 */ 770 static void 771 nak(error) 772 int error; 773 { 774 struct tftphdr *tp; 775 int length; 776 const struct errmsg *pe; 777 778 tp = (struct tftphdr *)buf; 779 tp->th_opcode = htons((u_short)ERROR); 780 for (pe = errmsgs; pe->e_code >= 0; pe++) 781 if (pe->e_code == error) 782 break; 783 if (pe->e_code < 0) { 784 tp->th_code = EUNDEF; /* set 'undef' errorcode */ 785 strcpy(tp->th_msg, strerror(error - 100)); 786 } else { 787 tp->th_code = htons((u_short)error); 788 strcpy(tp->th_msg, pe->e_msg); 789 } 790 length = strlen(pe->e_msg); 791 tp->th_msg[length] = '\0'; 792 length += 5; 793 if (send(peer, buf, length, 0) != length) 794 syslog(LOG_ERR, "nak: %m"); 795 } 796 797 static char * 798 verifyhost(fromp) 799 struct sockaddr *fromp; 800 { 801 static char hbuf[MAXHOSTNAMELEN]; 802 803 getnameinfo(fromp, fromp->sa_len, hbuf, sizeof(hbuf), NULL, 0, 0); 804 return hbuf; 805 } 806