1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.135 2016/03/04 13:22:22 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
6 * Purpose : Simple CGIs to get information about Privoxy's
9 * Copyright : Written by and Copyright (C) 2001-2016 the
10 * Privoxy team. http://www.privoxy.org/
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 **********************************************************************/
40 #include <sys/types.h>
46 #if defined (HAVE_ACCESS) && defined (HAVE_UNISTD_H)
48 #endif /* def HAVE_ACCESS && HAVE_UNISTD_H */
52 #include "cgisimple.h"
63 #ifdef FEATURE_CLIENT_TAGS
64 #include "client-tags.h"
67 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
69 static char *show_rcs(void);
70 static jb_err show_defines(struct map *exports);
71 static jb_err cgi_show_file(struct client_state *csp,
72 struct http_response *rsp,
73 const struct map *parameters);
74 static jb_err load_file(const char *filename, char **buffer, size_t *length);
76 /*********************************************************************
78 * Function : cgi_default
80 * Description : CGI function that is called for the CGI_SITE_1_HOST
81 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
82 * Boring - only exports the default exports.
85 * 1 : csp = Current client state (buffers, headers, etc...)
86 * 2 : rsp = http_response data structure for output
87 * 3 : parameters = map of cgi parameters
89 * CGI Parameters : none
91 * Returns : JB_ERR_OK on success
92 * JB_ERR_MEMORY on out-of-memory
94 *********************************************************************/
95 jb_err cgi_default(struct client_state *csp,
96 struct http_response *rsp,
97 const struct map *parameters)
106 if (NULL == (exports = default_exports(csp, "")))
108 return JB_ERR_MEMORY;
111 return template_fill_for_cgi(csp, "default", exports, rsp);
115 /*********************************************************************
117 * Function : cgi_error_404
119 * Description : CGI function that is called if an unknown action was
123 * 1 : csp = Current client state (buffers, headers, etc...)
124 * 2 : rsp = http_response data structure for output
125 * 3 : parameters = map of cgi parameters
127 * CGI Parameters : none
129 * Returns : JB_ERR_OK on success
130 * JB_ERR_MEMORY on out-of-memory error.
132 *********************************************************************/
133 jb_err cgi_error_404(struct client_state *csp,
134 struct http_response *rsp,
135 const struct map *parameters)
143 if (NULL == (exports = default_exports(csp, NULL)))
145 return JB_ERR_MEMORY;
148 rsp->status = strdup_or_die("404 Privoxy configuration page not found");
150 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
154 #ifdef FEATURE_GRACEFUL_TERMINATION
155 /*********************************************************************
159 * Description : CGI function to shut down Privoxy.
160 * NOTE: Turning this on in a production build
161 * would be a BAD idea. An EXTREMELY BAD idea.
162 * In short, don't do it.
165 * 1 : csp = Current client state (buffers, headers, etc...)
166 * 2 : rsp = http_response data structure for output
167 * 3 : parameters = map of cgi parameters
169 * CGI Parameters : none
171 * Returns : JB_ERR_OK on success
173 *********************************************************************/
174 jb_err cgi_die (struct client_state *csp,
175 struct http_response *rsp,
176 const struct map *parameters)
178 static const char status[] = "200 OK Privoxy shutdown request received";
179 static const char body[] =
182 " <title>Privoxy shutdown request received</title>\n"
183 " <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">\n"
184 " <link rel=\"stylesheet\" type=\"text/css\" href=\"" CGI_PREFIX "send-stylesheet\">\n"
187 "<h1>Privoxy shutdown request received</h1>\n"
188 "<p>Privoxy is going to shut down after the next request.</p>\n"
199 csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
201 rsp->content_length = 0;
202 rsp->head_length = 0;
205 rsp->body = strdup_or_die(body);
206 rsp->status = strdup_or_die(status);
210 #endif /* def FEATURE_GRACEFUL_TERMINATION */
213 /*********************************************************************
215 * Function : cgi_show_request
217 * Description : Show the client's request and what sed() would have
221 * 1 : csp = Current client state (buffers, headers, etc...)
222 * 2 : rsp = http_response data structure for output
223 * 3 : parameters = map of cgi parameters
225 * CGI Parameters : none
227 * Returns : JB_ERR_OK on success
228 * JB_ERR_MEMORY on out-of-memory error.
230 *********************************************************************/
231 jb_err cgi_show_request(struct client_state *csp,
232 struct http_response *rsp,
233 const struct map *parameters)
242 if (NULL == (exports = default_exports(csp, "show-request")))
244 return JB_ERR_MEMORY;
248 * Repair the damage done to the IOB by get_header()
250 for (p = csp->client_iob->buf; p < csp->client_iob->cur; p++)
252 if (*p == '\0') *p = '\n';
256 * Export the original client's request and the one we would
257 * be sending to the server if this wasn't a CGI call
260 if (map(exports, "client-request", 1, html_encode(csp->client_iob->buf), 0))
263 return JB_ERR_MEMORY;
266 if (map(exports, "processed-request", 1,
267 html_encode_and_free_original(list_to_text(csp->headers)), 0))
270 return JB_ERR_MEMORY;
273 return template_fill_for_cgi(csp, "show-request", exports, rsp);
277 #ifdef FEATURE_CLIENT_TAGS
278 /*********************************************************************
280 * Function : cgi_show_client_tags
282 * Description : Shows the tags that can be set based on the client
286 * 1 : csp = Current client state (buffers, headers, etc...)
287 * 2 : rsp = http_response data structure for output
288 * 3 : parameters = map of cgi parameters
290 * CGI Parameters : none
292 * Returns : JB_ERR_OK on success
293 * JB_ERR_MEMORY on out-of-memory error.
295 *********************************************************************/
296 jb_err cgi_show_client_tags(struct client_state *csp,
297 struct http_response *rsp,
298 const struct map *parameters)
301 struct client_tag_spec *this_tag;
302 jb_err err = JB_ERR_OK;
303 const char *toggled_tag;
304 const char *toggle_state;
305 const char *tag_expires;
307 char *client_tags = strdup_or_die("");
314 if (NULL == (exports = default_exports(csp, "show-client-tags")))
316 return JB_ERR_MEMORY;
319 toggled_tag = lookup(parameters, "tag");
320 if (*toggled_tag != '\0')
322 tag_expires = lookup(parameters, "expires");
323 if (*tag_expires == '0')
329 time_to_live = csp->config->client_tag_lifetime;
331 toggle_state = lookup(parameters, "toggle-state");
332 if (*toggle_state == '1')
334 enable_client_specific_tag(csp, toggled_tag, time_to_live);
338 disable_client_specific_tag(csp, toggled_tag);
342 this_tag = csp->config->client_tags;
343 if (this_tag->name == NULL)
345 if (!err) err = string_append(&client_tags, "<p>No tags available.</p>\n");
351 err = string_append(&client_tags, "<table border=\"1\">\n"
352 "<tr><th>Tag name</th>\n"
353 "<th>Current state</th><th>Change state</th><th>Description</th></tr>\n");
355 while ((this_tag != NULL) && (this_tag->name != NULL))
359 privoxy_mutex_lock(&client_tags_mutex);
360 tag_state = client_has_requested_tag(csp->ip_addr_str, this_tag->name);
361 privoxy_mutex_unlock(&client_tags_mutex);
362 if (!err) err = string_append(&client_tags, "<tr><td>");
363 if (!err) err = string_append(&client_tags, this_tag->name);
364 if (!err) err = string_append(&client_tags, "</td><td>");
365 if (!err) err = string_append(&client_tags, tag_state == 1 ? "Enabled" : "Disabled");
366 snprintf(buf, sizeof(buf),
367 "</td><td><a href=\"/show-client-tags?tag=%s&toggle-state=%d&expires=0\">%s</a>",
368 this_tag->name, !tag_state, tag_state == 1 ? "Disable" : "Enable");
369 if (!err) err = string_append(&client_tags, buf);
372 snprintf(buf, sizeof(buf), ". <a href=\"/show-client-tags?"
373 "tag=%s&toggle-state=1&expires=1\">Enable temporarily</a>",
375 if (!err) err = string_append(&client_tags, buf);
377 if (!err) err = string_append(&client_tags, "</td><td>");
378 if (!err) err = string_append(&client_tags, this_tag->description);
379 if (!err) err = string_append(&client_tags, "</td></tr>\n");
383 return JB_ERR_MEMORY;
385 this_tag = this_tag->next;
387 if (!err) err = string_append(&client_tags, "</table>\n");
390 if (map(exports, "client-tags", 1, client_tags, 0))
393 return JB_ERR_MEMORY;
396 if (map(exports, "client-ip-addr", 1, csp->ip_addr_str, 1))
399 return JB_ERR_MEMORY;
402 return template_fill_for_cgi(csp, "show-client-tags", exports, rsp);
404 #endif /* def FEATURE_CLIENT_TAGS */
407 /*********************************************************************
409 * Function : cgi_send_banner
411 * Description : CGI function that returns a banner.
414 * 1 : csp = Current client state (buffers, headers, etc...)
415 * 2 : rsp = http_response data structure for output
416 * 3 : parameters = map of cgi parameters
419 * type : Selects the type of banner between "trans", "logo",
420 * and "auto". Defaults to "logo" if absent or invalid.
421 * "auto" means to select as if we were image-blocking.
422 * (Only the first character really counts; b and t are
425 * Returns : JB_ERR_OK on success
426 * JB_ERR_MEMORY on out-of-memory error.
428 *********************************************************************/
429 jb_err cgi_send_banner(struct client_state *csp,
430 struct http_response *rsp,
431 const struct map *parameters)
433 char imagetype = lookup(parameters, "type")[0];
436 * If type is auto, then determine the right thing
437 * to do from the set-image-blocker action
439 if (imagetype == 'a')
446 #ifdef FEATURE_IMAGE_BLOCKING
447 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
449 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
450 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
451 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
455 /* Use default - nothing to do here. */
457 else if (0 == strcmpic(p, "blank"))
461 else if (0 == strcmpic(p, "pattern"))
467 * If the action is to call this CGI, determine
470 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
472 imagetype = p[sizeof(prefix1) - 1];
474 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
476 imagetype = p[sizeof(prefix2) - 1];
480 * Everything else must (should) be a URL to
488 #endif /* def FEATURE_IMAGE_BLOCKING */
492 * Now imagetype is either the non-auto type we were called with,
493 * or it was auto and has since been determined. In any case, we
494 * can proceed to actually answering the request by sending a redirect
495 * or an image as appropriate:
497 if (imagetype == 'r')
499 rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
500 if (enlist_unique_header(rsp->headers, "Location",
501 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
503 return JB_ERR_MEMORY;
508 if ((imagetype == 'b') || (imagetype == 't'))
510 rsp->body = bindup(image_blank_data, image_blank_length);
511 rsp->content_length = image_blank_length;
515 rsp->body = bindup(image_pattern_data, image_pattern_length);
516 rsp->content_length = image_pattern_length;
519 if (rsp->body == NULL)
521 return JB_ERR_MEMORY;
523 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
525 return JB_ERR_MEMORY;
536 /*********************************************************************
538 * Function : cgi_transparent_image
540 * Description : CGI function that sends a 1x1 transparent image.
543 * 1 : csp = Current client state (buffers, headers, etc...)
544 * 2 : rsp = http_response data structure for output
545 * 3 : parameters = map of cgi parameters
547 * CGI Parameters : None
549 * Returns : JB_ERR_OK on success
550 * JB_ERR_MEMORY on out-of-memory error.
552 *********************************************************************/
553 jb_err cgi_transparent_image(struct client_state *csp,
554 struct http_response *rsp,
555 const struct map *parameters)
560 rsp->body = bindup(image_blank_data, image_blank_length);
561 rsp->content_length = image_blank_length;
563 if (rsp->body == NULL)
565 return JB_ERR_MEMORY;
568 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
570 return JB_ERR_MEMORY;
580 /*********************************************************************
582 * Function : cgi_send_default_favicon
584 * Description : CGI function that sends the standard favicon.
587 * 1 : csp = Current client state (buffers, headers, etc...)
588 * 2 : rsp = http_response data structure for output
589 * 3 : parameters = map of cgi parameters
591 * CGI Parameters : None
593 * Returns : JB_ERR_OK on success
594 * JB_ERR_MEMORY on out-of-memory error.
596 *********************************************************************/
597 jb_err cgi_send_default_favicon(struct client_state *csp,
598 struct http_response *rsp,
599 const struct map *parameters)
601 static const char default_favicon_data[] =
602 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
603 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
604 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
605 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
606 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
607 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
608 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
609 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
610 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
611 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
612 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
613 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
614 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
616 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
621 rsp->body = bindup(default_favicon_data, favicon_length);
622 rsp->content_length = favicon_length;
624 if (rsp->body == NULL)
626 return JB_ERR_MEMORY;
629 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
631 return JB_ERR_MEMORY;
641 /*********************************************************************
643 * Function : cgi_send_error_favicon
645 * Description : CGI function that sends the favicon for error pages.
648 * 1 : csp = Current client state (buffers, headers, etc...)
649 * 2 : rsp = http_response data structure for output
650 * 3 : parameters = map of cgi parameters
652 * CGI Parameters : None
654 * Returns : JB_ERR_OK on success
655 * JB_ERR_MEMORY on out-of-memory error.
657 *********************************************************************/
658 jb_err cgi_send_error_favicon(struct client_state *csp,
659 struct http_response *rsp,
660 const struct map *parameters)
662 static const char error_favicon_data[] =
663 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
664 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
665 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
666 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
667 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
668 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
669 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
670 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
671 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
672 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
673 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
674 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
675 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
677 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
682 rsp->body = bindup(error_favicon_data, favicon_length);
683 rsp->content_length = favicon_length;
685 if (rsp->body == NULL)
687 return JB_ERR_MEMORY;
690 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
692 return JB_ERR_MEMORY;
702 /*********************************************************************
704 * Function : cgi_send_stylesheet
706 * Description : CGI function that sends a css stylesheet found
707 * in the cgi-style.css template
710 * 1 : csp = Current client state (buffers, headers, etc...)
711 * 2 : rsp = http_response data structure for output
712 * 3 : parameters = map of cgi parameters
714 * CGI Parameters : None
716 * Returns : JB_ERR_OK on success
717 * JB_ERR_MEMORY on out-of-memory error.
719 *********************************************************************/
720 jb_err cgi_send_stylesheet(struct client_state *csp,
721 struct http_response *rsp,
722 const struct map *parameters)
731 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
733 if (err == JB_ERR_FILE)
736 * No way to tell user; send empty stylesheet
738 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
742 return err; /* JB_ERR_MEMORY */
745 if (enlist(rsp->headers, "Content-Type: text/css"))
747 return JB_ERR_MEMORY;
755 /*********************************************************************
757 * Function : cgi_send_url_info_osd
759 * Description : CGI function that sends the OpenSearch Description
760 * template for the show-url-info page. It allows to
761 * access the page through "search engine plugins".
764 * 1 : csp = Current client state (buffers, headers, etc...)
765 * 2 : rsp = http_response data structure for output
766 * 3 : parameters = map of cgi parameters
768 * CGI Parameters : None
770 * Returns : JB_ERR_OK on success
771 * JB_ERR_MEMORY on out-of-memory error.
773 *********************************************************************/
774 jb_err cgi_send_url_info_osd(struct client_state *csp,
775 struct http_response *rsp,
776 const struct map *parameters)
778 jb_err err = JB_ERR_MEMORY;
779 struct map *exports = default_exports(csp, NULL);
786 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
787 if (JB_ERR_OK == err)
789 err = enlist(rsp->headers,
790 "Content-Type: application/opensearchdescription+xml");
799 /*********************************************************************
801 * Function : get_content_type
803 * Description : Use the file extension to guess the content type
804 * header we should use to serve the file.
807 * 1 : filename = Name of the file whose content type
810 * Returns : The guessed content type.
812 *********************************************************************/
813 static const char *get_content_type(const char *filename)
818 const char extension[6];
819 const char content_type[11];
821 static const struct content_type content_types[] =
823 {".css", "text/css"},
824 {".jpg", "image/jpeg"},
825 {".jpeg", "image/jpeg"},
826 {".png", "image/png"},
829 for (i = 0; i < SZ(content_types); i++)
831 if (strstr(filename, content_types[i].extension))
833 return content_types[i].content_type;
837 /* No match by extension, default to html */
841 /*********************************************************************
843 * Function : cgi_send_user_manual
845 * Description : CGI function that sends a file in the user
849 * 1 : csp = Current client state (buffers, headers, etc...)
850 * 2 : rsp = http_response data structure for output
851 * 3 : parameters = map of cgi parameters
853 * CGI Parameters : file=name.html, the name of the HTML file
854 * (relative to user-manual from config)
856 * Returns : JB_ERR_OK on success
857 * JB_ERR_MEMORY on out-of-memory error.
859 *********************************************************************/
860 jb_err cgi_send_user_manual(struct client_state *csp,
861 struct http_response *rsp,
862 const struct map *parameters)
864 const char *filename;
866 jb_err err = JB_ERR_OK;
867 const char *content_type;
873 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
875 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
876 "received while user-manual delivery is disabled.");
877 return cgi_error_404(csp, rsp, parameters);
880 if (!parameters->first)
882 /* requested http://p.p/user-manual (without trailing slash) */
883 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
886 get_string_param(parameters, "file", &filename);
887 if (filename == NULL)
889 /* It's '/' so serve the index.html if there is one. */
890 filename = "index.html";
892 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
895 * We currently only support a flat file
896 * hierarchy for the documentation.
898 log_error(LOG_LEVEL_ERROR,
899 "Rejecting the request to serve '%s' as it contains '/' or '..'",
901 return JB_ERR_CGI_PARAMS;
904 full_path = make_path(csp->config->usermanual, filename);
905 if (full_path == NULL)
907 return JB_ERR_MEMORY;
910 err = load_file(full_path, &rsp->body, &rsp->content_length);
911 if (JB_ERR_OK != err)
913 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
914 if (JB_ERR_FILE == err)
916 err = cgi_error_no_template(csp, rsp, full_path);
923 content_type = get_content_type(filename);
924 log_error(LOG_LEVEL_CGI,
925 "Content-Type guessed for %s: %s", filename, content_type);
927 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
932 /*********************************************************************
934 * Function : cgi_show_version
936 * Description : CGI function that returns a a web page describing the
937 * file versions of Privoxy.
940 * 1 : csp = Current client state (buffers, headers, etc...)
941 * 2 : rsp = http_response data structure for output
942 * 3 : parameters = map of cgi parameters
944 * CGI Parameters : none
946 * Returns : JB_ERR_OK on success
947 * JB_ERR_MEMORY on out-of-memory error.
949 *********************************************************************/
950 jb_err cgi_show_version(struct client_state *csp,
951 struct http_response *rsp,
952 const struct map *parameters)
960 if (NULL == (exports = default_exports(csp, "show-version")))
962 return JB_ERR_MEMORY;
965 if (map(exports, "sourceversions", 1, show_rcs(), 0))
968 return JB_ERR_MEMORY;
971 return template_fill_for_cgi(csp, "show-version", exports, rsp);
975 /*********************************************************************
977 * Function : cgi_show_status
979 * Description : CGI function that returns a web page describing the
980 * current status of Privoxy.
983 * 1 : csp = Current client state (buffers, headers, etc...)
984 * 2 : rsp = http_response data structure for output
985 * 3 : parameters = map of cgi parameters
988 * file : Which file to show. Only first letter is checked,
993 * Default is to show menu and other information.
995 * Returns : JB_ERR_OK on success
996 * JB_ERR_MEMORY on out-of-memory error.
998 *********************************************************************/
999 jb_err cgi_show_status(struct client_state *csp,
1000 struct http_response *rsp,
1001 const struct map *parameters)
1007 char buf[BUFFER_SIZE];
1008 #ifdef FEATURE_STATISTICS
1009 float perc_rej; /* Percentage of http requests rejected */
1010 int local_urls_read;
1011 int local_urls_rejected;
1012 #endif /* ndef FEATURE_STATISTICS */
1013 jb_err err = JB_ERR_OK;
1015 struct map *exports;
1021 if ('\0' != *(lookup(parameters, "file")))
1023 return cgi_show_file(csp, rsp, parameters);
1026 if (NULL == (exports = default_exports(csp, "show-status")))
1028 return JB_ERR_MEMORY;
1032 for (j = 0; (s != NULL) && (j < Argc); j++)
1034 if (!err) err = string_join (&s, html_encode(Argv[j]));
1035 if (!err) err = string_append(&s, " ");
1037 if (!err) err = map(exports, "invocation", 1, s, 0);
1039 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1040 if (!err) err = show_defines(exports);
1045 return JB_ERR_MEMORY;
1048 #ifdef FEATURE_STATISTICS
1049 local_urls_read = urls_read;
1050 local_urls_rejected = urls_rejected;
1053 * Need to alter the stats not to include the fetch of this
1056 * Can't do following thread safely! doh!
1059 * urls_rejected--; * This will be incremented subsequently *
1062 if (local_urls_read == 0)
1064 if (!err) err = map_block_killer(exports, "have-stats");
1068 if (!err) err = map_block_killer(exports, "have-no-stats");
1070 perc_rej = (float)local_urls_rejected * 100.0F /
1071 (float)local_urls_read;
1073 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1074 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1076 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1077 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1079 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1080 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1083 #else /* ndef FEATURE_STATISTICS */
1084 if (!err) err = map_block_killer(exports, "statistics");
1085 #endif /* ndef FEATURE_STATISTICS */
1088 * List all action files in use, together with view and edit links,
1089 * except for standard.action, which should only be viewable. (Not
1090 * enforced in the editor itself)
1091 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1094 for (i = 0; i < MAX_AF_FILES; i++)
1096 if (csp->actions_list[i] != NULL)
1098 if (!err) err = string_append(&s, "<tr><td>");
1099 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1100 snprintf(buf, sizeof(buf),
1101 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1102 if (!err) err = string_append(&s, buf);
1104 #ifdef FEATURE_CGI_EDIT_ACTIONS
1105 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1106 && (NULL != csp->config->actions_file_short[i]))
1109 if (access(csp->config->actions_file[i], W_OK) == 0)
1111 #endif /* def HAVE_ACCESS */
1112 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1113 if (!err) err = string_append(&s, buf);
1118 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1120 #endif /* def HAVE_ACCESS */
1124 if (!err) err = string_append(&s, "</td></tr>\n");
1129 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1133 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1137 * List all re_filterfiles in use, together with view options.
1138 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1141 for (i = 0; i < MAX_AF_FILES; i++)
1143 if (csp->rlist[i] != NULL)
1145 if (!err) err = string_append(&s, "<tr><td>");
1146 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1147 snprintf(buf, sizeof(buf),
1148 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1149 if (!err) err = string_append(&s, buf);
1150 if (!err) err = string_append(&s, "</td></tr>\n");
1155 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1159 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1160 if (!err) err = map_block_killer(exports, "have-filterfile");
1163 #ifdef FEATURE_TRUST
1166 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1170 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1171 if (!err) err = map_block_killer(exports, "have-trustfile");
1174 if (!err) err = map_block_killer(exports, "trust-support");
1175 #endif /* ndef FEATURE_TRUST */
1177 #ifdef FEATURE_CGI_EDIT_ACTIONS
1178 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1180 err = map_block_killer(exports, "cgi-editor-is-disabled");
1182 #endif /* ndef CGI_EDIT_ACTIONS */
1184 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1189 return JB_ERR_MEMORY;
1192 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1196 /*********************************************************************
1198 * Function : cgi_show_url_info
1200 * Description : CGI function that determines and shows which actions
1201 * Privoxy will perform for a given url, and which
1202 * matches starting from the defaults have lead to that.
1205 * 1 : csp = Current client state (buffers, headers, etc...)
1206 * 2 : rsp = http_response data structure for output
1207 * 3 : parameters = map of cgi parameters
1210 * url : The url whose actions are to be determined.
1211 * If url is unset, the url-given conditional will be
1212 * set, so that all but the form can be suppressed in
1215 * Returns : JB_ERR_OK on success
1216 * JB_ERR_MEMORY on out-of-memory error.
1218 *********************************************************************/
1219 jb_err cgi_show_url_info(struct client_state *csp,
1220 struct http_response *rsp,
1221 const struct map *parameters)
1224 struct map *exports;
1231 if (NULL == (exports = default_exports(csp, "show-url-info")))
1233 return JB_ERR_MEMORY;
1237 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1239 url_param = strdup_or_die(lookup(parameters, "url"));
1243 * Handle prefixes. 4 possibilities:
1244 * 1) "http://" or "https://" prefix present and followed by URL - OK
1245 * 2) Only the "http://" or "https://" part is present, no URL - change
1246 * to empty string so it will be detected later as "no URL".
1247 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1249 * 4) Parameter not specified or is empty string - let this fall through
1250 * for now, next block of code will handle it.
1252 if (0 == strncmp(url_param, "http://", 7))
1254 if (url_param[7] == '\0')
1257 * Empty URL (just prefix).
1258 * Make it totally empty so it's caught by the next if ()
1260 url_param[0] = '\0';
1263 else if (0 == strncmp(url_param, "https://", 8))
1265 if (url_param[8] == '\0')
1268 * Empty URL (just prefix).
1269 * Make it totally empty so it's caught by the next if ()
1271 url_param[0] = '\0';
1274 else if ((url_param[0] != '\0')
1275 && ((NULL == strstr(url_param, "://")
1276 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1279 * No prefix or at least no prefix before
1280 * the first slash - assume http://
1282 char *url_param_prefixed = strdup_or_die("http://");
1284 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1287 return JB_ERR_MEMORY;
1289 url_param = url_param_prefixed;
1293 * Hide "toggle off" warning if Privoxy is toggled on.
1296 #ifdef FEATURE_TOGGLE
1297 (global_toggle_state == 1) &&
1298 #endif /* def FEATURE_TOGGLE */
1299 map_block_killer(exports, "privoxy-is-toggled-off")
1304 return JB_ERR_MEMORY;
1307 if (url_param[0] == '\0')
1309 /* URL paramater not specified, display query form only. */
1311 if (map_block_killer(exports, "url-given")
1312 || map(exports, "url", 1, "", 1))
1315 return JB_ERR_MEMORY;
1320 /* Given a URL, so query it. */
1325 struct file_list *fl;
1326 struct url_actions *b;
1327 struct http_request url_to_query[1];
1328 struct current_action_spec action[1];
1331 if (map(exports, "url", 1, html_encode(url_param), 0))
1335 return JB_ERR_MEMORY;
1338 init_current_action(action);
1340 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1342 free_current_action(action);
1345 return JB_ERR_MEMORY;
1348 memset(url_to_query, '\0', sizeof(url_to_query));
1349 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1350 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1354 if (err == JB_ERR_MEMORY)
1356 free_http_request(url_to_query);
1357 free_current_action(action);
1359 return JB_ERR_MEMORY;
1365 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1366 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1367 if (!err) err = map_block_killer(exports, "valid-url");
1369 free_current_action(action);
1370 free_http_request(url_to_query);
1375 return JB_ERR_MEMORY;
1378 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1382 * We have a warning about SSL paths. Hide it for unencrypted sites.
1384 if (!url_to_query->ssl)
1386 if (map_block_killer(exports, "https"))
1388 free_current_action(action);
1390 free_http_request(url_to_query);
1391 return JB_ERR_MEMORY;
1395 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1397 for (i = 0; i < MAX_AF_FILES; i++)
1399 if (NULL == csp->config->actions_file_short[i]
1400 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1404 if ((fl = csp->actions_list[i]) != NULL)
1406 if ((b = fl->f) != NULL)
1408 /* FIXME: Hardcoded HTML! */
1409 string_append(&matches, "<tr><th>In file: ");
1410 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1411 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1412 string_append(&matches, buf);
1413 string_append(&matches, "View</a>");
1414 #ifdef FEATURE_CGI_EDIT_ACTIONS
1415 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1418 if (access(csp->config->actions_file[i], W_OK) == 0)
1420 #endif /* def HAVE_ACCESS */
1421 snprintf(buf, sizeof(buf),
1422 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1423 string_append(&matches, buf);
1424 string_append(&matches, "Edit</a>");
1429 string_append(&matches, " <strong>No write access.</strong>");
1431 #endif /* def HAVE_ACCESS */
1433 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1435 string_append(&matches, "</th></tr>\n");
1442 for (; (b != NULL) && (matches != NULL); b = b->next)
1444 if (url_match(b->url, url_to_query))
1446 string_append(&matches, "<tr><td>{");
1447 string_join (&matches, actions_to_html(csp, b->action));
1448 string_append(&matches, " }<br>\n<code>");
1449 string_join (&matches, html_encode(b->url->spec));
1450 string_append(&matches, "</code></td></tr>\n");
1452 if (merge_current_action(action, b->action))
1455 free_http_request(url_to_query);
1456 free_current_action(action);
1458 return JB_ERR_MEMORY;
1466 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1469 string_append(&matches, "</table>\n");
1472 * XXX: Kludge to make sure the "Forward settings" section
1473 * shows what forward-override{} would do with the requested URL.
1474 * No one really cares how the CGI request would be forwarded
1475 * if it wasn't intercepted as CGI request in the first place.
1477 * From here on the action bitmask will no longer reflect
1478 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1479 * but luckily it's no longer required later on anyway.
1481 free_current_action(csp->action);
1482 get_url_actions(csp, url_to_query);
1485 * Fill in forwarding settings.
1487 * The possibilities are:
1489 * - http forwarding only
1490 * - socks4(a) forwarding only
1491 * - socks4(a) and http forwarding.
1493 * XXX: Parts of this code could be reused for the
1494 * "forwarding-failed" template which currently doesn't
1495 * display the proxy port and an eventual second forwarder.
1498 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1500 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1502 if (!err) err = map_block_killer(exports, "socks-forwarder");
1503 if (!err) err = map_block_killer(exports, "http-forwarder");
1507 char port[10]; /* We save proxy ports as int but need a string here */
1509 if (!err) err = map_block_killer(exports, "no-forwarder");
1511 if (fwd->gateway_host != NULL)
1513 char *socks_type = NULL;
1518 socks_type = "socks4";
1521 socks_type = "socks4a";
1524 socks_type = "socks5";
1527 socks_type = "socks5t";
1530 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1533 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1534 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1535 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1536 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1540 if (!err) err = map_block_killer(exports, "socks-forwarder");
1543 if (fwd->forward_host != NULL)
1545 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1546 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1547 if (!err) err = map(exports, "forward-port", 1, port, 1);
1551 if (!err) err = map_block_killer(exports, "http-forwarder");
1556 free_http_request(url_to_query);
1558 if (err || matches == NULL)
1560 free_current_action(action);
1562 return JB_ERR_MEMORY;
1565 #ifdef FEATURE_CGI_EDIT_ACTIONS
1566 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1568 err = map_block_killer(exports, "cgi-editor-is-disabled");
1570 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1573 * If zlib support is available, if no content filters
1574 * are enabled or if the prevent-compression action is enabled,
1575 * suppress the "compression could prevent filtering" warning.
1577 #ifndef FEATURE_ZLIB
1578 if (!content_filters_enabled(action) ||
1579 (action->flags & ACTION_NO_COMPRESSION))
1582 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1585 if (err || map(exports, "matches", 1, matches , 0))
1587 free_current_action(action);
1589 return JB_ERR_MEMORY;
1592 s = current_action_to_html(csp, action);
1594 free_current_action(action);
1596 if (map(exports, "final", 1, s, 0))
1599 return JB_ERR_MEMORY;
1603 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1607 /*********************************************************************
1609 * Function : cgi_robots_txt
1611 * Description : CGI function to return "/robots.txt".
1614 * 1 : csp = Current client state (buffers, headers, etc...)
1615 * 2 : rsp = http_response data structure for output
1616 * 3 : parameters = map of cgi parameters
1618 * CGI Parameters : None
1620 * Returns : JB_ERR_OK on success
1621 * JB_ERR_MEMORY on out-of-memory error.
1623 *********************************************************************/
1624 jb_err cgi_robots_txt(struct client_state *csp,
1625 struct http_response *rsp,
1626 const struct map *parameters)
1634 rsp->body = strdup_or_die(
1635 "# This is the Privoxy control interface.\n"
1636 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1643 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1647 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1648 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1650 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1654 /*********************************************************************
1656 * Function : show_defines
1658 * Description : Add to a map the state od all conditional #defines
1659 * used when building
1662 * 1 : exports = map to extend
1664 * Returns : JB_ERR_OK on success
1665 * JB_ERR_MEMORY on out-of-memory error.
1667 *********************************************************************/
1668 static jb_err show_defines(struct map *exports)
1670 jb_err err = JB_ERR_OK;
1673 const char name[31];
1674 const unsigned char is_available;
1677 static const struct feature features[] = {
1679 "FEATURE_ACCEPT_FILTER",
1680 #ifdef FEATURE_ACCEPT_FILTER
1695 "FEATURE_CGI_EDIT_ACTIONS",
1696 #ifdef FEATURE_CGI_EDIT_ACTIONS
1703 "FEATURE_CLIENT_TAGS",
1704 #ifdef FEATURE_CLIENT_TAGS
1711 "FEATURE_COMPRESSION",
1712 #ifdef FEATURE_COMPRESSION
1719 "FEATURE_CONNECTION_KEEP_ALIVE",
1720 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1727 "FEATURE_CONNECTION_SHARING",
1728 #ifdef FEATURE_CONNECTION_SHARING
1735 "FEATURE_FAST_REDIRECTS",
1736 #ifdef FEATURE_FAST_REDIRECTS
1743 "FEATURE_FORCE_LOAD",
1744 #ifdef FEATURE_FORCE_LOAD
1751 "FEATURE_GRACEFUL_TERMINATION",
1752 #ifdef FEATURE_GRACEFUL_TERMINATION
1759 "FEATURE_IMAGE_BLOCKING",
1760 #ifdef FEATURE_IMAGE_BLOCKING
1767 "FEATURE_IMAGE_DETECT_MSIE",
1768 #ifdef FEATURE_IMAGE_DETECT_MSIE
1775 "FEATURE_IPV6_SUPPORT",
1784 #ifdef FEATURE_NO_GIFS
1792 #ifdef FEATURE_PTHREAD
1799 "FEATURE_STATISTICS",
1800 #ifdef FEATURE_STATISTICS
1807 "FEATURE_STRPTIME_SANITY_CHECKS",
1808 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
1816 #ifdef FEATURE_TOGGLE
1824 #ifdef FEATURE_TRUST
1839 "FEATURE_DYNAMIC_PCRE",
1840 #ifdef FEATURE_DYNAMIC_PCRE
1848 for (i = 0; i < SZ(features); i++)
1850 err = map_conditional(exports, features[i].name, features[i].is_available);
1862 /*********************************************************************
1864 * Function : show_rcs
1866 * Description : Create a string with the rcs info for all sourcefiles
1870 * Returns : A string, or NULL on out-of-memory.
1872 *********************************************************************/
1873 static char *show_rcs(void)
1875 char *result = strdup_or_die("");
1876 char buf[BUFFER_SIZE];
1878 /* Instead of including *all* dot h's in the project (thus creating a
1879 * tremendous amount of dependencies), I will concede to declaring them
1880 * as extern's. This forces the developer to add to this list, but oh well.
1883 #define SHOW_RCS(__x) \
1885 extern const char __x[]; \
1886 snprintf(buf, sizeof(buf), " %s\n", __x); \
1887 string_append(&result, buf); \
1890 /* In alphabetical order */
1891 SHOW_RCS(actions_h_rcs)
1892 SHOW_RCS(actions_rcs)
1894 SHOW_RCS(amiga_h_rcs)
1896 #endif /* def AMIGA */
1899 #ifdef FEATURE_CGI_EDIT_ACTIONS
1900 SHOW_RCS(cgiedit_h_rcs)
1901 SHOW_RCS(cgiedit_rcs)
1902 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1903 SHOW_RCS(cgisimple_h_rcs)
1904 SHOW_RCS(cgisimple_rcs)
1906 SHOW_RCS(cygwin_h_rcs)
1908 SHOW_RCS(deanimate_h_rcs)
1909 SHOW_RCS(deanimate_rcs)
1910 SHOW_RCS(encode_h_rcs)
1911 SHOW_RCS(encode_rcs)
1912 SHOW_RCS(errlog_h_rcs)
1913 SHOW_RCS(errlog_rcs)
1914 SHOW_RCS(filters_h_rcs)
1915 SHOW_RCS(filters_rcs)
1916 SHOW_RCS(gateway_h_rcs)
1917 SHOW_RCS(gateway_rcs)
1918 SHOW_RCS(jbsockets_h_rcs)
1919 SHOW_RCS(jbsockets_rcs)
1922 SHOW_RCS(list_h_rcs)
1924 SHOW_RCS(loadcfg_h_rcs)
1925 SHOW_RCS(loadcfg_rcs)
1926 SHOW_RCS(loaders_h_rcs)
1927 SHOW_RCS(loaders_rcs)
1928 SHOW_RCS(miscutil_h_rcs)
1929 SHOW_RCS(miscutil_rcs)
1930 SHOW_RCS(parsers_h_rcs)
1931 SHOW_RCS(parsers_rcs)
1933 SHOW_RCS(pcrs_h_rcs)
1934 SHOW_RCS(project_h_rcs)
1935 SHOW_RCS(ssplit_h_rcs)
1936 SHOW_RCS(ssplit_rcs)
1937 SHOW_RCS(urlmatch_h_rcs)
1938 SHOW_RCS(urlmatch_rcs)
1940 #ifndef _WIN_CONSOLE
1941 SHOW_RCS(w32log_h_rcs)
1942 SHOW_RCS(w32log_rcs)
1943 SHOW_RCS(w32res_h_rcs)
1944 SHOW_RCS(w32taskbar_h_rcs)
1945 SHOW_RCS(w32taskbar_rcs)
1946 #endif /* ndef _WIN_CONSOLE */
1947 SHOW_RCS(win32_h_rcs)
1949 #endif /* def _WIN32 */
1958 /*********************************************************************
1960 * Function : cgi_show_file
1962 * Description : CGI function that shows the content of a
1963 * configuration file.
1966 * 1 : csp = Current client state (buffers, headers, etc...)
1967 * 2 : rsp = http_response data structure for output
1968 * 3 : parameters = map of cgi parameters
1971 * file : Which file to show. Only first letter is checked,
1976 * Default is to show menu and other information.
1978 * Returns : JB_ERR_OK on success
1979 * JB_ERR_MEMORY on out-of-memory error.
1981 *********************************************************************/
1982 static jb_err cgi_show_file(struct client_state *csp,
1983 struct http_response *rsp,
1984 const struct map *parameters)
1987 const char * filename = NULL;
1988 char * file_description = NULL;
1994 switch (*(lookup(parameters, "file")))
1997 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1999 filename = csp->actions_list[i]->filename;
2000 file_description = "Actions File";
2005 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2007 filename = csp->rlist[i]->filename;
2008 file_description = "Filter File";
2012 #ifdef FEATURE_TRUST
2016 filename = csp->tlist->filename;
2017 file_description = "Trust File";
2020 #endif /* def FEATURE_TRUST */
2023 if (NULL != filename)
2025 struct map *exports;
2030 exports = default_exports(csp, "show-status");
2031 if (NULL == exports)
2033 return JB_ERR_MEMORY;
2036 if (map(exports, "file-description", 1, file_description, 1)
2037 || map(exports, "filepath", 1, html_encode(filename), 0))
2040 return JB_ERR_MEMORY;
2043 err = load_file(filename, &s, &length);
2044 if (JB_ERR_OK != err)
2046 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2049 return JB_ERR_MEMORY;
2054 s = html_encode_and_free_original(s);
2058 return JB_ERR_MEMORY;
2061 if (map(exports, "contents", 1, s, 0))
2064 return JB_ERR_MEMORY;
2068 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2071 return JB_ERR_CGI_PARAMS;
2075 /*********************************************************************
2077 * Function : load_file
2079 * Description : Loads a file into a buffer.
2082 * 1 : filename = Name of the file to be loaded.
2083 * 2 : buffer = Used to return the file's content.
2084 * 3 : length = Used to return the size of the file.
2086 * Returns : JB_ERR_OK in case of success,
2087 * JB_ERR_FILE in case of ordinary file loading errors
2088 * (fseek() and ftell() errors are fatal)
2089 * JB_ERR_MEMORY in case of out-of-memory.
2091 *********************************************************************/
2092 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2096 jb_err err = JB_ERR_OK;
2098 fp = fopen(filename, "rb");
2101 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
2105 /* Get file length */
2106 if (fseek(fp, 0, SEEK_END))
2108 log_error(LOG_LEVEL_FATAL,
2109 "Unexpected error while fseek()ing to the end of %s: %E",
2115 log_error(LOG_LEVEL_FATAL,
2116 "Unexpected ftell() error while loading %s: %E",
2119 *length = (size_t)ret;
2121 /* Go back to the beginning. */
2122 if (fseek(fp, 0, SEEK_SET))
2124 log_error(LOG_LEVEL_FATAL,
2125 "Unexpected error while fseek()ing to the beginning of %s: %E",
2129 *buffer = zalloc_or_die(*length + 1);
2131 if (1 != fread(*buffer, *length, 1, fp))
2134 * May theoretically happen if the file size changes between
2135 * fseek() and fread() because it's edited in-place. Privoxy
2136 * and common text editors don't do that, thus we just fail.
2138 log_error(LOG_LEVEL_ERROR,
2139 "Couldn't completely read file %s.", filename);