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