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