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