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