about summary refs log tree commit diff stats
path: root/src/handlers.c
blob: 1d4d92cdc40fc3ea59b3aa20a8641fe28a0afb6f (plain) (blame)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#ifdef __linux__
#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <syslog.h>
#include <limits.h>
#include <errno.h>
#include <unistd.h>
#ifdef __FreeBSD__
#include <sys/param.h>
#endif

#include "handlers.h"
#include "packet.h"
#include "netascii.h"

inline nbd_opmode get_mode(char *mode) {
	nbd_opmode opmode;
	if (strncmp(mode, (char *)&"netascii", NBD_NBTPD_ARGS_MODE_MAX) == 0) {
		opmode = NETASCII;
	} else if (strncmp(mode, (char *)&"octet", NBD_NBTPD_ARGS_MODE_MAX) == 0) {
		opmode = OCTET;
	} else if (strncmp(mode, (char *)&"mail", NBD_NBTPD_ARGS_MODE_MAX) == 0) {
		opmode = MAIL;
	} else {
		opmode = NOT_FOUND;
	}
	return opmode;
}

int makesock(nbd_nbtpd_args *argptr) {
	int s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s <= 0) {
		syslog(LOG_ERR, "unable to define socket: %s", strerror(errno));
		return -1;
	}
	struct timeval timeout = { 30, 0 };
	if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
		syslog(LOG_ERR, "unable to set socket timeout: %s", strerror(errno));
		close(s);
		return -1;
	}
	if (connect(s, (struct sockaddr *)&(argptr->client), sizeof(struct sockaddr)) < 0) {
		syslog(
			LOG_ERR,
			"unable to connect to client %s: %s",
			inet_ntoa(argptr->client.sin_addr),
			strerror(errno)
		);
		close(s);
		return -1;
	}
	return s;
}

char *checkpath(nbd_nbtpd_args *argptr, uint8_t read) {
	char *fname = NULL, *wd = NULL, *path = NULL, *ptr = NULL;
	char **parts = NULL;
	if (!is_netascii_str((char *)&argptr->path)) {
		argptr->err = 1;
		goto cleanup;
	}
	if (read) {
		fname = realpath((char *)&argptr->path, NULL);
	} else {
		//TODO: figure out how to canonicalize non-existent path
		char *tok = NULL;
		parts = malloc(sizeof(parts) * NBD_NBTPD_ARGS_PATH_MAX);
		if (parts == NULL) {
			goto cleanup;
		}
		memset(parts, '\0', sizeof(parts) * NBD_NBTPD_ARGS_PATH_MAX);
		ptr = (char *)&argptr->path;
		for (int i = 0; ((tok = strsep(&ptr, "/")) != NULL); i++ ) {
			parts[i] = tok;
		}
		path = malloc(NBD_NBTPD_ARGS_PATH_MAX);
		if (path == NULL) {
			goto cleanup;
		}
		memset(path, '\0', NBD_NBTPD_ARGS_PATH_MAX);
		int z = 0;
		for (int i = 0; parts[i] != NULL; i++) {
			if (strncmp(parts[i], "..", 2) == 0) {
				continue;
			}
			if (strncmp(parts[i], ".", NBD_NBTPD_ARGS_PATH_MAX) == 0) {
				continue;
			}
			for (int x = 0; parts[i][x] != '\0'; x++) {
				if (z < NBD_NBTPD_ARGS_PATH_MAX) {
					path[z++] = parts[i][x];
				} else {
					goto cleanup;
				}
			}
			if (z < NBD_NBTPD_ARGS_PATH_MAX) {
				path[z++] = '/';
			} else {
				goto cleanup;
			}
		}
		// erase trailing slash
		path[z - 1] = '\0';
		fname = strdup(path);
	}
	if (fname == NULL) {
		syslog(LOG_ERR, "unable to get real path: %s", strerror(errno));
		argptr->err = 1;
		goto cleanup;
	}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * POSIX does not define what happens when getcwd() is given NULL.     *
 * This means that illumos libc and GNU libc both return a newly       *
 * malloc()'d string, while FreeBSD libc does not.  Also, FreeBSD uses *
 * constant MAXPATHLEN defined in <sys/param.h> rather than PATH_MAX.  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifdef __FreeBSD__
	wd = malloc(MAXPATHLEN);
	if (wd == NULL) {
		syslog(LOG_ERR, "unable to allocate PATH_MAX memory: %s", strerror(errno));
		argptr->err = 0;
		goto cleanup;
	}
	if (getcwd(wd, MAXPATHLEN) == NULL) {
		argptr->err = 0;
		goto cleanup;
	}
#else
	if ((wd = getcwd(NULL, PATH_MAX)) == NULL) {
		argptr->err = 0;
		goto cleanup;
	}
#endif
	syslog(LOG_DEBUG, "cwd: %s :: realpath: %s", wd, fname);
	if (read) {
		if (strncmp(wd, fname, strlen(wd))) {
			syslog(
				LOG_ERR,
				"%s:%d requested invalid file %s",
				inet_ntoa(argptr->client.sin_addr),
				ntohs(argptr->client.sin_port),
				fname
			);
			argptr->err = 2;
			goto cleanup;
		}
	} else {
		strcat(wd, "/");
		strcat(wd, fname);
		char *intermediate = wd;
		wd = fname;
		fname = intermediate;
	}
cleanup:
	free(wd);
	free(path);
	free(parts);
	return fname;
}

