Debian Codegraph @

ftp/main.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1985, 1989, 1993, 1994
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /*
49 * FTP User Program -- Command Interface.
50 */
51
52 #include <config.h>
53
54 /*#include <sys/ioctl.h>*/
55 #include <sys/types.h>
56 #include <sys/socket.h>
57
58 #include <arpa/ftp.h>
59
60 #include <argp.h>
61 #include <ctype.h>
62 #include <error.h>
63 #include <netdb.h>
64 #include <pwd.h>
65 #include <signal.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 #ifdef HAVE_LOCALE_H
71 # include <locale.h>
72 #endif
73
74 /* Define macro to nothing so declarations
75 * in "ftp_var.h" become definitions.
76 */
77 #define FTP_EXTERN
78 #include "ftp_var.h"
79
80 #include "libinetutils.h"
81 #include "attribute.h"
82
83 #ifdef HAVE_READLINE_READLINE_H
84 # include <readline/readline.h>
85 #elif defined HAVE_EDITLINE_READLINE_H
86 # include <editline/readline.h>
87 #endif
88 #ifdef HAVE_READLINE_HISTORY_H
89 # include <readline/history.h>
90 #elif defined HAVE_EDITLINE_HISTORY_H
91 # include <editline/history.h>
92 #endif
93
94
95 static char *slurpstring (void);
96
97 static char *argbase; /* current pointer into arg buffer */
98 static char *argbuf; /* allocated argument storage buffer */
99 static char *stringbase; /* current scan point in line buffer */
100
101 #define DEFAULT_PROMPT "ftp> "
102 static char *prompt = NULL;
103
104 const char args_doc[] = "[HOST [PORT]]";
105 const char doc[] = "Remote file transfer.";
106
107 enum
108 {
109 OPT_PROMPT = CHAR_MAX + 1,
110 };
111
112 static struct argp_option argp_options[] = {
113 #define GRP 0
114 {"debug", 'd', NULL, 0, "enable debugging output", GRP + 1},
115 {"no-edit", 'e', NULL, 0,
116 #if HAVE_READLINE
117 "disable command line editing",
118 #else /* !HAVE_READLINE */
119 "(ignored)",
120 #endif /* !HAVE_READLINE */
121 GRP + 1},
122 {"no-glob", 'g', NULL, 0, "turn off file name globbing", GRP + 1},
123 {"no-prompt", 'i', NULL, 0, "do not prompt during multiple file transfers",
124 GRP + 1},
125 {"no-login", 'n', NULL, 0,
126 "do not automatically login to the remote system",
127 GRP + 1},
128 {"trace", 't', NULL, 0, "enable packet tracing", GRP + 1},
129 {"passive", 'p', NULL, 0,
130 "enable passive mode transfer, default for `pftp'", GRP + 1},
131 {"active", 'A', NULL, 0, "enable active mode transfer", GRP + 1},
132 {"prompt", OPT_PROMPT, "PROMPT", OPTION_ARG_OPTIONAL,
133 "print a command line PROMPT " "(optionally), even if not on a tty",
134 GRP + 1},
135 {"verbose", 'v', NULL, 0, "verbose output", GRP + 1},
136 {"ipv4", '4', NULL, 0, "contact IPv4 hosts", GRP + 1},
137 {"ipv6", '6', NULL, 0, "contact IPv6 hosts", GRP + 1},
138 {"netrc", 'N', "NETRC", 0, "select a specific initialization file",
139 GRP + 1},
140 #undef GRP
141 {NULL, 0, NULL, 0, NULL, 0}
142 };
143
144 static error_t
145 parse_opt (int key, char *arg, struct argp_state *state MAYBE_UNUSED)
146 {
147 switch (key)
148 {
149 case 'd': /* Enable debug mode. */
150 options |= SO_DEBUG;
151 debug++;
152 break;
153
154 case 'e':
155 usereadline = 0; /* No editing. */
156 break;
157
158 case 'g': /* No glob. */
159 doglob = 0;
160 break;
161
162 case 'i': /* No prompt. */
163 interactive = 0;
164 break;
165
166 case 'n': /* No automatic login. */
167 autologin = 0;
168 break;
169
170 case 't': /* Enable packet tracing. */
171 trace++;
172 break;
173
174 case 'v': /* Verbose. */
175 verbose++;
176 break;
177
178 case OPT_PROMPT: /* Print command line prompt. */
179 prompt = arg ? arg : DEFAULT_PROMPT;
180 break;
181
182 case 'p': /* Enable passive transfer mode. */
183 passivemode = 1;
184 break;
185
186 case 'A': /* Enable active transfer mode. */
187 passivemode = 0;
188 break;
189
190 case '4':
191 usefamily = AF_INET;
192 break;
193
194 case '6':
195 usefamily = AF_INET6;
196 break;
197
198 case 'N':
199 netrc = arg;
200 break;
201
202 default:
203 return ARGP_ERR_UNKNOWN;
204 }
205
206 return 0;
207 }
208
209 static struct argp argp =
210 { argp_options, parse_opt, args_doc, doc, NULL, NULL, NULL };
211
212
213 int
214 main (int argc, char *argv[])
215 {
216 int top;
217 int index;
218 struct passwd *pw = NULL;
219 char *cp;
220
221 set_program_name (argv[0]);
222
223 #ifdef HAVE_SETLOCALE
224 setlocale (LC_ALL, "");
225 #endif
226
227 doglob = 1;
228 interactive = 1;
229 autologin = 1;
230 passivemode = 0; /* passive mode not active */
231 doepsv4 = 0; /* use EPRT/EPSV for IPv4 */
232 usefamily = AF_UNSPEC; /* allow any address family */
233 usereadline = 1; /* normally using readline */
234
235 line = NULL; /* reset global input */
236 linelen = 0;
237 argbuf = NULL;
238 netrc = NULL;
239
240 /* Invoked as `pftp'? Then set passive mode. */
241 cp = strrchr (argv[0], '/');
242 if (cp)
243 cp++;
244 else
245 cp = argv[0];
246 if (!strcmp ("pftp", cp))
247 passivemode = 1;
248
249 /* Parse command line */
250 iu_argp_init ("ftp", default_program_authors);
251 argp_parse (&argp, argc, argv, 0, &index, NULL);
252
253 argc -= index;
254 argv += index;
255
256 fromatty = isatty (fileno (stdin));
257 if (fromatty)
258 {
259 verbose++;
260 if (!prompt)
261 prompt = DEFAULT_PROMPT;
262
263 cp = getenv ("TERM");
264 if (cp == NULL || strcmp (cp, "dumb") == 0)
265 usereadline = 0;
266 }
267 else
268 usereadline = 0;
269
270 cpend = 0; /* no pending replies */
271 proxy = 0; /* proxy not active */
272 crflag = 1; /* strip c.r. on ascii gets */
273 sendport = -1; /* not using ports */
274 /*
275 * Set up the home directory in case we're globbing.
276 */
277 cp = getlogin ();
278 if (cp != NULL)
279 pw = getpwnam (cp);
280 if (pw == NULL)
281 pw = getpwuid (getuid ());
282 if (pw != NULL)
283 {
284 char *buf = malloc (strlen (pw->pw_dir) + 1);
285 if (buf)
286 {
287 strcpy (buf, pw->pw_dir);
288 home = buf;
289 }
290 }
291 if (argc > 0)
292 {
293 char *xargv[5];
294 int i;
295
296 if (setjmp (toplevel))
297 exit (EXIT_SUCCESS);
298 signal (SIGINT, intr);
299 signal (SIGPIPE, lostpeer);
300 xargv[0] = program_invocation_name;
301 for (i = 0; i < argc && i < 3; i++)
302 xargv[i + 1] = argv[i];
303 setpeer (argc + 1, xargv);
304 }
305 top = setjmp (toplevel) == 0;
306 if (top)
307 {
308 signal (SIGINT, intr);
309 signal (SIGPIPE, lostpeer);
310 }
311 for (;;)
312 {
313 cmdscanner (top);
314 top = 1;
315 }
316 }
317
318 void
319 intr (int sig MAYBE_UNUSED)
320 {
321 longjmp (toplevel, 1);
322 }
323
324 void
325 lostpeer (int sig MAYBE_UNUSED)
326 {
327 if (connected)
328 {
329 if (cout != NULL)
330 {
331 shutdown (fileno (cout), 1 + 1);
332 fclose (cout);
333 cout = NULL;
334 }
335 if (data >= 0)
336 {
337 shutdown (data, 1 + 1);
338 close (data);
339 data = -1;
340 }
341 connected = 0;
342 }
343 pswitch (1);
344 if (connected)
345 {
346 if (cout != NULL)
347 {
348 shutdown (fileno (cout), 1 + 1);
349 fclose (cout);
350 cout = NULL;
351 }
352 connected = 0;
353 }
354 proxflag = 0;
355 pswitch (0);
356 }
357
358 /*
359 char *
360 tail(filename)
361 char *filename;
362 {
363 char *s;
364
365 while (*filename) {
366 s = strrchr(filename, '/');
367 if (s == NULL)
368 break;
369 if (s[1])
370 return (s + 1);
371 *s = '\0';
372 }
373 return (filename);
374 }
375 */
376
377 /*
378 * Command parser.
379 */
380 void
381 cmdscanner (int top)
382 {
383 struct cmd *c;
384 ssize_t l;
385
386 if (!top)
387 putchar ('\n');
388 for (;;)
389 {
390 if (line)
391 {
392 free (line);
393 line = NULL;
394 }
395 linelen = 0;
396
397 #if HAVE_READLINE
398 if (usereadline)
399 line = readline (prompt); /* malloc'd, no NL */
400 else
401 #endif /* HAVE_READLINE */
402 {
403 if (prompt)
404 {
405 fprintf (stdout, "%s", prompt);
406 fflush (stdout);
407 }
408
409 /* `linelen' is updated to allocated amount. */
410 l = getline (&line, &linelen, stdin); /* includes NL */
411 if ((l > 0) && line)
412 {
413 char *nl = strchr (line, '\n');
414
415 if (nl)
416 *nl = '\0';
417 }
418 else
419 {
420 /* Allocation takes place even without input. */
421 free (line); /* EOF, et cetera */
422 line = NULL;
423 linelen = 0;
424 }
425
426 if (!fromatty && prompt)
427 fprintf (stdout, "%s\n", line ? line : "");
428 } /* !usereadline ends */
429
430 if (!line)
431 quit (0, 0);
432
433 l = strlen (line);
434 if (l == 0)
435 break;
436
437 #if HAVE_READLINE
438 if (usereadline && line && *line)
439 add_history (line);
440 #endif /* HAVE_READLINE */
441
442 makeargv ();
443 if (margc == 0)
444 continue;
445
446 c = getcmd (margv[0]);
447 if (c == (struct cmd *) -1)
448 {
449 printf ("?Ambiguous command\n");
450 continue;
451 }
452 if (c == 0)
453 {
454 printf ("?Invalid command\n");
455 continue;
456 }
457 if (c->c_conn && !connected)
458 {
459 printf ("Not connected.\n");
460 continue;
461 }
462
463 /* Perform the requested action. */
464 (*c->c_handler) (margc, margv);
465
466 if (bell && c->c_bell)
467 putchar ('\007');
468 if (c->c_handler != help)
469 break;
470 }
471 signal (SIGINT, intr);
472 signal (SIGPIPE, lostpeer);
473 }
474
475 /*
476 * Slice a string up into argc/argv.
477 */
478
479 static int slrflag;
480
481 void
482 makeargv (void)
483 {
484 char **argp;
485
486 margc = 0; /* No content as of yet. */
487
488 /* Make sure that `argbuf' is large enough
489 * to contain `line'. As soon as `line' is
490 * invalidated, so will `argbuf' be.
491 **/
492 free (argbuf); /* Get rid of previous content. */
493 argbuf = malloc (strlen (line) + 4);
494 if (!argbuf)
495 {
496 /* `margc' is naught, which hopefully will cover our back. */
497 printf ("Allocation failure. Serious error.\n");
498 return;
499 }
500
501 argp = margv;
502 stringbase = line; /* scan from beginning of buffer */
503 argbase = argbuf; /* store at beginning of buffer */
504 slrflag = 0;
505 while ((margc < MAXMARGV) && (*argp++ = slurpstring ()))
506 margc++;
507 }
508
509 /*
510 * Parse string into argbuf;
511 * implemented with FSM to
512 * handle quoting and strings
513 */
514 static char *
515 slurpstring (void)
516 {
517 int got_one = 0;
518 char *sb = stringbase;
519 char *ap = argbase;
520 char *tmp = argbase; /* will return this if token found */
521
522 if (*sb == '!' || *sb == '$')
523 { /* recognize ! as a token for shell */
524 switch (slrflag) /* and $ as token for macro invoke */
525 {
526 case 0:
527 slrflag++;
528 stringbase++;
529 return ((*sb == '!') ? "!" : "$");
530
531 case 1:
532 slrflag++;
533 altarg = stringbase;
534 break;
535
536 default:
537 break;
538 }
539 }
540
541 S0:
542 switch (*sb)
543 {
544 case '\0':
545 goto OUT;
546
547 case ' ':
548 case '\t':
549 sb++;
550 goto S0;
551
552 default:
553 switch (slrflag)
554 {
555 case 0:
556 slrflag++;
557 break;
558
559 case 1:
560 slrflag++;
561 altarg = sb;
562 break;
563
564 default:
565 break;
566 }
567 goto S1;
568 }
569
570 S1:
571 switch (*sb)
572 {
573 case ' ':
574 case '\t':
575 case '\0':
576 goto OUT; /* end of token */
577
578 case '\\':
579 sb++;
580 goto S2; /* slurp next character */
581
582 case '"':
583 sb++;
584 goto S3; /* slurp quoted string */
585
586 default:
587 *ap++ = *sb++; /* add character to token */
588 got_one = 1;
589 goto S1;
590 }
591
592 S2:
593 switch (*sb)
594 {
595 case '\0':
596 goto OUT;
597
598 default:
599 *ap++ = *sb++;
600 got_one = 1;
601 goto S1;
602 }
603
604 S3:
605 switch (*sb)
606 {
607 case '\0':
608 goto OUT;
609
610 case '"':
611 sb++;
612 goto S1;
613
614 default:
615 *ap++ = *sb++;
616 got_one = 1;
617 goto S3;
618 }
619
620 OUT:
621 if (got_one)
622 *ap++ = '\0';
623 argbase = ap; /* update storage pointer */
624 stringbase = sb; /* update scan pointer */
625 if (got_one)
626 return (tmp);
627 switch (slrflag)
628 {
629 case 0:
630 slrflag++;
631 break;
632
633 case 1:
634 slrflag++;
635 altarg = (char *) 0;
636 break;
637
638 default:
639 break;
640 }
641 return ((char *) 0);
642 }