Deleted Added
sdiff udiff text old ( 71926 ) new ( 84047 )
full compact
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/libexec/tftpd/tftpd.c 71926 2001-02-02 10:53:02Z asmodai $";
46#endif /* not lint */
47
48/*
49 * Trivial file transfer protocol server.
50 *
51 * This version includes many modifications by Jim Guyton
52 * <guyton@rand-unix>.
53 */

--- 20 unchanged lines hidden (view full) ---

74#include <stdlib.h>
75#include <string.h>
76#include <syslog.h>
77#include <unistd.h>
78
79#include "tftpsubs.h"
80
81#define TIMEOUT 5
82
83int peer;
84int rexmtval = TIMEOUT;
85int maxtimeout = 5*TIMEOUT;
86
87#define PKTSIZE SEGSIZE+4
88char buf[PKTSIZE];
89char ackbuf[PKTSIZE];
90struct sockaddr_in from;
91int fromlen;
92
93void tftp __P((struct tftphdr *, int));

--- 11 unchanged lines hidden (view full) ---

105 int len;
106} dirs[MAXDIRS+1];
107static int suppress_naks;
108static int logging;
109static int ipchroot;
110
111static char *errtomsg __P((int));
112static void nak __P((int));
113
114int
115main(argc, argv)
116 int argc;
117 char *argv[];
118{
119 register struct tftphdr *tp;
120 register int n;

--- 185 unchanged lines hidden (view full) ---

306 { "netascii", validate_access, xmitfile, recvfile, 1 },
307 { "octet", validate_access, xmitfile, recvfile, 0 },
308#ifdef notdef
309 { "mail", validate_user, sendmail, recvmail, 1 },
310#endif
311 { 0 }
312};
313
314/*
315 * Handle initial connection protocol.
316 */
317void
318tftp(tp, size)
319 struct tftphdr *tp;
320 int size;
321{
322 register char *cp;
323 int first = 1, ecode;
324 register struct formats *pf;
325 char *filename, *mode;
326
327 filename = cp = tp->th_stuff;
328again:
329 while (cp < buf + size) {
330 if (*cp == '\0')
331 break;
332 cp++;
333 }

--- 11 unchanged lines hidden (view full) ---

345 *cp = tolower(*cp);
346 for (pf = formats; pf->f_mode; pf++)
347 if (strcmp(pf->f_mode, mode) == 0)
348 break;
349 if (pf->f_mode == 0) {
350 nak(EBADOP);
351 exit(1);
352 }
353 ecode = (*pf->f_validate)(&filename, tp->th_opcode);
354 if (logging) {
355 char host[MAXHOSTNAMELEN];
356
357 realhostname(host, sizeof(host) - 1, &from.sin_addr);
358 host[sizeof(host) - 1] = '\0';
359 syslog(LOG_INFO, "%s: %s request for %s: %s", host,
360 tp->th_opcode == WRQ ? "write" : "read",
361 filename, errtomsg(ecode));

--- 101 unchanged lines hidden (view full) ---

463 }
464 err = EACCESS;
465 }
466 }
467 if (dirp->name == NULL)
468 return (err);
469 *filep = filename = pathname;
470 }
471 fd = open(filename, mode == RRQ ? O_RDONLY : O_WRONLY|O_TRUNC);
472 if (fd < 0)
473 return (errno + 100);
474 file = fdopen(fd, (mode == RRQ)? "r":"w");
475 if (file == NULL) {
476 return errno+100;
477 }
478 return (0);
479}
480
481int timeout;
482jmp_buf timeoutbuf;
483
484void
485timer()
486{
487
488 timeout += rexmtval;
489 if (timeout >= maxtimeout)
490 exit(1);
491 longjmp(timeoutbuf, 1);
492}
493
494/*
495 * Send the requested file.
496 */
497void

--- 12 unchanged lines hidden (view full) ---

510 do {
511 size = readit(file, &dp, pf->f_convert);
512 if (size < 0) {
513 nak(errno + 100);
514 goto abort;
515 }
516 dp->th_opcode = htons((u_short)DATA);
517 dp->th_block = htons((u_short)block);
518 timeout = 0;
519 (void)setjmp(timeoutbuf);
520
521send_data:
522 if (send(peer, dp, size + 4, 0) != size + 4) {
523 syslog(LOG_ERR, "write: %m");
524 goto abort;
525 }
526 read_ahead(file, pf->f_convert);

--- 46 unchanged lines hidden (view full) ---

573 register int n, size;
574 volatile unsigned short block;
575
576 signal(SIGALRM, timer);
577 dp = w_init();
578 ap = (struct tftphdr *)ackbuf;
579 block = 0;
580 do {
581 timeout = 0;
582 ap->th_opcode = htons((u_short)ACK);
583 ap->th_block = htons((u_short)block);
584 block++;
585 (void) setjmp(timeoutbuf);
586send_ack:
587 if (send(peer, ackbuf, 4, 0) != 4) {
588 syslog(LOG_ERR, "write: %m");
589 goto abort;

--- 56 unchanged lines hidden (view full) ---

646 { EUNDEF, "Undefined error code" },
647 { ENOTFOUND, "File not found" },
648 { EACCESS, "Access violation" },
649 { ENOSPACE, "Disk full or allocation exceeded" },
650 { EBADOP, "Illegal TFTP operation" },
651 { EBADID, "Unknown transfer ID" },
652 { EEXISTS, "File already exists" },
653 { ENOUSER, "No such user" },
654 { -1, 0 }
655};
656
657static char *
658errtomsg(error)
659 int error;
660{
661 static char buf[20];

--- 33 unchanged lines hidden (view full) ---

695 }
696 strcpy(tp->th_msg, pe->e_msg);
697 length = strlen(pe->e_msg);
698 tp->th_msg[length] = '\0';
699 length += 5;
700 if (send(peer, buf, length, 0) != length)
701 syslog(LOG_ERR, "nak: %m");
702}