tftp.c 8.9 KB
Newer Older
B
bellard 已提交
1 2
/*
 * tftp.c - a simple, read-only tftp server for qemu
3
 *
B
bellard 已提交
4
 * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <slirp.h>
J
Jan Kiszka 已提交
26
#include "qemu-common.h"
B
bellard 已提交
27 28

struct tftp_session {
B
bellard 已提交
29
    int in_use;
30
    char *filename;
31

B
bellard 已提交
32 33
    struct in_addr client_ip;
    u_int16_t client_port;
34

B
bellard 已提交
35
    int timestamp;
B
bellard 已提交
36 37
};

38
static struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
B
bellard 已提交
39

40
char *tftp_prefix;
B
bellard 已提交
41 42 43

static void tftp_session_update(struct tftp_session *spt)
{
B
bellard 已提交
44 45
    spt->timestamp = curtime;
    spt->in_use = 1;
B
bellard 已提交
46 47 48 49
}

static void tftp_session_terminate(struct tftp_session *spt)
{
50
  qemu_free(spt->filename);
B
bellard 已提交
51 52 53 54 55 56 57 58 59 60 61
  spt->in_use = 0;
}

static int tftp_session_allocate(struct tftp_t *tp)
{
  struct tftp_session *spt;
  int k;

  for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
    spt = &tftp_sessions[k];

B
bellard 已提交
62 63
    if (!spt->in_use)
        goto found;
B
bellard 已提交
64 65

    /* sessions time out after 5 inactive seconds */
66 67
    if ((int)(curtime - spt->timestamp) > 5000) {
        qemu_free(spt->filename);
B
bellard 已提交
68
        goto found;
69
    }
B
bellard 已提交
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
  }

  return -1;

 found:
  memset(spt, 0, sizeof(*spt));
  memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
  spt->client_port = tp->udp.uh_sport;

  tftp_session_update(spt);

  return k;
}

static int tftp_session_find(struct tftp_t *tp)
{
  struct tftp_session *spt;
  int k;

  for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
    spt = &tftp_sessions[k];

    if (spt->in_use) {
      if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
	if (spt->client_port == tp->udp.uh_sport) {
	  return k;
	}
      }
    }
  }

  return -1;
}

static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr,
			  u_int8_t *buf, int len)
{
  int fd;
  int bytes_read = 0;
109

110
  fd = open(spt->filename, O_RDONLY | O_BINARY);
B
bellard 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

  if (fd < 0) {
    return -1;
  }

  if (len) {
    lseek(fd, block_nr * 512, SEEK_SET);

    bytes_read = read(fd, buf, len);
  }

  close(fd);

  return bytes_read;
}

127
static int tftp_send_oack(struct tftp_session *spt,
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
                          const char *key, uint32_t value,
                          struct tftp_t *recv_tp)
{
    struct sockaddr_in saddr, daddr;
    struct mbuf *m;
    struct tftp_t *tp;
    int n = 0;

    m = m_get();

    if (!m)
	return -1;

    memset(m->m_data, 0, m->m_size);

143
    m->m_data += IF_MAXLINKHDR;
144 145
    tp = (void *)m->m_data;
    m->m_data += sizeof(struct udpiphdr);
146

147
    tp->tp_op = htons(TFTP_OACK);
148 149 150 151
    n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
                  key) + 1;
    n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
                  value) + 1;
152 153 154

    saddr.sin_addr = recv_tp->ip.ip_dst;
    saddr.sin_port = recv_tp->udp.uh_dport;
155

156 157 158
    daddr.sin_addr = spt->client_ip;
    daddr.sin_port = spt->client_port;

159
    m->m_len = sizeof(struct tftp_t) - 514 + n -
160 161 162 163 164 165
        sizeof(struct ip) - sizeof(struct udphdr);
    udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);

    return 0;
}

166 167 168
static void tftp_send_error(struct tftp_session *spt,
                            u_int16_t errorcode, const char *msg,
                            struct tftp_t *recv_tp)
B
bellard 已提交
169 170 171 172 173 174 175 176 177
{
  struct sockaddr_in saddr, daddr;
  struct mbuf *m;
  struct tftp_t *tp;
  int nobytes;

  m = m_get();

  if (!m) {
178
    goto out;
B
bellard 已提交
179 180 181 182
  }

  memset(m->m_data, 0, m->m_size);

183
  m->m_data += IF_MAXLINKHDR;
B
bellard 已提交
184 185
  tp = (void *)m->m_data;
  m->m_data += sizeof(struct udpiphdr);
186

B
bellard 已提交
187 188
  tp->tp_op = htons(TFTP_ERROR);
  tp->x.tp_error.tp_error_code = htons(errorcode);
189
  pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
B
bellard 已提交
190 191 192 193 194 195 196 197 198

  saddr.sin_addr = recv_tp->ip.ip_dst;
  saddr.sin_port = recv_tp->udp.uh_dport;

  daddr.sin_addr = spt->client_ip;
  daddr.sin_port = spt->client_port;

  nobytes = 2;

199
  m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
B
bellard 已提交
200 201 202 203
        sizeof(struct ip) - sizeof(struct udphdr);

  udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);

204
out:
B
bellard 已提交
205 206 207
  tftp_session_terminate(spt);
}

208
static int tftp_send_data(struct tftp_session *spt,
B
bellard 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
			  u_int16_t block_nr,
			  struct tftp_t *recv_tp)
{
  struct sockaddr_in saddr, daddr;
  struct mbuf *m;
  struct tftp_t *tp;
  int nobytes;

