]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/cachemgr.cc
2 * DEBUG: section -- 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.
35 #include "getfullhostname.h"
36 #include "html_quote.h"
37 #include "ip/Address.h"
57 #include <gnumalloc.h>
71 #include <sys/param.h>
74 #include <sys/socket.h>
77 #include <netinet/in.h>
80 #include <arpa/inet.h>
109 #ifndef DEFAULT_CACHEMGR_CONFIG
110 #define DEFAULT_CACHEMGR_CONFIG "/etc/squid/cachemgr.conf"
126 * Static variables and constants
128 static const time_t passwd_ttl
= 60 * 60 * 3; /* in sec */
129 static const char *script_name
= "/cgi-bin/cachemgr.cgi";
130 static const char *progname
= NULL
;
134 * Function prototypes
136 static const char *safe_str(const char *str
);
137 static const char *xstrtok(char **str
, char del
);
138 static void print_trailer(void);
139 static void auth_html(const char *host
, int port
, const char *user_name
);
140 static void error_html(const char *msg
);
141 static char *menu_url(cachemgr_request
* req
, const char *action
);
142 static int parse_status_line(const char *sline
, const char **statusStr
);
143 static cachemgr_request
*read_request(void);
144 static char *read_get_request(void);
145 static char *read_post_request(void);
147 static void make_pub_auth(cachemgr_request
* req
);
148 static void decode_pub_auth(cachemgr_request
* req
);
149 static void reset_auth(cachemgr_request
* req
);
150 static const char *make_auth_header(const cachemgr_request
* req
);
152 static int check_target_acl(const char *hostname
, int port
);
155 static int s_iInitCount
= 0;
157 int Win32SockInit(void)
159 int iVersionRequested
;
163 if (s_iInitCount
> 0) {
166 } else if (s_iInitCount
< 0)
167 return (s_iInitCount
);
169 /* s_iInitCount == 0. Do the initailization */
170 iVersionRequested
= MAKEWORD(2, 0);
172 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
176 return (s_iInitCount
);
179 if (LOBYTE(wsaData
.wVersion
) != 2 ||
180 HIBYTE(wsaData
.wVersion
) != 0) {
183 return (s_iInitCount
);
187 return (s_iInitCount
);
190 void Win32SockCleanup(void)
192 if (--s_iInitCount
== 0)
201 safe_str(const char *str
)
203 return str
? str
: "";
206 /* relaxed number format */
208 is_number(const char *str
)
210 return strspn(str
, "\t -+01234567890./\n") == strlen(str
);
214 xstrtok(char **str
, char del
)
217 char *p
= strchr(*str
, del
);
230 while (len
&& xisspace(tok
[len
- 1]))
233 while (xisspace(*tok
))
244 printf("<HR noshade size=\"1px\">\n");
245 printf("<ADDRESS>\n");
246 printf("Generated %s, by %s/%s@%s\n",
247 mkrfc1123(now
), progname
, VERSION
, getfullhostname());
248 printf("</ADDRESS></BODY></HTML>\n");
252 auth_html(const char *host
, int port
, const char *user_name
)
260 if (!host
|| !strlen(host
))
263 fp
= fopen("cachemgr.conf", "r");
266 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
269 printf("X-Error: message=\"Unable to open config %s\"", DEFAULT_CACHEMGR_CONFIG
);
271 printf("Content-Type: text/html\r\n\r\n");
273 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
275 printf("<HTML><HEAD><TITLE>Cache Manager Interface</TITLE>\n");
277 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n");
279 printf("<script type=\"text/javascript\">\n");
280 printf("function TS(t, s) {\n");
281 printf(" var x = new XMLHttpRequest();\n");
282 printf(" x.open('GET', 'http' + s + '://' + t + '/squid-internal-mgr/', true);\n");
283 printf(" x.onreadystatechange=function() {\n");
284 printf(" if (x.readyState==4) {\n");
285 printf(" if ((x.status>=200 && x.status <= 299) || x.status==401) {\n");
286 printf(" var v = x.getResponseHeader('Server');\n");
287 printf(" if (v.substring(0,8) == 'squid/3.' && (v[8]=='H' || parseInt(v.substring(8)) >= 2)) {\n");
288 printf(" var d = document.getElementById('H' + s + 'mgr');\n");
289 printf(" if (d.innerHTML == '') d.innerHTML = '<h2>HTTP' + (s=='s'?'S':'') + ' Managed Proxies</h2>';\n");
290 printf(" d.innerHTML = d.innerHTML + '<p>Host: <a href=\"http' + s + '://' + t + '/squid-internal-mgr/\">' + t + '</a></p>';\n");
292 printf(" x.send(null);\n");
294 printf("</script>\n");
298 printf("<BODY><H1>Cache Manager Interface</H1>\n");
300 printf("<P>This is a WWW interface to the instrumentation interface\n");
302 printf("for the Squid object cache.</P>\n");
304 printf("<HR noshade size=\"1px\">\n");
306 printf("<div id=\"Hsmgr\"></div>\n");
307 printf("<div id=\"Hmgr\"></div>\n");
308 printf("<div id=\"Cmgr\">\n");
309 printf("<h2>CGI Managed Proxies</h2>\n");
310 printf("<FORM METHOD=\"POST\" ACTION=\"%s\">\n", script_name
);
312 printf("<TABLE BORDER=\"0\" CELLPADDING=\"10\" CELLSPACING=\"1\">\n");
316 char config_line
[BUFSIZ
];
318 while (fgets(config_line
, BUFSIZ
, fp
)) {
319 char *server
, *comment
;
320 strtok(config_line
, "\r\n");
322 if (config_line
[0] == '#')
325 if (config_line
[0] == '\0')
328 if ((server
= strtok(config_line
, " \t")) == NULL
)
331 if (strchr(server
, '*') || strchr(server
, '[') || strchr(server
, '?')) {
336 comment
= strtok(NULL
, "");
339 while (*comment
== ' ' || *comment
== '\t')
342 if (!comment
|| !*comment
)
346 printf("<TR><TH ALIGN=\"left\">Cache Server:</TH><TD><SELECT id=\"server\" NAME=\"server\">\n");
348 printf("<OPTION VALUE=\"%s\"%s>%s</OPTION>\n", server
, (servers
|| *host
) ? "" : " SELECTED", comment
);
353 if (need_host
== 1 && !*host
)
357 printf("<OPTION VALUE=\"\"%s>Other</OPTION>\n", (*host
) ? " SELECTED" : "");
359 printf("</SELECT></TR>\n");
366 if (need_host
== 1 && !*host
)
369 printf("<TR><TH ALIGN=\"left\">Cache Host:</TH><TD><INPUT NAME=\"host\" ");
371 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", host
);
373 printf("<TR><TH ALIGN=\"left\">Cache Port:</TH><TD><INPUT NAME=\"port\" ");
375 printf("size=\"30\" VALUE=\"%d\"></TD></TR>\n", port
);
378 printf("<TR><TH ALIGN=\"left\">Manager name:</TH><TD><INPUT NAME=\"user_name\" ");
380 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", user_name
);
382 printf("<TR><TH ALIGN=\"left\">Password:</TH><TD><INPUT TYPE=\"password\" NAME=\"passwd\" ");
384 printf("size=\"30\" VALUE=\"\"></TD></TR>\n");
386 printf("</TABLE><BR CLEAR=\"all\">\n");
388 printf("<INPUT TYPE=\"submit\" VALUE=\"Continue...\">\n");
390 printf("</FORM></div>\n");
392 printf("<script type=\"text/javascript\">\n");
393 printf("var s = document.getElementById(\"server\");\n");
394 printf("for (var i = 0; i < s.childElementCount; i++) {\n");
395 printf(" TS(s.children[i].value, '');\n");
396 printf(" TS(s.children[i].value, 's');\n");
397 printf("}</script>\n");
403 error_html(const char *msg
)
405 printf("Content-Type: text/html\r\n\r\n");
406 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
407 printf("<HTML><HEAD><TITLE>Cache Manager Error</TITLE>\n");
408 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
409 printf("<BODY><H1>Cache Manager Error</H1>\n");
410 printf("<P>\n%s</P>\n", html_quote(msg
));
414 /* returns http status extracted from status line or -1 on parsing failure */
416 parse_status_line(const char *sline
, const char **statusStr
)
418 const char *sp
= strchr(sline
, ' ');
423 if (strncasecmp(sline
, "HTTP/", 5) || !sp
)
426 while (xisspace(*++sp
));
437 menu_url(cachemgr_request
* req
, const char *action
)
439 static char url
[1024];
440 snprintf(url
, sizeof(url
), "%s?host=%s&port=%d&user_name=%s&operation=%s&auth=%s",
444 safe_str(req
->user_name
),
446 safe_str(req
->pub_auth
));
451 munge_menu_line(const char *buf
, cachemgr_request
* req
)
459 static char html
[2 * 1024];
467 buf_copy
= x
= xstrdup(buf
);
469 a
= xstrtok(&x
, '\t');
471 d
= xstrtok(&x
, '\t');
473 p
= xstrtok(&x
, '\t');
475 a_url
= xstrdup(menu_url(req
, a
));
477 /* no reason to give a url for a disabled action */
478 if (!strcmp(p
, "disabled"))
479 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (disabled)<A HREF=\"%s\">.</A>\n", d
, a_url
);
481 /* disable a hidden action (requires a password, but password is not in squid.conf) */
482 if (!strcmp(p
, "hidden"))
483 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (hidden)<A HREF=\"%s\">.</A>\n", d
, a_url
);
485 /* disable link if authentication is required and we have no password */
486 if (!strcmp(p
, "protected") && !req
->passwd
)
487 snprintf(html
, sizeof(html
), "<LI type=\"circle\">%s (requires <a href=\"%s\">authentication</a>)<A HREF=\"%s\">.</A>\n",
488 d
, menu_url(req
, "authenticate"), a_url
);
490 /* highlight protected but probably available entries */
491 if (!strcmp(p
, "protected"))
492 snprintf(html
, sizeof(html
), "<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n",
495 /* public entry or unknown type of protection */
497 snprintf(html
, sizeof(html
), "<LI type=\"disk\"><A HREF=\"%s\">%s</A>\n", a_url
, d
);
507 munge_other_line(const char *buf
, cachemgr_request
* req
)
509 static const char *ttags
[] = {"td", "th"};
511 static char html
[4096];
512 static int table_line_num
= 0;
513 static int next_is_header
= 0;
519 /* does it look like a table? */
521 if (!strchr(buf
, '\t') || *buf
== '\t') {
522 /* nope, just text */
523 snprintf(html
, sizeof(html
), "%s%s",
524 table_line_num
? "</table>\n<pre>" : "", html_quote(buf
));
529 /* start html table */
530 if (!table_line_num
) {
531 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "</pre><table cellpadding=\"2\" cellspacing=\"1\">\n");
536 is_header
= (!table_line_num
|| next_is_header
) && !strchr(buf
, ':') && !is_number(buf
);
538 ttag
= ttags
[is_header
];
541 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<tr>");
543 /* substitute '\t' */
544 buf_copy
= x
= xstrdup(buf
);
546 if ((p
= strchr(x
, '\n')))
549 while (x
&& strlen(x
)) {
551 const char *cell
= xstrtok(&x
, '\t');
553 while (x
&& *x
== '\t') {
558 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<%s colspan=\"%d\" align=\"%s\">%s</%s>",
560 is_header
? "center" : is_number(cell
) ? "right" : "left",
561 html_quote(cell
), ttag
);
566 snprintf(html
+ l
, sizeof(html
) - l
, "</tr>\n");
567 next_is_header
= is_header
&& strstr(buf
, "\t\t");
573 munge_action_line(const char *_buf
, cachemgr_request
* req
)
575 static char html
[2 * 1024];
576 char *buf
= xstrdup(_buf
);
578 const char *action
, *description
;
581 if ((p
= strchr(x
, '\n')))
583 action
= xstrtok(&x
, '\t');
588 description
= xstrtok(&x
, '\t');
590 description
= action
;
591 snprintf(html
, sizeof(html
), " <a href=\"%s\">%s</a>", menu_url(req
, action
), description
);
597 read_reply(int s
, cachemgr_request
* req
)
603 char *tmpfile
= tempnam(NULL
, "tmp0000");
604 FILE *fp
= fopen(tmpfile
, "w+");
607 FILE *fp
= fdopen(s
, "r");
609 /* interpretation states */
611 isStatusLine
, isHeaders
, isActions
, isBodyStart
, isBody
, isForward
, isEof
, isForwardEof
, isSuccess
, isError
612 } istate
= isStatusLine
;
614 const char *action
= req
->action
;
615 const char *statusStr
= NULL
;
618 if (0 == strlen(req
->action
))
620 else if (0 == strcasecmp(req
->action
, "menu"))
638 while ((reply
=recv(s
, buf
, sizeof(buf
), 0)) > 0)
639 fwrite(buf
, 1, reply
, fp
);
648 /* read reply interpreting one line at a time depending on state */
649 while (istate
< isEof
) {
650 if (!fgets(buf
, sizeof(buf
), fp
))
651 istate
= istate
== isForward
? isForwardEof
: isEof
;
656 /* get HTTP status */
657 /* uncomment the following if you want to debug headers */
658 /* fputs("\r\n\r\n", stdout); */
659 status
= parse_status_line(buf
, &statusStr
);
660 istate
= status
== 200 ? isHeaders
: isForward
;
661 /* if cache asks for authentication, we have to reset our info */
663 if (status
== 401 || status
== 407) {
665 status
= 403; /* Forbiden, see comments in case isForward: */
668 /* this is a way to pass HTTP status to the Web server */
670 printf("Status: %d %s", status
, statusStr
); /* statusStr has '\n' */
675 /* forward header field */
676 if (!strcmp(buf
, "\r\n")) { /* end of headers */
677 fputs("Content-Type: text/html\r\n", stdout
); /* add our type */
678 istate
= isBodyStart
;
681 if (strncasecmp(buf
, "Content-Type:", 13)) /* filter out their type */
687 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
689 printf("<HTML><HEAD><TITLE>CacheMgr@%s: %s</TITLE>\n",
690 req
->hostname
, action
);
692 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");
694 printf("</HEAD><BODY>\n");
697 printf("<H2><a href=\"%s\">Cache Manager</a> menu for %s:</H2>",
698 menu_url(req
, "authenticate"), req
->hostname
);
701 printf("<P><A HREF=\"%s\">%s</A>\n<HR noshade size=\"1px\">\n",
702 menu_url(req
, "menu"), "Cache Manager menu");
707 /* yes, fall through, we do not want to loose the first line */
710 if (strncmp(buf
, "action:", 7) == 0) {
712 fputs(munge_action_line(buf
+ 7, req
), stdout
);
718 printf("<HR noshade size=\"1px\">\n");
723 /* yes, fall through, we do not want to loose the first line */
726 /* interpret [and reformat] cache response */
729 fputs(munge_menu_line(buf
, req
), stdout
);
731 fputs(munge_other_line(buf
, req
), stdout
);
736 /* forward: no modifications allowed */
738 * Note: we currently do not know any way to get browser.reply to
739 * 401 to .cgi because web server filters out all auth info. Thus we
740 * disable authentication headers for now.
742 if (!strncasecmp(buf
, "WWW-Authenticate:", 17) || !strncasecmp(buf
, "Proxy-Authenticate:", 19)); /* skip */
753 printf("</table></PRE>\n");
762 /* indicate that we finished processing an "error" sequence */
768 printf("%s: internal bug: invalid state reached: %d", script_name
, istate
);
787 process_request(cachemgr_request
* req
)
790 char ipbuf
[MAX_IPSTRLEN
];
791 struct addrinfo
*AI
= NULL
;
796 static char buf
[2 * 1024];
799 auth_html(CACHEMGR_HOSTNAME
, CACHE_HTTP_PORT
, "");
803 if (req
->hostname
== NULL
) {
804 req
->hostname
= xstrdup(CACHEMGR_HOSTNAME
);
807 if (req
->port
== 0) {
808 req
->port
= CACHE_HTTP_PORT
;
811 if (req
->action
== NULL
) {
812 req
->action
= xstrdup("");
815 if (strcmp(req
->action
, "authenticate") == 0) {
816 auth_html(req
->hostname
, req
->port
, req
->user_name
);
820 if (!check_target_acl(req
->hostname
, req
->port
)) {
821 snprintf(buf
, sizeof(buf
), "target %s:%d not allowed in cachemgr.conf\n", req
->hostname
, req
->port
);
826 S
= *gethostbyname(req
->hostname
);
828 if ( !S
.isAnyAddr() ) {
830 } else if ((S
= req
->hostname
))
833 snprintf(buf
, sizeof(buf
), "Unknown host: %s\n", req
->hostname
);
843 if ((s
= socket( AI
->ai_family
, SOCK_STREAM
, 0)) < 0) {
845 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
847 snprintf(buf
, sizeof(buf
), "socket: %s\n", xstrerror());
849 Ip::Address::FreeAddrInfo(AI
);
853 if (connect(s
, AI
->ai_addr
, AI
->ai_addrlen
) < 0) {
854 snprintf(buf
, sizeof(buf
), "connect %s: %s\n",
855 S
.toUrl(ipbuf
,MAX_IPSTRLEN
),
858 Ip::Address::FreeAddrInfo(AI
);
863 Ip::Address::FreeAddrInfo(AI
);
865 l
= snprintf(buf
, sizeof(buf
),
866 "GET cache_object://%s/%s%s%s HTTP/1.0\r\n"
867 "User-Agent: cachemgr.cgi/%s\r\n"
869 "%s" /* Authentication info or nothing */
873 req
->workers
? "?workers=" : (req
->processes
? "?processes=" : ""),
874 req
->workers
? req
->workers
: (req
->processes
? req
->processes
: ""),
876 make_auth_header(req
));
877 if (write(s
, buf
, l
) < 0) {
878 fprintf(stderr
,"ERROR: (%d) writing request: '%s'\n", errno
, buf
);
880 debug("wrote request: '%s'\n", buf
);
882 return read_reply(s
, req
);
886 main(int argc
, char *argv
[])
889 cachemgr_request
*req
;
895 atexit(Win32SockCleanup
);
896 _setmode( _fileno( stdin
), _O_BINARY
);
897 _setmode( _fileno( stdout
), _O_BINARY
);
900 if ((s
= strrchr(argv
[0], '\\')))
903 if ((s
= strrchr(argv
[0], '/')))
906 progname
= xstrdup(s
+ 1);
908 progname
= xstrdup(argv
[0]);
910 if ((s
= getenv("SCRIPT_NAME")) != NULL
)
911 script_name
= xstrdup(s
);
914 while (argc
> 1 && args
[1][0] == '-') {
915 // const char *value = "";
916 char option
= args
[1][1];
922 #if 0 // unused for now.
923 if (strlen(args
[1]) > 2) {
925 } else if (argc
> 2) {
938 req
= read_request();
940 return process_request(req
);
944 read_post_request(void)
948 if ((s
= getenv("REQUEST_METHOD")) == NULL
)
951 if (0 != strcasecmp(s
, "POST"))
954 if ((s
= getenv("CONTENT_LENGTH")) == NULL
)
957 if (*s
== '-') // negative length content huh?
962 char *endptr
= s
+ strlen(s
);
963 if ((len
= strtoll(s
, &endptr
, 10)) <= 0)
966 // limit the input to something reasonable.
967 // 4KB should be enough for the GET/POST data length, but may be extended.
968 size_t bufLen
= (len
< 4096 ? len
: 4095);
969 char *buf
= (char *)xmalloc(bufLen
+ 1);
971 size_t readLen
= fread(buf
, 1, bufLen
, stdin
);
979 // purge the remainder of the request entity
980 while (len
> 0 && readLen
) {
982 readLen
= fread(temp
, 1, 65535, stdin
);
990 read_get_request(void)
994 if ((s
= getenv("QUERY_STRING")) == NULL
)
1000 static cachemgr_request
*
1005 cachemgr_request
*req
;
1010 if ((buf
= read_post_request()) != NULL
)
1012 else if ((buf
= read_get_request()) != NULL
)
1019 if (strlen(buf
) == 0 || strlen(buf
) == 4000)
1022 if (strlen(buf
) == 0)
1029 req
= (cachemgr_request
*)xcalloc(1, sizeof(cachemgr_request
));
1031 for (s
= strtok(buf
, "&"); s
!= NULL
; s
= strtok(NULL
, "&")) {
1034 if ((q
= strchr(t
, '=')) == NULL
)
1040 rfc1738_unescape(t
);
1042 rfc1738_unescape(q
);
1044 if (0 == strcmp(t
, "server") && strlen(q
))
1045 req
->server
= xstrdup(q
);
1046 else if (0 == strcmp(t
, "host") && strlen(q
))
1047 req
->hostname
= xstrdup(q
);
1048 else if (0 == strcmp(t
, "port") && strlen(q
))
1049 req
->port
= atoi(q
);
1050 else if (0 == strcmp(t
, "user_name") && strlen(q
))
1051 req
->user_name
= xstrdup(q
);
1052 else if (0 == strcmp(t
, "passwd") && strlen(q
))
1053 req
->passwd
= xstrdup(q
);
1054 else if (0 == strcmp(t
, "auth") && strlen(q
))
1055 req
->pub_auth
= xstrdup(q
), decode_pub_auth(req
);
1056 else if (0 == strcmp(t
, "operation"))
1057 req
->action
= xstrdup(q
);
1058 else if (0 == strcmp(t
, "workers") && strlen(q
))
1059 req
->workers
= xstrdup(q
);
1060 else if (0 == strcmp(t
, "processes") && strlen(q
))
1061 req
->processes
= xstrdup(q
);
1064 if (req
->server
&& !req
->hostname
) {
1066 req
->hostname
= strtok(req
->server
, ":");
1068 if ((p
= strtok(NULL
, ":")))
1069 req
->port
= atoi(p
);
1073 debug("cmgr: got req: host: '%s' port: %d uname: '%s' passwd: '%s' auth: '%s' oper: '%s' workers: '%s' processes: '%s'\n",
1074 safe_str(req
->hostname
), req
->port
, safe_str(req
->user_name
), safe_str(req
->passwd
), safe_str(req
->pub_auth
), safe_str(req
->action
), safe_str(req
->workers
), safe_str(req
->processes
));
1078 /* Routines to support authentication */
1081 * Encodes auth info into a "public" form.
1082 * Currently no powerful encryption is used.
1085 make_pub_auth(cachemgr_request
* req
)
1087 static char buf
[1024];
1088 safe_free(req
->pub_auth
);
1089 debug("cmgr: encoding for pub...\n");
1091 if (!req
->passwd
|| !strlen(req
->passwd
))
1094 /* host | time | user | passwd */
1095 const int bufLen
= snprintf(buf
, sizeof(buf
), "%s|%d|%s|%s",
1098 req
->user_name
? req
->user_name
: "",
1100 debug("cmgr: pre-encoded for pub: %s\n", buf
);
1102 const int encodedLen
= base64_encode_len(bufLen
);
1103 req
->pub_auth
= (char *) xmalloc(encodedLen
);
1104 base64_encode_str(req
->pub_auth
, encodedLen
, buf
, bufLen
);
1105 debug("cmgr: encoded: '%s'\n", req
->pub_auth
);
1109 decode_pub_auth(cachemgr_request
* req
)
1112 const char *host_name
;
1113 const char *time_str
;
1114 const char *user_name
;
1117 debug("cmgr: decoding pub: '%s'\n", safe_str(req
->pub_auth
));
1118 safe_free(req
->passwd
);
1120 if (!req
->pub_auth
|| strlen(req
->pub_auth
) < 4 + strlen(safe_str(req
->hostname
)))
1123 const int decodedLen
= base64_decode_len(req
->pub_auth
);
1124 buf
= (char*)xmalloc(decodedLen
);
1125 base64_decode(buf
, decodedLen
, req
->pub_auth
);
1127 debug("cmgr: length ok\n");
1129 /* parse ( a lot of memory leaks, but that is cachemgr style :) */
1130 if ((host_name
= strtok(buf
, "|")) == NULL
) {
1135 debug("cmgr: decoded host: '%s'\n", host_name
);
1137 if ((time_str
= strtok(NULL
, "|")) == NULL
) {
1142 debug("cmgr: decoded time: '%s' (now: %d)\n", time_str
, (int) now
);
1144 if ((user_name
= strtok(NULL
, "|")) == NULL
) {
1149 debug("cmgr: decoded uname: '%s'\n", user_name
);
1151 if ((passwd
= strtok(NULL
, "|")) == NULL
) {
1156 debug("cmgr: decoded passwd: '%s'\n", passwd
);
1158 /* verify freshness and validity */
1159 if (atoi(time_str
) + passwd_ttl
< now
) {
1164 if (strcasecmp(host_name
, req
->hostname
)) {
1169 debug("cmgr: verified auth. info.\n");
1172 safe_free(req
->user_name
);
1174 req
->user_name
= xstrdup(user_name
);
1176 req
->passwd
= xstrdup(passwd
);
1182 reset_auth(cachemgr_request
* req
)
1184 safe_free(req
->passwd
);
1185 safe_free(req
->pub_auth
);
1189 make_auth_header(const cachemgr_request
* req
)
1191 static char buf
[1024];
1192 size_t stringLength
= 0;
1197 int bufLen
= snprintf(buf
, sizeof(buf
), "%s:%s",
1198 req
->user_name
? req
->user_name
: "",
1201 int encodedLen
= base64_encode_len(bufLen
);
1202 if (encodedLen
<= 0)
1205 char *str64
= static_cast<char*>(xmalloc(encodedLen
));
1206 base64_encode_str(str64
, encodedLen
, buf
, bufLen
);
1208 stringLength
+= snprintf(buf
, sizeof(buf
), "Authorization: Basic %s\r\n", str64
);
1210 assert(stringLength
< sizeof(buf
));
1212 snprintf(&buf
[stringLength
], sizeof(buf
) - stringLength
, "Proxy-Authorization: Basic %s\r\n", str64
);
1219 check_target_acl(const char *hostname
, int port
)
1221 char config_line
[BUFSIZ
];
1224 fp
= fopen("cachemgr.conf", "r");
1227 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
1230 #ifdef CACHEMGR_HOSTNAME_DEFINED
1232 if (strcmp(hostname
, CACHEMGR_HOSTNAME
) == 0 && port
== CACHE_HTTP_PORT
)
1237 if (strcmp(hostname
, "localhost") == 0)
1240 if (strcmp(hostname
, getfullhostname()) == 0)
1248 while (fgets(config_line
, BUFSIZ
, fp
)) {
1250 strtok(config_line
, " \r\n\t");
1252 if (config_line
[0] == '#')
1255 if (config_line
[0] == '\0')
1258 if ((token
= strtok(config_line
, ":")) == NULL
)
1263 if (fnmatch(token
, hostname
, 0) != 0)
1268 if (strcmp(token
, hostname
) != 0)
1273 if ((token
= strtok(NULL
, ":")) != NULL
) {
1276 if (strcmp(token
, "*") == 0)
1278 ; /* Wildcard port specification */
1279 else if (strcmp(token
, "any") == 0)
1281 ; /* Wildcard port specification */
1282 else if (sscanf(token
, "%d", &i
) != 1)
1287 } else if (port
!= CACHE_HTTP_PORT
)