Common subdirectories: /usr/src/usr.bin/ftp/CVS and /home/luke/ftp/CVS diff -c /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,74 **** --- 69,75 ---- 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 -c /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 Tue Jun 2 11:37:35 2020 *************** *** 69,75 **** --- 69,77 ---- #include "cmds.h" static int file_get(const char *, const char *); + static int file_get_u(const char *, const char *, int, char **, 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**, int); static int save_chunked(FILE *, struct tls *, int , char *, size_t); static void aborthttp(int); static void abortfile(int); *************** *** 189,194 **** --- 191,202 ---- 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, int argpos) + { struct stat st; int fd, out, rval = -1, save_errno; volatile sig_t oldintr, oldinti; *************** *** 200,215 **** direction = "received"; ! 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 (outfile != NULL) savefile = outfile; --- 208,228 ---- direction = "received"; ! 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 (outfile != NULL) savefile = outfile; *************** *** 220,225 **** --- 233,244 ---- savefile = basename(path); } + if (!ready) { + if (!pipeout) + 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,313 **** --- 327,338 ---- 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,420 **** --- 440,451 ---- else savefile = basename(path); } + + if (!ready) { + unveil_list[(2 * argpos) | 1] = strdup(savefile); + rval = 0; + goto cleanup_url_get; + } if (EMPTYSTRING(savefile)) { if (isftpurl) *************** *** 1222,1227 **** --- 1253,1264 ---- 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,1277 **** 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) rval = argpos + 1; continue; } /* * Try HTTP URL-style arguments next. --- 1305,1320 ---- errx(1, "Can't allocate memory for auto-fetch."); if (strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) { ! 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,1288 **** strncasecmp(url, HTTPS_URL, sizeof(HTTPS_URL) -1) == 0) { redirect_loop = 0; retried = 0; ! if (url_get(url, httpproxy, outfile, lastfile) == -1) rval = argpos + 1; continue; } /* --- 1323,1334 ---- strncasecmp(url, HTTPS_URL, sizeof(HTTPS_URL) -1) == 0) { redirect_loop = 0; retried = 0; ! if (url_get_u(url, httpproxy, outfile, lastfile, ! ready, unveil_list, argpos) == -1) rval = argpos + 1; continue; + if (!ready) + continue; } /* *************** *** 1295,1304 **** char *passend, *passagain, *userend; if (ftpproxy) { ! if (url_get(url, ftpproxy, outfile, lastfile) == -1) rval = argpos + 1; continue; } host += sizeof(FTP_URL) - 1; dir = strchr(host, '/'); --- 1341,1353 ---- char *passend, *passagain, *userend; if (ftpproxy) { ! 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,1381 **** --- 1425,1436 ---- } else { /* classic style `host:file' */ dir = strchr(host, ':'); } + + if (!ready) { + rval = -2; + continue; + } + if (EMPTYSTRING(host)) { rval = argpos + 1; continue; diff -c /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 Tue Jun 2 12:16:17 2020 *************** *** 81,86 **** --- 81,90 ---- #include "cmds.h" #include "ftp_var.h" + #ifndef SMALL + #include "pathnames.h" + #endif + int connect_timeout; #ifndef NOSSL *************** *** 110,116 **** int tls_session_fd = -1; static void ! process_ssl_options(char *cp) { const char *errstr; long long depth; --- 114,120 ---- int tls_session_fd = -1; static void ! process_ssl_options(char *cp, char *ca_temp) { const char *errstr; long long depth; *************** *** 124,129 **** --- 128,134 ---- if (tls_config_set_ca_file(tls_config, str) != 0) errx(1, "tls ca file failed: %s", tls_config_error(tls_config)); + strlcpy(ca_temp, str, PATH_MAX + 1); break; case SSL_CAPATH: if (str == NULL) *************** *** 131,136 **** --- 136,142 ---- if (tls_config_set_ca_path(tls_config, str) != 0) errx(1, "tls ca path failed: %s", tls_config_error(tls_config)); + strlcpy(ca_temp, str, PATH_MAX + 1); break; case SSL_CIPHERS: if (str == NULL) *************** *** 188,194 **** int main(volatile int argc, char *argv[]) { ! int ch, rval; #ifndef SMALL int top; #endif --- 194,200 ---- int main(volatile int argc, char *argv[]) { ! int ch, rval, i, j; #ifndef SMALL int top; #endif *************** *** 197,203 **** char *outfile = NULL; const char *errstr; int dumb_terminal = 0; ! ftpport = "ftp"; httpport = "http"; #ifndef NOSSL --- 203,220 ---- 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_temp = NULL; ! char *ca_temp2 = NULL; ! char *ca_temp3; ! ca_temp = calloc(PATH_MAX + 1, sizeof(char)); ! if (ca_temp == NULL) ! err(1, "calloc"); ! #endif ftpport = "ftp"; httpport = "http"; #ifndef NOSSL *************** *** 409,415 **** case 'S': #ifndef NOSSL ! process_ssl_options(optarg); #endif /* !NOSSL */ break; --- 426,432 ---- case 'S': #ifndef NOSSL ! process_ssl_options(optarg, ca_temp); #endif /* !NOSSL */ break; *************** *** 486,511 **** if (isurl(argv[0])) { if (pipeout) { #ifndef SMALL ! if (pledge("stdio rpath dns tty inet proc exec fattr", NULL) == -1) err(1, "pledge"); #else ! if (pledge("stdio rpath dns tty inet fattr", NULL) == -1) err(1, "pledge"); #endif } else { #ifndef SMALL ! if (pledge("stdio rpath wpath cpath dns tty inet proc exec fattr", NULL) == -1) err(1, "pledge"); #else ! if (pledge("stdio rpath wpath cpath dns tty inet fattr", NULL) == -1) err(1, "pledge"); #endif } rval = auto_fetch(argc, argv, outfile); if (rval >= 0) /* -1 == connected and cd-ed */ exit(rval); --- 503,707 ---- if (isurl(argv[0])) { if (pipeout) { #ifndef SMALL ! 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 unveil", NULL) == -1) err(1, "pledge"); #endif } else { #ifndef SMALL ! 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 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; + + /* 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); + + #ifndef SMALL + + if (*ca_temp) { + + ca_temp2 = calloc(PATH_MAX + 1, sizeof(char)); + if (ca_temp2 == NULL) + err(1, "calloc"); + + for (;;) { + 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)) + break; + + ca_temp3 = ca_temp; + ca_temp = ca_temp2; + ca_temp2 = ca_temp3; + } + + free(ca_temp2); + + } else { + if (unveil("/etc/ssl/cert.pem", "r") == -1) + err(1, "unveil"); + } + #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 */ + for (i = 2 * argc - 2; i >= 0; i -= 2) { + if (unveil_list[i] == NULL) + continue; + for (j = i - 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: + + #ifndef SMALL + free(ca_temp); + #endif + + 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);