2 * citserver's main() function lives here.
4 * Copyright (c) 1987-2016 by the citadel.org team
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.
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.
15 #include <sys/types.h>
17 #include <libcitadel.h>
19 #include "citserver.h"
20 #include "svn_revision.h"
21 #include "modules_init.h"
24 #include "serv_extensions.h"
25 #include "citadel_dirs.h"
30 const char *CitadelServiceUDS="citadel-UDS";
31 const char *CitadelServiceTCP="citadel-TCP";
35 void go_threading(void);
38 * Here's where it all begins.
40 int main(int argc, char **argv)
44 int a; /* General-purpose variables */
45 struct passwd pw, *pwp = NULL;
47 int drop_root_perms = 1;
51 char relhome[PATH_MAX]="";
52 char ctdldir[PATH_MAX]=CTDLDIR;
53 int syslog_facility = LOG_DAEMON;
54 const char *eDebuglist[] = {NULL, NULL};
56 struct passwd *p = NULL;
58 struct stat filestats;
61 eCrashParameters params;
62 // eCrashSymbolTable symbol_table;
65 /* initialize the master context */
67 InitializeMasterTSD();
69 /* parse command-line arguments */
70 while ((a=getopt(argc, argv, "l:dh:x:t:B:Dru:")) != EOF) switch(a) {
73 safestrncpy(facility, optarg, sizeof(facility));
74 syslog_facility = SyslogFacility(facility);
77 /* run in the background if -d was specified */
79 running_as_daemon = 1;
83 relh = optarg[0] != '/';
85 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
88 safestrncpy(relhome, optarg, sizeof relhome);
94 eDebuglist [0] = optarg;
97 case 't': /* deprecated */
99 case 'B': /* Basesize */
100 basesize = atoi(optarg);
107 /* -r tells the server not to drop root permissions.
108 * Don't use this unless you know what you're doing.
114 /* -u tells the server what uid to run under... */
121 p = getpwnam(optarg);
132 /* any other parameter makes it crash and burn */
133 fprintf(stderr, "citserver: usage: "
143 /* Last ditch effort to determine the user name ... if there's a user called "citadel" then use that */
145 p = getpwnam("citadel");
150 p = getpwnam("guest");
160 if ((ctdluid == 0) && (drop_root_perms == 0)) {
161 fprintf(stderr, "citserver: cannot determine user to run as; please specify -r or -u options\n");
162 exit(CTDLEXIT_UNUSER);
165 StartLibCitadel(basesize);
167 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
171 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
172 /* daemonize, if we were asked to */
173 if (running_as_daemon) {
180 bzero(¶ms, sizeof(params));
181 params.filename = file_pid_paniclog;
182 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
183 params.filep = fopen(file_pid_paniclog, "a+");
184 params.debugLevel = ECRASH_DEBUG_VERBOSE;
185 params.dumpAllThreads = TRUE;
186 params.useBacktraceSymbols = 1;
187 params.signals[0]=SIGSEGV;
188 params.signals[1]=SIGILL;
189 params.signals[2]=SIGBUS;
190 params.signals[3]=SIGABRT;
191 eCrash_Init(¶ms);
192 eCrash_RegisterThread("MasterThread", 0);
195 /* Tell 'em who's in da house */
196 syslog(LOG_NOTICE, " ");
197 syslog(LOG_NOTICE, " ");
198 syslog(LOG_NOTICE, "*** Citadel server engine ***\n");
199 syslog(LOG_NOTICE, "Version %d (build %s) ***", REV_LEVEL, svn_revision());
200 syslog(LOG_NOTICE, "Copyright (C) 1987-2016 by the Citadel development team.");
201 syslog(LOG_NOTICE, "This program is distributed under the terms of the GNU "
202 "General Public License.");
203 syslog(LOG_NOTICE, " ");
204 syslog(LOG_DEBUG, "Called as: %s", argv[0]);
205 syslog(LOG_INFO, "%s", libcitadel_version_string());
208 /* on some dists rundir gets purged on startup. so we need to recreate it. */
210 if (stat(ctdl_run_dir, &filestats)==-1){
211 #ifdef HAVE_GETPWUID_R
212 #ifdef SOLARIS_GETPWUID
213 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
214 #else // SOLARIS_GETPWUID
215 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
216 #endif // SOLARIS_GETPWUID
217 #else // HAVE_GETPWUID_R
219 #endif // HAVE_GETPWUID_R
221 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST))
223 "unable to create run directory [%s]: %s",
224 ctdl_run_dir, strerror(errno));
226 if (chown(ctdl_run_dir, ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0)
228 "unable to set the access rights for [%s]: %s",
229 ctdl_run_dir, strerror(errno));
239 * Do non system dependent startup functions.
244 * Check that the control record is correct and place sensible values if it isn't
249 * Run any upgrade entry points
251 syslog(LOG_INFO, "Upgrading modules.");
255 * Load the user for the masterCC or create them if they don't exist
257 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
259 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
260 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
261 CtdlPutUser(&masterCC.user);
262 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
266 * Bind the server to a Unix-domain socket (user client access)
268 CtdlRegisterServiceHook(0,
270 citproto_begin_session,
276 * Bind the server to a Unix-domain socket (admin client access)
278 CtdlRegisterServiceHook(0,
279 file_citadel_admin_socket,
280 citproto_begin_admin_session,
284 chmod(file_citadel_admin_socket, S_IRWXU); /* for your eyes only */
287 * Bind the server to our favorite TCP port (usually 504).
289 CtdlRegisterServiceHook(CtdlGetConfigInt("c_port_number"),
291 citproto_begin_session,
300 * Load any server-side extensions available here.
302 syslog(LOG_INFO, "Initializing server extensions");
304 initialise_modules(0);
306 eDebuglist[1] = getenv("CITADEL_LOGDEBUG");
307 CtdlSetDebugLogFacilities(eDebuglist, 2);
310 * If we need host auth, start our chkpwd daemon.
312 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
313 start_chkpwd_daemon();
318 * check, whether we're fired up another time after a crash.
319 * if, post an aide message, so the admin has a chance to react.
325 * Now that we've bound the sockets, change to the Citadel user id and its
326 * corresponding group ids
328 if (drop_root_perms) {
329 cdb_chmod_data(); /* make sure we own our data files */
331 #ifdef HAVE_GETPWUID_R
332 #ifdef SOLARIS_GETPWUID
333 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
334 #else // SOLARIS_GETPWUID
335 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
336 #endif // SOLARIS_GETPWUID
337 #else // HAVE_GETPWUID_R
339 #endif // HAVE_GETPWUID_R
342 syslog(LOG_CRIT, "WARNING: getpwuid(%ld): %s"
343 "Group IDs will be incorrect.\n", (long)CTDLUID,
346 initgroups(pw.pw_name, pw.pw_gid);
347 if (setgid(pw.pw_gid))
348 syslog(LOG_CRIT, "setgid(%ld): %s", (long)pw.pw_gid,
351 syslog(LOG_INFO, "Changing uid to %ld", (long)CTDLUID);
352 if (setuid(CTDLUID) != 0) {
353 syslog(LOG_CRIT, "setuid() failed: %s", strerror(errno));
355 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
356 prctl(PR_SET_DUMPABLE, 1);
360 /* We want to check for idle sessions once per minute */
361 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
365 master_cleanup(exit_signal);