remove unused trace function
[citadel.git] / citadel / citserver.c
1 /* 
2  * Main source module for the Citadel server
3  *
4  * Copyright (c) 1987-2017 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 <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <sys/stat.h>
19 #include "sysdep.h"
20 #include <time.h>
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 /*
39  * Various things that need to be initialized at startup
40  */
41 void master_startup(void) {
42         struct timeval tv;
43         unsigned int seed;
44         FILE *urandom;
45         struct ctdlroom qrbuf;
46         int rv;
47         struct passwd *pw;
48         gid_t gid;
49         
50         syslog(LOG_DEBUG, "master_startup() started");
51         time(&server_startup_time);
52
53         syslog(LOG_INFO, "Checking directory access");
54         if ((pw = getpwuid(ctdluid)) == NULL) {
55                 gid = getgid();
56         } else {
57                 gid = pw->pw_gid;
58         }
59
60         if (create_run_directories(CTDLUID, gid) != 0) {
61                 syslog(LOG_EMERG, "failed to access & create directories");
62                 exit(1);
63         }
64         syslog(LOG_INFO, "Opening databases");
65         open_databases();
66
67         /* Load site-specific configuration */
68         syslog(LOG_INFO, "Initializing configuration system");
69         initialize_config_system();
70         validate_config();
71         migrate_legacy_control_record();
72
73         /* Check floor reference counts */
74         check_ref_counts();
75
76         syslog(LOG_INFO, "Creating base rooms (if necessary)");
77         CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"),  0, "", 0, 1, 0, VIEW_BBS);
78         CtdlCreateRoom(AIDEROOM,                        3, "", 0, 1, 0, VIEW_BBS);
79         CtdlCreateRoom(SYSCONFIGROOM,                   3, "", 0, 1, 0, VIEW_BBS);
80         CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"),  0, "", 0, 1, 0, VIEW_BBS);
81
82         /* The "Local System Configuration" room doesn't need to be visible */
83         if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
84                 qrbuf.QRflags2 |= QR2_SYSTEM;
85                 CtdlPutRoomLock(&qrbuf);
86         }
87
88         /* Aide needs to be public postable, else we're not RFC conformant. */
89         if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
90                 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
91                 CtdlPutRoomLock(&qrbuf);
92         }
93
94         syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
95         urandom = fopen("/dev/urandom", "r");
96         if (urandom != NULL) {
97                 rv = fread(&seed, sizeof seed, 1, urandom);
98                 if (rv == -1) {
99                         syslog(LOG_ERR, "citserver: failed to read random seed: %m");
100                 }
101                 fclose(urandom);
102         }
103         else {
104                 gettimeofday(&tv, NULL);
105                 seed = tv.tv_usec;
106         }
107         srand(seed);
108         srandom(seed);
109
110         syslog(LOG_DEBUG, "master_startup() finished");
111 }
112
113
114 /*
115  * Cleanup routine to be called when the server is shutting down.  Returns the needed exit code.
116  */
117 int master_cleanup(int exitcode) {
118         struct CleanupFunctionHook *fcn;
119         static int already_cleaning_up = 0;
120
121         if (already_cleaning_up) while(1) usleep(1000000);
122         already_cleaning_up = 1;
123
124         /* Run any cleanup routines registered by loadable modules */
125         for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
126                 (*fcn->h_function_pointer)();
127         }
128
129         /* Close the AdjRefCount queue file */
130         AdjRefCount(-1, 0);
131
132         /* Do system-dependent stuff */
133         sysdep_master_cleanup();
134
135         /* Close the configuration system */
136         shutdown_config_system();
137         
138         /* Close databases */
139         syslog(LOG_INFO, "Closing databases\n");
140         close_databases();
141
142         /* If the operator requested a halt but not an exit, halt here. */
143         if (shutdown_and_halt) {
144                 syslog(LOG_ERR, "citserver: Halting server without exiting.");
145                 fflush(stdout); fflush(stderr);
146                 while(1) {
147                         sleep(32767);
148                 }
149         }
150         
151         /* Now go away. */
152         syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
153         fflush(stdout); fflush(stderr);
154         
155         if (restart_server != 0) {
156                 exitcode = 1;
157         }
158         else if ((running_as_daemon != 0) && ((exitcode == 0) )) {
159                 exitcode = CTDLEXIT_SHUTDOWN;
160         }
161         return(exitcode);
162 }
163
164
165
166 /*
167  * returns an asterisk if there are any instant messages waiting,
168  * space otherwise.
169  */
170 char CtdlCheckExpress(void) {
171         if (CC->FirstExpressMessage == NULL) {
172                 return(' ');
173         }
174         else {
175                 return('*');
176         }
177 }
178
179
180 /*
181  * Check originating host against the public_clients file.  This determines
182  * whether the client is allowed to change the hostname for this session
183  * (for example, to show the location of the user rather than the location
184  * of the client).
185  */
186 int CtdlIsPublicClient(void)
187 {
188         char buf[1024];
189         char addrbuf[1024];
190         FILE *fp;
191         int i;
192         char *public_clientspos;
193         char *public_clientsend;
194         char *paddr = NULL;
195         struct stat statbuf;
196         static time_t pc_timestamp = 0;
197         static char public_clients[SIZ];
198         static char public_clients_file[SIZ];
199
200 #define LOCALHOSTSTR "127.0.0.1"
201
202         snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
203
204         /*
205          * Check the time stamp on the public_clients file.  If it's been
206          * updated since the last time we were here (or if this is the first
207          * time we've been through the loop), read its contents and learn
208          * the IP addresses of the listed hosts.
209          */
210         if (stat(public_clients_file, &statbuf) != 0) {
211                 /* No public_clients file exists, so bail out */
212                 syslog(LOG_WARNING, "Warning: '%s' does not exist", public_clients_file);
213                 return(0);
214         }
215
216         if (statbuf.st_mtime > pc_timestamp) {
217                 begin_critical_section(S_PUBLIC_CLIENTS);
218                 syslog(LOG_INFO, "Loading %s", public_clients_file);
219
220                 public_clientspos = &public_clients[0];
221                 public_clientsend = public_clientspos + SIZ;
222                 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
223                 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
224                 
225                 if (hostname_to_dotted_quad(addrbuf, CtdlGetConfigStr("c_fqdn")) == 0) {
226                         *(public_clientspos++) = '|';
227                         paddr = &addrbuf[0];
228                         while (!IsEmptyStr (paddr) && 
229                                (public_clientspos < public_clientsend))
230                                 *(public_clientspos++) = *(paddr++);
231                 }
232
233                 fp = fopen(public_clients_file, "r");
234                 if (fp != NULL) 
235                         while ((fgets(buf, sizeof buf, fp)!=NULL) &&
236                                (public_clientspos < public_clientsend)){
237                                 char *ptr;
238                                 ptr = buf;
239                                 while (!IsEmptyStr(ptr)) {
240                                         if (*ptr == '#') {
241                                                 *ptr = 0;
242                                                 break;
243                                         }
244                                 else ptr++;
245                                 }
246                                 ptr--;
247                                 while (ptr>buf && isspace(*ptr)) {
248                                         *(ptr--) = 0;
249                                 }
250                                 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
251                                         *(public_clientspos++) = '|';
252                                         paddr = addrbuf;
253                                         while (!IsEmptyStr(paddr) && 
254                                                (public_clientspos < public_clientsend)){
255                                                 *(public_clientspos++) = *(paddr++);
256                                         }
257                                 }
258                         }
259                 if (fp != NULL) fclose(fp);
260                 pc_timestamp = time(NULL);
261                 end_critical_section(S_PUBLIC_CLIENTS);
262         }
263
264         syslog(LOG_DEBUG, "Checking whether %s is a local or public client", CC->cs_addr);
265         for (i=0; i<num_parms(public_clients); ++i) {
266                 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
267                 if (!strcasecmp(CC->cs_addr, addrbuf)) {
268                         syslog(LOG_DEBUG, "... yes its local.");
269                         return(1);
270                 }
271         }
272
273         /* No hits.  This is not a public client. */
274         syslog(LOG_DEBUG, "... no it isn't.");
275         return(0);
276 }
277
278
279
280
281
282 void citproto_begin_session() {
283         if (CC->nologin==1) {
284                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
285                         ERROR + MAX_SESSIONS_EXCEEDED,
286                         CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions")
287                 );
288                 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
289         }
290         else {
291                 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
292                 CC->can_receive_im = 1;
293         }
294 }
295
296
297 void citproto_begin_admin_session() {
298         CC->internal_pgm = 1;
299         cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
300 }
301
302
303 /*
304  * This loop performs all asynchronous functions.
305  */
306 void do_async_loop(void) {
307         PerformSessionHooks(EVT_ASYNC);
308 }
309