1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.143 2016/09/27 22:48:28 ler762 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=\"toggle-client-tag\" 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 char *client_tag_status;
353 if (NULL == (exports = default_exports(csp, "client-tags")))
355 return JB_ERR_MEMORY;
357 assert(csp->client_address != NULL);
359 this_tag = csp->config->client_tags;
360 if (this_tag->name == NULL)
362 client_tag_status = strdup_or_die("<p>No tags available.</p>\n");
366 client_tag_status = strdup_or_die("<table border=\"1\">\n"
367 "<tr><th>Tag name</th>\n"
368 "<th>Current state</th><th>Change state</th><th>Description</th></tr>\n");
369 while ((this_tag != NULL) && (this_tag->name != NULL))
373 privoxy_mutex_lock(&client_tags_mutex);
374 tag_state = client_has_requested_tag(csp->client_address, this_tag->name);
375 privoxy_mutex_unlock(&client_tags_mutex);
376 if (!err) err = string_append(&client_tag_status, "<tr><td>");
377 if (!err) err = string_append(&client_tag_status, this_tag->name);
378 if (!err) err = string_append(&client_tag_status, "</td><td>");
379 if (!err) err = string_append(&client_tag_status, tag_state == 1 ? "Enabled" : "Disabled");
380 if (!err) err = string_append(&client_tag_status, "</td><td>");
381 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 1);
382 if (!err) err = string_append(&client_tag_status, buf);
385 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 0);
386 if (!err) err = string_append(&client_tag_status, buf);
388 if (!err) err = string_append(&client_tag_status, "</td><td>");
389 if (!err) err = string_append(&client_tag_status, this_tag->description);
390 if (!err) err = string_append(&client_tag_status, "</td></tr>\n");
394 return JB_ERR_MEMORY;
396 this_tag = this_tag->next;
398 if (!err) err = string_append(&client_tag_status, "</table>\n");
401 if (map(exports, "client-tags", 1, client_tag_status, 0))
404 return JB_ERR_MEMORY;
407 if (map(exports, "client-ip-addr", 1, csp->client_address, 1))
410 return JB_ERR_MEMORY;
413 return template_fill_for_cgi(csp, "client-tags", exports, rsp);
417 /*********************************************************************
419 * Function : cgi_toggle_client_tag
421 * Description : Toggles a client tag and redirects to the show-tags
425 * 1 : csp = Current client state (buffers, headers, etc...)
426 * 2 : rsp = http_response data structure for output
427 * 3 : parameters = map of cgi parameters
429 * CGI Parameters : none
430 * 1 : tag = Name of the tag to enable or disable
431 * 2 : toggle-state = How to toggle the tag (0/1)
432 * 3 : expires = Set to 1 if the tag should be enabled
433 * temporarily, otherwise set to 0
435 * Returns : JB_ERR_OK on success
436 * JB_ERR_MEMORY on out-of-memory error.
438 *********************************************************************/
439 jb_err cgi_toggle_client_tag(struct client_state *csp,
440 struct http_response *rsp,
441 const struct map *parameters)
443 const char *toggled_tag;
444 const char *toggle_state;
445 const char *tag_expires;
452 toggled_tag = lookup(parameters, "tag");
453 if (*toggled_tag == '\0')
455 log_error(LOG_LEVEL_ERROR, "Received tag toggle request without tag");
459 tag_expires = lookup(parameters, "expires");
460 if (*tag_expires == '0')
466 time_to_live = csp->config->client_tag_lifetime;
468 toggle_state = lookup(parameters, "toggle-state");
469 if (*toggle_state == '1')
471 enable_client_specific_tag(csp, toggled_tag, time_to_live);
475 disable_client_specific_tag(csp, toggled_tag);
478 rsp->status = strdup_or_die("302 Done dealing with toggle request");
479 if (enlist_unique_header(rsp->headers,
480 "Location", CGI_PREFIX "client-tags"))
482 return JB_ERR_MEMORY;
487 #endif /* def FEATURE_CLIENT_TAGS */
490 /*********************************************************************
492 * Function : cgi_send_banner
494 * Description : CGI function that returns a banner.
497 * 1 : csp = Current client state (buffers, headers, etc...)
498 * 2 : rsp = http_response data structure for output
499 * 3 : parameters = map of cgi parameters
502 * type : Selects the type of banner between "trans", "logo",
503 * and "auto". Defaults to "logo" if absent or invalid.
504 * "auto" means to select as if we were image-blocking.
505 * (Only the first character really counts; b and t are
508 * Returns : JB_ERR_OK on success
509 * JB_ERR_MEMORY on out-of-memory error.
511 *********************************************************************/
512 jb_err cgi_send_banner(struct client_state *csp,
513 struct http_response *rsp,
514 const struct map *parameters)
516 char imagetype = lookup(parameters, "type")[0];
519 * If type is auto, then determine the right thing
520 * to do from the set-image-blocker action
522 if (imagetype == 'a')
529 #ifdef FEATURE_IMAGE_BLOCKING
530 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
532 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
533 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
534 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
538 /* Use default - nothing to do here. */
540 else if (0 == strcmpic(p, "blank"))
544 else if (0 == strcmpic(p, "pattern"))
550 * If the action is to call this CGI, determine
553 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
555 imagetype = p[sizeof(prefix1) - 1];
557 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
559 imagetype = p[sizeof(prefix2) - 1];
563 * Everything else must (should) be a URL to
571 #endif /* def FEATURE_IMAGE_BLOCKING */
575 * Now imagetype is either the non-auto type we were called with,
576 * or it was auto and has since been determined. In any case, we
577 * can proceed to actually answering the request by sending a redirect
578 * or an image as appropriate:
580 if (imagetype == 'r')
582 rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
583 if (enlist_unique_header(rsp->headers, "Location",
584 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
586 return JB_ERR_MEMORY;
591 if ((imagetype == 'b') || (imagetype == 't'))
593 rsp->body = bindup(image_blank_data, image_blank_length);
594 rsp->content_length = image_blank_length;
598 rsp->body = bindup(image_pattern_data, image_pattern_length);
599 rsp->content_length = image_pattern_length;
602 if (rsp->body == NULL)
604 return JB_ERR_MEMORY;
606 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
608 return JB_ERR_MEMORY;
619 /*********************************************************************
621 * Function : cgi_transparent_image
623 * Description : CGI function that sends a 1x1 transparent image.
626 * 1 : csp = Current client state (buffers, headers, etc...)
627 * 2 : rsp = http_response data structure for output
628 * 3 : parameters = map of cgi parameters
630 * CGI Parameters : None
632 * Returns : JB_ERR_OK on success
633 * JB_ERR_MEMORY on out-of-memory error.
635 *********************************************************************/
636 jb_err cgi_transparent_image(struct client_state *csp,
637 struct http_response *rsp,
638 const struct map *parameters)
643 rsp->body = bindup(image_blank_data, image_blank_length);
644 rsp->content_length = image_blank_length;
646 if (rsp->body == NULL)
648 return JB_ERR_MEMORY;
651 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
653 return JB_ERR_MEMORY;
663 /*********************************************************************
665 * Function : cgi_send_default_favicon
667 * Description : CGI function that sends the standard favicon.
670 * 1 : csp = Current client state (buffers, headers, etc...)
671 * 2 : rsp = http_response data structure for output
672 * 3 : parameters = map of cgi parameters
674 * CGI Parameters : None
676 * Returns : JB_ERR_OK on success
677 * JB_ERR_MEMORY on out-of-memory error.
679 *********************************************************************/
680 jb_err cgi_send_default_favicon(struct client_state *csp,
681 struct http_response *rsp,
682 const struct map *parameters)
684 static const char default_favicon_data[] =
685 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
686 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
687 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
688 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
689 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
690 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
691 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
692 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
693 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
694 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
695 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
696 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
697 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
699 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
704 rsp->body = bindup(default_favicon_data, favicon_length);
705 rsp->content_length = favicon_length;
707 if (rsp->body == NULL)
709 return JB_ERR_MEMORY;
712 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
714 return JB_ERR_MEMORY;
724 /*********************************************************************
726 * Function : cgi_send_error_favicon
728 * Description : CGI function that sends the favicon for error pages.
731 * 1 : csp = Current client state (buffers, headers, etc...)
732 * 2 : rsp = http_response data structure for output
733 * 3 : parameters = map of cgi parameters
735 * CGI Parameters : None
737 * Returns : JB_ERR_OK on success
738 * JB_ERR_MEMORY on out-of-memory error.
740 *********************************************************************/
741 jb_err cgi_send_error_favicon(struct client_state *csp,
742 struct http_response *rsp,
743 const struct map *parameters)
745 static const char error_favicon_data[] =
746 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
747 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
748 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
749 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
750 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
751 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
752 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
753 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
754 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
755 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
756 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
757 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
758 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
760 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
765 rsp->body = bindup(error_favicon_data, favicon_length);
766 rsp->content_length = favicon_length;
768 if (rsp->body == NULL)
770 return JB_ERR_MEMORY;
773 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
775 return JB_ERR_MEMORY;
785 /*********************************************************************
787 * Function : cgi_send_stylesheet
789 * Description : CGI function that sends a css stylesheet found
790 * in the cgi-style.css template
793 * 1 : csp = Current client state (buffers, headers, etc...)
794 * 2 : rsp = http_response data structure for output
795 * 3 : parameters = map of cgi parameters
797 * CGI Parameters : None
799 * Returns : JB_ERR_OK on success
800 * JB_ERR_MEMORY on out-of-memory error.
802 *********************************************************************/
803 jb_err cgi_send_stylesheet(struct client_state *csp,
804 struct http_response *rsp,
805 const struct map *parameters)
814 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
816 if (err == JB_ERR_FILE)
819 * No way to tell user; send empty stylesheet
821 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
825 return err; /* JB_ERR_MEMORY */
828 if (enlist(rsp->headers, "Content-Type: text/css"))
830 return JB_ERR_MEMORY;
838 /*********************************************************************
840 * Function : cgi_send_url_info_osd
842 * Description : CGI function that sends the OpenSearch Description
843 * template for the show-url-info page. It allows to
844 * access the page through "search engine plugins".
847 * 1 : csp = Current client state (buffers, headers, etc...)
848 * 2 : rsp = http_response data structure for output
849 * 3 : parameters = map of cgi parameters
851 * CGI Parameters : None
853 * Returns : JB_ERR_OK on success
854 * JB_ERR_MEMORY on out-of-memory error.
856 *********************************************************************/
857 jb_err cgi_send_url_info_osd(struct client_state *csp,
858 struct http_response *rsp,
859 const struct map *parameters)
861 jb_err err = JB_ERR_MEMORY;
862 struct map *exports = default_exports(csp, NULL);
869 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
870 if (JB_ERR_OK == err)
872 err = enlist(rsp->headers,
873 "Content-Type: application/opensearchdescription+xml");
882 /*********************************************************************
884 * Function : get_content_type
886 * Description : Use the file extension to guess the content type
887 * header we should use to serve the file.
890 * 1 : filename = Name of the file whose content type
893 * Returns : The guessed content type.
895 *********************************************************************/
896 static const char *get_content_type(const char *filename)
901 const char extension[6];
902 const char content_type[11];
904 static const struct content_type content_types[] =
906 {".css", "text/css"},
907 {".jpg", "image/jpeg"},
908 {".jpeg", "image/jpeg"},
909 {".png", "image/png"},
912 for (i = 0; i < SZ(content_types); i++)
914 if (strstr(filename, content_types[i].extension))
916 return content_types[i].content_type;
920 /* No match by extension, default to html */
924 /*********************************************************************
926 * Function : cgi_send_user_manual
928 * Description : CGI function that sends a file in the user
932 * 1 : csp = Current client state (buffers, headers, etc...)
933 * 2 : rsp = http_response data structure for output
934 * 3 : parameters = map of cgi parameters
936 * CGI Parameters : file=name.html, the name of the HTML file
937 * (relative to user-manual from config)
939 * Returns : JB_ERR_OK on success
940 * JB_ERR_MEMORY on out-of-memory error.
942 *********************************************************************/
943 jb_err cgi_send_user_manual(struct client_state *csp,
944 struct http_response *rsp,
945 const struct map *parameters)
947 const char *filename;
949 jb_err err = JB_ERR_OK;
950 const char *content_type;
956 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
958 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
959 "received while user-manual delivery is disabled.");
960 return cgi_error_404(csp, rsp, parameters);
963 if (!parameters->first)
965 /* requested http://p.p/user-manual (without trailing slash) */
966 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
969 get_string_param(parameters, "file", &filename);
970 if (filename == NULL)
972 /* It's '/' so serve the index.html if there is one. */
973 filename = "index.html";
975 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
978 * We currently only support a flat file
979 * hierarchy for the documentation.
981 log_error(LOG_LEVEL_ERROR,
982 "Rejecting the request to serve '%s' as it contains '/' or '..'",
984 return JB_ERR_CGI_PARAMS;
987 full_path = make_path(csp->config->usermanual, filename);
988 if (full_path == NULL)
990 return JB_ERR_MEMORY;
993 err = load_file(full_path, &rsp->body, &rsp->content_length);
994 if (JB_ERR_OK != err)
996 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
997 if (JB_ERR_FILE == err)
999 err = cgi_error_no_template(csp, rsp, full_path);
1006 content_type = get_content_type(filename);
1007 log_error(LOG_LEVEL_CGI,
1008 "Content-Type guessed for %s: %s", filename, content_type);
1010 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
1015 /*********************************************************************
1017 * Function : cgi_show_version
1019 * Description : CGI function that returns a a web page describing the
1020 * file versions 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
1027 * CGI Parameters : none
1029 * Returns : JB_ERR_OK on success
1030 * JB_ERR_MEMORY on out-of-memory error.
1032 *********************************************************************/
1033 jb_err cgi_show_version(struct client_state *csp,
1034 struct http_response *rsp,
1035 const struct map *parameters)
1037 struct map *exports;
1043 if (NULL == (exports = default_exports(csp, "show-version")))
1045 return JB_ERR_MEMORY;
1048 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1051 return JB_ERR_MEMORY;
1054 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1058 /*********************************************************************
1060 * Function : cgi_show_status
1062 * Description : CGI function that returns a web page describing the
1063 * current status of Privoxy.
1066 * 1 : csp = Current client state (buffers, headers, etc...)
1067 * 2 : rsp = http_response data structure for output
1068 * 3 : parameters = map of cgi parameters
1071 * file : Which file to show. Only first letter is checked,
1076 * Default is to show menu and other information.
1078 * Returns : JB_ERR_OK on success
1079 * JB_ERR_MEMORY on out-of-memory error.
1081 *********************************************************************/
1082 jb_err cgi_show_status(struct client_state *csp,
1083 struct http_response *rsp,
1084 const struct map *parameters)
1090 char buf[BUFFER_SIZE];
1091 #ifdef FEATURE_STATISTICS
1092 float perc_rej; /* Percentage of http requests rejected */
1093 int local_urls_read;
1094 int local_urls_rejected;
1095 #endif /* ndef FEATURE_STATISTICS */
1096 jb_err err = JB_ERR_OK;
1098 struct map *exports;
1104 if ('\0' != *(lookup(parameters, "file")))
1106 return cgi_show_file(csp, rsp, parameters);
1109 if (NULL == (exports = default_exports(csp, "show-status")))
1111 return JB_ERR_MEMORY;
1115 for (j = 0; (s != NULL) && (j < Argc); j++)
1117 if (!err) err = string_join (&s, html_encode(Argv[j]));
1118 if (!err) err = string_append(&s, " ");
1120 if (!err) err = map(exports, "invocation", 1, s, 0);
1122 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1123 if (!err) err = show_defines(exports);
1128 return JB_ERR_MEMORY;
1131 #ifdef FEATURE_STATISTICS
1132 local_urls_read = urls_read;
1133 local_urls_rejected = urls_rejected;
1136 * Need to alter the stats not to include the fetch of this
1139 * Can't do following thread safely! doh!
1142 * urls_rejected--; * This will be incremented subsequently *
1145 if (local_urls_read == 0)
1147 if (!err) err = map_block_killer(exports, "have-stats");
1151 if (!err) err = map_block_killer(exports, "have-no-stats");
1153 perc_rej = (float)local_urls_rejected * 100.0F /
1154 (float)local_urls_read;
1156 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1157 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1159 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1160 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1162 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1163 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1166 #else /* ndef FEATURE_STATISTICS */
1167 if (!err) err = map_block_killer(exports, "statistics");
1168 #endif /* ndef FEATURE_STATISTICS */
1171 * List all action files in use, together with view and edit links,
1172 * except for standard.action, which should only be viewable. (Not
1173 * enforced in the editor itself)
1174 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1177 for (i = 0; i < MAX_AF_FILES; i++)
1179 if (csp->actions_list[i] != NULL)
1181 if (!err) err = string_append(&s, "<tr><td>");
1182 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1183 snprintf(buf, sizeof(buf),
1184 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1185 if (!err) err = string_append(&s, buf);
1187 #ifdef FEATURE_CGI_EDIT_ACTIONS
1188 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1189 && (NULL != csp->config->actions_file_short[i]))
1192 if (access(csp->config->actions_file[i], W_OK) == 0)
1194 #endif /* def HAVE_ACCESS */
1195 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1196 if (!err) err = string_append(&s, buf);
1201 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1203 #endif /* def HAVE_ACCESS */
1207 if (!err) err = string_append(&s, "</td></tr>\n");
1212 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1216 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1220 * List all re_filterfiles in use, together with view options.
1221 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1224 for (i = 0; i < MAX_AF_FILES; i++)
1226 if (csp->rlist[i] != NULL)
1228 if (!err) err = string_append(&s, "<tr><td>");
1229 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1230 snprintf(buf, sizeof(buf),
1231 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1232 if (!err) err = string_append(&s, buf);
1233 if (!err) err = string_append(&s, "</td></tr>\n");
1238 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1242 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1243 if (!err) err = map_block_killer(exports, "have-filterfile");
1246 #ifdef FEATURE_TRUST
1249 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1253 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1254 if (!err) err = map_block_killer(exports, "have-trustfile");
1257 if (!err) err = map_block_killer(exports, "trust-support");
1258 #endif /* ndef FEATURE_TRUST */
1260 #ifdef FEATURE_CGI_EDIT_ACTIONS
1261 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1263 err = map_block_killer(exports, "cgi-editor-is-disabled");
1265 #endif /* ndef CGI_EDIT_ACTIONS */
1267 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1272 return JB_ERR_MEMORY;
1275 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1279 /*********************************************************************
1281 * Function : cgi_show_url_info
1283 * Description : CGI function that determines and shows which actions
1284 * Privoxy will perform for a given url, and which
1285 * matches starting from the defaults have lead to that.
1288 * 1 : csp = Current client state (buffers, headers, etc...)
1289 * 2 : rsp = http_response data structure for output
1290 * 3 : parameters = map of cgi parameters
1293 * url : The url whose actions are to be determined.
1294 * If url is unset, the url-given conditional will be
1295 * set, so that all but the form can be suppressed in
1298 * Returns : JB_ERR_OK on success
1299 * JB_ERR_MEMORY on out-of-memory error.
1301 *********************************************************************/
1302 jb_err cgi_show_url_info(struct client_state *csp,
1303 struct http_response *rsp,
1304 const struct map *parameters)
1307 struct map *exports;
1314 if (NULL == (exports = default_exports(csp, "show-url-info")))
1316 return JB_ERR_MEMORY;
1320 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1322 url_param = strdup_or_die(lookup(parameters, "url"));
1326 * Handle prefixes. 4 possibilities:
1327 * 1) "http://" or "https://" prefix present and followed by URL - OK
1328 * 2) Only the "http://" or "https://" part is present, no URL - change
1329 * to empty string so it will be detected later as "no URL".
1330 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1332 * 4) Parameter not specified or is empty string - let this fall through
1333 * for now, next block of code will handle it.
1335 if (0 == strncmp(url_param, "http://", 7))
1337 if (url_param[7] == '\0')
1340 * Empty URL (just prefix).
1341 * Make it totally empty so it's caught by the next if ()
1343 url_param[0] = '\0';
1346 else if (0 == strncmp(url_param, "https://", 8))
1348 if (url_param[8] == '\0')
1351 * Empty URL (just prefix).
1352 * Make it totally empty so it's caught by the next if ()
1354 url_param[0] = '\0';
1357 else if ((url_param[0] != '\0')
1358 && ((NULL == strstr(url_param, "://")
1359 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1362 * No prefix or at least no prefix before
1363 * the first slash - assume http://
1365 char *url_param_prefixed = strdup_or_die("http://");
1367 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1370 return JB_ERR_MEMORY;
1372 url_param = url_param_prefixed;
1376 * Hide "toggle off" warning if Privoxy is toggled on.
1379 #ifdef FEATURE_TOGGLE
1380 (global_toggle_state == 1) &&
1381 #endif /* def FEATURE_TOGGLE */
1382 map_block_killer(exports, "privoxy-is-toggled-off")
1387 return JB_ERR_MEMORY;
1390 if (url_param[0] == '\0')
1392 /* URL paramater not specified, display query form only. */
1394 if (map_block_killer(exports, "url-given")
1395 || map(exports, "url", 1, "", 1))
1398 return JB_ERR_MEMORY;
1403 /* Given a URL, so query it. */
1408 struct file_list *fl;
1409 struct url_actions *b;
1410 struct http_request url_to_query[1];
1411 struct current_action_spec action[1];
1414 if (map(exports, "url", 1, html_encode(url_param), 0))
1418 return JB_ERR_MEMORY;
1421 init_current_action(action);
1423 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1425 free_current_action(action);
1428 return JB_ERR_MEMORY;
1431 memset(url_to_query, '\0', sizeof(url_to_query));
1432 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1433 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1437 if (err == JB_ERR_MEMORY)
1439 free_http_request(url_to_query);
1440 free_current_action(action);
1442 return JB_ERR_MEMORY;
1448 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1449 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1450 if (!err) err = map_block_killer(exports, "valid-url");
1452 free_current_action(action);
1453 free_http_request(url_to_query);
1458 return JB_ERR_MEMORY;
1461 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1465 * We have a warning about SSL paths. Hide it for unencrypted sites.
1467 if (!url_to_query->ssl)
1469 if (map_block_killer(exports, "https"))
1471 free_current_action(action);
1473 free_http_request(url_to_query);
1474 return JB_ERR_MEMORY;
1478 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1480 for (i = 0; i < MAX_AF_FILES; i++)
1482 if (NULL == csp->config->actions_file_short[i]
1483 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1487 if ((fl = csp->actions_list[i]) != NULL)
1489 if ((b = fl->f) != NULL)
1491 /* FIXME: Hardcoded HTML! */
1492 string_append(&matches, "<tr><th>In file: ");
1493 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1494 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1495 string_append(&matches, buf);
1496 string_append(&matches, "View</a>");
1497 #ifdef FEATURE_CGI_EDIT_ACTIONS
1498 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1501 if (access(csp->config->actions_file[i], W_OK) == 0)
1503 #endif /* def HAVE_ACCESS */
1504 snprintf(buf, sizeof(buf),
1505 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1506 string_append(&matches, buf);
1507 string_append(&matches, "Edit</a>");
1512 string_append(&matches, " <strong>No write access.</strong>");
1514 #endif /* def HAVE_ACCESS */
1516 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1518 string_append(&matches, "</th></tr>\n");
1525 for (; (b != NULL) && (matches != NULL); b = b->next)
1527 if (url_match(b->url, url_to_query))
1529 string_append(&matches, "<tr><td>{");
1530 string_join (&matches, actions_to_html(csp, b->action));
1531 string_append(&matches, " }<br>\n<code>");
1532 string_join (&matches, html_encode(b->url->spec));
1533 string_append(&matches, "</code></td></tr>\n");
1535 if (merge_current_action(action, b->action))
1538 free_http_request(url_to_query);
1539 free_current_action(action);
1541 return JB_ERR_MEMORY;
1549 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1552 string_append(&matches, "</table>\n");
1555 * XXX: Kludge to make sure the "Forward settings" section
1556 * shows what forward-override{} would do with the requested URL.
1557 * No one really cares how the CGI request would be forwarded
1558 * if it wasn't intercepted as CGI request in the first place.
1560 * From here on the action bitmask will no longer reflect
1561 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1562 * but luckily it's no longer required later on anyway.
1564 free_current_action(csp->action);
1565 get_url_actions(csp, url_to_query);
1568 * Fill in forwarding settings.
1570 * The possibilities are:
1572 * - http forwarding only
1573 * - socks4(a) forwarding only
1574 * - socks4(a) and http forwarding.
1576 * XXX: Parts of this code could be reused for the
1577 * "forwarding-failed" template which currently doesn't
1578 * display the proxy port and an eventual second forwarder.
1581 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1583 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1585 if (!err) err = map_block_killer(exports, "socks-forwarder");
1586 if (!err) err = map_block_killer(exports, "http-forwarder");
1590 char port[10]; /* We save proxy ports as int but need a string here */
1592 if (!err) err = map_block_killer(exports, "no-forwarder");
1594 if (fwd->gateway_host != NULL)
1596 char *socks_type = NULL;
1601 socks_type = "socks4";
1604 socks_type = "socks4a";
1607 socks_type = "socks5";
1610 socks_type = "socks5t";
1613 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1616 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1617 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1618 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1619 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1623 if (!err) err = map_block_killer(exports, "socks-forwarder");
1626 if (fwd->forward_host != NULL)
1628 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1629 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1630 if (!err) err = map(exports, "forward-port", 1, port, 1);
1634 if (!err) err = map_block_killer(exports, "http-forwarder");
1639 free_http_request(url_to_query);
1641 if (err || matches == NULL)
1643 free_current_action(action);
1645 return JB_ERR_MEMORY;
1648 #ifdef FEATURE_CGI_EDIT_ACTIONS
1649 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1651 err = map_block_killer(exports, "cgi-editor-is-disabled");
1653 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1656 * If zlib support is available, if no content filters
1657 * are enabled or if the prevent-compression action is enabled,
1658 * suppress the "compression could prevent filtering" warning.
1660 #ifndef FEATURE_ZLIB
1661 if (!content_filters_enabled(action) ||
1662 (action->flags & ACTION_NO_COMPRESSION))
1665 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1668 if (err || map(exports, "matches", 1, matches , 0))
1670 free_current_action(action);
1672 return JB_ERR_MEMORY;
1675 s = current_action_to_html(csp, action);
1677 free_current_action(action);
1679 if (map(exports, "final", 1, s, 0))
1682 return JB_ERR_MEMORY;
1686 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1690 /*********************************************************************
1692 * Function : cgi_robots_txt
1694 * Description : CGI function to return "/robots.txt".
1697 * 1 : csp = Current client state (buffers, headers, etc...)
1698 * 2 : rsp = http_response data structure for output
1699 * 3 : parameters = map of cgi parameters
1701 * CGI Parameters : None
1703 * Returns : JB_ERR_OK on success
1704 * JB_ERR_MEMORY on out-of-memory error.
1706 *********************************************************************/
1707 jb_err cgi_robots_txt(struct client_state *csp,
1708 struct http_response *rsp,
1709 const struct map *parameters)
1717 rsp->body = strdup_or_die(
1718 "# This is the Privoxy control interface.\n"
1719 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1726 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1730 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1731 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1733 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1737 /*********************************************************************
1739 * Function : show_defines
1741 * Description : Add to a map the state od all conditional #defines
1742 * used when building
1745 * 1 : exports = map to extend
1747 * Returns : JB_ERR_OK on success
1748 * JB_ERR_MEMORY on out-of-memory error.
1750 *********************************************************************/
1751 static jb_err show_defines(struct map *exports)
1753 jb_err err = JB_ERR_OK;
1756 const char name[31];
1757 const unsigned char is_available;
1760 static const struct feature features[] = {
1762 "FEATURE_64_BIT_TIME_T",
1763 #if (SIZEOF_TIME_T == 8)
1770 "FEATURE_ACCEPT_FILTER",
1771 #ifdef FEATURE_ACCEPT_FILTER
1786 "FEATURE_CGI_EDIT_ACTIONS",
1787 #ifdef FEATURE_CGI_EDIT_ACTIONS
1794 "FEATURE_CLIENT_TAGS",
1795 #ifdef FEATURE_CLIENT_TAGS
1802 "FEATURE_COMPRESSION",
1803 #ifdef FEATURE_COMPRESSION
1810 "FEATURE_CONNECTION_KEEP_ALIVE",
1811 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1818 "FEATURE_CONNECTION_SHARING",
1819 #ifdef FEATURE_CONNECTION_SHARING
1826 "FEATURE_FAST_REDIRECTS",
1827 #ifdef FEATURE_FAST_REDIRECTS
1834 "FEATURE_FORCE_LOAD",
1835 #ifdef FEATURE_FORCE_LOAD
1842 "FEATURE_GRACEFUL_TERMINATION",
1843 #ifdef FEATURE_GRACEFUL_TERMINATION
1850 "FEATURE_IMAGE_BLOCKING",
1851 #ifdef FEATURE_IMAGE_BLOCKING
1858 "FEATURE_IMAGE_DETECT_MSIE",
1859 #ifdef FEATURE_IMAGE_DETECT_MSIE
1866 "FEATURE_IPV6_SUPPORT",
1875 #ifdef FEATURE_NO_GIFS
1883 #ifdef FEATURE_PTHREAD
1890 "FEATURE_STATISTICS",
1891 #ifdef FEATURE_STATISTICS
1898 "FEATURE_STRPTIME_SANITY_CHECKS",
1899 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
1907 #ifdef FEATURE_TOGGLE
1915 #ifdef FEATURE_TRUST
1930 "FEATURE_DYNAMIC_PCRE",
1931 #ifdef FEATURE_DYNAMIC_PCRE
1939 for (i = 0; i < SZ(features); i++)
1941 err = map_conditional(exports, features[i].name, features[i].is_available);
1953 /*********************************************************************
1955 * Function : show_rcs
1957 * Description : Create a string with the rcs info for all sourcefiles
1961 * Returns : A string, or NULL on out-of-memory.
1963 *********************************************************************/
1964 static char *show_rcs(void)
1966 char *result = strdup_or_die("");
1967 char buf[BUFFER_SIZE];
1969 /* Instead of including *all* dot h's in the project (thus creating a
1970 * tremendous amount of dependencies), I will concede to declaring them
1971 * as extern's. This forces the developer to add to this list, but oh well.
1974 #define SHOW_RCS(__x) \
1976 extern const char __x[]; \
1977 snprintf(buf, sizeof(buf), " %s\n", __x); \
1978 string_append(&result, buf); \
1981 /* In alphabetical order */
1982 SHOW_RCS(actions_h_rcs)
1983 SHOW_RCS(actions_rcs)
1985 SHOW_RCS(amiga_h_rcs)
1987 #endif /* def AMIGA */
1990 #ifdef FEATURE_CGI_EDIT_ACTIONS
1991 SHOW_RCS(cgiedit_h_rcs)
1992 SHOW_RCS(cgiedit_rcs)
1993 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1994 SHOW_RCS(cgisimple_h_rcs)
1995 SHOW_RCS(cgisimple_rcs)
1996 SHOW_RCS(deanimate_h_rcs)
1997 SHOW_RCS(deanimate_rcs)
1998 SHOW_RCS(encode_h_rcs)
1999 SHOW_RCS(encode_rcs)
2000 SHOW_RCS(errlog_h_rcs)
2001 SHOW_RCS(errlog_rcs)
2002 SHOW_RCS(filters_h_rcs)
2003 SHOW_RCS(filters_rcs)
2004 SHOW_RCS(gateway_h_rcs)
2005 SHOW_RCS(gateway_rcs)
2006 SHOW_RCS(jbsockets_h_rcs)
2007 SHOW_RCS(jbsockets_rcs)
2010 SHOW_RCS(list_h_rcs)
2012 SHOW_RCS(loadcfg_h_rcs)
2013 SHOW_RCS(loadcfg_rcs)
2014 SHOW_RCS(loaders_h_rcs)
2015 SHOW_RCS(loaders_rcs)
2016 SHOW_RCS(miscutil_h_rcs)
2017 SHOW_RCS(miscutil_rcs)
2018 SHOW_RCS(parsers_h_rcs)
2019 SHOW_RCS(parsers_rcs)
2021 SHOW_RCS(pcrs_h_rcs)
2022 SHOW_RCS(project_h_rcs)
2023 SHOW_RCS(ssplit_h_rcs)
2024 SHOW_RCS(ssplit_rcs)
2025 SHOW_RCS(urlmatch_h_rcs)
2026 SHOW_RCS(urlmatch_rcs)
2028 #ifndef _WIN_CONSOLE
2029 SHOW_RCS(w32log_h_rcs)
2030 SHOW_RCS(w32log_rcs)
2031 SHOW_RCS(w32res_h_rcs)
2032 SHOW_RCS(w32taskbar_h_rcs)
2033 SHOW_RCS(w32taskbar_rcs)
2034 #endif /* ndef _WIN_CONSOLE */
2035 SHOW_RCS(win32_h_rcs)
2037 #endif /* def _WIN32 */
2046 /*********************************************************************
2048 * Function : cgi_show_file
2050 * Description : CGI function that shows the content of a
2051 * configuration file.
2054 * 1 : csp = Current client state (buffers, headers, etc...)
2055 * 2 : rsp = http_response data structure for output
2056 * 3 : parameters = map of cgi parameters
2059 * file : Which file to show. Only first letter is checked,
2064 * Default is to show menu and other information.
2066 * Returns : JB_ERR_OK on success
2067 * JB_ERR_MEMORY on out-of-memory error.
2069 *********************************************************************/
2070 static jb_err cgi_show_file(struct client_state *csp,
2071 struct http_response *rsp,
2072 const struct map *parameters)
2075 const char * filename = NULL;
2076 char * file_description = NULL;
2082 switch (*(lookup(parameters, "file")))
2085 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2087 filename = csp->actions_list[i]->filename;
2088 file_description = "Actions File";
2093 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2095 filename = csp->rlist[i]->filename;
2096 file_description = "Filter File";
2100 #ifdef FEATURE_TRUST
2104 filename = csp->tlist->filename;
2105 file_description = "Trust File";
2108 #endif /* def FEATURE_TRUST */
2111 if (NULL != filename)
2113 struct map *exports;
2118 exports = default_exports(csp, "show-status");
2119 if (NULL == exports)
2121 return JB_ERR_MEMORY;
2124 if (map(exports, "file-description", 1, file_description, 1)
2125 || map(exports, "filepath", 1, html_encode(filename), 0))
2128 return JB_ERR_MEMORY;
2131 err = load_file(filename, &s, &length);
2132 if (JB_ERR_OK != err)
2134 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2137 return JB_ERR_MEMORY;
2142 s = html_encode_and_free_original(s);
2146 return JB_ERR_MEMORY;
2149 if (map(exports, "contents", 1, s, 0))
2152 return JB_ERR_MEMORY;
2156 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2159 return JB_ERR_CGI_PARAMS;
2163 /*********************************************************************
2165 * Function : load_file
2167 * Description : Loads a file into a buffer.
2170 * 1 : filename = Name of the file to be loaded.
2171 * 2 : buffer = Used to return the file's content.
2172 * 3 : length = Used to return the size of the file.
2174 * Returns : JB_ERR_OK in case of success,
2175 * JB_ERR_FILE in case of ordinary file loading errors
2176 * (fseek() and ftell() errors are fatal)
2177 * JB_ERR_MEMORY in case of out-of-memory.
2179 *********************************************************************/
2180 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2184 jb_err err = JB_ERR_OK;
2186 fp = fopen(filename, "rb");
2189 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
2193 /* Get file length */
2194 if (fseek(fp, 0, SEEK_END))
2196 log_error(LOG_LEVEL_FATAL,
2197 "Unexpected error while fseek()ing to the end of %s: %E",
2203 log_error(LOG_LEVEL_FATAL,
2204 "Unexpected ftell() error while loading %s: %E",
2207 *length = (size_t)ret;
2209 /* Go back to the beginning. */
2210 if (fseek(fp, 0, SEEK_SET))
2212 log_error(LOG_LEVEL_FATAL,
2213 "Unexpected error while fseek()ing to the beginning of %s: %E",
2217 *buffer = zalloc_or_die(*length + 1);
2219 if (1 != fread(*buffer, *length, 1, fp))
2222 * May theoretically happen if the file size changes between
2223 * fseek() and fread() because it's edited in-place. Privoxy
2224 * and common text editors don't do that, thus we just fail.
2226 log_error(LOG_LEVEL_ERROR,
2227 "Couldn't completely read file %s.", filename);