xref: /freebsd/libexec/tftpd/tftp-options.c (revision fdf929ff)
1e6209940SPedro F. Giffuni /*-
2e6209940SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3e6209940SPedro F. Giffuni  *
4e7ff5475SWarner Losh  * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
5e7ff5475SWarner Losh  *
6e7ff5475SWarner Losh  * Redistribution and use in source and binary forms, with or without
7e7ff5475SWarner Losh  * modification, are permitted provided that the following conditions
8e7ff5475SWarner Losh  * are met:
9e7ff5475SWarner Losh  * 1. Redistributions of source code must retain the above copyright
10e7ff5475SWarner Losh  *    notice, this list of conditions and the following disclaimer.
11e7ff5475SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
12e7ff5475SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
13e7ff5475SWarner Losh  *    documentation and/or other materials provided with the distribution.
14e7ff5475SWarner Losh  *
15e7ff5475SWarner Losh  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16e7ff5475SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e7ff5475SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e7ff5475SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19e7ff5475SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e7ff5475SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21e7ff5475SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e7ff5475SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e7ff5475SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e7ff5475SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e7ff5475SWarner Losh  * SUCH DAMAGE.
26e7ff5475SWarner Losh  */
27e7ff5475SWarner Losh 
28e7ff5475SWarner Losh #include <sys/cdefs.h>
29e7ff5475SWarner Losh __FBSDID("$FreeBSD$");
30e7ff5475SWarner Losh 
31e7ff5475SWarner Losh #include <sys/socket.h>
32e7ff5475SWarner Losh #include <sys/types.h>
33e7ff5475SWarner Losh #include <sys/sysctl.h>
34e7ff5475SWarner Losh #include <sys/stat.h>
35e7ff5475SWarner Losh 
36e7ff5475SWarner Losh #include <netinet/in.h>
37e7ff5475SWarner Losh #include <arpa/tftp.h>
38e7ff5475SWarner Losh 
39e7ff5475SWarner Losh #include <ctype.h>
40e7ff5475SWarner Losh #include <stdio.h>
41e7ff5475SWarner Losh #include <stdlib.h>
42e7ff5475SWarner Losh #include <string.h>
43e7ff5475SWarner Losh #include <syslog.h>
44e7ff5475SWarner Losh 
45e7ff5475SWarner Losh #include "tftp-utils.h"
46e7ff5475SWarner Losh #include "tftp-io.h"
47e7ff5475SWarner Losh #include "tftp-options.h"
48e7ff5475SWarner Losh 
49e7ff5475SWarner Losh /*
50e7ff5475SWarner Losh  * Option handlers
51e7ff5475SWarner Losh  */
52e7ff5475SWarner Losh 
53e7ff5475SWarner Losh struct options options[] = {
54e7ff5475SWarner Losh 	{ "tsize",	NULL, NULL, NULL /* option_tsize */, 1 },
55e7ff5475SWarner Losh 	{ "timeout",	NULL, NULL, option_timeout, 1 },
56e7ff5475SWarner Losh 	{ "blksize",	NULL, NULL, option_blksize, 1 },
57e7ff5475SWarner Losh 	{ "blksize2",	NULL, NULL, option_blksize2, 0 },
58e7ff5475SWarner Losh 	{ "rollover",	NULL, NULL, option_rollover, 0 },
59fdf929ffSJohn Baldwin 	{ "windowsize",	NULL, NULL, option_windowsize, 1 },
60e7ff5475SWarner Losh 	{ NULL,		NULL, NULL, NULL, 0 }
61e7ff5475SWarner Losh };
62e7ff5475SWarner Losh 
63e7ff5475SWarner Losh /* By default allow them */
64e7ff5475SWarner Losh int options_rfc_enabled = 1;
65e7ff5475SWarner Losh int options_extra_enabled = 1;
66e7ff5475SWarner Losh 
67e7ff5475SWarner Losh /*
68e7ff5475SWarner Losh  * Rules for the option handlers:
69e7ff5475SWarner Losh  * - If there is no o_request, there will be no processing.
70e7ff5475SWarner Losh  *
71e7ff5475SWarner Losh  * For servers
72e7ff5475SWarner Losh  * - Logging is done as warnings.
73e7ff5475SWarner Losh  * - The handler exit()s if there is a serious problem with the
74e7ff5475SWarner Losh  *   values submitted in the option.
75e7ff5475SWarner Losh  *
76e7ff5475SWarner Losh  * For clients
77e7ff5475SWarner Losh  * - Logging is done as errors. After all, the server shouldn't
78e7ff5475SWarner Losh  *   return rubbish.
79e7ff5475SWarner Losh  * - The handler returns if there is a serious problem with the
80e7ff5475SWarner Losh  *   values submitted in the option.
81e7ff5475SWarner Losh  * - Sending the EBADOP packets is done by the handler.
82e7ff5475SWarner Losh  */
83e7ff5475SWarner Losh 
84e7ff5475SWarner Losh int
8504ebad38SMarius Strobl option_tsize(int peer __unused, struct tftphdr *tp __unused, int mode,
8604ebad38SMarius Strobl     struct stat *stbuf)
87e7ff5475SWarner Losh {
88e7ff5475SWarner Losh 
89e7ff5475SWarner Losh 	if (options[OPT_TSIZE].o_request == NULL)
90e7ff5475SWarner Losh 		return (0);
91e7ff5475SWarner Losh 
92e7ff5475SWarner Losh 	if (mode == RRQ)
93e7ff5475SWarner Losh 		asprintf(&options[OPT_TSIZE].o_reply,
94e7ff5475SWarner Losh 			"%ju", stbuf->st_size);
95e7ff5475SWarner Losh 	else
96e7ff5475SWarner Losh 		/* XXX Allows writes of all sizes. */
97e7ff5475SWarner Losh 		options[OPT_TSIZE].o_reply =
98e7ff5475SWarner Losh 			strdup(options[OPT_TSIZE].o_request);
99e7ff5475SWarner Losh 	return (0);
100e7ff5475SWarner Losh }
101e7ff5475SWarner Losh 
102e7ff5475SWarner Losh int
103e7ff5475SWarner Losh option_timeout(int peer)
104e7ff5475SWarner Losh {
105b713097aSMarius Strobl 	int to;
106e7ff5475SWarner Losh 
107e7ff5475SWarner Losh 	if (options[OPT_TIMEOUT].o_request == NULL)
108e7ff5475SWarner Losh 		return (0);
109e7ff5475SWarner Losh 
110b713097aSMarius Strobl 	to = atoi(options[OPT_TIMEOUT].o_request);
111e7ff5475SWarner Losh 	if (to < TIMEOUT_MIN || to > TIMEOUT_MAX) {
112e7ff5475SWarner Losh 		tftp_log(acting_as_client ? LOG_ERR : LOG_WARNING,
113e7ff5475SWarner Losh 		    "Received bad value for timeout. "
114b713097aSMarius Strobl 		    "Should be between %d and %d, received %d",
115b713097aSMarius Strobl 		    TIMEOUT_MIN, TIMEOUT_MAX, to);
116e7ff5475SWarner Losh 		send_error(peer, EBADOP);
117e7ff5475SWarner Losh 		if (acting_as_client)
118e7ff5475SWarner Losh 			return (1);
119e7ff5475SWarner Losh 		exit(1);
120e7ff5475SWarner Losh 	} else {
121e7ff5475SWarner Losh 		timeoutpacket = to;
122e7ff5475SWarner Losh 		options[OPT_TIMEOUT].o_reply =
123e7ff5475SWarner Losh 			strdup(options[OPT_TIMEOUT].o_request);
124e7ff5475SWarner Losh 	}
125e7ff5475SWarner Losh 	settimeouts(timeoutpacket, timeoutnetwork, maxtimeouts);
126e7ff5475SWarner Losh 
127e7ff5475SWarner Losh 	if (debug&DEBUG_OPTIONS)
128e7ff5475SWarner Losh 		tftp_log(LOG_DEBUG, "Setting timeout to '%s'",
129e7ff5475SWarner Losh 			options[OPT_TIMEOUT].o_reply);
130e7ff5475SWarner Losh 
131e7ff5475SWarner Losh 	return (0);
132e7ff5475SWarner Losh }
133e7ff5475SWarner Losh 
134e7ff5475SWarner Losh int
135e7ff5475SWarner Losh option_rollover(int peer)
136e7ff5475SWarner Losh {
137e7ff5475SWarner Losh 
138e7ff5475SWarner Losh 	if (options[OPT_ROLLOVER].o_request == NULL)
139e7ff5475SWarner Losh 		return (0);
140e7ff5475SWarner Losh 
141e7ff5475SWarner Losh 	if (strcmp(options[OPT_ROLLOVER].o_request, "0") != 0
142e7ff5475SWarner Losh 	 && strcmp(options[OPT_ROLLOVER].o_request, "1") != 0) {
143e7ff5475SWarner Losh 		tftp_log(acting_as_client ? LOG_ERR : LOG_WARNING,
144e7ff5475SWarner Losh 		    "Bad value for rollover, "
145e7ff5475SWarner Losh 		    "should be either 0 or 1, received '%s', "
146e7ff5475SWarner Losh 		    "ignoring request",
147e7ff5475SWarner Losh 		    options[OPT_ROLLOVER].o_request);
148e7ff5475SWarner Losh 		if (acting_as_client) {
149e7ff5475SWarner Losh 			send_error(peer, EBADOP);
150e7ff5475SWarner Losh 			return (1);
151e7ff5475SWarner Losh 		}
152e7ff5475SWarner Losh 		return (0);
153e7ff5475SWarner Losh 	}
154e7ff5475SWarner Losh 	options[OPT_ROLLOVER].o_reply =
155e7ff5475SWarner Losh 		strdup(options[OPT_ROLLOVER].o_request);
156e7ff5475SWarner Losh 
157e7ff5475SWarner Losh 	if (debug&DEBUG_OPTIONS)
158e7ff5475SWarner Losh 		tftp_log(LOG_DEBUG, "Setting rollover to '%s'",
159e7ff5475SWarner Losh 			options[OPT_ROLLOVER].o_reply);
160e7ff5475SWarner Losh 
161e7ff5475SWarner Losh 	return (0);
162e7ff5475SWarner Losh }
163e7ff5475SWarner Losh 
164e7ff5475SWarner Losh int
165e7ff5475SWarner Losh option_blksize(int peer)
166e7ff5475SWarner Losh {
16704ebad38SMarius Strobl 	u_long maxdgram;
168e7ff5475SWarner Losh 	size_t len;
169e7ff5475SWarner Losh 
170e7ff5475SWarner Losh 	if (options[OPT_BLKSIZE].o_request == NULL)
171e7ff5475SWarner Losh 		return (0);
172e7ff5475SWarner Losh 
173e7ff5475SWarner Losh 	/* maximum size of an UDP packet according to the system */
17404ebad38SMarius Strobl 	len = sizeof(maxdgram);
175e7ff5475SWarner Losh 	if (sysctlbyname("net.inet.udp.maxdgram",
17604ebad38SMarius Strobl 	    &maxdgram, &len, NULL, 0) < 0) {
177e7ff5475SWarner Losh 		tftp_log(LOG_ERR, "sysctl: net.inet.udp.maxdgram");
178e7ff5475SWarner Losh 		return (acting_as_client ? 1 : 0);
179e7ff5475SWarner Losh 	}
180e7ff5475SWarner Losh 
181e7ff5475SWarner Losh 	int size = atoi(options[OPT_BLKSIZE].o_request);
182e7ff5475SWarner Losh 	if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) {
183e7ff5475SWarner Losh 		if (acting_as_client) {
184e7ff5475SWarner Losh 			tftp_log(LOG_ERR,
185e7ff5475SWarner Losh 			    "Invalid blocksize (%d bytes), aborting",
186e7ff5475SWarner Losh 			    size);
187e7ff5475SWarner Losh 			send_error(peer, EBADOP);
188e7ff5475SWarner Losh 			return (1);
189e7ff5475SWarner Losh 		} else {
190e7ff5475SWarner Losh 			tftp_log(LOG_WARNING,
191e7ff5475SWarner Losh 			    "Invalid blocksize (%d bytes), ignoring request",
192e7ff5475SWarner Losh 			    size);
193e7ff5475SWarner Losh 			return (0);
194e7ff5475SWarner Losh 		}
195e7ff5475SWarner Losh 	}
196e7ff5475SWarner Losh 
19704ebad38SMarius Strobl 	if (size > (int)maxdgram) {
198e7ff5475SWarner Losh 		if (acting_as_client) {
199e7ff5475SWarner Losh 			tftp_log(LOG_ERR,
200e7ff5475SWarner Losh 			    "Invalid blocksize (%d bytes), "
201e7ff5475SWarner Losh 			    "net.inet.udp.maxdgram sysctl limits it to "
202b713097aSMarius Strobl 			    "%ld bytes.\n", size, maxdgram);
203e7ff5475SWarner Losh 			send_error(peer, EBADOP);
204e7ff5475SWarner Losh 			return (1);
205e7ff5475SWarner Losh 		} else {
206e7ff5475SWarner Losh 			tftp_log(LOG_WARNING,
207e7ff5475SWarner Losh 			    "Invalid blocksize (%d bytes), "
208e7ff5475SWarner Losh 			    "net.inet.udp.maxdgram sysctl limits it to "
209b713097aSMarius Strobl 			    "%ld bytes.\n", size, maxdgram);
21004ebad38SMarius Strobl 			size = maxdgram;
211e7ff5475SWarner Losh 			/* No reason to return */
212e7ff5475SWarner Losh 		}
213e7ff5475SWarner Losh 	}
214e7ff5475SWarner Losh 
215e7ff5475SWarner Losh 	asprintf(&options[OPT_BLKSIZE].o_reply, "%d", size);
216e7ff5475SWarner Losh 	segsize = size;
217e7ff5475SWarner Losh 	pktsize = size + 4;
218e7ff5475SWarner Losh 	if (debug&DEBUG_OPTIONS)
219e7ff5475SWarner Losh 		tftp_log(LOG_DEBUG, "Setting blksize to '%s'",
220e7ff5475SWarner Losh 		    options[OPT_BLKSIZE].o_reply);
221e7ff5475SWarner Losh 
222e7ff5475SWarner Losh 	return (0);
223e7ff5475SWarner Losh }
224e7ff5475SWarner Losh 
225e7ff5475SWarner Losh int
22604ebad38SMarius Strobl option_blksize2(int peer __unused)
227e7ff5475SWarner Losh {
22804ebad38SMarius Strobl 	u_long	maxdgram;
229e7ff5475SWarner Losh 	int	size, i;
230e7ff5475SWarner Losh 	size_t	len;
231e7ff5475SWarner Losh 
232e7ff5475SWarner Losh 	int sizes[] = {
233e7ff5475SWarner Losh 		8, 16, 32, 64, 128, 256, 512, 1024,
234e7ff5475SWarner Losh 		2048, 4096, 8192, 16384, 32768, 0
235e7ff5475SWarner Losh 	};
236e7ff5475SWarner Losh 
237e7ff5475SWarner Losh 	if (options[OPT_BLKSIZE2].o_request == NULL)
238e7ff5475SWarner Losh 		return (0);
239e7ff5475SWarner Losh 
240e7ff5475SWarner Losh 	/* maximum size of an UDP packet according to the system */
24104ebad38SMarius Strobl 	len = sizeof(maxdgram);
242e7ff5475SWarner Losh 	if (sysctlbyname("net.inet.udp.maxdgram",
24304ebad38SMarius Strobl 	    &maxdgram, &len, NULL, 0) < 0) {
244e7ff5475SWarner Losh 		tftp_log(LOG_ERR, "sysctl: net.inet.udp.maxdgram");
245e7ff5475SWarner Losh 		return (acting_as_client ? 1 : 0);
246e7ff5475SWarner Losh 	}
247e7ff5475SWarner Losh 
248e7ff5475SWarner Losh 	size = atoi(options[OPT_BLKSIZE2].o_request);
249e7ff5475SWarner Losh 	for (i = 0; sizes[i] != 0; i++) {
250e7ff5475SWarner Losh 		if (size == sizes[i]) break;
251e7ff5475SWarner Losh 	}
252e7ff5475SWarner Losh 	if (sizes[i] == 0) {
253e7ff5475SWarner Losh 		tftp_log(LOG_INFO,
254e7ff5475SWarner Losh 		    "Invalid blocksize2 (%d bytes), ignoring request", size);
255e7ff5475SWarner Losh 		return (acting_as_client ? 1 : 0);
256e7ff5475SWarner Losh 	}
257e7ff5475SWarner Losh 
25804ebad38SMarius Strobl 	if (size > (int)maxdgram) {
259e7ff5475SWarner Losh 		for (i = 0; sizes[i+1] != 0; i++) {
26004ebad38SMarius Strobl 			if ((int)maxdgram < sizes[i+1]) break;
261e7ff5475SWarner Losh 		}
262e7ff5475SWarner Losh 		tftp_log(LOG_INFO,
263e7ff5475SWarner Losh 		    "Invalid blocksize2 (%d bytes), net.inet.udp.maxdgram "
264b713097aSMarius Strobl 		    "sysctl limits it to %ld bytes.\n", size, maxdgram);
265e7ff5475SWarner Losh 		size = sizes[i];
266e7ff5475SWarner Losh 		/* No need to return */
267e7ff5475SWarner Losh 	}
268e7ff5475SWarner Losh 
269e7ff5475SWarner Losh 	asprintf(&options[OPT_BLKSIZE2].o_reply, "%d", size);
270e7ff5475SWarner Losh 	segsize = size;
271e7ff5475SWarner Losh 	pktsize = size + 4;
272e7ff5475SWarner Losh 	if (debug&DEBUG_OPTIONS)
273e7ff5475SWarner Losh 		tftp_log(LOG_DEBUG, "Setting blksize2 to '%s'",
274e7ff5475SWarner Losh 		    options[OPT_BLKSIZE2].o_reply);
275e7ff5475SWarner Losh 
276e7ff5475SWarner Losh 	return (0);
277e7ff5475SWarner Losh }
278e7ff5475SWarner Losh 
279fdf929ffSJohn Baldwin int
280fdf929ffSJohn Baldwin option_windowsize(int peer)
281fdf929ffSJohn Baldwin {
282fdf929ffSJohn Baldwin 	int size;
283fdf929ffSJohn Baldwin 
284fdf929ffSJohn Baldwin 	if (options[OPT_WINDOWSIZE].o_request == NULL)
285fdf929ffSJohn Baldwin 		return (0);
286fdf929ffSJohn Baldwin 
287fdf929ffSJohn Baldwin 	size = atoi(options[OPT_WINDOWSIZE].o_request);
288fdf929ffSJohn Baldwin 	if (size < WINDOWSIZE_MIN || size > WINDOWSIZE_MAX) {
289fdf929ffSJohn Baldwin 		if (acting_as_client) {
290fdf929ffSJohn Baldwin 			tftp_log(LOG_ERR,
291fdf929ffSJohn Baldwin 			    "Invalid windowsize (%d blocks), aborting",
292fdf929ffSJohn Baldwin 			    size);
293fdf929ffSJohn Baldwin 			send_error(peer, EBADOP);
294fdf929ffSJohn Baldwin 			return (1);
295fdf929ffSJohn Baldwin 		} else {
296fdf929ffSJohn Baldwin 			tftp_log(LOG_WARNING,
297fdf929ffSJohn Baldwin 			    "Invalid windowsize (%d blocks), ignoring request",
298fdf929ffSJohn Baldwin 			    size);
299fdf929ffSJohn Baldwin 			return (0);
300fdf929ffSJohn Baldwin 		}
301fdf929ffSJohn Baldwin 	}
302fdf929ffSJohn Baldwin 
303fdf929ffSJohn Baldwin 	/* XXX: Should force a windowsize of 1 for non-seekable files. */
304fdf929ffSJohn Baldwin 	asprintf(&options[OPT_WINDOWSIZE].o_reply, "%d", size);
305fdf929ffSJohn Baldwin 	windowsize = size;
306fdf929ffSJohn Baldwin 
307fdf929ffSJohn Baldwin 	if (debug&DEBUG_OPTIONS)
308fdf929ffSJohn Baldwin 		tftp_log(LOG_DEBUG, "Setting windowsize to '%s'",
309fdf929ffSJohn Baldwin 		    options[OPT_WINDOWSIZE].o_reply);
310fdf929ffSJohn Baldwin 
311fdf929ffSJohn Baldwin 	return (0);
312fdf929ffSJohn Baldwin }
313fdf929ffSJohn Baldwin 
314e7ff5475SWarner Losh /*
315e7ff5475SWarner Losh  * Append the available options to the header
316e7ff5475SWarner Losh  */
317e7ff5475SWarner Losh uint16_t
31804ebad38SMarius Strobl make_options(int peer __unused, char *buffer, uint16_t size) {
319e7ff5475SWarner Losh 	int	i;
320e7ff5475SWarner Losh 	char	*value;
321e7ff5475SWarner Losh 	const char *option;
322e7ff5475SWarner Losh 	uint16_t length;
323e7ff5475SWarner Losh 	uint16_t returnsize = 0;
324e7ff5475SWarner Losh 
325e7ff5475SWarner Losh 	if (!options_rfc_enabled) return (0);
326e7ff5475SWarner Losh 
327e7ff5475SWarner Losh 	for (i = 0; options[i].o_type != NULL; i++) {
328e7ff5475SWarner Losh 		if (options[i].rfc == 0 && !options_extra_enabled)
329e7ff5475SWarner Losh 			continue;
330e7ff5475SWarner Losh 
331e7ff5475SWarner Losh 		option = options[i].o_type;
332e7ff5475SWarner Losh 		if (acting_as_client)
333e7ff5475SWarner Losh 			value = options[i].o_request;
334e7ff5475SWarner Losh 		else
335e7ff5475SWarner Losh 			value = options[i].o_reply;
336e7ff5475SWarner Losh 		if (value == NULL)
337e7ff5475SWarner Losh 			continue;
338e7ff5475SWarner Losh 
339e7ff5475SWarner Losh 		length = strlen(value) + strlen(option) + 2;
340e7ff5475SWarner Losh 		if (size <= length) {
341e7ff5475SWarner Losh 			tftp_log(LOG_ERR,
342e7ff5475SWarner Losh 			    "Running out of option space for "
343e7ff5475SWarner Losh 			    "option '%s' with value '%s': "
344e7ff5475SWarner Losh 			    "needed %d bytes, got %d bytes",
345e7ff5475SWarner Losh 			    option, value, size, length);
346e7ff5475SWarner Losh 			continue;
347e7ff5475SWarner Losh 		}
348e7ff5475SWarner Losh 
349e7ff5475SWarner Losh 		sprintf(buffer, "%s%c%s%c", option, '\000', value, '\000');
350e7ff5475SWarner Losh 		size -= length;
351e7ff5475SWarner Losh 		buffer += length;
352e7ff5475SWarner Losh 		returnsize += length;
353e7ff5475SWarner Losh 	}
354e7ff5475SWarner Losh 
355e7ff5475SWarner Losh 	return (returnsize);
356e7ff5475SWarner Losh }
357e7ff5475SWarner Losh 
358e7ff5475SWarner Losh /*
359e7ff5475SWarner Losh  * Parse the received options in the header
360e7ff5475SWarner Losh  */
361e7ff5475SWarner Losh int
362e7ff5475SWarner Losh parse_options(int peer, char *buffer, uint16_t size)
363e7ff5475SWarner Losh {
364e7ff5475SWarner Losh 	int	i, options_failed;
365e7ff5475SWarner Losh 	char	*c, *cp, *option, *value;
366e7ff5475SWarner Losh 
367e7ff5475SWarner Losh 	if (!options_rfc_enabled) return (0);
368e7ff5475SWarner Losh 
369e7ff5475SWarner Losh 	/* Parse the options */
370e7ff5475SWarner Losh 	cp = buffer;
371e7ff5475SWarner Losh 	options_failed = 0;
372e7ff5475SWarner Losh 	while (size > 0) {
373e7ff5475SWarner Losh 		option = cp;
374e7ff5475SWarner Losh 		i = get_field(peer, cp, size);
375e7ff5475SWarner Losh 		cp += i;
376e7ff5475SWarner Losh 
377e7ff5475SWarner Losh 		value = cp;
378e7ff5475SWarner Losh 		i = get_field(peer, cp, size);
379e7ff5475SWarner Losh 		cp += i;
380e7ff5475SWarner Losh 
381e7ff5475SWarner Losh 		/* We are at the end */
382e7ff5475SWarner Losh 		if (*option == '\0') break;
383e7ff5475SWarner Losh 
384e7ff5475SWarner Losh 		if (debug&DEBUG_OPTIONS)
385e7ff5475SWarner Losh 			tftp_log(LOG_DEBUG,
386e7ff5475SWarner Losh 			    "option: '%s' value: '%s'", option, value);
387e7ff5475SWarner Losh 
388e7ff5475SWarner Losh 		for (c = option; *c; c++)
389e7ff5475SWarner Losh 			if (isupper(*c))
390e7ff5475SWarner Losh 				*c = tolower(*c);
391e7ff5475SWarner Losh 		for (i = 0; options[i].o_type != NULL; i++) {
392e7ff5475SWarner Losh 			if (strcmp(option, options[i].o_type) == 0) {
393e7ff5475SWarner Losh 				if (!acting_as_client)
394e7ff5475SWarner Losh 					options[i].o_request = value;
395e7ff5475SWarner Losh 				if (!options_extra_enabled && !options[i].rfc) {
396e7ff5475SWarner Losh 					tftp_log(LOG_INFO,
397e7ff5475SWarner Losh 					    "Option '%s' with value '%s' found "
398e7ff5475SWarner Losh 					    "but it is not an RFC option",
399e7ff5475SWarner Losh 					    option, value);
400e7ff5475SWarner Losh 					continue;
401e7ff5475SWarner Losh 				}
402e7ff5475SWarner Losh 				if (options[i].o_handler)
403e7ff5475SWarner Losh 					options_failed +=
404e7ff5475SWarner Losh 					    (options[i].o_handler)(peer);
405e7ff5475SWarner Losh 				break;
406e7ff5475SWarner Losh 			}
407e7ff5475SWarner Losh 		}
408e7ff5475SWarner Losh 		if (options[i].o_type == NULL)
409e7ff5475SWarner Losh 			tftp_log(LOG_WARNING,
410e7ff5475SWarner Losh 			    "Unknown option: '%s'", option);
411e7ff5475SWarner Losh 
412e7ff5475SWarner Losh 		size -= strlen(option) + strlen(value) + 2;
413e7ff5475SWarner Losh 	}
414e7ff5475SWarner Losh 
415e7ff5475SWarner Losh 	return (options_failed);
416e7ff5475SWarner Losh }
417e7ff5475SWarner Losh 
418e7ff5475SWarner Losh /*
419e7ff5475SWarner Losh  * Set some default values in the options
420e7ff5475SWarner Losh  */
421e7ff5475SWarner Losh void
422e7ff5475SWarner Losh init_options(void)
423e7ff5475SWarner Losh {
424e7ff5475SWarner Losh 
425e7ff5475SWarner Losh 	options[OPT_ROLLOVER].o_request = strdup("0");
426e7ff5475SWarner Losh }
427