1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.128 2014/06/03 10:29:23 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-2014 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"
64 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
66 static char *show_rcs(void);
67 static jb_err show_defines(struct map *exports);
68 static jb_err cgi_show_file(struct client_state *csp,
69 struct http_response *rsp,
70 const struct map *parameters);
71 static jb_err load_file(const char *filename, char **buffer, size_t *length);
73 /*********************************************************************
75 * Function : cgi_default
77 * Description : CGI function that is called for the CGI_SITE_1_HOST
78 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
79 * Boring - only exports the default exports.
82 * 1 : csp = Current client state (buffers, headers, etc...)
83 * 2 : rsp = http_response data structure for output
84 * 3 : parameters = map of cgi parameters
86 * CGI Parameters : none
88 * Returns : JB_ERR_OK on success
89 * JB_ERR_MEMORY on out-of-memory
91 *********************************************************************/
92 jb_err cgi_default(struct client_state *csp,
93 struct http_response *rsp,
94 const struct map *parameters)
103 if (NULL == (exports = default_exports(csp, "")))
105 return JB_ERR_MEMORY;
108 return template_fill_for_cgi(csp, "default", exports, rsp);
112 /*********************************************************************
114 * Function : cgi_error_404
116 * Description : CGI function that is called if an unknown action was
120 * 1 : csp = Current client state (buffers, headers, etc...)
121 * 2 : rsp = http_response data structure for output
122 * 3 : parameters = map of cgi parameters
124 * CGI Parameters : none
126 * Returns : JB_ERR_OK on success
127 * JB_ERR_MEMORY on out-of-memory error.
129 *********************************************************************/
130 jb_err cgi_error_404(struct client_state *csp,
131 struct http_response *rsp,
132 const struct map *parameters)
140 if (NULL == (exports = default_exports(csp, NULL)))
142 return JB_ERR_MEMORY;
145 rsp->status = strdup_or_die("404 Privoxy configuration page not found");
147 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
151 #ifdef FEATURE_GRACEFUL_TERMINATION
152 /*********************************************************************
156 * Description : CGI function to shut down Privoxy.
157 * NOTE: Turning this on in a production build
158 * would be a BAD idea. An EXTREMELY BAD idea.
159 * In short, don't do it.
162 * 1 : csp = Current client state (buffers, headers, etc...)
163 * 2 : rsp = http_response data structure for output
164 * 3 : parameters = map of cgi parameters
166 * CGI Parameters : none
168 * Returns : JB_ERR_OK on success
170 *********************************************************************/
171 jb_err cgi_die (struct client_state *csp,
172 struct http_response *rsp,
173 const struct map *parameters)
175 static const char status[] = "200 OK Privoxy shutdown request received";
176 static const char body[] =
179 " <title>Privoxy shutdown request received</title>\n"
180 " <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">\n"
181 " <link rel=\"stylesheet\" type=\"text/css\" href=\"http://config.privoxy.org/send-stylesheet\">\n"
184 "<h1>Privoxy shutdown request received</h1>\n"
185 "<p>Privoxy is going to shut down after the next request.</p>\n"
196 csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
198 rsp->content_length = 0;
199 rsp->head_length = 0;
202 rsp->body = strdup_or_die(body);
203 rsp->status = strdup_or_die(status);
207 #endif /* def FEATURE_GRACEFUL_TERMINATION */
210 /*********************************************************************
212 * Function : cgi_show_request
214 * Description : Show the client's request and what sed() would have
218 * 1 : csp = Current client state (buffers, headers, etc...)
219 * 2 : rsp = http_response data structure for output
220 * 3 : parameters = map of cgi parameters
222 * CGI Parameters : none
224 * Returns : JB_ERR_OK on success
225 * JB_ERR_MEMORY on out-of-memory error.
227 *********************************************************************/
228 jb_err cgi_show_request(struct client_state *csp,
229 struct http_response *rsp,
230 const struct map *parameters)
239 if (NULL == (exports = default_exports(csp, "show-request")))
241 return JB_ERR_MEMORY;
245 * Repair the damage done to the IOB by get_header()
247 for (p = csp->client_iob->buf; p < csp->client_iob->cur; p++)
249 if (*p == '\0') *p = '\n';
253 * Export the original client's request and the one we would
254 * be sending to the server if this wasn't a CGI call
257 if (map(exports, "client-request", 1, html_encode(csp->client_iob->buf), 0))
260 return JB_ERR_MEMORY;
263 if (map(exports, "processed-request", 1,
264 html_encode_and_free_original(list_to_text(csp->headers)), 0))
267 return JB_ERR_MEMORY;
270 return template_fill_for_cgi(csp, "show-request", exports, rsp);
274 /*********************************************************************
276 * Function : cgi_send_banner
278 * Description : CGI function that returns a banner.
281 * 1 : csp = Current client state (buffers, headers, etc...)
282 * 2 : rsp = http_response data structure for output
283 * 3 : parameters = map of cgi parameters
286 * type : Selects the type of banner between "trans", "logo",
287 * and "auto". Defaults to "logo" if absent or invalid.
288 * "auto" means to select as if we were image-blocking.
289 * (Only the first character really counts; b and t are
292 * Returns : JB_ERR_OK on success
293 * JB_ERR_MEMORY on out-of-memory error.
295 *********************************************************************/
296 jb_err cgi_send_banner(struct client_state *csp,
297 struct http_response *rsp,
298 const struct map *parameters)
300 char imagetype = lookup(parameters, "type")[0];
303 * If type is auto, then determine the right thing
304 * to do from the set-image-blocker action
306 if (imagetype == 'a')
313 #ifdef FEATURE_IMAGE_BLOCKING
314 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
316 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
317 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
318 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
322 /* Use default - nothing to do here. */
324 else if (0 == strcmpic(p, "blank"))
328 else if (0 == strcmpic(p, "pattern"))
334 * If the action is to call this CGI, determine
337 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
339 imagetype = p[sizeof(prefix1) - 1];
341 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
343 imagetype = p[sizeof(prefix2) - 1];
347 * Everything else must (should) be a URL to
355 #endif /* def FEATURE_IMAGE_BLOCKING */
359 * Now imagetype is either the non-auto type we were called with,
360 * or it was auto and has since been determined. In any case, we
361 * can proceed to actually answering the request by sending a redirect
362 * or an image as appropriate:
364 if (imagetype == 'r')
366 rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
367 if (enlist_unique_header(rsp->headers, "Location",
368 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
370 return JB_ERR_MEMORY;
375 if ((imagetype == 'b') || (imagetype == 't'))
377 rsp->body = bindup(image_blank_data, image_blank_length);
378 rsp->content_length = image_blank_length;
382 rsp->body = bindup(image_pattern_data, image_pattern_length);
383 rsp->content_length = image_pattern_length;
386 if (rsp->body == NULL)
388 return JB_ERR_MEMORY;
390 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
392 return JB_ERR_MEMORY;
403 /*********************************************************************
405 * Function : cgi_transparent_image
407 * Description : CGI function that sends a 1x1 transparent image.
410 * 1 : csp = Current client state (buffers, headers, etc...)
411 * 2 : rsp = http_response data structure for output
412 * 3 : parameters = map of cgi parameters
414 * CGI Parameters : None
416 * Returns : JB_ERR_OK on success
417 * JB_ERR_MEMORY on out-of-memory error.
419 *********************************************************************/
420 jb_err cgi_transparent_image(struct client_state *csp,
421 struct http_response *rsp,
422 const struct map *parameters)
427 rsp->body = bindup(image_blank_data, image_blank_length);
428 rsp->content_length = image_blank_length;
430 if (rsp->body == NULL)
432 return JB_ERR_MEMORY;
435 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
437 return JB_ERR_MEMORY;
447 /*********************************************************************
449 * Function : cgi_send_default_favicon
451 * Description : CGI function that sends the standard favicon.
454 * 1 : csp = Current client state (buffers, headers, etc...)
455 * 2 : rsp = http_response data structure for output
456 * 3 : parameters = map of cgi parameters
458 * CGI Parameters : None
460 * Returns : JB_ERR_OK on success
461 * JB_ERR_MEMORY on out-of-memory error.
463 *********************************************************************/
464 jb_err cgi_send_default_favicon(struct client_state *csp,
465 struct http_response *rsp,
466 const struct map *parameters)
468 static const char default_favicon_data[] =
469 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
470 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
471 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
472 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
473 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
474 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
475 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
476 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
477 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
478 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
479 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
480 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
481 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
483 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
488 rsp->body = bindup(default_favicon_data, favicon_length);
489 rsp->content_length = favicon_length;
491 if (rsp->body == NULL)
493 return JB_ERR_MEMORY;
496 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
498 return JB_ERR_MEMORY;
508 /*********************************************************************
510 * Function : cgi_send_error_favicon
512 * Description : CGI function that sends the favicon for error pages.
515 * 1 : csp = Current client state (buffers, headers, etc...)
516 * 2 : rsp = http_response data structure for output
517 * 3 : parameters = map of cgi parameters
519 * CGI Parameters : None
521 * Returns : JB_ERR_OK on success
522 * JB_ERR_MEMORY on out-of-memory error.
524 *********************************************************************/
525 jb_err cgi_send_error_favicon(struct client_state *csp,
526 struct http_response *rsp,
527 const struct map *parameters)
529 static const char error_favicon_data[] =
530 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
531 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
532 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
533 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
534 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
535 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
536 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
537 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
538 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
539 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
540 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
541 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
542 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
544 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
549 rsp->body = bindup(error_favicon_data, favicon_length);
550 rsp->content_length = favicon_length;
552 if (rsp->body == NULL)
554 return JB_ERR_MEMORY;
557 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
559 return JB_ERR_MEMORY;
569 /*********************************************************************
571 * Function : cgi_send_stylesheet
573 * Description : CGI function that sends a css stylesheet found
574 * in the cgi-style.css template
577 * 1 : csp = Current client state (buffers, headers, etc...)
578 * 2 : rsp = http_response data structure for output
579 * 3 : parameters = map of cgi parameters
581 * CGI Parameters : None
583 * Returns : JB_ERR_OK on success
584 * JB_ERR_MEMORY on out-of-memory error.
586 *********************************************************************/
587 jb_err cgi_send_stylesheet(struct client_state *csp,
588 struct http_response *rsp,
589 const struct map *parameters)
598 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
600 if (err == JB_ERR_FILE)
603 * No way to tell user; send empty stylesheet
605 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
609 return err; /* JB_ERR_MEMORY */
612 if (enlist(rsp->headers, "Content-Type: text/css"))
614 return JB_ERR_MEMORY;
622 /*********************************************************************
624 * Function : cgi_send_url_info_osd
626 * Description : CGI function that sends the OpenSearch Description
627 * template for the show-url-info page. It allows to
628 * access the page through "search engine plugins".
631 * 1 : csp = Current client state (buffers, headers, etc...)
632 * 2 : rsp = http_response data structure for output
633 * 3 : parameters = map of cgi parameters
635 * CGI Parameters : None
637 * Returns : JB_ERR_OK on success
638 * JB_ERR_MEMORY on out-of-memory error.
640 *********************************************************************/
641 jb_err cgi_send_url_info_osd(struct client_state *csp,
642 struct http_response *rsp,
643 const struct map *parameters)
645 jb_err err = JB_ERR_MEMORY;
646 struct map *exports = default_exports(csp, NULL);
653 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
654 if (JB_ERR_OK == err)
656 err = enlist(rsp->headers,
657 "Content-Type: application/opensearchdescription+xml");
666 /*********************************************************************
668 * Function : get_content_type
670 * Description : Use the file extension to guess the content type
671 * header we should use to serve the file.
674 * 1 : filename = Name of the file whose content type
677 * Returns : The guessed content type.
679 *********************************************************************/
680 static const char *get_content_type(const char *filename)
685 const char extension[6];
686 const char content_type[11];
688 static const struct content_type content_types[] =
690 {".css", "text/css"},
691 {".jpg", "image/jpeg"},
692 {".jpeg", "image/jpeg"},
693 {".png", "image/png"},
696 for (i = 0; i < SZ(content_types); i++)
698 if (strstr(filename, content_types[i].extension))
700 return content_types[i].content_type;
704 /* No match by extension, default to html */
708 /*********************************************************************
710 * Function : cgi_send_user_manual
712 * Description : CGI function that sends a file in the user
716 * 1 : csp = Current client state (buffers, headers, etc...)
717 * 2 : rsp = http_response data structure for output
718 * 3 : parameters = map of cgi parameters
720 * CGI Parameters : file=name.html, the name of the HTML file
721 * (relative to user-manual from config)
723 * Returns : JB_ERR_OK on success
724 * JB_ERR_MEMORY on out-of-memory error.
726 *********************************************************************/
727 jb_err cgi_send_user_manual(struct client_state *csp,
728 struct http_response *rsp,
729 const struct map *parameters)
731 const char *filename;
733 jb_err err = JB_ERR_OK;
734 const char *content_type;
740 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
742 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
743 "received while user-manual delivery is disabled.");
744 return cgi_error_404(csp, rsp, parameters);
747 if (!parameters->first)
749 /* requested http://p.p/user-manual (without trailing slash) */
750 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
753 get_string_param(parameters, "file", &filename);
754 if (filename == NULL)
756 /* It's '/' so serve the index.html if there is one. */
757 filename = "index.html";
759 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
762 * We currently only support a flat file
763 * hierarchy for the documentation.
765 log_error(LOG_LEVEL_ERROR,
766 "Rejecting the request to serve '%s' as it contains '/' or '..'",
768 return JB_ERR_CGI_PARAMS;
771 full_path = make_path(csp->config->usermanual, filename);
772 if (full_path == NULL)
774 return JB_ERR_MEMORY;
777 err = load_file(full_path, &rsp->body, &rsp->content_length);
778 if (JB_ERR_OK != err)
780 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
781 if (JB_ERR_FILE == err)
783 err = cgi_error_no_template(csp, rsp, full_path);
790 content_type = get_content_type(filename);
791 log_error(LOG_LEVEL_CGI,
792 "Content-Type guessed for %s: %s", filename, content_type);
794 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
799 /*********************************************************************
801 * Function : cgi_show_version
803 * Description : CGI function that returns a a web page describing the
804 * file versions of Privoxy.
807 * 1 : csp = Current client state (buffers, headers, etc...)
808 * 2 : rsp = http_response data structure for output
809 * 3 : parameters = map of cgi parameters
811 * CGI Parameters : none
813 * Returns : JB_ERR_OK on success
814 * JB_ERR_MEMORY on out-of-memory error.
816 *********************************************************************/
817 jb_err cgi_show_version(struct client_state *csp,
818 struct http_response *rsp,
819 const struct map *parameters)
827 if (NULL == (exports = default_exports(csp, "show-version")))
829 return JB_ERR_MEMORY;
832 if (map(exports, "sourceversions", 1, show_rcs(), 0))
835 return JB_ERR_MEMORY;
838 return template_fill_for_cgi(csp, "show-version", exports, rsp);
842 /*********************************************************************
844 * Function : cgi_show_status
846 * Description : CGI function that returns a web page describing the
847 * current status of Privoxy.
850 * 1 : csp = Current client state (buffers, headers, etc...)
851 * 2 : rsp = http_response data structure for output
852 * 3 : parameters = map of cgi parameters
855 * file : Which file to show. Only first letter is checked,
860 * Default is to show menu and other information.
862 * Returns : JB_ERR_OK on success
863 * JB_ERR_MEMORY on out-of-memory error.
865 *********************************************************************/
866 jb_err cgi_show_status(struct client_state *csp,
867 struct http_response *rsp,
868 const struct map *parameters)
874 char buf[BUFFER_SIZE];
875 #ifdef FEATURE_STATISTICS
876 float perc_rej; /* Percentage of http requests rejected */
878 int local_urls_rejected;
879 #endif /* ndef FEATURE_STATISTICS */
880 jb_err err = JB_ERR_OK;
888 if ('\0' != *(lookup(parameters, "file")))
890 return cgi_show_file(csp, rsp, parameters);
893 if (NULL == (exports = default_exports(csp, "show-status")))
895 return JB_ERR_MEMORY;
899 for (j = 0; (s != NULL) && (j < Argc); j++)
901 if (!err) err = string_join (&s, html_encode(Argv[j]));
902 if (!err) err = string_append(&s, " ");
904 if (!err) err = map(exports, "invocation", 1, s, 0);
906 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
907 if (!err) err = show_defines(exports);
912 return JB_ERR_MEMORY;
915 #ifdef FEATURE_STATISTICS
916 local_urls_read = urls_read;
917 local_urls_rejected = urls_rejected;
920 * Need to alter the stats not to include the fetch of this
923 * Can't do following thread safely! doh!
926 * urls_rejected--; * This will be incremented subsequently *
929 if (local_urls_read == 0)
931 if (!err) err = map_block_killer(exports, "have-stats");
935 if (!err) err = map_block_killer(exports, "have-no-stats");
937 perc_rej = (float)local_urls_rejected * 100.0F /
938 (float)local_urls_read;
940 snprintf(buf, sizeof(buf), "%d", local_urls_read);
941 if (!err) err = map(exports, "requests-received", 1, buf, 1);
943 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
944 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
946 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
947 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
950 #else /* ndef FEATURE_STATISTICS */
951 if (!err) err = map_block_killer(exports, "statistics");
952 #endif /* ndef FEATURE_STATISTICS */
955 * List all action files in use, together with view and edit links,
956 * except for standard.action, which should only be viewable. (Not
957 * enforced in the editor itself)
958 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
961 for (i = 0; i < MAX_AF_FILES; i++)
963 if (csp->actions_list[i] != NULL)
965 if (!err) err = string_append(&s, "<tr><td>");
966 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
967 snprintf(buf, sizeof(buf),
968 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
969 if (!err) err = string_append(&s, buf);
971 #ifdef FEATURE_CGI_EDIT_ACTIONS
972 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
973 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
974 && (NULL != csp->config->actions_file_short[i]))
977 if (access(csp->config->actions_file[i], W_OK) == 0)
979 #endif /* def HAVE_ACCESS */
980 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
981 if (!err) err = string_append(&s, buf);
986 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
988 #endif /* def HAVE_ACCESS */
992 if (!err) err = string_append(&s, "</td></tr>\n");
997 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1001 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1005 * List all re_filterfiles in use, together with view options.
1006 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1009 for (i = 0; i < MAX_AF_FILES; i++)
1011 if (csp->rlist[i] != NULL)
1013 if (!err) err = string_append(&s, "<tr><td>");
1014 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1015 snprintf(buf, sizeof(buf),
1016 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1017 if (!err) err = string_append(&s, buf);
1018 if (!err) err = string_append(&s, "</td></tr>\n");
1023 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1027 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1028 if (!err) err = map_block_killer(exports, "have-filterfile");
1031 #ifdef FEATURE_TRUST
1034 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1038 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1039 if (!err) err = map_block_killer(exports, "have-trustfile");
1042 if (!err) err = map_block_killer(exports, "trust-support");
1043 #endif /* ndef FEATURE_TRUST */
1045 #ifdef FEATURE_CGI_EDIT_ACTIONS
1046 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1048 err = map_block_killer(exports, "cgi-editor-is-disabled");
1050 #endif /* ndef CGI_EDIT_ACTIONS */
1052 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1057 return JB_ERR_MEMORY;
1060 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1064 /*********************************************************************
1066 * Function : cgi_show_url_info
1068 * Description : CGI function that determines and shows which actions
1069 * Privoxy will perform for a given url, and which
1070 * matches starting from the defaults have lead to that.
1073 * 1 : csp = Current client state (buffers, headers, etc...)
1074 * 2 : rsp = http_response data structure for output
1075 * 3 : parameters = map of cgi parameters
1078 * url : The url whose actions are to be determined.
1079 * If url is unset, the url-given conditional will be
1080 * set, so that all but the form can be suppressed in
1083 * Returns : JB_ERR_OK on success
1084 * JB_ERR_MEMORY on out-of-memory error.
1086 *********************************************************************/
1087 jb_err cgi_show_url_info(struct client_state *csp,
1088 struct http_response *rsp,
1089 const struct map *parameters)
1092 struct map *exports;
1099 if (NULL == (exports = default_exports(csp, "show-url-info")))
1101 return JB_ERR_MEMORY;
1105 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1107 url_param = strdup_or_die(lookup(parameters, "url"));
1111 * Handle prefixes. 4 possibilities:
1112 * 1) "http://" or "https://" prefix present and followed by URL - OK
1113 * 2) Only the "http://" or "https://" part is present, no URL - change
1114 * to empty string so it will be detected later as "no URL".
1115 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1117 * 4) Parameter not specified or is empty string - let this fall through
1118 * for now, next block of code will handle it.
1120 if (0 == strncmp(url_param, "http://", 7))
1122 if (url_param[7] == '\0')
1125 * Empty URL (just prefix).
1126 * Make it totally empty so it's caught by the next if ()
1128 url_param[0] = '\0';
1131 else if (0 == strncmp(url_param, "https://", 8))
1133 if (url_param[8] == '\0')
1136 * Empty URL (just prefix).
1137 * Make it totally empty so it's caught by the next if ()
1139 url_param[0] = '\0';
1142 else if ((url_param[0] != '\0')
1143 && ((NULL == strstr(url_param, "://")
1144 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1147 * No prefix or at least no prefix before
1148 * the first slash - assume http://
1150 char *url_param_prefixed = strdup_or_die("http://");
1152 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1155 return JB_ERR_MEMORY;
1157 url_param = url_param_prefixed;
1161 * Hide "toggle off" warning if Privoxy is toggled on.
1164 #ifdef FEATURE_TOGGLE
1165 (global_toggle_state == 1) &&
1166 #endif /* def FEATURE_TOGGLE */
1167 map_block_killer(exports, "privoxy-is-toggled-off")
1172 return JB_ERR_MEMORY;
1175 if (url_param[0] == '\0')
1177 /* URL paramater not specified, display query form only. */
1179 if (map_block_killer(exports, "url-given")
1180 || map(exports, "url", 1, "", 1))
1183 return JB_ERR_MEMORY;
1188 /* Given a URL, so query it. */
1193 struct file_list *fl;
1194 struct url_actions *b;
1195 struct http_request url_to_query[1];
1196 struct current_action_spec action[1];
1199 if (map(exports, "url", 1, html_encode(url_param), 0))
1203 return JB_ERR_MEMORY;
1206 init_current_action(action);
1208 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1210 free_current_action(action);
1213 return JB_ERR_MEMORY;
1216 memset(url_to_query, '\0', sizeof(url_to_query));
1217 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1218 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1222 if (err == JB_ERR_MEMORY)
1224 free_http_request(url_to_query);
1225 free_current_action(action);
1227 return JB_ERR_MEMORY;
1233 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1234 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1235 if (!err) err = map_block_killer(exports, "valid-url");
1237 free_current_action(action);
1238 free_http_request(url_to_query);
1243 return JB_ERR_MEMORY;
1246 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1250 * We have a warning about SSL paths. Hide it for unencrypted sites.
1252 if (!url_to_query->ssl)
1254 if (map_block_killer(exports, "https"))
1256 free_current_action(action);
1258 free_http_request(url_to_query);
1259 return JB_ERR_MEMORY;
1263 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1265 for (i = 0; i < MAX_AF_FILES; i++)
1267 if (NULL == csp->config->actions_file_short[i]
1268 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1272 if ((fl = csp->actions_list[i]) != NULL)
1274 if ((b = fl->f) != NULL)
1276 /* FIXME: Hardcoded HTML! */
1277 string_append(&matches, "<tr><th>In file: ");
1278 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1279 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1280 string_append(&matches, buf);
1281 string_append(&matches, "View</a>");
1282 #ifdef FEATURE_CGI_EDIT_ACTIONS
1283 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1286 if (access(csp->config->actions_file[i], W_OK) == 0)
1288 #endif /* def HAVE_ACCESS */
1289 snprintf(buf, sizeof(buf),
1290 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1291 string_append(&matches, buf);
1292 string_append(&matches, "Edit</a>");
1297 string_append(&matches, " <strong>No write access.</strong>");
1299 #endif /* def HAVE_ACCESS */
1301 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1303 string_append(&matches, "</th></tr>\n");
1310 for (; (b != NULL) && (matches != NULL); b = b->next)
1312 if (url_match(b->url, url_to_query))
1314 string_append(&matches, "<tr><td>{");
1315 string_join (&matches, actions_to_html(csp, b->action));
1316 string_append(&matches, " }<br>\n<code>");
1317 string_join (&matches, html_encode(b->url->spec));
1318 string_append(&matches, "</code></td></tr>\n");
1320 if (merge_current_action(action, b->action))
1323 free_http_request(url_to_query);
1324 free_current_action(action);
1326 return JB_ERR_MEMORY;
1334 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1337 string_append(&matches, "</table>\n");
1340 * XXX: Kludge to make sure the "Forward settings" section
1341 * shows what forward-override{} would do with the requested URL.
1342 * No one really cares how the CGI request would be forwarded
1343 * if it wasn't intercepted as CGI request in the first place.
1345 * From here on the action bitmask will no longer reflect
1346 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1347 * but luckily it's no longer required later on anyway.
1349 free_current_action(csp->action);
1350 get_url_actions(csp, url_to_query);
1353 * Fill in forwarding settings.
1355 * The possibilities are:
1357 * - http forwarding only
1358 * - socks4(a) forwarding only
1359 * - socks4(a) and http forwarding.
1361 * XXX: Parts of this code could be reused for the
1362 * "forwarding-failed" template which currently doesn't
1363 * display the proxy port and an eventual second forwarder.
1366 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1368 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1370 if (!err) err = map_block_killer(exports, "socks-forwarder");
1371 if (!err) err = map_block_killer(exports, "http-forwarder");
1375 char port[10]; /* We save proxy ports as int but need a string here */
1377 if (!err) err = map_block_killer(exports, "no-forwarder");
1379 if (fwd->gateway_host != NULL)
1381 char *socks_type = NULL;
1386 socks_type = "socks4";
1389 socks_type = "socks4a";
1392 socks_type = "socks5";
1395 socks_type = "socks5t";
1398 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1401 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1402 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1403 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1404 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1408 if (!err) err = map_block_killer(exports, "socks-forwarder");
1411 if (fwd->forward_host != NULL)
1413 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1414 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1415 if (!err) err = map(exports, "forward-port", 1, port, 1);
1419 if (!err) err = map_block_killer(exports, "http-forwarder");
1424 free_http_request(url_to_query);
1426 if (err || matches == NULL)
1428 free_current_action(action);
1430 return JB_ERR_MEMORY;
1433 #ifdef FEATURE_CGI_EDIT_ACTIONS
1434 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1436 err = map_block_killer(exports, "cgi-editor-is-disabled");
1438 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1441 * If zlib support is available, if no content filters
1442 * are enabled or if the prevent-compression action is enabled,
1443 * suppress the "compression could prevent filtering" warning.
1445 #ifndef FEATURE_ZLIB
1446 if (!content_filters_enabled(action) ||
1447 (action->flags & ACTION_NO_COMPRESSION))
1450 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1453 if (err || map(exports, "matches", 1, matches , 0))
1455 free_current_action(action);
1457 return JB_ERR_MEMORY;
1460 s = current_action_to_html(csp, action);
1462 free_current_action(action);
1464 if (map(exports, "final", 1, s, 0))
1467 return JB_ERR_MEMORY;
1471 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1475 /*********************************************************************
1477 * Function : cgi_robots_txt
1479 * Description : CGI function to return "/robots.txt".
1482 * 1 : csp = Current client state (buffers, headers, etc...)
1483 * 2 : rsp = http_response data structure for output
1484 * 3 : parameters = map of cgi parameters
1486 * CGI Parameters : None
1488 * Returns : JB_ERR_OK on success
1489 * JB_ERR_MEMORY on out-of-memory error.
1491 *********************************************************************/
1492 jb_err cgi_robots_txt(struct client_state *csp,
1493 struct http_response *rsp,
1494 const struct map *parameters)
1502 rsp->body = strdup_or_die(
1503 "# This is the Privoxy control interface.\n"
1504 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1511 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1515 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1516 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1518 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1522 /*********************************************************************
1524 * Function : show_defines
1526 * Description : Add to a map the state od all conditional #defines
1527 * used when building
1530 * 1 : exports = map to extend
1532 * Returns : JB_ERR_OK on success
1533 * JB_ERR_MEMORY on out-of-memory error.
1535 *********************************************************************/
1536 static jb_err show_defines(struct map *exports)
1538 jb_err err = JB_ERR_OK;
1541 const char name[31];
1542 const unsigned char is_available;
1545 static const struct feature features[] = {
1547 "FEATURE_ACCEPT_FILTER",
1548 #ifdef FEATURE_ACCEPT_FILTER
1563 "FEATURE_CGI_EDIT_ACTIONS",
1564 #ifdef FEATURE_CGI_EDIT_ACTIONS
1571 "FEATURE_COMPRESSION",
1572 #ifdef FEATURE_COMPRESSION
1579 "FEATURE_CONNECTION_KEEP_ALIVE",
1580 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1587 "FEATURE_CONNECTION_SHARING",
1588 #ifdef FEATURE_CONNECTION_SHARING
1595 "FEATURE_FAST_REDIRECTS",
1596 #ifdef FEATURE_FAST_REDIRECTS
1603 "FEATURE_FORCE_LOAD",
1604 #ifdef FEATURE_FORCE_LOAD
1611 "FEATURE_GRACEFUL_TERMINATION",
1612 #ifdef FEATURE_GRACEFUL_TERMINATION
1619 "FEATURE_IMAGE_BLOCKING",
1620 #ifdef FEATURE_IMAGE_BLOCKING
1627 "FEATURE_IMAGE_DETECT_MSIE",
1628 #ifdef FEATURE_IMAGE_DETECT_MSIE
1635 "FEATURE_IPV6_SUPPORT",
1644 #ifdef FEATURE_NO_GIFS
1652 #ifdef FEATURE_PTHREAD
1659 "FEATURE_STATISTICS",
1660 #ifdef FEATURE_STATISTICS
1667 "FEATURE_STRPTIME_SANITY_CHECKS",
1668 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
1676 #ifdef FEATURE_TOGGLE
1684 #ifdef FEATURE_TRUST
1699 "FEATURE_DYNAMIC_PCRE",
1700 #ifdef FEATURE_DYNAMIC_PCRE
1708 for (i = 0; i < SZ(features); i++)
1710 err = map_conditional(exports, features[i].name, features[i].is_available);
1722 /*********************************************************************
1724 * Function : show_rcs
1726 * Description : Create a string with the rcs info for all sourcefiles
1730 * Returns : A string, or NULL on out-of-memory.
1732 *********************************************************************/
1733 static char *show_rcs(void)
1735 char *result = strdup_or_die("");
1736 char buf[BUFFER_SIZE];
1738 /* Instead of including *all* dot h's in the project (thus creating a
1739 * tremendous amount of dependencies), I will concede to declaring them
1740 * as extern's. This forces the developer to add to this list, but oh well.
1743 #define SHOW_RCS(__x) \
1745 extern const char __x[]; \
1746 snprintf(buf, sizeof(buf), " %s\n", __x); \
1747 string_append(&result, buf); \
1750 /* In alphabetical order */
1751 SHOW_RCS(actions_h_rcs)
1752 SHOW_RCS(actions_rcs)
1754 SHOW_RCS(amiga_h_rcs)
1756 #endif /* def AMIGA */
1759 #ifdef FEATURE_CGI_EDIT_ACTIONS
1760 SHOW_RCS(cgiedit_h_rcs)
1761 SHOW_RCS(cgiedit_rcs)
1762 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1763 SHOW_RCS(cgisimple_h_rcs)
1764 SHOW_RCS(cgisimple_rcs)
1766 SHOW_RCS(cygwin_h_rcs)
1768 SHOW_RCS(deanimate_h_rcs)
1769 SHOW_RCS(deanimate_rcs)
1770 SHOW_RCS(encode_h_rcs)
1771 SHOW_RCS(encode_rcs)
1772 SHOW_RCS(errlog_h_rcs)
1773 SHOW_RCS(errlog_rcs)
1774 SHOW_RCS(filters_h_rcs)
1775 SHOW_RCS(filters_rcs)
1776 SHOW_RCS(gateway_h_rcs)
1777 SHOW_RCS(gateway_rcs)
1778 SHOW_RCS(jbsockets_h_rcs)
1779 SHOW_RCS(jbsockets_rcs)
1782 SHOW_RCS(list_h_rcs)
1784 SHOW_RCS(loadcfg_h_rcs)
1785 SHOW_RCS(loadcfg_rcs)
1786 SHOW_RCS(loaders_h_rcs)
1787 SHOW_RCS(loaders_rcs)
1788 SHOW_RCS(miscutil_h_rcs)
1789 SHOW_RCS(miscutil_rcs)
1790 SHOW_RCS(parsers_h_rcs)
1791 SHOW_RCS(parsers_rcs)
1793 SHOW_RCS(pcrs_h_rcs)
1794 SHOW_RCS(project_h_rcs)
1795 SHOW_RCS(ssplit_h_rcs)
1796 SHOW_RCS(ssplit_rcs)
1797 SHOW_RCS(urlmatch_h_rcs)
1798 SHOW_RCS(urlmatch_rcs)
1800 #ifndef _WIN_CONSOLE
1801 SHOW_RCS(w32log_h_rcs)
1802 SHOW_RCS(w32log_rcs)
1803 SHOW_RCS(w32res_h_rcs)
1804 SHOW_RCS(w32taskbar_h_rcs)
1805 SHOW_RCS(w32taskbar_rcs)
1806 #endif /* ndef _WIN_CONSOLE */
1807 SHOW_RCS(win32_h_rcs)
1809 #endif /* def _WIN32 */
1818 /*********************************************************************
1820 * Function : cgi_show_file
1822 * Description : CGI function that shows the content of a
1823 * configuration file.
1826 * 1 : csp = Current client state (buffers, headers, etc...)
1827 * 2 : rsp = http_response data structure for output
1828 * 3 : parameters = map of cgi parameters
1831 * file : Which file to show. Only first letter is checked,
1836 * Default is to show menu and other information.
1838 * Returns : JB_ERR_OK on success
1839 * JB_ERR_MEMORY on out-of-memory error.
1841 *********************************************************************/
1842 static jb_err cgi_show_file(struct client_state *csp,
1843 struct http_response *rsp,
1844 const struct map *parameters)
1847 const char * filename = NULL;
1848 char * file_description = NULL;
1854 switch (*(lookup(parameters, "file")))
1857 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1859 filename = csp->actions_list[i]->filename;
1860 file_description = "Actions File";
1865 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1867 filename = csp->rlist[i]->filename;
1868 file_description = "Filter File";
1872 #ifdef FEATURE_TRUST
1876 filename = csp->tlist->filename;
1877 file_description = "Trust File";
1880 #endif /* def FEATURE_TRUST */
1883 if (NULL != filename)
1885 struct map *exports;
1890 exports = default_exports(csp, "show-status");
1891 if (NULL == exports)
1893 return JB_ERR_MEMORY;
1896 if (map(exports, "file-description", 1, file_description, 1)
1897 || map(exports, "filepath", 1, html_encode(filename), 0))
1900 return JB_ERR_MEMORY;
1903 err = load_file(filename, &s, &length);
1904 if (JB_ERR_OK != err)
1906 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1909 return JB_ERR_MEMORY;
1914 s = html_encode_and_free_original(s);
1917 return JB_ERR_MEMORY;
1920 if (map(exports, "contents", 1, s, 0))
1923 return JB_ERR_MEMORY;
1927 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1930 return JB_ERR_CGI_PARAMS;
1934 /*********************************************************************
1936 * Function : load_file
1938 * Description : Loads a file into a buffer.
1941 * 1 : filename = Name of the file to be loaded.
1942 * 2 : buffer = Used to return the file's content.
1943 * 3 : length = Used to return the size of the file.
1945 * Returns : JB_ERR_OK in case of success,
1946 * JB_ERR_FILE in case of ordinary file loading errors
1947 * (fseek() and ftell() errors are fatal)
1948 * JB_ERR_MEMORY in case of out-of-memory.
1950 *********************************************************************/
1951 static jb_err load_file(const char *filename, char **buffer, size_t *length)
1955 jb_err err = JB_ERR_OK;
1957 fp = fopen(filename, "rb");
1960 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
1964 /* Get file length */
1965 if (fseek(fp, 0, SEEK_END))
1967 log_error(LOG_LEVEL_FATAL,
1968 "Unexpected error while fseek()ing to the end of %s: %E",
1974 log_error(LOG_LEVEL_FATAL,
1975 "Unexpected ftell() error while loading %s: %E",
1978 *length = (size_t)ret;
1980 /* Go back to the beginning. */
1981 if (fseek(fp, 0, SEEK_SET))
1983 log_error(LOG_LEVEL_FATAL,
1984 "Unexpected error while fseek()ing to the beginning of %s: %E",
1988 *buffer = (char *)zalloc(*length + 1);
1989 if (NULL == *buffer)
1991 err = JB_ERR_MEMORY;
1993 else if (!fread(*buffer, *length, 1, fp))
1996 * May happen if the file size changes between fseek() and
1997 * fread(). If it does, we just log it and serve what we got.
1999 log_error(LOG_LEVEL_ERROR,
2000 "Couldn't completely read file %s.", filename);