  if (block_nr < 1) {
    return -1;
  }

  m = m_get();

  if (!m) {
    return -1;
  }

  memset(m->m_data, 0, m->m_size);

229
  m->m_data += IF_MAXLINKHDR;
B
bellard 已提交
230 231
  tp = (void *)m->m_data;
  m->m_data += sizeof(struct udpiphdr);
232

B
bellard 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  tp->tp_op = htons(TFTP_DATA);
  tp->x.tp_data.tp_block_nr = htons(block_nr);

  saddr.sin_addr = recv_tp->ip.ip_dst;
  saddr.sin_port = recv_tp->udp.uh_dport;

  daddr.sin_addr = spt->client_ip;
  daddr.sin_port = spt->client_port;

  nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);

  if (nobytes < 0) {
    m_free(m);

    /* send "file not found" error back */

    tftp_send_error(spt, 1, "File not found", tp);

    return -1;
  }

254
  m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
B
bellard 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        sizeof(struct ip) - sizeof(struct udphdr);

  udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);

  if (nobytes == 512) {
    tftp_session_update(spt);
  }
  else {
    tftp_session_terminate(spt);
  }

  return 0;
}

static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
{
  struct tftp_session *spt;
272
  int s, k;
273
  size_t prefix_len;
274
  char *req_fname;
B
bellard 已提交
275 276 277 278 279 280 281 282 283

  s = tftp_session_allocate(tp);

  if (s < 0) {
    return;
  }

  spt = &tftp_sessions[s];

284 285 286 287 288 289
  /* unspecifed prefix means service disabled */
  if (!tftp_prefix) {
      tftp_send_error(spt, 2, "Access violation", tp);
      return;
  }

290 291 292
  /* skip header fields */
  k = 0;
  pktlen -= ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
B
bellard 已提交
293

294 295 296 297 298
  /* prepend tftp_prefix */
  prefix_len = strlen(tftp_prefix);
  spt->filename = qemu_malloc(prefix_len + TFTP_FILENAME_MAX + 1);
  memcpy(spt->filename, tftp_prefix, prefix_len);

B
bellard 已提交
299
  /* get name */
300
  req_fname = spt->filename + prefix_len;
B
bellard 已提交
301

302 303 304
  while (1) {
    if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
      tftp_send_error(spt, 2, "Access violation", tp);
B
bellard 已提交
305 306
      return;
    }
307 308
    req_fname[k] = (char)tp->x.tp_buf[k];
    if (req_fname[k++] == '\0') {
B
bellard 已提交
309 310 311
      break;
    }
  }
312

B
bellard 已提交
313
  /* check mode */
314 315
  if ((pktlen - k) < 6) {
    tftp_send_error(spt, 2, "Access violation", tp);
B
bellard 已提交
316 317
    return;
  }
318

319
  if (memcmp(&tp->x.tp_buf[k], "octet\0", 6) != 0) {
B
bellard 已提交
320 321 322 323
      tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
      return;
  }

324 325
  k += 6; /* skipping octet */

B
bellard 已提交
326
  /* do sanity checks on the filename */
327 328
  if (req_fname[0] != '/' || req_fname[strlen(req_fname) - 1] == '/' ||
      strstr(req_fname, "/../")) {
B
bellard 已提交
329 330 331 332 333
      tftp_send_error(spt, 2, "Access violation", tp);
      return;
  }

  /* check if the file exists */
334
  if (tftp_read_data(spt, 0, NULL, 0) < 0) {
B
bellard 已提交
335 336 337 338
      tftp_send_error(spt, 1, "File not found", tp);
      return;
  }

339
  if (tp->x.tp_buf[pktlen - 1] != 0) {
340 341 342 343
      tftp_send_error(spt, 2, "Access violation", tp);
      return;
  }

344
  while (k < pktlen) {
345 346
      const char *key, *value;

347
      key = (const char *)&tp->x.tp_buf[k];
348 349
      k += strlen(key) + 1;

350
      if (k >= pktlen) {
351 352 353 354
	  tftp_send_error(spt, 2, "Access violation", tp);
	  return;
      }

355
      value = (const char *)&tp->x.tp_buf[k];
356 357 358 359 360 361
      k += strlen(value) + 1;

      if (strcmp(key, "tsize") == 0) {
	  int tsize = atoi(value);
	  struct stat stat_p;

362
	  if (tsize == 0) {
363
	      if (stat(spt->filename, &stat_p) == 0)
364 365 366 367 368 369 370 371 372 373 374
		  tsize = stat_p.st_size;
	      else {
		  tftp_send_error(spt, 1, "File not found", tp);
		  return;
	      }
	  }

	  tftp_send_oack(spt, "tsize", tsize, tp);
      }
  }

B
bellard 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387
  tftp_send_data(spt, 1, tp);
}

static void tftp_handle_ack(struct tftp_t *tp, int pktlen)
{
  int s;

  s = tftp_session_find(tp);

  if (s < 0) {
    return;
  }

388 389
  if (tftp_send_data(&tftp_sessions[s],
		     ntohs(tp->x.tp_data.tp_block_nr) + 1,
B
bellard 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		     tp) < 0) {
    return;
  }
}

void tftp_input(struct mbuf *m)
{
  struct tftp_t *tp = (struct tftp_t *)m->m_data;

  switch(ntohs(tp->tp_op)) {
  case TFTP_RRQ:
    tftp_handle_rrq(tp, m->m_len);
    break;

  case TFTP_ACK:
    tftp_handle_ack(tp, m->m_len);
    break;
  }
}