1*8a16b7a1SPedro F. Giffuni /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1983, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 326dfc2060SDavid Malone #if 0 339b50d902SRodney W. Grimes #ifndef lint 346dfc2060SDavid Malone static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; 356dfc2060SDavid Malone #endif /* not lint */ 36fd129a02SPhilippe Charnier #endif 379b50d902SRodney W. Grimes 386dfc2060SDavid Malone #include <sys/cdefs.h> 396dfc2060SDavid Malone __FBSDID("$FreeBSD$"); 406dfc2060SDavid Malone 419b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ 429b50d902SRodney W. Grimes 439b50d902SRodney W. Grimes /* 449b50d902SRodney W. Grimes * TFTP User Program -- Protocol Machines 459b50d902SRodney W. Grimes */ 469b50d902SRodney W. Grimes #include <sys/socket.h> 47752fa694SWarner Losh #include <sys/stat.h> 489b50d902SRodney W. Grimes 499b50d902SRodney W. Grimes #include <netinet/in.h> 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes #include <arpa/tftp.h> 529b50d902SRodney W. Grimes 53fd129a02SPhilippe Charnier #include <err.h> 544dac6235SHajimu UMEMOTO #include <netdb.h> 55752fa694SWarner Losh #include <stdio.h> 56752fa694SWarner Losh #include <stdlib.h> 57752fa694SWarner Losh #include <string.h> 58752fa694SWarner Losh #include <syslog.h> 599b50d902SRodney W. Grimes 60752fa694SWarner Losh #include "tftp.h" 61752fa694SWarner Losh #include "tftp-file.h" 62752fa694SWarner Losh #include "tftp-utils.h" 63752fa694SWarner Losh #include "tftp-io.h" 64752fa694SWarner Losh #include "tftp-transfer.h" 65752fa694SWarner Losh #include "tftp-options.h" 669b50d902SRodney W. Grimes 679b50d902SRodney W. Grimes /* 689b50d902SRodney W. Grimes * Send the requested file. 699b50d902SRodney W. Grimes */ 709b50d902SRodney W. Grimes void 71752fa694SWarner Losh xmitfile(int peer, char *port, int fd, char *name, char *mode) 729b50d902SRodney W. Grimes { 73752fa694SWarner Losh struct tftphdr *rp; 74752fa694SWarner Losh int n, i; 75752fa694SWarner Losh uint16_t block; 764dac6235SHajimu UMEMOTO struct sockaddr_storage serv; /* valid server port number */ 77752fa694SWarner Losh char recvbuffer[MAXPKTSIZE]; 78752fa694SWarner Losh struct tftp_stats tftp_stats; 799b50d902SRodney W. Grimes 80752fa694SWarner Losh stats_init(&tftp_stats); 81752fa694SWarner Losh 824dac6235SHajimu UMEMOTO memset(&serv, 0, sizeof(serv)); 83752fa694SWarner Losh rp = (struct tftphdr *)recvbuffer; 849b50d902SRodney W. Grimes 85752fa694SWarner Losh if (port == NULL) { 86752fa694SWarner Losh struct servent *se; 87752fa694SWarner Losh se = getservbyname("tftp", "udp"); 88752fa694SWarner Losh ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port; 89752fa694SWarner Losh } else 90752fa694SWarner Losh ((struct sockaddr_in *)&peer_sock)->sin_port = 91752fa694SWarner Losh htons(atoi(port)); 929b50d902SRodney W. Grimes 93752fa694SWarner Losh for (i = 0; i < 12; i++) { 94752fa694SWarner Losh struct sockaddr_storage from; 95752fa694SWarner Losh 96752fa694SWarner Losh /* Tell the other side what we want to do */ 97752fa694SWarner Losh if (debug&DEBUG_SIMPLE) 98752fa694SWarner Losh printf("Sending %s\n", name); 99752fa694SWarner Losh 100752fa694SWarner Losh n = send_wrq(peer, name, mode); 101752fa694SWarner Losh if (n > 0) { 102752fa694SWarner Losh printf("Cannot send WRQ packet\n"); 103752fa694SWarner Losh return; 1049b50d902SRodney W. Grimes } 105752fa694SWarner Losh 106752fa694SWarner Losh /* 107752fa694SWarner Losh * The first packet we receive has the new destination port 108752fa694SWarner Losh * we have to send the next packets to. 1099b50d902SRodney W. Grimes */ 110752fa694SWarner Losh n = receive_packet(peer, recvbuffer, 111752fa694SWarner Losh MAXPKTSIZE, &from, timeoutpacket); 112752fa694SWarner Losh 113752fa694SWarner Losh /* We got some data! */ 114752fa694SWarner Losh if (n >= 0) { 115752fa694SWarner Losh ((struct sockaddr_in *)&peer_sock)->sin_port = 116752fa694SWarner Losh ((struct sockaddr_in *)&from)->sin_port; 117752fa694SWarner Losh break; 1189b50d902SRodney W. Grimes } 119752fa694SWarner Losh 120752fa694SWarner Losh /* This should be retried */ 121752fa694SWarner Losh if (n == RP_TIMEOUT) { 122752fa694SWarner Losh printf("Try %d, didn't receive answer from remote.\n", 123752fa694SWarner Losh i + 1); 124752fa694SWarner Losh continue; 1259b50d902SRodney W. Grimes } 126752fa694SWarner Losh 127752fa694SWarner Losh /* Everything else is fatal */ 128752fa694SWarner Losh break; 1299b50d902SRodney W. Grimes } 130752fa694SWarner Losh if (i == 12) { 131752fa694SWarner Losh printf("Transfer timed out.\n"); 132752fa694SWarner Losh return; 1339b50d902SRodney W. Grimes } 134752fa694SWarner Losh if (rp->th_opcode == ERROR) { 135752fa694SWarner Losh printf("Got ERROR, aborted\n"); 136752fa694SWarner Losh return; 137752fa694SWarner Losh } 138752fa694SWarner Losh 139752fa694SWarner Losh /* 140752fa694SWarner Losh * If the first packet is an OACK instead of an ACK packet, 141752fa694SWarner Losh * handle it different. 142752fa694SWarner Losh */ 143752fa694SWarner Losh if (rp->th_opcode == OACK) { 144752fa694SWarner Losh if (!options_rfc_enabled) { 145752fa694SWarner Losh printf("Got OACK while options are not enabled!\n"); 146752fa694SWarner Losh send_error(peer, EBADOP); 147752fa694SWarner Losh return; 148752fa694SWarner Losh } 149752fa694SWarner Losh 150752fa694SWarner Losh parse_options(peer, rp->th_stuff, n + 2); 151752fa694SWarner Losh } 152752fa694SWarner Losh 153752fa694SWarner Losh if (read_init(fd, NULL, mode) < 0) { 154752fa694SWarner Losh warn("read_init()"); 155752fa694SWarner Losh return; 156752fa694SWarner Losh } 157752fa694SWarner Losh 158752fa694SWarner Losh block = 1; 159752fa694SWarner Losh tftp_send(peer, &block, &tftp_stats); 160752fa694SWarner Losh 161752fa694SWarner Losh read_close(); 162c54da672SGavin Atkinson if (tftp_stats.amount > 0) 163752fa694SWarner Losh printstats("Sent", verbose, &tftp_stats); 164752fa694SWarner Losh 165752fa694SWarner Losh txrx_error = 1; 1669b50d902SRodney W. Grimes } 1679b50d902SRodney W. Grimes 1689b50d902SRodney W. Grimes /* 1699b50d902SRodney W. Grimes * Receive a file. 1709b50d902SRodney W. Grimes */ 1719b50d902SRodney W. Grimes void 172752fa694SWarner Losh recvfile(int peer, char *port, int fd, char *name, char *mode) 1739b50d902SRodney W. Grimes { 174752fa694SWarner Losh struct tftphdr *rp; 175752fa694SWarner Losh uint16_t block; 176752fa694SWarner Losh char recvbuffer[MAXPKTSIZE]; 177752fa694SWarner Losh int n, i; 178752fa694SWarner Losh struct tftp_stats tftp_stats; 179752fa694SWarner Losh 180752fa694SWarner Losh stats_init(&tftp_stats); 181752fa694SWarner Losh 182752fa694SWarner Losh rp = (struct tftphdr *)recvbuffer; 183752fa694SWarner Losh 184752fa694SWarner Losh if (port == NULL) { 185752fa694SWarner Losh struct servent *se; 186752fa694SWarner Losh se = getservbyname("tftp", "udp"); 187752fa694SWarner Losh ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port; 188752fa694SWarner Losh } else 189752fa694SWarner Losh ((struct sockaddr_in *)&peer_sock)->sin_port = 190752fa694SWarner Losh htons(atoi(port)); 191752fa694SWarner Losh 192752fa694SWarner Losh for (i = 0; i < 12; i++) { 1934dac6235SHajimu UMEMOTO struct sockaddr_storage from; 1949b50d902SRodney W. Grimes 195752fa694SWarner Losh /* Tell the other side what we want to do */ 196752fa694SWarner Losh if (debug&DEBUG_SIMPLE) 197752fa694SWarner Losh printf("Requesting %s\n", name); 1989b50d902SRodney W. Grimes 199752fa694SWarner Losh n = send_rrq(peer, name, mode); 200752fa694SWarner Losh if (n > 0) { 201752fa694SWarner Losh printf("Cannot send RRQ packet\n"); 202752fa694SWarner Losh return; 2039b50d902SRodney W. Grimes } 2049b50d902SRodney W. Grimes 2059b50d902SRodney W. Grimes /* 206752fa694SWarner Losh * The first packet we receive has the new destination port 207752fa694SWarner Losh * we have to send the next packets to. 2089b50d902SRodney W. Grimes */ 209752fa694SWarner Losh n = receive_packet(peer, recvbuffer, 210752fa694SWarner Losh MAXPKTSIZE, &from, timeoutpacket); 2119b50d902SRodney W. Grimes 212752fa694SWarner Losh /* We got something useful! */ 213752fa694SWarner Losh if (n >= 0) { 214752fa694SWarner Losh ((struct sockaddr_in *)&peer_sock)->sin_port = 215752fa694SWarner Losh ((struct sockaddr_in *)&from)->sin_port; 2169b50d902SRodney W. Grimes break; 2179b50d902SRodney W. Grimes } 218752fa694SWarner Losh 219752fa694SWarner Losh /* We should retry if this happens */ 220752fa694SWarner Losh if (n == RP_TIMEOUT) { 221752fa694SWarner Losh printf("Try %d, didn't receive answer from remote.\n", 222752fa694SWarner Losh i + 1); 223752fa694SWarner Losh continue; 2249b50d902SRodney W. Grimes } 2259b50d902SRodney W. Grimes 226752fa694SWarner Losh /* Otherwise it is a fatal error */ 227752fa694SWarner Losh break; 2289b50d902SRodney W. Grimes } 229a1ec94b8SGavin Atkinson if (i == 12) { 230a1ec94b8SGavin Atkinson printf("Transfer timed out.\n"); 231a1ec94b8SGavin Atkinson return; 232a1ec94b8SGavin Atkinson } 233752fa694SWarner Losh if (rp->th_opcode == ERROR) { 234752fa694SWarner Losh tftp_log(LOG_ERR, "Error code %d: %s", rp->th_code, rp->th_msg); 235752fa694SWarner Losh return; 2369b50d902SRodney W. Grimes } 2379b50d902SRodney W. Grimes 238752fa694SWarner Losh if (write_init(fd, NULL, mode) < 0) { 239752fa694SWarner Losh warn("write_init"); 240752fa694SWarner Losh return; 2419b50d902SRodney W. Grimes } 2429b50d902SRodney W. Grimes 243752fa694SWarner Losh /* 244752fa694SWarner Losh * If the first packet is an OACK packet instead of an DATA packet, 245752fa694SWarner Losh * handle it different. 246752fa694SWarner Losh */ 247752fa694SWarner Losh if (rp->th_opcode == OACK) { 248752fa694SWarner Losh if (!options_rfc_enabled) { 249752fa694SWarner Losh printf("Got OACK while options are not enabled!\n"); 250752fa694SWarner Losh send_error(peer, EBADOP); 251752fa694SWarner Losh return; 2529b50d902SRodney W. Grimes } 2534dac6235SHajimu UMEMOTO 254752fa694SWarner Losh parse_options(peer, rp->th_stuff, n + 2); 2554dac6235SHajimu UMEMOTO 256752fa694SWarner Losh n = send_ack(peer, 0); 257752fa694SWarner Losh if (n > 0) { 258752fa694SWarner Losh printf("Cannot send ACK on OACK.\n"); 259752fa694SWarner Losh return; 260752fa694SWarner Losh } 261752fa694SWarner Losh block = 0; 262752fa694SWarner Losh tftp_receive(peer, &block, &tftp_stats, NULL, 0); 263752fa694SWarner Losh } else { 264752fa694SWarner Losh block = 1; 265752fa694SWarner Losh tftp_receive(peer, &block, &tftp_stats, rp, n); 266752fa694SWarner Losh } 2674dac6235SHajimu UMEMOTO 268752fa694SWarner Losh write_close(); 269752fa694SWarner Losh if (tftp_stats.amount > 0) 270752fa694SWarner Losh printstats("Received", verbose, &tftp_stats); 271752fa694SWarner Losh return; 2724dac6235SHajimu UMEMOTO } 273