silenced a silly little compiler warning
[citadel.git] / citadel / server_main.c
1 /*
2  * citserver's main() function lives here.
3  * 
4  * Copyright (c) 1987-2017 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 #include "ecrash.h"
32
33 uid_t ctdluid = 0;
34 const char *CitadelServiceUDS="citadel-UDS";
35 const char *CitadelServiceTCP="citadel-TCP";
36 void go_threading(void);
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 #ifdef HAVE_BACKTRACE
92         eCrashParameters params;
93 //      eCrashSymbolTable symbol_table;
94 #endif
95
96         /* initialize the master context */
97         InitializeMasterCC();
98         InitializeMasterTSD();
99
100         /* parse command-line arguments */
101         while ((a=getopt(argc, argv, "l:dh:x:t:B:Dru:")) != EOF) switch(a) {
102
103                 case 'l':
104                         safestrncpy(facility, optarg, sizeof(facility));
105                         syslog_facility = SyslogFacility(facility);
106                         break;
107
108                 /* run in the background if -d was specified */
109                 case 'd':
110                         running_as_daemon = 1;
111                         break;
112
113                 case 'h':
114                         relh = optarg[0] != '/';
115                         if (!relh) {
116                                 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
117                         }
118                         else {
119                                 safestrncpy(relhome, optarg, sizeof relhome);
120                         }
121                         home=1;
122                         break;
123
124                 case 'x':
125                         max_log_level = atoi(optarg);
126                         break;
127
128                 case 't':       /* deprecated */
129                         break;
130                 case 'B': /* Basesize */
131                         basesize = atoi(optarg);
132                         break;
133
134                 case 'D':
135                         dbg = 1;
136                         break;
137
138                 /* -r tells the server not to drop root permissions.
139                  * Don't use this unless you know what you're doing.
140                  */
141                 case 'r':
142                         drop_root_perms = 0;
143                         break;
144
145                 /* -u tells the server what uid to run under... */
146                 case 'u':
147                         u = atoi(optarg);
148                         if (u > 0) {
149                                 ctdluid = u;
150                         }
151                         else {
152                                 p = getpwnam(optarg);
153                                 if (p) {
154                                         u = p->pw_uid;
155                                 }
156                         }
157                         if (u > 0) {
158                                 ctdluid = u;
159                         }
160                         break;
161
162                 default:
163                 /* any other parameter makes it crash and burn */
164                         fprintf(stderr, "citserver: usage: "
165                                         "citserver "
166                                         "[-l LogFacility] "
167                                         "[-x MaxLogLevel] "
168                                         "[-d] [-D] [-r] "
169                                         "[-u user] "
170                                         "[-h HomeDir]\n"
171                         );
172                         exit(1);
173         }
174
175         /* Last ditch effort to determine the user name ... if there's a user called "citadel" then use that */
176         if (ctdluid == 0) {
177                 p = getpwnam("citadel");
178                 if (!p) {
179                         p = getpwnam("bbs");
180                 }
181                 if (!p) {
182                         p = getpwnam("guest");
183                 }
184                 if (p) {
185                         u = p->pw_uid;
186                 }
187                 if (u > 0) {
188                         ctdluid = u;
189                 }
190         }
191
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);
195         }
196
197         StartLibCitadel(basesize);
198         setlogmask(LOG_UPTO(max_log_level));
199         openlog("citserver",
200                 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
201                 syslog_facility
202         );
203
204         calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
205         /* daemonize, if we were asked to */
206         if (running_as_daemon) {
207                 start_daemon(0);
208                 drop_root_perms = 1;
209         }
210
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-2017 by the Citadel development team.");
217         syslog(LOG_INFO, "This program is distributed under the terms of the GNU General Public License.");
218         syslog(LOG_INFO, " ");
219         syslog(LOG_INFO, "%s", libcitadel_version_string());
220
221 #ifdef HAVE_RUN_DIR
222         /* on some dists rundir gets purged on startup. so we need to recreate it. */
223
224         if (stat(ctdl_run_dir, &filestats)==-1){
225 #ifdef HAVE_GETPWUID_R
226 #ifdef SOLARIS_GETPWUID
227                 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
228 #else // SOLARIS_GETPWUID
229                 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
230 #endif // SOLARIS_GETPWUID
231 #else // HAVE_GETPWUID_R
232                 pwp = NULL;
233 #endif // HAVE_GETPWUID_R
234
235                 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST)) {
236                         syslog(LOG_ERR, "main: unable to create run directory [%s]: %m", ctdl_run_dir);
237                 }
238
239                 if (chown(ctdl_run_dir, ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0) {
240                         syslog(LOG_ERR, "main: unable to set the access rights for [%s]: %m", ctdl_run_dir);
241                 }
242         }
243 #endif
244
245         ctdl_lockfile(1);
246
247         /* Initialize... */
248         init_sysdep();
249
250         /*
251          * Do non system dependent startup functions.
252          */
253         master_startup();
254
255         /*
256          * Check that the control record is correct and place sensible values if it isn't
257          */
258         check_control();
259         
260         /*
261          * Run any upgrade entry points
262          */
263         syslog(LOG_INFO, "main: upgrading modules");
264         upgrade_modules();
265         
266 /*
267  * Load the user for the masterCC or create them if they don't exist
268  */
269         if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
270         {
271                 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
272                 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
273                 CtdlPutUser(&masterCC.user);
274                 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
275         }
276         
277         /*
278          * Bind the server to a Unix-domain socket (user client access)
279          */
280         CtdlRegisterServiceHook(0,
281                                 file_citadel_socket,
282                                 citproto_begin_session,
283                                 do_command_loop,
284                                 do_async_loop,
285                                 CitadelServiceUDS);
286
287         /*
288          * Bind the server to a Unix-domain socket (admin client access)
289          */
290         CtdlRegisterServiceHook(0,
291                                 file_citadel_admin_socket,
292                                 citproto_begin_admin_session,
293                                 do_command_loop,
294                                 do_async_loop,
295                                 CitadelServiceUDS);
296         chmod(file_citadel_admin_socket, S_IRWXU);      /* for your eyes only */
297
298         /*
299          * Bind the server to our favorite TCP port (usually 504).
300          */
301         CtdlRegisterServiceHook(CtdlGetConfigInt("c_port_number"),
302                                 NULL,
303                                 citproto_begin_session,
304                                 do_command_loop,
305                                 do_async_loop,
306                                 CitadelServiceTCP);
307
308                                 
309         
310         
311         /*
312          * Load any server-side extensions available here.
313          */
314         syslog(LOG_INFO, "main: initializing server extensions");
315         
316         initialise_modules(0);
317
318         /*
319          * If we need host auth, start our chkpwd daemon.
320          */
321         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
322                 start_chkpwd_daemon();
323         }
324
325
326         /*
327          * check, whether we're fired up another time after a crash.
328          * if, post an aide message, so the admin has a chance to react.
329          */
330         checkcrash ();
331
332
333         /*
334          * Now that we've bound the sockets, change to the Citadel user id and its
335          * corresponding group ids
336          */
337         if (drop_root_perms) {
338                 cdb_chmod_data();       /* make sure we own our data files */
339
340 #ifdef HAVE_GETPWUID_R
341 #ifdef SOLARIS_GETPWUID
342                 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
343 #else // SOLARIS_GETPWUID
344                 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
345 #endif // SOLARIS_GETPWUID
346 #else // HAVE_GETPWUID_R
347                 pwp = NULL;
348 #endif // HAVE_GETPWUID_R
349
350                 if (pwp == NULL)
351                         syslog(LOG_ERR, "main: WARNING, getpwuid(%ld): %m Group IDs will be incorrect.", (long)CTDLUID);
352                 else {
353                         initgroups(pw.pw_name, pw.pw_gid);
354                         if (setgid(pw.pw_gid))
355                                 syslog(LOG_ERR, "main: setgid(%ld): %m", (long)pw.pw_gid);
356                 }
357                 syslog(LOG_INFO, "main: changing uid to %ld", (long)CTDLUID);
358                 if (setuid(CTDLUID) != 0) {
359                         syslog(LOG_ERR, "main: setuid() failed: %m");
360                 }
361 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
362                 prctl(PR_SET_DUMPABLE, 1);
363 #endif
364         }
365
366         /* We want to check for idle sessions once per minute */
367         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
368
369         go_threading();
370         
371         int exit_code = master_cleanup(exit_signal);
372         ctdl_lockfile(0);
373         return(exit_code);
374 }