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