From: millert Date: Wed, 16 Apr 1997 05:02:39 +0000 (+0000) Subject: Sync with NetBSD (lukem): X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=deb2510a17e71c9f851f0bdc27210715896dfc13;p=openbsd Sync with NetBSD (lukem): * differentiate between being connected, and being logged in * cleanup some text messages * support username & password ftp URLs (ftp://user:pass@host/) in non-proxy situations; assume proxy supports it for proxy situations. * cd to / before performing any autofetch transfers --- diff --git a/usr.bin/ftp/cmds.c b/usr.bin/ftp/cmds.c index dc3bc4b86d9..23dfa9423eb 100644 --- a/usr.bin/ftp/cmds.c +++ b/usr.bin/ftp/cmds.c @@ -1,5 +1,5 @@ -/* $OpenBSD: cmds.c,v 1.18 1997/04/10 00:17:07 millert Exp $ */ -/* $NetBSD: cmds.c,v 1.22 1997/04/05 03:27:32 lukem Exp $ */ +/* $OpenBSD: cmds.c,v 1.19 1997/04/16 05:02:39 millert Exp $ */ +/* $NetBSD: cmds.c,v 1.23 1997/04/14 09:09:15 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94"; #else -static char rcsid[] = "$OpenBSD: cmds.c,v 1.18 1997/04/10 00:17:07 millert Exp $"; +static char rcsid[] = "$OpenBSD: cmds.c,v 1.19 1997/04/16 05:02:39 millert Exp $"; #endif #endif /* not lint */ @@ -630,7 +630,8 @@ status(argc, argv) int i; if (connected) - printf("Connected to %s.\n", hostname); + printf("Connected %sto %s.\n", + connected == -1 ? "and logged in" : "", hostname); else puts("Not connected."); if (!proxy) { @@ -1235,6 +1236,7 @@ user(argc, argv) if (!aflag && argc == 4) { (void)command("ACCT %s", argv[3]); } + connected = -1; } /* diff --git a/usr.bin/ftp/complete.c b/usr.bin/ftp/complete.c index 3f74d149473..84678420633 100644 --- a/usr.bin/ftp/complete.c +++ b/usr.bin/ftp/complete.c @@ -1,5 +1,5 @@ -/* $OpenBSD: complete.c,v 1.5 1997/03/21 20:59:28 millert Exp $ */ -/* $NetBSD: complete.c,v 1.6 1997/03/16 14:24:16 lukem Exp $ */ +/* $OpenBSD: complete.c,v 1.6 1997/04/16 05:02:43 millert Exp $ */ +/* $NetBSD: complete.c,v 1.7 1997/04/14 09:09:16 lukem Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -39,7 +39,7 @@ #ifndef SMALL #ifndef lint -static char rcsid[] = "$OpenBSD: complete.c,v 1.5 1997/03/21 20:59:28 millert Exp $"; +static char rcsid[] = "$OpenBSD: complete.c,v 1.6 1997/04/16 05:02:43 millert Exp $"; #endif /* not lint */ /* @@ -365,8 +365,8 @@ complete(el, ch) return (complete_local(word, dolist)); case 'r': /* remote complete */ case 'R': - if (!connected) { - puts("\nMust be connected to complete."); + if (connected != -1) { + puts("\nMust be logged in to complete."); return (CC_REDISPLAY); } return (complete_remote(word, dolist)); diff --git a/usr.bin/ftp/extern.h b/usr.bin/ftp/extern.h index 0cdc98ba3a0..5d9b72fec4e 100644 --- a/usr.bin/ftp/extern.h +++ b/usr.bin/ftp/extern.h @@ -1,5 +1,5 @@ -/* $OpenBSD: extern.h,v 1.11 1997/04/10 00:17:08 millert Exp $ */ -/* $NetBSD: extern.h,v 1.14 1997/04/05 03:27:34 lukem Exp $ */ +/* $OpenBSD: extern.h,v 1.12 1997/04/16 05:02:46 millert Exp $ */ +/* $NetBSD: extern.h,v 1.15 1997/04/14 09:09:17 lukem Exp $ */ /*- * Copyright (c) 1994 The Regents of the University of California. @@ -82,7 +82,7 @@ int initconn __P((void)); void intr __P((void)); void list_vertical __P((StringList *)); void lcd __P((int, char **)); -int login __P((const char *)); +int login __P((const char *, char *, char *)); void lostpeer __P((void)); void lpwd __P((int, char **)); void ls __P((int, char **)); diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c index 35dd41b09d1..9be8b18392b 100644 --- a/usr.bin/ftp/fetch.c +++ b/usr.bin/ftp/fetch.c @@ -1,5 +1,5 @@ -/* $OpenBSD: fetch.c,v 1.7 1997/04/14 00:48:44 millert Exp $ */ -/* $NetBSD: fetch.c,v 1.5 1997/04/05 03:27:36 lukem Exp $ */ +/* $OpenBSD: fetch.c,v 1.8 1997/04/16 05:02:48 millert Exp $ */ +/* $NetBSD: fetch.c,v 1.6 1997/04/14 09:09:19 lukem Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -38,7 +38,7 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: fetch.c,v 1.7 1997/04/14 00:48:44 millert Exp $"; +static char rcsid[] = "$OpenBSD: fetch.c,v 1.8 1997/04/16 05:02:48 millert Exp $"; #endif /* not lint */ /* @@ -128,7 +128,7 @@ url_get(line, proxyenv) else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0) host = proxy + sizeof(FTP_URL) - 1; else { - warnx("Malformed proxy url: %s", proxy); + warnx("Malformed proxy URL: %s", proxy); goto cleanup_url_get; } if (EMPTYSTRING(host)) @@ -153,7 +153,7 @@ url_get(line, proxyenv) if (isdigit(host[0])) { if (inet_aton(host, &sin.sin_addr) == 0) { - warnx("invalid IP address: %s", host); + warnx("Invalid IP address: %s", host); goto cleanup_url_get; } } else { @@ -174,7 +174,7 @@ url_get(line, proxyenv) if (! EMPTYSTRING(portnum)) { port = atoi(portnum); if (port < 1 || (port & 0xffff) != port) { - warnx("invalid port: %s", portnum); + warnx("Invalid port: %s", portnum); goto cleanup_url_get; } port = htons(port); @@ -327,7 +327,7 @@ url_get(line, proxyenv) return (0); improper: - warnx("improper response from %s", host); + warnx("Improper response from %s", host); cleanup_url_get: if (s != -1) close(s); @@ -372,6 +372,7 @@ auto_fetch(argc, argv) static char lasthost[MAXHOSTNAMELEN]; char *xargv[5]; char *cp, *line, *host, *dir, *file, *portnum; + char *user, *pass; char *ftpproxy, *httpproxy; int rval, xargc, argpos; int dirhasglob, filehasglob; @@ -396,7 +397,7 @@ auto_fetch(argc, argv) for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) { if (strchr(argv[argpos], ':') == NULL) break; - host = dir = file = portnum = NULL; + host = dir = file = portnum = user = pass = NULL; /* * We muck with the string, so we make a copy. @@ -427,14 +428,39 @@ auto_fetch(argc, argv) continue; } host += sizeof(FTP_URL) - 1; - cp = strchr(host, '/'); - - /* Look for a port number after the host name. */ + dir = strchr(host, '/'); + + /* Look for [user:pass@]host[:port] */ + user = host; + pass = strpbrk(user, ":@/"); + if (pass == NULL || *pass == '/') + goto parsed_url; + if (*pass == '@') { + warnx("Bad ftp URL: %s", argv[argpos]); + rval = argpos + 1; + continue; + } + *pass++ = '\0'; + cp = strpbrk(pass, ":@/"); + if (cp == NULL || *cp == '/') { + portnum = pass; + user = pass = NULL; + goto parsed_url; + } + if (*cp == ':') { + warnx("Bad ftp URL: %s", argv[argpos]); + rval = argpos + 1; + continue; + } + *cp++ = '\0'; + host = cp; portnum = strchr(host, ':'); if (portnum != NULL) *portnum++ = '\0'; - } else /* classic style `host:file' */ - cp = strchr(host, ':'); +parsed_url: + } else { /* classic style `host:file' */ + dir = strchr(host, ':'); + } if (EMPTYSTRING(host)) { rval = argpos + 1; continue; @@ -444,15 +470,14 @@ auto_fetch(argc, argv) * If cp is NULL, the file wasn't specified * (URL looked something like ftp://host) */ - if (cp != NULL) - *cp++ = '\0'; + if (dir != NULL) + *dir++ = '\0'; /* * Extract the file and (if present) directory name. */ - dir = cp; if (! EMPTYSTRING(dir)) { - cp = strrchr(cp, '/'); + cp = strrchr(dir, '/'); if (cp != NULL) { *cp++ = '\0'; file = cp; @@ -462,13 +487,15 @@ auto_fetch(argc, argv) } } if (debug) - printf("host '%s', dir '%s', file '%s'\n", - host, dir, file); + printf("user %s:%s host %s port %s dir %s file %s\n", + user, pass, host, portnum, dir, file); /* * Set up the connection if we don't have one. */ if (strcmp(host, lasthost) != 0) { + int oautologin; + (void)strcpy(lasthost, host); if (connected) disconnect(0, NULL); @@ -476,14 +503,20 @@ auto_fetch(argc, argv) xargv[1] = host; xargv[2] = NULL; xargc = 2; - if (portnum != NULL) { + if (! EMPTYSTRING(portnum)) { xargv[2] = portnum; xargv[3] = NULL; xargc = 3; } + oautologin = autologin; + if (user != NULL) + autologin = 0; setpeer(xargc, xargv); - if (connected == 0) { - warnx("Can't connect to host `%s'", host); + autologin = oautologin; + if ((connected == 0) || + ((connected == 1) && !login(host, user, pass)) ) { + warnx("Can't connect or login to host `%s'", + host); rval = argpos + 1; continue; } @@ -491,16 +524,14 @@ auto_fetch(argc, argv) /* Always use binary transfers. */ setbinary(0, NULL); } - else /* already have connection, cd back to '/' */ - { - xargv[0] = "cd"; - xargv[1] = "/"; - xargv[2] = NULL; - cd(2, xargv); - if (! dirchange) { - rval = argpos + 1; - continue; - } + /* cd back to '/' */ + xargv[0] = "cd"; + xargv[1] = "/"; + xargv[2] = NULL; + cd(2, xargv); + if (! dirchange) { + rval = argpos + 1; + continue; } dirhasglob = filehasglob = 0; diff --git a/usr.bin/ftp/ftp.1 b/usr.bin/ftp/ftp.1 index 66ff86344a9..b64dffd452d 100644 --- a/usr.bin/ftp/ftp.1 +++ b/usr.bin/ftp/ftp.1 @@ -1,5 +1,5 @@ -.\" $OpenBSD: ftp.1,v 1.10 1997/04/10 00:17:09 millert Exp $ -.\" $NetBSD: ftp.1,v 1.19 1997/04/05 03:27:37 lukem Exp $ +.\" $OpenBSD: ftp.1,v 1.11 1997/04/16 05:02:50 millert Exp $ +.\" $NetBSD: ftp.1,v 1.20 1997/04/14 09:09:20 lukem Exp $ .\" .\" Copyright (c) 1985, 1989, 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -58,7 +58,7 @@ file transfer program .Op Fl V .Op Ar host Op Ar port .Nm ftp -ftp://\fIhost\fR[:\fIport\fR]/\fIfile\fR[/] +ftp://[\fIuser\fR:\fIpassword\fR@]\fIhost\fR[:\fIport\fR]/\fIfile\fR[/] .Nm ftp http://\fIhost\fR[:\fIport\fR]/\fIfile\fR .Nm ftp @@ -1012,18 +1012,26 @@ To enable auto-fetch, simply pass the list of hostnames/files on the command line. .Pp The following formats are valid syntax for an auto-fetch element: -.Bl -tag -width "http://host[:port]/file" +.Bl -tag -width "ftp://[user:password@]host[:port]/file" .It host:/file .Dq Classic ftp format -.It ftp://host[:port]/file -FTP URL, using the ftp protocol if +.It ftp://[user:password@]host[:port]/file +An ftp URL, retrieved using the ftp protocol if .Ev ftp_proxy isn't defined. Otherwise, transfer using http via the proxy defined in .Ev ftp_proxy . +If +.Ar user:password@ +is given and +.Ev ftp_proxy +isn't defined, login as +.Ar user +with a password of +.Ar password . .It http://host[:port]/file -HTTP URL, using the http protocol. +An http URL, retrieved using the http protocol. If .Ev http_proxy is defined, it is used as a URL to an HTTP proxy server. diff --git a/usr.bin/ftp/ftp.c b/usr.bin/ftp/ftp.c index bfd62328979..044b3eab9b5 100644 --- a/usr.bin/ftp/ftp.c +++ b/usr.bin/ftp/ftp.c @@ -1,5 +1,5 @@ -/* $OpenBSD: ftp.c,v 1.16 1997/03/21 20:59:29 millert Exp $ */ -/* $NetBSD: ftp.c,v 1.24 1997/03/16 14:24:19 lukem Exp $ */ +/* $OpenBSD: ftp.c,v 1.17 1997/04/16 05:02:54 millert Exp $ */ +/* $NetBSD: ftp.c,v 1.25 1997/04/14 09:09:22 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94"; #else -static char rcsid[] = "$OpenBSD: ftp.c,v 1.16 1997/03/21 20:59:29 millert Exp $"; +static char rcsid[] = "$OpenBSD: ftp.c,v 1.17 1997/04/16 05:02:54 millert Exp $"; #endif #endif /* not lint */ @@ -57,7 +57,6 @@ static char rcsid[] = "$OpenBSD: ftp.c,v 1.16 1997/03/21 20:59:29 millert Exp $" #include #include #include -#include #include #include #include @@ -193,102 +192,6 @@ bad: return ((char *)0); } -int -login(host) - const char *host; -{ - char tmp[80]; - char *user, *pass, *acct; - char anonpass[MAXLOGNAME + 1 + MAXHOSTNAMELEN]; /* "user@hostname" */ - char hostname[MAXHOSTNAMELEN]; - int n, aflag = 0; - - user = pass = acct = NULL; - if (ruserpass(host, &user, &pass, &acct) < 0) { - code = -1; - return (0); - } - - /* - * Set up arguments for an anonymous FTP session, if necessary. - */ - if ((user == NULL || pass == NULL) && anonftp) { - memset(anonpass, 0, sizeof(anonpass)); - memset(hostname, 0, sizeof(hostname)); - - /* - * Set up anonymous login password. - */ - user = getlogin(); - gethostname(hostname, MAXHOSTNAMELEN); -#ifndef DONT_CHEAT_ANONPASS - /* - * Every anonymous FTP server I've encountered - * will accept the string "username@", and will - * append the hostname itself. We do this by default - * since many servers are picky about not having - * a FQDN in the anonymous password. - thorpej@netbsd.org - */ - snprintf(anonpass, sizeof(anonpass) - 1, "%s@", - user); -#else - snprintf(anonpass, sizeof(anonpass) - 1, "%s@%s", - user, hp->h_name); -#endif - pass = anonpass; - user = "anonymous"; - } - - while (user == NULL) { - char *myname = getlogin(); - - if (myname == NULL) { - struct passwd *pp = getpwuid(getuid()); - - if (pp != NULL) - myname = pp->pw_name; - } - if (myname) - printf("Name (%s:%s): ", host, myname); - else - printf("Name (%s): ", host); - (void)fgets(tmp, sizeof(tmp) - 1, stdin); - tmp[strlen(tmp) - 1] = '\0'; - if (*tmp == '\0') - user = myname; - else - user = tmp; - } - n = command("USER %s", user); - if (n == CONTINUE) { - if (pass == NULL) - pass = getpass("Password:"); - n = command("PASS %s", pass); - } - if (n == CONTINUE) { - aflag++; - acct = getpass("Account:"); - n = command("ACCT %s", acct); - } - if (n != COMPLETE) { - warnx("Login failed."); - return (0); - } - if (!aflag && acct != NULL) - (void)command("ACCT %s", acct); - if (proxy) - return (1); - for (n = 0; n < macnum; ++n) { - if (!strcmp("init", macros[n].mac_name)) { - (void)strcpy(line, "$init"); - makeargv(); - domacro(margc, margv); - break; - } - } - return (1); -} - void cmdabort(notused) int notused; diff --git a/usr.bin/ftp/ftp_var.h b/usr.bin/ftp/ftp_var.h index 6148f8cd963..8e875ff1947 100644 --- a/usr.bin/ftp/ftp_var.h +++ b/usr.bin/ftp/ftp_var.h @@ -1,5 +1,5 @@ -/* $OpenBSD: ftp_var.h,v 1.10 1997/03/21 20:59:30 millert Exp $ */ -/* $NetBSD: ftp_var.h,v 1.15 1997/03/14 01:39:38 christos Exp $ */ +/* $OpenBSD: ftp_var.h,v 1.11 1997/04/16 05:02:57 millert Exp $ */ +/* $NetBSD: ftp_var.h,v 1.16 1997/04/14 09:09:23 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -67,7 +67,7 @@ int hash; /* print # for each buffer transferred */ int mark; /* number of bytes between hashes */ int sendport; /* use PORT cmd for each data connection */ int verbose; /* print messages coming back from server */ -int connected; /* connected to server */ +int connected; /* 1 = connected to server, -1 = logged in */ int fromatty; /* input is from a terminal */ int interactive; /* interactively prompt on m* cmds */ int confirmrest; /* confirm rest of current m* cmd */ diff --git a/usr.bin/ftp/util.c b/usr.bin/ftp/util.c index e063a0cc061..a591aefcd82 100644 --- a/usr.bin/ftp/util.c +++ b/usr.bin/ftp/util.c @@ -1,5 +1,5 @@ -/* $OpenBSD: util.c,v 1.5 1997/04/10 00:17:11 millert Exp $ */ -/* $NetBSD: util.c,v 1.6 1997/04/05 03:27:39 lukem Exp $ */ +/* $OpenBSD: util.c,v 1.6 1997/04/16 05:02:59 millert Exp $ */ +/* $NetBSD: util.c,v 1.7 1997/04/14 09:09:24 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -35,7 +35,7 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: util.c,v 1.5 1997/04/10 00:17:11 millert Exp $"; +static char rcsid[] = "$OpenBSD: util.c,v 1.6 1997/04/16 05:02:59 millert Exp $"; #endif /* not lint */ /* @@ -50,6 +50,7 @@ static char rcsid[] = "$OpenBSD: util.c,v 1.5 1997/04/10 00:17:11 millert Exp $" #include #include #include +#include #include #include #include @@ -111,7 +112,7 @@ setpeer(argc, argv) (void)strcpy(structname, "file"), stru = STRU_F; (void)strcpy(bytename, "8"), bytesize = 8; if (autologin) - (void)login(argv[1]); + (void)login(argv[1], NULL, NULL); #if (defined(unix) || defined(BSD)) && NBBY == 8 /* @@ -169,6 +170,109 @@ setpeer(argc, argv) } } +/* + * login to remote host, using given username & password if supplied + */ +int +login(host, user, pass) + const char *host; + char *user, *pass; +{ + char tmp[80]; + char *acct; + char anonpass[MAXLOGNAME + 1 + MAXHOSTNAMELEN]; /* "user@hostname" */ + char hostname[MAXHOSTNAMELEN]; + int n, aflag = 0; + + acct = NULL; + if (user == NULL) { + if (ruserpass(host, &user, &pass, &acct) < 0) { + code = -1; + return (0); + } + } + + /* + * Set up arguments for an anonymous FTP session, if necessary. + */ + if ((user == NULL || pass == NULL) && anonftp) { + memset(anonpass, 0, sizeof(anonpass)); + memset(hostname, 0, sizeof(hostname)); + + /* + * Set up anonymous login password. + */ + user = getlogin(); + gethostname(hostname, MAXHOSTNAMELEN); +#ifndef DONT_CHEAT_ANONPASS + /* + * Every anonymous FTP server I've encountered + * will accept the string "username@", and will + * append the hostname itself. We do this by default + * since many servers are picky about not having + * a FQDN in the anonymous password. - thorpej@netbsd.org + */ + snprintf(anonpass, sizeof(anonpass) - 1, "%s@", + user); +#else + snprintf(anonpass, sizeof(anonpass) - 1, "%s@%s", + user, hp->h_name); +#endif + pass = anonpass; + user = "anonymous"; + } + + while (user == NULL) { + char *myname = getlogin(); + + if (myname == NULL) { + struct passwd *pp = getpwuid(getuid()); + + if (pp != NULL) + myname = pp->pw_name; + } + if (myname) + printf("Name (%s:%s): ", host, myname); + else + printf("Name (%s): ", host); + (void)fgets(tmp, sizeof(tmp) - 1, stdin); + tmp[strlen(tmp) - 1] = '\0'; + if (*tmp == '\0') + user = myname; + else + user = tmp; + } + n = command("USER %s", user); + if (n == CONTINUE) { + if (pass == NULL) + pass = getpass("Password:"); + n = command("PASS %s", pass); + } + if (n == CONTINUE) { + aflag++; + if (acct == NULL) + acct = getpass("Account:"); + n = command("ACCT %s", acct); + } + if ((n != COMPLETE) || + (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) { + warnx("Login failed."); + return (0); + } + if (proxy) + return (1); + connected = -1; + for (n = 0; n < macnum; ++n) { + if (!strcmp("init", macros[n].mac_name)) { + (void)strcpy(line, "$init"); + makeargv(); + domacro(margc, margv); + break; + } + } + return (1); +} + /* * `another' gets another argument, and stores the new argc and argv. * It reverts to the top level (via main.c's intr()) on EOF/error.