1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
5 * Purpose : Simple CGIs to get information about Privoxy's
8 * Copyright : Written by and Copyright (C) 2001-2020 the
9 * Privoxy team. https://www.privoxy.org/
11 * Based on the Internet Junkbuster originally written
12 * by and Copyright (C) 1997 Anonymous Coders and
13 * Junkbusters Corporation. http://www.junkbusters.com
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 **********************************************************************/
39 #include <sys/types.h>
45 #if defined (HAVE_ACCESS) && defined (HAVE_UNISTD_H)
47 #endif /* def HAVE_ACCESS && HAVE_UNISTD_H */
51 #include "cgisimple.h"
62 #ifdef FEATURE_CLIENT_TAGS
63 #include "client-tags.h"
66 static jb_err show_defines(struct map *exports);
67 static jb_err cgi_show_file(struct client_state *csp,
68 struct http_response *rsp,
69 const struct map *parameters);
70 static jb_err load_file(const char *filename, char **buffer, size_t *length);
72 /*********************************************************************
74 * Function : cgi_default
76 * Description : CGI function that is called for the CGI_SITE_1_HOST
77 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
78 * Boring - only exports the default exports.
81 * 1 : csp = Current client state (buffers, headers, etc...)
82 * 2 : rsp = http_response data structure for output
83 * 3 : parameters = map of cgi parameters
85 * CGI Parameters : none
87 * Returns : JB_ERR_OK on success
88 * JB_ERR_MEMORY on out-of-memory
90 *********************************************************************/
91 jb_err cgi_default(struct client_state *csp,
92 struct http_response *rsp,
93 const struct map *parameters)
102 if (NULL == (exports = default_exports(csp, "")))
104 return JB_ERR_MEMORY;
107 return template_fill_for_cgi(csp, "default", exports, rsp);
111 /*********************************************************************
113 * Function : cgi_error_404
115 * Description : CGI function that is called if an unknown action was
119 * 1 : csp = Current client state (buffers, headers, etc...)
120 * 2 : rsp = http_response data structure for output
121 * 3 : parameters = map of cgi parameters
123 * CGI Parameters : none
125 * Returns : JB_ERR_OK on success
126 * JB_ERR_MEMORY on out-of-memory error.
128 *********************************************************************/
129 jb_err cgi_error_404(struct client_state *csp,
130 struct http_response *rsp,
131 const struct map *parameters)
139 if (NULL == (exports = default_exports(csp, NULL)))
141 return JB_ERR_MEMORY;
144 rsp->status = strdup_or_die("404 Privoxy configuration page not found");
146 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
150 #ifdef FEATURE_GRACEFUL_TERMINATION
151 /*********************************************************************
155 * Description : CGI function to shut down Privoxy.
156 * NOTE: Turning this on in a production build
157 * would be a BAD idea. An EXTREMELY BAD idea.
158 * In short, don't do it.
161 * 1 : csp = Current client state (buffers, headers, etc...)
162 * 2 : rsp = http_response data structure for output
163 * 3 : parameters = map of cgi parameters
165 * CGI Parameters : none
167 * Returns : JB_ERR_OK on success
169 *********************************************************************/
170 jb_err cgi_die (struct client_state *csp,
171 struct http_response *rsp,
172 const struct map *parameters)
174 static const char status[] = "200 OK Privoxy shutdown request received";
175 static const char body[] =
178 " <title>Privoxy shutdown request received</title>\n"
179 " <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">\n"
180 " <link rel=\"stylesheet\" type=\"text/css\" href=\"" CGI_PREFIX "send-stylesheet\">\n"
183 "<h1>Privoxy shutdown request received</h1>\n"
184 "<p>Privoxy is going to shut down after the next request.</p>\n"
195 csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
197 rsp->content_length = 0;
198 rsp->head_length = 0;
201 rsp->body = strdup_or_die(body);
202 rsp->status = strdup_or_die(status);
206 #endif /* def FEATURE_GRACEFUL_TERMINATION */
209 /*********************************************************************
211 * Function : cgi_show_request
213 * Description : Show the client's request and what sed() would have
217 * 1 : csp = Current client state (buffers, headers, etc...)
218 * 2 : rsp = http_response data structure for output
219 * 3 : parameters = map of cgi parameters
221 * CGI Parameters : none
223 * Returns : JB_ERR_OK on success
224 * JB_ERR_MEMORY on out-of-memory error.
226 *********************************************************************/
227 jb_err cgi_show_request(struct client_state *csp,
228 struct http_response *rsp,
229 const struct map *parameters)
238 if (NULL == (exports = default_exports(csp, "show-request")))
240 return JB_ERR_MEMORY;
244 * Repair the damage done to the IOB by get_header()
246 for (p = csp->client_iob->buf; p < csp->client_iob->cur; p++)
248 if (*p == '\0') *p = '\n';
252 * Export the original client's request and the one we would
253 * be sending to the server if this wasn't a CGI call
256 if (map(exports, "client-request", 1, html_encode(csp->client_iob->buf), 0))
259 return JB_ERR_MEMORY;
262 if (map(exports, "processed-request", 1,
263 html_encode_and_free_original(list_to_text(csp->headers)), 0))
266 return JB_ERR_MEMORY;
269 return template_fill_for_cgi(csp, "show-request", exports, rsp);
273 #ifdef FEATURE_CLIENT_TAGS
274 /*********************************************************************
276 * Function : cgi_create_client_tag_form
278 * Description : Creates a HTML form to enable or disable a given
280 * XXX: Could use a template.
283 * 1 : form = Buffer to fill with the generated form
284 * 2 : size = Size of the form buffer
285 * 3 : tag = Name of the tag this form should affect
286 * 4 : toggle_state = Desired state after the button pressed 0
287 * 5 : expires = Whether or not the tag should be enabled.
288 * Only checked if toggle_state is 1.
292 *********************************************************************/
293 static void cgi_create_client_tag_form(char *form, size_t size,
294 const char *tag, int toggle_state, int expires)
298 if (toggle_state == 1)
300 button_name = (expires == 1) ? "Enable" : "Enable temporarily";
304 assert(toggle_state == 0);
305 button_name = "Disable";
309 "<form method=\"GET\" action=\""CGI_PREFIX"toggle-client-tag\" style=\"display: inline\">\n"
310 " <input type=\"hidden\" name=\"tag\" value=\"%s\">\n"
311 " <input type=\"hidden\" name=\"toggle-state\" value=\"%i\">\n"
312 " <input type=\"hidden\" name=\"expires\" value=\"%u\">\n"
313 " <input type=\"submit\" value=\"%s\">\n"
314 "</form>", tag, toggle_state, !expires, button_name);
317 /*********************************************************************
319 * Function : cgi_show_client_tags
321 * Description : Shows the tags that can be set based on the client
325 * 1 : csp = Current client state (buffers, headers, etc...)
326 * 2 : rsp = http_response data structure for output
327 * 3 : parameters = map of cgi parameters
329 * CGI Parameters : none
331 * Returns : JB_ERR_OK on success
332 * JB_ERR_MEMORY on out-of-memory error.
334 *********************************************************************/
335 jb_err cgi_show_client_tags(struct client_state *csp,
336 struct http_response *rsp,
337 const struct map *parameters)
340 struct client_tag_spec *this_tag;
341 jb_err err = JB_ERR_OK;
342 char *client_tag_status;
344 time_t refresh_delay;
350 if (NULL == (exports = default_exports(csp, "client-tags")))
352 return JB_ERR_MEMORY;
354 assert(csp->client_address != NULL);
356 this_tag = csp->config->client_tags;
357 if (this_tag->name == NULL)
359 client_tag_status = strdup_or_die("<p>No tags available.</p>\n");
363 client_tag_status = strdup_or_die("<table border=\"1\">\n"
364 "<tr><th>Tag name</th>\n"
365 "<th>Current state</th><th>Change state</th><th>Description</th></tr>\n");
366 while ((this_tag != NULL) && (this_tag->name != NULL))
370 privoxy_mutex_lock(&client_tags_mutex);
371 tag_state = client_has_requested_tag(csp->client_address, this_tag->name);
372 privoxy_mutex_unlock(&client_tags_mutex);
373 if (!err) err = string_append(&client_tag_status, "<tr><td>");
374 if (!err) err = string_append(&client_tag_status, this_tag->name);
375 if (!err) err = string_append(&client_tag_status, "</td><td>");
376 if (!err) err = string_append(&client_tag_status, tag_state == 1 ? "Enabled" : "Disabled");
377 if (!err) err = string_append(&client_tag_status, "</td><td>");
378 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 1);
379 if (!err) err = string_append(&client_tag_status, buf);
382 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 0);
383 if (!err) err = string_append(&client_tag_status, buf);
385 if (!err) err = string_append(&client_tag_status, "</td><td>");
386 if (!err) err = string_append(&client_tag_status, this_tag->description);
387 if (!err) err = string_append(&client_tag_status, "</td></tr>\n");
392 this_tag = this_tag->next;
394 if (!err) err = string_append(&client_tag_status, "</table>\n");
398 return JB_ERR_MEMORY;
401 refresh_delay = get_next_tag_timeout_for_client(csp->client_address);
402 if (refresh_delay != 0)
404 snprintf(buf, sizeof(buf), "%u", csp->config->client_tag_lifetime);
405 if (map(exports, "refresh-delay", 1, buf, 1))
408 return JB_ERR_MEMORY;
413 err = map_block_killer(exports, "tags-expire");
414 if (err != JB_ERR_OK)
420 if (map(exports, "client-tags", 1, client_tag_status, 0))
423 return JB_ERR_MEMORY;
426 if (map(exports, "client-ip-addr", 1, csp->client_address, 1))
429 return JB_ERR_MEMORY;
432 return template_fill_for_cgi(csp, "client-tags", exports, rsp);
436 /*********************************************************************
438 * Function : cgi_toggle_client_tag
440 * Description : Toggles a client tag and redirects to the show-tags
444 * 1 : csp = Current client state (buffers, headers, etc...)
445 * 2 : rsp = http_response data structure for output
446 * 3 : parameters = map of cgi parameters
448 * CGI Parameters : none
449 * 1 : tag = Name of the tag to enable or disable
450 * 2 : toggle-state = How to toggle the tag (0/1)
451 * 3 : expires = Set to 1 if the tag should be enabled
452 * temporarily, otherwise set to 0
454 * Returns : JB_ERR_OK on success
455 * JB_ERR_MEMORY on out-of-memory error.
457 *********************************************************************/
458 jb_err cgi_toggle_client_tag(struct client_state *csp,
459 struct http_response *rsp,
460 const struct map *parameters)
462 const char *toggled_tag;
463 const char *toggle_state;
464 const char *tag_expires;
471 toggled_tag = lookup(parameters, "tag");
472 if (*toggled_tag == '\0')
474 log_error(LOG_LEVEL_ERROR, "Received tag toggle request without tag");
478 tag_expires = lookup(parameters, "expires");
479 if (*tag_expires == '0')
485 time_to_live = csp->config->client_tag_lifetime;
487 toggle_state = lookup(parameters, "toggle-state");
488 if (*toggle_state == '1')
490 enable_client_specific_tag(csp, toggled_tag, time_to_live);
494 disable_client_specific_tag(csp, toggled_tag);
497 rsp->status = strdup_or_die("302 Done dealing with toggle request");
498 if (enlist_unique_header(rsp->headers,
499 "Location", CGI_PREFIX "client-tags"))
501 return JB_ERR_MEMORY;
506 #endif /* def FEATURE_CLIENT_TAGS */
509 /*********************************************************************
511 * Function : cgi_send_banner
513 * Description : CGI function that returns a banner.
516 * 1 : csp = Current client state (buffers, headers, etc...)
517 * 2 : rsp = http_response data structure for output
518 * 3 : parameters = map of cgi parameters
521 * type : Selects the type of banner between "trans", "logo",
522 * and "auto". Defaults to "logo" if absent or invalid.
523 * "auto" means to select as if we were image-blocking.
524 * (Only the first character really counts; b and t are
527 * Returns : JB_ERR_OK on success
528 * JB_ERR_MEMORY on out-of-memory error.
530 *********************************************************************/
531 jb_err cgi_send_banner(struct client_state *csp,
532 struct http_response *rsp,
533 const struct map *parameters)
535 char imagetype = lookup(parameters, "type")[0];
538 * If type is auto, then determine the right thing
539 * to do from the set-image-blocker action
541 if (imagetype == 'a')
548 #ifdef FEATURE_IMAGE_BLOCKING
549 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
551 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
552 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
553 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
557 /* Use default - nothing to do here. */
559 else if (0 == strcmpic(p, "blank"))
563 else if (0 == strcmpic(p, "pattern"))
569 * If the action is to call this CGI, determine
572 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
574 imagetype = p[sizeof(prefix1) - 1];
576 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
578 imagetype = p[sizeof(prefix2) - 1];
582 * Everything else must (should) be a URL to
590 #endif /* def FEATURE_IMAGE_BLOCKING */
594 * Now imagetype is either the non-auto type we were called with,
595 * or it was auto and has since been determined. In any case, we
596 * can proceed to actually answering the request by sending a redirect
597 * or an image as appropriate:
599 if (imagetype == 'r')
601 rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
602 if (enlist_unique_header(rsp->headers, "Location",
603 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
605 return JB_ERR_MEMORY;
610 if ((imagetype == 'b') || (imagetype == 't'))
612 rsp->body = bindup(image_blank_data, image_blank_length);
613 rsp->content_length = image_blank_length;
617 rsp->body = bindup(image_pattern_data, image_pattern_length);
618 rsp->content_length = image_pattern_length;
621 if (rsp->body == NULL)
623 return JB_ERR_MEMORY;
625 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
627 return JB_ERR_MEMORY;
638 /*********************************************************************
640 * Function : cgi_transparent_image
642 * Description : CGI function that sends a 1x1 transparent image.
645 * 1 : csp = Current client state (buffers, headers, etc...)
646 * 2 : rsp = http_response data structure for output
647 * 3 : parameters = map of cgi parameters
649 * CGI Parameters : None
651 * Returns : JB_ERR_OK on success
652 * JB_ERR_MEMORY on out-of-memory error.
654 *********************************************************************/
655 jb_err cgi_transparent_image(struct client_state *csp,
656 struct http_response *rsp,
657 const struct map *parameters)
662 rsp->body = bindup(image_blank_data, image_blank_length);
663 rsp->content_length = image_blank_length;
665 if (rsp->body == NULL)
667 return JB_ERR_MEMORY;
670 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
672 return JB_ERR_MEMORY;
682 /*********************************************************************
684 * Function : cgi_send_default_favicon
686 * Description : CGI function that sends the standard favicon.
689 * 1 : csp = Current client state (buffers, headers, etc...)
690 * 2 : rsp = http_response data structure for output
691 * 3 : parameters = map of cgi parameters
693 * CGI Parameters : None
695 * Returns : JB_ERR_OK on success
696 * JB_ERR_MEMORY on out-of-memory error.
698 *********************************************************************/
699 jb_err cgi_send_default_favicon(struct client_state *csp,
700 struct http_response *rsp,
701 const struct map *parameters)
703 static const char default_favicon_data[] =
704 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
705 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
706 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
707 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
708 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
709 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
710 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
711 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
712 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
713 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
714 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
715 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
716 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
718 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
723 rsp->body = bindup(default_favicon_data, favicon_length);
724 rsp->content_length = favicon_length;
726 if (rsp->body == NULL)
728 return JB_ERR_MEMORY;
731 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
733 return JB_ERR_MEMORY;
743 /*********************************************************************
745 * Function : cgi_send_error_favicon
747 * Description : CGI function that sends the favicon for error pages.
750 * 1 : csp = Current client state (buffers, headers, etc...)
751 * 2 : rsp = http_response data structure for output
752 * 3 : parameters = map of cgi parameters
754 * CGI Parameters : None
756 * Returns : JB_ERR_OK on success
757 * JB_ERR_MEMORY on out-of-memory error.
759 *********************************************************************/
760 jb_err cgi_send_error_favicon(struct client_state *csp,
761 struct http_response *rsp,
762 const struct map *parameters)
764 static const char error_favicon_data[] =
765 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
766 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
767 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
768 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
769 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
770 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
771 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
772 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
773 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
774 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
775 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
776 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
777 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
779 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
784 rsp->body = bindup(error_favicon_data, favicon_length);
785 rsp->content_length = favicon_length;
787 if (rsp->body == NULL)
789 return JB_ERR_MEMORY;
792 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
794 return JB_ERR_MEMORY;
804 /*********************************************************************
806 * Function : cgi_send_stylesheet
808 * Description : CGI function that sends a css stylesheet found
809 * in the cgi-style.css template
812 * 1 : csp = Current client state (buffers, headers, etc...)
813 * 2 : rsp = http_response data structure for output
814 * 3 : parameters = map of cgi parameters
816 * CGI Parameters : None
818 * Returns : JB_ERR_OK on success
819 * JB_ERR_MEMORY on out-of-memory error.
821 *********************************************************************/
822 jb_err cgi_send_stylesheet(struct client_state *csp,
823 struct http_response *rsp,
824 const struct map *parameters)
833 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
835 if (err == JB_ERR_FILE)
838 * No way to tell user; send empty stylesheet
840 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
844 return err; /* JB_ERR_MEMORY */
847 if (enlist(rsp->headers, "Content-Type: text/css"))
849 return JB_ERR_MEMORY;
857 /*********************************************************************
859 * Function : cgi_send_url_info_osd
861 * Description : CGI function that sends the OpenSearch Description
862 * template for the show-url-info page. It allows to
863 * access the page through "search engine plugins".
866 * 1 : csp = Current client state (buffers, headers, etc...)
867 * 2 : rsp = http_response data structure for output
868 * 3 : parameters = map of cgi parameters
870 * CGI Parameters : None
872 * Returns : JB_ERR_OK on success
873 * JB_ERR_MEMORY on out-of-memory error.
875 *********************************************************************/
876 jb_err cgi_send_url_info_osd(struct client_state *csp,
877 struct http_response *rsp,
878 const struct map *parameters)
880 jb_err err = JB_ERR_MEMORY;
881 struct map *exports = default_exports(csp, NULL);
888 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
889 if (JB_ERR_OK == err)
891 err = enlist(rsp->headers,
892 "Content-Type: application/opensearchdescription+xml");
901 /*********************************************************************
903 * Function : get_content_type
905 * Description : Use the file extension to guess the content type
906 * header we should use to serve the file.
909 * 1 : filename = Name of the file whose content type
912 * Returns : The guessed content type.
914 *********************************************************************/
915 static const char *get_content_type(const char *filename)
920 const char extension[6];
921 const char content_type[11];
923 static const struct content_type content_types[] =
925 {".css", "text/css"},
926 {".jpg", "image/jpeg"},
927 {".jpeg", "image/jpeg"},
928 {".png", "image/png"},
931 for (i = 0; i < SZ(content_types); i++)
933 if (strstr(filename, content_types[i].extension))
935 return content_types[i].content_type;
939 /* No match by extension, default to html */
943 /*********************************************************************
945 * Function : cgi_send_user_manual
947 * Description : CGI function that sends a file in the user
951 * 1 : csp = Current client state (buffers, headers, etc...)
952 * 2 : rsp = http_response data structure for output
953 * 3 : parameters = map of cgi parameters
955 * CGI Parameters : file=name.html, the name of the HTML file
956 * (relative to user-manual from config)
958 * Returns : JB_ERR_OK on success
959 * JB_ERR_MEMORY on out-of-memory error.
961 *********************************************************************/
962 jb_err cgi_send_user_manual(struct client_state *csp,
963 struct http_response *rsp,
964 const struct map *parameters)
966 const char *filename;
968 jb_err err = JB_ERR_OK;
969 const char *content_type;
975 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
977 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
978 "received while user-manual delivery is disabled.");
979 return cgi_error_404(csp, rsp, parameters);
982 if (!parameters->first)
984 /* requested http://p.p/user-manual (without trailing slash) */
985 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
988 get_string_param(parameters, "file", &filename);
989 if (filename == NULL)
991 /* It's '/' so serve the index.html if there is one. */
992 filename = "index.html";
994 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
997 * We currently only support a flat file
998 * hierarchy for the documentation.
1000 log_error(LOG_LEVEL_ERROR,
1001 "Rejecting the request to serve '%s' as it contains '/' or '..'",
1003 return JB_ERR_CGI_PARAMS;
1006 full_path = make_path(csp->config->usermanual, filename);
1007 if (full_path == NULL)
1009 return JB_ERR_MEMORY;
1012 err = load_file(full_path, &rsp->body, &rsp->content_length);
1013 if (JB_ERR_OK != err)
1015 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1016 if (JB_ERR_FILE == err)
1018 err = cgi_error_no_template(csp, rsp, full_path);
1025 content_type = get_content_type(filename);
1026 log_error(LOG_LEVEL_CGI,
1027 "Content-Type guessed for %s: %s", filename, content_type);
1029 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
1034 #ifdef FEATURE_EXTENDED_STATISTICS
1035 /*********************************************************************
1037 * Function : get_block_reason_statistics_table
1039 * Description : Produces the block reason statistic table content.
1042 * 1 : csp = Current client state (buffers, headers, etc...)
1044 * Returns : Pointer to the HTML statistic table content or
1045 * NULL on out of memory
1047 *********************************************************************/
1048 static char *get_block_reason_statistics_table(const struct client_state *csp)
1050 char buf[BUFFER_SIZE];
1053 struct file_list *fl;
1054 jb_err err = JB_ERR_OK;
1056 statistics = strdup_or_die("");
1058 /* Run through all action files. */
1059 for (i = 0; i < MAX_AF_FILES; i++)
1061 struct url_actions *b;
1062 struct action_spec *last_action = NULL;
1064 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1066 /* Skip empty files */
1070 /* Go through all the actions. */
1071 for (b = b->next; NULL != b; b = b->next)
1073 if (last_action == b->action)
1077 if ((b->action->add & ACTION_BLOCK))
1079 unsigned long long count;
1080 const char *block_reason = b->action->string[ACTION_STRING_BLOCK];
1081 const char *encoded_block_reason = html_encode(block_reason);
1083 if (encoded_block_reason == NULL)
1088 get_block_reason_count(block_reason, &count);
1089 snprintf(buf, sizeof(buf),
1090 "<tr><td>%s</td><td style=\"text-align: right\">%llu</td>\n",
1091 encoded_block_reason, count);
1092 freez(encoded_block_reason);
1094 if (!err) err = string_append(&statistics, buf);
1096 last_action = b->action;
1105 /*********************************************************************
1107 * Function : get_filter_statistics_table
1109 * Description : Produces the filter statistic table content.
1112 * 1 : csp = Current client state (buffers, headers, etc...)
1114 * Returns : Pointer to the HTML statistic table content or
1115 * NULL on out of memory
1117 *********************************************************************/
1118 static char *get_filter_statistics_table(const struct client_state *csp)
1120 char buf[BUFFER_SIZE];
1123 struct file_list *fl;
1124 struct re_filterfile_spec *b;
1125 jb_err err = JB_ERR_OK;
1127 statistics = strdup_or_die("");
1129 for (i = 0; i < MAX_AF_FILES; i++)
1132 if ((NULL == fl) || (NULL == fl->f))
1135 * Either there are no filter files left or this
1136 * filter file just contains no valid filters.
1138 * Continue to be sure we don't miss valid filter
1139 * files that are chained after empty or invalid ones.
1144 for (b = fl->f; b != NULL; b = b->next)
1146 if (b->type == FT_CONTENT_FILTER)
1148 unsigned long long executions;
1149 unsigned long long response_bodies_modified;
1150 unsigned long long hits;
1152 get_filter_statistics(b->name, &executions, &response_bodies_modified, &hits);
1153 snprintf(buf, sizeof(buf),
1154 "<tr><td>%s</td><td style=\"text-align: right\">%llu</td>"
1155 "<td style=\"text-align: right\">%llu</td>"
1156 "<td style=\"text-align: right\">%llu</td><tr>\n",
1157 b->name, executions, response_bodies_modified, hits);
1159 if (!err) err = string_append(&statistics, buf);
1167 #endif /* def FEATURE_EXTENDED_STATISTICS */
1170 /*********************************************************************
1172 * Function : cgi_show_status
1174 * Description : CGI function that returns a web page describing the
1175 * current status of Privoxy.
1178 * 1 : csp = Current client state (buffers, headers, etc...)
1179 * 2 : rsp = http_response data structure for output
1180 * 3 : parameters = map of cgi parameters
1183 * file : Which file to show. Only first letter is checked,
1188 * Default is to show menu and other information.
1190 * Returns : JB_ERR_OK on success
1191 * JB_ERR_MEMORY on out-of-memory error.
1193 *********************************************************************/
1194 jb_err cgi_show_status(struct client_state *csp,
1195 struct http_response *rsp,
1196 const struct map *parameters)
1202 char buf[BUFFER_SIZE];
1203 #ifdef FEATURE_STATISTICS
1204 float perc_rej; /* Percentage of http requests rejected */
1205 int local_urls_read;
1206 int local_urls_rejected;
1207 #endif /* ndef FEATURE_STATISTICS */
1208 jb_err err = JB_ERR_OK;
1210 struct map *exports;
1216 if ('\0' != *(lookup(parameters, "file")))
1218 return cgi_show_file(csp, rsp, parameters);
1221 if (NULL == (exports = default_exports(csp, "show-status")))
1223 return JB_ERR_MEMORY;
1227 for (j = 0; (s != NULL) && (j < Argc); j++)
1229 if (!err) err = string_join (&s, html_encode(Argv[j]));
1230 if (!err) err = string_append(&s, " ");
1232 if (!err) err = map(exports, "invocation", 1, s, 0);
1234 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1235 if (!err) err = show_defines(exports);
1240 return JB_ERR_MEMORY;
1243 #ifdef FEATURE_STATISTICS
1244 local_urls_read = urls_read;
1245 local_urls_rejected = urls_rejected;
1248 * Need to alter the stats not to include the fetch of this
1251 * Can't do following thread safely! doh!
1254 * urls_rejected--; * This will be incremented subsequently *
1257 if (local_urls_read == 0)
1259 if (!err) err = map_block_killer(exports, "have-stats");
1263 if (!err) err = map_block_killer(exports, "have-no-stats");
1265 perc_rej = (float)local_urls_rejected * 100.0F /
1266 (float)local_urls_read;
1268 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1269 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1271 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1272 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1274 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1275 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1278 #else /* ndef FEATURE_STATISTICS */
1279 if (!err) err = map_block_killer(exports, "statistics");
1280 #endif /* ndef FEATURE_STATISTICS */
1282 #ifdef FEATURE_EXTENDED_STATISTICS
1285 char *block_reason_statistics = get_block_reason_statistics_table(csp);
1286 if (block_reason_statistics != NULL)
1288 err = map(exports, "block-reason-statistics", 1, block_reason_statistics, 0);
1292 err = map_block_killer(exports, "extended-statistics");
1297 char *filter_statistics = get_filter_statistics_table(csp);
1298 if (filter_statistics != NULL)
1300 err = map(exports, "filter-statistics", 1, filter_statistics, 0);
1304 err = map_block_killer(exports, "extended-statistics");
1307 #else /* ndef FEATURE_EXTENDED_STATISTICS */
1308 if (!err) err = map_block_killer(exports, "extended-statistics");
1309 #endif /* def FEATURE_EXTENDED_STATISTICS */
1312 * List all action files in use, together with view and edit links,
1313 * except for standard.action, which should only be viewable. (Not
1314 * enforced in the editor itself)
1315 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1318 for (i = 0; i < MAX_AF_FILES; i++)
1320 if (csp->actions_list[i] != NULL)
1322 if (!err) err = string_append(&s, "<tr><td>");
1323 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1324 snprintf(buf, sizeof(buf),
1325 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1326 if (!err) err = string_append(&s, buf);
1328 #ifdef FEATURE_CGI_EDIT_ACTIONS
1329 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1330 && (NULL != csp->config->actions_file_short[i]))
1333 if (access(csp->config->actions_file[i], W_OK) == 0)
1335 #endif /* def HAVE_ACCESS */
1336 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1337 if (!err) err = string_append(&s, buf);
1342 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1344 #endif /* def HAVE_ACCESS */
1348 if (!err) err = string_append(&s, "</td></tr>\n");
1351 if (!err && *s != '\0')
1353 err = map(exports, "actions-filenames", 1, s, 0);
1357 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1362 * List all re_filterfiles in use, together with view options.
1363 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1366 for (i = 0; i < MAX_AF_FILES; i++)
1368 if (csp->rlist[i] != NULL)
1370 if (!err) err = string_append(&s, "<tr><td>");
1371 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1372 snprintf(buf, sizeof(buf),
1373 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1374 if (!err) err = string_append(&s, buf);
1375 if (!err) err = string_append(&s, "</td></tr>\n");
1378 if (!err && *s != '\0')
1380 err = map(exports, "re-filter-filenames", 1, s, 0);
1384 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1385 if (!err) err = map_block_killer(exports, "have-filterfile");
1389 #ifdef FEATURE_TRUST
1392 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1396 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1397 if (!err) err = map_block_killer(exports, "have-trustfile");
1400 if (!err) err = map_block_killer(exports, "trust-support");
1401 #endif /* ndef FEATURE_TRUST */
1403 #ifdef FEATURE_CGI_EDIT_ACTIONS
1404 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1406 err = map_block_killer(exports, "cgi-editor-is-disabled");
1408 #endif /* ndef CGI_EDIT_ACTIONS */
1410 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1415 return JB_ERR_MEMORY;
1418 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1422 /*********************************************************************
1424 * Function : cgi_show_url_info
1426 * Description : CGI function that determines and shows which actions
1427 * Privoxy will perform for a given url, and which
1428 * matches starting from the defaults have lead to that.
1431 * 1 : csp = Current client state (buffers, headers, etc...)
1432 * 2 : rsp = http_response data structure for output
1433 * 3 : parameters = map of cgi parameters
1436 * url : The url whose actions are to be determined.
1437 * If url is unset, the url-given conditional will be
1438 * set, so that all but the form can be suppressed in
1441 * Returns : JB_ERR_OK on success
1442 * JB_ERR_MEMORY on out-of-memory error.
1444 *********************************************************************/
1445 jb_err cgi_show_url_info(struct client_state *csp,
1446 struct http_response *rsp,
1447 const struct map *parameters)
1450 struct map *exports;
1457 if (NULL == (exports = default_exports(csp, "show-url-info")))
1459 return JB_ERR_MEMORY;
1463 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1465 url_param = strdup_or_die(lookup(parameters, "url"));
1469 * Handle prefixes. 4 possibilities:
1470 * 1) "http://" or "https://" prefix present and followed by URL - OK
1471 * 2) Only the "http://" or "https://" part is present, no URL - change
1472 * to empty string so it will be detected later as "no URL".
1473 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1475 * 4) Parameter not specified or is empty string - let this fall through
1476 * for now, next block of code will handle it.
1478 if (0 == strncmp(url_param, "http://", 7))
1480 if (url_param[7] == '\0')
1483 * Empty URL (just prefix).
1484 * Make it totally empty so it's caught by the next if ()
1486 url_param[0] = '\0';
1489 else if (0 == strncmp(url_param, "https://", 8))
1491 if (url_param[8] == '\0')
1494 * Empty URL (just prefix).
1495 * Make it totally empty so it's caught by the next if ()
1497 url_param[0] = '\0';
1500 else if ((url_param[0] != '\0')
1501 && ((NULL == strstr(url_param, "://")
1502 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1505 * No prefix or at least no prefix before
1506 * the first slash - assume http://
1508 char *url_param_prefixed = strdup_or_die("http://");
1510 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1513 return JB_ERR_MEMORY;
1515 url_param = url_param_prefixed;
1519 * Hide "toggle off" warning if Privoxy is toggled on.
1522 #ifdef FEATURE_TOGGLE
1523 (global_toggle_state == 1) &&
1524 #endif /* def FEATURE_TOGGLE */
1525 map_block_killer(exports, "privoxy-is-toggled-off")
1530 return JB_ERR_MEMORY;
1533 if (url_param[0] == '\0')
1535 /* URL parameter not specified, display query form only. */
1537 if (map_block_killer(exports, "url-given")
1538 || map(exports, "url", 1, "", 1))
1541 return JB_ERR_MEMORY;
1546 /* Given a URL, so query it. */
1551 struct file_list *fl;
1552 struct url_actions *b;
1553 struct http_request url_to_query[1];
1554 struct current_action_spec action[1];
1557 if (map(exports, "url", 1, html_encode(url_param), 0))
1561 return JB_ERR_MEMORY;
1564 init_current_action(action);
1566 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1568 free_current_action(action);
1571 return JB_ERR_MEMORY;
1574 memset(url_to_query, '\0', sizeof(url_to_query));
1575 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1576 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1580 if (err == JB_ERR_MEMORY)
1582 free_http_request(url_to_query);
1583 free_current_action(action);
1585 return JB_ERR_MEMORY;
1591 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1592 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1593 if (!err) err = map_block_killer(exports, "valid-url");
1595 free_current_action(action);
1596 free_http_request(url_to_query);
1601 return JB_ERR_MEMORY;
1604 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1608 * We have a warning about SSL paths. Hide it for unencrypted sites
1609 * and unconditionally if https inspection is enabled.
1611 #ifndef FEATURE_HTTPS_INSPECTION
1612 if (!url_to_query->ssl)
1615 if (map_block_killer(exports, "https-and-no-https-inspection"))
1617 free_current_action(action);
1619 free_http_request(url_to_query);
1620 return JB_ERR_MEMORY;
1624 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1626 for (i = 0; i < MAX_AF_FILES; i++)
1628 if (NULL == csp->config->actions_file_short[i]
1629 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1633 if ((fl = csp->actions_list[i]) != NULL)
1635 if ((b = fl->f) != NULL)
1637 /* FIXME: Hardcoded HTML! */
1638 string_append(&matches, "<tr><th>In file: ");
1639 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1640 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1641 string_append(&matches, buf);
1642 string_append(&matches, "View</a>");
1643 #ifdef FEATURE_CGI_EDIT_ACTIONS
1644 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1647 if (access(csp->config->actions_file[i], W_OK) == 0)
1649 #endif /* def HAVE_ACCESS */
1650 snprintf(buf, sizeof(buf),
1651 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1652 string_append(&matches, buf);
1653 string_append(&matches, "Edit</a>");
1658 string_append(&matches, " <strong>No write access.</strong>");
1660 #endif /* def HAVE_ACCESS */
1662 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1664 string_append(&matches, "</th></tr>\n");
1671 for (; (b != NULL) && (matches != NULL); b = b->next)
1673 if (url_match(b->url, url_to_query))
1675 string_append(&matches, "<tr><td>{");
1676 string_join (&matches, actions_to_html(csp, b->action));
1677 string_append(&matches, " }<br>\n<code>");
1678 string_join (&matches, html_encode(b->url->spec));
1679 string_append(&matches, "</code></td></tr>\n");
1681 if (merge_current_action(action, b->action))
1684 free_http_request(url_to_query);
1685 free_current_action(action);
1687 return JB_ERR_MEMORY;
1695 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1698 string_append(&matches, "</table>\n");
1701 * XXX: Kludge to make sure the "Forward settings" section
1702 * shows what forward-override{} would do with the requested URL.
1703 * No one really cares how the CGI request would be forwarded
1704 * if it wasn't intercepted as CGI request in the first place.
1706 * From here on the action bitmask will no longer reflect
1707 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1708 * but luckily it's no longer required later on anyway.
1710 free_current_action(csp->action);
1711 get_url_actions(csp, url_to_query);
1714 * Fill in forwarding settings.
1716 * The possibilities are:
1718 * - http forwarding only
1719 * - socks4(a) forwarding only
1720 * - socks4(a) and http forwarding.
1722 * XXX: Parts of this code could be reused for the
1723 * "forwarding-failed" template which currently doesn't
1724 * display the proxy port and an eventual second forwarder.
1727 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1729 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1731 if (!err) err = map_block_killer(exports, "socks-forwarder");
1732 if (!err) err = map_block_killer(exports, "http-forwarder");
1736 char port[10]; /* We save proxy ports as int but need a string here */
1738 if (!err) err = map_block_killer(exports, "no-forwarder");
1740 if (fwd->gateway_host != NULL)
1742 char *socks_type = NULL;
1747 socks_type = "socks4";
1750 socks_type = "socks4a";
1753 socks_type = "socks5";
1756 socks_type = "socks5t";
1759 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1762 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1763 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1764 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1765 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1769 if (!err) err = map_block_killer(exports, "socks-forwarder");
1772 if (fwd->forward_host != NULL)
1774 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1775 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1776 if (!err) err = map(exports, "forward-port", 1, port, 1);
1780 if (!err) err = map_block_killer(exports, "http-forwarder");
1785 free_http_request(url_to_query);
1787 if (err || matches == NULL)
1789 free_current_action(action);
1791 return JB_ERR_MEMORY;
1794 #ifdef FEATURE_CGI_EDIT_ACTIONS
1795 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1797 err = map_block_killer(exports, "cgi-editor-is-disabled");
1799 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1802 * If zlib support is available, if no content filters
1803 * are enabled or if the prevent-compression action is enabled,
1804 * suppress the "compression could prevent filtering" warning.
1806 #ifndef FEATURE_ZLIB
1807 if (!content_filters_enabled(action) ||
1808 (action->flags & ACTION_NO_COMPRESSION))
1811 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1814 if (err || map(exports, "matches", 1, matches , 0))
1816 free_current_action(action);
1818 return JB_ERR_MEMORY;
1821 s = current_action_to_html(csp, action);
1823 free_current_action(action);
1825 if (map(exports, "final", 1, s, 0))
1828 return JB_ERR_MEMORY;
1832 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1836 /*********************************************************************
1838 * Function : cgi_robots_txt
1840 * Description : CGI function to return "/robots.txt".
1843 * 1 : csp = Current client state (buffers, headers, etc...)
1844 * 2 : rsp = http_response data structure for output
1845 * 3 : parameters = map of cgi parameters
1847 * CGI Parameters : None
1849 * Returns : JB_ERR_OK on success
1850 * JB_ERR_MEMORY on out-of-memory error.
1852 *********************************************************************/
1853 jb_err cgi_robots_txt(struct client_state *csp,
1854 struct http_response *rsp,
1855 const struct map *parameters)
1863 rsp->body = strdup_or_die(
1864 "# This is the Privoxy control interface.\n"
1865 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1872 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1876 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1877 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1879 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1883 /*********************************************************************
1885 * Function : show_defines
1887 * Description : Add to a map the state of all conditional #defines
1888 * used when building
1891 * 1 : exports = map to extend
1893 * Returns : JB_ERR_OK on success
1894 * JB_ERR_MEMORY on out-of-memory error.
1896 *********************************************************************/
1897 static jb_err show_defines(struct map *exports)
1899 jb_err err = JB_ERR_OK;
1902 const char name[31];
1903 const unsigned char is_available;
1906 static const struct feature features[] = {
1908 "FEATURE_64_BIT_TIME_T",
1909 #if (SIZEOF_TIME_T == 8)
1916 "FEATURE_ACCEPT_FILTER",
1917 #ifdef FEATURE_ACCEPT_FILTER
1933 #ifdef FEATURE_BROTLI
1940 "FEATURE_CGI_EDIT_ACTIONS",
1941 #ifdef FEATURE_CGI_EDIT_ACTIONS
1948 "FEATURE_CLIENT_TAGS",
1949 #ifdef FEATURE_CLIENT_TAGS
1956 "FEATURE_COMPRESSION",
1957 #ifdef FEATURE_COMPRESSION
1964 "FEATURE_CONNECTION_KEEP_ALIVE",
1965 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1972 "FEATURE_CONNECTION_SHARING",
1973 #ifdef FEATURE_CONNECTION_SHARING
1980 "FEATURE_EXTERNAL_FILTERS",
1981 #ifdef FEATURE_EXTERNAL_FILTERS
1988 "FEATURE_FAST_REDIRECTS",
1989 #ifdef FEATURE_FAST_REDIRECTS
1996 "FEATURE_FORCE_LOAD",
1997 #ifdef FEATURE_FORCE_LOAD
2004 "FEATURE_GRACEFUL_TERMINATION",
2005 #ifdef FEATURE_GRACEFUL_TERMINATION
2012 "FEATURE_HTTPS_INSPECTION",
2013 #ifdef FEATURE_HTTPS_INSPECTION
2020 "FEATURE_IMAGE_BLOCKING",
2021 #ifdef FEATURE_IMAGE_BLOCKING
2028 "FEATURE_IPV6_SUPPORT",
2037 #ifdef FEATURE_NO_GIFS
2045 #ifdef FEATURE_PTHREAD
2052 "FEATURE_STATISTICS",
2053 #ifdef FEATURE_STATISTICS
2060 "FEATURE_STRPTIME_SANITY_CHECKS",
2061 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
2069 #ifdef FEATURE_TOGGLE
2077 #ifdef FEATURE_TRUST
2092 "FEATURE_DYNAMIC_PCRE",
2093 #ifdef FEATURE_DYNAMIC_PCRE
2101 for (i = 0; i < SZ(features); i++)
2103 err = map_conditional(exports, features[i].name, features[i].is_available);
2115 /*********************************************************************
2117 * Function : cgi_show_file
2119 * Description : CGI function that shows the content of a
2120 * configuration file.
2123 * 1 : csp = Current client state (buffers, headers, etc...)
2124 * 2 : rsp = http_response data structure for output
2125 * 3 : parameters = map of cgi parameters
2128 * file : Which file to show. Only first letter is checked,
2133 * Default is to show menu and other information.
2135 * Returns : JB_ERR_OK on success
2136 * JB_ERR_MEMORY on out-of-memory error.
2138 *********************************************************************/
2139 static jb_err cgi_show_file(struct client_state *csp,
2140 struct http_response *rsp,
2141 const struct map *parameters)
2144 const char * filename = NULL;
2145 char * file_description = NULL;
2151 switch (*(lookup(parameters, "file")))
2154 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2156 filename = csp->actions_list[i]->filename;
2157 file_description = "Actions File";
2162 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2164 filename = csp->rlist[i]->filename;
2165 file_description = "Filter File";
2169 #ifdef FEATURE_TRUST
2173 filename = csp->tlist->filename;
2174 file_description = "Trust File";
2177 #endif /* def FEATURE_TRUST */
2180 if (NULL != filename)
2182 struct map *exports;
2187 exports = default_exports(csp, "show-status");
2188 if (NULL == exports)
2190 return JB_ERR_MEMORY;
2193 if (map(exports, "file-description", 1, file_description, 1)
2194 || map(exports, "filepath", 1, html_encode(filename), 0))
2197 return JB_ERR_MEMORY;
2200 err = load_file(filename, &s, &length);
2201 if (JB_ERR_OK != err)
2203 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2206 return JB_ERR_MEMORY;
2211 s = html_encode_and_free_original(s);
2215 return JB_ERR_MEMORY;
2218 if (map(exports, "contents", 1, s, 0))
2221 return JB_ERR_MEMORY;
2225 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2228 return JB_ERR_CGI_PARAMS;
2232 /*********************************************************************
2234 * Function : load_file
2236 * Description : Loads a file into a buffer.
2239 * 1 : filename = Name of the file to be loaded.
2240 * 2 : buffer = Used to return the file's content.
2241 * 3 : length = Used to return the size of the file.
2243 * Returns : JB_ERR_OK in case of success,
2244 * JB_ERR_FILE in case of ordinary file loading errors
2245 * (fseek() and ftell() errors are fatal)
2246 * JB_ERR_MEMORY in case of out-of-memory.
2248 *********************************************************************/
2249 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2253 jb_err err = JB_ERR_OK;
2255 fp = fopen(filename, "rb");
2258 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
2262 /* Get file length */
2263 if (fseek(fp, 0, SEEK_END))
2265 log_error(LOG_LEVEL_FATAL,
2266 "Unexpected error while fseek()ing to the end of %s: %E",
2272 log_error(LOG_LEVEL_FATAL,
2273 "Unexpected ftell() error while loading %s: %E",
2276 *length = (size_t)ret;
2278 /* Go back to the beginning. */
2279 if (fseek(fp, 0, SEEK_SET))
2281 log_error(LOG_LEVEL_FATAL,
2282 "Unexpected error while fseek()ing to the beginning of %s: %E",
2286 *buffer = zalloc_or_die(*length + 1);
2288 if (1 != fread(*buffer, *length, 1, fp))
2291 * May theoretically happen if the file size changes between
2292 * fseek() and fread() because it's edited in-place. Privoxy
2293 * and common text editors don't do that, thus we just fail.
2295 log_error(LOG_LEVEL_ERROR,
2296 "Couldn't completely read file %s.", filename);