-const char cgi_rcs[] = "$Id: cgi.c,v 1.1 2001/06/03 11:03:48 oes Exp $";
+const char cgi_rcs[] = "$Id: cgi.c,v 1.33 2001/10/14 22:28:41 jongfoster Exp $";
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/cgi.c,v $
*
* Purpose : Declares functions to intercept request, generate
* html or gif answers, and to compose HTTP resonses.
+ * This only contains the framework functions, the
+ * actual handler functions are declared elsewhere.
*
* Functions declared include:
*
*
* Revisions :
* $Log: cgi.c,v $
- * Revision 1.1 2001/06/03 11:03:48 oes
- * Makefile/in
+ * Revision 1.33 2001/10/14 22:28:41 jongfoster
+ * Fixing stupid typo.
+ *
+ * Revision 1.32 2001/10/14 22:20:18 jongfoster
+ * - Changes to CGI dispatching method to match CGI names exactly,
+ * rather than doing a prefix match.
+ * - No longer need to count the length of the CGI handler names by hand.
+ * - Adding new handler for 404 error when disptching a CGI, if none of
+ * the handlers match.
+ * - Adding new handlers for CGI actionsfile editor.
+ *
+ * Revision 1.31 2001/10/10 10:56:39 oes
+ * Failiure to load template now fatal. Before, the user got a hard-to-understand assertion failure from cgi.c
+ *
+ * Revision 1.30 2001/10/02 15:30:57 oes
+ * Introduced show-request cgi
+ *
+ * Revision 1.29 2001/09/20 15:47:44 steudten
+ *
+ * Fix BUG: Modify int size to size_t size in fill_template()
+ * - removes big trouble on machines where sizeof(int) != sizeof(size_t).
*
- * introduced cgi.c
+ * Revision 1.28 2001/09/19 18:00:37 oes
+ * - Deletef time() FIXME (Can't fail under Linux either, if
+ * the argument is guaranteed to be in out address space,
+ * which it is.)
+ * - Fixed comments
+ * - Pointer notation cosmetics
+ * - Fixed a minor bug in template_fill(): Failiure of
+ * pcrs_execute() now secure.
*
- * actions.c:
+ * Revision 1.27 2001/09/16 17:08:54 jongfoster
+ * Moving simple CGI functions from cgi.c to new file cgisimple.c
*
- * adapted to new enlist_unique arg format
+ * Revision 1.26 2001/09/16 15:47:37 jongfoster
+ * First version of CGI-based edit interface. This is very much a
+ * work-in-progress, and you can't actually use it to edit anything
+ * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
+ * to have any effect.
*
- * conf loadcfg.c
+ * Revision 1.25 2001/09/16 15:02:35 jongfoster
+ * Adding i.j.b/robots.txt.
+ * Inlining add_stats() since it's only ever called from one place.
*
- * introduced confdir option
+ * Revision 1.24 2001/09/16 11:38:01 jongfoster
+ * Splitting fill_template() into 2 functions:
+ * template_load() loads the file
+ * template_fill() performs the PCRS regexps.
+ * This is because the CGI edit interface has a "table row"
+ * template which is used many times in the page - this
+ * change means it's only loaded from disk once.
*
- * filters.c filtrers.h
+ * Revision 1.23 2001/09/16 11:16:05 jongfoster
+ * Better error handling in dispatch_cgi() and parse_cgi_parameters()
*
- * extracted-CGI relevant stuff
+ * Revision 1.22 2001/09/16 11:00:10 jongfoster
+ * New function alloc_http_response, for symmetry with free_http_response
*
- * jbsockets.c
+ * Revision 1.21 2001/09/13 23:53:03 jongfoster
+ * Support for both static and dynamically generated CGI pages.
+ * Correctly setting Last-Modified: and Expires: HTTP headers.
*
- * filled comment
+ * Revision 1.20 2001/09/13 23:40:36 jongfoster
+ * (Cosmetic only) Indentation correction
*
- * jcc.c
+ * Revision 1.19 2001/09/13 23:31:25 jongfoster
+ * Moving image data to cgi.c rather than cgi.h.
*
- * support for new cgi mechansim
+ * Revision 1.18 2001/08/05 16:06:20 jongfoster
+ * Modifiying "struct map" so that there are now separate header and
+ * "map_entry" structures. This means that functions which modify a
+ * map no longer need to return a pointer to the modified map.
+ * Also, it no longer reverses the order of the entries (which may be
+ * important with some advanced template substitutions).
*
- * list.c list.h
+ * Revision 1.17 2001/08/05 15:57:38 oes
+ * Adapted finish_http_response to new list_to_text
*
- * functions for new list type: "map"
- * extended enlist_unique
+ * Revision 1.16 2001/08/01 21:33:18 jongfoster
+ * Changes to fill_template() that reduce memory usage without having
+ * an impact on performance. I also renamed some variables so as not
+ * to clash with the C++ keywords "new" and "template".
*
- * miscutil.c .h
- * introduced bindup()
+ * Revision 1.15 2001/08/01 21:19:22 jongfoster
+ * Moving file version information to a separate CGI page.
*
- * parsers.c parsers.h
+ * Revision 1.14 2001/08/01 00:19:03 jongfoster
+ * New function: map_conditional() for an if-then-else syntax.
+ * Changing to use new version of show_defines()
*
- * deleted const struct interceptors
+ * Revision 1.13 2001/07/30 22:08:36 jongfoster
+ * Tidying up #defines:
+ * - All feature #defines are now of the form FEATURE_xxx
+ * - Permanently turned off WIN_GUI_EDIT
+ * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
*
- * pcrs.c
- * added FIXME
+ * Revision 1.12 2001/07/29 18:47:05 jongfoster
+ * Adding missing #include "loadcfg.h"
*
- * project.h
+ * Revision 1.11 2001/07/18 17:24:37 oes
+ * Changed to conform to new pcrs interface
*
- * added struct map
- * added struct http_response
- * changes struct interceptors to struct cgi_dispatcher
- * moved HTML stuff to cgi.h
+ * Revision 1.10 2001/07/13 13:53:13 oes
+ * Removed all #ifdef PCRS and related code
*
- * re_filterfile:
+ * Revision 1.9 2001/06/29 21:45:41 oes
+ * Indentation, CRLF->LF, Tab-> Space
*
- * changed
+ * Revision 1.8 2001/06/29 13:21:46 oes
+ * - Cosmetics: renamed and reordered functions, variables,
+ * texts, improved comments etc
*
- * showargs.c
- * NO TIME LEFT
+ * - Removed ij_untrusted_url() The relevant
+ * info is now part of the "untrusted" page,
+ * which is generated by filters.c:trust_url()
*
+ * - Generators of content now call finish_http_response()
+ * themselves, making jcc.c:chat() a little less
+ * cluttered
*
+ * - Removed obsolete "Pragma: no-cache" from our headers
+ *
+ * - http_responses now know their head length
+ *
+ * - fill_template now uses the new interface to pcrs, so that
+ * - long jobs (like whole files) no longer have to be assembled
+ * in a fixed size buffer
+ * - the new T (trivial) option is used, and the replacement may
+ * contain Perl syntax backrefs without confusing pcrs
+ *
+ * - Introduced default_exports() which generates a set of exports
+ * common to all CGIs and other content generators
+ *
+ * - Introduced convenience function map_block_killer()
+ *
+ * - Introduced convenience function make_menu()
+ *
+ * - Introduced CGI-like function error_response() which generates
+ * the "No such domain" and "Connect failed" messages using the
+ * CGI platform
+ *
+ * - cgi_show_url_info:
+ * - adapted to new CGI features
+ * - form and answers now generated from same template
+ * - http:// prefix in URL now OK
+ *
+ * - cgi_show_status:
+ * - adapted to new CGI features
+ * - no longer uses csp->init_proxy_args
+ *
+ * - cgi_default:
+ * - moved menu generation to make_menu()
+ *
+ * - add_stats now writes single export map entries instead
+ * of a fixed string
+ *
+ * - Moved redirect_url() to filters.c
+ *
+ * - Fixed mem leak in free_http_response(), map_block_killer(),
+ *
+ * - Removed logentry from cancelled commit
+ *
+ * Revision 1.7 2001/06/09 10:51:58 jongfoster
+ * Changing "show URL info" handler to new style.
+ * Changing BUFSIZ ==> BUFFER_SIZE
+ *
+ * Revision 1.6 2001/06/07 23:05:19 jongfoster
+ * Removing code related to old forward and ACL files.
+ *
+ * Revision 1.5 2001/06/05 19:59:16 jongfoster
+ * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
+ *
+ * Revision 1.4 2001/06/04 10:41:52 swa
+ * show version string of cgi.h and cgi.c
+ *
+ * Revision 1.3 2001/06/03 19:12:16 oes
+ * introduced new cgi handling
+ *
+ * No revisions before 1.3
*
**********************************************************************/
\f
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
+#include <assert.h>
+
+#ifdef _WIN32
+#define snprintf _snprintf
+#endif /* def _WIN32 */
#include "project.h"
#include "cgi.h"
#include "list.h"
-#include "pcrs.h"
#include "encode.h"
#include "ssplit.h"
-#include "jcc.h"
-#include "filters.h"
-#include "actions.h"
#include "errlog.h"
#include "miscutil.h"
-#include "showargs.h"
+#include "cgisimple.h"
+#ifdef FEATURE_CGI_EDIT_ACTIONS
+#include "cgiedit.h"
+#endif /* def FEATURE_CGI_EDIT_ACTIONS */
+const char cgi_h_rcs[] = CGI_H_VERSION;
-const struct cgi_dispatcher cgi_dispatchers[] = {
+static const struct cgi_dispatcher cgi_dispatchers[] = {
+ { "",
+ cgi_default,
+ "Junkbuster main page" },
{ "show-status",
- 11, cgi_show_status,
- "Show information about the version and configuration" },
-/* { "show-url-info",
- 13, cgi_show_url_info,
- "Show which actions apply to a URL and why" },*/
+ cgi_show_status,
+ "Show information about the current configuration" },
+ { "show-version",
+ cgi_show_version,
+ "Show the source code version numbers" },
+ { "show-request",
+ cgi_show_request,
+ "Show the client's request headers." },
+ { "show-url-info",
+ cgi_show_url_info,
+ "Show which actions apply to a URL and why" },
+#ifdef FEATURE_CGI_EDIT_ACTIONS
+ { "edit-actions",
+ cgi_edit_actions,
+ "Edit the actions list" },
+#endif /* def FEATURE_CGI_EDIT_ACTIONS */
+
+#ifdef FEATURE_CGI_EDIT_ACTIONS
+ { "edit-actions-for-url",
+ cgi_edit_actions_for_url,
+ NULL /* Edit the actions for (a) specified URL(s) */ },
+ { "edit-actions-list",
+ cgi_edit_actions_list,
+ NULL /* Edit the actions list */ },
+ { "edit-actions-submit",
+ cgi_edit_actions_submit,
+ NULL /* Change the actions for (a) specified URL(s) */ },
+#endif /* def FEATURE_CGI_EDIT_ACTIONS */
+ { "robots.txt",
+ cgi_robots_txt,
+ NULL /* Sends a robots.txt file to tell robots to go away. */ },
{ "send-banner",
- 11, cgi_send_banner,
- "HIDE Send the transparent or \"Junkbuster\" gif" },
-#ifdef TRUST_FILES
-/* { "untrusted-url",
- 15, ij_untrusted_url,
- "HIDE Show why a URL was not trusted" }, */
-#endif /* def TRUST_FILES */
- { "",
- 0, cgi_default,
- "HIDE Send a page linking to all unhidden CGIs" },
- { NULL, 0, NULL, NULL }
+ cgi_send_banner,
+ NULL /* Send the transparent or \"Junkbuster\" gif */ },
+ { NULL, /* NULL Indicates end of list and default page */
+ cgi_error_404,
+ NULL /* Unknown CGI page */ }
};
+/*
+ * Some images
+ *
+ * Hint: You can encode your own GIFs like this:
+ * perl -e 'while (read STDIN, $c, 1) { printf("\\%.3o,", unpack("C", $c)); }'
+ */
+
+const char image_junkbuster_gif_data[] =
+ "GIF89aD\000\013\000\360\000\000\000\000\000\377\377\377!"
+ "\371\004\001\000\000\001\000,\000\000\000\000D\000\013\000"
+ "\000\002a\214\217\251\313\355\277\000\200G&K\025\316hC\037"
+ "\200\234\230Y\2309\235S\230\266\206\372J\253<\3131\253\271"
+ "\270\215\342\254\013\203\371\202\264\334P\207\332\020o\266"
+ "N\215I\332=\211\312\3513\266:\026AK)\364\370\365aobr\305"
+ "\372\003S\275\274k2\354\254z\347?\335\274x\306^9\374\276"
+ "\037Q\000\000;";
+
+const int image_junkbuster_gif_length = sizeof(image_junkbuster_gif_data) - 1;
+
+
+const char image_blank_gif_data[] =
+ "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000"
+ "\000!\371\004\001\000\000\000\000,\000\000\000\000\001"
+ "\000\001\000\000\002\002D\001\000;";
+
+const int image_blank_gif_length = sizeof(image_blank_gif_data) - 1;
+
+
+static struct http_response *dispatch_known_cgi(struct client_state * csp,
+ const char * path);
+
+
/*********************************************************************
*
* Function : dispatch_cgi
*
* Description : Checks if a request URL has either the magical hostname
- * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
- * the (rest of the) path as a cgi name plus query string,
- * prepares a map that maps CGI parameter names to their values,
- * initializes the http_response struct, and calls the
- * relevant CGI handler function.
+ * i.j.b or matches HOME_PAGE_URL/config/. If so, it passes
+ * the (rest of the) path onto dispatch_known_cgi, which
+ * calls the relevant CGI handler function.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* Returns : http_response if match, NULL if nonmatch or handler fail
*
*********************************************************************/
-struct http_response *cgi_dispatch(struct client_state *csp)
+struct http_response *dispatch_cgi(struct client_state *csp)
{
- char *argstring = NULL;
- const struct cgi_dispatcher *d;
- struct map *param_list;
- struct http_response *response;
+ const char *host = csp->http->host;
+ const char *path = csp->http->path;
/*
* Should we intercept ?
*/
/* Either the host matches CGI_PREFIX_HOST ..*/
- if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
+ if ( (0 == strcmpic(host, CGI_PREFIX_HOST))
+ && (path[0] == '/') )
{
- /* ..then the path will all be for us */
- argstring = csp->http->path;
+ /* ..then the path will all be for us. Remove leading '/' */
+ path++;
}
- /* Or it's the host part of HOME_PAGE_URL ? */
- else if ( (0 == strcmpic(csp->http->host, *&HOME_PAGE_URL + 7 ))
- && (0 == strncmpic(csp->http->path,"/config", 7))
- && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
+ /* Or it's the host part HOME_PAGE_URL, and the path /config/ */
+ else if ( (0 == strcmpic(host, HOME_PAGE_URL + 7 ))
+ && (0 == strncmpic(path,"/config", 7)) )
{
- /* then it's everything following "/config" */
- argstring = csp->http->path + 7;
+ /* take everything following "/config" */
+ path += 7;
+ if (*path == '/')
+ {
+ /* skip the forward slash after "/config" */
+ path++;
+ }
+ else if (*path != '\0')
+ {
+ /* wierdness: URL is /configXXX, where XXX is some string */
+ return NULL;
+ }
}
else
{
+ /* Not a CGI */
return NULL;
}
/*
- * We have intercepted it.
+ * This is a CGI call.
*/
- /* Get mem for response */
- if (NULL == ( response = zalloc(sizeof(*response))))
+ return dispatch_known_cgi(csp, path);
+}
+
+
+/*********************************************************************
+ *
+ * Function : dispatch_known_cgi
+ *
+ * Description : Processes a CGI once dispatch_cgi has determined that
+ * it matches one of the magic prefixes. Parses the path
+ * as a cgi name plus query string, prepares a map that
+ * maps CGI parameter names to their values, initializes
+ * the http_response struct, and calls the relevant CGI
+ * handler function.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : path = Path of CGI, with the CGI prefix removed.
+ * Should not have a leading "/".
+ *
+ * Returns : http_response, or NULL on handler failure or out of
+ * memory.
+ *
+ *********************************************************************/
+static struct http_response *dispatch_known_cgi(struct client_state * csp,
+ const char * path)
+{
+ const struct cgi_dispatcher *d;
+ struct map *param_list;
+ struct http_response *rsp;
+ char *query_args_start;
+ char *path_copy;
+ int result;
+
+ if (NULL == (path_copy = strdup(path)))
{
return NULL;
}
- /* remove any leading slash */
- if (*argstring == '/')
+ query_args_start = path_copy;
+ while (*query_args_start && *query_args_start != '?')
+ {
+ query_args_start++;
+ }
+ if (*query_args_start == '?')
{
- argstring++;
+ *query_args_start++ = '\0';
+ }
+
+ if (NULL == (param_list = parse_cgi_parameters(query_args_start)))
+ {
+ free(path_copy);
+ return(NULL);
+ }
+
+
+ /*
+ * At this point:
+ * path_copy = CGI call name
+ * param_list = CGI params, as map
+ */
+
+ /* Get mem for response or fail*/
+ if (NULL == (rsp = alloc_http_response()))
+ {
+ free(path_copy);
+ free_map(param_list);
+ return NULL;
}
log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
csp->ip_addr_str, csp->http->cmd);
- for (d = cgi_dispatchers; d->handler; d++)
+ /* Find and start the right CGI function*/
+ for (d = cgi_dispatchers; FOREVER; d++)
{
- if (strncmp(argstring, d->name, d->name_length) == 0)
+ if ((d->name == NULL) || (strcmp(path_copy, d->name) == 0))
{
- param_list = parse_cgi(argstring + d->name_length);
- if ((d->handler)(csp, response, param_list))
- {
- freez(response);
- }
-
- free_map(param_list);
- return(response);
+ result = (d->handler)(csp, rsp, param_list);
+ free(path_copy);
+ free_map(param_list);
+ if (result)
+ {
+ /* Error in handler */
+ free_http_response(rsp);
+ return(NULL);
+ }
+ else
+ {
+ /* It worked */
+ return(finish_http_response(rsp));
+ }
}
}
-
- freez(response);
- return(NULL);
-
}
/*********************************************************************
*
- * Function : parse_cgi
+ * Function : parse_cgi_parameters
*
* Description : Parse a URL-encoded argument string into name/value
* pairs and store them in a struct map list.
*
* Parameters :
- * 1 : string = string to be parsed
+ * 1 : string = string to be parsed. Will be trashed.
*
- * Returns : poniter to param list, or NULL if failiure
+ * Returns : pointer to param list, or NULL if out of memory.
*
*********************************************************************/
-struct map *parse_cgi(char *argstring)
+static struct map *parse_cgi_parameters(char *argstring)
{
- char *tmp, *p;
- char *vector[BUFSIZ];
+ char *p;
+ char *vector[BUFFER_SIZE];
int pairs, i;
- struct map *cgi_params = NULL;
+ struct map *cgi_params;
- if(*argstring == '?') argstring++;
- tmp = strdup(argstring);
+ if (NULL == (cgi_params = new_map()))
+ {
+ return NULL;
+ }
- pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
+ pairs = ssplit(argstring, "&", vector, SZ(vector), 1, 1);
for (i = 0; i < pairs; i++)
{
if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
{
*p = '\0';
- cgi_params = map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
+ map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
}
}
- free(tmp);
return(cgi_params);
}
/*********************************************************************
*
- * Function : make_http_response
+ * Function : error_response
*
- * Description : Fill in the missing headers in an http response,
- * and flatten the headers to an http head.
+ * Description : returns an http_response that explains the reason
+ * why a request failed.
*
* Parameters :
- * 1 : rsp = pointer to http_response to be processed
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : templatename = Which template should be used for the answer
+ * 3 : errno = system error number
*
- * Returns : length of http head, or 0 on failiure
+ * Returns : NULL if no memory, else http_response
*
*********************************************************************/
-int make_http_response(struct http_response *rsp)
+struct http_response *error_response(struct client_state *csp, const char *templatename, int err)
{
- char buf[BUFSIZ];
+ struct http_response *rsp;
+ struct map * exports = default_exports(csp, NULL);
- /* Fill in the HTTP Status */
- sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
- enlist_first(rsp->headers, buf);
+ if (NULL == (rsp = alloc_http_response()))
+ {
+ return NULL;
+ }
- /* Set the Content-Length */
- if (rsp->content_length == 0)
- {
- rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
- }
+ map(exports, "host-html", 1, html_encode(csp->http->host), 0);
+ map(exports, "hostport", 1, csp->http->hostport, 1);
+ map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
+ map(exports, "path", 1, csp->http->path, 1);
+ map(exports, "path-html", 1, html_encode(csp->http->path), 0);
+ map(exports, "error", 1, safe_strerror(err), 0);
+ map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
+ rsp->body = template_load(csp, templatename);
+ template_fill(&rsp->body, exports);
+ free_map(exports);
- sprintf(buf, "Content-Length: %d", rsp->content_length);
- enlist(rsp->headers, buf);
+ if (!strcmp(templatename, "no-such-domain"))
+ {
+ rsp->status = strdup("404 No such domain");
+ }
+ else if (!strcmp(templatename, "connect-failed"))
+ {
+ rsp->status = strdup("503 Connect failed");
+ }
- /* Fill in the default headers FIXME: Are these correct? sequence OK? check rfc! */
- enlist_unique(rsp->headers, "Pragma: no-cache", 7);
- enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
- enlist_unique(rsp->headers, "Expires: Thu Jul 31, 1997 07:42:22 pm GMT", 8);
- enlist_unique(rsp->headers, "Content-Type: text/html", 13);
- enlist(rsp->headers, "");
-
+ return(finish_http_response(rsp));
- /* Write the head */
- if (NULL == (rsp->head = list_to_text(rsp->headers)))
- {
- free_http_response(rsp);
- return(0);
- }
-
- return(strlen(rsp->head));
}
-
+
/*********************************************************************
*
- * Function : free_http_response
+ * Function : get_http_time
*
- * Description : Free the memory occupied by an http_response
- * and its depandant structures.
+ * Description : Get the time in a format suitable for use in a
+ * HTTP header - e.g.:
+ * "Sun, 06 Nov 1994 08:49:37 GMT"
*
- * Parameters :
- * 1 : rsp = pointer to http_response to be freed
+ * Parameters :
+ * 1 : time_offset = Time returned will be current time
+ * plus this number of seconds.
+ * 2 : buf = Destination for result. Must be long enough
+ * to hold 29 characters plus a trailing zero.
*
* Returns : N/A
*
*********************************************************************/
-void free_http_response(struct http_response *rsp)
+void get_http_time(int time_offset, char *buf)
{
- if(rsp)
- {
- freez(rsp->status);
- freez(rsp->head);
- freez(rsp->body);
- destroy_list(rsp->headers);
- }
+ static const char day_names[7][4] =
+ { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+ static const char month_names[12][4] =
+ { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+ struct tm *t;
+ time_t current_time;
+
+ assert(buf);
+
+ time(¤t_time); /* get current time */
+
+ current_time += time_offset;
+
+ /* get and save the gmt */
+ t = gmtime(¤t_time);
+
+ /* Format: "Sun, 06 Nov 1994 08:49:37 GMT" */
+ snprintf(buf, 30,
+ "%s, %02d %s %4d %02d:%02d:%02d GMT",
+ day_names[t->tm_wday],
+ t->tm_mday,
+ month_names[t->tm_mon],
+ t->tm_year + 1900,
+ t->tm_hour,
+ t->tm_min,
+ t->tm_sec
+ );
+ buf[32] = '\0';
+
}
/*********************************************************************
*
- * Function : fill_template
+ * Function : finish_http_response
*
- * Description : CGI support function that loads a given HTML
- * template from the confdir, and fills it in
- * by replacing @name@ with value using pcrs,
- * for each item in the output map.
+ * Description : Fill in the missing headers in an http response,
+ * and flatten the headers to an http head.
*
* Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- * 3 : template = name of the HTML template to be used
- * 2 : answers = map with fill in symbol -> name pairs
- * FIXME: needs better name!
+ * 1 : rsp = pointer to http_response to be processed
*
- * Returns : char * with filled out form, or NULL if failiure
+ * Returns : http_response, or NULL on failiure
*
*********************************************************************/
-char *fill_template(struct client_state *csp, char *template, struct map *answers)
+struct http_response *finish_http_response(struct http_response *rsp)
{
- struct map *m;
- pcrs_job *job, *joblist = NULL;
- char buf[BUFSIZ];
- char *new, *old = NULL;
- int error, size;
- FILE *fp;
-
- /*
- * Open template file or fail
- */
- snprintf(buf, BUFSIZ, "%s/templates/%s", csp->config->confdir, template);
+ char buf[BUFFER_SIZE];
- if(NULL == (fp = fopen(buf, "r")))
- {
- log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
- return NULL;
- }
-
/*
- * Assemble pcrs joblist from answers map
+ * Fill in the HTTP Status
*/
- for (m = answers; m; m = m->next)
- {
- int error;
-
- snprintf(buf, BUFSIZ, "s°@%s@°%s°ig", m->name, m->value);
-
- if(NULL == (job = pcrs_make_job(buf, &error)))
- {
- log_error(LOG_LEVEL_ERROR, "Adding template fill job %s failed with error %d",
- buf, error);
- while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
- return NULL;
- }
- else
- {
- job->next = joblist;
- joblist = job;
- }
- }
+ sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
+ enlist_first(rsp->headers, buf);
/*
- * Read the file, ignoring comments
- */
- while (fgets(buf, BUFSIZ, fp))
- {
- /* skip lines starting with '#' */
- if(*buf == '#') continue;
-
- old = strsav(old, buf);
- }
- fclose(fp);
-
- /*
- * Execute the jobs
+ * Set the Content-Length
*/
- size = strlen(old) + 1;
- new = old;
-
- for (job = joblist; NULL != job; job = job->next)
+ if (rsp->content_length == 0)
{
- pcrs_exec_substitution(job, old, size, &new, &size);
- if (old != buf) free(old);
- old=new;
- }
+ rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
+ }
+ sprintf(buf, "Content-Length: %d", rsp->content_length);
+ enlist(rsp->headers, buf);
- /*
- * Free the jobs & return
+ /*
+ * Fill in the default headers:
+ *
+ * Content-Type: default to text/html if not already specified.
+ * Date: set to current date/time.
+ * Last-Modified: set to date/time the page was last changed.
+ * Expires: set to date/time page next needs reloading.
+ * Cache-Control: set to "no-cache" if applicable.
+ *
+ * See http://www.w3.org/Protocols/rfc2068/rfc2068
*/
- while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
- return(new);
+ enlist_unique(rsp->headers, "Content-Type: text/html", 13);
-}
+ if (rsp->is_static)
+ {
+ /*
+ * Set Expires to about 10 min into the future so it'll get reloaded
+ * occasionally, e.g. if IJB gets upgraded.
+ */
+ get_http_time(0, buf);
+ enlist_unique_header(rsp->headers, "Date", buf);
-/*********************************************************************
- *
- * Function : dump_map
- *
- * Description : HTML-dump a map for debugging
- *
- * Parameters :
- * 1 : map = map to dump
- *
- * Returns : string with HTML
- *
- *********************************************************************/
-char *dump_map(struct map *map)
-{
- struct map *p = map;
- char *ret = NULL;
+ /* Some date in the past. */
+ enlist_unique_header(rsp->headers, "Last-Modified", "Sat, 17 Jun 2000 12:00:00 GMT");
+ get_http_time(10 * 60, buf); /* 10 * 60sec = 10 minutes */
+ enlist_unique_header(rsp->headers, "Expires", buf);
+ }
+ else
+ {
+ /*
+ * Compliant browsers should not cache this due to the "Cache-Control"
+ * setting. However, to be certain, we also set both "Last-Modified"
+ * and "Expires" to the current time.
+ */
+ enlist_unique_header(rsp->headers, "Cache-Control", "no-cache");
+ get_http_time(0, buf);
+ enlist_unique_header(rsp->headers, "Date", buf);
+ enlist_unique_header(rsp->headers, "Last-Modified", buf);
+ enlist_unique_header(rsp->headers, "Expires", buf);
+ }
- ret = strsav(ret, "<table>\n");
- while (p)
+ /*
+ * Write the head
+ */
+ if (NULL == (rsp->head = list_to_text(rsp->headers)))
{
- ret = strsav(ret, "<tr><td><b>");
- ret = strsav(ret, p->name);
- ret = strsav(ret, "</b></td><td>");
- ret = strsav(ret, p->value);
- ret = strsav(ret, "</td></tr>\n");
- p = p->next;
+ free_http_response(rsp);
+ return(NULL);
}
+ rsp->head_length = strlen(rsp->head);
- ret = strsav(ret, "</table>\n");
- return(ret);
-}
+ return(rsp);
+}
+
/*********************************************************************
*
- * Function : cgi_default
+ * Function : alloc_http_response
*
- * Description : CGI function that is called if no action was given
- * lists menu of available unhidden CGIs.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- * 2 : rsp = http_response data structure for output
- * 3 : parameters = map of cgi parameters
+ * Description : Allocates a new http_response structure.
*
- * Returns : 0
+ * Parameters : N/A
+ *
+ * Returns : pointer to a new http_response, or NULL.
*
*********************************************************************/
-int cgi_default(struct client_state *csp, struct http_response *rsp,
- struct map *parameters)
+struct http_response *alloc_http_response(void)
{
- char *p, *tmp = NULL;
- char buf[BUFSIZ];
- const struct cgi_dispatcher *d;
- struct map *exports = NULL;
-
- /* List available unhidden CGI's and export as "other-cgis" */
- for (d = cgi_dispatchers; d->handler; d++)
- {
- if (strncmp(d->description, "HIDE", 4))
- {
- snprintf(buf, BUFSIZ, "<li><a href=%s/config/%s>%s</a></li>",
- HOME_PAGE_URL, d->name, d->description);
- tmp = strsav(tmp, buf);
- }
- }
- exports = map(exports, "other-cgis", 1, tmp, 0);
-
- /* If there were other parameters, export a dump as "cgi-parameters" */
- if(parameters)
- {
- p = dump_map(parameters);
- tmp = strsav(tmp, "<p>What made you think this cgi takes options?\n
- Anyway, here they are, in case you're interested:</p>\n");
- tmp = strsav(tmp, p);
- exports = map(exports, "cgi-parameters", 1, tmp, 0);
- free(p);
- }
- else
- {
- exports = map(exports, "cgi-parameters", 1, "", 1);
- }
-
- rsp->body = fill_template(csp, "default", exports);
-
- free_map(exports);
- return(0);
+ return (struct http_response *) zalloc(sizeof(struct http_response));
}
/*********************************************************************
*
- * Function : cgi_send_banner
+ * Function : free_http_response
*
- * Description : CGI function that returns a banner.
+ * Description : Free the memory occupied by an http_response
+ * and its depandant structures.
*
* Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- * 2 : rsp = http_response data structure for output
- * 3 : parameters = map of cgi parameters
- *
- * CGI Parameters :
- * type : Selects the type of banner between "trans" and "jb".
- * Defaults to "jb" if absent or != "trans".
+ * 1 : rsp = pointer to http_response to be freed
*
- * Returns : 0
+ * Returns : N/A
*
*********************************************************************/
-int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
- struct map *parameters)
+void free_http_response(struct http_response *rsp)
{
- if(strcmp(lookup(parameters, "type"), "trans"))
+ if (rsp)
{
- rsp->body = bindup(CJBGIF, sizeof(CJBGIF));
- rsp->content_length = sizeof(CJBGIF);
+ freez(rsp->status);
+ freez(rsp->head);
+ freez(rsp->body);
+ destroy_list(rsp->headers);
+ free(rsp);
}
- else
- {
- rsp->body = bindup(CBLANKGIF, sizeof(CBLANKGIF));
- rsp->content_length = sizeof(CBLANKGIF);
- }
- enlist(rsp->headers, "Content-Type: image/gif");
-
- return(0);
}
-#ifdef FAST_REDIRECTS
/*********************************************************************
*
- * Function : redirect_url
+ * Function : fill_template
*
- * Description : Checks for redirection URLs and returns a HTTP redirect
- * to the destination URL.
+ * Description : CGI support function that loads a given HTML
+ * template from the confdir, ignoring comment
+ * lines.
*
* Parameters :
- * 1 : http = http_request request, check `basename's of blocklist
- * 2 : csp = Current client state (buffers, headers, etc...)
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 3 : template = name of the HTML template to be used
*
- * Returns : NULL if URL was clean, HTTP redirect otherwise.
+ * Returns : char * with loaded template, or NULL if failure
*
*********************************************************************/
-char *redirect_url(struct http_request *http, struct client_state *csp)
+char *template_load(struct client_state *csp, const char *templatename)
{
- char *p, *q;
+ char buf[BUFFER_SIZE];
+ char *file_buffer = NULL;
+ FILE *fp;
- p = q = csp->http->path;
- log_error(LOG_LEVEL_REDIRECTS, "checking path: %s", p);
+ /*
+ * Open template file or fail
+ */
+ snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, templatename);
- /* find the last URL encoded in the request */
- while (p = strstr(p, "http://"))
+ if(NULL == (fp = fopen(buf, "r")))
{
- q = p++;
+ log_error(LOG_LEVEL_FATAL, "error loading template %s: %E", buf);
+ return NULL;
}
+
- /* if there was any, generate and return a HTTP redirect */
- if (q != csp->http->path)
- {
- log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
-
- p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(q));
- sprintf(p, HTTP_REDIRECT_TEMPLATE, q);
- return(p);
- }
- else
+ /*
+ * Read the file, ignoring comments
+ */
+ while (fgets(buf, BUFFER_SIZE, fp))
{
- return(NULL);
+ /* skip lines starting with '#' */
+ if(*buf == '#')
+ {
+ continue;
+ }
+
+ file_buffer = strsav(file_buffer, buf);
}
+ fclose(fp);
-}
-#endif /* def FAST_REDIRECTS */
+ return(file_buffer);
+}
/*********************************************************************
*
- * Function : cgi_show_status
+ * Function : fill_template
*
- * Description : CGI function that returns a a web page describing the
- * current status of IJB.
+ * Description : CGI support function that fills in a pre-loaded
+ * HTML template by replacing @name@ with value using
+ * pcrs, for each item in the output map.
*
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- * 2 : rsp = http_response data structure for output
- * 3 : parameters = map of cgi parameters
+ * Note that a leading '$' charachter in the export map's
+ * values will be stripped and toggle on backreference
+ * interpretation.
*
- * CGI Parameters :
- * type : Selects the type of banner between "trans" and "jb".
- * Defaults to "jb" if absent or != "trans".
+ * Parameters :
+ * 1 : template_ptr = IN: Template to be filled out.
+ * Will be free()d.
+ * OUT: Filled out template.
+ * Caller must free().
+ * 2 : exports = map with fill in symbol -> name pairs
*
- * Returns : 0
+ * Returns : N/A
*
*********************************************************************/
-int cgi_show_status(struct client_state *csp, struct http_response *rsp,
- struct map *parameters)
+void template_fill(char **template_ptr, struct map *exports)
{
- char *s = NULL;
- const struct gateway *g;
- int i;
-
- struct map *exports = NULL;
-
-#ifdef SPLIT_PROXY_ARGS
- FILE * fp;
- char buf[BUFSIZ];
- char * p;
- const char * filename = NULL;
- char * file_description = NULL;
-
+ struct map_entry *m;
+ pcrs_job *job;
+ char buf[BUFFER_SIZE];
+ char *tmp_out_buffer;
+ char *file_buffer;
+ size_t size;
+ int error;
+ const char *flags;
+
+ assert(template_ptr);
+ assert(*template_ptr);
+ assert(exports);
+
+ file_buffer = *template_ptr;
+ size = strlen(file_buffer) + 1;
- p = lookup(parameters, "file");
- switch (*p)
+ /*
+ * Assemble pcrs joblist from exports map
+ */
+ for (m = exports->first; m != NULL; m = m->next)
{
- case 'p':
- if (csp->actions_list)
- {
- filename = csp->actions_list->filename;
- file_description = "Actions List";
- }
- break;
- case 'f':
- if (csp->flist)
- {
- filename = csp->flist->filename;
- file_description = "Forward List";
- }
- break;
-
-#ifdef ACL_FILES
- case 'a':
- if (csp->alist)
+ if (*m->name == '$')
{
- filename = csp->alist->filename;
- file_description = "Access Control List";
+ /*
+ * First character of name is '$', so remove this flag
+ * character and allow backreferences ($1 etc) in the
+ * "replace with" text.
+ */
+ snprintf(buf, BUFFER_SIZE, "%s", m->name + 1);
+ flags = "sigU";
}
- break;
-#endif /* def ACL_FILES */
-
-#ifdef PCRS
- case 'r':
- if (csp->rlist)
+ else
{
- filename = csp->rlist->filename;
- file_description = "Regex Filter List";
+ /*
+ * Treat the "replace with" text as a literal string -
+ * no quoting needed, no backreferences allowed.
+ * ("Trivial" ['T'] flag).
+ */
+ flags = "sigTU";
+
+ /* Enclose name in @@ */
+ snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
}
- break;
-#endif /* def PCRS */
-#ifdef TRUST_FILES
- case 't':
- if (csp->tlist)
- {
- filename = csp->tlist->filename;
- file_description = "Trust List";
- }
- break;
-#endif /* def TRUST_FILES */
- }
- if (NULL != filename)
- {
- exports = map(exports, "filename", 1, file_description, 1);
- exports = map(exports, "filepath", 1, html_encode(filename), 0);
+ log_error(LOG_LEVEL_CGI, "Substituting: s/%s/%s/%s", buf, m->value, flags);
- if ((fp = fopen(filename, "r")) == NULL)
+ /* Make and run job. */
+ job = pcrs_compile(buf, m->value, flags, &error);
+ if (job == NULL)
{
- exports = map(exports, "content", 1, "</pre><h1>ERROR OPENING FILE!</h1><pre>", 1);
+ log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
}
else
{
- while (fgets(buf, sizeof(buf), fp))
+ pcrs_execute(job, file_buffer, size, &tmp_out_buffer, &size);
+ if (NULL != tmp_out_buffer)
{
- p = html_encode(buf);
- if (p)
- {
- s = strsav(s, p);
- freez(p);
- s = strsav(s, "<br>");
- }
+ free(file_buffer);
+ file_buffer = tmp_out_buffer;
}
- fclose(fp);
- exports = map(exports, "contents", 1, s, 0);
- }
- rsp->body = fill_template(csp, "show-status-file", exports);;
- free_map(exports);
- return(0);
-
+ pcrs_free_job(job);
+ }
}
-#endif /* def SPLIT_PROXY_ARGS */
-
- exports = map(exports, "redirect-url", 1, REDIRECT_URL, 1);
- exports = map(exports, "version", 1, VERSION, 1);
- exports = map(exports, "home-page", 1, HOME_PAGE_URL, 1);
- exports = map(exports, "invocation-args", 1, csp->config->proxy_args_header, 1);
- exports = map(exports, "gateways", 1, csp->config->proxy_args_gateways, 1);
- exports = map(exports, "gateway-protocols", 1, s, 0);
-
+ /*
+ * Return
+ */
+ *template_ptr = file_buffer;
-#ifdef STATISTICS
- exports = map(exports, "statistics", 1, add_stats(NULL), 0);
-#else
- exports = map(exports, "statistics", 1, "", 1);
-#endif /* ndef STATISTICS */
+}
-#ifdef SPLIT_PROXY_ARGS
- if (csp->actions_list)
- {
- exports = map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
- }
- else
- {
- exports = map(exports, "actions-filename", 1, "None specified", 1);
- }
- if (csp->flist)
+/*********************************************************************
+ *
+ * Function : default_exports
+ *
+ * Description : returns a struct map list that contains exports
+ * which are common to all CGI functions.
+ *
+ * Parameters :
+ * 1 : exports = Structure to write output to. This
+ * structure should be newly allocated and will be
+ * zeroed.
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : caller = name of CGI who calls us and which should
+ * be excluded from the generated menu.
+ * Returns : NULL if no memory, else map
+ *
+ *********************************************************************/
+struct map *default_exports(const struct client_state *csp, const char *caller)
+{
+ char buf[20];
+ int local_help_exists = 0;
+ struct map * exports = new_map();
+
+ map(exports, "version", 1, VERSION, 1);
+ map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
+ map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
+ map(exports, "homepage", 1, HOME_PAGE_URL, 1);
+ map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
+ map(exports, "menu", 1, make_menu(caller), 0);
+ map(exports, "code-status", 1, CODE_STATUS, 1);
+
+ snprintf(buf, 20, "%d", csp->config->hport);
+ map(exports, "my-port", 1, buf, 1);
+
+ if(!strcmp(CODE_STATUS, "stable"))
{
- exports = map(exports, "forward-filename", 1, csp->flist->filename, 1);
- }
- else
- {
- exports = map(exports, "forward-filename", 1, "None specified", 1);
- }
+ map_block_killer(exports, "unstable");
+ }
-#ifdef ACL_FILES
- if (csp->alist)
+ if(csp->config->admin_address != NULL)
{
- exports = map(exports, "acl-filename", 1, csp->alist->filename, 1);
- }
- else
- {
- exports = map(exports, "acl-filename", 1, "None specified", 1);
- }
-#else
- exports = map(exports, "acl-killer-start.*acl-killer-end", 1, "", 1);
-#endif /* ndef ACL_FILES */
-
-#ifdef PCRS
- if (csp->rlist)
- {
- exports = map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
- }
- else
- {
- exports = map(exports, "re-filter-filename", 1, "None specified", 1);
- }
-#else
- exports = map(exports, "re-filter-killer-start.*re-filter-killer-end", 1, "", 1);
-#endif /* ndef PCRS */
-
-#ifdef TRUST_FILES
- if (csp->tlist)
- {
- exports = map(exports, "trust-filename", 1, csp->tlist->filename, 1);
- }
+ map(exports, "admin-address", 1, csp->config->admin_address, 1);
+ local_help_exists = 1;
+ }
else
- {
- exports = map(exports, "trust-filename", 1, "None specified", 1);
- }
-#else
- exports = map(exports, "acl-killer-start.*acl-killer-end", 1, "", 1);
-#endif /* ndef TRUST_FILES */
-
- exports = map(exports, ".list", 1, "" , 1);
-
-#else /* ifndef SPLIT_PROXY_ARGS */
- exports = map(exports, "magic-eliminator-start.*magic-eliminator-end", 1, "", 1);
-
- if (csp->clist)
{
- map(exports, "clist", 1, csp->clist->proxy_args , 1);
+ map_block_killer(exports, "have-adminaddr-info");
}
- if (csp->flist)
- {
- map(exports, "flist", 1, csp->flist->proxy_args , 1);
- }
-
-#ifdef ACL_FILES
- if (csp->alist)
- {
- map(exports, "alist", 1, csp->alist->proxy_args , 1);
- }
-#endif /* def ACL_FILES */
-
-#ifdef PCRS
- if (csp->rlist)
+ if(csp->config->proxy_info_url != NULL)
{
- map(exports, "rlist", 1, csp->rlist->proxy_args , 1);
- }
-#endif /* def PCRS */
-
-#ifdef TRUST_FILES
- if (csp->tlist)
+ map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
+ local_help_exists = 1;
+ }
+ else
{
- map(exports, "tlist", 1, csp->tlist->proxy_args , 1);
- }
-#endif /* def TRUST_FILES */
-
-#endif /* ndef SPLIT_PROXY_ARGS */
-
- s = end_proxy_args(csp->config);
- exports = map(exports, "rcs-and-defines", 1, s , 0);
+ map_block_killer(exports, "have-proxy-info");
+ }
+ if (local_help_exists == 0)
+ map_block_killer(exports, "have-help-info");
- rsp->body = fill_template(csp, "show-status", exports);
- free_map(exports);
- return(0);
+ return (exports);
}
-
- /*********************************************************************
+
+/*********************************************************************
*
- * Function : cgi_show_url_info
+ * Function : map_block_killer
*
- * Description : (please fill me in)
+ * Description : Convenience function.
+ * Adds a "killer" for the conditional HTML-template
+ * block <name>, i.e. a substitution of the regex
+ * "if-<name>-start.*if-<name>-end" to the given
+ * export list.
*
- * Parameters :
- * 1 : http = http_request request for crunched URL
- * 2 : csp = Current client state (buffers, headers, etc...)
+ * Parameters :
+ * 1 : exports = map to extend
+ * 2 : name = name of conditional block
*
- * Returns : ???FIXME
+ * Returns : extended map
*
*********************************************************************/
-char *cgi_show_url_info(struct http_request *http, struct client_state *csp)
+void map_block_killer(struct map *exports, const char *name)
{
- char * query_string = strchr(http->path, '?');
- char * host = NULL;
-
- if (query_string != NULL)
- {
- query_string = url_decode(query_string + 1);
- if (strncmpic(query_string, "url=", 4) == 0)
- {
- host = strdup(query_string + 4);
- }
- freez(query_string);
- }
- if (host != NULL)
- {
- char * result;
- char * path;
- char * s;
- int port = 80;
- struct file_list *fl;
- struct url_actions *b;
- struct url_spec url[1];
- struct current_action_spec action[1];
-
- init_current_action(action);
-
- result = (char *)malloc(sizeof(C_URL_INFO_HEADER) + 2 * strlen(host));
- sprintf(result, C_URL_INFO_HEADER, host, host);
-
- s = current_action_to_text(action);
- result = strsav(result, "<h3>Defaults:</h3>\n<p><b>{");
- result = strsav(result, s);
- result = strsav(result, " }</b></p>\n<h3>Patterns affecting the URL:</h3>\n<p>\n");
- freez(s);
-
- s = strchr(host, '/');
- if (s != NULL)
- {
- path = strdup(s);
- *s = '\0';
- }
- else
- {
- path = strdup("");
- }
- s = strchr(host, ':');
- if (s != NULL)
- {
- *s++ = '\0';
- port = atoi(s);
- }
+ char buf[1000]; /* Will do, since the names are hardwired */
- if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
- {
- freez(host);
- freez(path);
- result = strsav(result, C_URL_INFO_FOOTER);
- return result;
- }
-
- *url = dsplit(host);
+ snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
+ map(exports, buf, 1, "", 1);
- /* if splitting the domain fails, punt */
- if (url->dbuf == NULL)
- {
- freez(host);
- freez(path);
- result = strsav(result, C_URL_INFO_FOOTER);
- return result;
- }
-
- for (b = b->next; NULL != b; b = b->next)
- {
- if ((b->url->port == 0) || (b->url->port == port))
- {
- if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
- {
- if ((b->url->path == NULL) ||
-#ifdef REGEX
- (regexec(b->url->preg, path, 0, NULL, 0) == 0)
-#else
- (strncmp(b->url->path, path, b->url->pathlen) == 0)
-#endif
- )
- {
- s = actions_to_text(b->action);
- result = strsav(result, "<b>{");
- result = strsav(result, s);
- result = strsav(result, " }</b><br>\n<code>");
- result = strsav(result, b->url->spec);
- result = strsav(result, "</code><br>\n<br>\n");
- freez(s);
-
- merge_current_action(action, b->action);
- }
- }
- }
- }
-
- freez(url->dbuf);
- freez(url->dvec);
-
- freez(host);
- freez(path);
-
- s = current_action_to_text(action);
- result = strsav(result, "</p>\n<h2>Final Results:</h2>\n<p><b>{");
- result = strsav(result, s);
- result = strsav(result, " }</b><br>\n<br>\n");
- freez(s);
-
- free_current_action(action);
-
- result = strsav(result, C_URL_INFO_FOOTER);
- return result;
- }
- else
- {
- return strdup(C_URL_INFO_FORM);
- }
}
-
-#ifdef TRUST_FILES
/*********************************************************************
*
- * Function : ij_untrusted_url
+ * Function : map_conditional
*
- * Description : This "crunch"es "http:/any.thing/ij-untrusted-url" and
- * returns a web page describing why it was untrusted.
+ * Description : Convenience function.
+ * Adds an "if-then-else" for the conditional HTML-template
+ * block <name>, i.e. a substitution of the form:
+ * @if-<name>-then@
+ * True text
+ * @else-not-<name>@
+ * False text
+ * @endif-<name>@
*
- * Parameters :
- * 1 : http = http_request request for crunched URL
- * 2 : csp = Current client state (buffers, headers, etc...)
+ * The control structure and one of the alternatives
+ * will be hidden.
+ *
+ * Parameters :
+ * 1 : exports = map to extend
+ * 2 : name = name of conditional block
+ * 3 : choose_first = nonzero for first, zero for second.
*
- * Returns : A string that contains why this was untrusted.
+ * Returns : extended map
*
*********************************************************************/
-char *ij_untrusted_url(struct http_request *http, struct client_state *csp)
+void map_conditional(struct map *exports, const char *name, int choose_first)
{
- int n;
- char *hostport, *path, *refer, *p, *v[9];
- char buf[BUFSIZ];
- struct url_spec **tl, *t;
-
-
- static const char format[] =
- "HTTP/1.0 200 OK\r\n"
- "Pragma: no-cache\n"
- "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
- "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
- "Content-Type: text/html\n\n"
- "<html>\n"
- "<head>\n"
- "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
- "</head>\n"
- BODY
- "<center><h1>"
- BANNER
- "</h1></center>"
- "The " BANNER " Proxy "
- "<A href=\"" HOME_PAGE_URL "\">"
- "(" HOME_PAGE_URL ") </A>"
- "intercepted the request for %s%s\n"
- "because the URL is not trusted.\n"
- "<br><br>\n";
-
- if ((n = ssplit(http->path, "?+", v, SZ(v), 0, 0)) == 4)
- {
- hostport = url_decode(v[1]);
- path = url_decode(v[2]);
- refer = url_decode(v[3]);
- }
- else
- {
- hostport = strdup("undefined_host");
- path = strdup("/undefined_path");
- refer = strdup("undefined");
- }
+ char buf[1000]; /* Will do, since the names are hardwired */
- n = sizeof(format);
- n += strlen(hostport);
- n += strlen(path );
+ snprintf(buf, 1000, (choose_first
+ ? "else-not-%s@.*@endif-%s"
+ : "if-%s-then@.*@else-not-%s"),
+ name, name);
+ map(exports, buf, 1, "", 1);
- if ((p = (char *)malloc(n)))
- {
- sprintf(p, format, hostport, path);
- }
+ snprintf(buf, 1000, (choose_first ? "if-%s-then" : "endif-%s"), name);
+ map(exports, buf, 1, "", 1);
- strsav(p, "The referrer in this request was <strong>");
- strsav(p, refer);
- strsav(p, "</strong><br>\n");
+}
- freez(hostport);
- freez(path );
- freez(refer );
- p = strsav(p, "<h3>The following referrers are trusted</h3>\n");
+/*********************************************************************
+ *
+ * Function : make_menu
+ *
+ * Description : Returns an HTML-formatted menu of the available
+ * unhidden CGIs, excluding the one given in <self>.
+ *
+ * Parameters : self = name of CGI to leave out, can be NULL
+ *
+ * Returns : menu string
+ *
+ *********************************************************************/
+char *make_menu(const char *self)
+{
+ const struct cgi_dispatcher *d;
+ char buf[BUFFER_SIZE];
+ char *result = NULL;
- for (tl = csp->config->trust_list; (t = *tl) ; tl++)
+ if (self == NULL)
{
- sprintf(buf, "%s<br>\n", t->spec);
- p = strsav(p, buf);
+ self = "NO-SUCH-CGI!";
}
- if (csp->config->trust_info->next)
+ /* List available unhidden CGI's and export as "other-cgis" */
+ for (d = cgi_dispatchers; d->name; d++)
{
- struct list *l;
-
- strcpy(buf,
- "<p>"
- "You can learn more about what this means "
- "and what you may be able to do about it by "
- "reading the following documents:<br>\n"
- "<ol>\n"
- );
-
- p = strsav(p, buf);
-
- for (l = csp->config->trust_info->next; l ; l = l->next)
+ if (d->description && strcmp(d->name, self))
{
- sprintf(buf,
- "<li> <a href=%s>%s</a><br>\n",
- l->str, l->str);
- p = strsav(p, buf);
+ snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
+ HOME_PAGE_URL, d->name, d->description);
+ result = strsav(result, buf);
}
-
- p = strsav(p, "</ol>\n");
}
-
- p = strsav(p, "</body>\n" "</html>\n");
-
- return(p);
+ return(result);
}
-#endif /* def TRUST_FILES */
-#ifdef STATISTICS
/*********************************************************************
*
- * Function : add_stats
+ * Function : dump_map
*
- * Description : Statistics function of JB. Called by `show_proxy_args'.
+ * Description : HTML-dump a map for debugging
*
* Parameters :
- * 1 : s = string that holds the proxy args description page
+ * 1 : the_map = map to dump
*
- * Returns : A pointer to the descriptive status web page.
+ * Returns : string with HTML
*
*********************************************************************/
-char *add_stats(char *s)
+char *dump_map(const struct map *the_map)
{
- /*
- * Output details of the number of requests rejected and
- * accepted. This is switchable in the junkbuster config.
- * Does nothing if this option is not enabled.
- */
-
- float perc_rej; /* Percentage of http requests rejected */
- char out_str[81];
- int local_urls_read = urls_read;
- int local_urls_rejected = urls_rejected;
-
- /*
- * Need to alter the stats not to include the fetch of this
- * page.
- *
- * Can't do following thread safely! doh!
- *
- * urls_read--;
- * urls_rejected--; * This will be incremented subsequently *
- */
+ struct map_entry *cur_entry = the_map->first;
+ char *ret = NULL;
- s = strsav(s,"<h2>Statistics for this " BANNER ":</h2>\n");
+ ret = strsav(ret, "<table>\n");
- if (local_urls_read == 0)
+ while (cur_entry)
{
-
- s = strsav(s,"No activity so far!\n");
-
+ ret = strsav(ret, "<tr><td><b>");
+ ret = strsav(ret, cur_entry->name);
+ ret = strsav(ret, "</b></td><td>");
+ ret = strsav(ret, cur_entry->value);
+ ret = strsav(ret, "</td></tr>\n");
+ cur_entry = cur_entry->next;
}
- else
- {
-
- perc_rej = (float)local_urls_rejected * 100.0F /
- (float)local_urls_read;
- sprintf(out_str,
- "%d requests received, %d filtered "
- "(%6.2f %%).",
- local_urls_read,
- local_urls_rejected, perc_rej);
-
- s = strsav(s,out_str);
- }
+ ret = strsav(ret, "</table>\n");
+ return(ret);
- return(s);
}
-#endif /* def STATISTICS */
+
/*
Local Variables: