-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtftpcmd.c
406 lines (326 loc) · 9.3 KB
/
tftpcmd.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* TFTP Engine
*
* Copyright (c) 2014-2021 Joachim Wiberg <troglobit@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "uftpd.h"
#include <poll.h>
#include <arpa/tftp.h>
/*
* Theory of operation, from RFC1350:
*
* Client gets file, RRQ:
*
* client server
* |----------- RRQ ---------->|
* |<--------- DATA -----------|
* |----------- ACK ---------->|
* | |
*
* Client puts file, WRQ:
*
* client server
* |----------- WRQ ---------->|
* |<---------- ACK -----------|
* |---------- DATA ---------->|
* |<---------- ACK -----------|
* | |
*
*/
/* Send @len bytes data in @ctrl->buf */
static int do_send(ctrl_t *ctrl, size_t len)
{
int result;
size_t hdrsz = ctrl->th->th_msg - ctrl->buf;
size_t salen = sizeof(struct sockaddr_in);
if (ctrl->client_sa.ss_family == AF_INET6)
salen = sizeof(struct sockaddr_in6);
if (ctrl->th->th_opcode == OACK)
hdrsz = ctrl->th->th_stuff - ctrl->buf;
DBG("SND %c: header size: %zd, data len: %zd ...", ctrl->th->th_code, hdrsz, len);
result = sendto(ctrl->sd, ctrl->buf, hdrsz + len, 0, (struct sockaddr *)&ctrl->client_sa, salen);
if (-1 == result)
return 1;
return 0;
}
/* If @block is non-zero, resend that block */
static int send_DATA(ctrl_t *ctrl, int block)
{
size_t len;
memset(ctrl->buf, 0, ctrl->bufsz);
/* Create message */
ctrl->th->th_opcode = htons(DATA);
if (block) {
int pos = (block - 1) * ctrl->segsize;
ctrl->th->th_block = htons(block);
if (-1 == fseek(ctrl->fp, pos, SEEK_SET)) {
ERR(errno, "Failed resending block");
return 1;
}
} else {
ctrl->th->th_block = htons((ftell(ctrl->fp) / ctrl->segsize) + 1);
}
DBG("tftp block %d reading %zd bytes ...", ctrl->th->th_block, ctrl->segsize);
len = fread(ctrl->th->th_data, sizeof(char), ctrl->segsize, ctrl->fp);
return do_send(ctrl, len);
}
static int send_ACK(ctrl_t *ctrl, int block)
{
memset(ctrl->buf, 0, ctrl->bufsz);
ctrl->th->th_opcode = htons(ACK);
ctrl->th->th_block = htons(block);
DBG("ACK block %d", block);
return do_send(ctrl, 4);
}
/* Acknowledge options sent by client */
static int send_OACK(ctrl_t *ctrl)
{
char *ptr;
memset(ctrl->buf, 0, ctrl->bufsz);
/* Create message */
ctrl->th->th_opcode = htons(OACK);
ptr = &ctrl->th->th_stuff[0];
if (isset(&ctrl->tftp_options, 1)) {
ptr += sprintf(ptr, "blksize");
ptr ++;
ptr += sprintf(ptr, "%zd", ctrl->segsize);
ptr ++;
}
return do_send(ctrl, ptr - ctrl->buf);
}
static int send_ERROR(ctrl_t *ctrl, int code, char *str)
{
size_t len;
if (!str)
str = strerror(code);
len = strlen(str);
memset(ctrl->buf, 0, ctrl->segsize);
/* Create error message */
ctrl->th->th_opcode = htons(ERROR);
ctrl->th->th_code = htons(code);
strlcpy(ctrl->th->th_msg, str, len);
DBG("ERR %d: %s", code, str);
/* Error is ASCIIZ string, hence +1 */
return do_send(ctrl, len + 1);
}
static int alloc_buf(ctrl_t *ctrl, size_t segsize)
{
if (!ctrl) {
errno = EINVAL;
return 1;
}
ctrl->segsize = segsize;
ctrl->bufsz = sizeof(tftp_t) + ctrl->segsize;
if (ctrl->buf)
ctrl->buf = realloc(ctrl->buf, ctrl->bufsz);
else
ctrl->buf = malloc(ctrl->bufsz);
if (!ctrl->buf)
return 1;
ctrl->th = (tftp_t *)ctrl->buf;
return 0;
}
/* Parse TFTP payload in WRQ/RRQ for filename and optional blksize+timeout */
static int parse_RWRQ(ctrl_t *ctrl, char *buf, size_t len)
{
size_t opt_len = strlen(buf) + 1;
/* First opt is always filename */
ctrl->file = strdup(buf);
if (!ctrl->file)
return send_ERROR(ctrl, EUNDEF, NULL);
do {
/* Prepare to read options */
buf += opt_len;
len -= opt_len;
opt_len = strlen(buf) + 1;
if (!strncasecmp(buf, "blksize", 7)) {
size_t sz = 0;
buf += opt_len;
len -= opt_len;
opt_len = strlen(buf) + 1;
sscanf(buf, "%zd", &sz);
if (sz < MIN_SEGSIZE)
continue; /* Ignore if too small for us. */
if (alloc_buf(ctrl, sz)) {
ERR(errno, "Failed reallocating TFTP buffer memory");
return send_ERROR(ctrl, EUNDEF, NULL);
}
DBG("Negotiated blksize %zd", sz);
setbit(&ctrl->tftp_options, 1);
}
} while (len);
if (!ctrl->tftp_options)
return 0;
return send_OACK(ctrl);
}
static int handle_RRQ(ctrl_t *ctrl)
{
char *path;
path = compose_path(ctrl, ctrl->file);
if (!path) {
ERR(errno, "%s: Invalid path to file %s", ctrl->clientaddr, ctrl->file);
return send_ERROR(ctrl, ENOTFOUND, NULL);
}
ctrl->fp = fopen(path, "r");
if (!ctrl->fp) {
ERR(errno, "%s: Failed opening '%s'", ctrl->clientaddr, path);
return send_ERROR(ctrl, ENOTFOUND, NULL);
}
return !send_DATA(ctrl, 0);
}
static int handle_WRQ(ctrl_t *ctrl)
{
char *path;
path = compose_path(ctrl, ctrl->file);
if (!path) {
ERR(errno, "%s: Invalid path to file %s", ctrl->clientaddr, ctrl->file);
return send_ERROR(ctrl, ENOTFOUND, NULL);
}
ctrl->offset = 1; /* First expected block */
ctrl->fp = fopen(path, "w");
if (!ctrl->fp) {
ERR(errno, "%s: Failed opening '%s'", ctrl->clientaddr, path);
return send_ERROR(ctrl, ENOTFOUND, NULL);
}
if (ctrl->tftp_options)
return 0;
return send_ACK(ctrl, 0);
}
static int handle_DATA(ctrl_t *ctrl, size_t len)
{
char errmsg[80];
int block;
block = ntohs(ctrl->th->th_block);
if (block != ctrl->offset) {
snprintf(errmsg, sizeof(errmsg), "Expected block %ld, "
"got DATA for block %d", ctrl->offset, block);
return !send_ERROR(ctrl, EUNDEF, errmsg);
}
DBG("tftp block %d writing %zd bytes ...", ctrl->th->th_block, len);
if (len != fwrite(ctrl->th->th_data, sizeof(char), len, ctrl->fp)) {
snprintf(errmsg, sizeof(errmsg), "Failed writing file: %s",
strerror(errno));
return !send_ERROR(ctrl, ENOSPACE, errmsg);
}
ctrl->offset++;
if (send_ACK(ctrl, block) || len < ctrl->segsize)
return 0;
return 1;
}
/* TODO: Add support for ACK timeout and resend */
static int handle_ACK(ctrl_t *ctrl, int block)
{
if (ctrl->fp) {
if (feof(ctrl->fp)) {
fclose(ctrl->fp);
ctrl->fp = NULL;
return 0;
}
DBG("ACK block %d, file still open ... ", block);
return !send_DATA(ctrl, 0);
}
return 0;
}
static void read_client_command(uev_t *w, void *arg, int events)
{
int active = 1;
ctrl_t *ctrl = (ctrl_t *)arg;
ssize_t len;
uint16_t port, op, block;
struct sockaddr *addr = (struct sockaddr *)&ctrl->client_sa;
socklen_t addr_len = sizeof(ctrl->client_sa);
/* Reset inactivity timer. */
uev_timer_set(&ctrl->timeout_watcher, INACTIVITY_TIMER, 0);
memset(ctrl->buf, 0, ctrl->bufsz);
len = recvfrom(ctrl->sd, ctrl->buf, ctrl->bufsz, 0, addr, &addr_len);
if (-1 == len) {
if (errno != EINTR)
ERR(errno, "Failed reading command/status from client");
uev_exit(w->ctx);
return;
}
convert_address(&ctrl->client_sa, ctrl->clientaddr, sizeof(ctrl->clientaddr));
port = ntohs(((struct sockaddr_in *)addr)->sin_port);
op = ntohs(ctrl->th->th_opcode);
block = ntohs(ctrl->th->th_block);
switch (op) {
case RRQ:
len -= ctrl->th->th_stuff - ctrl->buf;
if (parse_RWRQ(ctrl, ctrl->th->th_stuff, len)) {
ERR(errno, "Failed parsing TFTP RRQ");
active = 0;
break;
}
LOG("tftp RRQ '%s' from %s:%d", ctrl->file, ctrl->clientaddr, port);
active = handle_RRQ(ctrl);
free(ctrl->file);
break;
case WRQ:
len -= ctrl->th->th_stuff - ctrl->buf;
if (parse_RWRQ(ctrl, ctrl->th->th_stuff, len)) {
ERR(errno, "Failed parsing TFTP WRQ");
active = 0;
break;
}
LOG("tftp WRQ '%s' from %s:%d", ctrl->file, ctrl->clientaddr, port);
handle_WRQ(ctrl);
free(ctrl->file);
break;
case DATA: /* Received data after WRQ */
INFO("tftp DATA '%s' from %s:%d", ctrl->file, ctrl->clientaddr, port);
len -= ctrl->th->th_data - ctrl->buf;
active = handle_DATA(ctrl, len);
break;
case ERROR:
DBG("tftp ERROR: %hd", ntohs(ctrl->th->th_code));
active = 0;
break;
case ACK: /* Sent for each DATA we send in a RRQ */
DBG("tftp ACK, block # %hu", block);
active = handle_ACK(ctrl, block);
break;
default:
DBG("tftp opcode: %hd", op);
DBG("tftp block#: %hu", block);
break;
}
if (!active)
uev_exit(w->ctx);
}
static void tftp_command(ctrl_t *ctrl)
{
/* Default buffer and segment size */
if (alloc_buf(ctrl, SEGSIZE)) {
ERR(errno, "Failed allocating TFTP buffer memory");
return;
}
uev_io_init(ctrl->ctx, &ctrl->io_watcher, read_client_command, ctrl, ctrl->sd, UEV_READ);
uev_run(ctrl->ctx, 0);
}
int tftp_session(uev_ctx_t *ctx, int sd)
{
int pid = 0;
ctrl_t *ctrl;
ctrl = new_session(ctx, sd, &pid);
if (!ctrl)
return pid;
tftp_command(ctrl);
exit(del_session(ctrl, 0));
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/