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-2021 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(
264 #ifdef FEATURE_HTTPS_INSPECTION
266 list_to_text(csp->https_headers) :
268 list_to_text(csp->headers)
272 return JB_ERR_MEMORY;
275 return template_fill_for_cgi(csp, "show-request", exports, rsp);
279 #ifdef FEATURE_CLIENT_TAGS
280 /*********************************************************************
282 * Function : cgi_create_client_tag_form
284 * Description : Creates a HTML form to enable or disable a given
286 * XXX: Could use a template.
289 * 1 : form = Buffer to fill with the generated form
290 * 2 : size = Size of the form buffer
291 * 3 : tag = Name of the tag this form should affect
292 * 4 : toggle_state = Desired state after the button pressed 0
293 * 5 : expires = Whether or not the tag should be enabled.
294 * Only checked if toggle_state is 1.
298 *********************************************************************/
299 static void cgi_create_client_tag_form(char *form, size_t size,
300 const char *tag, int toggle_state, int expires)
304 if (toggle_state == 1)
306 button_name = (expires == 1) ? "Enable" : "Enable temporarily";
310 assert(toggle_state == 0);
311 button_name = "Disable";
315 "<form method=\"GET\" action=\""CGI_PREFIX"toggle-client-tag\" style=\"display: inline\">\n"
316 " <input type=\"hidden\" name=\"tag\" value=\"%s\">\n"
317 " <input type=\"hidden\" name=\"toggle-state\" value=\"%i\">\n"
318 " <input type=\"hidden\" name=\"expires\" value=\"%u\">\n"
319 " <input type=\"submit\" value=\"%s\">\n"
320 "</form>", tag, toggle_state, !expires, button_name);
323 /*********************************************************************
325 * Function : cgi_show_client_tags
327 * Description : Shows the tags that can be set based on the client
331 * 1 : csp = Current client state (buffers, headers, etc...)
332 * 2 : rsp = http_response data structure for output
333 * 3 : parameters = map of cgi parameters
335 * CGI Parameters : none
337 * Returns : JB_ERR_OK on success
338 * JB_ERR_MEMORY on out-of-memory error.
340 *********************************************************************/
341 jb_err cgi_show_client_tags(struct client_state *csp,
342 struct http_response *rsp,
343 const struct map *parameters)
346 struct client_tag_spec *this_tag;
347 jb_err err = JB_ERR_OK;
348 char *client_tag_status;
350 time_t refresh_delay;
356 if (NULL == (exports = default_exports(csp, "client-tags")))
358 return JB_ERR_MEMORY;
360 assert(csp->client_address != NULL);
362 this_tag = csp->config->client_tags;
363 if (this_tag->name == NULL)
365 client_tag_status = strdup_or_die("<p>No tags have been configured.</p>\n");
369 client_tag_status = strdup_or_die("<table border=\"1\">\n"
370 "<tr><th>Tag name</th>\n"
371 "<th>Current state</th><th>Change state</th><th>Description</th></tr>\n");
372 while ((this_tag != NULL) && (this_tag->name != NULL))
376 privoxy_mutex_lock(&client_tags_mutex);
377 tag_state = client_has_requested_tag(csp->client_address, this_tag->name);
378 privoxy_mutex_unlock(&client_tags_mutex);
379 if (!err) err = string_append(&client_tag_status, "<tr><td>");
380 if (!err) err = string_append(&client_tag_status, this_tag->name);
381 if (!err) err = string_append(&client_tag_status, "</td><td>");
382 if (!err) err = string_append(&client_tag_status, tag_state == 1 ? "Enabled" : "Disabled");
383 if (!err) err = string_append(&client_tag_status, "</td><td>");
384 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 1);
385 if (!err) err = string_append(&client_tag_status, buf);
388 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 0);
389 if (!err) err = string_append(&client_tag_status, buf);
391 if (!err) err = string_append(&client_tag_status, "</td><td>");
392 if (!err) err = string_append(&client_tag_status, this_tag->description);
393 if (!err) err = string_append(&client_tag_status, "</td></tr>\n");
398 this_tag = this_tag->next;
400 if (!err) err = string_append(&client_tag_status, "</table>\n");
404 return JB_ERR_MEMORY;
407 refresh_delay = get_next_tag_timeout_for_client(csp->client_address);
408 if (refresh_delay != 0)
410 snprintf(buf, sizeof(buf), "%u", csp->config->client_tag_lifetime);
411 if (map(exports, "refresh-delay", 1, buf, 1))
413 freez(client_tag_status);
415 return JB_ERR_MEMORY;
420 err = map_block_killer(exports, "tags-expire");
421 if (err != JB_ERR_OK)
423 freez(client_tag_status);
428 if (map(exports, "client-tags", 1, client_tag_status, 0))
431 return JB_ERR_MEMORY;
434 if (map(exports, "client-ip-addr", 1, csp->client_address, 1))
437 return JB_ERR_MEMORY;
440 return template_fill_for_cgi(csp, "client-tags", exports, rsp);
444 /*********************************************************************
446 * Function : cgi_toggle_client_tag
448 * Description : Toggles a client tag and redirects to the show-tags
452 * 1 : csp = Current client state (buffers, headers, etc...)
453 * 2 : rsp = http_response data structure for output
454 * 3 : parameters = map of cgi parameters
456 * CGI Parameters : none
457 * 1 : tag = Name of the tag to enable or disable
458 * 2 : toggle-state = How to toggle the tag (0/1)
459 * 3 : expires = Set to 1 if the tag should be enabled
460 * temporarily, otherwise set to 0
462 * Returns : JB_ERR_OK on success
463 * JB_ERR_MEMORY on out-of-memory error.
465 *********************************************************************/
466 jb_err cgi_toggle_client_tag(struct client_state *csp,
467 struct http_response *rsp,
468 const struct map *parameters)
470 const char *toggled_tag;
471 const char *toggle_state;
472 const char *tag_expires;
479 toggled_tag = lookup(parameters, "tag");
480 if (*toggled_tag == '\0')
482 log_error(LOG_LEVEL_ERROR, "Received tag toggle request without tag");
486 tag_expires = lookup(parameters, "expires");
487 if (*tag_expires == '0')
493 time_to_live = csp->config->client_tag_lifetime;
495 toggle_state = lookup(parameters, "toggle-state");
496 if (*toggle_state == '1')
498 enable_client_specific_tag(csp, toggled_tag, time_to_live);
502 disable_client_specific_tag(csp, toggled_tag);
505 rsp->status = strdup_or_die("302 Done dealing with toggle request");
506 if (enlist_unique_header(rsp->headers,
507 "Location", CGI_PREFIX "client-tags"))
509 return JB_ERR_MEMORY;
514 #endif /* def FEATURE_CLIENT_TAGS */
517 /*********************************************************************
519 * Function : cgi_send_banner
521 * Description : CGI function that returns a banner.
524 * 1 : csp = Current client state (buffers, headers, etc...)
525 * 2 : rsp = http_response data structure for output
526 * 3 : parameters = map of cgi parameters
529 * type : Selects the type of banner between "trans", "pattern",
530 * and "auto". Defaults to "pattern" if absent or invalid.
531 * "auto" means to select as if we were image-blocking.
532 * (Only the first character really counts; b and t are
535 * Returns : JB_ERR_OK on success
536 * JB_ERR_MEMORY on out-of-memory error.
538 *********************************************************************/
539 jb_err cgi_send_banner(struct client_state *csp,
540 struct http_response *rsp,
541 const struct map *parameters)
543 char imagetype = lookup(parameters, "type")[0];
545 if (imagetype != 'a' && imagetype != 'b' &&
546 imagetype != 'p' && imagetype != 't')
548 log_error(LOG_LEVEL_ERROR, "Overruling invalid image type '%c'.",
554 * If type is auto, then determine the right thing
555 * to do from the set-image-blocker action
557 if (imagetype == 'a')
564 #ifdef FEATURE_IMAGE_BLOCKING
565 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
567 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
568 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
569 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
573 /* Use default - nothing to do here. */
575 else if (0 == strcmpic(p, "blank"))
579 else if (0 == strcmpic(p, "pattern"))
585 * If the action is to call this CGI, determine
588 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
590 imagetype = p[sizeof(prefix1) - 1];
592 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
594 imagetype = p[sizeof(prefix2) - 1];
598 * Everything else must (should) be a URL to
606 #endif /* def FEATURE_IMAGE_BLOCKING */
610 * Now imagetype is either the non-auto type we were called with,
611 * or it was auto and has since been determined. In any case, we
612 * can proceed to actually answering the request by sending a redirect
613 * or an image as appropriate:
615 if (imagetype == 'r')
617 rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
618 if (enlist_unique_header(rsp->headers, "Location",
619 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
621 return JB_ERR_MEMORY;
626 if ((imagetype == 'b') || (imagetype == 't'))
628 rsp->body = bindup(image_blank_data, image_blank_length);
629 rsp->content_length = image_blank_length;
633 rsp->body = bindup(image_pattern_data, image_pattern_length);
634 rsp->content_length = image_pattern_length;
637 if (rsp->body == NULL)
639 return JB_ERR_MEMORY;
641 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
643 return JB_ERR_MEMORY;
654 /*********************************************************************
656 * Function : cgi_transparent_image
658 * Description : CGI function that sends a 1x1 transparent image.
661 * 1 : csp = Current client state (buffers, headers, etc...)
662 * 2 : rsp = http_response data structure for output
663 * 3 : parameters = map of cgi parameters
665 * CGI Parameters : None
667 * Returns : JB_ERR_OK on success
668 * JB_ERR_MEMORY on out-of-memory error.
670 *********************************************************************/
671 jb_err cgi_transparent_image(struct client_state *csp,
672 struct http_response *rsp,
673 const struct map *parameters)
678 rsp->body = bindup(image_blank_data, image_blank_length);
679 rsp->content_length = image_blank_length;
681 if (rsp->body == NULL)
683 return JB_ERR_MEMORY;
686 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
688 return JB_ERR_MEMORY;
698 /*********************************************************************
700 * Function : cgi_send_default_favicon
702 * Description : CGI function that sends the standard favicon.
705 * 1 : csp = Current client state (buffers, headers, etc...)
706 * 2 : rsp = http_response data structure for output
707 * 3 : parameters = map of cgi parameters
709 * CGI Parameters : None
711 * Returns : JB_ERR_OK on success
712 * JB_ERR_MEMORY on out-of-memory error.
714 *********************************************************************/
715 jb_err cgi_send_default_favicon(struct client_state *csp,
716 struct http_response *rsp,
717 const struct map *parameters)
719 static const char default_favicon_data[] =
720 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
721 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
722 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
723 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
724 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
725 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
726 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
727 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
728 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
729 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
730 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
731 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
732 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
734 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
739 rsp->body = bindup(default_favicon_data, favicon_length);
740 rsp->content_length = favicon_length;
742 if (rsp->body == NULL)
744 return JB_ERR_MEMORY;
747 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
749 return JB_ERR_MEMORY;
759 /*********************************************************************
761 * Function : cgi_send_error_favicon
763 * Description : CGI function that sends the favicon for error pages.
766 * 1 : csp = Current client state (buffers, headers, etc...)
767 * 2 : rsp = http_response data structure for output
768 * 3 : parameters = map of cgi parameters
770 * CGI Parameters : None
772 * Returns : JB_ERR_OK on success
773 * JB_ERR_MEMORY on out-of-memory error.
775 *********************************************************************/
776 jb_err cgi_send_error_favicon(struct client_state *csp,
777 struct http_response *rsp,
778 const struct map *parameters)
780 static const char error_favicon_data[] =
781 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
782 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
783 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
784 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
785 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
786 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
787 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
788 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
789 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
790 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
791 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
792 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
793 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
795 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
800 rsp->body = bindup(error_favicon_data, favicon_length);
801 rsp->content_length = favicon_length;
803 if (rsp->body == NULL)
805 return JB_ERR_MEMORY;
808 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
810 return JB_ERR_MEMORY;
820 /*********************************************************************
822 * Function : cgi_send_stylesheet
824 * Description : CGI function that sends a css stylesheet found
825 * in the cgi-style.css template
828 * 1 : csp = Current client state (buffers, headers, etc...)
829 * 2 : rsp = http_response data structure for output
830 * 3 : parameters = map of cgi parameters
832 * CGI Parameters : None
834 * Returns : JB_ERR_OK on success
835 * JB_ERR_MEMORY on out-of-memory error.
837 *********************************************************************/
838 jb_err cgi_send_stylesheet(struct client_state *csp,
839 struct http_response *rsp,
840 const struct map *parameters)
849 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
851 if (err == JB_ERR_FILE)
854 * No way to tell user; send empty stylesheet
856 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
860 return err; /* JB_ERR_MEMORY */
863 if (enlist(rsp->headers, "Content-Type: text/css"))
865 return JB_ERR_MEMORY;
873 /*********************************************************************
875 * Function : cgi_send_wpad
877 * Description : CGI function that sends a Proxy Auto-Configuration
881 * 1 : csp = Current client state (buffers, headers, etc...)
882 * 2 : rsp = http_response data structure for output
883 * 3 : parameters = map of cgi parameters
885 * CGI Parameters : None
887 * Returns : JB_ERR_OK on success
888 * JB_ERR_MEMORY on out-of-memory error.
890 *********************************************************************/
891 jb_err cgi_send_wpad(struct client_state *csp,
892 struct http_response *rsp,
893 const struct map *parameters)
901 if (NULL == (exports = default_exports(csp, NULL)))
903 return JB_ERR_MEMORY;
906 if (enlist(rsp->headers, "Content-Type: application/x-ns-proxy-autoconfig"))
909 return JB_ERR_MEMORY;
912 return template_fill_for_cgi(csp, "wpad.dat", exports, rsp);
917 /*********************************************************************
919 * Function : cgi_send_url_info_osd
921 * Description : CGI function that sends the OpenSearch Description
922 * template for the show-url-info page. It allows to
923 * access the page through "search engine plugins".
926 * 1 : csp = Current client state (buffers, headers, etc...)
927 * 2 : rsp = http_response data structure for output
928 * 3 : parameters = map of cgi parameters
930 * CGI Parameters : None
932 * Returns : JB_ERR_OK on success
933 * JB_ERR_MEMORY on out-of-memory error.
935 *********************************************************************/
936 jb_err cgi_send_url_info_osd(struct client_state *csp,
937 struct http_response *rsp,
938 const struct map *parameters)
940 jb_err err = JB_ERR_MEMORY;
941 struct map *exports = default_exports(csp, NULL);
948 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
949 if (JB_ERR_OK == err)
951 err = enlist(rsp->headers,
952 "Content-Type: application/opensearchdescription+xml");
961 /*********************************************************************
963 * Function : get_content_type
965 * Description : Use the file extension to guess the content type
966 * header we should use to serve the file.
969 * 1 : filename = Name of the file whose content type
972 * Returns : The guessed content type.
974 *********************************************************************/
975 static const char *get_content_type(const char *filename)
980 const char extension[6];
981 const char content_type[11];
983 static const struct content_type content_types[] =
985 {".css", "text/css"},
986 {".jpg", "image/jpeg"},
987 {".jpeg", "image/jpeg"},
988 {".png", "image/png"},
991 for (i = 0; i < SZ(content_types); i++)
993 if (strstr(filename, content_types[i].extension))
995 return content_types[i].content_type;
999 /* No match by extension, default to html */
1003 /*********************************************************************
1005 * Function : cgi_send_user_manual
1007 * Description : CGI function that sends a file in the user
1011 * 1 : csp = Current client state (buffers, headers, etc...)
1012 * 2 : rsp = http_response data structure for output
1013 * 3 : parameters = map of cgi parameters
1015 * CGI Parameters : file=name.html, the name of the HTML file
1016 * (relative to user-manual from config)
1018 * Returns : JB_ERR_OK on success
1019 * JB_ERR_MEMORY on out-of-memory error.
1021 *********************************************************************/
1022 jb_err cgi_send_user_manual(struct client_state *csp,
1023 struct http_response *rsp,
1024 const struct map *parameters)
1026 const char *filename;
1028 jb_err err = JB_ERR_OK;
1029 const char *content_type;
1035 if (0 == strncmpic(csp->config->usermanual, "http://", 7) ||
1036 0 == strncmpic(csp->config->usermanual, "https://", 8))
1038 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
1039 "received while user-manual delivery is disabled.");
1040 return cgi_error_404(csp, rsp, parameters);
1043 if (!parameters->first)
1045 /* requested http://p.p/user-manual (without trailing slash) */
1046 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1049 get_string_param(parameters, "file", &filename);
1050 if (filename == NULL)
1052 /* It's '/' so serve the index.html if there is one. */
1053 filename = "index.html";
1055 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
1058 * We currently only support a flat file
1059 * hierarchy for the documentation.
1061 log_error(LOG_LEVEL_ERROR,
1062 "Rejecting the request to serve '%s' as it contains '/' or '..'",
1064 return JB_ERR_CGI_PARAMS;
1067 full_path = make_path(csp->config->usermanual, filename);
1068 if (full_path == NULL)
1070 return JB_ERR_MEMORY;
1073 err = load_file(full_path, &rsp->body, &rsp->content_length);
1074 if (JB_ERR_OK != err)
1076 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1077 if (JB_ERR_FILE == err)
1079 err = cgi_error_no_template(csp, rsp, full_path);
1086 content_type = get_content_type(filename);
1087 log_error(LOG_LEVEL_CGI,
1088 "Content-Type guessed for %s: %s", filename, content_type);
1090 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
1095 #ifdef FEATURE_EXTENDED_STATISTICS
1096 /*********************************************************************
1098 * Function : get_block_reason_statistics_table
1100 * Description : Produces the block reason statistic table content.
1103 * 1 : csp = Current client state (buffers, headers, etc...)
1105 * Returns : Pointer to the HTML statistic table content or
1106 * NULL on out of memory
1108 *********************************************************************/
1109 static char *get_block_reason_statistics_table(const struct client_state *csp)
1111 char buf[BUFFER_SIZE];
1114 struct file_list *fl;
1115 jb_err err = JB_ERR_OK;
1117 statistics = strdup_or_die("");
1119 /* Run through all action files. */
1120 for (i = 0; i < MAX_AF_FILES; i++)
1122 struct url_actions *b;
1123 struct action_spec *last_action = NULL;
1125 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1127 /* Skip empty files */
1131 /* Go through all the actions. */
1132 for (b = b->next; NULL != b; b = b->next)
1134 if (last_action == b->action)
1138 if ((b->action->add & ACTION_BLOCK))
1140 unsigned long long count;
1141 const char *block_reason = b->action->string[ACTION_STRING_BLOCK];
1142 const char *encoded_block_reason = html_encode(block_reason);
1144 if (encoded_block_reason == NULL)
1149 get_block_reason_count(block_reason, &count);
1150 snprintf(buf, sizeof(buf),
1151 "<tr><td>%s</td><td style=\"text-align: right\">%llu</td>\n",
1152 encoded_block_reason, count);
1153 freez(encoded_block_reason);
1155 if (!err) err = string_append(&statistics, buf);
1157 last_action = b->action;
1166 /*********************************************************************
1168 * Function : get_filter_statistics_table
1170 * Description : Produces the filter statistic table content.
1173 * 1 : csp = Current client state (buffers, headers, etc...)
1175 * Returns : Pointer to the HTML statistic table content or
1176 * NULL on out of memory
1178 *********************************************************************/
1179 static char *get_filter_statistics_table(const struct client_state *csp)
1181 char buf[BUFFER_SIZE];
1184 struct file_list *fl;
1185 struct re_filterfile_spec *b;
1186 jb_err err = JB_ERR_OK;
1188 statistics = strdup_or_die("");
1190 for (i = 0; i < MAX_AF_FILES; i++)
1193 if ((NULL == fl) || (NULL == fl->f))
1196 * Either there are no filter files left or this
1197 * filter file just contains no valid filters.
1199 * Continue to be sure we don't miss valid filter
1200 * files that are chained after empty or invalid ones.
1205 for (b = fl->f; b != NULL; b = b->next)
1207 if (b->type == FT_CONTENT_FILTER)
1209 unsigned long long executions;
1210 unsigned long long response_bodies_modified;
1211 unsigned long long hits;
1213 get_filter_statistics(b->name, &executions, &response_bodies_modified, &hits);
1214 snprintf(buf, sizeof(buf),
1215 "<tr><td>%s</td><td style=\"text-align: right\">%llu</td>"
1216 "<td style=\"text-align: right\">%llu</td>"
1217 "<td style=\"text-align: right\">%llu</td><tr>\n",
1218 b->name, executions, response_bodies_modified, hits);
1220 if (!err) err = string_append(&statistics, buf);
1228 #endif /* def FEATURE_EXTENDED_STATISTICS */
1231 /*********************************************************************
1233 * Function : cgi_show_status
1235 * Description : CGI function that returns a web page describing the
1236 * current status of Privoxy.
1239 * 1 : csp = Current client state (buffers, headers, etc...)
1240 * 2 : rsp = http_response data structure for output
1241 * 3 : parameters = map of cgi parameters
1244 * file : Which file to show. Only first letter is checked,
1249 * Default is to show menu and other information.
1251 * Returns : JB_ERR_OK on success
1252 * JB_ERR_MEMORY on out-of-memory error.
1254 *********************************************************************/
1255 jb_err cgi_show_status(struct client_state *csp,
1256 struct http_response *rsp,
1257 const struct map *parameters)
1263 char buf[BUFFER_SIZE];
1264 #ifdef FEATURE_STATISTICS
1265 float perc_rej; /* Percentage of http requests rejected */
1266 int local_urls_read;
1267 int local_urls_rejected;
1268 #endif /* ndef FEATURE_STATISTICS */
1269 jb_err err = JB_ERR_OK;
1271 struct map *exports;
1277 if ('\0' != *(lookup(parameters, "file")))
1279 return cgi_show_file(csp, rsp, parameters);
1282 if (NULL == (exports = default_exports(csp, "show-status")))
1284 return JB_ERR_MEMORY;
1288 for (j = 0; (s != NULL) && (j < Argc); j++)
1290 if (!err) err = string_join (&s, html_encode(Argv[j]));
1291 if (!err) err = string_append(&s, " ");
1293 if (!err) err = map(exports, "invocation", 1, s, 0);
1295 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1296 if (!err) err = show_defines(exports);
1301 return JB_ERR_MEMORY;
1304 #ifdef FEATURE_STATISTICS
1305 local_urls_read = urls_read;
1306 local_urls_rejected = urls_rejected;
1309 * Need to alter the stats not to include the fetch of this
1312 * Can't do following thread safely! doh!
1315 * urls_rejected--; * This will be incremented subsequently *
1318 if (local_urls_read == 0)
1320 if (!err) err = map_block_killer(exports, "have-stats");
1324 if (!err) err = map_block_killer(exports, "have-no-stats");
1326 perc_rej = (float)local_urls_rejected * 100.0F /
1327 (float)local_urls_read;
1329 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1330 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1332 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1333 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1335 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1336 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1339 #else /* ndef FEATURE_STATISTICS */
1340 if (!err) err = map_block_killer(exports, "statistics");
1341 #endif /* ndef FEATURE_STATISTICS */
1343 #ifdef FEATURE_EXTENDED_STATISTICS
1346 char *block_reason_statistics = get_block_reason_statistics_table(csp);
1347 if (block_reason_statistics != NULL)
1349 err = map(exports, "block-reason-statistics", 1, block_reason_statistics, 0);
1353 err = map_block_killer(exports, "extended-statistics");
1358 char *filter_statistics = get_filter_statistics_table(csp);
1359 if (filter_statistics != NULL)
1361 err = map(exports, "filter-statistics", 1, filter_statistics, 0);
1365 err = map_block_killer(exports, "extended-statistics");
1368 #else /* ndef FEATURE_EXTENDED_STATISTICS */
1369 if (!err) err = map_block_killer(exports, "extended-statistics");
1370 #endif /* def FEATURE_EXTENDED_STATISTICS */
1373 * List all action files in use, together with view and edit links,
1374 * except for standard.action, which should only be viewable. (Not
1375 * enforced in the editor itself)
1376 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1379 for (i = 0; i < MAX_AF_FILES; i++)
1381 if (csp->actions_list[i] != NULL)
1383 if (!err) err = string_append(&s, "<tr><td>");
1384 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1385 snprintf(buf, sizeof(buf),
1386 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1387 if (!err) err = string_append(&s, buf);
1389 #ifdef FEATURE_CGI_EDIT_ACTIONS
1390 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1391 && (NULL != csp->config->actions_file_short[i]))
1394 if (access(csp->config->actions_file[i], W_OK) == 0)
1396 #endif /* def HAVE_ACCESS */
1397 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1398 if (!err) err = string_append(&s, buf);
1403 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1405 #endif /* def HAVE_ACCESS */
1409 if (!err) err = string_append(&s, "</td></tr>\n");
1412 if (!err && *s != '\0')
1414 err = map(exports, "actions-filenames", 1, s, 0);
1418 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1423 * List all re_filterfiles in use, together with view options.
1424 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1427 for (i = 0; i < MAX_AF_FILES; i++)
1429 if (csp->rlist[i] != NULL)
1431 if (!err) err = string_append(&s, "<tr><td>");
1432 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1433 snprintf(buf, sizeof(buf),
1434 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1435 if (!err) err = string_append(&s, buf);
1436 if (!err) err = string_append(&s, "</td></tr>\n");
1439 if (!err && *s != '\0')
1441 err = map(exports, "re-filter-filenames", 1, s, 0);
1445 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1446 if (!err) err = map_block_killer(exports, "have-filterfile");
1450 #ifdef FEATURE_TRUST
1453 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1457 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1458 if (!err) err = map_block_killer(exports, "have-trustfile");
1461 if (!err) err = map_block_killer(exports, "trust-support");
1462 #endif /* ndef FEATURE_TRUST */
1464 #ifdef FEATURE_CGI_EDIT_ACTIONS
1465 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1467 err = map_block_killer(exports, "cgi-editor-is-disabled");
1469 #endif /* ndef CGI_EDIT_ACTIONS */
1471 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1476 return JB_ERR_MEMORY;
1479 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1483 /*********************************************************************
1485 * Function : cgi_show_url_info
1487 * Description : CGI function that determines and shows which actions
1488 * Privoxy will perform for a given url, and which
1489 * matches starting from the defaults have lead to that.
1492 * 1 : csp = Current client state (buffers, headers, etc...)
1493 * 2 : rsp = http_response data structure for output
1494 * 3 : parameters = map of cgi parameters
1497 * url : The url whose actions are to be determined.
1498 * If url is unset, the url-given conditional will be
1499 * set, so that all but the form can be suppressed in
1502 * Returns : JB_ERR_OK on success
1503 * JB_ERR_MEMORY on out-of-memory error.
1505 *********************************************************************/
1506 jb_err cgi_show_url_info(struct client_state *csp,
1507 struct http_response *rsp,
1508 const struct map *parameters)
1511 struct map *exports;
1518 if (NULL == (exports = default_exports(csp, "show-url-info")))
1520 return JB_ERR_MEMORY;
1524 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1526 url_param = strdup_or_die(lookup(parameters, "url"));
1530 * Handle prefixes. 4 possibilities:
1531 * 1) "http://" or "https://" prefix present and followed by URL - OK
1532 * 2) Only the "http://" or "https://" part is present, no URL - change
1533 * to empty string so it will be detected later as "no URL".
1534 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1536 * 4) Parameter not specified or is empty string - let this fall through
1537 * for now, next block of code will handle it.
1539 if (0 == strncmp(url_param, "http://", 7))
1541 if (url_param[7] == '\0')
1544 * Empty URL (just prefix).
1545 * Make it totally empty so it's caught by the next if ()
1547 url_param[0] = '\0';
1550 else if (0 == strncmp(url_param, "https://", 8))
1552 if (url_param[8] == '\0')
1555 * Empty URL (just prefix).
1556 * Make it totally empty so it's caught by the next if ()
1558 url_param[0] = '\0';
1561 else if ((url_param[0] != '\0')
1562 && ((NULL == strstr(url_param, "://")
1563 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1566 * No prefix or at least no prefix before
1567 * the first slash - assume http://
1569 char *url_param_prefixed = strdup_or_die("http://");
1571 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1574 return JB_ERR_MEMORY;
1576 url_param = url_param_prefixed;
1580 * Hide "toggle off" warning if Privoxy is toggled on.
1583 #ifdef FEATURE_TOGGLE
1584 (global_toggle_state == 1) &&
1585 #endif /* def FEATURE_TOGGLE */
1586 map_block_killer(exports, "privoxy-is-toggled-off")
1591 return JB_ERR_MEMORY;
1594 if (url_param[0] == '\0')
1596 /* URL parameter not specified, display query form only. */
1598 if (map_block_killer(exports, "url-given")
1599 || map(exports, "url", 1, "", 1))
1602 return JB_ERR_MEMORY;
1607 /* Given a URL, so query it. */
1612 struct file_list *fl;
1613 struct url_actions *b;
1614 struct http_request url_to_query[1];
1615 struct current_action_spec action[1];
1618 if (map(exports, "url", 1, html_encode(url_param), 0))
1622 return JB_ERR_MEMORY;
1625 init_current_action(action);
1627 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1629 free_current_action(action);
1632 return JB_ERR_MEMORY;
1635 memset(url_to_query, '\0', sizeof(url_to_query));
1636 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1637 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1641 if (err == JB_ERR_MEMORY)
1643 free_http_request(url_to_query);
1644 free_current_action(action);
1646 return JB_ERR_MEMORY;
1652 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1653 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1654 if (!err) err = map_block_killer(exports, "valid-url");
1656 free_current_action(action);
1657 free_http_request(url_to_query);
1662 return JB_ERR_MEMORY;
1665 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1669 * We have a warning about SSL paths. Hide it for unencrypted sites
1670 * and unconditionally if https inspection is enabled.
1672 #ifndef FEATURE_HTTPS_INSPECTION
1673 if (!url_to_query->ssl)
1676 if (map_block_killer(exports, "https-and-no-https-inspection"))
1678 free_current_action(action);
1680 free_http_request(url_to_query);
1681 return JB_ERR_MEMORY;
1685 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1687 for (i = 0; i < MAX_AF_FILES; i++)
1689 if (NULL == csp->config->actions_file_short[i]
1690 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1694 if ((fl = csp->actions_list[i]) != NULL)
1696 if ((b = fl->f) != NULL)
1698 /* FIXME: Hardcoded HTML! */
1699 string_append(&matches, "<tr><th>In file: ");
1700 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1701 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1702 string_append(&matches, buf);
1703 string_append(&matches, "View</a>");
1704 #ifdef FEATURE_CGI_EDIT_ACTIONS
1705 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1708 if (access(csp->config->actions_file[i], W_OK) == 0)
1710 #endif /* def HAVE_ACCESS */
1711 snprintf(buf, sizeof(buf),
1712 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1713 string_append(&matches, buf);
1714 string_append(&matches, "Edit</a>");
1719 string_append(&matches, " <strong>No write access.</strong>");
1721 #endif /* def HAVE_ACCESS */
1723 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1725 string_append(&matches, "</th></tr>\n");
1732 for (; (b != NULL) && (matches != NULL); b = b->next)
1734 if (url_match(b->url, url_to_query))
1736 string_append(&matches, "<tr><td>{");
1737 string_join (&matches, actions_to_html(csp, b->action));
1738 string_append(&matches, " }<br>\n<code>");
1739 string_join (&matches, html_encode(b->url->spec));
1740 string_append(&matches, "</code></td></tr>\n");
1742 if (merge_current_action(action, b->action))
1745 free_http_request(url_to_query);
1746 free_current_action(action);
1748 return JB_ERR_MEMORY;
1756 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1759 string_append(&matches, "</table>\n");
1762 * XXX: Kludge to make sure the "Forward settings" section
1763 * shows what forward-override{} would do with the requested URL.
1764 * No one really cares how the CGI request would be forwarded
1765 * if it wasn't intercepted as CGI request in the first place.
1767 * From here on the action bitmask will no longer reflect
1768 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1769 * but luckily it's no longer required later on anyway.
1771 free_current_action(csp->action);
1772 get_url_actions(csp, url_to_query);
1775 * Fill in forwarding settings.
1777 * The possibilities are:
1779 * - http forwarding only
1780 * - socks4(a) forwarding only
1781 * - socks4(a) and http forwarding.
1783 * XXX: Parts of this code could be reused for the
1784 * "forwarding-failed" template which currently doesn't
1785 * display the proxy port and an eventual second forwarder.
1788 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1790 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1792 if (!err) err = map_block_killer(exports, "socks-forwarder");
1793 if (!err) err = map_block_killer(exports, "http-forwarder");
1797 char port[10]; /* We save proxy ports as int but need a string here */
1799 if (!err) err = map_block_killer(exports, "no-forwarder");
1801 if (fwd->gateway_host != NULL)
1803 char *socks_type = NULL;
1808 socks_type = "socks4";
1811 socks_type = "socks4a";
1814 socks_type = "socks5";
1817 socks_type = "socks5t";
1820 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1823 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1824 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1825 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1826 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1830 if (!err) err = map_block_killer(exports, "socks-forwarder");
1833 if (fwd->forward_host != NULL)
1835 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1836 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1837 if (!err) err = map(exports, "forward-port", 1, port, 1);
1841 if (!err) err = map_block_killer(exports, "http-forwarder");
1846 free_http_request(url_to_query);
1848 if (err || matches == NULL)
1850 free_current_action(action);
1852 return JB_ERR_MEMORY;
1855 #ifdef FEATURE_CGI_EDIT_ACTIONS
1856 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1858 err = map_block_killer(exports, "cgi-editor-is-disabled");
1860 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1863 * If zlib support is available, if no content filters
1864 * are enabled or if the prevent-compression action is enabled,
1865 * suppress the "compression could prevent filtering" warning.
1867 #ifndef FEATURE_ZLIB
1868 if (!content_filters_enabled(action) ||
1869 (action->flags & ACTION_NO_COMPRESSION))
1872 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1875 if (err || map(exports, "matches", 1, matches , 0))
1877 free_current_action(action);
1879 return JB_ERR_MEMORY;
1882 s = current_action_to_html(csp, action);
1884 free_current_action(action);
1886 if (map(exports, "final", 1, s, 0))
1889 return JB_ERR_MEMORY;
1893 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1897 /*********************************************************************
1899 * Function : cgi_robots_txt
1901 * Description : CGI function to return "/robots.txt".
1904 * 1 : csp = Current client state (buffers, headers, etc...)
1905 * 2 : rsp = http_response data structure for output
1906 * 3 : parameters = map of cgi parameters
1908 * CGI Parameters : None
1910 * Returns : JB_ERR_OK on success
1911 * JB_ERR_MEMORY on out-of-memory error.
1913 *********************************************************************/
1914 jb_err cgi_robots_txt(struct client_state *csp,
1915 struct http_response *rsp,
1916 const struct map *parameters)
1924 rsp->body = strdup_or_die(
1925 "# This is the Privoxy control interface.\n"
1926 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1933 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1937 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1938 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1940 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1944 /*********************************************************************
1946 * Function : show_defines
1948 * Description : Add to a map the state of all conditional #defines
1949 * used when building
1952 * 1 : exports = map to extend
1954 * Returns : JB_ERR_OK on success
1955 * JB_ERR_MEMORY on out-of-memory error.
1957 *********************************************************************/
1958 static jb_err show_defines(struct map *exports)
1960 jb_err err = JB_ERR_OK;
1963 const char name[31];
1964 const unsigned char is_available;
1967 static const struct feature features[] = {
1969 "FEATURE_64_BIT_TIME_T",
1970 #if (SIZEOF_TIME_T == 8)
1977 "FEATURE_ACCEPT_FILTER",
1978 #ifdef FEATURE_ACCEPT_FILTER
1994 #ifdef FEATURE_BROTLI
2001 "FEATURE_CGI_EDIT_ACTIONS",
2002 #ifdef FEATURE_CGI_EDIT_ACTIONS
2009 "FEATURE_CLIENT_TAGS",
2010 #ifdef FEATURE_CLIENT_TAGS
2017 "FEATURE_COMPRESSION",
2018 #ifdef FEATURE_COMPRESSION
2025 "FEATURE_CONNECTION_KEEP_ALIVE",
2026 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2033 "FEATURE_CONNECTION_SHARING",
2034 #ifdef FEATURE_CONNECTION_SHARING
2041 "FEATURE_EXTERNAL_FILTERS",
2042 #ifdef FEATURE_EXTERNAL_FILTERS
2049 "FEATURE_FAST_REDIRECTS",
2050 #ifdef FEATURE_FAST_REDIRECTS
2057 "FEATURE_FORCE_LOAD",
2058 #ifdef FEATURE_FORCE_LOAD
2065 "FEATURE_GRACEFUL_TERMINATION",
2066 #ifdef FEATURE_GRACEFUL_TERMINATION
2073 "FEATURE_HTTPS_INSPECTION",
2074 #ifdef FEATURE_HTTPS_INSPECTION
2081 "FEATURE_IMAGE_BLOCKING",
2082 #ifdef FEATURE_IMAGE_BLOCKING
2089 "FEATURE_IPV6_SUPPORT",
2098 #ifdef FEATURE_NO_GIFS
2106 #ifdef FEATURE_PTHREAD
2113 "FEATURE_STATISTICS",
2114 #ifdef FEATURE_STATISTICS
2121 "FEATURE_STRPTIME_SANITY_CHECKS",
2122 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
2130 #ifdef FEATURE_TOGGLE
2138 #ifdef FEATURE_TRUST
2153 "FEATURE_DYNAMIC_PCRE",
2154 #ifdef FEATURE_DYNAMIC_PCRE
2161 "FEATURE_EXTENDED_STATISTICS",
2162 #ifdef FEATURE_EXTENDED_STATISTICS
2169 "FEATURE_PCRE_HOST_PATTERNS",
2170 #ifdef FEATURE_PCRE_HOST_PATTERNS
2178 for (i = 0; i < SZ(features); i++)
2180 err = map_conditional(exports, features[i].name, features[i].is_available);
2192 /*********************************************************************
2194 * Function : cgi_show_file
2196 * Description : CGI function that shows the content of a
2197 * configuration file.
2200 * 1 : csp = Current client state (buffers, headers, etc...)
2201 * 2 : rsp = http_response data structure for output
2202 * 3 : parameters = map of cgi parameters
2205 * file : Which file to show. Only first letter is checked,
2210 * Default is to show menu and other information.
2212 * Returns : JB_ERR_OK on success
2213 * JB_ERR_MEMORY on out-of-memory error.
2215 *********************************************************************/
2216 static jb_err cgi_show_file(struct client_state *csp,
2217 struct http_response *rsp,
2218 const struct map *parameters)
2221 const char * filename = NULL;
2222 char * file_description = NULL;
2228 switch (*(lookup(parameters, "file")))
2231 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2233 filename = csp->actions_list[i]->filename;
2234 file_description = "Actions File";
2239 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2241 filename = csp->rlist[i]->filename;
2242 file_description = "Filter File";
2246 #ifdef FEATURE_TRUST
2250 filename = csp->tlist->filename;
2251 file_description = "Trust File";
2254 #endif /* def FEATURE_TRUST */
2257 if (NULL != filename)
2259 struct map *exports;
2264 exports = default_exports(csp, "show-status");
2265 if (NULL == exports)
2267 return JB_ERR_MEMORY;
2270 if (map(exports, "file-description", 1, file_description, 1)
2271 || map(exports, "filepath", 1, html_encode(filename), 0))
2274 return JB_ERR_MEMORY;
2277 err = load_file(filename, &s, &length);
2278 if (JB_ERR_OK != err)
2280 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2283 return JB_ERR_MEMORY;
2288 s = html_encode_and_free_original(s);
2292 return JB_ERR_MEMORY;
2295 if (map(exports, "contents", 1, s, 0))
2298 return JB_ERR_MEMORY;
2302 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2305 return JB_ERR_CGI_PARAMS;
2309 /*********************************************************************
2311 * Function : load_file
2313 * Description : Loads a file into a buffer.
2316 * 1 : filename = Name of the file to be loaded.
2317 * 2 : buffer = Used to return the file's content.
2318 * 3 : length = Used to return the size of the file.
2320 * Returns : JB_ERR_OK in case of success,
2321 * JB_ERR_FILE in case of ordinary file loading errors
2322 * (fseek() and ftell() errors are fatal)
2323 * JB_ERR_MEMORY in case of out-of-memory.
2325 *********************************************************************/
2326 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2330 jb_err err = JB_ERR_OK;
2332 fp = fopen(filename, "rb");
2335 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
2339 /* Get file length */
2340 if (fseek(fp, 0, SEEK_END))
2342 log_error(LOG_LEVEL_FATAL,
2343 "Unexpected error while fseek()ing to the end of %s: %E",
2349 log_error(LOG_LEVEL_FATAL,
2350 "Unexpected ftell() error while loading %s: %E",
2353 *length = (size_t)ret;
2355 /* Go back to the beginning. */
2356 if (fseek(fp, 0, SEEK_SET))
2358 log_error(LOG_LEVEL_FATAL,
2359 "Unexpected error while fseek()ing to the beginning of %s: %E",
2363 *buffer = zalloc_or_die(*length + 1);
2365 if (1 != fread(*buffer, *length, 1, fp))
2368 * May theoretically happen if the file size changes between
2369 * fseek() and fread() because it's edited in-place. Privoxy
2370 * and common text editors don't do that, thus we just fail.
2372 log_error(LOG_LEVEL_ERROR,
2373 "Couldn't completely read file %s.", filename);