]>
git.saurik.com Git - apple/network_cmds.git/blob - tftpd.tproj/tftpd.c
27a616e6d305bb52b007f74d20fe577b5fe60ef6
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $NetBSD: tftpd.c,v 1.28 2004/05/05 20:15:45 kleink Exp $ */
25 * Copyright (c) 1983, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 #include <sys/cdefs.h>
55 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
56 The Regents of the University of California. All rights reserved.\n");
58 static char sccsid
[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93";
60 __RCSID("$NetBSD: tftpd.c,v 1.28 2004/05/05 20:15:45 kleink Exp $");
65 * Trivial file transfer protocol server.
67 * This version includes many modifications by Jim Guyton
71 #include <sys/param.h>
72 #include <sys/ioctl.h>
74 #include <sys/socket.h>
76 #include <netinet/in.h>
78 #include <arpa/inet.h>
97 #define DEFAULTUSER "nobody"
102 int rexmtval
= TIMEOUT
;
103 int maxtimeout
= 5*TIMEOUT
;
105 char buf
[MAXPKTSIZE
];
106 char ackbuf
[PKTSIZE
];
107 char oackbuf
[PKTSIZE
];
108 struct sockaddr_storage from
;
112 int tftp_opt_tsize
= 0;
113 int tftp_blksize
= SEGSIZE
;
117 * Null-terminated directory prefix list for absolute pathname requests and
118 * search list for relative pathname requests.
120 * MAXDIRS should be at least as large as the number of arguments that
121 * inetd allows (currently 20).
124 static struct dirlist
{
128 static int suppress_naks
;
130 static int insecure
=0;
132 static char *securedir
;
136 static const char *errtomsg(int);
137 static void nak(int);
138 static void tftp(struct tftphdr
*, int);
139 static void usage(void);
140 static char *verifyhost(struct sockaddr
*);
142 int main(int, char **);
143 void recvfile(struct formats
*, int, int);
144 void sendfile(struct formats
*, int, int);
146 static const char *opcode(int);
147 int validate_access(char **, int);
151 int (*f_validate
)(char **, int);
152 void (*f_send
)(struct formats
*, int, int);
153 void (*f_recv
)(struct formats
*, int, int);
156 { "netascii", validate_access
, sendfile
, recvfile
, 1 },
157 { "octet", validate_access
, sendfile
, recvfile
, 0 },
166 "Usage: %s [-diln] [-u user] [-g group] [-s directory] [directory ...]",
172 main(int argc
, char *argv
[])
174 struct sockaddr_storage me
;
175 struct passwd
*pwent
;
178 char *tgtuser
, *tgtgroup
, *ep
;
181 uid_t curuid
, tgtuid
;
182 gid_t curgid
, tgtgid
;
188 openlog("tftpd", LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
189 tgtuser
= DEFAULTUSER
;
194 while ((ch
= getopt(argc
, argv
, "dg:ilns:u:")) != -1)
231 struct dirlist
*dirp
;
233 /* Get list of directory prefixes. Skip relative pathnames. */
234 for (dirp
= dirs
; optind
< argc
&& dirp
< &dirs
[MAXDIRS
];
236 if (argv
[optind
][0] == '/') {
237 dirp
->name
= argv
[optind
];
238 dirp
->len
= strlen(dirp
->name
);
244 if (*tgtuser
== '\0' || (tgtgroup
!= NULL
&& *tgtgroup
== '\0'))
247 nid
= (strtol(tgtuser
, &ep
, 10));
250 syslog(LOG_ERR
, "uid %ld is too large", nid
);
253 pwent
= getpwuid((uid_t
)nid
);
255 pwent
= getpwnam(tgtuser
);
257 syslog(LOG_ERR
, "unknown user `%s'", tgtuser
);
260 tgtuid
= pwent
->pw_uid
;
261 tgtgid
= pwent
->pw_gid
;
263 if (tgtgroup
!= NULL
) {
264 nid
= (strtol(tgtgroup
, &ep
, 10));
267 syslog(LOG_ERR
, "gid %ld is too large", nid
);
270 grent
= getgrgid((gid_t
)nid
);
272 grent
= getgrnam(tgtgroup
);
274 tgtgid
= grent
->gr_gid
;
276 syslog(LOG_ERR
, "unknown group `%s'", tgtgroup
);
282 if (chdir(securedir
) < 0) {
283 syslog(LOG_ERR
, "chdir %s: %m", securedir
);
287 syslog(LOG_ERR
, "chroot: %m");
293 syslog(LOG_DEBUG
, "running as user `%s' (%d), group `%s' (%d)",
294 tgtuser
, tgtuid
, tgtgroup
? tgtgroup
: "(unspecified)",
296 if (curgid
!= tgtgid
) {
297 if (setgid(tgtgid
)) {
298 syslog(LOG_ERR
, "setgid to %d: %m", (int)tgtgid
);
301 if (setgroups(0, NULL
)) {
302 syslog(LOG_ERR
, "setgroups: %m");
307 if (curuid
!= tgtuid
) {
308 if (setuid(tgtuid
)) {
309 syslog(LOG_ERR
, "setuid to %d: %m", (int)tgtuid
);
315 if (ioctl(fd
, FIONBIO
, &on
) < 0) {
316 syslog(LOG_ERR
, "ioctl(FIONBIO): %m");
319 fromlen
= sizeof (from
);
320 n
= recvfrom(fd
, buf
, sizeof (buf
), 0,
321 (struct sockaddr
*)&from
, &fromlen
);
323 syslog(LOG_ERR
, "recvfrom: %m");
327 * Now that we have read the message out of the UDP
328 * socket, we fork and exit. Thus, inetd will go back
329 * to listening to the tftp port, and the next request
330 * to come in will start up a new instance of tftpd.
332 * We do this so that inetd can run tftpd in "wait" mode.
333 * The problem with tftpd running in "nowait" mode is that
334 * inetd may get one or more successful "selects" on the
335 * tftp port before we do our receive, so more than one
336 * instance of tftpd may be started up. Worse, if tftpd
337 * break before doing the above "recvfrom", inetd would
338 * spawn endless instances, clogging the system.
344 for (i
= 1; i
< 20; i
++) {
349 * flush out to most recently sent request.
351 * This may drop some request, but those
352 * will be resent by the clients when
353 * they timeout. The positive effect of
354 * this flush is to (try to) prevent more
355 * than one tftpd being started up to service
356 * a single request from a single client.
359 i
= recvfrom(fd
, buf
, sizeof (buf
), 0,
360 (struct sockaddr
*)&from
, &j
);
370 syslog(LOG_ERR
, "fork: %m");
372 } else if (pid
!= 0) {
378 * remember what address this was sent to, so we can respond on the
382 if (getsockname(fd
, (struct sockaddr
*)&me
, &len
) == 0) {
383 switch (me
.ss_family
) {
385 ((struct sockaddr_in
*)&me
)->sin_port
= 0;
388 ((struct sockaddr_in6
*)&me
)->sin6_port
= 0;
395 memset(&me
, 0, sizeof(me
));
396 me
.ss_family
= from
.ss_family
;
397 me
.ss_len
= from
.ss_len
;
403 peer
= socket(from
.ss_family
, SOCK_DGRAM
, 0);
405 syslog(LOG_ERR
, "socket: %m");
408 if (bind(peer
, (struct sockaddr
*)&me
, me
.ss_len
) < 0) {
409 syslog(LOG_ERR
, "bind: %m");
412 if (connect(peer
, (struct sockaddr
*)&from
, from
.ss_len
) < 0) {
413 syslog(LOG_ERR
, "connect: %m");
416 soopt
= 65536; /* larger than we'll ever need */
417 if (setsockopt(peer
, SOL_SOCKET
, SO_SNDBUF
, (void *) &soopt
, sizeof(soopt
)) < 0) {
418 syslog(LOG_ERR
, "set SNDBUF: %m");
421 if (setsockopt(peer
, SOL_SOCKET
, SO_RCVBUF
, (void *) &soopt
, sizeof(soopt
)) < 0) {
422 syslog(LOG_ERR
, "set RCVBUF: %m");
426 tp
= (struct tftphdr
*)buf
;
427 tp
->th_opcode
= ntohs(tp
->th_opcode
);
428 if (tp
->th_opcode
== RRQ
|| tp
->th_opcode
== WRQ
)
434 blk_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
442 * On these failures, we could just ignore the blocksize option.
443 * Perhaps that should be a command-line option.
446 bsize
= strtoul(val
, &endp
, 10);
447 if ((bsize
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
448 syslog(LOG_NOTICE
, "%s: %s request for %s: "
449 "illegal value %s for blksize option",
450 verifyhost((struct sockaddr
*)&from
),
451 tp
->th_opcode
== WRQ
? "write" : "read",
455 if (bsize
< 8 || bsize
> 65464) {
456 syslog(LOG_NOTICE
, "%s: %s request for %s: "
457 "out of range value %s for blksize option",
458 verifyhost((struct sockaddr
*)&from
),
459 tp
->th_opcode
== WRQ
? "write" : "read",
464 tftp_blksize
= bsize
;
465 strcpy(ack
+ *ackl
, "blksize");
467 l
= sprintf(ack
+ *ackl
, "%lu", bsize
);
474 timeout_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
482 tout
= strtoul(val
, &endp
, 10);
483 if ((tout
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
484 syslog(LOG_NOTICE
, "%s: %s request for %s: "
485 "illegal value %s for timeout option",
486 verifyhost((struct sockaddr
*)&from
),
487 tp
->th_opcode
== WRQ
? "write" : "read",
491 if (tout
< 1 || tout
> 255) {
492 syslog(LOG_NOTICE
, "%s: %s request for %s: "
493 "out of range value %s for timeout option",
494 verifyhost((struct sockaddr
*)&from
),
495 tp
->th_opcode
== WRQ
? "write" : "read",
501 strcpy(ack
+ *ackl
, "timeout");
503 l
= sprintf(ack
+ *ackl
, "%lu", tout
);
507 * Arbitrarily pick a maximum timeout on a request to 3
508 * retransmissions if the interval timeout is more than
509 * one minute. Longest possible timeout is therefore
510 * 3 * 255 - 1, or 764 seconds.
513 maxtimeout
= rexmtval
* 3;
515 maxtimeout
= rexmtval
* 5;
522 tsize_handler(struct tftphdr
*tp
, char *opt
, char *val
, char *ack
,
529 * Maximum file even with extended tftp is 65535 blocks of
530 * length 65464, or 4290183240 octets (4784056 less than 2^32).
531 * unsigned long is at least 32 bits on all NetBSD archs.
535 fsize
= strtoul(val
, &endp
, 10);
536 if ((fsize
== ULONG_MAX
&& errno
== ERANGE
) || *endp
) {
537 syslog(LOG_NOTICE
, "%s: %s request for %s: "
538 "illegal value %s for tsize option",
539 verifyhost((struct sockaddr
*)&from
),
540 tp
->th_opcode
== WRQ
? "write" : "read",
544 if (fsize
> (unsigned long) 65535 * 65464) {
545 syslog(LOG_NOTICE
, "%s: %s request for %s: "
546 "out of range value %s for tsize option",
547 verifyhost((struct sockaddr
*)&from
),
548 tp
->th_opcode
== WRQ
? "write" : "read",
556 * We will report this later -- either replying with the fsize (WRQ)
557 * or replying with the actual filesize (RRQ).
563 struct tftp_options
{
565 int (*o_handler
)(struct tftphdr
*, char *, char *, char *,
568 { "blksize", blk_handler
},
569 { "timeout", timeout_handler
},
570 { "tsize", tsize_handler
},
575 * Get options for an extended tftp session. Stuff the ones we
576 * recognize in oackbuf.
579 get_options(struct tftphdr
*tp
, char *cp
, int size
, char *ackb
,
582 struct tftp_options
*op
;
583 char *option
, *value
, *endp
;
589 while (*cp
&& cp
< endp
) {
594 /* if we have garbage at the end, just ignore it */
597 cp
++; /* skip over NUL */
599 while (*cp
&& cp
< endp
) {
603 /* if we have garbage at the end, just ignore it */
607 for (op
= options
; op
->o_name
; op
++) {
608 if (strcmp(op
->o_name
, option
) == 0)
612 r
= op
->o_handler(tp
, option
, value
, ackb
, alen
, &ec
);
618 } /* else ignore unknown options */
628 * Handle initial connection protocol.
631 tftp(struct tftphdr
*tp
, int size
)
635 char *filename
, *mode
;
636 int first
, ecode
, alen
, etftp
=0, r
;
641 filename
= cp
= tp
->th_stuff
;
643 while (cp
< buf
+ size
) {
657 for (cp
= mode
; *cp
; cp
++)
660 for (pf
= formats
; pf
->f_mode
; pf
++)
661 if (strcmp(pf
->f_mode
, mode
) == 0)
663 if (pf
->f_mode
== 0) {
668 * cp currently points to the NUL byte following the mode.
670 * If we have some valid options, then let's assume that we're
671 * now dealing with an extended tftp session. Note that if we
672 * don't get any options, then we *must* assume that we do not
673 * have an extended tftp session. If we get options, we fill
674 * in the ack buf to acknowledge them. If we skip that, then
675 * the client *must* assume that we are not using an extended
678 size
-= (++cp
- (char *) tp
);
679 if (size
> 0 && *cp
) {
680 alen
= 2; /* Skip over opcode */
681 r
= get_options(tp
, cp
, size
, oackbuf
, &alen
, &ecode
);
689 ecode
= (*pf
->f_validate
)(&filename
, tp
->th_opcode
);
691 syslog(LOG_INFO
, "%s: %s request for %s: %s",
692 verifyhost((struct sockaddr
*)&from
),
693 tp
->th_opcode
== WRQ
? "write" : "read",
694 filename
, errtomsg(ecode
));
698 * Avoid storms of naks to a RRQ broadcast for a relative
699 * bootfile pathname from a diskless Sun.
701 if (suppress_naks
&& *filename
!= '/' && ecode
== ENOTFOUND
)
708 struct tftphdr
*oack_h
;
710 if (tftp_opt_tsize
) {
713 strcpy(oackbuf
+ alen
, "tsize");
715 l
= sprintf(oackbuf
+ alen
, "%u", tftp_tsize
);
718 oack_h
= (struct tftphdr
*) oackbuf
;
719 oack_h
->th_opcode
= htons(OACK
);
722 if (tp
->th_opcode
== WRQ
)
723 (*pf
->f_recv
)(pf
, etftp
, alen
);
725 (*pf
->f_send
)(pf
, etftp
, alen
);
733 * Validate file access. Since we
734 * have no uid or gid, for now require
735 * file to exist and be publicly
737 * If we were invoked with arguments
738 * from inetd then the file must also be
739 * in one of the given directory prefixes.
742 validate_access(char **filep
, int mode
)
745 struct dirlist
*dirp
;
746 static char pathname
[MAXPATHLEN
];
750 static char resolved_path
[PATH_MAX
+1];
751 bzero(resolved_path
,PATH_MAX
+1);
755 if (realpath(*filep
, resolved_path
)==NULL
) {
758 filename
= resolved_path
;
764 * Prevent tricksters from getting around the directory restrictions
766 if (strstr(filename
, "/../"))
769 if (*filename
== '/') {
771 * Allow the request if it's in one of the approved locations.
772 * Special case: check the null prefix ("/") by looking
773 * for length = 1 and relying on the arg. processing that
776 for (dirp
= dirs
; dirp
->name
!= NULL
; dirp
++) {
777 if (dirp
->len
== 1 ||
778 (!strncmp(filename
, dirp
->name
, dirp
->len
) &&
779 filename
[dirp
->len
] == '/'))
782 /* If directory list is empty, allow access to any file */
783 if (dirp
->name
== NULL
&& dirp
!= dirs
)
785 if (stat(filename
, &stbuf
) < 0)
786 return (errno
== ENOENT
? ENOTFOUND
: EACCESS
);
787 if (!S_ISREG(stbuf
.st_mode
))
790 if ((stbuf
.st_mode
& S_IROTH
) == 0)
793 if ((stbuf
.st_mode
& S_IWOTH
) == 0)
798 * Relative file name: search the approved locations for it.
801 if (!strncmp(filename
, "../", 3))
805 * Find the first file that exists in any of the directories,
806 * check access on it.
808 if (dirs
[0].name
!= NULL
) {
809 for (dirp
= dirs
; dirp
->name
!= NULL
; dirp
++) {
810 snprintf(pathname
, sizeof pathname
, "%s/%s",
811 dirp
->name
, filename
);
812 if (stat(pathname
, &stbuf
) == 0 &&
813 (stbuf
.st_mode
& S_IFMT
) == S_IFREG
) {
817 if (dirp
->name
== NULL
)
819 if (mode
== RRQ
&& !(stbuf
.st_mode
& S_IROTH
))
821 if (mode
== WRQ
&& !(stbuf
.st_mode
& S_IWOTH
))
827 * If there's no directory list, take our cue from the
828 * absolute file request check above (*filename == '/'),
829 * and allow access to anything.
831 if (stat(filename
, &stbuf
) < 0)
832 return (errno
== ENOENT
? ENOTFOUND
: EACCESS
);
833 if (!S_ISREG(stbuf
.st_mode
))
836 if ((stbuf
.st_mode
& S_IROTH
) == 0)
839 if ((stbuf
.st_mode
& S_IWOTH
) == 0)
846 if (tftp_opt_tsize
&& mode
== RRQ
)
847 tftp_tsize
= (unsigned long) stbuf
.st_size
;
849 fd
= open(filename
, mode
== RRQ
? O_RDONLY
: O_WRONLY
| O_TRUNC
);
851 return (errno
+ 100);
852 file
= fdopen(fd
, (mode
== RRQ
)? "r":"w");
855 return (errno
+ 100);
868 if (timeout
>= maxtimeout
)
870 longjmp(timeoutbuf
, 1);
892 (void)snprintf(buf
, sizeof(buf
), "*code %d*", code
);
898 * Send the requested file.
901 sendfile(struct formats
*pf
, int etftp
, int acklength
)
903 volatile unsigned int block
;
905 struct tftphdr
*ap
; /* ack packet */
908 signal(SIGALRM
, timer
);
909 ap
= (struct tftphdr
*)ackbuf
;
911 dp
= (struct tftphdr
*)oackbuf
;
912 size
= acklength
- 4;
922 size
= readit(file
, &dp
, tftp_blksize
, pf
->f_convert
);
927 dp
->th_opcode
= htons((u_short
)DATA
);
928 dp
->th_block
= htons((u_short
)block
);
931 (void)setjmp(timeoutbuf
);
935 syslog(LOG_DEBUG
, "Send DATA %u", block
);
936 if ((n
= send(peer
, dp
, size
+ 4, 0)) != size
+ 4) {
937 syslog(LOG_ERR
, "tftpd: write: %m");
941 read_ahead(file
, tftp_blksize
, pf
->f_convert
);
943 alarm(rexmtval
); /* read the ack */
944 n
= recv(peer
, ackbuf
, tftp_blksize
, 0);
947 syslog(LOG_ERR
, "tftpd: read: %m");
950 ap
->th_opcode
= ntohs((u_short
)ap
->th_opcode
);
951 ap
->th_block
= ntohs((u_short
)ap
->th_block
);
952 switch (ap
->th_opcode
) {
957 if (ap
->th_block
== 0) {
963 if (ap
->th_block
== block
)
966 syslog(LOG_DEBUG
, "Resync ACK %u != %u",
967 (unsigned int)ap
->th_block
, block
);
968 /* Re-synchronize with the other side */
969 (void) synchnet(peer
, tftp_blksize
);
970 if (ap
->th_block
== (block
-1))
973 syslog(LOG_INFO
, "Received %s in sendfile\n",
974 opcode(dp
->th_opcode
));
980 syslog(LOG_DEBUG
, "Received ACK for block %u", block
);
982 } while (size
== tftp_blksize
|| block
== 1);
998 recvfile(struct formats
*pf
, int etftp
, int acklength
)
1000 volatile unsigned int block
;
1002 struct tftphdr
*ap
; /* ack buffer */
1005 signal(SIGALRM
, timer
);
1007 ap
= (struct tftphdr
*)oackbuf
;
1012 ap
= (struct tftphdr
*)ackbuf
;
1013 ap
->th_opcode
= htons((u_short
)ACK
);
1014 ap
->th_block
= htons((u_short
)block
);
1018 syslog(LOG_DEBUG
, "Sending ACK for block %u\n", block
);
1020 (void) setjmp(timeoutbuf
);
1022 if (send(peer
, ap
, acklength
, 0) != acklength
) {
1023 syslog(LOG_ERR
, "tftpd: write: %m");
1026 write_behind(file
, pf
->f_convert
);
1029 n
= recv(peer
, dp
, tftp_blksize
+ 4, 0);
1031 if (n
< 0) { /* really? */
1032 syslog(LOG_ERR
, "tftpd: read: %m");
1036 dp
->th_opcode
= ntohs((u_short
)dp
->th_opcode
);
1037 dp
->th_block
= ntohs((u_short
)dp
->th_block
);
1039 syslog(LOG_DEBUG
, "Received %s for block %u",
1040 opcode(dp
->th_opcode
),
1041 (unsigned int)dp
->th_block
);
1043 switch (dp
->th_opcode
) {
1047 if (dp
->th_block
== block
)
1048 goto done
; /* normal */
1050 syslog(LOG_DEBUG
, "Resync %u != %u",
1051 (unsigned int)dp
->th_block
, block
);
1052 /* Re-synchronize with the other side */
1053 (void) synchnet(peer
, tftp_blksize
);
1054 if (dp
->th_block
== (block
-1))
1055 goto send_ack
; /* rexmit */
1058 syslog(LOG_INFO
, "Received %s in recvfile\n",
1059 opcode(dp
->th_opcode
));
1065 syslog(LOG_DEBUG
, "Got block %u", block
);
1066 /* size = write(file, dp->th_data, n - 4); */
1067 size
= writeit(file
, &dp
, n
- 4, pf
->f_convert
);
1068 if (size
!= (n
-4)) { /* ahem */
1069 if (size
< 0) nak(errno
+ 100);
1073 } while (size
== tftp_blksize
);
1074 write_behind(file
, pf
->f_convert
);
1075 (void) fclose(file
); /* close data file */
1077 ap
->th_opcode
= htons((u_short
)ACK
); /* send the "final" ack */
1078 ap
->th_block
= htons((u_short
)(block
));
1080 syslog(LOG_DEBUG
, "Send final ACK %u", block
);
1081 (void) send(peer
, ackbuf
, 4, 0);
1083 signal(SIGALRM
, justquit
); /* just quit on timeout */
1085 n
= recv(peer
, buf
, sizeof (buf
), 0); /* normally times out and quits */
1087 if (n
>= 4 && /* if read some data */
1088 dp
->th_opcode
== DATA
&& /* and got a data block */
1089 block
== dp
->th_block
) { /* then my last ack was lost */
1090 (void) send(peer
, ackbuf
, 4, 0); /* resend final ack */
1096 const struct errmsg
{
1100 { EUNDEF
, "Undefined error code" },
1101 { ENOTFOUND
, "File not found" },
1102 { EACCESS
, "Access violation" },
1103 { ENOSPACE
, "Disk full or allocation exceeded" },
1104 { EBADOP
, "Illegal TFTP operation" },
1105 { EBADID
, "Unknown transfer ID" },
1106 { EEXISTS
, "File already exists" },
1107 { ENOUSER
, "No such user" },
1108 { EOPTNEG
, "Option negotiation failed" },
1115 static char ebuf
[20];
1116 const struct errmsg
*pe
;
1120 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
1121 if (pe
->e_code
== error
)
1123 snprintf(ebuf
, sizeof(ebuf
), "error %d", error
);
1128 * Send a nak packet (error message).
1129 * Error code passed in is one of the
1130 * standard TFTP codes, or a UNIX errno
1136 const struct errmsg
*pe
;
1141 tp
= (struct tftphdr
*)buf
;
1142 tp
->th_opcode
= htons((u_short
)ERROR
);
1143 msglen
= sizeof(buf
) - (&tp
->th_msg
[0] - buf
);
1144 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
1145 if (pe
->e_code
== error
)
1147 if (pe
->e_code
< 0) {
1148 tp
->th_code
= EUNDEF
; /* set 'undef' errorcode */
1149 strlcpy(tp
->th_msg
, strerror(error
- 100), msglen
);
1151 tp
->th_code
= htons((u_short
)error
);
1152 strlcpy(tp
->th_msg
, pe
->e_msg
, msglen
);
1155 syslog(LOG_DEBUG
, "Send NACK %s", tp
->th_msg
);
1156 length
= strlen(tp
->th_msg
);
1157 msglen
= &tp
->th_msg
[length
+ 1] - buf
;
1158 if (send(peer
, buf
, msglen
, 0) != msglen
)
1159 syslog(LOG_ERR
, "nak: %m");
1163 verifyhost(struct sockaddr
*fromp
)
1165 static char hbuf
[MAXHOSTNAMELEN
];
1167 if (getnameinfo(fromp
, fromp
->sa_len
, hbuf
, sizeof(hbuf
), NULL
, 0, 0))
1168 strlcpy(hbuf
, "?", sizeof(hbuf
));