home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Magazine / wwwoffle-2.1.tar.gz / wwwoffle-2.1 / config.c < prev    next >
C/C++ Source or Header  |  1998-02-20  |  61KB  |  2,151 lines

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/config.c 2.21 1998/02/20 20:15:41 amb Exp $
  3.  
  4.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.1.
  5.   Configuration file management functions.
  6.   ******************/ /******************
  7.   Written by Andrew M. Bishop
  8.  
  9.   This file Copyright 1997,98 Andrew M. Bishop
  10.   It may be distributed under the GNU Public License, version 2, or
  11.   any higher version.  See section COPYING of the GNU Public license
  12.   for conditions under which this file may be redistributed.
  13.   ***************************************/
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/utsname.h>
  25. #include <dirent.h>
  26. #include <pwd.h>
  27. #include <grp.h>
  28.  
  29. #include "config.h"
  30. #include "misc.h"
  31. #include "proto.h"
  32. #include "sockets.h"
  33. #include "errors.h"
  34. #include "wwwoffle.h"
  35.  
  36.  
  37. #ifndef SPOOL_DIR
  38. #define SPOOL_DIR DEF_SPOOL
  39. #endif
  40.  
  41. #ifndef CONF_DIR
  42. #define CONF_DIR DEF_CONF
  43. #endif
  44.  
  45.  
  46. /* For upgrade-cache and wwwoffle-tools we don't need to read the config file
  47.    we just need some variables so we keep this SIMPLE. */
  48.  
  49. #ifndef SIMPLE
  50.  
  51. /* Type definitions */
  52.  
  53. /*+ The type of value to expect for a value. +*/
  54. typedef enum _ConfigType
  55. {
  56.  Fixed,                         /*+ When the left hand side is fixed. +*/
  57.  None,                          /*+ When there is no right hand side. +*/
  58.  
  59.  Boolean,                       /*+ A boolean response (yes/no 1/0 true/false). +*/
  60.  
  61.  PortNumber,                    /*+ For port numbers (>0). +*/
  62.  AgeDays,                       /*+ An age in days (can be -ve). +*/
  63.  TimeSecs,                      /*+ An time in seconds (can be -ve). +*/
  64.  FileSize,                      /*+ A file size (must be >=0). +*/
  65.  CfgMaxServers,                 /*+ Max number of servers to fork (>0, <MAX_SERVERS). +*/
  66.  CfgMaxFetchServers,            /*+ Max number of servers for fetching pages (>0, <MAX_FETCH_SERVERS). +*/
  67.  
  68.  CfgLogLevel,                   /*+ A log level (debug, info, important, warning or fatal). +*/
  69.  
  70.  UserId,                        /*+ For user IDs, (numeric or string). +*/
  71.  GroupId,                       /*+ For group IDs, (numeric or string). +*/
  72.  
  73.  String,                        /*+ For an arbitrary string (no whitespace). +*/
  74.  
  75.  DirName,                       /*+ For directory name values (string starting with '/'). +*/
  76.  PathOrFileExt,                 /*+ A path (/string) or file extension (.string). +*/
  77.  
  78.  MIMEType,                      /*+ A MIME type (string/string). +*/
  79.  
  80.  Host,                          /*+ For host names (string). +*/
  81.  HostAndPort,                   /*+ For host name and port numbers (string[:port]). +*/
  82.  HostAndPortOrNone,             /*+ For host name and port numbers (string[:port]) or nothing. +*/
  83.  ProtoAndHost,                  /*+ For protocols and host names (string/string). +*/
  84.  ProtoAndHostOrDefault          /*+ For protocols and host names (string/string) or default. +*/
  85. }
  86. ConfigType;
  87.  
  88. /*+ A description of an entry in a section of the config file. +*/
  89. typedef struct _Entry
  90. {
  91.  char *name;                    /*+ The name of the entry. +*/
  92.  void *pointer;                 /*+ A pointer to the value of the entry. +*/
  93.  char list;                     /*+ Set to true if it is a list of KeyPairs. +*/
  94.  ConfigType left_type;          /*+ The type of the left side of the equals sign. +*/
  95.  ConfigType right_type;         /*+ The type of the right side of the equals sign. +*/
  96. }
  97. Entry;
  98.  
  99. /*+ A description of a section in the config file. +*/
  100. typedef struct _Section
  101. {
  102.  char *name;                    /*+ The name of the section. +*/
  103.  Entry *entries;                /*+ The entries in the section (NULL terminated). +*/
  104. }
  105. Section;
  106.  
  107. /*+ A keyed entry for a wildcard left hand side. +*/
  108. typedef struct _KeyPair
  109. {
  110.  char *name;                    /*+ The key name of the entry. +*/
  111.  union
  112.  {
  113.   int   integer;                /*+ An integer value. +*/
  114.   char *string;                 /*+ A string value. +*/
  115.  }
  116.  value;
  117. }
  118. KeyPair;
  119.  
  120. /*+ The last of a KeyPair list. +*/
  121. static KeyPair KeyPairEnd={NULL};
  122.  
  123. #endif /* SIMPLE */
  124.  
  125. /* StartUp section */
  126.  
  127. /*+ The name of the configuration file. +*/
  128. char *ConfigFile=CONF_DIR "/wwwoffle.conf";
  129.  
  130. /*+ The port number to use for the HTTP proxy port. +*/
  131. int HTTP_Port=DEF_HTTP_PORT;
  132.  
  133. /*+ The port number to use for the wwwoffle port. +*/
  134. int WWWOFFLE_Port=DEF_WWWOFFLE_PORT;
  135.  
  136. /*+ The spool directory. +*/
  137. char *SpoolDir=SPOOL_DIR;
  138.  
  139. /*+ The user id for wwwoffled or -1 for none. +*/
  140. int WWWOFFLE_Uid=-1;
  141.  
  142. /*+ The group id for wwwoffled or -1 for none. +*/
  143. int WWWOFFLE_Gid=-1;
  144.  
  145. /*+ Whether to use the syslog facility or not. +*/
  146. int UseSyslog=1;
  147.  
  148. /*+ The password required for demon configuration. +*/
  149. char *PassWord=NULL;
  150.  
  151. #ifndef SIMPLE
  152.  
  153. /*+ Maximum number of servers  +*/
  154. int MaxServers=DEF_MAX_SERVERS; /*+ in total. +*/
  155. int MaxFetchServers=DEF_MAX_FETCH_SERVERS; /*+ for fetching. +*/
  156.  
  157. /*+ The entries in the StartUp section. +*/
  158. static Entry startup_entries[]={{"http-port"        ,(void*)&HTTP_Port       ,0,Fixed,PortNumber        },
  159.                                 {"wwwoffle-port"    ,(void*)&WWWOFFLE_Port   ,0,Fixed,PortNumber        },
  160.                                 {"spool-dir"        ,(void*)&SpoolDir        ,0,Fixed,DirName           },
  161.                                 {"run-uid"          ,(void*)&WWWOFFLE_Uid    ,0,Fixed,UserId            },
  162.                                 {"run-gid"          ,(void*)&WWWOFFLE_Gid    ,0,Fixed,GroupId           },
  163.                                 {"use-syslog"       ,(void*)&UseSyslog       ,0,Fixed,Boolean           },
  164.                                 {"password"         ,(void*)&PassWord        ,0,Fixed,String            },
  165.                                 {"max-servers"      ,(void*)&MaxServers      ,0,Fixed,CfgMaxServers     },
  166.                                 {"max-fetch-servers",(void*)&MaxFetchServers ,0,Fixed,CfgMaxFetchServers},
  167.                                 {NULL               ,NULL                    ,0,-1   ,-1                }};
  168.  
  169. /*+ The StartUp section. +*/
  170. static Section startup_section={"StartUp",startup_entries};
  171.  
  172. #endif /* SIMPLE */
  173.  
  174. /* Options Section */
  175.  
  176. /*+ The level of error logging (see ErrorLevel in errors.h) +*/
  177. int LogLevel=Important,  /*+ in the config file for syslog and stderr. +*/ /* see also SetDefaultValues() */
  178.     DebugLevel=-1;       /*+ on the command line for stderr. +*/           /* not in the config file */
  179.  
  180. #ifndef SIMPLE
  181.  
  182. /*+ The option to also fetch images. +*/
  183. int FetchImages; /* see SetDefaultValues() */
  184.  
  185. /*+ The option to also fetch frames. +*/
  186. int FetchFrames; /* see SetDefaultValues() */
  187.  
  188. /*+ The number of days to display in the index of the latest pages. +*/
  189. int IndexLatestDays; /* see SetDefaultValues() */
  190.  
  191. /*+ The option of a tag that can be added to the bottom of the spooled pages with the date and a refresh button. +*/
  192. int AddInfoRefresh; /* see SetDefaultValues() */
  193.  
  194. /*+ The maximum age of a cached page to use in preference while online. +*/
  195. int RequestChanged; /* see SetDefaultValues() */
  196.  
  197. /*+ The option to allow or ignore the 'Pragma: no-cache' request. +*/
  198. int PragmaNoCache; /* see SetDefaultValues() */
  199.  
  200. /*+ The option to not make requests while offline but to return an error. +*/
  201. int OfflineRequests; /* see SetDefaultValues() */
  202.  
  203. /*+ The interval in days between monitoring the specified pages. +*/
  204. int MonitorInterval; /* see SetDefaultValues() */
  205.  
  206. /*+ The entries in the Options section. +*/
  207. static Entry options_entries[]={{"log-level"        ,(void*)&LogLevel       ,0,Fixed,CfgLogLevel},
  208.                                 {"fetch-images"     ,(void*)&FetchImages    ,0,Fixed,Boolean    },
  209.                                 {"fetch-frames"     ,(void*)&FetchFrames    ,0,Fixed,Boolean    },
  210.                                 {"index-latest-days",(void*)&IndexLatestDays,0,Fixed,AgeDays    },
  211.                                 {"add-info-refresh" ,(void*)&AddInfoRefresh ,0,Fixed,Boolean    },
  212.                                 {"request-changed"  ,(void*)&RequestChanged ,0,Fixed,TimeSecs   },
  213.                                 {"pragma-no-cache"  ,(void*)&PragmaNoCache  ,0,Fixed,Boolean    },
  214.                                 {"offline-requests" ,(void*)&OfflineRequests,0,Fixed,Boolean    },
  215.                                 {"monitor-interval" ,(void*)&MonitorInterval,0,Fixed,AgeDays    },
  216.                                 {NULL               ,NULL                   ,0,-1   ,-1         }};
  217.  
  218. /*+ The Options section. +*/
  219. static Section options_section={"Options",options_entries};
  220.  
  221.  
  222. /* LocalHost section */
  223.  
  224. /*+ The list of localhost hostnames. +*/
  225. static KeyPair **LocalHost; /* see SetDefaultValues() */
  226.  
  227. /*+ The entries in the LocalHost section. +*/
  228. static Entry localhost_entries[]={{""  ,(void*)&LocalHost,1,Host,None},
  229.                                   {NULL,NULL             ,0,-1  ,-1  }};
  230.  
  231. /*+ The LocalHost section. +*/
  232. static Section localhost_section={"LocalHost",localhost_entries};
  233.  
  234.  
  235. /* LocalNet section */
  236.  
  237. /*+ The list of local network hostnames. +*/
  238. static KeyPair **LocalNet; /* see SetDefaultValues() */
  239.  
  240. /*+ The entries in the LocalNet section. +*/
  241. static Entry localnet_entries[]={{""  ,(void*)&LocalNet,1,Host,None},
  242.                                  {NULL,NULL            ,0,-1  ,-1  }};
  243.  
  244. /*+ The LocalNet section. +*/
  245. static Section localnet_section={"LocalNet",localnet_entries};
  246.  
  247.  
  248. /* AllowedConnect section */
  249.  
  250. /*+ The list of allowed hostnames. +*/
  251. static KeyPair **AllowedConnect; /* see SetDefaultValues() */
  252.  
  253. /*+ The entries in the AllowedConnect section. +*/
  254. static Entry allowedconnect_entries[]={{""  ,(void*)&AllowedConnect,1,Host,None},
  255.                                        {NULL,NULL                  ,0,-1  ,-1  }};
  256.  
  257. /*+ The AllowedConnect section. +*/
  258. static Section allowedconnect_section={"AllowedConnect",allowedconnect_entries};
  259.  
  260.  
  261. /* DontCache section */
  262.  
  263. /*+ The list of servers and reasons not to cache. +*/
  264. static KeyPair **DontCache; /* see SetDefaultValues() */
  265.  
  266. /*+ The entries in the DontCache section. +*/
  267. static Entry dontcache_entries[]={{""  ,(void*)&DontCache,1,ProtoAndHostOrDefault,PathOrFileExt},
  268.                                   {NULL,NULL             ,0,-1                   ,-1           }};
  269.  
  270. /*+ The DontCache section. +*/
  271. static Section dontcache_section={"DontCache",dontcache_entries};
  272.  
  273.  
  274. /* DontGet section */
  275.  
  276. /*+ The list of servers and reasons not to get. +*/
  277. static KeyPair **DontGet; /* see SetDefaultValues() */
  278.  
  279. /*+ The entries in the DontGet section. +*/
  280. static Entry dontget_entries[]={{""  ,(void*)&DontGet,1,ProtoAndHostOrDefault,PathOrFileExt},
  281.                                 {NULL,NULL           ,0,-1                   ,-1           }};
  282.  
  283. /*+ The DontGet section. +*/
  284. static Section dontget_section={"DontGet",dontget_entries};
  285.  
  286.  
  287. /* DontGetRecursive section */
  288.  
  289. /*+ The list of servers and reasons not to get. +*/
  290. static KeyPair **DontGetRecursive; /* see SetDefaultValues() */
  291.  
  292. /*+ The entries in the DontGetRecursive section. +*/
  293. static Entry dontgetrecursive_entries[]={{""  ,(void*)&DontGetRecursive,1,ProtoAndHostOrDefault,PathOrFileExt},
  294.                                          {NULL,NULL                    ,0,-1                   ,-1           }};
  295.  
  296. /*+ The DontGetRecursive section. +*/
  297. static Section dontgetrecursive_section={"DontGetRecursive",dontgetrecursive_entries};
  298.  
  299.  
  300. /* CensorHeader section */
  301.  
  302. /*+ The list of censored headers. +*/
  303. static KeyPair **CensorHeader; /* see SetDefaultValues() */
  304.  
  305. /*+ The entries in the censor headers section. +*/
  306. static Entry censorheader_entries[]={{""  ,(void*)&CensorHeader,1,String,None},
  307.                                      {NULL,NULL                ,0,-1    ,-1  }};
  308.  
  309. /*+ The CensorHeader section. +*/
  310. static Section censorheader_section={"CensorHeader",censorheader_entries};
  311.  
  312.  
  313. /* FTPOptions section */
  314.  
  315. /*+ The anon-ftp username. +*/
  316. static char *FTPUserName; /* see SetDefaultValues() */
  317.  
  318. /*+ The anon-ftp password. +*/
  319. static char *FTPPassWord; /* see SetDefaultValues() */
  320.  
  321. /*+ The information that is needed to allow non-anonymous access, +*/
  322. static KeyPair **FTPAuthHost, /*+ hostname +*/ /* see SetDefaultValues() */
  323.                **FTPAuthUser, /*+ username +*/ /* see SetDefaultValues() */
  324.                **FTPAuthPass; /*+ password +*/ /* see SetDefaultValues() */
  325.  
  326. /*+ The entries in the FTPOptions section. +*/
  327. static Entry ftpoptions_entries[]={{"anon-username",(void*)&FTPUserName,0,Fixed,String     },
  328.                                    {"anon-password",(void*)&FTPPassWord,0,Fixed,String     },
  329.                                    {"auth-hostname",(void*)&FTPAuthHost,1,Fixed,HostAndPort},
  330.                                    {"auth-username",(void*)&FTPAuthUser,1,Fixed,String     },
  331.                                    {"auth-password",(void*)&FTPAuthPass,1,Fixed,String     },
  332.                                    {NULL           ,NULL               ,0,-1   ,-1         }};
  333.  
  334. /*+ The FTPOptions section. +*/
  335. static Section ftpoptions_section={"FTPOptions",ftpoptions_entries};
  336.  
  337.  
  338. /* MIMETypes section */
  339.  
  340. /*+ The default MIME type. +*/
  341. static char *DefaultMIMEType; /* see SetDefaultValues() */
  342.  
  343. /*+ The list of MIME types. +*/
  344. static KeyPair **MIMETypes; /* see SetDefaultValues() */
  345.  
  346. /*+ The entries in the FTPOptions section. +*/
  347. static Entry mimetypes_entries[]={{"default",(void*)&DefaultMIMEType,0,Fixed        ,MIMEType},
  348.                                   {""       ,(void*)&MIMETypes      ,1,PathOrFileExt,MIMEType},
  349.                                   {NULL     ,NULL                   ,0,-1           ,-1      }};
  350.  
  351. /*+ The MIMETypes section. +*/
  352. static Section mimetypes_section={"MIMETypes",mimetypes_entries};
  353.  
  354.  
  355. /* Proxy section */
  356.  
  357. /*+ The list of hostnames and proxies. +*/
  358. static KeyPair **Proxies; /* see SetDefaultValues() */
  359.  
  360. /*+ The information that is needed to allow authorisation headers to be added, +*/
  361. static KeyPair **ProxyAuthHost, /*+ hostname +*/ /* see SetDefaultValues() */
  362.                **ProxyAuthUser, /*+ username +*/ /* see SetDefaultValues() */
  363.                **ProxyAuthPass; /*+ password +*/ /* see SetDefaultValues() */
  364.  
  365. /*+ The entries in the Proxy section. +*/
  366. static Entry proxy_entries[]={{"auth-hostname",(void*)&ProxyAuthHost,1,Fixed                ,HostAndPort      },
  367.                               {"auth-username",(void*)&ProxyAuthUser,1,Fixed                ,String           },
  368.                               {"auth-password",(void*)&ProxyAuthPass,1,Fixed                ,String           },
  369.                               {""             ,(void*)&Proxies      ,1,ProtoAndHostOrDefault,HostAndPortOrNone},
  370.                               {NULL           ,NULL                 ,0,-1                   ,-1               }};
  371.  
  372. /*+ The Proxy section. +*/
  373. static Section proxy_section={"Proxy",proxy_entries};
  374.  
  375.  
  376. /* Mirror section */
  377.  
  378. /*+ The list of protocols/hostnames and their mirrors. +*/
  379. static KeyPair **Mirrors; /* see SetDefaultValues() */
  380.  
  381. /*+ The entries in the Purge section. +*/
  382. static Entry mirror_entries[]={{""  ,(void*)&Mirrors,1,ProtoAndHost,ProtoAndHost},
  383.                                {NULL,NULL           ,0,-1          ,-1          }};
  384.  
  385. /*+ The Mirror section. +*/
  386. static Section mirror_section={"Mirror",mirror_entries};
  387.  
  388.  
  389. /* Purge section */
  390.  
  391. /*+ If true then use modification time instead of access time. +*/
  392. int PurgeUseMTime; /* see SetDefaultValues() */
  393.  
  394. /*+ The default age for purging files. +*/
  395. int DefaultPurgeAge; /* see SetDefaultValues() */
  396.  
  397. /*+ The maximum allowed size of the cache. +*/
  398. int PurgeCacheSize; /* see SetDefaultValues() */
  399.  
  400. /*+ The list of hostnames and purge ages. +*/
  401. static KeyPair **PurgeAges; /* see SetDefaultValues() */
  402.  
  403. /*+ The entries in the Purge section. +*/
  404. static Entry purge_entries[]={{"use-mtime",(void*)&PurgeUseMTime  ,0,Fixed       ,Boolean },
  405.                               {"default"  ,(void*)&DefaultPurgeAge,0,Fixed       ,AgeDays },
  406.                               {"max-size" ,(void*)&PurgeCacheSize ,0,Fixed       ,FileSize},
  407.                               {""         ,(void*)&PurgeAges      ,1,ProtoAndHost,AgeDays },
  408.                               {NULL       ,NULL                   ,0,-1          ,-1      }};
  409.  
  410. /*+ The Purge section. +*/
  411. static Section purge_section={"Purge",purge_entries};
  412.  
  413.  
  414. /* All sections */
  415.  
  416. /*+ The list of sections (NULL terminated). +*/
  417. static Section *sections[]={&startup_section,
  418.                             &options_section,
  419.                             &localhost_section,
  420.                             &localnet_section,
  421.                             &allowedconnect_section,
  422.                             &dontcache_section,
  423.                             &dontget_section,
  424.                             &dontgetrecursive_section,
  425.                             &censorheader_section,
  426.                             &ftpoptions_section,
  427.                             &mimetypes_section,
  428.                             &proxy_section,
  429.                             &mirror_section,
  430.                             &purge_section,
  431.                             NULL};
  432.  
  433.  
  434. /* Local functions */
  435.  
  436. static int match_host_specification(char *proto,char *host,char *host_spec);
  437. static int match_file_specification(char *path,char *file_spec);
  438.  
  439. static char *DefaultFTPPassWord(void);
  440.  
  441. static KeyPair **DefaultMirrorLinks(void);
  442.  
  443. static int ReadFile(FILE *config);
  444. static int ReadSection(FILE *config,Section *section);
  445. static int ParseEntry(Entry *entry,char *left,char *right);
  446. static int ParseValue(char *text,ConfigType type,void *value,int rhs);
  447. static int isanumber(const char *string);
  448.  
  449. static void SetDefaultValues(void);
  450. static void SaveOldValues(void);
  451. static void RestoreOldValues(void);
  452. static void RemoveOldValues(void);
  453. static void FreeKeyPairList(KeyPair **list,int andvalue);
  454.  
  455.  
  456. /*+ Only the first time that the config file is read can the StartUp section be used. +*/
  457. static int first_time=1;
  458.  
  459. /*+ The error message. +*/
  460. static char *errmsg;
  461.  
  462.  
  463. /*++++++++++++++++++++++++++++++++++++++
  464.   Read in the configuration file.
  465.  
  466.   int fd The file descriptor to write the error message to.
  467.   ++++++++++++++++++++++++++++++++++++++*/
  468.  
  469. int ReadConfigFile(int fd)
  470. {
  471.  FILE* config;
  472.  int line_no;
  473.  
  474.  if(first_time)
  475.     SetDefaultValues();
  476.  
  477.  errmsg=NULL;
  478.  
  479.  config=fopen(ConfigFile,"r");
  480.  if(!config)
  481.    {
  482.     write_formatted(fd,"Cannot open the configuration file '%s'\n",ConfigFile);
  483.     return(1);
  484.    }
  485.  
  486.  SaveOldValues();
  487.  SetDefaultValues();
  488.  
  489.  line_no=ReadFile(config);
  490.  
  491.  if(errmsg)
  492.     RestoreOldValues();
  493.  else
  494.     RemoveOldValues();
  495.  
  496.  fclose(config);
  497.  
  498.  if(errmsg)
  499.    {
  500.     write_formatted(fd,"Syntax error at line %d in '%s'; %s\n",line_no,ConfigFile,errmsg);
  501.     free(errmsg);
  502.    }
  503.  
  504.  if(first_time)
  505.     first_time=0;
  506.  
  507.  if(errmsg)
  508.     return(1);
  509.  else
  510.     return(0);
  511. }
  512.  
  513.  
  514. /*++++++++++++++++++++++++++++++++++++++
  515.   Get the first specified name of the server host.
  516.  
  517.   char *GetLocalHost Returns the first named localhost.
  518.  
  519.   int port If true then return the port as well.
  520.   ++++++++++++++++++++++++++++++++++++++*/
  521.  
  522. char *GetLocalHost(int port)
  523. {
  524.  char *localhost,*ret;
  525.  
  526.  if(LocalHost)
  527.     localhost=(*LocalHost)->name;
  528.  else
  529.     localhost="localhost";
  530.  
  531.  ret=(char*)malloc(strlen(localhost)+8);
  532.  
  533.  if(port)
  534.     sprintf(ret,"%s:%d",localhost,HTTP_Port);
  535.  else
  536.     strcpy(ret,localhost);
  537.  
  538.  return(ret);
  539. }
  540.  
  541.  
  542. /*++++++++++++++++++++++++++++++++++++++
  543.   Check if the specified hostname is to be cached.
  544.  
  545.   int IsLocalNetHost Return true if the host is on the local network.
  546.  
  547.   char *host The name of the host (and port number) to be checked.
  548.   ++++++++++++++++++++++++++++++++++++++*/
  549.  
  550. int IsLocalNetHost(char *host)
  551. {
  552.  KeyPair **p;
  553.  char *colon=strchr(host,':');
  554.  int isip=isdigit(*host);
  555.  int isit=0;
  556.  
  557.  if(colon)
  558.     *colon=0;
  559.  
  560.  if(LocalHost)
  561.     for(p=LocalHost;(*p)->name;p++)
  562.        if(!strcmp((*p)->name,host))
  563.          {isit=1;break;}
  564.  
  565.  if(LocalNet && !isit)
  566.     for(p=LocalNet;(*p)->name;p++)
  567.        if(strlen((*p)->name)<=strlen(host) &&
  568.           ((!isip && !strcmp ((*p)->name,host+strlen(host)-strlen((*p)->name))) ||
  569.            ( isip && !strncmp((*p)->name,host,strlen((*p)->name)))))
  570.          {isit=1;break;}
  571.  
  572.  if(colon)
  573.     *colon=':';
  574.  
  575.  return(isit);
  576. }
  577.  
  578.  
  579. /*++++++++++++++++++++++++++++++++++++++
  580.   Check if the specified hostname is allowed to connect.
  581.  
  582.   int IsAllowedConnect Return true if it is allowed to connect.
  583.  
  584.   char *host The name of the host (and port number) to be checked.
  585.   ++++++++++++++++++++++++++++++++++++++*/
  586.  
  587. int IsAllowedConnect(char *host)
  588. {
  589.  KeyPair **p;
  590.  int isip=isdigit(*host);
  591.  int isit=0;
  592.  
  593.  if(LocalHost)
  594.     for(p=LocalHost;(*p)->name;p++)
  595.        if(!strcmp((*p)->name,host))
  596.          {isit=1;break;}
  597.  
  598.  if(AllowedConnect && !isit)
  599.     for(p=AllowedConnect;(*p)->name;p++)
  600.        if(strlen((*p)->name)<=strlen(host) &&
  601.           ((!isip && !strcmp ((*p)->name,host+strlen(host)-strlen((*p)->name))) ||
  602.            ( isip && !strncmp((*p)->name,host,strlen((*p)->name)))))
  603.          {isit=1;break;}
  604.  
  605.  return(isit);
  606. }
  607.  
  608.  
  609. /*++++++++++++++++++++++++++++++++++++++
  610.   Check if the specified URL is to be cached.
  611.  
  612.   int IsNotCached Return true if it is not to be cached.
  613.  
  614.   char *proto The protocol to be checked.
  615.  
  616.   char *host The name of the host to be checked.
  617.  
  618.   char *path The path on the host to be checked.
  619.   ++++++++++++++++++++++++++++++++++++++*/
  620.  
  621. int IsNotCached(char *proto,char *host,char *path)
  622. {
  623.  KeyPair **d;
  624.  
  625.  if(DontCache)
  626.     for(d=DontCache;(*d)->name;d++)
  627.        if(match_host_specification(proto,host,(*d)->name))
  628.           if(match_file_specification(path,(*d)->value.string))
  629.              return(1);
  630.  
  631.  return(0);
  632. }
  633.  
  634.  
  635. /*++++++++++++++++++++++++++++++++++++++
  636.   Check if the specified URL is to be got.
  637.  
  638.   int IsNotGot Return true if it is not to be got.
  639.  
  640.   char *proto The protocol to be checked.
  641.  
  642.   char *host The name of the host (and port number) to be checked.
  643.  
  644.   char *path The path on the host to be checked.
  645.   ++++++++++++++++++++++++++++++++++++++*/
  646.  
  647. int IsNotGot(char *proto,char *host,char *path)
  648. {
  649.  KeyPair **d;
  650.  
  651.  if(DontGet)
  652.     for(d=DontGet;(*d)->name;d++)
  653.        if(match_host_specification(proto,host,(*d)->name))
  654.           if(match_file_specification(path,(*d)->value.string))
  655.              return(1);
  656.  
  657.  return(0);
  658. }
  659.  
  660.  
  661. /*++++++++++++++++++++++++++++++++++++++
  662.   Check if the specified URL is to be got recursively.
  663.  
  664.   int IsNotGotRecursive Return true if it is not to be got recursively.
  665.  
  666.   char *proto The protocol to be checked.
  667.  
  668.   char *host The name of the host (and port number) to be checked.
  669.  
  670.   char *path The path on the host to be checked.
  671.   ++++++++++++++++++++++++++++++++++++++*/
  672.  
  673. int IsNotGotRecursive(char *proto,char *host,char *path)
  674. {
  675.  KeyPair **d;
  676.  
  677.  if(DontGetRecursive)
  678.     for(d=DontGetRecursive;(*d)->name;d++)
  679.        if(match_host_specification(proto,host,(*d)->name))
  680.           if(match_file_specification(path,(*d)->value.string))
  681.              return(1);
  682.  
  683.  return(0);
  684. }
  685.  
  686.  
  687. /*++++++++++++++++++++++++++++++++++++++
  688.   Decide if the header line is to be sent to the server.
  689.  
  690.   int IsCensoredheader Returns true if the header line is to be deleted.
  691.  
  692.   char *line The line to check.
  693.   ++++++++++++++++++++++++++++++++++++++*/
  694.  
  695. int IsCensoredHeader(char *line)
  696. {
  697.  KeyPair **p;
  698.  int isit=0;
  699.  
  700.  if(CensorHeader && !isit)
  701.     for(p=CensorHeader;(*p)->name;p++)
  702.        if(line[strlen((*p)->name)]==':' &&
  703.           !strncmp((*p)->name,line,strlen((*p)->name)))
  704.          {isit=1;break;}
  705.  
  706.  return(isit);
  707. }
  708.  
  709.  
  710. /*++++++++++++++++++++++++++++++++++++++
  711.   Determine an email address to use as the FTP password.
  712.  
  713.   char *DefaultFTPPassword Returns a best-guess password.
  714.   ++++++++++++++++++++++++++++++++++++++*/
  715.  
  716. static char *DefaultFTPPassWord(void)
  717. {
  718.  struct passwd *pwd;
  719.  char *username,*fqdn,*password;
  720.  
  721.  pwd=getpwuid(getuid());
  722.  
  723.  if(!pwd)
  724.     username="root";
  725.  else
  726.     username=pwd->pw_name;
  727.  
  728.  fqdn=GetFQDN();
  729.  
  730.  if(!fqdn)
  731.     fqdn="";
  732.  
  733.  password=(char*)malloc(strlen(username)+strlen(fqdn)+4);
  734.  sprintf(password,"%s@%s",username,fqdn);
  735.  
  736.  return(password);
  737. }
  738.  
  739.  
  740. /*++++++++++++++++++++++++++++++++++++++
  741.   Determine what username and password to use for the FTP server.
  742.  
  743.   int WhatFTPUserPass Return 1 if there is a non-default one.
  744.  
  745.   char *host The FTP server.
  746.  
  747.   char **user Returns the username.
  748.  
  749.   char **pass Returns the password.
  750.   ++++++++++++++++++++++++++++++++++++++*/
  751.  
  752. int WhatFTPUserPass(char *host,char **user,char **pass)
  753. {
  754.  int def=0;
  755.  KeyPair **h,**u,**p;
  756.  
  757.  *user=FTPUserName;
  758.  *pass=FTPPassWord;
  759.  
  760.  if(FTPAuthHost && FTPAuthUser && FTPAuthPass)
  761.     for(h=FTPAuthHost,u=FTPAuthUser,p=FTPAuthPass;(*h)->value.string && (*u)->value.string && (*p)->value.string;h++,u++,p++)
  762.       {
  763.        if(!strcmp(host,(*h)->value.string))
  764.          {
  765.           *user=(*u)->value.string;
  766.           *pass=(*p)->value.string;
  767.           def=1;
  768.           break;
  769.          }
  770.       }
  771.  
  772.  return(def); 
  773. }
  774.  
  775.  
  776. /*++++++++++++++++++++++++++++++++++++++
  777.   Decide what mime type to apply for a given file.
  778.  
  779.   char *WhatMIMEType Returns the MIME Type.
  780.  
  781.   char *path The path of the file.
  782.   ++++++++++++++++++++++++++++++++++++++*/
  783.  
  784. char *WhatMIMEType(char *path)
  785. {
  786.  char *mimetype=DefaultMIMEType;
  787.  int maxlen=0;
  788.  KeyPair **m;
  789.  
  790.  if(MIMETypes)
  791.     for(m=MIMETypes;(*m)->name;m++)
  792.        if(strlen(path)>strlen((*m)->name) &&
  793.           strlen((*m)->name)>maxlen &&
  794.           !strcmp((*m)->name,path+strlen(path)-strlen((*m)->name)))
  795.          {mimetype=(*m)->value.string;maxlen=strlen((*m)->value.string);}
  796.  
  797.  return(mimetype);
  798. }
  799.  
  800.  
  801. /*++++++++++++++++++++++++++++++++++++++
  802.   Determine the proxy to use for a specified host.
  803.  
  804.   char *WhichProxy Return a pointer to the proxy.
  805.  
  806.   char *proto The name of the protocol used.
  807.  
  808.   char *host The name of the host to be contacted.
  809.   ++++++++++++++++++++++++++++++++++++++*/
  810.  
  811. char *WhichProxy(char *proto,char *host)
  812. {
  813.  KeyPair **p;
  814.  char *proxy=NULL;
  815.  int matchlen=0;
  816.  
  817.  if(Proxies)
  818.     for(p=Proxies;(*p)->name;p++)
  819.        if(match_host_specification(proto,host,(*p)->name))
  820.           if(strlen((*p)->name)>matchlen)
  821.             {
  822.              matchlen=strlen((*p)->name);
  823.              proxy=(*p)->value.string;
  824.             }
  825.  
  826.  return(proxy);
  827. }
  828.  
  829.  
  830. /*++++++++++++++++++++++++++++++++++++++
  831.   Determine what authentication password is required for the proxy.
  832.  
  833.   char *WhatProxyAuth Returns the password:username combination.
  834.  
  835.   char *proxy The name of the proxy.
  836.   ++++++++++++++++++++++++++++++++++++++*/
  837.  
  838. char *WhatProxyAuth(char *proxy)
  839. {
  840.  KeyPair **h,**u,**p;
  841.  char *user=NULL,*pass=NULL;
  842.  char *userpass=NULL;
  843.  
  844.  if(ProxyAuthHost && ProxyAuthUser && ProxyAuthPass)
  845.    {
  846.     for(h=ProxyAuthHost,u=ProxyAuthUser,p=ProxyAuthPass;(*h)->value.string && (*u)->value.string && (*p)->value.string;h++,u++,p++)
  847.       {
  848.        if(!strcmp(proxy,(*h)->value.string))
  849.          {
  850.           user=(*u)->value.string;
  851.           pass=(*p)->value.string;
  852.           break;
  853.          }
  854.       }
  855.  
  856.     if(user)
  857.       {
  858.        userpass=(char*)malloc(strlen(user)+strlen(pass)+2);
  859.        strcpy(userpass,user);
  860.        strcat(userpass,":");
  861.        strcat(userpass,pass);
  862.       }
  863.    }
  864.  
  865.  return(userpass);
  866. }
  867.  
  868.  
  869. /*++++++++++++++++++++++++++++++++++++++
  870.   Check if a specified protocol and host is mirrored on another host.
  871.  
  872.   int IsMirrored Returns non-zero if this is host is mirrored on another host.
  873.  
  874.   char *proto The protocol to check.
  875.  
  876.   char *host The hostname to check.
  877.  
  878.   char **new_proto The protocol of the preferred mirror.
  879.  
  880.   char **new_host The hostname of the preferred mirror.
  881.   ++++++++++++++++++++++++++++++++++++++*/
  882.  
  883. int IsMirrored(char *proto,char *host,char **new_proto,char **new_host)
  884. {
  885.  KeyPair **m;
  886.  
  887.  *new_proto=*new_host=NULL;
  888.  
  889.  if(Mirrors)
  890.     for(m=Mirrors;(*m)->name;m++)
  891.       {
  892.        char *slash1=strchr((*m)->name,'/'),*protopart1=NULL,*hostpart1=NULL;
  893.  
  894.        if(slash1)
  895.          {*slash1=0;protopart1=(*m)->name;hostpart1=slash1+1;}
  896.        else
  897.          {protopart1=NULL;hostpart1=(*m)->name;}
  898.  
  899.        if((protopart1==NULL || !strcmp(protopart1,proto)) &&
  900.           !strcmp(hostpart1,host))
  901.          {
  902.           char *slash2=strchr((*m)->value.string,'/'),*protopart2=NULL,*hostpart2=NULL;
  903.  
  904.           if(slash2)
  905.             {*slash2=0;protopart2=(*m)->value.string;hostpart2=slash2+1;}
  906.           else
  907.             {protopart2=NULL;hostpart2=(*m)->value.string;}
  908.  
  909.           if(protopart2)
  910.             {*new_proto=(char*)malloc(strlen(protopart2)+1);strcpy(*new_proto,protopart2);}
  911.           else
  912.              if(*protopart2)
  913.                {*new_proto=(char*)malloc(strlen(proto)+1);strcpy(*new_proto,proto);}
  914.  
  915.           if(new_proto)
  916.              *new_host=(char*)malloc(strlen(hostpart2)+1);strcpy(*new_host,hostpart2);
  917.  
  918.           if(slash2)
  919.              *slash2='/';
  920.          }
  921.  
  922.        if(slash1)
  923.           *slash1='/';
  924.       }
  925.  
  926.  return(*new_proto || *new_host);
  927. }
  928.  
  929.  
  930. /*++++++++++++++++++++++++++++++++++++++
  931.   Provide a list of all of the mirrored hosts for this protocol.
  932.  
  933.   char **ListMirrors Returns a list with hostname and mirror hostname.
  934.  
  935.   char *proto The protocol we are listing for.
  936.   ++++++++++++++++++++++++++++++++++++++*/
  937.  
  938. char ***ListMirrors(char *proto)
  939. {
  940.  char ***mirrors=NULL;
  941.  int nmirrors=0;
  942.  KeyPair **m;
  943.  
  944.  if(Mirrors)
  945.     for(m=Mirrors;(*m)->name;m++)
  946.       {
  947.        char *slash1=strchr((*m)->name,'/'),*protopart1=NULL,*hostpart1=NULL;
  948.  
  949.        if(slash1)
  950.          {*slash1=0;protopart1=(*m)->name;hostpart1=slash1+1;}
  951.        else
  952.          {protopart1=NULL;hostpart1=(*m)->name;}
  953.  
  954.        if((protopart1==NULL || !strcmp(protopart1,proto)))
  955.          {
  956.           char *slash2=strchr((*m)->value.string,'/'),*protopart2=NULL,*hostpart2=NULL;
  957.  
  958.           if(slash2)
  959.             {protopart2=(*m)->value.string;hostpart2=slash2+1;}
  960.           else
  961.             {protopart2=NULL;hostpart2=(*m)->value.string;}
  962.  
  963.           if(!(nmirrors%16))
  964.             {
  965.              if(!mirrors)
  966.                 mirrors=(char***)malloc(17*sizeof(char**));
  967.              else
  968.                 mirrors=(char***)realloc(mirrors,(nmirrors+17)*sizeof(char**));
  969.             }
  970.           mirrors[nmirrors]=(char**)malloc(2*sizeof(char*));
  971.  
  972.           mirrors[nmirrors][0]=(char*)malloc(strlen(hostpart1)+1);
  973.           strcpy(mirrors[nmirrors][0],hostpart1);
  974.  
  975.           mirrors[nmirrors][1]=(char*)malloc((protopart2?strlen(protopart2):strlen(hostpart2))+4);
  976.           if(protopart2)
  977.             {
  978.              strcpy(mirrors[nmirrors][1],"../");
  979.              strcat(mirrors[nmirrors][1],protopart2);
  980.             }
  981.           else
  982.              strcpy(mirrors[nmirrors][1],hostpart2);
  983.  
  984.           nmirrors++;
  985.          }
  986.  
  987.        if(slash1)
  988.           *slash1='/';
  989.       }
  990.  
  991.  if(mirrors)
  992.     mirrors[nmirrors]=NULL;
  993.  
  994.  return(mirrors);
  995. }
  996.  
  997.  
  998. /*++++++++++++++++++++++++++++++++++++++
  999.   Search through the cache for the symbolic links that are also mirror entries.
  1000.  
  1001.   KeyPair **DefaultMirrorLinks Return the list of links.
  1002.   ++++++++++++++++++++++++++++++++++++++*/
  1003.  
  1004. static KeyPair **DefaultMirrorLinks(void)
  1005. {
  1006.  KeyPair **links=NULL;
  1007.  int p;
  1008.  
  1009.  for(p=0;p<NProtocols;p++)
  1010.    {
  1011.     struct stat buf;
  1012.  
  1013.     if(!lstat(Protocols[p].name,&buf) && S_ISDIR(buf.st_mode))
  1014.       {
  1015.        DIR *dir2;
  1016.        struct dirent* ent2;
  1017.  
  1018.        if(chdir(Protocols[p].name))
  1019.          {PrintMessage(Warning,"Cannot change to directory '%s' [%!s]; no mirrors.",Protocols[p].name);continue;}
  1020.  
  1021.        /* Open the spool directory. */
  1022.  
  1023.        dir2=opendir(".");
  1024.        if(!dir2)
  1025.          {PrintMessage(Warning,"Cannot open directory '%s' [%!s]; no mirrors.",Protocols[p].name);chdir("..");continue;}
  1026.  
  1027.        ent2=readdir(dir2);  /* skip .  */
  1028.        if(!ent2)
  1029.          {PrintMessage(Warning,"Cannot read directory '%s' [%!s]; no mirrors.",Protocols[p].name);closedir(dir2);chdir("..");continue;}
  1030.        ent2=readdir(dir2);  /* skip .. */
  1031.  
  1032.        /* Search for all of the symbolic links. */
  1033.  
  1034.        while((ent2=readdir(dir2)))
  1035.          {
  1036.           struct stat buf2;
  1037.           char *host=ent2->d_name;
  1038.  
  1039.           if(!lstat(host,&buf2) && S_ISLNK(buf2.st_mode))
  1040.             {
  1041.              KeyPair **l;
  1042.              char link[257],*link_proto=Protocols[p].name,*link_host=host;
  1043.              int n,pp;
  1044.  
  1045.              /* Follow the link. */
  1046.  
  1047.              link[0]=0;
  1048.  
  1049.              if((n=readlink(host,link,256))==-1)
  1050.                {PrintMessage(Warning,"Cannot read link '%s/%s' [%!s].",Protocols[p].name,host);*link=0;break;}
  1051.  
  1052.              link[n]=0;
  1053.  
  1054.              if(!strncmp(link,"../",3))
  1055.                {
  1056.                 link_proto=link+3;
  1057.                 link_host=link_proto+1;
  1058.                 while(link_host && *link_host!='/')
  1059.                    link_host++;
  1060.                 *link_host++=0;
  1061.                }
  1062.              else
  1063.                 link_host=link;
  1064.  
  1065.              if(strchr(link_proto,'/') || strchr(link_host,'/'))
  1066.                 continue;
  1067.  
  1068.               for(pp=0;pp<NProtocols;pp++)
  1069.                  if(!strcmp(Protocols[pp].name,link_proto))
  1070.                     break;
  1071.  
  1072.               if(pp==NProtocols)
  1073.                  continue;
  1074.  
  1075.              /* Add it to the list. */
  1076.  
  1077.              if(!links)
  1078.                {
  1079.                 links=(KeyPair**)malloc(8*sizeof(KeyPair*));
  1080.                 *links=&KeyPairEnd;
  1081.                }
  1082.  
  1083.              l=links;
  1084.              for(n=0;(*l)->name;l++,n++);
  1085.  
  1086.              if(n%8==7)
  1087.                 links=(KeyPair**)realloc(links,(n+9)*sizeof(KeyPair*));
  1088.  
  1089.              for(l=links;(*l)->name;l++);
  1090.  
  1091.              *l=(KeyPair*)malloc(sizeof(KeyPair));
  1092.              (*l)->name=(char*)malloc(strlen(Protocols[p].name)+strlen(host)+2);
  1093.              sprintf((*l)->name,"%s/%s",Protocols[p].name,host);
  1094.              (*l)->value.string=(char*)malloc(strlen(link_proto)+strlen(link_host)+2);
  1095.              sprintf((*l)->value.string,"%s/%s",link_proto,link_host);
  1096.              l++;
  1097.              *l=&KeyPairEnd;
  1098.             }
  1099.          }
  1100.  
  1101.        closedir(dir2);
  1102.        chdir("..");
  1103.       }
  1104.    }
  1105.  
  1106.  return(links);
  1107. }
  1108.  
  1109.  
  1110. /*++++++++++++++++++++++++++++++++++++++
  1111.   Determine the age to use when purging for a specified host.
  1112.  
  1113.   int WhatPurgeAge Return the age in days.
  1114.  
  1115.   char *proto The name of the protocol used.
  1116.  
  1117.   char *host The name of the host to be purged.
  1118.   ++++++++++++++++++++++++++++++++++++++*/
  1119.  
  1120. int WhatPurgeAge(char *proto,char *host)
  1121. {
  1122.  KeyPair **p;
  1123.  int age=DefaultPurgeAge;
  1124.  int matchlen=0;
  1125.  
  1126.  if(PurgeAges)
  1127.     for(p=PurgeAges;(*p)->name;p++)
  1128.        if(match_host_specification(proto,host,(*p)->name))
  1129.           if(strlen((*p)->name)>matchlen)
  1130.             {
  1131.              matchlen=strlen((*p)->name);
  1132.              age=(*p)->value.integer;
  1133.             }
  1134.  
  1135.  return(age);
  1136. }
  1137.  
  1138.  
  1139. /*++++++++++++++++++++++++++++++++++++++
  1140.   Check if a protocol and host match a HOST-SPECIFICATION in the config file.
  1141.  
  1142.   int match_host_specification Return 1 if true else 0.
  1143.  
  1144.   char *proto The protocol.
  1145.  
  1146.   char *host The host.
  1147.  
  1148.   char *host_spec The HOST-SPECIFICATION from the config file.
  1149.   ++++++++++++++++++++++++++++++++++++++*/
  1150.  
  1151. static int match_host_specification(char *proto,char *host,char *host_spec)
  1152. {
  1153.  int match=0;
  1154.  char *colon=strchr(host,':');
  1155.  int isip=isdigit(*host);
  1156.  char *slash=strchr(host_spec,'/'),*colon2,*protopart=NULL,*hostpart=NULL;
  1157.  
  1158.  if(colon)
  1159.     *colon=0;
  1160.  
  1161.  if(slash)
  1162.    {*slash=0;protopart=host_spec;hostpart=slash+1;}
  1163.  else
  1164.    {protopart=NULL;hostpart=host_spec;}
  1165.  
  1166.  colon2=strchr(hostpart,':');
  1167.  if(colon2)
  1168.     *colon2=0;
  1169.  
  1170.  if((protopart==NULL || !*protopart || !strcmp(protopart,proto)) &&
  1171.     strlen(hostpart)<=strlen(host) &&
  1172.     (!*hostpart ||
  1173.      (!isip && !strcmp (hostpart,host+strlen(host)-strlen(hostpart))) ||
  1174.      ( isip && !strncmp(hostpart,host,strlen(hostpart)))) &&
  1175.     ((!colon && !colon2) || (colon2 && !*(colon2+1)) || (colon && colon2 && !strcmp(colon+1,colon2+1))))
  1176.     match=1;
  1177.  else
  1178.     match=0;
  1179.  
  1180.  if(slash)
  1181.     *slash='/';
  1182.  if(colon2)
  1183.     *colon2=':';
  1184.  
  1185.  if(colon)
  1186.     *colon=':';
  1187.  
  1188.  return(match);
  1189. }
  1190.  
  1191.  
  1192. /*++++++++++++++++++++++++++++++++++++++
  1193.   Check if a path matches a FILE-SPECIFICATION in the config file.
  1194.  
  1195.   int match_file_specification Return 1 if true else 0.
  1196.  
  1197.   char *path The path.
  1198.  
  1199.   char *file_spec The FILE-SPECIFICATION from the config file.
  1200.   ++++++++++++++++++++++++++++++++++++++*/
  1201.  
  1202. static int match_file_specification(char *path,char *file_spec)
  1203. {
  1204.  int match=0;
  1205.  
  1206.  if(*file_spec=='/' && !strncmp(file_spec,path,strlen(file_spec)))
  1207.     match=1;
  1208.  else if(*file_spec=='.' && strlen(path)>strlen(file_spec) && !strcmp(file_spec,path+strlen(path)-strlen(file_spec)))
  1209.     match=1;
  1210.  
  1211.  return(match);
  1212. }
  1213.  
  1214.  
  1215. /*++++++++++++++++++++++++++++++++++++++
  1216.   Read the data from the file.
  1217.  
  1218.   int ReadFile Returns the number of lines read.
  1219.  
  1220.   FILE *config The file to read from.
  1221.   ++++++++++++++++++++++++++++++++++++++*/
  1222.  
  1223. static int ReadFile(FILE *config)
  1224. {
  1225.  char *line=NULL;
  1226.  int line_no=0;
  1227.  
  1228.  while((line=fgets_realloc(line,config)))
  1229.    {
  1230.     int i;
  1231.     char *l=line;
  1232.     char *r=line+strlen(line)-1;
  1233.  
  1234.     line_no++;
  1235.  
  1236.     while(*l==' ' || *l=='\t' || *l=='\r' || *l=='\n')
  1237.        l++;
  1238.  
  1239.     if(!*l || *l=='#')
  1240.        continue;
  1241.  
  1242.     while(r>l && (*r==' ' || *r=='\t' || *r=='\r' || *r=='\n'))
  1243.        *r--=0;
  1244.  
  1245.     for(i=0;sections[i];i++)
  1246.        if(!strcmp(sections[i]->name,l))
  1247.           break;
  1248.  
  1249.     if(!sections[i])
  1250.       {errmsg=(char*)malloc(32+strlen(l));sprintf(errmsg,"Unrecognised section label '%s'.",l);break;}
  1251.     else
  1252.        line_no+=ReadSection(config,sections[i]);
  1253.  
  1254.     if(errmsg)
  1255.        break;
  1256.    }
  1257.  
  1258.  if(line)
  1259.     free(line);
  1260.  
  1261.  return(line_no);
  1262. }
  1263.  
  1264.  
  1265. /*++++++++++++++++++++++++++++++++++++++
  1266.   Read all the data from a section.
  1267.  
  1268.   int ReadSection Returns the number of lines read.
  1269.  
  1270.   FILE *config The file to read from.
  1271.  
  1272.   Section *section The section to read.
  1273.   ++++++++++++++++++++++++++++++++++++++*/
  1274.  
  1275. static int ReadSection(FILE *config,Section *section)
  1276. {
  1277.  char *line=NULL;
  1278.  int line_no=0;
  1279.  int open_brace=0;
  1280.  
  1281.  while((line=fgets_realloc(line,config)))
  1282.    {
  1283.     int i;
  1284.     char *l=line;
  1285.     char *r=line+strlen(line)-1;
  1286.  
  1287.     line_no++;
  1288.  
  1289.     while(*l==' ' || *l=='\t' || *l=='\r' || *l=='\n')
  1290.        l++;
  1291.  
  1292.     if(!*l || *l=='#')
  1293.        continue;
  1294.  
  1295.     while(r>l && (*r==' ' || *r=='\t' || *r=='\r' || *r=='\n'))
  1296.        *r--=0;
  1297.  
  1298.     if(*l=='{')
  1299.       {
  1300.        if(*++l)
  1301.          {errmsg=(char*)malloc(32);strcpy(errmsg,"Open brace has trailing junk.");break;}
  1302.        else
  1303.          {
  1304.           if(open_brace)
  1305.             {errmsg=(char*)malloc(32);strcpy(errmsg,"Open brace repeated.");break;}
  1306.  
  1307.           open_brace=1;
  1308.          }
  1309.       }
  1310.     else if(*l=='}')
  1311.       {
  1312.        if(*++l)
  1313.          {errmsg=(char*)malloc(32);strcpy(errmsg,"Close brace has trailing junk.");break;}
  1314.        else
  1315.          {
  1316.           if(!open_brace)
  1317.             {errmsg=(char*)malloc(32);strcpy(errmsg,"No open brace seen.");break;}
  1318.  
  1319.           break;
  1320.          }
  1321.       }
  1322.     else
  1323.       {
  1324.        if(!open_brace)
  1325.          {errmsg=(char*)malloc(32);strcpy(errmsg,"Open brace missing.");break;}
  1326.  
  1327.        if(!first_time && section==*sections)
  1328.           continue;
  1329.  
  1330.        for(i=0;section->entries[i].name;i++)
  1331.           if(!*section->entries[i].name || !strncmp(section->entries[i].name,l,strlen(section->entries[i].name)))
  1332.             {
  1333.              char *ll,*rr;
  1334.  
  1335.              if(*section->entries[i].name)
  1336.                {
  1337.                 ll=l+strlen(section->entries[i].name);
  1338.  
  1339.                 if(*ll && *ll!='=' && *ll!=' ' && *ll!='\t' && *ll!='\r' && *ll!='\n')
  1340.                    continue;
  1341.                }
  1342.              else
  1343.                {
  1344.                 ll=l;
  1345.                 while(*ll && *ll!='=' && *ll!=' ' && *ll!='\t' && *ll!='\r' && *ll!='\n')
  1346.                    ll++;
  1347.                }
  1348.  
  1349.              if(section->entries[i].right_type==None)
  1350.                {
  1351.                 *ll=0;
  1352.                 r=NULL;
  1353.                }
  1354.              else
  1355.                {
  1356.                 r=strchr(line,'=');
  1357.                 if(!r)
  1358.                   {errmsg=(char*)malloc(32);strcpy(errmsg,"No equal sign for entry.");break;}
  1359.  
  1360.                 *ll=0;
  1361.                 if(!*l)
  1362.                   {errmsg=(char*)malloc(48);strcpy(errmsg,"Nothing to the left of the equal sign.");break;}
  1363.  
  1364.                 r++;
  1365.                 while(*r==' ' || *r=='\t' || *r=='\r' || *r=='\n')
  1366.                    r++;
  1367.  
  1368.                 rr=r;
  1369.                 while(*rr && *rr!=' ' && *rr!='\t' && *rr!='\r' && *rr!='\n')
  1370.                    rr++;
  1371.  
  1372.                 *rr=0;
  1373.                }
  1374.              break;
  1375.             }
  1376.  
  1377.        if(errmsg)
  1378.           break;
  1379.  
  1380.        if(!section->entries[i].name)
  1381.          {errmsg=(char*)malloc(32+strlen(l));sprintf(errmsg,"Unrecognised entry label '%s'.",l);break;}
  1382.  
  1383.        ParseEntry(§ion->entries[i],l,r);
  1384.  
  1385.        if(errmsg)
  1386.           break;
  1387.       }
  1388.    }
  1389.  
  1390.  if(line)
  1391.     free(line);
  1392.  
  1393.  return(line_no);
  1394. }
  1395.  
  1396.  
  1397. /*++++++++++++++++++++++++++++++++++++++
  1398.   Parse an entry from the file.
  1399.  
  1400.   int ParseEntry Returns non-zero if there was an error.
  1401.  
  1402.   Entry *entries The list of matching entry in the section.
  1403.  
  1404.   char *left The string to the left of the equals sign.
  1405.  
  1406.   char *right The string to the right of the equals sign.
  1407.   ++++++++++++++++++++++++++++++++++++++*/
  1408.  
  1409. static int ParseEntry(Entry *entry,char *left,char *right)
  1410. {
  1411.  if(entry->list)
  1412.    {
  1413.     KeyPair **p,*new=(KeyPair*)malloc(sizeof(KeyPair));
  1414.     int i;
  1415.  
  1416.     if(entry->left_type==Fixed)
  1417.        new->name=entry->name;
  1418.     else
  1419.        if(ParseValue(left,entry->left_type,(void*)&new->name,0))
  1420.          {free(new);return(1);}
  1421.  
  1422.     if(entry->right_type==None)
  1423.        new->value.string=NULL;
  1424.     else
  1425.        if(ParseValue(right,entry->right_type,(void*)&new->value,1))
  1426.          {free(new);return(1);}
  1427.  
  1428.     if(!*(KeyPair***)entry->pointer)
  1429.       {
  1430.        *(KeyPair***)entry->pointer=(KeyPair**)malloc(8*sizeof(KeyPair*));
  1431.        **(KeyPair***)entry->pointer=&KeyPairEnd;
  1432.       }
  1433.  
  1434.     p=*(KeyPair***)entry->pointer;
  1435.     for(i=0;(*p)->name;p++,i++);
  1436.  
  1437.     if(i%8==7)
  1438.        *(KeyPair***)entry->pointer=(KeyPair**)realloc(*(void**)entry->pointer,(i+9)*sizeof(KeyPair*));
  1439.  
  1440.     for(p=*(KeyPair***)entry->pointer;(*p)->name;p++);
  1441.  
  1442.     *p=new;
  1443.     p++;
  1444.     *p=&KeyPairEnd;
  1445.    }
  1446.  else
  1447.     if(ParseValue(right,entry->right_type,entry->pointer,1))
  1448.        return(1);
  1449.  
  1450.  return(0);
  1451. }
  1452.  
  1453.  
  1454. /*++++++++++++++++++++++++++++++++++++++
  1455.   Parse the text and put a value into the location.
  1456.  
  1457.   int ParseValue Returns non-zero in case of error.
  1458.  
  1459.   char *text The text string to parse.
  1460.  
  1461.   ConfigType type The type we are looking for.
  1462.  
  1463.   void *value The location to store the value.
  1464.  
  1465.   int rhs Set to true if this is the right hand side.
  1466.   ++++++++++++++++++++++++++++++++++++++*/
  1467.  
  1468. static int ParseValue(char *text,ConfigType type,void *value,int rhs)
  1469. {
  1470.  switch(type)
  1471.    {
  1472.    case Fixed:
  1473.    case None:
  1474.     break;
  1475.  
  1476.    case Boolean:
  1477.     if(!*text)
  1478.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a Boolean, got nothing.");}
  1479.     else if(!strcasecmp(text,"yes") || !strcasecmp(text,"true"))
  1480.        *(int*)value=1;
  1481.     else if(!strcasecmp(text,"no") || !strcasecmp(text,"false"))
  1482.        *(int*)value=0;
  1483.     else
  1484.       {errmsg=(char*)malloc(48+strlen(text));sprintf(errmsg,"Expecting a Boolean, got '%s'.",text);}
  1485.     break;
  1486.  
  1487.    case PortNumber:
  1488.     if(!*text)
  1489.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a port number, got nothing.");}
  1490.     else if(!isanumber(text))
  1491.       {errmsg=(char*)malloc(40+strlen(text));sprintf(errmsg,"Expecting a port number, got '%s'.",text);}
  1492.     else
  1493.       {
  1494.        *(int*)value=atoi(text);
  1495.        if(*(int*)value<=0 || *(int*)value>65535)
  1496.          {errmsg=(char*)malloc(32);sprintf(errmsg,"Invalid port number %d.",*(int*)value);}
  1497.       }
  1498.     break;
  1499.  
  1500.    case AgeDays:
  1501.     if(!*text)
  1502.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting an age in days, got nothing.");}
  1503.     else if(!isanumber(text))
  1504.       {errmsg=(char*)malloc(40+strlen(text));sprintf(errmsg,"Expecting an age in days, got '%s'.",text);}
  1505.     else
  1506.        *(int*)value=atoi(text);
  1507.     break;
  1508.  
  1509.    case TimeSecs:
  1510.     if(!*text)
  1511.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a time in seconds, got nothing.");}
  1512.     else if(!isanumber(text))
  1513.       {errmsg=(char*)malloc(40+strlen(text));sprintf(errmsg,"Expecting an time in seconds, got '%s'.",text);}
  1514.     else
  1515.        *(int*)value=atoi(text);
  1516.     break;
  1517.  
  1518.    case FileSize:
  1519.     if(!*text)
  1520.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a file size in MB, got nothing.");}
  1521.     else if(!isanumber(text))
  1522.       {errmsg=(char*)malloc(40+strlen(text));sprintf(errmsg,"Expecting a file size in MB, got '%s'.",text);}
  1523.     else
  1524.       {
  1525.        *(int*)value=atoi(text);
  1526.        if(*(int*)value<0)
  1527.          {errmsg=(char*)malloc(48);sprintf(errmsg,"Invalid file size %d.",*(int*)value);}
  1528.       }
  1529.     break;
  1530.  
  1531.    case CfgMaxServers:
  1532.     if(!*text)
  1533.       {errmsg=(char*)malloc(56);strcpy(errmsg,"Expecting a maximum server count, got nothing.");}
  1534.     else if(!isanumber(text))
  1535.       {errmsg=(char*)malloc(48+strlen(text));sprintf(errmsg,"Expecting a maximum server count, got '%s'.",text);}
  1536.     else
  1537.       {
  1538.        *(int*)value=atoi(text);
  1539.        if(*(int*)value<=0 || *(int*)value>MAX_SERVERS)
  1540.          {errmsg=(char*)malloc(48);sprintf(errmsg,"Invalid maximum server count: %d.",*(int*)value);}
  1541.       }
  1542.     break;
  1543.  
  1544.    case CfgMaxFetchServers:
  1545.     if(!*text)
  1546.       {errmsg=(char*)malloc(56);strcpy(errmsg,"Expecting a maximum fetch server count, got nothing.");}
  1547.     else if(!isanumber(text))
  1548.       {errmsg=(char*)malloc(56+strlen(text));sprintf(errmsg,"Expecting a maximum fetch server count, got '%s'.",text);}
  1549.     else
  1550.       {
  1551.        *(int*)value=atoi(text);
  1552.        if(*(int*)value<=0 || *(int*)value>MAX_FETCH_SERVERS)
  1553.          {errmsg=(char*)malloc(48);sprintf(errmsg,"Invalid maximum fetch server count: %d.",*(int*)value);}
  1554.       }
  1555.     break;
  1556.  
  1557.    case CfgLogLevel:
  1558.     if(!*text)
  1559.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a log level, got nothing.");}
  1560.     else if(strcasecmp(text,"debug")==0)
  1561.        *(int*)value=Debug;
  1562.     else if(strcasecmp(text,"info")==0)
  1563.        *(int*)value=Inform;
  1564.     else if(strcasecmp(text,"important")==0)
  1565.        *(int*)value=Important;
  1566.     else if(strcasecmp(text,"warning")==0)
  1567.        *(int*)value=Warning;
  1568.     else if(strcasecmp(text,"fatal")==0)
  1569.        *(int*)value=Fatal;
  1570.     else
  1571.       {errmsg=(char*)malloc(48+strlen(text));sprintf(errmsg,"Expecting a log level, got '%s'.",text);}
  1572.     break;
  1573.  
  1574.    case UserId:
  1575.     if(!*text)
  1576.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a username or uid, got nothing.");}
  1577.     else
  1578.       {
  1579.        int uid;
  1580.        struct passwd *userInfo=getpwnam(text);
  1581.        if(userInfo)
  1582.           uid=userInfo->pw_uid;
  1583.        else
  1584.          {
  1585.           if(sscanf(text,"%d",&uid)!=1)
  1586.             {errmsg=(char*)malloc(32+strlen(text));sprintf(errmsg,"Invalid user %s.",text);}
  1587.           else if(!getpwuid(uid))
  1588.             {errmsg=(char*)malloc(32);sprintf(errmsg,"Unknown user id %d.",uid);}
  1589.          }
  1590.        *(int*)value=uid;
  1591.       }
  1592.     break;
  1593.  
  1594.    case GroupId:
  1595.     if(!*text)
  1596.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a group name or gid, got nothing.");}
  1597.     else
  1598.       {
  1599.        int gid;
  1600.        struct group *groupInfo=getgrnam(text);
  1601.        if(groupInfo)
  1602.           gid=groupInfo->gr_gid;
  1603.        else
  1604.          {
  1605.           if(sscanf(text,"%d",&gid)!=1)
  1606.             {errmsg=(char*)malloc(32+strlen(text));sprintf(errmsg,"Invalid group %s.",text);}
  1607.           else if(!getgrgid(gid))
  1608.             {errmsg=(char*)malloc(32);sprintf(errmsg,"Unknown group id %d.",gid);}
  1609.          }
  1610.        *(int*)value=gid;
  1611.       }
  1612.     break;
  1613.  
  1614.    case String:
  1615.     if(!*text || !strcasecmp(text,"none"))
  1616.        *(char**)value=NULL;
  1617.     else
  1618.       {
  1619.        *(char**)value=(char*)malloc(strlen(text)+1);
  1620.        strcpy(*(char**)value,text);
  1621.       }
  1622.     break;
  1623.  
  1624.    case DirName:
  1625.     if(!*text)
  1626.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a directory name, got nothing.");}
  1627.     else if(*text!='/')
  1628.       {errmsg=(char*)malloc(48+strlen(text));sprintf(errmsg,"Expecting an absolute directory name, got '%s'.",text);}
  1629.     else
  1630.       {
  1631.        *(char**)value=(char*)malloc(strlen(text)+1);
  1632.        strcpy(*(char**)value,text);
  1633.       }
  1634.     break;
  1635.  
  1636.    case PathOrFileExt:
  1637.     if(!*text)
  1638.       {errmsg=(char*)malloc(40);strcpy(errmsg,"Expecting a path name or file extension, got nothing.");}
  1639.     else if(*text!='.' && *text!='/')
  1640.       {errmsg=(char*)malloc(40+strlen(text));sprintf(errmsg,"Expecting a path name or file extension, got '%s'.",text);}
  1641.     else
  1642.       {
  1643.        *(char**)value=(char*)malloc(strlen(text)+1);
  1644.        strcpy(*(char**)value,text);
  1645.       }
  1646.     break;
  1647.  
  1648.    case MIMEType:
  1649.      if(!*text)
  1650.        {errmsg=(char*)malloc(40);strcpy(errmsg,"Expecting a MIME Type, got nothing.");}
  1651.      else
  1652.        {
  1653.         char *slash=strchr(text,'/');
  1654.         if(!slash)
  1655.           {errmsg=(char*)malloc(48+strlen(text));sprintf(errmsg,"Expecting a MIME Type/Subtype, got '%s'.",text);}
  1656.         *(char**)value=(char*)malloc(strlen(text)+1);
  1657.         strcpy(*(char**)value,text);
  1658.        }
  1659.     break;
  1660.  
  1661.    case Host:
  1662.      if(!*text)
  1663.        {errmsg=(char*)malloc(40);strcpy(errmsg,"Expecting a hostname, got nothing.");}
  1664.      else
  1665.        {
  1666.         char *colon=strchr(text,':'),*p;
  1667.         if(colon)
  1668.           {errmsg=(char*)malloc(56+strlen(text));sprintf(errmsg,"Expecting a hostname not a port number, got '%s'.",text);}
  1669.         else
  1670.           {
  1671.            *(char**)value=(char*)malloc(strlen(text)+1);
  1672.            for(p=text;*p;p++)
  1673.               *p=tolower(*p);
  1674.            strcpy(*(char**)value,text);
  1675.           }
  1676.        }
  1677.      break;
  1678.  
  1679.    case HostAndPortOrNone:
  1680.     if(rhs && (!*text || !strcasecmp(text,"none")))
  1681.       {
  1682.        *(char**)value=NULL;
  1683.        break;
  1684.       }
  1685.  
  1686.     /* fall through */
  1687.  
  1688.    case HostAndPort:
  1689.     if(!*text)
  1690.       {errmsg=(char*)malloc(48);strcpy(errmsg,"Expecting a hostname (and port), got nothing.");}
  1691.     else
  1692.       {
  1693.        char *colon=strchr(text,':'),*p;
  1694.        if(*text==':')
  1695.          {errmsg=(char*)malloc(48+strlen(text));sprintf(errmsg,"Expecting a hostname before the ':', got '%s'.",text);}
  1696.        else
  1697.          {
  1698.           if(colon && (!isanumber(colon+1) || atoi(colon+1)<=0 || atoi(colon+1)>65535))
  1699.             {errmsg=(char*)malloc(32+strlen(colon));sprintf(errmsg,"Invalid port number %s.",colon+1);}
  1700.           else
  1701.             {
  1702.              *(char**)value=(char*)malloc(strlen(text)+1);
  1703.              for(p=text;*p;p++)
  1704.                 *p=tolower(*p);
  1705.              strcpy(*(char**)value,text);
  1706.             }
  1707.          }
  1708.       }
  1709.     break;
  1710.  
  1711.    case ProtoAndHostOrDefault:
  1712.     if(!strcmp("default",text))
  1713.       {
  1714.        *(char**)value=(char*)malloc(2);
  1715.        strcpy(*(char**)value,"/");
  1716.        break;
  1717.       }
  1718.  
  1719.     /* fall through */
  1720.  
  1721.    case ProtoAndHost:
  1722.      if(!*text)
  1723.        {errmsg=(char*)malloc(64);strcpy(errmsg,"Expecting a protoocol and hostname, got nothing.");}
  1724.      else
  1725.        {
  1726.         char *p;
  1727.         *(char**)value=(char*)malloc(strlen(text)+1);
  1728.         for(p=text;*p;p++)
  1729.            *p=tolower(*p);
  1730.         strcpy(*(char**)value,text);
  1731.        }
  1732.      break;
  1733.    }
  1734.  
  1735.  if(errmsg)
  1736.     return(1);
  1737.  else
  1738.     return(0);
  1739. }
  1740.  
  1741.  
  1742. /*++++++++++++++++++++++++++++++++++++++
  1743.   Decide if a string is an integer.
  1744.  
  1745.   int isanumber Returns 1 if it is, 0 if not.
  1746.  
  1747.   const char *string The string that may be an integer.
  1748.   ++++++++++++++++++++++++++++++++++++++*/
  1749.  
  1750. static int isanumber(const char *string)
  1751. {
  1752.  int i=0;
  1753.  
  1754.  if(string[0]=='-' || string[i]=='+')
  1755.     i++;
  1756.  
  1757.  for(;string[i];i++)
  1758.     if(!isdigit(string[i]))
  1759.        return(0);
  1760.  
  1761.  return(1);
  1762. }
  1763.  
  1764.  
  1765. /*+ The old value of the level of error logging, see ErrorLevel in errors.h +*/
  1766. static int old_LogLevel;
  1767.  
  1768. /*+ The old value of the option to also fetch images. +*/
  1769. static int old_FetchImages;
  1770.  
  1771. /*+ The old value of the option to also fetch frames. +*/
  1772. static int old_FetchFrames;
  1773.  
  1774. /*+ The old value of the number of days to display in the index of the latest pages. +*/
  1775. static int old_IndexLatestDays;
  1776.  
  1777. /*+ The old value of the option for a tag that can be added to the bottom of the spooled pages with the date and a refresh button. +*/
  1778. static int old_AddInfoRefresh;
  1779.  
  1780. /*+ The old value of the maximum age of a cached page to use in preference while online. +*/
  1781. static int old_RequestChanged;
  1782.  
  1783. /*+ The old value of the option to allow or ignore the 'Pragma: no-cache' request. +*/
  1784. static int old_PragmaNoCache;
  1785.  
  1786. /*+ The old value of the option to not make requests while offline but to return an error. +*/
  1787. static int old_OfflineRequests;
  1788.  
  1789. /*+ The old value of the interval in days between monitoring the specified pages. +*/
  1790. static int old_MonitorInterval;
  1791.  
  1792. /*+ The old values of the list of localhost hostnames. +*/
  1793. static KeyPair **old_LocalHost;
  1794.  
  1795. /*+ The old value of the list of local network hostnames. +*/
  1796. static KeyPair **old_LocalNet;
  1797.  
  1798. /*+ The old values of the list of allowed hostnames. +*/
  1799. static KeyPair **old_AllowedConnect;
  1800.  
  1801. /*+ The old value of the list of servers and reasons not to cache. +*/
  1802. static KeyPair **old_DontCache;
  1803.  
  1804. /*+ The old value of the list of servers and reasons not to get. +*/
  1805. static KeyPair **old_DontGet;
  1806.  
  1807. /*+ The old value of the list of servers and reasons not to get. +*/
  1808. static KeyPair **old_DontGetRecursive;
  1809.  
  1810. /*+ The old_value of the list of censored headers. +*/
  1811. static KeyPair **old_CensorHeader;
  1812.  
  1813. /*+ The old value of the anon-ftp username. +*/
  1814. static char *old_FTPUserName;
  1815.  
  1816. /*+ The old value of the anon-ftp password. +*/
  1817. static char *old_FTPPassWord;
  1818.  
  1819. /*+ The old value of the information that is needed to allow non-anonymous access, +*/
  1820. static KeyPair **old_FTPAuthHost, /*+ hostname +*/
  1821.                **old_FTPAuthUser, /*+ username +*/
  1822.                **old_FTPAuthPass; /*+ password +*/
  1823.  
  1824. /*+ The old value of the default MIME type. +*/
  1825. static char *old_DefaultMIMEType;
  1826.  
  1827. /*+ The old value of the list of MIME types. +*/
  1828. static KeyPair **old_MIMETypes;
  1829.  
  1830. /*+ The old value of the list of hostnames and proxies. +*/
  1831. static KeyPair **old_Proxies;
  1832.  
  1833. /*+ The old value of the information that is needed to allow authorisation headers to be added, +*/
  1834. static KeyPair **old_ProxyAuthHost, /*+ hostname +*/
  1835.                **old_ProxyAuthUser, /*+ username +*/
  1836.                **old_ProxyAuthPass; /*+ password +*/
  1837.  
  1838. /*+ The old value of the list of protocols/hostnames and their mirrors. +*/
  1839. static KeyPair **old_Mirrors;
  1840.  
  1841. /*+ The old value of the flag that if true then use modification time instead of access time. +*/
  1842. static int old_PurgeUseMTime;
  1843.  
  1844. /*+ The old value of the default age for purging files. +*/
  1845. static int old_DefaultPurgeAge;
  1846.  
  1847. /*+ The old value of the maximum allowed size of the cache. +*/
  1848. static int old_PurgeCacheSize;
  1849.  
  1850. /*+ The old value of the list of hostnames and purge ages. +*/
  1851. static KeyPair **old_PurgeAges;
  1852.  
  1853.  
  1854. /*++++++++++++++++++++++++++++++++++++++
  1855.   Set default values.
  1856.   ++++++++++++++++++++++++++++++++++++++*/
  1857.  
  1858. static void SetDefaultValues(void)
  1859. {
  1860.  LogLevel=Important;            /* see also variable declaration. */
  1861.  FetchImages=0;
  1862.  FetchFrames=0;
  1863.  IndexLatestDays=7;
  1864.  AddInfoRefresh=0;
  1865.  RequestChanged=600;
  1866.  PragmaNoCache=1;
  1867.  OfflineRequests=1;
  1868.  MonitorInterval=7;
  1869.  
  1870.  LocalHost=NULL;
  1871.  
  1872.  LocalNet=NULL;
  1873.  
  1874.  AllowedConnect=NULL;
  1875.  
  1876.  DontCache=NULL;
  1877.  
  1878.  DontGet=NULL;
  1879.  
  1880.  DontGetRecursive=NULL;
  1881.  
  1882.  CensorHeader=NULL;
  1883.  
  1884.  FTPUserName=(char*)malloc(16); strcpy(FTPUserName,"anonymous");
  1885.  FTPPassWord=DefaultFTPPassWord();
  1886.  FTPAuthHost=NULL;
  1887.  FTPAuthUser=NULL;
  1888.  FTPAuthPass=NULL;
  1889.  
  1890.  DefaultMIMEType=(char*)malloc(32); strcpy(DefaultMIMEType,"text/plain");
  1891.  MIMETypes=NULL;
  1892.  
  1893.  Proxies=NULL;
  1894.  ProxyAuthHost=NULL;
  1895.  ProxyAuthUser=NULL;
  1896.  ProxyAuthPass=NULL;
  1897.  
  1898.  Mirrors=DefaultMirrorLinks();
  1899.  
  1900.  PurgeUseMTime=0;
  1901.  DefaultPurgeAge=DEF_PURGE_AGE;
  1902.  PurgeCacheSize=0;
  1903.  PurgeAges=NULL;
  1904. }
  1905.  
  1906.  
  1907. /*++++++++++++++++++++++++++++++++++++++
  1908.   Save the old values in case the re-read of the file fails.
  1909.   ++++++++++++++++++++++++++++++++++++++*/
  1910.  
  1911. static void SaveOldValues(void)
  1912. {
  1913.  old_LogLevel=LogLevel;
  1914.  old_FetchImages=FetchImages;
  1915.  old_FetchFrames=FetchFrames;
  1916.  old_IndexLatestDays=IndexLatestDays;
  1917.  old_AddInfoRefresh=AddInfoRefresh;
  1918.  old_RequestChanged=RequestChanged;
  1919.  old_PragmaNoCache=PragmaNoCache;
  1920.  old_OfflineRequests=OfflineRequests;
  1921.  old_MonitorInterval=MonitorInterval;
  1922.  
  1923.  old_LocalHost=LocalHost;
  1924.  
  1925.  old_LocalNet=LocalNet;
  1926.  
  1927.  old_AllowedConnect=AllowedConnect;
  1928.  
  1929.  old_DontCache=DontCache;
  1930.  
  1931.  old_DontGet=DontGet;
  1932.  
  1933.  old_DontGetRecursive=DontGetRecursive;
  1934.  
  1935.  old_CensorHeader=CensorHeader;
  1936.  
  1937.  old_FTPUserName=FTPUserName;
  1938.  old_FTPPassWord=FTPPassWord;
  1939.  old_FTPAuthHost=FTPAuthHost;
  1940.  old_FTPAuthUser=FTPAuthUser;
  1941.  old_FTPAuthPass=FTPAuthPass;
  1942.  
  1943.  old_DefaultMIMEType=DefaultMIMEType;
  1944.  old_MIMETypes=MIMETypes;
  1945.  
  1946.  old_Proxies=Proxies;
  1947.  old_ProxyAuthHost=ProxyAuthHost;
  1948.  old_ProxyAuthUser=ProxyAuthUser;
  1949.  old_ProxyAuthPass=ProxyAuthPass;
  1950.  
  1951.  old_Mirrors=Mirrors;
  1952.  
  1953.  old_PurgeUseMTime=PurgeUseMTime;
  1954.  old_DefaultPurgeAge=DefaultPurgeAge;
  1955.  old_PurgeCacheSize=PurgeCacheSize;
  1956.  old_PurgeAges=PurgeAges;
  1957. }
  1958.  
  1959.  
  1960. /*++++++++++++++++++++++++++++++++++++++
  1961.   Restore the old values if the re-read failed.
  1962.   ++++++++++++++++++++++++++++++++++++++*/
  1963.  
  1964. static void RestoreOldValues(void)
  1965. {
  1966.  LogLevel=old_LogLevel;
  1967.  FetchImages=old_FetchImages;
  1968.  FetchFrames=old_FetchFrames;
  1969.  IndexLatestDays=old_IndexLatestDays;
  1970.  AddInfoRefresh=old_AddInfoRefresh;
  1971.  RequestChanged=old_RequestChanged;
  1972.  PragmaNoCache=old_PragmaNoCache;
  1973.  OfflineRequests=old_OfflineRequests;
  1974.  MonitorInterval=old_MonitorInterval;
  1975.  
  1976.  if(LocalHost)
  1977.     FreeKeyPairList(LocalHost,1);
  1978.  LocalHost=old_LocalHost;
  1979.  
  1980.  if(LocalNet)
  1981.     FreeKeyPairList(LocalNet,1);
  1982.  LocalNet=old_LocalNet;
  1983.  
  1984.  if(AllowedConnect)
  1985.     FreeKeyPairList(AllowedConnect,1);
  1986.  AllowedConnect=old_AllowedConnect;
  1987.  
  1988.  if(DontCache)
  1989.     FreeKeyPairList(DontCache,3);
  1990.  DontCache=old_DontCache;
  1991.  
  1992.  if(DontGet)
  1993.     FreeKeyPairList(DontGet,3);
  1994.  DontGet=old_DontGet;
  1995.  
  1996.  if(DontGetRecursive)
  1997.     FreeKeyPairList(DontGetRecursive,3);
  1998.  DontGetRecursive=old_DontGetRecursive;
  1999.  
  2000.  if(CensorHeader)
  2001.     FreeKeyPairList(CensorHeader,1);
  2002.  CensorHeader=old_CensorHeader;
  2003.  
  2004.  if(FTPUserName)
  2005.     free(FTPUserName);
  2006.  FTPUserName=old_FTPUserName;
  2007.  if(FTPPassWord)
  2008.     free(FTPPassWord);
  2009.  FTPPassWord=old_FTPPassWord;
  2010.  if(FTPAuthHost)
  2011.     FreeKeyPairList(FTPAuthHost,2);
  2012.  FTPAuthHost=old_FTPAuthHost;
  2013.  if(FTPAuthUser)
  2014.     FreeKeyPairList(FTPAuthUser,2);
  2015.  FTPAuthUser=old_FTPAuthUser;
  2016.  if(FTPAuthPass)
  2017.     FreeKeyPairList(FTPAuthPass,2);
  2018.  FTPAuthPass=old_FTPAuthPass;
  2019.  
  2020.  if(DefaultMIMEType)
  2021.     free(DefaultMIMEType);
  2022.  DefaultMIMEType=old_DefaultMIMEType;
  2023.  if(MIMETypes)
  2024.     FreeKeyPairList(MIMETypes,3);
  2025.  MIMETypes=old_MIMETypes;
  2026.  
  2027.  if(Proxies)
  2028.     FreeKeyPairList(Proxies,3);
  2029.  Proxies=old_Proxies;
  2030.  if(ProxyAuthHost)
  2031.     FreeKeyPairList(ProxyAuthHost,2);
  2032.  ProxyAuthHost=old_ProxyAuthHost;
  2033.  if(ProxyAuthUser)
  2034.     FreeKeyPairList(ProxyAuthUser,2);
  2035.  ProxyAuthUser=old_ProxyAuthUser;
  2036.  if(ProxyAuthPass)
  2037.     FreeKeyPairList(ProxyAuthPass,2);
  2038.  ProxyAuthPass=old_ProxyAuthPass;
  2039.  
  2040.  if(Mirrors)
  2041.     FreeKeyPairList(Mirrors,3);
  2042.  Mirrors=old_Mirrors;
  2043.  
  2044.  PurgeUseMTime=old_PurgeUseMTime;
  2045.  DefaultPurgeAge=old_DefaultPurgeAge;
  2046.  PurgeCacheSize=old_PurgeCacheSize;
  2047.  if(PurgeAges)
  2048.     FreeKeyPairList(PurgeAges,1);
  2049.  PurgeAges=old_PurgeAges;
  2050. }
  2051.  
  2052.  
  2053. /*++++++++++++++++++++++++++++++++++++++
  2054.   Remove the old values if the re-read succeeded.
  2055.   ++++++++++++++++++++++++++++++++++++++*/
  2056.  
  2057. static void RemoveOldValues(void)
  2058. {
  2059.  if(old_LocalHost)
  2060.     FreeKeyPairList(old_LocalHost,1);
  2061.  
  2062.  if(old_LocalNet)
  2063.     FreeKeyPairList(old_LocalNet,1);
  2064.  
  2065.  if(old_AllowedConnect)
  2066.     FreeKeyPairList(old_AllowedConnect,1);
  2067.  
  2068.  if(old_DontCache)
  2069.     FreeKeyPairList(old_DontCache,3);
  2070.  
  2071.  if(old_DontGet)
  2072.     FreeKeyPairList(old_DontGet,3);
  2073.  
  2074.  if(old_DontGetRecursive)
  2075.     FreeKeyPairList(old_DontGetRecursive,3);
  2076.  
  2077.  if(old_CensorHeader)
  2078.     FreeKeyPairList(old_CensorHeader,1);
  2079.  
  2080.  if(old_FTPUserName)
  2081.     free(old_FTPUserName);
  2082.  if(old_FTPPassWord)
  2083.     free(old_FTPPassWord);
  2084.  if(old_FTPAuthHost)
  2085.     FreeKeyPairList(old_FTPAuthHost,2);
  2086.  if(old_FTPAuthUser)
  2087.     FreeKeyPairList(old_FTPAuthUser,2);
  2088.  if(old_FTPAuthPass)
  2089.     FreeKeyPairList(old_FTPAuthPass,2);
  2090.  
  2091.  if(old_DefaultMIMEType)
  2092.     free(old_DefaultMIMEType);
  2093.  if(old_MIMETypes)
  2094.     FreeKeyPairList(old_MIMETypes,3);
  2095.  
  2096.  if(old_Proxies)
  2097.     FreeKeyPairList(old_Proxies,3);
  2098.  if(old_ProxyAuthHost)
  2099.     FreeKeyPairList(old_ProxyAuthHost,2);
  2100.  if(old_ProxyAuthUser)
  2101.     FreeKeyPairList(old_ProxyAuthUser,2);
  2102.  if(old_ProxyAuthPass)
  2103.     FreeKeyPairList(old_ProxyAuthPass,2);
  2104.  
  2105.  if(old_Mirrors)
  2106.     FreeKeyPairList(old_Mirrors,3);
  2107.  
  2108.  if(old_PurgeAges)
  2109.     FreeKeyPairList(old_PurgeAges,1);
  2110. }
  2111.  
  2112.  
  2113. /*++++++++++++++++++++++++++++++++++++++
  2114.   Free a KeyPair list.
  2115.  
  2116.   KeyPair **list The list to free.
  2117.  
  2118.   int freewhat If 1 then free the name, if 2 then free the value, if 3 free both, if 0 free neither.
  2119.   ++++++++++++++++++++++++++++++++++++++*/
  2120.  
  2121. static void FreeKeyPairList(KeyPair **list,int freewhat)
  2122. {
  2123.  KeyPair **p;
  2124.  
  2125.  for(p=list;(*p)->name;p++)
  2126.    {
  2127.     if(freewhat&1)
  2128.        free((*p)->name);
  2129.     if(freewhat&2 && (*p)->value.string)
  2130.        free((*p)->value.string);
  2131.     free(*p);
  2132.    }
  2133.  free(list);
  2134. }
  2135.  
  2136. #else /* SIMPLE */
  2137.  
  2138. char *GetLocalHost(int port)
  2139. {
  2140.  static char ret[16];
  2141.  
  2142.  if(port)
  2143.     sprintf(ret,"localhost:%d",HTTP_Port);
  2144.  else
  2145.     sprintf(ret,"localhost");
  2146.  
  2147.  return(ret);
  2148. }
  2149.  
  2150. #endif /* SIMPLE */
  2151.