summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLawrence Teo <lteo@cvs.openbsd.org>2014-07-11 03:31:53 +0000
committerLawrence Teo <lteo@cvs.openbsd.org>2014-07-11 03:31:53 +0000
commitb744d39fcd7ed9beea429837851bf28f17e66645 (patch)
tree32e03d0d99db59c5207222c294715d2ee0c1b54d
parent6368818603865a9199bf2c7b4e9fec08b87b423a (diff)
Allow ftp(1) to change its User-Agent for HTTP(S) URL requests using a
-U command-line option. feedback from deraadt@, halex@, and Adam Thompson ok deraadt@ sthen@, man page changes ok jmc@
-rw-r--r--usr.bin/ftp/fetch.c21
-rw-r--r--usr.bin/ftp/ftp.110
-rw-r--r--usr.bin/ftp/ftp_var.h3
-rw-r--r--usr.bin/ftp/main.c21
4 files changed, 45 insertions, 10 deletions
diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c
index c9bb16e53a3..5ac2231d1c0 100644
--- a/usr.bin/ftp/fetch.c
+++ b/usr.bin/ftp/fetch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fetch.c,v 1.123 2014/07/05 09:20:54 guenther Exp $ */
+/* $OpenBSD: fetch.c,v 1.124 2014/07/11 03:31:52 lteo Exp $ */
/* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */
/*-
@@ -884,10 +884,10 @@ again:
ftp_printf(fin, ssl, "GET %s HTTP/1.0\r\n"
"Proxy-Authorization: Basic %s%s\r\n%s\r\n\r\n",
epath, credentials, buf ? buf : "",
- HTTP_USER_AGENT);
+ httpuseragent);
else
ftp_printf(fin, ssl, "GET %s HTTP/1.0\r\n%s%s\r\n\r\n",
- epath, buf ? buf : "", HTTP_USER_AGENT);
+ epath, buf ? buf : "", httpuseragent);
} else {
#ifndef SMALL
@@ -945,7 +945,7 @@ again:
ftp_printf(fin, ssl, ":%s", port);
#endif /* !SMALL */
ftp_printf(fin, ssl, "\r\n%s%s\r\n\r\n",
- buf ? buf : "", HTTP_USER_AGENT);
+ buf ? buf : "", httpuseragent);
if (verbose)
fprintf(ttyout, "\n");
}
@@ -1284,6 +1284,9 @@ auto_fetch(int argc, char *argv[], char *outfile)
char *cp, *url, *host, *dir, *file, *portnum;
char *username, *pass, *pathstart;
char *ftpproxy, *httpproxy;
+#ifndef SMALL
+ char *uagent = NULL;
+#endif /* !SMALL */
int rval, xargc;
volatile int argpos;
int dirhasglob, filehasglob, oautologin;
@@ -1304,6 +1307,13 @@ auto_fetch(int argc, char *argv[], char *outfile)
if ((httpproxy = getenv(HTTP_PROXY)) != NULL && *httpproxy == '\0')
httpproxy = NULL;
+ if (httpuseragent == NULL)
+ httpuseragent = HTTP_USER_AGENT;
+#ifndef SMALL
+ else
+ uagent = httpuseragent;
+#endif /* !SMALL */
+
/*
* Loop through as long as there's files to fetch.
*/
@@ -1580,6 +1590,9 @@ bad_ftp_url:
}
if (connected && rval != -1)
disconnect(0, NULL);
+#ifndef SMALL
+ free(uagent);
+#endif /* !SMALL */
return (rval);
}
diff --git a/usr.bin/ftp/ftp.1 b/usr.bin/ftp/ftp.1
index cef516a2cac..34338ef2db9 100644
--- a/usr.bin/ftp/ftp.1
+++ b/usr.bin/ftp/ftp.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: ftp.1,v 1.92 2014/06/25 06:57:42 jmc Exp $
+.\" $OpenBSD: ftp.1,v 1.93 2014/07/11 03:31:52 lteo Exp $
.\" $NetBSD: ftp.1,v 1.22 1997/08/18 10:20:22 lukem Exp $
.\"
.\" Copyright (c) 1985, 1989, 1990, 1993
@@ -30,7 +30,7 @@
.\"
.\" @(#)ftp.1 8.3 (Berkeley) 10/9/94
.\"
-.Dd $Mdocdate: June 25 2014 $
+.Dd $Mdocdate: July 11 2014 $
.Dt FTP 1
.Os
.Sh NAME
@@ -62,6 +62,7 @@
.Op Fl o Ar output
.Op Fl S Ar ssl_options
.Op Fl s Ar srcaddr
+.Op Fl U Ar useragent
.Sm off
.No http[s]:// Oo Ar user : password No @
.Oc Ar host Oo : Ar port
@@ -268,6 +269,11 @@ of the connection.
Only useful on systems with more than one address.
.It Fl t
Enables packet tracing.
+.It Fl U Ar useragent
+Set
+.Ar useragent
+as the User-Agent for HTTP(S) URL requests.
+If not specified, the default User-Agent is ``OpenBSD ftp''.
.It Fl V
Disable verbose mode, overriding the default of enabled when input
is from a terminal.
diff --git a/usr.bin/ftp/ftp_var.h b/usr.bin/ftp/ftp_var.h
index f68f547bf9a..eef6ba2f854 100644
--- a/usr.bin/ftp/ftp_var.h
+++ b/usr.bin/ftp/ftp_var.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ftp_var.h,v 1.33 2013/12/24 13:00:59 jca Exp $ */
+/* $OpenBSD: ftp_var.h,v 1.34 2014/07/11 03:31:52 lteo Exp $ */
/* $NetBSD: ftp_var.h,v 1.18 1997/08/18 10:20:25 lukem Exp $ */
/*
@@ -181,6 +181,7 @@ char *httpport; /* port number to use for http connections */
#ifndef SMALL
char *httpsport; /* port number to use for https connections */
#endif /* !SMALL */
+char *httpuseragent; /* user agent for http(s) connections */
char *gateport; /* port number to use for gateftp connections */
jmp_buf toplevel; /* non-local goto stuff for cmd scanner */
diff --git a/usr.bin/ftp/main.c b/usr.bin/ftp/main.c
index b39cf4c6820..5c3c2e858a3 100644
--- a/usr.bin/ftp/main.c
+++ b/usr.bin/ftp/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.87 2014/01/23 00:39:15 deraadt Exp $ */
+/* $OpenBSD: main.c,v 1.88 2014/07/11 03:31:52 lteo Exp $ */
/* $NetBSD: main.c,v 1.24 1997/08/18 10:20:26 lukem Exp $ */
/*
@@ -198,9 +198,10 @@ main(volatile int argc, char *argv[])
#ifndef SMALL
cookiefile = getenv("http_cookies");
#endif /* !SMALL */
+ httpuseragent = NULL;
while ((ch = getopt(argc, argv,
- "46AaCc:dD:Eegik:mno:pP:r:S:s:tvV")) != -1) {
+ "46AaCc:dD:Eegik:mno:pP:r:S:s:tU:vV")) != -1) {
switch (ch) {
case '4':
family = PF_INET;
@@ -361,6 +362,20 @@ main(volatile int argc, char *argv[])
trace = 1;
break;
+ case 'U':
+#ifndef SMALL
+ if (httpuseragent)
+ errx(1, "User-Agent was already defined");
+ /* Ensure that User-Agent value is in a single line. */
+ if (strcspn(optarg, "\r\n") != strlen(optarg))
+ errx(1, "Invalid User-Agent: %s.", optarg);
+ if (asprintf(&httpuseragent, "User-Agent: %s",
+ optarg) == -1)
+ errx(1, "Can't allocate memory for HTTP(S) "
+ "User-Agent");
+#endif /* !SMALL */
+ break;
+
case 'v':
verbose = 1;
break;
@@ -852,7 +867,7 @@ usage(void)
#ifndef SMALL
"[-S ssl_options] "
"[-s srcaddr]\n"
- " "
+ " [-U useragent] "
#endif /* !SMALL */
"http"
#ifndef SMALL