+ beautification: remove double slashes from filenames, they tend to irretate users.
[citadel.git] / citadel / citadel_dirs.c
1 #include "sysdep.h"
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <sys/stat.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <limits.h>
9
10
11 #if TIME_WITH_SYS_TIME
12 # include <sys/time.h>
13 # include <time.h>
14 #else
15 # if HAVE_SYS_TIME_H
16 #  include <sys/time.h>
17 # else
18 #  include <time.h>
19 # endif
20 #endif
21
22 #include <errno.h>
23 #include <libcitadel.h>
24
25
26 #include "citadel.h"
27
28 /* our directories... */
29 char ctdl_home_directory[PATH_MAX] = "";
30 char ctdl_bio_dir[PATH_MAX]="bio";
31 char ctdl_bb_dir[PATH_MAX]="bitbucket";
32 char ctdl_data_dir[PATH_MAX]="data";
33 char ctdl_dspam_dir[PATH_MAX]="dspam";
34 char ctdl_file_dir[PATH_MAX]="files";
35 char ctdl_hlp_dir[PATH_MAX]="help";
36 char ctdl_image_dir[PATH_MAX]="images";
37 char ctdl_info_dir[PATH_MAX]="info";
38 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
39 char ctdl_message_dir[PATH_MAX]="messages";
40 char ctdl_usrpic_dir[PATH_MAX]="userpics";
41 char ctdl_bbsbase_dir[PATH_MAX]="";
42 char ctdl_etc_dir[PATH_MAX]="";
43 /* attention! this may be non volatile on some oses */
44 char ctdl_run_dir[PATH_MAX]="";
45 char ctdl_spool_dir[PATH_MAX]="network";
46 char ctdl_netout_dir[PATH_MAX]="network/spoolout";
47 char ctdl_netin_dir[PATH_MAX]="network/spoolin";
48 char ctdl_netcfg_dir[PATH_MAX]="netconfigs";
49 char ctdl_utilbin_dir[PATH_MAX]="";
50 char ctdl_sbin_dir[PATH_MAX]="";
51 char ctdl_bin_dir[PATH_MAX]="";
52
53 /* some of our files, that are needed in several places */
54 char file_citadel_control[PATH_MAX]="";
55 char file_citadel_rc[PATH_MAX]="";
56 char file_citadel_config[PATH_MAX]="";
57 char file_lmtp_socket[PATH_MAX]="";
58 char file_lmtp_unfiltered_socket[PATH_MAX]="";
59 char file_arcq[PATH_MAX]="";
60 char file_citadel_socket[PATH_MAX]="";
61 char file_mail_aliases[PATH_MAX]="";
62 char file_pid_file[PATH_MAX]="";
63 char file_pid_paniclog[PATH_MAX]="";
64 char file_crpt_file_key[PATH_MAX]="";
65 char file_crpt_file_csr[PATH_MAX]="";
66 char file_crpt_file_cer[PATH_MAX]="";
67 char file_chkpwd[PATH_MAX]="";
68 char file_base64[PATH_MAX]="";
69 char file_guesstimezone[PATH_MAX]="";
70 char file_funambol_msg[PATH_MAX] = "";
71 char file_dpsam_conf[PATH_MAX] = "";
72 char file_dspam_log[PATH_MAX] = "";
73
74
75
76
77 void StripSlashes(char *Dir, int TrailingSlash)
78 {
79         char *a, *b;
80
81         a = b = Dir;
82
83         while (!IsEmptyStr(a)) {
84                 if (*a == '/') {
85                         while (*a == '/')
86                                 a++;
87                         *b = '/';
88                         b++;
89                 }
90                 else {
91                         *b = *a;
92                         b++; a++;
93                 }
94         }
95         if ((TrailingSlash) && (*(b - 1) != '/')){
96                 *b = '/';
97                 b++;
98         }
99         *b = '\0';
100
101 }
102
103
104 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
105         snprintf(SUBDIR,sizeof SUBDIR,  "%s%s%s%s%s%s%s", \
106                          (home&!relh)?ctdl_home_directory:basedir, \
107              ((basedir!=ctdldir)&(home&!relh))?basedir:"/", \
108              ((basedir!=ctdldir)&(home&!relh))?"/":"", \
109                          relhome, \
110              (relhome[0]!='\0')?"/":"",\
111                          dirbuffer,\
112                          (dirbuffer[0]!='\0')?"/":"");
113
114 #define DBG_PRINT(A) if (dbg==1) fprintf (stderr,"%s : %s \n", #A, A)
115
116
117 void calc_dirs_n_files(int relh, int home, const char *relhome, char  *ctdldir, int dbg)
118 {
119         const char* basedir = "";
120         char dirbuffer[PATH_MAX] = "";
121
122         /*
123          * Ok, we keep our binaries either in the citadel base dir,
124          * or in /usr/sbin / /usr/bin
125          */
126         StripSlashes(ctdldir, 1);
127 #ifdef HAVE_ETC_DIR
128         snprintf(ctdl_sbin_dir, sizeof ctdl_sbin_dir, "/usr/sbin/");
129         snprintf(ctdl_bin_dir, sizeof ctdl_bin_dir, "/usr/bin/");
130 #else
131         snprintf(ctdl_sbin_dir, sizeof ctdl_sbin_dir, ctdldir);
132         snprintf(ctdl_bin_dir, sizeof ctdl_bin_dir, ctdldir);
133 #endif
134         StripSlashes(ctdl_sbin_dir, 1);
135         StripSlashes(ctdl_bin_dir, 1);
136 #ifndef HAVE_ETC_DIR
137         basedir=ctdldir;
138 #else
139         basedir=ETC_DIR;
140 #endif
141         COMPUTE_DIRECTORY(ctdl_netcfg_dir);
142         COMPUTE_DIRECTORY(ctdl_etc_dir);
143         StripSlashes(ctdl_netcfg_dir, 1);
144         StripSlashes(ctdl_etc_dir, 1);
145
146 #ifndef HAVE_UTILBIN_DIR
147         basedir=ctdldir;
148 #else
149         basedir=UTILBIN_DIR;
150 #endif
151         COMPUTE_DIRECTORY(ctdl_utilbin_dir);
152         StripSlashes(ctdl_utilbin_dir, 1);
153
154 #ifndef HAVE_RUN_DIR
155         basedir=ctdldir;
156 #else
157         basedir=RUN_DIR;
158 #endif
159         COMPUTE_DIRECTORY(ctdl_run_dir);
160         StripSlashes(ctdl_run_dir, 1);
161
162 #ifndef HAVE_STATICDATA_DIR
163         basedir=ctdldir;
164 #else
165         basedir=STATICDATA_DIR;
166 #endif
167         COMPUTE_DIRECTORY(ctdl_message_dir);
168         StripSlashes(ctdl_message_dir, 1);
169
170 #ifndef HAVE_HELP_DIR
171         basedir=ctdldir;
172 #else
173         basedir=HELP_DIR;
174 #endif
175         COMPUTE_DIRECTORY(ctdl_hlp_dir);
176         StripSlashes(ctdl_hlp_dir, 1);
177
178 #ifndef HAVE_DATA_DIR
179         basedir=ctdldir;
180 #else
181         basedir=DATA_DIR;
182 #endif
183         COMPUTE_DIRECTORY(ctdl_bio_dir);
184         COMPUTE_DIRECTORY(ctdl_bb_dir);
185         COMPUTE_DIRECTORY(ctdl_data_dir);
186         COMPUTE_DIRECTORY(ctdl_dspam_dir);
187         COMPUTE_DIRECTORY(ctdl_file_dir);
188         COMPUTE_DIRECTORY(ctdl_image_dir);
189         COMPUTE_DIRECTORY(ctdl_info_dir);
190         COMPUTE_DIRECTORY(ctdl_usrpic_dir);
191         COMPUTE_DIRECTORY(ctdl_bbsbase_dir);
192
193         StripSlashes(ctdl_bio_dir, 1);
194         StripSlashes(ctdl_bb_dir, 1);
195         StripSlashes(ctdl_data_dir, 1);
196         StripSlashes(ctdl_dspam_dir, 1);
197         StripSlashes(ctdl_file_dir, 1);
198         StripSlashes(ctdl_image_dir, 1);
199         StripSlashes(ctdl_info_dir, 1);
200         StripSlashes(ctdl_usrpic_dir, 1);
201         StripSlashes(ctdl_bbsbase_dir, 1);
202
203 #ifndef HAVE_SPOOL_DIR
204         basedir=ctdldir;
205 #else
206         basedir=SPOOL_DIR;
207 #endif
208         COMPUTE_DIRECTORY(ctdl_spool_dir);
209         COMPUTE_DIRECTORY(ctdl_netout_dir);
210         COMPUTE_DIRECTORY(ctdl_netin_dir);
211
212         StripSlashes(ctdl_spool_dir, 1);
213         StripSlashes(ctdl_netout_dir, 1);
214         StripSlashes(ctdl_netin_dir, 1);
215
216         /* ok, now we know the dirs, calc some commonly used files */
217
218         snprintf(file_arcq, 
219                          sizeof file_arcq,
220                          "%srefcount_adjustments.dat",
221                          ctdl_etc_dir);
222         StripSlashes(file_arcq, 0);
223         snprintf(file_citadel_control, 
224                          sizeof file_citadel_control,
225                          "%scitadel.control",
226                          ctdl_etc_dir
227                          );
228         StripSlashes(file_citadel_control, 0);
229         snprintf(file_citadel_config, 
230                          sizeof file_citadel_config,
231                          "%scitadel.config",
232                          ctdl_etc_dir);
233         StripSlashes(file_citadel_config, 0);
234         snprintf(file_citadel_rc, 
235                          sizeof file_citadel_rc,
236                          "%scitadel.rc",
237                          ctdl_etc_dir);
238         StripSlashes(file_citadel_rc, 0);
239         snprintf(file_lmtp_socket, 
240                          sizeof file_lmtp_socket,
241                          "%slmtp.socket",
242                          ctdl_run_dir);
243         StripSlashes(file_lmtp_socket, 0);
244         snprintf(file_lmtp_unfiltered_socket, 
245                          sizeof file_lmtp_socket,
246                          "%slmtp-unfiltered.socket",
247                          ctdl_run_dir);
248         StripSlashes(file_lmtp_unfiltered_socket, 0);
249         snprintf(file_citadel_socket, 
250                          sizeof file_citadel_socket,
251                                 "%scitadel.socket",
252                          ctdl_run_dir);
253         StripSlashes(file_citadel_socket, 0);
254         snprintf(file_pid_file, 
255                  sizeof file_pid_file,
256                  "%scitadel.pid",
257                  ctdl_run_dir);
258         StripSlashes(file_pid_file, 0);
259         snprintf(file_pid_paniclog, 
260                  sizeof file_pid_paniclog, 
261                  "%spanic.log",
262                  ctdl_home_directory);
263         StripSlashes(file_pid_paniclog, 0);
264         snprintf(file_crpt_file_key,
265                  sizeof file_crpt_file_key, 
266                  "%s/citadel.key",
267                  ctdl_key_dir);
268         StripSlashes(file_crpt_file_key, 0);
269         snprintf(file_crpt_file_csr,
270                  sizeof file_crpt_file_csr, 
271                  "%s/citadel.csr",
272                  ctdl_key_dir);
273         StripSlashes(file_crpt_file_csr, 0);
274         snprintf(file_crpt_file_cer,
275                  sizeof file_crpt_file_cer, 
276                  "%s/citadel.cer",
277                  ctdl_key_dir);
278         StripSlashes(file_crpt_file_cer, 0);
279         snprintf(file_chkpwd,
280                  sizeof file_chkpwd, 
281                  "%schkpwd",
282                  ctdl_utilbin_dir);
283         StripSlashes(file_chkpwd, 0);
284         snprintf(file_base64,
285                  sizeof file_base64,
286                  "%sbase64",
287                  ctdl_utilbin_dir);
288         StripSlashes(file_base64, 0);
289         snprintf(file_guesstimezone,
290                  sizeof file_guesstimezone,
291                  "%sguesstimezone.sh",
292                  ctdl_utilbin_dir);
293
294         snprintf(file_dpsam_conf,
295                  sizeof file_dpsam_conf,
296                  "%sdspam.conf",
297                  ctdl_etc_dir);
298         StripSlashes(file_dpsam_conf, 0);
299         snprintf(file_dspam_log, 
300                  sizeof file_dspam_log, 
301                  "%sdspam.log",
302                  ctdl_home_directory);
303         StripSlashes(file_dspam_log, 0);
304         /* 
305          * DIRTY HACK FOLLOWS! due to configs in the network dir in the 
306          * legacy installations, we need to calculate ifdeffed here.
307          */
308         snprintf(file_mail_aliases, 
309                  sizeof file_mail_aliases,
310                  "%smail.aliases",
311 #ifdef HAVE_ETC_DIR
312                  ctdl_etc_dir
313 #else
314                  ctdl_spool_dir
315 #endif
316                 );
317         StripSlashes(file_mail_aliases, 0);
318         snprintf(file_funambol_msg,
319                 sizeof file_funambol_msg,
320                 "%sfunambol_newmail_soap.xml",
321                 ctdl_spool_dir);
322         StripSlashes(file_funambol_msg, 0);
323
324         DBG_PRINT(ctdl_bio_dir);
325         DBG_PRINT(ctdl_bb_dir);
326         DBG_PRINT(ctdl_data_dir);
327         DBG_PRINT(ctdl_dspam_dir);
328         DBG_PRINT(ctdl_file_dir);
329         DBG_PRINT(ctdl_hlp_dir);
330         DBG_PRINT(ctdl_image_dir);
331         DBG_PRINT(ctdl_info_dir);
332         DBG_PRINT(ctdl_key_dir);
333         DBG_PRINT(ctdl_message_dir);
334         DBG_PRINT(ctdl_usrpic_dir);
335         DBG_PRINT(ctdl_etc_dir);
336         DBG_PRINT(ctdl_run_dir);
337         DBG_PRINT(ctdl_spool_dir);
338         DBG_PRINT(ctdl_netout_dir);
339         DBG_PRINT(ctdl_netin_dir);
340         DBG_PRINT(ctdl_netcfg_dir);
341         DBG_PRINT(ctdl_bbsbase_dir);
342         DBG_PRINT(ctdl_sbin_dir);
343         DBG_PRINT(ctdl_bin_dir);
344         DBG_PRINT(ctdl_utilbin_dir);
345         DBG_PRINT(file_citadel_control);
346         DBG_PRINT(file_citadel_rc);
347         DBG_PRINT(file_citadel_config);
348         DBG_PRINT(file_lmtp_socket);
349         DBG_PRINT(file_lmtp_unfiltered_socket);
350         DBG_PRINT(file_arcq);
351         DBG_PRINT(file_citadel_socket);
352         DBG_PRINT(file_mail_aliases);
353         DBG_PRINT(file_pid_file);
354         DBG_PRINT(file_pid_paniclog);
355         DBG_PRINT(file_crpt_file_key);
356         DBG_PRINT(file_crpt_file_csr);
357         DBG_PRINT(file_crpt_file_cer);
358         DBG_PRINT(file_chkpwd);
359         DBG_PRINT(file_base64);
360         DBG_PRINT(file_guesstimezone);
361         DBG_PRINT(file_funambol_msg);
362 }
363
364
365 /*
366  * Generate an associated file name for a room
367  */
368 void assoc_file_name(char *buf, size_t n,
369                      struct ctdlroom *qrbuf, const char *prefix)
370 {
371         snprintf(buf, n, "%s%ld", prefix, qrbuf->QRnumber);
372 }
373