1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.109 2011/04/19 13:00:47 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
6 * Purpose : Simple CGIs to get information about Privoxy's
9 * Functions declared include:
12 * Copyright : Written by and Copyright (C) 2001-2011 the
13 * Privoxy team. http://www.privoxy.org/
15 * Based on the Internet Junkbuster originally written
16 * by and Copyright (C) 1997 Anonymous Coders and
17 * Junkbusters Corporation. http://www.junkbusters.com
19 * This program is free software; you can redistribute it
20 * and/or modify it under the terms of the GNU General
21 * Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at
23 * your option) any later version.
25 * This program is distributed in the hope that it will
26 * be useful, but WITHOUT ANY WARRANTY; without even the
27 * implied warranty of MERCHANTABILITY or FITNESS FOR A
28 * PARTICULAR PURPOSE. See the GNU General Public
29 * License for more details.
31 * The GNU General Public License should be included with
32 * this file. If not, you can view it at
33 * http://www.gnu.org/copyleft/gpl.html
34 * or write to the Free Software Foundation, Inc., 59
35 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 **********************************************************************/
43 #include <sys/types.h>
51 #endif /* def HAVE_ACCESS */
55 #include "cgisimple.h"
67 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
69 static char *show_rcs(void);
70 static jb_err show_defines(struct map *exports);
71 static jb_err cgi_show_file(struct client_state *csp,
72 struct http_response *rsp,
73 const struct map *parameters);
74 static jb_err load_file(const char *filename, char **buffer, size_t *length);
76 /*********************************************************************
78 * Function : cgi_default
80 * Description : CGI function that is called for the CGI_SITE_1_HOST
81 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
82 * Boring - only exports the default exports.
85 * 1 : csp = Current client state (buffers, headers, etc...)
86 * 2 : rsp = http_response data structure for output
87 * 3 : parameters = map of cgi parameters
89 * CGI Parameters : none
91 * Returns : JB_ERR_OK on success
92 * JB_ERR_MEMORY on out-of-memory
94 *********************************************************************/
95 jb_err cgi_default(struct client_state *csp,
96 struct http_response *rsp,
97 const struct map *parameters)
106 if (NULL == (exports = default_exports(csp, "")))
108 return JB_ERR_MEMORY;
111 return template_fill_for_cgi(csp, "default", exports, rsp);
115 /*********************************************************************
117 * Function : cgi_error_404
119 * Description : CGI function that is called if an unknown action was
123 * 1 : csp = Current client state (buffers, headers, etc...)
124 * 2 : rsp = http_response data structure for output
125 * 3 : parameters = map of cgi parameters
127 * CGI Parameters : none
129 * Returns : JB_ERR_OK on success
130 * JB_ERR_MEMORY on out-of-memory error.
132 *********************************************************************/
133 jb_err cgi_error_404(struct client_state *csp,
134 struct http_response *rsp,
135 const struct map *parameters)
143 if (NULL == (exports = default_exports(csp, NULL)))
145 return JB_ERR_MEMORY;
148 rsp->status = strdup("404 Privoxy configuration page not found");
149 if (rsp->status == NULL)
152 return JB_ERR_MEMORY;
155 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
159 #ifdef FEATURE_GRACEFUL_TERMINATION
160 /*********************************************************************
164 * Description : CGI function to shut down Privoxy.
165 * NOTE: Turning this on in a production build
166 * would be a BAD idea. An EXTREMELY BAD idea.
167 * In short, don't do it.
170 * 1 : csp = Current client state (buffers, headers, etc...)
171 * 2 : rsp = http_response data structure for output
172 * 3 : parameters = map of cgi parameters
174 * CGI Parameters : none
176 * Returns : JB_ERR_OK on success
177 * JB_ERR_MEMORY on out-of-memory error.
179 *********************************************************************/
180 jb_err cgi_die (struct client_state *csp,
181 struct http_response *rsp,
182 const struct map *parameters)
184 static const char status[] = "200 OK Privoxy shutdown request received";
185 static const char body[] =
188 " <title>Privoxy shutdown request received</title>\n"
189 " <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">\n"
190 " <link rel=\"stylesheet\" type=\"text/css\" href=\"http://config.privoxy.org/send-stylesheet\">\n"
193 "<h1>Privoxy shutdown request received</h1>\n"
194 "<p>Privoxy is going to shut down after the next request.</p>\n"
205 csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
207 rsp->content_length = 0;
208 rsp->head_length = 0;
211 rsp->body = strdup(body);
212 rsp->status = strdup(status);
214 if ((rsp->body == NULL) || (rsp->status == NULL))
216 return JB_ERR_MEMORY;
221 #endif /* def FEATURE_GRACEFUL_TERMINATION */
224 /*********************************************************************
226 * Function : cgi_show_request
228 * Description : Show the client's request and what sed() would have
232 * 1 : csp = Current client state (buffers, headers, etc...)
233 * 2 : rsp = http_response data structure for output
234 * 3 : parameters = map of cgi parameters
236 * CGI Parameters : none
238 * Returns : JB_ERR_OK on success
239 * JB_ERR_MEMORY on out-of-memory error.
241 *********************************************************************/
242 jb_err cgi_show_request(struct client_state *csp,
243 struct http_response *rsp,
244 const struct map *parameters)
253 if (NULL == (exports = default_exports(csp, "show-request")))
255 return JB_ERR_MEMORY;
259 * Repair the damage done to the IOB by get_header()
261 for (p = csp->iob->buf; p < csp->iob->eod; p++)
263 if (*p == '\0') *p = '\n';
267 * Export the original client's request and the one we would
268 * be sending to the server if this wasn't a CGI call
271 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
274 return JB_ERR_MEMORY;
277 if (map(exports, "processed-request", 1,
278 html_encode_and_free_original(list_to_text(csp->headers)), 0))
281 return JB_ERR_MEMORY;
284 return template_fill_for_cgi(csp, "show-request", exports, rsp);
288 /*********************************************************************
290 * Function : cgi_send_banner
292 * Description : CGI function that returns a banner.
295 * 1 : csp = Current client state (buffers, headers, etc...)
296 * 2 : rsp = http_response data structure for output
297 * 3 : parameters = map of cgi parameters
300 * type : Selects the type of banner between "trans", "logo",
301 * and "auto". Defaults to "logo" if absent or invalid.
302 * "auto" means to select as if we were image-blocking.
303 * (Only the first character really counts; b and t are
306 * Returns : JB_ERR_OK on success
307 * JB_ERR_MEMORY on out-of-memory error.
309 *********************************************************************/
310 jb_err cgi_send_banner(struct client_state *csp,
311 struct http_response *rsp,
312 const struct map *parameters)
314 char imagetype = lookup(parameters, "type")[0];
317 * If type is auto, then determine the right thing
318 * to do from the set-image-blocker action
320 if (imagetype == 'a')
327 #ifdef FEATURE_IMAGE_BLOCKING
328 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
330 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
331 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
332 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
336 /* Use default - nothing to do here. */
338 else if (0 == strcmpic(p, "blank"))
342 else if (0 == strcmpic(p, "pattern"))
348 * If the action is to call this CGI, determine
351 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
353 imagetype = p[sizeof(prefix1) - 1];
355 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
357 imagetype = p[sizeof(prefix2) - 1];
361 * Everything else must (should) be a URL to
369 #endif /* def FEATURE_IMAGE_BLOCKING */
373 * Now imagetype is either the non-auto type we were called with,
374 * or it was auto and has since been determined. In any case, we
375 * can proceed to actually answering the request by sending a redirect
376 * or an image as appropriate:
378 if (imagetype == 'r')
380 rsp->status = strdup("302 Local Redirect from Privoxy");
381 if (rsp->status == NULL)
383 return JB_ERR_MEMORY;
385 if (enlist_unique_header(rsp->headers, "Location",
386 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
388 return JB_ERR_MEMORY;
393 if ((imagetype == 'b') || (imagetype == 't'))
395 rsp->body = bindup(image_blank_data, image_blank_length);
396 rsp->content_length = image_blank_length;
400 rsp->body = bindup(image_pattern_data, image_pattern_length);
401 rsp->content_length = image_pattern_length;
404 if (rsp->body == NULL)
406 return JB_ERR_MEMORY;
408 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
410 return JB_ERR_MEMORY;
421 /*********************************************************************
423 * Function : cgi_transparent_image
425 * Description : CGI function that sends a 1x1 transparent image.
428 * 1 : csp = Current client state (buffers, headers, etc...)
429 * 2 : rsp = http_response data structure for output
430 * 3 : parameters = map of cgi parameters
432 * CGI Parameters : None
434 * Returns : JB_ERR_OK on success
435 * JB_ERR_MEMORY on out-of-memory error.
437 *********************************************************************/
438 jb_err cgi_transparent_image(struct client_state *csp,
439 struct http_response *rsp,
440 const struct map *parameters)
445 rsp->body = bindup(image_blank_data, image_blank_length);
446 rsp->content_length = image_blank_length;
448 if (rsp->body == NULL)
450 return JB_ERR_MEMORY;
453 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
455 return JB_ERR_MEMORY;
465 /*********************************************************************
467 * Function : cgi_send_default_favicon
469 * Description : CGI function that sends the standard favicon.
472 * 1 : csp = Current client state (buffers, headers, etc...)
473 * 2 : rsp = http_response data structure for output
474 * 3 : parameters = map of cgi parameters
476 * CGI Parameters : None
478 * Returns : JB_ERR_OK on success
479 * JB_ERR_MEMORY on out-of-memory error.
481 *********************************************************************/
482 jb_err cgi_send_default_favicon(struct client_state *csp,
483 struct http_response *rsp,
484 const struct map *parameters)
486 static const char default_favicon_data[] =
487 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
488 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
489 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
490 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
491 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
492 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
493 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
494 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
495 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
496 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
497 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
498 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
499 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
501 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
506 rsp->body = bindup(default_favicon_data, favicon_length);
507 rsp->content_length = favicon_length;
509 if (rsp->body == NULL)
511 return JB_ERR_MEMORY;
514 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
516 return JB_ERR_MEMORY;
526 /*********************************************************************
528 * Function : cgi_send_error_favicon
530 * Description : CGI function that sends the favicon for error pages.
533 * 1 : csp = Current client state (buffers, headers, etc...)
534 * 2 : rsp = http_response data structure for output
535 * 3 : parameters = map of cgi parameters
537 * CGI Parameters : None
539 * Returns : JB_ERR_OK on success
540 * JB_ERR_MEMORY on out-of-memory error.
542 *********************************************************************/
543 jb_err cgi_send_error_favicon(struct client_state *csp,
544 struct http_response *rsp,
545 const struct map *parameters)
547 static const char error_favicon_data[] =
548 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
549 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
550 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
551 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
552 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
553 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
554 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
555 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
556 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
557 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
558 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
559 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
560 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
562 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
567 rsp->body = bindup(error_favicon_data, favicon_length);
568 rsp->content_length = favicon_length;
570 if (rsp->body == NULL)
572 return JB_ERR_MEMORY;
575 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
577 return JB_ERR_MEMORY;
587 /*********************************************************************
589 * Function : cgi_send_stylesheet
591 * Description : CGI function that sends a css stylesheet found
592 * in the cgi-style.css template
595 * 1 : csp = Current client state (buffers, headers, etc...)
596 * 2 : rsp = http_response data structure for output
597 * 3 : parameters = map of cgi parameters
599 * CGI Parameters : None
601 * Returns : JB_ERR_OK on success
602 * JB_ERR_MEMORY on out-of-memory error.
604 *********************************************************************/
605 jb_err cgi_send_stylesheet(struct client_state *csp,
606 struct http_response *rsp,
607 const struct map *parameters)
616 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
618 if (err == JB_ERR_FILE)
621 * No way to tell user; send empty stylesheet
623 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
627 return err; /* JB_ERR_MEMORY */
630 if (enlist(rsp->headers, "Content-Type: text/css"))
632 return JB_ERR_MEMORY;
640 /*********************************************************************
642 * Function : cgi_send_url_info_osd
644 * Description : CGI function that sends the OpenSearch Description
645 * template for the show-url-info page. It allows to
646 * access the page through "search engine plugins".
649 * 1 : csp = Current client state (buffers, headers, etc...)
650 * 2 : rsp = http_response data structure for output
651 * 3 : parameters = map of cgi parameters
653 * CGI Parameters : None
655 * Returns : JB_ERR_OK on success
656 * JB_ERR_MEMORY on out-of-memory error.
658 *********************************************************************/
659 jb_err cgi_send_url_info_osd(struct client_state *csp,
660 struct http_response *rsp,
661 const struct map *parameters)
663 jb_err err = JB_ERR_MEMORY;
664 struct map *exports = default_exports(csp, NULL);
671 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
672 if (JB_ERR_OK == err)
674 err = enlist(rsp->headers,
675 "Content-Type: application/opensearchdescription+xml");
684 /*********************************************************************
686 * Function : get_content_type
688 * Description : Use the file extension to guess the content type
689 * header we should use to serve the file.
692 * 1 : filename = Name of the file whose content type
695 * Returns : The guessed content type.
697 *********************************************************************/
698 static const char *get_content_type(const char *filename)
703 const char *extension;
704 const char *content_type;
706 static const struct content_type content_types[] =
708 {".css", "text/css"},
709 {".jpg", "image/jpeg"},
710 {".jpeg", "image/jpeg"},
711 {".png", "image/png"},
714 for (i = 0; i < SZ(content_types); i++)
716 if (strstr(filename, content_types[i].extension))
718 return content_types[i].content_type;
722 /* No match by extension, default to html */
726 /*********************************************************************
728 * Function : cgi_send_user_manual
730 * Description : CGI function that sends a file in the user
734 * 1 : csp = Current client state (buffers, headers, etc...)
735 * 2 : rsp = http_response data structure for output
736 * 3 : parameters = map of cgi parameters
738 * CGI Parameters : file=name.html, the name of the HTML file
739 * (relative to user-manual from config)
741 * Returns : JB_ERR_OK on success
742 * JB_ERR_MEMORY on out-of-memory error.
744 *********************************************************************/
745 jb_err cgi_send_user_manual(struct client_state *csp,
746 struct http_response *rsp,
747 const struct map *parameters)
749 const char *filename;
751 jb_err err = JB_ERR_OK;
752 const char *content_type;
758 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
760 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
761 "received while user-manual delivery is disabled.");
762 return cgi_error_404(csp, rsp, parameters);
765 if (!parameters->first)
767 /* requested http://p.p/user-manual (without trailing slash) */
768 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
771 get_string_param(parameters, "file", &filename);
772 if (filename == NULL)
774 /* It's '/' so serve the index.html if there is one. */
775 filename = "index.html";
777 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
780 * We currently only support a flat file
781 * hierarchy for the documentation.
783 log_error(LOG_LEVEL_ERROR,
784 "Rejecting the request to serve '%s' as it contains '/' or '..'",
786 return JB_ERR_CGI_PARAMS;
789 full_path = make_path(csp->config->usermanual, filename);
790 if (full_path == NULL)
792 return JB_ERR_MEMORY;
795 err = load_file(full_path, &rsp->body, &rsp->content_length);
796 if (JB_ERR_OK != err)
798 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
799 if (JB_ERR_FILE == err)
801 err = cgi_error_no_template(csp, rsp, full_path);
808 content_type = get_content_type(filename);
809 log_error(LOG_LEVEL_CGI,
810 "Content-Type guessed for %s: %s", filename, content_type);
812 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
817 /*********************************************************************
819 * Function : cgi_show_version
821 * Description : CGI function that returns a a web page describing the
822 * file versions of Privoxy.
825 * 1 : csp = Current client state (buffers, headers, etc...)
826 * 2 : rsp = http_response data structure for output
827 * 3 : parameters = map of cgi parameters
829 * CGI Parameters : none
831 * Returns : JB_ERR_OK on success
832 * JB_ERR_MEMORY on out-of-memory error.
834 *********************************************************************/
835 jb_err cgi_show_version(struct client_state *csp,
836 struct http_response *rsp,
837 const struct map *parameters)
845 if (NULL == (exports = default_exports(csp, "show-version")))
847 return JB_ERR_MEMORY;
850 if (map(exports, "sourceversions", 1, show_rcs(), 0))
853 return JB_ERR_MEMORY;
856 return template_fill_for_cgi(csp, "show-version", exports, rsp);
860 /*********************************************************************
862 * Function : cgi_show_status
864 * Description : CGI function that returns a web page describing the
865 * current status of Privoxy.
868 * 1 : csp = Current client state (buffers, headers, etc...)
869 * 2 : rsp = http_response data structure for output
870 * 3 : parameters = map of cgi parameters
873 * file : Which file to show. Only first letter is checked,
878 * Default is to show menu and other information.
880 * Returns : JB_ERR_OK on success
881 * JB_ERR_MEMORY on out-of-memory error.
883 *********************************************************************/
884 jb_err cgi_show_status(struct client_state *csp,
885 struct http_response *rsp,
886 const struct map *parameters)
892 char buf[BUFFER_SIZE];
893 #ifdef FEATURE_STATISTICS
894 float perc_rej; /* Percentage of http requests rejected */
896 int local_urls_rejected;
897 #endif /* ndef FEATURE_STATISTICS */
898 jb_err err = JB_ERR_OK;
906 if ('\0' != *(lookup(parameters, "file")))
908 return cgi_show_file(csp, rsp, parameters);
911 if (NULL == (exports = default_exports(csp, "show-status")))
913 return JB_ERR_MEMORY;
917 for (j = 0; (s != NULL) && (j < Argc); j++)
919 if (!err) err = string_join (&s, html_encode(Argv[j]));
920 if (!err) err = string_append(&s, " ");
922 if (!err) err = map(exports, "invocation", 1, s, 0);
924 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
925 if (!err) err = show_defines(exports);
930 return JB_ERR_MEMORY;
933 #ifdef FEATURE_STATISTICS
934 local_urls_read = urls_read;
935 local_urls_rejected = urls_rejected;
938 * Need to alter the stats not to include the fetch of this
941 * Can't do following thread safely! doh!
944 * urls_rejected--; * This will be incremented subsequently *
947 if (local_urls_read == 0)
949 if (!err) err = map_block_killer(exports, "have-stats");
953 if (!err) err = map_block_killer(exports, "have-no-stats");
955 perc_rej = (float)local_urls_rejected * 100.0F /
956 (float)local_urls_read;
958 snprintf(buf, sizeof(buf), "%d", local_urls_read);
959 if (!err) err = map(exports, "requests-received", 1, buf, 1);
961 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
962 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
964 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
965 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
968 #else /* ndef FEATURE_STATISTICS */
969 err = err || map_block_killer(exports, "statistics");
970 #endif /* ndef FEATURE_STATISTICS */
973 * List all action files in use, together with view and edit links,
974 * except for standard.action, which should only be viewable. (Not
975 * enforced in the editor itself)
976 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
979 for (i = 0; i < MAX_AF_FILES; i++)
981 if (csp->actions_list[i] != NULL)
983 if (!err) err = string_append(&s, "<tr><td>");
984 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
985 snprintf(buf, sizeof(buf),
986 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
987 if (!err) err = string_append(&s, buf);
989 #ifdef FEATURE_CGI_EDIT_ACTIONS
990 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
991 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
992 && (NULL != csp->config->actions_file_short[i]))
995 if (access(csp->config->actions_file[i], W_OK) == 0)
997 #endif /* def HAVE_ACCESS */
998 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
999 if (!err) err = string_append(&s, buf);
1004 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1006 #endif /* def HAVE_ACCESS */
1010 if (!err) err = string_append(&s, "</td></tr>\n");
1015 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1019 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1023 * List all re_filterfiles in use, together with view options.
1024 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1027 for (i = 0; i < MAX_AF_FILES; i++)
1029 if (csp->rlist[i] != NULL)
1031 if (!err) err = string_append(&s, "<tr><td>");
1032 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1033 snprintf(buf, sizeof(buf),
1034 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1035 if (!err) err = string_append(&s, buf);
1036 if (!err) err = string_append(&s, "</td></tr>\n");
1041 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1045 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1046 if (!err) err = map_block_killer(exports, "have-filterfile");
1049 #ifdef FEATURE_TRUST
1052 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1056 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1057 if (!err) err = map_block_killer(exports, "have-trustfile");
1060 if (!err) err = map_block_killer(exports, "trust-support");
1061 #endif /* ndef FEATURE_TRUST */
1063 #ifdef FEATURE_CGI_EDIT_ACTIONS
1064 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1066 err = map_block_killer(exports, "cgi-editor-is-disabled");
1068 #endif /* ndef CGI_EDIT_ACTIONS */
1073 return JB_ERR_MEMORY;
1076 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1080 /*********************************************************************
1082 * Function : cgi_show_url_info
1084 * Description : CGI function that determines and shows which actions
1085 * Privoxy will perform for a given url, and which
1086 * matches starting from the defaults have lead to that.
1089 * 1 : csp = Current client state (buffers, headers, etc...)
1090 * 2 : rsp = http_response data structure for output
1091 * 3 : parameters = map of cgi parameters
1094 * url : The url whose actions are to be determined.
1095 * If url is unset, the url-given conditional will be
1096 * set, so that all but the form can be suppressed in
1099 * Returns : JB_ERR_OK on success
1100 * JB_ERR_MEMORY on out-of-memory error.
1102 *********************************************************************/
1103 jb_err cgi_show_url_info(struct client_state *csp,
1104 struct http_response *rsp,
1105 const struct map *parameters)
1108 struct map *exports;
1115 if (NULL == (exports = default_exports(csp, "show-url-info")))
1117 return JB_ERR_MEMORY;
1121 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1123 url_param = strdup(lookup(parameters, "url"));
1124 if (url_param == NULL)
1127 return JB_ERR_MEMORY;
1132 * Handle prefixes. 4 possibilities:
1133 * 1) "http://" or "https://" prefix present and followed by URL - OK
1134 * 2) Only the "http://" or "https://" part is present, no URL - change
1135 * to empty string so it will be detected later as "no URL".
1136 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1138 * 4) Parameter not specified or is empty string - let this fall through
1139 * for now, next block of code will handle it.
1141 if (0 == strncmp(url_param, "http://", 7))
1143 if (url_param[7] == '\0')
1146 * Empty URL (just prefix).
1147 * Make it totally empty so it's caught by the next if()
1149 url_param[0] = '\0';
1152 else if (0 == strncmp(url_param, "https://", 8))
1154 if (url_param[8] == '\0')
1157 * Empty URL (just prefix).
1158 * Make it totally empty so it's caught by the next if()
1160 url_param[0] = '\0';
1163 else if ((url_param[0] != '\0')
1164 && ((NULL == strstr(url_param, "://")
1165 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1168 * No prefix or at least no prefix before
1169 * the first slash - assume http://
1171 char *url_param_prefixed = strdup("http://");
1173 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1176 return JB_ERR_MEMORY;
1178 url_param = url_param_prefixed;
1182 * Hide "toggle off" warning if Privoxy is toggled on.
1185 #ifdef FEATURE_TOGGLE
1186 (global_toggle_state == 1) &&
1187 #endif /* def FEATURE_TOGGLE */
1188 map_block_killer(exports, "privoxy-is-toggled-off")
1192 return JB_ERR_MEMORY;
1195 if (url_param[0] == '\0')
1197 /* URL paramater not specified, display query form only. */
1199 if (map_block_killer(exports, "url-given")
1200 || map(exports, "url", 1, "", 1))
1203 return JB_ERR_MEMORY;
1208 /* Given a URL, so query it. */
1213 struct file_list *fl;
1214 struct url_actions *b;
1215 struct http_request url_to_query[1];
1216 struct current_action_spec action[1];
1219 if (map(exports, "url", 1, html_encode(url_param), 0))
1223 return JB_ERR_MEMORY;
1226 init_current_action(action);
1228 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1230 free_current_action(action);
1233 return JB_ERR_MEMORY;
1236 memset(url_to_query, '\0', sizeof(url_to_query));
1237 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1238 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1242 if (err == JB_ERR_MEMORY)
1244 free_http_request(url_to_query);
1245 free_current_action(action);
1247 return JB_ERR_MEMORY;
1253 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1254 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1255 if (!err) err = map_block_killer(exports, "valid-url");
1257 free_current_action(action);
1258 free_http_request(url_to_query);
1263 return JB_ERR_MEMORY;
1266 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1270 * We have a warning about SSL paths. Hide it for unencrypted sites.
1272 if (!url_to_query->ssl)
1274 if (map_block_killer(exports, "https"))
1276 free_current_action(action);
1278 free_http_request(url_to_query);
1279 return JB_ERR_MEMORY;
1283 matches = strdup("<table summary=\"\" class=\"transparent\">");
1285 for (i = 0; i < MAX_AF_FILES; i++)
1287 if (NULL == csp->config->actions_file_short[i]
1288 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1292 if ((fl = csp->actions_list[i]) != NULL)
1294 if ((b = fl->f) != NULL)
1296 /* FIXME: Hardcoded HTML! */
1297 string_append(&matches, "<tr><th>In file: ");
1298 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1299 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1300 string_append(&matches, buf);
1301 string_append(&matches, "View</a>");
1302 #ifdef FEATURE_CGI_EDIT_ACTIONS
1303 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1306 if (access(csp->config->actions_file[i], W_OK) == 0)
1308 #endif /* def HAVE_ACCESS */
1309 snprintf(buf, sizeof(buf),
1310 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1311 string_append(&matches, buf);
1312 string_append(&matches, "Edit</a>");
1317 string_append(&matches, " <strong>No write access.</strong>");
1319 #endif /* def HAVE_ACCESS */
1321 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1323 string_append(&matches, "</th></tr>\n");
1330 for (; (b != NULL) && (matches != NULL); b = b->next)
1332 if (url_match(b->url, url_to_query))
1334 string_append(&matches, "<tr><td>{");
1335 string_join (&matches, actions_to_html(csp, b->action));
1336 string_append(&matches, " }<br>\n<code>");
1337 string_join (&matches, html_encode(b->url->spec));
1338 string_append(&matches, "</code></td></tr>\n");
1340 if (merge_current_action(action, b->action))
1343 free_http_request(url_to_query);
1344 free_current_action(action);
1346 return JB_ERR_MEMORY;
1354 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1357 string_append(&matches, "</table>\n");
1360 * XXX: Kludge to make sure the "Forward settings" section
1361 * shows what forward-override{} would do with the requested URL.
1362 * No one really cares how the CGI request would be forwarded
1363 * if it wasn't intercepted as CGI request in the first place.
1365 * From here on the action bitmask will no longer reflect
1366 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1367 * but luckily it's no longer required later on anyway.
1369 free_current_action(csp->action);
1370 get_url_actions(csp, url_to_query);
1373 * Fill in forwarding settings.
1375 * The possibilities are:
1377 * - http forwarding only
1378 * - socks4(a) forwarding only
1379 * - socks4(a) and http forwarding.
1381 * XXX: Parts of this code could be reused for the
1382 * "forwarding-failed" template which currently doesn't
1383 * display the proxy port and an eventual second forwarder.
1386 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1388 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1390 if (!err) err = map_block_killer(exports, "socks-forwarder");
1391 if (!err) err = map_block_killer(exports, "http-forwarder");
1395 char port[10]; /* We save proxy ports as int but need a string here */
1397 if (!err) err = map_block_killer(exports, "no-forwarder");
1399 if (fwd->gateway_host != NULL)
1401 char *socks_type = NULL;
1406 socks_type = "socks4";
1409 socks_type = "socks4a";
1412 socks_type = "socks5";
1415 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1418 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1419 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1420 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1421 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1425 if (!err) err = map_block_killer(exports, "socks-forwarder");
1428 if (fwd->forward_host != NULL)
1430 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1431 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1432 if (!err) err = map(exports, "forward-port", 1, port, 1);
1436 if (!err) err = map_block_killer(exports, "http-forwarder");
1441 free_http_request(url_to_query);
1443 if (err || matches == NULL)
1445 free_current_action(action);
1447 return JB_ERR_MEMORY;
1450 #ifdef FEATURE_CGI_EDIT_ACTIONS
1451 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1453 err = map_block_killer(exports, "cgi-editor-is-disabled");
1455 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1458 * If zlib support is available, if no content filters
1459 * are enabled or if the prevent-compression action is enabled,
1460 * suppress the "compression could prevent filtering" warning.
1462 #ifndef FEATURE_ZLIB
1463 if (!content_filters_enabled(action) ||
1464 (action->flags & ACTION_NO_COMPRESSION))
1467 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1470 if (err || map(exports, "matches", 1, matches , 0))
1472 free_current_action(action);
1474 return JB_ERR_MEMORY;
1477 s = current_action_to_html(csp, action);
1479 free_current_action(action);
1481 if (map(exports, "final", 1, s, 0))
1484 return JB_ERR_MEMORY;
1488 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1492 /*********************************************************************
1494 * Function : cgi_robots_txt
1496 * Description : CGI function to return "/robots.txt".
1499 * 1 : csp = Current client state (buffers, headers, etc...)
1500 * 2 : rsp = http_response data structure for output
1501 * 3 : parameters = map of cgi parameters
1503 * CGI Parameters : None
1505 * Returns : JB_ERR_OK on success
1506 * JB_ERR_MEMORY on out-of-memory error.
1508 *********************************************************************/
1509 jb_err cgi_robots_txt(struct client_state *csp,
1510 struct http_response *rsp,
1511 const struct map *parameters)
1520 "# This is the Privoxy control interface.\n"
1521 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1527 if (rsp->body == NULL)
1529 return JB_ERR_MEMORY;
1532 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1536 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1537 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1539 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1543 /*********************************************************************
1545 * Function : show_defines
1547 * Description : Add to a map the state od all conditional #defines
1548 * used when building
1551 * 1 : exports = map to extend
1553 * Returns : JB_ERR_OK on success
1554 * JB_ERR_MEMORY on out-of-memory error.
1556 *********************************************************************/
1557 static jb_err show_defines(struct map *exports)
1559 jb_err err = JB_ERR_OK;
1561 #ifdef FEATURE_ACCEPT_FILTER
1562 if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 1);
1563 #else /* ifndef FEATURE_ACCEPT_FILTER */
1564 if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 0);
1565 #endif /* ndef FEATURE_ACCEPT_FILTER */
1568 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1569 #else /* ifndef FEATURE_ACL */
1570 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1571 #endif /* ndef FEATURE_ACL */
1573 #ifdef FEATURE_CGI_EDIT_ACTIONS
1574 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1575 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1576 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1577 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1579 #ifdef FEATURE_COMPRESSION
1580 if (!err) err = map_conditional(exports, "FEATURE_COMPRESSION", 1);
1581 #else /* ifndef FEATURE_COMPRESSION */
1582 if (!err) err = map_conditional(exports, "FEATURE_COMPRESSION", 0);
1583 #endif /* ndef FEATURE_COMPRESSION */
1585 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1586 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1587 #else /* ifndef FEATURE_CONNECTION_KEEP_ALIVE */
1588 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1589 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1591 #ifdef FEATURE_CONNECTION_SHARING
1592 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 1);
1593 #else /* ifndef FEATURE_CONNECTION_SHARING */
1594 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 0);
1595 #endif /* ndef FEATURE_CONNECTION_SHARING */
1597 #ifdef FEATURE_FAST_REDIRECTS
1598 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1599 #else /* ifndef FEATURE_FAST_REDIRECTS */
1600 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1601 #endif /* ndef FEATURE_FAST_REDIRECTS */
1603 #ifdef FEATURE_FORCE_LOAD
1604 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1605 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1606 #else /* ifndef FEATURE_FORCE_LOAD */
1607 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1608 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1609 #endif /* ndef FEATURE_FORCE_LOAD */
1611 #ifdef FEATURE_GRACEFUL_TERMINATION
1612 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1613 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1614 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1615 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1617 #ifdef FEATURE_IMAGE_BLOCKING
1618 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1619 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1620 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1621 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1623 #ifdef FEATURE_IMAGE_DETECT_MSIE
1624 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1625 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1626 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1627 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1630 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 1);
1631 #else /* ifndef HAVE_RFC2553 */
1632 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 0);
1633 #endif /* ndef HAVE_RFC2553 */
1635 #ifdef FEATURE_NO_GIFS
1636 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1637 #else /* ifndef FEATURE_NO_GIFS */
1638 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1639 #endif /* ndef FEATURE_NO_GIFS */
1641 #ifdef FEATURE_PTHREAD
1642 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1643 #else /* ifndef FEATURE_PTHREAD */
1644 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1645 #endif /* ndef FEATURE_PTHREAD */
1647 #ifdef FEATURE_STATISTICS
1648 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1649 #else /* ifndef FEATURE_STATISTICS */
1650 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1651 #endif /* ndef FEATURE_STATISTICS */
1653 #ifdef FEATURE_TOGGLE
1654 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1655 #else /* ifndef FEATURE_TOGGLE */
1656 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1657 #endif /* ndef FEATURE_TOGGLE */
1659 #ifdef FEATURE_TRUST
1660 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1661 #else /* ifndef FEATURE_TRUST */
1662 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1663 #endif /* ndef FEATURE_TRUST */
1666 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1667 #else /* ifndef FEATURE_ZLIB */
1668 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1669 #endif /* ndef FEATURE_ZLIB */
1672 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1673 #else /* ifndef STATIC_PCRE */
1674 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1675 #endif /* ndef STATIC_PCRE */
1678 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1679 #else /* ifndef STATIC_PCRS */
1680 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1681 #endif /* ndef STATIC_PCRS */
1687 /*********************************************************************
1689 * Function : show_rcs
1691 * Description : Create a string with the rcs info for all sourcefiles
1695 * Returns : A string, or NULL on out-of-memory.
1697 *********************************************************************/
1698 static char *show_rcs(void)
1700 char *result = strdup("");
1701 char buf[BUFFER_SIZE];
1703 /* Instead of including *all* dot h's in the project (thus creating a
1704 * tremendous amount of dependencies), I will concede to declaring them
1705 * as extern's. This forces the developer to add to this list, but oh well.
1708 #define SHOW_RCS(__x) \
1710 extern const char __x[]; \
1711 snprintf(buf, sizeof(buf), " %s\n", __x); \
1712 string_append(&result, buf); \
1715 /* In alphabetical order */
1716 SHOW_RCS(actions_h_rcs)
1717 SHOW_RCS(actions_rcs)
1719 SHOW_RCS(amiga_h_rcs)
1721 #endif /* def AMIGA */
1724 #ifdef FEATURE_CGI_EDIT_ACTIONS
1725 SHOW_RCS(cgiedit_h_rcs)
1726 SHOW_RCS(cgiedit_rcs)
1727 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1728 SHOW_RCS(cgisimple_h_rcs)
1729 SHOW_RCS(cgisimple_rcs)
1731 SHOW_RCS(cygwin_h_rcs)
1733 SHOW_RCS(deanimate_h_rcs)
1734 SHOW_RCS(deanimate_rcs)
1735 SHOW_RCS(encode_h_rcs)
1736 SHOW_RCS(encode_rcs)
1737 SHOW_RCS(errlog_h_rcs)
1738 SHOW_RCS(errlog_rcs)
1739 SHOW_RCS(filters_h_rcs)
1740 SHOW_RCS(filters_rcs)
1741 SHOW_RCS(gateway_h_rcs)
1742 SHOW_RCS(gateway_rcs)
1743 SHOW_RCS(jbsockets_h_rcs)
1744 SHOW_RCS(jbsockets_rcs)
1747 SHOW_RCS(list_h_rcs)
1749 SHOW_RCS(loadcfg_h_rcs)
1750 SHOW_RCS(loadcfg_rcs)
1751 SHOW_RCS(loaders_h_rcs)
1752 SHOW_RCS(loaders_rcs)
1753 SHOW_RCS(miscutil_h_rcs)
1754 SHOW_RCS(miscutil_rcs)
1755 SHOW_RCS(parsers_h_rcs)
1756 SHOW_RCS(parsers_rcs)
1758 SHOW_RCS(pcrs_h_rcs)
1759 SHOW_RCS(project_h_rcs)
1760 SHOW_RCS(ssplit_h_rcs)
1761 SHOW_RCS(ssplit_rcs)
1762 SHOW_RCS(urlmatch_h_rcs)
1763 SHOW_RCS(urlmatch_rcs)
1765 #ifndef _WIN_CONSOLE
1766 SHOW_RCS(w32log_h_rcs)
1767 SHOW_RCS(w32log_rcs)
1768 SHOW_RCS(w32res_h_rcs)
1769 SHOW_RCS(w32taskbar_h_rcs)
1770 SHOW_RCS(w32taskbar_rcs)
1771 #endif /* ndef _WIN_CONSOLE */
1772 SHOW_RCS(win32_h_rcs)
1774 #endif /* def _WIN32 */
1783 /*********************************************************************
1785 * Function : cgi_show_file
1787 * Description : CGI function that shows the content of a
1788 * configuration file.
1791 * 1 : csp = Current client state (buffers, headers, etc...)
1792 * 2 : rsp = http_response data structure for output
1793 * 3 : parameters = map of cgi parameters
1796 * file : Which file to show. Only first letter is checked,
1801 * Default is to show menu and other information.
1803 * Returns : JB_ERR_OK on success
1804 * JB_ERR_MEMORY on out-of-memory error.
1806 *********************************************************************/
1807 static jb_err cgi_show_file(struct client_state *csp,
1808 struct http_response *rsp,
1809 const struct map *parameters)
1812 const char * filename = NULL;
1813 char * file_description = NULL;
1819 switch (*(lookup(parameters, "file")))
1822 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1824 filename = csp->actions_list[i]->filename;
1825 file_description = "Actions File";
1830 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1832 filename = csp->rlist[i]->filename;
1833 file_description = "Filter File";
1837 #ifdef FEATURE_TRUST
1841 filename = csp->tlist->filename;
1842 file_description = "Trust File";
1845 #endif /* def FEATURE_TRUST */
1848 if (NULL != filename)
1850 struct map *exports;
1855 exports = default_exports(csp, "show-status");
1856 if (NULL == exports)
1858 return JB_ERR_MEMORY;
1861 if ( map(exports, "file-description", 1, file_description, 1)
1862 || map(exports, "filepath", 1, html_encode(filename), 0) )
1865 return JB_ERR_MEMORY;
1868 err = load_file(filename, &s, &length);
1869 if (JB_ERR_OK != err)
1871 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1874 return JB_ERR_MEMORY;
1879 s = html_encode_and_free_original(s);
1882 return JB_ERR_MEMORY;
1885 if (map(exports, "contents", 1, s, 0))
1888 return JB_ERR_MEMORY;
1892 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1895 return JB_ERR_CGI_PARAMS;
1899 /*********************************************************************
1901 * Function : load_file
1903 * Description : Loads a file into a buffer.
1906 * 1 : filename = Name of the file to be loaded.
1907 * 2 : buffer = Used to return the file's content.
1908 * 3 : length = Used to return the size of the file.
1910 * Returns : JB_ERR_OK in case of success,
1911 * JB_ERR_FILE in case of ordinary file loading errors
1912 * (fseek() and ftell() errors are fatal)
1913 * JB_ERR_MEMORY in case of out-of-memory.
1915 *********************************************************************/
1916 static jb_err load_file(const char *filename, char **buffer, size_t *length)
1920 jb_err err = JB_ERR_OK;
1922 fp = fopen(filename, "rb");
1925 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
1929 /* Get file length */
1930 if (fseek(fp, 0, SEEK_END))
1932 log_error(LOG_LEVEL_FATAL,
1933 "Unexpected error while fseek()ing to the end of %s: %E",
1939 log_error(LOG_LEVEL_FATAL,
1940 "Unexpected ftell() error while loading %s: %E",
1943 *length = (size_t)ret;
1945 /* Go back to the beginning. */
1946 if (fseek(fp, 0, SEEK_SET))
1948 log_error(LOG_LEVEL_FATAL,
1949 "Unexpected error while fseek()ing to the beginning of %s: %E",
1953 *buffer = (char *)zalloc(*length + 1);
1954 if (NULL == *buffer)
1956 err = JB_ERR_MEMORY;
1958 else if (!fread(*buffer, *length, 1, fp))
1961 * May happen if the file size changes between fseek() and
1962 * fread(). If it does, we just log it and serve what we got.
1964 log_error(LOG_LEVEL_ERROR,
1965 "Couldn't completely read file %s.", filename);