1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.35.2.1 2002/07/04 15:02:38 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/cgisimple.c,v $
6 * Purpose : Simple CGIs to get information about Privoxy's
9 * Functions declared include:
12 * Copyright : Written by and Copyright (C) 2001 the SourceForge
13 * Privoxy team. http://www.privoxy.org/
15 * Based on the Internet Junkbuster originally written
16 * by and Copyright (C) 1997 Anonymous Coders and
17 * Junkbusters Corporation. http://www.junkbusters.com
19 * This program is free software; you can redistribute it
20 * and/or modify it under the terms of the GNU General
21 * Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at
23 * your option) any later version.
25 * This program is distributed in the hope that it will
26 * be useful, but WITHOUT ANY WARRANTY; without even the
27 * implied warranty of MERCHANTABILITY or FITNESS FOR A
28 * PARTICULAR PURPOSE. See the GNU General Public
29 * License for more details.
31 * The GNU General Public License should be included with
32 * this file. If not, you can view it at
33 * http://www.gnu.org/copyleft/gpl.html
34 * or write to the Free Software Foundation, Inc., 59
35 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * $Log: cgisimple.c,v $
39 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
40 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
42 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
43 * Applying patch from Andreas as provided by Hal on the list.
44 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
46 * Revision 1.35 2002/05/12 21:44:44 jongfoster
47 * Adding amiga.[ch] revision information, if on an amiga.
49 * Revision 1.34 2002/04/30 12:06:12 oes
50 * Deleted unused code from default_cgi
52 * Revision 1.33 2002/04/30 11:14:52 oes
53 * Made csp the first parameter in *action_to_html
55 * Revision 1.32 2002/04/26 18:29:13 jongfoster
56 * Fixing this Visual C++ warning:
57 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
59 * Revision 1.31 2002/04/26 12:54:36 oes
60 * - Kill obsolete REDIRECT_URL code
61 * - Error handling fixes
62 * - Style sheet related HTML snipplet changes
63 * - cgi_show_url_info:
64 * - Matches now in table, actions on single lines,
66 * - standard.action suppressed
67 * - Buttons to View and Edit AFs
69 * Revision 1.30 2002/04/24 02:18:08 oes
70 * - show-status is now the starting point for editing
71 * the actions files, generate list of all AFs with buttons
72 * for viewing and editing, new look for file list (Jon:
73 * buttons now aligned ;-P ), view mode now supports multiple
74 * AFs, name changes, no view links for unspecified files,
75 * no edit link for standard.action.
77 * - Jon's multiple AF patch: cgi_show_url_info now uses all
78 * AFs and marks the output accordingly
80 * Revision 1.29 2002/04/10 13:38:35 oes
81 * load_template signature changed
83 * Revision 1.28 2002/04/07 15:42:12 jongfoster
84 * Fixing send-banner?type=auto when the image-blocker is
85 * a redirect to send-banner
87 * Revision 1.27 2002/04/05 15:50:48 oes
88 * added send-stylesheet CGI
90 * Revision 1.26 2002/04/04 00:36:36 gliptak
91 * always use pcre for matching
93 * Revision 1.25 2002/04/03 22:28:03 gliptak
94 * Removed references to gnu_regex
96 * Revision 1.24 2002/04/02 16:12:47 oes
97 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
99 * Revision 1.23 2002/03/26 22:29:54 swa
100 * we have a new homepage!
102 * Revision 1.22 2002/03/24 16:18:15 jongfoster
105 * Revision 1.21 2002/03/24 15:23:33 jongfoster
108 * Revision 1.20 2002/03/24 13:25:43 swa
109 * name change related issues
111 * Revision 1.19 2002/03/16 23:54:06 jongfoster
112 * Adding graceful termination feature, to help look for memory leaks.
113 * If you enable this (which, by design, has to be done by hand
114 * editing config.h) and then go to http://i.j.b/die, then the program
115 * will exit cleanly after the *next* request. It should free all the
116 * memory that was used.
118 * Revision 1.18 2002/03/12 01:44:49 oes
119 * Changed default for "blocked" image from jb logo to checkboard pattern
121 * Revision 1.17 2002/03/08 16:43:18 oes
122 * Added choice beween GIF and PNG built-in images
124 * Revision 1.16 2002/03/07 03:48:38 oes
125 * - Changed built-in images from GIF to PNG
126 * (with regard to Unisys patent issue)
127 * - Added a 4x4 pattern PNG which is less intrusive
128 * than the logo but also clearly marks the deleted banners
130 * Revision 1.15 2002/03/06 22:54:35 jongfoster
131 * Automated function-comment nitpicking.
133 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
134 * Clean up a little CRLF unpleasantness that suddenly appeared
136 * Revision 1.13 2002/02/21 00:10:37 jongfoster
137 * Adding send-banner?type=auto option
139 * Revision 1.12 2002/01/23 01:03:32 jongfoster
140 * Fixing gcc [CygWin] compiler warnings
142 * Revision 1.11 2002/01/23 00:01:04 jongfoster
143 * Adding cgi_transparent_gif() for http://i.j.b/t
144 * Adding missing html_encode() to many CGI functions.
145 * Adding urlmatch.[ch] to http://i.j.b/show-version
147 * Revision 1.10 2002/01/17 21:10:37 jongfoster
148 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
149 * Also fixing a problem in the same function with improperly quoted URLs
150 * in output HTML, and adding code to handle https:// URLs correctly.
152 * Revision 1.9 2001/11/30 23:09:15 jongfoster
153 * Now reports on FEATURE_CGI_EDIT_ACTIONS
154 * Removing FEATURE_DENY_GZIP from template
156 * Revision 1.8 2001/11/13 00:14:07 jongfoster
157 * Fixing stupid bug now I've figured out what || means.
158 * (It always returns 0 or 1, not one of it's paramaters.)
160 * Revision 1.7 2001/10/23 21:48:19 jongfoster
161 * Cleaning up error handling in CGI functions - they now send back
162 * a HTML error page and should never cause a FATAL error. (Fixes one
163 * potential source of "denial of service" attacks).
165 * CGI actions file editor that works and is actually useful.
167 * Ability to toggle JunkBuster remotely using a CGI call.
169 * You can turn off both the above features in the main configuration
170 * file, e.g. if you are running a multi-user proxy.
172 * Revision 1.6 2001/10/14 22:00:32 jongfoster
173 * Adding support for a 404 error when an invalid CGI page is requested.
175 * Revision 1.5 2001/10/07 15:30:41 oes
176 * Removed FEATURE_DENY_GZIP
178 * Revision 1.4 2001/10/02 15:31:12 oes
179 * Introduced show-request cgi
181 * Revision 1.3 2001/09/22 16:34:44 jongfoster
182 * Removing unneeded #includes
184 * Revision 1.2 2001/09/19 18:01:11 oes
185 * Fixed comments; cosmetics
187 * Revision 1.1 2001/09/16 17:08:54 jongfoster
188 * Moving simple CGI functions from cgi.c to new file cgisimple.c
191 **********************************************************************/
197 #include <sys/types.h>
204 #define snprintf _snprintf
205 #endif /* def _WIN32 */
209 #include "cgisimple.h"
215 #include "miscutil.h"
218 #include "urlmatch.h"
221 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
224 static char *show_rcs(void);
225 static jb_err show_defines(struct map *exports);
228 /*********************************************************************
230 * Function : cgi_default
232 * Description : CGI function that is called for the CGI_SITE_1_HOST
233 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
234 * Boring - only exports the default exports.
237 * 1 : csp = Current client state (buffers, headers, etc...)
238 * 2 : rsp = http_response data structure for output
239 * 3 : parameters = map of cgi parameters
241 * CGI Parameters : none
243 * Returns : JB_ERR_OK on success
244 * JB_ERR_MEMORY on out-of-memory
246 *********************************************************************/
247 jb_err cgi_default(struct client_state *csp,
248 struct http_response *rsp,
249 const struct map *parameters)
256 if (NULL == (exports = default_exports(csp, "")))
258 return JB_ERR_MEMORY;
261 return template_fill_for_cgi(csp, "default", exports, rsp);
265 /*********************************************************************
267 * Function : cgi_error_404
269 * Description : CGI function that is called if an unknown action was
273 * 1 : csp = Current client state (buffers, headers, etc...)
274 * 2 : rsp = http_response data structure for output
275 * 3 : parameters = map of cgi parameters
277 * CGI Parameters : none
279 * Returns : JB_ERR_OK on success
280 * JB_ERR_MEMORY on out-of-memory error.
282 *********************************************************************/
283 jb_err cgi_error_404(struct client_state *csp,
284 struct http_response *rsp,
285 const struct map *parameters)
293 if (NULL == (exports = default_exports(csp, NULL)))
295 return JB_ERR_MEMORY;
298 rsp->status = strdup("404 Privoxy configuration page not found");
299 if (rsp->status == NULL)
302 return JB_ERR_MEMORY;
305 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
309 #ifdef FEATURE_GRACEFUL_TERMINATION
310 /*********************************************************************
314 * Description : CGI function to shut down Privoxy.
315 * NOTE: Turning this on in a production build
316 * would be a BAD idea. An EXTREMELY BAD idea.
317 * In short, don't do it.
320 * 1 : csp = Current client state (buffers, headers, etc...)
321 * 2 : rsp = http_response data structure for output
322 * 3 : parameters = map of cgi parameters
324 * CGI Parameters : none
326 * Returns : JB_ERR_OK on success
327 * JB_ERR_MEMORY on out-of-memory error.
329 *********************************************************************/
330 jb_err cgi_die (struct client_state *csp,
331 struct http_response *rsp,
332 const struct map *parameters)
342 * I don't really care what gets sent back to the browser.
343 * Take the easy option - "out of memory" page.
346 return JB_ERR_MEMORY;
348 #endif /* def FEATURE_GRACEFUL_TERMINATION */
351 /*********************************************************************
353 * Function : cgi_show_request
355 * Description : Show the client's request and what sed() would have
359 * 1 : csp = Current client state (buffers, headers, etc...)
360 * 2 : rsp = http_response data structure for output
361 * 3 : parameters = map of cgi parameters
363 * CGI Parameters : none
365 * Returns : JB_ERR_OK on success
366 * JB_ERR_MEMORY on out-of-memory error.
368 *********************************************************************/
369 jb_err cgi_show_request(struct client_state *csp,
370 struct http_response *rsp,
371 const struct map *parameters)
380 if (NULL == (exports = default_exports(csp, "show-request")))
382 return JB_ERR_MEMORY;
386 * Repair the damage done to the IOB by get_header()
388 for (p = csp->iob->buf; p < csp->iob->eod; p++)
390 if (*p == '\0') *p = '\n';
394 * Export the original client's request and the one we would
395 * be sending to the server if this wasn't a CGI call
398 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
401 return JB_ERR_MEMORY;
404 if (map(exports, "processed-request", 1, html_encode_and_free_original(
405 sed(client_patterns, add_client_headers, csp)), 0))
408 return JB_ERR_MEMORY;
411 return template_fill_for_cgi(csp, "show-request", exports, rsp);
415 /*********************************************************************
417 * Function : cgi_send_banner
419 * Description : CGI function that returns a banner.
422 * 1 : csp = Current client state (buffers, headers, etc...)
423 * 2 : rsp = http_response data structure for output
424 * 3 : parameters = map of cgi parameters
427 * type : Selects the type of banner between "trans", "logo",
428 * and "auto". Defaults to "logo" if absent or invalid.
429 * "auto" means to select as if we were image-blocking.
430 * (Only the first character really counts; b and t are
433 * Returns : JB_ERR_OK on success
434 * JB_ERR_MEMORY on out-of-memory error.
436 *********************************************************************/
437 jb_err cgi_send_banner(struct client_state *csp,
438 struct http_response *rsp,
439 const struct map *parameters)
441 char imagetype = lookup(parameters, "type")[0];
444 * If type is auto, then determine the right thing
445 * to do from the set-image-blocker action
447 if (imagetype == 'a')
454 #ifdef FEATURE_IMAGE_BLOCKING
455 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
457 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
458 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
459 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
463 /* Use default - nothing to do here. */
465 else if (0 == strcmpic(p, "blank"))
469 else if (0 == strcmpic(p, "pattern"))
475 * If the action is to call this CGI, determine
478 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
480 imagetype = p[sizeof(prefix1) - 1];
482 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
484 imagetype = p[sizeof(prefix2) - 1];
488 * Everything else must (should) be a URL to
496 #endif /* def FEATURE_IMAGE_BLOCKING */
500 * Now imagetype is either the non-auto type we were called with,
501 * or it was auto and has since been determined. In any case, we
502 * can proceed to actually answering the request by sending a redirect
503 * or an image as appropriate:
505 if (imagetype == 'r')
507 rsp->status = strdup("302 Local Redirect from Privoxy");
508 if (rsp->status == NULL)
510 return JB_ERR_MEMORY;
512 if (enlist_unique_header(rsp->headers, "Location",
513 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
515 return JB_ERR_MEMORY;
520 if ((imagetype == 'b') || (imagetype == 't'))
522 rsp->body = bindup(image_blank_data, image_blank_length);
523 rsp->content_length = image_blank_length;
527 rsp->body = bindup(image_pattern_data, image_pattern_length);
528 rsp->content_length = image_pattern_length;
531 if (rsp->body == NULL)
533 return JB_ERR_MEMORY;
535 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
537 return JB_ERR_MEMORY;
548 /*********************************************************************
550 * Function : cgi_transparent_image
552 * Description : CGI function that sends a 1x1 transparent image.
555 * 1 : csp = Current client state (buffers, headers, etc...)
556 * 2 : rsp = http_response data structure for output
557 * 3 : parameters = map of cgi parameters
559 * CGI Parameters : None
561 * Returns : JB_ERR_OK on success
562 * JB_ERR_MEMORY on out-of-memory error.
564 *********************************************************************/
565 jb_err cgi_transparent_image(struct client_state *csp,
566 struct http_response *rsp,
567 const struct map *parameters)
569 rsp->body = bindup(image_blank_data, image_blank_length);
570 rsp->content_length = image_blank_length;
572 if (rsp->body == NULL)
574 return JB_ERR_MEMORY;
577 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
579 return JB_ERR_MEMORY;
589 /*********************************************************************
591 * Function : cgi_send_stylesheet
593 * Description : CGI function that sends a css stylesheet found
594 * in the cgi-style.css template
597 * 1 : csp = Current client state (buffers, headers, etc...)
598 * 2 : rsp = http_response data structure for output
599 * 3 : parameters = map of cgi parameters
601 * CGI Parameters : None
603 * Returns : JB_ERR_OK on success
604 * JB_ERR_MEMORY on out-of-memory error.
606 *********************************************************************/
607 jb_err cgi_send_stylesheet(struct client_state *csp,
608 struct http_response *rsp,
609 const struct map *parameters)
616 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
618 if (err == JB_ERR_FILE)
621 * No way to tell user; send empty stylesheet
623 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
627 return err; /* JB_ERR_MEMORY */
630 if (enlist(rsp->headers, "Content-Type: text/css"))
632 return JB_ERR_MEMORY;
640 /*********************************************************************
642 * Function : cgi_show_version
644 * Description : CGI function that returns a a web page describing the
645 * file versions of Privoxy.
648 * 1 : csp = Current client state (buffers, headers, etc...)
649 * 2 : rsp = http_response data structure for output
650 * 3 : parameters = map of cgi parameters
652 * CGI Parameters : none
654 * Returns : JB_ERR_OK on success
655 * JB_ERR_MEMORY on out-of-memory error.
657 *********************************************************************/
658 jb_err cgi_show_version(struct client_state *csp,
659 struct http_response *rsp,
660 const struct map *parameters)
668 if (NULL == (exports = default_exports(csp, "show-version")))
670 return JB_ERR_MEMORY;
673 if (map(exports, "sourceversions", 1, show_rcs(), 0))
676 return JB_ERR_MEMORY;
679 return template_fill_for_cgi(csp, "show-version", exports, rsp);
683 /*********************************************************************
685 * Function : cgi_show_status
687 * Description : CGI function that returns a a web page describing the
688 * current status of Privoxy.
691 * 1 : csp = Current client state (buffers, headers, etc...)
692 * 2 : rsp = http_response data structure for output
693 * 3 : parameters = map of cgi parameters
696 * file : Which file to show. Only first letter is checked,
698 * - "p"ermissions (actions) file
701 * Default is to show menu and other information.
703 * Returns : JB_ERR_OK on success
704 * JB_ERR_MEMORY on out-of-memory error.
706 *********************************************************************/
707 jb_err cgi_show_status(struct client_state *csp,
708 struct http_response *rsp,
709 const struct map *parameters)
716 char buf[BUFFER_SIZE];
717 const char * filename = NULL;
718 char * file_description = NULL;
719 #ifdef FEATURE_STATISTICS
720 float perc_rej; /* Percentage of http requests rejected */
722 int local_urls_rejected;
723 #endif /* ndef FEATURE_STATISTICS */
724 struct file_list * fl;
725 struct url_actions * b;
726 jb_err err = JB_ERR_OK;
734 if (NULL == (exports = default_exports(csp, "show-status")))
736 return JB_ERR_MEMORY;
739 switch (*(lookup(parameters, "file")))
742 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_ACTION_FILES && csp->actions_list[i])
744 filename = csp->actions_list[i]->filename;
745 file_description = "Actions File";
752 filename = csp->rlist->filename;
753 file_description = "Filter File";
761 filename = csp->tlist->filename;
762 file_description = "Trust File";
765 #endif /* def FEATURE_TRUST */
768 if (NULL != filename)
770 if ( map(exports, "file-description", 1, file_description, 1)
771 || map(exports, "filepath", 1, html_encode(filename), 0) )
774 return JB_ERR_MEMORY;
777 if ((fp = fopen(filename, "r")) == NULL)
779 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
782 return JB_ERR_MEMORY;
788 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
790 string_join (&s, html_encode(buf));
794 if (map(exports, "contents", 1, s, 0))
797 return JB_ERR_MEMORY;
801 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
805 for (j = 0; (s != NULL) && (j < Argc); j++)
807 if (!err) err = string_join (&s, html_encode(Argv[j]));
808 if (!err) err = string_append(&s, " ");
810 if (!err) err = map(exports, "invocation", 1, s, 0);
812 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
813 if (!err) err = show_defines(exports);
818 return JB_ERR_MEMORY;
821 #ifdef FEATURE_STATISTICS
822 local_urls_read = urls_read;
823 local_urls_rejected = urls_rejected;
826 * Need to alter the stats not to include the fetch of this
829 * Can't do following thread safely! doh!
832 * urls_rejected--; * This will be incremented subsequently *
835 if (local_urls_read == 0)
837 if (!err) err = map_block_killer(exports, "have-stats");
841 if (!err) err = map_block_killer(exports, "have-no-stats");
843 perc_rej = (float)local_urls_rejected * 100.0F /
844 (float)local_urls_read;
846 sprintf(buf, "%d", local_urls_read);
847 if (!err) err = map(exports, "requests-received", 1, buf, 1);
849 sprintf(buf, "%d", local_urls_rejected);
850 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
852 sprintf(buf, "%6.2f", perc_rej);
853 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
856 #else /* ndef FEATURE_STATISTICS */
857 err = err || map_block_killer(exports, "statistics");
858 #endif /* ndef FEATURE_STATISTICS */
861 * List all action files in use, together with view and edit links,
862 * except for standard.action, which should only be viewable. (Not
863 * enforced in the editor itself)
864 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
867 for (i = 0; i < MAX_ACTION_FILES; i++)
869 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
871 if (!err) err = string_append(&s, "<tr><td>");
872 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
873 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a> ", i);
874 if (!err) err = string_append(&s, buf);
876 #ifdef FEATURE_CGI_EDIT_ACTIONS
877 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
879 snprintf(buf, 100, "<a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
880 if (!err) err = string_append(&s, buf);
884 if (!err) err = string_append(&s, "</td></tr>\n");
889 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
893 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
898 if (!err) err = map(exports, "re-filter-filename", 1, html_encode(csp->rlist->filename), 0);
902 if (!err) err = map(exports, "re-filter-filename", 1, "None specified", 1);
903 if (!err) err = map_block_killer(exports, "have-filterfile");
909 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
913 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
914 if (!err) err = map_block_killer(exports, "have-trustfile");
917 if (!err) err = map_block_killer(exports, "trust-support");
918 #endif /* ndef FEATURE_TRUST */
923 return JB_ERR_MEMORY;
926 return template_fill_for_cgi(csp, "show-status", exports, rsp);
930 /*********************************************************************
932 * Function : cgi_show_url_info
934 * Description : CGI function that determines and shows which actions
935 * Privoxy will perform for a given url, and which
936 * matches starting from the defaults have lead to that.
939 * 1 : csp = Current client state (buffers, headers, etc...)
940 * 2 : rsp = http_response data structure for output
941 * 3 : parameters = map of cgi parameters
944 * url : The url whose actions are to be determined.
945 * If url is unset, the url-given conditional will be
946 * set, so that all but the form can be suppressed in
949 * Returns : JB_ERR_OK on success
950 * JB_ERR_MEMORY on out-of-memory error.
952 *********************************************************************/
953 jb_err cgi_show_url_info(struct client_state *csp,
954 struct http_response *rsp,
955 const struct map *parameters)
965 if (NULL == (exports = default_exports(csp, "show-url-info")))
967 return JB_ERR_MEMORY;
971 * Get the url= parameter (if present) and remove any leading/trailing spaces.
973 url_param = strdup(lookup(parameters, "url"));
974 if (url_param == NULL)
977 return JB_ERR_MEMORY;
982 * Handle prefixes. 4 possibilities:
983 * 1) "http://" or "https://" prefix present and followed by URL - OK
984 * 2) Only the "http://" or "https://" part is present, no URL - change
985 * to empty string so it will be detected later as "no URL".
986 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
988 * 4) Parameter not specified or is empty string - let this fall through
989 * for now, next block of code will handle it.
991 if (0 == strncmp(url_param, "http://", 7))
993 if (url_param[7] == '\0')
996 * Empty URL (just prefix).
997 * Make it totally empty so it's caught by the next if()
1002 else if (0 == strncmp(url_param, "https://", 8))
1004 if (url_param[8] == '\0')
1007 * Empty URL (just prefix).
1008 * Make it totally empty so it's caught by the next if()
1010 url_param[0] = '\0';
1013 else if (url_param[0] != '\0')
1016 * Unknown prefix - assume http://
1018 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1019 if (NULL == url_param_prefixed)
1023 return JB_ERR_MEMORY;
1025 strcpy(url_param_prefixed, "http://");
1026 strcpy(url_param_prefixed + 7, url_param);
1028 url_param = url_param_prefixed;
1032 if (url_param[0] == '\0')
1034 /* URL paramater not specified, display query form only. */
1036 if (map_block_killer(exports, "url-given")
1037 || map(exports, "url", 1, "", 1))
1040 return JB_ERR_MEMORY;
1045 /* Given a URL, so query it. */
1050 struct file_list *fl;
1051 struct url_actions *b;
1052 struct http_request url_to_query[1];
1053 struct current_action_spec action[1];
1056 if (map(exports, "url", 1, html_encode(url_param), 0))
1060 return JB_ERR_MEMORY;
1063 init_current_action(action);
1065 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1067 free_current_action(action);
1070 return JB_ERR_MEMORY;
1073 err = parse_http_url(url_param, url_to_query, csp);
1077 if (err == JB_ERR_MEMORY)
1079 free_current_action(action);
1081 return JB_ERR_MEMORY;
1087 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1088 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1090 free_current_action(action);
1095 return JB_ERR_MEMORY;
1098 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1102 * We have a warning about SSL paths. Hide it for insecure sites.
1104 if (!url_to_query->ssl)
1106 if (map_block_killer(exports, "https"))
1108 free_current_action(action);
1110 return JB_ERR_MEMORY;
1114 matches = strdup("<table class=\"transparent\">");
1116 for (i = 0; i < MAX_ACTION_FILES; i++)
1118 if (NULL == csp->config->actions_file_short[i]
1119 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1123 if ((fl = csp->actions_list[i]) != NULL)
1125 if ((b = fl->f) != NULL)
1127 /* FIXME: Hardcoded HTML! */
1128 string_append(&matches, "<tr><th>In file: ");
1129 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1130 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1131 string_append(&matches, buf);
1132 string_append(&matches, "View</a> <a class=\"cmd\" href=\"/edit-actions-list?f=");
1133 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1134 string_append(&matches, "\">Edit</a></th></tr>\n");
1141 for (; (b != NULL) && (matches != NULL); b = b->next)
1143 if (url_match(b->url, url_to_query))
1145 string_append(&matches, "<tr><td>{");
1146 string_join (&matches, actions_to_html(csp, b->action));
1147 string_append(&matches, " }</b><br>\n<code>");
1148 string_join (&matches, html_encode(b->url->spec));
1149 string_append(&matches, "</code></td></tr>\n");
1151 if (merge_current_action(action, b->action))
1154 free_http_request(url_to_query);
1155 free_current_action(action);
1157 return JB_ERR_MEMORY;
1165 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1168 string_append(&matches, "</table>\n");
1170 free_http_request(url_to_query);
1172 if (matches == NULL)
1174 free_current_action(action);
1176 return JB_ERR_MEMORY;
1179 if (map(exports, "matches", 1, matches , 0))
1181 free_current_action(action);
1183 return JB_ERR_MEMORY;
1186 s = current_action_to_html(csp, action);
1188 free_current_action(action);
1190 if (map(exports, "final", 1, s, 0))
1193 return JB_ERR_MEMORY;
1197 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1201 /*********************************************************************
1203 * Function : cgi_robots_txt
1205 * Description : CGI function to return "/robots.txt".
1208 * 1 : csp = Current client state (buffers, headers, etc...)
1209 * 2 : rsp = http_response data structure for output
1210 * 3 : parameters = map of cgi parameters
1212 * CGI Parameters : None
1214 * Returns : JB_ERR_OK on success
1215 * JB_ERR_MEMORY on out-of-memory error.
1217 *********************************************************************/
1218 jb_err cgi_robots_txt(struct client_state *csp,
1219 struct http_response *rsp,
1220 const struct map *parameters)
1226 "# This is the Privoxy control interface.\n"
1227 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1233 if (rsp->body == NULL)
1235 return JB_ERR_MEMORY;
1238 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1242 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1243 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1245 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1249 /*********************************************************************
1251 * Function : show_defines
1253 * Description : Add to a map the state od all conditional #defines
1254 * used when building
1257 * 1 : exports = map to extend
1259 * Returns : JB_ERR_OK on success
1260 * JB_ERR_MEMORY on out-of-memory error.
1262 *********************************************************************/
1263 static jb_err show_defines(struct map *exports)
1265 jb_err err = JB_ERR_OK;
1268 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1269 #else /* ifndef FEATURE_ACL */
1270 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1271 #endif /* ndef FEATURE_ACL */
1273 #ifdef FEATURE_CGI_EDIT_ACTIONS
1274 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1275 #else /* ifndef FEATURE_COOKIE_JAR */
1276 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1277 #endif /* ndef FEATURE_COOKIE_JAR */
1279 #ifdef FEATURE_COOKIE_JAR
1280 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1281 #else /* ifndef FEATURE_COOKIE_JAR */
1282 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1283 #endif /* ndef FEATURE_COOKIE_JAR */
1285 #ifdef FEATURE_FAST_REDIRECTS
1286 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1287 #else /* ifndef FEATURE_FAST_REDIRECTS */
1288 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1289 #endif /* ndef FEATURE_FAST_REDIRECTS */
1291 #ifdef FEATURE_FORCE_LOAD
1292 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1293 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1294 #else /* ifndef FEATURE_FORCE_LOAD */
1295 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1296 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1297 #endif /* ndef FEATURE_FORCE_LOAD */
1299 #ifdef FEATURE_IMAGE_BLOCKING
1300 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1301 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1302 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1303 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1305 #ifdef FEATURE_IMAGE_DETECT_MSIE
1306 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1307 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1308 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1309 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1311 #ifdef FEATURE_KILL_POPUPS
1312 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1313 #else /* ifndef FEATURE_KILL_POPUPS */
1314 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1315 #endif /* ndef FEATURE_KILL_POPUPS */
1317 #ifdef FEATURE_NO_GIFS
1318 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1319 #else /* ifndef FEATURE_NO_GIFS */
1320 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1321 #endif /* ndef FEATURE_NO_GIFS */
1323 #ifdef FEATURE_PTHREAD
1324 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1325 #else /* ifndef FEATURE_PTHREAD */
1326 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1327 #endif /* ndef FEATURE_PTHREAD */
1329 #ifdef FEATURE_STATISTICS
1330 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1331 #else /* ifndef FEATURE_STATISTICS */
1332 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1333 #endif /* ndef FEATURE_STATISTICS */
1335 #ifdef FEATURE_TOGGLE
1336 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1337 #else /* ifndef FEATURE_TOGGLE */
1338 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1339 #endif /* ndef FEATURE_TOGGLE */
1341 #ifdef FEATURE_TRUST
1342 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1343 #else /* ifndef FEATURE_TRUST */
1344 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1345 #endif /* ndef FEATURE_TRUST */
1348 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1349 #else /* ifndef STATIC_PCRE */
1350 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1351 #endif /* ndef STATIC_PCRE */
1354 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1355 #else /* ifndef STATIC_PCRS */
1356 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1357 #endif /* ndef STATIC_PCRS */
1363 /*********************************************************************
1365 * Function : show_rcs
1367 * Description : Create a string with the rcs info for all sourcefiles
1371 * Returns : A string, or NULL on out-of-memory.
1373 *********************************************************************/
1374 static char *show_rcs(void)
1376 char *result = strdup("");
1377 char buf[BUFFER_SIZE];
1379 /* Instead of including *all* dot h's in the project (thus creating a
1380 * tremendous amount of dependencies), I will concede to declaring them
1381 * as extern's. This forces the developer to add to this list, but oh well.
1384 #define SHOW_RCS(__x) \
1386 extern const char __x[]; \
1387 sprintf(buf, "%s\n", __x); \
1388 string_append(&result, buf); \
1391 /* In alphabetical order */
1392 SHOW_RCS(actions_h_rcs)
1393 SHOW_RCS(actions_rcs)
1395 SHOW_RCS(amiga_h_rcs)
1397 #endif /* def AMIGA */
1400 #ifdef FEATURE_CGI_EDIT_ACTIONS
1401 SHOW_RCS(cgiedit_h_rcs)
1402 SHOW_RCS(cgiedit_rcs)
1403 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1404 SHOW_RCS(cgisimple_h_rcs)
1405 SHOW_RCS(cgisimple_rcs)
1407 SHOW_RCS(cygwin_h_rcs)
1409 SHOW_RCS(deanimate_h_rcs)
1410 SHOW_RCS(deanimate_rcs)
1411 SHOW_RCS(encode_h_rcs)
1412 SHOW_RCS(encode_rcs)
1413 SHOW_RCS(errlog_h_rcs)
1414 SHOW_RCS(errlog_rcs)
1415 SHOW_RCS(filters_h_rcs)
1416 SHOW_RCS(filters_rcs)
1417 SHOW_RCS(gateway_h_rcs)
1418 SHOW_RCS(gateway_rcs)
1419 SHOW_RCS(jbsockets_h_rcs)
1420 SHOW_RCS(jbsockets_rcs)
1423 #ifdef FEATURE_KILL_POPUPS
1424 SHOW_RCS(killpopup_h_rcs)
1425 SHOW_RCS(killpopup_rcs)
1426 #endif /* def FEATURE_KILL_POPUPS */
1427 SHOW_RCS(list_h_rcs)
1429 SHOW_RCS(loadcfg_h_rcs)
1430 SHOW_RCS(loadcfg_rcs)
1431 SHOW_RCS(loaders_h_rcs)
1432 SHOW_RCS(loaders_rcs)
1433 SHOW_RCS(miscutil_h_rcs)
1434 SHOW_RCS(miscutil_rcs)
1435 SHOW_RCS(parsers_h_rcs)
1436 SHOW_RCS(parsers_rcs)
1438 SHOW_RCS(pcrs_h_rcs)
1439 SHOW_RCS(project_h_rcs)
1440 SHOW_RCS(ssplit_h_rcs)
1441 SHOW_RCS(ssplit_rcs)
1442 SHOW_RCS(urlmatch_h_rcs)
1443 SHOW_RCS(urlmatch_rcs)
1445 #ifndef _WIN_CONSOLE
1446 SHOW_RCS(w32log_h_rcs)
1447 SHOW_RCS(w32log_rcs)
1448 SHOW_RCS(w32res_h_rcs)
1449 SHOW_RCS(w32taskbar_h_rcs)
1450 SHOW_RCS(w32taskbar_rcs)
1451 #endif /* ndef _WIN_CONSOLE */
1452 SHOW_RCS(win32_h_rcs)
1454 #endif /* def _WIN32 */