user access level helper moved to the other accesslevel helpers: user_ops.c
[citadel.git] / citadel / citserver.c
1 /* 
2  * Main source module for the Citadel server
3  *
4  * Copyright (c) 1987-2011 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 "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
26 # include <time.h>
27 #else
28 # if HAVE_SYS_TIME_H
29 #  include <sys/time.h>
30 # else
31 #  include <time.h>
32 # endif
33 #endif
34
35 #if HAVE_BACKTRACE
36 #include <execinfo.h>
37 #endif
38
39 #include <ctype.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <netdb.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "sysdep_decls.h"
52 #include "threads.h"
53 #include "citserver.h"
54 #include "config.h"
55 #include "database.h"
56 #include "housekeeping.h"
57 #include "user_ops.h"
58 #include "msgbase.h"
59 #include "support.h"
60 #include "locate_host.h"
61 #include "room_ops.h"
62 #include "control.h"
63 #include "euidindex.h"
64 #include "context.h"
65 #include "svn_revision.h"
66 #include "ctdl_module.h"
67
68 char *unique_session_numbers;
69 int ScheduledShutdown = 0;
70 time_t server_startup_time;
71 int panic_fd;
72 int openid_level_supported = 0;
73
74 /*
75  * print the actual stack frame.
76  */
77 void cit_backtrace(void)
78 {
79 #ifdef HAVE_BACKTRACE
80         void *stack_frames[50];
81         size_t size, i;
82         char **strings;
83
84
85         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
86         strings = backtrace_symbols(stack_frames, size);
87         for (i = 0; i < size; i++) {
88                 if (strings != NULL)
89                         syslog(LOG_ALERT, "%s\n", strings[i]);
90                 else
91                         syslog(LOG_ALERT, "%p\n", stack_frames[i]);
92         }
93         free(strings);
94 #endif
95 }
96
97 void cit_oneline_backtrace(void)
98 {
99 #ifdef HAVE_BACKTRACE
100         void *stack_frames[50];
101         size_t size, i;
102         char **strings;
103         StrBuf *Buf;
104
105         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
106         strings = backtrace_symbols(stack_frames, size);
107         if (size > 0)
108         {
109                 Buf = NewStrBuf();
110                 for (i = 1; i < size; i++) {
111                         if (strings != NULL)
112                                 StrBufAppendPrintf(Buf, "%s : ", strings[i]);
113                         else
114                                 StrBufAppendPrintf(Buf, "%p : ", stack_frames[i]);
115                 }
116                 free(strings);
117                 syslog(LOG_ALERT, "%s\n", ChrPtr(Buf));
118                 FreeStrBuf(&Buf);
119         }
120 #endif
121 }
122
123 /*
124  * print the actual stack frame.
125  */
126 void cit_panic_backtrace(int SigNum)
127 {
128 #ifdef HAVE_BACKTRACE
129         void *stack_frames[10];
130         size_t size, i;
131         char **strings;
132
133         printf("caught signal 11\n");
134         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
135         strings = backtrace_symbols(stack_frames, size);
136         for (i = 0; i < size; i++) {
137                 if (strings != NULL)
138                         syslog(LOG_ALERT, "%s\n", strings[i]);
139                 else
140                         syslog(LOG_ALERT, "%p\n", stack_frames[i]);
141         }
142         free(strings);
143 #endif
144         exit(-1);
145 }
146
147 /*
148  * Various things that need to be initialized at startup
149  */
150 void master_startup(void) {
151         struct timeval tv;
152         unsigned int seed;
153         FILE *urandom;
154         struct ctdlroom qrbuf;
155         int rv;
156         
157         syslog(LOG_DEBUG, "master_startup() started\n");
158         time(&server_startup_time);
159         get_config();
160
161         syslog(LOG_INFO, "Opening databases\n");
162         open_databases();
163         check_ref_counts();
164
165         syslog(LOG_INFO, "Creating base rooms (if necessary)\n");
166         CtdlCreateRoom(config.c_baseroom,       0, "", 0, 1, 0, VIEW_BBS);
167         CtdlCreateRoom(AIDEROOM,                3, "", 0, 1, 0, VIEW_BBS);
168         CtdlCreateRoom(SYSCONFIGROOM,           3, "", 0, 1, 0, VIEW_BBS);
169         CtdlCreateRoom(config.c_twitroom,       0, "", 0, 1, 0, VIEW_BBS);
170
171         /* The "Local System Configuration" room doesn't need to be visible */
172         if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
173                 qrbuf.QRflags2 |= QR2_SYSTEM;
174                 CtdlPutRoomLock(&qrbuf);
175         }
176
177         /* Aide needs to be public postable, else we're not RFC conformant. */
178         if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
179                 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
180                 CtdlPutRoomLock(&qrbuf);
181         }
182
183         syslog(LOG_INFO, "Seeding the pseudo-random number generator...\n");
184         urandom = fopen("/dev/urandom", "r");
185         if (urandom != NULL) {
186                 rv = fread(&seed, sizeof seed, 1, urandom);
187                 if (rv == -1)
188                         syslog(LOG_EMERG, "failed to read random seed: %s\n", 
189                                strerror(errno));
190                 fclose(urandom);
191         }
192         else {
193                 gettimeofday(&tv, NULL);
194                 seed = tv.tv_usec;
195         }
196         srand(seed);
197         srandom(seed);
198
199         put_config();
200
201         syslog(LOG_DEBUG, "master_startup() finished\n");
202 }
203
204
205 /*
206  * Cleanup routine to be called when the server is shutting down.
207  */
208 void master_cleanup(int exitcode) {
209         struct CleanupFunctionHook *fcn;
210         static int already_cleaning_up = 0;
211
212         if (already_cleaning_up) while(1) usleep(1000000);
213         already_cleaning_up = 1;
214
215         /* Run any cleanup routines registered by loadable modules */
216         for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
217                 (*fcn->h_function_pointer)();
218         }
219
220         /* Close the AdjRefCount queue file */
221         AdjRefCount(-1, 0);
222
223         /* Do system-dependent stuff */
224         sysdep_master_cleanup();
225         
226         /* Close databases */
227         syslog(LOG_INFO, "Closing databases\n");
228         close_databases();
229
230         /* If the operator requested a halt but not an exit, halt here. */
231         if (shutdown_and_halt) {
232                 syslog(LOG_NOTICE, "citserver: Halting server without exiting.\n");
233                 fflush(stdout); fflush(stderr);
234                 while(1) {
235                         sleep(32767);
236                 }
237         }
238         
239         release_control();
240
241         /* Now go away. */
242         syslog(LOG_NOTICE, "citserver: Exiting with status %d\n", exitcode);
243         fflush(stdout); fflush(stderr);
244         
245         if (restart_server != 0)
246                 exit(1);
247         if ((running_as_daemon != 0) && ((exitcode == 0) ))
248                 exitcode = CTDLEXIT_SHUTDOWN;
249         exit(exitcode);
250 }
251
252
253
254 /*
255  * returns an asterisk if there are any instant messages waiting,
256  * space otherwise.
257  */
258 char CtdlCheckExpress(void) {
259         if (CC->FirstExpressMessage == NULL) {
260                 return(' ');
261         }
262         else {
263                 return('*');
264         }
265 }
266
267
268 /*
269  * Check originating host against the public_clients file.  This determines
270  * whether the client is allowed to change the hostname for this session
271  * (for example, to show the location of the user rather than the location
272  * of the client).
273  */
274 int CtdlIsPublicClient(void)
275 {
276         char buf[1024];
277         char addrbuf[1024];
278         FILE *fp;
279         int i;
280         char *public_clientspos;
281         char *public_clientsend;
282         char *paddr = NULL;
283         struct stat statbuf;
284         static time_t pc_timestamp = 0;
285         static char public_clients[SIZ];
286         static char public_clients_file[SIZ];
287
288 #define LOCALHOSTSTR "127.0.0.1"
289
290         snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
291
292         /*
293          * Check the time stamp on the public_clients file.  If it's been
294          * updated since the last time we were here (or if this is the first
295          * time we've been through the loop), read its contents and learn
296          * the IP addresses of the listed hosts.
297          */
298         if (stat(public_clients_file, &statbuf) != 0) {
299                 /* No public_clients file exists, so bail out */
300                 syslog(LOG_WARNING, "Warning: '%s' does not exist\n", 
301                                 public_clients_file);
302                 return(0);
303         }
304
305         if (statbuf.st_mtime > pc_timestamp) {
306                 begin_critical_section(S_PUBLIC_CLIENTS);
307                 syslog(LOG_INFO, "Loading %s\n", public_clients_file);
308
309                 public_clientspos = &public_clients[0];
310                 public_clientsend = public_clientspos + SIZ;
311                 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
312                 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
313                 
314                 if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
315                         *(public_clientspos++) = '|';
316                         paddr = &addrbuf[0];
317                         while (!IsEmptyStr (paddr) && 
318                                (public_clientspos < public_clientsend))
319                                 *(public_clientspos++) = *(paddr++);
320                 }
321
322                 fp = fopen(public_clients_file, "r");
323                 if (fp != NULL) 
324                         while ((fgets(buf, sizeof buf, fp)!=NULL) &&
325                                (public_clientspos < public_clientsend)){
326                                 char *ptr;
327                                 ptr = buf;
328                                 while (!IsEmptyStr(ptr)) {
329                                         if (*ptr == '#') {
330                                                 *ptr = 0;
331                                                 break;
332                                         }
333                                 else ptr++;
334                                 }
335                                 ptr--;
336                                 while (ptr>buf && isspace(*ptr)) {
337                                         *(ptr--) = 0;
338                                 }
339                                 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
340                                         *(public_clientspos++) = '|';
341                                         paddr = addrbuf;
342                                         while (!IsEmptyStr(paddr) && 
343                                                (public_clientspos < public_clientsend)){
344                                                 *(public_clientspos++) = *(paddr++);
345                                         }
346                                 }
347                         }
348                 if (fp != NULL) fclose(fp);
349                 pc_timestamp = time(NULL);
350                 end_critical_section(S_PUBLIC_CLIENTS);
351         }
352
353         syslog(LOG_DEBUG, "Checking whether %s is a local or public client\n",
354                 CC->cs_addr);
355         for (i=0; i<num_parms(public_clients); ++i) {
356                 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
357                 if (!strcasecmp(CC->cs_addr, addrbuf)) {
358                         syslog(LOG_DEBUG, "... yes its local.\n");
359                         return(1);
360                 }
361         }
362
363         /* No hits.  This is not a public client. */
364         syslog(LOG_DEBUG, "... no it isn't.\n");
365         return(0);
366 }
367
368
369
370
371
372 /*
373  * Back-end function for starting a session
374  */
375 void begin_session(CitContext *con)
376 {
377         /* 
378          * Initialize some variables specific to our context.
379          */
380         con->logged_in = 0;
381         con->internal_pgm = 0;
382         con->download_fp = NULL;
383         con->upload_fp = NULL;
384         con->cached_msglist = NULL;
385         con->cached_num_msgs = 0;
386         con->FirstExpressMessage = NULL;
387         time(&con->lastcmd);
388         time(&con->lastidle);
389         strcpy(con->lastcmdname, "    ");
390         strcpy(con->cs_clientname, "(unknown)");
391         strcpy(con->curr_user, NLI);
392         *con->net_node = '\0';
393         *con->fake_username = '\0';
394         *con->fake_hostname = '\0';
395         *con->fake_roomname = '\0';
396         *con->cs_clientinfo = '\0';
397         safestrncpy(con->cs_host, config.c_fqdn, sizeof con->cs_host);
398         safestrncpy(con->cs_addr, "", sizeof con->cs_addr);
399         con->cs_UDSclientUID = -1;
400         con->cs_host[sizeof con->cs_host - 1] = 0;
401         if (!CC->is_local_socket) {
402                 locate_host(con->cs_host, sizeof con->cs_host,
403                         con->cs_addr, sizeof con->cs_addr,
404                         con->client_socket
405                 );
406         }
407         else {
408                 con->cs_host[0] = 0;
409                 con->cs_addr[0] = 0;
410 #ifdef HAVE_STRUCT_UCRED
411                 {
412                         /* as http://www.wsinnovations.com/softeng/articles/uds.html told us... */
413                         struct ucred credentials;
414                         socklen_t ucred_length = sizeof(struct ucred);
415                         
416                         /*fill in the user data structure */
417                         if(getsockopt(con->client_socket, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length)) {
418                                 syslog(LOG_NOTICE, "could obtain credentials from unix domain socket");
419                                 
420                         }
421                         else {          
422                                 /* the process ID of the process on the other side of the socket */
423                                 /* credentials.pid; */
424                                 
425                                 /* the effective UID of the process on the other side of the socket  */
426                                 con->cs_UDSclientUID = credentials.uid;
427                                 
428                                 /* the effective primary GID of the process on the other side of the socket */
429                                 /* credentials.gid; */
430                                 
431                                 /* To get supplemental groups, we will have to look them up in our account
432                                    database, after a reverse lookup on the UID to get the account name.
433                                    We can take this opportunity to check to see if this is a legit account.
434                                 */
435                                 snprintf(con->cs_clientinfo, sizeof(con->cs_clientinfo),
436                                          "PID: "F_PID_T"; UID: "F_UID_T"; GID: "F_XPID_T" ", 
437                                          credentials.pid,
438                                          credentials.uid,
439                                          credentials.gid);
440                         }
441                 }
442 #endif
443         }
444         con->cs_flags = 0;
445         con->upload_type = UPL_FILE;
446         con->dl_is_net = 0;
447
448         con->nologin = 0;
449         if (((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions)) || CtdlWantSingleUser()) {
450                 con->nologin = 1;
451         }
452
453         if (!CC->is_local_socket) {
454                 syslog(LOG_NOTICE, "Session (%s) started from %s (%s).\n", con->ServiceName, con->cs_host, con->cs_addr);
455         }
456         else {
457                 syslog(LOG_NOTICE, "Session (%s) started via local socket UID:%d.\n", con->ServiceName, con->cs_UDSclientUID);
458         }
459
460         /* Run any session startup routines registered by loadable modules */
461         PerformSessionHooks(EVT_START);
462 }
463
464
465 void citproto_begin_session() {
466         if (CC->nologin==1) {
467                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
468                         ERROR + MAX_SESSIONS_EXCEEDED,
469                         config.c_nodename, config.c_maxsessions
470                 );
471                 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
472         }
473         else {
474                 cprintf("%d %s Citadel server ready.\n", CIT_OK, config.c_nodename);
475                 CC->can_receive_im = 1;
476         }
477 }
478
479
480 void citproto_begin_admin_session() {
481         CC->internal_pgm = 1;
482         cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, config.c_nodename);
483 }
484
485
486
487
488 /*
489  * This loop recognizes all server commands.
490  */
491 void do_command_loop(void) {
492         char cmdbuf[SIZ];
493         
494         time(&CC->lastcmd);
495         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
496         if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
497                 syslog(LOG_ERR, "Citadel client disconnected: ending session.\n");
498                 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
499                 return;
500         }
501
502         /* Log the server command, but don't show passwords... */
503         if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
504                 syslog(LOG_INFO, "[%d][%s(%ld)] %s",
505                         CC->cs_pid, CC->curr_user, CC->user.usernum, cmdbuf
506                 );
507         }
508         else {
509                 syslog(LOG_INFO, "[%d][%s(%ld)] <password command hidden from log>",
510                         CC->cs_pid, CC->curr_user, CC->user.usernum
511                 );
512         }
513
514         buffer_output();
515
516         /*
517          * Let other clients see the last command we executed, and
518          * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
519          */
520         if ( (strncasecmp(cmdbuf, "NOOP", 4))
521            && (strncasecmp(cmdbuf, "QNOP", 4))
522            && (strncasecmp(cmdbuf, "PEXP", 4))
523            && (strncasecmp(cmdbuf, "GEXP", 4))
524            && (strncasecmp(cmdbuf, "RWHO", 4))
525            && (strncasecmp(cmdbuf, "TIME", 4)) ) {
526                 strcpy(CC->lastcmdname, "    ");
527                 safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
528                 time(&CC->lastidle);
529         }
530         
531         if ((strncasecmp(cmdbuf, "ENT0", 4))
532            && (strncasecmp(cmdbuf, "MESG", 4))
533            && (strncasecmp(cmdbuf, "MSGS", 4)))
534         {
535            CC->cs_flags &= ~CS_POSTING;
536         }
537                    
538         if (!DLoader_Exec_Cmd(cmdbuf)) {
539                 cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
540         }       
541
542         unbuffer_output();
543
544         /* Run any after-each-command routines registered by modules */
545         PerformSessionHooks(EVT_CMD);
546 }
547
548
549 /*
550  * This loop performs all asynchronous functions.
551  */
552 void do_async_loop(void) {
553         PerformSessionHooks(EVT_ASYNC);
554 }
555