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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include <stddef.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/async.h>
  9. #include <gpxe/buffer.h>
  10. #include <gpxe/uri.h>
  11. #include <gpxe/download.h>
  12. #include <gpxe/tcp.h>
  13. #include <gpxe/ftp.h>
  14. /** @file
  15. *
  16. * File transfer protocol
  17. *
  18. */
  19. /*****************************************************************************
  20. *
  21. * FTP control channel
  22. *
  23. */
  24. /** FTP control channel strings
  25. *
  26. * These are used as printf() format strings. Since only one of them
  27. * (RETR) takes an argument, we always supply that argument to the
  28. * snprintf() call.
  29. */
  30. static const char * ftp_strings[] = {
  31. [FTP_CONNECT] = "",
  32. [FTP_USER] = "USER anonymous\r\n",
  33. [FTP_PASS] = "PASS etherboot@etherboot.org\r\n",
  34. [FTP_TYPE] = "TYPE I\r\n",
  35. [FTP_PASV] = "PASV\r\n",
  36. [FTP_RETR] = "RETR %s\r\n",
  37. [FTP_QUIT] = "QUIT\r\n",
  38. [FTP_DONE] = "",
  39. };
  40. /**
  41. * Get FTP request from control stream application
  42. *
  43. * @v app Stream application
  44. * @ret ftp FTP request
  45. */
  46. static inline struct ftp_request *
  47. stream_to_ftp ( struct stream_application *app ) {
  48. return container_of ( app, struct ftp_request, stream );
  49. }
  50. /**
  51. * Mark FTP operation as complete
  52. *
  53. * @v ftp FTP request
  54. * @v rc Return status code
  55. */
  56. static void ftp_done ( struct ftp_request *ftp, int rc ) {
  57. DBGC ( ftp, "FTP %p completed with status %d\n", ftp, rc );
  58. /* Close both stream connections */
  59. stream_close ( &ftp->stream );
  60. stream_close ( &ftp->stream_data );
  61. /* Mark asynchronous operation as complete */
  62. async_done ( &ftp->async, rc );
  63. }
  64. /**
  65. * Parse FTP byte sequence value
  66. *
  67. * @v text Text string
  68. * @v value Value buffer
  69. * @v len Length of value buffer
  70. *
  71. * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
  72. * form for IP addresses in PORT commands) into a byte sequence. @c
  73. * *text will be updated to point beyond the end of the parsed byte
  74. * sequence.
  75. *
  76. * This function is safe in the presence of malformed data, though the
  77. * output is undefined.
  78. */
  79. static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
  80. do {
  81. *(value++) = strtoul ( *text, text, 10 );
  82. if ( **text )
  83. (*text)++;
  84. } while ( --len );
  85. }
  86. /**
  87. * Handle an FTP control channel response
  88. *
  89. * @v ftp FTP request
  90. *
  91. * This is called once we have received a complete response line.
  92. */
  93. static void ftp_reply ( struct ftp_request *ftp ) {
  94. char status_major = ftp->status_text[0];
  95. DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
  96. /* Ignore "intermediate" responses (1xx codes) */
  97. if ( status_major == '1' )
  98. return;
  99. /* Anything other than success (2xx) or, in the case of a
  100. * repsonse to a "USER" command, a password prompt (3xx), is a
  101. * fatal error.
  102. */
  103. if ( ! ( ( status_major == '2' ) ||
  104. ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
  105. /* Flag protocol error and close connections */
  106. ftp_done ( ftp, -EPROTO );
  107. }
  108. /* Open passive connection when we get "PASV" response */
  109. if ( ftp->state == FTP_PASV ) {
  110. char *ptr = ftp->passive_text;
  111. union {
  112. struct sockaddr_in sin;
  113. struct sockaddr sa;
  114. } sa;
  115. int rc;
  116. sa.sin.sin_family = AF_INET;
  117. ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
  118. sizeof ( sa.sin.sin_addr ) );
  119. ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
  120. sizeof ( sa.sin.sin_port ) );
  121. if ( ( rc = tcp_open ( &ftp->stream_data ) ) != 0 ) {
  122. DBGC ( ftp, "FTP %p could not open data connection\n",
  123. ftp );
  124. ftp_done ( ftp, rc );
  125. return;
  126. }
  127. if ( ( rc = stream_connect ( &ftp->stream_data,
  128. &sa.sa ) ) != 0 ){
  129. DBGC ( ftp, "FTP %p could not make data connection\n",
  130. ftp );
  131. ftp_done ( ftp, rc );
  132. return;
  133. }
  134. }
  135. /* Move to next state */
  136. if ( ftp->state < FTP_DONE )
  137. ftp->state++;
  138. ftp->already_sent = 0;
  139. if ( ftp->state < FTP_DONE ) {
  140. DBGC ( ftp, "FTP %p sending ", ftp );
  141. DBGC ( ftp, ftp_strings[ftp->state], ftp->uri->path );
  142. }
  143. return;
  144. }
  145. /**
  146. * Handle new data arriving on FTP control channel
  147. *
  148. * @v app Stream application
  149. * @v data New data
  150. * @v len Length of new data
  151. *
  152. * Data is collected until a complete line is received, at which point
  153. * its information is passed to ftp_reply().
  154. */
  155. static void ftp_newdata ( struct stream_application *app,
  156. void *data, size_t len ) {
  157. struct ftp_request *ftp = stream_to_ftp ( app );
  158. char *recvbuf = ftp->recvbuf;
  159. size_t recvsize = ftp->recvsize;
  160. char c;
  161. while ( len-- ) {
  162. c = * ( ( char * ) data++ );
  163. switch ( c ) {
  164. case '\r' :
  165. case '\n' :
  166. /* End of line: call ftp_reply() to handle
  167. * completed reply. Avoid calling ftp_reply()
  168. * twice if we receive both \r and \n.
  169. */
  170. if ( recvsize == 0 )
  171. ftp_reply ( ftp );
  172. /* Start filling up the status code buffer */
  173. recvbuf = ftp->status_text;
  174. recvsize = sizeof ( ftp->status_text ) - 1;
  175. break;
  176. case '(' :
  177. /* Start filling up the passive parameter buffer */
  178. recvbuf = ftp->passive_text;
  179. recvsize = sizeof ( ftp->passive_text ) - 1;
  180. break;
  181. case ')' :
  182. /* Stop filling the passive parameter buffer */
  183. recvsize = 0;
  184. break;
  185. default :
  186. /* Fill up buffer if applicable */
  187. if ( recvsize > 0 ) {
  188. *(recvbuf++) = c;
  189. recvsize--;
  190. }
  191. break;
  192. }
  193. }
  194. /* Store for next invocation */
  195. ftp->recvbuf = recvbuf;
  196. ftp->recvsize = recvsize;
  197. }
  198. /**
  199. * Handle acknowledgement of data sent on FTP control channel
  200. *
  201. * @v app Stream application
  202. */
  203. static void ftp_acked ( struct stream_application *app, size_t len ) {
  204. struct ftp_request *ftp = stream_to_ftp ( app );
  205. /* Mark off ACKed portion of the currently-transmitted data */
  206. ftp->already_sent += len;
  207. }
  208. /**
  209. * Construct data to send on FTP control channel
  210. *
  211. * @v app Stream application
  212. * @v buf Temporary data buffer
  213. * @v len Length of temporary data buffer
  214. */
  215. static void ftp_senddata ( struct stream_application *app,
  216. void *buf, size_t len ) {
  217. struct ftp_request *ftp = stream_to_ftp ( app );
  218. /* Send the as-yet-unACKed portion of the string for the
  219. * current state.
  220. */
  221. len = snprintf ( buf, len, ftp_strings[ftp->state], ftp->uri->path );
  222. stream_send ( app, buf + ftp->already_sent, len - ftp->already_sent );
  223. }
  224. /**
  225. * Handle control channel being closed
  226. *
  227. * @v app Stream application
  228. *
  229. * When the control channel is closed, the data channel must also be
  230. * closed, if it is currently open.
  231. */
  232. static void ftp_closed ( struct stream_application *app, int rc ) {
  233. struct ftp_request *ftp = stream_to_ftp ( app );
  234. DBGC ( ftp, "FTP %p control connection closed: %s\n",
  235. ftp, strerror ( rc ) );
  236. /* Complete FTP operation */
  237. ftp_done ( ftp, rc );
  238. }
  239. /** FTP control channel operations */
  240. static struct stream_application_operations ftp_stream_operations = {
  241. .closed = ftp_closed,
  242. .acked = ftp_acked,
  243. .newdata = ftp_newdata,
  244. .senddata = ftp_senddata,
  245. };
  246. /*****************************************************************************
  247. *
  248. * FTP data channel
  249. *
  250. */
  251. /**
  252. * Get FTP request from data stream application
  253. *
  254. * @v app Stream application
  255. * @ret ftp FTP request
  256. */
  257. static inline struct ftp_request *
  258. stream_to_ftp_data ( struct stream_application *app ) {
  259. return container_of ( app, struct ftp_request, stream_data );
  260. }
  261. /**
  262. * Handle data channel being closed
  263. *
  264. * @v app Stream application
  265. *
  266. * When the data channel is closed, the control channel should be left
  267. * alone; the server will send a completion message via the control
  268. * channel which we'll pick up.
  269. *
  270. * If the data channel is closed due to an error, we abort the request.
  271. */
  272. static void ftp_data_closed ( struct stream_application *app, int rc ) {
  273. struct ftp_request *ftp = stream_to_ftp_data ( app );
  274. DBGC ( ftp, "FTP %p data connection closed: %s\n",
  275. ftp, strerror ( rc ) );
  276. /* If there was an error, close control channel and record status */
  277. if ( rc )
  278. ftp_done ( ftp, rc );
  279. }
  280. /**
  281. * Handle new data arriving on the FTP data channel
  282. *
  283. * @v app Stream application
  284. * @v data New data
  285. * @v len Length of new data
  286. */
  287. static void ftp_data_newdata ( struct stream_application *app,
  288. void *data, size_t len ) {
  289. struct ftp_request *ftp = stream_to_ftp_data ( app );
  290. int rc;
  291. /* Fill data buffer */
  292. if ( ( rc = fill_buffer ( ftp->buffer, data,
  293. ftp->buffer->fill, len ) ) != 0 ){
  294. DBGC ( ftp, "FTP %p failed to fill data buffer: %s\n",
  295. ftp, strerror ( rc ) );
  296. ftp_done ( ftp, rc );
  297. return;
  298. }
  299. }
  300. /** FTP data channel operations */
  301. static struct stream_application_operations ftp_data_stream_operations = {
  302. .closed = ftp_data_closed,
  303. .newdata = ftp_data_newdata,
  304. };
  305. /*****************************************************************************
  306. *
  307. * API
  308. *
  309. */
  310. /**
  311. * Reap asynchronous operation
  312. *
  313. * @v async Asynchronous operation
  314. */
  315. static void ftp_reap ( struct async *async ) {
  316. struct ftp_request *ftp =
  317. container_of ( async, struct ftp_request, async );
  318. free ( ftp );
  319. }
  320. /** FTP asynchronous operations */
  321. static struct async_operations ftp_async_operations = {
  322. .reap = ftp_reap,
  323. };
  324. /**
  325. * Initiate an FTP connection
  326. *
  327. * @v uri Uniform Resource Identifier
  328. * @v buffer Buffer into which to download file
  329. * @v parent Parent asynchronous operation
  330. * @ret rc Return status code
  331. */
  332. int ftp_get ( struct uri *uri, struct buffer *buffer, struct async *parent ) {
  333. struct ftp_request *ftp = NULL;
  334. int rc;
  335. /* Sanity checks */
  336. if ( ! uri->path ) {
  337. rc = -EINVAL;
  338. goto err;
  339. }
  340. /* Allocate and populate FTP structure */
  341. ftp = malloc ( sizeof ( *ftp ) );
  342. if ( ! ftp ) {
  343. rc = -ENOMEM;
  344. goto err;
  345. }
  346. memset ( ftp, 0, sizeof ( *ftp ) );
  347. ftp->uri = uri;
  348. ftp->buffer = buffer;
  349. ftp->state = FTP_CONNECT;
  350. ftp->already_sent = 0;
  351. ftp->recvbuf = ftp->status_text;
  352. ftp->recvsize = sizeof ( ftp->status_text ) - 1;
  353. ftp->stream.op = &ftp_stream_operations;
  354. ftp->stream_data.op = &ftp_data_stream_operations;
  355. #warning "Quick name resolution hack"
  356. union {
  357. struct sockaddr sa;
  358. struct sockaddr_in sin;
  359. } server;
  360. server.sin.sin_port = htons ( FTP_PORT );
  361. server.sin.sin_family = AF_INET;
  362. if ( inet_aton ( uri->host, &server.sin.sin_addr ) == 0 ) {
  363. rc = -EINVAL;
  364. goto err;
  365. }
  366. DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
  367. if ( ( rc = tcp_open ( &ftp->stream ) ) != 0 )
  368. goto err;
  369. if ( ( rc = stream_connect ( &ftp->stream, &server.sa ) ) != 0 )
  370. goto err;
  371. async_init ( &ftp->async, &ftp_async_operations, parent );
  372. return 0;
  373. err:
  374. DBGC ( ftp, "FTP %p could not create request: %s\n",
  375. ftp, strerror ( rc ) );
  376. free ( ftp );
  377. return rc;
  378. }
  379. /** HTTP download protocol */
  380. struct download_protocol ftp_download_protocol __download_protocol = {
  381. .name = "ftp",
  382. .start_download = ftp_get,
  383. };