Show enablement/disablement status in almost all templates.
[privoxy.git] / cgi.c
1 const char cgi_rcs[] = "$Id: cgi.c,v 1.36 2001/10/26 17:33:27 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/cgi.c,v $
5  *
6  * Purpose     :  Declares functions to intercept request, generate
7  *                html or gif answers, and to compose HTTP resonses.
8  *                This only contains the framework functions, the
9  *                actual handler functions are declared elsewhere.
10  *                
11  *                Functions declared include:
12  * 
13  *
14  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
15  *                IJBSWA team.  http://ijbswa.sourceforge.net
16  *
17  *                Based on the Internet Junkbuster originally written
18  *                by and Copyright (C) 1997 Anonymous Coders and 
19  *                Junkbusters Corporation.  http://www.junkbusters.com
20  *
21  *                This program is free software; you can redistribute it 
22  *                and/or modify it under the terms of the GNU General
23  *                Public License as published by the Free Software
24  *                Foundation; either version 2 of the License, or (at
25  *                your option) any later version.
26  *
27  *                This program is distributed in the hope that it will
28  *                be useful, but WITHOUT ANY WARRANTY; without even the
29  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
30  *                PARTICULAR PURPOSE.  See the GNU General Public
31  *                License for more details.
32  *
33  *                The GNU General Public License should be included with
34  *                this file.  If not, you can view it at
35  *                http://www.gnu.org/copyleft/gpl.html
36  *                or write to the Free Software Foundation, Inc., 59
37  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
38  *
39  * Revisions   :
40  *    $Log: cgi.c,v $
41  *    Revision 1.36  2001/10/26 17:33:27  oes
42  *    marginal bugfix
43  *
44  *    Revision 1.35  2001/10/23 21:48:19  jongfoster
45  *    Cleaning up error handling in CGI functions - they now send back
46  *    a HTML error page and should never cause a FATAL error.  (Fixes one
47  *    potential source of "denial of service" attacks).
48  *
49  *    CGI actions file editor that works and is actually useful.
50  *
51  *    Ability to toggle JunkBuster remotely using a CGI call.
52  *
53  *    You can turn off both the above features in the main configuration
54  *    file, e.g. if you are running a multi-user proxy.
55  *
56  *    Revision 1.34  2001/10/18 22:22:09  david__schmidt
57  *    Only show "Local support" on templates conditionally:
58  *      - if either 'admin-address' or 'proxy-info-url' are uncommented in config
59  *      - if not, no Local support section appears
60  *
61  *    Revision 1.33  2001/10/14 22:28:41  jongfoster
62  *    Fixing stupid typo.
63  *
64  *    Revision 1.32  2001/10/14 22:20:18  jongfoster
65  *    - Changes to CGI dispatching method to match CGI names exactly,
66  *      rather than doing a prefix match.
67  *    - No longer need to count the length of the CGI handler names by hand.
68  *    - Adding new handler for 404 error when disptching a CGI, if none of
69  *      the handlers match.
70  *    - Adding new handlers for CGI actionsfile editor.
71  *
72  *    Revision 1.31  2001/10/10 10:56:39  oes
73  *    Failiure to load template now fatal. Before, the user got a hard-to-understand assertion failure from cgi.c
74  *
75  *    Revision 1.30  2001/10/02 15:30:57  oes
76  *    Introduced show-request cgi
77  *
78  *    Revision 1.29  2001/09/20 15:47:44  steudten
79  *
80  *    Fix BUG: Modify int size to size_t size in fill_template()
81  *     - removes big trouble on machines where sizeof(int) != sizeof(size_t).
82  *
83  *    Revision 1.28  2001/09/19 18:00:37  oes
84  *     - Deletef time() FIXME (Can't fail under Linux either, if
85  *       the argument is guaranteed to be in out address space,
86  *       which it is.)
87  *     - Fixed comments
88  *     - Pointer notation cosmetics
89  *     - Fixed a minor bug in template_fill(): Failiure of
90  *       pcrs_execute() now secure.
91  *
92  *    Revision 1.27  2001/09/16 17:08:54  jongfoster
93  *    Moving simple CGI functions from cgi.c to new file cgisimple.c
94  *
95  *    Revision 1.26  2001/09/16 15:47:37  jongfoster
96  *    First version of CGI-based edit interface.  This is very much a
97  *    work-in-progress, and you can't actually use it to edit anything
98  *    yet.  You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
99  *    to have any effect.
100  *
101  *    Revision 1.25  2001/09/16 15:02:35  jongfoster
102  *    Adding i.j.b/robots.txt.
103  *    Inlining add_stats() since it's only ever called from one place.
104  *
105  *    Revision 1.24  2001/09/16 11:38:01  jongfoster
106  *    Splitting fill_template() into 2 functions:
107  *    template_load() loads the file
108  *    template_fill() performs the PCRS regexps.
109  *    This is because the CGI edit interface has a "table row"
110  *    template which is used many times in the page - this
111  *    change means it's only loaded from disk once.
112  *
113  *    Revision 1.23  2001/09/16 11:16:05  jongfoster
114  *    Better error handling in dispatch_cgi() and parse_cgi_parameters()
115  *
116  *    Revision 1.22  2001/09/16 11:00:10  jongfoster
117  *    New function alloc_http_response, for symmetry with free_http_response
118  *
119  *    Revision 1.21  2001/09/13 23:53:03  jongfoster
120  *    Support for both static and dynamically generated CGI pages.
121  *    Correctly setting Last-Modified: and Expires: HTTP headers.
122  *
123  *    Revision 1.20  2001/09/13 23:40:36  jongfoster
124  *    (Cosmetic only) Indentation correction
125  *
126  *    Revision 1.19  2001/09/13 23:31:25  jongfoster
127  *    Moving image data to cgi.c rather than cgi.h.
128  *
129  *    Revision 1.18  2001/08/05 16:06:20  jongfoster
130  *    Modifiying "struct map" so that there are now separate header and
131  *    "map_entry" structures.  This means that functions which modify a
132  *    map no longer need to return a pointer to the modified map.
133  *    Also, it no longer reverses the order of the entries (which may be
134  *    important with some advanced template substitutions).
135  *
136  *    Revision 1.17  2001/08/05 15:57:38  oes
137  *    Adapted finish_http_response to new list_to_text
138  *
139  *    Revision 1.16  2001/08/01 21:33:18  jongfoster
140  *    Changes to fill_template() that reduce memory usage without having
141  *    an impact on performance.  I also renamed some variables so as not
142  *    to clash with the C++ keywords "new" and "template".
143  *
144  *    Revision 1.15  2001/08/01 21:19:22  jongfoster
145  *    Moving file version information to a separate CGI page.
146  *
147  *    Revision 1.14  2001/08/01 00:19:03  jongfoster
148  *    New function: map_conditional() for an if-then-else syntax.
149  *    Changing to use new version of show_defines()
150  *
151  *    Revision 1.13  2001/07/30 22:08:36  jongfoster
152  *    Tidying up #defines:
153  *    - All feature #defines are now of the form FEATURE_xxx
154  *    - Permanently turned off WIN_GUI_EDIT
155  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
156  *
157  *    Revision 1.12  2001/07/29 18:47:05  jongfoster
158  *    Adding missing #include "loadcfg.h"
159  *
160  *    Revision 1.11  2001/07/18 17:24:37  oes
161  *    Changed to conform to new pcrs interface
162  *
163  *    Revision 1.10  2001/07/13 13:53:13  oes
164  *    Removed all #ifdef PCRS and related code
165  *
166  *    Revision 1.9  2001/06/29 21:45:41  oes
167  *    Indentation, CRLF->LF, Tab-> Space
168  *
169  *    Revision 1.8  2001/06/29 13:21:46  oes
170  *    - Cosmetics: renamed and reordered functions, variables,
171  *      texts, improved comments  etc
172  *
173  *    - Removed ij_untrusted_url() The relevant
174  *      info is now part of the "untrusted" page,
175  *      which is generated by filters.c:trust_url()
176  *
177  *    - Generators of content now call finish_http_response()
178  *      themselves, making jcc.c:chat() a little less
179  *      cluttered
180  *
181  *    - Removed obsolete "Pragma: no-cache" from our headers
182  *
183  *    - http_responses now know their head length
184  *
185  *    - fill_template now uses the new interface to pcrs, so that
186  *       - long jobs (like whole files) no longer have to be assembled
187  *         in a fixed size buffer
188  *       - the new T (trivial) option is used, and the replacement may
189  *         contain Perl syntax backrefs without confusing pcrs
190  *
191  *    - Introduced default_exports() which generates a set of exports
192  *      common to all CGIs and other content generators
193  *
194  *    - Introduced convenience function map_block_killer()
195  *
196  *    - Introduced convenience function make_menu()
197  *
198  *    - Introduced CGI-like function error_response() which generates
199  *      the "No such domain" and "Connect failed" messages using the
200  *      CGI platform
201  *
202  *    - cgi_show_url_info:
203  *      - adapted to new CGI features
204  *      - form and answers now generated from same template
205  *      - http:// prefix in URL now OK
206  *
207  *    - cgi_show_status:
208  *      - adapted to new CGI features
209  *      - no longer uses csp->init_proxy_args
210  *
211  *    - cgi_default:
212  *      - moved menu generation to make_menu()
213  *
214  *    - add_stats now writes single export map entries instead
215  *      of a fixed string
216  *
217  *    - Moved redirect_url() to filters.c
218  *
219  *    - Fixed mem leak in free_http_response(), map_block_killer(),
220  *
221  *    - Removed logentry from cancelled commit
222  *
223  *    Revision 1.7  2001/06/09 10:51:58  jongfoster
224  *    Changing "show URL info" handler to new style.
225  *    Changing BUFSIZ ==> BUFFER_SIZE
226  *
227  *    Revision 1.6  2001/06/07 23:05:19  jongfoster
228  *    Removing code related to old forward and ACL files.
229  *
230  *    Revision 1.5  2001/06/05 19:59:16  jongfoster
231  *    Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
232  *
233  *    Revision 1.4  2001/06/04 10:41:52  swa
234  *    show version string of cgi.h and cgi.c
235  *
236  *    Revision 1.3  2001/06/03 19:12:16  oes
237  *    introduced new cgi handling
238  *
239  *    No revisions before 1.3
240  *
241  **********************************************************************/
242 \f
243
244 #include "config.h"
245
246 #include <stdio.h>
247 #include <sys/types.h>
248 #include <stdlib.h>
249 #include <ctype.h>
250 #include <string.h>
251 #include <assert.h>
252
253 #ifdef _WIN32
254 #define snprintf _snprintf
255 #endif /* def _WIN32 */
256
257 #include "project.h"
258 #include "cgi.h"
259 #include "list.h"
260 #include "encode.h"
261 #include "ssplit.h"
262 #include "errlog.h"
263 #include "miscutil.h"
264 #include "cgisimple.h"
265 #ifdef FEATURE_CGI_EDIT_ACTIONS
266 #include "cgiedit.h"
267 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
268 #include "loadcfg.h"
269 /* loadcfg.h is for g_bToggleIJB only */
270
271 const char cgi_h_rcs[] = CGI_H_VERSION;
272
273 static const struct cgi_dispatcher cgi_dispatchers[] = {
274    { "",
275          cgi_default,
276          "Junkbuster main page" },
277    { "show-status", 
278          cgi_show_status,  
279          "Show information about the current configuration" }, 
280    { "show-version", 
281          cgi_show_version,  
282          "Show the source code version numbers" }, 
283    { "show-request", 
284          cgi_show_request,  
285          "Show the client's request headers." }, 
286    { "show-url-info",
287          cgi_show_url_info, 
288          "Show which actions apply to a URL and why"  },
289    { "toggle",
290          cgi_toggle, 
291          "Toggle JunkBuster on or off" },
292 #ifdef FEATURE_CGI_EDIT_ACTIONS
293    { "edit-actions",
294          cgi_edit_actions, 
295          "Edit the actions list" },
296    { "edit-actions-for-url",
297          cgi_edit_actions_for_url, 
298          NULL /* Edit the actions for (a) specified URL(s) */ },
299    { "edit-actions-list",
300          cgi_edit_actions_list, 
301          NULL /* Edit the actions list */ },
302    { "edit-actions-submit",
303          cgi_edit_actions_submit, 
304          NULL /* Change the actions for (a) specified URL(s) */ },
305    { "edit-actions-url",
306          cgi_edit_actions_url, 
307          NULL /* Change a URL pattern in the actionsfile */ },
308    { "edit-actions-add-url",
309          cgi_edit_actions_add_url, 
310          NULL /* Add a URL pattern to the actionsfile */ },
311    { "edit-actions-remove-url",
312          cgi_edit_actions_remove_url, 
313          NULL /* Add a URL pattern to the actionsfile */ },
314    { "edit-actions-section-remove",
315          cgi_edit_actions_section_remove, 
316          NULL /* Remove a section from the actionsfile */ },
317    { "edit-actions-section-add",
318          cgi_edit_actions_section_add, 
319          NULL /* Remove a section from the actionsfile */ },
320 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
321    { "robots.txt", 
322          cgi_robots_txt,  
323          NULL /* Sends a robots.txt file to tell robots to go away. */ }, 
324    { "send-banner",
325          cgi_send_banner, 
326          NULL /* Send the transparent or \"Junkbuster\" gif */ },
327    { NULL, /* NULL Indicates end of list and default page */
328          cgi_error_404,
329          NULL /* Unknown CGI page */ }
330 };
331
332
333 /*
334  * Some images
335  *
336  * Hint: You can encode your own GIFs like this:
337  * perl -e 'while (read STDIN, $c, 1) { printf("\\%.3o,", unpack("C", $c)); }'
338  */
339
340 const char image_junkbuster_gif_data[] =
341    "GIF89aD\000\013\000\360\000\000\000\000\000\377\377\377!"
342    "\371\004\001\000\000\001\000,\000\000\000\000D\000\013\000"
343    "\000\002a\214\217\251\313\355\277\000\200G&K\025\316hC\037"
344    "\200\234\230Y\2309\235S\230\266\206\372J\253<\3131\253\271"
345    "\270\215\342\254\013\203\371\202\264\334P\207\332\020o\266"
346    "N\215I\332=\211\312\3513\266:\026AK)\364\370\365aobr\305"
347    "\372\003S\275\274k2\354\254z\347?\335\274x\306^9\374\276"
348    "\037Q\000\000;";
349
350 const int image_junkbuster_gif_length = sizeof(image_junkbuster_gif_data) - 1;
351
352
353 const char image_blank_gif_data[] =
354    "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000"
355    "\000!\371\004\001\000\000\000\000,\000\000\000\000\001"
356    "\000\001\000\000\002\002D\001\000;";
357
358 const int image_blank_gif_length = sizeof(image_blank_gif_data) - 1;
359
360
361 static struct http_response cgi_error_memory_response[1];
362
363
364 static struct http_response *dispatch_known_cgi(struct client_state * csp,
365                                                 const char * path);
366
367
368 /*********************************************************************
369  * 
370  * Function    :  dispatch_cgi
371  *
372  * Description :  Checks if a request URL has either the magical hostname
373  *                i.j.b or matches HOME_PAGE_URL/config/. If so, it passes
374  *                the (rest of the) path onto dispatch_known_cgi, which
375  *                calls the relevant CGI handler function.
376  *
377  * Parameters  :
378  *          1  :  csp = Current client state (buffers, headers, etc...)
379  *
380  * Returns     :  http_response if match, NULL if nonmatch or handler fail
381  *
382  *********************************************************************/
383 struct http_response *dispatch_cgi(struct client_state *csp)
384 {
385    const char *host = csp->http->host;
386    const char *path = csp->http->path;
387
388    /*
389     * Should we intercept ?
390     */
391
392    /* Either the host matches CGI_PREFIX_HOST ..*/
393    if (   (0 == strcmpic(host, CGI_PREFIX_HOST))
394        && (path[0] == '/') )
395    {
396       /* ..then the path will all be for us.  Remove leading '/' */
397       path++;
398    }
399    /* Or it's the host part HOME_PAGE_URL, and the path /config/ */
400    else if (   (0 == strcmpic(host, HOME_PAGE_URL + 7 ))
401             && (0 == strncmpic(path,"/config", 7)) )
402    {
403       /* take everything following "/config" */
404       path += 7;
405       if (*path == '/')
406       {
407          /* skip the forward slash after "/config" */
408          path++;
409       }
410       else if (*path != '\0')
411       {
412          /* wierdness: URL is /configXXX, where XXX is some string */
413          return NULL;
414       }
415    }
416    else
417    {
418       /* Not a CGI */
419       return NULL;
420    }
421
422    /* 
423     * This is a CGI call.
424     */
425
426    return dispatch_known_cgi(csp, path);
427 }
428
429
430 /*********************************************************************
431  * 
432  * Function    :  dispatch_known_cgi
433  *
434  * Description :  Processes a CGI once dispatch_cgi has determined that
435  *                it matches one of the magic prefixes. Parses the path
436  *                as a cgi name plus query string, prepares a map that
437  *                maps CGI parameter names to their values, initializes
438  *                the http_response struct, and calls the relevant CGI
439  *                handler function.
440  *
441  * Parameters  :
442  *          1  :  csp = Current client state (buffers, headers, etc...)
443  *          2  :  path = Path of CGI, with the CGI prefix removed.
444  *                       Should not have a leading "/".
445  *
446  * Returns     :  http_response, or NULL on handler failure or out of
447  *                memory.
448  *
449  *********************************************************************/
450 static struct http_response *dispatch_known_cgi(struct client_state * csp,
451                                                 const char * path)
452 {
453    const struct cgi_dispatcher *d;
454    struct map *param_list;
455    struct http_response *rsp;
456    char *query_args_start;
457    char *path_copy;
458    jb_err err;
459
460    if (NULL == (path_copy = strdup(path)))
461    {
462       return cgi_error_memory();
463    }
464
465    query_args_start = path_copy;
466    while (*query_args_start && *query_args_start != '?')
467    {
468       query_args_start++;
469    }
470    if (*query_args_start == '?')
471    {
472       *query_args_start++ = '\0';
473    }
474
475    if (NULL == (param_list = parse_cgi_parameters(query_args_start)))
476    {
477       free(path_copy);
478       return cgi_error_memory();
479    }
480
481
482    /*
483     * At this point:
484     * path_copy        = CGI call name
485     * param_list       = CGI params, as map
486     */
487
488    /* Get mem for response or fail*/
489    if (NULL == (rsp = alloc_http_response()))
490    {
491       free(path_copy);
492       free_map(param_list);
493       return cgi_error_memory();
494    }
495
496    log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
497    log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3", 
498                             csp->ip_addr_str, csp->http->cmd); 
499
500    /* Find and start the right CGI function*/
501    for (d = cgi_dispatchers; FOREVER; d++)
502    {
503       if ((d->name == NULL) || (strcmp(path_copy, d->name) == 0))
504       {
505          err = (d->handler)(csp, rsp, param_list);
506          free(path_copy);
507          free_map(param_list);
508          if (err == JB_ERR_CGI_PARAMS)
509          {
510             err = cgi_error_bad_param(csp, rsp);
511          }
512          if (!err)
513          {
514             /* It worked */
515             return finish_http_response(rsp);
516          }
517          else
518          {
519             /* Error in handler, probably out-of-memory */
520             free_http_response(rsp);
521             return cgi_error_memory();
522          }
523       }
524    }
525 }
526
527
528 /*********************************************************************
529  *
530  * Function    :  parse_cgi_parameters
531  *
532  * Description :  Parse a URL-encoded argument string into name/value
533  *                pairs and store them in a struct map list.
534  *
535  * Parameters  :
536  *          1  :  string = string to be parsed.  Will be trashed.
537  *
538  * Returns     :  pointer to param list, or NULL if out of memory.
539  *
540  *********************************************************************/
541 static struct map *parse_cgi_parameters(char *argstring)
542 {
543    char *p;
544    char *vector[BUFFER_SIZE];
545    int pairs, i;
546    struct map *cgi_params;
547
548    if (NULL == (cgi_params = new_map()))
549    {
550       return NULL;
551    }
552
553    pairs = ssplit(argstring, "&", vector, SZ(vector), 1, 1);
554
555    for (i = 0; i < pairs; i++)
556    {
557       if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
558       {
559          *p = '\0';
560          if (map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0))
561          {
562             free_map(cgi_params);
563             return NULL;
564          }
565       }
566    }
567
568    return cgi_params;
569
570 }
571
572
573 /*********************************************************************
574  *
575  * Function    :  error_response
576  *
577  * Description :  returns an http_response that explains the reason
578  *                why a request failed.
579  *
580  * Parameters  :
581  *          1  :  csp = Current client state (buffers, headers, etc...)
582  *          2  :  templatename = Which template should be used for the answer
583  *          3  :  sys_err = system error number
584  *
585  * Returns     :  A http_response.  If we run out of memory, this
586  *                will be cgi_error_memory().
587  *
588  *********************************************************************/
589 struct http_response *error_response(struct client_state *csp,
590                                      const char *templatename,
591                                      int sys_err)
592 {
593    jb_err err;
594    struct http_response *rsp;
595    struct map * exports = default_exports(csp, NULL);
596    if (exports == NULL)
597    {
598       return cgi_error_memory();
599    }
600
601    if (NULL == (rsp = alloc_http_response()))
602    {
603       free_map(exports);
604       return cgi_error_memory();
605    }
606
607    err = map(exports, "host-html", 1, html_encode(csp->http->host), 0)
608       || map(exports, "hostport", 1, csp->http->hostport, 1)
609       || map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0)
610       || map(exports, "path", 1, csp->http->path, 1)
611       || map(exports, "path-html", 1, html_encode(csp->http->path), 0)
612       || map(exports, "error", 1, safe_strerror(sys_err), 0)
613       || map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
614
615    if (err)
616    {
617       free_map(exports);
618       free_http_response(rsp);
619       return cgi_error_memory();
620    }
621
622    if (!strcmp(templatename, "no-such-domain"))
623    {
624       rsp->status = strdup("404 No such domain");
625       if (rsp->status == NULL)
626       {
627          free_map(exports);
628          free_http_response(rsp);
629          return cgi_error_memory();
630       }
631    }
632    else if (!strcmp(templatename, "connect-failed"))
633    {
634       rsp->status = strdup("503 Connect failed");
635       if (rsp->status == NULL)
636       {
637          free_map(exports);
638          free_http_response(rsp);
639          return cgi_error_memory();
640       }
641    }
642
643    err = template_fill_for_cgi(csp, templatename, exports, rsp);
644    if (err)
645    {
646       free_http_response(rsp);
647       return cgi_error_memory();
648    }
649
650    return finish_http_response(rsp);
651 }
652
653
654 /*********************************************************************
655  *
656  * Function    :  cgi_init_error_messages
657  *
658  * Description :  Call at the start of the program to initialize
659  *                the error message used by cgi_error_memory().
660  *
661  * Parameters  :  N/A
662  *
663  * Returns     :  N/A
664  *
665  *********************************************************************/
666 void cgi_init_error_messages(void)
667 {
668    memset(cgi_error_memory_response, '\0', sizeof(*cgi_error_memory_response));
669    cgi_error_memory_response->head =
670       "HTTP/1.0 500 Internal JunkBuster Proxy Error\r\n"
671       "Content-Type: text/html\r\n"
672       "\r\n";
673    cgi_error_memory_response->body =
674       "<html>\r\n"
675       "<head><title>500 Internal JunkBuster Proxy Error</title></head>\r\n"
676       "<body>\r\n"
677       "<h1>500 Internal JunkBuster Proxy Error</h1>\r\n"
678       "<p>JunkBuster <b>ran out of memory</b> whilst processing your request.</p>\r\n"
679       "<p>Please contact your proxy administrator, or try again later</p>\r\n"
680       "</body>\r\n"
681       "</html>\r\n";
682
683    cgi_error_memory_response->head_length =
684       strlen(cgi_error_memory_response->head);
685    cgi_error_memory_response->content_length =
686       strlen(cgi_error_memory_response->body);
687 }
688
689
690 /*********************************************************************
691  *
692  * Function    :  cgi_error_memory
693  *
694  * Description :  Called if a CGI function runs out of memory.
695  *                Returns a statically-allocated error response.
696  *
697  * Parameters  :
698  *           1 :  csp = Current client state (buffers, headers, etc...)
699  *           2 :  rsp = http_response data structure for output
700  *           3 :  template_name = Name of template that could not
701  *                                be loaded.
702  *
703  * Returns     :  JB_ERR_OK on success
704  *                JB_ERR_MEMORY on out-of-memory error.  
705  *
706  *********************************************************************/
707 struct http_response *cgi_error_memory(void)
708 {
709    /* assert that it's been initialized. */
710    assert(cgi_error_memory_response->head);
711
712    return cgi_error_memory_response;
713 }
714
715
716 /*********************************************************************
717  *
718  * Function    :  cgi_error_no_template
719  *
720  * Description :  Almost-CGI function that is called if a templae
721  *                cannot be loaded.  Note this is not a true CGI,
722  *                it takes a template name rather than a map of 
723  *                parameters.
724  *
725  * Parameters  :
726  *           1 :  csp = Current client state (buffers, headers, etc...)
727  *           2 :  rsp = http_response data structure for output
728  *           3 :  template_name = Name of template that could not
729  *                                be loaded.
730  *
731  * Returns     :  JB_ERR_OK on success
732  *                JB_ERR_MEMORY on out-of-memory error.  
733  *
734  *********************************************************************/
735 jb_err cgi_error_no_template(struct client_state *csp,
736                              struct http_response *rsp,
737                              const char *template_name)
738 {
739    static const char status[] =
740       "500 Internal JunkBuster Proxy Error";
741    static const char body_prefix[] =
742       "<html>\r\n"
743       "<head><title>500 Internal JunkBuster Proxy Error</title></head>\r\n"
744       "<body>\r\n"
745       "<h1>500 Internal JunkBuster Proxy Error</h1>\r\n"
746       "<p>JunkBuster encountered an error whilst processing your request:</p>\r\n"
747       "<p><b>Could not load template file <code>";
748    static const char body_suffix[] =
749       "</code></b></p>\r\n"
750       "<p>Please contact your proxy administrator.</p>\r\n"
751       "<p>If you are the proxy administrator, please put the required file "
752       "in the <code><i>(confdir)</i>/templates</code> directory.  The "
753       "location of the <code><i>(confdir)</i></code> directory "
754       "is specified in the main JunkBuster <code>config</code> "
755       "file.  (It's typically the JunkBuster install directory"
756 #ifndef _WIN32
757       ", or <code>/etc/junkbuster/</code>"
758 #endif /* ndef _WIN32 */
759       ").</p>\r\n"
760       "</body>\r\n"
761       "</html>\r\n";
762
763    assert(csp);
764    assert(rsp);
765    assert(template_name);
766
767    /* Reset rsp, if needed */
768    freez(rsp->status);
769    freez(rsp->head);
770    freez(rsp->body);
771    rsp->content_length = 0;
772    rsp->head_length = 0;
773    rsp->is_static = 0;
774
775    rsp->body = malloc(strlen(body_prefix) + strlen(template_name) + strlen(body_suffix) + 1);
776    if (rsp->body == NULL)
777    {
778       return JB_ERR_MEMORY;
779    }
780    strcpy(rsp->body, body_prefix);
781    strcat(rsp->body, template_name);
782    strcat(rsp->body, body_suffix);
783
784    rsp->status = strdup(status);
785    if (rsp->body == NULL)
786    {
787       return JB_ERR_MEMORY;
788    }
789
790    return JB_ERR_OK;
791 }
792
793
794 /*********************************************************************
795  *
796  * Function    :  cgi_error_bad_param
797  *
798  * Description :  CGI function that is called if the parameters
799  *                (query string) for a CGI were wrong.
800  *               
801  * Parameters  :
802  *           1 :  csp = Current client state (buffers, headers, etc...)
803  *           2 :  rsp = http_response data structure for output
804  *
805  * CGI Parameters : none
806  *
807  * Returns     :  JB_ERR_OK on success
808  *                JB_ERR_MEMORY on out-of-memory error.  
809  *
810  *********************************************************************/
811 jb_err cgi_error_bad_param(struct client_state *csp,
812                            struct http_response *rsp)
813 {
814    struct map *exports;
815
816    assert(csp);
817    assert(rsp);
818
819    if (NULL == (exports = default_exports(csp, NULL)))
820    {
821       return JB_ERR_MEMORY;
822    }
823
824    return template_fill_for_cgi(csp, "cgi-error-bad-param", exports, rsp);
825 }
826
827
828 /*********************************************************************
829  *
830  * Function    :  get_http_time
831  *
832  * Description :  Get the time in a format suitable for use in a
833  *                HTTP header - e.g.:
834  *                "Sun, 06 Nov 1994 08:49:37 GMT"
835  *
836  * Parameters  :  
837  *          1  :  time_offset = Time returned will be current time
838  *                              plus this number of seconds.
839  *          2  :  buf = Destination for result.  Must be long enough
840  *                      to hold 29 characters plus a trailing zero.
841  *
842  * Returns     :  N/A
843  *
844  *********************************************************************/
845 void get_http_time(int time_offset, char *buf)
846 {
847    static const char day_names[7][4] =
848       { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
849    static const char month_names[12][4] =
850       { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
851         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
852
853    struct tm *t;
854    time_t current_time;
855
856    assert(buf);
857
858    time(&current_time); /* get current time */
859
860    current_time += time_offset;
861
862    /* get and save the gmt */
863    t = gmtime(&current_time);
864
865    /* Format: "Sun, 06 Nov 1994 08:49:37 GMT" */
866    snprintf(buf, 30,
867       "%s, %02d %s %4d %02d:%02d:%02d GMT",
868       day_names[t->tm_wday],
869       t->tm_mday,
870       month_names[t->tm_mon],
871       t->tm_year + 1900,
872       t->tm_hour,
873       t->tm_min,
874       t->tm_sec
875       );
876
877 }
878
879
880 /*********************************************************************
881  *
882  * Function    :  finish_http_response
883  *
884  * Description :  Fill in the missing headers in an http response,
885  *                and flatten the headers to an http head.
886  *
887  * Parameters  :
888  *          1  :  rsp = pointer to http_response to be processed
889  *
890  * Returns     :  A http_response, usually the rsp parameter.
891  *                On error, free()s rsp and returns cgi_error_memory()
892  *
893  *********************************************************************/
894 struct http_response *finish_http_response(struct http_response *rsp)
895 {
896    char buf[BUFFER_SIZE];
897    jb_err err;
898
899    /* Special case - do NOT change this statically allocated response,
900     * which is ready for output anyway.
901     */
902    if (rsp == cgi_error_memory_response)
903    {
904       return rsp;
905    }
906
907    /* 
908     * Fill in the HTTP Status
909     */
910    sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
911    err = enlist_first(rsp->headers, buf);
912
913    /* 
914     * Set the Content-Length
915     */
916    if (rsp->content_length == 0)
917    {
918       rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
919    }
920    sprintf(buf, "Content-Length: %d", rsp->content_length);
921    err = err || enlist(rsp->headers, buf);
922
923    /* 
924     * Fill in the default headers:
925     *
926     * Content-Type: default to text/html if not already specified.
927     * Date: set to current date/time.
928     * Last-Modified: set to date/time the page was last changed.
929     * Expires: set to date/time page next needs reloading.
930     * Cache-Control: set to "no-cache" if applicable.
931     * 
932     * See http://www.w3.org/Protocols/rfc2068/rfc2068
933     */
934    err = err || enlist_unique(rsp->headers, "Content-Type: text/html", 13);
935
936    if (rsp->is_static)
937    {
938       /*
939        * Set Expires to about 10 min into the future so it'll get reloaded
940        * occasionally, e.g. if IJB gets upgraded.
941        */
942
943       get_http_time(0, buf);
944       err = err || enlist_unique_header(rsp->headers, "Date", buf);
945
946       /* Some date in the past. */
947       err = err || enlist_unique_header(rsp->headers, "Last-Modified", "Sat, 17 Jun 2000 12:00:00 GMT");
948
949       get_http_time(10 * 60, buf); /* 10 * 60sec = 10 minutes */
950       err = err || enlist_unique_header(rsp->headers, "Expires", buf);
951    }
952    else
953    {
954       /*
955        * Compliant browsers should not cache this due to the "Cache-Control"
956        * setting.  However, to be certain, we also set both "Last-Modified"
957        * and "Expires" to the current time.
958        */
959       err = err || enlist_unique_header(rsp->headers, "Cache-Control", "no-cache");
960       get_http_time(0, buf);
961       err = err || enlist_unique_header(rsp->headers, "Date", buf);
962       err = err || enlist_unique_header(rsp->headers, "Last-Modified", buf);
963       err = err || enlist_unique_header(rsp->headers, "Expires", buf);
964    }
965
966
967    /* 
968     * Write the head
969     */
970    if (err || (NULL == (rsp->head = list_to_text(rsp->headers))))
971    {
972       free_http_response(rsp);
973       return cgi_error_memory();
974    }
975    rsp->head_length = strlen(rsp->head);
976
977    return rsp;
978
979 }
980
981
982 /*********************************************************************
983  *
984  * Function    :  alloc_http_response
985  *
986  * Description :  Allocates a new http_response structure.
987  *
988  * Parameters  :  N/A
989  *
990  * Returns     :  pointer to a new http_response, or NULL.
991  *
992  *********************************************************************/
993 struct http_response *alloc_http_response(void)
994 {
995    return (struct http_response *) zalloc(sizeof(struct http_response));
996
997 }
998
999
1000 /*********************************************************************
1001  *
1002  * Function    :  free_http_response
1003  *
1004  * Description :  Free the memory occupied by an http_response
1005  *                and its depandant structures.
1006  *
1007  * Parameters  :
1008  *          1  :  rsp = pointer to http_response to be freed
1009  *
1010  * Returns     :  N/A
1011  *
1012  *********************************************************************/
1013 void free_http_response(struct http_response *rsp)
1014 {
1015    /*
1016     * Must special case cgi_error_memory_response, which is never freed.
1017     */
1018    if (rsp && (rsp != cgi_error_memory_response))
1019    {
1020       freez(rsp->status);
1021       freez(rsp->head);
1022       freez(rsp->body);
1023       destroy_list(rsp->headers);
1024       free(rsp);
1025    }
1026
1027 }
1028
1029
1030 /*********************************************************************
1031  *
1032  * Function    :  template_load
1033  *
1034  * Description :  CGI support function that loads a given HTML
1035  *                template from the confdir, ignoring comment
1036  *                lines. 
1037  *
1038  * Parameters  :
1039  *           1 :  csp = Current client state (buffers, headers, etc...)
1040  *           2 :  template_ptr = Destination for pointer to loaded
1041  *                               template text.
1042  *           3 :  template = name of the HTML template to be used
1043  *
1044  * Returns     :  JB_ERR_OK on success
1045  *                JB_ERR_MEMORY on out-of-memory error.  
1046  *                JB_ERR_FILE if the template file cannot be read
1047  *
1048  *********************************************************************/
1049 jb_err template_load(struct client_state *csp, char ** template_ptr, 
1050                      const char *templatename)
1051 {
1052    char *templates_dir_path;
1053    char *full_path;
1054    char *file_buffer;
1055    FILE *fp;
1056    char buf[BUFFER_SIZE];
1057
1058    assert(csp);
1059    assert(template_ptr);
1060    assert(templatename);
1061
1062    *template_ptr = NULL;
1063
1064    /*
1065     * Open template file or fail
1066     */
1067
1068    templates_dir_path = make_path(csp->config->confdir, "templates");
1069    if (templates_dir_path == NULL)
1070    {
1071       return JB_ERR_MEMORY;
1072    }
1073
1074    full_path = make_path(templates_dir_path, templatename);
1075    free(templates_dir_path);
1076    if (full_path == NULL)
1077    {
1078       return JB_ERR_MEMORY;
1079    }
1080
1081    file_buffer = strdup("");
1082    if (file_buffer == NULL)
1083    {
1084       free(full_path);
1085       return JB_ERR_MEMORY;
1086    }
1087
1088    if (NULL == (fp = fopen(full_path, "r")))
1089    {
1090       log_error(LOG_LEVEL_ERROR, "Cannot open template file %s: %E", full_path);
1091       free(full_path);
1092       free(file_buffer);
1093       return JB_ERR_FILE;
1094    }
1095    free(full_path);
1096
1097    /* 
1098     * Read the file, ignoring comments.
1099     *
1100     * FIXME: The comment handling could break with lines >BUFFER_SIZE long.
1101     *        This is unlikely in practise.
1102     */
1103    while (fgets(buf, BUFFER_SIZE, fp))
1104    {
1105       /* skip lines starting with '#' */
1106       if(*buf == '#')
1107       {
1108          continue;
1109       }
1110
1111       if (string_append(&file_buffer, buf))
1112       {
1113          fclose(fp);
1114          return JB_ERR_MEMORY;
1115       }
1116    }
1117    fclose(fp);
1118
1119    *template_ptr = file_buffer;
1120
1121    return JB_ERR_OK;
1122 }
1123
1124
1125 /*********************************************************************
1126  *
1127  * Function    :  template_fill
1128  *
1129  * Description :  CGI support function that fills in a pre-loaded
1130  *                HTML template by replacing @name@ with value using
1131  *                pcrs, for each item in the output map.
1132  *
1133  *                Note that a leading '$' charachter in the export map's
1134  *                values will be stripped and toggle on backreference
1135  *                interpretation.
1136  *
1137  * Parameters  :
1138  *           1 :  template_ptr = IN: Template to be filled out.
1139  *                                   Will be free()d.
1140  *                               OUT: Filled out template.
1141  *                                    Caller must free().
1142  *           2 :  exports = map with fill in symbol -> name pairs
1143  *
1144  * Returns     :  JB_ERR_OK on success
1145  *                JB_ERR_MEMORY on out-of-memory error
1146  *
1147  *********************************************************************/
1148 jb_err template_fill(char **template_ptr, const struct map *exports)
1149 {
1150    struct map_entry *m;
1151    pcrs_job *job;
1152    char buf[BUFFER_SIZE];
1153    char *tmp_out_buffer;
1154    char *file_buffer;
1155    size_t  size;
1156    int error;
1157    const char *flags;
1158
1159    assert(template_ptr);
1160    assert(*template_ptr);
1161    assert(exports);
1162
1163    file_buffer = *template_ptr;
1164    size = strlen(file_buffer) + 1;
1165
1166    /* 
1167     * Assemble pcrs joblist from exports map
1168     */
1169    for (m = exports->first; m != NULL; m = m->next)
1170    {
1171       if (*m->name == '$')
1172       {
1173          /*
1174           * First character of name is '$', so remove this flag
1175           * character and allow backreferences ($1 etc) in the
1176           * "replace with" text.
1177           */
1178          snprintf(buf, BUFFER_SIZE, "%s", m->name + 1);
1179          flags = "sigU";
1180       }
1181       else
1182       {
1183          /*
1184           * Treat the "replace with" text as a literal string - 
1185           * no quoting needed, no backreferences allowed.
1186           * ("Trivial" ['T'] flag).
1187           */
1188          flags = "sigTU";
1189
1190          /* Enclose name in @@ */
1191          snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
1192       }
1193
1194
1195       log_error(LOG_LEVEL_CGI, "Substituting: s/%s/%s/%s", buf, m->value, flags);
1196
1197       /* Make and run job. */
1198       job = pcrs_compile(buf, m->value, flags,  &error);
1199       if (job == NULL) 
1200       {
1201          if (error == PCRS_ERR_NOMEM)
1202          {
1203             free(file_buffer);
1204             *template_ptr = NULL;
1205             return JB_ERR_MEMORY;
1206          }
1207          else
1208          {
1209             log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
1210             /* Hope it wasn't important and silently ignore the invalid job */
1211          }
1212       }
1213       else
1214       {
1215          pcrs_execute(job, file_buffer, size, &tmp_out_buffer, &size);
1216          free(file_buffer);
1217          pcrs_free_job(job);
1218          if (NULL == tmp_out_buffer)
1219          {
1220             *template_ptr = NULL;
1221             return JB_ERR_MEMORY;
1222          }
1223          file_buffer = tmp_out_buffer;
1224       }
1225    }
1226
1227    /*
1228     * Return
1229     */
1230    *template_ptr = file_buffer;
1231    return JB_ERR_OK;
1232 }
1233
1234
1235 /*********************************************************************
1236  *
1237  * Function    :  template_fill_for_cgi
1238  *
1239  * Description :  CGI support function that loads a HTML template
1240  *                and fills it in.  Handles file-not-found errors
1241  *                by sending a HTML error message.  For convenience,
1242  *                this function also frees the passed "exports" map.
1243  *
1244  * Parameters  :
1245  *           1 :  csp = Client state
1246  *           2 :  templatename = name of the HTML template to be used
1247  *           3 :  exports = map with fill in symbol -> name pairs.
1248  *                          Will be freed by this function.
1249  *
1250  * Returns     :  JB_ERR_OK on success
1251  *                JB_ERR_MEMORY on out-of-memory error
1252  *
1253  *********************************************************************/
1254 jb_err template_fill_for_cgi(struct client_state *csp,
1255                              const char *templatename,
1256                              struct map *exports,
1257                              struct http_response *rsp)
1258 {
1259    jb_err err;
1260    
1261    assert(csp);
1262    assert(templatename);
1263    assert(exports);
1264    assert(rsp);
1265
1266    err = template_load(csp, &rsp->body, templatename);
1267    if (err == JB_ERR_FILE)
1268    {
1269       free_map(exports);
1270       return cgi_error_no_template(csp, rsp, templatename);
1271    }
1272    else if (err)
1273    {
1274       free_map(exports);
1275       return err; /* JB_ERR_MEMORY */
1276    }
1277    err = template_fill(&rsp->body, exports);
1278    free_map(exports);
1279    return err;
1280 }
1281
1282
1283 /*********************************************************************
1284  *
1285  * Function    :  default_exports
1286  *
1287  * Description :  returns a struct map list that contains exports
1288  *                which are common to all CGI functions.
1289  *
1290  * Parameters  :
1291  *          1  :  csp = Current client state (buffers, headers, etc...)
1292  *          2  :  caller = name of CGI who calls us and which should
1293  *                         be excluded from the generated menu. May be
1294  *                         NULL.
1295  * Returns     :  NULL if no memory, else a new map.  Caller frees.
1296  *
1297  *********************************************************************/
1298 struct map *default_exports(const struct client_state *csp, const char *caller)
1299 {
1300    char buf[20];
1301    int err = 0;
1302    struct map * exports;
1303    int local_help_exists = 0;
1304
1305    assert(csp);
1306
1307    exports = new_map();
1308    if (exports == NULL)
1309    {
1310       return NULL;
1311    }
1312
1313    err = map(exports, "version", 1, VERSION, 1)
1314       || map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1)
1315       || map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1)
1316       || map(exports, "homepage", 1, HOME_PAGE_URL, 1)
1317       || map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1)
1318       || map(exports, "menu", 1, make_menu(caller), 0)
1319       || map(exports, "code-status", 1, CODE_STATUS, 1);
1320
1321    err = err || map_conditional(exports, "enabled-display", g_bToggleIJB);
1322
1323    snprintf(buf, 20, "%d", csp->config->hport);
1324    err = err || map(exports, "my-port", 1, buf, 1);
1325
1326    if(!strcmp(CODE_STATUS, "stable"))
1327    {
1328       err = err || map_block_killer(exports, "unstable");
1329    }
1330
1331    if(csp->config->admin_address != NULL)
1332    {
1333       err = err || map(exports, "admin-address", 1, csp->config->admin_address, 1);
1334       local_help_exists = 1;
1335    }
1336    else
1337    {
1338       err = err || map_block_killer(exports, "have-adminaddr-info");
1339    }
1340
1341    if(csp->config->proxy_info_url != NULL)
1342    {
1343       err = err || map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
1344       local_help_exists = 1;
1345    }
1346    else
1347    {
1348       err = err || map_block_killer(exports, "have-proxy-info");
1349    }   
1350
1351    if (local_help_exists == 0)
1352    {
1353       err = err || map_block_killer(exports, "have-help-info");
1354    }
1355
1356    if (err)
1357    {
1358       free_map(exports);
1359       return NULL;
1360    }
1361
1362    return exports;
1363 }
1364
1365
1366 /*********************************************************************
1367  *
1368  * Function    :  map_block_killer
1369  *
1370  * Description :  Convenience function.
1371  *                Adds a "killer" for the conditional HTML-template
1372  *                block <name>, i.e. a substitution of the regex
1373  *                "if-<name>-start.*if-<name>-end" to the given
1374  *                export list.
1375  *
1376  * Parameters  :  
1377  *          1  :  exports = map to extend
1378  *          2  :  name = name of conditional block
1379  *
1380  * Returns     :  JB_ERR_OK on success
1381  *                JB_ERR_MEMORY on out-of-memory error.  
1382  *
1383  *********************************************************************/
1384 jb_err map_block_killer(struct map *exports, const char *name)
1385 {
1386    char buf[1000]; /* Will do, since the names are hardwired */
1387
1388    assert(exports);
1389    assert(name);
1390    assert(strlen(name) < 490);
1391
1392    snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
1393    return map(exports, buf, 1, "", 1);
1394 }
1395
1396
1397 /*********************************************************************
1398  *
1399  * Function    :  map_conditional
1400  *
1401  * Description :  Convenience function.
1402  *                Adds an "if-then-else" for the conditional HTML-template
1403  *                block <name>, i.e. a substitution of the form:
1404  *                @if-<name>-then@
1405  *                   True text
1406  *                @else-not-<name>@
1407  *                   False text
1408  *                @endif-<name>@
1409  *
1410  *                The control structure and one of the alternatives
1411  *                will be hidden.
1412  *
1413  * Parameters  :  
1414  *          1  :  exports = map to extend
1415  *          2  :  name = name of conditional block
1416  *          3  :  choose_first = nonzero for first, zero for second.
1417  *
1418  * Returns     :  JB_ERR_OK on success
1419  *                JB_ERR_MEMORY on out-of-memory error.  
1420  *
1421  *********************************************************************/
1422 jb_err map_conditional(struct map *exports, const char *name, int choose_first)
1423 {
1424    char buf[1000]; /* Will do, since the names are hardwired */
1425    jb_err err;
1426
1427    assert(exports);
1428    assert(name);
1429    assert(strlen(name) < 480);
1430
1431    snprintf(buf, 1000, (choose_first
1432       ? "else-not-%s@.*@endif-%s"
1433       : "if-%s-then@.*@else-not-%s"),
1434       name, name);
1435
1436    err = map(exports, buf, 1, "", 1);
1437    if (err)
1438    {
1439       return err;
1440    }
1441
1442    snprintf(buf, 1000, (choose_first ? "if-%s-then" : "endif-%s"), name);
1443    return map(exports, buf, 1, "", 1);
1444 }
1445
1446
1447 /*********************************************************************
1448  *
1449  * Function    :  make_menu
1450  *
1451  * Description :  Returns an HTML-formatted menu of the available 
1452  *                unhidden CGIs, excluding the one given in <self>.
1453  *
1454  * Parameters  :  self = name of CGI to leave out, can be NULL
1455  *
1456  * Returns     :  menu string
1457  *
1458  *********************************************************************/
1459 char *make_menu(const char *self)
1460 {
1461    const struct cgi_dispatcher *d;
1462    char buf[BUFFER_SIZE];
1463    char *result = NULL;
1464
1465    if (self == NULL)
1466    {
1467       self = "NO-SUCH-CGI!";
1468    }
1469
1470    /* List available unhidden CGI's and export as "other-cgis" */
1471    for (d = cgi_dispatchers; d->name; d++)
1472    {
1473       if (d->description && strcmp(d->name, self))
1474       {
1475          snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
1476                HOME_PAGE_URL, d->name, d->description);
1477          result = strsav(result, buf);
1478       }
1479    }
1480    return(result);
1481
1482 }
1483
1484
1485 /*********************************************************************
1486  *
1487  * Function    :  dump_map
1488  *
1489  * Description :  HTML-dump a map for debugging
1490  *
1491  * Parameters  :
1492  *          1  :  the_map = map to dump
1493  *
1494  * Returns     :  string with HTML
1495  *
1496  *********************************************************************/
1497 char *dump_map(const struct map *the_map)
1498 {
1499    struct map_entry *cur_entry = the_map->first;
1500    char *ret = NULL;
1501
1502    ret = strsav(ret, "<table>\n");
1503
1504    while (cur_entry)
1505    {
1506       ret = strsav(ret, "<tr><td><b>");
1507       ret = strsav(ret, cur_entry->name);
1508       ret = strsav(ret, "</b></td><td>");
1509       ret = strsav(ret, cur_entry->value);
1510       ret = strsav(ret, "</td></tr>\n");
1511       cur_entry = cur_entry->next;
1512    }
1513
1514    ret = strsav(ret, "</table>\n");
1515    return(ret);
1516
1517 }
1518
1519
1520 /*
1521   Local Variables:
1522   tab-width: 3
1523   end:
1524 */