The encoding and decoding tables for Base64 are
[citadel.git] / citadel / server_main.c
1 /*
2  * citserver's main() function lives here.
3  *
4  * $Id$
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <syslog.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <limits.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #include <sys/un.h>
35 #include <string.h>
36 #include <pwd.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #ifdef HAVE_SYS_PRCTL_H
42 #include <sys/prctl.h>
43 #endif
44 #include <libcitadel.h>
45 #include "citadel.h"
46 #include "server.h"
47 #include "serv_extensions.h"
48 #include "sysdep_decls.h"
49 #include "threads.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "control.h"
54 #include "database.h"
55 #include "user_ops.h"
56 #include "housekeeping.h"
57 #include "citadel_dirs.c"
58
59 #include "modules_init.h"
60 #include "ecrash.h"
61
62 #ifdef HAVE_SYS_SELECT_H
63 #include <sys/select.h>
64 #endif
65
66 #ifndef HAVE_SNPRINTF
67 #include "snprintf.h"
68 #endif
69 const char *CitadelServiceUDS="citadel-UDS";
70 const char *CitadelServiceTCP="citadel-TCP";
71
72
73 void go_threading(void);
74
75
76 /*
77  * Here's where it all begins.
78  */
79 int main(int argc, char **argv)
80 {
81         char facility[32];
82         int a;                  /* General-purpose variables */
83         struct passwd pw, *pwp = NULL;
84         char pwbuf[SIZ];
85         int drop_root_perms = 1;
86         size_t size;
87         int relh=0;
88         int home=0;
89         int dbg=0;
90         char relhome[PATH_MAX]="";
91         char ctdldir[PATH_MAX]=CTDLDIR;
92 #ifdef HAVE_RUN_DIR
93         struct stat filestats;
94 #endif
95 #ifdef HAVE_BACKTRACE
96         eCrashParameters params;
97 //      eCrashSymbolTable symbol_table;
98 #endif
99         /* initialise semaphores here. Patch by Matt and davew
100          * its called here as they are needed by CtdlLogPrintf for thread safety
101          */
102         InitialiseSemaphores();
103         
104         /* initialize the master context */
105         InitializeMasterCC();
106
107         /* parse command-line arguments */
108         for (a=1; a<argc; ++a) {
109
110                 if (!strncmp(argv[a], "-l", 2)) {
111                         safestrncpy(facility, &argv[a][2], sizeof(facility));
112                         syslog_facility = SyslogFacility(facility);
113                         enable_syslog = 1;
114                 }
115
116                 /* run in the background if -d was specified */
117                 else if (!strcmp(argv[a], "-d")) {
118                         running_as_daemon = 1;
119                 }
120
121                 /* -x specifies the desired logging level */
122                 else if (!strncmp(argv[a], "-x", 2)) {
123                         verbosity = atoi(&argv[a][2]);
124                 }
125
126                 else if (!strncmp(argv[a], "-h", 2)) {
127                         relh=argv[a][2]!='/';
128                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
129                                                                    sizeof ctdl_home_directory);
130                         else
131                                 safestrncpy(relhome, &argv[a][2],
132                                                         sizeof relhome);
133                         home_specified = 1;
134                         home=1;
135                 }
136
137                 else if (!strncmp(argv[a], "-t", 2)) {
138                         freopen(&argv[a][2], "w", stderr);
139                 }
140
141                 else if (!strncmp(argv[a], "-f", 2)) {
142                         do_defrag = 1;
143                 }
144
145                 else if (!strncmp(argv[a], "-D", 2)) {
146                         dbg = 1;
147                 }
148
149                 /* -r tells the server not to drop root permissions. don't use
150                  * this unless you know what you're doing. this should be
151                  * removed in the next release if it proves unnecessary. */
152                 else if (!strcmp(argv[a], "-r"))
153                         drop_root_perms = 0;
154
155                 /* any other parameter makes it crash and burn */
156                 else {
157                         CtdlLogPrintf(CTDL_EMERG,       "citserver: usage: "
158                                         "citserver "
159                                         "[-lLogFacility] "
160                                         "[-d] [-f] [-D] "
161                                         " [-tTraceFile]"
162                                         " [-xLogLevel] [-hHomeDir]\n");
163                         exit(1);
164                 }
165
166         }
167
168         calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
169         /* daemonize, if we were asked to */
170         if (running_as_daemon) {
171                 start_daemon(0);
172                 drop_root_perms = 1;
173         }
174
175 #ifdef HAVE_BACKTRACE
176         bzero(&params, sizeof(params));
177         params.filename = file_pid_paniclog;
178         panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
179         params.filep = fopen(file_pid_paniclog, "a+");
180         params.debugLevel = ECRASH_DEBUG_VERBOSE;
181         params.dumpAllThreads = TRUE;
182         params.useBacktraceSymbols = 1;
183 ///     BuildSymbolTable(&symbol_table);
184 //      params.symbolTable = &symbol_table;
185         params.signals[0]=SIGSEGV;
186         params.signals[1]=SIGILL;
187         params.signals[2]=SIGBUS;
188         params.signals[3]=SIGABRT;
189
190         eCrash_Init(&params);
191                 
192         eCrash_RegisterThread("MasterThread", 0);
193
194 ///     signal(SIGSEGV, cit_panic_backtrace);
195 #endif
196         /* Initialize the syslogger.  Yes, we are really using 0 as the
197          * facility, because we are going to bitwise-OR the facility to
198          * the severity of each message, allowing us to write to other
199          * facilities when we need to...
200          */
201         if (enable_syslog) {
202                 openlog("citadel", LOG_NDELAY, 0);
203                 setlogmask(LOG_UPTO(verbosity));
204         }
205         
206         /* Tell 'em who's in da house */
207         CtdlLogPrintf(CTDL_NOTICE, "\n");
208         CtdlLogPrintf(CTDL_NOTICE, "\n");
209         CtdlLogPrintf(CTDL_NOTICE,
210                 "*** Citadel server engine v%d.%02d ***\n",
211                 (REV_LEVEL/100), (REV_LEVEL%100));
212         CtdlLogPrintf(CTDL_NOTICE,
213                 "Copyright (C) 1987-2008 by the Citadel development team.\n");
214         CtdlLogPrintf(CTDL_NOTICE,
215                 "This program is distributed under the terms of the GNU "
216                 "General Public License.\n");
217         CtdlLogPrintf(CTDL_NOTICE, "\n");
218         CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
219         CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
220
221         /* Load site-specific parameters, and set the ipgm secret */
222         CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
223         get_config();
224         config.c_ipgm_secret = rand();
225         put_config();
226
227 #ifdef HAVE_RUN_DIR
228         /* on some dists rundir gets purged on startup. so we need to recreate it. */
229
230         if (stat(ctdl_run_dir, &filestats)==-1){
231 #ifdef HAVE_GETPWUID_R
232 #ifdef SOLARIS_GETPWUID
233                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
234 #else // SOLARIS_GETPWUID
235                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
236 #endif // SOLARIS_GETPWUID
237 #else // HAVE_GETPWUID_R
238                 pwp = NULL;
239 #endif // HAVE_GETPWUID_R
240                 mkdir(ctdl_run_dir, 0755);
241                 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
242         }
243                         
244
245 #endif
246
247         /* Initialize... */
248         init_sysdep();
249
250         /*
251          * Do non system dependent startup functions.
252          */
253         master_startup();
254
255         CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
256         get_control();
257
258         /*
259          * Bind the server to a Unix-domain socket.
260          */
261         CtdlRegisterServiceHook(0,
262                                 file_citadel_socket,
263                                 citproto_begin_session,
264                                 do_command_loop,
265                                 do_async_loop,
266                                 CitadelServiceUDS);
267
268         /*
269          * Bind the server to our favorite TCP port (usually 504).
270          */
271         CtdlRegisterServiceHook(config.c_port_number,
272                                 NULL,
273                                 citproto_begin_session,
274                                 do_command_loop,
275                                 do_async_loop,
276                                 CitadelServiceTCP);
277
278                                 
279         /*
280          * Run any upgrade entry points
281          */
282         CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
283         upgrade_modules();
284         
285         
286         /*
287          * Load any server-side extensions available here.
288          */
289         CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
290         size = strlen(ctdl_home_directory) + 9;
291         
292         initialise_modules(0);
293         
294         
295
296         /*
297          * If we need host auth, start our chkpwd daemon.
298          */
299         if (config.c_auth_mode == AUTHMODE_HOST) {
300                 start_chkpwd_daemon();
301         }
302
303         /*
304          * Now that we've bound the sockets, change to the Citadel user id and its
305          * corresponding group ids
306          */
307         if (drop_root_perms) {
308                 cdb_chmod_data();       /* make sure we own our data files */
309
310 #ifdef HAVE_GETPWUID_R
311 #ifdef SOLARIS_GETPWUID
312                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
313 #else // SOLARIS_GETPWUID
314                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
315 #endif // SOLARIS_GETPWUID
316 #else // HAVE_GETPWUID_R
317                 pwp = NULL;
318 #endif // HAVE_GETPWUID_R
319
320                 if (pwp == NULL)
321                         CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
322                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
323                                 strerror(errno));
324                 else {
325                         initgroups(pw.pw_name, pw.pw_gid);
326                         if (setgid(pw.pw_gid))
327                                 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
328                                         strerror(errno));
329                 }
330                 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
331                 if (setuid(CTDLUID) != 0) {
332                         CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
333                 }
334 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
335                 prctl(PR_SET_DUMPABLE, 1);
336 #endif
337         }
338
339         /* We want to check for idle sessions once per minute */
340         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
341
342         go_threading();
343         
344         
345         master_cleanup(exit_signal);
346         return(0);
347 }