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 12KB

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