You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ftp.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <byteswap.h>
  8. #include <gpxe/socket.h>
  9. #include <gpxe/tcpip.h>
  10. #include <gpxe/in.h>
  11. #include <gpxe/xfer.h>
  12. #include <gpxe/open.h>
  13. #include <gpxe/uri.h>
  14. #include <gpxe/ftp.h>
  15. /** @file
  16. *
  17. * File transfer protocol
  18. *
  19. */
  20. /**
  21. * FTP states
  22. *
  23. * These @b must be sequential, i.e. a successful FTP session must
  24. * pass through each of these states in order.
  25. */
  26. enum ftp_state {
  27. FTP_CONNECT = 0,
  28. FTP_USER,
  29. FTP_PASS,
  30. FTP_TYPE,
  31. FTP_PASV,
  32. FTP_RETR,
  33. FTP_QUIT,
  34. FTP_DONE,
  35. };
  36. /**
  37. * An FTP request
  38. *
  39. */
  40. struct ftp_request {
  41. /** Reference counter */
  42. struct refcnt refcnt;
  43. /** Data transfer interface */
  44. struct xfer_interface xfer;
  45. /** URI being fetched */
  46. struct uri *uri;
  47. /** FTP control channel interface */
  48. struct xfer_interface control;
  49. /** FTP data channel interface */
  50. struct xfer_interface data;
  51. /** Current state */
  52. enum ftp_state state;
  53. /** Buffer to be filled with data received via the control channel */
  54. char *recvbuf;
  55. /** Remaining size of recvbuf */
  56. size_t recvsize;
  57. /** FTP status code, as text */
  58. char status_text[5];
  59. /** Passive-mode parameters, as text */
  60. char passive_text[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
  61. };
  62. /**
  63. * Free FTP request
  64. *
  65. * @v refcnt Reference counter
  66. */
  67. static void ftp_free ( struct refcnt *refcnt ) {
  68. struct ftp_request *ftp =
  69. container_of ( refcnt, struct ftp_request, refcnt );
  70. DBGC ( ftp, "FTP %p freed\n", ftp );
  71. uri_put ( ftp->uri );
  72. free ( ftp );
  73. }
  74. /**
  75. * Mark FTP operation as complete
  76. *
  77. * @v ftp FTP request
  78. * @v rc Return status code
  79. */
  80. static void ftp_done ( struct ftp_request *ftp, int rc ) {
  81. DBGC ( ftp, "FTP %p completed (%s)\n", ftp, strerror ( rc ) );
  82. /* Close all data transfer interfaces */
  83. xfer_nullify ( &ftp->xfer );
  84. xfer_close ( &ftp->xfer, rc );
  85. xfer_nullify ( &ftp->control );
  86. xfer_close ( &ftp->control, rc );
  87. xfer_nullify ( &ftp->data );
  88. xfer_close ( &ftp->data, rc );
  89. }
  90. /*****************************************************************************
  91. *
  92. * FTP control channel
  93. *
  94. */
  95. /**
  96. * FTP control channel strings
  97. *
  98. * These are used as printf() format strings. Since only one of them
  99. * (RETR) takes an argument, we always supply that argument to the
  100. * snprintf() call.
  101. */
  102. static const char * ftp_strings[] = {
  103. [FTP_CONNECT] = "",
  104. [FTP_USER] = "USER anonymous\r\n",
  105. [FTP_PASS] = "PASS etherboot@etherboot.org\r\n",
  106. [FTP_TYPE] = "TYPE I\r\n",
  107. [FTP_PASV] = "PASV\r\n",
  108. [FTP_RETR] = "RETR %s\r\n",
  109. [FTP_QUIT] = "QUIT\r\n",
  110. [FTP_DONE] = "",
  111. };
  112. /**
  113. * Handle control channel being closed
  114. *
  115. * @v control FTP control channel interface
  116. * @v rc Reason for close
  117. *
  118. * When the control channel is closed, the data channel must also be
  119. * closed, if it is currently open.
  120. */
  121. static void ftp_control_close ( struct xfer_interface *control, int rc ) {
  122. struct ftp_request *ftp =
  123. container_of ( control, struct ftp_request, control );
  124. DBGC ( ftp, "FTP %p control connection closed: %s\n",
  125. ftp, strerror ( rc ) );
  126. /* Complete FTP operation */
  127. ftp_done ( ftp, rc );
  128. }
  129. /**
  130. * Parse FTP byte sequence value
  131. *
  132. * @v text Text string
  133. * @v value Value buffer
  134. * @v len Length of value buffer
  135. *
  136. * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
  137. * form for IP addresses in PORT commands) into a byte sequence. @c
  138. * *text will be updated to point beyond the end of the parsed byte
  139. * sequence.
  140. *
  141. * This function is safe in the presence of malformed data, though the
  142. * output is undefined.
  143. */
  144. static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
  145. do {
  146. *(value++) = strtoul ( *text, text, 10 );
  147. if ( **text )
  148. (*text)++;
  149. } while ( --len );
  150. }
  151. /**
  152. * Handle an FTP control channel response
  153. *
  154. * @v ftp FTP request
  155. *
  156. * This is called once we have received a complete response line.
  157. */
  158. static void ftp_reply ( struct ftp_request *ftp ) {
  159. char status_major = ftp->status_text[0];
  160. char separator = ftp->status_text[3];
  161. DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
  162. /* Ignore malformed lines */
  163. if ( separator != ' ' )
  164. return;
  165. /* Ignore "intermediate" responses (1xx codes) */
  166. if ( status_major == '1' )
  167. return;
  168. /* Anything other than success (2xx) or, in the case of a
  169. * repsonse to a "USER" command, a password prompt (3xx), is a
  170. * fatal error.
  171. */
  172. if ( ! ( ( status_major == '2' ) ||
  173. ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
  174. /* Flag protocol error and close connections */
  175. ftp_done ( ftp, -EPROTO );
  176. }
  177. /* Open passive connection when we get "PASV" response */
  178. if ( ftp->state == FTP_PASV ) {
  179. char *ptr = ftp->passive_text;
  180. union {
  181. struct sockaddr_in sin;
  182. struct sockaddr sa;
  183. } sa;
  184. int rc;
  185. sa.sin.sin_family = AF_INET;
  186. ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
  187. sizeof ( sa.sin.sin_addr ) );
  188. ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
  189. sizeof ( sa.sin.sin_port ) );
  190. if ( ( rc = xfer_open_socket ( &ftp->data, SOCK_STREAM,
  191. &sa.sa, NULL ) ) != 0 ) {
  192. DBGC ( ftp, "FTP %p could not open data connection\n",
  193. ftp );
  194. ftp_done ( ftp, rc );
  195. return;
  196. }
  197. }
  198. /* Move to next state */
  199. if ( ftp->state < FTP_DONE )
  200. ftp->state++;
  201. /* Send control string */
  202. if ( ftp->state < FTP_DONE ) {
  203. DBGC ( ftp, "FTP %p sending ", ftp );
  204. DBGC ( ftp, ftp_strings[ftp->state], ftp->uri->path );
  205. xfer_printf ( &ftp->control, ftp_strings[ftp->state],
  206. ftp->uri->path );
  207. }
  208. }
  209. /**
  210. * Handle new data arriving on FTP control channel
  211. *
  212. * @v control FTP control channel interface
  213. * @v data New data
  214. * @v len Length of new data
  215. *
  216. * Data is collected until a complete line is received, at which point
  217. * its information is passed to ftp_reply().
  218. */
  219. static int ftp_control_deliver_raw ( struct xfer_interface *control,
  220. const void *data, size_t len ) {
  221. struct ftp_request *ftp =
  222. container_of ( control, struct ftp_request, control );
  223. char *recvbuf = ftp->recvbuf;
  224. size_t recvsize = ftp->recvsize;
  225. char c;
  226. while ( len-- ) {
  227. c = * ( ( char * ) data++ );
  228. switch ( c ) {
  229. case '\r' :
  230. case '\n' :
  231. /* End of line: call ftp_reply() to handle
  232. * completed reply. Avoid calling ftp_reply()
  233. * twice if we receive both \r and \n.
  234. */
  235. if ( recvsize == 0 )
  236. ftp_reply ( ftp );
  237. /* Start filling up the status code buffer */
  238. recvbuf = ftp->status_text;
  239. recvsize = sizeof ( ftp->status_text ) - 1;
  240. break;
  241. case '(' :
  242. /* Start filling up the passive parameter buffer */
  243. recvbuf = ftp->passive_text;
  244. recvsize = sizeof ( ftp->passive_text ) - 1;
  245. break;
  246. case ')' :
  247. /* Stop filling the passive parameter buffer */
  248. recvsize = 0;
  249. break;
  250. default :
  251. /* Fill up buffer if applicable */
  252. if ( recvsize > 0 ) {
  253. *(recvbuf++) = c;
  254. recvsize--;
  255. }
  256. break;
  257. }
  258. }
  259. /* Store for next invocation */
  260. ftp->recvbuf = recvbuf;
  261. ftp->recvsize = recvsize;
  262. return 0;
  263. }
  264. /** FTP control channel operations */
  265. static struct xfer_interface_operations ftp_control_operations = {
  266. .close = ftp_control_close,
  267. .vredirect = xfer_vopen,
  268. .seek = ignore_xfer_seek,
  269. .window = unlimited_xfer_window,
  270. .alloc_iob = default_xfer_alloc_iob,
  271. .deliver_iob = xfer_deliver_as_raw,
  272. .deliver_raw = ftp_control_deliver_raw,
  273. };
  274. /*****************************************************************************
  275. *
  276. * FTP data channel
  277. *
  278. */
  279. /**
  280. * Handle FTP data channel being closed
  281. *
  282. * @v data FTP data channel interface
  283. * @v rc Reason for closure
  284. *
  285. * When the data channel is closed, the control channel should be left
  286. * alone; the server will send a completion message via the control
  287. * channel which we'll pick up.
  288. *
  289. * If the data channel is closed due to an error, we abort the request.
  290. */
  291. static void ftp_data_closed ( struct xfer_interface *data, int rc ) {
  292. struct ftp_request *ftp =
  293. container_of ( data, struct ftp_request, data );
  294. DBGC ( ftp, "FTP %p data connection closed: %s\n",
  295. ftp, strerror ( rc ) );
  296. /* If there was an error, close control channel and record status */
  297. if ( rc )
  298. ftp_done ( ftp, rc );
  299. }
  300. /**
  301. * Handle data delivery via FTP data channel
  302. *
  303. * @v xfer FTP data channel interface
  304. * @v iobuf I/O buffer
  305. * @v meta Data transfer metadata, or NULL
  306. * @ret rc Return status code
  307. */
  308. static int ftp_data_deliver_iob ( struct xfer_interface *data,
  309. struct io_buffer *iobuf,
  310. struct xfer_metadata *meta __unused ) {
  311. struct ftp_request *ftp =
  312. container_of ( data, struct ftp_request, data );
  313. int rc;
  314. if ( ( rc = xfer_deliver_iob ( &ftp->xfer, iobuf ) ) != 0 ) {
  315. DBGC ( ftp, "FTP %p failed to deliver data: %s\n",
  316. ftp, strerror ( rc ) );
  317. return rc;
  318. }
  319. return 0;
  320. }
  321. /** FTP data channel operations */
  322. static struct xfer_interface_operations ftp_data_operations = {
  323. .close = ftp_data_closed,
  324. .vredirect = xfer_vopen,
  325. .seek = ignore_xfer_seek,
  326. .window = unlimited_xfer_window,
  327. .alloc_iob = default_xfer_alloc_iob,
  328. .deliver_iob = ftp_data_deliver_iob,
  329. .deliver_raw = xfer_deliver_as_iob,
  330. };
  331. /*****************************************************************************
  332. *
  333. * Data transfer interface
  334. *
  335. */
  336. /**
  337. * Close FTP data transfer interface
  338. *
  339. * @v xfer FTP data transfer interface
  340. * @v rc Reason for close
  341. */
  342. static void ftp_xfer_closed ( struct xfer_interface *xfer, int rc ) {
  343. struct ftp_request *ftp =
  344. container_of ( xfer, struct ftp_request, xfer );
  345. DBGC ( ftp, "FTP %p data transfer interface closed: %s\n",
  346. ftp, strerror ( rc ) );
  347. ftp_done ( ftp, rc );
  348. }
  349. /** FTP data transfer interface operations */
  350. static struct xfer_interface_operations ftp_xfer_operations = {
  351. .close = ftp_xfer_closed,
  352. .vredirect = ignore_xfer_vredirect,
  353. .seek = ignore_xfer_seek,
  354. .window = unlimited_xfer_window,
  355. .alloc_iob = default_xfer_alloc_iob,
  356. .deliver_iob = xfer_deliver_as_raw,
  357. .deliver_raw = ignore_xfer_deliver_raw,
  358. };
  359. /*****************************************************************************
  360. *
  361. * URI opener
  362. *
  363. */
  364. /**
  365. * Initiate an FTP connection
  366. *
  367. * @v xfer Data transfer interface
  368. * @v uri Uniform Resource Identifier
  369. * @ret rc Return status code
  370. */
  371. static int ftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
  372. struct ftp_request *ftp;
  373. struct sockaddr_tcpip server;
  374. int rc;
  375. /* Sanity checks */
  376. if ( ! uri->path )
  377. return -EINVAL;
  378. if ( ! uri->host )
  379. return -EINVAL;
  380. /* Allocate and populate structure */
  381. ftp = zalloc ( sizeof ( *ftp ) );
  382. if ( ! ftp )
  383. return -ENOMEM;
  384. ftp->refcnt.free = ftp_free;
  385. xfer_init ( &ftp->xfer, &ftp_xfer_operations, &ftp->refcnt );
  386. ftp->uri = uri_get ( uri );
  387. xfer_init ( &ftp->control, &ftp_control_operations, &ftp->refcnt );
  388. xfer_init ( &ftp->data, &ftp_data_operations, &ftp->refcnt );
  389. ftp->recvbuf = ftp->status_text;
  390. ftp->recvsize = sizeof ( ftp->status_text ) - 1;
  391. DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
  392. /* Open control connection */
  393. memset ( &server, 0, sizeof ( server ) );
  394. server.st_port = htons ( uri_port ( uri, FTP_PORT ) );
  395. if ( ( rc = xfer_open_named_socket ( &ftp->control, SOCK_STREAM,
  396. ( struct sockaddr * ) &server,
  397. uri->host, NULL ) ) != 0 )
  398. goto err;
  399. /* Attach to parent interface, mortalise self, and return */
  400. xfer_plug_plug ( &ftp->xfer, xfer );
  401. ref_put ( &ftp->refcnt );
  402. return 0;
  403. err:
  404. DBGC ( ftp, "FTP %p could not create request: %s\n",
  405. ftp, strerror ( rc ) );
  406. ftp_done ( ftp, rc );
  407. ref_put ( &ftp->refcnt );
  408. return rc;
  409. }
  410. /** FTP URI opener */
  411. struct uri_opener ftp_uri_opener __uri_opener = {
  412. .scheme = "ftp",
  413. .open = ftp_open,
  414. };