1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.103 2011/02/14 16:06:37 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)
192 * I don't really care what gets sent back to the browser.
193 * Take the easy option - "out of memory" page.
196 return JB_ERR_MEMORY;
198 #endif /* def FEATURE_GRACEFUL_TERMINATION */
201 /*********************************************************************
203 * Function : cgi_show_request
205 * Description : Show the client's request and what sed() would have
209 * 1 : csp = Current client state (buffers, headers, etc...)
210 * 2 : rsp = http_response data structure for output
211 * 3 : parameters = map of cgi parameters
213 * CGI Parameters : none
215 * Returns : JB_ERR_OK on success
216 * JB_ERR_MEMORY on out-of-memory error.
218 *********************************************************************/
219 jb_err cgi_show_request(struct client_state *csp,
220 struct http_response *rsp,
221 const struct map *parameters)
230 if (NULL == (exports = default_exports(csp, "show-request")))
232 return JB_ERR_MEMORY;
236 * Repair the damage done to the IOB by get_header()
238 for (p = csp->iob->buf; p < csp->iob->eod; p++)
240 if (*p == '\0') *p = '\n';
244 * Export the original client's request and the one we would
245 * be sending to the server if this wasn't a CGI call
248 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
251 return JB_ERR_MEMORY;
254 if (map(exports, "processed-request", 1,
255 html_encode_and_free_original(list_to_text(csp->headers)), 0))
258 return JB_ERR_MEMORY;
261 return template_fill_for_cgi(csp, "show-request", exports, rsp);
265 /*********************************************************************
267 * Function : cgi_send_banner
269 * Description : CGI function that returns a banner.
272 * 1 : csp = Current client state (buffers, headers, etc...)
273 * 2 : rsp = http_response data structure for output
274 * 3 : parameters = map of cgi parameters
277 * type : Selects the type of banner between "trans", "logo",
278 * and "auto". Defaults to "logo" if absent or invalid.
279 * "auto" means to select as if we were image-blocking.
280 * (Only the first character really counts; b and t are
283 * Returns : JB_ERR_OK on success
284 * JB_ERR_MEMORY on out-of-memory error.
286 *********************************************************************/
287 jb_err cgi_send_banner(struct client_state *csp,
288 struct http_response *rsp,
289 const struct map *parameters)
291 char imagetype = lookup(parameters, "type")[0];
294 * If type is auto, then determine the right thing
295 * to do from the set-image-blocker action
297 if (imagetype == 'a')
304 #ifdef FEATURE_IMAGE_BLOCKING
305 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
307 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
308 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
309 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
313 /* Use default - nothing to do here. */
315 else if (0 == strcmpic(p, "blank"))
319 else if (0 == strcmpic(p, "pattern"))
325 * If the action is to call this CGI, determine
328 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
330 imagetype = p[sizeof(prefix1) - 1];
332 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
334 imagetype = p[sizeof(prefix2) - 1];
338 * Everything else must (should) be a URL to
346 #endif /* def FEATURE_IMAGE_BLOCKING */
350 * Now imagetype is either the non-auto type we were called with,
351 * or it was auto and has since been determined. In any case, we
352 * can proceed to actually answering the request by sending a redirect
353 * or an image as appropriate:
355 if (imagetype == 'r')
357 rsp->status = strdup("302 Local Redirect from Privoxy");
358 if (rsp->status == NULL)
360 return JB_ERR_MEMORY;
362 if (enlist_unique_header(rsp->headers, "Location",
363 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
365 return JB_ERR_MEMORY;
370 if ((imagetype == 'b') || (imagetype == 't'))
372 rsp->body = bindup(image_blank_data, image_blank_length);
373 rsp->content_length = image_blank_length;
377 rsp->body = bindup(image_pattern_data, image_pattern_length);
378 rsp->content_length = image_pattern_length;
381 if (rsp->body == NULL)
383 return JB_ERR_MEMORY;
385 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
387 return JB_ERR_MEMORY;
398 /*********************************************************************
400 * Function : cgi_transparent_image
402 * Description : CGI function that sends a 1x1 transparent image.
405 * 1 : csp = Current client state (buffers, headers, etc...)
406 * 2 : rsp = http_response data structure for output
407 * 3 : parameters = map of cgi parameters
409 * CGI Parameters : None
411 * Returns : JB_ERR_OK on success
412 * JB_ERR_MEMORY on out-of-memory error.
414 *********************************************************************/
415 jb_err cgi_transparent_image(struct client_state *csp,
416 struct http_response *rsp,
417 const struct map *parameters)
422 rsp->body = bindup(image_blank_data, image_blank_length);
423 rsp->content_length = image_blank_length;
425 if (rsp->body == NULL)
427 return JB_ERR_MEMORY;
430 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
432 return JB_ERR_MEMORY;
442 /*********************************************************************
444 * Function : cgi_send_default_favicon
446 * Description : CGI function that sends the standard favicon.
449 * 1 : csp = Current client state (buffers, headers, etc...)
450 * 2 : rsp = http_response data structure for output
451 * 3 : parameters = map of cgi parameters
453 * CGI Parameters : None
455 * Returns : JB_ERR_OK on success
456 * JB_ERR_MEMORY on out-of-memory error.
458 *********************************************************************/
459 jb_err cgi_send_default_favicon(struct client_state *csp,
460 struct http_response *rsp,
461 const struct map *parameters)
463 static const char default_favicon_data[] =
464 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
465 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
466 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
467 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
468 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
469 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
470 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
471 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
472 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
473 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
474 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
475 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
476 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
478 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
483 rsp->body = bindup(default_favicon_data, favicon_length);
484 rsp->content_length = favicon_length;
486 if (rsp->body == NULL)
488 return JB_ERR_MEMORY;
491 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
493 return JB_ERR_MEMORY;
503 /*********************************************************************
505 * Function : cgi_send_error_favicon
507 * Description : CGI function that sends the favicon for error pages.
510 * 1 : csp = Current client state (buffers, headers, etc...)
511 * 2 : rsp = http_response data structure for output
512 * 3 : parameters = map of cgi parameters
514 * CGI Parameters : None
516 * Returns : JB_ERR_OK on success
517 * JB_ERR_MEMORY on out-of-memory error.
519 *********************************************************************/
520 jb_err cgi_send_error_favicon(struct client_state *csp,
521 struct http_response *rsp,
522 const struct map *parameters)
524 static const char error_favicon_data[] =
525 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
526 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
527 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
528 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
529 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
530 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
531 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
532 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
533 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
534 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
535 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
536 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
537 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
539 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
544 rsp->body = bindup(error_favicon_data, favicon_length);
545 rsp->content_length = favicon_length;
547 if (rsp->body == NULL)
549 return JB_ERR_MEMORY;
552 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
554 return JB_ERR_MEMORY;
564 /*********************************************************************
566 * Function : cgi_send_stylesheet
568 * Description : CGI function that sends a css stylesheet found
569 * in the cgi-style.css template
572 * 1 : csp = Current client state (buffers, headers, etc...)
573 * 2 : rsp = http_response data structure for output
574 * 3 : parameters = map of cgi parameters
576 * CGI Parameters : None
578 * Returns : JB_ERR_OK on success
579 * JB_ERR_MEMORY on out-of-memory error.
581 *********************************************************************/
582 jb_err cgi_send_stylesheet(struct client_state *csp,
583 struct http_response *rsp,
584 const struct map *parameters)
593 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
595 if (err == JB_ERR_FILE)
598 * No way to tell user; send empty stylesheet
600 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
604 return err; /* JB_ERR_MEMORY */
607 if (enlist(rsp->headers, "Content-Type: text/css"))
609 return JB_ERR_MEMORY;
617 /*********************************************************************
619 * Function : cgi_send_url_info_osd
621 * Description : CGI function that sends the OpenSearch Description
622 * template for the show-url-info page. It allows to
623 * access the page through "search engine plugins".
626 * 1 : csp = Current client state (buffers, headers, etc...)
627 * 2 : rsp = http_response data structure for output
628 * 3 : parameters = map of cgi parameters
630 * CGI Parameters : None
632 * Returns : JB_ERR_OK on success
633 * JB_ERR_MEMORY on out-of-memory error.
635 *********************************************************************/
636 jb_err cgi_send_url_info_osd(struct client_state *csp,
637 struct http_response *rsp,
638 const struct map *parameters)
640 jb_err err = JB_ERR_MEMORY;
641 struct map *exports = default_exports(csp, NULL);
648 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
649 if (JB_ERR_OK == err)
651 err = enlist(rsp->headers,
652 "Content-Type: application/opensearchdescription+xml");
661 /*********************************************************************
663 * Function : get_content_type
665 * Description : Use the file extension to guess the content type
666 * header we should use to serve the file.
669 * 1 : filename = Name of the file whose content type
672 * Returns : The guessed content type.
674 *********************************************************************/
675 static const char *get_content_type(const char *filename)
680 const char *extension;
681 const char *content_type;
683 static const struct content_type content_types[] =
685 {".css", "text/css"},
686 {".jpg", "image/jpeg"},
689 for (i = 0; i < SZ(content_types); i++)
691 if (strstr(filename, content_types[i].extension))
693 return content_types[i].content_type;
697 /* No match by extension, default to html */
701 /*********************************************************************
703 * Function : cgi_send_user_manual
705 * Description : CGI function that sends a file in the user
709 * 1 : csp = Current client state (buffers, headers, etc...)
710 * 2 : rsp = http_response data structure for output
711 * 3 : parameters = map of cgi parameters
713 * CGI Parameters : file=name.html, the name of the HTML file
714 * (relative to user-manual from config)
716 * Returns : JB_ERR_OK on success
717 * JB_ERR_MEMORY on out-of-memory error.
719 *********************************************************************/
720 jb_err cgi_send_user_manual(struct client_state *csp,
721 struct http_response *rsp,
722 const struct map *parameters)
724 const char *filename;
726 jb_err err = JB_ERR_OK;
727 const char *content_type;
733 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
735 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
736 "received while user-manual delivery is disabled.");
737 return cgi_error_404(csp, rsp, parameters);
740 if (!parameters->first)
742 /* requested http://p.p/user-manual (without trailing slash) */
743 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
746 get_string_param(parameters, "file", &filename);
747 if (filename == NULL)
749 /* It's '/' so serve the index.html if there is one. */
750 filename = "index.html";
752 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
755 * We currently only support a flat file
756 * hierachy for the documentation.
758 log_error(LOG_LEVEL_ERROR,
759 "Rejecting the request to serve '%s' as it contains '/' or '..'",
761 return JB_ERR_CGI_PARAMS;
764 full_path = make_path(csp->config->usermanual, filename);
765 if (full_path == NULL)
767 return JB_ERR_MEMORY;
770 err = load_file(full_path, &rsp->body, &rsp->content_length);
771 if (JB_ERR_OK != err)
773 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
774 if (JB_ERR_FILE == err)
776 err = cgi_error_no_template(csp, rsp, full_path);
783 content_type = get_content_type(filename);
784 log_error(LOG_LEVEL_CGI,
785 "Content-Type guessed for %s: %s", filename, content_type);
787 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
792 /*********************************************************************
794 * Function : cgi_show_version
796 * Description : CGI function that returns a a web page describing the
797 * file versions of Privoxy.
800 * 1 : csp = Current client state (buffers, headers, etc...)
801 * 2 : rsp = http_response data structure for output
802 * 3 : parameters = map of cgi parameters
804 * CGI Parameters : none
806 * Returns : JB_ERR_OK on success
807 * JB_ERR_MEMORY on out-of-memory error.
809 *********************************************************************/
810 jb_err cgi_show_version(struct client_state *csp,
811 struct http_response *rsp,
812 const struct map *parameters)
820 if (NULL == (exports = default_exports(csp, "show-version")))
822 return JB_ERR_MEMORY;
825 if (map(exports, "sourceversions", 1, show_rcs(), 0))
828 return JB_ERR_MEMORY;
831 return template_fill_for_cgi(csp, "show-version", exports, rsp);
835 /*********************************************************************
837 * Function : cgi_show_status
839 * Description : CGI function that returns a web page describing the
840 * current status of Privoxy.
843 * 1 : csp = Current client state (buffers, headers, etc...)
844 * 2 : rsp = http_response data structure for output
845 * 3 : parameters = map of cgi parameters
848 * file : Which file to show. Only first letter is checked,
853 * Default is to show menu and other information.
855 * Returns : JB_ERR_OK on success
856 * JB_ERR_MEMORY on out-of-memory error.
858 *********************************************************************/
859 jb_err cgi_show_status(struct client_state *csp,
860 struct http_response *rsp,
861 const struct map *parameters)
867 char buf[BUFFER_SIZE];
868 #ifdef FEATURE_STATISTICS
869 float perc_rej; /* Percentage of http requests rejected */
871 int local_urls_rejected;
872 #endif /* ndef FEATURE_STATISTICS */
873 jb_err err = JB_ERR_OK;
881 if ('\0' != *(lookup(parameters, "file")))
883 return cgi_show_file(csp, rsp, parameters);
886 if (NULL == (exports = default_exports(csp, "show-status")))
888 return JB_ERR_MEMORY;
892 for (j = 0; (s != NULL) && (j < Argc); j++)
894 if (!err) err = string_join (&s, html_encode(Argv[j]));
895 if (!err) err = string_append(&s, " ");
897 if (!err) err = map(exports, "invocation", 1, s, 0);
899 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
900 if (!err) err = show_defines(exports);
905 return JB_ERR_MEMORY;
908 #ifdef FEATURE_STATISTICS
909 local_urls_read = urls_read;
910 local_urls_rejected = urls_rejected;
913 * Need to alter the stats not to include the fetch of this
916 * Can't do following thread safely! doh!
919 * urls_rejected--; * This will be incremented subsequently *
922 if (local_urls_read == 0)
924 if (!err) err = map_block_killer(exports, "have-stats");
928 if (!err) err = map_block_killer(exports, "have-no-stats");
930 perc_rej = (float)local_urls_rejected * 100.0F /
931 (float)local_urls_read;
933 snprintf(buf, sizeof(buf), "%d", local_urls_read);
934 if (!err) err = map(exports, "requests-received", 1, buf, 1);
936 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
937 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
939 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
940 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
943 #else /* ndef FEATURE_STATISTICS */
944 err = err || map_block_killer(exports, "statistics");
945 #endif /* ndef FEATURE_STATISTICS */
948 * List all action files in use, together with view and edit links,
949 * except for standard.action, which should only be viewable. (Not
950 * enforced in the editor itself)
951 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
954 for (i = 0; i < MAX_AF_FILES; i++)
956 if (csp->actions_list[i] != NULL)
958 if (!err) err = string_append(&s, "<tr><td>");
959 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
960 snprintf(buf, sizeof(buf),
961 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
962 if (!err) err = string_append(&s, buf);
964 #ifdef FEATURE_CGI_EDIT_ACTIONS
965 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
966 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
967 && (NULL != csp->config->actions_file_short[i]))
970 if (access(csp->config->actions_file[i], W_OK) == 0)
972 #endif /* def HAVE_ACCESS */
973 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
974 if (!err) err = string_append(&s, buf);
979 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
981 #endif /* def HAVE_ACCESS */
985 if (!err) err = string_append(&s, "</td></tr>\n");
990 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
994 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
998 * List all re_filterfiles in use, together with view options.
999 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1002 for (i = 0; i < MAX_AF_FILES; i++)
1004 if (csp->rlist[i] != NULL)
1006 if (!err) err = string_append(&s, "<tr><td>");
1007 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1008 snprintf(buf, sizeof(buf),
1009 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1010 if (!err) err = string_append(&s, buf);
1011 if (!err) err = string_append(&s, "</td></tr>\n");
1016 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1020 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1021 if (!err) err = map_block_killer(exports, "have-filterfile");
1024 #ifdef FEATURE_TRUST
1027 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1031 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1032 if (!err) err = map_block_killer(exports, "have-trustfile");
1035 if (!err) err = map_block_killer(exports, "trust-support");
1036 #endif /* ndef FEATURE_TRUST */
1038 #ifdef FEATURE_CGI_EDIT_ACTIONS
1039 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1041 err = map_block_killer(exports, "cgi-editor-is-disabled");
1043 #endif /* ndef CGI_EDIT_ACTIONS */
1048 return JB_ERR_MEMORY;
1051 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1055 /*********************************************************************
1057 * Function : cgi_show_url_info
1059 * Description : CGI function that determines and shows which actions
1060 * Privoxy will perform for a given url, and which
1061 * matches starting from the defaults have lead to that.
1064 * 1 : csp = Current client state (buffers, headers, etc...)
1065 * 2 : rsp = http_response data structure for output
1066 * 3 : parameters = map of cgi parameters
1069 * url : The url whose actions are to be determined.
1070 * If url is unset, the url-given conditional will be
1071 * set, so that all but the form can be suppressed in
1074 * Returns : JB_ERR_OK on success
1075 * JB_ERR_MEMORY on out-of-memory error.
1077 *********************************************************************/
1078 jb_err cgi_show_url_info(struct client_state *csp,
1079 struct http_response *rsp,
1080 const struct map *parameters)
1083 struct map *exports;
1090 if (NULL == (exports = default_exports(csp, "show-url-info")))
1092 return JB_ERR_MEMORY;
1096 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1098 url_param = strdup(lookup(parameters, "url"));
1099 if (url_param == NULL)
1102 return JB_ERR_MEMORY;
1107 * Handle prefixes. 4 possibilities:
1108 * 1) "http://" or "https://" prefix present and followed by URL - OK
1109 * 2) Only the "http://" or "https://" part is present, no URL - change
1110 * to empty string so it will be detected later as "no URL".
1111 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1113 * 4) Parameter not specified or is empty string - let this fall through
1114 * for now, next block of code will handle it.
1116 if (0 == strncmp(url_param, "http://", 7))
1118 if (url_param[7] == '\0')
1121 * Empty URL (just prefix).
1122 * Make it totally empty so it's caught by the next if()
1124 url_param[0] = '\0';
1127 else if (0 == strncmp(url_param, "https://", 8))
1129 if (url_param[8] == '\0')
1132 * Empty URL (just prefix).
1133 * Make it totally empty so it's caught by the next if()
1135 url_param[0] = '\0';
1138 else if ((url_param[0] != '\0')
1139 && ((NULL == strstr(url_param, "://")
1140 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1143 * No prefix or at least no prefix before
1144 * the first slash - assume http://
1146 char *url_param_prefixed = strdup("http://");
1148 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1151 return JB_ERR_MEMORY;
1153 url_param = url_param_prefixed;
1157 * Hide "toggle off" warning if Privoxy is toggled on.
1160 #ifdef FEATURE_TOGGLE
1161 (global_toggle_state == 1) &&
1162 #endif /* def FEATURE_TOGGLE */
1163 map_block_killer(exports, "privoxy-is-toggled-off")
1167 return JB_ERR_MEMORY;
1170 if (url_param[0] == '\0')
1172 /* URL paramater not specified, display query form only. */
1174 if (map_block_killer(exports, "url-given")
1175 || map(exports, "url", 1, "", 1))
1178 return JB_ERR_MEMORY;
1183 /* Given a URL, so query it. */
1188 struct file_list *fl;
1189 struct url_actions *b;
1190 struct http_request url_to_query[1];
1191 struct current_action_spec action[1];
1194 if (map(exports, "url", 1, html_encode(url_param), 0))
1198 return JB_ERR_MEMORY;
1201 init_current_action(action);
1203 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1205 free_current_action(action);
1208 return JB_ERR_MEMORY;
1211 memset(url_to_query, '\0', sizeof(url_to_query));
1212 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1213 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1217 if (err == JB_ERR_MEMORY)
1219 free_http_request(url_to_query);
1220 free_current_action(action);
1222 return JB_ERR_MEMORY;
1228 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1229 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1230 if (!err) err = map_block_killer(exports, "valid-url");
1232 free_current_action(action);
1233 free_http_request(url_to_query);
1238 return JB_ERR_MEMORY;
1241 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1245 * We have a warning about SSL paths. Hide it for unencrypted sites.
1247 if (!url_to_query->ssl)
1249 if (map_block_killer(exports, "https"))
1251 free_current_action(action);
1253 free_http_request(url_to_query);
1254 return JB_ERR_MEMORY;
1258 matches = strdup("<table summary=\"\" class=\"transparent\">");
1260 for (i = 0; i < MAX_AF_FILES; i++)
1262 if (NULL == csp->config->actions_file_short[i]
1263 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1267 if ((fl = csp->actions_list[i]) != NULL)
1269 if ((b = fl->f) != NULL)
1271 /* FIXME: Hardcoded HTML! */
1272 string_append(&matches, "<tr><th>In file: ");
1273 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1274 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1275 string_append(&matches, buf);
1276 string_append(&matches, "View</a>");
1277 #ifdef FEATURE_CGI_EDIT_ACTIONS
1278 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1281 if (access(csp->config->actions_file[i], W_OK) == 0)
1283 #endif /* def HAVE_ACCESS */
1284 snprintf(buf, sizeof(buf),
1285 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1286 string_append(&matches, buf);
1287 string_append(&matches, "Edit</a>");
1292 string_append(&matches, " <strong>No write access.</strong>");
1294 #endif /* def HAVE_ACCESS */
1296 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1298 string_append(&matches, "</th></tr>\n");
1305 for (; (b != NULL) && (matches != NULL); b = b->next)
1307 if (url_match(b->url, url_to_query))
1309 string_append(&matches, "<tr><td>{");
1310 string_join (&matches, actions_to_html(csp, b->action));
1311 string_append(&matches, " }<br>\n<code>");
1312 string_join (&matches, html_encode(b->url->spec));
1313 string_append(&matches, "</code></td></tr>\n");
1315 if (merge_current_action(action, b->action))
1318 free_http_request(url_to_query);
1319 free_current_action(action);
1321 return JB_ERR_MEMORY;
1329 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1332 string_append(&matches, "</table>\n");
1335 * XXX: Kludge to make sure the "Forward settings" section
1336 * shows what forward-override{} would do with the requested URL.
1337 * No one really cares how the CGI request would be forwarded
1338 * if it wasn't intercepted as CGI request in the first place.
1340 * From here on the action bitmask will no longer reflect
1341 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1342 * but luckily it's no longer required later on anyway.
1344 free_current_action(csp->action);
1345 get_url_actions(csp, url_to_query);
1348 * Fill in forwarding settings.
1350 * The possibilities are:
1352 * - http forwarding only
1353 * - socks4(a) forwarding only
1354 * - socks4(a) and http forwarding.
1356 * XXX: Parts of this code could be reused for the
1357 * "forwarding-failed" template which currently doesn't
1358 * display the proxy port and an eventual second forwarder.
1361 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1363 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1365 if (!err) err = map_block_killer(exports, "socks-forwarder");
1366 if (!err) err = map_block_killer(exports, "http-forwarder");
1370 char port[10]; /* We save proxy ports as int but need a string here */
1372 if (!err) err = map_block_killer(exports, "no-forwarder");
1374 if (fwd->gateway_host != NULL)
1376 char *socks_type = NULL;
1381 socks_type = "socks4";
1384 socks_type = "socks4a";
1387 socks_type = "socks5";
1390 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1393 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1394 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1395 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1396 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1400 if (!err) err = map_block_killer(exports, "socks-forwarder");
1403 if (fwd->forward_host != NULL)
1405 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1406 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1407 if (!err) err = map(exports, "forward-port", 1, port, 1);
1411 if (!err) err = map_block_killer(exports, "http-forwarder");
1416 free_http_request(url_to_query);
1418 if (err || matches == NULL)
1420 free_current_action(action);
1422 return JB_ERR_MEMORY;
1425 #ifdef FEATURE_CGI_EDIT_ACTIONS
1426 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1428 err = map_block_killer(exports, "cgi-editor-is-disabled");
1430 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1433 * If zlib support is available, if no content filters
1434 * are enabled or if the prevent-compression action is enabled,
1435 * suppress the "compression could prevent filtering" warning.
1437 #ifndef FEATURE_ZLIB
1438 if (!content_filters_enabled(action) ||
1439 (action->flags & ACTION_NO_COMPRESSION))
1442 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1445 if (err || map(exports, "matches", 1, matches , 0))
1447 free_current_action(action);
1449 return JB_ERR_MEMORY;
1452 s = current_action_to_html(csp, action);
1454 free_current_action(action);
1456 if (map(exports, "final", 1, s, 0))
1459 return JB_ERR_MEMORY;
1463 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1467 /*********************************************************************
1469 * Function : cgi_robots_txt
1471 * Description : CGI function to return "/robots.txt".
1474 * 1 : csp = Current client state (buffers, headers, etc...)
1475 * 2 : rsp = http_response data structure for output
1476 * 3 : parameters = map of cgi parameters
1478 * CGI Parameters : None
1480 * Returns : JB_ERR_OK on success
1481 * JB_ERR_MEMORY on out-of-memory error.
1483 *********************************************************************/
1484 jb_err cgi_robots_txt(struct client_state *csp,
1485 struct http_response *rsp,
1486 const struct map *parameters)
1495 "# This is the Privoxy control interface.\n"
1496 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1502 if (rsp->body == NULL)
1504 return JB_ERR_MEMORY;
1507 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1511 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1512 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1514 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1518 /*********************************************************************
1520 * Function : show_defines
1522 * Description : Add to a map the state od all conditional #defines
1523 * used when building
1526 * 1 : exports = map to extend
1528 * Returns : JB_ERR_OK on success
1529 * JB_ERR_MEMORY on out-of-memory error.
1531 *********************************************************************/
1532 static jb_err show_defines(struct map *exports)
1534 jb_err err = JB_ERR_OK;
1536 #ifdef FEATURE_ACCEPT_FILTER
1537 if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 1);
1538 #else /* ifndef FEATURE_ACCEPT_FILTER */
1539 if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 0);
1540 #endif /* ndef FEATURE_ACCEPT_FILTER */
1543 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1544 #else /* ifndef FEATURE_ACL */
1545 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1546 #endif /* ndef FEATURE_ACL */
1548 #ifdef FEATURE_CGI_EDIT_ACTIONS
1549 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1550 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1551 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1552 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1554 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1555 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1556 #else /* ifndef FEATURE_CONNECTION_KEEP_ALIVE */
1557 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1558 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1560 #ifdef FEATURE_CONNECTION_SHARING
1561 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 1);
1562 #else /* ifndef FEATURE_CONNECTION_SHARING */
1563 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 0);
1564 #endif /* ndef FEATURE_CONNECTION_SHARING */
1566 #ifdef FEATURE_FAST_REDIRECTS
1567 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1568 #else /* ifndef FEATURE_FAST_REDIRECTS */
1569 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1570 #endif /* ndef FEATURE_FAST_REDIRECTS */
1572 #ifdef FEATURE_FORCE_LOAD
1573 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1574 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1575 #else /* ifndef FEATURE_FORCE_LOAD */
1576 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1577 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1578 #endif /* ndef FEATURE_FORCE_LOAD */
1580 #ifdef FEATURE_GRACEFUL_TERMINATION
1581 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1582 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1583 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1584 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1586 #ifdef FEATURE_IMAGE_BLOCKING
1587 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1588 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1589 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1590 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1592 #ifdef FEATURE_IMAGE_DETECT_MSIE
1593 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1594 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1595 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1596 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1599 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 1);
1600 #else /* ifndef HAVE_RFC2553 */
1601 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 0);
1602 #endif /* ndef HAVE_RFC2553 */
1604 #ifdef FEATURE_NO_GIFS
1605 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1606 #else /* ifndef FEATURE_NO_GIFS */
1607 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1608 #endif /* ndef FEATURE_NO_GIFS */
1610 #ifdef FEATURE_PTHREAD
1611 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1612 #else /* ifndef FEATURE_PTHREAD */
1613 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1614 #endif /* ndef FEATURE_PTHREAD */
1616 #ifdef FEATURE_STATISTICS
1617 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1618 #else /* ifndef FEATURE_STATISTICS */
1619 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1620 #endif /* ndef FEATURE_STATISTICS */
1622 #ifdef FEATURE_TOGGLE
1623 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1624 #else /* ifndef FEATURE_TOGGLE */
1625 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1626 #endif /* ndef FEATURE_TOGGLE */
1628 #ifdef FEATURE_TRUST
1629 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1630 #else /* ifndef FEATURE_TRUST */
1631 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1632 #endif /* ndef FEATURE_TRUST */
1635 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1636 #else /* ifndef FEATURE_ZLIB */
1637 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1638 #endif /* ndef FEATURE_ZLIB */
1641 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1642 #else /* ifndef STATIC_PCRE */
1643 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1644 #endif /* ndef STATIC_PCRE */
1647 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1648 #else /* ifndef STATIC_PCRS */
1649 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1650 #endif /* ndef STATIC_PCRS */
1656 /*********************************************************************
1658 * Function : show_rcs
1660 * Description : Create a string with the rcs info for all sourcefiles
1664 * Returns : A string, or NULL on out-of-memory.
1666 *********************************************************************/
1667 static char *show_rcs(void)
1669 char *result = strdup("");
1670 char buf[BUFFER_SIZE];
1672 /* Instead of including *all* dot h's in the project (thus creating a
1673 * tremendous amount of dependencies), I will concede to declaring them
1674 * as extern's. This forces the developer to add to this list, but oh well.
1677 #define SHOW_RCS(__x) \
1679 extern const char __x[]; \
1680 snprintf(buf, sizeof(buf), " %s\n", __x); \
1681 string_append(&result, buf); \
1684 /* In alphabetical order */
1685 SHOW_RCS(actions_h_rcs)
1686 SHOW_RCS(actions_rcs)
1688 SHOW_RCS(amiga_h_rcs)
1690 #endif /* def AMIGA */
1693 #ifdef FEATURE_CGI_EDIT_ACTIONS
1694 SHOW_RCS(cgiedit_h_rcs)
1695 SHOW_RCS(cgiedit_rcs)
1696 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1697 SHOW_RCS(cgisimple_h_rcs)
1698 SHOW_RCS(cgisimple_rcs)
1700 SHOW_RCS(cygwin_h_rcs)
1702 SHOW_RCS(deanimate_h_rcs)
1703 SHOW_RCS(deanimate_rcs)
1704 SHOW_RCS(encode_h_rcs)
1705 SHOW_RCS(encode_rcs)
1706 SHOW_RCS(errlog_h_rcs)
1707 SHOW_RCS(errlog_rcs)
1708 SHOW_RCS(filters_h_rcs)
1709 SHOW_RCS(filters_rcs)
1710 SHOW_RCS(gateway_h_rcs)
1711 SHOW_RCS(gateway_rcs)
1712 SHOW_RCS(jbsockets_h_rcs)
1713 SHOW_RCS(jbsockets_rcs)
1716 SHOW_RCS(list_h_rcs)
1718 SHOW_RCS(loadcfg_h_rcs)
1719 SHOW_RCS(loadcfg_rcs)
1720 SHOW_RCS(loaders_h_rcs)
1721 SHOW_RCS(loaders_rcs)
1722 SHOW_RCS(miscutil_h_rcs)
1723 SHOW_RCS(miscutil_rcs)
1724 SHOW_RCS(parsers_h_rcs)
1725 SHOW_RCS(parsers_rcs)
1727 SHOW_RCS(pcrs_h_rcs)
1728 SHOW_RCS(project_h_rcs)
1729 SHOW_RCS(ssplit_h_rcs)
1730 SHOW_RCS(ssplit_rcs)
1731 SHOW_RCS(urlmatch_h_rcs)
1732 SHOW_RCS(urlmatch_rcs)
1734 #ifndef _WIN_CONSOLE
1735 SHOW_RCS(w32log_h_rcs)
1736 SHOW_RCS(w32log_rcs)
1737 SHOW_RCS(w32res_h_rcs)
1738 SHOW_RCS(w32taskbar_h_rcs)
1739 SHOW_RCS(w32taskbar_rcs)
1740 #endif /* ndef _WIN_CONSOLE */
1741 SHOW_RCS(win32_h_rcs)
1743 #endif /* def _WIN32 */
1752 /*********************************************************************
1754 * Function : cgi_show_file
1756 * Description : CGI function that shows the content of a
1757 * configuration file.
1760 * 1 : csp = Current client state (buffers, headers, etc...)
1761 * 2 : rsp = http_response data structure for output
1762 * 3 : parameters = map of cgi parameters
1765 * file : Which file to show. Only first letter is checked,
1770 * Default is to show menu and other information.
1772 * Returns : JB_ERR_OK on success
1773 * JB_ERR_MEMORY on out-of-memory error.
1775 *********************************************************************/
1776 static jb_err cgi_show_file(struct client_state *csp,
1777 struct http_response *rsp,
1778 const struct map *parameters)
1781 const char * filename = NULL;
1782 char * file_description = NULL;
1788 switch (*(lookup(parameters, "file")))
1791 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1793 filename = csp->actions_list[i]->filename;
1794 file_description = "Actions File";
1799 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1801 filename = csp->rlist[i]->filename;
1802 file_description = "Filter File";
1806 #ifdef FEATURE_TRUST
1810 filename = csp->tlist->filename;
1811 file_description = "Trust File";
1814 #endif /* def FEATURE_TRUST */
1817 if (NULL != filename)
1819 struct map *exports;
1824 exports = default_exports(csp, "show-status");
1825 if (NULL == exports)
1827 return JB_ERR_MEMORY;
1830 if ( map(exports, "file-description", 1, file_description, 1)
1831 || map(exports, "filepath", 1, html_encode(filename), 0) )
1834 return JB_ERR_MEMORY;
1837 err = load_file(filename, &s, &length);
1838 if (JB_ERR_OK != err)
1840 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1843 return JB_ERR_MEMORY;
1848 s = html_encode_and_free_original(s);
1851 return JB_ERR_MEMORY;
1854 if (map(exports, "contents", 1, s, 0))
1857 return JB_ERR_MEMORY;
1861 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1864 return JB_ERR_CGI_PARAMS;
1868 /*********************************************************************
1870 * Function : load_file
1872 * Description : Loads a file into a buffer.
1875 * 1 : filename = Name of the file to be loaded.
1876 * 2 : buffer = Used to return the file's content.
1877 * 3 : length = Used to return the size of the file.
1879 * Returns : JB_ERR_OK in case of success,
1880 * JB_ERR_FILE in case of ordinary file loading errors
1881 * (fseek() and ftell() errors are fatal)
1882 * JB_ERR_MEMORY in case of out-of-memory.
1884 *********************************************************************/
1885 static jb_err load_file(const char *filename, char **buffer, size_t *length)
1889 jb_err err = JB_ERR_OK;
1891 fp = fopen(filename, "rb");
1894 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
1898 /* Get file length */
1899 if (fseek(fp, 0, SEEK_END))
1901 log_error(LOG_LEVEL_FATAL,
1902 "Unexpected error while fseek()ing to the end of %s: %E",
1908 log_error(LOG_LEVEL_FATAL,
1909 "Unexpected ftell() error while loading %s: %E",
1912 *length = (size_t)ret;
1914 /* Go back to the beginning. */
1915 if (fseek(fp, 0, SEEK_SET))
1917 log_error(LOG_LEVEL_FATAL,
1918 "Unexpected error while fseek()ing to the beginning of %s: %E",
1922 *buffer = (char *)zalloc(*length + 1);
1923 if (NULL == *buffer)
1925 err = JB_ERR_MEMORY;
1927 else if (!fread(*buffer, *length, 1, fp))
1930 * May happen if the file size changes between fseek() and
1931 * fread(). If it does, we just log it and serve what we got.
1933 log_error(LOG_LEVEL_ERROR,
1934 "Couldn't completely read file %s.", filename);