1 /* $NetBSD: tftpd.c,v 1.31 2008/07/21 13:25:47 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
37 static char sccsid
[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93";
39 __RCSID("$NetBSD: tftpd.c,v 1.31 2008/07/21 13:25:47 lukem Exp $");
44 * Trivial file transfer protocol server.
46 * This version includes many modifications by Jim Guyton
50 #include <sys/param.h>
51 #include <sys/ioctl.h>
53 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/tftp.h>
57 #include <arpa/inet.h>
76 #define DEFAULTUSER "nobody"
81 int rexmtval
= TIMEOUT
;
82 int maxtimeout
= 5*TIMEOUT
;
86 char oackbuf
[PKTSIZE
];
87 struct sockaddr_storage from
;
91 int tftp_opt_tsize
= 0;
92 int tftp_blksize
= SEGSIZE
;
96 * Null-terminated directory prefix list for absolute pathname requests and
97 * search list for relative pathname requests.
99 * MAXDIRS should be at least as large as the number of arguments that
100 * inetd allows (currently 20).
103 static struct dirlist
{
107 static int suppress_naks
;
110 static char *securedir
;
114 static const char *errtomsg(int);
115 static void nak(int);
116 static void tftp(struct tftphdr
*, int);
117 static void usage(void);
118 static char *verifyhost(struct sockaddr
*);
120 int main(int, char **);
121 void recvfile(struct formats
*, int, int);
122 void sendfile(struct formats
*, int, int);
124 static const char *opcode(int);
125 int validate_access(char **, int);
129 int (*f_validate
)(char **, int);
130 void (*f_send
)(struct formats
*, int, int);
131 void (*f_recv
)(struct formats
*, int, int);
134 { "netascii", validate_access
, sendfile
, recvfile
, 1 },
135 { "octet", validate_access
, sendfile
, recvfile
, 0 },
144 "Usage: %s [-dln] [-u user] [-g group] [-s directory] [directory ...]",
150 main(int argc
, char *argv
[])
152 struct sockaddr_storage me
;
153 struct passwd
*pwent
;
156 char *tgtuser
, *tgtgroup
, *ep
;
160 uid_t curuid
, tgtuid
;
161 gid_t curgid
, tgtgid
;
167 openlog("tftpd", LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
168 tgtuser
= DEFAULTUSER
;
173 while ((ch
= getopt(argc
, argv
, "dg:lns:u:")) != -1)
206 struct dirlist
*dirp
;
208 /* Get list of directory prefixes. Skip relative pathnames. */
209 for (dirp
= dirs
; optind
< argc
&& dirp
< &dirs
[MAXDIRS
];
211 if (argv
[optind
][0] == '/') {
212 dirp
->name
= argv
[optind
];
213 dirp
->len
= strlen(dirp
->name
);
219 if (*tgtuser
== '\0' || (tgtgroup
!= NULL
&& *tgtgroup
== '\0'))
222 nid
= (strtol(tgtuser
, &ep
, 10));
225 syslog(LOG_ERR
, "uid %ld is too large", nid
);
228 pwent
= getpwuid((uid_t
)nid
);
230 pwent
= getpwnam(tgtuser
);
232 syslog(LOG_ERR
, "unknown user `%s'", tgtuser
);
235 tgtuid
= pwent
->pw_uid
;
236 tgtgid
= pwent
->pw_gid
;
238 if (tgtgroup
!= NULL
) {
239 nid
= (strtol(tgtgroup
, &ep
, 10));
242 syslog(LOG_ERR
, "gid %ld is too large", nid
);
245 grent
= getgrgid((gid_t
)nid
);
247 grent
= getgrnam(tgtgroup
);
249 tgtgid
= grent
->gr_gid
;
251 syslog(LOG_ERR
, "unknown group `%s'", tgtgroup
);
257 if (chdir(securedir
) < 0) {
258 syslog(LOG_ERR
, "chdir %s: %m", securedir
);
262 syslog(LOG_ERR
, "chroot: %m");
268 syslog(LOG_DEBUG
, "running as user `%s' (%d), group `%s' (%d)",
269 tgtuser
, tgtuid
, tgtgroup
? tgtgroup
: "(unspecified)",
271 if (curgid
!= tgtgid
) {
272 if (setgid(tgtgid
)) {
273 syslog(LOG_ERR
, "setgid to %d: %m", (int)tgtgid
);
276 if (setgroups(0, NULL
)) {
277 syslog(LOG_ERR
, "setgroups: %m");
282 if (curuid
!= tgtuid
) {
283 if (setuid(tgtuid
)) {
284 syslog(LOG_ERR
, "setuid to %d: %m", (int)tgtuid
);
290 if (ioctl(fd
, FIONBIO
, &on
) < 0) {
291 syslog(LOG_ERR
, "ioctl(FIONBIO): %m");
294 fromlen
= sizeof (from
);
295 n
= recvfrom(fd
, buf
, sizeof (buf
), 0,
296 (struct sockaddr
*)&from
, &fromlen
);
298 syslog(LOG_ERR
, "recvfrom: %m");
302 * Now that we have read the message out of the UDP
303 * socket, we fork and exit. Thus, inetd will go back
304 * to listening to the tftp port, and the next request
305 * to come in will start up a new instance of tftpd.
307 * We do this so that inetd can run tftpd in "wait" mode.
308 * The problem with tftpd running in "nowait" mode is that
309 * inetd may get one or more successful "selects" on the
310 * tftp port before we do our receive, so more than one
311 * instance of tftpd may be started up. Worse, if tftpd
312 * break before doing the above "recvfrom", inetd would
313 * spawn endless instances, clogging the system.
320 for (i
= 1; i
< 20; i
++) {
325 * flush out to most recently sent request.
327 * This may drop some request, but those
328 * will be resent by the clients when
329 * they timeout. The positive effect of
330 * this flush is to (try to) prevent more
331 * than one tftpd being started up to service
332 * a single request from a single client.
335 i
= recvfrom(fd
, buf
, sizeof (buf
), 0,
336 (struct sockaddr
*)&from
, &j
);
346 syslog(LOG_ERR
, "fork: %m");
348 } else if (pid
!= 0) {
354 * remember what address this was sent to, so we can respond on the
358 if (getsockname(fd
, (struct sockaddr
*)&me
, &len
) == 0) {
359 switch (me
.ss_family
) {
361 ((struct sockaddr_in
*)&me
)->sin_port
= 0;
364 ((struct sockaddr_in6
*)&me
)->sin6_port
= 0;
371 memset(&me
, 0, sizeof(me
));
372 me
.ss_family
= from
.ss_family
;
373 me
.ss_len
= from
.ss_len
;
379 peer
= socket(from
.ss_family
, SOCK_DGRAM
, 0);
381 syslog(LOG_ERR
, "socket: %m");
384 if (bind(peer
, (struct sockaddr
*)&me
, me
.ss_len
) < 0) {
385 syslog(LOG_ERR
, "bind: %m");
388 if (connect(peer
, (struct sockaddr
*)&from
, from
.ss_len
) < 0) {
389 syslog(LOG_ERR
, "connect: %m");
392 soopt
= 65536; /* larger than we'll ever need */
393 if (setsockopt(peer
, SOL_SOCKET
, SO_SNDBUF
, (void *) &soopt
, sizeof(soopt
)) < 0) {
394 syslog(LOG_ERR
, "set SNDBUF: %m");
397 if (setsockopt(peer
, SOL_SOCKET
, SO_RCVBUF
, (void *) &soopt
, sizeof(soopt
)) < 0) {
398 syslog(LOG_ERR
, "set RCVBUF: %m");
402 tp
= (struct tftphdr
*)buf
;
403 tp
->th_opcode
= ntohs(tp
->th_opcode
);
404 if (tp
->th_opcode
== RRQ
|| tp
->th_opcode
== WRQ
)
410 blk_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
418 * On these failures, we could just ignore the blocksize option.
419 * Perhaps that should be a command-line option.
422 bsize
= strtoul(val
, &endp
, 10);
423 if ((bsize
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
424 syslog(LOG_NOTICE
, "%s: %s request for %s: "
425 "illegal value %s for blksize option",
426 verifyhost((struct sockaddr
*)&from
),
427 tp
->th_opcode
== WRQ
? "write" : "read",
431 if (bsize
< 8 || bsize
> 65464) {
432 syslog(LOG_NOTICE
, "%s: %s request for %s: "
433 "out of range value %s for blksize option",
434 verifyhost((struct sockaddr
*)&from
),
435 tp
->th_opcode
== WRQ
? "write" : "read",
440 tftp_blksize
= bsize
;
441 strcpy(ack
+ *ackl
, "blksize");
443 l
= sprintf(ack
+ *ackl
, "%lu", bsize
);
450 timeout_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
458 tout
= strtoul(val
, &endp
, 10);
459 if ((tout
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
460 syslog(LOG_NOTICE
, "%s: %s request for %s: "
461 "illegal value %s for timeout option",
462 verifyhost((struct sockaddr
*)&from
),
463 tp
->th_opcode
== WRQ
? "write" : "read",
467 if (tout
< 1 || tout
> 255) {
468 syslog(LOG_NOTICE
, "%s: %s request for %s: "
469 "out of range value %s for timeout option",
470 verifyhost((struct sockaddr
*)&from
),
471 tp
->th_opcode
== WRQ
? "write" : "read",
477 strcpy(ack
+ *ackl
, "timeout");
479 l
= sprintf(ack
+ *ackl
, "%lu", tout
);
483 * Arbitrarily pick a maximum timeout on a request to 3
484 * retransmissions if the interval timeout is more than
485 * one minute. Longest possible timeout is therefore
486 * 3 * 255 - 1, or 764 seconds.
489 maxtimeout
= rexmtval
* 3;
491 maxtimeout
= rexmtval
* 5;
498 tsize_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
505 * Maximum file even with extended tftp is 65535 blocks of
506 * length 65464, or 4290183240 octets (4784056 less than 2^32).
507 * unsigned long is at least 32 bits on all NetBSD archs.
511 fsize
= strtoul(val
, &endp
, 10);
512 if ((fsize
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
513 syslog(LOG_NOTICE
, "%s: %s request for %s: "
514 "illegal value %s for tsize option",
515 verifyhost((struct sockaddr
*)&from
),
516 tp
->th_opcode
== WRQ
? "write" : "read",
520 if (fsize
> (unsigned long) 65535 * 65464) {
521 syslog(LOG_NOTICE
, "%s: %s request for %s: "
522 "out of range value %s for tsize option",
523 verifyhost((struct sockaddr
*)&from
),
524 tp
->th_opcode
== WRQ
? "write" : "read",
532 * We will report this later -- either replying with the fsize (WRQ)
533 * or replying with the actual filesize (RRQ).
539 struct tftp_options
{
541 int (*o_handler
)(struct tftphdr
*, char *, char *, char *,
544 { "blksize", blk_handler
},
545 { "timeout", timeout_handler
},
546 { "tsize", tsize_handler
},
551 * Get options for an extended tftp session. Stuff the ones we
552 * recognize in oackbuf.
555 get_options(struct tftphdr
*tp
, char *cp
, int size
, char *ackb
,
558 struct tftp_options
*op
;
559 char *option
, *value
, *endp
;
565 while (*cp
&& cp
< endp
) {
566 *cp
= tolower((unsigned char)*cp
);
570 /* if we have garbage at the end, just ignore it */
573 cp
++; /* skip over NUL */
575 while (*cp
&& cp
< endp
) {
579 /* if we have garbage at the end, just ignore it */
583 for (op
= options
; op
->o_name
; op
++) {
584 if (strcmp(op
->o_name
, option
) == 0)
588 r
= op
->o_handler(tp
, option
, value
, ackb
, alen
, &ec
);
594 } /* else ignore unknown options */
604 * Handle initial connection protocol.
607 tftp(struct tftphdr
*tp
, int size
)
611 char *filename
, *mode
;
612 int first
, ecode
, alen
, etftp
=0, r
;
614 ecode
= 0; /* XXX gcc */
618 filename
= cp
= tp
->th_stuff
;
620 while (cp
< buf
+ size
) {
634 for (cp
= mode
; *cp
; cp
++)
635 *cp
= tolower((unsigned char)*cp
);
636 for (pf
= formats
; pf
->f_mode
; pf
++)
637 if (strcmp(pf
->f_mode
, mode
) == 0)
639 if (pf
->f_mode
== 0) {
644 * cp currently points to the NUL byte following the mode.
646 * If we have some valid options, then let's assume that we're
647 * now dealing with an extended tftp session. Note that if we
648 * don't get any options, then we *must* assume that we do not
649 * have an extended tftp session. If we get options, we fill
650 * in the ack buf to acknowledge them. If we skip that, then
651 * the client *must* assume that we are not using an extended
654 size
-= (++cp
- (char *) tp
);
655 if (size
> 0 && *cp
) {
656 alen
= 2; /* Skip over opcode */
657 r
= get_options(tp
, cp
, size
, oackbuf
, &alen
, &ecode
);
665 ecode
= (*pf
->f_validate
)(&filename
, tp
->th_opcode
);
667 syslog(LOG_INFO
, "%s: %s request for %s: %s",
668 verifyhost((struct sockaddr
*)&from
),
669 tp
->th_opcode
== WRQ
? "write" : "read",
670 filename
, errtomsg(ecode
));
674 * Avoid storms of naks to a RRQ broadcast for a relative
675 * bootfile pathname from a diskless Sun.
677 if (suppress_naks
&& *filename
!= '/' && ecode
== ENOTFOUND
)
684 struct tftphdr
*oack_h
;
686 if (tftp_opt_tsize
) {
689 strcpy(oackbuf
+ alen
, "tsize");
691 l
= sprintf(oackbuf
+ alen
, "%u", tftp_tsize
);
694 oack_h
= (struct tftphdr
*) oackbuf
;
695 oack_h
->th_opcode
= htons(OACK
);
698 if (tp
->th_opcode
== WRQ
)
699 (*pf
->f_recv
)(pf
, etftp
, alen
);
701 (*pf
->f_send
)(pf
, etftp
, alen
);
709 * Validate file access. Since we
710 * have no uid or gid, for now require
711 * file to exist and be publicly
713 * If we were invoked with arguments
714 * from inetd then the file must also be
715 * in one of the given directory prefixes.
718 validate_access(char **filep
, int mode
)
721 struct dirlist
*dirp
;
722 static char pathname
[MAXPATHLEN
];
729 * Prevent tricksters from getting around the directory restrictions
731 if (strstr(filename
, "/../"))
734 if (*filename
== '/') {
736 * Allow the request if it's in one of the approved locations.
737 * Special case: check the null prefix ("/") by looking
738 * for length = 1 and relying on the arg. processing that
741 for (dirp
= dirs
; dirp
->name
!= NULL
; dirp
++) {
742 if (dirp
->len
== 1 ||
743 (!strncmp(filename
, dirp
->name
, dirp
->len
) &&
744 filename
[dirp
->len
] == '/'))
747 /* If directory list is empty, allow access to any file */
748 if (dirp
->name
== NULL
&& dirp
!= dirs
)
750 if (stat(filename
, &stbuf
) < 0)
751 return (errno
== ENOENT
? ENOTFOUND
: EACCESS
);
752 if (!S_ISREG(stbuf
.st_mode
))
755 if ((stbuf
.st_mode
& S_IROTH
) == 0)
758 if ((stbuf
.st_mode
& S_IWOTH
) == 0)
763 * Relative file name: search the approved locations for it.
766 if (!strncmp(filename
, "../", 3))
770 * Find the first file that exists in any of the directories,
771 * check access on it.
773 if (dirs
[0].name
!= NULL
) {
774 for (dirp
= dirs
; dirp
->name
!= NULL
; dirp
++) {
775 snprintf(pathname
, sizeof pathname
, "%s/%s",
776 dirp
->name
, filename
);
777 if (stat(pathname
, &stbuf
) == 0 &&
778 (stbuf
.st_mode
& S_IFMT
) == S_IFREG
) {
782 if (dirp
->name
== NULL
)
784 if (mode
== RRQ
&& !(stbuf
.st_mode
& S_IROTH
))
786 if (mode
== WRQ
&& !(stbuf
.st_mode
& S_IWOTH
))
788 *filep
= filename
= pathname
;
791 * If there's no directory list, take our cue from the
792 * absolute file request check above (*filename == '/'),
793 * and allow access to anything.
795 if (stat(filename
, &stbuf
) < 0)
796 return (errno
== ENOENT
? ENOTFOUND
: EACCESS
);
797 if (!S_ISREG(stbuf
.st_mode
))
800 if ((stbuf
.st_mode
& S_IROTH
) == 0)
803 if ((stbuf
.st_mode
& S_IWOTH
) == 0)
810 if (tftp_opt_tsize
&& mode
== RRQ
)
811 tftp_tsize
= (unsigned long) stbuf
.st_size
;
813 fd
= open(filename
, mode
== RRQ
? O_RDONLY
: O_WRONLY
| O_TRUNC
);
815 return (errno
+ 100);
816 file
= fdopen(fd
, (mode
== RRQ
)? "r":"w");
819 return (errno
+ 100);
832 if (timeout
>= maxtimeout
)
834 longjmp(timeoutbuf
, 1);
840 static char obuf
[64];
856 (void)snprintf(obuf
, sizeof(obuf
), "*code %d*", code
);
862 * Send the requested file.
865 sendfile(struct formats
*pf
, int etftp
, int acklength
)
867 volatile unsigned int block
;
869 struct tftphdr
*ap
; /* ack packet */
872 signal(SIGALRM
, timer
);
873 ap
= (struct tftphdr
*)ackbuf
;
875 dp
= (struct tftphdr
*)oackbuf
;
876 size
= acklength
- 4;
886 size
= readit(file
, &dp
, tftp_blksize
, pf
->f_convert
);
891 dp
->th_opcode
= htons((u_short
)DATA
);
892 dp
->th_block
= htons((u_short
)block
);
895 (void)setjmp(timeoutbuf
);
899 syslog(LOG_DEBUG
, "Send DATA %u", block
);
900 if ((n
= send(peer
, dp
, size
+ 4, 0)) != size
+ 4) {
901 syslog(LOG_ERR
, "tftpd: write: %m");
905 read_ahead(file
, tftp_blksize
, pf
->f_convert
);
907 alarm(rexmtval
); /* read the ack */
908 n
= recv(peer
, ackbuf
, tftp_blksize
, 0);
911 syslog(LOG_ERR
, "tftpd: read: %m");
914 ap
->th_opcode
= ntohs((u_short
)ap
->th_opcode
);
915 ap
->th_block
= ntohs((u_short
)ap
->th_block
);
916 switch (ap
->th_opcode
) {
921 if (ap
->th_block
== 0) {
927 if (ap
->th_block
== block
)
930 syslog(LOG_DEBUG
, "Resync ACK %u != %u",
931 (unsigned int)ap
->th_block
, block
);
932 /* Re-synchronize with the other side */
933 (void) synchnet(peer
, tftp_blksize
);
934 if (ap
->th_block
== (block
-1))
937 syslog(LOG_INFO
, "Received %s in sendfile\n",
938 opcode(dp
->th_opcode
));
944 syslog(LOG_DEBUG
, "Received ACK for block %u", block
);
946 } while (size
== tftp_blksize
|| block
== 1);
962 recvfile(struct formats
*pf
, int etftp
, int acklength
)
964 volatile unsigned int block
;
966 struct tftphdr
*ap
; /* ack buffer */
969 signal(SIGALRM
, timer
);
971 ap
= (struct tftphdr
*)oackbuf
;
976 ap
= (struct tftphdr
*)ackbuf
;
977 ap
->th_opcode
= htons((u_short
)ACK
);
978 ap
->th_block
= htons((u_short
)block
);
982 syslog(LOG_DEBUG
, "Sending ACK for block %u\n", block
);
984 (void) setjmp(timeoutbuf
);
986 if (send(peer
, ap
, acklength
, 0) != acklength
) {
987 syslog(LOG_ERR
, "tftpd: write: %m");
990 write_behind(file
, pf
->f_convert
);
993 n
= recv(peer
, dp
, tftp_blksize
+ 4, 0);
995 if (n
< 0) { /* really? */
996 syslog(LOG_ERR
, "tftpd: read: %m");
1000 dp
->th_opcode
= ntohs((u_short
)dp
->th_opcode
);
1001 dp
->th_block
= ntohs((u_short
)dp
->th_block
);
1003 syslog(LOG_DEBUG
, "Received %s for block %u",
1004 opcode(dp
->th_opcode
),
1005 (unsigned int)dp
->th_block
);
1007 switch (dp
->th_opcode
) {
1011 if (dp
->th_block
== block
)
1012 goto done
; /* normal */
1014 syslog(LOG_DEBUG
, "Resync %u != %u",
1015 (unsigned int)dp
->th_block
, block
);
1016 /* Re-synchronize with the other side */
1017 (void) synchnet(peer
, tftp_blksize
);
1018 if (dp
->th_block
== (block
-1))
1019 goto send_ack
; /* rexmit */
1022 syslog(LOG_INFO
, "Received %s in recvfile\n",
1023 opcode(dp
->th_opcode
));
1029 syslog(LOG_DEBUG
, "Got block %u", block
);
1030 /* size = write(file, dp->th_data, n - 4); */
1031 size
= writeit(file
, &dp
, n
- 4, pf
->f_convert
);
1032 if (size
!= (n
-4)) { /* ahem */
1033 if (size
< 0) nak(errno
+ 100);
1037 } while (size
== tftp_blksize
);
1038 write_behind(file
, pf
->f_convert
);
1039 (void) fclose(file
); /* close data file */
1041 ap
->th_opcode
= htons((u_short
)ACK
); /* send the "final" ack */
1042 ap
->th_block
= htons((u_short
)(block
));
1044 syslog(LOG_DEBUG
, "Send final ACK %u", block
);
1045 (void) send(peer
, ackbuf
, 4, 0);
1047 signal(SIGALRM
, justquit
); /* just quit on timeout */
1049 n
= recv(peer
, buf
, sizeof (buf
), 0); /* normally times out and quits */
1051 if (n
>= 4 && /* if read some data */
1052 dp
->th_opcode
== DATA
&& /* and got a data block */
1053 block
== dp
->th_block
) { /* then my last ack was lost */
1054 (void) send(peer
, ackbuf
, 4, 0); /* resend final ack */
1060 const struct errmsg
{
1064 { EUNDEF
, "Undefined error code" },
1065 { ENOTFOUND
, "File not found" },
1066 { EACCESS
, "Access violation" },
1067 { ENOSPACE
, "Disk full or allocation exceeded" },
1068 { EBADOP
, "Illegal TFTP operation" },
1069 { EBADID
, "Unknown transfer ID" },
1070 { EEXISTS
, "File already exists" },
1071 { ENOUSER
, "No such user" },
1072 { EOPTNEG
, "Option negotiation failed" },
1079 static char ebuf
[20];
1080 const struct errmsg
*pe
;
1084 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
1085 if (pe
->e_code
== error
)
1087 snprintf(ebuf
, sizeof(ebuf
), "error %d", error
);
1092 * Send a nak packet (error message).
1093 * Error code passed in is one of the
1094 * standard TFTP codes, or a UNIX errno
1100 const struct errmsg
*pe
;
1105 tp
= (struct tftphdr
*)buf
;
1106 tp
->th_opcode
= htons((u_short
)ERROR
);
1107 msglen
= sizeof(buf
) - (&tp
->th_msg
[0] - buf
);
1108 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
1109 if (pe
->e_code
== error
)
1111 if (pe
->e_code
< 0) {
1112 tp
->th_code
= EUNDEF
; /* set 'undef' errorcode */
1113 strlcpy(tp
->th_msg
, strerror(error
- 100), msglen
);
1115 tp
->th_code
= htons((u_short
)error
);
1116 strlcpy(tp
->th_msg
, pe
->e_msg
, msglen
);
1119 syslog(LOG_DEBUG
, "Send NACK %s", tp
->th_msg
);
1120 length
= strlen(tp
->th_msg
);
1121 msglen
= &tp
->th_msg
[length
+ 1] - buf
;
1122 if (send(peer
, buf
, msglen
, 0) != msglen
)
1123 syslog(LOG_ERR
, "nak: %m");
1127 verifyhost(struct sockaddr
*fromp
)
1129 static char hbuf
[MAXHOSTNAMELEN
];
1131 if (getnameinfo(fromp
, fromp
->sa_len
, hbuf
, sizeof(hbuf
), NULL
, 0, 0))
1132 strlcpy(hbuf
, "?", sizeof(hbuf
));