1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.141 2016/05/08 10:46:29 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=\"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_tag_status;
357 if (NULL == (exports = default_exports(csp, "client-tags")))
359 return JB_ERR_MEMORY;
361 assert(csp->client_address != NULL);
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);
384 this_tag = csp->config->client_tags;
385 if (this_tag->name == NULL)
387 client_tag_status = strdup_or_die("<p>No tags available.</p>\n");
391 client_tag_status = strdup_or_die("<table border=\"1\">\n"
392 "<tr><th>Tag name</th>\n"
393 "<th>Current state</th><th>Change state</th><th>Description</th></tr>\n");
394 while ((this_tag != NULL) && (this_tag->name != NULL))
398 privoxy_mutex_lock(&client_tags_mutex);
399 tag_state = client_has_requested_tag(csp->client_address, this_tag->name);
400 privoxy_mutex_unlock(&client_tags_mutex);
401 if (!err) err = string_append(&client_tag_status, "<tr><td>");
402 if (!err) err = string_append(&client_tag_status, this_tag->name);
403 if (!err) err = string_append(&client_tag_status, "</td><td>");
404 if (!err) err = string_append(&client_tag_status, tag_state == 1 ? "Enabled" : "Disabled");
405 if (!err) err = string_append(&client_tag_status, "</td><td>");
406 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 1);
407 if (!err) err = string_append(&client_tag_status, buf);
410 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 0);
411 if (!err) err = string_append(&client_tag_status, buf);
413 if (!err) err = string_append(&client_tag_status, "</td><td>");
414 if (!err) err = string_append(&client_tag_status, this_tag->description);
415 if (!err) err = string_append(&client_tag_status, "</td></tr>\n");
419 return JB_ERR_MEMORY;
421 this_tag = this_tag->next;
423 if (!err) err = string_append(&client_tag_status, "</table>\n");
426 if (map(exports, "client-tags", 1, client_tag_status, 0))
429 return JB_ERR_MEMORY;
432 if (map(exports, "client-ip-addr", 1, csp->client_address, 1))
435 return JB_ERR_MEMORY;
438 return template_fill_for_cgi(csp, "client-tags", exports, rsp);
440 #endif /* def FEATURE_CLIENT_TAGS */
443 /*********************************************************************
445 * Function : cgi_send_banner
447 * Description : CGI function that returns a banner.
450 * 1 : csp = Current client state (buffers, headers, etc...)
451 * 2 : rsp = http_response data structure for output
452 * 3 : parameters = map of cgi parameters
455 * type : Selects the type of banner between "trans", "logo",
456 * and "auto". Defaults to "logo" if absent or invalid.
457 * "auto" means to select as if we were image-blocking.
458 * (Only the first character really counts; b and t are
461 * Returns : JB_ERR_OK on success
462 * JB_ERR_MEMORY on out-of-memory error.
464 *********************************************************************/
465 jb_err cgi_send_banner(struct client_state *csp,
466 struct http_response *rsp,
467 const struct map *parameters)
469 char imagetype = lookup(parameters, "type")[0];
472 * If type is auto, then determine the right thing
473 * to do from the set-image-blocker action
475 if (imagetype == 'a')
482 #ifdef FEATURE_IMAGE_BLOCKING
483 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
485 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
486 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
487 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
491 /* Use default - nothing to do here. */
493 else if (0 == strcmpic(p, "blank"))
497 else if (0 == strcmpic(p, "pattern"))
503 * If the action is to call this CGI, determine
506 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
508 imagetype = p[sizeof(prefix1) - 1];
510 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
512 imagetype = p[sizeof(prefix2) - 1];
516 * Everything else must (should) be a URL to
524 #endif /* def FEATURE_IMAGE_BLOCKING */
528 * Now imagetype is either the non-auto type we were called with,
529 * or it was auto and has since been determined. In any case, we
530 * can proceed to actually answering the request by sending a redirect
531 * or an image as appropriate:
533 if (imagetype == 'r')
535 rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
536 if (enlist_unique_header(rsp->headers, "Location",
537 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
539 return JB_ERR_MEMORY;
544 if ((imagetype == 'b') || (imagetype == 't'))
546 rsp->body = bindup(image_blank_data, image_blank_length);
547 rsp->content_length = image_blank_length;
551 rsp->body = bindup(image_pattern_data, image_pattern_length);
552 rsp->content_length = image_pattern_length;
555 if (rsp->body == NULL)
557 return JB_ERR_MEMORY;
559 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
561 return JB_ERR_MEMORY;
572 /*********************************************************************
574 * Function : cgi_transparent_image
576 * Description : CGI function that sends a 1x1 transparent image.
579 * 1 : csp = Current client state (buffers, headers, etc...)
580 * 2 : rsp = http_response data structure for output
581 * 3 : parameters = map of cgi parameters
583 * CGI Parameters : None
585 * Returns : JB_ERR_OK on success
586 * JB_ERR_MEMORY on out-of-memory error.
588 *********************************************************************/
589 jb_err cgi_transparent_image(struct client_state *csp,
590 struct http_response *rsp,
591 const struct map *parameters)
596 rsp->body = bindup(image_blank_data, image_blank_length);
597 rsp->content_length = image_blank_length;
599 if (rsp->body == NULL)
601 return JB_ERR_MEMORY;
604 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
606 return JB_ERR_MEMORY;
616 /*********************************************************************
618 * Function : cgi_send_default_favicon
620 * Description : CGI function that sends the standard favicon.
623 * 1 : csp = Current client state (buffers, headers, etc...)
624 * 2 : rsp = http_response data structure for output
625 * 3 : parameters = map of cgi parameters
627 * CGI Parameters : None
629 * Returns : JB_ERR_OK on success
630 * JB_ERR_MEMORY on out-of-memory error.
632 *********************************************************************/
633 jb_err cgi_send_default_favicon(struct client_state *csp,
634 struct http_response *rsp,
635 const struct map *parameters)
637 static const char default_favicon_data[] =
638 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
639 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
640 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
641 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
642 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
643 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
644 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
645 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
646 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
647 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
648 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
649 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
650 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
652 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
657 rsp->body = bindup(default_favicon_data, favicon_length);
658 rsp->content_length = favicon_length;
660 if (rsp->body == NULL)
662 return JB_ERR_MEMORY;
665 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
667 return JB_ERR_MEMORY;
677 /*********************************************************************
679 * Function : cgi_send_error_favicon
681 * Description : CGI function that sends the favicon for error pages.
684 * 1 : csp = Current client state (buffers, headers, etc...)
685 * 2 : rsp = http_response data structure for output
686 * 3 : parameters = map of cgi parameters
688 * CGI Parameters : None
690 * Returns : JB_ERR_OK on success
691 * JB_ERR_MEMORY on out-of-memory error.
693 *********************************************************************/
694 jb_err cgi_send_error_favicon(struct client_state *csp,
695 struct http_response *rsp,
696 const struct map *parameters)
698 static const char error_favicon_data[] =
699 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
700 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
701 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
702 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
703 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
704 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
705 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
706 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
707 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
708 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
709 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
710 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
711 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
713 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
718 rsp->body = bindup(error_favicon_data, favicon_length);
719 rsp->content_length = favicon_length;
721 if (rsp->body == NULL)
723 return JB_ERR_MEMORY;
726 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
728 return JB_ERR_MEMORY;
738 /*********************************************************************
740 * Function : cgi_send_stylesheet
742 * Description : CGI function that sends a css stylesheet found
743 * in the cgi-style.css template
746 * 1 : csp = Current client state (buffers, headers, etc...)
747 * 2 : rsp = http_response data structure for output
748 * 3 : parameters = map of cgi parameters
750 * CGI Parameters : None
752 * Returns : JB_ERR_OK on success
753 * JB_ERR_MEMORY on out-of-memory error.
755 *********************************************************************/
756 jb_err cgi_send_stylesheet(struct client_state *csp,
757 struct http_response *rsp,
758 const struct map *parameters)
767 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
769 if (err == JB_ERR_FILE)
772 * No way to tell user; send empty stylesheet
774 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
778 return err; /* JB_ERR_MEMORY */
781 if (enlist(rsp->headers, "Content-Type: text/css"))
783 return JB_ERR_MEMORY;
791 /*********************************************************************
793 * Function : cgi_send_url_info_osd
795 * Description : CGI function that sends the OpenSearch Description
796 * template for the show-url-info page. It allows to
797 * access the page through "search engine plugins".
800 * 1 : csp = Current client state (buffers, headers, etc...)
801 * 2 : rsp = http_response data structure for output
802 * 3 : parameters = map of cgi parameters
804 * CGI Parameters : None
806 * Returns : JB_ERR_OK on success
807 * JB_ERR_MEMORY on out-of-memory error.
809 *********************************************************************/
810 jb_err cgi_send_url_info_osd(struct client_state *csp,
811 struct http_response *rsp,
812 const struct map *parameters)
814 jb_err err = JB_ERR_MEMORY;
815 struct map *exports = default_exports(csp, NULL);
822 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
823 if (JB_ERR_OK == err)
825 err = enlist(rsp->headers,
826 "Content-Type: application/opensearchdescription+xml");
835 /*********************************************************************
837 * Function : get_content_type
839 * Description : Use the file extension to guess the content type
840 * header we should use to serve the file.
843 * 1 : filename = Name of the file whose content type
846 * Returns : The guessed content type.
848 *********************************************************************/
849 static const char *get_content_type(const char *filename)
854 const char extension[6];
855 const char content_type[11];
857 static const struct content_type content_types[] =
859 {".css", "text/css"},
860 {".jpg", "image/jpeg"},
861 {".jpeg", "image/jpeg"},
862 {".png", "image/png"},
865 for (i = 0; i < SZ(content_types); i++)
867 if (strstr(filename, content_types[i].extension))
869 return content_types[i].content_type;
873 /* No match by extension, default to html */
877 /*********************************************************************
879 * Function : cgi_send_user_manual
881 * Description : CGI function that sends a file in the user
885 * 1 : csp = Current client state (buffers, headers, etc...)
886 * 2 : rsp = http_response data structure for output
887 * 3 : parameters = map of cgi parameters
889 * CGI Parameters : file=name.html, the name of the HTML file
890 * (relative to user-manual from config)
892 * Returns : JB_ERR_OK on success
893 * JB_ERR_MEMORY on out-of-memory error.
895 *********************************************************************/
896 jb_err cgi_send_user_manual(struct client_state *csp,
897 struct http_response *rsp,
898 const struct map *parameters)
900 const char *filename;
902 jb_err err = JB_ERR_OK;
903 const char *content_type;
909 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
911 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
912 "received while user-manual delivery is disabled.");
913 return cgi_error_404(csp, rsp, parameters);
916 if (!parameters->first)
918 /* requested http://p.p/user-manual (without trailing slash) */
919 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
922 get_string_param(parameters, "file", &filename);
923 if (filename == NULL)
925 /* It's '/' so serve the index.html if there is one. */
926 filename = "index.html";
928 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
931 * We currently only support a flat file
932 * hierarchy for the documentation.
934 log_error(LOG_LEVEL_ERROR,
935 "Rejecting the request to serve '%s' as it contains '/' or '..'",
937 return JB_ERR_CGI_PARAMS;
940 full_path = make_path(csp->config->usermanual, filename);
941 if (full_path == NULL)
943 return JB_ERR_MEMORY;
946 err = load_file(full_path, &rsp->body, &rsp->content_length);
947 if (JB_ERR_OK != err)
949 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
950 if (JB_ERR_FILE == err)
952 err = cgi_error_no_template(csp, rsp, full_path);
959 content_type = get_content_type(filename);
960 log_error(LOG_LEVEL_CGI,
961 "Content-Type guessed for %s: %s", filename, content_type);
963 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
968 /*********************************************************************
970 * Function : cgi_show_version
972 * Description : CGI function that returns a a web page describing the
973 * file versions of Privoxy.
976 * 1 : csp = Current client state (buffers, headers, etc...)
977 * 2 : rsp = http_response data structure for output
978 * 3 : parameters = map of cgi parameters
980 * CGI Parameters : none
982 * Returns : JB_ERR_OK on success
983 * JB_ERR_MEMORY on out-of-memory error.
985 *********************************************************************/
986 jb_err cgi_show_version(struct client_state *csp,
987 struct http_response *rsp,
988 const struct map *parameters)
996 if (NULL == (exports = default_exports(csp, "show-version")))
998 return JB_ERR_MEMORY;
1001 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1004 return JB_ERR_MEMORY;
1007 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1011 /*********************************************************************
1013 * Function : cgi_show_status
1015 * Description : CGI function that returns a web page describing the
1016 * current status of Privoxy.
1019 * 1 : csp = Current client state (buffers, headers, etc...)
1020 * 2 : rsp = http_response data structure for output
1021 * 3 : parameters = map of cgi parameters
1024 * file : Which file to show. Only first letter is checked,
1029 * Default is to show menu and other information.
1031 * Returns : JB_ERR_OK on success
1032 * JB_ERR_MEMORY on out-of-memory error.
1034 *********************************************************************/
1035 jb_err cgi_show_status(struct client_state *csp,
1036 struct http_response *rsp,
1037 const struct map *parameters)
1043 char buf[BUFFER_SIZE];
1044 #ifdef FEATURE_STATISTICS
1045 float perc_rej; /* Percentage of http requests rejected */
1046 int local_urls_read;
1047 int local_urls_rejected;
1048 #endif /* ndef FEATURE_STATISTICS */
1049 jb_err err = JB_ERR_OK;
1051 struct map *exports;
1057 if ('\0' != *(lookup(parameters, "file")))
1059 return cgi_show_file(csp, rsp, parameters);
1062 if (NULL == (exports = default_exports(csp, "show-status")))
1064 return JB_ERR_MEMORY;
1068 for (j = 0; (s != NULL) && (j < Argc); j++)
1070 if (!err) err = string_join (&s, html_encode(Argv[j]));
1071 if (!err) err = string_append(&s, " ");
1073 if (!err) err = map(exports, "invocation", 1, s, 0);
1075 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1076 if (!err) err = show_defines(exports);
1081 return JB_ERR_MEMORY;
1084 #ifdef FEATURE_STATISTICS
1085 local_urls_read = urls_read;
1086 local_urls_rejected = urls_rejected;
1089 * Need to alter the stats not to include the fetch of this
1092 * Can't do following thread safely! doh!
1095 * urls_rejected--; * This will be incremented subsequently *
1098 if (local_urls_read == 0)
1100 if (!err) err = map_block_killer(exports, "have-stats");
1104 if (!err) err = map_block_killer(exports, "have-no-stats");
1106 perc_rej = (float)local_urls_rejected * 100.0F /
1107 (float)local_urls_read;
1109 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1110 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1112 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1113 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1115 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1116 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1119 #else /* ndef FEATURE_STATISTICS */
1120 if (!err) err = map_block_killer(exports, "statistics");
1121 #endif /* ndef FEATURE_STATISTICS */
1124 * List all action files in use, together with view and edit links,
1125 * except for standard.action, which should only be viewable. (Not
1126 * enforced in the editor itself)
1127 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1130 for (i = 0; i < MAX_AF_FILES; i++)
1132 if (csp->actions_list[i] != NULL)
1134 if (!err) err = string_append(&s, "<tr><td>");
1135 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1136 snprintf(buf, sizeof(buf),
1137 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1138 if (!err) err = string_append(&s, buf);
1140 #ifdef FEATURE_CGI_EDIT_ACTIONS
1141 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1142 && (NULL != csp->config->actions_file_short[i]))
1145 if (access(csp->config->actions_file[i], W_OK) == 0)
1147 #endif /* def HAVE_ACCESS */
1148 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1149 if (!err) err = string_append(&s, buf);
1154 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1156 #endif /* def HAVE_ACCESS */
1160 if (!err) err = string_append(&s, "</td></tr>\n");
1165 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1169 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1173 * List all re_filterfiles in use, together with view options.
1174 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1177 for (i = 0; i < MAX_AF_FILES; i++)
1179 if (csp->rlist[i] != NULL)
1181 if (!err) err = string_append(&s, "<tr><td>");
1182 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1183 snprintf(buf, sizeof(buf),
1184 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1185 if (!err) err = string_append(&s, buf);
1186 if (!err) err = string_append(&s, "</td></tr>\n");
1191 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1195 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1196 if (!err) err = map_block_killer(exports, "have-filterfile");
1199 #ifdef FEATURE_TRUST
1202 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1206 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1207 if (!err) err = map_block_killer(exports, "have-trustfile");
1210 if (!err) err = map_block_killer(exports, "trust-support");
1211 #endif /* ndef FEATURE_TRUST */
1213 #ifdef FEATURE_CGI_EDIT_ACTIONS
1214 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1216 err = map_block_killer(exports, "cgi-editor-is-disabled");
1218 #endif /* ndef CGI_EDIT_ACTIONS */
1220 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1225 return JB_ERR_MEMORY;
1228 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1232 /*********************************************************************
1234 * Function : cgi_show_url_info
1236 * Description : CGI function that determines and shows which actions
1237 * Privoxy will perform for a given url, and which
1238 * matches starting from the defaults have lead to that.
1241 * 1 : csp = Current client state (buffers, headers, etc...)
1242 * 2 : rsp = http_response data structure for output
1243 * 3 : parameters = map of cgi parameters
1246 * url : The url whose actions are to be determined.
1247 * If url is unset, the url-given conditional will be
1248 * set, so that all but the form can be suppressed in
1251 * Returns : JB_ERR_OK on success
1252 * JB_ERR_MEMORY on out-of-memory error.
1254 *********************************************************************/
1255 jb_err cgi_show_url_info(struct client_state *csp,
1256 struct http_response *rsp,
1257 const struct map *parameters)
1260 struct map *exports;
1267 if (NULL == (exports = default_exports(csp, "show-url-info")))
1269 return JB_ERR_MEMORY;
1273 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1275 url_param = strdup_or_die(lookup(parameters, "url"));
1279 * Handle prefixes. 4 possibilities:
1280 * 1) "http://" or "https://" prefix present and followed by URL - OK
1281 * 2) Only the "http://" or "https://" part is present, no URL - change
1282 * to empty string so it will be detected later as "no URL".
1283 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1285 * 4) Parameter not specified or is empty string - let this fall through
1286 * for now, next block of code will handle it.
1288 if (0 == strncmp(url_param, "http://", 7))
1290 if (url_param[7] == '\0')
1293 * Empty URL (just prefix).
1294 * Make it totally empty so it's caught by the next if ()
1296 url_param[0] = '\0';
1299 else if (0 == strncmp(url_param, "https://", 8))
1301 if (url_param[8] == '\0')
1304 * Empty URL (just prefix).
1305 * Make it totally empty so it's caught by the next if ()
1307 url_param[0] = '\0';
1310 else if ((url_param[0] != '\0')
1311 && ((NULL == strstr(url_param, "://")
1312 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1315 * No prefix or at least no prefix before
1316 * the first slash - assume http://
1318 char *url_param_prefixed = strdup_or_die("http://");
1320 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1323 return JB_ERR_MEMORY;
1325 url_param = url_param_prefixed;
1329 * Hide "toggle off" warning if Privoxy is toggled on.
1332 #ifdef FEATURE_TOGGLE
1333 (global_toggle_state == 1) &&
1334 #endif /* def FEATURE_TOGGLE */
1335 map_block_killer(exports, "privoxy-is-toggled-off")
1340 return JB_ERR_MEMORY;
1343 if (url_param[0] == '\0')
1345 /* URL paramater not specified, display query form only. */
1347 if (map_block_killer(exports, "url-given")
1348 || map(exports, "url", 1, "", 1))
1351 return JB_ERR_MEMORY;
1356 /* Given a URL, so query it. */
1361 struct file_list *fl;
1362 struct url_actions *b;
1363 struct http_request url_to_query[1];
1364 struct current_action_spec action[1];
1367 if (map(exports, "url", 1, html_encode(url_param), 0))
1371 return JB_ERR_MEMORY;
1374 init_current_action(action);
1376 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1378 free_current_action(action);
1381 return JB_ERR_MEMORY;
1384 memset(url_to_query, '\0', sizeof(url_to_query));
1385 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1386 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1390 if (err == JB_ERR_MEMORY)
1392 free_http_request(url_to_query);
1393 free_current_action(action);
1395 return JB_ERR_MEMORY;
1401 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1402 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1403 if (!err) err = map_block_killer(exports, "valid-url");
1405 free_current_action(action);
1406 free_http_request(url_to_query);
1411 return JB_ERR_MEMORY;
1414 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1418 * We have a warning about SSL paths. Hide it for unencrypted sites.
1420 if (!url_to_query->ssl)
1422 if (map_block_killer(exports, "https"))
1424 free_current_action(action);
1426 free_http_request(url_to_query);
1427 return JB_ERR_MEMORY;
1431 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1433 for (i = 0; i < MAX_AF_FILES; i++)
1435 if (NULL == csp->config->actions_file_short[i]
1436 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1440 if ((fl = csp->actions_list[i]) != NULL)
1442 if ((b = fl->f) != NULL)
1444 /* FIXME: Hardcoded HTML! */
1445 string_append(&matches, "<tr><th>In file: ");
1446 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1447 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1448 string_append(&matches, buf);
1449 string_append(&matches, "View</a>");
1450 #ifdef FEATURE_CGI_EDIT_ACTIONS
1451 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1454 if (access(csp->config->actions_file[i], W_OK) == 0)
1456 #endif /* def HAVE_ACCESS */
1457 snprintf(buf, sizeof(buf),
1458 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1459 string_append(&matches, buf);
1460 string_append(&matches, "Edit</a>");
1465 string_append(&matches, " <strong>No write access.</strong>");
1467 #endif /* def HAVE_ACCESS */
1469 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1471 string_append(&matches, "</th></tr>\n");
1478 for (; (b != NULL) && (matches != NULL); b = b->next)
1480 if (url_match(b->url, url_to_query))
1482 string_append(&matches, "<tr><td>{");
1483 string_join (&matches, actions_to_html(csp, b->action));
1484 string_append(&matches, " }<br>\n<code>");
1485 string_join (&matches, html_encode(b->url->spec));
1486 string_append(&matches, "</code></td></tr>\n");
1488 if (merge_current_action(action, b->action))
1491 free_http_request(url_to_query);
1492 free_current_action(action);
1494 return JB_ERR_MEMORY;
1502 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1505 string_append(&matches, "</table>\n");
1508 * XXX: Kludge to make sure the "Forward settings" section
1509 * shows what forward-override{} would do with the requested URL.
1510 * No one really cares how the CGI request would be forwarded
1511 * if it wasn't intercepted as CGI request in the first place.
1513 * From here on the action bitmask will no longer reflect
1514 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1515 * but luckily it's no longer required later on anyway.
1517 free_current_action(csp->action);
1518 get_url_actions(csp, url_to_query);
1521 * Fill in forwarding settings.
1523 * The possibilities are:
1525 * - http forwarding only
1526 * - socks4(a) forwarding only
1527 * - socks4(a) and http forwarding.
1529 * XXX: Parts of this code could be reused for the
1530 * "forwarding-failed" template which currently doesn't
1531 * display the proxy port and an eventual second forwarder.
1534 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1536 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1538 if (!err) err = map_block_killer(exports, "socks-forwarder");
1539 if (!err) err = map_block_killer(exports, "http-forwarder");
1543 char port[10]; /* We save proxy ports as int but need a string here */
1545 if (!err) err = map_block_killer(exports, "no-forwarder");
1547 if (fwd->gateway_host != NULL)
1549 char *socks_type = NULL;
1554 socks_type = "socks4";
1557 socks_type = "socks4a";
1560 socks_type = "socks5";
1563 socks_type = "socks5t";
1566 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1569 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1570 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1571 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1572 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1576 if (!err) err = map_block_killer(exports, "socks-forwarder");
1579 if (fwd->forward_host != NULL)
1581 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1582 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1583 if (!err) err = map(exports, "forward-port", 1, port, 1);
1587 if (!err) err = map_block_killer(exports, "http-forwarder");
1592 free_http_request(url_to_query);
1594 if (err || matches == NULL)
1596 free_current_action(action);
1598 return JB_ERR_MEMORY;
1601 #ifdef FEATURE_CGI_EDIT_ACTIONS
1602 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1604 err = map_block_killer(exports, "cgi-editor-is-disabled");
1606 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1609 * If zlib support is available, if no content filters
1610 * are enabled or if the prevent-compression action is enabled,
1611 * suppress the "compression could prevent filtering" warning.
1613 #ifndef FEATURE_ZLIB
1614 if (!content_filters_enabled(action) ||
1615 (action->flags & ACTION_NO_COMPRESSION))
1618 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1621 if (err || map(exports, "matches", 1, matches , 0))
1623 free_current_action(action);
1625 return JB_ERR_MEMORY;
1628 s = current_action_to_html(csp, action);
1630 free_current_action(action);
1632 if (map(exports, "final", 1, s, 0))
1635 return JB_ERR_MEMORY;
1639 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1643 /*********************************************************************
1645 * Function : cgi_robots_txt
1647 * Description : CGI function to return "/robots.txt".
1650 * 1 : csp = Current client state (buffers, headers, etc...)
1651 * 2 : rsp = http_response data structure for output
1652 * 3 : parameters = map of cgi parameters
1654 * CGI Parameters : None
1656 * Returns : JB_ERR_OK on success
1657 * JB_ERR_MEMORY on out-of-memory error.
1659 *********************************************************************/
1660 jb_err cgi_robots_txt(struct client_state *csp,
1661 struct http_response *rsp,
1662 const struct map *parameters)
1670 rsp->body = strdup_or_die(
1671 "# This is the Privoxy control interface.\n"
1672 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1679 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1683 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1684 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1686 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1690 /*********************************************************************
1692 * Function : show_defines
1694 * Description : Add to a map the state od all conditional #defines
1695 * used when building
1698 * 1 : exports = map to extend
1700 * Returns : JB_ERR_OK on success
1701 * JB_ERR_MEMORY on out-of-memory error.
1703 *********************************************************************/
1704 static jb_err show_defines(struct map *exports)
1706 jb_err err = JB_ERR_OK;
1709 const char name[31];
1710 const unsigned char is_available;
1713 static const struct feature features[] = {
1715 "FEATURE_64_BIT_TIME_T",
1716 #if (SIZEOF_TIME_T == 8)
1723 "FEATURE_ACCEPT_FILTER",
1724 #ifdef FEATURE_ACCEPT_FILTER
1739 "FEATURE_CGI_EDIT_ACTIONS",
1740 #ifdef FEATURE_CGI_EDIT_ACTIONS
1747 "FEATURE_CLIENT_TAGS",
1748 #ifdef FEATURE_CLIENT_TAGS
1755 "FEATURE_COMPRESSION",
1756 #ifdef FEATURE_COMPRESSION
1763 "FEATURE_CONNECTION_KEEP_ALIVE",
1764 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1771 "FEATURE_CONNECTION_SHARING",
1772 #ifdef FEATURE_CONNECTION_SHARING
1779 "FEATURE_FAST_REDIRECTS",
1780 #ifdef FEATURE_FAST_REDIRECTS
1787 "FEATURE_FORCE_LOAD",
1788 #ifdef FEATURE_FORCE_LOAD
1795 "FEATURE_GRACEFUL_TERMINATION",
1796 #ifdef FEATURE_GRACEFUL_TERMINATION
1803 "FEATURE_IMAGE_BLOCKING",
1804 #ifdef FEATURE_IMAGE_BLOCKING
1811 "FEATURE_IMAGE_DETECT_MSIE",
1812 #ifdef FEATURE_IMAGE_DETECT_MSIE
1819 "FEATURE_IPV6_SUPPORT",
1828 #ifdef FEATURE_NO_GIFS
1836 #ifdef FEATURE_PTHREAD
1843 "FEATURE_STATISTICS",
1844 #ifdef FEATURE_STATISTICS
1851 "FEATURE_STRPTIME_SANITY_CHECKS",
1852 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
1860 #ifdef FEATURE_TOGGLE
1868 #ifdef FEATURE_TRUST
1883 "FEATURE_DYNAMIC_PCRE",
1884 #ifdef FEATURE_DYNAMIC_PCRE
1892 for (i = 0; i < SZ(features); i++)
1894 err = map_conditional(exports, features[i].name, features[i].is_available);
1906 /*********************************************************************
1908 * Function : show_rcs
1910 * Description : Create a string with the rcs info for all sourcefiles
1914 * Returns : A string, or NULL on out-of-memory.
1916 *********************************************************************/
1917 static char *show_rcs(void)
1919 char *result = strdup_or_die("");
1920 char buf[BUFFER_SIZE];
1922 /* Instead of including *all* dot h's in the project (thus creating a
1923 * tremendous amount of dependencies), I will concede to declaring them
1924 * as extern's. This forces the developer to add to this list, but oh well.
1927 #define SHOW_RCS(__x) \
1929 extern const char __x[]; \
1930 snprintf(buf, sizeof(buf), " %s\n", __x); \
1931 string_append(&result, buf); \
1934 /* In alphabetical order */
1935 SHOW_RCS(actions_h_rcs)
1936 SHOW_RCS(actions_rcs)
1938 SHOW_RCS(amiga_h_rcs)
1940 #endif /* def AMIGA */
1943 #ifdef FEATURE_CGI_EDIT_ACTIONS
1944 SHOW_RCS(cgiedit_h_rcs)
1945 SHOW_RCS(cgiedit_rcs)
1946 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1947 SHOW_RCS(cgisimple_h_rcs)
1948 SHOW_RCS(cgisimple_rcs)
1950 SHOW_RCS(cygwin_h_rcs)
1952 SHOW_RCS(deanimate_h_rcs)
1953 SHOW_RCS(deanimate_rcs)
1954 SHOW_RCS(encode_h_rcs)
1955 SHOW_RCS(encode_rcs)
1956 SHOW_RCS(errlog_h_rcs)
1957 SHOW_RCS(errlog_rcs)
1958 SHOW_RCS(filters_h_rcs)
1959 SHOW_RCS(filters_rcs)
1960 SHOW_RCS(gateway_h_rcs)
1961 SHOW_RCS(gateway_rcs)
1962 SHOW_RCS(jbsockets_h_rcs)
1963 SHOW_RCS(jbsockets_rcs)
1966 SHOW_RCS(list_h_rcs)
1968 SHOW_RCS(loadcfg_h_rcs)
1969 SHOW_RCS(loadcfg_rcs)
1970 SHOW_RCS(loaders_h_rcs)
1971 SHOW_RCS(loaders_rcs)
1972 SHOW_RCS(miscutil_h_rcs)
1973 SHOW_RCS(miscutil_rcs)
1974 SHOW_RCS(parsers_h_rcs)
1975 SHOW_RCS(parsers_rcs)
1977 SHOW_RCS(pcrs_h_rcs)
1978 SHOW_RCS(project_h_rcs)
1979 SHOW_RCS(ssplit_h_rcs)
1980 SHOW_RCS(ssplit_rcs)
1981 SHOW_RCS(urlmatch_h_rcs)
1982 SHOW_RCS(urlmatch_rcs)
1984 #ifndef _WIN_CONSOLE
1985 SHOW_RCS(w32log_h_rcs)
1986 SHOW_RCS(w32log_rcs)
1987 SHOW_RCS(w32res_h_rcs)
1988 SHOW_RCS(w32taskbar_h_rcs)
1989 SHOW_RCS(w32taskbar_rcs)
1990 #endif /* ndef _WIN_CONSOLE */
1991 SHOW_RCS(win32_h_rcs)
1993 #endif /* def _WIN32 */
2002 /*********************************************************************
2004 * Function : cgi_show_file
2006 * Description : CGI function that shows the content of a
2007 * configuration file.
2010 * 1 : csp = Current client state (buffers, headers, etc...)
2011 * 2 : rsp = http_response data structure for output
2012 * 3 : parameters = map of cgi parameters
2015 * file : Which file to show. Only first letter is checked,
2020 * Default is to show menu and other information.
2022 * Returns : JB_ERR_OK on success
2023 * JB_ERR_MEMORY on out-of-memory error.
2025 *********************************************************************/
2026 static jb_err cgi_show_file(struct client_state *csp,
2027 struct http_response *rsp,
2028 const struct map *parameters)
2031 const char * filename = NULL;
2032 char * file_description = NULL;
2038 switch (*(lookup(parameters, "file")))
2041 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2043 filename = csp->actions_list[i]->filename;
2044 file_description = "Actions File";
2049 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2051 filename = csp->rlist[i]->filename;
2052 file_description = "Filter File";
2056 #ifdef FEATURE_TRUST
2060 filename = csp->tlist->filename;
2061 file_description = "Trust File";
2064 #endif /* def FEATURE_TRUST */
2067 if (NULL != filename)
2069 struct map *exports;
2074 exports = default_exports(csp, "show-status");
2075 if (NULL == exports)
2077 return JB_ERR_MEMORY;
2080 if (map(exports, "file-description", 1, file_description, 1)
2081 || map(exports, "filepath", 1, html_encode(filename), 0))
2084 return JB_ERR_MEMORY;
2087 err = load_file(filename, &s, &length);
2088 if (JB_ERR_OK != err)
2090 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2093 return JB_ERR_MEMORY;
2098 s = html_encode_and_free_original(s);
2102 return JB_ERR_MEMORY;
2105 if (map(exports, "contents", 1, s, 0))
2108 return JB_ERR_MEMORY;
2112 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2115 return JB_ERR_CGI_PARAMS;
2119 /*********************************************************************
2121 * Function : load_file
2123 * Description : Loads a file into a buffer.
2126 * 1 : filename = Name of the file to be loaded.
2127 * 2 : buffer = Used to return the file's content.
2128 * 3 : length = Used to return the size of the file.
2130 * Returns : JB_ERR_OK in case of success,
2131 * JB_ERR_FILE in case of ordinary file loading errors
2132 * (fseek() and ftell() errors are fatal)
2133 * JB_ERR_MEMORY in case of out-of-memory.
2135 *********************************************************************/
2136 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2140 jb_err err = JB_ERR_OK;
2142 fp = fopen(filename, "rb");
2145 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
2149 /* Get file length */
2150 if (fseek(fp, 0, SEEK_END))
2152 log_error(LOG_LEVEL_FATAL,
2153 "Unexpected error while fseek()ing to the end of %s: %E",
2159 log_error(LOG_LEVEL_FATAL,
2160 "Unexpected ftell() error while loading %s: %E",
2163 *length = (size_t)ret;
2165 /* Go back to the beginning. */
2166 if (fseek(fp, 0, SEEK_SET))
2168 log_error(LOG_LEVEL_FATAL,
2169 "Unexpected error while fseek()ing to the beginning of %s: %E",
2173 *buffer = zalloc_or_die(*length + 1);
2175 if (1 != fread(*buffer, *length, 1, fp))
2178 * May theoretically happen if the file size changes between
2179 * fseek() and fread() because it's edited in-place. Privoxy
2180 * and common text editors don't do that, thus we just fail.
2182 log_error(LOG_LEVEL_ERROR,
2183 "Couldn't completely read file %s.", filename);