-
Notifications
You must be signed in to change notification settings - Fork 6
/
options.c
384 lines (349 loc) · 10.1 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* hey emacs! -*- Mode: C; c-file-style: "k&r"; indent-tabs-mode: nil -*- */
/*
* options.c
* Set of functions to deal with the options structure and for parsing
* options in TFTP data buffer.
*
* $Id: options.c,v 1.16 2003/04/25 00:16:18 jp Exp $
*
* Copyright (c) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca>
* and Remi Lefebvre <remi@debian.org>
*
* atftp is free software; you can redistribute them and/or modify them
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#if HAVE_ARGZ_H
#include <argz.h>
#else
#include "argz.h"
#endif
#include <arpa/tftp.h>
#include <string.h>
#include "options.h"
/*
* Fill a structure with the request packet of the client.
*/
int opt_parse_request(char *data, int data_size, struct tftp_opt *options)
{
char *entry = NULL;
char *tmp;
struct tftphdr *tftp_data = (struct tftphdr *)data;
size_t size = data_size - sizeof(tftp_data->th_opcode);
/* sanity check - requests always end in a null byte,
* check to prevent argz_next from reading past the end of
* data, as it doesn't do bounds checks */
if (data_size == 0 || data[data_size-1] != '\0')
return ERR;
/* read filename */
entry = argz_next(tftp_data->th_stuff, size, entry);
if (!entry)
return ERR;
else
opt_set_options(options, "filename", entry);
/* read mode */
entry = argz_next(tftp_data->th_stuff, size, entry);
if (!entry)
return ERR;
else
opt_set_options(options, "mode", entry);
/* scan for options */
// FIXME: we should use opt_parse_options() here
while ((entry = argz_next(tftp_data->th_stuff, size, entry)))
{
tmp = entry;
entry = argz_next(tftp_data->th_stuff, size, entry);
if (!entry)
return ERR;
else
opt_set_options(options, tmp, entry);
}
return OK;
}
/*
* Fill a structure looking only at TFTP options.
*/
int opt_parse_options(char *data, int data_size, struct tftp_opt *options)
{
char *entry = NULL;
char *tmp;
struct tftphdr *tftp_data = (struct tftphdr *)data;
size_t size = data_size - sizeof(tftp_data->th_opcode);
/* sanity check - options always end in a null byte,
* check to prevent argz_next from reading past the end of
* data, as it doesn't do bounds checks */
if (data_size == 0 || data[data_size-1] != '\0')
return ERR;
while ((entry = argz_next(tftp_data->th_stuff, size, entry)))
{
tmp = entry;
entry = argz_next(tftp_data->th_stuff, size, entry);
if (!entry)
return ERR;
else
opt_set_options(options, tmp, entry);
}
return OK;
}
/*
* Set an option by name in the structure.
* name is the name of the option as in tftp_def.c.
* value is it's new value, that must comply with the rfc's.
* When setting an option, it is marked as specified.
*
*/
int opt_set_options(struct tftp_opt *options, char *name, char *value)
{
int i;
for (i = 0; i < OPT_NUMBER; i++)
{
if (strncasecmp(name, options[i].option, OPT_SIZE) == 0)
{
options[i].specified = 1;
if (value)
Strncpy(options[i].value, value, VAL_SIZE);
else
Strncpy(options[i].value, tftp_default_options[i].value,
VAL_SIZE);
return OK;
}
}
return ERR;
}
/*
* Return "value" for a given option name in the given option
* structure.
*/
int opt_get_options(struct tftp_opt *options, char *name, char *value)
{
int i;
for (i = 0; i < OPT_NUMBER; i++)
{
if (strncasecmp(name, options[i].option, OPT_SIZE) == 0)
{
if (options[i].enabled)
Strncpy(value, options[i].value, VAL_SIZE);
else
return ERR;
return OK;
}
}
return ERR;
}
/*
* Disable an option by name.
*/
int opt_disable_options(struct tftp_opt *options, char *name)
{
int i;
for (i = 2; i < OPT_NUMBER; i++)
{
if (name == NULL)
options[i].specified = 0;
else
{
if (strncasecmp(name, options[i].option, OPT_SIZE) == 0)
{
options[i].specified = 0;
return OK;
}
}
}
if (name == NULL)
return OK;
return ERR;
}
/*
* Return 1 if one or more options are specified in the options structure.
*/
int opt_support_options(struct tftp_opt *options)
{
int i;
int support = 0;
for (i = 2; i < OPT_NUMBER; i++)
{
if (options[i].specified)
support = 1;
}
return support;
}
/*
* The next few functions deal with TFTP options. Function's name are self
* explicative.
*/
int opt_get_tsize(struct tftp_opt *options)
{
int tsize;
if (options[OPT_TSIZE].enabled && options[OPT_TSIZE].specified)
{
tsize = atoi(options[OPT_TSIZE].value);
return tsize;
}
return ERR;
}
int opt_get_timeout(struct tftp_opt *options)
{
int timeout;
if (options[OPT_TIMEOUT].enabled && options[OPT_TIMEOUT].specified)
{
timeout = atoi(options[OPT_TIMEOUT].value);
return timeout;
}
return ERR;
}
int opt_get_blksize(struct tftp_opt *options)
{
int blksize;
if (options[OPT_BLKSIZE].enabled && options[OPT_BLKSIZE].specified)
{
blksize = atoi(options[OPT_BLKSIZE].value);
return blksize;
}
return ERR;
}
int opt_get_windowsize(struct tftp_opt *options)
{
int windowsize;
if (options[OPT_WINDOWSIZE].enabled && options[OPT_WINDOWSIZE].specified)
{
windowsize = atoi(options[OPT_WINDOWSIZE].value);
return windowsize;
}
return ERR;
}
int opt_get_multicast(struct tftp_opt *options, char *addr, int *port, int *mc)
{
char *token = NULL;
char *string = NULL;
char *temp = NULL;
if (options[OPT_MULTICAST].enabled && options[OPT_MULTICAST].specified)
{
string = strdup(options[OPT_MULTICAST].value);
/* get first argument */
if ((token = strtok_r(string, ",", &temp)) == NULL)
{
free(string);
return ERR;
}
else
Strncpy(addr, token, IPADDRLEN);
/* get second argument */
if ((token = strtok_r(NULL, ",", &temp)) == NULL)
{
free(string);
return ERR;
}
else
{
*port = atoi(token);
if ((*port < 0) || (*port > 65536))
{
free(string);
return ERR;
}
}
/* get third (last) argument */
if ((token = strtok_r(NULL, ",", &temp)) == NULL)
{
free(string);
return ERR;
}
else
{
*mc = atoi(token);
if ((*mc != 0) && (*mc != 1))
{
free(string);
return ERR;
}
}
free(string);
return *mc;
}
return ERR;
}
void opt_set_tsize(int tsize, struct tftp_opt *options)
{
snprintf(options[OPT_TSIZE].value, VAL_SIZE, "%d", tsize);
}
void opt_set_timeout(int timeout, struct tftp_opt *options)
{
snprintf(options[OPT_TIMEOUT].value, VAL_SIZE, "%d", timeout);
}
void opt_set_blksize(int blksize, struct tftp_opt *options)
{
snprintf(options[OPT_BLKSIZE].value, VAL_SIZE, "%d", blksize);
}
void opt_set_windowsize(int windowsize, struct tftp_opt *options)
{
snprintf(options[OPT_WINDOWSIZE].value, VAL_SIZE, "%d", windowsize);
}
void opt_set_multicast(struct tftp_opt *options, char *addr, int port, int mc)
{
snprintf(options[OPT_MULTICAST].value, VAL_SIZE, "%s,%d,%d", addr, port,
mc);
}
/*
* Format the content of the options structure (this is the content of a
* read or write request) to a string.
*/
void opt_request_to_string(struct tftp_opt *options, char *string, int len)
{
int i, index = 0;
for (i = 0; i < 2; i++)
{
if ((index + (int)strlen(options[i].option) + 2) < len)
{
Strncpy(string + index, options[i].option, len - index);
index += strlen(options[i].option);
Strncpy(string + index, ": ", len - index);
index += 2;
}
if ((index + (int)strlen(options[i].value) + 2) < len)
{
Strncpy(string + index, options[i].value, len - index);
index += strlen(options[i].value);
Strncpy(string + index, ", ", len - index);
index += 2;
}
}
opt_options_to_string(options, string + index, len - index);
}
/*
* Convert the options structure to a string.
*/
void opt_options_to_string(struct tftp_opt *options, char *string, int len)
{
int i, index = 0;
for (i = 2; i < OPT_NUMBER; i++)
{
if (options[i].specified && options[i].enabled)
{
if ((index + (int)strlen(options[i].option) + 2) < len)
{
Strncpy(string + index, options[i].option, len - index);
index += strlen(options[i].option);
Strncpy(string + index, ": ", len - index);
index += 2;
}
if ((index + (int)strlen(options[i].value) + 2) < len)
{
Strncpy(string + index, options[i].value, len - index);
index += strlen(options[i].value);
Strncpy(string + index, ", ", len - index);
index += 2;
}
}
}
if (index > 0)
string[index - 2] = 0;
else
string[0] = 0;
}