summaryrefslogtreecommitdiff
path: root/usr.bin/ftp/fetch.c
diff options
context:
space:
mode:
authorJeremie Courreges-Anglas <jca@cvs.openbsd.org>2019-10-13 15:07:14 +0000
committerJeremie Courreges-Anglas <jca@cvs.openbsd.org>2019-10-13 15:07:14 +0000
commitb53c57c4d0aeac6f8f1aa9737927ff60fff8e4d5 (patch)
tree0a2a0d9fc8d98bb3e39edc785d86f7e1dd54ab4f /usr.bin/ftp/fetch.c
parent10609ad5816eeeb8eb701a5d5834894b2492dcb3 (diff)
Factor out socket cleanup code
As a side effect this shuts down the TLS connection before closing the underlying socket for redirectionss. ok job@
Diffstat (limited to 'usr.bin/ftp/fetch.c')
-rw-r--r--usr.bin/ftp/fetch.c57
1 files changed, 31 insertions, 26 deletions
diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c
index 552eaee2fe1..6f87ea58a45 100644
--- a/usr.bin/ftp/fetch.c
+++ b/usr.bin/ftp/fetch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fetch.c,v 1.172 2019/10/09 16:43:22 jca Exp $ */
+/* $OpenBSD: fetch.c,v 1.173 2019/10/13 15:07:13 jca Exp $ */
/* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */
/*-
@@ -76,6 +76,7 @@ char *recode_credentials(const char *_userinfo);
int ftp_printf(FILE *, struct tls *, const char *, ...) __attribute__((format(printf, 3, 4)));
char *ftp_readline(FILE *, struct tls *, size_t *);
size_t ftp_read(FILE *, struct tls *, char *, size_t);
+void ftp_close(FILE **fin, struct tls **tls, volatile int *fd);
#ifndef NOSSL
int proxy_connect(int, char *, char *);
int SSL_vprintf(struct tls *, const char *, va_list);
@@ -919,14 +920,7 @@ noslash:
*loctail = '\0';
if (verbose)
fprintf(ttyout, "Redirected to %s\n", redirurl);
- if (fin != NULL) {
- fclose(fin);
- fin = NULL;
- }
- if (fd != -1) {
- close(fd);
- fd = -1;
- }
+ ftp_close(&fin, &tls, &fd);
rval = url_get(redirurl, proxyenv, savefile, lastfile);
free(redirurl);
goto cleanup_url_get;
@@ -1063,26 +1057,10 @@ improper:
cleanup_url_get:
#ifndef NOSSL
- if (tls != NULL) {
- if (tls_session_fd != -1)
- dprintf(STDERR_FILENO, "tls session resumed: %s\n",
- tls_conn_session_resumed(tls) ? "yes" : "no");
- do {
- i = tls_close(tls);
- } while (i == TLS_WANT_POLLIN || i == TLS_WANT_POLLOUT);
- tls_free(tls);
- }
free(full_host);
free(sslhost);
#endif /* !NOSSL */
- if (fin != NULL) {
- fclose(fin);
- fin = NULL;
- }
- if (fd != -1) {
- close(fd);
- fd = -1;
- }
+ ftp_close(&fin, &tls, &fd);
if (out >= 0 && out != fileno(stdout))
close(out);
free(buf);
@@ -1594,6 +1572,33 @@ ftp_printf(FILE *fp, struct tls *tls, const char *fmt, ...)
return (ret);
}
+void
+ftp_close(FILE **fin, struct tls **tls, volatile int *fd)
+{
+#ifndef NOSSL
+ int ret;
+
+ if (*tls != NULL) {
+ if (tls_session_fd != -1)
+ dprintf(STDERR_FILENO, "tls session resumed: %s\n",
+ tls_conn_session_resumed(*tls) ? "yes" : "no");
+ do {
+ ret = tls_close(*tls);
+ } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
+ tls_free(*tls);
+ *tls = NULL;
+ }
+#endif
+ if (*fin != NULL) {
+ fclose(*fin);
+ *fin = NULL;
+ }
+ if (*fd != -1) {
+ close(*fd);
+ *fd = -1;
+ }
+}
+
#ifndef NOSSL
int
SSL_vprintf(struct tls *tls, const char *fmt, va_list ap)