-
- /*********************************************************************
- *
- * Function : cgi_show_url_info
- *
- * Description : CGI function that determines and shows which actions
- * junkbuster will perform for a given url, and which
- * matches starting from the defaults have lead to that.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- * 2 : rsp = http_response data structure for output
- * 3 : parameters = map of cgi parameters
- *
- * CGI Parameters :
- * url : The url whose actions are to be determined.
- * If url is unset, the url-given conditional will be
- * set, so that all but the form can be suppressed in
- * the template.
- *
- * Returns : 0
- *
- *********************************************************************/
-int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
- struct map *parameters)
-{
- struct map *exports = default_exports(csp, "show-url-info");
- char *url_param, *host = NULL;
-
- if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
- {
- exports = map_block_killer(exports, "url-given");
- exports = map(exports, "url", 1, "", 1);
- }
- else
- {
- char *matches = NULL;
- char *path;
- char *s;
- int port = 80;
- int hits = 0;
- struct file_list *fl;
- struct url_actions *b;
- struct url_spec url[1];
- struct current_action_spec action[1];
-
- host = url_param;
- host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
-
- exports = map(exports, "url", 1, host, 1);
- exports = map(exports, "url-html", 1, html_encode(host), 0);
-
- init_current_action(action);
-
- s = current_action_to_text(action);
- exports = map(exports, "default", 1, s , 0);
-
- if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
- {
- exports = map(exports, "matches", 1, "none" , 1);
- exports = map(exports, "final", 1, lookup(exports, "default"), 1);
-
- freez(url_param);
- free_current_action(action);
-
- rsp->body = fill_template(csp, "show-url-info", exports);
- free_map(exports);
-
- return 0;
- }
-
- s = strchr(host, '/');
- if (s != NULL)
- {
- path = strdup(s);
- *s = '\0';
- }
- else
- {
- path = strdup("");
- }
- s = strchr(host, ':');
- if (s != NULL)
- {
- *s++ = '\0';
- port = atoi(s);
- s = NULL;
- }
-
- *url = dsplit(host);
-
- /* if splitting the domain fails, punt */
- if (url->dbuf == NULL)
- {
- exports = map(exports, "matches", 1, "none" , 1);
- exports = map(exports, "final", 1, lookup(exports, "default"), 1);
-
- freez(url_param);
- freez(path);
- free_current_action(action);
-
- rsp->body = fill_template(csp, "show-url-info", exports);
- free_map(exports);
-
- return 0;
- }
-
- for (b = b->next; NULL != b; b = b->next)
- {
- if ((b->url->port == 0) || (b->url->port == port))
- {
- if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
- {
- if ((b->url->path == NULL) ||
-#ifdef REGEX
- (regexec(b->url->preg, path, 0, NULL, 0) == 0)
-#else
- (strncmp(b->url->path, path, b->url->pathlen) == 0)
-#endif
- )
- {
- s = actions_to_text(b->action);
- matches = strsav(matches, "<b>{");
- matches = strsav(matches, s);
- matches = strsav(matches, " }</b><br>\n<code>");
- matches = strsav(matches, b->url->spec);
- matches = strsav(matches, "</code><br>\n<br>\n");
- freez(s);
-
- merge_current_action(action, b->action);
- hits++;
- }
- }
- }
- }
-
- if (hits)
- {
- exports = map(exports, "matches", 1, matches , 0);
- }
- else
- {
- exports = map(exports, "matches", 1, "none", 1);
- }
- matches = NULL;
-
- freez(url->dbuf);
- freez(url->dvec);
-
- freez(url_param);
- freez(path);
-
- s = current_action_to_text(action);
- exports = map(exports, "final", 1, s, 0);
- s = NULL;
-
- free_current_action(action);
- }
-
- rsp->body = fill_template(csp, "show-url-info", exports);
- free_map(exports);
- return 0;
-
-}
-
-
-/*********************************************************************
- *
- * Function : error_response
- *
- * Description : returns an http_response that explains the reason
- * why a request failed.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- * 2 : template = Which template should be used for the answer
- * 3 : errno = system error number
- *
- * Returns : NULL if no memory, else http_response
- *
- *********************************************************************/
-struct http_response *error_response(struct client_state *csp, const char *template, int err)
-{
- struct http_response *rsp;
- struct map *exports = default_exports(csp, NULL);
-
- if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
- {
- return NULL;
- }
-
- exports = map(exports, "host-html", 1, html_encode(csp->http->host), 0);
- exports = map(exports, "hostport", 1, csp->http->hostport, 1);
- exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
- exports = map(exports, "path", 1, csp->http->path, 1);
- exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
- exports = map(exports, "error", 1, safe_strerror(err), 0);
- exports = map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
-
- rsp->body = fill_template(csp, template, exports);
- free_map(exports);
-
- if (!strcmp(template, "no-such-domain"))
- {
- rsp->status = strdup("404 No such domain");
- }
- else if (!strcmp(template, "connect-failed"))
- {
- rsp->status = strdup("503 Connect failed");
- }
-
- return(finish_http_response(rsp));
-}
-