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