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