1
0

tftpd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * Trivial file transfer protocol server.
  11. *
  12. * This code includes many modifications by Jim Guyton <guyton@rand-unix>
  13. *
  14. * This source file was started based on netkit-tftpd 0.17
  15. * Heavily modified for curl's test suite
  16. */
  17. /*
  18. * Copyright (c) 1983 Regents of the University of California.
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. * 1. Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. * 3. All advertising materials mentioning features or use of this software
  30. * must display the following acknowledgement:
  31. * This product includes software developed by the University of
  32. * California, Berkeley and its contributors.
  33. * 4. Neither the name of the University nor the names of its contributors
  34. * may be used to endorse or promote products derived from this software
  35. * without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  38. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  43. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. */
  49. #include "setup.h" /* portability help from the lib directory */
  50. #ifdef HAVE_SYS_IOCTL_H
  51. #include <sys/ioctl.h>
  52. #endif
  53. #ifdef HAVE_SIGNAL_H
  54. #include <signal.h>
  55. #endif
  56. #ifdef HAVE_FCNTL_H
  57. #include <fcntl.h>
  58. #endif
  59. #ifdef HAVE_SYS_SOCKET_H
  60. #include <sys/socket.h>
  61. #endif
  62. #ifdef HAVE_NETINET_IN_H
  63. #include <netinet/in.h>
  64. #endif
  65. #ifdef HAVE_ARPA_TFTP_H
  66. #include <arpa/tftp.h>
  67. #else
  68. #include "tftp.h"
  69. #endif
  70. #ifdef HAVE_NETDB_H
  71. #include <netdb.h>
  72. #endif
  73. #ifdef HAVE_SYS_FILIO_H
  74. /* FIONREAD on Solaris 7 */
  75. #include <sys/filio.h>
  76. #endif
  77. #include <setjmp.h>
  78. #ifdef HAVE_UNISTD_H
  79. #include <unistd.h>
  80. #endif
  81. #ifdef HAVE_PWD_H
  82. #include <pwd.h>
  83. #endif
  84. #define ENABLE_CURLX_PRINTF
  85. /* make the curlx header define all printf() functions to use the curlx_*
  86. versions instead */
  87. #include "curlx.h" /* from the private lib dir */
  88. #include "getpart.h"
  89. #include "util.h"
  90. /* include memdebug.h last */
  91. #include "memdebug.h"
  92. struct testcase {
  93. char *buffer; /* holds the file data to send to the client */
  94. size_t bufsize; /* size of the data in buffer */
  95. char *rptr; /* read pointer into the buffer */
  96. size_t rcount; /* amount of data left to read of the file */
  97. long num; /* test case number */
  98. int ofile; /* file descriptor for output file when uploading to us */
  99. };
  100. static int synchnet(curl_socket_t);
  101. static struct tftphdr *r_init(void);
  102. static struct tftphdr *w_init(void);
  103. static int readit(struct testcase *test, struct tftphdr **dpp, int convert);
  104. static int writeit(struct testcase *test, struct tftphdr **dpp, int ct,
  105. int convert);
  106. static void mysignal(int, void (*func)(int));
  107. #define TIMEOUT 5
  108. #define PKTSIZE SEGSIZE+4
  109. struct formats;
  110. static int tftp(struct testcase *test, struct tftphdr *tp, ssize_t size);
  111. static void nak(int error);
  112. static void sendtftp(struct testcase *test, struct formats *pf);
  113. static void recvtftp(struct testcase *test, struct formats *pf);
  114. static int validate_access(struct testcase *test, const char *, int);
  115. static curl_socket_t peer;
  116. static int rexmtval = TIMEOUT;
  117. static int maxtimeout = 5*TIMEOUT;
  118. static char buf[PKTSIZE];
  119. static char ackbuf[PKTSIZE];
  120. static struct sockaddr_in from;
  121. static socklen_t fromlen;
  122. struct bf {
  123. int counter; /* size of data in buffer, or flag */
  124. char buf[PKTSIZE]; /* room for data packet */
  125. } bfs[2];
  126. /* Values for bf.counter */
  127. #define BF_ALLOC -3 /* alloc'd but not yet filled */
  128. #define BF_FREE -2 /* free */
  129. /* [-1 .. SEGSIZE] = size of data in the data buffer */
  130. static int nextone; /* index of next buffer to use */
  131. static int current; /* index of buffer in use */
  132. /* control flags for crlf conversions */
  133. int newline = 0; /* fillbuf: in middle of newline expansion */
  134. int prevchar = -1; /* putbuf: previous char (cr check) */
  135. static void read_ahead(struct testcase *test,
  136. int convert /* if true, convert to ascii */);
  137. static ssize_t write_behind(struct testcase *test, int convert);
  138. static struct tftphdr *rw_init(int);
  139. static struct tftphdr *w_init(void) { return rw_init(0); } /* write-behind */
  140. static struct tftphdr *r_init(void) { return rw_init(1); } /* read-ahead */
  141. static struct tftphdr *
  142. rw_init(int x) /* init for either read-ahead or write-behind */
  143. { /* zero for write-behind, one for read-head */
  144. newline = 0; /* init crlf flag */
  145. prevchar = -1;
  146. bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
  147. current = 0;
  148. bfs[1].counter = BF_FREE;
  149. nextone = x; /* ahead or behind? */
  150. return (struct tftphdr *)bfs[0].buf;
  151. }
  152. /* Have emptied current buffer by sending to net and getting ack.
  153. Free it and return next buffer filled with data.
  154. */
  155. static int readit(struct testcase *test, struct tftphdr **dpp,
  156. int convert /* if true, convert to ascii */)
  157. {
  158. struct bf *b;
  159. bfs[current].counter = BF_FREE; /* free old one */
  160. current = !current; /* "incr" current */
  161. b = &bfs[current]; /* look at new buffer */
  162. if (b->counter == BF_FREE) /* if it's empty */
  163. read_ahead(test, convert); /* fill it */
  164. *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
  165. return b->counter;
  166. }
  167. #undef MIN /* some systems have this defined already, some don't */
  168. #define MIN(x,y) ((x)<(y)?(x):(y));
  169. /*
  170. * fill the input buffer, doing ascii conversions if requested
  171. * conversions are lf -> cr,lf and cr -> cr, nul
  172. */
  173. static void read_ahead(struct testcase *test,
  174. int convert /* if true, convert to ascii */)
  175. {
  176. int i;
  177. char *p;
  178. int c;
  179. struct bf *b;
  180. struct tftphdr *dp;
  181. b = &bfs[nextone]; /* look at "next" buffer */
  182. if (b->counter != BF_FREE) /* nop if not free */
  183. return;
  184. nextone = !nextone; /* "incr" next buffer ptr */
  185. dp = (struct tftphdr *)b->buf;
  186. if (convert == 0) {
  187. /* The former file reading code did this:
  188. b->counter = read(fileno(file), dp->th_data, SEGSIZE); */
  189. size_t copy_n = MIN(SEGSIZE, test->rcount);
  190. memcpy(dp->th_data, test->rptr, copy_n);
  191. /* decrease amount, advance pointer */
  192. test->rcount -= copy_n;
  193. test->rptr += copy_n;
  194. b->counter = (int)copy_n;
  195. return;
  196. }
  197. p = dp->th_data;
  198. for (i = 0 ; i < SEGSIZE; i++) {
  199. if (newline) {
  200. if (prevchar == '\n')
  201. c = '\n'; /* lf to cr,lf */
  202. else
  203. c = '\0'; /* cr to cr,nul */
  204. newline = 0;
  205. }
  206. else {
  207. if(test->rcount) {
  208. c=test->rptr[0];
  209. test->rptr++;
  210. test->rcount--;
  211. }
  212. else
  213. break;
  214. if (c == '\n' || c == '\r') {
  215. prevchar = c;
  216. c = '\r';
  217. newline = 1;
  218. }
  219. }
  220. *p++ = (char)c;
  221. }
  222. b->counter = (int)(p - dp->th_data);
  223. }
  224. /* Update count associated with the buffer, get new buffer from the queue.
  225. Calls write_behind only if next buffer not available.
  226. */
  227. static int writeit(struct testcase *test, struct tftphdr **dpp,
  228. int ct, int convert)
  229. {
  230. bfs[current].counter = ct; /* set size of data to write */
  231. current = !current; /* switch to other buffer */
  232. if (bfs[current].counter != BF_FREE) /* if not free */
  233. write_behind(test, convert); /* flush it */
  234. bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
  235. *dpp = (struct tftphdr *)bfs[current].buf;
  236. return ct; /* this is a lie of course */
  237. }
  238. /*
  239. * Output a buffer to a file, converting from netascii if requested.
  240. * CR,NUL -> CR and CR,LF => LF.
  241. * Note spec is undefined if we get CR as last byte of file or a
  242. * CR followed by anything else. In this case we leave it alone.
  243. */
  244. static ssize_t write_behind(struct testcase *test, int convert)
  245. {
  246. char *writebuf;
  247. int count;
  248. int ct;
  249. char *p;
  250. int c; /* current character */
  251. struct bf *b;
  252. struct tftphdr *dp;
  253. b = &bfs[nextone];
  254. if (b->counter < -1) /* anything to flush? */
  255. return 0; /* just nop if nothing to do */
  256. if(!test->ofile) {
  257. char outfile[256];
  258. snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->num);
  259. test->ofile=open(outfile, O_CREAT|O_RDWR, 0777);
  260. if(test->ofile == -1) {
  261. logmsg("Couldn't create and/or open file %s for upload!", outfile);
  262. return -1; /* failure! */
  263. }
  264. }
  265. count = b->counter; /* remember byte count */
  266. b->counter = BF_FREE; /* reset flag */
  267. dp = (struct tftphdr *)b->buf;
  268. nextone = !nextone; /* incr for next time */
  269. writebuf = dp->th_data;
  270. if (count <= 0)
  271. return -1; /* nak logic? */
  272. if (convert == 0)
  273. return write(test->ofile, writebuf, count);
  274. p = writebuf;
  275. ct = count;
  276. while (ct--) { /* loop over the buffer */
  277. c = *p++; /* pick up a character */
  278. if (prevchar == '\r') { /* if prev char was cr */
  279. if (c == '\n') /* if have cr,lf then just */
  280. lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */
  281. else
  282. if (c == '\0') /* if have cr,nul then */
  283. goto skipit; /* just skip over the putc */
  284. /* else just fall through and allow it */
  285. }
  286. /* formerly
  287. putc(c, file); */
  288. write(test->ofile, &c, 1);
  289. skipit:
  290. prevchar = c;
  291. }
  292. return count;
  293. }
  294. /* When an error has occurred, it is possible that the two sides are out of
  295. * synch. Ie: that what I think is the other side's response to packet N is
  296. * really their response to packet N-1.
  297. *
  298. * So, to try to prevent that, we flush all the input queued up for us on the
  299. * network connection on our host.
  300. *
  301. * We return the number of packets we flushed (mostly for reporting when trace
  302. * is active).
  303. */
  304. static int synchnet(curl_socket_t f /* socket to flush */)
  305. {
  306. #if defined(HAVE_IOCTLSOCKET)
  307. unsigned long i;
  308. #else
  309. int i;
  310. #endif
  311. int j = 0;
  312. char rbuf[PKTSIZE];
  313. struct sockaddr_in fromaddr;
  314. socklen_t fromaddrlen;
  315. while (1) {
  316. #if defined(HAVE_IOCTLSOCKET)
  317. (void) ioctlsocket(f, FIONREAD, &i);
  318. #else
  319. (void) ioctl(f, FIONREAD, &i);
  320. #endif
  321. if (i) {
  322. j++;
  323. fromaddrlen = sizeof(fromaddr);
  324. (void)recvfrom(f, rbuf, sizeof(rbuf), 0,
  325. (struct sockaddr *)&fromaddr, &fromaddrlen);
  326. }
  327. else
  328. break;
  329. }
  330. return j;
  331. }
  332. #if defined(HAVE_ALARM) && defined(SIGALRM)
  333. /*
  334. * Like signal(), but with well-defined semantics.
  335. */
  336. static void mysignal(int sig, void (*handler)(int))
  337. {
  338. struct sigaction sa;
  339. memset(&sa, 0, sizeof(sa));
  340. sa.sa_handler = handler;
  341. sigaction(sig, &sa, NULL);
  342. }
  343. #endif
  344. #ifndef DEFAULT_LOGFILE
  345. #define DEFAULT_LOGFILE "log/tftpd.log"
  346. #endif
  347. #define DEFAULT_PORT 8999 /* UDP */
  348. const char *serverlogfile = DEFAULT_LOGFILE;
  349. #define REQUEST_DUMP "log/server.input"
  350. char use_ipv6=FALSE;
  351. int main(int argc, char **argv)
  352. {
  353. struct sockaddr_in me;
  354. #ifdef ENABLE_IPV6
  355. struct sockaddr_in6 me6;
  356. #endif /* ENABLE_IPV6 */
  357. struct tftphdr *tp;
  358. ssize_t n = 0;
  359. int arg = 1;
  360. char *pidname= (char *)".tftpd.pid";
  361. unsigned short port = DEFAULT_PORT;
  362. curl_socket_t sock;
  363. int flag;
  364. int rc;
  365. struct testcase test;
  366. int result = 0;
  367. while(argc>arg) {
  368. if(!strcmp("--version", argv[arg])) {
  369. printf("tftpd IPv4%s\n",
  370. #ifdef ENABLE_IPV6
  371. "/IPv6"
  372. #else
  373. ""
  374. #endif
  375. );
  376. return 0;
  377. }
  378. else if(!strcmp("--pidfile", argv[arg])) {
  379. arg++;
  380. if(argc>arg)
  381. pidname = argv[arg++];
  382. }
  383. else if(!strcmp("--ipv6", argv[arg])) {
  384. #ifdef ENABLE_IPV6
  385. use_ipv6=TRUE;
  386. #endif
  387. arg++;
  388. }
  389. else if(argc>arg) {
  390. if(atoi(argv[arg]))
  391. port = (unsigned short)atoi(argv[arg++]);
  392. if(argc>arg)
  393. path = argv[arg++];
  394. }
  395. }
  396. #ifdef WIN32
  397. win32_init();
  398. atexit(win32_cleanup);
  399. #endif
  400. #ifdef ENABLE_IPV6
  401. if(!use_ipv6)
  402. #endif
  403. sock = socket(AF_INET, SOCK_DGRAM, 0);
  404. #ifdef ENABLE_IPV6
  405. else
  406. sock = socket(AF_INET6, SOCK_DGRAM, 0);
  407. #endif
  408. if (sock < 0) {
  409. perror("opening stream socket");
  410. logmsg("Error opening socket");
  411. return 1;
  412. }
  413. flag = 1;
  414. if (setsockopt
  415. (sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &flag,
  416. sizeof(int)) < 0) {
  417. perror("setsockopt(SO_REUSEADDR)");
  418. }
  419. #ifdef ENABLE_IPV6
  420. if(!use_ipv6) {
  421. #endif
  422. me.sin_family = AF_INET;
  423. me.sin_addr.s_addr = INADDR_ANY;
  424. me.sin_port = htons(port);
  425. rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
  426. #ifdef ENABLE_IPV6
  427. }
  428. else {
  429. memset(&me6, 0, sizeof(struct sockaddr_in6));
  430. me6.sin6_family = AF_INET6;
  431. me6.sin6_addr = in6addr_any;
  432. me6.sin6_port = htons(port);
  433. rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
  434. }
  435. #endif /* ENABLE_IPV6 */
  436. if(rc < 0) {
  437. perror("binding stream socket");
  438. logmsg("Error binding socket");
  439. sclose(sock);
  440. return 1;
  441. }
  442. if(!write_pidfile(pidname)) {
  443. sclose(sock);
  444. return 1;
  445. }
  446. logmsg("Running IPv%d version on port UDP/%d",
  447. #ifdef ENABLE_IPV6
  448. (use_ipv6?6:4)
  449. #else
  450. 4
  451. #endif
  452. , port );
  453. do {
  454. fromlen = sizeof(from);
  455. n = (ssize_t)recvfrom(sock, buf, sizeof(buf), 0,
  456. (struct sockaddr *)&from, &fromlen);
  457. if (n < 0) {
  458. logmsg("recvfrom:\n");
  459. result = 3;
  460. break;
  461. }
  462. set_advisor_read_lock(SERVERLOGS_LOCK);
  463. from.sin_family = AF_INET;
  464. peer = socket(AF_INET, SOCK_DGRAM, 0);
  465. if (peer < 0) {
  466. logmsg("socket:\n");
  467. result = 2;
  468. break;
  469. }
  470. if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
  471. logmsg("connect: fail\n");
  472. result = 1;
  473. break;
  474. }
  475. maxtimeout = 5*TIMEOUT;
  476. tp = (struct tftphdr *)buf;
  477. tp->th_opcode = ntohs(tp->th_opcode);
  478. if (tp->th_opcode == RRQ || tp->th_opcode == WRQ) {
  479. memset(&test, 0, sizeof(test));
  480. if (tftp(&test, tp, n) < 0)
  481. break;
  482. if(test.buffer)
  483. free(test.buffer);
  484. }
  485. sclose(peer);
  486. clear_advisor_read_lock(SERVERLOGS_LOCK);
  487. } while(1);
  488. clear_advisor_read_lock(SERVERLOGS_LOCK);
  489. return result;
  490. }
  491. struct formats {
  492. const char *f_mode;
  493. int f_convert;
  494. } formats[] = {
  495. { "netascii", 1 },
  496. { "octet", 0 },
  497. { NULL, 0 }
  498. };
  499. /*
  500. * Handle initial connection protocol.
  501. */
  502. static int tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
  503. {
  504. char *cp;
  505. int first = 1, ecode;
  506. struct formats *pf;
  507. char *filename, *mode = NULL;
  508. int error;
  509. FILE *server;
  510. /* Open request dump file. */
  511. server = fopen(REQUEST_DUMP, "ab");
  512. if(!server) {
  513. error = ERRNO;
  514. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  515. logmsg("Error opening file: %s", REQUEST_DUMP);
  516. return -1;
  517. }
  518. /* store input protocol */
  519. fprintf(server, "opcode: %x\n", tp->th_opcode);
  520. cp = (char *)&tp->th_stuff;
  521. filename = cp;
  522. again:
  523. while (cp < buf + size) {
  524. if (*cp == '\0')
  525. break;
  526. cp++;
  527. }
  528. if (*cp) {
  529. nak(EBADOP);
  530. fclose(server);
  531. return 3;
  532. }
  533. if (first) {
  534. mode = ++cp;
  535. first = 0;
  536. goto again;
  537. }
  538. /* store input protocol */
  539. fprintf(server, "filename: %s\n", filename);
  540. for (cp = mode; *cp; cp++)
  541. if(ISUPPER(*cp))
  542. *cp = (char)tolower((int)*cp);
  543. /* store input protocol */
  544. fprintf(server, "mode: %s\n", mode);
  545. fclose(server);
  546. for (pf = formats; pf->f_mode; pf++)
  547. if (strcmp(pf->f_mode, mode) == 0)
  548. break;
  549. if (!pf->f_mode) {
  550. nak(EBADOP);
  551. return 2;
  552. }
  553. ecode = validate_access(test, filename, tp->th_opcode);
  554. if (ecode) {
  555. nak(ecode);
  556. return 1;
  557. }
  558. if (tp->th_opcode == WRQ)
  559. recvtftp(test, pf);
  560. else
  561. sendtftp(test, pf);
  562. return 0;
  563. }
  564. /*
  565. * Validate file access.
  566. */
  567. static int validate_access(struct testcase *test,
  568. const char *filename, int mode)
  569. {
  570. char *ptr;
  571. long testno, partno;
  572. int error;
  573. char partbuf[80]="data";
  574. logmsg("trying to get file: %s mode %x", filename, mode);
  575. if(!strncmp("verifiedserver", filename, 14)) {
  576. char weare[128];
  577. size_t count = sprintf(weare, "WE ROOLZ: %ld\r\n", (long)getpid());
  578. logmsg("Are-we-friendly question received");
  579. test->buffer = strdup(weare);
  580. test->rptr = test->buffer; /* set read pointer */
  581. test->bufsize = count; /* set total count */
  582. test->rcount = count; /* set data left to read */
  583. return 0; /* fine */
  584. }
  585. /* find the last slash */
  586. ptr = strrchr(filename, '/');
  587. if(ptr) {
  588. char *file;
  589. ptr++; /* skip the slash */
  590. /* skip all non-numericals following the slash */
  591. while(*ptr && !ISDIGIT(*ptr))
  592. ptr++;
  593. /* get the number */
  594. testno = strtol(ptr, &ptr, 10);
  595. if(testno > 10000) {
  596. partno = testno % 10000;
  597. testno /= 10000;
  598. }
  599. else
  600. partno = 0;
  601. logmsg("requested test number %ld part %ld", testno, partno);
  602. test->num = testno;
  603. file = test2file(testno);
  604. if(0 != partno)
  605. sprintf(partbuf, "data%ld", partno);
  606. if(file) {
  607. FILE *stream=fopen(file, "rb");
  608. if(!stream) {
  609. error = ERRNO;
  610. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  611. logmsg("Error opening file: %s", file);
  612. logmsg("Couldn't open test file: %s", file);
  613. return EACCESS;
  614. }
  615. else {
  616. size_t count;
  617. test->buffer = (char *)spitout(stream, "reply", partbuf, &count);
  618. fclose(stream);
  619. if(test->buffer) {
  620. test->rptr = test->buffer; /* set read pointer */
  621. test->bufsize = count; /* set total count */
  622. test->rcount = count; /* set data left to read */
  623. }
  624. else
  625. return EACCESS;
  626. }
  627. }
  628. else
  629. return EACCESS;
  630. }
  631. else {
  632. logmsg("no slash found in path");
  633. return EACCESS; /* failure */
  634. }
  635. return 0;
  636. }
  637. int timeout;
  638. #ifdef HAVE_SIGSETJMP
  639. sigjmp_buf timeoutbuf;
  640. #endif
  641. static void timer(int signum)
  642. {
  643. (void)signum;
  644. logmsg("alarm!");
  645. timeout += rexmtval;
  646. if(timeout >= maxtimeout) {
  647. clear_advisor_read_lock(SERVERLOGS_LOCK);
  648. exit(1);
  649. }
  650. #ifdef HAVE_SIGSETJMP
  651. siglongjmp(timeoutbuf, 1);
  652. #endif
  653. }
  654. /*
  655. * Send the requested file.
  656. */
  657. static void sendtftp(struct testcase *test, struct formats *pf)
  658. {
  659. struct tftphdr *dp;
  660. struct tftphdr *ap; /* ack packet */
  661. unsigned short block = 1;
  662. int size;
  663. ssize_t n;
  664. #if defined(HAVE_ALARM) && defined(SIGALRM)
  665. mysignal(SIGALRM, timer);
  666. #endif
  667. dp = r_init();
  668. ap = (struct tftphdr *)ackbuf;
  669. do {
  670. size = readit(test, &dp, pf->f_convert);
  671. if (size < 0) {
  672. nak(ERRNO + 100);
  673. return;
  674. }
  675. dp->th_opcode = htons((u_short)DATA);
  676. dp->th_block = htons((u_short)block);
  677. timeout = 0;
  678. #ifdef HAVE_SIGSETJMP
  679. (void) sigsetjmp(timeoutbuf, 1);
  680. #endif
  681. send_data:
  682. if (swrite(peer, dp, size + 4) != size + 4) {
  683. logmsg("write\n");
  684. return;
  685. }
  686. read_ahead(test, pf->f_convert);
  687. for ( ; ; ) {
  688. #ifdef HAVE_ALARM
  689. alarm(rexmtval); /* read the ack */
  690. #endif
  691. n = sread(peer, ackbuf, sizeof (ackbuf));
  692. #ifdef HAVE_ALARM
  693. alarm(0);
  694. #endif
  695. if (n < 0) {
  696. logmsg("read: fail\n");
  697. return;
  698. }
  699. ap->th_opcode = ntohs((u_short)ap->th_opcode);
  700. ap->th_block = ntohs((u_short)ap->th_block);
  701. if (ap->th_opcode == ERROR) {
  702. logmsg("got ERROR");
  703. return;
  704. }
  705. if (ap->th_opcode == ACK) {
  706. if (ap->th_block == block) {
  707. break;
  708. }
  709. /* Re-synchronize with the other side */
  710. (void) synchnet(peer);
  711. if (ap->th_block == (block -1)) {
  712. goto send_data;
  713. }
  714. }
  715. }
  716. block++;
  717. } while (size == SEGSIZE);
  718. }
  719. static void justtimeout(int signum)
  720. {
  721. (void)signum;
  722. }
  723. /*
  724. * Receive a file.
  725. */
  726. static void recvtftp(struct testcase *test, struct formats *pf)
  727. {
  728. struct tftphdr *dp;
  729. struct tftphdr *ap; /* ack buffer */
  730. unsigned short block = 0;
  731. ssize_t n, size;
  732. #if defined(HAVE_ALARM) && defined(SIGALRM)
  733. mysignal(SIGALRM, timer);
  734. #endif
  735. dp = w_init();
  736. ap = (struct tftphdr *)ackbuf;
  737. do {
  738. timeout = 0;
  739. ap->th_opcode = htons((u_short)ACK);
  740. ap->th_block = htons((u_short)block);
  741. block++;
  742. #ifdef HAVE_SIGSETJMP
  743. (void) sigsetjmp(timeoutbuf, 1);
  744. #endif
  745. send_ack:
  746. if (swrite(peer, ackbuf, 4) != 4) {
  747. logmsg("write: fail\n");
  748. goto abort;
  749. }
  750. write_behind(test, pf->f_convert);
  751. for ( ; ; ) {
  752. #ifdef HAVE_ALARM
  753. alarm(rexmtval);
  754. #endif
  755. n = sread(peer, dp, PKTSIZE);
  756. #ifdef HAVE_ALARM
  757. alarm(0);
  758. #endif
  759. if (n < 0) { /* really? */
  760. logmsg("read: fail\n");
  761. goto abort;
  762. }
  763. dp->th_opcode = ntohs((u_short)dp->th_opcode);
  764. dp->th_block = ntohs((u_short)dp->th_block);
  765. if (dp->th_opcode == ERROR)
  766. goto abort;
  767. if (dp->th_opcode == DATA) {
  768. if (dp->th_block == block) {
  769. break; /* normal */
  770. }
  771. /* Re-synchronize with the other side */
  772. (void) synchnet(peer);
  773. if (dp->th_block == (block-1))
  774. goto send_ack; /* rexmit */
  775. }
  776. }
  777. size = writeit(test, &dp, (int)(n - 4), pf->f_convert);
  778. if (size != (n-4)) { /* ahem */
  779. if (size < 0)
  780. nak(ERRNO + 100);
  781. else
  782. nak(ENOSPACE);
  783. goto abort;
  784. }
  785. } while (size == SEGSIZE);
  786. write_behind(test, pf->f_convert);
  787. ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */
  788. ap->th_block = htons((u_short)(block));
  789. (void) swrite(peer, ackbuf, 4);
  790. #if defined(HAVE_ALARM) && defined(SIGALRM)
  791. mysignal(SIGALRM, justtimeout); /* just abort read on timeout */
  792. alarm(rexmtval);
  793. #endif
  794. n = sread(peer, buf, sizeof(buf)); /* normally times out and quits */
  795. #ifdef HAVE_ALARM
  796. alarm(0);
  797. #endif
  798. if (n >= 4 && /* if read some data */
  799. dp->th_opcode == DATA && /* and got a data block */
  800. block == dp->th_block) { /* then my last ack was lost */
  801. (void) swrite(peer, ackbuf, 4); /* resend final ack */
  802. }
  803. abort:
  804. return;
  805. }
  806. struct errmsg {
  807. int e_code;
  808. const char *e_msg;
  809. } errmsgs[] = {
  810. { EUNDEF, "Undefined error code" },
  811. { ENOTFOUND, "File not found" },
  812. { EACCESS, "Access violation" },
  813. { ENOSPACE, "Disk full or allocation exceeded" },
  814. { EBADOP, "Illegal TFTP operation" },
  815. { EBADID, "Unknown transfer ID" },
  816. { EEXISTS, "File already exists" },
  817. { ENOUSER, "No such user" },
  818. { -1, 0 }
  819. };
  820. /*
  821. * Send a nak packet (error message). Error code passed in is one of the
  822. * standard TFTP codes, or a UNIX errno offset by 100.
  823. */
  824. static void nak(int error)
  825. {
  826. struct tftphdr *tp;
  827. int length;
  828. struct errmsg *pe;
  829. tp = (struct tftphdr *)buf;
  830. tp->th_opcode = htons((u_short)ERROR);
  831. tp->th_code = htons((u_short)error);
  832. for (pe = errmsgs; pe->e_code >= 0; pe++)
  833. if (pe->e_code == error)
  834. break;
  835. if (pe->e_code < 0) {
  836. pe->e_msg = strerror(error - 100);
  837. tp->th_code = EUNDEF; /* set 'undef' errorcode */
  838. }
  839. strcpy(tp->th_msg, pe->e_msg);
  840. length = (int)strlen(pe->e_msg);
  841. tp->th_msg[length] = '\0';
  842. length += 5;
  843. if (swrite(peer, buf, length) != length)
  844. logmsg("nak: fail\n");
  845. }