您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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