]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/cachemgr.cc
3 * $Id: cachemgr.cc,v 1.6 2007/12/14 23:11:53 amosjeffries Exp $
5 * DEBUG: section 0 CGI Cache Manager
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
48 #include <sys/types.h>
63 #include <gnumalloc.h>
70 #if HAVE_NETDB_H && !defined(_SQUID_NETDB_H_) /* protect NEXTSTEP */
71 #define _SQUID_NETDB_H_
84 #include <sys/param.h>
89 #if HAVE_SYS_RESOURCE_H
90 #include <sys/resource.h> /* needs sys/time.h above it */
93 #include <sys/socket.h>
96 #include <netinet/in.h>
99 #include <arpa/inet.h>
102 #include <sys/stat.h>
108 #include <sys/wait.h>
125 #if HAVE_SYS_SELECT_H
126 #include <sys/select.h>
137 #include "IPAddress.h"
139 #ifndef DEFAULT_CACHEMGR_CONFIG
140 #define DEFAULT_CACHEMGR_CONFIG "/etc/squid/cachemgr.conf"
157 * Debugging macros (info goes to error_log on your web server)
158 * Note: do not run cache manager with non zero debugging level
159 * if you do not debug, it may write a lot of [sensitive]
160 * information to your error log.
163 /* debugging level 0 (disabled) - 3 (max) */
164 #define DEBUG_LEVEL 0
165 #define debug(level) if ((level) <= DEBUG_LEVEL && DEBUG_LEVEL > 0)
168 * Static variables and constants
170 static const time_t passwd_ttl
= 60 * 60 * 3; /* in sec */
171 static const char *script_name
= "/cgi-bin/cachemgr.cgi";
172 static const char *progname
= NULL
;
176 * Function prototypes
178 #define safe_free(str) { if (str) { xfree(str); (str) = NULL; } }
179 static const char *safe_str(const char *str
);
180 static const char *xstrtok(char **str
, char del
);
181 static void print_trailer(void);
182 static void auth_html(const char *host
, int port
, const char *user_name
);
183 static void error_html(const char *msg
);
184 static char *menu_url(cachemgr_request
* req
, const char *action
);
185 static int parse_status_line(const char *sline
, const char **statusStr
);
186 static cachemgr_request
*read_request(void);
187 static char *read_get_request(void);
188 static char *read_post_request(void);
190 static void make_pub_auth(cachemgr_request
* req
);
191 static void decode_pub_auth(cachemgr_request
* req
);
192 static void reset_auth(cachemgr_request
* req
);
193 static const char *make_auth_header(const cachemgr_request
* req
);
195 static int check_target_acl(const char *hostname
, int port
);
198 static int s_iInitCount
= 0;
200 int Win32SockInit(void)
202 int iVersionRequested
;
206 if (s_iInitCount
> 0) {
209 } else if (s_iInitCount
< 0)
210 return (s_iInitCount
);
212 /* s_iInitCount == 0. Do the initailization */
213 iVersionRequested
= MAKEWORD(2, 0);
215 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
219 return (s_iInitCount
);
222 if (LOBYTE(wsaData
.wVersion
) != 2 ||
223 HIBYTE(wsaData
.wVersion
) != 0) {
226 return (s_iInitCount
);
230 return (s_iInitCount
);
233 void Win32SockCleanup(void)
235 if (--s_iInitCount
== 0)
241 #endif /* ifdef _SQUID_MSWIN_ */
244 safe_str(const char *str
)
246 return str
? str
: "";
249 /* relaxed number format */
251 is_number(const char *str
)
253 return strspn(str
, "\t -+01234567890./\n") == strlen(str
);
257 xstrtok(char **str
, char del
)
260 char *p
= strchr(*str
, del
);
273 while (len
&& xisspace(tok
[len
- 1]))
276 while (xisspace(*tok
))
287 printf("<HR noshade size=\"1px\">\n");
288 printf("<ADDRESS>\n");
289 printf("Generated %s, by %s/%s@%s\n",
290 mkrfc1123(now
), progname
, VERSION
, getfullhostname());
291 printf("</ADDRESS></BODY></HTML>\n");
295 auth_html(const char *host
, int port
, const char *user_name
)
303 if (!host
|| !strlen(host
))
306 printf("Content-Type: text/html\r\n\r\n");
308 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
310 printf("<HTML><HEAD><TITLE>Cache Manager Interface</TITLE>\n");
312 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
314 printf("<BODY><H1>Cache Manager Interface</H1>\n");
316 printf("<P>This is a WWW interface to the instrumentation interface\n");
318 printf("for the Squid object cache.</P>\n");
320 printf("<HR noshade size=\"1px\">\n");
322 printf("<FORM METHOD=\"POST\" ACTION=\"%s\">\n", script_name
);
324 printf("<TABLE BORDER=\"0\" CELLPADDING=\"10\" CELLSPACING=\"1\">\n");
327 fp
= fopen("cachemgr.conf", "r");
330 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
334 char config_line
[BUFSIZ
];
336 while (fgets(config_line
, BUFSIZ
, fp
)) {
337 char *server
, *comment
;
338 strtok(config_line
, "\r\n");
340 if (config_line
[0] == '#')
343 if (config_line
[0] == '\0')
346 if ((server
= strtok(config_line
, " \t")) == NULL
)
349 if (strchr(server
, '*') || strchr(server
, '[') || strchr(server
, '?')) {
354 comment
= strtok(NULL
, "");
357 while (*comment
== ' ' || *comment
== '\t')
360 if (!comment
|| !*comment
)
364 printf("<TR><TH ALIGN=\"left\">Cache Server:</TH><TD><SELECT NAME=\"server\">\n");
367 printf("<OPTION VALUE=\"%s\"%s>%s</OPTION>\n", server
, (servers
|| *host
) ? "" : " SELECTED", comment
);
372 if (need_host
== 1 && !*host
)
376 printf("<OPTION VALUE=\"\"%s>Other</OPTION>\n", (*host
) ? " SELECTED" : "");
378 printf("</SELECT></TR>\n");
385 if (need_host
== 1 && !*host
)
388 printf("<TR><TH ALIGN=\"left\">Cache Host:</TH><TD><INPUT NAME=\"host\" ");
390 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", host
);
392 printf("<TR><TH ALIGN=\"left\">Cache Port:</TH><TD><INPUT NAME=\"port\" ");
394 printf("size=\"30\" VALUE=\"%d\"></TD></TR>\n", port
);
397 printf("<TR><TH ALIGN=\"left\">Manager name:</TH><TD><INPUT NAME=\"user_name\" ");
399 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", user_name
);
401 printf("<TR><TH ALIGN=\"left\">Password:</TH><TD><INPUT TYPE=\"password\" NAME=\"passwd\" ");
403 printf("size=\"30\" VALUE=\"\"></TD></TR>\n");
405 printf("</TABLE><BR CLEAR=\"all\">\n");
407 printf("<INPUT TYPE=\"submit\" VALUE=\"Continue...\">\n");
415 error_html(const char *msg
)
417 printf("Content-Type: text/html\r\n\r\n");
418 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
419 printf("<HTML><HEAD><TITLE>Cache Manager Error</TITLE>\n");
420 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
421 printf("<BODY><H1>Cache Manager Error</H1>\n");
422 printf("<P>\n%s</P>\n", msg
);
426 /* returns http status extracted from status line or -1 on parsing failure */
428 parse_status_line(const char *sline
, const char **statusStr
)
430 const char *sp
= strchr(sline
, ' ');
435 if (strncasecmp(sline
, "HTTP/", 5) || !sp
)
438 while (xisspace(*++sp
))
451 menu_url(cachemgr_request
* req
, const char *action
)
453 static char url
[1024];
454 snprintf(url
, sizeof(url
), "%s?host=%s&port=%d&user_name=%s&operation=%s&auth=%s",
458 safe_str(req
->user_name
),
460 safe_str(req
->pub_auth
));
465 munge_menu_line(const char *buf
, cachemgr_request
* req
)
473 static char html
[2 * 1024];
481 buf_copy
= x
= xstrdup(buf
);
483 a
= xstrtok(&x
, '\t');
485 d
= xstrtok(&x
, '\t');
487 p
= xstrtok(&x
, '\t');
489 a_url
= xstrdup(menu_url(req
, a
));
491 /* no reason to give a url for a disabled action */
492 if (!strcmp(p
, "disabled"))
493 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (disabled)<A HREF=\"%s\">.</A>\n", d
, a_url
);
495 /* disable a hidden action (requires a password, but password is not in squid.conf) */
496 if (!strcmp(p
, "hidden"))
497 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (hidden)<A HREF=\"%s\">.</A>\n", d
, a_url
);
499 /* disable link if authentication is required and we have no password */
500 if (!strcmp(p
, "protected") && !req
->passwd
)
501 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (requires <a href=\"%s\">authentication</a>)<A HREF=\"%s\">.</A>\n",
502 d
, menu_url(req
, "authenticate"), a_url
);
504 /* highlight protected but probably available entries */
505 if (!strcmp(p
, "protected"))
506 snprintf(html
, sizeof(html
), "<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n",
509 /* public entry or unknown type of protection */
511 snprintf(html
, sizeof(html
), "<LI type=\"disk\"><A HREF=\"%s\">%s</A>\n", a_url
, d
);
521 munge_other_line(const char *buf
, cachemgr_request
* req
)
523 static const char *ttags
[] = {"td", "th"};
525 static char html
[4096];
526 static int table_line_num
= 0;
527 static int next_is_header
= 0;
533 /* does it look like a table? */
535 if (!strchr(buf
, '\t') || *buf
== '\t') {
536 /* nope, just text */
537 snprintf(html
, sizeof(html
), "%s%s",
538 table_line_num
? "</table>\n<pre>" : "", buf
);
543 /* start html table */
544 if (!table_line_num
) {
545 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "</pre><table cellpadding=\"2\" cellspacing=\"1\">\n");
550 is_header
= (!table_line_num
|| next_is_header
) && !strchr(buf
, ':') && !is_number(buf
);
552 ttag
= ttags
[is_header
];
555 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<tr>");
557 /* substitute '\t' */
558 buf_copy
= x
= xstrdup(buf
);
560 if ((p
= strchr(x
, '\n')))
563 while (x
&& strlen(x
)) {
565 const char *cell
= xstrtok(&x
, '\t');
567 while (x
&& *x
== '\t') {
572 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<%s colspan=\"%d\" align=\"%s\">%s</%s>",
574 is_header
? "center" : is_number(cell
) ? "right" : "left",
580 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "</tr>\n");
581 next_is_header
= is_header
&& strstr(buf
, "\t\t");
587 read_reply(int s
, cachemgr_request
* req
)
593 char *tmpfile
= tempnam(NULL
, "tmp0000");
594 FILE *fp
= fopen(tmpfile
, "w+");
597 FILE *fp
= fdopen(s
, "r");
599 /* interpretation states */
601 isStatusLine
, isHeaders
, isBodyStart
, isBody
, isForward
, isEof
, isForwardEof
, isSuccess
, isError
602 } istate
= isStatusLine
;
604 const char *action
= req
->action
;
605 const char *statusStr
= NULL
;
608 if (0 == strlen(req
->action
))
610 else if (0 == strcasecmp(req
->action
, "menu"))
628 while ((reply
=recv(s
, buf
, sizeof(buf
), 0)) > 0)
629 fwrite(buf
, 1, reply
, fp
);
638 /* read reply interpreting one line at a time depending on state */
639 while (istate
< isEof
) {
640 if (!fgets(buf
, sizeof(buf
), fp
))
641 istate
= istate
== isForward
? isForwardEof
: isEof
;
646 /* get HTTP status */
647 /* uncomment the following if you want to debug headers */
648 /* fputs("\r\n\r\n", stdout); */
649 status
= parse_status_line(buf
, &statusStr
);
650 istate
= status
== 200 ? isHeaders
: isForward
;
651 /* if cache asks for authentication, we have to reset our info */
653 if (status
== 401 || status
== 407) {
655 status
= 403; /* Forbiden, see comments in case isForward: */
658 /* this is a way to pass HTTP status to the Web server */
660 printf("Status: %d %s", status
, statusStr
); /* statusStr has '\n' */
665 /* forward header field */
666 if (!strcmp(buf
, "\r\n")) { /* end of headers */
667 fputs("Content-Type: text/html\r\n", stdout
); /* add our type */
668 istate
= isBodyStart
;
671 if (strncasecmp(buf
, "Content-Type:", 13)) /* filter out their type */
677 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
679 printf("<HTML><HEAD><TITLE>CacheMgr@%s: %s</TITLE>\n",
680 req
->hostname
, action
);
682 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");
684 printf("</HEAD><BODY>\n");
687 printf("<H2><a href=\"%s\">Cache Manager</a> menu for %s:</H2>",
688 menu_url(req
, "authenticate"), req
->hostname
);
691 printf("<P><A HREF=\"%s\">%s</A>\n<HR noshade size=\"1px\">\n",
692 menu_url(req
, "menu"), "Cache Manager menu");
697 /* yes, fall through, we do not want to loose the first line */
700 /* interpret [and reformat] cache response */
703 fputs(munge_menu_line(buf
, req
), stdout
);
705 fputs(munge_other_line(buf
, req
), stdout
);
710 /* forward: no modifications allowed */
712 * Note: we currently do not know any way to get browser.reply to
713 * 401 to .cgi because web server filters out all auth info. Thus we
714 * disable authentication headers for now.
716 if (!strncasecmp(buf
, "WWW-Authenticate:", 17) || !strncasecmp(buf
, "Proxy-Authenticate:", 19))
729 printf("</table></PRE>\n");
738 /* indicate that we finished processing an "error" sequence */
744 printf("%s: internal bug: invalid state reached: %d", script_name
, istate
);
763 process_request(cachemgr_request
* req
)
766 char ipbuf
[MAX_IPSTRLEN
];
767 struct addrinfo
*AI
= NULL
;
772 static char buf
[2 * 1024];
775 auth_html(CACHEMGR_HOSTNAME
, CACHE_HTTP_PORT
, "");
779 if (req
->hostname
== NULL
) {
780 req
->hostname
= xstrdup(CACHEMGR_HOSTNAME
);
783 if (req
->port
== 0) {
784 req
->port
= CACHE_HTTP_PORT
;
787 if (req
->action
== NULL
) {
788 req
->action
= xstrdup("");
791 if (strcmp(req
->action
, "authenticate") == 0) {
792 auth_html(req
->hostname
, req
->port
, req
->user_name
);
796 if (!check_target_acl(req
->hostname
, req
->port
)) {
797 snprintf(buf
, 1024, "target %s:%d not allowed in cachemgr.conf\n", req
->hostname
, req
->port
);
803 if ((s
= socket(PF_INET6
, SOCK_STREAM
, 0)) < 0) {
805 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
807 snprintf(buf
, 1024, "socket: %s\n", xstrerror());
812 S
= *gethostbyname(req
->hostname
);
814 if ( !S
.IsAnyAddr() ) {
816 } else if ( S
= req
->hostname
)
819 snprintf(buf
, 1024, "Unknown host: %s\n", req
->hostname
);
824 S
.SetPort(req
->port
);
828 if (connect(s
, AI
->ai_addr
, AI
->ai_addrlen
) < 0) {
829 snprintf(buf
, 1024, "connect %s: %s\n",
830 S
.ToURL(ipbuf
,MAX_IPSTRLEN
),
839 l
= snprintf(buf
, sizeof(buf
),
840 "GET cache_object://%s/%s HTTP/1.0\r\n"
842 "%s" /* Authentication info or nothing */
846 make_auth_header(req
));
848 debug(1) fprintf(stderr
, "wrote request: '%s'\n", buf
);
849 return read_reply(s
, req
);
853 main(int argc
, char *argv
[])
856 cachemgr_request
*req
;
862 atexit(Win32SockCleanup
);
863 _setmode( _fileno( stdin
), _O_BINARY
);
864 _setmode( _fileno( stdout
), _O_BINARY
);
867 if ((s
= strrchr(argv
[0], '\\')))
870 if ((s
= strrchr(argv
[0], '/')))
873 progname
= xstrdup(s
+ 1);
875 progname
= xstrdup(argv
[0]);
877 if ((s
= getenv("SCRIPT_NAME")) != NULL
)
878 script_name
= xstrdup(s
);
880 req
= read_request();
882 return process_request(req
);
886 read_post_request(void)
892 if ((s
= getenv("REQUEST_METHOD")) == NULL
)
895 if (0 != strcasecmp(s
, "POST"))
898 if ((s
= getenv("CONTENT_LENGTH")) == NULL
)
901 if ((len
= atoi(s
)) <= 0)
904 buf
= (char *)xmalloc(len
+ 1);
906 fread(buf
, len
, 1, stdin
);
914 read_get_request(void)
918 if ((s
= getenv("QUERY_STRING")) == NULL
)
924 static cachemgr_request
*
929 cachemgr_request
*req
;
934 if ((buf
= read_post_request()) != NULL
)
936 else if ((buf
= read_get_request()) != NULL
)
943 if (strlen(buf
) == 0 || strlen(buf
) == 4000)
946 if (strlen(buf
) == 0)
953 req
= (cachemgr_request
*)xcalloc(1, sizeof(cachemgr_request
));
955 for (s
= strtok(buf
, "&"); s
!= NULL
; s
= strtok(NULL
, "&")) {
958 if ((q
= strchr(t
, '=')) == NULL
)
967 if (0 == strcasecmp(t
, "server") && strlen(q
))
968 req
->server
= xstrdup(q
);
969 else if (0 == strcasecmp(t
, "host") && strlen(q
))
970 req
->hostname
= xstrdup(q
);
971 else if (0 == strcasecmp(t
, "port") && strlen(q
))
973 else if (0 == strcasecmp(t
, "user_name") && strlen(q
))
974 req
->user_name
= xstrdup(q
);
975 else if (0 == strcasecmp(t
, "passwd") && strlen(q
))
976 req
->passwd
= xstrdup(q
);
977 else if (0 == strcasecmp(t
, "auth") && strlen(q
))
978 req
->pub_auth
= xstrdup(q
), decode_pub_auth(req
);
979 else if (0 == strcasecmp(t
, "operation"))
980 req
->action
= xstrdup(q
);
983 if (req
->server
&& !req
->hostname
) {
985 req
->hostname
= strtok(req
->server
, ":");
987 if ((p
= strtok(NULL
, ":")))
992 debug(1) fprintf(stderr
, "cmgr: got req: host: '%s' port: %d uname: '%s' passwd: '%s' auth: '%s' oper: '%s'\n",
993 safe_str(req
->hostname
), req
->port
, safe_str(req
->user_name
), safe_str(req
->passwd
), safe_str(req
->pub_auth
), safe_str(req
->action
));
998 /* Routines to support authentication */
1001 * Encodes auth info into a "public" form.
1002 * Currently no powerful encryption is used.
1005 make_pub_auth(cachemgr_request
* req
)
1007 static char buf
[1024];
1008 safe_free(req
->pub_auth
);
1009 debug(3) fprintf(stderr
, "cmgr: encoding for pub...\n");
1011 if (!req
->passwd
|| !strlen(req
->passwd
))
1014 /* host | time | user | passwd */
1015 snprintf(buf
, sizeof(buf
), "%s|%d|%s|%s",
1018 req
->user_name
? req
->user_name
: "",
1021 debug(3) fprintf(stderr
, "cmgr: pre-encoded for pub: %s\n", buf
);
1023 debug(3) fprintf(stderr
, "cmgr: encoded: '%s'\n", base64_encode(buf
));
1025 req
->pub_auth
= xstrdup(base64_encode(buf
));
1029 decode_pub_auth(cachemgr_request
* req
)
1032 const char *host_name
;
1033 const char *time_str
;
1034 const char *user_name
;
1037 debug(2) fprintf(stderr
, "cmgr: decoding pub: '%s'\n", safe_str(req
->pub_auth
));
1038 safe_free(req
->passwd
);
1040 if (!req
->pub_auth
|| strlen(req
->pub_auth
) < 4 + strlen(safe_str(req
->hostname
)))
1043 buf
= xstrdup(base64_decode(req
->pub_auth
));
1045 debug(3) fprintf(stderr
, "cmgr: length ok\n");
1047 /* parse ( a lot of memory leaks, but that is cachemgr style :) */
1048 if ((host_name
= strtok(buf
, "|")) == NULL
)
1051 debug(3) fprintf(stderr
, "cmgr: decoded host: '%s'\n", host_name
);
1053 if ((time_str
= strtok(NULL
, "|")) == NULL
)
1056 debug(3) fprintf(stderr
, "cmgr: decoded time: '%s' (now: %d)\n", time_str
, (int) now
);
1058 if ((user_name
= strtok(NULL
, "|")) == NULL
)
1061 debug(3) fprintf(stderr
, "cmgr: decoded uname: '%s'\n", user_name
);
1063 if ((passwd
= strtok(NULL
, "|")) == NULL
)
1066 debug(2) fprintf(stderr
, "cmgr: decoded passwd: '%s'\n", passwd
);
1068 /* verify freshness and validity */
1069 if (atoi(time_str
) + passwd_ttl
< now
)
1072 if (strcasecmp(host_name
, req
->hostname
))
1075 debug(1) fprintf(stderr
, "cmgr: verified auth. info.\n");
1078 xfree(req
->user_name
);
1080 req
->user_name
= xstrdup(user_name
);
1082 req
->passwd
= xstrdup(passwd
);
1088 reset_auth(cachemgr_request
* req
)
1090 safe_free(req
->passwd
);
1091 safe_free(req
->pub_auth
);
1095 make_auth_header(const cachemgr_request
* req
)
1097 static char buf
[1024];
1098 size_t stringLength
= 0;
1104 snprintf(buf
, sizeof(buf
), "%s:%s",
1105 req
->user_name
? req
->user_name
: "",
1108 str64
= base64_encode(buf
);
1110 stringLength
+= snprintf(buf
, sizeof(buf
), "Authorization: Basic %s\r\n", str64
);
1112 assert(stringLength
< sizeof(buf
));
1114 stringLength
+= snprintf(&buf
[stringLength
], sizeof(buf
) - stringLength
,
1115 "Proxy-Authorization: Basic %s\r\n", str64
);
1121 check_target_acl(const char *hostname
, int port
)
1123 char config_line
[BUFSIZ
];
1126 fp
= fopen("cachemgr.conf", "r");
1129 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
1132 #ifdef CACHEMGR_HOSTNAME_DEFINED
1134 if (strcmp(hostname
, CACHEMGR_HOSTNAME
) == 0 && port
== CACHE_HTTP_PORT
)
1139 if (strcmp(hostname
, "localhost") == 0)
1142 if (strcmp(hostname
, getfullhostname()) == 0)
1150 while (fgets(config_line
, BUFSIZ
, fp
)) {
1152 strtok(config_line
, " \r\n\t");
1154 if (config_line
[0] == '#')
1157 if (config_line
[0] == '\0')
1160 if ((token
= strtok(config_line
, ":")) == NULL
)
1165 if (fnmatch(token
, hostname
, 0) != 0)
1170 if (strcmp(token
, hostname
) != 0)
1175 if ((token
= strtok(NULL
, ":")) != NULL
) {
1178 if (strcmp(token
, "*") == 0)
1180 ; /* Wildcard port specification */
1181 else if (strcasecmp(token
, "any") == 0)
1183 ; /* Wildcard port specification */
1184 else if (sscanf(token
, "%d", &i
) != 1)
1189 } else if (port
!= CACHE_HTTP_PORT
)