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