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