ssize_t senderror(int s, nbd_nbtpd_args *argptr) {
	size_t buflen = 4 + strlen(nbd_tftp_error_to_message((nbd_tftp_ecode)argptr->err));
	char *buf = nbd_tftp_ser_error_from_code((nbd_tftp_ecode)argptr->err);
	ssize_t sb = send(s, buf, buflen, 0);
	free(buf);
	return sb;
}

void *read_req_resp(void *args) {
	char *fname = NULL, *buf = NULL, *rxb = NULL;
	FILE *fp = NULL;
	nbd_nbtpd_args *argptr = (nbd_nbtpd_args *)args;
	syslog(
		LOG_DEBUG,
		"%s:%d is requesting file %s",
		inet_ntoa(argptr->client.sin_addr),
		ntohs(argptr->client.sin_port),
		argptr->path
	);
	fname = checkpath(argptr, 1);
	if (fname == NULL) {
		goto pre_socket;
	}
	if (!is_netascii_str((char *)&(argptr->mode))) {
		syslog(
			LOG_ERR,
			"%s:%d mode is invalid %s",
			inet_ntoa(argptr->client.sin_addr),
			ntohs(argptr->client.sin_port),
			argptr->mode
		);
		argptr->err = 4;
		goto pre_socket;
	}
	nbd_opmode opmode = get_mode((char *)&(argptr->mode));
	if ((opmode != NETASCII) && (opmode != OCTET)) {
		argptr->err = 4;
		goto pre_socket;
	}
	fp = fopen(fname, "r");
	if (fp == NULL) {
		syslog(
			LOG_ERR,
			"failed to open file for reading: %s",
			strerror(errno)
		);
		argptr->err = 2;
		goto pre_socket;
	}
	buf = malloc(512);
	rxb = malloc(256);
	if ((buf == NULL) || (rxb == NULL)) {
		syslog(
			LOG_CRIT,
			"failed to allocate memory: %s",
			strerror(errno)
		);
		argptr->err = 0;
		goto pre_socket;
	}
	int s = makesock(argptr);
	if (s <= 0) {
		goto cleanup;
	}
	uint8_t rxon = 0, lon = 1;
	uint16_t bnum = 0;
	while (lon) {
		memset(buf, '\0', 512);
		uint8_t verif = 0;
		uint8_t vcount = 0;
		size_t num_read = fread(buf, 1, 512, fp);
		if (((num_read < 512) && (!feof(fp))) || (vcount > 5)) {
			argptr->err = 0;
			senderror(s, argptr);
			goto cleanup;
		}
		if ((opmode == NETASCII) && (!is_netascii_buf(buf, num_read))) {
			syslog(
				LOG_ERR,
				"%s:%d: requested file is not netascii compliant.",
				inet_ntoa(argptr->client.sin_addr),
				ntohs(argptr->client.sin_port)
			);
			argptr->err = 5;
			senderror(s, argptr);
			goto cleanup;
		}
		char *packet = nbd_tftp_ser_data_from_parts(++bnum, buf, num_read);
		while (!verif) {
			syslog(LOG_DEBUG, "sending block number %d to %s:%d",
				bnum,
				inet_ntoa(argptr->client.sin_addr),
				htons(argptr->client.sin_port)
			);
			if (send(s, packet, num_read + 4, 0) < 0) {
				syslog(
					LOG_ERR,
					"unable to send data to %s: %s",
					inet_ntoa(argptr->client.sin_addr),
					strerror(errno)
				);
				continue;
			}
			rxon = 1;
			uint8_t rxcount = 0;
			while (rxon) {
				syslog(LOG_DEBUG, "waiting for ack %d from %s:%d",
					bnum,
					inet_ntoa(argptr->client.sin_addr),
					htons(argptr->client.sin_port)
				);
				memset(rxb, '\0', 256);
				ssize_t rcv = recv(s, rxb, 256, 0);
				syslog(LOG_DEBUG, "recv'd %ld bytes from %s:%d",
					rcv,
					inet_ntoa(argptr->client.sin_addr),
					htons(argptr->client.sin_port)
				);
				if (rcv == 4) {
					nbd_tftp_packet_ack ack = nbd_tftp_de_ack(rxb, rcv);
					if (ack.opcode == 5) {
						goto socket_clean;
					}
					syslog(LOG_DEBUG, "%s:%d ack'd with block number: %d (expect %d)",
						inet_ntoa(argptr->client.sin_addr),
						htons(argptr->client.sin_port),
						ack.block_num,
						bnum
					);
					if (ack.block_num == bnum) {
						rxon = 0;
						verif = 1;
						break;
					}
					if (ack.block_num == (bnum - 1)) {
						rxon = 0;
						vcount++;
						break;
					}
				}
				if (++rxcount > 30) {
					argptr->err = 0;
					senderror(s, argptr);
					goto cleanup;
				}
				if (rcv > 4) {
					if ((((uint16_t)buf[0] << 8) + buf[1]) == 5) {
						goto socket_clean;
					}
				}
			}
		}
		free(packet);
		if (num_read < 512) {
			lon = 0;
		}
	}
socket_clean:
	close(s);
cleanup:
	fclose(fp);
	free(buf);
	free(rxb);
	free(fname);
	free(args);
	return (void *)NULL;
pre_socket:
	if (fp != NULL) {
		fclose(fp);
	}
	free(buf);
	free(rxb);
	free(fname);
	return nbd_nbtpd_resp_error(args);
}

