2 * Copyright (c) 1983 Regents of the University of California.
3 * Copyright (c) 1999-2009 H. Peter Anvin
4 * Copyright (c) 2011-2014 Intel Corporation; author: H. Peter Anvin
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "config.h" /* Must be included first */
40 * Trivial file transfer protocol server.
42 * This version includes many modifications by Jim Guyton <guyton@rand-unix>
45 #include <sys/ioctl.h>
52 #include "common/tftpsubs.h"
56 #ifdef HAVE_SYS_FILIO_H
57 #include <sys/filio.h> /* Necessary for FIONBIO on Solaris */
60 #ifdef HAVE_TCPWRAPPERS
63 int deny_severity
= LOG_WARNING
;
64 int allow_severity
= -1; /* Don't log at all */
66 static struct request_info wrap_request
;
70 static int ai_fam
= AF_UNSPEC
;
72 static int ai_fam
= AF_INET
;
75 #define TIMEOUT 1000000 /* Default timeout (us) */
76 #define TRIES 6 /* Number of attempts to send each packet */
77 #define TIMEOUT_LIMIT ((1 << TRIES)-1)
79 const char *tftpd_progname
;
81 static unsigned long timeout
= TIMEOUT
; /* Current timeout value */
82 static unsigned long rexmtval
= TIMEOUT
; /* Basic timeout value */
83 static unsigned long maxtimeout
= TIMEOUT_LIMIT
* TIMEOUT
;
84 static int timeout_quit
= 0;
85 static sigjmp_buf timeoutbuf
;
86 static uint16_t rollover_val
= 0;
88 #define PKTSIZE MAX_SEGSIZE+4
89 static char buf
[PKTSIZE
];
90 static char ackbuf
[PKTSIZE
];
91 static unsigned int max_blksize
= MAX_SEGSIZE
;
93 static char tmpbuf
[INET6_ADDRSTRLEN
], *tmp_p
;
95 static union sock_addr from
;
100 static const char **dirs
;
102 static int secure
= 0;
106 unsigned int portrange_from
, portrange_to
;
110 static struct rule
*rewrite_rules
= NULL
;
115 int tftp(struct tftphdr
*, int);
116 static void nak(int, const char *);
117 static void timer(int);
118 static void do_opt(const char *, const char *, char **);
120 static int set_blksize(uintmax_t *);
121 static int set_blksize2(uintmax_t *);
122 static int set_tsize(uintmax_t *);
123 static int set_timeout(uintmax_t *);
124 static int set_utimeout(uintmax_t *);
125 static int set_rollover(uintmax_t *);
129 int (*o_fnc
)(uintmax_t *);
131 {"blksize", set_blksize
},
132 {"blksize2", set_blksize2
},
133 {"tsize", set_tsize
},
134 {"timeout", set_timeout
},
135 {"utimeout", set_utimeout
},
136 {"rollover", set_rollover
},
140 /* Simple handler for SIGHUP */
141 static volatile sig_atomic_t caught_sighup
= 0;
142 static void handle_sighup(int sig
)
144 (void)sig
; /* Suppress unused warning */
148 /* Handle exit requests by SIGTERM and SIGINT */
149 static volatile sig_atomic_t exit_signal
= 0;
150 static void handle_exit(int sig
)
155 /* Handle timeout signal or timeout event */
156 static void timer(int sig
)
158 (void)sig
; /* Suppress unused warning */
160 if (timeout
>= maxtimeout
|| timeout_quit
)
162 siglongjmp(timeoutbuf
, 1);
166 static struct rule
*read_remap_rules(const char *rulefile
)
171 f
= fopen(rulefile
, "rt");
173 syslog(LOG_ERR
, "Cannot open map file: %s: %m", rulefile
);
176 rulep
= parserulefile(f
);
184 * Rules for locking files; return 0 on success, -1 on failure
186 static int lock_file(int fd
, int lock_write
)
189 #if defined(HAVE_FCNTL) && HAVE_DECL_F_SETLK
192 fl
.l_type
= lock_write
? F_WRLCK
: F_RDLCK
;
193 fl
.l_whence
= SEEK_SET
;
195 fl
.l_len
= 0; /* Whole file */
196 return fcntl(fd
, F_SETLK
, &fl
);
197 #elif defined(HAVE_FLOCK) && HAVE_DECL_LOCK_SH && HAVE_DECL_LOCK_EX
198 return flock(fd
, lock_write
? LOCK_EX
|LOCK_NB
: LOCK_SH
|LOCK_NB
);
200 return 0; /* Hope & pray... */
204 static void set_socket_nonblock(int fd
, int flag
)
208 #if defined(HAVE_FCNTL) && defined(HAVE_O_NONBLOCK_DEFINITION)
209 /* Posixly correct */
210 err
= ((flags
= fcntl(fd
, F_GETFL
, 0)) < 0) ||
213 flag
? flags
| O_NONBLOCK
: flags
& ~O_NONBLOCK
) < 0);
215 flags
= flag
? 1 : 0;
216 err
= (ioctl(fd
, FIONBIO
, &flags
) < 0);
219 syslog(LOG_ERR
, "Cannot set nonblock flag on socket: %m");
224 static void pmtu_discovery_off(int fd
)
226 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
227 int pmtu
= IP_PMTUDISC_DONT
;
229 setsockopt(fd
, IPPROTO_IP
, IP_MTU_DISCOVER
, &pmtu
, sizeof(pmtu
));
234 * Receive packet with synchronous timeout; timeout is adjusted
235 * to account for time spent waiting.
237 static int recv_time(int s
, void *rbuf
, int len
, unsigned int flags
,
238 unsigned long *timeout_us_p
)
241 struct timeval tmv
, t0
, t1
;
243 unsigned long timeout_us
= *timeout_us_p
;
244 unsigned long timeout_left
, dt
;
246 gettimeofday(&t0
, NULL
);
247 timeout_left
= timeout_us
;
254 tmv
.tv_sec
= timeout_left
/ 1000000;
255 tmv
.tv_usec
= timeout_left
% 1000000;
257 rv
= select(s
+ 1, &fdset
, NULL
, NULL
, &tmv
);
260 gettimeofday(&t1
, NULL
);
262 dt
= (t1
.tv_sec
- t0
.tv_sec
) * 1000000 +
263 (t1
.tv_usec
- t0
.tv_usec
);
264 *timeout_us_p
= timeout_left
=
265 (dt
>= timeout_us
) ? 1 : (timeout_us
- dt
);
266 } while (rv
== -1 && err
== EINTR
);
269 timer(0); /* Should not return */
273 set_socket_nonblock(s
, 1);
274 rv
= recv(s
, rbuf
, len
, flags
);
276 set_socket_nonblock(s
, 0);
279 if (E_WOULD_BLOCK(err
) || err
== EINTR
) {
280 continue; /* Once again, with feeling... */
291 static int split_port(char **ap
, char **pp
)
298 if (is_numeric_ipv6(a
)) {
319 if (inet_aton(a
, &in
))
326 enum long_only_options
{
331 static struct option long_options
[] = {
332 { "ipv4", 0, NULL
, '4' },
333 { "ipv6", 0, NULL
, '6' },
334 { "create", 0, NULL
, 'c' },
335 { "secure", 0, NULL
, 's' },
336 { "permissive", 0, NULL
, 'p' },
337 { "verbose", 0, NULL
, 'v' },
338 { "verbosity", 1, NULL
, OPT_VERBOSITY
},
339 { "version", 0, NULL
, 'V' },
340 { "listen", 0, NULL
, 'l' },
341 { "foreground", 0, NULL
, 'L' },
342 { "address", 1, NULL
, 'a' },
343 { "blocksize", 1, NULL
, 'B' },
344 { "user", 1, NULL
, 'u' },
345 { "umask", 1, NULL
, 'U' },
346 { "refuse", 1, NULL
, 'r' },
347 { "timeout", 1, NULL
, 't' },
348 { "retransmit", 1, NULL
, 'T' },
349 { "port-range", 1, NULL
, 'R' },
350 { "map-file", 1, NULL
, 'm' },
351 { "map-steps", 1, NULL
, OPT_MAP_STEPS
},
352 { "pidfile", 1, NULL
, 'P' },
355 static const char short_options
[] = "46cspvVlLa:B:u:U:r:t:T:R:m:P:";
357 int main(int argc
, char **argv
)
362 union sock_addr myaddr
;
363 struct sockaddr_in bindaddr4
;
365 struct sockaddr_in6 bindaddr6
;
373 int standalone
= 0; /* Standalone (listen) mode */
374 int nodaemon
= 0; /* Do not detach process */
375 char *address
= NULL
; /* Address to listen to */
382 int waittime
= 900; /* Default time to wait for a connect */
383 const char *user
= "nobody"; /* Default user */
386 char *rewrite_file
= NULL
;
388 const char *pidfile
= NULL
;
391 /* basename() is way too much of a pain from a portability standpoint */
393 p
= strrchr(argv
[0], '/');
394 tftpd_progname
= (p
&& p
[1]) ? p
+ 1 : argv
[0];
396 openlog(tftpd_progname
, LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
398 srand(time(NULL
) ^ getpid());
400 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
))
432 waittime
= atoi(optarg
);
437 max_blksize
= (unsigned int)strtoul(optarg
, &vp
, 10);
438 if (max_blksize
< 512 || max_blksize
> MAX_SEGSIZE
|| *vp
) {
440 "Bad maximum blocksize value (range 512-%d): %s",
441 MAX_SEGSIZE
, optarg
);
449 unsigned long tov
= strtoul(optarg
, &vp
, 10);
450 if (tov
< 10000UL || tov
> 255000000UL || *vp
) {
451 syslog(LOG_ERR
, "Bad timeout value: %s", optarg
);
454 rexmtval
= timeout
= tov
;
455 maxtimeout
= rexmtval
* TIMEOUT_LIMIT
;
460 if (sscanf(optarg
, "%u:%u", &portrange_from
, &portrange_to
)
461 != 2 || portrange_from
> portrange_to
462 || portrange_to
>= 65535) {
463 syslog(LOG_ERR
, "Bad port range: %s", optarg
);
473 my_umask
= strtoul(optarg
, &ep
, 8);
475 syslog(LOG_ERR
, "Invalid umask: %s", optarg
);
481 for (opt
= options
; opt
->o_opt
; opt
++) {
482 if (!strcasecmp(optarg
, opt
->o_opt
)) {
483 opt
->o_opt
= ""; /* Don't support this option */
488 syslog(LOG_ERR
, "Unknown option: %s", optarg
);
495 syslog(LOG_ERR
, "Multiple -m options");
498 rewrite_file
= optarg
;
502 unsigned long steps
= strtoul(optarg
, &ep
, 0);
503 if (*optarg
&& *ep
&& steps
> 0 && steps
<= INT_MAX
) {
504 deadman_max_steps
= steps
;
506 syslog(LOG_ERR
, "Bad --map-steps option: %s", optarg
);
516 verbosity
= atoi(optarg
);
519 /* Print configuration to stdout and exit */
520 printf("%s\n", TFTPD_CONFIG_STR
);
527 syslog(LOG_ERR
, "Unknown option: '%c'", optopt
);
531 dirs
= xmalloc((argc
- optind
+ 1) * sizeof(char *));
532 for (ndirs
= 0; optind
!= argc
; optind
++)
533 dirs
[ndirs
++] = argv
[optind
];
539 syslog(LOG_ERR
, "no -s directory");
543 syslog(LOG_ERR
, "too many -s directories");
546 if (chdir(dirs
[0])) {
547 syslog(LOG_ERR
, "%s: %m", dirs
[0]);
554 syslog(LOG_ERR
, "no user %s: %m", user
);
560 rewrite_rules
= read_remap_rules(rewrite_file
);
563 if (pidfile
&& !standalone
) {
564 syslog(LOG_WARNING
, "not in standalone mode, ignoring pid file");
568 /* If we're running standalone, set up the input port */
572 if (ai_fam
!= AF_INET6
) {
574 fd4
= socket(AF_INET
, SOCK_DGRAM
, 0);
576 syslog(LOG_ERR
, "cannot open IPv4 socket: %m");
580 set_socket_nonblock(fd4
, 1);
582 memset(&bindaddr4
, 0, sizeof bindaddr4
);
583 bindaddr4
.sin_family
= AF_INET
;
584 bindaddr4
.sin_addr
.s_addr
= INADDR_ANY
;
585 bindaddr4
.sin_port
= htons(IPPORT_TFTP
);
588 if (ai_fam
!= AF_INET
) {
589 fd6
= socket(AF_INET6
, SOCK_DGRAM
, 0);
592 syslog(LOG_ERR
, "cannot open IPv6 socket: %m");
596 "cannot open IPv6 socket, disable IPv6: %m");
600 set_socket_nonblock(fd6
, 1);
602 memset(&bindaddr6
, 0, sizeof bindaddr6
);
603 bindaddr6
.sin6_family
= AF_INET6
;
604 bindaddr6
.sin6_port
= htons(IPPORT_TFTP
);
608 char *portptr
= NULL
, *eportptr
;
610 struct servent
*servent
;
613 address
= tfstrdup(address
);
614 err
= split_port(&address
, &portptr
);
621 if (ai_fam
== AF_INET6
) {
623 "Address %s is not in address family AF_INET6",
634 if (ai_fam
== AF_INET
) {
636 "Address %s is not in address family AF_INET",
648 "Numeric IPv6 addresses need to be enclosed in []");
652 portptr
= (char *)"tftp";
655 bindaddr4
.sin_family
= AF_INET
;
656 err
= set_sock_addr(address
,
657 (union sock_addr
*)&bindaddr4
, NULL
);
660 "cannot resolve local IPv4 bind address: %s, %s",
661 address
, gai_strerror(err
));
667 bindaddr6
.sin6_family
= AF_INET6
;
668 err
= set_sock_addr(address
,
669 (union sock_addr
*)&bindaddr6
, NULL
);
673 "cannot resolve local IPv6 bind address: %s"
674 "(%s); using IPv4 only",
675 address
, gai_strerror(err
));
680 "cannot resolve local IPv6 bind address: %s"
681 "(%s)", address
, gai_strerror(err
));
688 /* Default to using INADDR_ANY */
691 if (portptr
&& *portptr
) {
692 servent
= getservbyname(portptr
, "udp");
695 bindaddr4
.sin_port
= servent
->s_port
;
698 bindaddr6
.sin6_port
= servent
->s_port
;
700 } else if ((port
= strtoul(portptr
, &eportptr
, 0))
703 bindaddr4
.sin_port
= htons(port
);
706 bindaddr6
.sin6_port
= htons(port
);
708 } else if (!strcmp(portptr
, "tftp")) {
709 /* It's TFTP, we're OK */
711 syslog(LOG_ERR
, "cannot resolve local bind port: %s",
719 if (bind(fd4
, (struct sockaddr
*)&bindaddr4
,
720 sizeof(bindaddr4
)) < 0) {
721 syslog(LOG_ERR
, "cannot bind to local IPv4 socket: %m");
727 #if defined(IPV6_V6ONLY)
729 if (fd4
>= 0 || force_ipv6
)
730 if (setsockopt(fd6
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char*)&on
,
732 syslog(LOG_ERR
, "cannot setsockopt IPV6_V6ONLY %m");
734 if (bind(fd6
, (struct sockaddr
*)&bindaddr6
,
735 sizeof(bindaddr6
)) < 0) {
738 "cannot bind to local IPv6 socket,"
739 "IPv6 disabled: %m");
743 syslog(LOG_ERR
, "cannot bind to local IPv6 socket: %m");
749 /* Daemonize this process */
750 /* Note: when running in secure mode (-s), we must not chdir, since
751 we are already in the proper directory. */
752 if (!nodaemon
&& daemon(secure
, 0) < 0) {
753 syslog(LOG_ERR
, "cannot daemonize: %m");
756 set_signal(SIGTERM
, handle_exit
, 0);
757 set_signal(SIGINT
, handle_exit
, 0);
759 pf
= fopen (pidfile
, "w");
761 syslog(LOG_ERR
, "cannot open pid file '%s' for writing: %m", pidfile
);
764 if (fprintf(pf
, "%d\n", getpid()) < 0)
765 syslog(LOG_ERR
, "error writing pid file '%s': %m", pidfile
);
767 syslog(LOG_ERR
, "error closing pid file '%s': %m", pidfile
);
775 /* 0 is our socket descriptor */
780 /* Note: on Cygwin, select() on a nonblocking socket becomes
781 a nonblocking select. */
783 set_socket_nonblock(fd
, 1);
787 /* Disable path MTU discovery */
788 pmtu_discovery_off(fd
);
790 /* This means we don't want to wait() for children */
792 set_signal(SIGCHLD
, SIG_IGN
, SA_NOCLDSTOP
| SA_NOCLDWAIT
);
794 set_signal(SIGCHLD
, SIG_IGN
, SA_NOCLDSTOP
);
797 /* Take SIGHUP and use it to set a variable. This
798 is polled synchronously to make sure we don't
799 lose packets as a result. */
800 set_signal(SIGHUP
, handle_sighup
, 0);
802 if (spec_umask
|| !unixperms
)
807 struct timeval tv_waittime
;
810 if (exit_signal
) { /* happens in standalone mode only */
811 if (pidfile
&& unlink(pidfile
)) {
812 syslog(LOG_WARNING
, "error removing pid file '%s': %m", pidfile
);
824 freerules(rewrite_rules
);
825 rewrite_rules
= read_remap_rules(rewrite_file
);
829 /* Return to inetd for respawn */
837 FD_SET(fd4
, &readset
);
839 /* On Cygwin, select() on a nonblocking socket returns
840 immediately, with a rv of 0! */
841 set_socket_nonblock(fd4
, 0);
845 FD_SET(fd6
, &readset
);
847 /* On Cygwin, select() on a nonblocking socket returns
848 immediately, with a rv of 0! */
849 set_socket_nonblock(fd6
, 0);
852 } else { /* fd always 0 */
855 /* On Cygwin, select() on a nonblocking socket returns
856 immediately, with a rv of 0! */
857 set_socket_nonblock(fd
, 0);
859 FD_SET(fd
, &readset
);
861 tv_waittime
.tv_sec
= waittime
;
862 tv_waittime
.tv_usec
= 0;
865 /* Never time out if we're in standalone mode */
866 rv
= select(fdmax
+ 1, &readset
, NULL
, NULL
,
867 standalone
? NULL
: &tv_waittime
);
868 if (rv
== -1 && errno
== EINTR
)
869 continue; /* Signal caught, reloop */
872 syslog(LOG_ERR
, "select loop: %m");
874 } else if (rv
== 0) {
875 exit(0); /* Timeout, return to inetd */
879 if ((fd4
>= 0) && FD_ISSET(fd4
, &readset
))
881 else if ((fd6
>= 0) && FD_ISSET(fd6
, &readset
))
883 else /* not in set ??? */
887 /* On Cygwin, select() on a nonblocking socket returns
888 immediately, with a rv of 0! */
889 set_socket_nonblock(fd
, 0);
892 n
= myrecvfrom(fd
, buf
, sizeof(buf
), 0, &from
, &myaddr
);
895 if (E_WOULD_BLOCK(errno
) || errno
== EINTR
) {
896 continue; /* Again, from the top */
898 syslog(LOG_ERR
, "recvfrom: %m");
903 if ((from
.sa
.sa_family
!= AF_INET
) && (from
.sa
.sa_family
!= AF_INET6
)) {
904 syslog(LOG_ERR
, "received address was not AF_INET/AF_INET6,"
905 " please check your inetd config");
907 if (from
.sa
.sa_family
!= AF_INET
) {
908 syslog(LOG_ERR
, "received address was not AF_INET,"
909 " please check your inetd config");
915 if ((from
.sa
.sa_family
== AF_INET
) &&
916 (myaddr
.si
.sin_addr
.s_addr
== INADDR_ANY
)) {
917 /* myrecvfrom() didn't capture the source address; but we might
918 have bound to a specific address, if so we should use it */
919 memcpy(SOCKADDR_P(&myaddr
), &bindaddr4
.sin_addr
,
920 sizeof(bindaddr4
.sin_addr
));
922 } else if ((from
.sa
.sa_family
== AF_INET6
) &&
923 IN6_IS_ADDR_UNSPECIFIED((struct in6_addr
*)
924 SOCKADDR_P(&myaddr
))) {
925 memcpy(SOCKADDR_P(&myaddr
), &bindaddr6
.sin6_addr
,
926 sizeof(bindaddr6
.sin6_addr
));
932 * Now that we have read the request packet from the UDP
933 * socket, we fork and go back to listening to the socket.
937 syslog(LOG_ERR
, "fork: %m");
938 exit(EX_OSERR
); /* Return to inetd, just in case */
940 break; /* Child exit, parent loop */
943 /* Child process: handle the actual request here */
946 set_signal(SIGHUP
, SIG_IGN
, 0);
948 /* Make sure the log socket is still connected. This has to be
949 done before the chroot, while /dev/log is still accessible.
950 When not running standalone, there is little chance that the
951 syslog daemon gets restarted by the time we get here. */
952 if (secure
&& standalone
) {
954 openlog(tftpd_progname
, LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
957 #ifdef HAVE_TCPWRAPPERS
958 /* Verify if this was a legal request for us. This has to be
959 done before the chroot, while /etc is still accessible. */
960 request_init(&wrap_request
,
961 RQ_DAEMON
, tftpd_progname
,
963 RQ_CLIENT_SIN
, &from
, RQ_SERVER_SIN
, &myaddr
, 0);
964 sock_methods(&wrap_request
);
966 tmp_p
= (char *)inet_ntop(myaddr
.sa
.sa_family
, SOCKADDR_P(&myaddr
),
967 tmpbuf
, INET6_ADDRSTRLEN
);
970 strcpy(tmpbuf
, "???");
972 if (hosts_access(&wrap_request
) == 0) {
973 if (deny_severity
!= -1)
974 syslog(deny_severity
, "connection refused from %s", tmp_p
);
975 exit(EX_NOPERM
); /* Access denied */
976 } else if (allow_severity
!= -1) {
977 syslog(allow_severity
, "connect from %s", tmp_p
);
981 /* Close file descriptors we don't need */
984 /* Get a socket. This has to be done before the chroot(), since
985 some systems require access to /dev to create a socket. */
987 peer
= socket(myaddr
.sa
.sa_family
, SOCK_DGRAM
, 0);
989 syslog(LOG_ERR
, "socket: %m");
993 /* Set up the supplementary group access list if possible
994 /etc/group still need to be accessible at this point.
995 If we get EPERM, this is already a restricted process, e.g.
996 using user namespaces on Linux. */
998 #ifdef HAVE_SETGROUPS
999 setrv
= setgroups(0, NULL
);
1000 if (setrv
&& errno
!= EPERM
) {
1001 syslog(LOG_ERR
, "cannot clear group list");
1005 #ifdef HAVE_INITGROUPS
1006 setrv
= initgroups(user
, pw
->pw_gid
);
1009 } else if (errno
!= EPERM
) {
1010 syslog(LOG_ERR
, "cannot set groups for user %s", user
);
1017 /* Chroot and drop privileges */
1020 syslog(LOG_ERR
, "chroot: %m");
1024 chdir("/"); /* Cygwin chroot() bug workaround */
1028 #ifdef HAVE_SETRESGID
1029 setrv
= setresgid(pw
->pw_gid
, pw
->pw_gid
, pw
->pw_gid
);
1030 #elif defined(HAVE_SETREGID)
1031 setrv
= setregid(pw
->pw_gid
, pw
->pw_gid
);
1033 setrv
= setegid(pw
->pw_gid
) || setgid(pw
->pw_gid
);
1035 if (setrv
&& errno
== EPERM
) {
1036 setrv
= 0; /* Assume already restricted by system policy */
1039 #ifdef HAVE_SETRESUID
1040 setrv
= setrv
|| setresuid(pw
->pw_uid
, pw
->pw_uid
, pw
->pw_uid
);
1041 #elif defined(HAVE_SETREUID)
1042 setrv
= setrv
|| setreuid(pw
->pw_uid
, pw
->pw_uid
);
1044 /* Important: setuid() must come first */
1045 setrv
= setrv
|| setuid(pw
->pw_uid
) ||
1046 (geteuid() != pw
->pw_uid
&& seteuid(pw
->pw_uid
));
1048 if (setrv
&& errno
== EPERM
) {
1049 setrv
= 0; /* Assume already restricted by system policy */
1053 syslog(LOG_ERR
, "cannot drop privileges: %m");
1057 /* Process the request... */
1058 if (pick_port_bind(peer
, &myaddr
, portrange_from
, portrange_to
) < 0) {
1059 syslog(LOG_ERR
, "bind: %m");
1063 if (connect(peer
, &from
.sa
, SOCKLEN(&from
)) < 0) {
1064 syslog(LOG_ERR
, "connect: %m");
1068 /* Disable path MTU discovery */
1069 pmtu_discovery_off(peer
);
1071 tp
= (struct tftphdr
*)buf
;
1072 tp_opcode
= ntohs(tp
->th_opcode
);
1073 if (tp_opcode
== RRQ
|| tp_opcode
== WRQ
)
1078 static char *rewrite_access(const struct formats
*,
1079 char *, int, int, const char **);
1080 static int validate_access(char *, int, const struct formats
*, const char **);
1081 static void tftp_sendfile(const struct formats
*, struct tftphdr
*, int);
1082 static void tftp_recvfile(const struct formats
*, struct tftphdr
*, int);
1084 static const struct formats formats
[] = {
1086 "netascii", rewrite_access
, validate_access
, tftp_sendfile
,
1087 tftp_recvfile
, 1}, {
1088 "octet", rewrite_access
, validate_access
, tftp_sendfile
,
1089 tftp_recvfile
, 0}, {
1090 NULL
, NULL
, NULL
, NULL
, NULL
, 0}
1094 * Handle initial connection protocol.
1096 int tftp(struct tftphdr
*tp
, int size
)
1100 const struct formats
*pf
= NULL
;
1102 char *filename
, *mode
= NULL
;
1103 const char *errmsgptr
;
1104 u_short tp_opcode
= ntohs(tp
->th_opcode
);
1106 char *val
= NULL
, *opt
= NULL
;
1107 char *ap
= ackbuf
+ 2;
1109 ((struct tftphdr
*)ackbuf
)->th_opcode
= htons(OACK
);
1111 origfilename
= cp
= (char *)&(tp
->th_stuff
);
1114 end
= (char *)tp
+ size
;
1116 while (cp
< end
&& *cp
) {
1119 } while (cp
< end
&& *cp
);
1122 nak(EBADOP
, "Request not null-terminated");
1129 } else if (argn
== 2) {
1130 for (cp
= mode
; *cp
; cp
++)
1132 for (pf
= formats
; pf
->f_mode
; pf
++) {
1133 if (!strcmp(pf
->f_mode
, mode
))
1137 nak(EBADOP
, "Unknown mode");
1141 if (!(filename
= (*pf
->f_rewrite
)
1142 (pf
, origfilename
, tp_opcode
, from
.sa
.sa_family
, &errmsgptr
))) {
1143 nak(EACCESS
, errmsgptr
); /* File denied by mapping rule */
1146 if (verbosity
>= 1) {
1147 tmp_p
= (char *)inet_ntop(from
.sa
.sa_family
, SOCKADDR_P(&from
),
1148 tmpbuf
, INET6_ADDRSTRLEN
);
1151 strcpy(tmpbuf
, "???");
1153 if (filename
== origfilename
1154 || !strcmp(filename
, origfilename
))
1155 syslog(LOG_NOTICE
, "%s from %s filename %s\n",
1156 tp_opcode
== WRQ
? "WRQ" : "RRQ",
1160 "%s from %s filename %s remapped to %s\n",
1161 tp_opcode
== WRQ
? "WRQ" : "RRQ",
1162 tmp_p
, origfilename
,
1166 * If "file" is already set, then a file was already validated
1167 * and opened during remap processing.
1171 (*pf
->f_validate
) (filename
, tp_opcode
, pf
, &errmsgptr
);
1173 nak(ecode
, errmsgptr
);
1178 } else if (argn
& 1) {
1181 do_opt(opt
, val
, &ap
);
1187 nak(EBADOP
, "Missing mode");
1191 if (ap
!= (ackbuf
+ 2)) {
1192 if (tp_opcode
== WRQ
)
1193 (*pf
->f_recv
) (pf
, (struct tftphdr
*)ackbuf
, ap
- ackbuf
);
1195 (*pf
->f_send
) (pf
, (struct tftphdr
*)ackbuf
, ap
- ackbuf
);
1197 if (tp_opcode
== WRQ
)
1198 (*pf
->f_recv
) (pf
, NULL
, 0);
1200 (*pf
->f_send
) (pf
, NULL
, 0);
1202 exit(0); /* Request completed */
1205 static int blksize_set
;
1208 * Set a non-standard block size (c.f. RFC2348)
1210 static int set_blksize(uintmax_t *vp
)
1219 else if (sz
> max_blksize
)
1228 * Set a power-of-two block size (nonstandard)
1230 static int set_blksize2(uintmax_t *vp
)
1239 else if (sz
> max_blksize
)
1243 /* Convert to a power of two */
1244 if (sz
& (sz
- 1)) {
1245 unsigned int sz1
= 1;
1246 /* Not a power of two - need to convert */
1258 * Set the block number rollover value
1260 static int set_rollover(uintmax_t *vp
)
1267 rollover_val
= (uint16_t)ro
;
1272 * Return a file size (c.f. RFC2349)
1273 * For netascii mode, we don't know the size ahead of time;
1274 * so reject the option.
1276 static int set_tsize(uintmax_t *vp
)
1291 * Set the timeout (c.f. RFC2349). This is supposed
1292 * to be the (default) retransmission timeout, but being an
1293 * integer in seconds it seems a bit limited.
1295 static int set_timeout(uintmax_t *vp
)
1299 if (to
< 1 || to
> 255)
1302 rexmtval
= timeout
= to
* 1000000UL;
1303 maxtimeout
= rexmtval
* TIMEOUT_LIMIT
;
1308 /* Similar, but in microseconds. We allow down to 10 ms. */
1309 static int set_utimeout(uintmax_t *vp
)
1313 if (to
< 10000UL || to
> 255000000UL)
1316 rexmtval
= timeout
= to
;
1317 maxtimeout
= rexmtval
* TIMEOUT_LIMIT
;
1323 * Conservative calculation for the size of a buffer which can hold an
1326 #define OPTBUFSIZE (sizeof(uintmax_t) * CHAR_BIT / 3 + 3)
1329 * Parse RFC2347 style options; we limit the arguments to positive
1330 * integers which matches all our current options.
1332 static void do_opt(const char *opt
, const char *val
, char **ap
)
1335 char retbuf
[OPTBUFSIZE
];
1337 size_t optlen
, retlen
;
1341 /* Global option-parsing variables initialization */
1348 v
= strtoumax(val
, &vend
, 10);
1349 if (*vend
|| errno
== ERANGE
)
1352 for (po
= options
; po
->o_opt
; po
++)
1353 if (!strcasecmp(po
->o_opt
, opt
)) {
1354 if (po
->o_fnc(&v
)) {
1355 optlen
= strlen(opt
);
1356 retlen
= sprintf(retbuf
, "%"PRIuMAX
, v
);
1358 if (p
+ optlen
+ retlen
+ 2 >= ackbuf
+ sizeof(ackbuf
)) {
1359 nak(EOPTNEG
, "Insufficient space for options");
1363 memcpy(p
, opt
, optlen
+1);
1365 memcpy(p
, retbuf
, retlen
+1);
1368 nak(EOPTNEG
, "Unsupported option(s) requested");
1380 * This is called by the remap engine when it encounters macros such
1381 * as \i. It should write the output in "output" if non-NULL, and
1382 * return the length of the output (generated or not).
1384 * Return -1 on failure.
1386 static int rewrite_macros(char macro
, char *output
)
1388 char *p
, tb
[INET6_ADDRSTRLEN
];
1393 p
= (char *)inet_ntop(from
.sa
.sa_family
, SOCKADDR_P(&from
),
1394 tb
, INET6_ADDRSTRLEN
);
1404 if (from
.sa
.sa_family
== AF_INET
) {
1405 sprintf(output
, "%08lX",
1406 (unsigned long)ntohl(from
.si
.sin_addr
.s_addr
));
1410 unsigned char *c
= (unsigned char *)SOCKADDR_P(&from
);
1412 for (l
= 0; l
< 16; l
++) {
1413 sprintf(p
, "%02X", *c
);
1430 * Modify the filename, if applicable. If it returns NULL, deny the access.
1432 static char *rewrite_access(const struct formats
*pf
, char *filename
,
1433 int mode
, int af
, const char **msg
)
1435 if (rewrite_rules
) {
1437 rewrite_string(pf
, filename
, rewrite_rules
, mode
, af
,
1438 rewrite_macros
, msg
);
1445 static char *rewrite_access(const struct formats
*pf
, char *filename
,
1446 int mode
, int af
, const char **msg
)
1449 (void)mode
; /* Avoid warning */
1457 * Validate file access. Since we
1458 * have no uid or gid, for now require
1459 * file to exist and be publicly
1460 * readable/writable, unless -p specified.
1461 * If we were invoked with arguments
1462 * from inetd then the file must also be
1463 * in one of the given directory prefixes.
1464 * Note also, full path name must be
1465 * given as we have no login directory.
1467 static int validate_access(char *filename
, int mode
,
1468 const struct formats
*pf
, const char **errmsg
)
1472 int fd
, wmode
, rmode
;
1481 if (*filename
!= '/') {
1482 *errmsg
= "Only absolute filenames allowed";
1487 * prevent tricksters from getting around the directory
1490 len
= strlen(filename
);
1491 for (i
= 1; i
< len
- 3; i
++) {
1493 if (*cp
== '.' && memcmp(cp
- 1, "/../", 4) == 0) {
1494 *errmsg
= "Reverse path not allowed";
1499 for (dirp
= dirs
; *dirp
; dirp
++)
1500 if (strncmp(filename
, *dirp
, strlen(*dirp
)) == 0)
1502 if (*dirp
== 0 && dirp
!= dirs
) {
1503 *errmsg
= "Forbidden directory";
1509 * We use different a different permissions scheme if `cancreate' is
1512 wmode
= O_WRONLY
| (cancreate
? O_CREAT
: 0) | (pf
->f_convert
? O_TEXT
: O_BINARY
);
1513 rmode
= O_RDONLY
| (pf
->f_convert
? O_TEXT
: O_BINARY
);
1515 #ifndef HAVE_FTRUNCATE
1516 wmode
|= O_TRUNC
; /* This really sucks on a dupe */
1519 fd
= open(filename
, mode
== RRQ
? rmode
: wmode
, 0666);
1534 if (fstat(fd
, &stbuf
) < 0)
1535 exit(EX_OSERR
); /* This shouldn't happen */
1537 /* A duplicate RRQ or (worse!) WRQ packet could really cause havoc... */
1538 if (lock_file(fd
, mode
!= RRQ
))
1542 if (!unixperms
&& (stbuf
.st_mode
& (S_IREAD
>> 6)) == 0) {
1543 *errmsg
= "File must have global read permissions";
1546 tsize
= stbuf
.st_size
;
1547 /* We don't know the tsize if conversion is needed */
1548 tsize_ok
= !pf
->f_convert
;
1551 if ((stbuf
.st_mode
& (S_IWRITE
>> 6)) == 0) {
1552 *errmsg
= "File must have global write permissions";
1557 #ifdef HAVE_FTRUNCATE
1558 /* We didn't get to truncate the file at open() time */
1559 if (ftruncate(fd
, (off_t
) 0)) {
1560 *errmsg
= "Cannot reset file size";
1568 stdio_mode
[0] = (mode
== RRQ
) ? 'r' : 'w';
1569 stdio_mode
[1] = (pf
->f_convert
) ? 't' : 'b';
1570 stdio_mode
[2] = '\0';
1572 file
= fdopen(fd
, stdio_mode
);
1574 exit(EX_OSERR
); /* Internal error */
1580 * Send the requested file.
1582 static void tftp_sendfile(const struct formats
*pf
, struct tftphdr
*oap
, int oacklen
)
1585 struct tftphdr
*ap
; /* ack packet */
1586 static u_short block
= 1; /* Static to avoid longjmp funnies */
1587 u_short ap_opcode
, ap_block
;
1588 unsigned long r_timeout
;
1593 (void)sigsetjmp(timeoutbuf
, 1);
1595 r_timeout
= timeout
;
1596 if (send(peer
, oap
, oacklen
, 0) != oacklen
) {
1597 syslog(LOG_WARNING
, "tftpd: oack: %m\n");
1601 n
= recv_time(peer
, ackbuf
, sizeof(ackbuf
), 0, &r_timeout
);
1603 syslog(LOG_WARNING
, "tftpd: read: %m\n");
1606 ap
= (struct tftphdr
*)ackbuf
;
1607 ap_opcode
= ntohs((u_short
) ap
->th_opcode
);
1608 ap_block
= ntohs((u_short
) ap
->th_block
);
1610 if (ap_opcode
== ERROR
) {
1612 "tftp: client does not accept options\n");
1615 if (ap_opcode
== ACK
) {
1618 /* Resynchronize with the other side */
1619 (void)synchnet(peer
);
1627 size
= readit(file
, &dp
, pf
->f_convert
);
1629 nak(errno
+ 100, NULL
);
1632 dp
->th_opcode
= htons((u_short
) DATA
);
1633 dp
->th_block
= htons((u_short
) block
);
1635 (void)sigsetjmp(timeoutbuf
, 1);
1637 r_timeout
= timeout
;
1638 if (send(peer
, dp
, size
+ 4, 0) != size
+ 4) {
1639 syslog(LOG_WARNING
, "tftpd: write: %m");
1642 read_ahead(file
, pf
->f_convert
);
1644 n
= recv_time(peer
, ackbuf
, sizeof(ackbuf
), 0, &r_timeout
);
1646 syslog(LOG_WARNING
, "tftpd: read(ack): %m");
1649 ap
= (struct tftphdr
*)ackbuf
;
1650 ap_opcode
= ntohs((u_short
) ap
->th_opcode
);
1651 ap_block
= ntohs((u_short
) ap
->th_block
);
1653 if (ap_opcode
== ERROR
)
1656 if (ap_opcode
== ACK
) {
1657 if (ap_block
== block
) {
1660 /* Re-synchronize with the other side */
1661 (void)synchnet(peer
);
1663 * RFC1129/RFC1350: We MUST NOT re-send the DATA
1664 * packet in response to an invalid ACK. Doing so
1665 * would cause the Sorcerer's Apprentice bug.
1671 block
= rollover_val
;
1672 } while (size
== segsize
);
1680 static void tftp_recvfile(const struct formats
*pf
,
1681 struct tftphdr
*oack
, int oacklen
)
1685 /* These are "static" to avoid longjmp funnies */
1686 static struct tftphdr
*oap
;
1687 static struct tftphdr
*ap
; /* ack buffer */
1688 static u_short block
= 0;
1690 u_short dp_opcode
, dp_block
;
1691 unsigned long r_timeout
;
1699 if (!block
&& oap
) {
1700 ap
= (struct tftphdr
*)ackbuf
;
1703 ap
= (struct tftphdr
*)ackbuf
;
1704 ap
->th_opcode
= htons((u_short
) ACK
);
1705 ap
->th_block
= htons((u_short
) block
);
1707 /* If we're sending a regular ACK, that means we have successfully
1708 * sent the OACK. Clear oap so that we won't try to send another
1709 * OACK when the block number wraps back to 0. */
1713 block
= rollover_val
;
1714 (void)sigsetjmp(timeoutbuf
, 1);
1716 r_timeout
= timeout
;
1717 if (send(peer
, ackbuf
, acksize
, 0) != acksize
) {
1718 syslog(LOG_WARNING
, "tftpd: write(ack): %m");
1721 write_behind(file
, pf
->f_convert
);
1723 n
= recv_time(peer
, dp
, PKTSIZE
, 0, &r_timeout
);
1724 if (n
< 0) { /* really? */
1725 syslog(LOG_WARNING
, "tftpd: read: %m");
1728 dp_opcode
= ntohs((u_short
) dp
->th_opcode
);
1729 dp_block
= ntohs((u_short
) dp
->th_block
);
1730 if (dp_opcode
== ERROR
)
1732 if (dp_opcode
== DATA
) {
1733 if (dp_block
== block
) {
1736 /* Re-synchronize with the other side */
1737 (void)synchnet(peer
);
1738 if (dp_block
== (block
- 1))
1739 goto send_ack
; /* rexmit */
1742 /* size = write(file, dp->th_data, n - 4); */
1743 size
= writeit(file
, &dp
, n
- 4, pf
->f_convert
);
1744 if (size
!= (n
- 4)) { /* ahem */
1746 nak(errno
+ 100, NULL
);
1748 nak(ENOSPACE
, NULL
);
1751 } while (size
== segsize
);
1752 write_behind(file
, pf
->f_convert
);
1753 (void)fclose(file
); /* close data file */
1755 ap
->th_opcode
= htons((u_short
) ACK
); /* send the "final" ack */
1756 ap
->th_block
= htons((u_short
) (block
));
1757 (void)send(peer
, ackbuf
, 4, 0);
1759 timeout_quit
= 1; /* just quit on timeout */
1760 n
= recv_time(peer
, buf
, sizeof(buf
), 0, &timeout
); /* normally times out and quits */
1763 if (n
>= 4 && /* if read some data */
1764 dp_opcode
== DATA
&& /* and got a data block */
1765 block
== dp_block
) { /* then my last ack was lost */
1766 (void)send(peer
, ackbuf
, 4, 0); /* resend final ack */
1772 static const char *const errmsgs
[] = {
1773 "Undefined error code", /* 0 - EUNDEF */
1774 "File not found", /* 1 - ENOTFOUND */
1775 "Access denied", /* 2 - EACCESS */
1776 "Disk full or allocation exceeded", /* 3 - ENOSPACE */
1777 "Illegal TFTP operation", /* 4 - EBADOP */
1778 "Unknown transfer ID", /* 5 - EBADID */
1779 "File already exists", /* 6 - EEXISTS */
1780 "No such user", /* 7 - ENOUSER */
1781 "Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
1784 #define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
1787 * Send a nak packet (error message).
1788 * Error code passed in is one of the
1789 * standard TFTP codes, or a UNIX errno
1792 static void nak(int error
, const char *msg
)
1797 tp
= (struct tftphdr
*)buf
;
1798 tp
->th_opcode
= htons((u_short
) ERROR
);
1801 /* This is a Unix errno+100 */
1803 msg
= strerror(error
- 100);
1806 if ((unsigned)error
>= ERR_CNT
)
1810 msg
= errmsgs
[error
];
1813 tp
->th_code
= htons((u_short
) error
);
1815 length
= strlen(msg
) + 1;
1816 memcpy(tp
->th_msg
, msg
, length
);
1817 length
+= 4; /* Add space for header */
1819 if (verbosity
>= 2) {
1820 tmp_p
= (char *)inet_ntop(from
.sa
.sa_family
, SOCKADDR_P(&from
),
1821 tmpbuf
, INET6_ADDRSTRLEN
);
1824 strcpy(tmpbuf
, "???");
1826 syslog(LOG_INFO
, "sending NAK (%d, %s) to %s",
1827 error
, tp
->th_msg
, tmp_p
);
1830 if (send(peer
, buf
, length
, 0) != length
)
1831 syslog(LOG_WARNING
, "nak: %m");