Changed the year in the copyright banner(s) to 2019.
[citadel.git] / citadel / server_main.c
1 /*
2  * citserver's main() function lives here.
3  * 
4  * Copyright (c) 1987-2019 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 <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <grp.h>
20 #include <sys/file.h>
21 #include <libcitadel.h>
22
23 #include "citserver.h"
24 #include "svn_revision.h"
25 #include "modules_init.h"
26 #include "config.h"
27 #include "control.h"
28 #include "serv_extensions.h"
29 #include "citadel_dirs.h"
30 #include "user_ops.h"
31
32 uid_t ctdluid = 0;
33 const char *CitadelServiceUDS="citadel-UDS";
34 const char *CitadelServiceTCP="citadel-TCP";
35 void go_threading(void);
36 int sanity_diag_mode = 0;
37
38
39 /*
40  * Create or remove a lock file, so we only have one Citadel Server running at a time.
41  */
42 void ctdl_lockfile(int yo) {
43         static char lockfilename[SIZ];
44         static FILE *fp;
45
46         if (yo) {
47                 syslog(LOG_DEBUG, "main: creating lockfile");
48                 snprintf(lockfilename, sizeof lockfilename, "%s/citadel.lock", ctdl_run_dir);
49                 fp = fopen(lockfilename, "w");
50                 if (!fp) {
51                         syslog(LOG_ERR, "%s: %m", lockfilename);
52                         exit(CTDLEXIT_DB);
53                 }
54                 if (flock(fileno(fp), (LOCK_EX|LOCK_NB)) != 0) {
55                         syslog(LOG_ERR, "main: cannot lock %s , is another citserver running?", lockfilename);
56                         exit(CTDLEXIT_DB);
57                 }
58                 return;
59         }
60
61         syslog(LOG_DEBUG, "main: removing lockfile");
62         unlink(lockfilename);
63         flock(fileno(fp), LOCK_UN);
64         fclose(fp);
65 }
66
67
68 /*
69  * Here's where it all begins.
70  */
71 int main(int argc, char **argv)
72 {
73         size_t basesize = 64;
74         char facility[32];
75         int a;                  /* General-purpose variables */
76         struct passwd pw, *pwp = NULL;
77         char pwbuf[SIZ];
78         int drop_root_perms = 1;
79         int relh=0;
80         int home=0;
81         int dbg=0;
82         int max_log_level = LOG_INFO;
83         char relhome[PATH_MAX]="";
84         char ctdldir[PATH_MAX]=CTDLDIR;
85         int syslog_facility = LOG_DAEMON;
86         uid_t u = 0;
87         struct passwd *p = NULL;
88 #ifdef HAVE_RUN_DIR
89         struct stat filestats;
90 #endif
91
92         /* initialize the master context */
93         InitializeMasterCC();
94         InitializeMasterTSD();
95
96         /* parse command-line arguments */
97         while ((a=getopt(argc, argv, "l:dh:x:t:B:Dru:s:")) != EOF) switch(a) {
98
99                 case 'l':
100                         safestrncpy(facility, optarg, sizeof(facility));
101                         syslog_facility = SyslogFacility(facility);
102                         break;
103
104                 /* run in the background if -d was specified */
105                 case 'd':
106                         running_as_daemon = 1;
107                         break;
108
109                 case 'h':
110                         relh = optarg[0] != '/';
111                         if (!relh) {
112                                 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
113                         }
114                         else {
115                                 safestrncpy(relhome, optarg, sizeof relhome);
116                         }
117                         home=1;
118                         break;
119
120                 case 'x':
121                         max_log_level = atoi(optarg);
122                         break;
123
124                 case 't':       /* deprecated */
125                         break;
126                 case 'B': /* Basesize */
127                         basesize = atoi(optarg);
128                         break;
129
130                 case 'D':
131                         dbg = 1;
132                         break;
133
134                 /* -r tells the server not to drop root permissions.
135                  * Don't use this unless you know what you're doing.
136                  */
137                 case 'r':
138                         drop_root_perms = 0;
139                         break;
140
141                 /* -u tells the server what uid to run under... */
142                 case 'u':
143                         u = atoi(optarg);
144                         if (u > 0) {
145                                 ctdluid = u;
146                         }
147                         else {
148                                 p = getpwnam(optarg);
149                                 if (p) {
150                                         u = p->pw_uid;
151                                 }
152                         }
153                         if (u > 0) {
154                                 ctdluid = u;
155                         }
156                         break;
157
158                 /* -s tells the server to behave differently during sanity checks */
159                 case 's':
160                         sanity_diag_mode = atoi(optarg);
161                         break;
162
163                 default:
164                 /* any other parameter makes it crash and burn */
165                         fprintf(stderr, "citserver: usage: "
166                                         "citserver "
167                                         "[-l LogFacility] "
168                                         "[-x MaxLogLevel] "
169                                         "[-d] [-D] [-r] "
170                                         "[-u user] "
171                                         "[-h HomeDir]\n"
172                         );
173                         exit(1);
174         }
175
176         /* Last ditch effort to determine the user name ... if there's a user called "citadel" then use that */
177         if (ctdluid == 0) {
178                 p = getpwnam("citadel");
179                 if (!p) {
180                         p = getpwnam("bbs");
181                 }
182                 if (!p) {
183                         p = getpwnam("guest");
184                 }
185                 if (p) {
186                         u = p->pw_uid;
187                 }
188                 if (u > 0) {
189                         ctdluid = u;
190                 }
191         }
192
193         if ((ctdluid == 0) && (drop_root_perms == 0)) {
194                 fprintf(stderr, "citserver: cannot determine user to run as; please specify -r or -u options\n");
195                 exit(CTDLEXIT_UNUSER);
196         }
197
198         StartLibCitadel(basesize);
199         setlogmask(LOG_UPTO(max_log_level));
200         openlog("citserver",
201                 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
202                 syslog_facility
203         );
204
205         calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
206         /* daemonize, if we were asked to */
207         if (running_as_daemon) {
208                 start_daemon(0);
209                 drop_root_perms = 1;
210         }
211
212         /* Tell 'em who's in da house */
213         syslog(LOG_INFO, " ");
214         syslog(LOG_INFO, " ");
215         syslog(LOG_INFO, "*** Citadel server engine ***\n");
216         syslog(LOG_INFO, "Version %d (build %s) ***", REV_LEVEL, svn_revision());
217         syslog(LOG_INFO, "Copyright (C) 1987-2019 by the Citadel development team.");
218         syslog(LOG_INFO, " ");
219         syslog(LOG_INFO, "This program is open source software: you can redistribute it and/or");
220         syslog(LOG_INFO, "modify it under the terms of the GNU General Public License, version 3.");
221         syslog(LOG_INFO, " ");
222         syslog(LOG_INFO, "This program is distributed in the hope that it will be useful,");
223         syslog(LOG_INFO, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
224         syslog(LOG_INFO, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
225         syslog(LOG_INFO, "GNU General Public License for more details.");
226         syslog(LOG_INFO, " ");
227         syslog(LOG_INFO, "%s", libcitadel_version_string());
228
229 #ifdef HAVE_RUN_DIR
230         /* on some dists rundir gets purged on startup. so we need to recreate it. */
231
232         if (stat(ctdl_run_dir, &filestats) == -1) {
233 #ifdef HAVE_GETPWUID_R
234 #ifdef SOLARIS_GETPWUID
235                 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
236 #else // SOLARIS_GETPWUID
237                 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
238 #endif // SOLARIS_GETPWUID
239 #else // HAVE_GETPWUID_R
240                 pwp = NULL;
241 #endif // HAVE_GETPWUID_R
242
243                 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST)) {
244                         syslog(LOG_ERR, "main: unable to create run directory [%s]: %m", ctdl_run_dir);
245                 }
246
247                 if (chown(ctdl_run_dir, ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0) {
248                         syslog(LOG_ERR, "main: unable to set the access rights for [%s]: %m", ctdl_run_dir);
249                 }
250         }
251 #endif
252
253         ctdl_lockfile(1);
254
255         /* Initialize... */
256         init_sysdep();
257
258         /*
259          * Do non system dependent startup functions.
260          */
261         master_startup();
262
263         /*
264          * Check that the control record is correct and place sensible values if it isn't
265          */
266         check_control();
267         
268         /*
269          * Run any upgrade entry points
270          */
271         syslog(LOG_INFO, "main: upgrading modules");
272         upgrade_modules();
273         
274 /*
275  * Load the user for the masterCC or create them if they don't exist
276  */
277         if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
278         {
279                 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
280                 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
281                 CtdlPutUser(&masterCC.user);
282                 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
283         }
284         
285         /*
286          * Bind the server to a Unix-domain socket (user client access)
287          */
288         CtdlRegisterServiceHook(0,
289                                 file_citadel_socket,
290                                 citproto_begin_session,
291                                 do_command_loop,
292                                 do_async_loop,
293                                 CitadelServiceUDS);
294
295         /*
296          * Bind the server to a Unix-domain socket (admin client access)
297          */
298         CtdlRegisterServiceHook(0,
299                                 file_citadel_admin_socket,
300                                 citproto_begin_admin_session,
301                                 do_command_loop,
302                                 do_async_loop,
303                                 CitadelServiceUDS);
304         chmod(file_citadel_admin_socket, S_IRWXU);      /* for your eyes only */
305
306         /*
307          * Bind the server to our favorite TCP port (usually 504).
308          */
309         CtdlRegisterServiceHook(CtdlGetConfigInt("c_port_number"),
310                                 NULL,
311                                 citproto_begin_session,
312                                 do_command_loop,
313                                 do_async_loop,
314                                 CitadelServiceTCP);
315
316                                 
317         
318         
319         /*
320          * Load any server-side extensions available here.
321          */
322         syslog(LOG_INFO, "main: initializing server extensions");
323         
324         initialise_modules(0);
325
326         /*
327          * If we need host auth, start our chkpwd daemon.
328          */
329         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
330                 start_chkpwd_daemon();
331         }
332
333         /*
334          * check, whether we're fired up another time after a crash.
335          * if, post an aide message, so the admin has a chance to react.
336          */
337         checkcrash();
338
339         /*
340          * Now that we've bound the sockets, change to the Citadel user id and its
341          * corresponding group ids
342          */
343         if (drop_root_perms) {
344                 cdb_chmod_data();       /* make sure we own our data files */
345
346 #ifdef HAVE_GETPWUID_R
347 #ifdef SOLARIS_GETPWUID
348                 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
349 #else // SOLARIS_GETPWUID
350                 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
351 #endif // SOLARIS_GETPWUID
352 #else // HAVE_GETPWUID_R
353                 pwp = NULL;
354 #endif // HAVE_GETPWUID_R
355
356                 if (pwp == NULL)
357                         syslog(LOG_ERR, "main: WARNING, getpwuid(%ld): %m Group IDs will be incorrect.", (long)CTDLUID);
358                 else {
359                         initgroups(pw.pw_name, pw.pw_gid);
360                         if (setgid(pw.pw_gid))
361                                 syslog(LOG_ERR, "main: setgid(%ld): %m", (long)pw.pw_gid);
362                 }
363                 syslog(LOG_INFO, "main: changing uid to %ld", (long)CTDLUID);
364                 if (setuid(CTDLUID) != 0) {
365                         syslog(LOG_ERR, "main: setuid() failed: %m");
366                 }
367 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
368                 prctl(PR_SET_DUMPABLE, 1);
369 #endif
370         }
371
372         /* We want to check for idle sessions once per minute */
373         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
374
375         go_threading();
376         
377         int exit_code = master_cleanup(exit_signal);
378         ctdl_lockfile(0);
379         return(exit_code);
380 }