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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <byteswap.h>
  26. #include <ipxe/socket.h>
  27. #include <ipxe/tcpip.h>
  28. #include <ipxe/in.h>
  29. #include <ipxe/iobuf.h>
  30. #include <ipxe/xfer.h>
  31. #include <ipxe/open.h>
  32. #include <ipxe/uri.h>
  33. #include <ipxe/features.h>
  34. #include <ipxe/ftp.h>
  35. /** @file
  36. *
  37. * File transfer protocol
  38. *
  39. */
  40. FEATURE ( FEATURE_PROTOCOL, "FTP", DHCP_EB_FEATURE_FTP, 1 );
  41. /**
  42. * FTP states
  43. *
  44. * These @b must be sequential, i.e. a successful FTP session must
  45. * pass through each of these states in order.
  46. */
  47. enum ftp_state {
  48. FTP_CONNECT = 0,
  49. FTP_USER,
  50. FTP_PASS,
  51. FTP_TYPE,
  52. FTP_SIZE,
  53. FTP_PASV,
  54. FTP_RETR,
  55. FTP_WAIT,
  56. FTP_QUIT,
  57. FTP_DONE,
  58. };
  59. /**
  60. * An FTP request
  61. *
  62. */
  63. struct ftp_request {
  64. /** Reference counter */
  65. struct refcnt refcnt;
  66. /** Data transfer interface */
  67. struct interface xfer;
  68. /** URI being fetched */
  69. struct uri *uri;
  70. /** FTP control channel interface */
  71. struct interface control;
  72. /** FTP data channel interface */
  73. struct interface data;
  74. /** Current state */
  75. enum ftp_state state;
  76. /** Buffer to be filled with data received via the control channel */
  77. char *recvbuf;
  78. /** Remaining size of recvbuf */
  79. size_t recvsize;
  80. /** FTP status code, as text */
  81. char status_text[5];
  82. /** Passive-mode parameters, as text */
  83. char passive_text[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
  84. /** File size, as text */
  85. char filesize[20];
  86. };
  87. /**
  88. * Free FTP request
  89. *
  90. * @v refcnt Reference counter
  91. */
  92. static void ftp_free ( struct refcnt *refcnt ) {
  93. struct ftp_request *ftp =
  94. container_of ( refcnt, struct ftp_request, refcnt );
  95. DBGC ( ftp, "FTP %p freed\n", ftp );
  96. uri_put ( ftp->uri );
  97. free ( ftp );
  98. }
  99. /**
  100. * Mark FTP operation as complete
  101. *
  102. * @v ftp FTP request
  103. * @v rc Return status code
  104. */
  105. static void ftp_done ( struct ftp_request *ftp, int rc ) {
  106. DBGC ( ftp, "FTP %p completed (%s)\n", ftp, strerror ( rc ) );
  107. /* Close all data transfer interfaces */
  108. intf_shutdown ( &ftp->data, rc );
  109. intf_shutdown ( &ftp->control, rc );
  110. intf_shutdown ( &ftp->xfer, rc );
  111. }
  112. /*****************************************************************************
  113. *
  114. * FTP control channel
  115. *
  116. */
  117. /** An FTP control channel string */
  118. struct ftp_control_string {
  119. /** Literal portion */
  120. const char *literal;
  121. /** Variable portion
  122. *
  123. * @v ftp FTP request
  124. * @ret string Variable portion of string
  125. */
  126. const char * ( *variable ) ( struct ftp_request *ftp );
  127. };
  128. /**
  129. * Retrieve FTP pathname
  130. *
  131. * @v ftp FTP request
  132. * @ret path FTP pathname
  133. */
  134. static const char * ftp_uri_path ( struct ftp_request *ftp ) {
  135. return ftp->uri->path;
  136. }
  137. /**
  138. * Retrieve FTP user
  139. *
  140. * @v ftp FTP request
  141. * @ret user FTP user
  142. */
  143. static const char * ftp_user ( struct ftp_request *ftp ) {
  144. static char *ftp_default_user = "anonymous";
  145. return ftp->uri->user ? ftp->uri->user : ftp_default_user;
  146. }
  147. /**
  148. * Retrieve FTP password
  149. *
  150. * @v ftp FTP request
  151. * @ret password FTP password
  152. */
  153. static const char * ftp_password ( struct ftp_request *ftp ) {
  154. static char *ftp_default_password = "ipxe@ipxe.org";
  155. return ftp->uri->password ? ftp->uri->password : ftp_default_password;
  156. }
  157. /** FTP control channel strings */
  158. static struct ftp_control_string ftp_strings[] = {
  159. [FTP_CONNECT] = { NULL, NULL },
  160. [FTP_USER] = { "USER ", ftp_user },
  161. [FTP_PASS] = { "PASS ", ftp_password },
  162. [FTP_TYPE] = { "TYPE I", NULL },
  163. [FTP_SIZE] = { "SIZE ", ftp_uri_path },
  164. [FTP_PASV] = { "PASV", NULL },
  165. [FTP_RETR] = { "RETR ", ftp_uri_path },
  166. [FTP_WAIT] = { NULL, NULL },
  167. [FTP_QUIT] = { "QUIT", NULL },
  168. [FTP_DONE] = { NULL, NULL },
  169. };
  170. /**
  171. * Parse FTP byte sequence value
  172. *
  173. * @v text Text string
  174. * @v value Value buffer
  175. * @v len Length of value buffer
  176. *
  177. * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
  178. * form for IP addresses in PORT commands) into a byte sequence. @c
  179. * *text will be updated to point beyond the end of the parsed byte
  180. * sequence.
  181. *
  182. * This function is safe in the presence of malformed data, though the
  183. * output is undefined.
  184. */
  185. static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
  186. do {
  187. *(value++) = strtoul ( *text, text, 10 );
  188. if ( **text )
  189. (*text)++;
  190. } while ( --len );
  191. }
  192. /**
  193. * Move to next state and send the appropriate FTP control string
  194. *
  195. * @v ftp FTP request
  196. *
  197. */
  198. static void ftp_next_state ( struct ftp_request *ftp ) {
  199. struct ftp_control_string *ftp_string;
  200. const char *literal;
  201. const char *variable;
  202. /* Move to next state */
  203. if ( ftp->state < FTP_DONE )
  204. ftp->state++;
  205. /* Send control string if needed */
  206. ftp_string = &ftp_strings[ftp->state];
  207. literal = ftp_string->literal;
  208. variable = ( ftp_string->variable ?
  209. ftp_string->variable ( ftp ) : "" );
  210. if ( literal ) {
  211. DBGC ( ftp, "FTP %p sending %s%s\n", ftp, literal, variable );
  212. xfer_printf ( &ftp->control, "%s%s\r\n", literal, variable );
  213. }
  214. }
  215. /**
  216. * Handle an FTP control channel response
  217. *
  218. * @v ftp FTP request
  219. *
  220. * This is called once we have received a complete response line.
  221. */
  222. static void ftp_reply ( struct ftp_request *ftp ) {
  223. char status_major = ftp->status_text[0];
  224. char separator = ftp->status_text[3];
  225. DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
  226. /* Ignore malformed lines */
  227. if ( separator != ' ' )
  228. return;
  229. /* Ignore "intermediate" responses (1xx codes) */
  230. if ( status_major == '1' )
  231. return;
  232. /* If the SIZE command is not supported by the server, we go to
  233. * the next step.
  234. */
  235. if ( ( status_major == '5' ) && ( ftp->state == FTP_SIZE ) ) {
  236. ftp_next_state ( ftp );
  237. return;
  238. }
  239. /* Anything other than success (2xx) or, in the case of a
  240. * repsonse to a "USER" command, a password prompt (3xx), is a
  241. * fatal error.
  242. */
  243. if ( ! ( ( status_major == '2' ) ||
  244. ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
  245. /* Flag protocol error and close connections */
  246. ftp_done ( ftp, -EPROTO );
  247. return;
  248. }
  249. /* Parse file size */
  250. if ( ftp->state == FTP_SIZE ) {
  251. size_t filesize;
  252. char *endptr;
  253. /* Parse size */
  254. filesize = strtoul ( ftp->filesize, &endptr, 10 );
  255. if ( *endptr != '\0' ) {
  256. DBGC ( ftp, "FTP %p invalid SIZE \"%s\"\n",
  257. ftp, ftp->filesize );
  258. ftp_done ( ftp, -EPROTO );
  259. return;
  260. }
  261. /* Use seek() to notify recipient of filesize */
  262. DBGC ( ftp, "FTP %p file size is %zd bytes\n", ftp, filesize );
  263. xfer_seek ( &ftp->xfer, filesize );
  264. xfer_seek ( &ftp->xfer, 0 );
  265. }
  266. /* Open passive connection when we get "PASV" response */
  267. if ( ftp->state == FTP_PASV ) {
  268. char *ptr = ftp->passive_text;
  269. union {
  270. struct sockaddr_in sin;
  271. struct sockaddr sa;
  272. } sa;
  273. int rc;
  274. sa.sin.sin_family = AF_INET;
  275. ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
  276. sizeof ( sa.sin.sin_addr ) );
  277. ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
  278. sizeof ( sa.sin.sin_port ) );
  279. if ( ( rc = xfer_open_socket ( &ftp->data, SOCK_STREAM,
  280. &sa.sa, NULL ) ) != 0 ) {
  281. DBGC ( ftp, "FTP %p could not open data connection\n",
  282. ftp );
  283. ftp_done ( ftp, rc );
  284. return;
  285. }
  286. }
  287. /* Move to next state and send control string */
  288. ftp_next_state ( ftp );
  289. }
  290. /**
  291. * Handle new data arriving on FTP control channel
  292. *
  293. * @v ftp FTP request
  294. * @v iob I/O buffer
  295. * @v meta Data transfer metadata
  296. * @ret rc Return status code
  297. *
  298. * Data is collected until a complete line is received, at which point
  299. * its information is passed to ftp_reply().
  300. */
  301. static int ftp_control_deliver ( struct ftp_request *ftp,
  302. struct io_buffer *iobuf,
  303. struct xfer_metadata *meta __unused ) {
  304. char *data = iobuf->data;
  305. size_t len = iob_len ( iobuf );
  306. char *recvbuf = ftp->recvbuf;
  307. size_t recvsize = ftp->recvsize;
  308. char c;
  309. while ( len-- ) {
  310. c = *(data++);
  311. if ( ( c == '\r' ) || ( c == '\n' ) ) {
  312. /* End of line: call ftp_reply() to handle
  313. * completed reply. Avoid calling ftp_reply()
  314. * twice if we receive both \r and \n.
  315. */
  316. if ( recvbuf != ftp->status_text )
  317. ftp_reply ( ftp );
  318. /* Start filling up the status code buffer */
  319. recvbuf = ftp->status_text;
  320. recvsize = sizeof ( ftp->status_text ) - 1;
  321. } else if ( ( ftp->state == FTP_PASV ) && ( c == '(' ) ) {
  322. /* Start filling up the passive parameter buffer */
  323. recvbuf = ftp->passive_text;
  324. recvsize = sizeof ( ftp->passive_text ) - 1;
  325. } else if ( ( ftp->state == FTP_PASV ) && ( c == ')' ) ) {
  326. /* Stop filling the passive parameter buffer */
  327. recvsize = 0;
  328. } else if ( ( ftp->state == FTP_SIZE ) && ( c == ' ' ) ) {
  329. /* Start filling up the file size buffer */
  330. recvbuf = ftp->filesize;
  331. recvsize = sizeof ( ftp->filesize ) - 1;
  332. } else {
  333. /* Fill up buffer if applicable */
  334. if ( recvsize > 0 ) {
  335. *(recvbuf++) = c;
  336. recvsize--;
  337. }
  338. }
  339. }
  340. /* Store for next invocation */
  341. ftp->recvbuf = recvbuf;
  342. ftp->recvsize = recvsize;
  343. /* Free I/O buffer */
  344. free_iob ( iobuf );
  345. return 0;
  346. }
  347. /** FTP control channel interface operations */
  348. static struct interface_operation ftp_control_operations[] = {
  349. INTF_OP ( xfer_deliver, struct ftp_request *, ftp_control_deliver ),
  350. INTF_OP ( intf_close, struct ftp_request *, ftp_done ),
  351. };
  352. /** FTP control channel interface descriptor */
  353. static struct interface_descriptor ftp_control_desc =
  354. INTF_DESC ( struct ftp_request, control, ftp_control_operations );
  355. /*****************************************************************************
  356. *
  357. * FTP data channel
  358. *
  359. */
  360. /**
  361. * Handle FTP data channel being closed
  362. *
  363. * @v ftp FTP request
  364. * @v rc Reason for closure
  365. *
  366. * When the data channel is closed, the control channel should be left
  367. * alone; the server will send a completion message via the control
  368. * channel which we'll pick up.
  369. *
  370. * If the data channel is closed due to an error, we abort the request.
  371. */
  372. static void ftp_data_closed ( struct ftp_request *ftp, int rc ) {
  373. DBGC ( ftp, "FTP %p data connection closed: %s\n",
  374. ftp, strerror ( rc ) );
  375. /* If there was an error, close control channel and record status */
  376. if ( rc ) {
  377. ftp_done ( ftp, rc );
  378. } else {
  379. ftp_next_state ( ftp );
  380. }
  381. }
  382. /** FTP data channel interface operations */
  383. static struct interface_operation ftp_data_operations[] = {
  384. INTF_OP ( intf_close, struct ftp_request *, ftp_data_closed ),
  385. };
  386. /** FTP data channel interface descriptor */
  387. static struct interface_descriptor ftp_data_desc =
  388. INTF_DESC_PASSTHRU ( struct ftp_request, data, ftp_data_operations,
  389. xfer );
  390. /*****************************************************************************
  391. *
  392. * Data transfer interface
  393. *
  394. */
  395. /** FTP data transfer interface operations */
  396. static struct interface_operation ftp_xfer_operations[] = {
  397. INTF_OP ( intf_close, struct ftp_request *, ftp_done ),
  398. };
  399. /** FTP data transfer interface descriptor */
  400. static struct interface_descriptor ftp_xfer_desc =
  401. INTF_DESC_PASSTHRU ( struct ftp_request, xfer, ftp_xfer_operations,
  402. data );
  403. /*****************************************************************************
  404. *
  405. * URI opener
  406. *
  407. */
  408. /**
  409. * Initiate an FTP connection
  410. *
  411. * @v xfer Data transfer interface
  412. * @v uri Uniform Resource Identifier
  413. * @ret rc Return status code
  414. */
  415. static int ftp_open ( struct interface *xfer, struct uri *uri ) {
  416. struct ftp_request *ftp;
  417. struct sockaddr_tcpip server;
  418. int rc;
  419. /* Sanity checks */
  420. if ( ! uri->path )
  421. return -EINVAL;
  422. if ( ! uri->host )
  423. return -EINVAL;
  424. /* Allocate and populate structure */
  425. ftp = zalloc ( sizeof ( *ftp ) );
  426. if ( ! ftp )
  427. return -ENOMEM;
  428. ref_init ( &ftp->refcnt, ftp_free );
  429. intf_init ( &ftp->xfer, &ftp_xfer_desc, &ftp->refcnt );
  430. intf_init ( &ftp->control, &ftp_control_desc, &ftp->refcnt );
  431. intf_init ( &ftp->data, &ftp_data_desc, &ftp->refcnt );
  432. ftp->uri = uri_get ( uri );
  433. ftp->recvbuf = ftp->status_text;
  434. ftp->recvsize = sizeof ( ftp->status_text ) - 1;
  435. DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
  436. /* Open control connection */
  437. memset ( &server, 0, sizeof ( server ) );
  438. server.st_port = htons ( uri_port ( uri, FTP_PORT ) );
  439. if ( ( rc = xfer_open_named_socket ( &ftp->control, SOCK_STREAM,
  440. ( struct sockaddr * ) &server,
  441. uri->host, NULL ) ) != 0 )
  442. goto err;
  443. /* Attach to parent interface, mortalise self, and return */
  444. intf_plug_plug ( &ftp->xfer, xfer );
  445. ref_put ( &ftp->refcnt );
  446. return 0;
  447. err:
  448. DBGC ( ftp, "FTP %p could not create request: %s\n",
  449. ftp, strerror ( rc ) );
  450. ftp_done ( ftp, rc );
  451. ref_put ( &ftp->refcnt );
  452. return rc;
  453. }
  454. /** FTP URI opener */
  455. struct uri_opener ftp_uri_opener __uri_opener = {
  456. .scheme = "ftp",
  457. .open = ftp_open,
  458. };