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