Back to index

citadel  8.12
citadel_dirs.c
Go to the documentation of this file.
00001 /*
00002  * citadel_dirs.c : calculate pathnames for various files used in the Citadel system
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 #include <unistd.h>
00015 #include <stdio.h>
00016 #include <sys/stat.h>
00017 #include <ctype.h>
00018 #include <string.h>
00019 #include <limits.h>
00020 
00021 #if TIME_WITH_SYS_TIME
00022 # include <sys/time.h>
00023 # include <time.h>
00024 #else
00025 # if HAVE_SYS_TIME_H
00026 #  include <sys/time.h>
00027 # else
00028 #  include <time.h>
00029 # endif
00030 #endif
00031 
00032 #include <errno.h>
00033 #include <libcitadel.h>
00034 
00035 
00036 #include "citadel.h"
00037 
00038 /* our directories... */
00039 char ctdl_home_directory[PATH_MAX] = "";
00040 char ctdl_bio_dir[PATH_MAX]="bio";
00041 char ctdl_bb_dir[PATH_MAX]="bitbucket";
00042 char ctdl_data_dir[PATH_MAX]="data";
00043 char ctdl_dspam_dir[PATH_MAX]="dspam";
00044 char ctdl_file_dir[PATH_MAX]="files";
00045 char ctdl_hlp_dir[PATH_MAX]="help";
00046 char ctdl_shared_dir[PATH_MAX]="";
00047 char ctdl_image_dir[PATH_MAX]="images";
00048 char ctdl_info_dir[PATH_MAX]="info";
00049 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
00050 char ctdl_message_dir[PATH_MAX]="messages";
00051 char ctdl_usrpic_dir[PATH_MAX]="userpics";
00052 char ctdl_bbsbase_dir[PATH_MAX]="";
00053 char ctdl_etc_dir[PATH_MAX]="";
00054 char ctdl_autoetc_dir[PATH_MAX]="";
00055 /* attention! this may be non volatile on some oses */
00056 char ctdl_run_dir[PATH_MAX]="";
00057 char ctdl_spool_dir[PATH_MAX]="network";
00058 char ctdl_netout_dir[PATH_MAX]="network/spoolout";
00059 char ctdl_netin_dir[PATH_MAX]="network/spoolin";
00060 char ctdl_nettmp_dir[PATH_MAX]="network/spooltmp";
00061 char ctdl_netcfg_dir[PATH_MAX]="netconfigs";
00062 char ctdl_utilbin_dir[PATH_MAX]="";
00063 char ctdl_sbin_dir[PATH_MAX]="";
00064 char ctdl_bin_dir[PATH_MAX]="";
00065 
00066 /* some of our files, that are needed in several places */
00067 char file_citadel_control[PATH_MAX]="";
00068 char file_citadel_rc[PATH_MAX]="";
00069 char file_citadel_config[PATH_MAX]="";
00070 char file_citadel_urlshorteners[PATH_MAX]="";
00071 char file_lmtp_socket[PATH_MAX]="";
00072 char file_lmtp_unfiltered_socket[PATH_MAX]="";
00073 char file_arcq[PATH_MAX]="";
00074 char file_citadel_socket[PATH_MAX]="";
00075 char file_citadel_admin_socket[PATH_MAX]="";
00076 char file_mail_aliases[PATH_MAX]="";
00077 char file_pid_file[PATH_MAX]="";
00078 char file_pid_paniclog[PATH_MAX]="";
00079 char file_crpt_file_key[PATH_MAX]="";
00080 char file_crpt_file_csr[PATH_MAX]="";
00081 char file_crpt_file_cer[PATH_MAX]="";
00082 char file_chkpwd[PATH_MAX]="";
00083 char file_base64[PATH_MAX]="";
00084 char file_guesstimezone[PATH_MAX]="";
00085 char file_funambol_msg[PATH_MAX] = "";
00086 char file_dpsam_conf[PATH_MAX] = "";
00087 char file_dspam_log[PATH_MAX] = "";
00088 
00089 
00090 
00091 
00092 
00093 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
00094        snprintf(SUBDIR,sizeof SUBDIR,  "%s%s%s%s%s%s%s", \
00095                       (home&!relh)?ctdl_home_directory:basedir, \
00096              ((basedir!=ctdldir)&(home&!relh))?basedir:"/", \
00097              ((basedir!=ctdldir)&(home&!relh))?"/":"", \
00098                       relhome, \
00099              (relhome[0]!='\0')?"/":"",\
00100                       dirbuffer,\
00101                       (dirbuffer[0]!='\0')?"/":"");
00102 
00103 #define DBG_PRINT(A) if (dbg==1) fprintf (stderr,"%s : %s \n", #A, A)
00104 
00105 
00106 void calc_dirs_n_files(int relh, int home, const char *relhome, char  *ctdldir, int dbg)
00107 {
00108        const char* basedir = "";
00109        char dirbuffer[PATH_MAX] = "";
00110 
00111        /*
00112         * Ok, we keep our binaries either in the citadel base dir,
00113         * or in /usr/sbin / /usr/bin
00114         */
00115        StripSlashes(ctdldir, 1);
00116 #ifdef HAVE_ETC_DIR
00117        snprintf(ctdl_sbin_dir, sizeof ctdl_sbin_dir, "/usr/sbin/");
00118        snprintf(ctdl_bin_dir, sizeof ctdl_bin_dir, "/usr/bin/");
00119 #else
00120        snprintf(ctdl_sbin_dir, sizeof ctdl_sbin_dir, ctdldir);
00121        snprintf(ctdl_bin_dir, sizeof ctdl_bin_dir, ctdldir);
00122 #endif
00123        StripSlashes(ctdl_sbin_dir, 1);
00124        StripSlashes(ctdl_bin_dir, 1);
00125 
00126 #ifndef HAVE_AUTO_ETC_DIR
00127        basedir=ctdldir;
00128 #else
00129        basedir=AUTO_ETC_DIR;
00130 #endif
00131        COMPUTE_DIRECTORY(ctdl_autoetc_dir);
00132        StripSlashes(ctdl_autoetc_dir, 1);
00133 
00134 #ifndef HAVE_ETC_DIR
00135        basedir=ctdldir;
00136 #else
00137        basedir=ETC_DIR;
00138 #endif
00139        COMPUTE_DIRECTORY(ctdl_netcfg_dir);
00140        COMPUTE_DIRECTORY(ctdl_etc_dir);
00141        StripSlashes(ctdl_netcfg_dir, 1);
00142        StripSlashes(ctdl_etc_dir, 1);
00143 
00144 #ifndef HAVE_UTILBIN_DIR
00145        basedir=ctdldir;
00146 #else
00147        basedir=UTILBIN_DIR;
00148 #endif
00149        COMPUTE_DIRECTORY(ctdl_utilbin_dir);
00150        StripSlashes(ctdl_utilbin_dir, 1);
00151 
00152 #ifndef HAVE_RUN_DIR
00153        basedir=ctdldir;
00154 #else
00155        basedir=RUN_DIR;
00156 #endif
00157        COMPUTE_DIRECTORY(ctdl_run_dir);
00158        StripSlashes(ctdl_run_dir, 1);
00159 
00160 #ifndef HAVE_STATICDATA_DIR
00161        basedir=ctdldir;
00162 #else
00163        basedir=STATICDATA_DIR;
00164 #endif
00165        COMPUTE_DIRECTORY(ctdl_message_dir);
00166        StripSlashes(ctdl_message_dir, 1);
00167 
00168 #ifndef HAVE_HELP_DIR
00169        basedir=ctdldir;
00170 #else
00171        basedir=HELP_DIR;
00172 #endif
00173        COMPUTE_DIRECTORY(ctdl_hlp_dir);
00174        StripSlashes(ctdl_hlp_dir, 1);
00175        COMPUTE_DIRECTORY(ctdl_shared_dir);
00176        StripSlashes(ctdl_shared_dir, 1);
00177 
00178 #ifndef HAVE_DATA_DIR
00179        basedir=ctdldir;
00180 #else
00181        basedir=DATA_DIR;
00182 #endif
00183        COMPUTE_DIRECTORY(ctdl_bio_dir);
00184        COMPUTE_DIRECTORY(ctdl_bb_dir);
00185        COMPUTE_DIRECTORY(ctdl_data_dir);
00186        COMPUTE_DIRECTORY(ctdl_dspam_dir);
00187        COMPUTE_DIRECTORY(ctdl_file_dir);
00188        COMPUTE_DIRECTORY(ctdl_image_dir);
00189        COMPUTE_DIRECTORY(ctdl_info_dir);
00190        COMPUTE_DIRECTORY(ctdl_usrpic_dir);
00191        COMPUTE_DIRECTORY(ctdl_bbsbase_dir);
00192 
00193        StripSlashes(ctdl_bio_dir, 1);
00194        StripSlashes(ctdl_bb_dir, 1);
00195        StripSlashes(ctdl_data_dir, 1);
00196        StripSlashes(ctdl_dspam_dir, 1);
00197        StripSlashes(ctdl_file_dir, 1);
00198        StripSlashes(ctdl_image_dir, 1);
00199        StripSlashes(ctdl_info_dir, 1);
00200        StripSlashes(ctdl_usrpic_dir, 1);
00201        StripSlashes(ctdl_bbsbase_dir, 1);
00202 
00203 #ifndef HAVE_SPOOL_DIR
00204        basedir=ctdldir;
00205 #else
00206        basedir=SPOOL_DIR;
00207 #endif
00208        COMPUTE_DIRECTORY(ctdl_spool_dir);
00209        COMPUTE_DIRECTORY(ctdl_netout_dir);
00210        COMPUTE_DIRECTORY(ctdl_netin_dir);
00211        COMPUTE_DIRECTORY(ctdl_nettmp_dir);
00212 
00213        StripSlashes(ctdl_spool_dir, 1);
00214        StripSlashes(ctdl_netout_dir, 1);
00215        StripSlashes(ctdl_netin_dir, 1);
00216        StripSlashes(ctdl_nettmp_dir, 1);
00217 
00218        /* ok, now we know the dirs, calc some commonly used files */
00219 
00220        snprintf(file_arcq, 
00221                       sizeof file_arcq,
00222                       "%srefcount_adjustments.dat",
00223                       ctdl_autoetc_dir);
00224        StripSlashes(file_arcq, 0);
00225        snprintf(file_citadel_control, 
00226                       sizeof file_citadel_control,
00227                       "%scitadel.control",
00228                       ctdl_autoetc_dir
00229                       );
00230        StripSlashes(file_citadel_control, 0);
00231        snprintf(file_citadel_config, 
00232                       sizeof file_citadel_config,
00233                       "%scitadel.config",
00234                       ctdl_autoetc_dir);
00235        StripSlashes(file_citadel_config, 0);
00236        snprintf(file_citadel_rc, 
00237                       sizeof file_citadel_rc,
00238                       "%scitadel.rc",
00239                       ctdl_etc_dir);
00240        StripSlashes(file_citadel_rc, 0);
00241        snprintf(file_citadel_urlshorteners, 
00242                       sizeof file_citadel_urlshorteners,
00243                       "%scitadel_urlshorteners.rc",
00244                       ctdl_etc_dir);
00245        StripSlashes(file_citadel_urlshorteners, 0);
00246        snprintf(file_lmtp_socket, 
00247                       sizeof file_lmtp_socket,
00248                       "%slmtp.socket",
00249                       ctdl_run_dir);
00250        StripSlashes(file_lmtp_socket, 0);
00251        snprintf(file_lmtp_unfiltered_socket, 
00252                       sizeof file_lmtp_socket,
00253                       "%slmtp-unfiltered.socket",
00254                       ctdl_run_dir);
00255        StripSlashes(file_lmtp_unfiltered_socket, 0);
00256        snprintf(file_citadel_socket, 
00257                       sizeof file_citadel_socket,
00258                             "%scitadel.socket",
00259                       ctdl_run_dir);
00260        StripSlashes(file_citadel_socket, 0);
00261        snprintf(file_citadel_admin_socket, 
00262                       sizeof file_citadel_admin_socket,
00263                             "%scitadel-admin.socket",
00264                       ctdl_run_dir);
00265        StripSlashes(file_citadel_admin_socket, 0);
00266        snprintf(file_pid_file, 
00267                sizeof file_pid_file,
00268                "%scitadel.pid",
00269                ctdl_run_dir);
00270        StripSlashes(file_pid_file, 0);
00271        snprintf(file_pid_paniclog, 
00272                sizeof file_pid_paniclog, 
00273                "%spanic.log",
00274                ctdl_home_directory);
00275        StripSlashes(file_pid_paniclog, 0);
00276        snprintf(file_crpt_file_key,
00277                sizeof file_crpt_file_key, 
00278                "%s/citadel.key",
00279                ctdl_key_dir);
00280        StripSlashes(file_crpt_file_key, 0);
00281        snprintf(file_crpt_file_csr,
00282                sizeof file_crpt_file_csr, 
00283                "%s/citadel.csr",
00284                ctdl_key_dir);
00285        StripSlashes(file_crpt_file_csr, 0);
00286        snprintf(file_crpt_file_cer,
00287                sizeof file_crpt_file_cer, 
00288                "%s/citadel.cer",
00289                ctdl_key_dir);
00290        StripSlashes(file_crpt_file_cer, 0);
00291        snprintf(file_chkpwd,
00292                sizeof file_chkpwd, 
00293                "%schkpwd",
00294                ctdl_utilbin_dir);
00295        StripSlashes(file_chkpwd, 0);
00296        snprintf(file_base64,
00297                sizeof file_base64,
00298                "%sbase64",
00299                ctdl_utilbin_dir);
00300        StripSlashes(file_base64, 0);
00301        snprintf(file_guesstimezone,
00302                sizeof file_guesstimezone,
00303                "%sguesstimezone.sh",
00304                ctdl_utilbin_dir);
00305 
00306        snprintf(file_dpsam_conf,
00307                sizeof file_dpsam_conf,
00308                "%sdspam.conf",
00309                ctdl_etc_dir);
00310        StripSlashes(file_dpsam_conf, 0);
00311        snprintf(file_dspam_log, 
00312                sizeof file_dspam_log, 
00313                "%sdspam.log",
00314                ctdl_home_directory);
00315        StripSlashes(file_dspam_log, 0);
00316        /* 
00317         * DIRTY HACK FOLLOWS! due to configs in the network dir in the 
00318         * legacy installations, we need to calculate ifdeffed here.
00319         */
00320        snprintf(file_mail_aliases, 
00321                sizeof file_mail_aliases,
00322                "%smail.aliases",
00323 #ifdef HAVE_ETC_DIR
00324                ctdl_etc_dir
00325 #else
00326                ctdl_spool_dir
00327 #endif
00328               );
00329        StripSlashes(file_mail_aliases, 0);
00330         snprintf(file_funambol_msg,
00331                 sizeof file_funambol_msg,
00332                 "%sfunambol_newmail_soap.xml",
00333                 ctdl_shared_dir);
00334        StripSlashes(file_funambol_msg, 0);
00335 
00336        DBG_PRINT(ctdl_bio_dir);
00337        DBG_PRINT(ctdl_bb_dir);
00338        DBG_PRINT(ctdl_data_dir);
00339        DBG_PRINT(ctdl_dspam_dir);
00340        DBG_PRINT(ctdl_file_dir);
00341        DBG_PRINT(ctdl_hlp_dir);
00342        DBG_PRINT(ctdl_image_dir);
00343        DBG_PRINT(ctdl_info_dir);
00344        DBG_PRINT(ctdl_key_dir);
00345        DBG_PRINT(ctdl_message_dir);
00346        DBG_PRINT(ctdl_usrpic_dir);
00347        DBG_PRINT(ctdl_etc_dir);
00348        DBG_PRINT(ctdl_run_dir);
00349        DBG_PRINT(ctdl_spool_dir);
00350        DBG_PRINT(ctdl_netout_dir);
00351        DBG_PRINT(ctdl_netin_dir);
00352        DBG_PRINT(ctdl_nettmp_dir);
00353        DBG_PRINT(ctdl_netcfg_dir);
00354        DBG_PRINT(ctdl_bbsbase_dir);
00355        DBG_PRINT(ctdl_sbin_dir);
00356        DBG_PRINT(ctdl_bin_dir);
00357        DBG_PRINT(ctdl_utilbin_dir);
00358        DBG_PRINT(file_citadel_control);
00359        DBG_PRINT(file_citadel_rc);
00360        DBG_PRINT(file_citadel_config);
00361        DBG_PRINT(file_lmtp_socket);
00362        DBG_PRINT(file_lmtp_unfiltered_socket);
00363        DBG_PRINT(file_arcq);
00364        DBG_PRINT(file_citadel_socket);
00365        DBG_PRINT(file_mail_aliases);
00366        DBG_PRINT(file_pid_file);
00367        DBG_PRINT(file_pid_paniclog);
00368        DBG_PRINT(file_crpt_file_key);
00369        DBG_PRINT(file_crpt_file_csr);
00370        DBG_PRINT(file_crpt_file_cer);
00371        DBG_PRINT(file_chkpwd);
00372        DBG_PRINT(file_base64);
00373        DBG_PRINT(file_guesstimezone);
00374        DBG_PRINT(file_funambol_msg);
00375 }
00376 
00377 
00378 /*
00379  * Generate an associated file name for a room
00380  */
00381 size_t assoc_file_name(char *buf, size_t n,
00382                    struct ctdlroom *qrbuf, const char *prefix)
00383 {
00384        return snprintf(buf, n, "%s%ld", prefix, qrbuf->QRnumber);
00385 }
00386 
00387 
00388 int create_dir(char *which, long ACCESS, long UID, long GID)
00389 {
00390        int rv;
00391        rv = mkdir(which, ACCESS);
00392        if ((rv == -1) && (errno == EEXIST))
00393               return rv;
00394        rv = chmod(which, ACCESS);
00395        if (rv == -1)
00396               return rv;
00397        rv = chown(which, UID, GID);
00398        return rv;
00399 }
00400 
00401 int create_run_directories(long UID, long GID)
00402 {
00403        int rv;
00404 
00405        rv = create_dir(ctdl_info_dir    , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00406        if (rv != -1)
00407               rv = create_dir(ctdl_bio_dir     , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00408        if (rv != -1)
00409               rv = create_dir(ctdl_usrpic_dir  , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00410        if (rv != -1)
00411               rv = create_dir(ctdl_message_dir , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00412        if (rv != -1)
00413               rv = create_dir(ctdl_hlp_dir     , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00414        if (rv != -1)
00415               rv = create_dir(ctdl_image_dir   , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00416        if (rv != -1)
00417               rv = create_dir(ctdl_bb_dir      , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00418        if (rv != -1)
00419               rv = create_dir(ctdl_file_dir    , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00420        if (rv != -1)
00421               rv = create_dir(ctdl_netcfg_dir  , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00422        if (rv != -1)
00423               rv = create_dir(ctdl_key_dir     , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
00424        if (rv != -1)
00425               rv = create_dir(ctdl_run_dir     , S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, UID, GID);
00426        return rv;
00427 }