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