tftpd.c 25 KB

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