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