8 #include <gpxe/socket.h>
9 #include <gpxe/tcpip.h>
11 #include <gpxe/xfer.h>
12 #include <gpxe/open.h>
14 #include <gpxe/features.h>
19 * File transfer protocol
23 FEATURE ( FEATURE_PROTOCOL
, "FTP", DHCP_EB_FEATURE_FTP
, 1 );
28 * These @b must be sequential, i.e. a successful FTP session must
29 * pass through each of these states in order.
48 /** Reference counter */
50 /** Data transfer interface */
51 struct xfer_interface xfer
;
53 /** URI being fetched */
55 /** FTP control channel interface */
56 struct xfer_interface control
;
57 /** FTP data channel interface */
58 struct xfer_interface data
;
62 /** Buffer to be filled with data received via the control channel */
64 /** Remaining size of recvbuf */
66 /** FTP status code, as text */
68 /** Passive-mode parameters, as text */
69 char passive_text
[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
75 * @v refcnt Reference counter
77 static void ftp_free ( struct refcnt
*refcnt
) {
78 struct ftp_request
*ftp
=
79 container_of ( refcnt
, struct ftp_request
, refcnt
);
81 DBGC ( ftp
, "FTP %p freed\n", ftp
);
88 * Mark FTP operation as complete
91 * @v rc Return status code
93 static void ftp_done ( struct ftp_request
*ftp
, int rc
) {
95 DBGC ( ftp
, "FTP %p completed (%s)\n", ftp
, strerror ( rc
) );
97 /* Close all data transfer interfaces */
98 xfer_nullify ( &ftp
->xfer
);
99 xfer_close ( &ftp
->xfer
, rc
);
100 xfer_nullify ( &ftp
->control
);
101 xfer_close ( &ftp
->control
, rc
);
102 xfer_nullify ( &ftp
->data
);
103 xfer_close ( &ftp
->data
, rc
);
106 /*****************************************************************************
108 * FTP control channel
112 /** An FTP control channel string */
113 struct ftp_control_string
{
114 /** Literal portion */
119 * @ret string Variable portion of string
121 const char * ( *variable
) ( struct ftp_request
*ftp
);
125 * Retrieve FTP pathname
128 * @ret path FTP pathname
130 static const char * ftp_uri_path ( struct ftp_request
*ftp
) {
131 return ftp
->uri
->path
;
140 static const char * ftp_user ( struct ftp_request
*ftp
) {
141 static char *ftp_default_user
= "anonymous";
142 return ftp
->uri
->user
? ftp
->uri
->user
: ftp_default_user
;
146 * Retrieve FTP password
149 * @ret password FTP password
151 static const char * ftp_password ( struct ftp_request
*ftp
) {
152 static char *ftp_default_password
= "etherboot@etherboot.org";
153 return ftp
->uri
->password
? ftp
->uri
->password
: ftp_default_password
;
156 /** FTP control channel strings */
157 static struct ftp_control_string ftp_strings
[] = {
158 [FTP_CONNECT
] = { NULL
, NULL
},
159 [FTP_USER
] = { "USER ", ftp_user
},
160 [FTP_PASS
] = { "PASS ", ftp_password
},
161 [FTP_TYPE
] = { "TYPE I", NULL
},
162 [FTP_PASV
] = { "PASV", NULL
},
163 [FTP_RETR
] = { "RETR ", ftp_uri_path
},
164 [FTP_WAIT
] = { NULL
, NULL
},
165 [FTP_QUIT
] = { "QUIT", NULL
},
166 [FTP_DONE
] = { NULL
, NULL
},
170 * Handle control channel being closed
172 * @v control FTP control channel interface
173 * @v rc Reason for close
175 * When the control channel is closed, the data channel must also be
176 * closed, if it is currently open.
178 static void ftp_control_close ( struct xfer_interface
*control
, int rc
) {
179 struct ftp_request
*ftp
=
180 container_of ( control
, struct ftp_request
, control
);
182 DBGC ( ftp
, "FTP %p control connection closed: %s\n",
183 ftp
, strerror ( rc
) );
185 /* Complete FTP operation */
186 ftp_done ( ftp
, rc
);
190 * Parse FTP byte sequence value
192 * @v text Text string
193 * @v value Value buffer
194 * @v len Length of value buffer
196 * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
197 * form for IP addresses in PORT commands) into a byte sequence. @c
198 * *text will be updated to point beyond the end of the parsed byte
201 * This function is safe in the presence of malformed data, though the
202 * output is undefined.
204 static void ftp_parse_value ( char **text
, uint8_t *value
, size_t len
) {
206 *(value
++) = strtoul ( *text
, text
, 10 );
213 * Move to next state and send the appropriate FTP control string
218 static void ftp_next_state ( struct ftp_request
*ftp
) {
219 struct ftp_control_string
*ftp_string
;
221 const char *variable
;
223 /* Move to next state */
224 if ( ftp
->state
< FTP_DONE
)
227 /* Send control string if needed */
228 ftp_string
= &ftp_strings
[ftp
->state
];
229 literal
= ftp_string
->literal
;
230 variable
= ( ftp_string
->variable
?
231 ftp_string
->variable ( ftp
) : "" );
233 DBGC ( ftp
, "FTP %p sending %s%s\n", ftp
, literal
, variable
);
234 xfer_printf ( &ftp
->control
, "%s%s\r\n", literal
, variable
);
239 * Handle an FTP control channel response
243 * This is called once we have received a complete response line.
245 static void ftp_reply ( struct ftp_request
*ftp
) {
246 char status_major
= ftp
->status_text
[0];
247 char separator
= ftp
->status_text
[3];
249 DBGC ( ftp
, "FTP %p received status %s\n", ftp
, ftp
->status_text
);
251 /* Ignore malformed lines */
252 if ( separator
!= ' ' )
255 /* Ignore "intermediate" responses (1xx codes) */
256 if ( status_major
== '1' )
259 /* Anything other than success (2xx) or, in the case of a
260 * repsonse to a "USER" command, a password prompt (3xx), is a
263 if ( ! ( ( status_major
== '2' ) ||
264 ( ( status_major
== '3' ) && ( ftp
->state
== FTP_USER
) ) ) ){
265 /* Flag protocol error and close connections */
266 ftp_done ( ftp
, -EPROTO
);
270 /* Open passive connection when we get "PASV" response */
271 if ( ftp
->state
== FTP_PASV
) {
272 char *ptr
= ftp
->passive_text
;
274 struct sockaddr_in sin
;
279 sa
.sin
.sin_family
= AF_INET
;
280 ftp_parse_value ( &ptr
, ( uint8_t * ) &sa
.sin
.sin_addr
,
281 sizeof ( sa
.sin
.sin_addr
) );
282 ftp_parse_value ( &ptr
, ( uint8_t * ) &sa
.sin
.sin_port
,
283 sizeof ( sa
.sin
.sin_port
) );
284 if ( ( rc
= xfer_open_socket ( &ftp
->data
, SOCK_STREAM
,
285 &sa
.sa
, NULL
) ) != 0 ) {
286 DBGC ( ftp
, "FTP %p could not open data connection\n",
288 ftp_done ( ftp
, rc
);
293 /* Move to next state and send control string */
294 ftp_next_state ( ftp
);
299 * Handle new data arriving on FTP control channel
301 * @v control FTP control channel interface
303 * @v len Length of new data
305 * Data is collected until a complete line is received, at which point
306 * its information is passed to ftp_reply().
308 static int ftp_control_deliver_raw ( struct xfer_interface
*control
,
309 const void *data
, size_t len
) {
310 struct ftp_request
*ftp
=
311 container_of ( control
, struct ftp_request
, control
);
312 char *recvbuf
= ftp
->recvbuf
;
313 size_t recvsize
= ftp
->recvsize
;
317 c
= * ( ( char * ) data
++ );
321 /* End of line: call ftp_reply() to handle
322 * completed reply. Avoid calling ftp_reply()
323 * twice if we receive both \r and \n.
327 /* Start filling up the status code buffer */
328 recvbuf
= ftp
->status_text
;
329 recvsize
= sizeof ( ftp
->status_text
) - 1;
332 /* Start filling up the passive parameter buffer */
333 recvbuf
= ftp
->passive_text
;
334 recvsize
= sizeof ( ftp
->passive_text
) - 1;
337 /* Stop filling the passive parameter buffer */
341 /* Fill up buffer if applicable */
342 if ( recvsize
> 0 ) {
350 /* Store for next invocation */
351 ftp
->recvbuf
= recvbuf
;
352 ftp
->recvsize
= recvsize
;
357 /** FTP control channel operations */
358 static struct xfer_interface_operations ftp_control_operations
= {
359 .close
= ftp_control_close
,
360 .vredirect
= xfer_vreopen
,
361 .window
= unlimited_xfer_window
,
362 .alloc_iob
= default_xfer_alloc_iob
,
363 .deliver_iob
= xfer_deliver_as_raw
,
364 .deliver_raw
= ftp_control_deliver_raw
,
367 /*****************************************************************************
374 * Handle FTP data channel being closed
376 * @v data FTP data channel interface
377 * @v rc Reason for closure
379 * When the data channel is closed, the control channel should be left
380 * alone; the server will send a completion message via the control
381 * channel which we'll pick up.
383 * If the data channel is closed due to an error, we abort the request.
385 static void ftp_data_closed ( struct xfer_interface
*data
, int rc
) {
386 struct ftp_request
*ftp
=
387 container_of ( data
, struct ftp_request
, data
);
389 DBGC ( ftp
, "FTP %p data connection closed: %s\n",
390 ftp
, strerror ( rc
) );
392 /* If there was an error, close control channel and record status */
394 ftp_done ( ftp
, rc
);
396 ftp_next_state ( ftp
);
401 * Handle data delivery via FTP data channel
403 * @v xfer FTP data channel interface
404 * @v iobuf I/O buffer
405 * @v meta Data transfer metadata
406 * @ret rc Return status code
408 static int ftp_data_deliver_iob ( struct xfer_interface
*data
,
409 struct io_buffer
*iobuf
,
410 struct xfer_metadata
*meta __unused
) {
411 struct ftp_request
*ftp
=
412 container_of ( data
, struct ftp_request
, data
);
415 if ( ( rc
= xfer_deliver_iob ( &ftp
->xfer
, iobuf
) ) != 0 ) {
416 DBGC ( ftp
, "FTP %p failed to deliver data: %s\n",
417 ftp
, strerror ( rc
) );
424 /** FTP data channel operations */
425 static struct xfer_interface_operations ftp_data_operations
= {
426 .close
= ftp_data_closed
,
427 .vredirect
= xfer_vreopen
,
428 .window
= unlimited_xfer_window
,
429 .alloc_iob
= default_xfer_alloc_iob
,
430 .deliver_iob
= ftp_data_deliver_iob
,
431 .deliver_raw
= xfer_deliver_as_iob
,
434 /*****************************************************************************
436 * Data transfer interface
441 * Close FTP data transfer interface
443 * @v xfer FTP data transfer interface
444 * @v rc Reason for close
446 static void ftp_xfer_closed ( struct xfer_interface
*xfer
, int rc
) {
447 struct ftp_request
*ftp
=
448 container_of ( xfer
, struct ftp_request
, xfer
);
450 DBGC ( ftp
, "FTP %p data transfer interface closed: %s\n",
451 ftp
, strerror ( rc
) );
453 ftp_done ( ftp
, rc
);
456 /** FTP data transfer interface operations */
457 static struct xfer_interface_operations ftp_xfer_operations
= {
458 .close
= ftp_xfer_closed
,
459 .vredirect
= ignore_xfer_vredirect
,
460 .window
= unlimited_xfer_window
,
461 .alloc_iob
= default_xfer_alloc_iob
,
462 .deliver_iob
= xfer_deliver_as_raw
,
463 .deliver_raw
= ignore_xfer_deliver_raw
,
466 /*****************************************************************************
473 * Initiate an FTP connection
475 * @v xfer Data transfer interface
476 * @v uri Uniform Resource Identifier
477 * @ret rc Return status code
479 static int ftp_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
480 struct ftp_request
*ftp
;
481 struct sockaddr_tcpip server
;
490 /* Allocate and populate structure */
491 ftp
= zalloc ( sizeof ( *ftp
) );
494 ftp
->refcnt
.free
= ftp_free
;
495 xfer_init ( &ftp
->xfer
, &ftp_xfer_operations
, &ftp
->refcnt
);
496 ftp
->uri
= uri_get ( uri
);
497 xfer_init ( &ftp
->control
, &ftp_control_operations
, &ftp
->refcnt
);
498 xfer_init ( &ftp
->data
, &ftp_data_operations
, &ftp
->refcnt
);
499 ftp
->recvbuf
= ftp
->status_text
;
500 ftp
->recvsize
= sizeof ( ftp
->status_text
) - 1;
502 DBGC ( ftp
, "FTP %p fetching %s\n", ftp
, ftp
->uri
->path
);
504 /* Open control connection */
505 memset ( &server
, 0, sizeof ( server
) );
506 server
.st_port
= htons ( uri_port ( uri
, FTP_PORT
) );
507 if ( ( rc
= xfer_open_named_socket ( &ftp
->control
, SOCK_STREAM
,
508 ( struct sockaddr
* ) &server
,
509 uri
->host
, NULL
) ) != 0 )
512 /* Attach to parent interface, mortalise self, and return */
513 xfer_plug_plug ( &ftp
->xfer
, xfer
);
514 ref_put ( &ftp
->refcnt
);
518 DBGC ( ftp
, "FTP %p could not create request: %s\n",
519 ftp
, strerror ( rc
) );
520 ftp_done ( ftp
, rc
);
521 ref_put ( &ftp
->refcnt
);
525 /** FTP URI opener */
526 struct uri_opener ftp_uri_opener __uri_opener
= {