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 Mon Jun 1 03:49:47 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, char**); 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 01:59:32 2020 @@ -69,7 +69,9 @@ struct tls; #include "cmds.h" static int file_get(const char *, const char *); +static int file_get_u(const char *, const char *, int, char **, volatile 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, char**, volatile int); static int save_chunked(FILE *, struct tls *, int , char *, size_t); static void aborthttp(int); static void abortfile(int); @@ -189,6 +191,12 @@ tooslow(int signo) static int file_get(const char *path, const char *outfile) { + return file_get_u(path, outfile, 1, NULL, 0); +} + +static int +file_get_u(const char *path, const char *outfile, int ready, char **unveil_list, volatile int argpos) +{ struct stat st; int fd, out, rval = -1, save_errno; volatile sig_t oldintr, oldinti; @@ -200,17 +208,25 @@ 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; + } else { + unveil_list[2 * argpos] = strdup(path); + if (pipeout) + return 0; } + - if (fstat(fd, &st) == -1) - filesize = -1; - else - filesize = st.st_size; - if (outfile != NULL) savefile = outfile; else { @@ -220,6 +236,11 @@ file_get(const char *path, const char *outfile) savefile = basename(path); } + if (!ready) { + unveil_list[(2 * argpos) | 1] = strdup(savefile); + return 0; + } + if (EMPTYSTRING(savefile)) { warnx("No filename after directory (use -o): %s", path); goto cleanup_copy; @@ -308,6 +329,13 @@ 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, NULL, 0); +} + +static int +url_get_u(const char *origline, const char *proxyenv, const char *outfile, + int lastfile, int ready, char** unveil_list, volatile int argpos) +{ 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,6 +443,13 @@ noslash: else savefile = basename(path); } + + if (!ready) { + unveil_list[(2 * argpos) | 1] = strdup(savefile); + free(newline); + free(credentials); + return (0); + } if (EMPTYSTRING(savefile)) { if (isftpurl) @@ -1222,6 +1257,12 @@ abortfile(int signo) int auto_fetch(int argc, char *argv[], char *outfile) { + return auto_fetch_u(argc, argv, outfile, 1, NULL); +} + +int +auto_fetch_u(int argc, char *argv[], char *outfile, int ready, char **unveil_list) +{ char *xargv[5]; char *cp, *url, *host, *dir, *file, *portnum; char *username, *pass, *pathstart; @@ -1268,10 +1309,16 @@ 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 (file_get_u(url + sizeof(FILE_URL) - 1, outfile, + ready, unveil_list, argpos) == -1) rval = argpos + 1; continue; + if (!ready) + continue; } + + if (!ready && pipeout) + continue; /* * Try HTTP URL-style arguments next. @@ -1280,9 +1327,12 @@ auto_fetch(int argc, char *argv[], char *outfile) strncasecmp(url, HTTPS_URL, sizeof(HTTPS_URL) -1) == 0) { redirect_loop = 0; retried = 0; - if (url_get(url, httpproxy, outfile, lastfile) == -1) + if (url_get_u(url, httpproxy, outfile, lastfile, + ready, unveil_list, argpos) == -1) rval = argpos + 1; continue; + if (!ready) + continue; } /* @@ -1295,10 +1345,13 @@ auto_fetch(int argc, char *argv[], char *outfile) char *passend, *passagain, *userend; if (ftpproxy) { - if (url_get(url, ftpproxy, outfile, lastfile) == -1) + if (url_get_u(url, ftpproxy, outfile, lastfile, + ready, unveil_list, argpos) == -1) rval = argpos + 1; continue; } + if (!ready) + continue; host += sizeof(FTP_URL) - 1; dir = strchr(host, '/'); @@ -1376,6 +1429,12 @@ bad_ftp_url: } else { /* classic style `host:file' */ dir = strchr(host, ':'); } + + if (!ready) { + 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 01:37:15 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, j; #ifndef SMALL int top; #endif @@ -197,7 +203,25 @@ main(volatile int argc, char *argv[]) char *outfile = NULL; const char *errstr; int dumb_terminal = 0; - + char **unveil_list = NULL; + char *shellp; + int temp_stdout; + int temp_stderr; +#ifndef SMALL + 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 +433,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; @@ -486,31 +510,224 @@ main(volatile int argc, char *argv[]) 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 } + + + unveil_list = calloc(2 * argc, sizeof(char*)); + if (unveil_list == NULL) + err(1, "calloc"); + + + /* 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, + 0, unveil_list); + + + + /* 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, "calloc"); + 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"); + } + free(ca_file); + free(ca_path); + ca_file = NULL; + ca_path = NULL; +#endif + + + /* remove duplicate r unveils */ + for (i = 2 * argc - 2; i >= 0; i -= 2) { + if (unveil_list[i] == NULL) + continue; + for (j = i - 2; j >= 0; j -= 2) { + if (unveil_list[j] == NULL) + continue; + if (!strcmp(unveil_list[i], unveil_list[j])) { + free(unveil_list[j]); + unveil_list[j] = NULL; + } + } + } + + /* remove duplicate cw unveils */ + for (i = 2 * argc - 1; i >= 0; i -= 2) { + if (unveil_list[i] == NULL) + continue; + for (j = i - 2; j >= 0; j -= 2) { + if (unveil_list[j] == NULL) + continue; + if (!strcmp(unveil_list[i], unveil_list[j])) { + free(unveil_list[j]); + unveil_list[j] = NULL; + } + } + } + + /* + * combine cw unveils which are also r unveils + * probably not needed. + */ + for (i = 2 * argc - 2; i >= 0; i -= 2) { + if (unveil_list[i] == NULL) + continue; + for (j = 2 * argc - 1; j >= 0; j -= 2) { + if (unveil_list[j] == NULL) + continue; + if (!strcmp(unveil_list[i], unveil_list[j])) { + if (unveil(unveil_list[i], "crw") == -1) + err(1, "unveil"); + + fprintf(ttyout, + "unveil(%s, \"crw\")\n", + unveil_list[i]); + + free(unveil_list[i]); + unveil_list[i] = NULL; + free(unveil_list[j]); + unveil_list[j] = NULL; + break; + } + } + } + + for (i = 2 * argc - 2; i >= 0; i -= 2) { + if (unveil_list[i]) { + if (unveil(unveil_list[i], "r") == -1) + err(1, "unveil"); + + fprintf(ttyout, "unveil(%s, \"r\")\n", + unveil_list[i]); + + + } + if (unveil_list[i | 1]) { + if (unveil(unveil_list[i | 1], "cw") == -1) + err(1, "unveil"); + + fprintf(ttyout, "unveil(%s, \"cw\")\n", + unveil_list[i | 1]); + + } + } + + if (unveil(NULL, NULL) == -1) + err(1, "unveil"); + + unveil_cleanup: + + for (i = 2 * argc - 1; i >= 0; i--) + free(unveil_list[i]); + free(unveil_list); + rval = auto_fetch(argc, argv, outfile); if (rval >= 0) /* -1 == connected and cd-ed */ exit(rval); } else { #ifndef SMALL + free(ca_file); + free(ca_path); + ca_file = NULL; + ca_path = NULL; char *xargv[5]; if (setjmp(toplevel)) @@ -537,6 +754,10 @@ main(volatile int argc, char *argv[]) } } #ifndef SMALL + free(ca_file); + free(ca_path); + ca_file = NULL; + ca_path = NULL; controlediting(); top = setjmp(toplevel) == 0; if (top) {