Moved to new module init structure.
[citadel.git] / citadel / config.c
1 /*
2  * $Id$
3  *
4  * This function reads the citadel.config file.  It should be called at
5  * the beginning of EVERY Citadel program.
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <limits.h>
17 #include "citadel.h"
18 #include "server.h"
19 #include "config.h"
20
21 struct config config;
22
23 /*
24  * get_config() is called during the initialization of any program which
25  * directly accesses Citadel data files.  It verifies the system's integrity
26  * and reads citadel.config into memory.
27  */
28 void get_config(void) {
29         FILE *cfp;
30         struct stat st;
31
32         if (chdir(ctdl_bbsbase_dir) != 0) {
33                 fprintf(stderr,
34                         "This program could not be started.\n"
35                         "Unable to change directory to %s\n"
36                         "Error: %s\n",
37                         ctdl_bbsbase_dir,
38                         strerror(errno));
39                 exit(CTDLEXIT_HOME);
40         }
41         cfp = fopen(file_citadel_config, "rb");
42         if (cfp == NULL) {
43                 fprintf(stderr, "This program could not be started.\n"
44                                 "Unable to open %s\n"
45                                 "Error: %s\n",
46                                 file_citadel_config,
47                                 strerror(errno));
48                 exit(CTDLEXIT_CONFIG);
49         }
50         fread((char *) &config, sizeof(struct config), 1, cfp);
51         if (fstat(fileno(cfp), &st)) {
52                 perror(file_citadel_config);
53                 exit(CTDLEXIT_CONFIG);
54         }
55 #ifndef __CYGWIN__
56         if (st.st_uid != CTDLUID) {
57                 fprintf(stderr, "%s must be owned by uid="F_UID_T" but "F_UID_T" owns it!\n", 
58                         file_citadel_config, CTDLUID, st.st_uid);
59                 exit(CTDLEXIT_CONFIG);
60         }
61         int desired_mode = (S_IFREG | S_IRUSR | S_IWUSR) ;
62         if (st.st_mode != desired_mode) {
63                 fprintf(stderr, "%s must be set to permissions mode %03o but they are %03o\n",
64                         file_citadel_config, desired_mode, st.st_mode);
65                 exit(CTDLEXIT_CONFIG);
66         }
67 #endif
68         fclose(cfp);
69
70         if (config.c_setup_level < REV_MIN) {
71                 fprintf(stderr, "config: Your data files are out of date.  ");
72                 fprintf(stderr, "Run setup to update them.\n");
73                 fprintf(stderr,
74                         "        This program requires level %d.%02d\n",
75                         (REV_LEVEL / 100), (REV_LEVEL % 100));
76                 fprintf(stderr,
77                         "        Data files are currently at %d.%02d\n",
78                         (config.c_setup_level / 100),
79                         (config.c_setup_level % 100));
80                 exit(CTDLEXIT_OOD);
81         }
82
83         /* Default maximum message length is 10 megabytes.  This is site
84          * configurable.  Also check to make sure the limit has not been
85          * set below 8192 bytes.
86          */
87         if (config.c_maxmsglen <= 0)
88                 config.c_maxmsglen = 10485760;
89         if (config.c_maxmsglen < 8192)
90                 config.c_maxmsglen = 8192;
91
92         /* Default lower and upper limits on number of worker threads */
93
94         if (config.c_min_workers < 3)           /* no less than 3 */
95                 config.c_min_workers = 5;
96
97         if (config.c_max_workers == 0)                  /* default maximum */
98                 config.c_max_workers = 256;
99
100         if (config.c_max_workers < config.c_min_workers)   /* max >= min */
101                 config.c_max_workers = config.c_min_workers;
102
103         /* Networking more than once every five minutes just isn't sane */
104         if (config.c_net_freq == 0L)
105                 config.c_net_freq = 3600L;      /* once per hour default */
106         if (config.c_net_freq < 300L) 
107                 config.c_net_freq = 300L;
108
109         /* "create new user" never works with host auth */
110         if (config.c_auth_mode == 1)
111                 config.c_disable_newu = 1;
112 }
113
114
115 /*
116  * Occasionally, we will need to write the config file, because some operations
117  * change site-wide parameters.
118  */
119 void put_config(void)
120 {
121         FILE *cfp;
122
123         if ((cfp = fopen(file_citadel_config, "rb+")) == NULL)
124                 perror(file_citadel_config);
125         else {
126                 fwrite((char *) &config, sizeof(struct config), 1, cfp);
127                 fclose(cfp);
128         }
129 }