1 | /*
|
---|
2 | * tftp.c - a simple, read-only tftp server for qemu
|
---|
3 | *
|
---|
4 | * Copyright (c) 2004 Magnus Damm <[email protected]>
|
---|
5 | *
|
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
7 | * of this software and associated documentation files (the "Software"), to deal
|
---|
8 | * in the Software without restriction, including without limitation the rights
|
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
10 | * copies of the Software, and to permit persons to whom the Software is
|
---|
11 | * furnished to do so, subject to the following conditions:
|
---|
12 | *
|
---|
13 | * The above copyright notice and this permission notice shall be included in
|
---|
14 | * all copies or substantial portions of the Software.
|
---|
15 | *
|
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
22 | * THE SOFTWARE.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include <slirp.h>
|
---|
26 |
|
---|
27 | struct tftp_session {
|
---|
28 | int in_use;
|
---|
29 | unsigned char filename[TFTP_FILENAME_MAX];
|
---|
30 |
|
---|
31 | struct in_addr client_ip;
|
---|
32 | u_int16_t client_port;
|
---|
33 |
|
---|
34 | int timestamp;
|
---|
35 | };
|
---|
36 |
|
---|
37 | struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
|
---|
38 |
|
---|
39 | const char *tftp_prefix;
|
---|
40 |
|
---|
41 | static void tftp_session_update(struct tftp_session *spt)
|
---|
42 | {
|
---|
43 | spt->timestamp = curtime;
|
---|
44 | spt->in_use = 1;
|
---|
45 | }
|
---|
46 |
|
---|
47 | static void tftp_session_terminate(struct tftp_session *spt)
|
---|
48 | {
|
---|
49 | spt->in_use = 0;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static int tftp_session_allocate(struct tftp_t *tp)
|
---|
53 | {
|
---|
54 | struct tftp_session *spt;
|
---|
55 | int k;
|
---|
56 |
|
---|
57 | for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
|
---|
58 | spt = &tftp_sessions[k];
|
---|
59 |
|
---|
60 | if (!spt->in_use)
|
---|
61 | goto found;
|
---|
62 |
|
---|
63 | /* sessions time out after 5 inactive seconds */
|
---|
64 | if ((int)(curtime - spt->timestamp) > 5000)
|
---|
65 | goto found;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return -1;
|
---|
69 |
|
---|
70 | found:
|
---|
71 | memset(spt, 0, sizeof(*spt));
|
---|
72 | memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
|
---|
73 | spt->client_port = tp->udp.uh_sport;
|
---|
74 |
|
---|
75 | tftp_session_update(spt);
|
---|
76 |
|
---|
77 | return k;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static int tftp_session_find(struct tftp_t *tp)
|
---|
81 | {
|
---|
82 | struct tftp_session *spt;
|
---|
83 | int k;
|
---|
84 |
|
---|
85 | for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
|
---|
86 | spt = &tftp_sessions[k];
|
---|
87 |
|
---|
88 | if (spt->in_use) {
|
---|
89 | if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
|
---|
90 | if (spt->client_port == tp->udp.uh_sport) {
|
---|
91 | return k;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | return -1;
|
---|
98 | }
|
---|
99 |
|
---|
100 | static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr,
|
---|
101 | u_int8_t *buf, int len)
|
---|
102 | {
|
---|
103 | int fd;
|
---|
104 | int bytes_read = 0;
|
---|
105 |
|
---|
106 | #ifndef VBOX
|
---|
107 | fd = open(spt->filename, O_RDONLY | O_BINARY);
|
---|
108 | #else /* VBOX */
|
---|
109 | fd = open((char *)spt->filename, O_RDONLY | O_BINARY);
|
---|
110 | #endif /* VBOX */
|
---|
111 |
|
---|
112 | if (fd < 0) {
|
---|
113 | return -1;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (len) {
|
---|
117 | lseek(fd, block_nr * 512, SEEK_SET);
|
---|
118 |
|
---|
119 | bytes_read = read(fd, buf, len);
|
---|
120 | }
|
---|
121 |
|
---|
122 | close(fd);
|
---|
123 |
|
---|
124 | return bytes_read;
|
---|
125 | }
|
---|
126 |
|
---|
127 | static int tftp_send_error(struct tftp_session *spt,
|
---|
128 | u_int16_t errorcode, const char *msg,
|
---|
129 | struct tftp_t *recv_tp)
|
---|
130 | {
|
---|
131 | struct sockaddr_in saddr, daddr;
|
---|
132 | struct mbuf *m;
|
---|
133 | struct tftp_t *tp;
|
---|
134 | int nobytes;
|
---|
135 |
|
---|
136 | m = m_get();
|
---|
137 |
|
---|
138 | if (!m) {
|
---|
139 | return -1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | memset(m->m_data, 0, m->m_size);
|
---|
143 |
|
---|
144 | m->m_data += if_maxlinkhdr;
|
---|
145 | tp = (void *)m->m_data;
|
---|
146 | m->m_data += sizeof(struct udpiphdr);
|
---|
147 |
|
---|
148 | tp->tp_op = htons(TFTP_ERROR);
|
---|
149 | tp->x.tp_error.tp_error_code = htons(errorcode);
|
---|
150 | #ifndef VBOX
|
---|
151 | strcpy(tp->x.tp_error.tp_msg, msg);
|
---|
152 | #else /* VBOX */
|
---|
153 | strcpy((char *)tp->x.tp_error.tp_msg, msg);
|
---|
154 | #endif /* VBOX */
|
---|
155 |
|
---|
156 | saddr.sin_addr = recv_tp->ip.ip_dst;
|
---|
157 | saddr.sin_port = recv_tp->udp.uh_dport;
|
---|
158 |
|
---|
159 | daddr.sin_addr = spt->client_ip;
|
---|
160 | daddr.sin_port = spt->client_port;
|
---|
161 |
|
---|
162 | nobytes = 2;
|
---|
163 |
|
---|
164 | m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
|
---|
165 | sizeof(struct ip) - sizeof(struct udphdr);
|
---|
166 |
|
---|
167 | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
---|
168 |
|
---|
169 | tftp_session_terminate(spt);
|
---|
170 |
|
---|
171 | return 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 | static int tftp_send_data(struct tftp_session *spt,
|
---|
175 | u_int16_t block_nr,
|
---|
176 | struct tftp_t *recv_tp)
|
---|
177 | {
|
---|
178 | struct sockaddr_in saddr, daddr;
|
---|
179 | struct mbuf *m;
|
---|
180 | struct tftp_t *tp;
|
---|
181 | int nobytes;
|
---|
182 |
|
---|
183 | if (block_nr < 1) {
|
---|
184 | return -1;
|
---|
185 | }
|
---|
186 |
|
---|
187 | m = m_get();
|
---|
188 |
|
---|
189 | if (!m) {
|
---|
190 | return -1;
|
---|
191 | }
|
---|
192 |
|
---|
193 | memset(m->m_data, 0, m->m_size);
|
---|
194 |
|
---|
195 | m->m_data += if_maxlinkhdr;
|
---|
196 | tp = (void *)m->m_data;
|
---|
197 | m->m_data += sizeof(struct udpiphdr);
|
---|
198 |
|
---|
199 | tp->tp_op = htons(TFTP_DATA);
|
---|
200 | tp->x.tp_data.tp_block_nr = htons(block_nr);
|
---|
201 |
|
---|
202 | saddr.sin_addr = recv_tp->ip.ip_dst;
|
---|
203 | saddr.sin_port = recv_tp->udp.uh_dport;
|
---|
204 |
|
---|
205 | daddr.sin_addr = spt->client_ip;
|
---|
206 | daddr.sin_port = spt->client_port;
|
---|
207 |
|
---|
208 | nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
|
---|
209 |
|
---|
210 | if (nobytes < 0) {
|
---|
211 | m_free(m);
|
---|
212 |
|
---|
213 | /* send "file not found" error back */
|
---|
214 |
|
---|
215 | tftp_send_error(spt, 1, "File not found", tp);
|
---|
216 |
|
---|
217 | return -1;
|
---|
218 | }
|
---|
219 |
|
---|
220 | m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
|
---|
221 | sizeof(struct ip) - sizeof(struct udphdr);
|
---|
222 |
|
---|
223 | udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
---|
224 |
|
---|
225 | if (nobytes == 512) {
|
---|
226 | tftp_session_update(spt);
|
---|
227 | }
|
---|
228 | else {
|
---|
229 | tftp_session_terminate(spt);
|
---|
230 | }
|
---|
231 |
|
---|
232 | return 0;
|
---|
233 | }
|
---|
234 |
|
---|
235 | static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
|
---|
236 | {
|
---|
237 | struct tftp_session *spt;
|
---|
238 | int s, k, n;
|
---|
239 | u_int8_t *src, *dst;
|
---|
240 |
|
---|
241 | s = tftp_session_allocate(tp);
|
---|
242 |
|
---|
243 | if (s < 0) {
|
---|
244 | return;
|
---|
245 | }
|
---|
246 |
|
---|
247 | spt = &tftp_sessions[s];
|
---|
248 |
|
---|
249 | src = tp->x.tp_buf;
|
---|
250 | dst = spt->filename;
|
---|
251 | n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
|
---|
252 |
|
---|
253 | /* get name */
|
---|
254 |
|
---|
255 | for (k = 0; k < n; k++) {
|
---|
256 | if (k < TFTP_FILENAME_MAX) {
|
---|
257 | dst[k] = src[k];
|
---|
258 | }
|
---|
259 | else {
|
---|
260 | return;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (src[k] == '\0') {
|
---|
264 | break;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (k >= n) {
|
---|
269 | return;
|
---|
270 | }
|
---|
271 |
|
---|
272 | k++;
|
---|
273 |
|
---|
274 | /* check mode */
|
---|
275 | if ((n - k) < 6) {
|
---|
276 | return;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (memcmp(&src[k], "octet\0", 6) != 0) {
|
---|
280 | tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
|
---|
281 | return;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /* do sanity checks on the filename */
|
---|
285 |
|
---|
286 | #ifndef VBOX
|
---|
287 | if ((spt->filename[0] != '/')
|
---|
288 | || (spt->filename[strlen(spt->filename) - 1] == '/')
|
---|
289 | || strstr(spt->filename, "/../")) {
|
---|
290 | #else /* VBOX */
|
---|
291 | if ((spt->filename[0] != '/')
|
---|
292 | || (spt->filename[strlen((const char *)spt->filename) - 1] == '/')
|
---|
293 | || strstr((char *)spt->filename, "/../")) {
|
---|
294 | #endif /* VBOX */
|
---|
295 | tftp_send_error(spt, 2, "Access violation", tp);
|
---|
296 | return;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /* only allow exported prefixes */
|
---|
300 |
|
---|
301 | #ifndef VBOX
|
---|
302 | if (!tftp_prefix
|
---|
303 | || (strncmp(spt->filename, tftp_prefix, strlen(tftp_prefix)) != 0)) {
|
---|
304 | #else /* VBOX */
|
---|
305 | if (!tftp_prefix
|
---|
306 | || (strncmp((char *)spt->filename, tftp_prefix, strlen(tftp_prefix)) != 0)) {
|
---|
307 | #endif /* VBOX */
|
---|
308 | tftp_send_error(spt, 2, "Access violation", tp);
|
---|
309 | return;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /* check if the file exists */
|
---|
313 |
|
---|
314 | if (tftp_read_data(spt, 0, spt->filename, 0) < 0) {
|
---|
315 | tftp_send_error(spt, 1, "File not found", tp);
|
---|
316 | return;
|
---|
317 | }
|
---|
318 |
|
---|
319 | tftp_send_data(spt, 1, tp);
|
---|
320 | }
|
---|
321 |
|
---|
322 | static void tftp_handle_ack(struct tftp_t *tp, int pktlen)
|
---|
323 | {
|
---|
324 | int s;
|
---|
325 |
|
---|
326 | s = tftp_session_find(tp);
|
---|
327 |
|
---|
328 | if (s < 0) {
|
---|
329 | return;
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (tftp_send_data(&tftp_sessions[s],
|
---|
333 | ntohs(tp->x.tp_data.tp_block_nr) + 1,
|
---|
334 | tp) < 0) {
|
---|
335 | return;
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | void tftp_input(struct mbuf *m)
|
---|
340 | {
|
---|
341 | struct tftp_t *tp = (struct tftp_t *)m->m_data;
|
---|
342 |
|
---|
343 | switch(ntohs(tp->tp_op)) {
|
---|
344 | case TFTP_RRQ:
|
---|
345 | tftp_handle_rrq(tp, m->m_len);
|
---|
346 | break;
|
---|
347 |
|
---|
348 | case TFTP_ACK:
|
---|
349 | tftp_handle_ack(tp, m->m_len);
|
---|
350 | break;
|
---|
351 | }
|
---|
352 | }
|
---|