]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/cachemgr.cc
2 * Copyright (C) 1996-2014 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"
33 #include <gnumalloc.h>
47 #include <sys/param.h>
50 #include <sys/socket.h>
53 #include <netinet/in.h>
56 #include <arpa/inet.h>
85 #ifndef DEFAULT_CACHEMGR_CONFIG
86 #define DEFAULT_CACHEMGR_CONFIG "/etc/squid/cachemgr.conf"
102 * Static variables and constants
104 static const time_t passwd_ttl
= 60 * 60 * 3; /* in sec */
105 static const char *script_name
= "/cgi-bin/cachemgr.cgi";
106 static const char *progname
= NULL
;
110 * Function prototypes
112 static const char *safe_str(const char *str
);
113 static const char *xstrtok(char **str
, char del
);
114 static void print_trailer(void);
115 static void auth_html(const char *host
, int port
, const char *user_name
);
116 static void error_html(const char *msg
);
117 static char *menu_url(cachemgr_request
* req
, const char *action
);
118 static int parse_status_line(const char *sline
, const char **statusStr
);
119 static cachemgr_request
*read_request(void);
120 static char *read_get_request(void);
121 static char *read_post_request(void);
123 static void make_pub_auth(cachemgr_request
* req
);
124 static void decode_pub_auth(cachemgr_request
* req
);
125 static void reset_auth(cachemgr_request
* req
);
126 static const char *make_auth_header(const cachemgr_request
* req
);
128 static int check_target_acl(const char *hostname
, int port
);
131 static int s_iInitCount
= 0;
133 int Win32SockInit(void)
135 int iVersionRequested
;
139 if (s_iInitCount
> 0) {
142 } else if (s_iInitCount
< 0)
143 return (s_iInitCount
);
145 /* s_iInitCount == 0. Do the initailization */
146 iVersionRequested
= MAKEWORD(2, 0);
148 err
= WSAStartup((WORD
) iVersionRequested
, &wsaData
);
152 return (s_iInitCount
);
155 if (LOBYTE(wsaData
.wVersion
) != 2 ||
156 HIBYTE(wsaData
.wVersion
) != 0) {
159 return (s_iInitCount
);
163 return (s_iInitCount
);
166 void Win32SockCleanup(void)
168 if (--s_iInitCount
== 0)
177 safe_str(const char *str
)
179 return str
? str
: "";
182 /* relaxed number format */
184 is_number(const char *str
)
186 return strspn(str
, "\t -+01234567890./\n") == strlen(str
);
190 xstrtok(char **str
, char del
)
193 char *p
= strchr(*str
, del
);
206 while (len
&& xisspace(tok
[len
- 1]))
209 while (xisspace(*tok
))
220 printf("<HR noshade size=\"1px\">\n");
221 printf("<ADDRESS>\n");
222 printf("Generated %s, by %s/%s@%s\n",
223 mkrfc1123(now
), progname
, VERSION
, getfullhostname());
224 printf("</ADDRESS></BODY></HTML>\n");
228 auth_html(const char *host
, int port
, const char *user_name
)
236 if (!host
|| !strlen(host
))
239 fp
= fopen("cachemgr.conf", "r");
242 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
245 printf("X-Error: message=\"Unable to open config %s\"", DEFAULT_CACHEMGR_CONFIG
);
247 printf("Content-Type: text/html\r\n\r\n");
249 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
251 printf("<HTML><HEAD><TITLE>Cache Manager Interface</TITLE>\n");
253 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n");
255 printf("<script type=\"text/javascript\">\n");
256 printf("function TS(t, s) {\n");
257 printf(" var x = new XMLHttpRequest();\n");
258 printf(" x.open('GET', 'http' + s + '://' + t + '/squid-internal-mgr/', true);\n");
259 printf(" x.onreadystatechange=function() {\n");
260 printf(" if (x.readyState==4) {\n");
261 printf(" if ((x.status>=200 && x.status <= 299) || x.status==401) {\n");
262 printf(" var v = x.getResponseHeader('Server');\n");
263 printf(" if (v.substring(0,8) == 'squid/3.' && (v[8]=='H' || parseInt(v.substring(8)) >= 2)) {\n");
264 printf(" var d = document.getElementById('H' + s + 'mgr');\n");
265 printf(" if (d.innerHTML == '') d.innerHTML = '<h2>HTTP' + (s=='s'?'S':'') + ' Managed Proxies</h2>';\n");
266 printf(" d.innerHTML = d.innerHTML + '<p>Host: <a href=\"http' + s + '://' + t + '/squid-internal-mgr/\">' + t + '</a></p>';\n");
268 printf(" x.send(null);\n");
270 printf("</script>\n");
274 printf("<BODY><H1>Cache Manager Interface</H1>\n");
276 printf("<P>This is a WWW interface to the instrumentation interface\n");
278 printf("for the Squid object cache.</P>\n");
280 printf("<HR noshade size=\"1px\">\n");
282 printf("<div id=\"Hsmgr\"></div>\n");
283 printf("<div id=\"Hmgr\"></div>\n");
284 printf("<div id=\"Cmgr\">\n");
285 printf("<h2>CGI Managed Proxies</h2>\n");
286 printf("<FORM METHOD=\"POST\" ACTION=\"%s\">\n", script_name
);
288 printf("<TABLE BORDER=\"0\" CELLPADDING=\"10\" CELLSPACING=\"1\">\n");
292 char config_line
[BUFSIZ
];
294 while (fgets(config_line
, BUFSIZ
, fp
)) {
295 char *server
, *comment
;
296 strtok(config_line
, "\r\n");
298 if (config_line
[0] == '#')
301 if (config_line
[0] == '\0')
304 if ((server
= strtok(config_line
, " \t")) == NULL
)
307 if (strchr(server
, '*') || strchr(server
, '[') || strchr(server
, '?')) {
312 comment
= strtok(NULL
, "");
315 while (*comment
== ' ' || *comment
== '\t')
318 if (!comment
|| !*comment
)
322 printf("<TR><TH ALIGN=\"left\">Cache Server:</TH><TD><SELECT id=\"server\" NAME=\"server\">\n");
324 printf("<OPTION VALUE=\"%s\"%s>%s</OPTION>\n", server
, (servers
|| *host
) ? "" : " SELECTED", comment
);
329 if (need_host
== 1 && !*host
)
333 printf("<OPTION VALUE=\"\"%s>Other</OPTION>\n", (*host
) ? " SELECTED" : "");
335 printf("</SELECT></TR>\n");
342 if (need_host
== 1 && !*host
)
345 printf("<TR><TH ALIGN=\"left\">Cache Host:</TH><TD><INPUT NAME=\"host\" ");
347 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", host
);
349 printf("<TR><TH ALIGN=\"left\">Cache Port:</TH><TD><INPUT NAME=\"port\" ");
351 printf("size=\"30\" VALUE=\"%d\"></TD></TR>\n", port
);
354 printf("<TR><TH ALIGN=\"left\">Manager name:</TH><TD><INPUT NAME=\"user_name\" ");
356 printf("size=\"30\" VALUE=\"%s\"></TD></TR>\n", user_name
);
358 printf("<TR><TH ALIGN=\"left\">Password:</TH><TD><INPUT TYPE=\"password\" NAME=\"passwd\" ");
360 printf("size=\"30\" VALUE=\"\"></TD></TR>\n");
362 printf("</TABLE><BR CLEAR=\"all\">\n");
364 printf("<INPUT TYPE=\"submit\" VALUE=\"Continue...\">\n");
366 printf("</FORM></div>\n");
368 printf("<script type=\"text/javascript\">\n");
369 printf("var s = document.getElementById(\"server\");\n");
370 printf("for (var i = 0; i < s.childElementCount; i++) {\n");
371 printf(" TS(s.children[i].value, '');\n");
372 printf(" TS(s.children[i].value, 's');\n");
373 printf("}</script>\n");
379 error_html(const char *msg
)
381 printf("Content-Type: text/html\r\n\r\n");
382 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
383 printf("<HTML><HEAD><TITLE>Cache Manager Error</TITLE>\n");
384 printf("<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE></HEAD>\n");
385 printf("<BODY><H1>Cache Manager Error</H1>\n");
386 printf("<P>\n%s</P>\n", html_quote(msg
));
390 /* returns http status extracted from status line or -1 on parsing failure */
392 parse_status_line(const char *sline
, const char **statusStr
)
394 const char *sp
= strchr(sline
, ' ');
399 if (strncasecmp(sline
, "HTTP/", 5) || !sp
)
402 while (xisspace(*++sp
));
413 menu_url(cachemgr_request
* req
, const char *action
)
415 static char url
[1024];
416 snprintf(url
, sizeof(url
), "%s?host=%s&port=%d&user_name=%s&operation=%s&auth=%s",
420 safe_str(req
->user_name
),
422 safe_str(req
->pub_auth
));
427 munge_menu_line(const char *buf
, cachemgr_request
* req
)
435 static char html
[2 * 1024];
443 buf_copy
= x
= xstrdup(buf
);
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 snprintf(html
, sizeof(html
), "<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 snprintf(html
, sizeof(html
), "<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 snprintf(html
, sizeof(html
), "<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 snprintf(html
, sizeof(html
), "<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n",
471 /* public entry or unknown type of protection */
473 snprintf(html
, sizeof(html
), "<LI type=\"disk\"><A HREF=\"%s\">%s</A>\n", a_url
, d
);
483 munge_other_line(const char *buf
, cachemgr_request
*)
485 static const char *ttags
[] = {"td", "th"};
487 static char html
[4096];
488 static int table_line_num
= 0;
489 static int next_is_header
= 0;
495 /* does it look like a table? */
497 if (!strchr(buf
, '\t') || *buf
== '\t') {
498 /* nope, just text */
499 snprintf(html
, sizeof(html
), "%s%s",
500 table_line_num
? "</table>\n<pre>" : "", html_quote(buf
));
505 /* start html table */
506 if (!table_line_num
) {
507 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "</pre><table cellpadding=\"2\" cellspacing=\"1\">\n");
512 is_header
= (!table_line_num
|| next_is_header
) && !strchr(buf
, ':') && !is_number(buf
);
514 ttag
= ttags
[is_header
];
517 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<tr>");
519 /* substitute '\t' */
520 buf_copy
= x
= xstrdup(buf
);
522 if ((p
= strchr(x
, '\n')))
525 while (x
&& strlen(x
)) {
527 const char *cell
= xstrtok(&x
, '\t');
529 while (x
&& *x
== '\t') {
534 l
+= snprintf(html
+ l
, sizeof(html
) - l
, "<%s colspan=\"%d\" align=\"%s\">%s</%s>",
536 is_header
? "center" : is_number(cell
) ? "right" : "left",
537 html_quote(cell
), ttag
);
542 snprintf(html
+ l
, sizeof(html
) - l
, "</tr>\n");
543 next_is_header
= is_header
&& strstr(buf
, "\t\t");
549 munge_action_line(const char *_buf
, cachemgr_request
* req
)
551 static char html
[2 * 1024];
552 char *buf
= xstrdup(_buf
);
554 const char *action
, *description
;
557 if ((p
= strchr(x
, '\n')))
559 action
= xstrtok(&x
, '\t');
564 description
= xstrtok(&x
, '\t');
566 description
= action
;
567 snprintf(html
, sizeof(html
), " <a href=\"%s\">%s</a>", menu_url(req
, action
), description
);
573 read_reply(int s
, cachemgr_request
* req
)
579 char *tmpfile
= tempnam(NULL
, "tmp0000");
580 FILE *fp
= fopen(tmpfile
, "w+");
583 FILE *fp
= fdopen(s
, "r");
585 /* interpretation states */
587 isStatusLine
, isHeaders
, isActions
, isBodyStart
, isBody
, isForward
, isEof
, isForwardEof
, isSuccess
, isError
588 } istate
= isStatusLine
;
590 const char *action
= req
->action
;
591 const char *statusStr
= NULL
;
594 if (0 == strlen(req
->action
))
596 else if (0 == strcasecmp(req
->action
, "menu"))
614 while ((reply
=recv(s
, buf
, sizeof(buf
), 0)) > 0)
615 fwrite(buf
, 1, reply
, fp
);
624 /* read reply interpreting one line at a time depending on state */
625 while (istate
< isEof
) {
626 if (!fgets(buf
, sizeof(buf
), fp
))
627 istate
= istate
== isForward
? isForwardEof
: isEof
;
632 /* get HTTP status */
633 /* uncomment the following if you want to debug headers */
634 /* fputs("\r\n\r\n", stdout); */
635 status
= parse_status_line(buf
, &statusStr
);
636 istate
= status
== 200 ? isHeaders
: isForward
;
637 /* if cache asks for authentication, we have to reset our info */
639 if (status
== 401 || status
== 407) {
641 status
= 403; /* Forbiden, see comments in case isForward: */
644 /* this is a way to pass HTTP status to the Web server */
646 printf("Status: %d %s", status
, statusStr
); /* statusStr has '\n' */
651 /* forward header field */
652 if (!strcmp(buf
, "\r\n")) { /* end of headers */
653 fputs("Content-Type: text/html\r\n", stdout
); /* add our type */
654 istate
= isBodyStart
;
657 if (strncasecmp(buf
, "Content-Type:", 13)) /* filter out their type */
663 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
665 printf("<HTML><HEAD><TITLE>CacheMgr@%s: %s</TITLE>\n",
666 req
->hostname
, action
);
668 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");
670 printf("</HEAD><BODY>\n");
673 printf("<H2><a href=\"%s\">Cache Manager</a> menu for %s:</H2>",
674 menu_url(req
, "authenticate"), req
->hostname
);
677 printf("<P><A HREF=\"%s\">%s</A>\n<HR noshade size=\"1px\">\n",
678 menu_url(req
, "menu"), "Cache Manager menu");
683 /* yes, fall through, we do not want to loose the first line */
686 if (strncmp(buf
, "action:", 7) == 0) {
688 fputs(munge_action_line(buf
+ 7, req
), stdout
);
694 printf("<HR noshade size=\"1px\">\n");
699 /* yes, fall through, we do not want to loose the first line */
702 /* interpret [and reformat] cache response */
705 fputs(munge_menu_line(buf
, req
), stdout
);
707 fputs(munge_other_line(buf
, req
), 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) {
823 snprintf(buf
, sizeof(buf
), "socket: %s\n", xstrerror());
825 Ip::Address::FreeAddr(AI
);
829 if (connect(s
, AI
->ai_addr
, AI
->ai_addrlen
) < 0) {
830 snprintf(buf
, sizeof(buf
), "connect %s: %s\n",
831 S
.toUrl(ipbuf
,MAX_IPSTRLEN
),
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
, "&")) {
1010 if ((q
= strchr(t
, '=')) == NULL
)
1016 rfc1738_unescape(t
);
1018 rfc1738_unescape(q
);
1020 if (0 == strcmp(t
, "server") && strlen(q
))
1021 req
->server
= xstrdup(q
);
1022 else if (0 == strcmp(t
, "host") && strlen(q
))
1023 req
->hostname
= xstrdup(q
);
1024 else if (0 == strcmp(t
, "port") && strlen(q
))
1025 req
->port
= atoi(q
);
1026 else if (0 == strcmp(t
, "user_name") && strlen(q
))
1027 req
->user_name
= xstrdup(q
);
1028 else if (0 == strcmp(t
, "passwd") && strlen(q
))
1029 req
->passwd
= xstrdup(q
);
1030 else if (0 == strcmp(t
, "auth") && strlen(q
))
1031 req
->pub_auth
= xstrdup(q
), decode_pub_auth(req
);
1032 else if (0 == strcmp(t
, "operation"))
1033 req
->action
= xstrdup(q
);
1034 else if (0 == strcmp(t
, "workers") && strlen(q
))
1035 req
->workers
= xstrdup(q
);
1036 else if (0 == strcmp(t
, "processes") && strlen(q
))
1037 req
->processes
= xstrdup(q
);
1040 if (req
->server
&& !req
->hostname
) {
1042 req
->hostname
= strtok(req
->server
, ":");
1044 if ((p
= strtok(NULL
, ":")))
1045 req
->port
= atoi(p
);
1049 debug("cmgr: got req: host: '%s' port: %d uname: '%s' passwd: '%s' auth: '%s' oper: '%s' workers: '%s' processes: '%s'\n",
1050 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
));
1054 /* Routines to support authentication */
1057 * Encodes auth info into a "public" form.
1058 * Currently no powerful encryption is used.
1061 make_pub_auth(cachemgr_request
* req
)
1063 static char buf
[1024];
1064 safe_free(req
->pub_auth
);
1065 debug("cmgr: encoding for pub...\n");
1067 if (!req
->passwd
|| !strlen(req
->passwd
))
1070 /* host | time | user | passwd */
1071 const int bufLen
= snprintf(buf
, sizeof(buf
), "%s|%d|%s|%s",
1074 req
->user_name
? req
->user_name
: "",
1076 debug("cmgr: pre-encoded for pub: %s\n", buf
);
1078 const int encodedLen
= base64_encode_len(bufLen
);
1079 req
->pub_auth
= (char *) xmalloc(encodedLen
);
1080 struct base64_encode_ctx ctx
;
1081 base64_encode_init(&ctx
);
1082 size_t blen
= base64_encode_update(&ctx
, reinterpret_cast<uint8_t*>(req
->pub_auth
), bufLen
, reinterpret_cast<uint8_t*>(buf
));
1083 blen
+= base64_encode_final(&ctx
, reinterpret_cast<uint8_t*>(req
->pub_auth
)+blen
);
1084 req
->pub_auth
[blen
] = '\0';
1085 debug("cmgr: encoded: '%s'\n", req
->pub_auth
);
1089 decode_pub_auth(cachemgr_request
* req
)
1092 const char *host_name
;
1093 const char *time_str
;
1094 const char *user_name
;
1097 debug("cmgr: decoding pub: '%s'\n", safe_str(req
->pub_auth
));
1098 safe_free(req
->passwd
);
1100 if (!req
->pub_auth
|| strlen(req
->pub_auth
) < 4 + strlen(safe_str(req
->hostname
)))
1103 size_t decodedLen
= BASE64_DECODE_LENGTH(strlen(req
->pub_auth
));
1104 buf
= (char*)xmalloc(decodedLen
);
1105 struct base64_decode_ctx ctx
;
1106 base64_decode_init(&ctx
);
1107 base64_decode_update(&ctx
, &decodedLen
, reinterpret_cast<uint8_t*>(buf
), strlen(req
->pub_auth
), reinterpret_cast<const uint8_t*>(req
->pub_auth
));
1108 if (!base64_decode_final(&ctx
)) {
1109 debug("cmgr: base64 decode failure. Incomplete auth token string.\n");
1114 debug("cmgr: length ok\n");
1116 /* parse ( a lot of memory leaks, but that is cachemgr style :) */
1117 if ((host_name
= strtok(buf
, "|")) == NULL
) {
1122 debug("cmgr: decoded host: '%s'\n", host_name
);
1124 if ((time_str
= strtok(NULL
, "|")) == NULL
) {
1129 debug("cmgr: decoded time: '%s' (now: %d)\n", time_str
, (int) now
);
1131 if ((user_name
= strtok(NULL
, "|")) == NULL
) {
1136 debug("cmgr: decoded uname: '%s'\n", user_name
);
1138 if ((passwd
= strtok(NULL
, "|")) == NULL
) {
1143 debug("cmgr: decoded passwd: '%s'\n", passwd
);
1145 /* verify freshness and validity */
1146 if (atoi(time_str
) + passwd_ttl
< now
) {
1151 if (strcasecmp(host_name
, req
->hostname
)) {
1156 debug("cmgr: verified auth. info.\n");
1159 safe_free(req
->user_name
);
1161 req
->user_name
= xstrdup(user_name
);
1163 req
->passwd
= xstrdup(passwd
);
1169 reset_auth(cachemgr_request
* req
)
1171 safe_free(req
->passwd
);
1172 safe_free(req
->pub_auth
);
1176 make_auth_header(const cachemgr_request
* req
)
1178 static char buf
[1024];
1179 size_t stringLength
= 0;
1184 int bufLen
= snprintf(buf
, sizeof(buf
), "%s:%s",
1185 req
->user_name
? req
->user_name
: "",
1188 int encodedLen
= base64_encode_len(bufLen
);
1189 if (encodedLen
<= 0)
1192 uint8_t *str64
= static_cast<uint8_t*>(xmalloc(encodedLen
));
1193 struct base64_encode_ctx ctx
;
1194 base64_encode_init(&ctx
);
1195 size_t blen
= base64_encode_update(&ctx
, str64
, bufLen
, reinterpret_cast<uint8_t*>(buf
));
1196 blen
+= base64_encode_final(&ctx
, str64
+blen
);
1199 stringLength
+= snprintf(buf
, sizeof(buf
), "Authorization: Basic %.*s\r\n", (int)blen
, str64
);
1201 assert(stringLength
< sizeof(buf
));
1203 snprintf(&buf
[stringLength
], sizeof(buf
) - stringLength
, "Proxy-Authorization: Basic %.*s\r\n", (int)blen
, str64
);
1210 check_target_acl(const char *hostname
, int port
)
1212 char config_line
[BUFSIZ
];
1215 fp
= fopen("cachemgr.conf", "r");
1218 fp
= fopen(DEFAULT_CACHEMGR_CONFIG
, "r");
1221 #ifdef CACHEMGR_HOSTNAME_DEFINED
1223 if (strcmp(hostname
, CACHEMGR_HOSTNAME
) == 0 && port
== CACHE_HTTP_PORT
)
1228 if (strcmp(hostname
, "localhost") == 0)
1231 if (strcmp(hostname
, getfullhostname()) == 0)
1239 while (fgets(config_line
, BUFSIZ
, fp
)) {
1241 strtok(config_line
, " \r\n\t");
1243 if (config_line
[0] == '#')
1246 if (config_line
[0] == '\0')
1249 if ((token
= strtok(config_line
, ":")) == NULL
)
1254 if (fnmatch(token
, hostname
, 0) != 0)
1259 if (strcmp(token
, hostname
) != 0)
1264 if ((token
= strtok(NULL
, ":")) != NULL
) {
1267 if (strcmp(token
, "*") == 0)
1269 ; /* Wildcard port specification */
1270 else if (strcmp(token
, "any") == 0)
1272 ; /* Wildcard port specification */
1273 else if (sscanf(token
, "%d", &i
) != 1)
1278 } else if (port
!= CACHE_HTTP_PORT
)