2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * Trivial file transfer protocol server.
49 * This version includes many modifications by Jim Guyton
53 #include <sys/param.h>
54 #include <sys/ioctl.h>
56 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/tftp.h>
73 #include "tftp-file.h"
75 #include "tftp-utils.h"
76 #include "tftp-transfer.h"
77 #include "tftp-options.h"
83 static void tftp_wrq(int peer, char *, ssize_t);
84 static void tftp_rrq(int peer, char *, ssize_t);
87 * Null-terminated directory prefix list for absolute pathname requests and
88 * search list for relative pathname requests.
90 * MAXDIRS should be at least as large as the number of arguments that
91 * inetd allows (currently 20).
94 static struct dirlist {
98 static int suppress_naks;
101 static int create_new = 0;
102 static const char *newfile_format = "%Y%m%d";
103 static int increase_name = 0;
104 static mode_t mask = S_IWGRP | S_IWOTH;
107 static void tftp_recvfile(int peer, const char *mode);
108 static void tftp_xmitfile(int peer, const char *mode);
109 static int validate_access(int peer, char **, int);
110 static char peername[NI_MAXHOST];
114 static struct formats {
124 main(int argc, char *argv[])
128 socklen_t peerlen, len;
131 char *chroot_dir = NULL;
132 struct passwd *nobody;
133 const char *chuser = "nobody";
134 char recvbuffer[MAXPKTSIZE];
135 int allow_ro = 1, allow_wo = 1;
137 tzset(); /* syslog in localtime */
138 acting_as_client = 0;
140 tftp_openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
141 while ((ch = getopt(argc, argv, "cCd:F:lnoOp:s:u:U:wW")) != -1) {
150 if (atoi(optarg) != 0)
151 debug += atoi(optarg);
153 debug |= debug_finds(optarg);
156 newfile_format = optarg;
165 options_rfc_enabled = 0;
168 options_extra_enabled = 0;
171 packetdroppercentage = atoi(optarg);
173 "Randomly dropping %d out of 100 packets",
174 packetdroppercentage);
183 mask = strtol(optarg, NULL, 0);
193 tftp_log(LOG_WARNING,
194 "ignoring unknown option -%c", ch);
198 struct dirlist *dirp;
200 /* Get list of directory prefixes. Skip relative pathnames. */
201 for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
203 if (argv[optind][0] == '/') {
204 dirp->name = argv[optind];
205 dirp->len = strlen(dirp->name);
210 else if (chroot_dir) {
214 if (ipchroot > 0 && chroot_dir == NULL) {
215 tftp_log(LOG_ERR, "-c requires -s");
223 if (ioctl(0, FIONBIO, &on) < 0) {
224 tftp_log(LOG_ERR, "ioctl(FIONBIO): %s", strerror(errno));
229 /* Find out who we are talking to and what we are going to do */
230 peerlen = sizeof(peer_sock);
231 n = recvfrom(0, recvbuffer, MAXPKTSIZE, 0,
232 (struct sockaddr *)&peer_sock, &peerlen);
234 tftp_log(LOG_ERR, "recvfrom: %s", strerror(errno));
237 getnameinfo((struct sockaddr *)&peer_sock, peer_sock.ss_len,
238 peername, sizeof(peername), NULL, 0, NI_NUMERICHOST);
241 * Now that we have read the message out of the UDP
242 * socket, we fork and exit. Thus, inetd will go back
243 * to listening to the tftp port, and the next request
244 * to come in will start up a new instance of tftpd.
246 * We do this so that inetd can run tftpd in "wait" mode.
247 * The problem with tftpd running in "nowait" mode is that
248 * inetd may get one or more successful "selects" on the
249 * tftp port before we do our receive, so more than one
250 * instance of tftpd may be started up. Worse, if tftpd
251 * break before doing the above "recvfrom", inetd would
252 * spawn endless instances, clogging the system.
257 for (i = 1; i < 20; i++) {
262 * flush out to most recently sent request.
264 * This may drop some request, but those
265 * will be resent by the clients when
266 * they timeout. The positive effect of
267 * this flush is to (try to) prevent more
268 * than one tftpd being started up to service
269 * a single request from a single client.
271 peerlen = sizeof peer_sock;
272 i = recvfrom(0, recvbuffer, MAXPKTSIZE, 0,
273 (struct sockaddr *)&peer_sock, &peerlen);
282 tftp_log(LOG_ERR, "fork: %s", strerror(errno));
284 } else if (pid != 0) {
291 * See if the client is allowed to talk to me.
292 * (This needs to be done before the chroot())
295 struct request_info req;
297 request_init(&req, RQ_CLIENT_ADDR, peername, 0);
298 request_set(&req, RQ_DAEMON, "tftpd", 0);
300 if (hosts_access(&req) == 0) {
301 if (debug&DEBUG_ACCESS)
302 tftp_log(LOG_WARNING,
303 "Access denied by 'tftpd' entry "
304 "in /etc/hosts.allow");
307 * Full access might be disabled, but maybe the
308 * client is allowed to do read-only access.
310 request_set(&req, RQ_DAEMON, "tftpd-ro", 0);
311 allow_ro = hosts_access(&req);
313 request_set(&req, RQ_DAEMON, "tftpd-wo", 0);
314 allow_wo = hosts_access(&req);
316 if (allow_ro == 0 && allow_wo == 0) {
317 tftp_log(LOG_WARNING,
318 "Unauthorized access from %s", peername);
322 if (debug&DEBUG_ACCESS) {
324 tftp_log(LOG_WARNING,
325 "But allowed readonly access "
326 "via 'tftpd-ro' entry");
328 tftp_log(LOG_WARNING,
329 "But allowed writeonly access "
330 "via 'tftpd-wo' entry");
333 if (debug&DEBUG_ACCESS)
334 tftp_log(LOG_WARNING,
335 "Full access allowed"
336 "in /etc/hosts.allow");
341 * Since we exit here, we should do that only after the above
342 * recvfrom to keep inetd from constantly forking should there
343 * be a problem. See the above comment about system clogging.
350 struct sockaddr_storage ss;
351 char hbuf[NI_MAXHOST];
354 memcpy(&ss, &peer_sock, peer_sock.ss_len);
355 unmappedaddr((struct sockaddr_in6 *)&ss);
356 getnameinfo((struct sockaddr *)&ss, ss.ss_len,
357 hbuf, sizeof(hbuf), NULL, 0,
359 asprintf(&tempchroot, "%s/%s", chroot_dir, hbuf);
361 statret = stat(tempchroot, &sb);
363 (statret == 0 && (sb.st_mode & S_IFDIR)))
364 chroot_dir = tempchroot;
366 /* Must get this before chroot because /etc might go away */
367 if ((nobody = getpwnam(chuser)) == NULL) {
368 tftp_log(LOG_ERR, "%s: no such user", chuser);
371 if (chroot(chroot_dir)) {
372 tftp_log(LOG_ERR, "chroot: %s: %s",
373 chroot_dir, strerror(errno));
376 if (chdir("/") != 0) {
377 tftp_log(LOG_ERR, "chdir: %s", strerror(errno));
380 if (setgroups(1, &nobody->pw_gid) != 0) {
381 tftp_log(LOG_ERR, "setgroups failed");
384 if (setuid(nobody->pw_uid) != 0) {
385 tftp_log(LOG_ERR, "setuid failed");
390 len = sizeof(me_sock);
391 if (getsockname(0, (struct sockaddr *)&me_sock, &len) == 0) {
392 switch (me_sock.ss_family) {
394 ((struct sockaddr_in *)&me_sock)->sin_port = 0;
397 ((struct sockaddr_in6 *)&me_sock)->sin6_port = 0;
404 memset(&me_sock, 0, sizeof(me_sock));
405 me_sock.ss_family = peer_sock.ss_family;
406 me_sock.ss_len = peer_sock.ss_len;
410 peer = socket(peer_sock.ss_family, SOCK_DGRAM, 0);
412 tftp_log(LOG_ERR, "socket: %s", strerror(errno));
415 if (bind(peer, (struct sockaddr *)&me_sock, me_sock.ss_len) < 0) {
416 tftp_log(LOG_ERR, "bind: %s", strerror(errno));
420 tp = (struct tftphdr *)recvbuffer;
421 tp->th_opcode = ntohs(tp->th_opcode);
422 if (tp->th_opcode == RRQ) {
424 tftp_rrq(peer, tp->th_stuff, n - 1);
426 tftp_log(LOG_WARNING,
427 "%s read access denied", peername);
430 } else if (tp->th_opcode == WRQ) {
432 tftp_wrq(peer, tp->th_stuff, n - 1);
434 tftp_log(LOG_WARNING,
435 "%s write access denied", peername);
439 send_error(peer, EBADOP);
444 reduce_path(char *fn)
448 /* Reduce all "/+./" to "/" (just in case we've got "/./../" later */
449 while ((slash = strstr(fn, "/./")) != NULL) {
450 for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--)
457 /* Now reduce all "/something/+../" to "/" */
458 while ((slash = strstr(fn, "/../")) != NULL) {
461 for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--)
463 for (ptr--; ptr >= fn; ptr--)
475 parse_header(int peer, char *recvbuffer, ssize_t size,
476 char **filename, char **mode)
485 i = get_field(peer, recvbuffer, size);
487 tftp_log(LOG_ERR, "Bad option - filename too long");
488 send_error(peer, EBADOP);
491 *filename = recvbuffer;
492 tftp_log(LOG_INFO, "Filename: '%s'", *filename);
495 i = get_field(peer, cp, size);
499 /* Find the file transfer mode */
500 for (cp = *mode; *cp; cp++)
503 for (pf = formats; pf->f_mode; pf++)
504 if (strcmp(pf->f_mode, *mode) == 0)
506 if (pf->f_mode == NULL) {
508 "Bad option - Unknown transfer mode (%s)", *mode);
509 send_error(peer, EBADOP);
512 tftp_log(LOG_INFO, "Mode: '%s'", *mode);
518 * WRQ - receive a file from the client
521 tftp_wrq(int peer, char *recvbuffer, ssize_t size)
524 int has_options = 0, ecode;
525 char *filename, *mode;
526 char fnbuf[PATH_MAX];
528 cp = parse_header(peer, recvbuffer, size, &filename, &mode);
529 size -= (cp - recvbuffer) + 1;
531 strlcpy(fnbuf, filename, sizeof(fnbuf));
536 if (options_rfc_enabled)
537 has_options = !parse_options(peer, cp, size);
539 tftp_log(LOG_INFO, "Options found but not enabled");
542 ecode = validate_access(peer, &filename, WRQ);
550 tftp_log(LOG_INFO, "%s: write request for %s: %s", peername,
551 filename, errtomsg(ecode));
555 send_error(peer, ecode);
558 tftp_recvfile(peer, mode);
563 * RRQ - send a file to the client
566 tftp_rrq(int peer, char *recvbuffer, ssize_t size)
569 int has_options = 0, ecode;
570 char *filename, *mode;
571 char fnbuf[PATH_MAX];
573 cp = parse_header(peer, recvbuffer, size, &filename, &mode);
574 size -= (cp - recvbuffer) + 1;
576 strlcpy(fnbuf, filename, sizeof(fnbuf));
581 if (options_rfc_enabled)
582 has_options = !parse_options(peer, cp, size);
584 tftp_log(LOG_INFO, "Options found but not enabled");
587 ecode = validate_access(peer, &filename, RRQ);
591 char lrecvbuffer[MAXPKTSIZE];
592 struct tftphdr *rp = (struct tftphdr *)lrecvbuffer;
595 n = receive_packet(peer, lrecvbuffer, MAXPKTSIZE,
596 NULL, timeoutpacket);
598 if (debug&DEBUG_SIMPLE)
599 tftp_log(LOG_DEBUG, "Aborting: %s",
603 if (rp->th_opcode != ACK) {
604 if (debug&DEBUG_SIMPLE)
606 "Expected ACK, got %s on OACK",
607 packettype(rp->th_opcode));
614 tftp_log(LOG_INFO, "%s: read request for %s: %s", peername,
615 filename, errtomsg(ecode));
619 * Avoid storms of naks to a RRQ broadcast for a relative
620 * bootfile pathname from a diskless Sun.
622 if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
624 send_error(peer, ecode);
627 tftp_xmitfile(peer, mode);
631 * Find the next value for YYYYMMDD.nn when the file to be written should
632 * be unique. Due to the limitations of nn, we will fail if nn reaches 100.
633 * Besides, that is four updates per hour on a file, which is kind of
637 find_next_name(char *filename, int *fd)
643 char yyyymmdd[MAXPATHLEN];
644 char newname[MAXPATHLEN];
646 /* Create the YYYYMMDD part of the filename */
648 lt = *localtime(&tval);
649 len = strftime(yyyymmdd, sizeof(yyyymmdd), newfile_format, <);
652 "Filename suffix too long (%d characters maximum)",
657 /* Make sure the new filename is not too long */
658 if (strlen(filename) > MAXPATHLEN - len - 5) {
660 "Filename too long (%zd characters, %zd maximum)",
661 strlen(filename), MAXPATHLEN - len - 5);
665 /* Find the first file which doesn't exist */
666 for (i = 0; i < 100; i++) {
667 sprintf(newname, "%s.%s.%02d", filename, yyyymmdd, i);
669 O_WRONLY | O_CREAT | O_EXCL,
670 S_IRUSR | S_IWUSR | S_IRGRP |
671 S_IWGRP | S_IROTH | S_IWOTH);
680 * Validate file access. Since we
681 * have no uid or gid, for now require
682 * file to exist and be publicly
684 * If we were invoked with arguments
685 * from inetd then the file must also be
686 * in one of the given directory prefixes.
687 * Note also, full path name must be
688 * given as we have no login directory.
691 validate_access(int peer, char **filep, int mode)
696 struct dirlist *dirp;
697 static char pathname[MAXPATHLEN];
698 char *filename = *filep;
701 * Prevent tricksters from getting around the directory restrictions
703 if (strstr(filename, "/../"))
706 if (*filename == '/') {
708 * Allow the request if it's in one of the approved locations.
709 * Special case: check the null prefix ("/") by looking
710 * for length = 1 and relying on the arg. processing that
713 for (dirp = dirs; dirp->name != NULL; dirp++) {
714 if (dirp->len == 1 ||
715 (!strncmp(filename, dirp->name, dirp->len) &&
716 filename[dirp->len] == '/'))
719 /* If directory list is empty, allow access to any file */
720 if (dirp->name == NULL && dirp != dirs)
722 if (stat(filename, &stbuf) < 0)
723 return (errno == ENOENT ? ENOTFOUND : EACCESS);
724 if ((stbuf.st_mode & S_IFMT) != S_IFREG)
727 if ((stbuf.st_mode & S_IROTH) == 0)
730 if ((stbuf.st_mode & S_IWOTH) == 0)
737 * Relative file name: search the approved locations for it.
738 * Don't allow write requests that avoid directory
742 if (!strncmp(filename, "../", 3))
746 * If the file exists in one of the directories and isn't
747 * readable, continue looking. However, change the error code
748 * to give an indication that the file exists.
751 for (dirp = dirs; dirp->name != NULL; dirp++) {
752 snprintf(pathname, sizeof(pathname), "%s/%s",
753 dirp->name, filename);
754 if (stat(pathname, &stbuf) == 0 &&
755 (stbuf.st_mode & S_IFMT) == S_IFREG) {
757 if ((stbuf.st_mode & S_IROTH) != 0)
760 if ((stbuf.st_mode & S_IWOTH) != 0)
766 if (dirp->name != NULL)
767 *filep = filename = pathname;
768 else if (mode == RRQ)
770 else if (err != ENOTFOUND || !create_new)
775 * This option is handled here because it (might) require(s) the
778 option_tsize(peer, NULL, mode, &stbuf);
781 fd = open(filename, O_RDONLY);
785 error = find_next_name(filename, &fd);
787 return (error + 100);
790 O_WRONLY | O_TRUNC | O_CREAT,
791 S_IRUSR | S_IWUSR | S_IRGRP |
792 S_IWGRP | S_IROTH | S_IWOTH );
794 fd = open(filename, O_WRONLY | O_TRUNC);
797 return (errno + 100);
798 file = fdopen(fd, (mode == RRQ)? "r":"w");
801 return (errno + 100);
807 tftp_xmitfile(int peer, const char *mode)
811 struct tftp_stats ts;
813 memset(&ts, 0, sizeof(ts));
815 if (debug&DEBUG_SIMPLE)
816 tftp_log(LOG_DEBUG, "Transmitting file");
818 read_init(0, file, mode);
820 tftp_send(peer, &block, &ts);
822 if (debug&DEBUG_SIMPLE)
823 tftp_log(LOG_INFO, "Sent %jd bytes in %jd seconds",
824 (intmax_t)ts.amount, (intmax_t)time(NULL) - now);
828 tftp_recvfile(int peer, const char *mode)
831 struct timeval now1, now2;
832 struct tftp_stats ts;
834 gettimeofday(&now1, NULL);
835 if (debug&DEBUG_SIMPLE)
836 tftp_log(LOG_DEBUG, "Receiving file");
838 write_init(0, file, mode);
841 tftp_receive(peer, &block, &ts, NULL, 0);
843 gettimeofday(&now2, NULL);
845 if (debug&DEBUG_SIMPLE) {
847 if (now1.tv_usec > now2.tv_usec) {
848 now2.tv_usec += 1000000;
852 f = now2.tv_sec - now1.tv_sec +
853 (now2.tv_usec - now1.tv_usec) / 100000.0;
855 "Download of %jd bytes in %d blocks completed after %0.1f seconds\n",
856 (intmax_t)ts.amount, block, f);