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