2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
28 #include <ipxe/socket.h>
29 #include <ipxe/tcpip.h>
31 #include <ipxe/iobuf.h>
32 #include <ipxe/xfer.h>
33 #include <ipxe/open.h>
35 #include <ipxe/features.h>
40 * File transfer protocol
44 FEATURE ( FEATURE_PROTOCOL, "FTP", DHCP_EB_FEATURE_FTP, 1 );
49 * These @b must be sequential, i.e. a successful FTP session must
50 * pass through each of these states in order.
70 /** Reference counter */
72 /** Data transfer interface */
73 struct interface xfer;
75 /** URI being fetched */
77 /** FTP control channel interface */
78 struct interface control;
79 /** FTP data channel interface */
80 struct interface data;
84 /** Buffer to be filled with data received via the control channel */
86 /** Remaining size of recvbuf */
88 /** FTP status code, as text */
90 /** Passive-mode parameters, as text */
91 char passive_text[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
92 /** File size, as text */
99 * @v refcnt Reference counter
101 static void ftp_free ( struct refcnt *refcnt ) {
102 struct ftp_request *ftp =
103 container_of ( refcnt, struct ftp_request, refcnt );
105 DBGC ( ftp, "FTP %p freed\n", ftp );
107 uri_put ( ftp->uri );
112 * Mark FTP operation as complete
115 * @v rc Return status code
117 static void ftp_done ( struct ftp_request *ftp, int rc ) {
119 DBGC ( ftp, "FTP %p completed (%s)\n", ftp, strerror ( rc ) );
121 /* Close all data transfer interfaces */
122 intf_shutdown ( &ftp->data, rc );
123 intf_shutdown ( &ftp->control, rc );
124 intf_shutdown ( &ftp->xfer, rc );
127 /*****************************************************************************
129 * FTP control channel
133 /** An FTP control channel string */
134 struct ftp_control_string {
135 /** Literal portion */
140 * @ret string Variable portion of string
142 const char * ( *variable ) ( struct ftp_request *ftp );
146 * Retrieve FTP pathname
149 * @ret path FTP pathname
151 static const char * ftp_uri_path ( struct ftp_request *ftp ) {
152 return ftp->uri->path;
161 static const char * ftp_user ( struct ftp_request *ftp ) {
162 static char *ftp_default_user = "anonymous";
163 return ftp->uri->user ? ftp->uri->user : ftp_default_user;
167 * Retrieve FTP password
170 * @ret password FTP password
172 static const char * ftp_password ( struct ftp_request *ftp ) {
173 static char *ftp_default_password = "ipxe@ipxe.org";
174 return ftp->uri->password ? ftp->uri->password : ftp_default_password;
177 /** FTP control channel strings */
178 static struct ftp_control_string ftp_strings[] = {
179 [FTP_CONNECT] = { NULL, NULL },
180 [FTP_USER] = { "USER ", ftp_user },
181 [FTP_PASS] = { "PASS ", ftp_password },
182 [FTP_TYPE] = { "TYPE I", NULL },
183 [FTP_SIZE] = { "SIZE ", ftp_uri_path },
184 [FTP_PASV] = { "PASV", NULL },
185 [FTP_RETR] = { "RETR ", ftp_uri_path },
186 [FTP_WAIT] = { NULL, NULL },
187 [FTP_QUIT] = { "QUIT", NULL },
188 [FTP_DONE] = { NULL, NULL },
192 * Parse FTP byte sequence value
194 * @v text Text string
195 * @v value Value buffer
196 * @v len Length of value buffer
198 * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
199 * form for IP addresses in PORT commands) into a byte sequence. @c
200 * *text will be updated to point beyond the end of the parsed byte
203 * This function is safe in the presence of malformed data, though the
204 * output is undefined.
206 static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
208 *(value++) = strtoul ( *text, text, 10 );
215 * Move to next state and send the appropriate FTP control string
220 static void ftp_next_state ( struct ftp_request *ftp ) {
221 struct ftp_control_string *ftp_string;
223 const char *variable;
225 /* Move to next state */
226 if ( ftp->state < FTP_DONE )
229 /* Send control string if needed */
230 ftp_string = &ftp_strings[ftp->state];
231 literal = ftp_string->literal;
232 variable = ( ftp_string->variable ?
233 ftp_string->variable ( ftp ) : "" );
235 DBGC ( ftp, "FTP %p sending %s%s\n", ftp, literal, variable );
236 xfer_printf ( &ftp->control, "%s%s\r\n", literal, variable );
241 * Handle an FTP control channel response
245 * This is called once we have received a complete response line.
247 static void ftp_reply ( struct ftp_request *ftp ) {
248 char status_major = ftp->status_text[0];
249 char separator = ftp->status_text[3];
251 DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
253 /* Ignore malformed lines */
254 if ( separator != ' ' )
257 /* Ignore "intermediate" responses (1xx codes) */
258 if ( status_major == '1' )
261 /* If the SIZE command is not supported by the server, we go to
264 if ( ( status_major == '5' ) && ( ftp->state == FTP_SIZE ) ) {
265 ftp_next_state ( ftp );
269 /* Anything other than success (2xx) or, in the case of a
270 * repsonse to a "USER" command, a password prompt (3xx), is a
273 if ( ! ( ( status_major == '2' ) ||
274 ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
275 /* Flag protocol error and close connections */
276 ftp_done ( ftp, -EPROTO );
280 /* Parse file size */
281 if ( ftp->state == FTP_SIZE ) {
286 filesize = strtoul ( ftp->filesize, &endptr, 10 );
287 if ( *endptr != '\0' ) {
288 DBGC ( ftp, "FTP %p invalid SIZE \"%s\"\n",
289 ftp, ftp->filesize );
290 ftp_done ( ftp, -EPROTO );
294 /* Use seek() to notify recipient of filesize */
295 DBGC ( ftp, "FTP %p file size is %zd bytes\n", ftp, filesize );
296 xfer_seek ( &ftp->xfer, filesize );
297 xfer_seek ( &ftp->xfer, 0 );
300 /* Open passive connection when we get "PASV" response */
301 if ( ftp->state == FTP_PASV ) {
302 char *ptr = ftp->passive_text;
304 struct sockaddr_in sin;
309 sa.sin.sin_family = AF_INET;
310 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
311 sizeof ( sa.sin.sin_addr ) );
312 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
313 sizeof ( sa.sin.sin_port ) );
314 if ( ( rc = xfer_open_socket ( &ftp->data, SOCK_STREAM,
315 &sa.sa, NULL ) ) != 0 ) {
316 DBGC ( ftp, "FTP %p could not open data connection\n",
318 ftp_done ( ftp, rc );
323 /* Move to next state and send control string */
324 ftp_next_state ( ftp );
329 * Handle new data arriving on FTP control channel
333 * @v meta Data transfer metadata
334 * @ret rc Return status code
336 * Data is collected until a complete line is received, at which point
337 * its information is passed to ftp_reply().
339 static int ftp_control_deliver ( struct ftp_request *ftp,
340 struct io_buffer *iobuf,
341 struct xfer_metadata *meta __unused ) {
342 char *data = iobuf->data;
343 size_t len = iob_len ( iobuf );
344 char *recvbuf = ftp->recvbuf;
345 size_t recvsize = ftp->recvsize;
350 if ( ( c == '\r' ) || ( c == '\n' ) ) {
351 /* End of line: call ftp_reply() to handle
352 * completed reply. Avoid calling ftp_reply()
353 * twice if we receive both \r and \n.
355 if ( recvbuf != ftp->status_text )
357 /* Start filling up the status code buffer */
358 recvbuf = ftp->status_text;
359 recvsize = sizeof ( ftp->status_text ) - 1;
360 } else if ( ( ftp->state == FTP_PASV ) && ( c == '(' ) ) {
361 /* Start filling up the passive parameter buffer */
362 recvbuf = ftp->passive_text;
363 recvsize = sizeof ( ftp->passive_text ) - 1;
364 } else if ( ( ftp->state == FTP_PASV ) && ( c == ')' ) ) {
365 /* Stop filling the passive parameter buffer */
367 } else if ( ( ftp->state == FTP_SIZE ) && ( c == ' ' ) ) {
368 /* Start filling up the file size buffer */
369 recvbuf = ftp->filesize;
370 recvsize = sizeof ( ftp->filesize ) - 1;
372 /* Fill up buffer if applicable */
373 if ( recvsize > 0 ) {
380 /* Store for next invocation */
381 ftp->recvbuf = recvbuf;
382 ftp->recvsize = recvsize;
384 /* Free I/O buffer */
390 /** FTP control channel interface operations */
391 static struct interface_operation ftp_control_operations[] = {
392 INTF_OP ( xfer_deliver, struct ftp_request *, ftp_control_deliver ),
393 INTF_OP ( intf_close, struct ftp_request *, ftp_done ),
396 /** FTP control channel interface descriptor */
397 static struct interface_descriptor ftp_control_desc =
398 INTF_DESC ( struct ftp_request, control, ftp_control_operations );
400 /*****************************************************************************
407 * Handle FTP data channel being closed
410 * @v rc Reason for closure
412 * When the data channel is closed, the control channel should be left
413 * alone; the server will send a completion message via the control
414 * channel which we'll pick up.
416 * If the data channel is closed due to an error, we abort the request.
418 static void ftp_data_closed ( struct ftp_request *ftp, int rc ) {
420 DBGC ( ftp, "FTP %p data connection closed: %s\n",
421 ftp, strerror ( rc ) );
423 /* If there was an error, close control channel and record status */
425 ftp_done ( ftp, rc );
427 ftp_next_state ( ftp );
431 /** FTP data channel interface operations */
432 static struct interface_operation ftp_data_operations[] = {
433 INTF_OP ( intf_close, struct ftp_request *, ftp_data_closed ),
436 /** FTP data channel interface descriptor */
437 static struct interface_descriptor ftp_data_desc =
438 INTF_DESC_PASSTHRU ( struct ftp_request, data, ftp_data_operations,
441 /*****************************************************************************
443 * Data transfer interface
447 /** FTP data transfer interface operations */
448 static struct interface_operation ftp_xfer_operations[] = {
449 INTF_OP ( intf_close, struct ftp_request *, ftp_done ),
452 /** FTP data transfer interface descriptor */
453 static struct interface_descriptor ftp_xfer_desc =
454 INTF_DESC_PASSTHRU ( struct ftp_request, xfer, ftp_xfer_operations,
457 /*****************************************************************************
464 * Check validity of FTP control channel string
467 * @ret rc Return status code
469 static int ftp_check_string ( const char *string ) {
472 /* The FTP control channel is line-based. Check for invalid
473 * non-printable characters (e.g. newlines).
475 while ( ( c = *(string++) ) ) {
476 if ( ! isprint ( c ) )
483 * Initiate an FTP connection
485 * @v xfer Data transfer interface
486 * @v uri Uniform Resource Identifier
487 * @ret rc Return status code
489 static int ftp_open ( struct interface *xfer, struct uri *uri ) {
490 struct ftp_request *ftp;
491 struct sockaddr_tcpip server;
499 if ( ( rc = ftp_check_string ( uri->path ) ) != 0 )
501 if ( uri->user && ( ( rc = ftp_check_string ( uri->user ) ) != 0 ) )
503 if ( uri->password &&
504 ( ( rc = ftp_check_string ( uri->password ) ) != 0 ) )
507 /* Allocate and populate structure */
508 ftp = zalloc ( sizeof ( *ftp ) );
511 ref_init ( &ftp->refcnt, ftp_free );
512 intf_init ( &ftp->xfer, &ftp_xfer_desc, &ftp->refcnt );
513 intf_init ( &ftp->control, &ftp_control_desc, &ftp->refcnt );
514 intf_init ( &ftp->data, &ftp_data_desc, &ftp->refcnt );
515 ftp->uri = uri_get ( uri );
516 ftp->recvbuf = ftp->status_text;
517 ftp->recvsize = sizeof ( ftp->status_text ) - 1;
519 DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
521 /* Open control connection */
522 memset ( &server, 0, sizeof ( server ) );
523 server.st_port = htons ( uri_port ( uri, FTP_PORT ) );
524 if ( ( rc = xfer_open_named_socket ( &ftp->control, SOCK_STREAM,
525 ( struct sockaddr * ) &server,
526 uri->host, NULL ) ) != 0 )
529 /* Attach to parent interface, mortalise self, and return */
530 intf_plug_plug ( &ftp->xfer, xfer );
531 ref_put ( &ftp->refcnt );
535 DBGC ( ftp, "FTP %p could not create request: %s\n",
536 ftp, strerror ( rc ) );
537 ftp_done ( ftp, rc );
538 ref_put ( &ftp->refcnt );
542 /** FTP URI opener */
543 struct uri_opener ftp_uri_opener __uri_opener = {