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