Completed initial code for config system cutover. Not tested yet. Also the import...
[citadel.git] / citadel / citserver.c
1 /* 
2  * Main source module for the Citadel server
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
15 #include <stdio.h>
16 #include "sysdep.h"
17 #include <time.h>
18 #if HAVE_BACKTRACE
19 #include <execinfo.h>
20 #endif
21 #include <libcitadel.h>
22
23 #include "ctdl_module.h"
24 #include "housekeeping.h"
25 #include "locate_host.h"
26 #include "citserver.h"
27 #include "user_ops.h"
28 #include "control.h"
29 #include "config.h"
30
31 char *unique_session_numbers;
32 int ScheduledShutdown = 0;
33 time_t server_startup_time;
34 int panic_fd;
35 int openid_level_supported = 0;
36
37 /*
38  * print the actual stack frame.
39  */
40 void cit_backtrace(void)
41 {
42 #ifdef HAVE_BACKTRACE
43         void *stack_frames[50];
44         size_t size, i;
45         char **strings;
46
47         const char *p = IOSTR;
48         if (p == NULL) p = "";
49         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
50         strings = backtrace_symbols(stack_frames, size);
51         for (i = 0; i < size; i++) {
52                 if (strings != NULL)
53                         syslog(LOG_ALERT, "%s %s\n", p, strings[i]);
54                 else
55                         syslog(LOG_ALERT, "%s %p\n", p, stack_frames[i]);
56         }
57         free(strings);
58 #endif
59 }
60
61 void cit_oneline_backtrace(void)
62 {
63 #ifdef HAVE_BACKTRACE
64         void *stack_frames[50];
65         size_t size, i;
66         char **strings;
67         StrBuf *Buf;
68
69         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
70         strings = backtrace_symbols(stack_frames, size);
71         if (size > 0)
72         {
73                 Buf = NewStrBuf();
74                 for (i = 1; i < size; i++) {
75                         if (strings != NULL)
76                                 StrBufAppendPrintf(Buf, "%s : ", strings[i]);
77                         else
78                                 StrBufAppendPrintf(Buf, "%p : ", stack_frames[i]);
79                 }
80                 free(strings);
81                 syslog(LOG_ALERT, "%s %s\n", IOSTR, ChrPtr(Buf));
82                 FreeStrBuf(&Buf);
83         }
84 #endif
85 }
86
87 /*
88  * print the actual stack frame.
89  */
90 void cit_panic_backtrace(int SigNum)
91 {
92 #ifdef HAVE_BACKTRACE
93         void *stack_frames[10];
94         size_t size, i;
95         char **strings;
96
97         printf("caught signal 11\n");
98         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
99         strings = backtrace_symbols(stack_frames, size);
100         for (i = 0; i < size; i++) {
101                 if (strings != NULL)
102                         syslog(LOG_ALERT, "%s\n", strings[i]);
103                 else
104                         syslog(LOG_ALERT, "%p\n", stack_frames[i]);
105         }
106         free(strings);
107 #endif
108         exit(-1);
109 }
110
111 /*
112  * Various things that need to be initialized at startup
113  */
114 void master_startup(void) {
115         struct timeval tv;
116         unsigned int seed;
117         FILE *urandom;
118         struct ctdlroom qrbuf;
119         int rv;
120         struct passwd *pw;
121         gid_t gid;
122         
123         syslog(LOG_DEBUG, "master_startup() started\n");
124         time(&server_startup_time);
125
126         syslog(LOG_INFO, "Checking directory access");
127         if ((pw = getpwuid(ctdluid)) == NULL) {
128                 gid = getgid();
129         } else {
130                 gid = pw->pw_gid;
131         }
132
133         if (create_run_directories(CTDLUID, gid) != 0) {
134                 syslog(LOG_EMERG, "failed to access & create directories");
135                 exit(1);
136         }
137         syslog(LOG_INFO, "Opening databases");
138         open_databases();
139
140         /* Load site-specific configuration */
141         syslog(LOG_INFO, "Loading citadel.config");
142         initialize_config_system();
143         validate_config();
144
145         syslog(LOG_INFO, "Acquiring control record");
146         get_control();
147
148         /* Check floor reference counts */
149         check_ref_counts();
150
151         syslog(LOG_INFO, "Creating base rooms (if necessary)\n");
152         CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"),  0, "", 0, 1, 0, VIEW_BBS);
153         CtdlCreateRoom(AIDEROOM,                        3, "", 0, 1, 0, VIEW_BBS);
154         CtdlCreateRoom(SYSCONFIGROOM,                   3, "", 0, 1, 0, VIEW_BBS);
155         CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"),  0, "", 0, 1, 0, VIEW_BBS);
156
157         /* The "Local System Configuration" room doesn't need to be visible */
158         if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
159                 qrbuf.QRflags2 |= QR2_SYSTEM;
160                 CtdlPutRoomLock(&qrbuf);
161         }
162
163         /* Aide needs to be public postable, else we're not RFC conformant. */
164         if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
165                 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
166                 CtdlPutRoomLock(&qrbuf);
167         }
168
169         syslog(LOG_INFO, "Seeding the pseudo-random number generator...\n");
170         urandom = fopen("/dev/urandom", "r");
171         if (urandom != NULL) {
172                 rv = fread(&seed, sizeof seed, 1, urandom);
173                 if (rv == -1)
174                         syslog(LOG_EMERG, "failed to read random seed: %s\n", 
175                                strerror(errno));
176                 fclose(urandom);
177         }
178         else {
179                 gettimeofday(&tv, NULL);
180                 seed = tv.tv_usec;
181         }
182         srand(seed);
183         srandom(seed);
184
185         syslog(LOG_DEBUG, "master_startup() finished\n");
186 }
187
188
189 /*
190  * Cleanup routine to be called when the server is shutting down.
191  */
192 void master_cleanup(int exitcode) {
193         struct CleanupFunctionHook *fcn;
194         static int already_cleaning_up = 0;
195
196         if (already_cleaning_up) while(1) usleep(1000000);
197         already_cleaning_up = 1;
198
199         /* Run any cleanup routines registered by loadable modules */
200         for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
201                 (*fcn->h_function_pointer)();
202         }
203
204         /* Close the AdjRefCount queue file */
205         AdjRefCount(-1, 0);
206
207         /* Do system-dependent stuff */
208         sysdep_master_cleanup();
209
210         /* Close the configuration system */
211         shutdown_config_system();
212         
213         /* Close databases */
214         syslog(LOG_INFO, "Closing databases\n");
215         close_databases();
216
217         /* If the operator requested a halt but not an exit, halt here. */
218         if (shutdown_and_halt) {
219                 syslog(LOG_NOTICE, "citserver: Halting server without exiting.\n");
220                 fflush(stdout); fflush(stderr);
221                 while(1) {
222                         sleep(32767);
223                 }
224         }
225         
226         release_control();
227
228         /* Now go away. */
229         syslog(LOG_NOTICE, "citserver: Exiting with status %d\n", exitcode);
230         fflush(stdout); fflush(stderr);
231         
232         if (restart_server != 0)
233                 exit(1);
234         if ((running_as_daemon != 0) && ((exitcode == 0) ))
235                 exitcode = CTDLEXIT_SHUTDOWN;
236         exit(exitcode);
237 }
238
239
240
241 /*
242  * returns an asterisk if there are any instant messages waiting,
243  * space otherwise.
244  */
245 char CtdlCheckExpress(void) {
246         if (CC->FirstExpressMessage == NULL) {
247                 return(' ');
248         }
249         else {
250                 return('*');
251         }
252 }
253
254
255 /*
256  * Check originating host against the public_clients file.  This determines
257  * whether the client is allowed to change the hostname for this session
258  * (for example, to show the location of the user rather than the location
259  * of the client).
260  */
261 int CtdlIsPublicClient(void)
262 {
263         char buf[1024];
264         char addrbuf[1024];
265         FILE *fp;
266         int i;
267         char *public_clientspos;
268         char *public_clientsend;
269         char *paddr = NULL;
270         struct stat statbuf;
271         static time_t pc_timestamp = 0;
272         static char public_clients[SIZ];
273         static char public_clients_file[SIZ];
274
275 #define LOCALHOSTSTR "127.0.0.1"
276
277         snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
278
279         /*
280          * Check the time stamp on the public_clients file.  If it's been
281          * updated since the last time we were here (or if this is the first
282          * time we've been through the loop), read its contents and learn
283          * the IP addresses of the listed hosts.
284          */
285         if (stat(public_clients_file, &statbuf) != 0) {
286                 /* No public_clients file exists, so bail out */
287                 syslog(LOG_WARNING, "Warning: '%s' does not exist\n", 
288                                 public_clients_file);
289                 return(0);
290         }
291
292         if (statbuf.st_mtime > pc_timestamp) {
293                 begin_critical_section(S_PUBLIC_CLIENTS);
294                 syslog(LOG_INFO, "Loading %s\n", public_clients_file);
295
296                 public_clientspos = &public_clients[0];
297                 public_clientsend = public_clientspos + SIZ;
298                 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
299                 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
300                 
301                 if (hostname_to_dotted_quad(addrbuf, CtdlGetConfigStr("c_fqdn")) == 0) {
302                         *(public_clientspos++) = '|';
303                         paddr = &addrbuf[0];
304                         while (!IsEmptyStr (paddr) && 
305                                (public_clientspos < public_clientsend))
306                                 *(public_clientspos++) = *(paddr++);
307                 }
308
309                 fp = fopen(public_clients_file, "r");
310                 if (fp != NULL) 
311                         while ((fgets(buf, sizeof buf, fp)!=NULL) &&
312                                (public_clientspos < public_clientsend)){
313                                 char *ptr;
314                                 ptr = buf;
315                                 while (!IsEmptyStr(ptr)) {
316                                         if (*ptr == '#') {
317                                                 *ptr = 0;
318                                                 break;
319                                         }
320                                 else ptr++;
321                                 }
322                                 ptr--;
323                                 while (ptr>buf && isspace(*ptr)) {
324                                         *(ptr--) = 0;
325                                 }
326                                 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
327                                         *(public_clientspos++) = '|';
328                                         paddr = addrbuf;
329                                         while (!IsEmptyStr(paddr) && 
330                                                (public_clientspos < public_clientsend)){
331                                                 *(public_clientspos++) = *(paddr++);
332                                         }
333                                 }
334                         }
335                 if (fp != NULL) fclose(fp);
336                 pc_timestamp = time(NULL);
337                 end_critical_section(S_PUBLIC_CLIENTS);
338         }
339
340         syslog(LOG_DEBUG, "Checking whether %s is a local or public client\n",
341                 CC->cs_addr);
342         for (i=0; i<num_parms(public_clients); ++i) {
343                 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
344                 if (!strcasecmp(CC->cs_addr, addrbuf)) {
345                         syslog(LOG_DEBUG, "... yes its local.\n");
346                         return(1);
347                 }
348         }
349
350         /* No hits.  This is not a public client. */
351         syslog(LOG_DEBUG, "... no it isn't.\n");
352         return(0);
353 }
354
355
356
357
358
359 void citproto_begin_session() {
360         if (CC->nologin==1) {
361                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
362                         ERROR + MAX_SESSIONS_EXCEEDED,
363                         CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions")
364                 );
365                 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
366         }
367         else {
368                 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
369                 CC->can_receive_im = 1;
370         }
371 }
372
373
374 void citproto_begin_admin_session() {
375         CC->internal_pgm = 1;
376         cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
377 }
378
379
380
381
382 /*
383  * This loop recognizes all server commands.
384  */
385 void do_command_loop(void) {
386         char cmdbuf[SIZ];
387         
388         time(&CC->lastcmd);
389         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
390         if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
391                 syslog(LOG_ERR, "Citadel client disconnected: ending session.\n");
392                 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
393                 return;
394         }
395
396         /* Log the server command, but don't show passwords... */
397         if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
398                 syslog(LOG_INFO, "[%d][%s(%ld)] %s",
399                         CC->cs_pid, CC->curr_user, CC->user.usernum, cmdbuf
400                 );
401         }
402         else {
403                 syslog(LOG_INFO, "[%d][%s(%ld)] <password command hidden from log>",
404                         CC->cs_pid, CC->curr_user, CC->user.usernum
405                 );
406         }
407
408         buffer_output();
409
410         /*
411          * Let other clients see the last command we executed, and
412          * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
413          */
414         if ( (strncasecmp(cmdbuf, "NOOP", 4))
415            && (strncasecmp(cmdbuf, "QNOP", 4))
416            && (strncasecmp(cmdbuf, "PEXP", 4))
417            && (strncasecmp(cmdbuf, "GEXP", 4))
418            && (strncasecmp(cmdbuf, "RWHO", 4))
419            && (strncasecmp(cmdbuf, "TIME", 4)) ) {
420                 strcpy(CC->lastcmdname, "    ");
421                 safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
422                 time(&CC->lastidle);
423         }
424         
425         if ((strncasecmp(cmdbuf, "ENT0", 4))
426            && (strncasecmp(cmdbuf, "MESG", 4))
427            && (strncasecmp(cmdbuf, "MSGS", 4)))
428         {
429            CC->cs_flags &= ~CS_POSTING;
430         }
431                    
432         if (!DLoader_Exec_Cmd(cmdbuf)) {
433                 cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
434         }       
435
436         unbuffer_output();
437
438         /* Run any after-each-command routines registered by modules */
439         PerformSessionHooks(EVT_CMD);
440 }
441
442
443 /*
444  * This loop performs all asynchronous functions.
445  */
446 void do_async_loop(void) {
447         PerformSessionHooks(EVT_ASYNC);
448 }
449