]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/cachemgr.cc
2 * DEBUG: section 0 CGI Cache Manager
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
45 #include <sys/types.h>
60 #include <gnumalloc.h>
67 #if HAVE_NETDB_H && !defined(_SQUID_NETDB_H_) /* protect NEXTSTEP */
68 #define _SQUID_NETDB_H_
81 #include <sys/param.h>
86 #if HAVE_SYS_RESOURCE_H
87 #include <sys/resource.h> /* needs sys/time.h above it */
90 #include <sys/socket.h>
93 #include <netinet/in.h>
96 #include <arpa/inet.h>
105 #include <sys/wait.h>
122 #if HAVE_SYS_SELECT_H
123 #include <sys/select.h>
133 #include "ip/IpAddress.h"
134 #include "getfullhostname.h"
136 #ifndef DEFAULT_CACHEMGR_CONFIG
137 #define DEFAULT_CACHEMGR_CONFIG "/etc/squid/cachemgr.conf"
151 * Debugging macros (info goes to error_log on your web server)
152 * Note: do not run cache manager with non zero debugging level
153 * if you do not debug, it may write a lot of [sensitive]
154 * information to your error log.
157 /* debugging level 0 (disabled) - 3 (max) */
158 #define DEBUG_LEVEL 0
159 #define debug(level) if ((level) <= DEBUG_LEVEL && DEBUG_LEVEL > 0)
162 * Static variables and constants
164 static const time_t passwd_ttl
= 60 * 60 * 3; /* in sec */
165 static const char *script_name
= "/cgi-bin/cachemgr.cgi";
166 static const char *progname
= NULL
;
170 * Function prototypes
172 #define safe_free(str) { if (str) { xfree(str); (str) = NULL; } }
173 static const char *safe_str(const char *str
);
174 static const char *xstrtok(char **str
, char del
);
175 static void print_trailer(void);
176 static void auth_html(const char *host
, int port
, const char *user_name
);
177 static void error_html(const char *msg
);
178 static char *menu_url(cachemgr_request
* req
, const char *action
);
179 static int parse_status_line(const char *sline
, const char **statusStr
);
180 static cachemgr_request
*read_request(void);
181 static char *read_get_request(void);
182 static char *read_post_request(void);
184 static void make_pub_auth(cachemgr_request
* req
);
185 static void decode_pub_auth(cachemgr_request
* req
);
186 static void reset_auth(cachemgr_request
* req
);
187 static const char *make_auth_header(const cachemgr_request
* req
);
189 static int check_target_acl(const char *hostname
, int port
);
192 static int s_iInitCount
= 0;
194 int Win32SockInit(void)
196 int iVersionRequested
;
200 if (s_iInitCount
> 0) {
203 } else if (s_iInitCount
< 0)
204 return (s_iInitCount
);
206 /* s_iInitCount == 0. Do the initailization */
207 iVersionRequested
= MAKEWORD(2, 0);
209 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
213 return (s_iInitCount
);
216 if (LOBYTE(wsaData
.wVersion
) != 2 ||
217 HIBYTE(wsaData
.wVersion
) != 0) {
220 return (s_iInitCount
);
224 return (s_iInitCount
);
227 void Win32SockCleanup(void)
229 if (--s_iInitCount
== 0)
235 #endif /* ifdef _SQUID_MSWIN_ */
238 safe_str(const char *str
)
240 return str
? str
: "";
243 /* relaxed number format */
245 is_number(const char *str
)
247 return strspn(str
, "\t -+01234567890./\n") == strlen(str
);
251 xstrtok(char **str
, char del
)
254 char *p
= strchr(*str
, del
);
267 while (len
&& xisspace(tok
[len
- 1]))
270 while (xisspace(*tok
))
281 printf("<HR noshade size=\"1px\">\n");
282 printf("<ADDRESS>\n");
283 printf("Generated %s, by %s/%s@%s\n",
284 mkrfc1123(now
), progname
, VERSION
, getfullhostname());
285 printf("</ADDRESS></BODY></HTML>\n");
289 auth_html(const char *host
, int port
, const char *user_name
)
297 if (!host
|| !strlen(host
))
300 printf("Content-Type: text/html\r\n\r\n");
302 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
304 printf("<HTML><HEAD><TITLE>Cache Manager Interface</TITLE>\n");
306 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
308 printf("<BODY><H1>Cache Manager Interface</H1>\n");
310 printf("<P>This is a WWW interface to the instrumentation interface\n");
312 printf("for the Squid object cache.</P>\n");
314 printf("<HR noshade size=\"1px\">\n");
316 printf("<FORM METHOD=\"POST\" ACTION=\"%s\">\n", script_name
);
318 printf("<TABLE BORDER=\"0\" CELLPADDING=\"10\" CELLSPACING=\"1\">\n");
321 fp
= fopen("cachemgr.conf", "r");
324 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
328 char config_line
[BUFSIZ
];
330 while (fgets(config_line
, BUFSIZ
, fp
)) {
331 char *server
, *comment
;
332 strtok(config_line
, "\r\n");
334 if (config_line
[0] == '#')
337 if (config_line
[0] == '\0')
340 if ((server
= strtok(config_line
, " \t")) == NULL
)
343 if (strchr(server
, '*') || strchr(server
, '[') || strchr(server
, '?')) {
348 comment
= strtok(NULL
, "");
351 while (*comment
== ' ' || *comment
== '\t')
354 if (!comment
|| !*comment
)
358 printf("<TR><TH ALIGN=\"left\">Cache Server:</TH><TD><SELECT NAME=\"server\">\n");
361 printf("<OPTION VALUE=\"%s\"%s>%s</OPTION>\n", server
, (servers
|| *host
) ? "" : " SELECTED", comment
);
366 if (need_host
== 1 && !*host
)
370 printf("<OPTION VALUE=\"\"%s>Other</OPTION>\n", (*host
) ? " SELECTED" : "");
372 printf("</SELECT></TR>\n");
379 if (need_host
== 1 && !*host
)
382 printf("<TR><TH ALIGN=\"left\">Cache Host:</TH><TD><INPUT NAME=\"host\" ");
384 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", host
);
386 printf("<TR><TH ALIGN=\"left\">Cache Port:</TH><TD><INPUT NAME=\"port\" ");
388 printf("size=\"30\" VALUE=\"%d\"></TD></TR>\n", port
);
391 printf("<TR><TH ALIGN=\"left\">Manager name:</TH><TD><INPUT NAME=\"user_name\" ");
393 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", user_name
);
395 printf("<TR><TH ALIGN=\"left\">Password:</TH><TD><INPUT TYPE=\"password\" NAME=\"passwd\" ");
397 printf("size=\"30\" VALUE=\"\"></TD></TR>\n");
399 printf("</TABLE><BR CLEAR=\"all\">\n");
401 printf("<INPUT TYPE=\"submit\" VALUE=\"Continue...\">\n");
409 error_html(const char *msg
)
411 printf("Content-Type: text/html\r\n\r\n");
412 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
413 printf("<HTML><HEAD><TITLE>Cache Manager Error</TITLE>\n");
414 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
415 printf("<BODY><H1>Cache Manager Error</H1>\n");
416 printf("<P>\n%s</P>\n", html_quote(msg
));
420 /* returns http status extracted from status line or -1 on parsing failure */
422 parse_status_line(const char *sline
, const char **statusStr
)
424 const char *sp
= strchr(sline
, ' ');
429 if (strncasecmp(sline
, "HTTP/", 5) || !sp
)
432 while (xisspace(*++sp
));
443 menu_url(cachemgr_request
* req
, const char *action
)
445 static char url
[1024];
446 snprintf(url
, sizeof(url
), "%s?host=%s&port=%d&user_name=%s&operation=%s&auth=%s",
450 safe_str(req
->user_name
),
452 safe_str(req
->pub_auth
));
457 munge_menu_line(const char *buf
, cachemgr_request
* req
)
465 static char html
[2 * 1024];
473 buf_copy
= x
= xstrdup(buf
);
475 a
= xstrtok(&x
, '\t');
477 d
= xstrtok(&x
, '\t');
479 p
= xstrtok(&x
, '\t');
481 a_url
= xstrdup(menu_url(req
, a
));
483 /* no reason to give a url for a disabled action */
484 if (!strcmp(p
, "disabled"))
485 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (disabled)<A HREF=\"%s\">.</A>\n", d
, a_url
);
487 /* disable a hidden action (requires a password, but password is not in squid.conf) */
488 if (!strcmp(p
, "hidden"))
489 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (hidden)<A HREF=\"%s\">.</A>\n", d
, a_url
);
491 /* disable link if authentication is required and we have no password */
492 if (!strcmp(p
, "protected") && !req
->passwd
)
493 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (requires <a href=\"%s\">authentication</a>)<A HREF=\"%s\">.</A>\n",
494 d
, menu_url(req
, "authenticate"), a_url
);
496 /* highlight protected but probably available entries */
497 if (!strcmp(p
, "protected"))
498 snprintf(html
, sizeof(html
), "<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n",
501 /* public entry or unknown type of protection */
503 snprintf(html
, sizeof(html
), "<LI type=\"disk\"><A HREF=\"%s\">%s</A>\n", a_url
, d
);
513 munge_other_line(const char *buf
, cachemgr_request
* req
)
515 static const char *ttags
[] = {"td", "th"};
517 static char html
[4096];
518 static int table_line_num
= 0;
519 static int next_is_header
= 0;
525 /* does it look like a table? */
527 if (!strchr(buf
, '\t') || *buf
== '\t') {
528 /* nope, just text */
529 snprintf(html
, sizeof(html
), "%s%s",
530 table_line_num
? "</table>\n<pre>" : "", html_quote(buf
));
535 /* start html table */
536 if (!table_line_num
) {
537 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "</pre><table cellpadding=\"2\" cellspacing=\"1\">\n");
542 is_header
= (!table_line_num
|| next_is_header
) && !strchr(buf
, ':') && !is_number(buf
);
544 ttag
= ttags
[is_header
];
547 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<tr>");
549 /* substitute '\t' */
550 buf_copy
= x
= xstrdup(buf
);
552 if ((p
= strchr(x
, '\n')))
555 while (x
&& strlen(x
)) {
557 const char *cell
= xstrtok(&x
, '\t');
559 while (x
&& *x
== '\t') {
564 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<%s colspan=\"%d\" align=\"%s\">%s</%s>",
566 is_header
? "center" : is_number(cell
) ? "right" : "left",
567 html_quote(cell
), ttag
);
572 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "</tr>\n");
573 next_is_header
= is_header
&& strstr(buf
, "\t\t");
579 munge_action_line(const char *_buf
, cachemgr_request
* req
)
581 static char html
[2 * 1024];
582 char *buf
= xstrdup(_buf
);
584 const char *action
, *description
;
587 if ((p
= strchr(x
, '\n')))
589 action
= xstrtok(&x
, '\t');
590 description
= xstrtok(&x
, '\t');
592 description
= action
;
595 snprintf(html
, sizeof(html
), " <a href=\"%s\">%s</a>", menu_url(req
, action
), description
);
600 read_reply(int s
, cachemgr_request
* req
)
606 char *tmpfile
= tempnam(NULL
, "tmp0000");
607 FILE *fp
= fopen(tmpfile
, "w+");
610 FILE *fp
= fdopen(s
, "r");
612 /* interpretation states */
614 isStatusLine
, isHeaders
, isActions
, isBodyStart
, isBody
, isForward
, isEof
, isForwardEof
, isSuccess
, isError
615 } istate
= isStatusLine
;
617 const char *action
= req
->action
;
618 const char *statusStr
= NULL
;
621 if (0 == strlen(req
->action
))
623 else if (0 == strcasecmp(req
->action
, "menu"))
641 while ((reply
=recv(s
, buf
, sizeof(buf
), 0)) > 0)
642 fwrite(buf
, 1, reply
, fp
);
651 /* read reply interpreting one line at a time depending on state */
652 while (istate
< isEof
) {
653 if (!fgets(buf
, sizeof(buf
), fp
))
654 istate
= istate
== isForward
? isForwardEof
: isEof
;
659 /* get HTTP status */
660 /* uncomment the following if you want to debug headers */
661 /* fputs("\r\n\r\n", stdout); */
662 status
= parse_status_line(buf
, &statusStr
);
663 istate
= status
== 200 ? isHeaders
: isForward
;
664 /* if cache asks for authentication, we have to reset our info */
666 if (status
== 401 || status
== 407) {
668 status
= 403; /* Forbiden, see comments in case isForward: */
671 /* this is a way to pass HTTP status to the Web server */
673 printf("Status: %d %s", status
, statusStr
); /* statusStr has '\n' */
678 /* forward header field */
679 if (!strcmp(buf
, "\r\n")) { /* end of headers */
680 fputs("Content-Type: text/html\r\n", stdout
); /* add our type */
681 istate
= isBodyStart
;
684 if (strncasecmp(buf
, "Content-Type:", 13)) /* filter out their type */
690 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
692 printf("<HTML><HEAD><TITLE>CacheMgr@%s: %s</TITLE>\n",
693 req
->hostname
, action
);
695 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}TABLE{background-color:#333333;border:0pt;padding:0pt}TH,TD{background-color:#ffffff;white-space:nowrap}--></STYLE>\n");
697 printf("</HEAD><BODY>\n");
700 printf("<H2><a href=\"%s\">Cache Manager</a> menu for %s:</H2>",
701 menu_url(req
, "authenticate"), req
->hostname
);
704 printf("<P><A HREF=\"%s\">%s</A>\n<HR noshade size=\"1px\">\n",
705 menu_url(req
, "menu"), "Cache Manager menu");
710 /* yes, fall through, we do not want to loose the first line */
713 if (strncmp(buf
, "action:", 7) == 0) {
715 fputs(munge_action_line(buf
+ 7, req
), stdout
);
721 printf("<HR noshade size=\"1px\">\n");
726 /* yes, fall through, we do not want to loose the first line */
729 /* interpret [and reformat] cache response */
732 fputs(munge_menu_line(buf
, req
), stdout
);
734 fputs(munge_other_line(buf
, req
), stdout
);
739 /* forward: no modifications allowed */
741 * Note: we currently do not know any way to get browser.reply to
742 * 401 to .cgi because web server filters out all auth info. Thus we
743 * disable authentication headers for now.
745 if (!strncasecmp(buf
, "WWW-Authenticate:", 17) || !strncasecmp(buf
, "Proxy-Authenticate:", 19)); /* skip */
756 printf("</table></PRE>\n");
765 /* indicate that we finished processing an "error" sequence */
771 printf("%s: internal bug: invalid state reached: %d", script_name
, istate
);
790 process_request(cachemgr_request
* req
)
793 char ipbuf
[MAX_IPSTRLEN
];
794 struct addrinfo
*AI
= NULL
;
799 static char buf
[2 * 1024];
802 auth_html(CACHEMGR_HOSTNAME
, CACHE_HTTP_PORT
, "");
806 if (req
->hostname
== NULL
) {
807 req
->hostname
= xstrdup(CACHEMGR_HOSTNAME
);
810 if (req
->port
== 0) {
811 req
->port
= CACHE_HTTP_PORT
;
814 if (req
->action
== NULL
) {
815 req
->action
= xstrdup("");
818 if (strcmp(req
->action
, "authenticate") == 0) {
819 auth_html(req
->hostname
, req
->port
, req
->user_name
);
823 if (!check_target_acl(req
->hostname
, req
->port
)) {
824 snprintf(buf
, 1024, "target %s:%d not allowed in cachemgr.conf\n", req
->hostname
, req
->port
);
829 S
= *gethostbyname(req
->hostname
);
831 if ( !S
.IsAnyAddr() ) {
833 } else if ( S
= req
->hostname
)
836 snprintf(buf
, 1024, "Unknown host: %s\n", req
->hostname
);
841 S
.SetPort(req
->port
);
846 if ((s
= socket( AI
->ai_family
, SOCK_STREAM
, 0)) < 0) {
848 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
850 snprintf(buf
, 1024, "socket: %s\n", xstrerror());
855 if (connect(s
, AI
->ai_addr
, AI
->ai_addrlen
) < 0) {
856 snprintf(buf
, 1024, "connect %s: %s\n",
857 S
.ToURL(ipbuf
,MAX_IPSTRLEN
),
866 l
= snprintf(buf
, sizeof(buf
),
867 "GET cache_object://%s/%s HTTP/1.0\r\n"
869 "%s" /* Authentication info or nothing */
873 make_auth_header(req
));
874 if (write(s
, buf
, l
) < 0) {
875 debug(1) fprintf(stderr
, "ERROR: (%d) writing request: '%s'\n", errno
, buf
);
877 debug(1) fprintf(stderr
, "wrote request: '%s'\n", buf
);
879 return read_reply(s
, req
);
883 main(int argc
, char *argv
[]) {
885 cachemgr_request
*req
;
891 atexit(Win32SockCleanup
);
892 _setmode( _fileno( stdin
), _O_BINARY
);
893 _setmode( _fileno( stdout
), _O_BINARY
);
896 if ((s
= strrchr(argv
[0], '\\')))
899 if ((s
= strrchr(argv
[0], '/')))
902 progname
= xstrdup(s
+ 1);
904 progname
= xstrdup(argv
[0]);
906 if ((s
= getenv("SCRIPT_NAME")) != NULL
)
907 script_name
= xstrdup(s
);
909 req
= read_request();
911 return process_request(req
);
915 read_post_request(void) {
920 if ((s
= getenv("REQUEST_METHOD")) == NULL
)
923 if (0 != strcasecmp(s
, "POST"))
926 if ((s
= getenv("CONTENT_LENGTH")) == NULL
)
929 if ((len
= atoi(s
)) <= 0)
932 buf
= (char *)xmalloc(len
+ 1);
934 if (fread(buf
, len
, 1, stdin
) == 0)
943 read_get_request(void) {
946 if ((s
= getenv("QUERY_STRING")) == NULL
)
952 static cachemgr_request
*
956 cachemgr_request
*req
;
961 if ((buf
= read_post_request()) != NULL
)
963 else if ((buf
= read_get_request()) != NULL
)
970 if (strlen(buf
) == 0 || strlen(buf
) == 4000)
973 if (strlen(buf
) == 0)
980 req
= (cachemgr_request
*)xcalloc(1, sizeof(cachemgr_request
));
982 for (s
= strtok(buf
, "&"); s
!= NULL
; s
= strtok(NULL
, "&")) {
985 if ((q
= strchr(t
, '=')) == NULL
)
994 if (0 == strcasecmp(t
, "server") && strlen(q
))
995 req
->server
= xstrdup(q
);
996 else if (0 == strcasecmp(t
, "host") && strlen(q
))
997 req
->hostname
= xstrdup(q
);
998 else if (0 == strcasecmp(t
, "port") && strlen(q
))
1000 else if (0 == strcasecmp(t
, "user_name") && strlen(q
))
1001 req
->user_name
= xstrdup(q
);
1002 else if (0 == strcasecmp(t
, "passwd") && strlen(q
))
1003 req
->passwd
= xstrdup(q
);
1004 else if (0 == strcasecmp(t
, "auth") && strlen(q
))
1005 req
->pub_auth
= xstrdup(q
), decode_pub_auth(req
);
1006 else if (0 == strcasecmp(t
, "operation"))
1007 req
->action
= xstrdup(q
);
1010 if (req
->server
&& !req
->hostname
) {
1012 req
->hostname
= strtok(req
->server
, ":");
1014 if ((p
= strtok(NULL
, ":")))
1015 req
->port
= atoi(p
);
1019 debug(1) fprintf(stderr
, "cmgr: got req: host: '%s' port: %d uname: '%s' passwd: '%s' auth: '%s' oper: '%s'\n",
1020 safe_str(req
->hostname
), req
->port
, safe_str(req
->user_name
), safe_str(req
->passwd
), safe_str(req
->pub_auth
), safe_str(req
->action
));
1025 /* Routines to support authentication */
1028 * Encodes auth info into a "public" form.
1029 * Currently no powerful encryption is used.
1032 make_pub_auth(cachemgr_request
* req
) {
1033 static char buf
[1024];
1034 safe_free(req
->pub_auth
);
1035 debug(3) fprintf(stderr
, "cmgr: encoding for pub...\n");
1037 if (!req
->passwd
|| !strlen(req
->passwd
))
1040 /* host | time | user | passwd */
1041 snprintf(buf
, sizeof(buf
), "%s|%d|%s|%s",
1044 req
->user_name
? req
->user_name
: "",
1047 debug(3) fprintf(stderr
, "cmgr: pre-encoded for pub: %s\n", buf
);
1049 debug(3) fprintf(stderr
, "cmgr: encoded: '%s'\n", base64_encode(buf
));
1051 req
->pub_auth
= xstrdup(base64_encode(buf
));
1055 decode_pub_auth(cachemgr_request
* req
) {
1057 const char *host_name
;
1058 const char *time_str
;
1059 const char *user_name
;
1062 debug(2) fprintf(stderr
, "cmgr: decoding pub: '%s'\n", safe_str(req
->pub_auth
));
1063 safe_free(req
->passwd
);
1065 if (!req
->pub_auth
|| strlen(req
->pub_auth
) < 4 + strlen(safe_str(req
->hostname
)))
1068 buf
= xstrdup(base64_decode(req
->pub_auth
));
1070 debug(3) fprintf(stderr
, "cmgr: length ok\n");
1072 /* parse ( a lot of memory leaks, but that is cachemgr style :) */
1073 if ((host_name
= strtok(buf
, "|")) == NULL
)
1076 debug(3) fprintf(stderr
, "cmgr: decoded host: '%s'\n", host_name
);
1078 if ((time_str
= strtok(NULL
, "|")) == NULL
)
1081 debug(3) fprintf(stderr
, "cmgr: decoded time: '%s' (now: %d)\n", time_str
, (int) now
);
1083 if ((user_name
= strtok(NULL
, "|")) == NULL
)
1086 debug(3) fprintf(stderr
, "cmgr: decoded uname: '%s'\n", user_name
);
1088 if ((passwd
= strtok(NULL
, "|")) == NULL
)
1091 debug(2) fprintf(stderr
, "cmgr: decoded passwd: '%s'\n", passwd
);
1093 /* verify freshness and validity */
1094 if (atoi(time_str
) + passwd_ttl
< now
)
1097 if (strcasecmp(host_name
, req
->hostname
))
1100 debug(1) fprintf(stderr
, "cmgr: verified auth. info.\n");
1103 xfree(req
->user_name
);
1105 req
->user_name
= xstrdup(user_name
);
1107 req
->passwd
= xstrdup(passwd
);
1113 reset_auth(cachemgr_request
* req
) {
1114 safe_free(req
->passwd
);
1115 safe_free(req
->pub_auth
);
1119 make_auth_header(const cachemgr_request
* req
) {
1120 static char buf
[1024];
1121 size_t stringLength
= 0;
1127 snprintf(buf
, sizeof(buf
), "%s:%s",
1128 req
->user_name
? req
->user_name
: "",
1131 str64
= base64_encode(buf
);
1133 stringLength
+= snprintf(buf
, sizeof(buf
), "Authorization: Basic %s\r\n", str64
);
1135 assert(stringLength
< sizeof(buf
));
1137 stringLength
+= snprintf(&buf
[stringLength
], sizeof(buf
) - stringLength
,
1138 "Proxy-Authorization: Basic %s\r\n", str64
);
1144 check_target_acl(const char *hostname
, int port
) {
1145 char config_line
[BUFSIZ
];
1148 fp
= fopen("cachemgr.conf", "r");
1151 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
1154 #ifdef CACHEMGR_HOSTNAME_DEFINED
1156 if (strcmp(hostname
, CACHEMGR_HOSTNAME
) == 0 && port
== CACHE_HTTP_PORT
)
1161 if (strcmp(hostname
, "localhost") == 0)
1164 if (strcmp(hostname
, getfullhostname()) == 0)
1172 while (fgets(config_line
, BUFSIZ
, fp
)) {
1174 strtok(config_line
, " \r\n\t");
1176 if (config_line
[0] == '#')
1179 if (config_line
[0] == '\0')
1182 if ((token
= strtok(config_line
, ":")) == NULL
)
1187 if (fnmatch(token
, hostname
, 0) != 0)
1192 if (strcmp(token
, hostname
) != 0)
1197 if ((token
= strtok(NULL
, ":")) != NULL
) {
1200 if (strcmp(token
, "*") == 0)
1202 ; /* Wildcard port specification */
1203 else if (strcasecmp(token
, "any") == 0)
1205 ; /* Wildcard port specification */
1206 else if (sscanf(token
, "%d", &i
) != 1)
1211 } else if (port
!= CACHE_HTTP_PORT
)