1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.96 2009/12/16 08:36:39 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-2010 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 : cgi_send_user_manual
665 * Description : CGI function that sends a file in the user
669 * 1 : csp = Current client state (buffers, headers, etc...)
670 * 2 : rsp = http_response data structure for output
671 * 3 : parameters = map of cgi parameters
673 * CGI Parameters : file=name.html, the name of the HTML file
674 * (relative to user-manual from config)
676 * Returns : JB_ERR_OK on success
677 * JB_ERR_MEMORY on out-of-memory error.
679 *********************************************************************/
680 jb_err cgi_send_user_manual(struct client_state *csp,
681 struct http_response *rsp,
682 const struct map *parameters)
684 const char * filename;
686 jb_err err = JB_ERR_OK;
693 if (0 == strncmpic(csp->config->usermanual, "http://", 7))
695 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
696 "received while user-manual delivery is disabled.");
697 return cgi_error_404(csp, rsp, parameters);
700 if (!parameters->first)
702 /* requested http://p.p/user-manual (without trailing slash) */
703 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
706 get_string_param(parameters, "file", &filename);
707 /* Check paramter for hack attempts */
708 if (filename && strchr(filename, '/'))
710 return JB_ERR_CGI_PARAMS;
712 if (filename && strstr(filename, ".."))
714 return JB_ERR_CGI_PARAMS;
717 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
718 if (full_path == NULL)
720 return JB_ERR_MEMORY;
723 err = load_file(full_path, &rsp->body, &rsp->content_length);
724 if (JB_ERR_OK != err)
726 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
727 if (JB_ERR_FILE == err)
729 err = cgi_error_no_template(csp, rsp, full_path);
736 /* Guess correct Content-Type based on the filename's ending */
739 length = strlen(filename);
745 if((length>=4) && !strcmp(&filename[length-4], ".css"))
747 err = enlist(rsp->headers, "Content-Type: text/css");
749 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
751 err = enlist(rsp->headers, "Content-Type: image/jpeg");
755 err = enlist(rsp->headers, "Content-Type: text/html");
762 /*********************************************************************
764 * Function : cgi_show_version
766 * Description : CGI function that returns a a web page describing the
767 * file versions of Privoxy.
770 * 1 : csp = Current client state (buffers, headers, etc...)
771 * 2 : rsp = http_response data structure for output
772 * 3 : parameters = map of cgi parameters
774 * CGI Parameters : none
776 * Returns : JB_ERR_OK on success
777 * JB_ERR_MEMORY on out-of-memory error.
779 *********************************************************************/
780 jb_err cgi_show_version(struct client_state *csp,
781 struct http_response *rsp,
782 const struct map *parameters)
790 if (NULL == (exports = default_exports(csp, "show-version")))
792 return JB_ERR_MEMORY;
795 if (map(exports, "sourceversions", 1, show_rcs(), 0))
798 return JB_ERR_MEMORY;
801 return template_fill_for_cgi(csp, "show-version", exports, rsp);
805 /*********************************************************************
807 * Function : cgi_show_status
809 * Description : CGI function that returns a web page describing the
810 * current status of Privoxy.
813 * 1 : csp = Current client state (buffers, headers, etc...)
814 * 2 : rsp = http_response data structure for output
815 * 3 : parameters = map of cgi parameters
818 * file : Which file to show. Only first letter is checked,
823 * Default is to show menu and other information.
825 * Returns : JB_ERR_OK on success
826 * JB_ERR_MEMORY on out-of-memory error.
828 *********************************************************************/
829 jb_err cgi_show_status(struct client_state *csp,
830 struct http_response *rsp,
831 const struct map *parameters)
837 char buf[BUFFER_SIZE];
838 #ifdef FEATURE_STATISTICS
839 float perc_rej; /* Percentage of http requests rejected */
841 int local_urls_rejected;
842 #endif /* ndef FEATURE_STATISTICS */
843 jb_err err = JB_ERR_OK;
851 if ('\0' != *(lookup(parameters, "file")))
853 return cgi_show_file(csp, rsp, parameters);
856 if (NULL == (exports = default_exports(csp, "show-status")))
858 return JB_ERR_MEMORY;
862 for (j = 0; (s != NULL) && (j < Argc); j++)
864 if (!err) err = string_join (&s, html_encode(Argv[j]));
865 if (!err) err = string_append(&s, " ");
867 if (!err) err = map(exports, "invocation", 1, s, 0);
869 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
870 if (!err) err = show_defines(exports);
875 return JB_ERR_MEMORY;
878 #ifdef FEATURE_STATISTICS
879 local_urls_read = urls_read;
880 local_urls_rejected = urls_rejected;
883 * Need to alter the stats not to include the fetch of this
886 * Can't do following thread safely! doh!
889 * urls_rejected--; * This will be incremented subsequently *
892 if (local_urls_read == 0)
894 if (!err) err = map_block_killer(exports, "have-stats");
898 if (!err) err = map_block_killer(exports, "have-no-stats");
900 perc_rej = (float)local_urls_rejected * 100.0F /
901 (float)local_urls_read;
903 snprintf(buf, sizeof(buf), "%d", local_urls_read);
904 if (!err) err = map(exports, "requests-received", 1, buf, 1);
906 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
907 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
909 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
910 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
913 #else /* ndef FEATURE_STATISTICS */
914 err = err || map_block_killer(exports, "statistics");
915 #endif /* ndef FEATURE_STATISTICS */
918 * List all action files in use, together with view and edit links,
919 * except for standard.action, which should only be viewable. (Not
920 * enforced in the editor itself)
921 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
924 for (i = 0; i < MAX_AF_FILES; i++)
926 if (csp->actions_list[i] != NULL)
928 if (!err) err = string_append(&s, "<tr><td>");
929 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
930 snprintf(buf, sizeof(buf),
931 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
932 if (!err) err = string_append(&s, buf);
934 #ifdef FEATURE_CGI_EDIT_ACTIONS
935 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
936 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
937 && (NULL != csp->config->actions_file_short[i]))
940 if (access(csp->config->actions_file[i], W_OK) == 0)
942 #endif /* def HAVE_ACCESS */
943 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
944 if (!err) err = string_append(&s, buf);
949 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
951 #endif /* def HAVE_ACCESS */
955 if (!err) err = string_append(&s, "</td></tr>\n");
960 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
964 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
968 * List all re_filterfiles in use, together with view options.
969 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
972 for (i = 0; i < MAX_AF_FILES; i++)
974 if (csp->rlist[i] != NULL)
976 if (!err) err = string_append(&s, "<tr><td>");
977 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
978 snprintf(buf, sizeof(buf),
979 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
980 if (!err) err = string_append(&s, buf);
981 if (!err) err = string_append(&s, "</td></tr>\n");
986 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
990 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
991 if (!err) err = map_block_killer(exports, "have-filterfile");
997 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1001 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1002 if (!err) err = map_block_killer(exports, "have-trustfile");
1005 if (!err) err = map_block_killer(exports, "trust-support");
1006 #endif /* ndef FEATURE_TRUST */
1008 #ifdef FEATURE_CGI_EDIT_ACTIONS
1009 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1011 err = map_block_killer(exports, "cgi-editor-is-disabled");
1013 #endif /* ndef CGI_EDIT_ACTIONS */
1018 return JB_ERR_MEMORY;
1021 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1025 /*********************************************************************
1027 * Function : cgi_show_url_info
1029 * Description : CGI function that determines and shows which actions
1030 * Privoxy will perform for a given url, and which
1031 * matches starting from the defaults have lead to that.
1034 * 1 : csp = Current client state (buffers, headers, etc...)
1035 * 2 : rsp = http_response data structure for output
1036 * 3 : parameters = map of cgi parameters
1039 * url : The url whose actions are to be determined.
1040 * If url is unset, the url-given conditional will be
1041 * set, so that all but the form can be suppressed in
1044 * Returns : JB_ERR_OK on success
1045 * JB_ERR_MEMORY on out-of-memory error.
1047 *********************************************************************/
1048 jb_err cgi_show_url_info(struct client_state *csp,
1049 struct http_response *rsp,
1050 const struct map *parameters)
1053 struct map *exports;
1060 if (NULL == (exports = default_exports(csp, "show-url-info")))
1062 return JB_ERR_MEMORY;
1066 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1068 url_param = strdup(lookup(parameters, "url"));
1069 if (url_param == NULL)
1072 return JB_ERR_MEMORY;
1077 * Handle prefixes. 4 possibilities:
1078 * 1) "http://" or "https://" prefix present and followed by URL - OK
1079 * 2) Only the "http://" or "https://" part is present, no URL - change
1080 * to empty string so it will be detected later as "no URL".
1081 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1083 * 4) Parameter not specified or is empty string - let this fall through
1084 * for now, next block of code will handle it.
1086 if (0 == strncmp(url_param, "http://", 7))
1088 if (url_param[7] == '\0')
1091 * Empty URL (just prefix).
1092 * Make it totally empty so it's caught by the next if()
1094 url_param[0] = '\0';
1097 else if (0 == strncmp(url_param, "https://", 8))
1099 if (url_param[8] == '\0')
1102 * Empty URL (just prefix).
1103 * Make it totally empty so it's caught by the next if()
1105 url_param[0] = '\0';
1108 else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1110 /* No prefix - assume http:// */
1111 char *url_param_prefixed = strdup("http://");
1113 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1116 return JB_ERR_MEMORY;
1118 url_param = url_param_prefixed;
1122 * Hide "toggle off" warning if Privoxy is toggled on.
1125 #ifdef FEATURE_TOGGLE
1126 (global_toggle_state == 1) &&
1127 #endif /* def FEATURE_TOGGLE */
1128 map_block_killer(exports, "privoxy-is-toggled-off")
1132 return JB_ERR_MEMORY;
1135 if (url_param[0] == '\0')
1137 /* URL paramater not specified, display query form only. */
1139 if (map_block_killer(exports, "url-given")
1140 || map(exports, "url", 1, "", 1))
1143 return JB_ERR_MEMORY;
1148 /* Given a URL, so query it. */
1153 struct file_list *fl;
1154 struct url_actions *b;
1155 struct http_request url_to_query[1];
1156 struct current_action_spec action[1];
1159 if (map(exports, "url", 1, html_encode(url_param), 0))
1163 return JB_ERR_MEMORY;
1166 init_current_action(action);
1168 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1170 free_current_action(action);
1173 return JB_ERR_MEMORY;
1176 memset(url_to_query, '\0', sizeof(url_to_query));
1177 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1178 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1182 if (err == JB_ERR_MEMORY)
1184 free_http_request(url_to_query);
1185 free_current_action(action);
1187 return JB_ERR_MEMORY;
1193 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1194 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1195 if (!err) err = map_block_killer(exports, "valid-url");
1197 free_current_action(action);
1198 free_http_request(url_to_query);
1203 return JB_ERR_MEMORY;
1206 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1210 * We have a warning about SSL paths. Hide it for unencrypted sites.
1212 if (!url_to_query->ssl)
1214 if (map_block_killer(exports, "https"))
1216 free_current_action(action);
1218 free_http_request(url_to_query);
1219 return JB_ERR_MEMORY;
1223 matches = strdup("<table summary=\"\" class=\"transparent\">");
1225 for (i = 0; i < MAX_AF_FILES; i++)
1227 if (NULL == csp->config->actions_file_short[i]
1228 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1232 if ((fl = csp->actions_list[i]) != NULL)
1234 if ((b = fl->f) != NULL)
1236 /* FIXME: Hardcoded HTML! */
1237 string_append(&matches, "<tr><th>In file: ");
1238 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1239 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1240 string_append(&matches, buf);
1241 string_append(&matches, "View</a>");
1242 #ifdef FEATURE_CGI_EDIT_ACTIONS
1243 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1246 if (access(csp->config->actions_file[i], W_OK) == 0)
1248 #endif /* def HAVE_ACCESS */
1249 snprintf(buf, sizeof(buf),
1250 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1251 string_append(&matches, buf);
1252 string_append(&matches, "Edit</a>");
1257 string_append(&matches, " <strong>No write access.</strong>");
1259 #endif /* def HAVE_ACCESS */
1261 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1263 string_append(&matches, "</th></tr>\n");
1270 for (; (b != NULL) && (matches != NULL); b = b->next)
1272 if (url_match(b->url, url_to_query))
1274 string_append(&matches, "<tr><td>{");
1275 string_join (&matches, actions_to_html(csp, b->action));
1276 string_append(&matches, " }<br>\n<code>");
1277 string_join (&matches, html_encode(b->url->spec));
1278 string_append(&matches, "</code></td></tr>\n");
1280 if (merge_current_action(action, b->action))
1283 free_http_request(url_to_query);
1284 free_current_action(action);
1286 return JB_ERR_MEMORY;
1294 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1297 string_append(&matches, "</table>\n");
1300 * XXX: Kludge to make sure the "Forward settings" section
1301 * shows what forward-override{} would do with the requested URL.
1302 * No one really cares how the CGI request would be forwarded
1303 * if it wasn't intercepted as CGI request in the first place.
1305 * From here on the action bitmask will no longer reflect
1306 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1307 * but luckily it's no longer required later on anyway.
1309 free_current_action(csp->action);
1310 get_url_actions(csp, url_to_query);
1313 * Fill in forwarding settings.
1315 * The possibilities are:
1317 * - http forwarding only
1318 * - socks4(a) forwarding only
1319 * - socks4(a) and http forwarding.
1321 * XXX: Parts of this code could be reused for the
1322 * "forwarding-failed" template which currently doesn't
1323 * display the proxy port and an eventual second forwarder.
1326 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1328 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1330 if (!err) err = map_block_killer(exports, "socks-forwarder");
1331 if (!err) err = map_block_killer(exports, "http-forwarder");
1335 char port[10]; /* We save proxy ports as int but need a string here */
1337 if (!err) err = map_block_killer(exports, "no-forwarder");
1339 if (fwd->gateway_host != NULL)
1341 char *socks_type = NULL;
1346 socks_type = "socks4";
1349 socks_type = "socks4a";
1352 socks_type = "socks5";
1355 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1358 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1359 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1360 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1361 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1365 if (!err) err = map_block_killer(exports, "socks-forwarder");
1368 if (fwd->forward_host != NULL)
1370 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1371 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1372 if (!err) err = map(exports, "forward-port", 1, port, 1);
1376 if (!err) err = map_block_killer(exports, "http-forwarder");
1381 free_http_request(url_to_query);
1383 if (err || matches == NULL)
1385 free_current_action(action);
1387 return JB_ERR_MEMORY;
1390 #ifdef FEATURE_CGI_EDIT_ACTIONS
1391 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1393 err = map_block_killer(exports, "cgi-editor-is-disabled");
1395 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1398 * If zlib support is available, if no content filters
1399 * are enabled or if the prevent-compression action is enabled,
1400 * suppress the "compression could prevent filtering" warning.
1402 #ifndef FEATURE_ZLIB
1403 if (!content_filters_enabled(action) ||
1404 (action->flags & ACTION_NO_COMPRESSION))
1407 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1410 if (err || map(exports, "matches", 1, matches , 0))
1412 free_current_action(action);
1414 return JB_ERR_MEMORY;
1417 s = current_action_to_html(csp, action);
1419 free_current_action(action);
1421 if (map(exports, "final", 1, s, 0))
1424 return JB_ERR_MEMORY;
1428 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1432 /*********************************************************************
1434 * Function : cgi_robots_txt
1436 * Description : CGI function to return "/robots.txt".
1439 * 1 : csp = Current client state (buffers, headers, etc...)
1440 * 2 : rsp = http_response data structure for output
1441 * 3 : parameters = map of cgi parameters
1443 * CGI Parameters : None
1445 * Returns : JB_ERR_OK on success
1446 * JB_ERR_MEMORY on out-of-memory error.
1448 *********************************************************************/
1449 jb_err cgi_robots_txt(struct client_state *csp,
1450 struct http_response *rsp,
1451 const struct map *parameters)
1460 "# This is the Privoxy control interface.\n"
1461 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1467 if (rsp->body == NULL)
1469 return JB_ERR_MEMORY;
1472 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1476 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1477 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1479 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1483 /*********************************************************************
1485 * Function : show_defines
1487 * Description : Add to a map the state od all conditional #defines
1488 * used when building
1491 * 1 : exports = map to extend
1493 * Returns : JB_ERR_OK on success
1494 * JB_ERR_MEMORY on out-of-memory error.
1496 *********************************************************************/
1497 static jb_err show_defines(struct map *exports)
1499 jb_err err = JB_ERR_OK;
1501 #ifdef FEATURE_ACCEPT_FILTER
1502 if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 1);
1503 #else /* ifndef FEATURE_ACCEPT_FILTER */
1504 if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 0);
1505 #endif /* ndef FEATURE_ACCEPT_FILTER */
1508 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1509 #else /* ifndef FEATURE_ACL */
1510 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1511 #endif /* ndef FEATURE_ACL */
1513 #ifdef FEATURE_CGI_EDIT_ACTIONS
1514 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1515 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1516 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1517 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1519 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1520 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1521 #else /* ifndef FEATURE_CONNECTION_KEEP_ALIVE */
1522 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1523 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1525 #ifdef FEATURE_CONNECTION_SHARING
1526 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 1);
1527 #else /* ifndef FEATURE_CONNECTION_SHARING */
1528 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 0);
1529 #endif /* ndef FEATURE_CONNECTION_SHARING */
1531 #ifdef FEATURE_FAST_REDIRECTS
1532 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1533 #else /* ifndef FEATURE_FAST_REDIRECTS */
1534 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1535 #endif /* ndef FEATURE_FAST_REDIRECTS */
1537 #ifdef FEATURE_FORCE_LOAD
1538 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1539 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1540 #else /* ifndef FEATURE_FORCE_LOAD */
1541 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1542 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1543 #endif /* ndef FEATURE_FORCE_LOAD */
1545 #ifdef FEATURE_GRACEFUL_TERMINATION
1546 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1547 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1548 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1549 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1551 #ifdef FEATURE_IMAGE_BLOCKING
1552 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1553 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1554 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1555 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1557 #ifdef FEATURE_IMAGE_DETECT_MSIE
1558 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1559 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1560 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1561 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1564 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 1);
1565 #else /* ifndef HAVE_RFC2553 */
1566 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 0);
1567 #endif /* ndef HAVE_RFC2553 */
1569 #ifdef FEATURE_NO_GIFS
1570 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1571 #else /* ifndef FEATURE_NO_GIFS */
1572 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1573 #endif /* ndef FEATURE_NO_GIFS */
1575 #ifdef FEATURE_PTHREAD
1576 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1577 #else /* ifndef FEATURE_PTHREAD */
1578 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1579 #endif /* ndef FEATURE_PTHREAD */
1581 #ifdef FEATURE_STATISTICS
1582 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1583 #else /* ifndef FEATURE_STATISTICS */
1584 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1585 #endif /* ndef FEATURE_STATISTICS */
1587 #ifdef FEATURE_TOGGLE
1588 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1589 #else /* ifndef FEATURE_TOGGLE */
1590 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1591 #endif /* ndef FEATURE_TOGGLE */
1593 #ifdef FEATURE_TRUST
1594 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1595 #else /* ifndef FEATURE_TRUST */
1596 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1597 #endif /* ndef FEATURE_TRUST */
1600 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1601 #else /* ifndef FEATURE_ZLIB */
1602 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1603 #endif /* ndef FEATURE_ZLIB */
1606 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1607 #else /* ifndef STATIC_PCRE */
1608 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1609 #endif /* ndef STATIC_PCRE */
1612 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1613 #else /* ifndef STATIC_PCRS */
1614 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1615 #endif /* ndef STATIC_PCRS */
1621 /*********************************************************************
1623 * Function : show_rcs
1625 * Description : Create a string with the rcs info for all sourcefiles
1629 * Returns : A string, or NULL on out-of-memory.
1631 *********************************************************************/
1632 static char *show_rcs(void)
1634 char *result = strdup("");
1635 char buf[BUFFER_SIZE];
1637 /* Instead of including *all* dot h's in the project (thus creating a
1638 * tremendous amount of dependencies), I will concede to declaring them
1639 * as extern's. This forces the developer to add to this list, but oh well.
1642 #define SHOW_RCS(__x) \
1644 extern const char __x[]; \
1645 snprintf(buf, sizeof(buf), " %s\n", __x); \
1646 string_append(&result, buf); \
1649 /* In alphabetical order */
1650 SHOW_RCS(actions_h_rcs)
1651 SHOW_RCS(actions_rcs)
1653 SHOW_RCS(amiga_h_rcs)
1655 #endif /* def AMIGA */
1658 #ifdef FEATURE_CGI_EDIT_ACTIONS
1659 SHOW_RCS(cgiedit_h_rcs)
1660 SHOW_RCS(cgiedit_rcs)
1661 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1662 SHOW_RCS(cgisimple_h_rcs)
1663 SHOW_RCS(cgisimple_rcs)
1665 SHOW_RCS(cygwin_h_rcs)
1667 SHOW_RCS(deanimate_h_rcs)
1668 SHOW_RCS(deanimate_rcs)
1669 SHOW_RCS(encode_h_rcs)
1670 SHOW_RCS(encode_rcs)
1671 SHOW_RCS(errlog_h_rcs)
1672 SHOW_RCS(errlog_rcs)
1673 SHOW_RCS(filters_h_rcs)
1674 SHOW_RCS(filters_rcs)
1675 SHOW_RCS(gateway_h_rcs)
1676 SHOW_RCS(gateway_rcs)
1677 SHOW_RCS(jbsockets_h_rcs)
1678 SHOW_RCS(jbsockets_rcs)
1681 SHOW_RCS(list_h_rcs)
1683 SHOW_RCS(loadcfg_h_rcs)
1684 SHOW_RCS(loadcfg_rcs)
1685 SHOW_RCS(loaders_h_rcs)
1686 SHOW_RCS(loaders_rcs)
1687 SHOW_RCS(miscutil_h_rcs)
1688 SHOW_RCS(miscutil_rcs)
1689 SHOW_RCS(parsers_h_rcs)
1690 SHOW_RCS(parsers_rcs)
1692 SHOW_RCS(pcrs_h_rcs)
1693 SHOW_RCS(project_h_rcs)
1694 SHOW_RCS(ssplit_h_rcs)
1695 SHOW_RCS(ssplit_rcs)
1696 SHOW_RCS(urlmatch_h_rcs)
1697 SHOW_RCS(urlmatch_rcs)
1699 #ifndef _WIN_CONSOLE
1700 SHOW_RCS(w32log_h_rcs)
1701 SHOW_RCS(w32log_rcs)
1702 SHOW_RCS(w32res_h_rcs)
1703 SHOW_RCS(w32taskbar_h_rcs)
1704 SHOW_RCS(w32taskbar_rcs)
1705 #endif /* ndef _WIN_CONSOLE */
1706 SHOW_RCS(win32_h_rcs)
1708 #endif /* def _WIN32 */
1717 /*********************************************************************
1719 * Function : cgi_show_file
1721 * Description : CGI function that shows the content of a
1722 * configuration file.
1725 * 1 : csp = Current client state (buffers, headers, etc...)
1726 * 2 : rsp = http_response data structure for output
1727 * 3 : parameters = map of cgi parameters
1730 * file : Which file to show. Only first letter is checked,
1735 * Default is to show menu and other information.
1737 * Returns : JB_ERR_OK on success
1738 * JB_ERR_MEMORY on out-of-memory error.
1740 *********************************************************************/
1741 static jb_err cgi_show_file(struct client_state *csp,
1742 struct http_response *rsp,
1743 const struct map *parameters)
1746 const char * filename = NULL;
1747 char * file_description = NULL;
1753 switch (*(lookup(parameters, "file")))
1756 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1758 filename = csp->actions_list[i]->filename;
1759 file_description = "Actions File";
1764 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1766 filename = csp->rlist[i]->filename;
1767 file_description = "Filter File";
1771 #ifdef FEATURE_TRUST
1775 filename = csp->tlist->filename;
1776 file_description = "Trust File";
1779 #endif /* def FEATURE_TRUST */
1782 if (NULL != filename)
1784 struct map *exports;
1789 exports = default_exports(csp, "show-status");
1790 if (NULL == exports)
1792 return JB_ERR_MEMORY;
1795 if ( map(exports, "file-description", 1, file_description, 1)
1796 || map(exports, "filepath", 1, html_encode(filename), 0) )
1799 return JB_ERR_MEMORY;
1802 err = load_file(filename, &s, &length);
1803 if (JB_ERR_OK != err)
1805 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1808 return JB_ERR_MEMORY;
1813 s = html_encode_and_free_original(s);
1816 return JB_ERR_MEMORY;
1819 if (map(exports, "contents", 1, s, 0))
1822 return JB_ERR_MEMORY;
1826 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1829 return JB_ERR_CGI_PARAMS;
1833 /*********************************************************************
1835 * Function : load_file
1837 * Description : Loads a file into a buffer.
1840 * 1 : filename = Name of the file to be loaded.
1841 * 2 : buffer = Used to return the file's content.
1842 * 3 : length = Used to return the size of the file.
1844 * Returns : JB_ERR_OK in case of success,
1845 * JB_ERR_FILE in case of ordinary file loading errors
1846 * (fseek() and ftell() errors are fatal)
1847 * JB_ERR_MEMORY in case of out-of-memory.
1849 *********************************************************************/
1850 static jb_err load_file(const char *filename, char **buffer, size_t *length)
1854 jb_err err = JB_ERR_OK;
1856 fp = fopen(filename, "rb");
1862 /* Get file length */
1863 if (fseek(fp, 0, SEEK_END))
1865 log_error(LOG_LEVEL_FATAL,
1866 "Unexpected error while fseek()ing to the end of %s: %E",
1872 log_error(LOG_LEVEL_FATAL,
1873 "Unexpected ftell() error while loading %s: %E",
1876 *length = (size_t)ret;
1878 /* Go back to the beginning. */
1879 if (fseek(fp, 0, SEEK_SET))
1881 log_error(LOG_LEVEL_FATAL,
1882 "Unexpected error while fseek()ing to the beginning of %s: %E",
1886 *buffer = (char *)zalloc(*length + 1);
1887 if (NULL == *buffer)
1889 err = JB_ERR_MEMORY;
1891 else if (!fread(*buffer, *length, 1, fp))
1894 * May happen if the file size changes between fseek() and
1895 * fread(). If it does, we just log it and serve what we got.
1897 log_error(LOG_LEVEL_ERROR,
1898 "Couldn't completely read file %s.", filename);