]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/cachemgr.cc
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
11 #include "getfullhostname.h"
12 #include "html_quote.h"
13 #include "ip/Address.h"
34 #include <gnumalloc.h>
48 #include <sys/param.h>
51 #include <sys/socket.h>
54 #include <netinet/in.h>
57 #include <arpa/inet.h>
86 #ifndef DEFAULT_CACHEMGR_CONFIG
87 #define DEFAULT_CACHEMGR_CONFIG "/etc/squid/cachemgr.conf"
103 * Static variables and constants
105 static const time_t passwd_ttl
= 60 * 60 * 3; /* in sec */
106 static const char *script_name
= "/cgi-bin/cachemgr.cgi";
107 static const char *progname
= NULL
;
111 * Function prototypes
113 static const char *safe_str(const char *str
);
114 static const char *xstrtok(char **str
, char del
);
115 static void print_trailer(void);
116 static void auth_html(const char *host
, int port
, const char *user_name
);
117 static void error_html(const char *msg
);
118 static char *menu_url(cachemgr_request
* req
, const char *action
);
119 static int parse_status_line(const char *sline
, const char **statusStr
);
120 static cachemgr_request
*read_request(void);
121 static char *read_get_request(void);
122 static char *read_post_request(void);
124 static void make_pub_auth(cachemgr_request
* req
);
125 static void decode_pub_auth(cachemgr_request
* req
);
126 static void reset_auth(cachemgr_request
* req
);
127 static const char *make_auth_header(const cachemgr_request
* req
);
129 static int check_target_acl(const char *hostname
, int port
);
132 static int s_iInitCount
= 0;
134 int Win32SockInit(void)
136 int iVersionRequested
;
140 if (s_iInitCount
> 0) {
143 } else if (s_iInitCount
< 0)
144 return (s_iInitCount
);
146 /* s_iInitCount == 0. Do the initailization */
147 iVersionRequested
= MAKEWORD(2, 0);
149 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
153 return (s_iInitCount
);
156 if (LOBYTE(wsaData
.wVersion
) != 2 ||
157 HIBYTE(wsaData
.wVersion
) != 0) {
160 return (s_iInitCount
);
164 return (s_iInitCount
);
167 void Win32SockCleanup(void)
169 if (--s_iInitCount
== 0)
178 safe_str(const char *str
)
180 return str
? str
: "";
183 /* relaxed number format */
185 is_number(const char *str
)
187 return strspn(str
, "\t -+01234567890./\n") == strlen(str
);
191 xstrtok(char **str
, char del
)
194 char *p
= strchr(*str
, del
);
207 while (len
&& xisspace(tok
[len
- 1]))
210 while (xisspace(*tok
))
221 printf("<HR noshade size=\"1px\">\n");
222 printf("<ADDRESS>\n");
223 printf("Generated %s, by %s/%s@%s\n",
224 mkrfc1123(now
), progname
, VERSION
, getfullhostname());
225 printf("</ADDRESS></BODY></HTML>\n");
229 auth_html(const char *host
, int port
, const char *user_name
)
237 if (!host
|| !strlen(host
))
240 fp
= fopen("cachemgr.conf", "r");
243 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
246 printf("X-Error: message=\"Unable to open config %s\"", DEFAULT_CACHEMGR_CONFIG
);
248 printf("Content-Type: text/html\r\n\r\n");
250 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
252 printf("<HTML><HEAD><TITLE>Cache Manager Interface</TITLE>\n");
254 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n");
256 printf("<script type=\"text/javascript\">\n");
257 printf("function TS(t, s) {\n");
258 printf(" var x = new XMLHttpRequest();\n");
259 printf(" x.open('GET', 'http' + s + '://' + t + '/squid-internal-mgr/', true);\n");
260 printf(" x.onreadystatechange=function() {\n");
261 printf(" if (x.readyState==4) {\n");
262 printf(" if ((x.status>=200 && x.status <= 299) || x.status==401) {\n");
263 printf(" var v = x.getResponseHeader('Server');\n");
264 printf(" if (v.substring(0,8) == 'squid/3.' && (v[8]=='H' || parseInt(v.substring(8)) >= 2)) {\n");
265 printf(" var d = document.getElementById('H' + s + 'mgr');\n");
266 printf(" if (d.innerHTML == '') d.innerHTML = '<h2>HTTP' + (s=='s'?'S':'') + ' Managed Proxies</h2>';\n");
267 printf(" d.innerHTML = d.innerHTML + '<p>Host: <a href=\"http' + s + '://' + t + '/squid-internal-mgr/\">' + t + '</a></p>';\n");
269 printf(" x.send(null);\n");
271 printf("</script>\n");
275 printf("<BODY><H1>Cache Manager Interface</H1>\n");
277 printf("<P>This is a WWW interface to the instrumentation interface\n");
279 printf("for the Squid object cache.</P>\n");
281 printf("<HR noshade size=\"1px\">\n");
283 printf("<div id=\"Hsmgr\"></div>\n");
284 printf("<div id=\"Hmgr\"></div>\n");
285 printf("<div id=\"Cmgr\">\n");
286 printf("<h2>CGI Managed Proxies</h2>\n");
287 printf("<FORM METHOD=\"POST\" ACTION=\"%s\">\n", script_name
);
289 printf("<TABLE BORDER=\"0\" CELLPADDING=\"10\" CELLSPACING=\"1\">\n");
293 char config_line
[BUFSIZ
];
295 while (fgets(config_line
, BUFSIZ
, fp
)) {
296 char *server
, *comment
;
297 strtok(config_line
, "\r\n");
299 if (config_line
[0] == '#')
302 if (config_line
[0] == '\0')
305 if ((server
= strtok(config_line
, " \t")) == NULL
)
308 if (strchr(server
, '*') || strchr(server
, '[') || strchr(server
, '?')) {
313 comment
= strtok(NULL
, "");
316 while (*comment
== ' ' || *comment
== '\t')
319 if (!comment
|| !*comment
)
323 printf("<TR><TH ALIGN=\"left\">Cache Server:</TH><TD><SELECT id=\"server\" NAME=\"server\">\n");
325 printf("<OPTION VALUE=\"%s\"%s>%s</OPTION>\n", server
, (servers
|| *host
) ? "" : " SELECTED", comment
);
330 if (need_host
== 1 && !*host
)
334 printf("<OPTION VALUE=\"\"%s>Other</OPTION>\n", (*host
) ? " SELECTED" : "");
336 printf("</SELECT></TR>\n");
343 if (need_host
== 1 && !*host
)
346 printf("<TR><TH ALIGN=\"left\">Cache Host:</TH><TD><INPUT NAME=\"host\" ");
348 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", host
);
350 printf("<TR><TH ALIGN=\"left\">Cache Port:</TH><TD><INPUT NAME=\"port\" ");
352 printf("size=\"30\" VALUE=\"%d\"></TD></TR>\n", port
);
355 printf("<TR><TH ALIGN=\"left\">Manager name:</TH><TD><INPUT NAME=\"user_name\" ");
357 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", user_name
);
359 printf("<TR><TH ALIGN=\"left\">Password:</TH><TD><INPUT TYPE=\"password\" NAME=\"passwd\" ");
361 printf("size=\"30\" VALUE=\"\"></TD></TR>\n");
363 printf("</TABLE><BR CLEAR=\"all\">\n");
365 printf("<INPUT TYPE=\"submit\" VALUE=\"Continue...\">\n");
367 printf("</FORM></div>\n");
369 printf("<script type=\"text/javascript\">\n");
370 printf("var s = document.getElementById(\"server\");\n");
371 printf("for (var i = 0; i < s.childElementCount; i++) {\n");
372 printf(" TS(s.children[i].value, '');\n");
373 printf(" TS(s.children[i].value, 's');\n");
374 printf("}</script>\n");
380 error_html(const char *msg
)
382 printf("Content-Type: text/html\r\n\r\n");
383 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
384 printf("<HTML><HEAD><TITLE>Cache Manager Error</TITLE>\n");
385 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
386 printf("<BODY><H1>Cache Manager Error</H1>\n");
387 printf("<P>\n%s</P>\n", html_quote(msg
));
391 /* returns http status extracted from status line or -1 on parsing failure */
393 parse_status_line(const char *sline
, const char **statusStr
)
395 const char *sp
= strchr(sline
, ' ');
400 if (strncasecmp(sline
, "HTTP/", 5) || !sp
)
403 while (xisspace(*++sp
));
414 menu_url(cachemgr_request
* req
, const char *action
)
416 static char url
[1024];
417 snprintf(url
, sizeof(url
), "%s?host=%s&port=%d&user_name=%s&operation=%s&auth=%s",
421 safe_str(req
->user_name
),
423 safe_str(req
->pub_auth
));
428 munge_menu_line(MemBuf
&out
, const char *buf
, cachemgr_request
* req
)
437 const char bufLen
= strlen(buf
);
438 if (bufLen
< 1 || *buf
!= ' ') {
439 out
.append(buf
, bufLen
);
443 buf_copy
= x
= xstrndup(buf
, bufLen
+1);
445 a
= xstrtok(&x
, '\t');
447 d
= xstrtok(&x
, '\t');
449 p
= xstrtok(&x
, '\t');
451 a_url
= xstrdup(menu_url(req
, a
));
453 /* no reason to give a url for a disabled action */
454 if (!strcmp(p
, "disabled"))
455 out
.appendf("<LI type=\"circle\">%s (disabled)<A HREF=\"%s\">.</A>\n", d
, a_url
);
457 /* disable a hidden action (requires a password, but password is not in squid.conf) */
458 if (!strcmp(p
, "hidden"))
459 out
.appendf("<LI type=\"circle\">%s (hidden)<A HREF=\"%s\">.</A>\n", d
, a_url
);
461 /* disable link if authentication is required and we have no password */
462 if (!strcmp(p
, "protected") && !req
->passwd
)
463 out
.appendf("<LI type=\"circle\">%s (requires <a href=\"%s\">authentication</a>)<A HREF=\"%s\">.</A>\n",
464 d
, menu_url(req
, "authenticate"), a_url
);
466 /* highlight protected but probably available entries */
467 if (!strcmp(p
, "protected"))
468 out
.appendf("<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n",
471 /* public entry or unknown type of protection */
473 out
.appendf("<LI type=\"disk\"><A HREF=\"%s\">%s</A>\n", a_url
, d
);
481 munge_other_line(MemBuf
&out
, const char *buf
, cachemgr_request
*)
483 static const char *ttags
[] = {"td", "th"};
485 static int table_line_num
= 0;
486 static int next_is_header
= 0;
491 /* does it look like a table? */
493 if (!strchr(buf
, '\t') || *buf
== '\t') {
494 /* nope, just text */
496 out
.append("</table>\n<pre>", 14);
497 out
.appendf("%s", html_quote(buf
));
502 /* start html table */
503 if (!table_line_num
) {
504 out
.append("</pre><table cellpadding=\"2\" cellspacing=\"1\">\n", 46);
509 is_header
= (!table_line_num
|| next_is_header
) && !strchr(buf
, ':') && !is_number(buf
);
511 ttag
= ttags
[is_header
];
514 out
.append("<tr>", 4);
516 /* substitute '\t' */
517 buf_copy
= x
= xstrdup(buf
);
519 if ((p
= strchr(x
, '\n')))
522 while (x
&& strlen(x
)) {
524 const char *cell
= xstrtok(&x
, '\t');
526 while (x
&& *x
== '\t') {
531 out
.appendf("<%s colspan=\"%d\" align=\"%s\">%s</%s>",
533 is_header
? "center" : is_number(cell
) ? "right" : "left",
534 html_quote(cell
), ttag
);
539 out
.append("</tr>\n", 6);
540 next_is_header
= is_header
&& strstr(buf
, "\t\t");
545 munge_action_line(const char *_buf
, cachemgr_request
* req
)
547 static char html
[2 * 1024];
548 char *buf
= xstrdup(_buf
);
550 const char *action
, *description
;
553 if ((p
= strchr(x
, '\n')))
555 action
= xstrtok(&x
, '\t');
560 description
= xstrtok(&x
, '\t');
562 description
= action
;
563 snprintf(html
, sizeof(html
), " <a href=\"%s\">%s</a>", menu_url(req
, action
), description
);
569 read_reply(int s
, cachemgr_request
* req
)
575 char *tmpfile
= tempnam(NULL
, "tmp0000");
576 FILE *fp
= fopen(tmpfile
, "w+");
579 FILE *fp
= fdopen(s
, "r");
581 /* interpretation states */
583 isStatusLine
, isHeaders
, isActions
, isBodyStart
, isBody
, isForward
, isEof
, isForwardEof
, isSuccess
, isError
584 } istate
= isStatusLine
;
586 const char *action
= req
->action
;
587 const char *statusStr
= NULL
;
590 if (0 == strlen(req
->action
))
592 else if (0 == strcasecmp(req
->action
, "menu"))
610 while ((reply
=recv(s
, buf
, sizeof(buf
), 0)) > 0)
611 fwrite(buf
, 1, reply
, fp
);
620 /* read reply interpreting one line at a time depending on state */
621 while (istate
< isEof
) {
622 if (!fgets(buf
, sizeof(buf
), fp
))
623 istate
= istate
== isForward
? isForwardEof
: isEof
;
628 /* get HTTP status */
629 /* uncomment the following if you want to debug headers */
630 /* fputs("\r\n\r\n", stdout); */
631 status
= parse_status_line(buf
, &statusStr
);
632 istate
= status
== 200 ? isHeaders
: isForward
;
633 /* if cache asks for authentication, we have to reset our info */
635 if (status
== 401 || status
== 407) {
637 status
= 403; /* Forbiden, see comments in case isForward: */
640 /* this is a way to pass HTTP status to the Web server */
642 printf("Status: %d %s", status
, statusStr
); /* statusStr has '\n' */
647 /* forward header field */
648 if (!strcmp(buf
, "\r\n")) { /* end of headers */
649 fputs("Content-Type: text/html\r\n", stdout
); /* add our type */
650 istate
= isBodyStart
;
653 if (strncasecmp(buf
, "Content-Type:", 13)) /* filter out their type */
659 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
661 printf("<HTML><HEAD><TITLE>CacheMgr@%s: %s</TITLE>\n",
662 req
->hostname
, action
);
664 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");
666 printf("</HEAD><BODY>\n");
669 printf("<H2><a href=\"%s\">Cache Manager</a> menu for %s:</H2>",
670 menu_url(req
, "authenticate"), req
->hostname
);
673 printf("<P><A HREF=\"%s\">%s</A>\n<HR noshade size=\"1px\">\n",
674 menu_url(req
, "menu"), "Cache Manager menu");
679 /* yes, fall through, we do not want to loose the first line */
682 if (strncmp(buf
, "action:", 7) == 0) {
684 fputs(munge_action_line(buf
+ 7, req
), stdout
);
690 printf("<HR noshade size=\"1px\">\n");
695 /* yes, fall through, we do not want to loose the first line */
699 /* interpret [and reformat] cache response */
703 munge_menu_line(out
, buf
, req
);
705 munge_other_line(out
, buf
, req
);
707 fputs(out
.buf
, stdout
);
712 /* forward: no modifications allowed */
714 * Note: we currently do not know any way to get browser.reply to
715 * 401 to .cgi because web server filters out all auth info. Thus we
716 * disable authentication headers for now.
718 if (!strncasecmp(buf
, "WWW-Authenticate:", 17) || !strncasecmp(buf
, "Proxy-Authenticate:", 19)); /* skip */
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
, sizeof(buf
), "target %s:%d not allowed in cachemgr.conf\n", req
->hostname
, req
->port
);
802 S
= *gethostbyname(req
->hostname
);
804 if ( !S
.isAnyAddr() ) {
806 } else if ((S
= req
->hostname
))
809 snprintf(buf
, sizeof(buf
), "Unknown host: %s\n", req
->hostname
);
819 if ((s
= socket( AI
->ai_family
, SOCK_STREAM
, 0)) < 0) {
821 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
824 snprintf(buf
, sizeof(buf
), "socket: %s\n", xstrerr(xerrno
));
826 Ip::Address::FreeAddr(AI
);
830 if (connect(s
, AI
->ai_addr
, AI
->ai_addrlen
) < 0) {
832 snprintf(buf
, sizeof(buf
), "connect %s: %s\n", S
.toUrl(ipbuf
,MAX_IPSTRLEN
), xstrerr(xerrno
));
834 Ip::Address::FreeAddr(AI
);
839 Ip::Address::FreeAddr(AI
);
841 l
= snprintf(buf
, sizeof(buf
),
842 "GET cache_object://%s/%s%s%s HTTP/1.0\r\n"
843 "User-Agent: cachemgr.cgi/%s\r\n"
845 "%s" /* Authentication info or nothing */
849 req
->workers
? "?workers=" : (req
->processes
? "?processes=" : ""),
850 req
->workers
? req
->workers
: (req
->processes
? req
->processes
: ""),
852 make_auth_header(req
));
853 if (write(s
, buf
, l
) < 0) {
854 fprintf(stderr
,"ERROR: (%d) writing request: '%s'\n", errno
, buf
);
856 debug("wrote request: '%s'\n", buf
);
858 return read_reply(s
, req
);
862 main(int argc
, char *argv
[])
865 cachemgr_request
*req
;
871 atexit(Win32SockCleanup
);
872 _setmode( _fileno( stdin
), _O_BINARY
);
873 _setmode( _fileno( stdout
), _O_BINARY
);
876 if ((s
= strrchr(argv
[0], '\\')))
879 if ((s
= strrchr(argv
[0], '/')))
882 progname
= xstrdup(s
+ 1);
884 progname
= xstrdup(argv
[0]);
886 if ((s
= getenv("SCRIPT_NAME")) != NULL
)
887 script_name
= xstrdup(s
);
890 while (argc
> 1 && args
[1][0] == '-') {
891 // const char *value = "";
892 char option
= args
[1][1];
898 #if 0 // unused for now.
899 if (strlen(args
[1]) > 2) {
901 } else if (argc
> 2) {
914 req
= read_request();
916 return process_request(req
);
920 read_post_request(void)
924 if ((s
= getenv("REQUEST_METHOD")) == NULL
)
927 if (0 != strcasecmp(s
, "POST"))
930 if ((s
= getenv("CONTENT_LENGTH")) == NULL
)
933 if (*s
== '-') // negative length content huh?
938 char *endptr
= s
+ strlen(s
);
939 if ((len
= strtoll(s
, &endptr
, 10)) <= 0)
942 // limit the input to something reasonable.
943 // 4KB should be enough for the GET/POST data length, but may be extended.
944 size_t bufLen
= (len
< 4096 ? len
: 4095);
945 char *buf
= (char *)xmalloc(bufLen
+ 1);
947 size_t readLen
= fread(buf
, 1, bufLen
, stdin
);
955 // purge the remainder of the request entity
956 while (len
> 0 && readLen
) {
958 readLen
= fread(temp
, 1, 65535, stdin
);
966 read_get_request(void)
970 if ((s
= getenv("QUERY_STRING")) == NULL
)
976 static cachemgr_request
*
981 cachemgr_request
*req
;
986 if ((buf
= read_post_request()) != NULL
)
988 else if ((buf
= read_get_request()) != NULL
)
995 if (strlen(buf
) == 0 || strlen(buf
) == 4000)
998 if (strlen(buf
) == 0)
1005 req
= (cachemgr_request
*)xcalloc(1, sizeof(cachemgr_request
));
1007 for (s
= strtok(buf
, "&"); s
!= NULL
; s
= strtok(NULL
, "&")) {
1011 if ((q
= strchr(t
, '=')) == NULL
)
1017 rfc1738_unescape(t
);
1019 rfc1738_unescape(q
);
1021 if (0 == strcmp(t
, "server") && strlen(q
))
1022 req
->server
= xstrdup(q
);
1023 else if (0 == strcmp(t
, "host") && strlen(q
))
1024 req
->hostname
= xstrdup(q
);
1025 else if (0 == strcmp(t
, "port") && strlen(q
))
1026 req
->port
= atoi(q
);
1027 else if (0 == strcmp(t
, "user_name") && strlen(q
))
1028 req
->user_name
= xstrdup(q
);
1029 else if (0 == strcmp(t
, "passwd") && strlen(q
))
1030 req
->passwd
= xstrdup(q
);
1031 else if (0 == strcmp(t
, "auth") && strlen(q
))
1032 req
->pub_auth
= xstrdup(q
), decode_pub_auth(req
);
1033 else if (0 == strcmp(t
, "operation"))
1034 req
->action
= xstrdup(q
);
1035 else if (0 == strcmp(t
, "workers") && strlen(q
))
1036 req
->workers
= xstrdup(q
);
1037 else if (0 == strcmp(t
, "processes") && strlen(q
))
1038 req
->processes
= xstrdup(q
);
1042 if (req
->server
&& !req
->hostname
) {
1044 req
->hostname
= strtok(req
->server
, ":");
1046 if ((p
= strtok(NULL
, ":")))
1047 req
->port
= atoi(p
);
1051 debug("cmgr: got req: host: '%s' port: %d uname: '%s' passwd: '%s' auth: '%s' oper: '%s' workers: '%s' processes: '%s'\n",
1052 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
));
1056 /* Routines to support authentication */
1059 * Encodes auth info into a "public" form.
1060 * Currently no powerful encryption is used.
1063 make_pub_auth(cachemgr_request
* req
)
1065 static char buf
[1024];
1066 safe_free(req
->pub_auth
);
1067 debug("cmgr: encoding for pub...\n");
1069 if (!req
->passwd
|| !strlen(req
->passwd
))
1072 /* host | time | user | passwd */
1073 const int bufLen
= snprintf(buf
, sizeof(buf
), "%s|%d|%s|%s",
1076 req
->user_name
? req
->user_name
: "",
1078 debug("cmgr: pre-encoded for pub: %s\n", buf
);
1080 const int encodedLen
= base64_encode_len(bufLen
);
1081 req
->pub_auth
= (char *) xmalloc(encodedLen
);
1082 struct base64_encode_ctx ctx
;
1083 base64_encode_init(&ctx
);
1084 size_t blen
= base64_encode_update(&ctx
, req
->pub_auth
, bufLen
, reinterpret_cast<uint8_t*>(buf
));
1085 blen
+= base64_encode_final(&ctx
, req
->pub_auth
+ blen
);
1086 req
->pub_auth
[blen
] = '\0';
1087 debug("cmgr: encoded: '%s'\n", req
->pub_auth
);
1091 decode_pub_auth(cachemgr_request
* req
)
1094 const char *host_name
;
1095 const char *time_str
;
1096 const char *user_name
;
1099 debug("cmgr: decoding pub: '%s'\n", safe_str(req
->pub_auth
));
1100 safe_free(req
->passwd
);
1102 if (!req
->pub_auth
|| strlen(req
->pub_auth
) < 4 + strlen(safe_str(req
->hostname
)))
1105 size_t decodedLen
= BASE64_DECODE_LENGTH(strlen(req
->pub_auth
));
1106 buf
= (char*)xmalloc(decodedLen
);
1107 struct base64_decode_ctx ctx
;
1108 base64_decode_init(&ctx
);
1109 if (!base64_decode_update(&ctx
, &decodedLen
, reinterpret_cast<uint8_t*>(buf
), strlen(req
->pub_auth
), req
->pub_auth
) ||
1110 !base64_decode_final(&ctx
)) {
1111 debug("cmgr: base64 decode failure. Incomplete auth token string.\n");
1116 debug("cmgr: length ok\n");
1118 /* parse ( a lot of memory leaks, but that is cachemgr style :) */
1119 if ((host_name
= strtok(buf
, "|")) == NULL
) {
1124 debug("cmgr: decoded host: '%s'\n", host_name
);
1126 if ((time_str
= strtok(NULL
, "|")) == NULL
) {
1131 debug("cmgr: decoded time: '%s' (now: %d)\n", time_str
, (int) now
);
1133 if ((user_name
= strtok(NULL
, "|")) == NULL
) {
1138 debug("cmgr: decoded uname: '%s'\n", user_name
);
1140 if ((passwd
= strtok(NULL
, "|")) == NULL
) {
1145 debug("cmgr: decoded passwd: '%s'\n", passwd
);
1147 /* verify freshness and validity */
1148 if (atoi(time_str
) + passwd_ttl
< now
) {
1153 if (strcasecmp(host_name
, req
->hostname
)) {
1158 debug("cmgr: verified auth. info.\n");
1161 safe_free(req
->user_name
);
1163 req
->user_name
= xstrdup(user_name
);
1165 req
->passwd
= xstrdup(passwd
);
1171 reset_auth(cachemgr_request
* req
)
1173 safe_free(req
->passwd
);
1174 safe_free(req
->pub_auth
);
1178 make_auth_header(const cachemgr_request
* req
)
1180 static char buf
[1024];
1181 size_t stringLength
= 0;
1186 int bufLen
= snprintf(buf
, sizeof(buf
), "%s:%s",
1187 req
->user_name
? req
->user_name
: "",
1190 int encodedLen
= base64_encode_len(bufLen
);
1191 if (encodedLen
<= 0)
1194 char *str64
= static_cast<char *>(xmalloc(encodedLen
));
1195 struct base64_encode_ctx ctx
;
1196 base64_encode_init(&ctx
);
1197 size_t blen
= base64_encode_update(&ctx
, str64
, bufLen
, reinterpret_cast<uint8_t*>(buf
));
1198 blen
+= base64_encode_final(&ctx
, str64
+blen
);
1201 stringLength
+= snprintf(buf
, sizeof(buf
), "Authorization: Basic %.*s\r\n", (int)blen
, str64
);
1203 assert(stringLength
< sizeof(buf
));
1205 snprintf(&buf
[stringLength
], sizeof(buf
) - stringLength
, "Proxy-Authorization: Basic %.*s\r\n", (int)blen
, str64
);
1212 check_target_acl(const char *hostname
, int port
)
1214 char config_line
[BUFSIZ
];
1217 fp
= fopen("cachemgr.conf", "r");
1220 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
1223 #ifdef CACHEMGR_HOSTNAME_DEFINED
1225 if (strcmp(hostname
, CACHEMGR_HOSTNAME
) == 0 && port
== CACHE_HTTP_PORT
)
1230 if (strcmp(hostname
, "localhost") == 0)
1233 if (strcmp(hostname
, getfullhostname()) == 0)
1241 while (fgets(config_line
, BUFSIZ
, fp
)) {
1243 strtok(config_line
, " \r\n\t");
1245 if (config_line
[0] == '#')
1248 if (config_line
[0] == '\0')
1251 if ((token
= strtok(config_line
, ":")) == NULL
)
1256 if (fnmatch(token
, hostname
, 0) != 0)
1261 if (strcmp(token
, hostname
) != 0)
1266 if ((token
= strtok(NULL
, ":")) != NULL
) {
1269 if (strcmp(token
, "*") == 0)
1271 ; /* Wildcard port specification */
1272 else if (strcmp(token
, "any") == 0)
1274 ; /* Wildcard port specification */
1275 else if (sscanf(token
, "%d", &i
) != 1)
1280 } else if (port
!= CACHE_HTTP_PORT
)