xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * |
| 9 | * Trivial file transfer protocol server. |
| 10 | * |
| 11 | * This code includes many modifications by Jim Guyton <guyton@rand-unix> |
| 12 | * |
| 13 | * This source file was started based on netkit-tftpd 0.17 |
| 14 | * Heavily modified for curl's test suite |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Copyright (C) 2005 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. |
| 19 | * Copyright (c) 1983, Regents of the University of California. |
| 20 | * All rights reserved. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * This product includes software developed by the University of |
| 33 | * California, Berkeley and its contributors. |
| 34 | * 4. Neither the name of the University nor the names of its contributors |
| 35 | * may be used to endorse or promote products derived from this software |
| 36 | * without specific prior written permission. |
| 37 | * |
| 38 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 39 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 42 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 43 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 44 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 45 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 46 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 47 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 48 | * SUCH DAMAGE. |
| 49 | * |
| 50 | * SPDX-License-Identifier: BSD-4-Clause-UC |
| 51 | */ |
| 52 | |
| 53 | #include "server_setup.h" |
| 54 | |
| 55 | #ifdef HAVE_SYS_IOCTL_H |
| 56 | #include <sys/ioctl.h> |
| 57 | #endif |
| 58 | #ifdef HAVE_SIGNAL_H |
| 59 | #include <signal.h> |
| 60 | #endif |
| 61 | #ifdef HAVE_FCNTL_H |
| 62 | #include <fcntl.h> |
| 63 | #endif |
| 64 | #ifdef HAVE_NETINET_IN_H |
| 65 | #include <netinet/in.h> |
| 66 | #endif |
| 67 | #ifdef HAVE_ARPA_INET_H |
| 68 | #include <arpa/inet.h> |
| 69 | #endif |
| 70 | #ifdef HAVE_ARPA_TFTP_H |
| 71 | #include <arpa/tftp.h> |
| 72 | #else |
| 73 | #include "tftp.h" |
| 74 | #endif |
| 75 | #ifdef HAVE_NETDB_H |
| 76 | #include <netdb.h> |
| 77 | #endif |
| 78 | #ifdef HAVE_SYS_FILIO_H |
| 79 | /* FIONREAD on Solaris 7 */ |
| 80 | #include <sys/filio.h> |
| 81 | #endif |
| 82 | |
| 83 | #ifdef HAVE_SETJMP_H |
| 84 | #include <setjmp.h> |
| 85 | #endif |
| 86 | |
| 87 | #ifdef HAVE_PWD_H |
| 88 | #include <pwd.h> |
| 89 | #endif |
| 90 | |
| 91 | #include <ctype.h> |
| 92 | |
| 93 | #define ENABLE_CURLX_PRINTF |
| 94 | /* make the curlx header define all printf() functions to use the curlx_* |
| 95 | versions instead */ |
| 96 | #include "curlx.h" /* from the private lib dir */ |
| 97 | #include "getpart.h" |
| 98 | #include "util.h" |
| 99 | #include "server_sockaddr.h" |
| 100 | |
| 101 | /* include memdebug.h last */ |
| 102 | #include "memdebug.h" |
| 103 | |
| 104 | /***************************************************************************** |
| 105 | * STRUCT DECLARATIONS AND DEFINES * |
| 106 | *****************************************************************************/ |
| 107 | |
| 108 | #ifndef PKTSIZE |
| 109 | #define PKTSIZE (SEGSIZE + 4) /* SEGSIZE defined in arpa/tftp.h */ |
| 110 | #endif |
| 111 | |
| 112 | struct testcase { |
| 113 | char *buffer; /* holds the file data to send to the client */ |
| 114 | size_t bufsize; /* size of the data in buffer */ |
| 115 | char *rptr; /* read pointer into the buffer */ |
| 116 | size_t rcount; /* amount of data left to read of the file */ |
| 117 | long testno; /* test case number */ |
| 118 | int ofile; /* file descriptor for output file when uploading to us */ |
| 119 | |
| 120 | int writedelay; /* number of seconds between each packet */ |
| 121 | }; |
| 122 | |
| 123 | struct formats { |
| 124 | const char *f_mode; |
| 125 | int f_convert; |
| 126 | }; |
| 127 | |
| 128 | struct errmsg { |
| 129 | int e_code; |
| 130 | const char *e_msg; |
| 131 | }; |
| 132 | |
| 133 | typedef union { |
| 134 | struct tftphdr hdr; |
| 135 | char storage[PKTSIZE]; |
| 136 | } tftphdr_storage_t; |
| 137 | |
| 138 | /* |
| 139 | * bf.counter values in range [-1 .. SEGSIZE] represents size of data in the |
| 140 | * bf.buf buffer. Additionally it can also hold flags BF_ALLOC or BF_FREE. |
| 141 | */ |
| 142 | |
| 143 | struct bf { |
| 144 | int counter; /* size of data in buffer, or flag */ |
| 145 | tftphdr_storage_t buf; /* room for data packet */ |
| 146 | }; |
| 147 | |
| 148 | #define BF_ALLOC -3 /* alloc'd but not yet filled */ |
| 149 | #define BF_FREE -2 /* free */ |
| 150 | |
| 151 | #define opcode_RRQ 1 |
| 152 | #define opcode_WRQ 2 |
| 153 | #define opcode_DATA 3 |
| 154 | #define opcode_ACK 4 |
| 155 | #define opcode_ERROR 5 |
| 156 | |
| 157 | #define TIMEOUT 5 |
| 158 | |
| 159 | #undef MIN |
| 160 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
| 161 | |
| 162 | #ifndef DEFAULT_LOGFILE |
| 163 | #define DEFAULT_LOGFILE "log/tftpd.log" |
| 164 | #endif |
| 165 | |
| 166 | #define REQUEST_DUMP "log/server.input" |
| 167 | |
| 168 | #define DEFAULT_PORT 8999 /* UDP */ |
| 169 | |
| 170 | /***************************************************************************** |
| 171 | * GLOBAL VARIABLES * |
| 172 | *****************************************************************************/ |
| 173 | |
| 174 | static struct errmsg errmsgs[] = { |
| 175 | { EUNDEF, "Undefined error code" }, |
| 176 | { ENOTFOUND, "File not found" }, |
| 177 | { EACCESS, "Access violation" }, |
| 178 | { ENOSPACE, "Disk full or allocation exceeded" }, |
| 179 | { EBADOP, "Illegal TFTP operation" }, |
| 180 | { EBADID, "Unknown transfer ID" }, |
| 181 | { EEXISTS, "File already exists" }, |
| 182 | { ENOUSER, "No such user" }, |
| 183 | { -1, 0 } |
| 184 | }; |
| 185 | |
| 186 | static const struct formats formata[] = { |
| 187 | { "netascii", 1 }, |
| 188 | { "octet", 0 }, |
| 189 | { NULL, 0 } |
| 190 | }; |
| 191 | |
| 192 | static struct bf bfs[2]; |
| 193 | |
| 194 | static int nextone; /* index of next buffer to use */ |
| 195 | static int current; /* index of buffer in use */ |
| 196 | |
| 197 | /* control flags for crlf conversions */ |
| 198 | static int newline = 0; /* fillbuf: in middle of newline expansion */ |
| 199 | static int prevchar = -1; /* putbuf: previous char (cr check) */ |
| 200 | |
| 201 | static tftphdr_storage_t buf; |
| 202 | static tftphdr_storage_t ackbuf; |
| 203 | |
| 204 | static srvr_sockaddr_union_t from; |
| 205 | static curl_socklen_t fromlen; |
| 206 | |
| 207 | static curl_socket_t peer = CURL_SOCKET_BAD; |
| 208 | |
| 209 | static unsigned int timeout; |
| 210 | static unsigned int maxtimeout = 5 * TIMEOUT; |
| 211 | |
| 212 | #ifdef ENABLE_IPV6 |
| 213 | static bool use_ipv6 = FALSE; |
| 214 | #endif |
| 215 | static const char *ipv_inuse = "IPv4"; |
| 216 | |
| 217 | const char *serverlogfile = DEFAULT_LOGFILE; |
| 218 | static const char *pidname = ".tftpd.pid"; |
| 219 | static const char *portname = NULL; /* none by default */ |
| 220 | static int serverlogslocked = 0; |
| 221 | static int wrotepidfile = 0; |
| 222 | static int wroteportfile = 0; |
| 223 | |
| 224 | #ifdef HAVE_SIGSETJMP |
| 225 | static sigjmp_buf timeoutbuf; |
| 226 | #endif |
| 227 | |
| 228 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 229 | static const unsigned int rexmtval = TIMEOUT; |
| 230 | #endif |
| 231 | |
| 232 | /***************************************************************************** |
| 233 | * FUNCTION PROTOTYPES * |
| 234 | *****************************************************************************/ |
| 235 | |
| 236 | static struct tftphdr *rw_init(int); |
| 237 | |
| 238 | static struct tftphdr *w_init(void); |
| 239 | |
| 240 | static struct tftphdr *r_init(void); |
| 241 | |
| 242 | static void read_ahead(struct testcase *test, int convert); |
| 243 | |
| 244 | static ssize_t write_behind(struct testcase *test, int convert); |
| 245 | |
| 246 | static int synchnet(curl_socket_t); |
| 247 | |
| 248 | static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size); |
| 249 | |
| 250 | static int validate_access(struct testcase *test, const char *fname, int mode); |
| 251 | |
| 252 | static void sendtftp(struct testcase *test, const struct formats *pf); |
| 253 | |
| 254 | static void recvtftp(struct testcase *test, const struct formats *pf); |
| 255 | |
| 256 | static void nak(int error); |
| 257 | |
| 258 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 259 | |
| 260 | static void mysignal(int sig, void (*handler)(int)); |
| 261 | |
| 262 | static void timer(int signum); |
| 263 | |
| 264 | static void justtimeout(int signum); |
| 265 | |
| 266 | #endif /* HAVE_ALARM && SIGALRM */ |
| 267 | |
| 268 | /***************************************************************************** |
| 269 | * FUNCTION IMPLEMENTATIONS * |
| 270 | *****************************************************************************/ |
| 271 | |
| 272 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 273 | |
| 274 | /* |
| 275 | * Like signal(), but with well-defined semantics. |
| 276 | */ |
| 277 | static void mysignal(int sig, void (*handler)(int)) |
| 278 | { |
| 279 | struct sigaction sa; |
| 280 | memset(&sa, 0, sizeof(sa)); |
| 281 | sa.sa_handler = handler; |
| 282 | sigaction(sig, &sa, NULL); |
| 283 | } |
| 284 | |
| 285 | static void timer(int signum) |
| 286 | { |
| 287 | (void)signum; |
| 288 | |
| 289 | logmsg("alarm!"); |
| 290 | |
| 291 | timeout += rexmtval; |
| 292 | if(timeout >= maxtimeout) { |
| 293 | if(wrotepidfile) { |
| 294 | wrotepidfile = 0; |
| 295 | unlink(pidname); |
| 296 | } |
| 297 | if(wroteportfile) { |
| 298 | wroteportfile = 0; |
| 299 | unlink(portname); |
| 300 | } |
| 301 | if(serverlogslocked) { |
| 302 | serverlogslocked = 0; |
| 303 | clear_advisor_read_lock(SERVERLOGS_LOCK); |
| 304 | } |
| 305 | exit(1); |
| 306 | } |
| 307 | #ifdef HAVE_SIGSETJMP |
| 308 | siglongjmp(timeoutbuf, 1); |
| 309 | #endif |
| 310 | } |
| 311 | |
| 312 | static void justtimeout(int signum) |
| 313 | { |
| 314 | (void)signum; |
| 315 | } |
| 316 | |
| 317 | #endif /* HAVE_ALARM && SIGALRM */ |
| 318 | |
| 319 | /* |
| 320 | * init for either read-ahead or write-behind. |
| 321 | * zero for write-behind, one for read-head. |
| 322 | */ |
| 323 | static struct tftphdr *rw_init(int x) |
| 324 | { |
| 325 | newline = 0; /* init crlf flag */ |
| 326 | prevchar = -1; |
| 327 | bfs[0].counter = BF_ALLOC; /* pass out the first buffer */ |
| 328 | current = 0; |
| 329 | bfs[1].counter = BF_FREE; |
| 330 | nextone = x; /* ahead or behind? */ |
| 331 | return &bfs[0].buf.hdr; |
| 332 | } |
| 333 | |
| 334 | static struct tftphdr *w_init(void) |
| 335 | { |
| 336 | return rw_init(0); /* write-behind */ |
| 337 | } |
| 338 | |
| 339 | static struct tftphdr *r_init(void) |
| 340 | { |
| 341 | return rw_init(1); /* read-ahead */ |
| 342 | } |
| 343 | |
| 344 | /* Have emptied current buffer by sending to net and getting ack. |
| 345 | Free it and return next buffer filled with data. |
| 346 | */ |
| 347 | static int readit(struct testcase *test, struct tftphdr **dpp, |
| 348 | int convert /* if true, convert to ascii */) |
| 349 | { |
| 350 | struct bf *b; |
| 351 | |
| 352 | bfs[current].counter = BF_FREE; /* free old one */ |
| 353 | current = !current; /* "incr" current */ |
| 354 | |
| 355 | b = &bfs[current]; /* look at new buffer */ |
| 356 | if(b->counter == BF_FREE) /* if it's empty */ |
| 357 | read_ahead(test, convert); /* fill it */ |
| 358 | |
| 359 | *dpp = &b->buf.hdr; /* set caller's ptr */ |
| 360 | return b->counter; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * fill the input buffer, doing ascii conversions if requested |
| 365 | * conversions are lf -> cr, lf and cr -> cr, nul |
| 366 | */ |
| 367 | static void read_ahead(struct testcase *test, |
| 368 | int convert /* if true, convert to ascii */) |
| 369 | { |
| 370 | int i; |
| 371 | char *p; |
| 372 | int c; |
| 373 | struct bf *b; |
| 374 | struct tftphdr *dp; |
| 375 | |
| 376 | b = &bfs[nextone]; /* look at "next" buffer */ |
| 377 | if(b->counter != BF_FREE) /* nop if not free */ |
| 378 | return; |
| 379 | nextone = !nextone; /* "incr" next buffer ptr */ |
| 380 | |
| 381 | dp = &b->buf.hdr; |
| 382 | |
| 383 | if(convert == 0) { |
| 384 | /* The former file reading code did this: |
| 385 | b->counter = read(fileno(file), dp->th_data, SEGSIZE); */ |
| 386 | size_t copy_n = MIN(SEGSIZE, test->rcount); |
| 387 | memcpy(dp->th_data, test->rptr, copy_n); |
| 388 | |
| 389 | /* decrease amount, advance pointer */ |
| 390 | test->rcount -= copy_n; |
| 391 | test->rptr += copy_n; |
| 392 | b->counter = (int)copy_n; |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | p = dp->th_data; |
| 397 | for(i = 0 ; i < SEGSIZE; i++) { |
| 398 | if(newline) { |
| 399 | if(prevchar == '\n') |
| 400 | c = '\n'; /* lf to cr,lf */ |
| 401 | else |
| 402 | c = '\0'; /* cr to cr,nul */ |
| 403 | newline = 0; |
| 404 | } |
| 405 | else { |
| 406 | if(test->rcount) { |
| 407 | c = test->rptr[0]; |
| 408 | test->rptr++; |
| 409 | test->rcount--; |
| 410 | } |
| 411 | else |
| 412 | break; |
| 413 | if(c == '\n' || c == '\r') { |
| 414 | prevchar = c; |
| 415 | c = '\r'; |
| 416 | newline = 1; |
| 417 | } |
| 418 | } |
| 419 | *p++ = (char)c; |
| 420 | } |
| 421 | b->counter = (int)(p - dp->th_data); |
| 422 | } |
| 423 | |
| 424 | /* Update count associated with the buffer, get new buffer from the queue. |
| 425 | Calls write_behind only if next buffer not available. |
| 426 | */ |
| 427 | static int writeit(struct testcase *test, struct tftphdr * volatile *dpp, |
| 428 | int ct, int convert) |
| 429 | { |
| 430 | bfs[current].counter = ct; /* set size of data to write */ |
| 431 | current = !current; /* switch to other buffer */ |
| 432 | if(bfs[current].counter != BF_FREE) /* if not free */ |
| 433 | write_behind(test, convert); /* flush it */ |
| 434 | bfs[current].counter = BF_ALLOC; /* mark as alloc'd */ |
| 435 | *dpp = &bfs[current].buf.hdr; |
| 436 | return ct; /* this is a lie of course */ |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Output a buffer to a file, converting from netascii if requested. |
| 441 | * CR, NUL -> CR and CR, LF => LF. |
| 442 | * Note spec is undefined if we get CR as last byte of file or a |
| 443 | * CR followed by anything else. In this case we leave it alone. |
| 444 | */ |
| 445 | static ssize_t write_behind(struct testcase *test, int convert) |
| 446 | { |
| 447 | char *writebuf; |
| 448 | int count; |
| 449 | int ct; |
| 450 | char *p; |
| 451 | int c; /* current character */ |
| 452 | struct bf *b; |
| 453 | struct tftphdr *dp; |
| 454 | |
| 455 | b = &bfs[nextone]; |
| 456 | if(b->counter < -1) /* anything to flush? */ |
| 457 | return 0; /* just nop if nothing to do */ |
| 458 | |
| 459 | if(!test->ofile) { |
| 460 | char outfile[256]; |
| 461 | msnprintf(outfile, sizeof(outfile), "log/upload.%ld", test->testno); |
| 462 | #ifdef WIN32 |
| 463 | test->ofile = open(outfile, O_CREAT|O_RDWR|O_BINARY, 0777); |
| 464 | #else |
| 465 | test->ofile = open(outfile, O_CREAT|O_RDWR, 0777); |
| 466 | #endif |
| 467 | if(test->ofile == -1) { |
| 468 | logmsg("Couldn't create and/or open file %s for upload!", outfile); |
| 469 | return -1; /* failure! */ |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | count = b->counter; /* remember byte count */ |
| 474 | b->counter = BF_FREE; /* reset flag */ |
| 475 | dp = &b->buf.hdr; |
| 476 | nextone = !nextone; /* incr for next time */ |
| 477 | writebuf = dp->th_data; |
| 478 | |
| 479 | if(count <= 0) |
| 480 | return -1; /* nak logic? */ |
| 481 | |
| 482 | if(convert == 0) |
| 483 | return write(test->ofile, writebuf, count); |
| 484 | |
| 485 | p = writebuf; |
| 486 | ct = count; |
| 487 | while(ct--) { /* loop over the buffer */ |
| 488 | c = *p++; /* pick up a character */ |
| 489 | if(prevchar == '\r') { /* if prev char was cr */ |
| 490 | if(c == '\n') /* if have cr,lf then just */ |
| 491 | lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */ |
| 492 | else |
| 493 | if(c == '\0') /* if have cr,nul then */ |
| 494 | goto skipit; /* just skip over the putc */ |
| 495 | /* else just fall through and allow it */ |
| 496 | } |
| 497 | /* formerly |
| 498 | putc(c, file); */ |
| 499 | if(1 != write(test->ofile, &c, 1)) |
| 500 | break; |
| 501 | skipit: |
| 502 | prevchar = c; |
| 503 | } |
| 504 | return count; |
| 505 | } |
| 506 | |
| 507 | /* When an error has occurred, it is possible that the two sides are out of |
| 508 | * synch. Ie: that what I think is the other side's response to packet N is |
| 509 | * really their response to packet N-1. |
| 510 | * |
| 511 | * So, to try to prevent that, we flush all the input queued up for us on the |
| 512 | * network connection on our host. |
| 513 | * |
| 514 | * We return the number of packets we flushed (mostly for reporting when trace |
| 515 | * is active). |
| 516 | */ |
| 517 | |
| 518 | static int synchnet(curl_socket_t f /* socket to flush */) |
| 519 | { |
| 520 | |
| 521 | #if defined(HAVE_IOCTLSOCKET) |
| 522 | unsigned long i; |
| 523 | #else |
| 524 | int i; |
| 525 | #endif |
| 526 | int j = 0; |
| 527 | char rbuf[PKTSIZE]; |
| 528 | srvr_sockaddr_union_t fromaddr; |
| 529 | curl_socklen_t fromaddrlen; |
| 530 | |
| 531 | for(;;) { |
| 532 | #if defined(HAVE_IOCTLSOCKET) |
| 533 | (void) ioctlsocket(f, FIONREAD, &i); |
| 534 | #else |
| 535 | (void) ioctl(f, FIONREAD, &i); |
| 536 | #endif |
| 537 | if(i) { |
| 538 | j++; |
| 539 | #ifdef ENABLE_IPV6 |
| 540 | if(!use_ipv6) |
| 541 | #endif |
| 542 | fromaddrlen = sizeof(fromaddr.sa4); |
| 543 | #ifdef ENABLE_IPV6 |
| 544 | else |
| 545 | fromaddrlen = sizeof(fromaddr.sa6); |
| 546 | #endif |
| 547 | (void) recvfrom(f, rbuf, sizeof(rbuf), 0, |
| 548 | &fromaddr.sa, &fromaddrlen); |
| 549 | } |
| 550 | else |
| 551 | break; |
| 552 | } |
| 553 | return j; |
| 554 | } |
| 555 | |
| 556 | int main(int argc, char **argv) |
| 557 | { |
| 558 | srvr_sockaddr_union_t me; |
| 559 | struct tftphdr *tp; |
| 560 | ssize_t n = 0; |
| 561 | int arg = 1; |
| 562 | unsigned short port = DEFAULT_PORT; |
| 563 | curl_socket_t sock = CURL_SOCKET_BAD; |
| 564 | int flag; |
| 565 | int rc; |
| 566 | int error; |
| 567 | struct testcase test; |
| 568 | int result = 0; |
| 569 | |
| 570 | memset(&test, 0, sizeof(test)); |
| 571 | |
| 572 | while(argc>arg) { |
| 573 | if(!strcmp("--version", argv[arg])) { |
| 574 | printf("tftpd IPv4%s\n", |
| 575 | #ifdef ENABLE_IPV6 |
| 576 | "/IPv6" |
| 577 | #else |
| 578 | "" |
| 579 | #endif |
| 580 | ); |
| 581 | return 0; |
| 582 | } |
| 583 | else if(!strcmp("--pidfile", argv[arg])) { |
| 584 | arg++; |
| 585 | if(argc>arg) |
| 586 | pidname = argv[arg++]; |
| 587 | } |
| 588 | else if(!strcmp("--portfile", argv[arg])) { |
| 589 | arg++; |
| 590 | if(argc>arg) |
| 591 | portname = argv[arg++]; |
| 592 | } |
| 593 | else if(!strcmp("--logfile", argv[arg])) { |
| 594 | arg++; |
| 595 | if(argc>arg) |
| 596 | serverlogfile = argv[arg++]; |
| 597 | } |
| 598 | else if(!strcmp("--ipv4", argv[arg])) { |
| 599 | #ifdef ENABLE_IPV6 |
| 600 | ipv_inuse = "IPv4"; |
| 601 | use_ipv6 = FALSE; |
| 602 | #endif |
| 603 | arg++; |
| 604 | } |
| 605 | else if(!strcmp("--ipv6", argv[arg])) { |
| 606 | #ifdef ENABLE_IPV6 |
| 607 | ipv_inuse = "IPv6"; |
| 608 | use_ipv6 = TRUE; |
| 609 | #endif |
| 610 | arg++; |
| 611 | } |
| 612 | else if(!strcmp("--port", argv[arg])) { |
| 613 | arg++; |
| 614 | if(argc>arg) { |
| 615 | char *endptr; |
| 616 | unsigned long ulnum = strtoul(argv[arg], &endptr, 10); |
| 617 | port = curlx_ultous(ulnum); |
| 618 | arg++; |
| 619 | } |
| 620 | } |
| 621 | else if(!strcmp("--srcdir", argv[arg])) { |
| 622 | arg++; |
| 623 | if(argc>arg) { |
| 624 | path = argv[arg]; |
| 625 | arg++; |
| 626 | } |
| 627 | } |
| 628 | else { |
| 629 | puts("Usage: tftpd [option]\n" |
| 630 | " --version\n" |
| 631 | " --logfile [file]\n" |
| 632 | " --pidfile [file]\n" |
| 633 | " --portfile [file]\n" |
| 634 | " --ipv4\n" |
| 635 | " --ipv6\n" |
| 636 | " --port [port]\n" |
| 637 | " --srcdir [path]"); |
| 638 | return 0; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | #ifdef WIN32 |
| 643 | win32_init(); |
| 644 | atexit(win32_cleanup); |
| 645 | #endif |
| 646 | |
| 647 | install_signal_handlers(true); |
| 648 | |
| 649 | #ifdef ENABLE_IPV6 |
| 650 | if(!use_ipv6) |
| 651 | #endif |
| 652 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 653 | #ifdef ENABLE_IPV6 |
| 654 | else |
| 655 | sock = socket(AF_INET6, SOCK_DGRAM, 0); |
| 656 | #endif |
| 657 | |
| 658 | if(CURL_SOCKET_BAD == sock) { |
| 659 | error = SOCKERRNO; |
| 660 | logmsg("Error creating socket: (%d) %s", |
| 661 | error, strerror(error)); |
| 662 | result = 1; |
| 663 | goto tftpd_cleanup; |
| 664 | } |
| 665 | |
| 666 | flag = 1; |
| 667 | if(0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, |
| 668 | (void *)&flag, sizeof(flag))) { |
| 669 | error = SOCKERRNO; |
| 670 | logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s", |
| 671 | error, strerror(error)); |
| 672 | result = 1; |
| 673 | goto tftpd_cleanup; |
| 674 | } |
| 675 | |
| 676 | #ifdef ENABLE_IPV6 |
| 677 | if(!use_ipv6) { |
| 678 | #endif |
| 679 | memset(&me.sa4, 0, sizeof(me.sa4)); |
| 680 | me.sa4.sin_family = AF_INET; |
| 681 | me.sa4.sin_addr.s_addr = INADDR_ANY; |
| 682 | me.sa4.sin_port = htons(port); |
| 683 | rc = bind(sock, &me.sa, sizeof(me.sa4)); |
| 684 | #ifdef ENABLE_IPV6 |
| 685 | } |
| 686 | else { |
| 687 | memset(&me.sa6, 0, sizeof(me.sa6)); |
| 688 | me.sa6.sin6_family = AF_INET6; |
| 689 | me.sa6.sin6_addr = in6addr_any; |
| 690 | me.sa6.sin6_port = htons(port); |
| 691 | rc = bind(sock, &me.sa, sizeof(me.sa6)); |
| 692 | } |
| 693 | #endif /* ENABLE_IPV6 */ |
| 694 | if(0 != rc) { |
| 695 | error = SOCKERRNO; |
| 696 | logmsg("Error binding socket on port %hu: (%d) %s", |
| 697 | port, error, strerror(error)); |
| 698 | result = 1; |
| 699 | goto tftpd_cleanup; |
| 700 | } |
| 701 | |
| 702 | if(!port) { |
| 703 | /* The system was supposed to choose a port number, figure out which |
| 704 | port we actually got and update the listener port value with it. */ |
| 705 | curl_socklen_t la_size; |
| 706 | srvr_sockaddr_union_t localaddr; |
| 707 | #ifdef ENABLE_IPV6 |
| 708 | if(!use_ipv6) |
| 709 | #endif |
| 710 | la_size = sizeof(localaddr.sa4); |
| 711 | #ifdef ENABLE_IPV6 |
| 712 | else |
| 713 | la_size = sizeof(localaddr.sa6); |
| 714 | #endif |
| 715 | memset(&localaddr.sa, 0, (size_t)la_size); |
| 716 | if(getsockname(sock, &localaddr.sa, &la_size) < 0) { |
| 717 | error = SOCKERRNO; |
| 718 | logmsg("getsockname() failed with error: (%d) %s", |
| 719 | error, strerror(error)); |
| 720 | sclose(sock); |
| 721 | goto tftpd_cleanup; |
| 722 | } |
| 723 | switch(localaddr.sa.sa_family) { |
| 724 | case AF_INET: |
| 725 | port = ntohs(localaddr.sa4.sin_port); |
| 726 | break; |
| 727 | #ifdef ENABLE_IPV6 |
| 728 | case AF_INET6: |
| 729 | port = ntohs(localaddr.sa6.sin6_port); |
| 730 | break; |
| 731 | #endif |
| 732 | default: |
| 733 | break; |
| 734 | } |
| 735 | if(!port) { |
| 736 | /* Real failure, listener port shall not be zero beyond this point. */ |
| 737 | logmsg("Apparently getsockname() succeeded, with listener port zero."); |
| 738 | logmsg("A valid reason for this failure is a binary built without"); |
| 739 | logmsg("proper network library linkage. This might not be the only"); |
| 740 | logmsg("reason, but double check it before anything else."); |
| 741 | result = 2; |
| 742 | goto tftpd_cleanup; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | wrotepidfile = write_pidfile(pidname); |
| 747 | if(!wrotepidfile) { |
| 748 | result = 1; |
| 749 | goto tftpd_cleanup; |
| 750 | } |
| 751 | |
| 752 | if(portname) { |
| 753 | wroteportfile = write_portfile(portname, port); |
| 754 | if(!wroteportfile) { |
| 755 | result = 1; |
| 756 | goto tftpd_cleanup; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port); |
| 761 | |
| 762 | for(;;) { |
| 763 | fromlen = sizeof(from); |
| 764 | #ifdef ENABLE_IPV6 |
| 765 | if(!use_ipv6) |
| 766 | #endif |
| 767 | fromlen = sizeof(from.sa4); |
| 768 | #ifdef ENABLE_IPV6 |
| 769 | else |
| 770 | fromlen = sizeof(from.sa6); |
| 771 | #endif |
| 772 | n = (ssize_t)recvfrom(sock, &buf.storage[0], sizeof(buf.storage), 0, |
| 773 | &from.sa, &fromlen); |
| 774 | if(got_exit_signal) |
| 775 | break; |
| 776 | if(n < 0) { |
| 777 | logmsg("recvfrom"); |
| 778 | result = 3; |
| 779 | break; |
| 780 | } |
| 781 | |
| 782 | set_advisor_read_lock(SERVERLOGS_LOCK); |
| 783 | serverlogslocked = 1; |
| 784 | |
| 785 | #ifdef ENABLE_IPV6 |
| 786 | if(!use_ipv6) { |
| 787 | #endif |
| 788 | from.sa4.sin_family = AF_INET; |
| 789 | peer = socket(AF_INET, SOCK_DGRAM, 0); |
| 790 | if(CURL_SOCKET_BAD == peer) { |
| 791 | logmsg("socket"); |
| 792 | result = 2; |
| 793 | break; |
| 794 | } |
| 795 | if(connect(peer, &from.sa, sizeof(from.sa4)) < 0) { |
| 796 | logmsg("connect: fail"); |
| 797 | result = 1; |
| 798 | break; |
| 799 | } |
| 800 | #ifdef ENABLE_IPV6 |
| 801 | } |
| 802 | else { |
| 803 | from.sa6.sin6_family = AF_INET6; |
| 804 | peer = socket(AF_INET6, SOCK_DGRAM, 0); |
| 805 | if(CURL_SOCKET_BAD == peer) { |
| 806 | logmsg("socket"); |
| 807 | result = 2; |
| 808 | break; |
| 809 | } |
| 810 | if(connect(peer, &from.sa, sizeof(from.sa6)) < 0) { |
| 811 | logmsg("connect: fail"); |
| 812 | result = 1; |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | #endif |
| 817 | |
| 818 | maxtimeout = 5*TIMEOUT; |
| 819 | |
| 820 | tp = &buf.hdr; |
| 821 | tp->th_opcode = ntohs(tp->th_opcode); |
| 822 | if(tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) { |
| 823 | memset(&test, 0, sizeof(test)); |
| 824 | if(do_tftp(&test, tp, n) < 0) |
| 825 | break; |
| 826 | free(test.buffer); |
| 827 | } |
| 828 | sclose(peer); |
| 829 | peer = CURL_SOCKET_BAD; |
| 830 | |
| 831 | if(got_exit_signal) |
| 832 | break; |
| 833 | |
| 834 | if(serverlogslocked) { |
| 835 | serverlogslocked = 0; |
| 836 | clear_advisor_read_lock(SERVERLOGS_LOCK); |
| 837 | } |
| 838 | |
| 839 | logmsg("end of one transfer"); |
| 840 | |
| 841 | } |
| 842 | |
| 843 | tftpd_cleanup: |
| 844 | |
| 845 | if(test.ofile > 0) |
| 846 | close(test.ofile); |
| 847 | |
| 848 | if((peer != sock) && (peer != CURL_SOCKET_BAD)) |
| 849 | sclose(peer); |
| 850 | |
| 851 | if(sock != CURL_SOCKET_BAD) |
| 852 | sclose(sock); |
| 853 | |
| 854 | if(got_exit_signal) |
| 855 | logmsg("signalled to die"); |
| 856 | |
| 857 | if(wrotepidfile) |
| 858 | unlink(pidname); |
| 859 | if(wroteportfile) |
| 860 | unlink(portname); |
| 861 | |
| 862 | if(serverlogslocked) { |
| 863 | serverlogslocked = 0; |
| 864 | clear_advisor_read_lock(SERVERLOGS_LOCK); |
| 865 | } |
| 866 | |
| 867 | restore_signal_handlers(true); |
| 868 | |
| 869 | if(got_exit_signal) { |
| 870 | logmsg("========> %s tftpd (port: %d pid: %ld) exits with signal (%d)", |
| 871 | ipv_inuse, (int)port, (long)getpid(), exit_signal); |
| 872 | /* |
| 873 | * To properly set the return status of the process we |
| 874 | * must raise the same signal SIGINT or SIGTERM that we |
| 875 | * caught and let the old handler take care of it. |
| 876 | */ |
| 877 | raise(exit_signal); |
| 878 | } |
| 879 | |
| 880 | logmsg("========> tftpd quits"); |
| 881 | return result; |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Handle initial connection protocol. |
| 886 | */ |
| 887 | static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size) |
| 888 | { |
| 889 | char *cp; |
| 890 | int first = 1, ecode; |
| 891 | const struct formats *pf; |
| 892 | char *filename, *mode = NULL; |
| 893 | #ifdef USE_WINSOCK |
| 894 | DWORD recvtimeout, recvtimeoutbak; |
| 895 | #endif |
| 896 | const char *option = "mode"; /* mode is implicit */ |
| 897 | int toggle = 1; |
| 898 | |
| 899 | /* Open request dump file. */ |
| 900 | FILE *server = fopen(REQUEST_DUMP, "ab"); |
| 901 | if(!server) { |
| 902 | int error = errno; |
| 903 | logmsg("fopen() failed with error: %d %s", error, strerror(error)); |
| 904 | logmsg("Error opening file: %s", REQUEST_DUMP); |
| 905 | return -1; |
| 906 | } |
| 907 | |
| 908 | /* store input protocol */ |
| 909 | fprintf(server, "opcode = %x\n", tp->th_opcode); |
| 910 | |
| 911 | cp = (char *)&tp->th_stuff; |
| 912 | filename = cp; |
| 913 | do { |
| 914 | bool endofit = true; |
| 915 | while(cp < &buf.storage[size]) { |
| 916 | if(*cp == '\0') { |
| 917 | endofit = false; |
| 918 | break; |
| 919 | } |
| 920 | cp++; |
| 921 | } |
| 922 | if(endofit) |
| 923 | /* no more options */ |
| 924 | break; |
| 925 | |
| 926 | /* before increasing pointer, make sure it is still within the legal |
| 927 | space */ |
| 928 | if((cp + 1) < &buf.storage[size]) { |
| 929 | ++cp; |
| 930 | if(first) { |
| 931 | /* store the mode since we need it later */ |
| 932 | mode = cp; |
| 933 | first = 0; |
| 934 | } |
| 935 | if(toggle) |
| 936 | /* name/value pair: */ |
| 937 | fprintf(server, "%s = %s\n", option, cp); |
| 938 | else { |
| 939 | /* store the name pointer */ |
| 940 | option = cp; |
| 941 | } |
| 942 | toggle ^= 1; |
| 943 | } |
| 944 | else |
| 945 | /* No more options */ |
| 946 | break; |
| 947 | } while(1); |
| 948 | |
| 949 | if(*cp) { |
| 950 | nak(EBADOP); |
| 951 | fclose(server); |
| 952 | return 3; |
| 953 | } |
| 954 | |
| 955 | /* store input protocol */ |
| 956 | fprintf(server, "filename = %s\n", filename); |
| 957 | |
| 958 | for(cp = mode; cp && *cp; cp++) |
| 959 | if(ISUPPER(*cp)) |
| 960 | *cp = (char)tolower((int)*cp); |
| 961 | |
| 962 | /* store input protocol */ |
| 963 | fclose(server); |
| 964 | |
| 965 | for(pf = formata; pf->f_mode; pf++) |
| 966 | if(strcmp(pf->f_mode, mode) == 0) |
| 967 | break; |
| 968 | if(!pf->f_mode) { |
| 969 | nak(EBADOP); |
| 970 | return 2; |
| 971 | } |
| 972 | ecode = validate_access(test, filename, tp->th_opcode); |
| 973 | if(ecode) { |
| 974 | nak(ecode); |
| 975 | return 1; |
| 976 | } |
| 977 | |
| 978 | #ifdef USE_WINSOCK |
| 979 | recvtimeout = sizeof(recvtimeoutbak); |
| 980 | getsockopt(peer, SOL_SOCKET, SO_RCVTIMEO, |
| 981 | (char *)&recvtimeoutbak, (int *)&recvtimeout); |
| 982 | recvtimeout = TIMEOUT*1000; |
| 983 | setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO, |
| 984 | (const char *)&recvtimeout, sizeof(recvtimeout)); |
| 985 | #endif |
| 986 | |
| 987 | if(tp->th_opcode == opcode_WRQ) |
| 988 | recvtftp(test, pf); |
| 989 | else |
| 990 | sendtftp(test, pf); |
| 991 | |
| 992 | #ifdef USE_WINSOCK |
| 993 | recvtimeout = recvtimeoutbak; |
| 994 | setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO, |
| 995 | (const char *)&recvtimeout, sizeof(recvtimeout)); |
| 996 | #endif |
| 997 | |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | /* Based on the testno, parse the correct server commands. */ |
| 1002 | static int parse_servercmd(struct testcase *req) |
| 1003 | { |
| 1004 | FILE *stream; |
| 1005 | int error; |
| 1006 | |
| 1007 | stream = test2fopen(req->testno); |
| 1008 | if(!stream) { |
| 1009 | error = errno; |
| 1010 | logmsg("fopen() failed with error: %d %s", error, strerror(error)); |
| 1011 | logmsg(" Couldn't open test file %ld", req->testno); |
| 1012 | return 1; /* done */ |
| 1013 | } |
| 1014 | else { |
| 1015 | char *orgcmd = NULL; |
| 1016 | char *cmd = NULL; |
| 1017 | size_t cmdsize = 0; |
| 1018 | int num = 0; |
| 1019 | |
| 1020 | /* get the custom server control "commands" */ |
| 1021 | error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream); |
| 1022 | fclose(stream); |
| 1023 | if(error) { |
| 1024 | logmsg("getpart() failed with error: %d", error); |
| 1025 | return 1; /* done */ |
| 1026 | } |
| 1027 | |
| 1028 | cmd = orgcmd; |
| 1029 | while(cmd && cmdsize) { |
| 1030 | char *check; |
| 1031 | if(1 == sscanf(cmd, "writedelay: %d", &num)) { |
| 1032 | logmsg("instructed to delay %d secs between packets", num); |
| 1033 | req->writedelay = num; |
| 1034 | } |
| 1035 | else { |
| 1036 | logmsg("Unknown <servercmd> instruction found: %s", cmd); |
| 1037 | } |
| 1038 | /* try to deal with CRLF or just LF */ |
| 1039 | check = strchr(cmd, '\r'); |
| 1040 | if(!check) |
| 1041 | check = strchr(cmd, '\n'); |
| 1042 | |
| 1043 | if(check) { |
| 1044 | /* get to the letter following the newline */ |
| 1045 | while((*check == '\r') || (*check == '\n')) |
| 1046 | check++; |
| 1047 | |
| 1048 | if(!*check) |
| 1049 | /* if we reached a zero, get out */ |
| 1050 | break; |
| 1051 | cmd = check; |
| 1052 | } |
| 1053 | else |
| 1054 | break; |
| 1055 | } |
| 1056 | free(orgcmd); |
| 1057 | } |
| 1058 | |
| 1059 | return 0; /* OK! */ |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | /* |
| 1064 | * Validate file access. |
| 1065 | */ |
| 1066 | static int validate_access(struct testcase *test, |
| 1067 | const char *filename, int mode) |
| 1068 | { |
| 1069 | char *ptr; |
| 1070 | |
| 1071 | logmsg("trying to get file: %s mode %x", filename, mode); |
| 1072 | |
| 1073 | if(!strncmp("verifiedserver", filename, 14)) { |
| 1074 | char weare[128]; |
| 1075 | size_t count = msnprintf(weare, sizeof(weare), "WE ROOLZ: %" |
| 1076 | CURL_FORMAT_CURL_OFF_T "\r\n", our_getpid()); |
| 1077 | |
| 1078 | logmsg("Are-we-friendly question received"); |
| 1079 | test->buffer = strdup(weare); |
| 1080 | test->rptr = test->buffer; /* set read pointer */ |
| 1081 | test->bufsize = count; /* set total count */ |
| 1082 | test->rcount = count; /* set data left to read */ |
| 1083 | return 0; /* fine */ |
| 1084 | } |
| 1085 | |
| 1086 | /* find the last slash */ |
| 1087 | ptr = strrchr(filename, '/'); |
| 1088 | |
| 1089 | if(ptr) { |
| 1090 | char partbuf[80]="data"; |
| 1091 | long partno; |
| 1092 | long testno; |
| 1093 | FILE *stream; |
| 1094 | |
| 1095 | ptr++; /* skip the slash */ |
| 1096 | |
| 1097 | /* skip all non-numericals following the slash */ |
| 1098 | while(*ptr && !ISDIGIT(*ptr)) |
| 1099 | ptr++; |
| 1100 | |
| 1101 | /* get the number */ |
| 1102 | testno = strtol(ptr, &ptr, 10); |
| 1103 | |
| 1104 | if(testno > 10000) { |
| 1105 | partno = testno % 10000; |
| 1106 | testno /= 10000; |
| 1107 | } |
| 1108 | else |
| 1109 | partno = 0; |
| 1110 | |
| 1111 | |
| 1112 | logmsg("requested test number %ld part %ld", testno, partno); |
| 1113 | |
| 1114 | test->testno = testno; |
| 1115 | |
| 1116 | (void)parse_servercmd(test); |
| 1117 | |
| 1118 | stream = test2fopen(testno); |
| 1119 | |
| 1120 | if(0 != partno) |
| 1121 | msnprintf(partbuf, sizeof(partbuf), "data%ld", partno); |
| 1122 | |
| 1123 | if(!stream) { |
| 1124 | int error = errno; |
| 1125 | logmsg("fopen() failed with error: %d %s", error, strerror(error)); |
| 1126 | logmsg("Couldn't open test file for test : %d", testno); |
| 1127 | return EACCESS; |
| 1128 | } |
| 1129 | else { |
| 1130 | size_t count; |
| 1131 | int error = getpart(&test->buffer, &count, "reply", partbuf, stream); |
| 1132 | fclose(stream); |
| 1133 | if(error) { |
| 1134 | logmsg("getpart() failed with error: %d", error); |
| 1135 | return EACCESS; |
| 1136 | } |
| 1137 | if(test->buffer) { |
| 1138 | test->rptr = test->buffer; /* set read pointer */ |
| 1139 | test->bufsize = count; /* set total count */ |
| 1140 | test->rcount = count; /* set data left to read */ |
| 1141 | } |
| 1142 | else |
| 1143 | return EACCESS; |
| 1144 | } |
| 1145 | } |
| 1146 | else { |
| 1147 | logmsg("no slash found in path"); |
| 1148 | return EACCESS; /* failure */ |
| 1149 | } |
| 1150 | |
| 1151 | logmsg("file opened and all is good"); |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
| 1156 | * Send the requested file. |
| 1157 | */ |
| 1158 | static void sendtftp(struct testcase *test, const struct formats *pf) |
| 1159 | { |
| 1160 | int size; |
| 1161 | ssize_t n; |
| 1162 | /* These are volatile to live through a siglongjmp */ |
| 1163 | volatile unsigned short sendblock; /* block count */ |
| 1164 | struct tftphdr * volatile sdp = r_init(); /* data buffer */ |
| 1165 | struct tftphdr * const sap = &ackbuf.hdr; /* ack buffer */ |
| 1166 | |
| 1167 | sendblock = 1; |
| 1168 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 1169 | mysignal(SIGALRM, timer); |
| 1170 | #endif |
| 1171 | do { |
| 1172 | size = readit(test, (struct tftphdr **)&sdp, pf->f_convert); |
| 1173 | if(size < 0) { |
| 1174 | nak(errno + 100); |
| 1175 | return; |
| 1176 | } |
| 1177 | sdp->th_opcode = htons((unsigned short)opcode_DATA); |
| 1178 | sdp->th_block = htons(sendblock); |
| 1179 | timeout = 0; |
| 1180 | #ifdef HAVE_SIGSETJMP |
| 1181 | (void) sigsetjmp(timeoutbuf, 1); |
| 1182 | #endif |
| 1183 | if(test->writedelay) { |
| 1184 | logmsg("Pausing %d seconds before %d bytes", test->writedelay, |
| 1185 | size); |
| 1186 | wait_ms(1000*test->writedelay); |
| 1187 | } |
| 1188 | |
| 1189 | send_data: |
| 1190 | logmsg("write"); |
| 1191 | if(swrite(peer, sdp, size + 4) != size + 4) { |
| 1192 | logmsg("write: fail"); |
| 1193 | return; |
| 1194 | } |
| 1195 | read_ahead(test, pf->f_convert); |
| 1196 | for(;;) { |
| 1197 | #ifdef HAVE_ALARM |
| 1198 | alarm(rexmtval); /* read the ack */ |
| 1199 | #endif |
| 1200 | logmsg("read"); |
| 1201 | n = sread(peer, &ackbuf.storage[0], sizeof(ackbuf.storage)); |
| 1202 | logmsg("read: %zd", n); |
| 1203 | #ifdef HAVE_ALARM |
| 1204 | alarm(0); |
| 1205 | #endif |
| 1206 | if(got_exit_signal) |
| 1207 | return; |
| 1208 | if(n < 0) { |
| 1209 | logmsg("read: fail"); |
| 1210 | return; |
| 1211 | } |
| 1212 | sap->th_opcode = ntohs((unsigned short)sap->th_opcode); |
| 1213 | sap->th_block = ntohs(sap->th_block); |
| 1214 | |
| 1215 | if(sap->th_opcode == opcode_ERROR) { |
| 1216 | logmsg("got ERROR"); |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | if(sap->th_opcode == opcode_ACK) { |
| 1221 | if(sap->th_block == sendblock) { |
| 1222 | break; |
| 1223 | } |
| 1224 | /* Re-synchronize with the other side */ |
| 1225 | (void) synchnet(peer); |
| 1226 | if(sap->th_block == (sendblock-1)) { |
| 1227 | goto send_data; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | } |
| 1232 | sendblock++; |
| 1233 | } while(size == SEGSIZE); |
| 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * Receive a file. |
| 1238 | */ |
| 1239 | static void recvtftp(struct testcase *test, const struct formats *pf) |
| 1240 | { |
| 1241 | ssize_t n, size; |
| 1242 | /* These are volatile to live through a siglongjmp */ |
| 1243 | volatile unsigned short recvblock; /* block count */ |
| 1244 | struct tftphdr * volatile rdp; /* data buffer */ |
| 1245 | struct tftphdr *rap; /* ack buffer */ |
| 1246 | |
| 1247 | recvblock = 0; |
| 1248 | rdp = w_init(); |
| 1249 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 1250 | mysignal(SIGALRM, timer); |
| 1251 | #endif |
| 1252 | rap = &ackbuf.hdr; |
| 1253 | do { |
| 1254 | timeout = 0; |
| 1255 | rap->th_opcode = htons((unsigned short)opcode_ACK); |
| 1256 | rap->th_block = htons(recvblock); |
| 1257 | recvblock++; |
| 1258 | #ifdef HAVE_SIGSETJMP |
| 1259 | (void) sigsetjmp(timeoutbuf, 1); |
| 1260 | #endif |
| 1261 | send_ack: |
| 1262 | logmsg("write"); |
| 1263 | if(swrite(peer, &ackbuf.storage[0], 4) != 4) { |
| 1264 | logmsg("write: fail"); |
| 1265 | goto abort; |
| 1266 | } |
| 1267 | write_behind(test, pf->f_convert); |
| 1268 | for(;;) { |
| 1269 | #ifdef HAVE_ALARM |
| 1270 | alarm(rexmtval); |
| 1271 | #endif |
| 1272 | logmsg("read"); |
| 1273 | n = sread(peer, rdp, PKTSIZE); |
| 1274 | logmsg("read: %zd", n); |
| 1275 | #ifdef HAVE_ALARM |
| 1276 | alarm(0); |
| 1277 | #endif |
| 1278 | if(got_exit_signal) |
| 1279 | goto abort; |
| 1280 | if(n < 0) { /* really? */ |
| 1281 | logmsg("read: fail"); |
| 1282 | goto abort; |
| 1283 | } |
| 1284 | rdp->th_opcode = ntohs((unsigned short)rdp->th_opcode); |
| 1285 | rdp->th_block = ntohs(rdp->th_block); |
| 1286 | if(rdp->th_opcode == opcode_ERROR) |
| 1287 | goto abort; |
| 1288 | if(rdp->th_opcode == opcode_DATA) { |
| 1289 | if(rdp->th_block == recvblock) { |
| 1290 | break; /* normal */ |
| 1291 | } |
| 1292 | /* Re-synchronize with the other side */ |
| 1293 | (void) synchnet(peer); |
| 1294 | if(rdp->th_block == (recvblock-1)) |
| 1295 | goto send_ack; /* rexmit */ |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | size = writeit(test, &rdp, (int)(n - 4), pf->f_convert); |
| 1300 | if(size != (n-4)) { /* ahem */ |
| 1301 | if(size < 0) |
| 1302 | nak(errno + 100); |
| 1303 | else |
| 1304 | nak(ENOSPACE); |
| 1305 | goto abort; |
| 1306 | } |
| 1307 | } while(size == SEGSIZE); |
| 1308 | write_behind(test, pf->f_convert); |
| 1309 | /* close the output file as early as possible after upload completion */ |
| 1310 | if(test->ofile > 0) { |
| 1311 | close(test->ofile); |
| 1312 | test->ofile = 0; |
| 1313 | } |
| 1314 | |
| 1315 | rap->th_opcode = htons((unsigned short)opcode_ACK); /* send the "final" |
| 1316 | ack */ |
| 1317 | rap->th_block = htons(recvblock); |
| 1318 | (void) swrite(peer, &ackbuf.storage[0], 4); |
| 1319 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 1320 | mysignal(SIGALRM, justtimeout); /* just abort read on timeout */ |
| 1321 | alarm(rexmtval); |
| 1322 | #endif |
| 1323 | /* normally times out and quits */ |
| 1324 | n = sread(peer, &buf.storage[0], sizeof(buf.storage)); |
| 1325 | #ifdef HAVE_ALARM |
| 1326 | alarm(0); |
| 1327 | #endif |
| 1328 | if(got_exit_signal) |
| 1329 | goto abort; |
| 1330 | if(n >= 4 && /* if read some data */ |
| 1331 | rdp->th_opcode == opcode_DATA && /* and got a data block */ |
| 1332 | recvblock == rdp->th_block) { /* then my last ack was lost */ |
| 1333 | (void) swrite(peer, &ackbuf.storage[0], 4); /* resend final ack */ |
| 1334 | } |
| 1335 | abort: |
| 1336 | /* make sure the output file is closed in case of abort */ |
| 1337 | if(test->ofile > 0) { |
| 1338 | close(test->ofile); |
| 1339 | test->ofile = 0; |
| 1340 | } |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * Send a nak packet (error message). Error code passed in is one of the |
| 1346 | * standard TFTP codes, or a Unix errno offset by 100. |
| 1347 | */ |
| 1348 | static void nak(int error) |
| 1349 | { |
| 1350 | struct tftphdr *tp; |
| 1351 | int length; |
| 1352 | struct errmsg *pe; |
| 1353 | |
| 1354 | tp = &buf.hdr; |
| 1355 | tp->th_opcode = htons((unsigned short)opcode_ERROR); |
| 1356 | tp->th_code = htons((unsigned short)error); |
| 1357 | for(pe = errmsgs; pe->e_code >= 0; pe++) |
| 1358 | if(pe->e_code == error) |
| 1359 | break; |
| 1360 | if(pe->e_code < 0) { |
| 1361 | pe->e_msg = strerror(error - 100); |
| 1362 | tp->th_code = EUNDEF; /* set 'undef' errorcode */ |
| 1363 | } |
| 1364 | length = (int)strlen(pe->e_msg); |
| 1365 | |
| 1366 | /* we use memcpy() instead of strcpy() in order to avoid buffer overflow |
| 1367 | * report from glibc with FORTIFY_SOURCE */ |
| 1368 | memcpy(tp->th_msg, pe->e_msg, length + 1); |
| 1369 | length += 5; |
| 1370 | if(swrite(peer, &buf.storage[0], length) != length) |
| 1371 | logmsg("nak: fail\n"); |
| 1372 | } |