1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.136 2016/03/17 10:40:53 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_create_client_tag_form
282 * Description : Creates a HTML form to enable or disable a given
284 * XXX: Could use a template.
287 * 1 : form = Buffer to fill with the generated form
288 * 2 : size = Size of the form buffer
289 * 3 : tag = Name of the tag this form should affect
290 * 4 : toggle_state = Desired state after the button pressed 0
291 * 5 : expires = Whether or not the tag should be enabled.
292 * Only checked if toggle_state is 1.
296 *********************************************************************/
297 static void cgi_create_client_tag_form(char *form, size_t size,
298 const char *tag, int toggle_state, int expires)
302 if (toggle_state == 1)
304 button_name = (expires == 1) ? "Enable" : "Enable temporarily";
308 assert(toggle_state == 0);
309 button_name = "Disable";
313 "<form method=\"GET\" action=\"show-client-tags\" style=\"display: inline\">\n"
314 " <input type=\"hidden\" name=\"tag\" value=\"%s\">\n"
315 " <input type=\"hidden\" name=\"toggle-state\" value=\"%u\">\n"
316 " <input type=\"hidden\" name=\"expires\" value=\"%u\">\n"
317 " <input type=\"submit\" value=\"%s\">\n"
318 "</form>", tag, toggle_state, !expires, button_name);
321 /*********************************************************************
323 * Function : cgi_show_client_tags
325 * Description : Shows the tags that can be set based on the client
329 * 1 : csp = Current client state (buffers, headers, etc...)
330 * 2 : rsp = http_response data structure for output
331 * 3 : parameters = map of cgi parameters
333 * CGI Parameters : none
335 * Returns : JB_ERR_OK on success
336 * JB_ERR_MEMORY on out-of-memory error.
338 *********************************************************************/
339 jb_err cgi_show_client_tags(struct client_state *csp,
340 struct http_response *rsp,
341 const struct map *parameters)
344 struct client_tag_spec *this_tag;
345 jb_err err = JB_ERR_OK;
346 const char *toggled_tag;
347 const char *toggle_state;
348 const char *tag_expires;
350 char *client_tags = strdup_or_die("");
357 if (NULL == (exports = default_exports(csp, "show-client-tags")))
359 return JB_ERR_MEMORY;
362 toggled_tag = lookup(parameters, "tag");
363 if (*toggled_tag != '\0')
365 tag_expires = lookup(parameters, "expires");
366 if (*tag_expires == '0')
372 time_to_live = csp->config->client_tag_lifetime;
374 toggle_state = lookup(parameters, "toggle-state");
375 if (*toggle_state == '1')
377 enable_client_specific_tag(csp, toggled_tag, time_to_live);
381 disable_client_specific_tag(csp, toggled_tag);
385 this_tag = csp->config->client_tags;
386 if (this_tag->name == NULL)
388 if (!err) err = string_append(&client_tags, "<p>No tags available.</p>\n");
394 err = string_append(&client_tags, "<table border=\"1\">\n"
395 "<tr><th>Tag name</th>\n"
396 "<th>Current state</th><th>Change state</th><th>Description</th></tr>\n");
398 while ((this_tag != NULL) && (this_tag->name != NULL))
402 privoxy_mutex_lock(&client_tags_mutex);
403 tag_state = client_has_requested_tag(csp->ip_addr_str, this_tag->name);
404 privoxy_mutex_unlock(&client_tags_mutex);
405 if (!err) err = string_append(&client_tags, "<tr><td>");
406 if (!err) err = string_append(&client_tags, this_tag->name);
407 if (!err) err = string_append(&client_tags, "</td><td>");
408 if (!err) err = string_append(&client_tags, tag_state == 1 ? "Enabled" : "Disabled");
409 if (!err) err = string_append(&client_tags, "</td><td>");
410 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 1);
411 if (!err) err = string_append(&client_tags, buf);
414 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 0);
415 if (!err) err = string_append(&client_tags, buf);
417 if (!err) err = string_append(&client_tags, "</td><td>");
418 if (!err) err = string_append(&client_tags, this_tag->description);
419 if (!err) err = string_append(&client_tags, "</td></tr>\n");
423 return JB_ERR_MEMORY;
425 this_tag = this_tag->next;
427 if (!err) err = string_append(&client_tags, "</table>\n");
430 if (map(exports, "client-tags", 1, client_tags, 0))
433 return JB_ERR_MEMORY;
436 if (map(exports, "client-ip-addr", 1, csp->ip_addr_str, 1))
439 return JB_ERR_MEMORY;
442 return template_fill_for_cgi(csp, "show-client-tags", exports, rsp);
444 #endif /* def FEATURE_CLIENT_TAGS */
447 /*********************************************************************
449 * Function : cgi_send_banner
451 * Description : CGI function that returns a banner.
454 * 1 : csp = Current client state (buffers, headers, etc...)
455 * 2 : rsp = http_response data structure for output
456 * 3 : parameters = map of cgi parameters
459 * type : Selects the type of banner between "trans", "logo",
460 * and "auto". Defaults to "logo" if absent or invalid.
461 * "auto" means to select as if we were image-blocking.
462 * (Only the first character really counts; b and t are
465 * Returns : JB_ERR_OK on success
466 * JB_ERR_MEMORY on out-of-memory error.
468 *********************************************************************/
469 jb_err cgi_send_banner(struct client_state *csp,
470 struct http_response *rsp,
471 const struct map *parameters)
473 char imagetype = lookup(parameters, "type")[0];
476 * If type is auto, then determine the right thing
477 * to do from the set-image-blocker action
479 if (imagetype == 'a')
486 #ifdef FEATURE_IMAGE_BLOCKING
487 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
489 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
490 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
491 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
495 /* Use default - nothing to do here. */
497 else if (0 == strcmpic(p, "blank"))
501 else if (0 == strcmpic(p, "pattern"))
507 * If the action is to call this CGI, determine
510 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
512 imagetype = p[sizeof(prefix1) - 1];
514 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
516 imagetype = p[sizeof(prefix2) - 1];
520 * Everything else must (should) be a URL to
528 #endif /* def FEATURE_IMAGE_BLOCKING */
532 * Now imagetype is either the non-auto type we were called with,
533 * or it was auto and has since been determined. In any case, we
534 * can proceed to actually answering the request by sending a redirect
535 * or an image as appropriate:
537 if (imagetype == 'r')
539 rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
540 if (enlist_unique_header(rsp->headers, "Location",
541 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
543 return JB_ERR_MEMORY;
548 if ((imagetype == 'b') || (imagetype == 't'))
550 rsp->body = bindup(image_blank_data, image_blank_length);
551 rsp->content_length = image_blank_length;
555 rsp->body = bindup(image_pattern_data, image_pattern_length);
556 rsp->content_length = image_pattern_length;
559 if (rsp->body == NULL)
561 return JB_ERR_MEMORY;
563 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
565 return JB_ERR_MEMORY;
576 /*********************************************************************
578 * Function : cgi_transparent_image
580 * Description : CGI function that sends a 1x1 transparent image.
583 * 1 : csp = Current client state (buffers, headers, etc...)
584 * 2 : rsp = http_response data structure for output
585 * 3 : parameters = map of cgi parameters
587 * CGI Parameters : None
589 * Returns : JB_ERR_OK on success
590 * JB_ERR_MEMORY on out-of-memory error.
592 *********************************************************************/
593 jb_err cgi_transparent_image(struct client_state *csp,
594 struct http_response *rsp,
595 const struct map *parameters)
600 rsp->body = bindup(image_blank_data, image_blank_length);
601 rsp->content_length = image_blank_length;
603 if (rsp->body == NULL)
605 return JB_ERR_MEMORY;
608 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
610 return JB_ERR_MEMORY;
620 /*********************************************************************
622 * Function : cgi_send_default_favicon
624 * Description : CGI function that sends the standard favicon.
627 * 1 : csp = Current client state (buffers, headers, etc...)
628 * 2 : rsp = http_response data structure for output
629 * 3 : parameters = map of cgi parameters
631 * CGI Parameters : None
633 * Returns : JB_ERR_OK on success
634 * JB_ERR_MEMORY on out-of-memory error.
636 *********************************************************************/
637 jb_err cgi_send_default_favicon(struct client_state *csp,
638 struct http_response *rsp,
639 const struct map *parameters)
641 static const char default_favicon_data[] =
642 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
643 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
644 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
645 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
646 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
647 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
648 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
649 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
650 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
651 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
652 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
653 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
654 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
656 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
661 rsp->body = bindup(default_favicon_data, favicon_length);
662 rsp->content_length = favicon_length;
664 if (rsp->body == NULL)
666 return JB_ERR_MEMORY;
669 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
671 return JB_ERR_MEMORY;
681 /*********************************************************************
683 * Function : cgi_send_error_favicon
685 * Description : CGI function that sends the favicon for error pages.
688 * 1 : csp = Current client state (buffers, headers, etc...)
689 * 2 : rsp = http_response data structure for output
690 * 3 : parameters = map of cgi parameters
692 * CGI Parameters : None
694 * Returns : JB_ERR_OK on success
695 * JB_ERR_MEMORY on out-of-memory error.
697 *********************************************************************/
698 jb_err cgi_send_error_favicon(struct client_state *csp,
699 struct http_response *rsp,
700 const struct map *parameters)
702 static const char error_favicon_data[] =
703 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
704 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
705 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
706 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
707 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
708 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
709 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
710 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
711 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
712 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
713 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
714 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
715 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
717 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
722 rsp->body = bindup(error_favicon_data, favicon_length);
723 rsp->content_length = favicon_length;
725 if (rsp->body == NULL)
727 return JB_ERR_MEMORY;
730 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
732 return JB_ERR_MEMORY;
742 /*********************************************************************
744 * Function : cgi_send_stylesheet
746 * Description : CGI function that sends a css stylesheet found
747 * in the cgi-style.css template
750 * 1 : csp = Current client state (buffers, headers, etc...)
751 * 2 : rsp = http_response data structure for output
752 * 3 : parameters = map of cgi parameters
754 * CGI Parameters : None
756 * Returns : JB_ERR_OK on success
757 * JB_ERR_MEMORY on out-of-memory error.
759 *********************************************************************/
760 jb_err cgi_send_stylesheet(struct client_state *csp,
761 struct http_response *rsp,
762 const struct map *parameters)
771 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
773 if (err == JB_ERR_FILE)
776 * No way to tell user; send empty stylesheet
778 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
782 return err; /* JB_ERR_MEMORY */
785 if (enlist(rsp->headers, "Content-Type: text/css"))
787 return JB_ERR_MEMORY;
795 /*********************************************************************
797 * Function : cgi_send_url_info_osd
799 * Description : CGI function that sends the OpenSearch Description
800 * template for the show-url-info page. It allows to
801 * access the page through "search engine plugins".
804 * 1 : csp = Current client state (buffers, headers, etc...)
805 * 2 : rsp = http_response data structure for output
806 * 3 : parameters = map of cgi parameters
808 * CGI Parameters : None
810 * Returns : JB_ERR_OK on success
811 * JB_ERR_MEMORY on out-of-memory error.
813 *********************************************************************/
814 jb_err cgi_send_url_info_osd(struct client_state *csp,
815 struct http_response *rsp,
816 const struct map *parameters)
818 jb_err err = JB_ERR_MEMORY;
819 struct map *exports = default_exports(csp, NULL);
826 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
827 if (JB_ERR_OK == err)
829 err = enlist(rsp->headers,
830 "Content-Type: application/opensearchdescription+xml");
839 /*********************************************************************
841 * Function : get_content_type
843 * Description : Use the file extension to guess the content type
844 * header we should use to serve the file.
847 * 1 : filename = Name of the file whose content type
850 * Returns : The guessed content type.
852 *********************************************************************/
853 static const char *get_content_type(const char *filename)
858 const char extension[6];
859 const char content_type[11];
861 static const struct content_type content_types[] =
863 {".css", "text/css"},
864 {".jpg", "image/jpeg"},
865 {".jpeg", "image/jpeg"},
866 {".png", "image/png"},
869 for (i = 0; i < SZ(content_types); i++)
871 if (strstr(filename, content_types[i].extension))
873 return content_types[i].content_type;
877 /* No match by extension, default to html */
881 /*********************************************************************
883 * Function : cgi_send_user_manual
885 * Description : CGI function that sends a file in the user
889 * 1 : csp = Current client state (buffers, headers, etc...)
890 * 2 : rsp = http_response data structure for output
891 * 3 : parameters = map of cgi parameters
893 * CGI Parameters : file=name.html, the name of the HTML file
894 * (relative to user-manual from config)
896 * Returns : JB_ERR_OK on success
897 * JB_ERR_MEMORY on out-of-memory error.
899 *********************************************************************/
900 jb_err cgi_send_user_manual(struct client_state *csp,
901 struct http_response *rsp,
902 const struct map *parameters)
904 const char *filename;
906 jb_err err = JB_ERR_OK;
907 const char *content_type;
913 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
915 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
916 "received while user-manual delivery is disabled.");
917 return cgi_error_404(csp, rsp, parameters);
920 if (!parameters->first)
922 /* requested http://p.p/user-manual (without trailing slash) */
923 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
926 get_string_param(parameters, "file", &filename);
927 if (filename == NULL)
929 /* It's '/' so serve the index.html if there is one. */
930 filename = "index.html";
932 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
935 * We currently only support a flat file
936 * hierarchy for the documentation.
938 log_error(LOG_LEVEL_ERROR,
939 "Rejecting the request to serve '%s' as it contains '/' or '..'",
941 return JB_ERR_CGI_PARAMS;
944 full_path = make_path(csp->config->usermanual, filename);
945 if (full_path == NULL)
947 return JB_ERR_MEMORY;
950 err = load_file(full_path, &rsp->body, &rsp->content_length);
951 if (JB_ERR_OK != err)
953 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
954 if (JB_ERR_FILE == err)
956 err = cgi_error_no_template(csp, rsp, full_path);
963 content_type = get_content_type(filename);
964 log_error(LOG_LEVEL_CGI,
965 "Content-Type guessed for %s: %s", filename, content_type);
967 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
972 /*********************************************************************
974 * Function : cgi_show_version
976 * Description : CGI function that returns a a web page describing the
977 * file versions of Privoxy.
980 * 1 : csp = Current client state (buffers, headers, etc...)
981 * 2 : rsp = http_response data structure for output
982 * 3 : parameters = map of cgi parameters
984 * CGI Parameters : none
986 * Returns : JB_ERR_OK on success
987 * JB_ERR_MEMORY on out-of-memory error.
989 *********************************************************************/
990 jb_err cgi_show_version(struct client_state *csp,
991 struct http_response *rsp,
992 const struct map *parameters)
1000 if (NULL == (exports = default_exports(csp, "show-version")))
1002 return JB_ERR_MEMORY;
1005 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1008 return JB_ERR_MEMORY;
1011 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1015 /*********************************************************************
1017 * Function : cgi_show_status
1019 * Description : CGI function that returns a web page describing the
1020 * current status of Privoxy.
1023 * 1 : csp = Current client state (buffers, headers, etc...)
1024 * 2 : rsp = http_response data structure for output
1025 * 3 : parameters = map of cgi parameters
1028 * file : Which file to show. Only first letter is checked,
1033 * Default is to show menu and other information.
1035 * Returns : JB_ERR_OK on success
1036 * JB_ERR_MEMORY on out-of-memory error.
1038 *********************************************************************/
1039 jb_err cgi_show_status(struct client_state *csp,
1040 struct http_response *rsp,
1041 const struct map *parameters)
1047 char buf[BUFFER_SIZE];
1048 #ifdef FEATURE_STATISTICS
1049 float perc_rej; /* Percentage of http requests rejected */
1050 int local_urls_read;
1051 int local_urls_rejected;
1052 #endif /* ndef FEATURE_STATISTICS */
1053 jb_err err = JB_ERR_OK;
1055 struct map *exports;
1061 if ('\0' != *(lookup(parameters, "file")))
1063 return cgi_show_file(csp, rsp, parameters);
1066 if (NULL == (exports = default_exports(csp, "show-status")))
1068 return JB_ERR_MEMORY;
1072 for (j = 0; (s != NULL) && (j < Argc); j++)
1074 if (!err) err = string_join (&s, html_encode(Argv[j]));
1075 if (!err) err = string_append(&s, " ");
1077 if (!err) err = map(exports, "invocation", 1, s, 0);
1079 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1080 if (!err) err = show_defines(exports);
1085 return JB_ERR_MEMORY;
1088 #ifdef FEATURE_STATISTICS
1089 local_urls_read = urls_read;
1090 local_urls_rejected = urls_rejected;
1093 * Need to alter the stats not to include the fetch of this
1096 * Can't do following thread safely! doh!
1099 * urls_rejected--; * This will be incremented subsequently *
1102 if (local_urls_read == 0)
1104 if (!err) err = map_block_killer(exports, "have-stats");
1108 if (!err) err = map_block_killer(exports, "have-no-stats");
1110 perc_rej = (float)local_urls_rejected * 100.0F /
1111 (float)local_urls_read;
1113 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1114 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1116 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1117 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1119 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1120 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1123 #else /* ndef FEATURE_STATISTICS */
1124 if (!err) err = map_block_killer(exports, "statistics");
1125 #endif /* ndef FEATURE_STATISTICS */
1128 * List all action files in use, together with view and edit links,
1129 * except for standard.action, which should only be viewable. (Not
1130 * enforced in the editor itself)
1131 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1134 for (i = 0; i < MAX_AF_FILES; i++)
1136 if (csp->actions_list[i] != NULL)
1138 if (!err) err = string_append(&s, "<tr><td>");
1139 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1140 snprintf(buf, sizeof(buf),
1141 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1142 if (!err) err = string_append(&s, buf);
1144 #ifdef FEATURE_CGI_EDIT_ACTIONS
1145 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1146 && (NULL != csp->config->actions_file_short[i]))
1149 if (access(csp->config->actions_file[i], W_OK) == 0)
1151 #endif /* def HAVE_ACCESS */
1152 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1153 if (!err) err = string_append(&s, buf);
1158 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1160 #endif /* def HAVE_ACCESS */
1164 if (!err) err = string_append(&s, "</td></tr>\n");
1169 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1173 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1177 * List all re_filterfiles in use, together with view options.
1178 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1181 for (i = 0; i < MAX_AF_FILES; i++)
1183 if (csp->rlist[i] != NULL)
1185 if (!err) err = string_append(&s, "<tr><td>");
1186 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1187 snprintf(buf, sizeof(buf),
1188 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1189 if (!err) err = string_append(&s, buf);
1190 if (!err) err = string_append(&s, "</td></tr>\n");
1195 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1199 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1200 if (!err) err = map_block_killer(exports, "have-filterfile");
1203 #ifdef FEATURE_TRUST
1206 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1210 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1211 if (!err) err = map_block_killer(exports, "have-trustfile");
1214 if (!err) err = map_block_killer(exports, "trust-support");
1215 #endif /* ndef FEATURE_TRUST */
1217 #ifdef FEATURE_CGI_EDIT_ACTIONS
1218 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1220 err = map_block_killer(exports, "cgi-editor-is-disabled");
1222 #endif /* ndef CGI_EDIT_ACTIONS */
1224 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1229 return JB_ERR_MEMORY;
1232 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1236 /*********************************************************************
1238 * Function : cgi_show_url_info
1240 * Description : CGI function that determines and shows which actions
1241 * Privoxy will perform for a given url, and which
1242 * matches starting from the defaults have lead to that.
1245 * 1 : csp = Current client state (buffers, headers, etc...)
1246 * 2 : rsp = http_response data structure for output
1247 * 3 : parameters = map of cgi parameters
1250 * url : The url whose actions are to be determined.
1251 * If url is unset, the url-given conditional will be
1252 * set, so that all but the form can be suppressed in
1255 * Returns : JB_ERR_OK on success
1256 * JB_ERR_MEMORY on out-of-memory error.
1258 *********************************************************************/
1259 jb_err cgi_show_url_info(struct client_state *csp,
1260 struct http_response *rsp,
1261 const struct map *parameters)
1264 struct map *exports;
1271 if (NULL == (exports = default_exports(csp, "show-url-info")))
1273 return JB_ERR_MEMORY;
1277 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1279 url_param = strdup_or_die(lookup(parameters, "url"));
1283 * Handle prefixes. 4 possibilities:
1284 * 1) "http://" or "https://" prefix present and followed by URL - OK
1285 * 2) Only the "http://" or "https://" part is present, no URL - change
1286 * to empty string so it will be detected later as "no URL".
1287 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1289 * 4) Parameter not specified or is empty string - let this fall through
1290 * for now, next block of code will handle it.
1292 if (0 == strncmp(url_param, "http://", 7))
1294 if (url_param[7] == '\0')
1297 * Empty URL (just prefix).
1298 * Make it totally empty so it's caught by the next if ()
1300 url_param[0] = '\0';
1303 else if (0 == strncmp(url_param, "https://", 8))
1305 if (url_param[8] == '\0')
1308 * Empty URL (just prefix).
1309 * Make it totally empty so it's caught by the next if ()
1311 url_param[0] = '\0';
1314 else if ((url_param[0] != '\0')
1315 && ((NULL == strstr(url_param, "://")
1316 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1319 * No prefix or at least no prefix before
1320 * the first slash - assume http://
1322 char *url_param_prefixed = strdup_or_die("http://");
1324 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1327 return JB_ERR_MEMORY;
1329 url_param = url_param_prefixed;
1333 * Hide "toggle off" warning if Privoxy is toggled on.
1336 #ifdef FEATURE_TOGGLE
1337 (global_toggle_state == 1) &&
1338 #endif /* def FEATURE_TOGGLE */
1339 map_block_killer(exports, "privoxy-is-toggled-off")
1344 return JB_ERR_MEMORY;
1347 if (url_param[0] == '\0')
1349 /* URL paramater not specified, display query form only. */
1351 if (map_block_killer(exports, "url-given")
1352 || map(exports, "url", 1, "", 1))
1355 return JB_ERR_MEMORY;
1360 /* Given a URL, so query it. */
1365 struct file_list *fl;
1366 struct url_actions *b;
1367 struct http_request url_to_query[1];
1368 struct current_action_spec action[1];
1371 if (map(exports, "url", 1, html_encode(url_param), 0))
1375 return JB_ERR_MEMORY;
1378 init_current_action(action);
1380 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1382 free_current_action(action);
1385 return JB_ERR_MEMORY;
1388 memset(url_to_query, '\0', sizeof(url_to_query));
1389 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1390 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1394 if (err == JB_ERR_MEMORY)
1396 free_http_request(url_to_query);
1397 free_current_action(action);
1399 return JB_ERR_MEMORY;
1405 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1406 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1407 if (!err) err = map_block_killer(exports, "valid-url");
1409 free_current_action(action);
1410 free_http_request(url_to_query);
1415 return JB_ERR_MEMORY;
1418 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1422 * We have a warning about SSL paths. Hide it for unencrypted sites.
1424 if (!url_to_query->ssl)
1426 if (map_block_killer(exports, "https"))
1428 free_current_action(action);
1430 free_http_request(url_to_query);
1431 return JB_ERR_MEMORY;
1435 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1437 for (i = 0; i < MAX_AF_FILES; i++)
1439 if (NULL == csp->config->actions_file_short[i]
1440 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1444 if ((fl = csp->actions_list[i]) != NULL)
1446 if ((b = fl->f) != NULL)
1448 /* FIXME: Hardcoded HTML! */
1449 string_append(&matches, "<tr><th>In file: ");
1450 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1451 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1452 string_append(&matches, buf);
1453 string_append(&matches, "View</a>");
1454 #ifdef FEATURE_CGI_EDIT_ACTIONS
1455 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1458 if (access(csp->config->actions_file[i], W_OK) == 0)
1460 #endif /* def HAVE_ACCESS */
1461 snprintf(buf, sizeof(buf),
1462 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1463 string_append(&matches, buf);
1464 string_append(&matches, "Edit</a>");
1469 string_append(&matches, " <strong>No write access.</strong>");
1471 #endif /* def HAVE_ACCESS */
1473 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1475 string_append(&matches, "</th></tr>\n");
1482 for (; (b != NULL) && (matches != NULL); b = b->next)
1484 if (url_match(b->url, url_to_query))
1486 string_append(&matches, "<tr><td>{");
1487 string_join (&matches, actions_to_html(csp, b->action));
1488 string_append(&matches, " }<br>\n<code>");
1489 string_join (&matches, html_encode(b->url->spec));
1490 string_append(&matches, "</code></td></tr>\n");
1492 if (merge_current_action(action, b->action))
1495 free_http_request(url_to_query);
1496 free_current_action(action);
1498 return JB_ERR_MEMORY;
1506 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1509 string_append(&matches, "</table>\n");
1512 * XXX: Kludge to make sure the "Forward settings" section
1513 * shows what forward-override{} would do with the requested URL.
1514 * No one really cares how the CGI request would be forwarded
1515 * if it wasn't intercepted as CGI request in the first place.
1517 * From here on the action bitmask will no longer reflect
1518 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1519 * but luckily it's no longer required later on anyway.
1521 free_current_action(csp->action);
1522 get_url_actions(csp, url_to_query);
1525 * Fill in forwarding settings.
1527 * The possibilities are:
1529 * - http forwarding only
1530 * - socks4(a) forwarding only
1531 * - socks4(a) and http forwarding.
1533 * XXX: Parts of this code could be reused for the
1534 * "forwarding-failed" template which currently doesn't
1535 * display the proxy port and an eventual second forwarder.
1538 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1540 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1542 if (!err) err = map_block_killer(exports, "socks-forwarder");
1543 if (!err) err = map_block_killer(exports, "http-forwarder");
1547 char port[10]; /* We save proxy ports as int but need a string here */
1549 if (!err) err = map_block_killer(exports, "no-forwarder");
1551 if (fwd->gateway_host != NULL)
1553 char *socks_type = NULL;
1558 socks_type = "socks4";
1561 socks_type = "socks4a";
1564 socks_type = "socks5";
1567 socks_type = "socks5t";
1570 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1573 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1574 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1575 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1576 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1580 if (!err) err = map_block_killer(exports, "socks-forwarder");
1583 if (fwd->forward_host != NULL)
1585 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1586 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1587 if (!err) err = map(exports, "forward-port", 1, port, 1);
1591 if (!err) err = map_block_killer(exports, "http-forwarder");
1596 free_http_request(url_to_query);
1598 if (err || matches == NULL)
1600 free_current_action(action);
1602 return JB_ERR_MEMORY;
1605 #ifdef FEATURE_CGI_EDIT_ACTIONS
1606 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1608 err = map_block_killer(exports, "cgi-editor-is-disabled");
1610 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1613 * If zlib support is available, if no content filters
1614 * are enabled or if the prevent-compression action is enabled,
1615 * suppress the "compression could prevent filtering" warning.
1617 #ifndef FEATURE_ZLIB
1618 if (!content_filters_enabled(action) ||
1619 (action->flags & ACTION_NO_COMPRESSION))
1622 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1625 if (err || map(exports, "matches", 1, matches , 0))
1627 free_current_action(action);
1629 return JB_ERR_MEMORY;
1632 s = current_action_to_html(csp, action);
1634 free_current_action(action);
1636 if (map(exports, "final", 1, s, 0))
1639 return JB_ERR_MEMORY;
1643 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1647 /*********************************************************************
1649 * Function : cgi_robots_txt
1651 * Description : CGI function to return "/robots.txt".
1654 * 1 : csp = Current client state (buffers, headers, etc...)
1655 * 2 : rsp = http_response data structure for output
1656 * 3 : parameters = map of cgi parameters
1658 * CGI Parameters : None
1660 * Returns : JB_ERR_OK on success
1661 * JB_ERR_MEMORY on out-of-memory error.
1663 *********************************************************************/
1664 jb_err cgi_robots_txt(struct client_state *csp,
1665 struct http_response *rsp,
1666 const struct map *parameters)
1674 rsp->body = strdup_or_die(
1675 "# This is the Privoxy control interface.\n"
1676 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1683 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1687 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1688 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1690 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1694 /*********************************************************************
1696 * Function : show_defines
1698 * Description : Add to a map the state od all conditional #defines
1699 * used when building
1702 * 1 : exports = map to extend
1704 * Returns : JB_ERR_OK on success
1705 * JB_ERR_MEMORY on out-of-memory error.
1707 *********************************************************************/
1708 static jb_err show_defines(struct map *exports)
1710 jb_err err = JB_ERR_OK;
1713 const char name[31];
1714 const unsigned char is_available;
1717 static const struct feature features[] = {
1719 "FEATURE_ACCEPT_FILTER",
1720 #ifdef FEATURE_ACCEPT_FILTER
1735 "FEATURE_CGI_EDIT_ACTIONS",
1736 #ifdef FEATURE_CGI_EDIT_ACTIONS
1743 "FEATURE_CLIENT_TAGS",
1744 #ifdef FEATURE_CLIENT_TAGS
1751 "FEATURE_COMPRESSION",
1752 #ifdef FEATURE_COMPRESSION
1759 "FEATURE_CONNECTION_KEEP_ALIVE",
1760 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1767 "FEATURE_CONNECTION_SHARING",
1768 #ifdef FEATURE_CONNECTION_SHARING
1775 "FEATURE_FAST_REDIRECTS",
1776 #ifdef FEATURE_FAST_REDIRECTS
1783 "FEATURE_FORCE_LOAD",
1784 #ifdef FEATURE_FORCE_LOAD
1791 "FEATURE_GRACEFUL_TERMINATION",
1792 #ifdef FEATURE_GRACEFUL_TERMINATION
1799 "FEATURE_IMAGE_BLOCKING",
1800 #ifdef FEATURE_IMAGE_BLOCKING
1807 "FEATURE_IMAGE_DETECT_MSIE",
1808 #ifdef FEATURE_IMAGE_DETECT_MSIE
1815 "FEATURE_IPV6_SUPPORT",
1824 #ifdef FEATURE_NO_GIFS
1832 #ifdef FEATURE_PTHREAD
1839 "FEATURE_STATISTICS",
1840 #ifdef FEATURE_STATISTICS
1847 "FEATURE_STRPTIME_SANITY_CHECKS",
1848 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
1856 #ifdef FEATURE_TOGGLE
1864 #ifdef FEATURE_TRUST
1879 "FEATURE_DYNAMIC_PCRE",
1880 #ifdef FEATURE_DYNAMIC_PCRE
1888 for (i = 0; i < SZ(features); i++)
1890 err = map_conditional(exports, features[i].name, features[i].is_available);
1902 /*********************************************************************
1904 * Function : show_rcs
1906 * Description : Create a string with the rcs info for all sourcefiles
1910 * Returns : A string, or NULL on out-of-memory.
1912 *********************************************************************/
1913 static char *show_rcs(void)
1915 char *result = strdup_or_die("");
1916 char buf[BUFFER_SIZE];
1918 /* Instead of including *all* dot h's in the project (thus creating a
1919 * tremendous amount of dependencies), I will concede to declaring them
1920 * as extern's. This forces the developer to add to this list, but oh well.
1923 #define SHOW_RCS(__x) \
1925 extern const char __x[]; \
1926 snprintf(buf, sizeof(buf), " %s\n", __x); \
1927 string_append(&result, buf); \
1930 /* In alphabetical order */
1931 SHOW_RCS(actions_h_rcs)
1932 SHOW_RCS(actions_rcs)
1934 SHOW_RCS(amiga_h_rcs)
1936 #endif /* def AMIGA */
1939 #ifdef FEATURE_CGI_EDIT_ACTIONS
1940 SHOW_RCS(cgiedit_h_rcs)
1941 SHOW_RCS(cgiedit_rcs)
1942 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1943 SHOW_RCS(cgisimple_h_rcs)
1944 SHOW_RCS(cgisimple_rcs)
1946 SHOW_RCS(cygwin_h_rcs)
1948 SHOW_RCS(deanimate_h_rcs)
1949 SHOW_RCS(deanimate_rcs)
1950 SHOW_RCS(encode_h_rcs)
1951 SHOW_RCS(encode_rcs)
1952 SHOW_RCS(errlog_h_rcs)
1953 SHOW_RCS(errlog_rcs)
1954 SHOW_RCS(filters_h_rcs)
1955 SHOW_RCS(filters_rcs)
1956 SHOW_RCS(gateway_h_rcs)
1957 SHOW_RCS(gateway_rcs)
1958 SHOW_RCS(jbsockets_h_rcs)
1959 SHOW_RCS(jbsockets_rcs)
1962 SHOW_RCS(list_h_rcs)
1964 SHOW_RCS(loadcfg_h_rcs)
1965 SHOW_RCS(loadcfg_rcs)
1966 SHOW_RCS(loaders_h_rcs)
1967 SHOW_RCS(loaders_rcs)
1968 SHOW_RCS(miscutil_h_rcs)
1969 SHOW_RCS(miscutil_rcs)
1970 SHOW_RCS(parsers_h_rcs)
1971 SHOW_RCS(parsers_rcs)
1973 SHOW_RCS(pcrs_h_rcs)
1974 SHOW_RCS(project_h_rcs)
1975 SHOW_RCS(ssplit_h_rcs)
1976 SHOW_RCS(ssplit_rcs)
1977 SHOW_RCS(urlmatch_h_rcs)
1978 SHOW_RCS(urlmatch_rcs)
1980 #ifndef _WIN_CONSOLE
1981 SHOW_RCS(w32log_h_rcs)
1982 SHOW_RCS(w32log_rcs)
1983 SHOW_RCS(w32res_h_rcs)
1984 SHOW_RCS(w32taskbar_h_rcs)
1985 SHOW_RCS(w32taskbar_rcs)
1986 #endif /* ndef _WIN_CONSOLE */
1987 SHOW_RCS(win32_h_rcs)
1989 #endif /* def _WIN32 */
1998 /*********************************************************************
2000 * Function : cgi_show_file
2002 * Description : CGI function that shows the content of a
2003 * configuration file.
2006 * 1 : csp = Current client state (buffers, headers, etc...)
2007 * 2 : rsp = http_response data structure for output
2008 * 3 : parameters = map of cgi parameters
2011 * file : Which file to show. Only first letter is checked,
2016 * Default is to show menu and other information.
2018 * Returns : JB_ERR_OK on success
2019 * JB_ERR_MEMORY on out-of-memory error.
2021 *********************************************************************/
2022 static jb_err cgi_show_file(struct client_state *csp,
2023 struct http_response *rsp,
2024 const struct map *parameters)
2027 const char * filename = NULL;
2028 char * file_description = NULL;
2034 switch (*(lookup(parameters, "file")))
2037 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2039 filename = csp->actions_list[i]->filename;
2040 file_description = "Actions File";
2045 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2047 filename = csp->rlist[i]->filename;
2048 file_description = "Filter File";
2052 #ifdef FEATURE_TRUST
2056 filename = csp->tlist->filename;
2057 file_description = "Trust File";
2060 #endif /* def FEATURE_TRUST */
2063 if (NULL != filename)
2065 struct map *exports;
2070 exports = default_exports(csp, "show-status");
2071 if (NULL == exports)
2073 return JB_ERR_MEMORY;
2076 if (map(exports, "file-description", 1, file_description, 1)
2077 || map(exports, "filepath", 1, html_encode(filename), 0))
2080 return JB_ERR_MEMORY;
2083 err = load_file(filename, &s, &length);
2084 if (JB_ERR_OK != err)
2086 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2089 return JB_ERR_MEMORY;
2094 s = html_encode_and_free_original(s);
2098 return JB_ERR_MEMORY;
2101 if (map(exports, "contents", 1, s, 0))
2104 return JB_ERR_MEMORY;
2108 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2111 return JB_ERR_CGI_PARAMS;
2115 /*********************************************************************
2117 * Function : load_file
2119 * Description : Loads a file into a buffer.
2122 * 1 : filename = Name of the file to be loaded.
2123 * 2 : buffer = Used to return the file's content.
2124 * 3 : length = Used to return the size of the file.
2126 * Returns : JB_ERR_OK in case of success,
2127 * JB_ERR_FILE in case of ordinary file loading errors
2128 * (fseek() and ftell() errors are fatal)
2129 * JB_ERR_MEMORY in case of out-of-memory.
2131 *********************************************************************/
2132 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2136 jb_err err = JB_ERR_OK;
2138 fp = fopen(filename, "rb");
2141 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
2145 /* Get file length */
2146 if (fseek(fp, 0, SEEK_END))
2148 log_error(LOG_LEVEL_FATAL,
2149 "Unexpected error while fseek()ing to the end of %s: %E",
2155 log_error(LOG_LEVEL_FATAL,
2156 "Unexpected ftell() error while loading %s: %E",
2159 *length = (size_t)ret;
2161 /* Go back to the beginning. */
2162 if (fseek(fp, 0, SEEK_SET))
2164 log_error(LOG_LEVEL_FATAL,
2165 "Unexpected error while fseek()ing to the beginning of %s: %E",
2169 *buffer = zalloc_or_die(*length + 1);
2171 if (1 != fread(*buffer, *length, 1, fp))
2174 * May theoretically happen if the file size changes between
2175 * fseek() and fread() because it's edited in-place. Privoxy
2176 * and common text editors don't do that, thus we just fail.
2178 log_error(LOG_LEVEL_ERROR,
2179 "Couldn't completely read file %s.", filename);