1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.111 2011/09/04 11:10:56 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-2011 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>
48 #endif /* def HAVE_ACCESS */
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("404 Privoxy configuration page not found");
146 if (rsp->status == NULL)
149 return JB_ERR_MEMORY;
152 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
156 #ifdef FEATURE_GRACEFUL_TERMINATION
157 /*********************************************************************
161 * Description : CGI function to shut down Privoxy.
162 * NOTE: Turning this on in a production build
163 * would be a BAD idea. An EXTREMELY BAD idea.
164 * In short, don't do it.
167 * 1 : csp = Current client state (buffers, headers, etc...)
168 * 2 : rsp = http_response data structure for output
169 * 3 : parameters = map of cgi parameters
171 * CGI Parameters : none
173 * Returns : JB_ERR_OK on success
174 * JB_ERR_MEMORY on out-of-memory error.
176 *********************************************************************/
177 jb_err cgi_die (struct client_state *csp,
178 struct http_response *rsp,
179 const struct map *parameters)
181 static const char status[] = "200 OK Privoxy shutdown request received";
182 static const char body[] =
185 " <title>Privoxy shutdown request received</title>\n"
186 " <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">\n"
187 " <link rel=\"stylesheet\" type=\"text/css\" href=\"http://config.privoxy.org/send-stylesheet\">\n"
190 "<h1>Privoxy shutdown request received</h1>\n"
191 "<p>Privoxy is going to shut down after the next request.</p>\n"
202 csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
204 rsp->content_length = 0;
205 rsp->head_length = 0;
208 rsp->body = strdup(body);
209 rsp->status = strdup(status);
211 if ((rsp->body == NULL) || (rsp->status == NULL))
213 return JB_ERR_MEMORY;
218 #endif /* def FEATURE_GRACEFUL_TERMINATION */
221 /*********************************************************************
223 * Function : cgi_show_request
225 * Description : Show the client's request and what sed() would have
229 * 1 : csp = Current client state (buffers, headers, etc...)
230 * 2 : rsp = http_response data structure for output
231 * 3 : parameters = map of cgi parameters
233 * CGI Parameters : none
235 * Returns : JB_ERR_OK on success
236 * JB_ERR_MEMORY on out-of-memory error.
238 *********************************************************************/
239 jb_err cgi_show_request(struct client_state *csp,
240 struct http_response *rsp,
241 const struct map *parameters)
250 if (NULL == (exports = default_exports(csp, "show-request")))
252 return JB_ERR_MEMORY;
256 * Repair the damage done to the IOB by get_header()
258 for (p = csp->iob->buf; p < csp->iob->eod; p++)
260 if (*p == '\0') *p = '\n';
264 * Export the original client's request and the one we would
265 * be sending to the server if this wasn't a CGI call
268 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
271 return JB_ERR_MEMORY;
274 if (map(exports, "processed-request", 1,
275 html_encode_and_free_original(list_to_text(csp->headers)), 0))
278 return JB_ERR_MEMORY;
281 return template_fill_for_cgi(csp, "show-request", exports, rsp);
285 /*********************************************************************
287 * Function : cgi_send_banner
289 * Description : CGI function that returns a banner.
292 * 1 : csp = Current client state (buffers, headers, etc...)
293 * 2 : rsp = http_response data structure for output
294 * 3 : parameters = map of cgi parameters
297 * type : Selects the type of banner between "trans", "logo",
298 * and "auto". Defaults to "logo" if absent or invalid.
299 * "auto" means to select as if we were image-blocking.
300 * (Only the first character really counts; b and t are
303 * Returns : JB_ERR_OK on success
304 * JB_ERR_MEMORY on out-of-memory error.
306 *********************************************************************/
307 jb_err cgi_send_banner(struct client_state *csp,
308 struct http_response *rsp,
309 const struct map *parameters)
311 char imagetype = lookup(parameters, "type")[0];
314 * If type is auto, then determine the right thing
315 * to do from the set-image-blocker action
317 if (imagetype == 'a')
324 #ifdef FEATURE_IMAGE_BLOCKING
325 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
327 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
328 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
329 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
333 /* Use default - nothing to do here. */
335 else if (0 == strcmpic(p, "blank"))
339 else if (0 == strcmpic(p, "pattern"))
345 * If the action is to call this CGI, determine
348 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
350 imagetype = p[sizeof(prefix1) - 1];
352 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
354 imagetype = p[sizeof(prefix2) - 1];
358 * Everything else must (should) be a URL to
366 #endif /* def FEATURE_IMAGE_BLOCKING */
370 * Now imagetype is either the non-auto type we were called with,
371 * or it was auto and has since been determined. In any case, we
372 * can proceed to actually answering the request by sending a redirect
373 * or an image as appropriate:
375 if (imagetype == 'r')
377 rsp->status = strdup("302 Local Redirect from Privoxy");
378 if (rsp->status == NULL)
380 return JB_ERR_MEMORY;
382 if (enlist_unique_header(rsp->headers, "Location",
383 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
385 return JB_ERR_MEMORY;
390 if ((imagetype == 'b') || (imagetype == 't'))
392 rsp->body = bindup(image_blank_data, image_blank_length);
393 rsp->content_length = image_blank_length;
397 rsp->body = bindup(image_pattern_data, image_pattern_length);
398 rsp->content_length = image_pattern_length;
401 if (rsp->body == NULL)
403 return JB_ERR_MEMORY;
405 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
407 return JB_ERR_MEMORY;
418 /*********************************************************************
420 * Function : cgi_transparent_image
422 * Description : CGI function that sends a 1x1 transparent image.
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
431 * Returns : JB_ERR_OK on success
432 * JB_ERR_MEMORY on out-of-memory error.
434 *********************************************************************/
435 jb_err cgi_transparent_image(struct client_state *csp,
436 struct http_response *rsp,
437 const struct map *parameters)
442 rsp->body = bindup(image_blank_data, image_blank_length);
443 rsp->content_length = image_blank_length;
445 if (rsp->body == NULL)
447 return JB_ERR_MEMORY;
450 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
452 return JB_ERR_MEMORY;
462 /*********************************************************************
464 * Function : cgi_send_default_favicon
466 * Description : CGI function that sends the standard favicon.
469 * 1 : csp = Current client state (buffers, headers, etc...)
470 * 2 : rsp = http_response data structure for output
471 * 3 : parameters = map of cgi parameters
473 * CGI Parameters : None
475 * Returns : JB_ERR_OK on success
476 * JB_ERR_MEMORY on out-of-memory error.
478 *********************************************************************/
479 jb_err cgi_send_default_favicon(struct client_state *csp,
480 struct http_response *rsp,
481 const struct map *parameters)
483 static const char default_favicon_data[] =
484 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
485 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
486 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
487 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
488 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
489 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
490 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
491 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
492 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
493 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
494 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
495 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
496 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
498 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
503 rsp->body = bindup(default_favicon_data, favicon_length);
504 rsp->content_length = favicon_length;
506 if (rsp->body == NULL)
508 return JB_ERR_MEMORY;
511 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
513 return JB_ERR_MEMORY;
523 /*********************************************************************
525 * Function : cgi_send_error_favicon
527 * Description : CGI function that sends the favicon for error pages.
530 * 1 : csp = Current client state (buffers, headers, etc...)
531 * 2 : rsp = http_response data structure for output
532 * 3 : parameters = map of cgi parameters
534 * CGI Parameters : None
536 * Returns : JB_ERR_OK on success
537 * JB_ERR_MEMORY on out-of-memory error.
539 *********************************************************************/
540 jb_err cgi_send_error_favicon(struct client_state *csp,
541 struct http_response *rsp,
542 const struct map *parameters)
544 static const char error_favicon_data[] =
545 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
546 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
547 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
548 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
549 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
550 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
551 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
552 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
553 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
554 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
555 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
556 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
557 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
559 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
564 rsp->body = bindup(error_favicon_data, favicon_length);
565 rsp->content_length = favicon_length;
567 if (rsp->body == NULL)
569 return JB_ERR_MEMORY;
572 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
574 return JB_ERR_MEMORY;
584 /*********************************************************************
586 * Function : cgi_send_stylesheet
588 * Description : CGI function that sends a css stylesheet found
589 * in the cgi-style.css template
592 * 1 : csp = Current client state (buffers, headers, etc...)
593 * 2 : rsp = http_response data structure for output
594 * 3 : parameters = map of cgi parameters
596 * CGI Parameters : None
598 * Returns : JB_ERR_OK on success
599 * JB_ERR_MEMORY on out-of-memory error.
601 *********************************************************************/
602 jb_err cgi_send_stylesheet(struct client_state *csp,
603 struct http_response *rsp,
604 const struct map *parameters)
613 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
615 if (err == JB_ERR_FILE)
618 * No way to tell user; send empty stylesheet
620 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
624 return err; /* JB_ERR_MEMORY */
627 if (enlist(rsp->headers, "Content-Type: text/css"))
629 return JB_ERR_MEMORY;
637 /*********************************************************************
639 * Function : cgi_send_url_info_osd
641 * Description : CGI function that sends the OpenSearch Description
642 * template for the show-url-info page. It allows to
643 * access the page through "search engine plugins".
646 * 1 : csp = Current client state (buffers, headers, etc...)
647 * 2 : rsp = http_response data structure for output
648 * 3 : parameters = map of cgi parameters
650 * CGI Parameters : None
652 * Returns : JB_ERR_OK on success
653 * JB_ERR_MEMORY on out-of-memory error.
655 *********************************************************************/
656 jb_err cgi_send_url_info_osd(struct client_state *csp,
657 struct http_response *rsp,
658 const struct map *parameters)
660 jb_err err = JB_ERR_MEMORY;
661 struct map *exports = default_exports(csp, NULL);
668 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
669 if (JB_ERR_OK == err)
671 err = enlist(rsp->headers,
672 "Content-Type: application/opensearchdescription+xml");
681 /*********************************************************************
683 * Function : get_content_type
685 * Description : Use the file extension to guess the content type
686 * header we should use to serve the file.
689 * 1 : filename = Name of the file whose content type
692 * Returns : The guessed content type.
694 *********************************************************************/
695 static const char *get_content_type(const char *filename)
700 const char *extension;
701 const char *content_type;
703 static const struct content_type content_types[] =
705 {".css", "text/css"},
706 {".jpg", "image/jpeg"},
707 {".jpeg", "image/jpeg"},
708 {".png", "image/png"},
711 for (i = 0; i < SZ(content_types); i++)
713 if (strstr(filename, content_types[i].extension))
715 return content_types[i].content_type;
719 /* No match by extension, default to html */
723 /*********************************************************************
725 * Function : cgi_send_user_manual
727 * Description : CGI function that sends a file in the user
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 : file=name.html, the name of the HTML file
736 * (relative to user-manual from config)
738 * Returns : JB_ERR_OK on success
739 * JB_ERR_MEMORY on out-of-memory error.
741 *********************************************************************/
742 jb_err cgi_send_user_manual(struct client_state *csp,
743 struct http_response *rsp,
744 const struct map *parameters)
746 const char *filename;
748 jb_err err = JB_ERR_OK;
749 const char *content_type;
755 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
757 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
758 "received while user-manual delivery is disabled.");
759 return cgi_error_404(csp, rsp, parameters);
762 if (!parameters->first)
764 /* requested http://p.p/user-manual (without trailing slash) */
765 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
768 get_string_param(parameters, "file", &filename);
769 if (filename == NULL)
771 /* It's '/' so serve the index.html if there is one. */
772 filename = "index.html";
774 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
777 * We currently only support a flat file
778 * hierarchy for the documentation.
780 log_error(LOG_LEVEL_ERROR,
781 "Rejecting the request to serve '%s' as it contains '/' or '..'",
783 return JB_ERR_CGI_PARAMS;
786 full_path = make_path(csp->config->usermanual, filename);
787 if (full_path == NULL)
789 return JB_ERR_MEMORY;
792 err = load_file(full_path, &rsp->body, &rsp->content_length);
793 if (JB_ERR_OK != err)
795 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
796 if (JB_ERR_FILE == err)
798 err = cgi_error_no_template(csp, rsp, full_path);
805 content_type = get_content_type(filename);
806 log_error(LOG_LEVEL_CGI,
807 "Content-Type guessed for %s: %s", filename, content_type);
809 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
814 /*********************************************************************
816 * Function : cgi_show_version
818 * Description : CGI function that returns a a web page describing the
819 * file versions of Privoxy.
822 * 1 : csp = Current client state (buffers, headers, etc...)
823 * 2 : rsp = http_response data structure for output
824 * 3 : parameters = map of cgi parameters
826 * CGI Parameters : none
828 * Returns : JB_ERR_OK on success
829 * JB_ERR_MEMORY on out-of-memory error.
831 *********************************************************************/
832 jb_err cgi_show_version(struct client_state *csp,
833 struct http_response *rsp,
834 const struct map *parameters)
842 if (NULL == (exports = default_exports(csp, "show-version")))
844 return JB_ERR_MEMORY;
847 if (map(exports, "sourceversions", 1, show_rcs(), 0))
850 return JB_ERR_MEMORY;
853 return template_fill_for_cgi(csp, "show-version", exports, rsp);
857 /*********************************************************************
859 * Function : cgi_show_status
861 * Description : CGI function that returns a web page describing the
862 * current status of Privoxy.
865 * 1 : csp = Current client state (buffers, headers, etc...)
866 * 2 : rsp = http_response data structure for output
867 * 3 : parameters = map of cgi parameters
870 * file : Which file to show. Only first letter is checked,
875 * Default is to show menu and other information.
877 * Returns : JB_ERR_OK on success
878 * JB_ERR_MEMORY on out-of-memory error.
880 *********************************************************************/
881 jb_err cgi_show_status(struct client_state *csp,
882 struct http_response *rsp,
883 const struct map *parameters)
889 char buf[BUFFER_SIZE];
890 #ifdef FEATURE_STATISTICS
891 float perc_rej; /* Percentage of http requests rejected */
893 int local_urls_rejected;
894 #endif /* ndef FEATURE_STATISTICS */
895 jb_err err = JB_ERR_OK;
903 if ('\0' != *(lookup(parameters, "file")))
905 return cgi_show_file(csp, rsp, parameters);
908 if (NULL == (exports = default_exports(csp, "show-status")))
910 return JB_ERR_MEMORY;
914 for (j = 0; (s != NULL) && (j < Argc); j++)
916 if (!err) err = string_join (&s, html_encode(Argv[j]));
917 if (!err) err = string_append(&s, " ");
919 if (!err) err = map(exports, "invocation", 1, s, 0);
921 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
922 if (!err) err = show_defines(exports);
927 return JB_ERR_MEMORY;
930 #ifdef FEATURE_STATISTICS
931 local_urls_read = urls_read;
932 local_urls_rejected = urls_rejected;
935 * Need to alter the stats not to include the fetch of this
938 * Can't do following thread safely! doh!
941 * urls_rejected--; * This will be incremented subsequently *
944 if (local_urls_read == 0)
946 if (!err) err = map_block_killer(exports, "have-stats");
950 if (!err) err = map_block_killer(exports, "have-no-stats");
952 perc_rej = (float)local_urls_rejected * 100.0F /
953 (float)local_urls_read;
955 snprintf(buf, sizeof(buf), "%d", local_urls_read);
956 if (!err) err = map(exports, "requests-received", 1, buf, 1);
958 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
959 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
961 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
962 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
965 #else /* ndef FEATURE_STATISTICS */
966 err = err || map_block_killer(exports, "statistics");
967 #endif /* ndef FEATURE_STATISTICS */
970 * List all action files in use, together with view and edit links,
971 * except for standard.action, which should only be viewable. (Not
972 * enforced in the editor itself)
973 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
976 for (i = 0; i < MAX_AF_FILES; i++)
978 if (csp->actions_list[i] != NULL)
980 if (!err) err = string_append(&s, "<tr><td>");
981 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
982 snprintf(buf, sizeof(buf),
983 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
984 if (!err) err = string_append(&s, buf);
986 #ifdef FEATURE_CGI_EDIT_ACTIONS
987 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
988 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
989 && (NULL != csp->config->actions_file_short[i]))
992 if (access(csp->config->actions_file[i], W_OK) == 0)
994 #endif /* def HAVE_ACCESS */
995 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
996 if (!err) err = string_append(&s, buf);
1001 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1003 #endif /* def HAVE_ACCESS */
1007 if (!err) err = string_append(&s, "</td></tr>\n");
1012 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1016 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1020 * List all re_filterfiles in use, together with view options.
1021 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1024 for (i = 0; i < MAX_AF_FILES; i++)
1026 if (csp->rlist[i] != NULL)
1028 if (!err) err = string_append(&s, "<tr><td>");
1029 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1030 snprintf(buf, sizeof(buf),
1031 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1032 if (!err) err = string_append(&s, buf);
1033 if (!err) err = string_append(&s, "</td></tr>\n");
1038 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1042 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1043 if (!err) err = map_block_killer(exports, "have-filterfile");
1046 #ifdef FEATURE_TRUST
1049 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1053 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1054 if (!err) err = map_block_killer(exports, "have-trustfile");
1057 if (!err) err = map_block_killer(exports, "trust-support");
1058 #endif /* ndef FEATURE_TRUST */
1060 #ifdef FEATURE_CGI_EDIT_ACTIONS
1061 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1063 err = map_block_killer(exports, "cgi-editor-is-disabled");
1065 #endif /* ndef CGI_EDIT_ACTIONS */
1070 return JB_ERR_MEMORY;
1073 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1077 /*********************************************************************
1079 * Function : cgi_show_url_info
1081 * Description : CGI function that determines and shows which actions
1082 * Privoxy will perform for a given url, and which
1083 * matches starting from the defaults have lead to that.
1086 * 1 : csp = Current client state (buffers, headers, etc...)
1087 * 2 : rsp = http_response data structure for output
1088 * 3 : parameters = map of cgi parameters
1091 * url : The url whose actions are to be determined.
1092 * If url is unset, the url-given conditional will be
1093 * set, so that all but the form can be suppressed in
1096 * Returns : JB_ERR_OK on success
1097 * JB_ERR_MEMORY on out-of-memory error.
1099 *********************************************************************/
1100 jb_err cgi_show_url_info(struct client_state *csp,
1101 struct http_response *rsp,
1102 const struct map *parameters)
1105 struct map *exports;
1112 if (NULL == (exports = default_exports(csp, "show-url-info")))
1114 return JB_ERR_MEMORY;
1118 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1120 url_param = strdup(lookup(parameters, "url"));
1121 if (url_param == NULL)
1124 return JB_ERR_MEMORY;
1129 * Handle prefixes. 4 possibilities:
1130 * 1) "http://" or "https://" prefix present and followed by URL - OK
1131 * 2) Only the "http://" or "https://" part is present, no URL - change
1132 * to empty string so it will be detected later as "no URL".
1133 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1135 * 4) Parameter not specified or is empty string - let this fall through
1136 * for now, next block of code will handle it.
1138 if (0 == strncmp(url_param, "http://", 7))
1140 if (url_param[7] == '\0')
1143 * Empty URL (just prefix).
1144 * Make it totally empty so it's caught by the next if()
1146 url_param[0] = '\0';
1149 else if (0 == strncmp(url_param, "https://", 8))
1151 if (url_param[8] == '\0')
1154 * Empty URL (just prefix).
1155 * Make it totally empty so it's caught by the next if()
1157 url_param[0] = '\0';
1160 else if ((url_param[0] != '\0')
1161 && ((NULL == strstr(url_param, "://")
1162 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1165 * No prefix or at least no prefix before
1166 * the first slash - assume http://
1168 char *url_param_prefixed = strdup("http://");
1170 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1173 return JB_ERR_MEMORY;
1175 url_param = url_param_prefixed;
1179 * Hide "toggle off" warning if Privoxy is toggled on.
1182 #ifdef FEATURE_TOGGLE
1183 (global_toggle_state == 1) &&
1184 #endif /* def FEATURE_TOGGLE */
1185 map_block_killer(exports, "privoxy-is-toggled-off")
1189 return JB_ERR_MEMORY;
1192 if (url_param[0] == '\0')
1194 /* URL paramater not specified, display query form only. */
1196 if (map_block_killer(exports, "url-given")
1197 || map(exports, "url", 1, "", 1))
1200 return JB_ERR_MEMORY;
1205 /* Given a URL, so query it. */
1210 struct file_list *fl;
1211 struct url_actions *b;
1212 struct http_request url_to_query[1];
1213 struct current_action_spec action[1];
1216 if (map(exports, "url", 1, html_encode(url_param), 0))
1220 return JB_ERR_MEMORY;
1223 init_current_action(action);
1225 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1227 free_current_action(action);
1230 return JB_ERR_MEMORY;
1233 memset(url_to_query, '\0', sizeof(url_to_query));
1234 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1235 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1239 if (err == JB_ERR_MEMORY)
1241 free_http_request(url_to_query);
1242 free_current_action(action);
1244 return JB_ERR_MEMORY;
1250 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1251 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1252 if (!err) err = map_block_killer(exports, "valid-url");
1254 free_current_action(action);
1255 free_http_request(url_to_query);
1260 return JB_ERR_MEMORY;
1263 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1267 * We have a warning about SSL paths. Hide it for unencrypted sites.
1269 if (!url_to_query->ssl)
1271 if (map_block_killer(exports, "https"))
1273 free_current_action(action);
1275 free_http_request(url_to_query);
1276 return JB_ERR_MEMORY;
1280 matches = strdup("<table summary=\"\" class=\"transparent\">");
1282 for (i = 0; i < MAX_AF_FILES; i++)
1284 if (NULL == csp->config->actions_file_short[i]
1285 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1289 if ((fl = csp->actions_list[i]) != NULL)
1291 if ((b = fl->f) != NULL)
1293 /* FIXME: Hardcoded HTML! */
1294 string_append(&matches, "<tr><th>In file: ");
1295 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1296 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1297 string_append(&matches, buf);
1298 string_append(&matches, "View</a>");
1299 #ifdef FEATURE_CGI_EDIT_ACTIONS
1300 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1303 if (access(csp->config->actions_file[i], W_OK) == 0)
1305 #endif /* def HAVE_ACCESS */
1306 snprintf(buf, sizeof(buf),
1307 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1308 string_append(&matches, buf);
1309 string_append(&matches, "Edit</a>");
1314 string_append(&matches, " <strong>No write access.</strong>");
1316 #endif /* def HAVE_ACCESS */
1318 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1320 string_append(&matches, "</th></tr>\n");
1327 for (; (b != NULL) && (matches != NULL); b = b->next)
1329 if (url_match(b->url, url_to_query))
1331 string_append(&matches, "<tr><td>{");
1332 string_join (&matches, actions_to_html(csp, b->action));
1333 string_append(&matches, " }<br>\n<code>");
1334 string_join (&matches, html_encode(b->url->spec));
1335 string_append(&matches, "</code></td></tr>\n");
1337 if (merge_current_action(action, b->action))
1340 free_http_request(url_to_query);
1341 free_current_action(action);
1343 return JB_ERR_MEMORY;
1351 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1354 string_append(&matches, "</table>\n");
1357 * XXX: Kludge to make sure the "Forward settings" section
1358 * shows what forward-override{} would do with the requested URL.
1359 * No one really cares how the CGI request would be forwarded
1360 * if it wasn't intercepted as CGI request in the first place.
1362 * From here on the action bitmask will no longer reflect
1363 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1364 * but luckily it's no longer required later on anyway.
1366 free_current_action(csp->action);
1367 get_url_actions(csp, url_to_query);
1370 * Fill in forwarding settings.
1372 * The possibilities are:
1374 * - http forwarding only
1375 * - socks4(a) forwarding only
1376 * - socks4(a) and http forwarding.
1378 * XXX: Parts of this code could be reused for the
1379 * "forwarding-failed" template which currently doesn't
1380 * display the proxy port and an eventual second forwarder.
1383 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1385 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1387 if (!err) err = map_block_killer(exports, "socks-forwarder");
1388 if (!err) err = map_block_killer(exports, "http-forwarder");
1392 char port[10]; /* We save proxy ports as int but need a string here */
1394 if (!err) err = map_block_killer(exports, "no-forwarder");
1396 if (fwd->gateway_host != NULL)
1398 char *socks_type = NULL;
1403 socks_type = "socks4";
1406 socks_type = "socks4a";
1409 socks_type = "socks5";
1412 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1415 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1416 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1417 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1418 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1422 if (!err) err = map_block_killer(exports, "socks-forwarder");
1425 if (fwd->forward_host != NULL)
1427 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1428 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1429 if (!err) err = map(exports, "forward-port", 1, port, 1);
1433 if (!err) err = map_block_killer(exports, "http-forwarder");
1438 free_http_request(url_to_query);
1440 if (err || matches == NULL)
1442 free_current_action(action);
1444 return JB_ERR_MEMORY;
1447 #ifdef FEATURE_CGI_EDIT_ACTIONS
1448 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1450 err = map_block_killer(exports, "cgi-editor-is-disabled");
1452 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1455 * If zlib support is available, if no content filters
1456 * are enabled or if the prevent-compression action is enabled,
1457 * suppress the "compression could prevent filtering" warning.
1459 #ifndef FEATURE_ZLIB
1460 if (!content_filters_enabled(action) ||
1461 (action->flags & ACTION_NO_COMPRESSION))
1464 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1467 if (err || map(exports, "matches", 1, matches , 0))
1469 free_current_action(action);
1471 return JB_ERR_MEMORY;
1474 s = current_action_to_html(csp, action);
1476 free_current_action(action);
1478 if (map(exports, "final", 1, s, 0))
1481 return JB_ERR_MEMORY;
1485 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1489 /*********************************************************************
1491 * Function : cgi_robots_txt
1493 * Description : CGI function to return "/robots.txt".
1496 * 1 : csp = Current client state (buffers, headers, etc...)
1497 * 2 : rsp = http_response data structure for output
1498 * 3 : parameters = map of cgi parameters
1500 * CGI Parameters : None
1502 * Returns : JB_ERR_OK on success
1503 * JB_ERR_MEMORY on out-of-memory error.
1505 *********************************************************************/
1506 jb_err cgi_robots_txt(struct client_state *csp,
1507 struct http_response *rsp,
1508 const struct map *parameters)
1517 "# This is the Privoxy control interface.\n"
1518 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1524 if (rsp->body == NULL)
1526 return JB_ERR_MEMORY;
1529 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1533 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1534 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1536 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1540 /*********************************************************************
1542 * Function : show_defines
1544 * Description : Add to a map the state od all conditional #defines
1545 * used when building
1548 * 1 : exports = map to extend
1550 * Returns : JB_ERR_OK on success
1551 * JB_ERR_MEMORY on out-of-memory error.
1553 *********************************************************************/
1554 static jb_err show_defines(struct map *exports)
1556 jb_err err = JB_ERR_OK;
1558 #ifdef FEATURE_ACCEPT_FILTER
1559 if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 1);
1560 #else /* ifndef FEATURE_ACCEPT_FILTER */
1561 if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 0);
1562 #endif /* ndef FEATURE_ACCEPT_FILTER */
1565 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1566 #else /* ifndef FEATURE_ACL */
1567 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1568 #endif /* ndef FEATURE_ACL */
1570 #ifdef FEATURE_CGI_EDIT_ACTIONS
1571 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1572 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1573 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1574 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1576 #ifdef FEATURE_COMPRESSION
1577 if (!err) err = map_conditional(exports, "FEATURE_COMPRESSION", 1);
1578 #else /* ifndef FEATURE_COMPRESSION */
1579 if (!err) err = map_conditional(exports, "FEATURE_COMPRESSION", 0);
1580 #endif /* ndef FEATURE_COMPRESSION */
1582 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1583 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1584 #else /* ifndef FEATURE_CONNECTION_KEEP_ALIVE */
1585 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1586 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1588 #ifdef FEATURE_CONNECTION_SHARING
1589 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 1);
1590 #else /* ifndef FEATURE_CONNECTION_SHARING */
1591 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 0);
1592 #endif /* ndef FEATURE_CONNECTION_SHARING */
1594 #ifdef FEATURE_FAST_REDIRECTS
1595 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1596 #else /* ifndef FEATURE_FAST_REDIRECTS */
1597 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1598 #endif /* ndef FEATURE_FAST_REDIRECTS */
1600 #ifdef FEATURE_FORCE_LOAD
1601 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1602 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1603 #else /* ifndef FEATURE_FORCE_LOAD */
1604 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1605 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1606 #endif /* ndef FEATURE_FORCE_LOAD */
1608 #ifdef FEATURE_GRACEFUL_TERMINATION
1609 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1610 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1611 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1612 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1614 #ifdef FEATURE_IMAGE_BLOCKING
1615 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1616 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1617 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1618 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1620 #ifdef FEATURE_IMAGE_DETECT_MSIE
1621 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1622 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1623 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1624 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1627 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 1);
1628 #else /* ifndef HAVE_RFC2553 */
1629 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 0);
1630 #endif /* ndef HAVE_RFC2553 */
1632 #ifdef FEATURE_NO_GIFS
1633 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1634 #else /* ifndef FEATURE_NO_GIFS */
1635 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1636 #endif /* ndef FEATURE_NO_GIFS */
1638 #ifdef FEATURE_PTHREAD
1639 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1640 #else /* ifndef FEATURE_PTHREAD */
1641 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1642 #endif /* ndef FEATURE_PTHREAD */
1644 #ifdef FEATURE_STATISTICS
1645 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1646 #else /* ifndef FEATURE_STATISTICS */
1647 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1648 #endif /* ndef FEATURE_STATISTICS */
1650 #ifdef FEATURE_TOGGLE
1651 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1652 #else /* ifndef FEATURE_TOGGLE */
1653 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1654 #endif /* ndef FEATURE_TOGGLE */
1656 #ifdef FEATURE_TRUST
1657 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1658 #else /* ifndef FEATURE_TRUST */
1659 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1660 #endif /* ndef FEATURE_TRUST */
1663 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1664 #else /* ifndef FEATURE_ZLIB */
1665 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1666 #endif /* ndef FEATURE_ZLIB */
1669 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1670 #else /* ifndef STATIC_PCRE */
1671 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1672 #endif /* ndef STATIC_PCRE */
1675 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1676 #else /* ifndef STATIC_PCRS */
1677 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1678 #endif /* ndef STATIC_PCRS */
1684 /*********************************************************************
1686 * Function : show_rcs
1688 * Description : Create a string with the rcs info for all sourcefiles
1692 * Returns : A string, or NULL on out-of-memory.
1694 *********************************************************************/
1695 static char *show_rcs(void)
1697 char *result = strdup("");
1698 char buf[BUFFER_SIZE];
1700 /* Instead of including *all* dot h's in the project (thus creating a
1701 * tremendous amount of dependencies), I will concede to declaring them
1702 * as extern's. This forces the developer to add to this list, but oh well.
1705 #define SHOW_RCS(__x) \
1707 extern const char __x[]; \
1708 snprintf(buf, sizeof(buf), " %s\n", __x); \
1709 string_append(&result, buf); \
1712 /* In alphabetical order */
1713 SHOW_RCS(actions_h_rcs)
1714 SHOW_RCS(actions_rcs)
1716 SHOW_RCS(amiga_h_rcs)
1718 #endif /* def AMIGA */
1721 #ifdef FEATURE_CGI_EDIT_ACTIONS
1722 SHOW_RCS(cgiedit_h_rcs)
1723 SHOW_RCS(cgiedit_rcs)
1724 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1725 SHOW_RCS(cgisimple_h_rcs)
1726 SHOW_RCS(cgisimple_rcs)
1728 SHOW_RCS(cygwin_h_rcs)
1730 SHOW_RCS(deanimate_h_rcs)
1731 SHOW_RCS(deanimate_rcs)
1732 SHOW_RCS(encode_h_rcs)
1733 SHOW_RCS(encode_rcs)
1734 SHOW_RCS(errlog_h_rcs)
1735 SHOW_RCS(errlog_rcs)
1736 SHOW_RCS(filters_h_rcs)
1737 SHOW_RCS(filters_rcs)
1738 SHOW_RCS(gateway_h_rcs)
1739 SHOW_RCS(gateway_rcs)
1740 SHOW_RCS(jbsockets_h_rcs)
1741 SHOW_RCS(jbsockets_rcs)
1744 SHOW_RCS(list_h_rcs)
1746 SHOW_RCS(loadcfg_h_rcs)
1747 SHOW_RCS(loadcfg_rcs)
1748 SHOW_RCS(loaders_h_rcs)
1749 SHOW_RCS(loaders_rcs)
1750 SHOW_RCS(miscutil_h_rcs)
1751 SHOW_RCS(miscutil_rcs)
1752 SHOW_RCS(parsers_h_rcs)
1753 SHOW_RCS(parsers_rcs)
1755 SHOW_RCS(pcrs_h_rcs)
1756 SHOW_RCS(project_h_rcs)
1757 SHOW_RCS(ssplit_h_rcs)
1758 SHOW_RCS(ssplit_rcs)
1759 SHOW_RCS(urlmatch_h_rcs)
1760 SHOW_RCS(urlmatch_rcs)
1762 #ifndef _WIN_CONSOLE
1763 SHOW_RCS(w32log_h_rcs)
1764 SHOW_RCS(w32log_rcs)
1765 SHOW_RCS(w32res_h_rcs)
1766 SHOW_RCS(w32taskbar_h_rcs)
1767 SHOW_RCS(w32taskbar_rcs)
1768 #endif /* ndef _WIN_CONSOLE */
1769 SHOW_RCS(win32_h_rcs)
1771 #endif /* def _WIN32 */
1780 /*********************************************************************
1782 * Function : cgi_show_file
1784 * Description : CGI function that shows the content of a
1785 * configuration file.
1788 * 1 : csp = Current client state (buffers, headers, etc...)
1789 * 2 : rsp = http_response data structure for output
1790 * 3 : parameters = map of cgi parameters
1793 * file : Which file to show. Only first letter is checked,
1798 * Default is to show menu and other information.
1800 * Returns : JB_ERR_OK on success
1801 * JB_ERR_MEMORY on out-of-memory error.
1803 *********************************************************************/
1804 static jb_err cgi_show_file(struct client_state *csp,
1805 struct http_response *rsp,
1806 const struct map *parameters)
1809 const char * filename = NULL;
1810 char * file_description = NULL;
1816 switch (*(lookup(parameters, "file")))
1819 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1821 filename = csp->actions_list[i]->filename;
1822 file_description = "Actions File";
1827 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1829 filename = csp->rlist[i]->filename;
1830 file_description = "Filter File";
1834 #ifdef FEATURE_TRUST
1838 filename = csp->tlist->filename;
1839 file_description = "Trust File";
1842 #endif /* def FEATURE_TRUST */
1845 if (NULL != filename)
1847 struct map *exports;
1852 exports = default_exports(csp, "show-status");
1853 if (NULL == exports)
1855 return JB_ERR_MEMORY;
1858 if ( map(exports, "file-description", 1, file_description, 1)
1859 || map(exports, "filepath", 1, html_encode(filename), 0) )
1862 return JB_ERR_MEMORY;
1865 err = load_file(filename, &s, &length);
1866 if (JB_ERR_OK != err)
1868 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1871 return JB_ERR_MEMORY;
1876 s = html_encode_and_free_original(s);
1879 return JB_ERR_MEMORY;
1882 if (map(exports, "contents", 1, s, 0))
1885 return JB_ERR_MEMORY;
1889 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1892 return JB_ERR_CGI_PARAMS;
1896 /*********************************************************************
1898 * Function : load_file
1900 * Description : Loads a file into a buffer.
1903 * 1 : filename = Name of the file to be loaded.
1904 * 2 : buffer = Used to return the file's content.
1905 * 3 : length = Used to return the size of the file.
1907 * Returns : JB_ERR_OK in case of success,
1908 * JB_ERR_FILE in case of ordinary file loading errors
1909 * (fseek() and ftell() errors are fatal)
1910 * JB_ERR_MEMORY in case of out-of-memory.
1912 *********************************************************************/
1913 static jb_err load_file(const char *filename, char **buffer, size_t *length)
1917 jb_err err = JB_ERR_OK;
1919 fp = fopen(filename, "rb");
1922 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
1926 /* Get file length */
1927 if (fseek(fp, 0, SEEK_END))
1929 log_error(LOG_LEVEL_FATAL,
1930 "Unexpected error while fseek()ing to the end of %s: %E",
1936 log_error(LOG_LEVEL_FATAL,
1937 "Unexpected ftell() error while loading %s: %E",
1940 *length = (size_t)ret;
1942 /* Go back to the beginning. */
1943 if (fseek(fp, 0, SEEK_SET))
1945 log_error(LOG_LEVEL_FATAL,
1946 "Unexpected error while fseek()ing to the beginning of %s: %E",
1950 *buffer = (char *)zalloc(*length + 1);
1951 if (NULL == *buffer)
1953 err = JB_ERR_MEMORY;
1955 else if (!fread(*buffer, *length, 1, fp))
1958 * May happen if the file size changes between fseek() and
1959 * fread(). If it does, we just log it and serve what we got.
1961 log_error(LOG_LEVEL_ERROR,
1962 "Couldn't completely read file %s.", filename);