]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/cachemgr.cc
2 * $Id: cachemgr.cc,v 1.6 2007/12/14 23:11:53 amosjeffries Exp $
4 * DEBUG: section 0 CGI Cache Manager
5 * AUTHOR: Duane Wessels
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
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.
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.
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.
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
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
47 #include <sys/types.h>
62 #include <gnumalloc.h>
69 #if HAVE_NETDB_H && !defined(_SQUID_NETDB_H_) /* protect NEXTSTEP */
70 #define _SQUID_NETDB_H_
83 #include <sys/param.h>
88 #if HAVE_SYS_RESOURCE_H
89 #include <sys/resource.h> /* needs sys/time.h above it */
92 #include <sys/socket.h>
95 #include <netinet/in.h>
98 #include <arpa/inet.h>
101 #include <sys/stat.h>
107 #include <sys/wait.h>
124 #if HAVE_SYS_SELECT_H
125 #include <sys/select.h>
136 #include "IPAddress.h"
137 #include "getfullhostname.h"
139 #ifndef DEFAULT_CACHEMGR_CONFIG
140 #define DEFAULT_CACHEMGR_CONFIG "/etc/squid/cachemgr.conf"
154 * Debugging macros (info goes to error_log on your web server)
155 * Note: do not run cache manager with non zero debugging level
156 * if you do not debug, it may write a lot of [sensitive]
157 * information to your error log.
160 /* debugging level 0 (disabled) - 3 (max) */
161 #define DEBUG_LEVEL 0
162 #define debug(level) if ((level) <= DEBUG_LEVEL && DEBUG_LEVEL > 0)
165 * Static variables and constants
167 static const time_t passwd_ttl
= 60 * 60 * 3; /* in sec */
168 static const char *script_name
= "/cgi-bin/cachemgr.cgi";
169 static const char *progname
= NULL
;
173 * Function prototypes
175 #define safe_free(str) { if (str) { xfree(str); (str) = NULL; } }
176 static const char *safe_str(const char *str
);
177 static const char *xstrtok(char **str
, char del
);
178 static void print_trailer(void);
179 static void auth_html(const char *host
, int port
, const char *user_name
);
180 static void error_html(const char *msg
);
181 static char *menu_url(cachemgr_request
* req
, const char *action
);
182 static int parse_status_line(const char *sline
, const char **statusStr
);
183 static cachemgr_request
*read_request(void);
184 static char *read_get_request(void);
185 static char *read_post_request(void);
187 static void make_pub_auth(cachemgr_request
* req
);
188 static void decode_pub_auth(cachemgr_request
* req
);
189 static void reset_auth(cachemgr_request
* req
);
190 static const char *make_auth_header(const cachemgr_request
* req
);
192 static int check_target_acl(const char *hostname
, int port
);
195 static int s_iInitCount
= 0;
197 int Win32SockInit(void)
199 int iVersionRequested
;
203 if (s_iInitCount
> 0) {
206 } else if (s_iInitCount
< 0)
207 return (s_iInitCount
);
209 /* s_iInitCount == 0. Do the initailization */
210 iVersionRequested
= MAKEWORD(2, 0);
212 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
216 return (s_iInitCount
);
219 if (LOBYTE(wsaData
.wVersion
) != 2 ||
220 HIBYTE(wsaData
.wVersion
) != 0) {
223 return (s_iInitCount
);
227 return (s_iInitCount
);
230 void Win32SockCleanup(void)
232 if (--s_iInitCount
== 0)
238 #endif /* ifdef _SQUID_MSWIN_ */
241 safe_str(const char *str
)
243 return str
? str
: "";
246 /* relaxed number format */
248 is_number(const char *str
)
250 return strspn(str
, "\t -+01234567890./\n") == strlen(str
);
254 xstrtok(char **str
, char del
)
257 char *p
= strchr(*str
, del
);
270 while (len
&& xisspace(tok
[len
- 1]))
273 while (xisspace(*tok
))
284 printf("<HR noshade size=\"1px\">\n");
285 printf("<ADDRESS>\n");
286 printf("Generated %s, by %s/%s@%s\n",
287 mkrfc1123(now
), progname
, VERSION
, getfullhostname());
288 printf("</ADDRESS></BODY></HTML>\n");
292 auth_html(const char *host
, int port
, const char *user_name
)
300 if (!host
|| !strlen(host
))
303 printf("Content-Type: text/html\r\n\r\n");
305 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
307 printf("<HTML><HEAD><TITLE>Cache Manager Interface</TITLE>\n");
309 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
311 printf("<BODY><H1>Cache Manager Interface</H1>\n");
313 printf("<P>This is a WWW interface to the instrumentation interface\n");
315 printf("for the Squid object cache.</P>\n");
317 printf("<HR noshade size=\"1px\">\n");
319 printf("<FORM METHOD=\"POST\" ACTION=\"%s\">\n", script_name
);
321 printf("<TABLE BORDER=\"0\" CELLPADDING=\"10\" CELLSPACING=\"1\">\n");
324 fp
= fopen("cachemgr.conf", "r");
327 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
331 char config_line
[BUFSIZ
];
333 while (fgets(config_line
, BUFSIZ
, fp
)) {
334 char *server
, *comment
;
335 strtok(config_line
, "\r\n");
337 if (config_line
[0] == '#')
340 if (config_line
[0] == '\0')
343 if ((server
= strtok(config_line
, " \t")) == NULL
)
346 if (strchr(server
, '*') || strchr(server
, '[') || strchr(server
, '?')) {
351 comment
= strtok(NULL
, "");
354 while (*comment
== ' ' || *comment
== '\t')
357 if (!comment
|| !*comment
)
361 printf("<TR><TH ALIGN=\"left\">Cache Server:</TH><TD><SELECT NAME=\"server\">\n");
364 printf("<OPTION VALUE=\"%s\"%s>%s</OPTION>\n", server
, (servers
|| *host
) ? "" : " SELECTED", comment
);
369 if (need_host
== 1 && !*host
)
373 printf("<OPTION VALUE=\"\"%s>Other</OPTION>\n", (*host
) ? " SELECTED" : "");
375 printf("</SELECT></TR>\n");
382 if (need_host
== 1 && !*host
)
385 printf("<TR><TH ALIGN=\"left\">Cache Host:</TH><TD><INPUT NAME=\"host\" ");
387 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", host
);
389 printf("<TR><TH ALIGN=\"left\">Cache Port:</TH><TD><INPUT NAME=\"port\" ");
391 printf("size=\"30\" VALUE=\"%d\"></TD></TR>\n", port
);
394 printf("<TR><TH ALIGN=\"left\">Manager name:</TH><TD><INPUT NAME=\"user_name\" ");
396 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", user_name
);
398 printf("<TR><TH ALIGN=\"left\">Password:</TH><TD><INPUT TYPE=\"password\" NAME=\"passwd\" ");
400 printf("size=\"30\" VALUE=\"\"></TD></TR>\n");
402 printf("</TABLE><BR CLEAR=\"all\">\n");
404 printf("<INPUT TYPE=\"submit\" VALUE=\"Continue...\">\n");
412 error_html(const char *msg
)
414 printf("Content-Type: text/html\r\n\r\n");
415 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
416 printf("<HTML><HEAD><TITLE>Cache Manager Error</TITLE>\n");
417 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
418 printf("<BODY><H1>Cache Manager Error</H1>\n");
419 printf("<P>\n%s</P>\n", html_quote(msg
));
423 /* returns http status extracted from status line or -1 on parsing failure */
425 parse_status_line(const char *sline
, const char **statusStr
)
427 const char *sp
= strchr(sline
, ' ');
432 if (strncasecmp(sline
, "HTTP/", 5) || !sp
)
435 while (xisspace(*++sp
));
446 menu_url(cachemgr_request
* req
, const char *action
)
448 static char url
[1024];
449 snprintf(url
, sizeof(url
), "%s?host=%s&port=%d&user_name=%s&operation=%s&auth=%s",
453 safe_str(req
->user_name
),
455 safe_str(req
->pub_auth
));
460 munge_menu_line(const char *buf
, cachemgr_request
* req
)
468 static char html
[2 * 1024];
476 buf_copy
= x
= xstrdup(buf
);
478 a
= xstrtok(&x
, '\t');
480 d
= xstrtok(&x
, '\t');
482 p
= xstrtok(&x
, '\t');
484 a_url
= xstrdup(menu_url(req
, a
));
486 /* no reason to give a url for a disabled action */
487 if (!strcmp(p
, "disabled"))
488 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (disabled)<A HREF=\"%s\">.</A>\n", d
, a_url
);
490 /* disable a hidden action (requires a password, but password is not in squid.conf) */
491 if (!strcmp(p
, "hidden"))
492 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (hidden)<A HREF=\"%s\">.</A>\n", d
, a_url
);
494 /* disable link if authentication is required and we have no password */
495 if (!strcmp(p
, "protected") && !req
->passwd
)
496 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (requires <a href=\"%s\">authentication</a>)<A HREF=\"%s\">.</A>\n",
497 d
, menu_url(req
, "authenticate"), a_url
);
499 /* highlight protected but probably available entries */
500 if (!strcmp(p
, "protected"))
501 snprintf(html
, sizeof(html
), "<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n",
504 /* public entry or unknown type of protection */
506 snprintf(html
, sizeof(html
), "<LI type=\"disk\"><A HREF=\"%s\">%s</A>\n", a_url
, d
);
516 munge_other_line(const char *buf
, cachemgr_request
* req
)
518 static const char *ttags
[] = {"td", "th"};
520 static char html
[4096];
521 static int table_line_num
= 0;
522 static int next_is_header
= 0;
528 /* does it look like a table? */
530 if (!strchr(buf
, '\t') || *buf
== '\t') {
531 /* nope, just text */
532 snprintf(html
, sizeof(html
), "%s%s",
533 table_line_num
? "</table>\n<pre>" : "", html_quote(buf
));
538 /* start html table */
539 if (!table_line_num
) {
540 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "</pre><table cellpadding=\"2\" cellspacing=\"1\">\n");
545 is_header
= (!table_line_num
|| next_is_header
) && !strchr(buf
, ':') && !is_number(buf
);
547 ttag
= ttags
[is_header
];
550 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<tr>");
552 /* substitute '\t' */
553 buf_copy
= x
= xstrdup(buf
);
555 if ((p
= strchr(x
, '\n')))
558 while (x
&& strlen(x
)) {
560 const char *cell
= xstrtok(&x
, '\t');
562 while (x
&& *x
== '\t') {
567 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<%s colspan=\"%d\" align=\"%s\">%s</%s>",
569 is_header
? "center" : is_number(cell
) ? "right" : "left",
570 html_quote(cell
), ttag
);
575 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "</tr>\n");
576 next_is_header
= is_header
&& strstr(buf
, "\t\t");
582 munge_action_line(const char *_buf
, cachemgr_request
* req
)
584 static char html
[2 * 1024];
585 char *buf
= xstrdup(_buf
);
587 const char *action
, *description
;
590 if ((p
= strchr(x
, '\n')))
592 action
= xstrtok(&x
, '\t');
593 description
= xstrtok(&x
, '\t');
595 description
= action
;
598 snprintf(html
, sizeof(html
), " <a href=\"%s\">%s</a>", menu_url(req
, action
), description
);
603 read_reply(int s
, cachemgr_request
* req
)
609 char *tmpfile
= tempnam(NULL
, "tmp0000");
610 FILE *fp
= fopen(tmpfile
, "w+");
613 FILE *fp
= fdopen(s
, "r");
615 /* interpretation states */
617 isStatusLine
, isHeaders
, isActions
, isBodyStart
, isBody
, isForward
, isEof
, isForwardEof
, isSuccess
, isError
618 } istate
= isStatusLine
;
620 const char *action
= req
->action
;
621 const char *statusStr
= NULL
;
624 if (0 == strlen(req
->action
))
626 else if (0 == strcasecmp(req
->action
, "menu"))
644 while ((reply
=recv(s
, buf
, sizeof(buf
), 0)) > 0)
645 fwrite(buf
, 1, reply
, fp
);
654 /* read reply interpreting one line at a time depending on state */
655 while (istate
< isEof
) {
656 if (!fgets(buf
, sizeof(buf
), fp
))
657 istate
= istate
== isForward
? isForwardEof
: isEof
;
662 /* get HTTP status */
663 /* uncomment the following if you want to debug headers */
664 /* fputs("\r\n\r\n", stdout); */
665 status
= parse_status_line(buf
, &statusStr
);
666 istate
= status
== 200 ? isHeaders
: isForward
;
667 /* if cache asks for authentication, we have to reset our info */
669 if (status
== 401 || status
== 407) {
671 status
= 403; /* Forbiden, see comments in case isForward: */
674 /* this is a way to pass HTTP status to the Web server */
676 printf("Status: %d %s", status
, statusStr
); /* statusStr has '\n' */
681 /* forward header field */
682 if (!strcmp(buf
, "\r\n")) { /* end of headers */
683 fputs("Content-Type: text/html\r\n", stdout
); /* add our type */
684 istate
= isBodyStart
;
687 if (strncasecmp(buf
, "Content-Type:", 13)) /* filter out their type */
693 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
695 printf("<HTML><HEAD><TITLE>CacheMgr@%s: %s</TITLE>\n",
696 req
->hostname
, action
);
698 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");
700 printf("</HEAD><BODY>\n");
703 printf("<H2><a href=\"%s\">Cache Manager</a> menu for %s:</H2>",
704 menu_url(req
, "authenticate"), req
->hostname
);
707 printf("<P><A HREF=\"%s\">%s</A>\n<HR noshade size=\"1px\">\n",
708 menu_url(req
, "menu"), "Cache Manager menu");
713 /* yes, fall through, we do not want to loose the first line */
716 if (strncmp(buf
, "action:", 7) == 0) {
718 fputs(munge_action_line(buf
+ 7, req
), stdout
);
724 printf("<HR noshade size=\"1px\">\n");
729 /* yes, fall through, we do not want to loose the first line */
732 /* interpret [and reformat] cache response */
735 fputs(munge_menu_line(buf
, req
), stdout
);
737 fputs(munge_other_line(buf
, req
), stdout
);
742 /* forward: no modifications allowed */
744 * Note: we currently do not know any way to get browser.reply to
745 * 401 to .cgi because web server filters out all auth info. Thus we
746 * disable authentication headers for now.
748 if (!strncasecmp(buf
, "WWW-Authenticate:", 17) || !strncasecmp(buf
, "Proxy-Authenticate:", 19)); /* skip */
759 printf("</table></PRE>\n");
768 /* indicate that we finished processing an "error" sequence */
774 printf("%s: internal bug: invalid state reached: %d", script_name
, istate
);
793 process_request(cachemgr_request
* req
)
796 char ipbuf
[MAX_IPSTRLEN
];
797 struct addrinfo
*AI
= NULL
;
802 static char buf
[2 * 1024];
805 auth_html(CACHEMGR_HOSTNAME
, CACHE_HTTP_PORT
, "");
809 if (req
->hostname
== NULL
) {
810 req
->hostname
= xstrdup(CACHEMGR_HOSTNAME
);
813 if (req
->port
== 0) {
814 req
->port
= CACHE_HTTP_PORT
;
817 if (req
->action
== NULL
) {
818 req
->action
= xstrdup("");
821 if (strcmp(req
->action
, "authenticate") == 0) {
822 auth_html(req
->hostname
, req
->port
, req
->user_name
);
826 if (!check_target_acl(req
->hostname
, req
->port
)) {
827 snprintf(buf
, 1024, "target %s:%d not allowed in cachemgr.conf\n", req
->hostname
, req
->port
);
832 S
= *gethostbyname(req
->hostname
);
834 if ( !S
.IsAnyAddr() ) {
836 } else if ( S
= req
->hostname
)
839 snprintf(buf
, 1024, "Unknown host: %s\n", req
->hostname
);
844 S
.SetPort(req
->port
);
849 if ((s
= socket( AI
->ai_family
, SOCK_STREAM
, 0)) < 0) {
851 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
853 snprintf(buf
, 1024, "socket: %s\n", xstrerror());
858 if (connect(s
, AI
->ai_addr
, AI
->ai_addrlen
) < 0) {
859 snprintf(buf
, 1024, "connect %s: %s\n",
860 S
.ToURL(ipbuf
,MAX_IPSTRLEN
),
869 l
= snprintf(buf
, sizeof(buf
),
870 "GET cache_object://%s/%s HTTP/1.0\r\n"
872 "%s" /* Authentication info or nothing */
876 make_auth_header(req
));
878 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 fread(buf
, len
, 1, stdin
);
942 read_get_request(void) {
945 if ((s
= getenv("QUERY_STRING")) == NULL
)
951 static cachemgr_request
*
955 cachemgr_request
*req
;
960 if ((buf
= read_post_request()) != NULL
)
962 else if ((buf
= read_get_request()) != NULL
)
969 if (strlen(buf
) == 0 || strlen(buf
) == 4000)
972 if (strlen(buf
) == 0)
979 req
= (cachemgr_request
*)xcalloc(1, sizeof(cachemgr_request
));
981 for (s
= strtok(buf
, "&"); s
!= NULL
; s
= strtok(NULL
, "&")) {
984 if ((q
= strchr(t
, '=')) == NULL
)
993 if (0 == strcasecmp(t
, "server") && strlen(q
))
994 req
->server
= xstrdup(q
);
995 else if (0 == strcasecmp(t
, "host") && strlen(q
))
996 req
->hostname
= xstrdup(q
);
997 else if (0 == strcasecmp(t
, "port") && strlen(q
))
999 else if (0 == strcasecmp(t
, "user_name") && strlen(q
))
1000 req
->user_name
= xstrdup(q
);
1001 else if (0 == strcasecmp(t
, "passwd") && strlen(q
))
1002 req
->passwd
= xstrdup(q
);
1003 else if (0 == strcasecmp(t
, "auth") && strlen(q
))
1004 req
->pub_auth
= xstrdup(q
), decode_pub_auth(req
);
1005 else if (0 == strcasecmp(t
, "operation"))
1006 req
->action
= xstrdup(q
);
1009 if (req
->server
&& !req
->hostname
) {
1011 req
->hostname
= strtok(req
->server
, ":");
1013 if ((p
= strtok(NULL
, ":")))
1014 req
->port
= atoi(p
);
1018 debug(1) fprintf(stderr
, "cmgr: got req: host: '%s' port: %d uname: '%s' passwd: '%s' auth: '%s' oper: '%s'\n",
1019 safe_str(req
->hostname
), req
->port
, safe_str(req
->user_name
), safe_str(req
->passwd
), safe_str(req
->pub_auth
), safe_str(req
->action
));
1024 /* Routines to support authentication */
1027 * Encodes auth info into a "public" form.
1028 * Currently no powerful encryption is used.
1031 make_pub_auth(cachemgr_request
* req
) {
1032 static char buf
[1024];
1033 safe_free(req
->pub_auth
);
1034 debug(3) fprintf(stderr
, "cmgr: encoding for pub...\n");
1036 if (!req
->passwd
|| !strlen(req
->passwd
))
1039 /* host | time | user | passwd */
1040 snprintf(buf
, sizeof(buf
), "%s|%d|%s|%s",
1043 req
->user_name
? req
->user_name
: "",
1046 debug(3) fprintf(stderr
, "cmgr: pre-encoded for pub: %s\n", buf
);
1048 debug(3) fprintf(stderr
, "cmgr: encoded: '%s'\n", base64_encode(buf
));
1050 req
->pub_auth
= xstrdup(base64_encode(buf
));
1054 decode_pub_auth(cachemgr_request
* req
) {
1056 const char *host_name
;
1057 const char *time_str
;
1058 const char *user_name
;
1061 debug(2) fprintf(stderr
, "cmgr: decoding pub: '%s'\n", safe_str(req
->pub_auth
));
1062 safe_free(req
->passwd
);
1064 if (!req
->pub_auth
|| strlen(req
->pub_auth
) < 4 + strlen(safe_str(req
->hostname
)))
1067 buf
= xstrdup(base64_decode(req
->pub_auth
));
1069 debug(3) fprintf(stderr
, "cmgr: length ok\n");
1071 /* parse ( a lot of memory leaks, but that is cachemgr style :) */
1072 if ((host_name
= strtok(buf
, "|")) == NULL
)
1075 debug(3) fprintf(stderr
, "cmgr: decoded host: '%s'\n", host_name
);
1077 if ((time_str
= strtok(NULL
, "|")) == NULL
)
1080 debug(3) fprintf(stderr
, "cmgr: decoded time: '%s' (now: %d)\n", time_str
, (int) now
);
1082 if ((user_name
= strtok(NULL
, "|")) == NULL
)
1085 debug(3) fprintf(stderr
, "cmgr: decoded uname: '%s'\n", user_name
);
1087 if ((passwd
= strtok(NULL
, "|")) == NULL
)
1090 debug(2) fprintf(stderr
, "cmgr: decoded passwd: '%s'\n", passwd
);
1092 /* verify freshness and validity */
1093 if (atoi(time_str
) + passwd_ttl
< now
)
1096 if (strcasecmp(host_name
, req
->hostname
))
1099 debug(1) fprintf(stderr
, "cmgr: verified auth. info.\n");
1102 xfree(req
->user_name
);
1104 req
->user_name
= xstrdup(user_name
);
1106 req
->passwd
= xstrdup(passwd
);
1112 reset_auth(cachemgr_request
* req
) {
1113 safe_free(req
->passwd
);
1114 safe_free(req
->pub_auth
);
1118 make_auth_header(const cachemgr_request
* req
) {
1119 static char buf
[1024];
1120 size_t stringLength
= 0;
1126 snprintf(buf
, sizeof(buf
), "%s:%s",
1127 req
->user_name
? req
->user_name
: "",
1130 str64
= base64_encode(buf
);
1132 stringLength
+= snprintf(buf
, sizeof(buf
), "Authorization: Basic %s\r\n", str64
);
1134 assert(stringLength
< sizeof(buf
));
1136 stringLength
+= snprintf(&buf
[stringLength
], sizeof(buf
) - stringLength
,
1137 "Proxy-Authorization: Basic %s\r\n", str64
);
1143 check_target_acl(const char *hostname
, int port
) {
1144 char config_line
[BUFSIZ
];
1147 fp
= fopen("cachemgr.conf", "r");
1150 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
1153 #ifdef CACHEMGR_HOSTNAME_DEFINED
1155 if (strcmp(hostname
, CACHEMGR_HOSTNAME
) == 0 && port
== CACHE_HTTP_PORT
)
1160 if (strcmp(hostname
, "localhost") == 0)
1163 if (strcmp(hostname
, getfullhostname()) == 0)
1171 while (fgets(config_line
, BUFSIZ
, fp
)) {
1173 strtok(config_line
, " \r\n\t");
1175 if (config_line
[0] == '#')
1178 if (config_line
[0] == '\0')
1181 if ((token
= strtok(config_line
, ":")) == NULL
)
1186 if (fnmatch(token
, hostname
, 0) != 0)
1191 if (strcmp(token
, hostname
) != 0)
1196 if ((token
= strtok(NULL
, ":")) != NULL
) {
1199 if (strcmp(token
, "*") == 0)
1201 ; /* Wildcard port specification */
1202 else if (strcasecmp(token
, "any") == 0)
1204 ; /* Wildcard port specification */
1205 else if (sscanf(token
, "%d", &i
) != 1)
1210 } else if (port
!= CACHE_HTTP_PORT
)