1 /* $FreeBSD: src/usr.bin/ftp/main.c,v 1.25.2.4 2002/08/27 09:55:08 yar Exp $ */
2 /* $NetBSD: main.c,v 1.26 1997/10/14 16:31:22 christos Exp $ */
5 * Copyright (c) 1985, 1989, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
39 __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\
40 \tThe Regents of the University of California. All rights reserved.");
45 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
47 __RCSID("$FreeBSD: src/usr.bin/ftp/main.c,v 1.25.2.4 2002/08/27 09:55:08 yar Exp $");
48 __RCSID_SOURCE("$NetBSD: main.c,v 1.26 1997/10/14 16:31:22 christos Exp $");
53 * FTP User Program -- Command Interface.
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
71 #include "pathnames.h"
73 int family = AF_UNSPEC;
75 int main __P((int, char **));
83 struct passwd *pw = NULL;
84 char *cp, homedir[MAXPATHLEN], *s;
86 char *src_addr = NULL;
88 (void) setlocale(LC_ALL, "");
93 cp = getenv("FTPSERVERPORT");
95 asprintf(&gateport, "%s", cp);
97 asprintf(&gateport, "ftpgate");
103 try_epsv = epsv4; /* so status w/o connection isn't bogus */
104 restricted_data_ports = 1;
116 if ((tmpdir = getenv("TMPDIR")) == NULL)
119 cp = strrchr(argv[0], '/');
120 cp = (cp == NULL) ? argv[0] : cp + 1;
121 if ((s = getenv("FTP_PASSIVE_MODE")) != NULL
122 && strcasecmp(s, "no") != 0)
124 if (strcmp(cp, "pftp") == 0)
126 else if (strcmp(cp, "gate-ftp") == 0)
129 gateserver = getenv("FTPSERVER");
130 if (gateserver == NULL || *gateserver == '\0')
131 gateserver = GATE_SERVER;
133 if (*gateserver == '\0') {
135 "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
141 if (cp == NULL || strcmp(cp, "dumb") == 0)
145 fromatty = isatty(fileno(stdin));
147 verbose = 1; /* verbose if from a tty */
150 editing = 1; /* editing mode on if tty is usable */
153 if (isatty(fileno(stdout)) && !dumbterm)
154 progress = 1; /* progress bar on if tty is usable */
156 while ((ch = getopt(argc, argv, "46adeginpP:s:tUvV")) != -1) {
211 restricted_data_ports = 0;
229 cpend = 0; /* no pending replies */
230 proxy = 0; /* proxy not active */
231 crflag = 1; /* strip c.r. on ascii gets */
232 sendport = -1; /* not using ports */
235 struct addrinfo hints;
236 struct addrinfo *res;
239 memset(&hints, 0, sizeof(hints));
240 hints.ai_family = family;
241 hints.ai_socktype = SOCK_STREAM;
242 error = getaddrinfo(src_addr, NULL, &hints, &res);
244 warnx("%s: %s", src_addr, gai_strerror(error));
245 if (error == EAI_SYSTEM)
246 warnx("%s", strerror(errno));
253 * Set up the home directory in case we're globbing.
260 pw = getpwuid(getuid());
263 (void)strcpy(home, pw->pw_dir);
267 (void)signal(SIGWINCH, setttywidth);
269 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
275 if (strchr(argv[0], ':') != NULL && ! isipv6addr(argv[0])) {
276 anonftp = 1; /* Handle "automatic" transfers. */
277 rval = auto_fetch(argc, argv);
278 if (rval >= 0) /* -1 == connected and cd-ed */
281 char *xargv[4], **xargp = xargv;
283 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
286 if (setjmp(toplevel))
288 (void)signal(SIGINT, (sig_t)intr);
289 (void)signal(SIGPIPE, (sig_t)lostpeer);
290 *xargp++ = __progname;
291 *xargp++ = argv[0]; /* host */
293 *xargp++ = argv[1]; /* port */
295 setpeer(xargp-xargv, xargv);
301 top = setjmp(toplevel) == 0;
303 (void)signal(SIGINT, (sig_t)intr);
304 (void)signal(SIGPIPE, (sig_t)lostpeer);
317 longjmp(toplevel, 1);
327 (void)shutdown(fileno(cout), 1+1);
332 (void)shutdown(data, 1+1);
341 (void)shutdown(fileno(cout), 1+1);
381 fputs(prompt(), stdout);
382 (void)fflush(stdout);
384 if (fgets(line, sizeof(line), stdin) == NULL)
389 if (line[--num] == '\n') {
393 } else if (num == sizeof(line) - 2) {
394 puts("sorry, input line too long.");
395 while ((num = getchar()) != '\n' && num != EOF)
398 } /* else it was a line without a newline */
404 if ((buf = el_gets(el, &num)) == NULL || num == 0)
406 if (buf[--num] == '\n') {
409 } else if (num >= sizeof(line)) {
410 puts("sorry, input line too long.");
413 memcpy(line, buf, num);
415 history(hist, H_ENTER, buf);
422 #if 0 && !defined(SMALL) /* XXX: don't want el_parse */
424 * el_parse returns -1 to signal that it's not been handled
427 if (el_parse(el, margc, margv) != -1)
430 c = getcmd(margv[0]);
431 if (c == (struct cmd *)-1) {
432 puts("?Ambiguous command.");
436 puts("?Invalid command.");
439 if (c->c_conn && !connected) {
440 puts("Not connected.");
444 (*c->c_handler)(margc, margv);
445 if (bell && c->c_bell)
446 (void)putchar('\007');
447 if (c->c_handler != help)
450 (void)signal(SIGINT, (sig_t)intr);
451 (void)signal(SIGPIPE, (sig_t)lostpeer);
459 struct cmd *c, *found;
460 int nmatches, longest;
468 for (c = cmdtab; (p = c->c_name) != NULL; c++) {
469 for (q = name; *q == *p++; q++)
470 if (*q == 0) /* exact match? */
472 if (!*q) { /* the name was a prefix */
473 if (q - name > longest) {
477 } else if (q - name == longest)
482 return ((struct cmd *)-1);
487 * Slice a string up into argc/argv.
497 stringbase = line; /* scan from first of buffer */
498 argbase = argbuf; /* store from first of buffer */
500 marg_sl->sl_cur = 0; /* reset to start of marg_sl */
501 for (margc = 0; ; margc++) {
502 argp = slurpstring();
503 sl_add(marg_sl, argp);
508 if (cursor_pos == line) {
511 } else if (cursor_pos != NULL) {
513 cursor_argo = strlen(margv[margc-1]);
519 #define INC_CHKCURSOR(x) (x)++
521 #define INC_CHKCURSOR(x) { (x)++ ; \
522 if (x == cursor_pos) { \
523 cursor_argc = margc; \
524 cursor_argo = ap-argbase; \
531 * Parse string into argbuf;
532 * implemented with FSM to
533 * handle quoting and strings
539 char *sb = stringbase;
541 char *tmp = argbase; /* will return this if token found */
543 if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
544 switch (slrflag) { /* and $ as token for macro invoke */
547 INC_CHKCURSOR(stringbase);
548 return ((*sb == '!') ? "!" : "$");
591 goto OUT; /* end of token */
595 goto S2; /* slurp next character */
599 goto S3; /* slurp quoted string */
602 *ap = *sb; /* add character to token */
644 argbase = ap; /* update storage pointer */
645 stringbase = sb; /* update scan pointer */
665 * Call each command handler with argc == 0 and argv[0] == name.
678 printf("%sommands may be abbreviated. Commands are:\n\n",
679 proxy ? "Proxy c" : "C");
680 for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
681 if (c->c_name && (!proxy || c->c_proxy))
682 sl_add(buf, c->c_name);
688 #define HELPINDENT ((int) sizeof("disconnect"))
695 if (c == (struct cmd *)-1)
696 printf("?Ambiguous help command %s\n", arg);
697 else if (c == (struct cmd *)0)
698 printf("?Invalid help command %s\n", arg);
700 printf("%-*s\t%s\n", HELPINDENT,
701 c->c_name, c->c_help);
708 (void)fprintf(stderr,
709 "usage: %s [-46adeginptUvV] [-P port] [-s src_addr] [host [port]]\n"
711 " %s ftp://host[:port]/path[/]\n"
712 " %s http://host[:port]/file\n",
713 __progname, __progname, __progname, __progname);