xref: /freebsd/usr.bin/tftp/tftp.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro 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 #include <sys/cdefs.h>
339b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
349b50d902SRodney W. Grimes 
359b50d902SRodney W. Grimes /*
369b50d902SRodney W. Grimes  * TFTP User Program -- Protocol Machines
379b50d902SRodney W. Grimes  */
389b50d902SRodney W. Grimes #include <sys/socket.h>
39752fa694SWarner Losh #include <sys/stat.h>
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes #include <netinet/in.h>
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <arpa/tftp.h>
449b50d902SRodney W. Grimes 
45ca2d3691SAlan Somers #include <assert.h>
46fd129a02SPhilippe Charnier #include <err.h>
474dac6235SHajimu UMEMOTO #include <netdb.h>
48752fa694SWarner Losh #include <stdio.h>
49752fa694SWarner Losh #include <stdlib.h>
50752fa694SWarner Losh #include <string.h>
51752fa694SWarner Losh #include <syslog.h>
529b50d902SRodney W. Grimes 
53752fa694SWarner Losh #include "tftp.h"
54752fa694SWarner Losh #include "tftp-file.h"
55752fa694SWarner Losh #include "tftp-utils.h"
56752fa694SWarner Losh #include "tftp-io.h"
57752fa694SWarner Losh #include "tftp-transfer.h"
58752fa694SWarner Losh #include "tftp-options.h"
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes /*
619b50d902SRodney W. Grimes  * Send the requested file.
629b50d902SRodney W. Grimes  */
63*92570f67SDag-Erling Smørgrav int
xmitfile(int peer,char * port,int fd,char * name,char * mode)64752fa694SWarner Losh xmitfile(int peer, char *port, int fd, char *name, char *mode)
659b50d902SRodney W. Grimes {
66752fa694SWarner Losh 	struct tftphdr *rp;
67*92570f67SDag-Erling Smørgrav 	int n, i, ret = 0;
68752fa694SWarner Losh 	uint16_t block;
694dac6235SHajimu UMEMOTO 	struct sockaddr_storage serv;	/* valid server port number */
70752fa694SWarner Losh 	char recvbuffer[MAXPKTSIZE];
71752fa694SWarner Losh 	struct tftp_stats tftp_stats;
729b50d902SRodney W. Grimes 
73752fa694SWarner Losh 	stats_init(&tftp_stats);
74752fa694SWarner Losh 
754dac6235SHajimu UMEMOTO 	memset(&serv, 0, sizeof(serv));
76752fa694SWarner Losh 	rp = (struct tftphdr *)recvbuffer;
779b50d902SRodney W. Grimes 
78752fa694SWarner Losh 	if (port == NULL) {
79752fa694SWarner Losh 		struct servent *se;
80752fa694SWarner Losh 		se = getservbyname("tftp", "udp");
81ca2d3691SAlan Somers 		assert(se != NULL);
82752fa694SWarner Losh 		((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port;
83752fa694SWarner Losh 	} else
84752fa694SWarner Losh 		((struct sockaddr_in *)&peer_sock)->sin_port =
85752fa694SWarner Losh 		    htons(atoi(port));
869b50d902SRodney W. Grimes 
87752fa694SWarner Losh 	for (i = 0; i < 12; i++) {
88752fa694SWarner Losh 		struct sockaddr_storage from;
89752fa694SWarner Losh 
90752fa694SWarner Losh 		/* Tell the other side what we want to do */
91752fa694SWarner Losh 		if (debug & DEBUG_SIMPLE)
92752fa694SWarner Losh 			printf("Sending %s\n", name);
93752fa694SWarner Losh 
94752fa694SWarner Losh 		n = send_wrq(peer, name, mode);
95752fa694SWarner Losh 		if (n > 0) {
96752fa694SWarner Losh 			printf("Cannot send WRQ packet\n");
97*92570f67SDag-Erling Smørgrav 			return -1;
989b50d902SRodney W. Grimes 		}
99752fa694SWarner Losh 
100752fa694SWarner Losh 		/*
101752fa694SWarner Losh 		 * The first packet we receive has the new destination port
102752fa694SWarner Losh 		 * we have to send the next packets to.
1039b50d902SRodney W. Grimes 		 */
104752fa694SWarner Losh 		n = receive_packet(peer, recvbuffer,
105752fa694SWarner Losh 		    MAXPKTSIZE, &from, timeoutpacket);
106752fa694SWarner Losh 
107752fa694SWarner Losh 		/* We got some data! */
108752fa694SWarner Losh 		if (n >= 0) {
109752fa694SWarner Losh 			((struct sockaddr_in *)&peer_sock)->sin_port =
110752fa694SWarner Losh 			    ((struct sockaddr_in *)&from)->sin_port;
111752fa694SWarner Losh 			break;
1129b50d902SRodney W. Grimes 		}
113752fa694SWarner Losh 
114752fa694SWarner Losh 		/* This should be retried */
115752fa694SWarner Losh 		if (n == RP_TIMEOUT) {
116752fa694SWarner Losh 			printf("Try %d, didn't receive answer from remote.\n",
117752fa694SWarner Losh 			    i + 1);
118752fa694SWarner Losh 			continue;
1199b50d902SRodney W. Grimes 		}
120752fa694SWarner Losh 
121752fa694SWarner Losh 		/* Everything else is fatal */
122752fa694SWarner Losh 		break;
1239b50d902SRodney W. Grimes 	}
124752fa694SWarner Losh 	if (i == 12) {
125752fa694SWarner Losh 		printf("Transfer timed out.\n");
126*92570f67SDag-Erling Smørgrav 		return -1;
1279b50d902SRodney W. Grimes 	}
128752fa694SWarner Losh 	if (rp->th_opcode == ERROR) {
129752fa694SWarner Losh 		printf("Got ERROR, aborted\n");
130*92570f67SDag-Erling Smørgrav 		return -1;
131752fa694SWarner Losh 	}
132752fa694SWarner Losh 
133752fa694SWarner Losh 	/*
134752fa694SWarner Losh 	 * If the first packet is an OACK instead of an ACK packet,
135752fa694SWarner Losh 	 * handle it different.
136752fa694SWarner Losh 	 */
137752fa694SWarner Losh 	if (rp->th_opcode == OACK) {
138752fa694SWarner Losh 		if (!options_rfc_enabled) {
139752fa694SWarner Losh 			printf("Got OACK while options are not enabled!\n");
140752fa694SWarner Losh 			send_error(peer, EBADOP);
141*92570f67SDag-Erling Smørgrav 			return -1;
142752fa694SWarner Losh 		}
143752fa694SWarner Losh 
144752fa694SWarner Losh 		parse_options(peer, rp->th_stuff, n + 2);
145752fa694SWarner Losh 	}
146752fa694SWarner Losh 
147752fa694SWarner Losh 	if (read_init(fd, NULL, mode) < 0) {
148752fa694SWarner Losh 		warn("read_init()");
149*92570f67SDag-Erling Smørgrav 		return -1;
150752fa694SWarner Losh 	}
151752fa694SWarner Losh 
152752fa694SWarner Losh 	block = 1;
153*92570f67SDag-Erling Smørgrav 	if (tftp_send(peer, &block, &tftp_stats) != 0)
154*92570f67SDag-Erling Smørgrav 		ret = -1;
155752fa694SWarner Losh 
156752fa694SWarner Losh 	read_close();
157c54da672SGavin Atkinson 	if (tftp_stats.amount > 0)
158752fa694SWarner Losh 		printstats("Sent", verbose, &tftp_stats);
159*92570f67SDag-Erling Smørgrav 	return ret;
1609b50d902SRodney W. Grimes }
1619b50d902SRodney W. Grimes 
1629b50d902SRodney W. Grimes /*
1639b50d902SRodney W. Grimes  * Receive a file.
1649b50d902SRodney W. Grimes  */
165*92570f67SDag-Erling Smørgrav int
recvfile(int peer,char * port,int fd,char * name,char * mode)166752fa694SWarner Losh recvfile(int peer, char *port, int fd, char *name, char *mode)
1679b50d902SRodney W. Grimes {
168752fa694SWarner Losh 	struct tftphdr *rp;
169752fa694SWarner Losh 	uint16_t block;
170752fa694SWarner Losh 	char recvbuffer[MAXPKTSIZE];
171*92570f67SDag-Erling Smørgrav 	int n, i, ret = 0;
172752fa694SWarner Losh 	struct tftp_stats tftp_stats;
173752fa694SWarner Losh 
174752fa694SWarner Losh 	stats_init(&tftp_stats);
175752fa694SWarner Losh 
176752fa694SWarner Losh 	rp = (struct tftphdr *)recvbuffer;
177752fa694SWarner Losh 
178752fa694SWarner Losh 	if (port == NULL) {
179752fa694SWarner Losh 		struct servent *se;
180752fa694SWarner Losh 		se = getservbyname("tftp", "udp");
181ca2d3691SAlan Somers 		assert(se != NULL);
182752fa694SWarner Losh 		((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port;
183752fa694SWarner Losh 	} else
184752fa694SWarner Losh 		((struct sockaddr_in *)&peer_sock)->sin_port =
185752fa694SWarner Losh 		    htons(atoi(port));
186752fa694SWarner Losh 
187752fa694SWarner Losh 	for (i = 0; i < 12; i++) {
1884dac6235SHajimu UMEMOTO 		struct sockaddr_storage from;
1899b50d902SRodney W. Grimes 
190752fa694SWarner Losh 		/* Tell the other side what we want to do */
191752fa694SWarner Losh 		if (debug & DEBUG_SIMPLE)
192752fa694SWarner Losh 			printf("Requesting %s\n", name);
1939b50d902SRodney W. Grimes 
194752fa694SWarner Losh 		n = send_rrq(peer, name, mode);
195752fa694SWarner Losh 		if (n > 0) {
196752fa694SWarner Losh 			printf("Cannot send RRQ packet\n");
197*92570f67SDag-Erling Smørgrav 			return -1;
1989b50d902SRodney W. Grimes 		}
1999b50d902SRodney W. Grimes 
2009b50d902SRodney W. Grimes 		/*
201752fa694SWarner Losh 		 * The first packet we receive has the new destination port
202752fa694SWarner Losh 		 * we have to send the next packets to.
2039b50d902SRodney W. Grimes 		 */
204752fa694SWarner Losh 		n = receive_packet(peer, recvbuffer,
205752fa694SWarner Losh 		    MAXPKTSIZE, &from, timeoutpacket);
2069b50d902SRodney W. Grimes 
207752fa694SWarner Losh 		/* We got something useful! */
208752fa694SWarner Losh 		if (n >= 0) {
209752fa694SWarner Losh 			((struct sockaddr_in *)&peer_sock)->sin_port =
210752fa694SWarner Losh 			    ((struct sockaddr_in *)&from)->sin_port;
2119b50d902SRodney W. Grimes 			break;
2129b50d902SRodney W. Grimes 		}
213752fa694SWarner Losh 
214752fa694SWarner Losh 		/* We should retry if this happens */
215752fa694SWarner Losh 		if (n == RP_TIMEOUT) {
216752fa694SWarner Losh 			printf("Try %d, didn't receive answer from remote.\n",
217752fa694SWarner Losh 			    i + 1);
218752fa694SWarner Losh 			continue;
2199b50d902SRodney W. Grimes 		}
2209b50d902SRodney W. Grimes 
221752fa694SWarner Losh 		/* Otherwise it is a fatal error */
222752fa694SWarner Losh 		break;
2239b50d902SRodney W. Grimes 	}
224a1ec94b8SGavin Atkinson 	if (i == 12) {
225a1ec94b8SGavin Atkinson 		printf("Transfer timed out.\n");
226*92570f67SDag-Erling Smørgrav 		return -1;
227a1ec94b8SGavin Atkinson 	}
228752fa694SWarner Losh 	if (rp->th_opcode == ERROR) {
229752fa694SWarner Losh 		tftp_log(LOG_ERR, "Error code %d: %s", rp->th_code, rp->th_msg);
230*92570f67SDag-Erling Smørgrav 		return -1;
2319b50d902SRodney W. Grimes 	}
2329b50d902SRodney W. Grimes 
233752fa694SWarner Losh 	if (write_init(fd, NULL, mode) < 0) {
234752fa694SWarner Losh 		warn("write_init");
235*92570f67SDag-Erling Smørgrav 		return -1;
2369b50d902SRodney W. Grimes 	}
2379b50d902SRodney W. Grimes 
238752fa694SWarner Losh 	/*
239752fa694SWarner Losh 	 * If the first packet is an OACK packet instead of an DATA packet,
240752fa694SWarner Losh 	 * handle it different.
241752fa694SWarner Losh 	 */
242752fa694SWarner Losh 	if (rp->th_opcode == OACK) {
243752fa694SWarner Losh 		if (!options_rfc_enabled) {
244752fa694SWarner Losh 			printf("Got OACK while options are not enabled!\n");
245752fa694SWarner Losh 			send_error(peer, EBADOP);
246*92570f67SDag-Erling Smørgrav 			return -1;
2479b50d902SRodney W. Grimes 		}
2484dac6235SHajimu UMEMOTO 
249752fa694SWarner Losh 		parse_options(peer, rp->th_stuff, n + 2);
2504dac6235SHajimu UMEMOTO 
251752fa694SWarner Losh 		n = send_ack(peer, 0);
252752fa694SWarner Losh 		if (n > 0) {
253752fa694SWarner Losh 			printf("Cannot send ACK on OACK.\n");
254*92570f67SDag-Erling Smørgrav 			return -1;
255752fa694SWarner Losh 		}
256752fa694SWarner Losh 		block = 0;
257*92570f67SDag-Erling Smørgrav 		if (tftp_receive(peer, &block, &tftp_stats, NULL, 0) != 0)
258*92570f67SDag-Erling Smørgrav 			ret = -1;
259752fa694SWarner Losh 	} else {
260752fa694SWarner Losh 		block = 1;
261*92570f67SDag-Erling Smørgrav 		if (tftp_receive(peer, &block, &tftp_stats, rp, n) != 0)
262*92570f67SDag-Erling Smørgrav 			ret = -1;
263752fa694SWarner Losh 	}
2644dac6235SHajimu UMEMOTO 
265752fa694SWarner Losh 	if (tftp_stats.amount > 0)
266752fa694SWarner Losh 		printstats("Received", verbose, &tftp_stats);
267*92570f67SDag-Erling Smørgrav 	return ret;
2684dac6235SHajimu UMEMOTO }
269