1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.131 2014/10/18 11:28:49 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 != csp->config->actions_file_short[i]))
976 if (access(csp->config->actions_file[i], W_OK) == 0)
978 #endif /* def HAVE_ACCESS */
979 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
980 if (!err) err = string_append(&s, buf);
985 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
987 #endif /* def HAVE_ACCESS */
991 if (!err) err = string_append(&s, "</td></tr>\n");
996 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1000 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1004 * List all re_filterfiles in use, together with view options.
1005 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1008 for (i = 0; i < MAX_AF_FILES; i++)
1010 if (csp->rlist[i] != NULL)
1012 if (!err) err = string_append(&s, "<tr><td>");
1013 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1014 snprintf(buf, sizeof(buf),
1015 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1016 if (!err) err = string_append(&s, buf);
1017 if (!err) err = string_append(&s, "</td></tr>\n");
1022 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1026 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1027 if (!err) err = map_block_killer(exports, "have-filterfile");
1030 #ifdef FEATURE_TRUST
1033 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1037 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1038 if (!err) err = map_block_killer(exports, "have-trustfile");
1041 if (!err) err = map_block_killer(exports, "trust-support");
1042 #endif /* ndef FEATURE_TRUST */
1044 #ifdef FEATURE_CGI_EDIT_ACTIONS
1045 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1047 err = map_block_killer(exports, "cgi-editor-is-disabled");
1049 #endif /* ndef CGI_EDIT_ACTIONS */
1051 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1056 return JB_ERR_MEMORY;
1059 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1063 /*********************************************************************
1065 * Function : cgi_show_url_info
1067 * Description : CGI function that determines and shows which actions
1068 * Privoxy will perform for a given url, and which
1069 * matches starting from the defaults have lead to that.
1072 * 1 : csp = Current client state (buffers, headers, etc...)
1073 * 2 : rsp = http_response data structure for output
1074 * 3 : parameters = map of cgi parameters
1077 * url : The url whose actions are to be determined.
1078 * If url is unset, the url-given conditional will be
1079 * set, so that all but the form can be suppressed in
1082 * Returns : JB_ERR_OK on success
1083 * JB_ERR_MEMORY on out-of-memory error.
1085 *********************************************************************/
1086 jb_err cgi_show_url_info(struct client_state *csp,
1087 struct http_response *rsp,
1088 const struct map *parameters)
1091 struct map *exports;
1098 if (NULL == (exports = default_exports(csp, "show-url-info")))
1100 return JB_ERR_MEMORY;
1104 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1106 url_param = strdup_or_die(lookup(parameters, "url"));
1110 * Handle prefixes. 4 possibilities:
1111 * 1) "http://" or "https://" prefix present and followed by URL - OK
1112 * 2) Only the "http://" or "https://" part is present, no URL - change
1113 * to empty string so it will be detected later as "no URL".
1114 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1116 * 4) Parameter not specified or is empty string - let this fall through
1117 * for now, next block of code will handle it.
1119 if (0 == strncmp(url_param, "http://", 7))
1121 if (url_param[7] == '\0')
1124 * Empty URL (just prefix).
1125 * Make it totally empty so it's caught by the next if ()
1127 url_param[0] = '\0';
1130 else if (0 == strncmp(url_param, "https://", 8))
1132 if (url_param[8] == '\0')
1135 * Empty URL (just prefix).
1136 * Make it totally empty so it's caught by the next if ()
1138 url_param[0] = '\0';
1141 else if ((url_param[0] != '\0')
1142 && ((NULL == strstr(url_param, "://")
1143 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1146 * No prefix or at least no prefix before
1147 * the first slash - assume http://
1149 char *url_param_prefixed = strdup_or_die("http://");
1151 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1154 return JB_ERR_MEMORY;
1156 url_param = url_param_prefixed;
1160 * Hide "toggle off" warning if Privoxy is toggled on.
1163 #ifdef FEATURE_TOGGLE
1164 (global_toggle_state == 1) &&
1165 #endif /* def FEATURE_TOGGLE */
1166 map_block_killer(exports, "privoxy-is-toggled-off")
1171 return JB_ERR_MEMORY;
1174 if (url_param[0] == '\0')
1176 /* URL paramater not specified, display query form only. */
1178 if (map_block_killer(exports, "url-given")
1179 || map(exports, "url", 1, "", 1))
1182 return JB_ERR_MEMORY;
1187 /* Given a URL, so query it. */
1192 struct file_list *fl;
1193 struct url_actions *b;
1194 struct http_request url_to_query[1];
1195 struct current_action_spec action[1];
1198 if (map(exports, "url", 1, html_encode(url_param), 0))
1202 return JB_ERR_MEMORY;
1205 init_current_action(action);
1207 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1209 free_current_action(action);
1212 return JB_ERR_MEMORY;
1215 memset(url_to_query, '\0', sizeof(url_to_query));
1216 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1217 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1221 if (err == JB_ERR_MEMORY)
1223 free_http_request(url_to_query);
1224 free_current_action(action);
1226 return JB_ERR_MEMORY;
1232 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1233 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1234 if (!err) err = map_block_killer(exports, "valid-url");
1236 free_current_action(action);
1237 free_http_request(url_to_query);
1242 return JB_ERR_MEMORY;
1245 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1249 * We have a warning about SSL paths. Hide it for unencrypted sites.
1251 if (!url_to_query->ssl)
1253 if (map_block_killer(exports, "https"))
1255 free_current_action(action);
1257 free_http_request(url_to_query);
1258 return JB_ERR_MEMORY;
1262 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1264 for (i = 0; i < MAX_AF_FILES; i++)
1266 if (NULL == csp->config->actions_file_short[i]
1267 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1271 if ((fl = csp->actions_list[i]) != NULL)
1273 if ((b = fl->f) != NULL)
1275 /* FIXME: Hardcoded HTML! */
1276 string_append(&matches, "<tr><th>In file: ");
1277 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1278 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1279 string_append(&matches, buf);
1280 string_append(&matches, "View</a>");
1281 #ifdef FEATURE_CGI_EDIT_ACTIONS
1282 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1285 if (access(csp->config->actions_file[i], W_OK) == 0)
1287 #endif /* def HAVE_ACCESS */
1288 snprintf(buf, sizeof(buf),
1289 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1290 string_append(&matches, buf);
1291 string_append(&matches, "Edit</a>");
1296 string_append(&matches, " <strong>No write access.</strong>");
1298 #endif /* def HAVE_ACCESS */
1300 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1302 string_append(&matches, "</th></tr>\n");
1309 for (; (b != NULL) && (matches != NULL); b = b->next)
1311 if (url_match(b->url, url_to_query))
1313 string_append(&matches, "<tr><td>{");
1314 string_join (&matches, actions_to_html(csp, b->action));
1315 string_append(&matches, " }<br>\n<code>");
1316 string_join (&matches, html_encode(b->url->spec));
1317 string_append(&matches, "</code></td></tr>\n");
1319 if (merge_current_action(action, b->action))
1322 free_http_request(url_to_query);
1323 free_current_action(action);
1325 return JB_ERR_MEMORY;
1333 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1336 string_append(&matches, "</table>\n");
1339 * XXX: Kludge to make sure the "Forward settings" section
1340 * shows what forward-override{} would do with the requested URL.
1341 * No one really cares how the CGI request would be forwarded
1342 * if it wasn't intercepted as CGI request in the first place.
1344 * From here on the action bitmask will no longer reflect
1345 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1346 * but luckily it's no longer required later on anyway.
1348 free_current_action(csp->action);
1349 get_url_actions(csp, url_to_query);
1352 * Fill in forwarding settings.
1354 * The possibilities are:
1356 * - http forwarding only
1357 * - socks4(a) forwarding only
1358 * - socks4(a) and http forwarding.
1360 * XXX: Parts of this code could be reused for the
1361 * "forwarding-failed" template which currently doesn't
1362 * display the proxy port and an eventual second forwarder.
1365 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1367 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1369 if (!err) err = map_block_killer(exports, "socks-forwarder");
1370 if (!err) err = map_block_killer(exports, "http-forwarder");
1374 char port[10]; /* We save proxy ports as int but need a string here */
1376 if (!err) err = map_block_killer(exports, "no-forwarder");
1378 if (fwd->gateway_host != NULL)
1380 char *socks_type = NULL;
1385 socks_type = "socks4";
1388 socks_type = "socks4a";
1391 socks_type = "socks5";
1394 socks_type = "socks5t";
1397 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1400 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1401 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1402 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1403 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1407 if (!err) err = map_block_killer(exports, "socks-forwarder");
1410 if (fwd->forward_host != NULL)
1412 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1413 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1414 if (!err) err = map(exports, "forward-port", 1, port, 1);
1418 if (!err) err = map_block_killer(exports, "http-forwarder");
1423 free_http_request(url_to_query);
1425 if (err || matches == NULL)
1427 free_current_action(action);
1429 return JB_ERR_MEMORY;
1432 #ifdef FEATURE_CGI_EDIT_ACTIONS
1433 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1435 err = map_block_killer(exports, "cgi-editor-is-disabled");
1437 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1440 * If zlib support is available, if no content filters
1441 * are enabled or if the prevent-compression action is enabled,
1442 * suppress the "compression could prevent filtering" warning.
1444 #ifndef FEATURE_ZLIB
1445 if (!content_filters_enabled(action) ||
1446 (action->flags & ACTION_NO_COMPRESSION))
1449 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1452 if (err || map(exports, "matches", 1, matches , 0))
1454 free_current_action(action);
1456 return JB_ERR_MEMORY;
1459 s = current_action_to_html(csp, action);
1461 free_current_action(action);
1463 if (map(exports, "final", 1, s, 0))
1466 return JB_ERR_MEMORY;
1470 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1474 /*********************************************************************
1476 * Function : cgi_robots_txt
1478 * Description : CGI function to return "/robots.txt".
1481 * 1 : csp = Current client state (buffers, headers, etc...)
1482 * 2 : rsp = http_response data structure for output
1483 * 3 : parameters = map of cgi parameters
1485 * CGI Parameters : None
1487 * Returns : JB_ERR_OK on success
1488 * JB_ERR_MEMORY on out-of-memory error.
1490 *********************************************************************/
1491 jb_err cgi_robots_txt(struct client_state *csp,
1492 struct http_response *rsp,
1493 const struct map *parameters)
1501 rsp->body = strdup_or_die(
1502 "# This is the Privoxy control interface.\n"
1503 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1510 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1514 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1515 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1517 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1521 /*********************************************************************
1523 * Function : show_defines
1525 * Description : Add to a map the state od all conditional #defines
1526 * used when building
1529 * 1 : exports = map to extend
1531 * Returns : JB_ERR_OK on success
1532 * JB_ERR_MEMORY on out-of-memory error.
1534 *********************************************************************/
1535 static jb_err show_defines(struct map *exports)
1537 jb_err err = JB_ERR_OK;
1540 const char name[31];
1541 const unsigned char is_available;
1544 static const struct feature features[] = {
1546 "FEATURE_ACCEPT_FILTER",
1547 #ifdef FEATURE_ACCEPT_FILTER
1562 "FEATURE_CGI_EDIT_ACTIONS",
1563 #ifdef FEATURE_CGI_EDIT_ACTIONS
1570 "FEATURE_COMPRESSION",
1571 #ifdef FEATURE_COMPRESSION
1578 "FEATURE_CONNECTION_KEEP_ALIVE",
1579 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1586 "FEATURE_CONNECTION_SHARING",
1587 #ifdef FEATURE_CONNECTION_SHARING
1594 "FEATURE_FAST_REDIRECTS",
1595 #ifdef FEATURE_FAST_REDIRECTS
1602 "FEATURE_FORCE_LOAD",
1603 #ifdef FEATURE_FORCE_LOAD
1610 "FEATURE_GRACEFUL_TERMINATION",
1611 #ifdef FEATURE_GRACEFUL_TERMINATION
1618 "FEATURE_IMAGE_BLOCKING",
1619 #ifdef FEATURE_IMAGE_BLOCKING
1626 "FEATURE_IMAGE_DETECT_MSIE",
1627 #ifdef FEATURE_IMAGE_DETECT_MSIE
1634 "FEATURE_IPV6_SUPPORT",
1643 #ifdef FEATURE_NO_GIFS
1651 #ifdef FEATURE_PTHREAD
1658 "FEATURE_STATISTICS",
1659 #ifdef FEATURE_STATISTICS
1666 "FEATURE_STRPTIME_SANITY_CHECKS",
1667 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
1675 #ifdef FEATURE_TOGGLE
1683 #ifdef FEATURE_TRUST
1698 "FEATURE_DYNAMIC_PCRE",
1699 #ifdef FEATURE_DYNAMIC_PCRE
1707 for (i = 0; i < SZ(features); i++)
1709 err = map_conditional(exports, features[i].name, features[i].is_available);
1721 /*********************************************************************
1723 * Function : show_rcs
1725 * Description : Create a string with the rcs info for all sourcefiles
1729 * Returns : A string, or NULL on out-of-memory.
1731 *********************************************************************/
1732 static char *show_rcs(void)
1734 char *result = strdup_or_die("");
1735 char buf[BUFFER_SIZE];
1737 /* Instead of including *all* dot h's in the project (thus creating a
1738 * tremendous amount of dependencies), I will concede to declaring them
1739 * as extern's. This forces the developer to add to this list, but oh well.
1742 #define SHOW_RCS(__x) \
1744 extern const char __x[]; \
1745 snprintf(buf, sizeof(buf), " %s\n", __x); \
1746 string_append(&result, buf); \
1749 /* In alphabetical order */
1750 SHOW_RCS(actions_h_rcs)
1751 SHOW_RCS(actions_rcs)
1753 SHOW_RCS(amiga_h_rcs)
1755 #endif /* def AMIGA */
1758 #ifdef FEATURE_CGI_EDIT_ACTIONS
1759 SHOW_RCS(cgiedit_h_rcs)
1760 SHOW_RCS(cgiedit_rcs)
1761 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1762 SHOW_RCS(cgisimple_h_rcs)
1763 SHOW_RCS(cgisimple_rcs)
1765 SHOW_RCS(cygwin_h_rcs)
1767 SHOW_RCS(deanimate_h_rcs)
1768 SHOW_RCS(deanimate_rcs)
1769 SHOW_RCS(encode_h_rcs)
1770 SHOW_RCS(encode_rcs)
1771 SHOW_RCS(errlog_h_rcs)
1772 SHOW_RCS(errlog_rcs)
1773 SHOW_RCS(filters_h_rcs)
1774 SHOW_RCS(filters_rcs)
1775 SHOW_RCS(gateway_h_rcs)
1776 SHOW_RCS(gateway_rcs)
1777 SHOW_RCS(jbsockets_h_rcs)
1778 SHOW_RCS(jbsockets_rcs)
1781 SHOW_RCS(list_h_rcs)
1783 SHOW_RCS(loadcfg_h_rcs)
1784 SHOW_RCS(loadcfg_rcs)
1785 SHOW_RCS(loaders_h_rcs)
1786 SHOW_RCS(loaders_rcs)
1787 SHOW_RCS(miscutil_h_rcs)
1788 SHOW_RCS(miscutil_rcs)
1789 SHOW_RCS(parsers_h_rcs)
1790 SHOW_RCS(parsers_rcs)
1792 SHOW_RCS(pcrs_h_rcs)
1793 SHOW_RCS(project_h_rcs)
1794 SHOW_RCS(ssplit_h_rcs)
1795 SHOW_RCS(ssplit_rcs)
1796 SHOW_RCS(urlmatch_h_rcs)
1797 SHOW_RCS(urlmatch_rcs)
1799 #ifndef _WIN_CONSOLE
1800 SHOW_RCS(w32log_h_rcs)
1801 SHOW_RCS(w32log_rcs)
1802 SHOW_RCS(w32res_h_rcs)
1803 SHOW_RCS(w32taskbar_h_rcs)
1804 SHOW_RCS(w32taskbar_rcs)
1805 #endif /* ndef _WIN_CONSOLE */
1806 SHOW_RCS(win32_h_rcs)
1808 #endif /* def _WIN32 */
1817 /*********************************************************************
1819 * Function : cgi_show_file
1821 * Description : CGI function that shows the content of a
1822 * configuration file.
1825 * 1 : csp = Current client state (buffers, headers, etc...)
1826 * 2 : rsp = http_response data structure for output
1827 * 3 : parameters = map of cgi parameters
1830 * file : Which file to show. Only first letter is checked,
1835 * Default is to show menu and other information.
1837 * Returns : JB_ERR_OK on success
1838 * JB_ERR_MEMORY on out-of-memory error.
1840 *********************************************************************/
1841 static jb_err cgi_show_file(struct client_state *csp,
1842 struct http_response *rsp,
1843 const struct map *parameters)
1846 const char * filename = NULL;
1847 char * file_description = NULL;
1853 switch (*(lookup(parameters, "file")))
1856 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1858 filename = csp->actions_list[i]->filename;
1859 file_description = "Actions File";
1864 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1866 filename = csp->rlist[i]->filename;
1867 file_description = "Filter File";
1871 #ifdef FEATURE_TRUST
1875 filename = csp->tlist->filename;
1876 file_description = "Trust File";
1879 #endif /* def FEATURE_TRUST */
1882 if (NULL != filename)
1884 struct map *exports;
1889 exports = default_exports(csp, "show-status");
1890 if (NULL == exports)
1892 return JB_ERR_MEMORY;
1895 if (map(exports, "file-description", 1, file_description, 1)
1896 || map(exports, "filepath", 1, html_encode(filename), 0))
1899 return JB_ERR_MEMORY;
1902 err = load_file(filename, &s, &length);
1903 if (JB_ERR_OK != err)
1905 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1908 return JB_ERR_MEMORY;
1913 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 (1 != fread(*buffer, *length, 1, fp))
1996 * May theoretically happen if the file size changes between
1997 * fseek() and fread() because it's edited in-place. Privoxy
1998 * and common text editors don't do that, thus we just fail.
2000 log_error(LOG_LEVEL_ERROR,
2001 "Couldn't completely read file %s.", filename);