7c453cc1b505deeebd43e037ba40413fff5e39b5
[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 "citadel_dirs.c"
58
59 #include "modules_init.h"
60 #include "ecrash.h"
61
62 #ifdef HAVE_SYS_SELECT_H
63 #include <sys/select.h>
64 #endif
65
66 #ifndef HAVE_SNPRINTF
67 #include "snprintf.h"
68 #endif
69 const char *CitadelServiceUDS="citadel-UDS";
70 const char *CitadelServiceTCP="citadel-TCP";
71
72
73 void go_threading(void);
74
75 /*
76  * For the subversion revision number
77  */
78 const char *svn_revision(void);
79
80 /*
81  * Here's where it all begins.
82  */
83 int main(int argc, char **argv)
84 {
85         char facility[32];
86         int a;                  /* General-purpose variables */
87         struct passwd pw, *pwp = NULL;
88         char pwbuf[SIZ];
89         int drop_root_perms = 1;
90         size_t size;
91         int relh=0;
92         int home=0;
93         int dbg=0;
94         char relhome[PATH_MAX]="";
95         char ctdldir[PATH_MAX]=CTDLDIR;
96 #ifdef HAVE_RUN_DIR
97         struct stat filestats;
98 #endif
99 #ifdef HAVE_BACKTRACE
100         eCrashParameters params;
101 //      eCrashSymbolTable symbol_table;
102 #endif
103         /* initialise semaphores here. Patch by Matt and davew
104          * its called here as they are needed by CtdlLogPrintf for thread safety
105          */
106         InitialiseSemaphores();
107         
108         /* initialize the master context */
109         InitializeMasterCC();
110
111         /* parse command-line arguments */
112         for (a=1; a<argc; ++a) {
113
114                 if (!strncmp(argv[a], "-l", 2)) {
115                         safestrncpy(facility, &argv[a][2], sizeof(facility));
116                         syslog_facility = SyslogFacility(facility);
117                         enable_syslog = 1;
118                 }
119
120                 /* run in the background if -d was specified */
121                 else if (!strcmp(argv[a], "-d")) {
122                         running_as_daemon = 1;
123                 }
124
125                 /* -x specifies the desired logging level */
126                 else if (!strncmp(argv[a], "-x", 2)) {
127                         verbosity = atoi(&argv[a][2]);
128                 }
129
130                 else if (!strncmp(argv[a], "-h", 2)) {
131                         relh=argv[a][2]!='/';
132                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
133                                                                    sizeof ctdl_home_directory);
134                         else
135                                 safestrncpy(relhome, &argv[a][2],
136                                                         sizeof relhome);
137                         home_specified = 1;
138                         home=1;
139                 }
140
141                 else if (!strncmp(argv[a], "-t", 2)) {
142                         freopen(&argv[a][2], "w", stderr);
143                 }
144
145                 else if (!strncmp(argv[a], "-D", 2)) {
146                         dbg = 1;
147                 }
148
149                 /* -r tells the server not to drop root permissions. don't use
150                  * this unless you know what you're doing. this should be
151                  * removed in the next release if it proves unnecessary. */
152                 else if (!strcmp(argv[a], "-r"))
153                         drop_root_perms = 0;
154
155                 /* any other parameter makes it crash and burn */
156                 else {
157                         CtdlLogPrintf(CTDL_EMERG,       "citserver: usage: "
158                                         "citserver "
159                                         "[-lLogFacility] "
160                                         "[-d] [-D] "
161                                         " [-tTraceFile]"
162                                         " [-xLogLevel] [-hHomeDir]\n");
163                         exit(1);
164                 }
165
166         }
167
168         calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
169         /* daemonize, if we were asked to */
170         if (running_as_daemon) {
171                 start_daemon(0);
172                 drop_root_perms = 1;
173         }
174
175 #ifdef HAVE_BACKTRACE
176         bzero(&params, sizeof(params));
177         params.filename = file_pid_paniclog;
178         panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
179         params.filep = fopen(file_pid_paniclog, "a+");
180         params.debugLevel = ECRASH_DEBUG_VERBOSE;
181         params.dumpAllThreads = TRUE;
182         params.useBacktraceSymbols = 1;
183         params.signals[0]=SIGSEGV;
184         params.signals[1]=SIGILL;
185         params.signals[2]=SIGBUS;
186         params.signals[3]=SIGABRT;
187         eCrash_Init(&params);
188         eCrash_RegisterThread("MasterThread", 0);
189 #endif
190
191         /* Initialize the syslogger.  Yes, we are really using 0 as the
192          * facility, because we are going to bitwise-OR the facility to
193          * the severity of each message, allowing us to write to other
194          * facilities when we need to...
195          */
196         if (enable_syslog) {
197                 openlog("citadel", LOG_NDELAY, 0);
198                 setlogmask(LOG_UPTO(verbosity));
199         }
200         
201         /* Tell 'em who's in da house */
202         CtdlLogPrintf(CTDL_NOTICE, "\n");
203         CtdlLogPrintf(CTDL_NOTICE, "\n");
204         CtdlLogPrintf(CTDL_NOTICE,
205                 "*** Citadel server engine v%d.%02d ***\n",
206                 (REV_LEVEL/100), (REV_LEVEL%100));
207         CtdlLogPrintf(CTDL_NOTICE,
208                 "*** Build number %s ***\n", svn_revision());
209         CtdlLogPrintf(CTDL_NOTICE,
210                 "Copyright (C) 1987-2008 by the Citadel development team.\n");
211         CtdlLogPrintf(CTDL_NOTICE,
212                 "This program is distributed under the terms of the GNU "
213                 "General Public License.\n");
214         CtdlLogPrintf(CTDL_NOTICE, "\n");
215         CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
216         CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
217
218         /* Load site-specific parameters, and set the ipgm secret */
219         CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
220         get_config();
221         config.c_ipgm_secret = rand();
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                 mkdir(ctdl_run_dir, 0755);
238                 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
239         }
240                         
241
242 #endif
243
244         /* Initialize... */
245         init_sysdep();
246
247         /*
248          * Do non system dependent startup functions.
249          */
250         master_startup();
251
252         CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
253         get_control();
254
255         /*
256          * Bind the server to a Unix-domain socket.
257          */
258         CtdlRegisterServiceHook(0,
259                                 file_citadel_socket,
260                                 citproto_begin_session,
261                                 do_command_loop,
262                                 do_async_loop,
263                                 CitadelServiceUDS);
264
265         /*
266          * Bind the server to our favorite TCP port (usually 504).
267          */
268         CtdlRegisterServiceHook(config.c_port_number,
269                                 NULL,
270                                 citproto_begin_session,
271                                 do_command_loop,
272                                 do_async_loop,
273                                 CitadelServiceTCP);
274
275                                 
276         /*
277          * Run any upgrade entry points
278          */
279         CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
280         upgrade_modules();
281         
282         
283         /*
284          * Load any server-side extensions available here.
285          */
286         CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
287         size = strlen(ctdl_home_directory) + 9;
288         
289         initialise_modules(0);
290         
291         
292
293         /*
294          * If we need host auth, start our chkpwd daemon.
295          */
296         if (config.c_auth_mode == AUTHMODE_HOST) {
297                 start_chkpwd_daemon();
298         }
299
300         /*
301          * Now that we've bound the sockets, change to the Citadel user id and its
302          * corresponding group ids
303          */
304         if (drop_root_perms) {
305                 cdb_chmod_data();       /* make sure we own our data files */
306
307 #ifdef HAVE_GETPWUID_R
308 #ifdef SOLARIS_GETPWUID
309                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
310 #else // SOLARIS_GETPWUID
311                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
312 #endif // SOLARIS_GETPWUID
313 #else // HAVE_GETPWUID_R
314                 pwp = NULL;
315 #endif // HAVE_GETPWUID_R
316
317                 if (pwp == NULL)
318                         CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
319                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
320                                 strerror(errno));
321                 else {
322                         initgroups(pw.pw_name, pw.pw_gid);
323                         if (setgid(pw.pw_gid))
324                                 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
325                                         strerror(errno));
326                 }
327                 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
328                 if (setuid(CTDLUID) != 0) {
329                         CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
330                 }
331 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
332                 prctl(PR_SET_DUMPABLE, 1);
333 #endif
334         }
335
336         /* We want to check for idle sessions once per minute */
337         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
338
339         go_threading();
340         
341         
342         master_cleanup(exit_signal);
343         return(0);
344 }