Back to index

citadel  8.12
serv_inetcfg.c
Go to the documentation of this file.
00001 /*
00002  * This module handles the loading/saving and maintenance of the
00003  * system's Internet configuration.  It's not an optional component; I
00004  * wrote it as a module merely to keep things as clean and loosely coupled
00005  * as possible.
00006  *
00007  * Copyright (c) 1987-2012 by the citadel.org team
00008  *
00009  *  This program is open source software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License version 3.
00011  *  
00012  *  
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  
00020  *  
00021  *  
00022  */
00023 
00024 #include "sysdep.h"
00025 #include <stdlib.h>
00026 #include <unistd.h>
00027 #include <stdio.h>
00028 #include <fcntl.h>
00029 #include <signal.h>
00030 #include <pwd.h>
00031 #include <errno.h>
00032 #include <sys/types.h>
00033 
00034 #if TIME_WITH_SYS_TIME
00035 # include <sys/time.h>
00036 # include <time.h>
00037 #else
00038 # if HAVE_SYS_TIME_H
00039 #  include <sys/time.h>
00040 # else
00041 #  include <time.h>
00042 # endif
00043 #endif
00044 
00045 #include <sys/wait.h>
00046 #include <string.h>
00047 #include <limits.h>
00048 #include <libcitadel.h>
00049 #include "citadel.h"
00050 #include "server.h"
00051 #include "citserver.h"
00052 #include "support.h"
00053 #include "config.h"
00054 #include "user_ops.h"
00055 #include "database.h"
00056 #include "msgbase.h"
00057 #include "internet_addressing.h"
00058 #include "genstamp.h"
00059 #include "domain.h"
00060 
00061 
00062 #include "ctdl_module.h"
00063 
00064 
00065 void inetcfg_setTo(struct CtdlMessage *msg) {
00066        char *conf;
00067        char buf[SIZ];
00068        
00069        if (msg->cm_fields['M']==NULL) return;
00070        conf = strdup(msg->cm_fields['M']);
00071 
00072        if (conf != NULL) {
00073               do {
00074                      extract_token(buf, conf, 0, '\n', sizeof buf);
00075                      strcpy(conf, &conf[strlen(buf)+1]);
00076               } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
00077 
00078               if (inetcfg != NULL) free(inetcfg);
00079               inetcfg = conf;
00080        }
00081 }
00082 
00083 
00084 /*
00085  * This handler detects changes being made to the system's Internet
00086  * configuration.
00087  */
00088 int inetcfg_aftersave(struct CtdlMessage *msg) {
00089        char *ptr;
00090        int linelen;
00091 
00092        /* If this isn't the configuration room, or if this isn't a MIME
00093         * message, don't bother.
00094         */
00095        if (strcasecmp(msg->cm_fields['O'], SYSCONFIGROOM)) return(0);
00096        if (msg->cm_format_type != 4) return(0);
00097 
00098        ptr = msg->cm_fields['M'];
00099        while (ptr != NULL) {
00100        
00101               linelen = strcspn(ptr, "\n");
00102               if (linelen == 0) return(0);       /* end of headers */ 
00103               
00104               if (!strncasecmp(ptr, "Content-type: ", 14)) {
00105                      if (!strncasecmp(&ptr[14], INTERNETCFG,
00106                         strlen(INTERNETCFG))) {
00107                             inetcfg_setTo(msg);  /* changing configs */
00108                      }
00109               }
00110 
00111               ptr = strchr((char *)ptr, '\n');
00112               if (ptr != NULL) ++ptr;
00113        }
00114 
00115        return(0);
00116 }
00117 
00118 
00119 void inetcfg_init_backend(long msgnum, void *userdata) {
00120        struct CtdlMessage *msg;
00121 
00122               msg = CtdlFetchMessage(msgnum, 1);
00123               if (msg != NULL) {
00124               inetcfg_setTo(msg);
00125                      CtdlFreeMessage(msg);
00126        }
00127 }
00128 
00129 
00130 void inetcfg_init(void) {
00131        if (CtdlGetRoom(&CC->room, SYSCONFIGROOM) != 0) return;
00132        CtdlForEachMessage(MSGS_LAST, 1, NULL, INTERNETCFG, NULL,
00133               inetcfg_init_backend, NULL);
00134 }
00135 
00136 
00137 
00138 
00139 /*****************************************************************************/
00140 /*                      MODULE INITIALIZATION STUFF                          */
00141 /*****************************************************************************/
00142 void clenaup_inetcfg(void)
00143 {
00144        char *buf;
00145 
00146        buf = inetcfg;
00147        inetcfg = NULL;
00148        if (buf != NULL)
00149               free(buf);
00150 }
00151 
00152 CTDL_MODULE_INIT(inetcfg)
00153 {
00154        if (!threading)
00155        {
00156               CtdlRegisterMessageHook(inetcfg_aftersave, EVT_AFTERSAVE);
00157               inetcfg_init();
00158               CtdlRegisterCleanupHook(clenaup_inetcfg);
00159        }
00160        
00161        /* return our module name for the log */
00162        return "inetcfg";
00163 }
00164