1 // citserver's main() function lives here.
3 // Copyright (c) 1987-2023 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 3.
11 #include <sys/types.h>
15 #include <libcitadel.h>
16 #include "citserver.h"
17 #include "modules_init.h"
20 #include "serv_extensions.h"
21 #include "citadel_dirs.h"
25 const char *CitadelServiceUDS="citadel-UDS";
26 const char *CitadelServiceTCP="citadel-TCP";
27 int sanity_diag_mode = 0;
28 char *rescue_string = NULL;
31 // Create or remove a lock file, so we only have one Citadel Server running at a time.
32 // Set 'op' to nonzero to lock, zero to unlock.
33 void ctdl_lockfile(int op) {
34 static char lockfilename[PATH_MAX];
38 syslog(LOG_DEBUG, "main: creating lockfile");
39 snprintf(lockfilename, sizeof lockfilename, "%s/citadel.lock", ctdl_run_dir);
40 fp = fopen(lockfilename, "w");
42 syslog(LOG_ERR, "%s: %m", lockfilename);
45 if (flock(fileno(fp), (LOCK_EX|LOCK_NB)) != 0) {
46 syslog(LOG_ERR, "main: cannot lock %s (is another citserver running?)", lockfilename);
52 syslog(LOG_DEBUG, "main: removing lockfile");
54 flock(fileno(fp), LOCK_UN);
59 // Here's where it all begins.
60 int main(int argc, char **argv) {
63 struct passwd pw, *pwp = NULL;
65 int drop_root_perms = 1;
66 int max_log_level = LOG_INFO;
67 char *ctdldir = CTDLDIR;
68 int syslog_facility = LOG_DAEMON;
70 struct passwd *p = NULL;
72 struct stat filestats;
75 // Tell 'em who's in da house
76 syslog(LOG_INFO, " ");
77 syslog(LOG_INFO, " ");
78 syslog(LOG_INFO, "*** Citadel server engine ***\n");
79 syslog(LOG_INFO, "Version %d (build %s) ***", REV_LEVEL, BUILD_ID);
80 syslog(LOG_INFO, "Copyright (C) 1987-2023 by the Citadel development team.");
81 syslog(LOG_INFO, " ");
82 syslog(LOG_INFO, "This program is open source software. Use, duplication, or disclosure");
83 syslog(LOG_INFO, "is subject to the terms of the GNU General Public License, version 3.");
84 syslog(LOG_INFO, " ");
85 syslog(LOG_INFO, "%s", libcitadel_version_string());
87 // parse command-line arguments
89 while ((g=getopt(argc, argv, "cl:dh:x:t:B:Dru:s:R:")) != EOF) switch(g) {
91 // test this binary for compatibility and exit
93 fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
97 // identify the desired syslog facility
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;
108 // specify the data directory
113 // identify the desired logging severity level
115 max_log_level = atoi(optarg);
118 // deprecated flags from old versions -- ignore silently to prevent breaking scripts
124 // -r tells the server not to drop root permissions.
125 // Don't use this unless you know what you're doing.
130 // -u tells the server what uid to run under...
137 p = getpwnam(optarg);
147 // -s tells the server to behave differently during sanity checks
149 sanity_diag_mode = atoi(optarg);
152 // -R is an undocumented rescue mode that you should never use
154 rescue_string = strdup(optarg);
157 // any other parameter makes it crash and burn
159 fprintf(stderr, "citserver: usage: "
170 if (chdir(ctdldir) != 0) {
171 syslog(LOG_ERR, "main: unable to change directory to [%s]: %m", ctdldir);
175 syslog(LOG_INFO, "main: running in data directory %s", ctdldir);
178 if ((ctdluid == 0) && (drop_root_perms == 0)) {
179 fprintf(stderr, "citserver: cannot determine user to run as; please specify -r or -u options\n");
180 exit(CTDLEXIT_UNUSER);
183 // Last ditch effort to determine the user name ... if there's a user called "citadel" then use that
185 p = getpwnam("citadel");
190 p = getpwnam("guest");
200 // initialize the master context
201 InitializeMasterCC();
202 InitializeMasterTSD();
204 setlogmask(LOG_UPTO(max_log_level));
206 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
210 // daemonize, if we were asked to
211 if (running_as_daemon) {
216 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST)) {
217 syslog(LOG_ERR, "main: unable to create run directory [%s]: %m", ctdl_run_dir);
220 if (chown(ctdl_run_dir, ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0) {
221 syslog(LOG_ERR, "main: unable to set the access rights for [%s]: %m", ctdl_run_dir);
225 init_sysdep(); // Initialize...
226 master_startup(); // Do non system dependent startup functions
227 check_control(); // Check/sanitize/initialize control record, fix user indexes
228 syslog(LOG_INFO, "main: upgrading modules"); // Run any upgrade entry points
229 pre_startup_upgrades();
231 // Load the user for the masterCC or create them if they don't exist
232 if (CtdlGetUser(&masterCC.user, "SYS_Citadel")) {
233 // User doesn't exist. We can't use create user here as the user number needs to be 0
234 strcpy(masterCC.user.fullname, "SYS_Citadel") ;
235 CtdlPutUser(&masterCC.user);
236 CtdlGetUser(&masterCC.user, "SYS_Citadel"); // Just to be safe
239 // Bind the server to a Unix-domain socket (user client access)
240 CtdlRegisterServiceHook(0,
242 citproto_begin_session,
247 // Bind the server to a Unix-domain socket (admin client access)
248 CtdlRegisterServiceHook(0,
249 file_citadel_admin_socket,
250 citproto_begin_admin_session,
254 chmod(file_citadel_admin_socket, S_IRWXU); // protect the admin socket - it offers high privilege
256 // Bind the server to our favorite TCP port (usually 504).
257 CtdlRegisterServiceHook(CtdlGetConfigInt("c_port_number"),
259 citproto_begin_session,
264 // Load any server-side extensions available here.
265 syslog(LOG_INFO, "main: initializing server extensions");
266 initialize_modules(0);
268 // If we need host auth, start our chkpwd daemon.
269 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
270 start_chkpwd_daemon();
273 // check, whether we're fired up another time after a crash.
274 // if, post an aide message, so the admin has a chance to react.
277 // Now that we've bound the sockets, change to the Citadel user id and its corresponding group ids
278 if (drop_root_perms) {
279 cdb_chmod_data(); // make sure we own our data files
280 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
282 syslog(LOG_ERR, "main: WARNING, getpwuid(%ld): %m Group IDs will be incorrect.", (long)CTDLUID);
285 initgroups(pw.pw_name, pw.pw_gid);
286 if (setgid(pw.pw_gid)) {
287 syslog(LOG_ERR, "main: setgid(%ld): %m", (long)pw.pw_gid);
290 syslog(LOG_INFO, "main: changing uid to %ld", (long)CTDLUID);
291 if (setuid(CTDLUID) != 0) {
292 syslog(LOG_ERR, "main: setuid() failed: %m");
294 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
295 prctl(PR_SET_DUMPABLE, 1);
299 // We want to check for idle sessions once per minute
300 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
302 // Are we in the undocumented rescue mode?
304 undocumented_rescue_mode(rescue_string);
307 // Go into multithreaded mode. When this call exits, the server is stopping.
311 // Get ready to shut down the server.
312 int exit_code = master_cleanup(exit_signal);
314 if (restart_server) {
315 syslog(LOG_INFO, "main: *** CITADEL SERVER IS RESTARTING ***");
316 execv(argv[0], argv);