svn_revision() now declared in svn_revision.h
[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 "svn_revision.h"
58 #include "citadel_dirs.c"
59
60 #include "modules_init.h"
61 #include "ecrash.h"
62
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
65 #endif
66
67 #ifndef HAVE_SNPRINTF
68 #include "snprintf.h"
69 #endif
70 const char *CitadelServiceUDS="citadel-UDS";
71 const char *CitadelServiceTCP="citadel-TCP";
72
73
74 void go_threading(void);
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], "-D", 2)) {
142                         dbg = 1;
143                 }
144
145                 /* -r tells the server not to drop root permissions. don't use
146                  * this unless you know what you're doing. this should be
147                  * removed in the next release if it proves unnecessary. */
148                 else if (!strcmp(argv[a], "-r"))
149                         drop_root_perms = 0;
150
151                 /* any other parameter makes it crash and burn */
152                 else {
153                         CtdlLogPrintf(CTDL_EMERG,       "citserver: usage: "
154                                         "citserver "
155                                         "[-lLogFacility] "
156                                         "[-d] [-D] "
157                                         " [-tTraceFile]"
158                                         " [-xLogLevel] [-hHomeDir]\n");
159                         exit(1);
160                 }
161
162         }
163
164         calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
165         /* daemonize, if we were asked to */
166         if (running_as_daemon) {
167                 start_daemon(0);
168                 drop_root_perms = 1;
169         }
170
171 #ifdef HAVE_BACKTRACE
172         bzero(&params, sizeof(params));
173         params.filename = file_pid_paniclog;
174         panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
175         params.filep = fopen(file_pid_paniclog, "a+");
176         params.debugLevel = ECRASH_DEBUG_VERBOSE;
177         params.dumpAllThreads = TRUE;
178         params.useBacktraceSymbols = 1;
179         params.signals[0]=SIGSEGV;
180         params.signals[1]=SIGILL;
181         params.signals[2]=SIGBUS;
182         params.signals[3]=SIGABRT;
183         eCrash_Init(&params);
184         eCrash_RegisterThread("MasterThread", 0);
185 #endif
186
187         /* Initialize the syslogger.  Yes, we are really using 0 as the
188          * facility, because we are going to bitwise-OR the facility to
189          * the severity of each message, allowing us to write to other
190          * facilities when we need to...
191          */
192         if (enable_syslog) {
193                 openlog("citadel", LOG_NDELAY, 0);
194                 setlogmask(LOG_UPTO(verbosity));
195         }
196         
197         /* Tell 'em who's in da house */
198         CtdlLogPrintf(CTDL_NOTICE, "\n");
199         CtdlLogPrintf(CTDL_NOTICE, "\n");
200         CtdlLogPrintf(CTDL_NOTICE,
201                 "*** Citadel server engine v%d.%02d (build %s) ***\n",
202                 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
203         CtdlLogPrintf(CTDL_NOTICE, "Copyright (C) 1987-2008 by the Citadel development team.\n");
204         CtdlLogPrintf(CTDL_NOTICE, "This program is distributed under the terms of the GNU "
205                                         "General Public License.\n");
206         CtdlLogPrintf(CTDL_NOTICE, "\n");
207         CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
208         CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
209
210         /* Load site-specific parameters, and set the ipgm secret */
211         CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
212         get_config();
213         config.c_ipgm_secret = rand();
214         put_config();
215
216 #ifdef HAVE_RUN_DIR
217         /* on some dists rundir gets purged on startup. so we need to recreate it. */
218
219         if (stat(ctdl_run_dir, &filestats)==-1){
220 #ifdef HAVE_GETPWUID_R
221 #ifdef SOLARIS_GETPWUID
222                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
223 #else // SOLARIS_GETPWUID
224                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
225 #endif // SOLARIS_GETPWUID
226 #else // HAVE_GETPWUID_R
227                 pwp = NULL;
228 #endif // HAVE_GETPWUID_R
229                 mkdir(ctdl_run_dir, 0755);
230                 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
231         }
232                         
233
234 #endif
235
236         /* Initialize... */
237         init_sysdep();
238
239         /*
240          * Do non system dependent startup functions.
241          */
242         master_startup();
243
244         CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
245         get_control();
246
247         /*
248          * Bind the server to a Unix-domain socket.
249          */
250         CtdlRegisterServiceHook(0,
251                                 file_citadel_socket,
252                                 citproto_begin_session,
253                                 do_command_loop,
254                                 do_async_loop,
255                                 CitadelServiceUDS);
256
257         /*
258          * Bind the server to our favorite TCP port (usually 504).
259          */
260         CtdlRegisterServiceHook(config.c_port_number,
261                                 NULL,
262                                 citproto_begin_session,
263                                 do_command_loop,
264                                 do_async_loop,
265                                 CitadelServiceTCP);
266
267                                 
268         /*
269          * Run any upgrade entry points
270          */
271         CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
272         upgrade_modules();
273         
274         
275         /*
276          * Load any server-side extensions available here.
277          */
278         CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
279         size = strlen(ctdl_home_directory) + 9;
280         
281         initialise_modules(0);
282         
283         
284
285         /*
286          * If we need host auth, start our chkpwd daemon.
287          */
288         if (config.c_auth_mode == AUTHMODE_HOST) {
289                 start_chkpwd_daemon();
290         }
291
292         /*
293          * Now that we've bound the sockets, change to the Citadel user id and its
294          * corresponding group ids
295          */
296         if (drop_root_perms) {
297                 cdb_chmod_data();       /* make sure we own our data files */
298
299 #ifdef HAVE_GETPWUID_R
300 #ifdef SOLARIS_GETPWUID
301                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
302 #else // SOLARIS_GETPWUID
303                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
304 #endif // SOLARIS_GETPWUID
305 #else // HAVE_GETPWUID_R
306                 pwp = NULL;
307 #endif // HAVE_GETPWUID_R
308
309                 if (pwp == NULL)
310                         CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
311                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
312                                 strerror(errno));
313                 else {
314                         initgroups(pw.pw_name, pw.pw_gid);
315                         if (setgid(pw.pw_gid))
316                                 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
317                                         strerror(errno));
318                 }
319                 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
320                 if (setuid(CTDLUID) != 0) {
321                         CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
322                 }
323 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
324                 prctl(PR_SET_DUMPABLE, 1);
325 #endif
326         }
327
328         /* We want to check for idle sessions once per minute */
329         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
330
331         go_threading();
332         
333         
334         master_cleanup(exit_signal);
335         return(0);
336 }