2 * citserver's main() function lives here.
4 * Copyright (c) 1987-2021 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.
17 #include <sys/types.h>
21 #include <libcitadel.h>
23 #include "citserver.h"
24 #include "svn_revision.h"
25 #include "modules_init.h"
28 #include "serv_extensions.h"
29 #include "citadel_dirs.h"
33 const char *CitadelServiceUDS="citadel-UDS";
34 const char *CitadelServiceTCP="citadel-TCP";
35 int sanity_diag_mode = 0;
39 * Create or remove a lock file, so we only have one Citadel Server running at a time.
41 void ctdl_lockfile(int yo) {
42 static char lockfilename[PATH_MAX];
46 syslog(LOG_DEBUG, "main: creating lockfile");
47 snprintf(lockfilename, sizeof lockfilename, "%s/citadel.lock", ctdl_run_dir);
48 fp = fopen(lockfilename, "w");
50 syslog(LOG_ERR, "%s: %m", lockfilename);
53 if (flock(fileno(fp), (LOCK_EX|LOCK_NB)) != 0) {
54 syslog(LOG_ERR, "main: cannot lock %s , is another citserver running?", lockfilename);
60 syslog(LOG_DEBUG, "main: removing lockfile");
62 flock(fileno(fp), LOCK_UN);
68 * Here's where it all begins.
70 int main(int argc, char **argv)
74 int a; /* General-purpose variables */
75 struct passwd pw, *pwp = NULL;
77 int drop_root_perms = 1;
81 int max_log_level = LOG_INFO;
82 char relhome[PATH_MAX]="";
83 char ctdldir[PATH_MAX]=CTDLDIR;
84 int syslog_facility = LOG_DAEMON;
86 struct passwd *p = NULL;
88 struct stat filestats;
91 /* initialize the master context */
93 InitializeMasterTSD();
95 /* parse command-line arguments */
96 while ((a=getopt(argc, argv, "l:dh:x:t:B:Dru:s:")) != EOF) switch(a) {
99 safestrncpy(facility, optarg, sizeof(facility));
100 syslog_facility = SyslogFacility(facility);
103 /* run in the background if -d was specified */
105 running_as_daemon = 1;
109 relh = optarg[0] != '/';
111 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
114 safestrncpy(relhome, optarg, sizeof relhome);
120 max_log_level = atoi(optarg);
123 case 't': /* deprecated */
125 case 'B': /* Basesize */
126 basesize = atoi(optarg);
133 /* -r tells the server not to drop root permissions.
134 * Don't use this unless you know what you're doing.
140 /* -u tells the server what uid to run under... */
147 p = getpwnam(optarg);
157 /* -s tells the server to behave differently during sanity checks */
159 sanity_diag_mode = atoi(optarg);
163 /* any other parameter makes it crash and burn */
164 fprintf(stderr, "citserver: usage: "
175 /* Last ditch effort to determine the user name ... if there's a user called "citadel" then use that */
177 p = getpwnam("citadel");
182 p = getpwnam("guest");
192 if ((ctdluid == 0) && (drop_root_perms == 0)) {
193 fprintf(stderr, "citserver: cannot determine user to run as; please specify -r or -u options\n");
194 exit(CTDLEXIT_UNUSER);
197 StartLibCitadel(basesize);
198 setlogmask(LOG_UPTO(max_log_level));
200 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
204 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
205 /* daemonize, if we were asked to */
206 if (running_as_daemon) {
211 /* Tell 'em who's in da house */
212 syslog(LOG_INFO, " ");
213 syslog(LOG_INFO, " ");
214 syslog(LOG_INFO, "*** Citadel server engine ***\n");
215 syslog(LOG_INFO, "Version %d (build %s) ***", REV_LEVEL, svn_revision());
216 syslog(LOG_INFO, "Copyright (C) 1987-2021 by the Citadel development team.");
217 syslog(LOG_INFO, " ");
218 syslog(LOG_INFO, "This program is open source software: you can redistribute it and/or");
219 syslog(LOG_INFO, "modify it under the terms of the GNU General Public License, version 3.");
220 syslog(LOG_INFO, " ");
221 syslog(LOG_INFO, "This program is distributed in the hope that it will be useful,");
222 syslog(LOG_INFO, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
223 syslog(LOG_INFO, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the");
224 syslog(LOG_INFO, "GNU General Public License for more details.");
225 syslog(LOG_INFO, " ");
226 syslog(LOG_INFO, "%s", libcitadel_version_string());
229 /* on some dists rundir gets purged on startup. so we need to recreate it. */
231 if (stat(ctdl_run_dir, &filestats) == -1) {
232 #ifdef HAVE_GETPWUID_R
233 #ifdef SOLARIS_GETPWUID
234 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
235 #else // SOLARIS_GETPWUID
236 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
237 #endif // SOLARIS_GETPWUID
238 #else // HAVE_GETPWUID_R
240 #endif // HAVE_GETPWUID_R
242 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST)) {
243 syslog(LOG_ERR, "main: unable to create run directory [%s]: %m", ctdl_run_dir);
246 if (chown(ctdl_run_dir, ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0) {
247 syslog(LOG_ERR, "main: unable to set the access rights for [%s]: %m", ctdl_run_dir);
253 init_sysdep(); // Initialize...
254 master_startup(); // Do non system dependent startup functions
255 check_control(); // Check/sanitize/initialize control record, fix user indexes
256 syslog(LOG_INFO, "main: upgrading modules"); // Run any upgrade entry points
260 * Load the user for the masterCC or create them if they don't exist
262 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
264 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
265 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
266 CtdlPutUser(&masterCC.user);
267 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
271 * Bind the server to a Unix-domain socket (user client access)
273 CtdlRegisterServiceHook(0,
275 citproto_begin_session,
281 * Bind the server to a Unix-domain socket (admin client access)
283 CtdlRegisterServiceHook(0,
284 file_citadel_admin_socket,
285 citproto_begin_admin_session,
289 chmod(file_citadel_admin_socket, S_IRWXU); /* for your eyes only */
292 * Bind the server to our favorite TCP port (usually 504).
294 CtdlRegisterServiceHook(CtdlGetConfigInt("c_port_number"),
296 citproto_begin_session,
302 * Load any server-side extensions available here.
304 syslog(LOG_INFO, "main: initializing server extensions");
306 initialise_modules(0);
309 * If we need host auth, start our chkpwd daemon.
311 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
312 start_chkpwd_daemon();
316 * check, whether we're fired up another time after a crash.
317 * if, post an aide message, so the admin has a chance to react.
322 * Now that we've bound the sockets, change to the Citadel user id and its
323 * corresponding group ids
325 if (drop_root_perms) {
326 cdb_chmod_data(); /* make sure we own our data files */
328 #ifdef HAVE_GETPWUID_R
329 #ifdef SOLARIS_GETPWUID
330 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
331 #else // SOLARIS_GETPWUID
332 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
333 #endif // SOLARIS_GETPWUID
334 #else // HAVE_GETPWUID_R
336 #endif // HAVE_GETPWUID_R
339 syslog(LOG_ERR, "main: WARNING, getpwuid(%ld): %m Group IDs will be incorrect.", (long)CTDLUID);
341 initgroups(pw.pw_name, pw.pw_gid);
342 if (setgid(pw.pw_gid))
343 syslog(LOG_ERR, "main: setgid(%ld): %m", (long)pw.pw_gid);
345 syslog(LOG_INFO, "main: changing uid to %ld", (long)CTDLUID);
346 if (setuid(CTDLUID) != 0) {
347 syslog(LOG_ERR, "main: setuid() failed: %m");
349 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
350 prctl(PR_SET_DUMPABLE, 1);
354 /* We want to check for idle sessions once per minute */
355 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
359 int exit_code = master_cleanup(exit_signal);
361 if (restart_server) {
362 syslog(LOG_INFO, "main: *** CITADEL SERVER IS RESTARTING ***");
363 execv(argv[0], argv);