* Makefile.in, configure.in, chkpwd.c, acconfig.h: support for
[citadel.git] / citadel / config.c
1 /*
2  * This function reads the citadel.config file.  It should be called at
3  * the beginning of EVERY Citadel program.
4  *
5  * $Id$
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <limits.h>
16 #include "citadel.h"
17 #include "config.h"
18
19 struct config config;
20 char bbs_home_directory[PATH_MAX];
21 int home_specified = 0;
22
23 void get_config(void) {
24         FILE *cfp;
25         struct stat st;
26
27         if (chdir( home_specified ? bbs_home_directory : BBSDIR ) != 0) {
28                 fprintf(stderr, "Cannot start.\nThere is no Citadel installation in %s\n%s\n",
29                         (home_specified ? bbs_home_directory : BBSDIR),
30                         strerror(errno));
31                 exit(1);
32                 }
33         cfp=fopen("citadel.config","rb");
34         if (cfp==NULL) {
35                 fprintf(stderr, "Cannot start.\n");
36                 fprintf(stderr, "There is no citadel.config in %s\n%s\n",
37                         (home_specified ? bbs_home_directory : BBSDIR),
38                         strerror(errno));
39                 exit(1);
40                 }
41         fread((char *)&config,sizeof(struct config),1,cfp);
42         if (fstat(fileno(cfp), &st)) {
43                 perror("citadel.config");
44                 exit(1);
45                 }
46         if (st.st_uid != BBSUID || st.st_mode != (S_IFREG | S_IRUSR | S_IWUSR))
47                 {
48                 fprintf(stderr, "check the permissions on citadel.config\n");
49                 exit(1);
50                 }
51         fclose(cfp);
52         if ( (config.c_setup_level / 10) != (REV_LEVEL/10) ) {
53                 fprintf(stderr, "config: Your data files are out of date.  ");
54                 fprintf(stderr, "Run setup to update them.\n");
55                 fprintf(stderr,
56                         "        This program requires level %d.%02d\n",
57                                 (REV_LEVEL / 100), (REV_LEVEL % 100) );
58                 fprintf(stderr,
59                         "        Data files are currently at %d.%02d\n",
60                                 (config.c_setup_level / 100),
61                                 (config.c_setup_level % 100) );
62                 exit(1);
63                 }
64         }
65
66
67 /*
68  * Occasionally, we will need to write the config file, because some operations
69  * change site-wide parameters.
70  */
71 void put_config(void) {
72         FILE *cfp;
73         
74         if ((cfp = fopen("citadel.config", "rb+")) == NULL)
75                 perror("citadel.config");
76         else {
77                 fwrite((char *)&config, sizeof(struct config), 1, cfp);
78                 fclose(cfp);
79                 }
80         }