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
|
#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"
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;
}
void *read_req_resp(void *args) {
char *fname = NULL, *wd = 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);
if (!is_netascii_str((char *)&argptr->path)) {
argptr->err = 1;
goto pre_socket;
}
fname = realpath((char *)&argptr->path, NULL);
if (fname == NULL) {
syslog(LOG_ERR, "unable to get real path: %s", strerror(errno));
argptr->err = 1;
goto pre_socket;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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 pre_socket;
}
if (getcwd(wd, MAXPATHLEN) == NULL) {
argptr->err = 0;
goto pre_socket;
}
#else
if ((wd = getcwd(NULL, PATH_MAX)) == NULL) {
argptr->err = 0;
goto pre_socket;
}
#endif
syslog(LOG_DEBUG, "cwd: %s :: realpath: %s", wd, fname);
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 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) {
syslog(
LOG_CRIT,
"failed to allocate memory: %s",
strerror(errno)
);
argptr->err = 0;
goto pre_socket;
}
int s = socket(AF_INET, SOCK_DGRAM, 0);
if (s <= 0) {
syslog(LOG_ERR, "unable to define socket: %s", strerror(errno));
goto cleanup;
}
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));
goto socket_clean;
}
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)
);
goto socket_clean;
}
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)) {
//TODO: error response function!
}
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);
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) {
//TODO: error response function!
}
}
}
free(packet);
if (num_read < 512) {
lon = 0;
}
}
socket_clean:
close(s);
cleanup:
fclose(fp);
free(buf);
free(rxb);
free(fname);
free(wd);
free(args);
return (void *)NULL;
pre_socket:
if (fp != NULL) {
fclose(fp);
}
free(buf);
free(rxb);
free(fname);
free(wd);
return nbd_nbtpd_resp_error(args);
}
void *write_req_resp(void *args) {
free(args);
return (void *)NULL;
}
void *nbd_nbtpd_resp_error(void *args) {
//TODO: open socket and scream error, then cleanup.
//TODO: error response function!
free(args);
return (void *)NULL;
}
|