]>
git.saurik.com Git - apple/network_cmds.git/blob - tftpd.tproj/tftpd.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $NetBSD: tftpd.c,v 1.28 2004/05/05 20:15:45 kleink Exp $ */
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 #include <sys/cdefs.h>
56 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
57 The Regents of the University of California. All rights reserved.\n");
59 static char sccsid
[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93";
61 __RCSID("$NetBSD: tftpd.c,v 1.28 2004/05/05 20:15:45 kleink Exp $");
66 * Trivial file transfer protocol server.
68 * This version includes many modifications by Jim Guyton
72 #include <sys/param.h>
73 #include <sys/ioctl.h>
75 #include <sys/socket.h>
77 #include <netinet/in.h>
79 #include <arpa/inet.h>
98 #define DEFAULTUSER "nobody"
103 int rexmtval
= TIMEOUT
;
104 int maxtimeout
= 5*TIMEOUT
;
106 char buf
[MAXPKTSIZE
];
107 char ackbuf
[PKTSIZE
];
108 char oackbuf
[PKTSIZE
];
109 struct sockaddr_storage from
;
113 int tftp_opt_tsize
= 0;
114 int tftp_blksize
= SEGSIZE
;
118 * Null-terminated directory prefix list for absolute pathname requests and
119 * search list for relative pathname requests.
121 * MAXDIRS should be at least as large as the number of arguments that
122 * inetd allows (currently 20).
125 static struct dirlist
{
129 static int suppress_naks
;
131 static int insecure
=0;
133 static char *securedir
;
137 static const char *errtomsg(int);
138 static void nak(int);
139 static void tftp(struct tftphdr
*, int);
140 static void usage(void);
141 static char *verifyhost(struct sockaddr
*);
143 int main(int, char **);
144 void recvfile(struct formats
*, int, int);
145 void sendfile(struct formats
*, int, int);
147 static const char *opcode(int);
148 int validate_access(char **, int);
152 int (*f_validate
)(char **, int);
153 void (*f_send
)(struct formats
*, int, int);
154 void (*f_recv
)(struct formats
*, int, int);
157 { "netascii", validate_access
, sendfile
, recvfile
, 1 },
158 { "octet", validate_access
, sendfile
, recvfile
, 0 },
167 "Usage: %s [-diln] [-u user] [-g group] [-s directory] [directory ...]",
173 main(int argc
, char *argv
[])
175 struct sockaddr_storage me
;
176 struct passwd
*pwent
;
179 char *tgtuser
, *tgtgroup
, *ep
;
182 uid_t curuid
, tgtuid
;
183 gid_t curgid
, tgtgid
;
189 openlog("tftpd", LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
190 tgtuser
= DEFAULTUSER
;
195 while ((ch
= getopt(argc
, argv
, "dg:ilns:u:")) != -1)
232 struct dirlist
*dirp
;
234 /* Get list of directory prefixes. Skip relative pathnames. */
235 for (dirp
= dirs
; optind
< argc
&& dirp
< &dirs
[MAXDIRS
];
237 if (argv
[optind
][0] == '/') {
238 dirp
->name
= argv
[optind
];
239 dirp
->len
= strlen(dirp
->name
);
245 if (*tgtuser
== '\0' || (tgtgroup
!= NULL
&& *tgtgroup
== '\0'))
248 nid
= (strtol(tgtuser
, &ep
, 10));
251 syslog(LOG_ERR
, "uid %ld is too large", nid
);
254 pwent
= getpwuid((uid_t
)nid
);
256 pwent
= getpwnam(tgtuser
);
258 syslog(LOG_ERR
, "unknown user `%s'", tgtuser
);
261 tgtuid
= pwent
->pw_uid
;
262 tgtgid
= pwent
->pw_gid
;
264 if (tgtgroup
!= NULL
) {
265 nid
= (strtol(tgtgroup
, &ep
, 10));
268 syslog(LOG_ERR
, "gid %ld is too large", nid
);
271 grent
= getgrgid((gid_t
)nid
);
273 grent
= getgrnam(tgtgroup
);
275 tgtgid
= grent
->gr_gid
;
277 syslog(LOG_ERR
, "unknown group `%s'", tgtgroup
);
283 if (chdir(securedir
) < 0) {
284 syslog(LOG_ERR
, "chdir %s: %m", securedir
);
288 syslog(LOG_ERR
, "chroot: %m");
294 syslog(LOG_DEBUG
, "running as user `%s' (%d), group `%s' (%d)",
295 tgtuser
, tgtuid
, tgtgroup
? tgtgroup
: "(unspecified)",
297 if (curgid
!= tgtgid
) {
298 if (setgid(tgtgid
)) {
299 syslog(LOG_ERR
, "setgid to %d: %m", (int)tgtgid
);
302 if (setgroups(0, NULL
)) {
303 syslog(LOG_ERR
, "setgroups: %m");
308 if (curuid
!= tgtuid
) {
309 if (setuid(tgtuid
)) {
310 syslog(LOG_ERR
, "setuid to %d: %m", (int)tgtuid
);
316 if (ioctl(fd
, FIONBIO
, &on
) < 0) {
317 syslog(LOG_ERR
, "ioctl(FIONBIO): %m");
320 fromlen
= sizeof (from
);
321 n
= recvfrom(fd
, buf
, sizeof (buf
), 0,
322 (struct sockaddr
*)&from
, &fromlen
);
324 syslog(LOG_ERR
, "recvfrom: %m");
328 * Now that we have read the message out of the UDP
329 * socket, we fork and exit. Thus, inetd will go back
330 * to listening to the tftp port, and the next request
331 * to come in will start up a new instance of tftpd.
333 * We do this so that inetd can run tftpd in "wait" mode.
334 * The problem with tftpd running in "nowait" mode is that
335 * inetd may get one or more successful "selects" on the
336 * tftp port before we do our receive, so more than one
337 * instance of tftpd may be started up. Worse, if tftpd
338 * break before doing the above "recvfrom", inetd would
339 * spawn endless instances, clogging the system.
345 for (i
= 1; i
< 20; i
++) {
350 * flush out to most recently sent request.
352 * This may drop some request, but those
353 * will be resent by the clients when
354 * they timeout. The positive effect of
355 * this flush is to (try to) prevent more
356 * than one tftpd being started up to service
357 * a single request from a single client.
360 i
= recvfrom(fd
, buf
, sizeof (buf
), 0,
361 (struct sockaddr
*)&from
, &j
);
371 syslog(LOG_ERR
, "fork: %m");
373 } else if (pid
!= 0) {
379 * remember what address this was sent to, so we can respond on the
383 if (getsockname(fd
, (struct sockaddr
*)&me
, &len
) == 0) {
384 switch (me
.ss_family
) {
386 ((struct sockaddr_in
*)&me
)->sin_port
= 0;
389 ((struct sockaddr_in6
*)&me
)->sin6_port
= 0;
396 memset(&me
, 0, sizeof(me
));
397 me
.ss_family
= from
.ss_family
;
398 me
.ss_len
= from
.ss_len
;
404 peer
= socket(from
.ss_family
, SOCK_DGRAM
, 0);
406 syslog(LOG_ERR
, "socket: %m");
409 if (bind(peer
, (struct sockaddr
*)&me
, me
.ss_len
) < 0) {
410 syslog(LOG_ERR
, "bind: %m");
413 if (connect(peer
, (struct sockaddr
*)&from
, from
.ss_len
) < 0) {
414 syslog(LOG_ERR
, "connect: %m");
417 soopt
= 65536; /* larger than we'll ever need */
418 if (setsockopt(peer
, SOL_SOCKET
, SO_SNDBUF
, (void *) &soopt
, sizeof(soopt
)) < 0) {
419 syslog(LOG_ERR
, "set SNDBUF: %m");
422 if (setsockopt(peer
, SOL_SOCKET
, SO_RCVBUF
, (void *) &soopt
, sizeof(soopt
)) < 0) {
423 syslog(LOG_ERR
, "set RCVBUF: %m");
427 tp
= (struct tftphdr
*)buf
;
428 tp
->th_opcode
= ntohs(tp
->th_opcode
);
429 if (tp
->th_opcode
== RRQ
|| tp
->th_opcode
== WRQ
)
435 blk_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
443 * On these failures, we could just ignore the blocksize option.
444 * Perhaps that should be a command-line option.
447 bsize
= strtoul(val
, &endp
, 10);
448 if ((bsize
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
449 syslog(LOG_NOTICE
, "%s: %s request for %s: "
450 "illegal value %s for blksize option",
451 verifyhost((struct sockaddr
*)&from
),
452 tp
->th_opcode
== WRQ
? "write" : "read",
456 if (bsize
< 8 || bsize
> 65464) {
457 syslog(LOG_NOTICE
, "%s: %s request for %s: "
458 "out of range value %s for blksize option",
459 verifyhost((struct sockaddr
*)&from
),
460 tp
->th_opcode
== WRQ
? "write" : "read",
465 tftp_blksize
= bsize
;
466 strcpy(ack
+ *ackl
, "blksize");
468 l
= sprintf(ack
+ *ackl
, "%lu", bsize
);
475 timeout_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
483 tout
= strtoul(val
, &endp
, 10);
484 if ((tout
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
485 syslog(LOG_NOTICE
, "%s: %s request for %s: "
486 "illegal value %s for timeout option",
487 verifyhost((struct sockaddr
*)&from
),
488 tp
->th_opcode
== WRQ
? "write" : "read",
492 if (tout
< 1 || tout
> 255) {
493 syslog(LOG_NOTICE
, "%s: %s request for %s: "
494 "out of range value %s for timeout option",
495 verifyhost((struct sockaddr
*)&from
),
496 tp
->th_opcode
== WRQ
? "write" : "read",
502 strcpy(ack
+ *ackl
, "timeout");
504 l
= sprintf(ack
+ *ackl
, "%lu", tout
);
508 * Arbitrarily pick a maximum timeout on a request to 3
509 * retransmissions if the interval timeout is more than
510 * one minute. Longest possible timeout is therefore
511 * 3 * 255 - 1, or 764 seconds.
514 maxtimeout
= rexmtval
* 3;
516 maxtimeout
= rexmtval
* 5;
523 tsize_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
530 * Maximum file even with extended tftp is 65535 blocks of
531 * length 65464, or 4290183240 octets (4784056 less than 2^32).
532 * unsigned long is at least 32 bits on all NetBSD archs.
536 fsize
= strtoul(val
, &endp
, 10);
537 if ((fsize
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
538 syslog(LOG_NOTICE
, "%s: %s request for %s: "
539 "illegal value %s for tsize option",
540 verifyhost((struct sockaddr
*)&from
),
541 tp
->th_opcode
== WRQ
? "write" : "read",
545 if (fsize
> (unsigned long) 65535 * 65464) {
546 syslog(LOG_NOTICE
, "%s: %s request for %s: "
547 "out of range value %s for tsize option",
548 verifyhost((struct sockaddr
*)&from
),
549 tp
->th_opcode
== WRQ
? "write" : "read",
557 * We will report this later -- either replying with the fsize (WRQ)
558 * or replying with the actual filesize (RRQ).
564 struct tftp_options
{
566 int (*o_handler
)(struct tftphdr
*, char *, char *, char *,
569 { "blksize", blk_handler
},
570 { "timeout", timeout_handler
},
571 { "tsize", tsize_handler
},
576 * Get options for an extended tftp session. Stuff the ones we
577 * recognize in oackbuf.
580 get_options(struct tftphdr
*tp
, char *cp
, int size
, char *ackb
,
583 struct tftp_options
*op
;
584 char *option
, *value
, *endp
;
590 while (*cp
&& cp
< endp
) {
595 /* if we have garbage at the end, just ignore it */
598 cp
++; /* skip over NUL */
600 while (*cp
&& cp
< endp
) {
604 /* if we have garbage at the end, just ignore it */
608 for (op
= options
; op
->o_name
; op
++) {
609 if (strcmp(op
->o_name
, option
) == 0)
613 r
= op
->o_handler(tp
, option
, value
, ackb
, alen
, &ec
);
619 } /* else ignore unknown options */
629 * Handle initial connection protocol.
632 tftp(struct tftphdr
*tp
, int size
)
636 char *filename
, *mode
;
637 int first
, ecode
, alen
, etftp
=0, r
;
642 filename
= cp
= tp
->th_stuff
;
644 while (cp
< buf
+ size
) {
658 for (cp
= mode
; *cp
; cp
++)
661 for (pf
= formats
; pf
->f_mode
; pf
++)
662 if (strcmp(pf
->f_mode
, mode
) == 0)
664 if (pf
->f_mode
== 0) {
669 * cp currently points to the NUL byte following the mode.
671 * If we have some valid options, then let's assume that we're
672 * now dealing with an extended tftp session. Note that if we
673 * don't get any options, then we *must* assume that we do not
674 * have an extended tftp session. If we get options, we fill
675 * in the ack buf to acknowledge them. If we skip that, then
676 * the client *must* assume that we are not using an extended
679 size
-= (++cp
- (char *) tp
);
680 if (size
> 0 && *cp
) {
681 alen
= 2; /* Skip over opcode */
682 r
= get_options(tp
, cp
, size
, oackbuf
, &alen
, &ecode
);
690 ecode
= (*pf
->f_validate
)(&filename
, tp
->th_opcode
);
692 syslog(LOG_INFO
, "%s: %s request for %s: %s",
693 verifyhost((struct sockaddr
*)&from
),
694 tp
->th_opcode
== WRQ
? "write" : "read",
695 filename
, errtomsg(ecode
));
699 * Avoid storms of naks to a RRQ broadcast for a relative
700 * bootfile pathname from a diskless Sun.
702 if (suppress_naks
&& *filename
!= '/' && ecode
== ENOTFOUND
)
709 struct tftphdr
*oack_h
;
711 if (tftp_opt_tsize
) {
714 strcpy(oackbuf
+ alen
, "tsize");
716 l
= sprintf(oackbuf
+ alen
, "%u", tftp_tsize
);
719 oack_h
= (struct tftphdr
*) oackbuf
;
720 oack_h
->th_opcode
= htons(OACK
);
723 if (tp
->th_opcode
== WRQ
)
724 (*pf
->f_recv
)(pf
, etftp
, alen
);
726 (*pf
->f_send
)(pf
, etftp
, alen
);
734 * Validate file access. Since we
735 * have no uid or gid, for now require
736 * file to exist and be publicly
738 * If we were invoked with arguments
739 * from inetd then the file must also be
740 * in one of the given directory prefixes.
743 validate_access(char **filep
, int mode
)
746 struct dirlist
*dirp
;
747 static char pathname
[MAXPATHLEN
];
751 static char resolved_path
[PATH_MAX
+1];
752 bzero(resolved_path
,PATH_MAX
+1);
756 if (realpath(*filep
, resolved_path
)==NULL
) {
759 filename
= resolved_path
;
765 * Prevent tricksters from getting around the directory restrictions
767 if (strstr(filename
, "/../"))
770 if (*filename
== '/') {
772 * Allow the request if it's in one of the approved locations.
773 * Special case: check the null prefix ("/") by looking
774 * for length = 1 and relying on the arg. processing that
777 for (dirp
= dirs
; dirp
->name
!= NULL
; dirp
++) {
778 if (dirp
->len
== 1 ||
779 (!strncmp(filename
, dirp
->name
, dirp
->len
) &&
780 filename
[dirp
->len
] == '/'))
783 /* If directory list is empty, allow access to any file */
784 if (dirp
->name
== NULL
&& dirp
!= dirs
)
786 if (stat(filename
, &stbuf
) < 0)
787 return (errno
== ENOENT
? ENOTFOUND
: EACCESS
);
788 if (!S_ISREG(stbuf
.st_mode
))
791 if ((stbuf
.st_mode
& S_IROTH
) == 0)
794 if ((stbuf
.st_mode
& S_IWOTH
) == 0)
799 * Relative file name: search the approved locations for it.
802 if (!strncmp(filename
, "../", 3))
806 * Find the first file that exists in any of the directories,
807 * check access on it.
809 if (dirs
[0].name
!= NULL
) {
810 for (dirp
= dirs
; dirp
->name
!= NULL
; dirp
++) {
811 snprintf(pathname
, sizeof pathname
, "%s/%s",
812 dirp
->name
, filename
);
813 if (stat(pathname
, &stbuf
) == 0 &&
814 (stbuf
.st_mode
& S_IFMT
) == S_IFREG
) {
818 if (dirp
->name
== NULL
)
820 if (mode
== RRQ
&& !(stbuf
.st_mode
& S_IROTH
))
822 if (mode
== WRQ
&& !(stbuf
.st_mode
& S_IWOTH
))
828 * If there's no directory list, take our cue from the
829 * absolute file request check above (*filename == '/'),
830 * and allow access to anything.
832 if (stat(filename
, &stbuf
) < 0)
833 return (errno
== ENOENT
? ENOTFOUND
: EACCESS
);
834 if (!S_ISREG(stbuf
.st_mode
))
837 if ((stbuf
.st_mode
& S_IROTH
) == 0)
840 if ((stbuf
.st_mode
& S_IWOTH
) == 0)
847 if (tftp_opt_tsize
&& mode
== RRQ
)
848 tftp_tsize
= (unsigned long) stbuf
.st_size
;
850 fd
= open(filename
, mode
== RRQ
? O_RDONLY
: O_WRONLY
| O_TRUNC
);
852 return (errno
+ 100);
853 file
= fdopen(fd
, (mode
== RRQ
)? "r":"w");
856 return (errno
+ 100);
869 if (timeout
>= maxtimeout
)
871 longjmp(timeoutbuf
, 1);
893 (void)snprintf(buf
, sizeof(buf
), "*code %d*", code
);
899 * Send the requested file.
902 sendfile(struct formats
*pf
, int etftp
, int acklength
)
904 volatile unsigned int block
;
906 struct tftphdr
*ap
; /* ack packet */
909 signal(SIGALRM
, timer
);
910 ap
= (struct tftphdr
*)ackbuf
;
912 dp
= (struct tftphdr
*)oackbuf
;
913 size
= acklength
- 4;
923 size
= readit(file
, &dp
, tftp_blksize
, pf
->f_convert
);
928 dp
->th_opcode
= htons((u_short
)DATA
);
929 dp
->th_block
= htons((u_short
)block
);
932 (void)setjmp(timeoutbuf
);
936 syslog(LOG_DEBUG
, "Send DATA %u", block
);
937 if ((n
= send(peer
, dp
, size
+ 4, 0)) != size
+ 4) {
938 syslog(LOG_ERR
, "tftpd: write: %m");
942 read_ahead(file
, tftp_blksize
, pf
->f_convert
);
944 alarm(rexmtval
); /* read the ack */
945 n
= recv(peer
, ackbuf
, tftp_blksize
, 0);
948 syslog(LOG_ERR
, "tftpd: read: %m");
951 ap
->th_opcode
= ntohs((u_short
)ap
->th_opcode
);
952 ap
->th_block
= ntohs((u_short
)ap
->th_block
);
953 switch (ap
->th_opcode
) {
958 if (ap
->th_block
== 0) {
964 if (ap
->th_block
== block
)
967 syslog(LOG_DEBUG
, "Resync ACK %u != %u",
968 (unsigned int)ap
->th_block
, block
);
969 /* Re-synchronize with the other side */
970 (void) synchnet(peer
, tftp_blksize
);
971 if (ap
->th_block
== (block
-1))
974 syslog(LOG_INFO
, "Received %s in sendfile\n",
975 opcode(dp
->th_opcode
));
981 syslog(LOG_DEBUG
, "Received ACK for block %u", block
);
983 } while (size
== tftp_blksize
|| block
== 1);
999 recvfile(struct formats
*pf
, int etftp
, int acklength
)
1001 volatile unsigned int block
;
1003 struct tftphdr
*ap
; /* ack buffer */
1006 signal(SIGALRM
, timer
);
1008 ap
= (struct tftphdr
*)oackbuf
;
1013 ap
= (struct tftphdr
*)ackbuf
;
1014 ap
->th_opcode
= htons((u_short
)ACK
);
1015 ap
->th_block
= htons((u_short
)block
);
1019 syslog(LOG_DEBUG
, "Sending ACK for block %u\n", block
);
1021 (void) setjmp(timeoutbuf
);
1023 if (send(peer
, ap
, acklength
, 0) != acklength
) {
1024 syslog(LOG_ERR
, "tftpd: write: %m");
1027 write_behind(file
, pf
->f_convert
);
1030 n
= recv(peer
, dp
, tftp_blksize
+ 4, 0);
1032 if (n
< 0) { /* really? */
1033 syslog(LOG_ERR
, "tftpd: read: %m");
1037 dp
->th_opcode
= ntohs((u_short
)dp
->th_opcode
);
1038 dp
->th_block
= ntohs((u_short
)dp
->th_block
);
1040 syslog(LOG_DEBUG
, "Received %s for block %u",
1041 opcode(dp
->th_opcode
),
1042 (unsigned int)dp
->th_block
);
1044 switch (dp
->th_opcode
) {
1048 if (dp
->th_block
== block
)
1049 goto done
; /* normal */
1051 syslog(LOG_DEBUG
, "Resync %u != %u",
1052 (unsigned int)dp
->th_block
, block
);
1053 /* Re-synchronize with the other side */
1054 (void) synchnet(peer
, tftp_blksize
);
1055 if (dp
->th_block
== (block
-1))
1056 goto send_ack
; /* rexmit */
1059 syslog(LOG_INFO
, "Received %s in recvfile\n",
1060 opcode(dp
->th_opcode
));
1066 syslog(LOG_DEBUG
, "Got block %u", block
);
1067 /* size = write(file, dp->th_data, n - 4); */
1068 size
= writeit(file
, &dp
, n
- 4, pf
->f_convert
);
1069 if (size
!= (n
-4)) { /* ahem */
1070 if (size
< 0) nak(errno
+ 100);
1074 } while (size
== tftp_blksize
);
1075 write_behind(file
, pf
->f_convert
);
1076 (void) fclose(file
); /* close data file */
1078 ap
->th_opcode
= htons((u_short
)ACK
); /* send the "final" ack */
1079 ap
->th_block
= htons((u_short
)(block
));
1081 syslog(LOG_DEBUG
, "Send final ACK %u", block
);
1082 (void) send(peer
, ackbuf
, 4, 0);
1084 signal(SIGALRM
, justquit
); /* just quit on timeout */
1086 n
= recv(peer
, buf
, sizeof (buf
), 0); /* normally times out and quits */
1088 if (n
>= 4 && /* if read some data */
1089 dp
->th_opcode
== DATA
&& /* and got a data block */
1090 block
== dp
->th_block
) { /* then my last ack was lost */
1091 (void) send(peer
, ackbuf
, 4, 0); /* resend final ack */
1097 const struct errmsg
{
1101 { EUNDEF
, "Undefined error code" },
1102 { ENOTFOUND
, "File not found" },
1103 { EACCESS
, "Access violation" },
1104 { ENOSPACE
, "Disk full or allocation exceeded" },
1105 { EBADOP
, "Illegal TFTP operation" },
1106 { EBADID
, "Unknown transfer ID" },
1107 { EEXISTS
, "File already exists" },
1108 { ENOUSER
, "No such user" },
1109 { EOPTNEG
, "Option negotiation failed" },
1116 static char ebuf
[20];
1117 const struct errmsg
*pe
;
1121 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
1122 if (pe
->e_code
== error
)
1124 snprintf(ebuf
, sizeof(ebuf
), "error %d", error
);
1129 * Send a nak packet (error message).
1130 * Error code passed in is one of the
1131 * standard TFTP codes, or a UNIX errno
1137 const struct errmsg
*pe
;
1142 tp
= (struct tftphdr
*)buf
;
1143 tp
->th_opcode
= htons((u_short
)ERROR
);
1144 msglen
= sizeof(buf
) - (&tp
->th_msg
[0] - buf
);
1145 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
1146 if (pe
->e_code
== error
)
1148 if (pe
->e_code
< 0) {
1149 tp
->th_code
= EUNDEF
; /* set 'undef' errorcode */
1150 strlcpy(tp
->th_msg
, strerror(error
- 100), msglen
);
1152 tp
->th_code
= htons((u_short
)error
);
1153 strlcpy(tp
->th_msg
, pe
->e_msg
, msglen
);
1156 syslog(LOG_DEBUG
, "Send NACK %s", tp
->th_msg
);
1157 length
= strlen(tp
->th_msg
);
1158 msglen
= &tp
->th_msg
[length
+ 1] - buf
;
1159 if (send(peer
, buf
, msglen
, 0) != msglen
)
1160 syslog(LOG_ERR
, "nak: %m");
1164 verifyhost(struct sockaddr
*fromp
)
1166 static char hbuf
[MAXHOSTNAMELEN
];
1168 if (getnameinfo(fromp
, fromp
->sa_len
, hbuf
, sizeof(hbuf
), NULL
, 0, 0))
1169 strlcpy(hbuf
, "?", sizeof(hbuf
));