4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
22 * Use is subject to license terms.
23 * Copyright (c) 2016 by Delphix. All rights reserved.
27 * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
28 * All Rights Reserved.
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California.
34 * All Rights Reserved.
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
42 * Trivial file transfer protocol server. A top level process runs in
43 * an infinite loop fielding new TFTP requests. A child process,
44 * communicating via a pipe with the top level process, sends delayed
45 * NAKs for those that we can't handle. A new child process is created
46 * to service each request that we can handle. The top level process
47 * exits after a period of time during which no new requests are
51 #include <sys/types.h>
52 #include <sys/socket.h>
57 #include <netinet/in.h>
59 #include <arpa/inet.h>
70 #include <sys/param.h>
74 #include <priv_utils.h>
75 #include "tftpcommon.h"
81 #define SYSLOG_MSG(message) \
82 (syslog((((errno == ENETUNREACH) || (errno == EHOSTUNREACH) || \
83 (errno == ECONNREFUSED)) ? LOG_WARNING : LOG_ERR), message))
85 static int rexmtval
= TIMEOUT
;
86 static int maxtimeout
= 5*TIMEOUT
;
87 static int securetftp
;
89 static int disable_pnp
;
90 static int standalone
;
91 static uid_t uid_nobody
= UID_NOBODY
;
92 static uid_t gid_nobody
= GID_NOBODY
;
93 static int reqsock
= -1;
94 /* file descriptor of request socket */
95 static socklen_t fromlen
;
96 static socklen_t fromplen
;
97 static struct sockaddr_storage client
;
98 static struct sockaddr_in6
*sin6_ptr
;
99 static struct sockaddr_in
*sin_ptr
;
100 static struct sockaddr_in6
*from6_ptr
;
101 static struct sockaddr_in
*from_ptr
;
105 static tftpbuf ackbuf
;
106 static struct sockaddr_storage from
;
107 static boolean_t tsize_set
;
109 /* pid of child handling delayed replys */
110 static int delay_fd
[2];
111 /* pipe for communicating with child */
113 static char *filename
;
117 char data
[SEGSIZE
+ 4];
126 long timestamp
; /* time request received */
127 int ecode
; /* error code to return */
128 struct sockaddr_storage from
; /* address of client */
131 int blocksize
= SEGSIZE
; /* Number of data bytes in a DATA packet */
134 * Default directory for unqualified names
135 * Used by TFTP boot procedures
137 static char *homedir
= "/tftpboot";
141 int (*f_validate
)(int);
142 void (*f_send
)(struct formats
*, int);
143 void (*f_recv
)(struct formats
*, int);
147 static void delayed_responder(void);
148 static void tftp(struct tftphdr
*, int);
149 static int validate_filename(int);
150 static void tftpd_sendfile(struct formats
*, int);
151 static void tftpd_recvfile(struct formats
*, int);
152 static void nak(int);
153 static char *blksize_handler(int, char *, int *);
154 static char *timeout_handler(int, char *, int *);
155 static char *tsize_handler(int, char *, int *);
157 static struct formats formats
[] = {
158 { "netascii", validate_filename
, tftpd_sendfile
, tftpd_recvfile
, 1 },
159 { "octet", validate_filename
, tftpd_sendfile
, tftpd_recvfile
, 0 },
165 char *(*opt_handler
)(int, char *, int *);
168 static struct options options
[] = {
169 { "blksize", blksize_handler
},
170 { "timeout", timeout_handler
},
171 { "tsize", tsize_handler
},
175 static char optbuf
[MAX_OPTVAL_LEN
];
177 static sigjmp_buf timeoutbuf
;
180 main(int argc
, char **argv
)
185 struct passwd
*pwd
; /* for "nobody" entry */
186 struct in_addr ipv4addr
;
187 char abuf
[INET6_ADDRSTRLEN
];
190 openlog("tftpd", LOG_PID
, LOG_DAEMON
);
192 pwd
= getpwnam("nobody");
194 uid_nobody
= pwd
->pw_uid
;
195 gid_nobody
= pwd
->pw_gid
;
198 /* Tftp will not start new executables; clear the limit set. */
199 (void) __init_daemon_priv(PU_CLEARLIMITSET
, uid_nobody
, gid_nobody
,
200 PRIV_PROC_CHROOT
, PRIV_NET_PRIVADDR
, NULL
);
202 /* Remove the unneeded basic privileges everywhere. */
203 (void) priv_set(PRIV_OFF
, PRIV_ALLSETS
, PRIV_PROC_EXEC
,
204 PRIV_FILE_LINK_ANY
, PRIV_PROC_INFO
, PRIV_PROC_SESSION
, NULL
);
206 /* Remove the other privileges from E until we need them. */
207 (void) priv_set(PRIV_OFF
, PRIV_EFFECTIVE
, PRIV_PROC_CHROOT
,
208 PRIV_NET_PRIVADDR
, NULL
);
210 while ((c
= getopt(argc
, argv
, "dspST:")) != EOF
)
212 case 'd': /* enable debug */
215 case 's': /* secure daemon */
218 case 'p': /* disable name pnp mapping */
225 rexmtval
= atoi(optarg
);
226 if (rexmtval
<= 0 || rexmtval
> MAX_TIMEOUT
) {
227 (void) fprintf(stderr
,
228 "%s: Invalid retransmission "
229 "timeout value: %s\n", argv
[0], optarg
);
232 maxtimeout
= 5 * rexmtval
;
237 (void) fprintf(stderr
,
238 "usage: %s [-T rexmtval] [-spd] [home-directory]\n",
240 for (; optind
< argc
; optind
++)
241 syslog(LOG_ERR
, "bad argument %s",
247 if (optind
== argc
- 1 && *argv
[optind
] == '/')
248 homedir
= argv
[optind
];
252 if (pipe(delay_fd
) < 0) {
253 syslog(LOG_ERR
, "pipe (main): %m");
257 (void) sigset(SIGCHLD
, SIG_IGN
); /* no zombies please */
262 sin6_ptr
= (struct sockaddr_in6
*)&client
;
263 clientlen
= sizeof (struct sockaddr_in6
);
264 reqsock
= socket(AF_INET6
, SOCK_DGRAM
, 0);
269 (void) memset(&client
, 0, clientlen
);
270 sin6_ptr
->sin6_family
= AF_INET6
;
271 sin6_ptr
->sin6_port
= htons(IPPORT_TFTP
);
273 /* Enable privilege as tftp port is < 1024 */
274 (void) priv_set(PRIV_ON
,
275 PRIV_EFFECTIVE
, PRIV_NET_PRIVADDR
, NULL
);
276 if (bind(reqsock
, (struct sockaddr
*)&client
,
281 (void) priv_set(PRIV_OFF
, PRIV_EFFECTIVE
, PRIV_NET_PRIVADDR
,
285 (void) puts("running in standalone mode...");
287 /* request socket passed on fd 0 by inetd */
293 (void) setsockopt(reqsock
, SOL_SOCKET
, SO_DEBUG
,
294 (char *)&on
, sizeof (on
));
297 (void) chdir(homedir
);
299 (void) priv_set(PRIV_ON
, PRIV_EFFECTIVE
, PRIV_PROC_FORK
, NULL
);
300 if ((child
= fork()) < 0) {
301 syslog(LOG_ERR
, "fork (main): %m");
304 (void) priv_set(PRIV_OFF
, PRIV_EFFECTIVE
, PRIV_PROC_FORK
, NULL
);
310 /* close read side of pipe */
311 (void) close(delay_fd
[0]);
315 * Top level handling of incomming tftp requests. Read a request
316 * and pass it off to be handled. If request is valid, handling
317 * forks off and parent returns to this loop. If no new requests
318 * are received for DALLYSECS, exit and return to inetd.
323 struct timeval dally
;
326 FD_SET(reqsock
, &readfds
);
327 dally
.tv_sec
= DALLYSECS
;
330 n
= select(reqsock
+ 1, &readfds
, NULL
, NULL
, &dally
);
334 syslog(LOG_ERR
, "select: %m");
335 (void) kill(child
, SIGKILL
);
339 /* Select timed out. Its time to die. */
343 (void) kill(child
, SIGKILL
);
347 addrlen
= sizeof (from
);
348 if (getsockname(reqsock
, (struct sockaddr
*)&from
,
350 syslog(LOG_ERR
, "getsockname: %m");
354 switch (from
.ss_family
) {
356 fromlen
= (socklen_t
)sizeof (struct sockaddr_in
);
359 fromlen
= (socklen_t
)sizeof (struct sockaddr_in6
);
363 "Unknown address Family on peer connection %d",
368 n
= recvfrom(reqsock
, &buf
, sizeof (buf
), 0,
369 (struct sockaddr
*)&from
, &fromlen
);
376 syslog(LOG_ERR
, "recvfrom: %m");
377 (void) kill(child
, SIGKILL
);
383 switch (from
.ss_family
) {
386 fromplen
= sizeof (struct sockaddr_in
);
387 sin_ptr
= (struct sockaddr_in
*)&client
;
388 (void) memset(&client
, 0, fromplen
);
389 sin_ptr
->sin_family
= AF_INET
;
393 fromplen
= sizeof (struct sockaddr_in6
);
394 sin6_ptr
= (struct sockaddr_in6
*)&client
;
395 (void) memset(&client
, 0, fromplen
);
396 sin6_ptr
->sin6_family
= AF_INET6
;
400 "Unknown address Family on peer connection");
403 peer
= socket(addrfmly
, SOCK_DGRAM
, 0);
406 perror("socket (main)");
408 syslog(LOG_ERR
, "socket (main): %m");
409 (void) kill(child
, SIGKILL
);
415 (void) setsockopt(peer
, SOL_SOCKET
, SO_DEBUG
,
416 (char *)&on
, sizeof (on
));
419 if (bind(peer
, (struct sockaddr
*)&client
, fromplen
) < 0) {
421 perror("bind (main)");
423 syslog(LOG_ERR
, "bind (main): %m");
424 (void) kill(child
, SIGKILL
);
427 if (standalone
&& debug
) {
428 sin6_ptr
= (struct sockaddr_in6
*)&client
;
429 from6_ptr
= (struct sockaddr_in6
*)&from
;
430 if (IN6_IS_ADDR_V4MAPPED(&from6_ptr
->sin6_addr
)) {
431 IN6_V4MAPPED_TO_INADDR(&from6_ptr
->sin6_addr
,
433 (void) inet_ntop(AF_INET
, &ipv4addr
, abuf
,
436 (void) inet_ntop(AF_INET6
,
437 &from6_ptr
->sin6_addr
, abuf
,
441 if (getsockname(peer
, (struct sockaddr
*)&client
,
443 perror("getsockname (main)");
444 (void) fprintf(stderr
,
445 "request from %s port %d; local port %d\n",
446 abuf
, from6_ptr
->sin6_port
, sin6_ptr
->sin6_port
);
449 tp
->th_opcode
= ntohs((ushort_t
)tp
->th_opcode
);
450 if (tp
->th_opcode
== RRQ
|| tp
->th_opcode
== WRQ
)
462 delayed_responder(void)
464 struct delay_info dinfo
;
467 /* we don't use the descriptors passed in to the parent */
471 (void) close(reqsock
);
473 /* close write side of pipe */
474 (void) close(delay_fd
[1]);
479 if ((n
= read(delay_fd
[0], &dinfo
,
480 sizeof (dinfo
))) != sizeof (dinfo
)) {
485 perror("read from pipe "
486 "(delayed responder)");
488 syslog(LOG_ERR
, "read from pipe: %m");
492 switch (dinfo
.from
.ss_family
) {
495 fromplen
= sizeof (struct sockaddr_in
);
496 sin_ptr
= (struct sockaddr_in
*)&client
;
497 (void) memset(&client
, 0, fromplen
);
498 sin_ptr
->sin_family
= AF_INET
;
502 fromplen
= sizeof (struct sockaddr_in6
);
503 sin6_ptr
= (struct sockaddr_in6
*)&client
;
504 (void) memset(&client
, 0, fromplen
);
505 sin6_ptr
->sin6_family
= AF_INET6
;
508 peer
= socket(addrfmly
, SOCK_DGRAM
, 0);
511 perror("socket (delayed responder)");
513 syslog(LOG_ERR
, "socket (delay): %m");
519 (void) setsockopt(peer
, SOL_SOCKET
, SO_DEBUG
,
520 (char *)&on
, sizeof (on
));
523 if (bind(peer
, (struct sockaddr
*)&client
, fromplen
) < 0) {
525 perror("bind (delayed responder)");
527 syslog(LOG_ERR
, "bind (delay): %m");
530 if (client
.ss_family
== AF_INET
) {
531 from_ptr
= (struct sockaddr_in
*)&dinfo
.from
;
532 from_ptr
->sin_family
= AF_INET
;
534 from6_ptr
= (struct sockaddr_in6
*)&dinfo
.from
;
535 from6_ptr
->sin6_family
= AF_INET6
;
538 * Since a request hasn't been received from the client
539 * before the delayed responder process is forked, the
540 * from variable is uninitialized. So set it to contain
541 * the client address.
546 * only sleep if DELAY_SECS has not elapsed since
547 * original request was received. Ensure that `now'
548 * is not earlier than `dinfo.timestamp'
551 if ((uint_t
)(now
- dinfo
.timestamp
) < DELAY_SECS
)
552 (void) sleep(DELAY_SECS
- (now
- dinfo
.timestamp
));
561 * Handle the Blocksize option.
562 * Return the blksize option value string to include in the OACK reply.
566 blksize_handler(int opcode
, char *optval
, int *errcode
)
573 value
= (int)strtol(optval
, &endp
, 10);
574 if (errno
!= 0 || value
< MIN_BLKSIZE
|| *endp
!= '\0')
577 * As the blksize value in the OACK reply can be less than the value
578 * requested, to support broken clients if the value requested is larger
579 * than allowed in the RFC, reply with the maximum value permitted.
581 if (value
> MAX_BLKSIZE
)
585 (void) snprintf(optbuf
, sizeof (optbuf
), "%d", blocksize
);
590 * Handle the Timeout Interval option.
591 * Return the timeout option value string to include in the OACK reply.
595 timeout_handler(int opcode
, char *optval
, int *errcode
)
602 value
= (int)strtol(optval
, &endp
, 10);
603 if (errno
!= 0 || *endp
!= '\0')
606 * The timeout value in the OACK reply must match the value specified
607 * by the client, so if an invalid timeout is requested don't include
608 * the timeout option in the OACK reply.
610 if (value
< MIN_TIMEOUT
|| value
> MAX_TIMEOUT
)
614 maxtimeout
= 5 * rexmtval
;
615 (void) snprintf(optbuf
, sizeof (optbuf
), "%d", rexmtval
);
620 * Handle the Transfer Size option.
621 * Return the tsize option value string to include in the OACK reply.
624 tsize_handler(int opcode
, char *optval
, int *errcode
)
631 value
= strtoll(optval
, &endp
, 10);
632 if (errno
!= 0 || value
< 0 || *endp
!= '\0')
636 if (tsize_set
== B_FALSE
)
639 * The tsize value should be 0 for a read request, but to
640 * support broken clients we don't check that it is.
643 #if _FILE_OFFSET_BITS == 32
644 if (value
> MAXOFF_T
) {
652 (void) snprintf(optbuf
, sizeof (optbuf
), OFF_T_FMT
, tsize
);
657 * Process any options included by the client in the request packet.
658 * Return the size of the OACK reply packet built or 0 for no OACK reply.
661 process_options(int opcode
, char *opts
, char *endopts
)
663 char *cp
, *optname
, *optval
, *ostr
, *oackend
;
664 struct tftphdr
*oackp
;
668 * To continue to interoperate with broken TFTP clients, ignore
669 * null padding appended to requests which don't include options.
672 while ((cp
< endopts
) && (*cp
== '\0'))
678 * Construct an Option ACKnowledgement packet if any requested option
681 oackp
= &oackbuf
.hdr
;
682 oackend
= oackbuf
.data
+ sizeof (oackbuf
.data
);
683 oackp
->th_opcode
= htons((ushort_t
)OACK
);
684 cp
= (char *)&oackp
->th_stuff
;
685 while (opts
< endopts
) {
687 if ((optval
= next_field(optname
, endopts
)) == NULL
) {
691 if ((opts
= next_field(optval
, endopts
)) == NULL
) {
695 for (i
= 0; options
[i
].opt_name
!= NULL
; i
++) {
696 if (strcasecmp(optname
, options
[i
].opt_name
) == 0)
699 if (options
[i
].opt_name
!= NULL
) {
700 ostr
= options
[i
].opt_handler(opcode
, optval
, &errcode
);
702 cp
+= strlcpy(cp
, options
[i
].opt_name
,
705 cp
+= strlcpy(cp
, ostr
, oackend
- cp
)
712 } else if (errcode
>= 0) {
718 if (cp
!= (char *)&oackp
->th_stuff
)
719 return (cp
- oackbuf
.data
);
724 * Handle access errors caused by client requests.
728 delay_exit(int ecode
)
730 struct delay_info dinfo
;
733 * The most likely cause of an error here is that
734 * something has broadcast an RRQ packet because it's
735 * trying to boot and doesn't know who the server is.
736 * Rather then sending an ERROR packet immediately, we
737 * wait a while so that the real server has a better chance
738 * of getting through (in case client has lousy Ethernet
739 * interface). We write to a child that handles delayed
740 * ERROR packets to avoid delaying service to new
741 * requests. Of course, we would rather just not answer
742 * RRQ packets that are broadcasted, but there's no way
743 * for a user process to determine this.
746 dinfo
.timestamp
= time(0);
749 * If running in secure mode, we map all errors to EACCESS
750 * so that the client gets no information about which files
751 * or directories exist.
754 dinfo
.ecode
= EACCESS
;
759 if (write(delay_fd
[1], &dinfo
, sizeof (dinfo
)) !=
761 syslog(LOG_ERR
, "delayed write failed.");
762 (void) kill(child
, SIGKILL
);
769 * Handle initial connection protocol.
772 tftp(struct tftphdr
*tp
, int size
)
779 static boolean_t firsttime
= B_TRUE
;
783 readmode
= (tp
->th_opcode
== RRQ
);
784 filename
= (char *)&tp
->th_stuff
;
785 mode
= next_field(filename
, &buf
.data
[size
]);
786 cp
= (mode
!= NULL
) ? next_field(mode
, &buf
.data
[size
]) : NULL
;
791 if (debug
&& standalone
) {
792 (void) fprintf(stderr
, "%s for %s %s ",
793 readmode
? "RRQ" : "WRQ", filename
, mode
);
794 print_options(stderr
, cp
, size
+ buf
.data
- cp
);
795 (void) putc('\n', stderr
);
797 for (pf
= formats
; pf
->f_mode
!= NULL
; pf
++)
798 if (strcasecmp(pf
->f_mode
, mode
) == 0)
800 if (pf
->f_mode
== NULL
) {
806 * XXX fork a new process to handle this request before
807 * chroot(), otherwise the parent won't be able to create a
808 * new socket as that requires library access to system files
811 (void) priv_set(PRIV_ON
, PRIV_EFFECTIVE
, PRIV_PROC_FORK
, NULL
);
814 syslog(LOG_ERR
, "fork (tftp): %m");
815 (void) priv_set(PRIV_OFF
, PRIV_EFFECTIVE
, PRIV_PROC_FORK
, NULL
);
818 (void) priv_set(PRIV_OFF
, PRIV_EFFECTIVE
, PRIV_PROC_FORK
, NULL
);
821 (void) priv_set(PRIV_OFF
, PRIV_EFFECTIVE
, PRIV_PROC_FORK
, NULL
);
826 * Try to see if we can access the file. The access can still
827 * fail later if we are running in secure mode because of
828 * the chroot() call. We only want to execute the chroot() once.
830 if (securetftp
&& firsttime
) {
831 (void) priv_set(PRIV_ON
, PRIV_EFFECTIVE
, PRIV_PROC_CHROOT
,
833 if (chroot(homedir
) == -1) {
835 "tftpd: cannot chroot to directory %s: %m\n",
843 (void) priv_set(PRIV_OFF
, PRIV_EFFECTIVE
, PRIV_PROC_CHROOT
,
845 (void) chdir("/"); /* cd to new root */
847 (void) priv_set(PRIV_OFF
, PRIV_ALLSETS
, PRIV_PROC_CHROOT
,
848 PRIV_NET_PRIVADDR
, NULL
);
850 ecode
= (*pf
->f_validate
)(tp
->th_opcode
);
854 /* we don't use the descriptors passed in to the parent */
855 (void) close(STDIN_FILENO
);
856 (void) close(STDOUT_FILENO
);
859 * Try to open file as low-priv setuid/setgid. Note that
860 * a chroot() has already been done.
863 (readmode
? O_RDONLY
: (O_WRONLY
|O_TRUNC
)) | O_NONBLOCK
);
864 if ((fd
< 0) || (fstat(fd
, &statb
) < 0))
865 delay_exit((errno
== ENOENT
) ? ENOTFOUND
: EACCESS
);
867 if (((statb
.st_mode
& ((readmode
) ? S_IROTH
: S_IWOTH
)) == 0) ||
868 ((statb
.st_mode
& S_IFMT
) != S_IFREG
))
871 file
= fdopen(fd
, readmode
? "r" : "w");
873 delay_exit(errno
+ 100);
875 /* Don't know the size of transfers which involve conversion */
876 tsize_set
= (readmode
&& (pf
->f_convert
== 0));
878 tsize
= statb
.st_size
;
880 /* Deal with any options sent by the client */
881 oacklen
= process_options(tp
->th_opcode
, cp
, buf
.data
+ size
);
883 if (tp
->th_opcode
== WRQ
)
884 (*pf
->f_recv
)(pf
, oacklen
);
886 (*pf
->f_send
)(pf
, oacklen
);
892 * Maybe map filename into another one.
894 * For PNP, we get TFTP boot requests for filenames like
895 * <Unknown Hex IP Addr>.<Architecture Name>. We must
896 * map these to 'pnp.<Architecture Name>'. Note that
897 * uppercase is mapped to lowercase in the architecture names.
899 * For names <Hex IP Addr> there are two cases. First,
900 * it may be a buggy prom that omits the architecture code.
901 * So first check if <Hex IP Addr>.<arch> is on the filesystem.
902 * Second, this is how most Sun3s work; assume <arch> is sun3.
906 pnp_check(char *origname
)
908 static char buf
[MAXNAMLEN
+ 1];
909 char *arch
, *s
, *bufend
;
911 int len
= (origname
? strlen(origname
) : 0);
915 if (securetftp
|| disable_pnp
|| len
< 8 || len
> 14)
919 * XXX see if this cable allows pnp; if not, return NULL
920 * Requires YP support for determining this!
923 ipaddr
= htonl(strtol(origname
, &arch
, 16));
924 if ((arch
== NULL
) || (len
> 8 && *arch
!= '.'))
932 * Allow <Hex IP Addr>* filename request to to be
933 * satisfied by <Hex IP Addr><Any Suffix> rather
934 * than enforcing this to be Sun3 systems. Also serves
935 * to make case of suffix a don't-care.
937 if ((dir
= opendir(homedir
)) == NULL
)
939 while ((dp
= readdir(dir
)) != NULL
) {
940 if (strncmp(origname
, dp
->d_name
, 8) == 0) {
941 (void) strlcpy(buf
, dp
->d_name
, sizeof (buf
));
942 (void) closedir(dir
);
946 (void) closedir(dir
);
949 * XXX maybe call YP master for most current data iff
954 * only do mapping PNP boot file name for machines that
955 * are not in the hosts database.
957 if (gethostbyaddr((char *)&ipaddr
, sizeof (ipaddr
), AF_INET
) != NULL
)
960 s
= buf
+ strlcpy(buf
, "pnp.", sizeof (buf
));
961 bufend
= &buf
[sizeof (buf
) - 1];
962 while ((*arch
!= '\0') && (s
< bufend
))
963 *s
++ = tolower (*arch
++);
970 * Try to validate filename. If the filename doesn't exist try PNP mapping.
973 validate_filename(int mode
)
978 if (stat(filename
, &stbuf
) < 0) {
984 /* try to map requested filename into a pnp filename */
986 filename
= pnp_check(origfile
);
987 if (filename
== NULL
)
990 if (stat(filename
, &stbuf
) < 0)
991 return (errno
== ENOENT
? ENOTFOUND
: EACCESS
);
992 syslog(LOG_NOTICE
, "%s -> %s\n", origfile
, filename
);
1002 timeout
+= rexmtval
;
1003 if (timeout
>= maxtimeout
)
1005 siglongjmp(timeoutbuf
, 1);
1009 * Send the requested file.
1012 tftpd_sendfile(struct formats
*pf
, int oacklen
)
1015 volatile ushort_t block
= 1;
1016 int size
, n
, serrno
;
1019 (void) sigset(SIGALRM
, timer
);
1021 (void) sigsetjmp(timeoutbuf
, 1);
1022 if (debug
&& standalone
) {
1023 (void) fputs("Sending OACK ", stderr
);
1024 print_options(stderr
, (char *)&oackbuf
.hdr
.th_stuff
,
1026 (void) putc('\n', stderr
);
1028 if (sendto(peer
, &oackbuf
, oacklen
, 0,
1029 (struct sockaddr
*)&from
, fromplen
) != oacklen
) {
1030 if (debug
&& standalone
) {
1032 perror("sendto (oack)");
1035 SYSLOG_MSG("sendto (oack): %m");
1038 (void) alarm(rexmtval
); /* read the ack */
1040 (void) sigrelse(SIGALRM
);
1041 n
= recv(peer
, &ackbuf
, sizeof (ackbuf
), 0);
1042 (void) sighold(SIGALRM
);
1047 SYSLOG_MSG("recv (ack): %m");
1048 if (debug
&& standalone
) {
1050 perror("recv (ack)");
1054 ackbuf
.tb_hdr
.th_opcode
=
1055 ntohs((ushort_t
)ackbuf
.tb_hdr
.th_opcode
);
1056 ackbuf
.tb_hdr
.th_block
=
1057 ntohs((ushort_t
)ackbuf
.tb_hdr
.th_block
);
1059 if (ackbuf
.tb_hdr
.th_opcode
== ERROR
) {
1060 if (debug
&& standalone
) {
1061 (void) fprintf(stderr
,
1062 "received ERROR %d",
1063 ackbuf
.tb_hdr
.th_code
);
1065 (void) fprintf(stderr
,
1067 ackbuf
.tb_hdr
.th_msg
);
1068 (void) putc('\n', stderr
);
1073 if (ackbuf
.tb_hdr
.th_opcode
== ACK
) {
1074 if (debug
&& standalone
)
1075 (void) fprintf(stderr
,
1076 "received ACK for block %d\n",
1077 ackbuf
.tb_hdr
.th_block
);
1078 if (ackbuf
.tb_hdr
.th_block
== 0)
1081 * Don't resend the OACK, avoids getting stuck
1082 * in an OACK/ACK loop if the client keeps
1083 * replying with a bad ACK. Client will either
1084 * send a good ACK or timeout sending bad ones.
1092 (void) sigset(SIGALRM
, timer
);
1093 size
= readit(file
, &dp
, pf
->f_convert
);
1098 dp
->th_opcode
= htons((ushort_t
)DATA
);
1099 dp
->th_block
= htons((ushort_t
)block
);
1101 (void) sigsetjmp(timeoutbuf
, 1);
1102 if (debug
&& standalone
)
1103 (void) fprintf(stderr
, "Sending DATA block %d\n",
1105 if (sendto(peer
, dp
, size
+ 4, 0,
1106 (struct sockaddr
*)&from
, fromplen
) != size
+ 4) {
1107 if (debug
&& standalone
) {
1109 perror("sendto (data)");
1112 SYSLOG_MSG("sendto (data): %m");
1115 read_ahead(file
, pf
->f_convert
);
1116 (void) alarm(rexmtval
); /* read the ack */
1118 (void) sigrelse(SIGALRM
);
1119 n
= recv(peer
, &ackbuf
, sizeof (ackbuf
), 0);
1120 (void) sighold(SIGALRM
);
1125 SYSLOG_MSG("recv (ack): %m");
1126 if (debug
&& standalone
) {
1128 perror("recv (ack)");
1132 ackbuf
.tb_hdr
.th_opcode
=
1133 ntohs((ushort_t
)ackbuf
.tb_hdr
.th_opcode
);
1134 ackbuf
.tb_hdr
.th_block
=
1135 ntohs((ushort_t
)ackbuf
.tb_hdr
.th_block
);
1137 if (ackbuf
.tb_hdr
.th_opcode
== ERROR
) {
1138 if (debug
&& standalone
) {
1139 (void) fprintf(stderr
,
1140 "received ERROR %d",
1141 ackbuf
.tb_hdr
.th_code
);
1143 (void) fprintf(stderr
,
1145 ackbuf
.tb_hdr
.th_msg
);
1146 (void) putc('\n', stderr
);
1151 if (ackbuf
.tb_hdr
.th_opcode
== ACK
) {
1152 if (debug
&& standalone
)
1153 (void) fprintf(stderr
,
1154 "received ACK for block %d\n",
1155 ackbuf
.tb_hdr
.th_block
);
1156 if (ackbuf
.tb_hdr
.th_block
== block
) {
1160 * Never resend the current DATA packet on
1161 * receipt of a duplicate ACK, doing so would
1162 * cause the "Sorcerer's Apprentice Syndrome".
1168 } while (size
== blocksize
);
1172 (void) fclose(file
);
1177 justquit(int signum
)
1186 tftpd_recvfile(struct formats
*pf
, int oacklen
)
1189 struct tftphdr
*ap
; /* ack buffer */
1191 int n
, size
, acklen
, serrno
;
1194 ap
= &ackbuf
.tb_hdr
;
1196 (void) sigset(SIGALRM
, timer
);
1199 ap
->th_opcode
= htons((ushort_t
)ACK
);
1200 ap
->th_block
= htons((ushort_t
)block
);
1203 /* copy OACK packet to the ack buffer ready to send */
1204 (void) memcpy(&ackbuf
, &oackbuf
, oacklen
);
1209 (void) sigsetjmp(timeoutbuf
, 1);
1211 if (debug
&& standalone
) {
1212 if (ap
->th_opcode
== htons((ushort_t
)ACK
)) {
1213 (void) fprintf(stderr
,
1214 "Sending ACK for block %d\n", block
- 1);
1216 (void) fprintf(stderr
, "Sending OACK ");
1217 print_options(stderr
, (char *)&ap
->th_stuff
,
1219 (void) putc('\n', stderr
);
1222 if (sendto(peer
, &ackbuf
, acklen
, 0, (struct sockaddr
*)&from
,
1223 fromplen
) != acklen
) {
1224 if (ap
->th_opcode
== htons((ushort_t
)ACK
)) {
1225 if (debug
&& standalone
) {
1227 perror("sendto (ack)");
1230 syslog(LOG_ERR
, "sendto (ack): %m\n");
1232 if (debug
&& standalone
) {
1234 perror("sendto (oack)");
1237 syslog(LOG_ERR
, "sendto (oack): %m\n");
1241 if (write_behind(file
, pf
->f_convert
) < 0) {
1245 (void) alarm(rexmtval
);
1247 (void) sigrelse(SIGALRM
);
1248 n
= recv(peer
, dp
, blocksize
+ 4, 0);
1249 (void) sighold(SIGALRM
);
1250 if (n
< 0) { /* really? */
1253 syslog(LOG_ERR
, "recv (data): %m");
1256 dp
->th_opcode
= ntohs((ushort_t
)dp
->th_opcode
);
1257 dp
->th_block
= ntohs((ushort_t
)dp
->th_block
);
1258 if (dp
->th_opcode
== ERROR
) {
1260 if (debug
&& standalone
) {
1261 (void) fprintf(stderr
,
1262 "received ERROR %d", dp
->th_code
);
1264 (void) fprintf(stderr
,
1265 " %.*s", n
- 4, dp
->th_msg
);
1266 (void) putc('\n', stderr
);
1270 if (dp
->th_opcode
== DATA
) {
1271 if (debug
&& standalone
)
1272 (void) fprintf(stderr
,
1273 "Received DATA block %d\n",
1275 if (dp
->th_block
== block
) {
1278 /* Re-synchronize with the other side */
1279 if (synchnet(peer
) < 0) {
1283 if (dp
->th_block
== (block
-1))
1284 goto send_ack
; /* rexmit */
1288 /* size = write(file, dp->th_data, n - 4); */
1289 size
= writeit(file
, &dp
, n
- 4, pf
->f_convert
);
1290 if (size
!= (n
- 4)) {
1291 nak((size
< 0) ? (errno
+ 100) : ENOSPACE
);
1294 } while (size
== blocksize
);
1295 if (write_behind(file
, pf
->f_convert
) < 0) {
1299 n
= fclose(file
); /* close data file */
1306 ap
->th_opcode
= htons((ushort_t
)ACK
); /* send the "final" ack */
1307 ap
->th_block
= htons((ushort_t
)(block
));
1308 if (debug
&& standalone
)
1309 (void) fprintf(stderr
, "Sending ACK for block %d\n", block
);
1310 if (sendto(peer
, &ackbuf
, 4, 0, (struct sockaddr
*)&from
,
1312 if (debug
&& standalone
)
1313 perror("sendto (ack)");
1315 (void) sigset(SIGALRM
, justquit
); /* just quit on timeout */
1316 (void) alarm(rexmtval
);
1317 /* normally times out and quits */
1318 n
= recv(peer
, dp
, blocksize
+ 4, 0);
1320 dp
->th_opcode
= ntohs((ushort_t
)dp
->th_opcode
);
1321 dp
->th_block
= ntohs((ushort_t
)dp
->th_block
);
1322 if (n
>= 4 && /* if read some data */
1323 dp
->th_opcode
== DATA
&& /* and got a data block */
1324 block
== dp
->th_block
) { /* then my last ack was lost */
1325 if (debug
&& standalone
) {
1326 (void) fprintf(stderr
, "Sending ACK for block %d\n",
1329 /* resend final ack */
1330 if (sendto(peer
, &ackbuf
, 4, 0, (struct sockaddr
*)&from
,
1332 if (debug
&& standalone
)
1333 perror("sendto (last ack)");
1340 (void) fclose(file
);
1344 * Send a nak packet (error message).
1345 * Error code passed in is one of the
1346 * standard TFTP codes, or a UNIX errno
1348 * Handles connected as well as unconnected peer.
1359 tp
->th_opcode
= htons((ushort_t
)ERROR
);
1360 tp
->th_code
= htons((ushort_t
)error
);
1361 for (pe
= errmsgs
; pe
->e_code
>= 0; pe
++)
1362 if (pe
->e_code
== error
)
1364 if (pe
->e_code
< 0) {
1365 pe
->e_msg
= strerror(error
- 100);
1366 tp
->th_code
= EUNDEF
; /* set 'undef' errorcode */
1368 (void) strlcpy(tp
->th_msg
, (pe
->e_msg
!= NULL
) ? pe
->e_msg
: "UNKNOWN",
1369 sizeof (buf
) - sizeof (struct tftphdr
));
1370 length
= strlen(tp
->th_msg
);
1371 length
+= sizeof (struct tftphdr
);
1372 if (debug
&& standalone
)
1373 (void) fprintf(stderr
, "Sending NAK: %s\n", tp
->th_msg
);
1375 ret
= sendto(peer
, &buf
, length
, 0, (struct sockaddr
*)&from
,
1377 if (ret
== -1 && errno
== EISCONN
) {
1378 /* Try without an address */
1379 ret
= send(peer
, &buf
, length
, 0);
1383 perror("sendto (nak)");
1385 syslog(LOG_ERR
, "tftpd: nak: %m\n");
1386 } else if (ret
!= length
) {
1388 perror("sendto (nak) lost data");
1390 syslog(LOG_ERR
, "tftpd: nak: %d lost\n", length
- ret
);