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
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <byteswap.h>
28 #include <ipxe/socket.h>
29 #include <ipxe/tcpip.h>
30 #include <ipxe/in.h>
31 #include <ipxe/iobuf.h>
32 #include <ipxe/xfer.h>
33 #include <ipxe/open.h>
34 #include <ipxe/uri.h>
35 #include <ipxe/features.h>
36 #include <ipxe/ftp.h>
37
38 /** @file
39 *
40 * File transfer protocol
41 *
42 */
43
44 FEATURE ( FEATURE_PROTOCOL, "FTP", DHCP_EB_FEATURE_FTP, 1 );
45
46 /**
47 * FTP states
48 *
49 * These @b must be sequential, i.e. a successful FTP session must
50 * pass through each of these states in order.
51 */
52 enum ftp_state {
53 FTP_CONNECT = 0,
54 FTP_USER,
55 FTP_PASS,
56 FTP_TYPE,
57 FTP_SIZE,
58 FTP_PASV,
59 FTP_RETR,
60 FTP_WAIT,
61 FTP_QUIT,
62 FTP_DONE,
63 };
64
65 /**
66 * An FTP request
67 *
68 */
69 struct ftp_request {
70 /** Reference counter */
71 struct refcnt refcnt;
72 /** Data transfer interface */
73 struct interface xfer;
74
75 /** URI being fetched */
76 struct uri *uri;
77 /** FTP control channel interface */
78 struct interface control;
79 /** FTP data channel interface */
80 struct interface data;
81
82 /** Current state */
83 enum ftp_state state;
84 /** Buffer to be filled with data received via the control channel */
85 char *recvbuf;
86 /** Remaining size of recvbuf */
87 size_t recvsize;
88 /** FTP status code, as text */
89 char status_text[5];
90 /** Passive-mode parameters, as text */
91 char passive_text[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
92 /** File size, as text */
93 char filesize[20];
94 };
95
96 /**
97 * Free FTP request
98 *
99 * @v refcnt Reference counter
100 */
ftp_free(struct refcnt * refcnt)101 static void ftp_free ( struct refcnt *refcnt ) {
102 struct ftp_request *ftp =
103 container_of ( refcnt, struct ftp_request, refcnt );
104
105 DBGC ( ftp, "FTP %p freed\n", ftp );
106
107 uri_put ( ftp->uri );
108 free ( ftp );
109 }
110
111 /**
112 * Mark FTP operation as complete
113 *
114 * @v ftp FTP request
115 * @v rc Return status code
116 */
ftp_done(struct ftp_request * ftp,int rc)117 static void ftp_done ( struct ftp_request *ftp, int rc ) {
118
119 DBGC ( ftp, "FTP %p completed (%s)\n", ftp, strerror ( rc ) );
120
121 /* Close all data transfer interfaces */
122 intf_shutdown ( &ftp->data, rc );
123 intf_shutdown ( &ftp->control, rc );
124 intf_shutdown ( &ftp->xfer, rc );
125 }
126
127 /*****************************************************************************
128 *
129 * FTP control channel
130 *
131 */
132
133 /** An FTP control channel string */
134 struct ftp_control_string {
135 /** Literal portion */
136 const char *literal;
137 /** Variable portion
138 *
139 * @v ftp FTP request
140 * @ret string Variable portion of string
141 */
142 const char * ( *variable ) ( struct ftp_request *ftp );
143 };
144
145 /**
146 * Retrieve FTP pathname
147 *
148 * @v ftp FTP request
149 * @ret path FTP pathname
150 */
ftp_uri_path(struct ftp_request * ftp)151 static const char * ftp_uri_path ( struct ftp_request *ftp ) {
152 return ftp->uri->path;
153 }
154
155 /**
156 * Retrieve FTP user
157 *
158 * @v ftp FTP request
159 * @ret user FTP user
160 */
ftp_user(struct ftp_request * ftp)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;
164 }
165
166 /**
167 * Retrieve FTP password
168 *
169 * @v ftp FTP request
170 * @ret password FTP password
171 */
ftp_password(struct ftp_request * ftp)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;
175 }
176
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 },
189 };
190
191 /**
192 * Parse FTP byte sequence value
193 *
194 * @v text Text string
195 * @v value Value buffer
196 * @v len Length of value buffer
197 *
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
201 * sequence.
202 *
203 * This function is safe in the presence of malformed data, though the
204 * output is undefined.
205 */
ftp_parse_value(char ** text,uint8_t * value,size_t len)206 static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
207 do {
208 *(value++) = strtoul ( *text, text, 10 );
209 if ( **text )
210 (*text)++;
211 } while ( --len );
212 }
213
214 /**
215 * Move to next state and send the appropriate FTP control string
216 *
217 * @v ftp FTP request
218 *
219 */
ftp_next_state(struct ftp_request * ftp)220 static void ftp_next_state ( struct ftp_request *ftp ) {
221 struct ftp_control_string *ftp_string;
222 const char *literal;
223 const char *variable;
224
225 /* Move to next state */
226 if ( ftp->state < FTP_DONE )
227 ftp->state++;
228
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 ) : "" );
234 if ( literal ) {
235 DBGC ( ftp, "FTP %p sending %s%s\n", ftp, literal, variable );
236 xfer_printf ( &ftp->control, "%s%s\r\n", literal, variable );
237 }
238 }
239
240 /**
241 * Handle an FTP control channel response
242 *
243 * @v ftp FTP request
244 *
245 * This is called once we have received a complete response line.
246 */
ftp_reply(struct ftp_request * ftp)247 static void ftp_reply ( struct ftp_request *ftp ) {
248 char status_major = ftp->status_text[0];
249 char separator = ftp->status_text[3];
250
251 DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
252
253 /* Ignore malformed lines */
254 if ( separator != ' ' )
255 return;
256
257 /* Ignore "intermediate" responses (1xx codes) */
258 if ( status_major == '1' )
259 return;
260
261 /* If the SIZE command is not supported by the server, we go to
262 * the next step.
263 */
264 if ( ( status_major == '5' ) && ( ftp->state == FTP_SIZE ) ) {
265 ftp_next_state ( ftp );
266 return;
267 }
268
269 /* Anything other than success (2xx) or, in the case of a
270 * repsonse to a "USER" command, a password prompt (3xx), is a
271 * fatal error.
272 */
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 );
277 return;
278 }
279
280 /* Parse file size */
281 if ( ftp->state == FTP_SIZE ) {
282 size_t filesize;
283 char *endptr;
284
285 /* Parse 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 );
291 return;
292 }
293
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 );
298 }
299
300 /* Open passive connection when we get "PASV" response */
301 if ( ftp->state == FTP_PASV ) {
302 char *ptr = ftp->passive_text;
303 union {
304 struct sockaddr_in sin;
305 struct sockaddr sa;
306 } sa;
307 int rc;
308
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",
317 ftp );
318 ftp_done ( ftp, rc );
319 return;
320 }
321 }
322
323 /* Move to next state and send control string */
324 ftp_next_state ( ftp );
325
326 }
327
328 /**
329 * Handle new data arriving on FTP control channel
330 *
331 * @v ftp FTP request
332 * @v iob I/O buffer
333 * @v meta Data transfer metadata
334 * @ret rc Return status code
335 *
336 * Data is collected until a complete line is received, at which point
337 * its information is passed to ftp_reply().
338 */
ftp_control_deliver(struct ftp_request * ftp,struct io_buffer * iobuf,struct xfer_metadata * meta __unused)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;
346 char c;
347
348 while ( len-- ) {
349 c = *(data++);
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.
354 */
355 if ( recvbuf != ftp->status_text )
356 ftp_reply ( ftp );
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 */
366 recvsize = 0;
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;
371 } else {
372 /* Fill up buffer if applicable */
373 if ( recvsize > 0 ) {
374 *(recvbuf++) = c;
375 recvsize--;
376 }
377 }
378 }
379
380 /* Store for next invocation */
381 ftp->recvbuf = recvbuf;
382 ftp->recvsize = recvsize;
383
384 /* Free I/O buffer */
385 free_iob ( iobuf );
386
387 return 0;
388 }
389
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 ),
394 };
395
396 /** FTP control channel interface descriptor */
397 static struct interface_descriptor ftp_control_desc =
398 INTF_DESC ( struct ftp_request, control, ftp_control_operations );
399
400 /*****************************************************************************
401 *
402 * FTP data channel
403 *
404 */
405
406 /**
407 * Handle FTP data channel being closed
408 *
409 * @v ftp FTP request
410 * @v rc Reason for closure
411 *
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.
415 *
416 * If the data channel is closed due to an error, we abort the request.
417 */
ftp_data_closed(struct ftp_request * ftp,int rc)418 static void ftp_data_closed ( struct ftp_request *ftp, int rc ) {
419
420 DBGC ( ftp, "FTP %p data connection closed: %s\n",
421 ftp, strerror ( rc ) );
422
423 /* If there was an error, close control channel and record status */
424 if ( rc ) {
425 ftp_done ( ftp, rc );
426 } else {
427 ftp_next_state ( ftp );
428 }
429 }
430
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 ),
434 };
435
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,
439 xfer );
440
441 /*****************************************************************************
442 *
443 * Data transfer interface
444 *
445 */
446
447 /** FTP data transfer interface operations */
448 static struct interface_operation ftp_xfer_operations[] = {
449 INTF_OP ( intf_close, struct ftp_request *, ftp_done ),
450 };
451
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,
455 data );
456
457 /*****************************************************************************
458 *
459 * URI opener
460 *
461 */
462
463 /**
464 * Check validity of FTP control channel string
465 *
466 * @v string String
467 * @ret rc Return status code
468 */
ftp_check_string(const char * string)469 static int ftp_check_string ( const char *string ) {
470 char c;
471
472 /* The FTP control channel is line-based. Check for invalid
473 * non-printable characters (e.g. newlines).
474 */
475 while ( ( c = *(string++) ) ) {
476 if ( ! isprint ( c ) )
477 return -EINVAL;
478 }
479 return 0;
480 }
481
482 /**
483 * Initiate an FTP connection
484 *
485 * @v xfer Data transfer interface
486 * @v uri Uniform Resource Identifier
487 * @ret rc Return status code
488 */
ftp_open(struct interface * xfer,struct uri * uri)489 static int ftp_open ( struct interface *xfer, struct uri *uri ) {
490 struct ftp_request *ftp;
491 struct sockaddr_tcpip server;
492 int rc;
493
494 /* Sanity checks */
495 if ( ! uri->host )
496 return -EINVAL;
497 if ( ! uri->path )
498 return -EINVAL;
499 if ( ( rc = ftp_check_string ( uri->path ) ) != 0 )
500 return rc;
501 if ( uri->user && ( ( rc = ftp_check_string ( uri->user ) ) != 0 ) )
502 return rc;
503 if ( uri->password &&
504 ( ( rc = ftp_check_string ( uri->password ) ) != 0 ) )
505 return rc;
506
507 /* Allocate and populate structure */
508 ftp = zalloc ( sizeof ( *ftp ) );
509 if ( ! ftp )
510 return -ENOMEM;
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;
518
519 DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
520
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 )
527 goto err;
528
529 /* Attach to parent interface, mortalise self, and return */
530 intf_plug_plug ( &ftp->xfer, xfer );
531 ref_put ( &ftp->refcnt );
532 return 0;
533
534 err:
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 );
539 return rc;
540 }
541
542 /** FTP URI opener */
543 struct uri_opener ftp_uri_opener __uri_opener = {
544 .scheme = "ftp",
545 .open = ftp_open,
546 };
547