=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ftp/fetch.c,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- src/usr.bin/ftp/fetch.c 1997/04/14 00:48:44 1.7 +++ src/usr.bin/ftp/fetch.c 1997/04/16 05:02:48 1.8 @@ -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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ continue; } host += sizeof(FTP_URL) - 1; - cp = strchr(host, '/'); + dir = strchr(host, '/'); - /* Look for a port number after the host name. */ + /* 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 @@ * 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 @@ } } 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 @@ 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 @@ /* 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;