Back to index

citadel  8.12
config.c
Go to the documentation of this file.
00001 /*
00002  * Read and write the citadel.config file
00003  *
00004  * Copyright (c) 1987-2012 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "sysdep.h"
00016 #include <stdlib.h>
00017 #include <sys/stat.h>
00018 #include <unistd.h>
00019 #include <stdio.h>
00020 #include <errno.h>
00021 #include <string.h>
00022 #include <limits.h>
00023 #include <sys/utsname.h>
00024 #include <libcitadel.h>
00025 #include "citadel.h"
00026 #include "server.h"
00027 #include "config.h"
00028 #include "ctdl_module.h"
00029 
00030 struct config config;
00031 
00032 /*
00033  * Put some sane default values into our configuration.  Some will be overridden when we run setup.
00034  */
00035 void brand_new_installation_set_defaults(void) {
00036 
00037        struct passwd *pw;
00038        struct utsname my_utsname;
00039        struct hostent *he;
00040 
00041        /* Determine our host name, in case we need to use it as a default */
00042        uname(&my_utsname);
00043 
00044        /* set some sample/default values in place of blanks... */
00045        extract_token(config.c_nodename, my_utsname.nodename, 0, '.', sizeof config.c_nodename);
00046        if (IsEmptyStr(config.c_fqdn) ) {
00047               if ((he = gethostbyname(my_utsname.nodename)) != NULL) {
00048                      safestrncpy(config.c_fqdn, he->h_name, sizeof config.c_fqdn);
00049               }
00050               else {
00051                      safestrncpy(config.c_fqdn, my_utsname.nodename, sizeof config.c_fqdn);
00052               }
00053        }
00054 
00055        safestrncpy(config.c_humannode, "Citadel Server", sizeof config.c_humannode);
00056        safestrncpy(config.c_phonenum, "US 800 555 1212", sizeof config.c_phonenum);
00057        config.c_initax = 4;
00058        safestrncpy(config.c_moreprompt, "<more>", sizeof config.c_moreprompt);
00059        safestrncpy(config.c_twitroom, "Trashcan", sizeof config.c_twitroom);
00060        safestrncpy(config.c_baseroom, BASEROOM, sizeof config.c_baseroom);
00061        safestrncpy(config.c_aideroom, "Aide", sizeof config.c_aideroom);
00062        config.c_port_number = 504;
00063        config.c_sleeping = 900;
00064        config.c_instant_expunge = 1;
00065 
00066        if (config.c_ctdluid == 0) {
00067               pw = getpwnam("citadel");
00068               if (pw != NULL) {
00069                      config.c_ctdluid = pw->pw_uid;
00070               }
00071        }
00072        if (config.c_ctdluid == 0) {
00073               pw = getpwnam("bbs");
00074               if (pw != NULL) {
00075                      config.c_ctdluid = pw->pw_uid;
00076               }
00077        }
00078        if (config.c_ctdluid == 0) {
00079               pw = getpwnam("guest");
00080               if (pw != NULL) {
00081                      config.c_ctdluid = pw->pw_uid;
00082               }
00083        }
00084        if (config.c_createax == 0) {
00085               config.c_createax = 3;
00086        }
00087 
00088        /*
00089         * Default port numbers for various services
00090         */
00091        config.c_smtp_port = 25;
00092        config.c_pop3_port = 110;
00093        config.c_imap_port = 143;
00094        config.c_msa_port = 587;
00095        config.c_smtps_port = 465;
00096        config.c_pop3s_port = 995;
00097        config.c_imaps_port = 993;
00098        config.c_pftcpdict_port = -1 ;
00099        config.c_managesieve_port = 2020;
00100        config.c_xmpp_c2s_port = 5222;
00101        config.c_xmpp_s2s_port = 5269;
00102 }
00103 
00104 
00105 
00106 /*
00107  * get_config() is called during the initialization of Citadel server.
00108  * It verifies the system's integrity and reads citadel.config into memory.
00109  */
00110 void get_config(void) {
00111        FILE *cfp;
00112        int rv;
00113 
00114        if (chdir(ctdl_bbsbase_dir) != 0) {
00115               fprintf(stderr,
00116                      "This program could not be started.\nUnable to change directory to %s\nError: %s\n",
00117                      ctdl_bbsbase_dir,
00118                      strerror(errno)
00119               );
00120               exit(CTDLEXIT_HOME);
00121        }
00122 
00123        memset(&config, 0, sizeof(struct config));
00124        cfp = fopen(file_citadel_config, "rb");
00125        if (cfp != NULL) {
00126               rv = fread((char *) &config, sizeof(struct config), 1, cfp);
00127               if (rv != 1)
00128               {
00129                      fprintf(stderr, 
00130                             "Warning: The config file %s has unexpected size. \n",
00131                             file_citadel_config
00132                      );
00133               }
00134               fclose(cfp);
00135        }
00136        else {
00137               brand_new_installation_set_defaults();
00138        }
00139 
00140        /* Ensure that we are linked to the correct version of libcitadel */
00141        if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
00142               fprintf(stderr, "    You are running libcitadel version %d.%02d\n",
00143                      (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
00144               fprintf(stderr, "citserver was compiled against version %d.%02d\n",
00145                      (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
00146               exit(CTDLEXIT_LIBCITADEL);
00147        }
00148 
00149        /* Only allow LDAP auth mode if we actually have LDAP support */
00150 #ifndef HAVE_LDAP
00151        if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
00152               fprintf(stderr, "Your system is configured for LDAP authentication,\n"
00153                             "but you are running a server built without OpenLDAP support.\n");
00154               exit(CTDL_EXIT_UNSUP_AUTH);
00155        }
00156 #endif
00157 
00158         /* Default maximum message length is 10 megabytes.  This is site
00159         * configurable.  Also check to make sure the limit has not been
00160         * set below 8192 bytes.
00161          */
00162         if (config.c_maxmsglen <= 0)
00163                 config.c_maxmsglen = 10485760;
00164         if (config.c_maxmsglen < 8192)
00165                 config.c_maxmsglen = 8192;
00166 
00167         /* Default lower and upper limits on number of worker threads */
00168 
00169        if (config.c_min_workers < 3)             /* no less than 3 */
00170               config.c_min_workers = 5;
00171 
00172        if (config.c_max_workers == 0)                   /* default maximum */
00173               config.c_max_workers = 256;
00174 
00175        if (config.c_max_workers < config.c_min_workers)   /* max >= min */
00176               config.c_max_workers = config.c_min_workers;
00177 
00178        /* Networking more than once every five minutes just isn't sane */
00179        if (config.c_net_freq == 0L)
00180               config.c_net_freq = 3600L;  /* once per hour default */
00181        if (config.c_net_freq < 300L) 
00182               config.c_net_freq = 300L;
00183 
00184        /* Same goes for POP3 */
00185        if (config.c_pop3_fetch == 0L)
00186               config.c_pop3_fetch = 3600L;       /* once per hour default */
00187        if (config.c_pop3_fetch < 300L) 
00188               config.c_pop3_fetch = 300L;
00189        if (config.c_pop3_fastest == 0L)
00190               config.c_pop3_fastest = 3600L;     /* once per hour default */
00191        if (config.c_pop3_fastest < 300L) 
00192               config.c_pop3_fastest = 300L;
00193 
00194        /* "create new user" only works with native authentication mode */
00195        if (config.c_auth_mode != AUTHMODE_NATIVE) {
00196               config.c_disable_newu = 1;
00197        }
00198 }
00199 
00200 
00201 /*
00202  * Occasionally, we will need to write the config file, because some operations
00203  * change site-wide parameters.
00204  */
00205 void put_config(void)
00206 {
00207        FILE *cfp;
00208        int blocks_written = 0;
00209 
00210        if ((cfp = fopen(file_citadel_config, "w")) != NULL) {
00211               blocks_written = fwrite((char *) &config, sizeof(struct config), 1, cfp);
00212               if (blocks_written == 1) {
00213                      fclose(cfp);
00214                      chown(file_citadel_config, CTDLUID, (-1));
00215                      chmod(file_citadel_config, 0600);
00216                      return;
00217               }
00218        }
00219        syslog(LOG_EMERG, "%s: %s", file_citadel_config, strerror(errno));
00220 }