Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ftp.c 10KB

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