]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
5ac5029d AJ |
2 | * $Id$ |
3 | * | |
d090e020 | 4 | * DEBUG: section -- WWW Client |
30a4f2a8 | 5 | * AUTHOR: Harvest Derived |
6 | * | |
2b6662ba | 7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 8 | * ---------------------------------------------------------- |
30a4f2a8 | 9 | * |
2b6662ba | 10 | * Squid is the result of efforts by numerous individuals from |
11 | * the Internet community; see the CONTRIBUTORS file for full | |
12 | * details. Many organizations have provided support for Squid's | |
13 | * development; see the SPONSORS file for full details. Squid is | |
14 | * Copyrighted (C) 2001 by the Regents of the University of | |
15 | * California; see the COPYRIGHT file for full details. Squid | |
16 | * incorporates software developed and/or copyrighted by other | |
17 | * sources; see the CREDITS file for full details. | |
30a4f2a8 | 18 | * |
19 | * This program is free software; you can redistribute it and/or modify | |
20 | * it under the terms of the GNU General Public License as published by | |
21 | * the Free Software Foundation; either version 2 of the License, or | |
22 | * (at your option) any later version. | |
3afd7aae | 23 | * |
30a4f2a8 | 24 | * This program is distributed in the hope that it will be useful, |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
3afd7aae | 28 | * |
30a4f2a8 | 29 | * You should have received a copy of the GNU General Public License |
30 | * along with this program; if not, write to the Free Software | |
cbdec147 | 31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 32 | * |
30a4f2a8 | 33 | */ |
090089c4 | 34 | |
f7f3304a | 35 | #include "squid.h" |
25f98340 | 36 | #include "base64.h" |
055421ee | 37 | #include "ip/Address.h" |
25f98340 AJ |
38 | #include "rfc1123.h" |
39 | #include "SquidTime.h" | |
94ab55b0 | 40 | |
0e25b470 | 41 | #if _SQUID_WINDOWS_ |
63be0a78 | 42 | /** \cond AUTODOCS-IGNORE */ |
15443eec | 43 | using namespace Squid; |
63be0a78 | 44 | /** \endcond */ |
15443eec | 45 | #endif |
46 | ||
be266cb2 | 47 | #if _SQUID_WINDOWS_ |
b55fa77d | 48 | #include <io.h> |
49 | #endif | |
815f9118 | 50 | #if HAVE_STDIO_H |
94ab55b0 | 51 | #include <stdio.h> |
815f9118 | 52 | #endif |
815f9118 | 53 | #if HAVE_SYS_SOCKET_H |
94ab55b0 | 54 | #include <sys/socket.h> |
815f9118 | 55 | #endif |
56 | #if HAVE_STRING_H | |
94ab55b0 | 57 | #include <string.h> |
815f9118 | 58 | #endif |
59 | #if HAVE_UNISTD_H | |
94ab55b0 | 60 | #include <unistd.h> |
815f9118 | 61 | #endif |
489520a9 | 62 | #if HAVE_NETDB_H |
94ab55b0 | 63 | #include <netdb.h> |
815f9118 | 64 | #endif |
65 | #if HAVE_SIGNAL_H | |
94ab55b0 | 66 | #include <signal.h> |
815f9118 | 67 | #endif |
68 | #if HAVE_ERRNO_H | |
94ab55b0 | 69 | #include <errno.h> |
815f9118 | 70 | #endif |
71 | #if HAVE_SYS_STAT_H | |
94ab55b0 | 72 | #include <sys/stat.h> |
815f9118 | 73 | #endif |
74 | #if HAVE_FCNTL_H | |
94ab55b0 | 75 | #include <fcntl.h> |
815f9118 | 76 | #endif |
77 | #if HAVE_NETINET_IN_H | |
c7f83c7a | 78 | #include <netinet/in.h> |
815f9118 | 79 | #endif |
d3e3ff4f | 80 | #if HAVE_GETOPT_H |
81 | #include <getopt.h> | |
82 | #endif | |
94ab55b0 | 83 | |
823d23e4 | 84 | #if HAVE_GSSAPI |
c904ede7 | 85 | #if HAVE_GSSAPI_GSSAPI_H |
823d23e4 | 86 | #include <gssapi/gssapi.h> |
c904ede7 | 87 | #elif HAVE_GSSAPI_H |
823d23e4 | 88 | #include <gssapi.h> |
75a8c92e AJ |
89 | #endif /* HAVE_GSSAPI_GSSAPI_H/HAVE_GSSAPI_H */ |
90 | #if !HAVE_HEIMDAL_KERBEROS | |
c904ede7 | 91 | #if HAVE_GSSAPI_GSSAPI_KRB5_H |
823d23e4 | 92 | #include <gssapi/gssapi_krb5.h> |
75a8c92e | 93 | #endif |
c904ede7 | 94 | #if HAVE_GSSAPI_GSSAPI_GENERIC_H |
823d23e4 | 95 | #include <gssapi/gssapi_generic.h> |
75a8c92e AJ |
96 | #endif |
97 | #if HAVE_GSSAPI_GSSAPI_EXT_H | |
98 | #include <gssapi/gssapi_ext.h> | |
99 | #endif | |
100 | #endif | |
c904ede7 AJ |
101 | |
102 | #ifndef gss_nt_service_name | |
103 | #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE | |
823d23e4 AJ |
104 | #endif |
105 | ||
106 | #ifndef gss_mech_spnego | |
107 | static gss_OID_desc _gss_mech_spnego = {6, (void *) "\x2b\x06\x01\x05\x05\x02"}; | |
108 | gss_OID gss_mech_spnego = &_gss_mech_spnego; | |
109 | #endif | |
c904ede7 | 110 | #endif /* HAVE_GSSAPI */ |
090089c4 | 111 | |
112 | #ifndef BUFSIZ | |
7ed62376 AJ |
113 | #define BUFSIZ 8192 |
114 | #endif | |
115 | #ifndef MESSAGELEN | |
116 | #define MESSAGELEN 65536 | |
117 | #endif | |
118 | #ifndef HEADERLEN | |
119 | #define HEADERLEN 65536 | |
090089c4 | 120 | #endif |
121 | ||
94ab55b0 | 122 | typedef void SIGHDLR(int sig); |
123 | ||
090089c4 | 124 | /* Local functions */ |
b7ac5457 | 125 | static int client_comm_bind(int, const Ip::Address &); |
62e76326 | 126 | |
b7ac5457 | 127 | static int client_comm_connect(int, const Ip::Address &, struct timeval *); |
f5b8bbc4 | 128 | static void usage(const char *progname); |
62e76326 | 129 | |
899bab3f | 130 | static int Now(struct timeval *); |
daacd51f AJ |
131 | SIGHDLR catchSignal; |
132 | SIGHDLR pipe_handler; | |
d6d09e02 | 133 | static void set_our_signal(void); |
20cbfe5a | 134 | static ssize_t myread(int fd, void *buf, size_t len); |
135 | static ssize_t mywrite(int fd, void *buf, size_t len); | |
823d23e4 AJ |
136 | |
137 | ||
138 | #if HAVE_GSSAPI | |
139 | static int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, const char *function); | |
140 | static char *GSSAPI_token(const char *server); | |
141 | #endif | |
142 | ||
cca89eeb | 143 | static int put_fd; |
144 | static char *put_file = NULL; | |
62e76326 | 145 | |
b6c6bcef | 146 | static struct stat sb; |
147 | int total_bytes = 0; | |
20cbfe5a | 148 | int io_timeout = 120; |
090089c4 | 149 | |
0e25b470 | 150 | #if _SQUID_WINDOWS_ |
ec556193 GS |
151 | void |
152 | Win32SockCleanup(void) | |
153 | { | |
154 | WSACleanup(); | |
155 | return; | |
156 | } | |
1191b93b | 157 | #endif |
ec556193 | 158 | |
b8d8561b | 159 | static void |
0ee4272b | 160 | usage(const char *progname) |
090089c4 | 161 | { |
0ee4272b | 162 | fprintf(stderr, |
3afd7aae | 163 | "Version: %s\n" |
ba7ce724 | 164 | "Usage: %s [-arsv] [-A 'string'] [-g count] [-h remote host] [-H 'string'] [-i IMS] [-I ping-interval] [-j 'Host-header']" |
823d23e4 AJ |
165 | "[-k] [-l local-host] [-m method] " |
166 | #if HAVE_GSSAPI | |
167 | "[-n] [-N] " | |
168 | #endif | |
169 | "[-p port] [-P file] [-t count] [-T timeout] [-u proxy-user] [-U www-user] " | |
4f16e7af AJ |
170 | "[-V version] [-w proxy-password] [-W www-password] url\n" |
171 | "\n" | |
3afd7aae | 172 | "Options:\n" |
3afd7aae | 173 | " -a Do NOT include Accept: header.\n" |
ba7ce724 | 174 | " -A User-Agent: header. Use \"\" to omit.\n" |
4f16e7af | 175 | " -g count Ping mode, perform \"count\" iterations (0 to loop until interrupted).\n" |
3afd7aae | 176 | " -h host Retrieve URL from cache on hostname. Default is localhost.\n" |
4f16e7af AJ |
177 | " -H 'string' Extra headers to send. Use '\\n' for new lines.\n" |
178 | " -i IMS If-Modified-Since time (in Epoch seconds).\n" | |
179 | " -I interval Ping interval in seconds (default 1 second).\n" | |
5ac5029d | 180 | " -j hosthdr Host header content\n" |
51d94d10 | 181 | " -k Keep the connection active. Default is to do only one request then close.\n" |
4f16e7af | 182 | " -l host Specify a local IP address to bind to. Default is none.\n" |
3afd7aae | 183 | " -m method Request method, default is GET.\n" |
823d23e4 AJ |
184 | #if HAVE_GSSAPI |
185 | " -n Proxy Negotiate(Kerberos) authentication\n" | |
186 | " -N WWW Negotiate(Kerberos) authentication\n" | |
187 | #endif | |
4f16e7af AJ |
188 | " -p port Port number of cache. Default is %d.\n" |
189 | " -P file PUT request. Using the named file\n" | |
190 | " -r Force cache to reload URL.\n" | |
191 | " -s Silent. Do not print data to stdout.\n" | |
3afd7aae | 192 | " -t count Trace count cache-hops\n" |
3afd7aae AJ |
193 | " -T timeout Timeout value (seconds) for read/write operations.\n" |
194 | " -u user Proxy authentication username\n" | |
3afd7aae | 195 | " -U user WWW authentication username\n" |
4f16e7af | 196 | " -v Verbose. Print outgoing message to stderr.\n" |
823d23e4 | 197 | " -V version HTTP Version. Use '-' for HTTP/0.9 omitted case\n" |
4f16e7af | 198 | " -w password Proxy authentication password\n" |
823d23e4 | 199 | " -W password WWW authentication password\n", |
3afd7aae | 200 | VERSION, progname, CACHE_HTTP_PORT); |
090089c4 | 201 | exit(1); |
202 | } | |
203 | ||
899bab3f | 204 | static int interrupted = 0; |
b8d8561b | 205 | int |
206 | main(int argc, char *argv[]) | |
090089c4 | 207 | { |
208 | int conn, c, len, bytesWritten; | |
209 | int port, to_stdout, reload; | |
899bab3f | 210 | int ping, pcount; |
599eadbe | 211 | int keep_alive = 0; |
88738790 | 212 | int opt_noaccept = 0; |
63259c34 | 213 | int opt_verbose = 0; |
14b85b48 DK |
214 | #if HAVE_GSSAPI |
215 | int www_neg = 0, proxy_neg = 0; | |
216 | #endif | |
a2c963ae | 217 | const char *hostname, *localhost; |
b7ac5457 | 218 | Ip::Address iaddr; |
7ed62376 AJ |
219 | char url[BUFSIZ], msg[MESSAGELEN], buf[BUFSIZ]; |
220 | char extra_hdrs[HEADERLEN]; | |
0ee4272b | 221 | const char *method = "GET"; |
090089c4 | 222 | extern char *optarg; |
234967c9 | 223 | time_t ims = 0; |
b3b64e58 | 224 | int max_forwards = -1; |
62e76326 | 225 | |
899bab3f | 226 | struct timeval tv1, tv2; |
227 | int i = 0, loops; | |
228 | long ping_int; | |
229 | long ping_min = 0, ping_max = 0, ping_sum = 0, ping_mean = 0; | |
5ac5029d AJ |
230 | const char *proxy_user = NULL; |
231 | const char *proxy_password = NULL; | |
232 | const char *www_user = NULL; | |
233 | const char *www_password = NULL; | |
234 | const char *host = NULL; | |
235 | const char *version = "1.0"; | |
ba7ce724 | 236 | const char *useragent = NULL; |
090089c4 | 237 | |
238 | /* set the defaults */ | |
2c08acd9 | 239 | hostname = "localhost"; |
240 | localhost = NULL; | |
63259c34 | 241 | extra_hdrs[0] = '\0'; |
090089c4 | 242 | port = CACHE_HTTP_PORT; |
243 | to_stdout = 1; | |
244 | reload = 0; | |
899bab3f | 245 | ping = 0; |
246 | pcount = 0; | |
247 | ping_int = 1 * 1000; | |
090089c4 | 248 | |
249 | if (argc < 2) { | |
3afd7aae | 250 | usage(argv[0]); /* need URL */ |
090089c4 | 251 | } else if (argc >= 2) { |
3afd7aae AJ |
252 | strncpy(url, argv[argc - 1], BUFSIZ); |
253 | url[BUFSIZ - 1] = '\0'; | |
62e76326 | 254 | |
3afd7aae AJ |
255 | if (url[0] == '-') |
256 | usage(argv[0]); | |
823d23e4 | 257 | #if HAVE_GSSAPI |
ba7ce724 | 258 | while ((c = getopt(argc, argv, "aA:h:j:V:l:P:i:km:p:rsvt:g:p:I:H:T:u:U:w:W:nN?")) != -1) |
823d23e4 | 259 | #else |
ba7ce724 | 260 | while ((c = getopt(argc, argv, "aA:h:j:V:l:P:i:km:p:rsvt:g:p:I:H:T:u:U:w:W:?")) != -1) |
823d23e4 | 261 | #endif |
3afd7aae | 262 | switch (c) { |
62e76326 | 263 | |
3afd7aae AJ |
264 | case 'a': |
265 | opt_noaccept = 1; | |
266 | break; | |
62e76326 | 267 | |
ba7ce724 AJ |
268 | case 'A': |
269 | if (optarg != NULL) | |
270 | useragent = optarg; | |
271 | break; | |
272 | ||
3afd7aae | 273 | case 'h': /* remote host */ |
3afd7aae AJ |
274 | if (optarg != NULL) |
275 | hostname = optarg; | |
5ac5029d | 276 | break; |
62e76326 | 277 | |
5ac5029d | 278 | case 'j': |
af6a12ee AJ |
279 | host = optarg; |
280 | break; | |
5ac5029d AJ |
281 | |
282 | case 'V': | |
283 | if (optarg != NULL) | |
284 | version = optarg; | |
3afd7aae | 285 | break; |
62e76326 | 286 | |
3afd7aae AJ |
287 | case 'l': /* local host */ |
288 | if (optarg != NULL) | |
289 | localhost = optarg; | |
3afd7aae | 290 | break; |
62e76326 | 291 | |
3afd7aae AJ |
292 | case 's': /* silent */ |
293 | to_stdout = 0; | |
3afd7aae | 294 | break; |
62e76326 | 295 | |
3afd7aae AJ |
296 | case 'k': /* backward compat */ |
297 | keep_alive = 1; | |
3afd7aae | 298 | break; |
62e76326 | 299 | |
3afd7aae AJ |
300 | case 'r': /* reload */ |
301 | reload = 1; | |
3afd7aae | 302 | break; |
62e76326 | 303 | |
3afd7aae AJ |
304 | case 'p': /* port number */ |
305 | sscanf(optarg, "%d", &port); | |
3afd7aae AJ |
306 | if (port < 1) |
307 | port = CACHE_HTTP_PORT; /* default */ | |
3afd7aae | 308 | break; |
62e76326 | 309 | |
3afd7aae AJ |
310 | case 'P': |
311 | put_file = xstrdup(optarg); | |
3afd7aae | 312 | break; |
62e76326 | 313 | |
3afd7aae AJ |
314 | case 'i': /* IMS */ |
315 | ims = (time_t) atoi(optarg); | |
3afd7aae | 316 | break; |
62e76326 | 317 | |
3afd7aae AJ |
318 | case 'm': |
319 | method = xstrdup(optarg); | |
3afd7aae | 320 | break; |
62e76326 | 321 | |
3afd7aae AJ |
322 | case 't': |
323 | method = xstrdup("TRACE"); | |
3afd7aae | 324 | max_forwards = atoi(optarg); |
3afd7aae | 325 | break; |
62e76326 | 326 | |
3afd7aae AJ |
327 | case 'g': |
328 | ping = 1; | |
3afd7aae | 329 | pcount = atoi(optarg); |
3afd7aae | 330 | to_stdout = 0; |
3afd7aae | 331 | break; |
62e76326 | 332 | |
3afd7aae AJ |
333 | case 'I': |
334 | if ((ping_int = atoi(optarg) * 1000) <= 0) | |
335 | usage(argv[0]); | |
3afd7aae | 336 | break; |
62e76326 | 337 | |
3afd7aae AJ |
338 | case 'H': |
339 | if (strlen(optarg)) { | |
340 | char *t; | |
341 | strncpy(extra_hdrs, optarg, sizeof(extra_hdrs)); | |
3afd7aae AJ |
342 | while ((t = strstr(extra_hdrs, "\\n"))) |
343 | *t = '\r', *(t + 1) = '\n'; | |
344 | } | |
345 | break; | |
62e76326 | 346 | |
3afd7aae AJ |
347 | case 'T': |
348 | io_timeout = atoi(optarg); | |
349 | break; | |
62e76326 | 350 | |
3afd7aae AJ |
351 | case 'u': |
352 | proxy_user = optarg; | |
353 | break; | |
62e76326 | 354 | |
3afd7aae AJ |
355 | case 'w': |
356 | proxy_password = optarg; | |
357 | break; | |
62e76326 | 358 | |
3afd7aae AJ |
359 | case 'U': |
360 | www_user = optarg; | |
361 | break; | |
62e76326 | 362 | |
3afd7aae AJ |
363 | case 'W': |
364 | www_password = optarg; | |
365 | break; | |
62e76326 | 366 | |
823d23e4 AJ |
367 | #if HAVE_GSSAPI |
368 | case 'n': | |
369 | proxy_neg = 1; | |
370 | break; | |
371 | ||
372 | case 'N': | |
373 | www_neg = 1; | |
374 | break; | |
375 | #endif | |
3afd7aae AJ |
376 | case 'v': |
377 | /* undocumented: may increase verb-level by giving more -v's */ | |
aec55359 | 378 | ++opt_verbose; |
3afd7aae | 379 | break; |
62e76326 | 380 | |
3afd7aae | 381 | case '?': /* usage */ |
62e76326 | 382 | |
3afd7aae AJ |
383 | default: |
384 | usage(argv[0]); | |
385 | break; | |
386 | } | |
090089c4 | 387 | } |
0e25b470 | 388 | #if _SQUID_WINDOWS_ |
0ef0f1de | 389 | { |
3afd7aae AJ |
390 | WSADATA wsaData; |
391 | WSAStartup(2, &wsaData); | |
392 | atexit(Win32SockCleanup); | |
0ef0f1de | 393 | } |
394 | #endif | |
090089c4 | 395 | /* Build the HTTP request */ |
8a9b6b94 | 396 | if (strncmp(url, "mgr:", 4) == 0) { |
3afd7aae | 397 | char *t = xstrdup(url + 4); |
3f3e5473 AJ |
398 | const char *at = NULL; |
399 | if (!strrchr(t, '@')) { // ignore any -w password if @ is explicit already. | |
400 | at = proxy_password; | |
401 | } | |
402 | // embed the -w proxy password into old-style cachemgr URLs | |
403 | if (at) | |
404 | snprintf(url, BUFSIZ, "cache_object://%s/%s@%s", hostname, t, at); | |
405 | else | |
406 | snprintf(url, BUFSIZ, "cache_object://%s/%s", hostname, t); | |
3afd7aae | 407 | xfree(t); |
8a9b6b94 | 408 | } |
cca89eeb | 409 | if (put_file) { |
3afd7aae AJ |
410 | put_fd = open(put_file, O_RDONLY); |
411 | set_our_signal(); | |
412 | ||
413 | if (put_fd < 0) { | |
414 | fprintf(stderr, "%s: can't open file (%s)\n", argv[0], | |
415 | xstrerror()); | |
416 | exit(-1); | |
417 | } | |
be266cb2 | 418 | #if _SQUID_WINDOWS_ |
3afd7aae | 419 | setmode(put_fd, O_BINARY); |
c4aefe96 | 420 | #endif |
62e76326 | 421 | |
3afd7aae | 422 | fstat(put_fd, &sb); |
cca89eeb | 423 | } |
5ac5029d AJ |
424 | |
425 | if (!host) { | |
af6a12ee AJ |
426 | char *newhost = strstr(url, "://"); |
427 | if (newhost) { | |
428 | char *t; | |
429 | newhost += 3; | |
dc47f531 | 430 | newhost = xstrdup(newhost); |
af6a12ee AJ |
431 | t = newhost + strcspn(newhost, "@/?"); |
432 | if (*t == '@') { | |
433 | newhost = t + 1; | |
434 | t = newhost + strcspn(newhost, "@/?"); | |
435 | } | |
436 | *t = '\0'; | |
437 | host = newhost; | |
438 | } | |
5ac5029d AJ |
439 | } |
440 | ||
8d55d7ef | 441 | if (version[0] == '-' || !version[0]) { |
af6a12ee | 442 | /* HTTP/0.9, no headers, no version */ |
5ac5029d AJ |
443 | snprintf(msg, BUFSIZ, "%s %s\r\n", method, url); |
444 | } else { | |
8d55d7ef AJ |
445 | if (!xisdigit(version[0])) // not HTTP/n.n |
446 | snprintf(msg, BUFSIZ, "%s %s %s\r\n", method, url, version); | |
447 | else | |
448 | snprintf(msg, BUFSIZ, "%s %s HTTP/%s\r\n", method, url, version); | |
62e76326 | 449 | |
af6a12ee AJ |
450 | if (host) { |
451 | snprintf(buf, BUFSIZ, "Host: %s\r\n", host); | |
452 | strcat(msg,buf); | |
453 | } | |
454 | ||
ba7ce724 AJ |
455 | if (useragent == NULL) { |
456 | snprintf(buf, BUFSIZ, "User-Agent: squidclient/%s\r\n", VERSION); | |
457 | strcat(msg,buf); | |
458 | } else if (useragent[0] != '\0') { | |
459 | snprintf(buf, BUFSIZ, "User-Agent: %s\r\n", useragent); | |
460 | strcat(msg,buf); | |
461 | } | |
462 | ||
af6a12ee | 463 | if (reload) { |
d440ba10 | 464 | snprintf(buf, BUFSIZ, "Cache-Control: no-cache\r\n"); |
af6a12ee AJ |
465 | strcat(msg, buf); |
466 | } | |
467 | if (put_fd > 0) { | |
173bc2bf | 468 | snprintf(buf, BUFSIZ, "Content-length: %" PRId64 "\r\n", (int64_t) sb.st_size); |
af6a12ee AJ |
469 | strcat(msg, buf); |
470 | } | |
471 | if (opt_noaccept == 0) { | |
472 | snprintf(buf, BUFSIZ, "Accept: */*\r\n"); | |
473 | strcat(msg, buf); | |
474 | } | |
475 | if (ims) { | |
476 | snprintf(buf, BUFSIZ, "If-Modified-Since: %s\r\n", mkrfc1123(ims)); | |
477 | strcat(msg, buf); | |
478 | } | |
479 | if (max_forwards > -1) { | |
480 | snprintf(buf, BUFSIZ, "Max-Forwards: %d\r\n", max_forwards); | |
481 | strcat(msg, buf); | |
482 | } | |
483 | if (proxy_user) { | |
484 | const char *user = proxy_user; | |
485 | const char *password = proxy_password; | |
230c091c | 486 | #if HAVE_GETPASS |
af6a12ee AJ |
487 | if (!password) |
488 | password = getpass("Proxy password: "); | |
230c091c | 489 | #endif |
af6a12ee AJ |
490 | if (!password) { |
491 | fprintf(stderr, "ERROR: Proxy password missing\n"); | |
492 | exit(1); | |
493 | } | |
494 | snprintf(buf, BUFSIZ, "%s:%s", user, password); | |
8bdd0cec | 495 | snprintf(buf, BUFSIZ, "Proxy-Authorization: Basic %s\r\n", old_base64_encode(buf)); |
af6a12ee AJ |
496 | strcat(msg, buf); |
497 | } | |
498 | if (www_user) { | |
499 | const char *user = www_user; | |
500 | const char *password = www_password; | |
230c091c | 501 | #if HAVE_GETPASS |
af6a12ee AJ |
502 | if (!password) |
503 | password = getpass("WWW password: "); | |
230c091c | 504 | #endif |
af6a12ee AJ |
505 | if (!password) { |
506 | fprintf(stderr, "ERROR: WWW password missing\n"); | |
507 | exit(1); | |
508 | } | |
509 | snprintf(buf, BUFSIZ, "%s:%s", user, password); | |
8bdd0cec | 510 | snprintf(buf, BUFSIZ, "Authorization: Basic %s\r\n", old_base64_encode(buf)); |
af6a12ee AJ |
511 | strcat(msg, buf); |
512 | } | |
823d23e4 AJ |
513 | #if HAVE_GSSAPI |
514 | if (www_neg) { | |
515 | if (host) { | |
516 | snprintf(buf, BUFSIZ, "Authorization: Negotiate %s\r\n", GSSAPI_token(host)); | |
517 | strcat(msg, buf); | |
518 | } else | |
519 | fprintf(stderr, "ERROR: server host missing\n"); | |
520 | } | |
521 | if (proxy_neg) { | |
522 | if (hostname) { | |
523 | snprintf(buf, BUFSIZ, "Proxy-Authorization: Negotiate %s\r\n", GSSAPI_token(hostname)); | |
524 | strcat(msg, buf); | |
525 | } else | |
526 | fprintf(stderr, "ERROR: proxy server host missing\n"); | |
527 | } | |
528 | #endif | |
af6a12ee | 529 | |
95e78500 AJ |
530 | /* HTTP/1.0 may need keep-alive explicitly */ |
531 | if (strcmp(version, "1.0") == 0 && keep_alive) | |
532 | strcat(msg, "Connection: keep-alive\r\n"); | |
533 | ||
534 | /* HTTP/1.1 may need close explicitly */ | |
51d94d10 AJ |
535 | if (!keep_alive) |
536 | strcat(msg, "Connection: close\r\n"); | |
af6a12ee AJ |
537 | |
538 | strcat(msg, extra_hdrs); | |
539 | strcat(msg, "\r\n"); | |
a78886fc | 540 | } |
5ac5029d | 541 | |
63259c34 | 542 | if (opt_verbose) |
5d9acded | 543 | fprintf(stderr, "Request:'%s'\n", msg); |
63259c34 | 544 | |
899bab3f | 545 | if (ping) { |
546 | #if HAVE_SIGACTION | |
62e76326 | 547 | |
3afd7aae | 548 | struct sigaction sa, osa; |
62e76326 | 549 | |
3afd7aae AJ |
550 | if (sigaction(SIGINT, NULL, &osa) == 0 && osa.sa_handler == SIG_DFL) { |
551 | sa.sa_handler = catchSignal; | |
552 | sa.sa_flags = 0; | |
553 | sigemptyset(&sa.sa_mask); | |
554 | (void) sigaction(SIGINT, &sa, NULL); | |
555 | } | |
899bab3f | 556 | #else |
3afd7aae | 557 | void (*osig) (int); |
62e76326 | 558 | |
3afd7aae AJ |
559 | if ((osig = signal(SIGINT, catchSignal)) != SIG_DFL) |
560 | (void) signal(SIGINT, osig); | |
62e76326 | 561 | |
899bab3f | 562 | #endif |
62e76326 | 563 | |
899bab3f | 564 | } |
565 | loops = ping ? pcount : 1; | |
62e76326 | 566 | |
aec55359 | 567 | for (i = 0; loops == 0 || i < loops; ++i) { |
3afd7aae | 568 | int fsize = 0; |
cc192b50 | 569 | struct addrinfo *AI = NULL; |
570 | ||
5d9acded AJ |
571 | if (opt_verbose) |
572 | fprintf(stderr, "Resolving... %s\n", hostname); | |
573 | ||
3afd7aae | 574 | /* Connect to the server */ |
988e90e1 | 575 | |
3afd7aae AJ |
576 | if (localhost) { |
577 | if ( !iaddr.GetHostByName(localhost) ) { | |
cc192b50 | 578 | fprintf(stderr, "client: ERROR: Cannot resolve %s: Host unknown.\n", localhost); |
579 | exit(1); | |
580 | } | |
3afd7aae | 581 | } else { |
cc192b50 | 582 | /* Process the remote host name to locate the Protocol required |
583 | in case we are being asked to link to another version of squid */ | |
3afd7aae | 584 | if ( !iaddr.GetHostByName(hostname) ) { |
cc192b50 | 585 | fprintf(stderr, "client: ERROR: Cannot resolve %s: Host unknown.\n", hostname); |
586 | exit(1); | |
587 | } | |
588 | } | |
589 | ||
590 | iaddr.GetAddrInfo(AI); | |
591 | if ((conn = socket(AI->ai_family, AI->ai_socktype, 0)) < 0) { | |
592 | perror("client: socket"); | |
593 | iaddr.FreeAddrInfo(AI); | |
594 | exit(1); | |
595 | } | |
596 | iaddr.FreeAddrInfo(AI); | |
597 | ||
598 | if (localhost && client_comm_bind(conn, iaddr) < 0) { | |
599 | perror("client: bind"); | |
600 | exit(1); | |
601 | } | |
602 | ||
603 | iaddr.SetEmpty(); | |
3afd7aae | 604 | if ( !iaddr.GetHostByName(hostname) ) { |
cc192b50 | 605 | fprintf(stderr, "client: ERROR: Cannot resolve %s: Host unknown.\n", hostname); |
606 | exit(1); | |
607 | } | |
608 | ||
609 | iaddr.SetPort(port); | |
610 | ||
5d9acded AJ |
611 | if (opt_verbose) { |
612 | char ipbuf[MAX_IPSTRLEN]; | |
613 | fprintf(stderr, "Connecting... %s(%s)\n", hostname, iaddr.NtoA(ipbuf, MAX_IPSTRLEN)); | |
614 | } | |
615 | ||
cc192b50 | 616 | if (client_comm_connect(conn, iaddr, ping ? &tv1 : NULL) < 0) { |
e053c141 FC |
617 | char hostnameBuf[MAX_IPSTRLEN]; |
618 | iaddr.ToURL(hostnameBuf, MAX_IPSTRLEN); | |
cc192b50 | 619 | if (errno == 0) { |
e053c141 | 620 | fprintf(stderr, "client: ERROR: Cannot connect to %s: Host unknown.\n", hostnameBuf); |
cc192b50 | 621 | } else { |
622 | char tbuf[BUFSIZ]; | |
e053c141 | 623 | snprintf(tbuf, BUFSIZ, "client: ERROR: Cannot connect to %s", hostnameBuf); |
cc192b50 | 624 | perror(tbuf); |
625 | } | |
626 | exit(1); | |
627 | } | |
5d9acded AJ |
628 | if (opt_verbose) { |
629 | char ipbuf[MAX_IPSTRLEN]; | |
630 | fprintf(stderr, "Connected to: %s (%s)\n", hostname, iaddr.NtoA(ipbuf, MAX_IPSTRLEN)); | |
631 | } | |
988e90e1 | 632 | |
3afd7aae AJ |
633 | /* Send the HTTP request */ |
634 | bytesWritten = mywrite(conn, msg, strlen(msg)); | |
988e90e1 | 635 | |
3afd7aae AJ |
636 | if (bytesWritten < 0) { |
637 | perror("client: ERROR: write"); | |
638 | exit(1); | |
639 | } else if ((unsigned) bytesWritten != strlen(msg)) { | |
640 | fprintf(stderr, "client: ERROR: Cannot send request?: %s\n", msg); | |
641 | exit(1); | |
642 | } | |
cc192b50 | 643 | |
3afd7aae AJ |
644 | if (put_file) { |
645 | int x; | |
646 | lseek(put_fd, 0, SEEK_SET); | |
3afd7aae | 647 | while ((x = read(put_fd, buf, sizeof(buf))) > 0) { |
62e76326 | 648 | |
3afd7aae | 649 | x = mywrite(conn, buf, x); |
62e76326 | 650 | |
3afd7aae | 651 | total_bytes += x; |
62e76326 | 652 | |
3afd7aae AJ |
653 | if (x <= 0) |
654 | break; | |
655 | } | |
62e76326 | 656 | |
3afd7aae AJ |
657 | if (x != 0) |
658 | fprintf(stderr, "client: ERROR: Cannot send file.\n"); | |
659 | } | |
660 | /* Read the data */ | |
54220df8 | 661 | |
0e25b470 | 662 | #if _SQUID_WINDOWS_ |
3afd7aae | 663 | setmode(1, O_BINARY); |
00f768c1 | 664 | #endif |
62e76326 | 665 | |
3afd7aae AJ |
666 | while ((len = myread(conn, buf, sizeof(buf))) > 0) { |
667 | fsize += len; | |
62e76326 | 668 | |
8fee788b AR |
669 | if (to_stdout && fwrite(buf, len, 1, stdout) != 1) |
670 | perror("client: ERROR writing to stdout"); | |
3afd7aae | 671 | } |
62e76326 | 672 | |
0e25b470 | 673 | #if _SQUID_WINDOWS_ |
3afd7aae | 674 | setmode(1, O_TEXT); |
0ef0f1de | 675 | #endif |
62e76326 | 676 | |
3afd7aae | 677 | (void) close(conn); /* done with socket */ |
62e76326 | 678 | |
3afd7aae AJ |
679 | if (interrupted) |
680 | break; | |
62e76326 | 681 | |
3afd7aae | 682 | if (ping) { |
62e76326 | 683 | |
3afd7aae AJ |
684 | struct tm *tmp; |
685 | time_t t2s; | |
686 | long elapsed_msec; | |
62e76326 | 687 | |
3afd7aae AJ |
688 | (void) Now(&tv2); |
689 | elapsed_msec = tvSubMsec(tv1, tv2); | |
690 | t2s = tv2.tv_sec; | |
691 | tmp = localtime(&t2s); | |
692 | fprintf(stderr, "%d-%02d-%02d %02d:%02d:%02d [%d]: %ld.%03ld secs, %f KB/s\n", | |
693 | tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, | |
694 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec, i + 1, | |
695 | elapsed_msec / 1000, elapsed_msec % 1000, | |
696 | elapsed_msec ? (double) fsize / elapsed_msec : -1.0); | |
62e76326 | 697 | |
3afd7aae AJ |
698 | if (i == 0 || elapsed_msec < ping_min) |
699 | ping_min = elapsed_msec; | |
62e76326 | 700 | |
3afd7aae AJ |
701 | if (i == 0 || elapsed_msec > ping_max) |
702 | ping_max = elapsed_msec; | |
62e76326 | 703 | |
3afd7aae | 704 | ping_sum += elapsed_msec; |
62e76326 | 705 | |
3afd7aae AJ |
706 | /* Delay until next "ping_int" boundary */ |
707 | if ((loops == 0 || i + 1 < loops) && elapsed_msec < ping_int) { | |
62e76326 | 708 | |
3afd7aae AJ |
709 | struct timeval tvs; |
710 | long msec_left = ping_int - elapsed_msec; | |
62e76326 | 711 | |
3afd7aae AJ |
712 | tvs.tv_sec = msec_left / 1000; |
713 | tvs.tv_usec = (msec_left % 1000) * 1000; | |
714 | select(0, NULL, NULL, NULL, &tvs); | |
715 | } | |
716 | } | |
090089c4 | 717 | } |
899bab3f | 718 | |
719 | if (ping && i) { | |
3afd7aae AJ |
720 | ping_mean = ping_sum / i; |
721 | fprintf(stderr, "%d requests, round-trip (secs) min/avg/max = " | |
722 | "%ld.%03ld/%ld.%03ld/%ld.%03ld\n", i, | |
723 | ping_min / 1000, ping_min % 1000, ping_mean / 1000, ping_mean % 1000, | |
724 | ping_max / 1000, ping_max % 1000); | |
090089c4 | 725 | } |
090089c4 | 726 | exit(0); |
727 | /*NOTREACHED */ | |
983061ed | 728 | return 0; |
090089c4 | 729 | } |
730 | ||
2c08acd9 | 731 | static int |
b7ac5457 | 732 | client_comm_bind(int sock, const Ip::Address &addr) |
e1381638 | 733 | { |
62e76326 | 734 | |
cc192b50 | 735 | int res; |
62e76326 | 736 | |
cc192b50 | 737 | static struct addrinfo *AI = NULL; |
2c08acd9 | 738 | |
739 | /* Set up the source socket address from which to send. */ | |
62e76326 | 740 | |
cc192b50 | 741 | addr.GetAddrInfo(AI); |
2c08acd9 | 742 | |
cc192b50 | 743 | res = bind(sock, AI->ai_addr, AI->ai_addrlen); |
744 | ||
745 | addr.FreeAddrInfo(AI); | |
746 | ||
747 | return res; | |
2c08acd9 | 748 | } |
749 | ||
b8d8561b | 750 | static int |
b7ac5457 | 751 | client_comm_connect(int sock, const Ip::Address &addr, struct timeval *tvp) |
e1381638 | 752 | { |
cc192b50 | 753 | int res; |
754 | static struct addrinfo *AI = NULL; | |
62e76326 | 755 | |
cc192b50 | 756 | /* Set up the destination socket address for message to send to. */ |
62e76326 | 757 | |
cc192b50 | 758 | addr.GetAddrInfo(AI); |
090089c4 | 759 | |
cc192b50 | 760 | res = connect(sock, AI->ai_addr, AI->ai_addrlen); |
090089c4 | 761 | |
cc192b50 | 762 | addr.FreeAddrInfo(AI); |
62e76326 | 763 | |
899bab3f | 764 | if (tvp) |
cc192b50 | 765 | (void) Now(tvp); |
62e76326 | 766 | |
cc192b50 | 767 | return res; |
090089c4 | 768 | } |
899bab3f | 769 | |
770 | static int | |
e1381638 AJ |
771 | Now(struct timeval *tp) |
772 | { | |
899bab3f | 773 | #if GETTIMEOFDAY_NO_TZP |
774 | return gettimeofday(tp); | |
775 | #else | |
62e76326 | 776 | |
899bab3f | 777 | return gettimeofday(tp, NULL); |
778 | #endif | |
779 | } /* ARGSUSED */ | |
780 | ||
daacd51f | 781 | void |
e1381638 AJ |
782 | catchSignal(int sig) |
783 | { | |
899bab3f | 784 | interrupted = 1; |
785 | fprintf(stderr, "Interrupted.\n"); | |
786 | } | |
b6c6bcef | 787 | |
daacd51f | 788 | void |
e1381638 AJ |
789 | pipe_handler(int sig) |
790 | { | |
b6c6bcef | 791 | fprintf(stderr, "SIGPIPE received.\n"); |
54220df8 | 792 | } |
793 | ||
794 | static void | |
e1381638 AJ |
795 | set_our_signal(void) |
796 | { | |
54220df8 | 797 | #if HAVE_SIGACTION |
62e76326 | 798 | |
54220df8 | 799 | struct sigaction sa; |
800 | sa.sa_handler = pipe_handler; | |
801 | sa.sa_flags = SA_RESTART; | |
802 | sigemptyset(&sa.sa_mask); | |
62e76326 | 803 | |
54220df8 | 804 | if (sigaction(SIGPIPE, &sa, NULL) < 0) { |
3afd7aae AJ |
805 | fprintf(stderr, "Cannot set PIPE signal.\n"); |
806 | exit(-1); | |
54220df8 | 807 | } |
808 | #else | |
809 | signal(SIGPIPE, pipe_handler); | |
62e76326 | 810 | |
54220df8 | 811 | #endif |
812 | ||
813 | } | |
20cbfe5a | 814 | |
815 | static ssize_t | |
e1381638 AJ |
816 | myread(int fd, void *buf, size_t len) |
817 | { | |
0e25b470 AJ |
818 | #if _SQUID_WINDOWS_ |
819 | return recv(fd, buf, len, 0); | |
820 | #else | |
20cbfe5a | 821 | alarm(io_timeout); |
822 | return read(fd, buf, len); | |
00f768c1 | 823 | #endif |
20cbfe5a | 824 | } |
825 | ||
826 | static ssize_t | |
e1381638 AJ |
827 | mywrite(int fd, void *buf, size_t len) |
828 | { | |
0e25b470 AJ |
829 | #if _SQUID_WINDOWS_ |
830 | return send(fd, buf, len, 0); | |
831 | #else | |
20cbfe5a | 832 | alarm(io_timeout); |
833 | return write(fd, buf, len); | |
0ef0f1de | 834 | #endif |
00f768c1 | 835 | } |
823d23e4 AJ |
836 | |
837 | #if HAVE_GSSAPI | |
838 | /* | |
839 | * Check return valuse major_status, minor_status for error and print error description | |
840 | * in case of an error. | |
841 | * Returns 1 in case of gssapi error | |
842 | * 0 in case of no gssapi error | |
843 | */ | |
844 | #define BUFFER_SIZE 8192 | |
845 | static int | |
846 | check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, const char *function) | |
847 | { | |
848 | if (GSS_ERROR(major_status)) { | |
849 | OM_uint32 maj_stat, min_stat; | |
850 | OM_uint32 msg_ctx = 0; | |
851 | gss_buffer_desc status_string; | |
852 | char buf[BUFFER_SIZE]; | |
853 | size_t len; | |
854 | ||
855 | len = 0; | |
856 | msg_ctx = 0; | |
857 | while (!msg_ctx) { | |
858 | /* convert major status code (GSS-API error) to text */ | |
859 | maj_stat = gss_display_status(&min_stat, major_status, | |
860 | GSS_C_GSS_CODE, | |
861 | GSS_C_NULL_OID, | |
862 | &msg_ctx, &status_string); | |
863 | if (maj_stat == GSS_S_COMPLETE) { | |
864 | snprintf(buf + len, BUFFER_SIZE-len, "%s", (char *) status_string.value); | |
865 | len += status_string.length; | |
866 | gss_release_buffer(&min_stat, &status_string); | |
867 | break; | |
868 | } | |
869 | gss_release_buffer(&min_stat, &status_string); | |
870 | } | |
871 | snprintf(buf + len, BUFFER_SIZE-len, "%s", ". "); | |
872 | len += 2; | |
873 | msg_ctx = 0; | |
874 | while (!msg_ctx) { | |
875 | /* convert minor status code (underlying routine error) to text */ | |
876 | maj_stat = gss_display_status(&min_stat, minor_status, | |
877 | GSS_C_MECH_CODE, | |
878 | GSS_C_NULL_OID, | |
879 | &msg_ctx, &status_string); | |
880 | if (maj_stat == GSS_S_COMPLETE) { | |
881 | snprintf(buf + len, BUFFER_SIZE-len,"%s", (char *) status_string.value); | |
882 | len += status_string.length; | |
883 | gss_release_buffer(&min_stat, &status_string); | |
884 | break; | |
885 | } | |
886 | gss_release_buffer(&min_stat, &status_string); | |
887 | } | |
888 | fprintf(stderr, "%s failed: %s\n", function, buf); | |
889 | return (1); | |
890 | } | |
891 | return (0); | |
892 | } | |
893 | ||
894 | /* | |
895 | * Get gssapi token for service HTTP/<server> | |
896 | * User has to initiate a kinit user@DOMAIN on commandline first for the | |
897 | * function to be successful | |
898 | * Returns base64 encoded token if successful | |
899 | * string "ERROR" if unsuccessful | |
900 | */ | |
901 | static char * | |
902 | GSSAPI_token(const char *server) | |
903 | { | |
904 | OM_uint32 major_status, minor_status; | |
905 | gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT; | |
906 | gss_name_t server_name = GSS_C_NO_NAME; | |
907 | gss_buffer_desc service = GSS_C_EMPTY_BUFFER; | |
908 | gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER; | |
909 | gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER; | |
910 | char *token = NULL; | |
911 | ||
912 | setbuf(stdout, NULL); | |
913 | setbuf(stdin, NULL); | |
914 | ||
915 | if (!server) { | |
916 | fprintf(stderr, "Error: No server name\n"); | |
917 | return (char *)"ERROR"; | |
918 | } | |
919 | service.value = xmalloc(strlen("HTTP") + strlen(server) + 2); | |
920 | snprintf((char *) service.value, strlen("HTTP") + strlen(server) + 2, "%s@%s", "HTTP", server); | |
921 | service.length = strlen((char *) service.value); | |
922 | ||
923 | major_status = gss_import_name(&minor_status, &service, | |
924 | gss_nt_service_name, &server_name); | |
925 | ||
926 | if (!check_gss_err(major_status, minor_status, "gss_import_name()")) { | |
927 | ||
928 | major_status = gss_init_sec_context(&minor_status, | |
929 | GSS_C_NO_CREDENTIAL, | |
930 | &gss_context, | |
931 | server_name, | |
932 | gss_mech_spnego, | |
933 | 0, | |
934 | 0, | |
935 | GSS_C_NO_CHANNEL_BINDINGS, | |
936 | &input_token, | |
937 | NULL, | |
938 | &output_token, | |
939 | NULL, | |
940 | NULL); | |
941 | ||
942 | if (!check_gss_err(major_status, minor_status, "gss_init_sec_context()")) { | |
943 | ||
944 | if (output_token.length) | |
945 | token = (char *) base64_encode_bin((const char *) output_token.value, output_token.length); | |
946 | } | |
947 | } | |
948 | ||
949 | if (!output_token.length) | |
950 | token = (char *) "ERROR"; | |
951 | gss_delete_sec_context(&minor_status, &gss_context, NULL); | |
952 | gss_release_buffer(&minor_status, &service); | |
953 | gss_release_buffer(&minor_status, &input_token); | |
954 | gss_release_buffer(&minor_status, &output_token); | |
955 | gss_release_name(&minor_status, &server_name); | |
956 | ||
957 | return token; | |
958 | } | |
959 | #endif |