ctdluid is now specified on the command line with the new -u option. Config file...
[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 void go_threading(void);
33
34 /*
35  * Here's where it all begins.
36  */
37 int main(int argc, char **argv)
38 {
39         size_t basesize = 64;
40         char facility[32];
41         int a;                  /* General-purpose variables */
42         struct passwd pw, *pwp = NULL;
43         char pwbuf[SIZ];
44         int drop_root_perms = 1;
45         int relh=0;
46         int home=0;
47         int dbg=0;
48         char relhome[PATH_MAX]="";
49         char ctdldir[PATH_MAX]=CTDLDIR;
50         int syslog_facility = LOG_DAEMON;
51         const char *eDebuglist[] = {NULL, NULL};
52         uid_t u = 0;
53         struct passwd *p = NULL;
54 #ifdef HAVE_RUN_DIR
55         struct stat filestats;
56 #endif
57 #ifdef HAVE_BACKTRACE
58         eCrashParameters params;
59 //      eCrashSymbolTable symbol_table;
60 #endif
61
62         /* initialize the master context */
63         InitializeMasterCC();
64         InitializeMasterTSD();
65
66         /* parse command-line arguments */
67         while ((a=getopt(argc, argv, "l:dh:x:t:B:Dru:")) != EOF) switch(a) {
68
69                 case 'l':
70                         safestrncpy(facility, optarg, sizeof(facility));
71                         syslog_facility = SyslogFacility(facility);
72                         break;
73
74                 /* run in the background if -d was specified */
75                 case 'd':
76                         running_as_daemon = 1;
77                         break;
78
79                 case 'h':
80                         relh = optarg[0] != '/';
81                         if (!relh) {
82                                 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
83                         }
84                         else {
85                                 safestrncpy(relhome, optarg, sizeof relhome);
86                         }
87                         home=1;
88                         break;
89
90                 case 'x':
91                         eDebuglist [0] = optarg;
92                         break;
93
94                 case 't':       /* deprecated */
95                         break;
96                 case 'B': /* Basesize */
97                         basesize = atoi(optarg);
98                         break;
99
100                 case 'D':
101                         dbg = 1;
102                         break;
103
104                 /* -r tells the server not to drop root permissions.
105                  * Don't use this unless you know what you're doing.
106                  */
107                 case 'r':
108                         drop_root_perms = 0;
109                         break;
110
111                 /* -u tells the server what uid to run under... */
112                 case 'u':
113                         u = atoi(optarg);
114                         if (u > 0) {
115                                 ctdluid = u;
116                         }
117                         else {
118                                 p = getpwnam(optarg);
119                                 if (p) {
120                                         u = p->pw_uid;
121                                 }
122                         }
123                         if (u > 0) {
124                                 ctdluid = u;
125                         }
126                         break;
127
128                 default:
129                 /* any other parameter makes it crash and burn */
130                         fprintf(stderr, "citserver: usage: "
131                                         "citserver "
132                                         "[-l LogFacility] "
133                                         "[-d] [-D] [-r] "
134                                         "[-u user] "
135                                         "[-h HomeDir]\n"
136                         );
137                         exit(1);
138         }
139
140         /* Last ditch effort to determine the user name ... if there's a user called "citadel" then use that */
141         if (ctdluid == 0) {
142                 p = getpwnam("citadel");
143                 if (!p) {
144                         p = getpwnam("bbs");
145                 }
146                 if (!p) {
147                         p = getpwnam("guest");
148                 }
149                 if (p) {
150                         u = p->pw_uid;
151                 }
152                 if (u > 0) {
153                         ctdluid = u;
154                 }
155         }
156
157         if ((ctdluid == 0) && (drop_root_perms == 0)) {
158                 fprintf(stderr, "citserver: cannot determine user to run as; please specify -r or -u options\n");
159                 exit(CTDLEXIT_UNUSER);
160         }
161
162         StartLibCitadel(basesize);
163         openlog("citserver",
164                 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
165                 syslog_facility
166         );
167
168         calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
169         /* daemonize, if we were asked to */
170         if (running_as_daemon) {
171                 start_daemon(0);
172                 drop_root_perms = 1;
173         }
174
175 #if 0
176  def HAVE_BACKTRACE
177         bzero(&params, sizeof(params));
178         params.filename = file_pid_paniclog;
179         panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
180         params.filep = fopen(file_pid_paniclog, "a+");
181         params.debugLevel = ECRASH_DEBUG_VERBOSE;
182         params.dumpAllThreads = TRUE;
183         params.useBacktraceSymbols = 1;
184         params.signals[0]=SIGSEGV;
185         params.signals[1]=SIGILL;
186         params.signals[2]=SIGBUS;
187         params.signals[3]=SIGABRT;
188         eCrash_Init(&params);
189         eCrash_RegisterThread("MasterThread", 0);
190 #endif
191
192         /* Tell 'em who's in da house */
193         syslog(LOG_NOTICE, " ");
194         syslog(LOG_NOTICE, " ");
195         syslog(LOG_NOTICE,
196                 "*** Citadel server engine v%d.%02d (build %s) ***",
197                 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
198         syslog(LOG_NOTICE, "Copyright (C) 1987-2015 by the Citadel development team.");
199         syslog(LOG_NOTICE, "This program is distributed under the terms of the GNU "
200                                         "General Public License.");
201         syslog(LOG_NOTICE, " ");
202         syslog(LOG_DEBUG, "Called as: %s", argv[0]);
203         syslog(LOG_INFO, "%s", libcitadel_version_string());
204
205         /* Load site-specific configuration */
206         syslog(LOG_INFO, "Loading citadel.config");
207         get_config();
208         validate_config();
209
210         /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
211         syslog(LOG_INFO, "Acquiring control record");
212         get_control();
213
214         put_config();
215
216 #ifdef HAVE_RUN_DIR
217         /* on some dists rundir gets purged on startup. so we need to recreate it. */
218
219         if (stat(ctdl_run_dir, &filestats)==-1){
220 #ifdef HAVE_GETPWUID_R
221 #ifdef SOLARIS_GETPWUID
222                 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
223 #else // SOLARIS_GETPWUID
224                 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
225 #endif // SOLARIS_GETPWUID
226 #else // HAVE_GETPWUID_R
227                 pwp = NULL;
228 #endif // HAVE_GETPWUID_R
229
230                 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST))
231                         syslog(LOG_EMERG, 
232                                       "unable to create run directory [%s]: %s", 
233                                       ctdl_run_dir, strerror(errno));
234
235                 if (chown(ctdl_run_dir, ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0)
236                         syslog(LOG_EMERG, 
237                                       "unable to set the access rights for [%s]: %s", 
238                                       ctdl_run_dir, strerror(errno));
239         }
240                         
241
242 #endif
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, "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(config.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, "Initializing server extensions");
312         
313         initialise_modules(0);
314
315         eDebuglist[1] = getenv("CITADEL_LOGDEBUG");
316         CtdlSetDebugLogFacilities(eDebuglist, 2);
317
318         /*
319          * If we need host auth, start our chkpwd daemon.
320          */
321         if (config.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_CRIT, "WARNING: getpwuid(%ld): %s"
352                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
353                                 strerror(errno));
354                 else {
355                         initgroups(pw.pw_name, pw.pw_gid);
356                         if (setgid(pw.pw_gid))
357                                 syslog(LOG_CRIT, "setgid(%ld): %s", (long)pw.pw_gid,
358                                         strerror(errno));
359                 }
360                 syslog(LOG_INFO, "Changing uid to %ld", (long)CTDLUID);
361                 if (setuid(CTDLUID) != 0) {
362                         syslog(LOG_CRIT, "setuid() failed: %s", strerror(errno));
363                 }
364 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
365                 prctl(PR_SET_DUMPABLE, 1);
366 #endif
367         }
368
369         /* We want to check for idle sessions once per minute */
370         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
371
372         go_threading();
373         
374         master_cleanup(exit_signal);
375         return(0);
376 }