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