7c6a46434e8a51e7669f9029eff6d8a9dc701920
[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         size_t basesize = 64;
93         char facility[32];
94         int a;                  /* General-purpose variables */
95         struct passwd pw, *pwp = NULL;
96         char pwbuf[SIZ];
97         int drop_root_perms = 1;
98         int relh=0;
99         int home=0;
100         int dbg=0;
101         char relhome[PATH_MAX]="";
102         char ctdldir[PATH_MAX]=CTDLDIR;
103         int syslog_facility = LOG_DAEMON;
104         const char *eDebuglist[] = {NULL, NULL};
105 #ifdef HAVE_RUN_DIR
106         struct stat filestats;
107 #endif
108 #ifdef HAVE_BACKTRACE
109         eCrashParameters params;
110 //      eCrashSymbolTable symbol_table;
111 #endif
112
113         /* initialize the master context */
114         InitializeMasterCC();
115         InitializeMasterTSD();
116
117         /* parse command-line arguments */
118         while ((a=getopt(argc, argv, "l:dh:x:t:B:Dr")) != EOF) switch(a) {
119
120                 case 'l':
121                         safestrncpy(facility, optarg, sizeof(facility));
122                         syslog_facility = SyslogFacility(facility);
123                         break;
124
125                 /* run in the background if -d was specified */
126                 case 'd':
127                         running_as_daemon = 1;
128                         break;
129
130                 case 'h':
131                         relh = optarg[0] != '/';
132                         if (!relh) {
133                                 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
134                         }
135                         else {
136                                 safestrncpy(relhome, optarg, sizeof relhome);
137                         }
138                         home=1;
139                         break;
140
141                 case 'x':
142                         eDebuglist [0] = optarg;
143                         break;
144
145                 case 't':       /* deprecated */
146                         break;
147                 case 'B': /* Basesize */
148                         basesize = atoi(optarg);
149                         break;
150
151                 case 'D':
152                         dbg = 1;
153                         break;
154
155                 /* -r tells the server not to drop root permissions.
156                  * Don't use this unless you know what you're doing.
157                  */
158                 case 'r':
159                         drop_root_perms = 0;
160                         break;
161
162                 default:
163                 /* any other parameter makes it crash and burn */
164                         fprintf(stderr, "citserver: usage: "
165                                         "citserver "
166                                         "[-l LogFacility] "
167                                         "[-d] [-D] [-r] "
168                                         "[-h HomeDir]\n"
169                         );
170                         exit(1);
171         }
172         StartLibCitadel(basesize);
173         openlog("citserver",
174                 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
175                 syslog_facility
176         );
177
178         calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
179         /* daemonize, if we were asked to */
180         if (running_as_daemon) {
181                 start_daemon(0);
182                 drop_root_perms = 1;
183         }
184
185 #ifdef HAVE_BACKTRACE
186         bzero(&params, sizeof(params));
187         params.filename = file_pid_paniclog;
188         panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
189         params.filep = fopen(file_pid_paniclog, "a+");
190         params.debugLevel = ECRASH_DEBUG_VERBOSE;
191         params.dumpAllThreads = TRUE;
192         params.useBacktraceSymbols = 1;
193         params.signals[0]=SIGSEGV;
194         params.signals[1]=SIGILL;
195         params.signals[2]=SIGBUS;
196         params.signals[3]=SIGABRT;
197         eCrash_Init(&params);
198         eCrash_RegisterThread("MasterThread", 0);
199 #endif
200
201         /* Tell 'em who's in da house */
202         syslog(LOG_NOTICE, " ");
203         syslog(LOG_NOTICE, " ");
204         syslog(LOG_NOTICE,
205                 "*** Citadel server engine v%d.%02d (build %s) ***",
206                 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
207         syslog(LOG_NOTICE, "Copyright (C) 1987-2012 by the Citadel development team.");
208         syslog(LOG_NOTICE, "This program is distributed under the terms of the GNU "
209                                         "General Public License.");
210         syslog(LOG_NOTICE, " ");
211         syslog(LOG_DEBUG, "Called as: %s", argv[0]);
212         syslog(LOG_INFO, "%s", libcitadel_version_string());
213
214         /* Load site-specific configuration */
215         syslog(LOG_INFO, "Loading citadel.config");
216         get_config();
217
218         /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
219         syslog(LOG_INFO, "Acquiring control record");
220         get_control();
221
222         put_config();
223
224 #ifdef HAVE_RUN_DIR
225         /* on some dists rundir gets purged on startup. so we need to recreate it. */
226
227         if (stat(ctdl_run_dir, &filestats)==-1){
228 #ifdef HAVE_GETPWUID_R
229 #ifdef SOLARIS_GETPWUID
230                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
231 #else // SOLARIS_GETPWUID
232                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
233 #endif // SOLARIS_GETPWUID
234 #else // HAVE_GETPWUID_R
235                 pwp = NULL;
236 #endif // HAVE_GETPWUID_R
237
238                 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST))
239                         syslog(LOG_EMERG, 
240                                       "unable to create run directory [%s]: %s", 
241                                       ctdl_run_dir, strerror(errno));
242
243                 if (chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0)
244                         syslog(LOG_EMERG, 
245                                       "unable to set the access rights for [%s]: %s", 
246                                       ctdl_run_dir, strerror(errno));
247         }
248                         
249
250 #endif
251
252         /* Initialize... */
253         init_sysdep();
254
255         /*
256          * Do non system dependent startup functions.
257          */
258         master_startup();
259
260         /*
261          * Check that the control record is correct and place sensible values if it isn't
262          */
263         check_control();
264         
265         /*
266          * Run any upgrade entry points
267          */
268         syslog(LOG_INFO, "Upgrading modules.");
269         upgrade_modules();
270         
271 /*
272  * Load the user for the masterCC or create them if they don't exist
273  */
274         if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
275         {
276                 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
277                 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
278                 CtdlPutUser(&masterCC.user);
279                 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
280         }
281         
282         /*
283          * Bind the server to a Unix-domain socket (user client access)
284          */
285         CtdlRegisterServiceHook(0,
286                                 file_citadel_socket,
287                                 citproto_begin_session,
288                                 do_command_loop,
289                                 do_async_loop,
290                                 CitadelServiceUDS);
291
292         /*
293          * Bind the server to a Unix-domain socket (admin client access)
294          */
295         CtdlRegisterServiceHook(0,
296                                 file_citadel_admin_socket,
297                                 citproto_begin_admin_session,
298                                 do_command_loop,
299                                 do_async_loop,
300                                 CitadelServiceUDS);
301         chmod(file_citadel_admin_socket, S_IRWXU);      /* for your eyes only */
302
303         /*
304          * Bind the server to our favorite TCP port (usually 504).
305          */
306         CtdlRegisterServiceHook(config.c_port_number,
307                                 NULL,
308                                 citproto_begin_session,
309                                 do_command_loop,
310                                 do_async_loop,
311                                 CitadelServiceTCP);
312
313                                 
314         
315         
316         /*
317          * Load any server-side extensions available here.
318          */
319         syslog(LOG_INFO, "Initializing server extensions");
320         
321         initialise_modules(0);
322
323         eDebuglist[1] = getenv("CITADEL_LOGDEBUG");
324         CtdlSetDebugLogFacilities(eDebuglist, 2);
325
326         /*
327          * If we need host auth, start our chkpwd daemon.
328          */
329         if (config.c_auth_mode == AUTHMODE_HOST) {
330                 start_chkpwd_daemon();
331         }
332
333
334         /*
335          * check, whether we're fired up another time after a crash.
336          * if, post an aide message, so the admin has a chance to react.
337          */
338         checkcrash ();
339
340
341         /*
342          * Now that we've bound the sockets, change to the Citadel user id and its
343          * corresponding group ids
344          */
345         if (drop_root_perms) {
346                 cdb_chmod_data();       /* make sure we own our data files */
347
348 #ifdef HAVE_GETPWUID_R
349 #ifdef SOLARIS_GETPWUID
350                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
351 #else // SOLARIS_GETPWUID
352                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
353 #endif // SOLARIS_GETPWUID
354 #else // HAVE_GETPWUID_R
355                 pwp = NULL;
356 #endif // HAVE_GETPWUID_R
357
358                 if (pwp == NULL)
359                         syslog(LOG_CRIT, "WARNING: getpwuid(%ld): %s"
360                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
361                                 strerror(errno));
362                 else {
363                         initgroups(pw.pw_name, pw.pw_gid);
364                         if (setgid(pw.pw_gid))
365                                 syslog(LOG_CRIT, "setgid(%ld): %s", (long)pw.pw_gid,
366                                         strerror(errno));
367                 }
368                 syslog(LOG_INFO, "Changing uid to %ld", (long)CTDLUID);
369                 if (setuid(CTDLUID) != 0) {
370                         syslog(LOG_CRIT, "setuid() failed: %s", strerror(errno));
371                 }
372 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
373                 prctl(PR_SET_DUMPABLE, 1);
374 #endif
375         }
376
377         /* We want to check for idle sessions once per minute */
378         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
379
380         go_threading();
381         
382         master_cleanup(exit_signal);
383         return(0);
384 }