Common subdirectories: /usr/src/usr.bin/ftp/CVS and /home/luke/ftp/CVS diff -uNp /usr/src/usr.bin/ftp/extern.h /home/luke/ftp/extern.h --- /usr/src/usr.bin/ftp/extern.h Thu May 16 07:44:17 2019 +++ /home/luke/ftp/extern.h Wed Jun 3 13:41:49 2020 @@ -69,6 +69,7 @@ void abortrecv(int); void alarmtimer(int); int another(int *, char ***, const char *); int auto_fetch(int, char **, char *); +int auto_fetch_u(int, char **, char *, int, int); void blkfree(char **); void cdup(int, char **); void cmdabort(int); diff -uNp /usr/src/usr.bin/ftp/fetch.c /home/luke/ftp/fetch.c --- /usr/src/usr.bin/ftp/fetch.c Fri Feb 21 19:00:07 2020 +++ /home/luke/ftp/fetch.c Wed Jun 3 14:02:24 2020 @@ -68,8 +68,10 @@ struct tls; #include "ftp_var.h" #include "cmds.h" -static int file_get(const char *, const char *); +//~ static int file_get(const char *, const char *); +static int file_get_u(const char *, const char *, int, int); static int url_get(const char *, const char *, const char *, int); +static int url_get_u(const char *, const char *, const char *, int, int, int); static int save_chunked(FILE *, struct tls *, int , char *, size_t); static void aborthttp(int); static void abortfile(int); @@ -186,8 +188,14 @@ tooslow(int signo) * Copy a local file (used by the OpenBSD installer). * Returns -1 on failure, 0 on success */ +//~ static int +//~ file_get(const char *path, const char *outfile) +//~ { + //~ return file_get_u(path, outfile, 1, 0); +//~ } + static int -file_get(const char *path, const char *outfile) +file_get_u(const char *path, const char *outfile, int ready, int tout) { struct stat st; int fd, out, rval = -1, save_errno; @@ -200,17 +208,28 @@ file_get(const char *path, const char *outfile) direction = "received"; - fd = open(path, O_RDONLY); - if (fd == -1) { - warn("Can't open file %s", path); - return -1; - } + if (ready) { + + fd = open(path, O_RDONLY); + if (fd == -1) { + warn("Can't open file %s", path); + return -1; + } - if (fstat(fd, &st) == -1) - filesize = -1; - else - filesize = st.st_size; + if (fstat(fd, &st) == -1) + filesize = -1; + else + filesize = st.st_size; + } else { + if (unveil(path, "r") == -1) + err(1, "unveil"); + dprintf(tout, "unveil(%s, \"r\")\n", path); + if (pipeout) + return 0; + } + + if (outfile != NULL) savefile = outfile; else { @@ -220,6 +239,14 @@ file_get(const char *path, const char *outfile) savefile = basename(path); } + if (!ready) { + if (unveil(savefile, "cw") == -1) + err(1, "unveil"); + + dprintf(tout, "unveil(%s, \"cw\")\n", savefile); + return 0; + } + if (EMPTYSTRING(savefile)) { warnx("No filename after directory (use -o): %s", path); goto cleanup_copy; @@ -308,6 +335,12 @@ cleanup_copy: static int url_get(const char *origline, const char *proxyenv, const char *outfile, int lastfile) { + return url_get_u(origline, proxyenv, outfile, lastfile, 1, 0); +} + +static int +url_get_u(const char *origline, const char *proxyenv, const char *outfile, int lastfile, int ready, int tout) +{ char pbuf[NI_MAXSERV], hbuf[NI_MAXHOST], *cp, *portnum, *path, ststr[4]; char *hosttail, *cause = "unknown", *newline, *host, *port, *buf = NULL; char *epath, *redirurl, *loctail, *h, *p, gerror[200]; @@ -415,7 +448,15 @@ noslash: else savefile = basename(path); } + + if (!ready) { + if (unveil(savefile, "cw") == -1) + err(1, "unveil"); + dprintf(tout, "unveil(%s, \"cw\")\n", savefile); + return (0); + } + if (EMPTYSTRING(savefile)) { if (isftpurl) goto noftpautologin; @@ -1222,6 +1263,12 @@ abortfile(int signo) int auto_fetch(int argc, char *argv[], char *outfile) { + return auto_fetch_u(argc, argv, outfile, 1, 0); +} + +int +auto_fetch_u(int argc, char *argv[], char *outfile, int ready, int tout) +{ char *xargv[5]; char *cp, *url, *host, *dir, *file, *portnum; char *username, *pass, *pathstart; @@ -1268,21 +1315,32 @@ auto_fetch(int argc, char *argv[], char *outfile) errx(1, "Can't allocate memory for auto-fetch."); if (strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) { - if (file_get(url + sizeof(FILE_URL) - 1, outfile) == -1) + if (ready < 0) + continue; + if (file_get_u(url + sizeof(FILE_URL) - 1, outfile, ready, tout) == -1) rval = argpos + 1; continue; + if (!ready) + continue; } + + if (ready < 2 && pipeout) + continue; /* * Try HTTP URL-style arguments next. */ if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 || strncasecmp(url, HTTPS_URL, sizeof(HTTPS_URL) -1) == 0) { + if (ready < 0) + continue; redirect_loop = 0; retried = 0; - if (url_get(url, httpproxy, outfile, lastfile) == -1) + if (url_get_u(url, httpproxy, outfile, lastfile, ready, tout) == -1) rval = argpos + 1; continue; + if (!ready) + continue; } /* @@ -1295,9 +1353,14 @@ auto_fetch(int argc, char *argv[], char *outfile) char *passend, *passagain, *userend; if (ftpproxy) { - if (url_get(url, ftpproxy, outfile, lastfile) == -1) + if (ready < 0) + continue; + if (url_get_u(url, ftpproxy, outfile, lastfile, + ready, tout) == -1) rval = argpos + 1; continue; + if (!ready) + continue; } host += sizeof(FTP_URL) - 1; dir = strchr(host, '/'); @@ -1376,6 +1439,12 @@ bad_ftp_url: } else { /* classic style `host:file' */ dir = strchr(host, ':'); } + + if (ready < 2) { + rval = -2; + continue; + } + if (EMPTYSTRING(host)) { rval = argpos + 1; continue; diff -uNp /usr/src/usr.bin/ftp/main.c /home/luke/ftp/main.c --- /usr/src/usr.bin/ftp/main.c Tue Feb 11 12:41:39 2020 +++ /home/luke/ftp/main.c Wed Jun 3 14:14:13 2020 @@ -81,6 +81,10 @@ #include "cmds.h" #include "ftp_var.h" +#ifndef SMALL +#include "pathnames.h" +#endif + int connect_timeout; #ifndef NOSSL @@ -110,7 +114,7 @@ struct tls_config *tls_config; int tls_session_fd = -1; static void -process_ssl_options(char *cp) +process_ssl_options(char *cp, char *ca_file, char *ca_path) { const char *errstr; long long depth; @@ -124,6 +128,7 @@ process_ssl_options(char *cp) if (tls_config_set_ca_file(tls_config, str) != 0) errx(1, "tls ca file failed: %s", tls_config_error(tls_config)); + strlcpy(ca_file, str, PATH_MAX + 1); break; case SSL_CAPATH: if (str == NULL) @@ -131,6 +136,7 @@ process_ssl_options(char *cp) if (tls_config_set_ca_path(tls_config, str) != 0) errx(1, "tls ca path failed: %s", tls_config_error(tls_config)); + strlcpy(ca_path, str, PATH_MAX + 1); break; case SSL_CIPHERS: if (str == NULL) @@ -188,7 +194,7 @@ int pipeout; int main(volatile int argc, char *argv[]) { - int ch, rval; + int ch, rval, i; #ifndef SMALL int top; #endif @@ -197,7 +203,24 @@ main(volatile int argc, char *argv[]) char *outfile = NULL; const char *errstr; int dumb_terminal = 0; - + int temp_stdout; + int temp_stderr; +#ifndef SMALL + char *shellp; + char *ca_file; + char *ca_path; + char *ca_temp; + char *ca_temp2; + ca_file = malloc(PATH_MAX + 1); + if (ca_file == NULL) + err(1, "malloc"); + *ca_file = '\0'; + + ca_path = malloc(PATH_MAX + 1); + if (ca_path == NULL) + err(1, "malloc"); + *ca_path = '\0'; +#endif ftpport = "ftp"; httpport = "http"; #ifndef NOSSL @@ -409,7 +432,7 @@ main(volatile int argc, char *argv[]) case 'S': #ifndef NOSSL - process_ssl_options(optarg); + process_ssl_options(optarg, ca_file, ca_path); #endif /* !NOSSL */ break; @@ -482,30 +505,146 @@ main(volatile int argc, char *argv[]) setttywidth(0); (void)signal(SIGWINCH, setttywidth); + if ( !(argc > 0 && isurl(argv[0])) ) { + free(ca_file); + free(ca_path); + ca_file = NULL; + ca_path = NULL; + } + if (argc > 0) { if (isurl(argv[0])) { if (pipeout) { #ifndef SMALL - if (pledge("stdio rpath dns tty inet proc exec fattr", + if (pledge("stdio rpath dns tty inet proc exec fattr unveil", NULL) == -1) err(1, "pledge"); #else - if (pledge("stdio rpath dns tty inet fattr", + if (pledge("stdio rpath dns tty inet fattr unveil", NULL) == -1) err(1, "pledge"); #endif } else { #ifndef SMALL - if (pledge("stdio rpath wpath cpath dns tty inet proc exec fattr", + if (pledge("stdio rpath wpath cpath dns tty inet proc exec fattr unveil", NULL) == -1) err(1, "pledge"); #else - if (pledge("stdio rpath wpath cpath dns tty inet fattr", + if (pledge("stdio rpath wpath cpath dns tty inet fattr unveil", NULL) == -1) err(1, "pledge"); #endif } + + + /* suppress autofetch() error statements */ + + i = open("/dev/null", O_WRONLY); + if (i == -1) + err(1, "can't open /dev/null"); + + temp_stdout = dup(STDOUT_FILENO); + temp_stderr = dup(STDERR_FILENO); + + if(dup2(i, STDOUT_FILENO) < 0) + err(1, "dup2(i, STDOUT_FILENO)"); + + if(dup2(i, STDERR_FILENO) < 0) + err(1, "dup2(i, STDERR_FILENO)"); + + + + /* autofetch() dry run to populate unveil_list */ + rval = auto_fetch_u(argc, argv, outfile, -1, 0); + + if (rval != -2) + rval = auto_fetch_u(argc, argv, outfile, 0, temp_stderr); + + + + /* resume autofetch() error statements */ + if(dup2(temp_stderr, STDERR_FILENO) < 0) { + dprintf(temp_stderr, + "dup2(temp_stderr, STDERR_FILENO)\n"); + exit(1); + } + + if(dup2(temp_stdout, STDOUT_FILENO) < 0) + err(1, "dup2(temp_stdout, STDOUT_FILENO)"); + + close(i); + + + /* skip unveil */ + if (rval == -2) + goto unveil_cleanup; + +#ifndef SMALL + /* exerpt from cmds.c; needs pathnames.h */ + shellp = getenv("SHELL"); + if (shellp == NULL || *shellp == '\0') + shellp = _PATH_BSHELL; + + if (unveil(shellp, "x") == -1) + err(1, "unveil"); + + fprintf(ttyout, "unveil(%s, \"x\")\n", shellp); + + if (*ca_file || *ca_path) { + + ca_temp2 = malloc(PATH_MAX + 1); + if (ca_temp2 == NULL) + err(1, "malloc"); + i = 1; + do { + if (i) + ca_temp = ca_file; + else + ca_temp = ca_path; + if (*ca_temp == '\0') + continue; + if (unveil(ca_temp, "r") == -1) + err(1, "unveil"); + + fprintf(ttyout, + "unveil(%s, \"r\")\n", ca_temp); + + if (realpath(ca_temp, ca_temp2) == NULL) + err(1, "realpath"); + if (strcmp(ca_temp, ca_temp2)) { + + if (unveil(ca_temp2, "r") == -1) + err(1, "unveil"); + + fprintf(ttyout, + "unveil(%s, \"r\")\n", ca_temp2); + } + } while (i--); + + free(ca_temp2); + + } else { + if (unveil("/etc/ssl/cert.pem", "r") == -1) + err(1, "unveil"); + + fprintf(ttyout, + "unveil(/etc/ssl/cert.pem, \"r\")\n"); + } +#endif + + + if (unveil(NULL, NULL) == -1) + err(1, "unveil"); + + unveil_cleanup: + +#ifndef SMALL + free(ca_file); + free(ca_path); + ca_file = NULL; + ca_path = NULL; +#endif rval = auto_fetch(argc, argv, outfile); if (rval >= 0) /* -1 == connected and cd-ed */ exit(rval);