void *write_req_resp(void *args) {
	char *fname = NULL, *rxb = NULL;
	FILE *fp = NULL;
	nbd_nbtpd_args *argptr = (nbd_nbtpd_args *)args;
	syslog(
		LOG_DEBUG,
		"%s:%d is trying to write file %s",
		inet_ntoa(argptr->client.sin_addr),
		ntohs(argptr->client.sin_port),
		argptr->path
	);
	fname = checkpath(argptr, 0);
	if (fname == NULL) {
		syslog(LOG_ERR, "fname is NULL");
		goto pre_socket;
	}
	if (!is_netascii_str((char *)&(argptr->mode))) {
		syslog(
			LOG_ERR,
			"%s:%d mode is invalid %s",
			inet_ntoa(argptr->client.sin_addr),
			ntohs(argptr->client.sin_port),
			argptr->mode
		);
		argptr->err = 4;
		goto pre_socket;
	}
	nbd_opmode opmode = get_mode((char *)&(argptr->mode));
	if ((opmode != NETASCII) && (opmode != OCTET)) {
		syslog(LOG_ERR, "%s:%d mode is not supported.", inet_ntoa(argptr->client.sin_addr), ntohs(argptr->client.sin_port));
		argptr->err = 4;
		goto pre_socket;
	}
	fp = fopen(fname, "w");
	if (fp == NULL) {
		syslog(
			LOG_ERR,
			"failed to open file for writing: %s",
			strerror(errno)
		);
		argptr->err = 2;
		goto pre_socket;
	}
	rxb = malloc(768); // 768 is 1.5x expected block size
	if (rxb == NULL) {
		syslog(
			LOG_CRIT,
			"failed to allocate memory: %s",
			strerror(errno)
		);
		argptr->err = 0;
		goto pre_socket;
	}
	int s = makesock(argptr);
	if (s <= 0) {
		goto cleanup;
	}
	uint8_t rxon = 0, lon = 1;
	uint16_t bnum = 0;
	ssize_t rcv = 516;
	while (lon) {
		uint8_t verif = 0;
		uint8_t vcount = 0;
		char *packet = nbd_tftp_ser_ack_from_block_num(bnum++);
		while (!verif) {
			syslog(LOG_DEBUG, "sending ack number %d to %s:%d",
				bnum,
				inet_ntoa(argptr->client.sin_addr),
				htons(argptr->client.sin_port)
			);
			if (send(s, packet, 4, 0) < 0) {
				syslog(
					LOG_ERR,
					"unable to send ack to %s:%d: %s",
					inet_ntoa(argptr->client.sin_addr),
					htons(argptr->client.sin_port),
					strerror(errno)
				);
				continue;
			}
			if ((rcv < 516) || (vcount > 5)) {
				lon = 0;
				break;
			}
			rxon = 1;
			uint8_t rxcount = 0;
			while (rxon) {
				syslog(LOG_DEBUG, "waiting for data from %s:%d",
					inet_ntoa(argptr->client.sin_addr),
					htons(argptr->client.sin_port)
				);
				memset(rxb, '\0', 768);
				rcv = recv(s, rxb, 768, 0);
				syslog(LOG_DEBUG, "recv'd %ld bytes from %s:%d",
					rcv,
					inet_ntoa(argptr->client.sin_addr),
					htons(argptr->client.sin_port)
				);
				if (rcv >= 4) {
					nbd_tftp_packet_data data = nbd_tftp_de_data(rxb, rcv);
					if (data.opcode == 5) {
						goto cleanup;
					}
					syslog(
						LOG_DEBUG,
						"%s:%d sent block number: %d (expect %d)",
						inet_ntoa(argptr->client.sin_addr),
						htons(argptr->client.sin_port),
						data.block_num,
						bnum
					);
					if (data.block_num == bnum) {
						rxon = 0;
						verif = 1;
						if (data.datalen > 0) {
							if ((opmode == NETASCII) && (!is_netascii_buf(data.data, data.datalen))) {
								syslog(
									LOG_ERR,
									"%s:%d: requested file is not netascii compliant.",
									inet_ntoa(argptr->client.sin_addr),
									ntohs(argptr->client.sin_port)
								);
								argptr->err = 5;
								senderror(s, argptr);
								goto cleanup;
							}
							if (fwrite(data.data, 1, data.datalen, fp) < data.datalen) {
								syslog(LOG_ERR, "filewrite failed for %s: %s", fname, strerror(errno));
								argptr->err = 0;
								senderror(s, argptr);
								goto clean_socket;
							}
						}
						break;
					}
					if (data.block_num == (bnum - 1)) {
						rxon = 0;
						vcount++;
						break;
					}
				}
				if (++rxcount > 30) {
					syslog(LOG_ERR, "retry count exceeded");
					argptr->err = 0;
					senderror(s, argptr);
					goto clean_socket;
				}
			}
		}
		free(packet);
	}
clean_socket:
	close(s);
cleanup:
	fclose(fp);
	free(rxb);
	free(fname);
	free(args);
	return (void *)NULL;
pre_socket:
	if (fp != NULL) {
		fclose(fp);
	}
	free(rxb);
	free(fname);
	return nbd_nbtpd_resp_error(args);
}

void *nbd_nbtpd_resp_error(void *args) {
	nbd_nbtpd_args *argptr = (nbd_nbtpd_args *)args;
	int s = makesock(argptr);
	if (s <= 0) {
		syslog(
			LOG_ERR,
			"cannot send error to %s:%d",
			inet_ntoa(argptr->client.sin_addr),
			ntohs(argptr->client.sin_port)
		);
		goto cleanup;
	}
	senderror(s, argptr);
cleanup:
	free(args);
	return (void *)NULL;
}