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