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