1 /*********************************************************************
2 *
3 * File : $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
4 *
5 * Purpose : Simple CGIs to get information about Privoxy's
6 * status.
7 *
8 * Copyright : Written by and Copyright (C) 2001-2021 the
9 * Privoxy team. https://www.privoxy.org/
10 *
11 * Based on the Internet Junkbuster originally written
12 * by and Copyright (C) 1997 Anonymous Coders and
13 * Junkbusters Corporation. http://www.junkbusters.com
14 *
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
20 *
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
26 *
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 *
33 **********************************************************************/
34
35
36 #include "config.h"
37
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <stdlib.h>
41 #include <ctype.h>
42 #include <string.h>
43 #include <assert.h>
44
45 #if defined (HAVE_ACCESS) && defined (HAVE_UNISTD_H)
46 #include <unistd.h>
47 #endif /* def HAVE_ACCESS && HAVE_UNISTD_H */
48
49 #include "project.h"
50 #include "cgi.h"
51 #include "cgisimple.h"
52 #include "list.h"
53 #include "encode.h"
54 #include "jcc.h"
55 #include "filters.h"
56 #include "actions.h"
57 #include "miscutil.h"
58 #include "loadcfg.h"
59 #include "parsers.h"
60 #include "urlmatch.h"
61 #include "errlog.h"
62 #ifdef FEATURE_CLIENT_TAGS
63 #include "client-tags.h"
64 #endif
65
66 static jb_err show_defines(struct map *exports);
67 static jb_err cgi_show_file(struct client_state *csp,
68 struct http_response *rsp,
69 const struct map *parameters);
70 static jb_err load_file(const char *filename, char **buffer, size_t *length);
71
72 /*********************************************************************
73 *
74 * Function : cgi_default
75 *
76 * Description : CGI function that is called for the CGI_SITE_1_HOST
77 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
78 * Boring - only exports the default exports.
79 *
80 * Parameters :
81 * 1 : csp = Current client state (buffers, headers, etc...)
82 * 2 : rsp = http_response data structure for output
83 * 3 : parameters = map of cgi parameters
84 *
85 * CGI Parameters : none
86 *
87 * Returns : JB_ERR_OK on success
88 * JB_ERR_MEMORY on out-of-memory
89 *
90 *********************************************************************/
cgi_default(struct client_state * csp,struct http_response * rsp,const struct map * parameters)91 jb_err cgi_default(struct client_state *csp,
92 struct http_response *rsp,
93 const struct map *parameters)
94 {
95 struct map *exports;
96
97 (void)parameters;
98
99 assert(csp);
100 assert(rsp);
101
102 if (NULL == (exports = default_exports(csp, "")))
103 {
104 return JB_ERR_MEMORY;
105 }
106
107 return template_fill_for_cgi(csp, "default", exports, rsp);
108 }
109
110
111 /*********************************************************************
112 *
113 * Function : cgi_error_404
114 *
115 * Description : CGI function that is called if an unknown action was
116 * given.
117 *
118 * Parameters :
119 * 1 : csp = Current client state (buffers, headers, etc...)
120 * 2 : rsp = http_response data structure for output
121 * 3 : parameters = map of cgi parameters
122 *
123 * CGI Parameters : none
124 *
125 * Returns : JB_ERR_OK on success
126 * JB_ERR_MEMORY on out-of-memory error.
127 *
128 *********************************************************************/
cgi_error_404(struct client_state * csp,struct http_response * rsp,const struct map * parameters)129 jb_err cgi_error_404(struct client_state *csp,
130 struct http_response *rsp,
131 const struct map *parameters)
132 {
133 struct map *exports;
134
135 assert(csp);
136 assert(rsp);
137 assert(parameters);
138
139 if (NULL == (exports = default_exports(csp, NULL)))
140 {
141 return JB_ERR_MEMORY;
142 }
143
144 rsp->status = strdup_or_die("404 Privoxy configuration page not found");
145
146 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
147 }
148
149
150 #ifdef FEATURE_GRACEFUL_TERMINATION
151 /*********************************************************************
152 *
153 * Function : cgi_die
154 *
155 * Description : CGI function to shut down Privoxy.
156 * NOTE: Turning this on in a production build
157 * would be a BAD idea. An EXTREMELY BAD idea.
158 * In short, don't do it.
159 *
160 * Parameters :
161 * 1 : csp = Current client state (buffers, headers, etc...)
162 * 2 : rsp = http_response data structure for output
163 * 3 : parameters = map of cgi parameters
164 *
165 * CGI Parameters : none
166 *
167 * Returns : JB_ERR_OK on success
168 *
169 *********************************************************************/
cgi_die(struct client_state * csp,struct http_response * rsp,const struct map * parameters)170 jb_err cgi_die (struct client_state *csp,
171 struct http_response *rsp,
172 const struct map *parameters)
173 {
174 static const char status[] = "200 OK Privoxy shutdown request received";
175 static const char body[] =
176 "<html>\n"
177 "<head>\n"
178 " <title>Privoxy shutdown request received</title>\n"
179 " <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">\n"
180 " <link rel=\"stylesheet\" type=\"text/css\" href=\"" CGI_PREFIX "send-stylesheet\">\n"
181 "</head>\n"
182 "<body>\n"
183 "<h1>Privoxy shutdown request received</h1>\n"
184 "<p>Privoxy is going to shut down after the next request.</p>\n"
185 "</body>\n"
186 "</html>\n";
187
188 assert(csp);
189 assert(rsp);
190 assert(parameters);
191
192 /* quit */
193 g_terminate = 1;
194
195 csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
196
197 rsp->content_length = 0;
198 rsp->head_length = 0;
199 rsp->is_static = 0;
200
201 rsp->body = strdup_or_die(body);
202 rsp->status = strdup_or_die(status);
203
204 return JB_ERR_OK;
205 }
206 #endif /* def FEATURE_GRACEFUL_TERMINATION */
207
208
209 /*********************************************************************
210 *
211 * Function : cgi_show_request
212 *
213 * Description : Show the client's request and what sed() would have
214 * made of it.
215 *
216 * Parameters :
217 * 1 : csp = Current client state (buffers, headers, etc...)
218 * 2 : rsp = http_response data structure for output
219 * 3 : parameters = map of cgi parameters
220 *
221 * CGI Parameters : none
222 *
223 * Returns : JB_ERR_OK on success
224 * JB_ERR_MEMORY on out-of-memory error.
225 *
226 *********************************************************************/
cgi_show_request(struct client_state * csp,struct http_response * rsp,const struct map * parameters)227 jb_err cgi_show_request(struct client_state *csp,
228 struct http_response *rsp,
229 const struct map *parameters)
230 {
231 char *p;
232 struct map *exports;
233
234 assert(csp);
235 assert(rsp);
236 assert(parameters);
237
238 if (NULL == (exports = default_exports(csp, "show-request")))
239 {
240 return JB_ERR_MEMORY;
241 }
242
243 /*
244 * Repair the damage done to the IOB by get_header()
245 */
246 for (p = csp->client_iob->buf; p < csp->client_iob->cur; p++)
247 {
248 if (*p == '\0') *p = '\n';
249 }
250
251 /*
252 * Export the original client's request and the one we would
253 * be sending to the server if this wasn't a CGI call
254 */
255
256 if (map(exports, "client-request", 1, html_encode(csp->client_iob->buf), 0))
257 {
258 free_map(exports);
259 return JB_ERR_MEMORY;
260 }
261
262 if (map(exports, "processed-request", 1,
263 html_encode_and_free_original(
264 #ifdef FEATURE_HTTPS_INSPECTION
265 csp->http->ssl ?
266 list_to_text(csp->https_headers) :
267 #endif
268 list_to_text(csp->headers)
269 ), 0))
270 {
271 free_map(exports);
272 return JB_ERR_MEMORY;
273 }
274
275 return template_fill_for_cgi(csp, "show-request", exports, rsp);
276 }
277
278
279 #ifdef FEATURE_CLIENT_TAGS
280 /*********************************************************************
281 *
282 * Function : cgi_create_client_tag_form
283 *
284 * Description : Creates a HTML form to enable or disable a given
285 * client tag.
286 * XXX: Could use a template.
287 *
288 * Parameters :
289 * 1 : form = Buffer to fill with the generated form
290 * 2 : size = Size of the form buffer
291 * 3 : tag = Name of the tag this form should affect
292 * 4 : toggle_state = Desired state after the button pressed 0
293 * 5 : expires = Whether or not the tag should be enabled.
294 * Only checked if toggle_state is 1.
295 *
296 * Returns : void
297 *
298 *********************************************************************/
cgi_create_client_tag_form(char * form,size_t size,const char * tag,int toggle_state,int expires)299 static void cgi_create_client_tag_form(char *form, size_t size,
300 const char *tag, int toggle_state, int expires)
301 {
302 char *button_name;
303
304 if (toggle_state == 1)
305 {
306 button_name = (expires == 1) ? "Enable" : "Enable temporarily";
307 }
308 else
309 {
310 assert(toggle_state == 0);
311 button_name = "Disable";
312 }
313
314 snprintf(form, size,
315 "<form method=\"GET\" action=\""CGI_PREFIX"toggle-client-tag\" style=\"display: inline\">\n"
316 " <input type=\"hidden\" name=\"tag\" value=\"%s\">\n"
317 " <input type=\"hidden\" name=\"toggle-state\" value=\"%i\">\n"
318 " <input type=\"hidden\" name=\"expires\" value=\"%u\">\n"
319 " <input type=\"submit\" value=\"%s\">\n"
320 "</form>", tag, toggle_state, !expires, button_name);
321 }
322
323 /*********************************************************************
324 *
325 * Function : cgi_show_client_tags
326 *
327 * Description : Shows the tags that can be set based on the client
328 * address (opt-in).
329 *
330 * Parameters :
331 * 1 : csp = Current client state (buffers, headers, etc...)
332 * 2 : rsp = http_response data structure for output
333 * 3 : parameters = map of cgi parameters
334 *
335 * CGI Parameters : none
336 *
337 * Returns : JB_ERR_OK on success
338 * JB_ERR_MEMORY on out-of-memory error.
339 *
340 *********************************************************************/
cgi_show_client_tags(struct client_state * csp,struct http_response * rsp,const struct map * parameters)341 jb_err cgi_show_client_tags(struct client_state *csp,
342 struct http_response *rsp,
343 const struct map *parameters)
344 {
345 struct map *exports;
346 struct client_tag_spec *this_tag;
347 jb_err err = JB_ERR_OK;
348 char *client_tag_status;
349 char buf[1000];
350 time_t refresh_delay;
351
352 assert(csp);
353 assert(rsp);
354 assert(parameters);
355
356 if (NULL == (exports = default_exports(csp, "client-tags")))
357 {
358 return JB_ERR_MEMORY;
359 }
360 assert(csp->client_address != NULL);
361
362 this_tag = csp->config->client_tags;
363 if (this_tag->name == NULL)
364 {
365 client_tag_status = strdup_or_die("<p>No tags have been configured.</p>\n");
366 }
367 else
368 {
369 client_tag_status = strdup_or_die("<table border=\"1\">\n"
370 "<tr><th>Tag name</th>\n"
371 "<th>Current state</th><th>Change state</th><th>Description</th></tr>\n");
372 while ((this_tag != NULL) && (this_tag->name != NULL))
373 {
374 int tag_state;
375
376 privoxy_mutex_lock(&client_tags_mutex);
377 tag_state = client_has_requested_tag(csp->client_address, this_tag->name);
378 privoxy_mutex_unlock(&client_tags_mutex);
379 if (!err) err = string_append(&client_tag_status, "<tr><td>");
380 if (!err) err = string_append(&client_tag_status, this_tag->name);
381 if (!err) err = string_append(&client_tag_status, "</td><td>");
382 if (!err) err = string_append(&client_tag_status, tag_state == 1 ? "Enabled" : "Disabled");
383 if (!err) err = string_append(&client_tag_status, "</td><td>");
384 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 1);
385 if (!err) err = string_append(&client_tag_status, buf);
386 if (tag_state == 0)
387 {
388 cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 0);
389 if (!err) err = string_append(&client_tag_status, buf);
390 }
391 if (!err) err = string_append(&client_tag_status, "</td><td>");
392 if (!err) err = string_append(&client_tag_status, this_tag->description);
393 if (!err) err = string_append(&client_tag_status, "</td></tr>\n");
394 if (err)
395 {
396 break;
397 }
398 this_tag = this_tag->next;
399 }
400 if (!err) err = string_append(&client_tag_status, "</table>\n");
401 if (err)
402 {
403 free_map(exports);
404 return JB_ERR_MEMORY;
405 }
406 }
407 refresh_delay = get_next_tag_timeout_for_client(csp->client_address);
408 if (refresh_delay != 0)
409 {
410 snprintf(buf, sizeof(buf), "%u", csp->config->client_tag_lifetime);
411 if (map(exports, "refresh-delay", 1, buf, 1))
412 {
413 freez(client_tag_status);
414 free_map(exports);
415 return JB_ERR_MEMORY;
416 }
417 }
418 else
419 {
420 err = map_block_killer(exports, "tags-expire");
421 if (err != JB_ERR_OK)
422 {
423 freez(client_tag_status);
424 return err;
425 }
426 }
427
428 if (map(exports, "client-tags", 1, client_tag_status, 0))
429 {
430 free_map(exports);
431 return JB_ERR_MEMORY;
432 }
433
434 if (map(exports, "client-ip-addr", 1, csp->client_address, 1))
435 {
436 free_map(exports);
437 return JB_ERR_MEMORY;
438 }
439
440 return template_fill_for_cgi(csp, "client-tags", exports, rsp);
441 }
442
443
444 /*********************************************************************
445 *
446 * Function : cgi_toggle_client_tag
447 *
448 * Description : Toggles a client tag and redirects to the show-tags
449 * page
450 *
451 * Parameters :
452 * 1 : csp = Current client state (buffers, headers, etc...)
453 * 2 : rsp = http_response data structure for output
454 * 3 : parameters = map of cgi parameters
455 *
456 * CGI Parameters : none
457 * 1 : tag = Name of the tag to enable or disable
458 * 2 : toggle-state = How to toggle the tag (0/1)
459 * 3 : expires = Set to 1 if the tag should be enabled
460 * temporarily, otherwise set to 0
461 *
462 * Returns : JB_ERR_OK on success
463 * JB_ERR_MEMORY on out-of-memory error.
464 *
465 *********************************************************************/
cgi_toggle_client_tag(struct client_state * csp,struct http_response * rsp,const struct map * parameters)466 jb_err cgi_toggle_client_tag(struct client_state *csp,
467 struct http_response *rsp,
468 const struct map *parameters)
469 {
470 const char *toggled_tag;
471 const char *toggle_state;
472 const char *tag_expires;
473 time_t time_to_live;
474
475 assert(csp);
476 assert(rsp);
477 assert(parameters);
478
479 toggled_tag = lookup(parameters, "tag");
480 if (*toggled_tag == '\0')
481 {
482 log_error(LOG_LEVEL_ERROR, "Received tag toggle request without tag");
483 }
484 else
485 {
486 tag_expires = lookup(parameters, "expires");
487 if (*tag_expires == '0')
488 {
489 time_to_live = 0;
490 }
491 else
492 {
493 time_to_live = csp->config->client_tag_lifetime;
494 }
495 toggle_state = lookup(parameters, "toggle-state");
496 if (*toggle_state == '1')
497 {
498 enable_client_specific_tag(csp, toggled_tag, time_to_live);
499 }
500 else
501 {
502 disable_client_specific_tag(csp, toggled_tag);
503 }
504 }
505 rsp->status = strdup_or_die("302 Done dealing with toggle request");
506 if (enlist_unique_header(rsp->headers,
507 "Location", CGI_PREFIX "client-tags"))
508 {
509 return JB_ERR_MEMORY;
510 }
511 return JB_ERR_OK;
512
513 }
514 #endif /* def FEATURE_CLIENT_TAGS */
515
516
517 /*********************************************************************
518 *
519 * Function : cgi_send_banner
520 *
521 * Description : CGI function that returns a banner.
522 *
523 * Parameters :
524 * 1 : csp = Current client state (buffers, headers, etc...)
525 * 2 : rsp = http_response data structure for output
526 * 3 : parameters = map of cgi parameters
527 *
528 * CGI Parameters :
529 * type : Selects the type of banner between "trans", "pattern",
530 * and "auto". Defaults to "pattern" if absent or invalid.
531 * "auto" means to select as if we were image-blocking.
532 * (Only the first character really counts; b and t are
533 * equivalent).
534 *
535 * Returns : JB_ERR_OK on success
536 * JB_ERR_MEMORY on out-of-memory error.
537 *
538 *********************************************************************/
cgi_send_banner(struct client_state * csp,struct http_response * rsp,const struct map * parameters)539 jb_err cgi_send_banner(struct client_state *csp,
540 struct http_response *rsp,
541 const struct map *parameters)
542 {
543 char imagetype = lookup(parameters, "type")[0];
544
545 if (imagetype != 'a' && imagetype != 'b' &&
546 imagetype != 'p' && imagetype != 't')
547 {
548 log_error(LOG_LEVEL_ERROR, "Overruling invalid image type '%c'.",
549 imagetype);
550 imagetype = 'p';
551 }
552
553 /*
554 * If type is auto, then determine the right thing
555 * to do from the set-image-blocker action
556 */
557 if (imagetype == 'a')
558 {
559 /*
560 * Default to pattern
561 */
562 imagetype = 'p';
563
564 #ifdef FEATURE_IMAGE_BLOCKING
565 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
566 {
567 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
568 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
569 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
570
571 if (p == NULL)
572 {
573 /* Use default - nothing to do here. */
574 }
575 else if (0 == strcmpic(p, "blank"))
576 {
577 imagetype = 'b';
578 }
579 else if (0 == strcmpic(p, "pattern"))
580 {
581 imagetype = 'p';
582 }
583
584 /*
585 * If the action is to call this CGI, determine
586 * the argument:
587 */
588 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
589 {
590 imagetype = p[sizeof(prefix1) - 1];
591 }
592 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
593 {
594 imagetype = p[sizeof(prefix2) - 1];
595 }
596
597 /*
598 * Everything else must (should) be a URL to
599 * redirect to.
600 */
601 else
602 {
603 imagetype = 'r';
604 }
605 }
606 #endif /* def FEATURE_IMAGE_BLOCKING */
607 }
608
609 /*
610 * Now imagetype is either the non-auto type we were called with,
611 * or it was auto and has since been determined. In any case, we
612 * can proceed to actually answering the request by sending a redirect
613 * or an image as appropriate:
614 */
615 if (imagetype == 'r')
616 {
617 rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
618 if (enlist_unique_header(rsp->headers, "Location",
619 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
620 {
621 return JB_ERR_MEMORY;
622 }
623 }
624 else
625 {
626 if ((imagetype == 'b') || (imagetype == 't'))
627 {
628 rsp->body = bindup(image_blank_data, image_blank_length);
629 rsp->content_length = image_blank_length;
630 }
631 else
632 {
633 rsp->body = bindup(image_pattern_data, image_pattern_length);
634 rsp->content_length = image_pattern_length;
635 }
636
637 if (rsp->body == NULL)
638 {
639 return JB_ERR_MEMORY;
640 }
641 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
642 {
643 return JB_ERR_MEMORY;
644 }
645
646 rsp->is_static = 1;
647 }
648
649 return JB_ERR_OK;
650
651 }
652
653
654 /*********************************************************************
655 *
656 * Function : cgi_transparent_image
657 *
658 * Description : CGI function that sends a 1x1 transparent image.
659 *
660 * Parameters :
661 * 1 : csp = Current client state (buffers, headers, etc...)
662 * 2 : rsp = http_response data structure for output
663 * 3 : parameters = map of cgi parameters
664 *
665 * CGI Parameters : None
666 *
667 * Returns : JB_ERR_OK on success
668 * JB_ERR_MEMORY on out-of-memory error.
669 *
670 *********************************************************************/
cgi_transparent_image(struct client_state * csp,struct http_response * rsp,const struct map * parameters)671 jb_err cgi_transparent_image(struct client_state *csp,
672 struct http_response *rsp,
673 const struct map *parameters)
674 {
675 (void)csp;
676 (void)parameters;
677
678 rsp->body = bindup(image_blank_data, image_blank_length);
679 rsp->content_length = image_blank_length;
680
681 if (rsp->body == NULL)
682 {
683 return JB_ERR_MEMORY;
684 }
685
686 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
687 {
688 return JB_ERR_MEMORY;
689 }
690
691 rsp->is_static = 1;
692
693 return JB_ERR_OK;
694
695 }
696
697
698 /*********************************************************************
699 *
700 * Function : cgi_send_default_favicon
701 *
702 * Description : CGI function that sends the standard favicon.
703 *
704 * Parameters :
705 * 1 : csp = Current client state (buffers, headers, etc...)
706 * 2 : rsp = http_response data structure for output
707 * 3 : parameters = map of cgi parameters
708 *
709 * CGI Parameters : None
710 *
711 * Returns : JB_ERR_OK on success
712 * JB_ERR_MEMORY on out-of-memory error.
713 *
714 *********************************************************************/
cgi_send_default_favicon(struct client_state * csp,struct http_response * rsp,const struct map * parameters)715 jb_err cgi_send_default_favicon(struct client_state *csp,
716 struct http_response *rsp,
717 const struct map *parameters)
718 {
719 static const char default_favicon_data[] =
720 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
721 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
722 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
723 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
724 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
725 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
726 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
727 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
728 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
729 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
730 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
731 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
732 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
733 "\017\000\000";
734 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
735
736 (void)csp;
737 (void)parameters;
738
739 rsp->body = bindup(default_favicon_data, favicon_length);
740 rsp->content_length = favicon_length;
741
742 if (rsp->body == NULL)
743 {
744 return JB_ERR_MEMORY;
745 }
746
747 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
748 {
749 return JB_ERR_MEMORY;
750 }
751
752 rsp->is_static = 1;
753
754 return JB_ERR_OK;
755
756 }
757
758
759 /*********************************************************************
760 *
761 * Function : cgi_send_error_favicon
762 *
763 * Description : CGI function that sends the favicon for error pages.
764 *
765 * Parameters :
766 * 1 : csp = Current client state (buffers, headers, etc...)
767 * 2 : rsp = http_response data structure for output
768 * 3 : parameters = map of cgi parameters
769 *
770 * CGI Parameters : None
771 *
772 * Returns : JB_ERR_OK on success
773 * JB_ERR_MEMORY on out-of-memory error.
774 *
775 *********************************************************************/
cgi_send_error_favicon(struct client_state * csp,struct http_response * rsp,const struct map * parameters)776 jb_err cgi_send_error_favicon(struct client_state *csp,
777 struct http_response *rsp,
778 const struct map *parameters)
779 {
780 static const char error_favicon_data[] =
781 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
782 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
783 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
784 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
785 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
786 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
787 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
788 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
789 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
790 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
791 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
792 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
793 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
794 "\017\000\000";
795 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
796
797 (void)csp;
798 (void)parameters;
799
800 rsp->body = bindup(error_favicon_data, favicon_length);
801 rsp->content_length = favicon_length;
802
803 if (rsp->body == NULL)
804 {
805 return JB_ERR_MEMORY;
806 }
807
808 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
809 {
810 return JB_ERR_MEMORY;
811 }
812
813 rsp->is_static = 1;
814
815 return JB_ERR_OK;
816
817 }
818
819
820 /*********************************************************************
821 *
822 * Function : cgi_send_stylesheet
823 *
824 * Description : CGI function that sends a css stylesheet found
825 * in the cgi-style.css template
826 *
827 * Parameters :
828 * 1 : csp = Current client state (buffers, headers, etc...)
829 * 2 : rsp = http_response data structure for output
830 * 3 : parameters = map of cgi parameters
831 *
832 * CGI Parameters : None
833 *
834 * Returns : JB_ERR_OK on success
835 * JB_ERR_MEMORY on out-of-memory error.
836 *
837 *********************************************************************/
cgi_send_stylesheet(struct client_state * csp,struct http_response * rsp,const struct map * parameters)838 jb_err cgi_send_stylesheet(struct client_state *csp,
839 struct http_response *rsp,
840 const struct map *parameters)
841 {
842 jb_err err;
843
844 assert(csp);
845 assert(rsp);
846
847 (void)parameters;
848
849 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
850
851 if (err == JB_ERR_FILE)
852 {
853 /*
854 * No way to tell user; send empty stylesheet
855 */
856 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
857 }
858 else if (err)
859 {
860 return err; /* JB_ERR_MEMORY */
861 }
862
863 if (enlist(rsp->headers, "Content-Type: text/css"))
864 {
865 return JB_ERR_MEMORY;
866 }
867
868 return JB_ERR_OK;
869
870 }
871
872
873 /*********************************************************************
874 *
875 * Function : cgi_send_wpad
876 *
877 * Description : CGI function that sends a Proxy Auto-Configuration
878 * (PAC) file.
879 *
880 * Parameters :
881 * 1 : csp = Current client state (buffers, headers, etc...)
882 * 2 : rsp = http_response data structure for output
883 * 3 : parameters = map of cgi parameters
884 *
885 * CGI Parameters : None
886 *
887 * Returns : JB_ERR_OK on success
888 * JB_ERR_MEMORY on out-of-memory error.
889 *
890 *********************************************************************/
cgi_send_wpad(struct client_state * csp,struct http_response * rsp,const struct map * parameters)891 jb_err cgi_send_wpad(struct client_state *csp,
892 struct http_response *rsp,
893 const struct map *parameters)
894 {
895 struct map *exports;
896
897 assert(csp);
898 assert(rsp);
899 assert(parameters);
900
901 if (NULL == (exports = default_exports(csp, NULL)))
902 {
903 return JB_ERR_MEMORY;
904 }
905
906 if (enlist(rsp->headers, "Content-Type: application/x-ns-proxy-autoconfig"))
907 {
908 free_map(exports);
909 return JB_ERR_MEMORY;
910 }
911
912 return template_fill_for_cgi(csp, "wpad.dat", exports, rsp);
913
914 }
915
916
917 /*********************************************************************
918 *
919 * Function : cgi_send_url_info_osd
920 *
921 * Description : CGI function that sends the OpenSearch Description
922 * template for the show-url-info page. It allows to
923 * access the page through "search engine plugins".
924 *
925 * Parameters :
926 * 1 : csp = Current client state (buffers, headers, etc...)
927 * 2 : rsp = http_response data structure for output
928 * 3 : parameters = map of cgi parameters
929 *
930 * CGI Parameters : None
931 *
932 * Returns : JB_ERR_OK on success
933 * JB_ERR_MEMORY on out-of-memory error.
934 *
935 *********************************************************************/
cgi_send_url_info_osd(struct client_state * csp,struct http_response * rsp,const struct map * parameters)936 jb_err cgi_send_url_info_osd(struct client_state *csp,
937 struct http_response *rsp,
938 const struct map *parameters)
939 {
940 jb_err err = JB_ERR_MEMORY;
941 struct map *exports = default_exports(csp, NULL);
942
943 (void)csp;
944 (void)parameters;
945
946 if (NULL != exports)
947 {
948 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
949 if (JB_ERR_OK == err)
950 {
951 err = enlist(rsp->headers,
952 "Content-Type: application/opensearchdescription+xml");
953 }
954 }
955
956 return err;
957
958 }
959
960
961 /*********************************************************************
962 *
963 * Function : get_content_type
964 *
965 * Description : Use the file extension to guess the content type
966 * header we should use to serve the file.
967 *
968 * Parameters :
969 * 1 : filename = Name of the file whose content type
970 * we care about
971 *
972 * Returns : The guessed content type.
973 *
974 *********************************************************************/
get_content_type(const char * filename)975 static const char *get_content_type(const char *filename)
976 {
977 int i;
978 struct content_type
979 {
980 const char extension[6];
981 const char content_type[11];
982 };
983 static const struct content_type content_types[] =
984 {
985 {".css", "text/css"},
986 {".jpg", "image/jpeg"},
987 {".jpeg", "image/jpeg"},
988 {".png", "image/png"},
989 };
990
991 for (i = 0; i < SZ(content_types); i++)
992 {
993 if (strstr(filename, content_types[i].extension))
994 {
995 return content_types[i].content_type;
996 }
997 }
998
999 /* No match by extension, default to html */
1000 return "text/html";
1001 }
1002
1003 /*********************************************************************
1004 *
1005 * Function : cgi_send_user_manual
1006 *
1007 * Description : CGI function that sends a file in the user
1008 * manual directory.
1009 *
1010 * Parameters :
1011 * 1 : csp = Current client state (buffers, headers, etc...)
1012 * 2 : rsp = http_response data structure for output
1013 * 3 : parameters = map of cgi parameters
1014 *
1015 * CGI Parameters : file=name.html, the name of the HTML file
1016 * (relative to user-manual from config)
1017 *
1018 * Returns : JB_ERR_OK on success
1019 * JB_ERR_MEMORY on out-of-memory error.
1020 *
1021 *********************************************************************/
cgi_send_user_manual(struct client_state * csp,struct http_response * rsp,const struct map * parameters)1022 jb_err cgi_send_user_manual(struct client_state *csp,
1023 struct http_response *rsp,
1024 const struct map *parameters)
1025 {
1026 const char *filename;
1027 char *full_path;
1028 jb_err err = JB_ERR_OK;
1029 const char *content_type;
1030
1031 assert(csp);
1032 assert(rsp);
1033 assert(parameters);
1034
1035 if (0 == strncmpic(csp->config->usermanual, "http://", 7) ||
1036 0 == strncmpic(csp->config->usermanual, "https://", 8))
1037 {
1038 log_error(LOG_LEVEL_CGI, "Request for local user-manual "
1039 "received while user-manual delivery is disabled.");
1040 return cgi_error_404(csp, rsp, parameters);
1041 }
1042
1043 if (!parameters->first)
1044 {
1045 /* requested http://p.p/user-manual (without trailing slash) */
1046 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1047 }
1048
1049 get_string_param(parameters, "file", &filename);
1050 if (filename == NULL)
1051 {
1052 /* It's '/' so serve the index.html if there is one. */
1053 filename = "index.html";
1054 }
1055 else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
1056 {
1057 /*
1058 * We currently only support a flat file
1059 * hierarchy for the documentation.
1060 */
1061 log_error(LOG_LEVEL_ERROR,
1062 "Rejecting the request to serve '%s' as it contains '/' or '..'",
1063 filename);
1064 return JB_ERR_CGI_PARAMS;
1065 }
1066
1067 full_path = make_path(csp->config->usermanual, filename);
1068 if (full_path == NULL)
1069 {
1070 return JB_ERR_MEMORY;
1071 }
1072
1073 err = load_file(full_path, &rsp->body, &rsp->content_length);
1074 if (JB_ERR_OK != err)
1075 {
1076 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1077 if (JB_ERR_FILE == err)
1078 {
1079 err = cgi_error_no_template(csp, rsp, full_path);
1080 }
1081 freez(full_path);
1082 return err;
1083 }
1084 freez(full_path);
1085
1086 content_type = get_content_type(filename);
1087 log_error(LOG_LEVEL_CGI,
1088 "Content-Type guessed for %s: %s", filename, content_type);
1089
1090 return enlist_unique_header(rsp->headers, "Content-Type", content_type);
1091
1092 }
1093
1094
1095 #ifdef FEATURE_EXTENDED_STATISTICS
1096 /*********************************************************************
1097 *
1098 * Function : get_block_reason_statistics_table
1099 *
1100 * Description : Produces the block reason statistic table content.
1101 *
1102 * Parameters :
1103 * 1 : csp = Current client state (buffers, headers, etc...)
1104 *
1105 * Returns : Pointer to the HTML statistic table content or
1106 * NULL on out of memory
1107 *
1108 *********************************************************************/
get_block_reason_statistics_table(const struct client_state * csp)1109 static char *get_block_reason_statistics_table(const struct client_state *csp)
1110 {
1111 char buf[BUFFER_SIZE];
1112 char *statistics;
1113 int i;
1114 struct file_list *fl;
1115 jb_err err = JB_ERR_OK;
1116
1117 statistics = strdup_or_die("");
1118
1119 /* Run through all action files. */
1120 for (i = 0; i < MAX_AF_FILES; i++)
1121 {
1122 struct url_actions *b;
1123 struct action_spec *last_action = NULL;
1124
1125 if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
1126 {
1127 /* Skip empty files */
1128 continue;
1129 }
1130
1131 /* Go through all the actions. */
1132 for (b = b->next; NULL != b; b = b->next)
1133 {
1134 if (last_action == b->action)
1135 {
1136 continue;
1137 }
1138 if ((b->action->add & ACTION_BLOCK))
1139 {
1140 unsigned long long count;
1141 const char *block_reason = b->action->string[ACTION_STRING_BLOCK];
1142 const char *encoded_block_reason = html_encode(block_reason);
1143
1144 if (encoded_block_reason == NULL)
1145 {
1146 freez(statistics);
1147 return NULL;
1148 }
1149 get_block_reason_count(block_reason, &count);
1150 snprintf(buf, sizeof(buf),
1151 "<tr><td>%s</td><td style=\"text-align: right\">%llu</td>\n",
1152 encoded_block_reason, count);
1153 freez(encoded_block_reason);
1154
1155 if (!err) err = string_append(&statistics, buf);
1156 }
1157 last_action = b->action;
1158 }
1159 }
1160
1161 return statistics;
1162
1163 }
1164
1165
1166 /*********************************************************************
1167 *
1168 * Function : get_filter_statistics_table
1169 *
1170 * Description : Produces the filter statistic table content.
1171 *
1172 * Parameters :
1173 * 1 : csp = Current client state (buffers, headers, etc...)
1174 *
1175 * Returns : Pointer to the HTML statistic table content or
1176 * NULL on out of memory
1177 *
1178 *********************************************************************/
get_filter_statistics_table(const struct client_state * csp)1179 static char *get_filter_statistics_table(const struct client_state *csp)
1180 {
1181 char buf[BUFFER_SIZE];
1182 char *statistics;
1183 int i;
1184 struct file_list *fl;
1185 struct re_filterfile_spec *b;
1186 jb_err err = JB_ERR_OK;
1187
1188 statistics = strdup_or_die("");
1189
1190 for (i = 0; i < MAX_AF_FILES; i++)
1191 {
1192 fl = csp->rlist[i];
1193 if ((NULL == fl) || (NULL == fl->f))
1194 {
1195 /*
1196 * Either there are no filter files left or this
1197 * filter file just contains no valid filters.
1198 *
1199 * Continue to be sure we don't miss valid filter
1200 * files that are chained after empty or invalid ones.
1201 */
1202 continue;
1203 }
1204
1205 for (b = fl->f; b != NULL; b = b->next)
1206 {
1207 if (b->type == FT_CONTENT_FILTER)
1208 {
1209 unsigned long long executions;
1210 unsigned long long response_bodies_modified;
1211 unsigned long long hits;
1212
1213 get_filter_statistics(b->name, &executions, &response_bodies_modified, &hits);
1214 snprintf(buf, sizeof(buf),
1215 "<tr><td>%s</td><td style=\"text-align: right\">%llu</td>"
1216 "<td style=\"text-align: right\">%llu</td>"
1217 "<td style=\"text-align: right\">%llu</td><tr>\n",
1218 b->name, executions, response_bodies_modified, hits);
1219
1220 if (!err) err = string_append(&statistics, buf);
1221 }
1222 }
1223 }
1224
1225 return statistics;
1226
1227 }
1228 #endif /* def FEATURE_EXTENDED_STATISTICS */
1229
1230
1231 /*********************************************************************
1232 *
1233 * Function : cgi_show_status
1234 *
1235 * Description : CGI function that returns a web page describing the
1236 * current status of Privoxy.
1237 *
1238 * Parameters :
1239 * 1 : csp = Current client state (buffers, headers, etc...)
1240 * 2 : rsp = http_response data structure for output
1241 * 3 : parameters = map of cgi parameters
1242 *
1243 * CGI Parameters :
1244 * file : Which file to show. Only first letter is checked,
1245 * valid values are:
1246 * - "a"ction file
1247 * - "r"egex
1248 * - "t"rust
1249 * Default is to show menu and other information.
1250 *
1251 * Returns : JB_ERR_OK on success
1252 * JB_ERR_MEMORY on out-of-memory error.
1253 *
1254 *********************************************************************/
cgi_show_status(struct client_state * csp,struct http_response * rsp,const struct map * parameters)1255 jb_err cgi_show_status(struct client_state *csp,
1256 struct http_response *rsp,
1257 const struct map *parameters)
1258 {
1259 char *s = NULL;
1260 unsigned i;
1261 int j;
1262
1263 char buf[BUFFER_SIZE];
1264 #ifdef FEATURE_STATISTICS
1265 float perc_rej; /* Percentage of http requests rejected */
1266 int local_urls_read;
1267 int local_urls_rejected;
1268 #endif /* ndef FEATURE_STATISTICS */
1269 jb_err err = JB_ERR_OK;
1270
1271 struct map *exports;
1272
1273 assert(csp);
1274 assert(rsp);
1275 assert(parameters);
1276
1277 if ('\0' != *(lookup(parameters, "file")))
1278 {
1279 return cgi_show_file(csp, rsp, parameters);
1280 }
1281
1282 if (NULL == (exports = default_exports(csp, "show-status")))
1283 {
1284 return JB_ERR_MEMORY;
1285 }
1286
1287 s = strdup("");
1288 for (j = 0; (s != NULL) && (j < Argc); j++)
1289 {
1290 if (!err) err = string_join (&s, html_encode(Argv[j]));
1291 if (!err) err = string_append(&s, " ");
1292 }
1293 if (!err) err = map(exports, "invocation", 1, s, 0);
1294
1295 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1296 if (!err) err = show_defines(exports);
1297
1298 if (err)
1299 {
1300 free_map(exports);
1301 return JB_ERR_MEMORY;
1302 }
1303
1304 #ifdef FEATURE_STATISTICS
1305 local_urls_read = urls_read;
1306 local_urls_rejected = urls_rejected;
1307
1308 /*
1309 * Need to alter the stats not to include the fetch of this
1310 * page.
1311 *
1312 * Can't do following thread safely! doh!
1313 *
1314 * urls_read--;
1315 * urls_rejected--; * This will be incremented subsequently *
1316 */
1317
1318 if (local_urls_read == 0)
1319 {
1320 if (!err) err = map_block_killer(exports, "have-stats");
1321 }
1322 else
1323 {
1324 if (!err) err = map_block_killer(exports, "have-no-stats");
1325
1326 perc_rej = (float)local_urls_rejected * 100.0F /
1327 (float)local_urls_read;
1328
1329 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1330 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1331
1332 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1333 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1334
1335 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1336 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1337 }
1338
1339 #else /* ndef FEATURE_STATISTICS */
1340 if (!err) err = map_block_killer(exports, "statistics");
1341 #endif /* ndef FEATURE_STATISTICS */
1342
1343 #ifdef FEATURE_EXTENDED_STATISTICS
1344 if (!err)
1345 {
1346 char *block_reason_statistics = get_block_reason_statistics_table(csp);
1347 if (block_reason_statistics != NULL)
1348 {
1349 err = map(exports, "block-reason-statistics", 1, block_reason_statistics, 0);
1350 }
1351 else
1352 {
1353 err = map_block_killer(exports, "extended-statistics");
1354 }
1355 }
1356 if (!err)
1357 {
1358 char *filter_statistics = get_filter_statistics_table(csp);
1359 if (filter_statistics != NULL)
1360 {
1361 err = map(exports, "filter-statistics", 1, filter_statistics, 0);
1362 }
1363 else
1364 {
1365 err = map_block_killer(exports, "extended-statistics");
1366 }
1367 }
1368 #else /* ndef FEATURE_EXTENDED_STATISTICS */
1369 if (!err) err = map_block_killer(exports, "extended-statistics");
1370 #endif /* def FEATURE_EXTENDED_STATISTICS */
1371
1372 /*
1373 * List all action files in use, together with view and edit links,
1374 * except for standard.action, which should only be viewable. (Not
1375 * enforced in the editor itself)
1376 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1377 */
1378 s = strdup("");
1379 for (i = 0; i < MAX_AF_FILES; i++)
1380 {
1381 if (csp->actions_list[i] != NULL)
1382 {
1383 if (!err) err = string_append(&s, "<tr><td>");
1384 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1385 snprintf(buf, sizeof(buf),
1386 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1387 if (!err) err = string_append(&s, buf);
1388
1389 #ifdef FEATURE_CGI_EDIT_ACTIONS
1390 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1391 && (NULL != csp->config->actions_file_short[i]))
1392 {
1393 #ifdef HAVE_ACCESS
1394 if (access(csp->config->actions_file[i], W_OK) == 0)
1395 {
1396 #endif /* def HAVE_ACCESS */
1397 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1398 if (!err) err = string_append(&s, buf);
1399 #ifdef HAVE_ACCESS
1400 }
1401 else
1402 {
1403 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1404 }
1405 #endif /* def HAVE_ACCESS */
1406 }
1407 #endif
1408
1409 if (!err) err = string_append(&s, "</td></tr>\n");
1410 }
1411 }
1412 if (!err && *s != '\0')
1413 {
1414 err = map(exports, "actions-filenames", 1, s, 0);
1415 }
1416 else
1417 {
1418 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1419 freez(s);
1420 }
1421
1422 /*
1423 * List all re_filterfiles in use, together with view options.
1424 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1425 */
1426 s = strdup("");
1427 for (i = 0; i < MAX_AF_FILES; i++)
1428 {
1429 if (csp->rlist[i] != NULL)
1430 {
1431 if (!err) err = string_append(&s, "<tr><td>");
1432 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1433 snprintf(buf, sizeof(buf),
1434 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1435 if (!err) err = string_append(&s, buf);
1436 if (!err) err = string_append(&s, "</td></tr>\n");
1437 }
1438 }
1439 if (!err && *s != '\0')
1440 {
1441 err = map(exports, "re-filter-filenames", 1, s, 0);
1442 }
1443 else
1444 {
1445 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1446 if (!err) err = map_block_killer(exports, "have-filterfile");
1447 freez(s);
1448 }
1449
1450 #ifdef FEATURE_TRUST
1451 if (csp->tlist)
1452 {
1453 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1454 }
1455 else
1456 {
1457 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1458 if (!err) err = map_block_killer(exports, "have-trustfile");
1459 }
1460 #else
1461 if (!err) err = map_block_killer(exports, "trust-support");
1462 #endif /* ndef FEATURE_TRUST */
1463
1464 #ifdef FEATURE_CGI_EDIT_ACTIONS
1465 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1466 {
1467 err = map_block_killer(exports, "cgi-editor-is-disabled");
1468 }
1469 #endif /* ndef CGI_EDIT_ACTIONS */
1470
1471 if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1472
1473 if (err)
1474 {
1475 free_map(exports);
1476 return JB_ERR_MEMORY;
1477 }
1478
1479 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1480 }
1481
1482
1483 /*********************************************************************
1484 *
1485 * Function : cgi_show_url_info
1486 *
1487 * Description : CGI function that determines and shows which actions
1488 * Privoxy will perform for a given url, and which
1489 * matches starting from the defaults have lead to that.
1490 *
1491 * Parameters :
1492 * 1 : csp = Current client state (buffers, headers, etc...)
1493 * 2 : rsp = http_response data structure for output
1494 * 3 : parameters = map of cgi parameters
1495 *
1496 * CGI Parameters :
1497 * url : The url whose actions are to be determined.
1498 * If url is unset, the url-given conditional will be
1499 * set, so that all but the form can be suppressed in
1500 * the template.
1501 *
1502 * Returns : JB_ERR_OK on success
1503 * JB_ERR_MEMORY on out-of-memory error.
1504 *
1505 *********************************************************************/
cgi_show_url_info(struct client_state * csp,struct http_response * rsp,const struct map * parameters)1506 jb_err cgi_show_url_info(struct client_state *csp,
1507 struct http_response *rsp,
1508 const struct map *parameters)
1509 {
1510 char *url_param;
1511 struct map *exports;
1512 char buf[150];
1513
1514 assert(csp);
1515 assert(rsp);
1516 assert(parameters);
1517
1518 if (NULL == (exports = default_exports(csp, "show-url-info")))
1519 {
1520 return JB_ERR_MEMORY;
1521 }
1522
1523 /*
1524 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1525 */
1526 url_param = strdup_or_die(lookup(parameters, "url"));
1527 chomp(url_param);
1528
1529 /*
1530 * Handle prefixes. 4 possibilities:
1531 * 1) "http://" or "https://" prefix present and followed by URL - OK
1532 * 2) Only the "http://" or "https://" part is present, no URL - change
1533 * to empty string so it will be detected later as "no URL".
1534 * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1535 * "http://" prefix.
1536 * 4) Parameter not specified or is empty string - let this fall through
1537 * for now, next block of code will handle it.
1538 */
1539 if (0 == strncmp(url_param, "http://", 7))
1540 {
1541 if (url_param[7] == '\0')
1542 {
1543 /*
1544 * Empty URL (just prefix).
1545 * Make it totally empty so it's caught by the next if ()
1546 */
1547 url_param[0] = '\0';
1548 }
1549 }
1550 else if (0 == strncmp(url_param, "https://", 8))
1551 {
1552 if (url_param[8] == '\0')
1553 {
1554 /*
1555 * Empty URL (just prefix).
1556 * Make it totally empty so it's caught by the next if ()
1557 */
1558 url_param[0] = '\0';
1559 }
1560 }
1561 else if ((url_param[0] != '\0')
1562 && ((NULL == strstr(url_param, "://")
1563 || (strstr(url_param, "://") > strstr(url_param, "/")))))
1564 {
1565 /*
1566 * No prefix or at least no prefix before
1567 * the first slash - assume http://
1568 */
1569 char *url_param_prefixed = strdup_or_die("http://");
1570
1571 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1572 {
1573 free_map(exports);
1574 return JB_ERR_MEMORY;
1575 }
1576 url_param = url_param_prefixed;
1577 }
1578
1579 /*
1580 * Hide "toggle off" warning if Privoxy is toggled on.
1581 */
1582 if (
1583 #ifdef FEATURE_TOGGLE
1584 (global_toggle_state == 1) &&
1585 #endif /* def FEATURE_TOGGLE */
1586 map_block_killer(exports, "privoxy-is-toggled-off")
1587 )
1588 {
1589 freez(url_param);
1590 free_map(exports);
1591 return JB_ERR_MEMORY;
1592 }
1593
1594 if (url_param[0] == '\0')
1595 {
1596 /* URL parameter not specified, display query form only. */
1597 free(url_param);
1598 if (map_block_killer(exports, "url-given")
1599 || map(exports, "url", 1, "", 1))
1600 {
1601 free_map(exports);
1602 return JB_ERR_MEMORY;
1603 }
1604 }
1605 else
1606 {
1607 /* Given a URL, so query it. */
1608 jb_err err;
1609 char *matches;
1610 char *s;
1611 int hits = 0;
1612 struct file_list *fl;
1613 struct url_actions *b;
1614 struct http_request url_to_query[1];
1615 struct current_action_spec action[1];
1616 int i;
1617
1618 if (map(exports, "url", 1, html_encode(url_param), 0))
1619 {
1620 free(url_param);
1621 free_map(exports);
1622 return JB_ERR_MEMORY;
1623 }
1624
1625 init_current_action(action);
1626
1627 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1628 {
1629 free_current_action(action);
1630 free(url_param);
1631 free_map(exports);
1632 return JB_ERR_MEMORY;
1633 }
1634
1635 memset(url_to_query, '\0', sizeof(url_to_query));
1636 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1637 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1638
1639 free(url_param);
1640
1641 if (err == JB_ERR_MEMORY)
1642 {
1643 free_http_request(url_to_query);
1644 free_current_action(action);
1645 free_map(exports);
1646 return JB_ERR_MEMORY;
1647 }
1648 else if (err)
1649 {
1650 /* Invalid URL */
1651
1652 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1653 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1654 if (!err) err = map_block_killer(exports, "valid-url");
1655
1656 free_current_action(action);
1657 free_http_request(url_to_query);
1658
1659 if (err)
1660 {
1661 free_map(exports);
1662 return JB_ERR_MEMORY;
1663 }
1664
1665 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1666 }
1667
1668 /*
1669 * We have a warning about SSL paths. Hide it for unencrypted sites
1670 * and unconditionally if https inspection is enabled.
1671 */
1672 #ifndef FEATURE_HTTPS_INSPECTION
1673 if (!url_to_query->ssl)
1674 #endif
1675 {
1676 if (map_block_killer(exports, "https-and-no-https-inspection"))
1677 {
1678 free_current_action(action);
1679 free_map(exports);
1680 free_http_request(url_to_query);
1681 return JB_ERR_MEMORY;
1682 }
1683 }
1684
1685 matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1686
1687 for (i = 0; i < MAX_AF_FILES; i++)
1688 {
1689 if (NULL == csp->config->actions_file_short[i]
1690 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1691
1692 b = NULL;
1693 hits = 1;
1694 if ((fl = csp->actions_list[i]) != NULL)
1695 {
1696 if ((b = fl->f) != NULL)
1697 {
1698 /* FIXME: Hardcoded HTML! */
1699 string_append(&matches, "<tr><th>In file: ");
1700 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1701 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1702 string_append(&matches, buf);
1703 string_append(&matches, "View</a>");
1704 #ifdef FEATURE_CGI_EDIT_ACTIONS
1705 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1706 {
1707 #ifdef HAVE_ACCESS
1708 if (access(csp->config->actions_file[i], W_OK) == 0)
1709 {
1710 #endif /* def HAVE_ACCESS */
1711 snprintf(buf, sizeof(buf),
1712 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1713 string_append(&matches, buf);
1714 string_append(&matches, "Edit</a>");
1715 #ifdef HAVE_ACCESS
1716 }
1717 else
1718 {
1719 string_append(&matches, " <strong>No write access.</strong>");
1720 }
1721 #endif /* def HAVE_ACCESS */
1722 }
1723 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1724
1725 string_append(&matches, "</th></tr>\n");
1726
1727 hits = 0;
1728 b = b->next;
1729 }
1730 }
1731
1732 for (; (b != NULL) && (matches != NULL); b = b->next)
1733 {
1734 if (url_match(b->url, url_to_query))
1735 {
1736 string_append(&matches, "<tr><td>{");
1737 string_join (&matches, actions_to_html(csp, b->action));
1738 string_append(&matches, " }<br>\n<code>");
1739 string_join (&matches, html_encode(b->url->spec));
1740 string_append(&matches, "</code></td></tr>\n");
1741
1742 if (merge_current_action(action, b->action))
1743 {
1744 freez(matches);
1745 free_http_request(url_to_query);
1746 free_current_action(action);
1747 free_map(exports);
1748 return JB_ERR_MEMORY;
1749 }
1750 hits++;
1751 }
1752 }
1753
1754 if (!hits)
1755 {
1756 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1757 }
1758 }
1759 string_append(&matches, "</table>\n");
1760
1761 /*
1762 * XXX: Kludge to make sure the "Forward settings" section
1763 * shows what forward-override{} would do with the requested URL.
1764 * No one really cares how the CGI request would be forwarded
1765 * if it wasn't intercepted as CGI request in the first place.
1766 *
1767 * From here on the action bitmask will no longer reflect
1768 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1769 * but luckily it's no longer required later on anyway.
1770 */
1771 free_current_action(csp->action);
1772 get_url_actions(csp, url_to_query);
1773
1774 /*
1775 * Fill in forwarding settings.
1776 *
1777 * The possibilities are:
1778 * - no forwarding
1779 * - http forwarding only
1780 * - socks4(a) forwarding only
1781 * - socks4(a) and http forwarding.
1782 *
1783 * XXX: Parts of this code could be reused for the
1784 * "forwarding-failed" template which currently doesn't
1785 * display the proxy port and an eventual second forwarder.
1786 */
1787 {
1788 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1789
1790 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1791 {
1792 if (!err) err = map_block_killer(exports, "socks-forwarder");
1793 if (!err) err = map_block_killer(exports, "http-forwarder");
1794 }
1795 else
1796 {
1797 char port[10]; /* We save proxy ports as int but need a string here */
1798
1799 if (!err) err = map_block_killer(exports, "no-forwarder");
1800
1801 if (fwd->gateway_host != NULL)
1802 {
1803 char *socks_type = NULL;
1804
1805 switch (fwd->type)
1806 {
1807 case SOCKS_4:
1808 socks_type = "socks4";
1809 break;
1810 case SOCKS_4A:
1811 socks_type = "socks4a";
1812 break;
1813 case SOCKS_5:
1814 socks_type = "socks5";
1815 break;
1816 case SOCKS_5T:
1817 socks_type = "socks5t";
1818 break;
1819 default:
1820 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1821 }
1822
1823 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1824 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1825 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1826 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1827 }
1828 else
1829 {
1830 if (!err) err = map_block_killer(exports, "socks-forwarder");
1831 }
1832
1833 if (fwd->forward_host != NULL)
1834 {
1835 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1836 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1837 if (!err) err = map(exports, "forward-port", 1, port, 1);
1838 }
1839 else
1840 {
1841 if (!err) err = map_block_killer(exports, "http-forwarder");
1842 }
1843 }
1844 }
1845
1846 free_http_request(url_to_query);
1847
1848 if (err || matches == NULL)
1849 {
1850 free_current_action(action);
1851 free_map(exports);
1852 return JB_ERR_MEMORY;
1853 }
1854
1855 #ifdef FEATURE_CGI_EDIT_ACTIONS
1856 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1857 {
1858 err = map_block_killer(exports, "cgi-editor-is-disabled");
1859 }
1860 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1861
1862 /*
1863 * If zlib support is available, if no content filters
1864 * are enabled or if the prevent-compression action is enabled,
1865 * suppress the "compression could prevent filtering" warning.
1866 */
1867 #ifndef FEATURE_ZLIB
1868 if (!content_filters_enabled(action) ||
1869 (action->flags & ACTION_NO_COMPRESSION))
1870 #endif
1871 {
1872 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1873 }
1874
1875 if (err || map(exports, "matches", 1, matches , 0))
1876 {
1877 free_current_action(action);
1878 free_map(exports);
1879 return JB_ERR_MEMORY;
1880 }
1881
1882 s = current_action_to_html(csp, action);
1883
1884 free_current_action(action);
1885
1886 if (map(exports, "final", 1, s, 0))
1887 {
1888 free_map(exports);
1889 return JB_ERR_MEMORY;
1890 }
1891 }
1892
1893 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1894 }
1895
1896
1897 /*********************************************************************
1898 *
1899 * Function : cgi_robots_txt
1900 *
1901 * Description : CGI function to return "/robots.txt".
1902 *
1903 * Parameters :
1904 * 1 : csp = Current client state (buffers, headers, etc...)
1905 * 2 : rsp = http_response data structure for output
1906 * 3 : parameters = map of cgi parameters
1907 *
1908 * CGI Parameters : None
1909 *
1910 * Returns : JB_ERR_OK on success
1911 * JB_ERR_MEMORY on out-of-memory error.
1912 *
1913 *********************************************************************/
cgi_robots_txt(struct client_state * csp,struct http_response * rsp,const struct map * parameters)1914 jb_err cgi_robots_txt(struct client_state *csp,
1915 struct http_response *rsp,
1916 const struct map *parameters)
1917 {
1918 char buf[100];
1919 jb_err err;
1920
1921 (void)csp;
1922 (void)parameters;
1923
1924 rsp->body = strdup_or_die(
1925 "# This is the Privoxy control interface.\n"
1926 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1927 "# So go away!\n"
1928 "\n"
1929 "User-agent: *\n"
1930 "Disallow: /\n"
1931 "\n");
1932
1933 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1934
1935 rsp->is_static = 1;
1936
1937 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1938 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1939
1940 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1941 }
1942
1943
1944 /*********************************************************************
1945 *
1946 * Function : show_defines
1947 *
1948 * Description : Add to a map the state of all conditional #defines
1949 * used when building
1950 *
1951 * Parameters :
1952 * 1 : exports = map to extend
1953 *
1954 * Returns : JB_ERR_OK on success
1955 * JB_ERR_MEMORY on out-of-memory error.
1956 *
1957 *********************************************************************/
show_defines(struct map * exports)1958 static jb_err show_defines(struct map *exports)
1959 {
1960 jb_err err = JB_ERR_OK;
1961 int i;
1962 struct feature {
1963 const char name[31];
1964 const unsigned char is_available;
1965 };
1966
1967 static const struct feature features[] = {
1968 {
1969 "FEATURE_64_BIT_TIME_T",
1970 #if (SIZEOF_TIME_T == 8)
1971 1,
1972 #else
1973 0,
1974 #endif
1975 },
1976 {
1977 "FEATURE_ACCEPT_FILTER",
1978 #ifdef FEATURE_ACCEPT_FILTER
1979 1,
1980 #else
1981 0,
1982 #endif
1983 },
1984 {
1985 "FEATURE_ACL",
1986 #ifdef FEATURE_ACL
1987 1,
1988 #else
1989 0,
1990 #endif
1991 },
1992 {
1993 "FEATURE_BROTLI",
1994 #ifdef FEATURE_BROTLI
1995 1,
1996 #else
1997 0,
1998 #endif
1999 },
2000 {
2001 "FEATURE_CGI_EDIT_ACTIONS",
2002 #ifdef FEATURE_CGI_EDIT_ACTIONS
2003 1,
2004 #else
2005 0,
2006 #endif
2007 },
2008 {
2009 "FEATURE_CLIENT_TAGS",
2010 #ifdef FEATURE_CLIENT_TAGS
2011 1,
2012 #else
2013 0,
2014 #endif
2015 },
2016 {
2017 "FEATURE_COMPRESSION",
2018 #ifdef FEATURE_COMPRESSION
2019 1,
2020 #else
2021 0,
2022 #endif
2023 },
2024 {
2025 "FEATURE_CONNECTION_KEEP_ALIVE",
2026 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2027 1,
2028 #else
2029 0,
2030 #endif
2031 },
2032 {
2033 "FEATURE_CONNECTION_SHARING",
2034 #ifdef FEATURE_CONNECTION_SHARING
2035 1,
2036 #else
2037 0,
2038 #endif
2039 },
2040 {
2041 "FEATURE_EXTERNAL_FILTERS",
2042 #ifdef FEATURE_EXTERNAL_FILTERS
2043 1,
2044 #else
2045 0,
2046 #endif
2047 },
2048 {
2049 "FEATURE_FAST_REDIRECTS",
2050 #ifdef FEATURE_FAST_REDIRECTS
2051 1,
2052 #else
2053 0,
2054 #endif
2055 },
2056 {
2057 "FEATURE_FORCE_LOAD",
2058 #ifdef FEATURE_FORCE_LOAD
2059 1,
2060 #else
2061 0,
2062 #endif
2063 },
2064 {
2065 "FEATURE_GRACEFUL_TERMINATION",
2066 #ifdef FEATURE_GRACEFUL_TERMINATION
2067 1,
2068 #else
2069 0,
2070 #endif
2071 },
2072 {
2073 "FEATURE_HTTPS_INSPECTION",
2074 #ifdef FEATURE_HTTPS_INSPECTION
2075 1,
2076 #else
2077 0,
2078 #endif
2079 },
2080 {
2081 "FEATURE_IMAGE_BLOCKING",
2082 #ifdef FEATURE_IMAGE_BLOCKING
2083 1,
2084 #else
2085 0,
2086 #endif
2087 },
2088 {
2089 "FEATURE_IPV6_SUPPORT",
2090 #ifdef HAVE_RFC2553
2091 1,
2092 #else
2093 0,
2094 #endif
2095 },
2096 {
2097 "FEATURE_NO_GIFS",
2098 #ifdef FEATURE_NO_GIFS
2099 1,
2100 #else
2101 0,
2102 #endif
2103 },
2104 {
2105 "FEATURE_PTHREAD",
2106 #ifdef FEATURE_PTHREAD
2107 1,
2108 #else
2109 0,
2110 #endif
2111 },
2112 {
2113 "FEATURE_STATISTICS",
2114 #ifdef FEATURE_STATISTICS
2115 1,
2116 #else
2117 0,
2118 #endif
2119 },
2120 {
2121 "FEATURE_STRPTIME_SANITY_CHECKS",
2122 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
2123 1,
2124 #else
2125 0,
2126 #endif
2127 },
2128 {
2129 "FEATURE_TOGGLE",
2130 #ifdef FEATURE_TOGGLE
2131 1,
2132 #else
2133 0,
2134 #endif
2135 },
2136 {
2137 "FEATURE_TRUST",
2138 #ifdef FEATURE_TRUST
2139 1,
2140 #else
2141 0,
2142 #endif
2143 },
2144 {
2145 "FEATURE_ZLIB",
2146 #ifdef FEATURE_ZLIB
2147 1,
2148 #else
2149 0,
2150 #endif
2151 },
2152 {
2153 "FEATURE_DYNAMIC_PCRE",
2154 #ifdef FEATURE_DYNAMIC_PCRE
2155 1,
2156 #else
2157 0,
2158 #endif
2159 },
2160 {
2161 "FEATURE_EXTENDED_STATISTICS",
2162 #ifdef FEATURE_EXTENDED_STATISTICS
2163 1,
2164 #else
2165 0,
2166 #endif
2167 },
2168 {
2169 "FEATURE_PCRE_HOST_PATTERNS",
2170 #ifdef FEATURE_PCRE_HOST_PATTERNS
2171 1,
2172 #else
2173 0,
2174 #endif
2175 }
2176 };
2177
2178 for (i = 0; i < SZ(features); i++)
2179 {
2180 err = map_conditional(exports, features[i].name, features[i].is_available);
2181 if (err)
2182 {
2183 break;
2184 }
2185 }
2186
2187 return err;
2188
2189 }
2190
2191
2192 /*********************************************************************
2193 *
2194 * Function : cgi_show_file
2195 *
2196 * Description : CGI function that shows the content of a
2197 * configuration file.
2198 *
2199 * Parameters :
2200 * 1 : csp = Current client state (buffers, headers, etc...)
2201 * 2 : rsp = http_response data structure for output
2202 * 3 : parameters = map of cgi parameters
2203 *
2204 * CGI Parameters :
2205 * file : Which file to show. Only first letter is checked,
2206 * valid values are:
2207 * - "a"ction file
2208 * - "r"egex
2209 * - "t"rust
2210 * Default is to show menu and other information.
2211 *
2212 * Returns : JB_ERR_OK on success
2213 * JB_ERR_MEMORY on out-of-memory error.
2214 *
2215 *********************************************************************/
cgi_show_file(struct client_state * csp,struct http_response * rsp,const struct map * parameters)2216 static jb_err cgi_show_file(struct client_state *csp,
2217 struct http_response *rsp,
2218 const struct map *parameters)
2219 {
2220 unsigned i;
2221 const char * filename = NULL;
2222 char * file_description = NULL;
2223
2224 assert(csp);
2225 assert(rsp);
2226 assert(parameters);
2227
2228 switch (*(lookup(parameters, "file")))
2229 {
2230 case 'a':
2231 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2232 {
2233 filename = csp->actions_list[i]->filename;
2234 file_description = "Actions File";
2235 }
2236 break;
2237
2238 case 'f':
2239 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2240 {
2241 filename = csp->rlist[i]->filename;
2242 file_description = "Filter File";
2243 }
2244 break;
2245
2246 #ifdef FEATURE_TRUST
2247 case 't':
2248 if (csp->tlist)
2249 {
2250 filename = csp->tlist->filename;
2251 file_description = "Trust File";
2252 }
2253 break;
2254 #endif /* def FEATURE_TRUST */
2255 }
2256
2257 if (NULL != filename)
2258 {
2259 struct map *exports;
2260 char *s;
2261 jb_err err;
2262 size_t length;
2263
2264 exports = default_exports(csp, "show-status");
2265 if (NULL == exports)
2266 {
2267 return JB_ERR_MEMORY;
2268 }
2269
2270 if (map(exports, "file-description", 1, file_description, 1)
2271 || map(exports, "filepath", 1, html_encode(filename), 0))
2272 {
2273 free_map(exports);
2274 return JB_ERR_MEMORY;
2275 }
2276
2277 err = load_file(filename, &s, &length);
2278 if (JB_ERR_OK != err)
2279 {
2280 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2281 {
2282 free_map(exports);
2283 return JB_ERR_MEMORY;
2284 }
2285 }
2286 else
2287 {
2288 s = html_encode_and_free_original(s);
2289 if (NULL == s)
2290 {
2291 free_map(exports);
2292 return JB_ERR_MEMORY;
2293 }
2294
2295 if (map(exports, "contents", 1, s, 0))
2296 {
2297 free_map(exports);
2298 return JB_ERR_MEMORY;
2299 }
2300 }
2301
2302 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2303 }
2304
2305 return JB_ERR_CGI_PARAMS;
2306 }
2307
2308
2309 /*********************************************************************
2310 *
2311 * Function : load_file
2312 *
2313 * Description : Loads a file into a buffer.
2314 *
2315 * Parameters :
2316 * 1 : filename = Name of the file to be loaded.
2317 * 2 : buffer = Used to return the file's content.
2318 * 3 : length = Used to return the size of the file.
2319 *
2320 * Returns : JB_ERR_OK in case of success,
2321 * JB_ERR_FILE in case of ordinary file loading errors
2322 * (fseek() and ftell() errors are fatal)
2323 * JB_ERR_MEMORY in case of out-of-memory.
2324 *
2325 *********************************************************************/
load_file(const char * filename,char ** buffer,size_t * length)2326 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2327 {
2328 FILE *fp;
2329 long ret;
2330 jb_err err = JB_ERR_OK;
2331
2332 fp = fopen(filename, "rb");
2333 if (NULL == fp)
2334 {
2335 log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
2336 return JB_ERR_FILE;
2337 }
2338
2339 /* Get file length */
2340 if (fseek(fp, 0, SEEK_END))
2341 {
2342 log_error(LOG_LEVEL_FATAL,
2343 "Unexpected error while fseek()ing to the end of %s: %E",
2344 filename);
2345 }
2346 ret = ftell(fp);
2347 if (-1 == ret)
2348 {
2349 log_error(LOG_LEVEL_FATAL,
2350 "Unexpected ftell() error while loading %s: %E",
2351 filename);
2352 }
2353 *length = (size_t)ret;
2354
2355 /* Go back to the beginning. */
2356 if (fseek(fp, 0, SEEK_SET))
2357 {
2358 log_error(LOG_LEVEL_FATAL,
2359 "Unexpected error while fseek()ing to the beginning of %s: %E",
2360 filename);
2361 }
2362
2363 *buffer = zalloc_or_die(*length + 1);
2364
2365 if (1 != fread(*buffer, *length, 1, fp))
2366 {
2367 /*
2368 * May theoretically happen if the file size changes between
2369 * fseek() and fread() because it's edited in-place. Privoxy
2370 * and common text editors don't do that, thus we just fail.
2371 */
2372 log_error(LOG_LEVEL_ERROR,
2373 "Couldn't completely read file %s.", filename);
2374 freez(*buffer);
2375 err = JB_ERR_FILE;
2376 }
2377
2378 fclose(fp);
2379
2380 return err;
2381
2382 }
2383
2384
2385 /*
2386 Local Variables:
2387 tab-width: 3
2388 end:
2389 */
2390