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