citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
citserver.c
Go to the documentation of this file.
1// Main source module for the Citadel server
2//
3// Copyright (c) 1987-2024 by the citadel.org team
4//
5// This program is open source software. Use, duplication, or disclosure
6// is subject to the terms of the GNU General Public License, version 3.
7
8#include <stdlib.h>
9#include <unistd.h>
10#include <stdio.h>
11#include <sys/stat.h>
12#include "sysdep.h"
13#include <time.h>
14#include <sys/time.h>
15#include <libcitadel.h>
16#include "ctdl_module.h"
17#include "housekeeping.h"
18#include "locate_host.h"
19#include "citserver.h"
20#include "user_ops.h"
21#include "control.h"
22#include "config.h"
23
28
29
30// We need pseudo-random numbers for a few things. Seed generously.
32 FILE *urandom;
33 struct timeval tv;
34 unsigned int seed;
35
36 syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
37 urandom = fopen("/dev/urandom", "r");
38 if (urandom != NULL) {
39 if (fread(&seed, sizeof seed, 1, urandom) == -1) {
40 syslog(LOG_ERR, "citserver: failed to read random seed: %m");
41 }
42 fclose(urandom);
43 }
44 else {
45 gettimeofday(&tv, NULL);
46 seed = tv.tv_usec;
47 }
48 srand(seed);
49 srandom(seed);
50}
51
52
53// Various things that need to be initialized at startup
54void master_startup(void) {
55 struct ctdlroom qrbuf;
56 struct passwd *pw;
57 gid_t gid;
58
59 syslog(LOG_DEBUG, "master_startup() started");
61
62 syslog(LOG_INFO, "Checking directory access");
63 if ((pw = getpwuid(ctdluid)) == NULL) {
64 gid = getgid();
65 }
66 else {
67 gid = pw->pw_gid;
68 }
69
70 if (create_run_directories(ctdluid, gid) != 0) {
71 syslog(LOG_ERR, "citserver: failed to access and create directories");
72 exit(1);
73 }
74
75 syslog(LOG_DEBUG, "citserver: ctdl_message_dir is %s", ctdl_message_dir);
76 syslog(LOG_DEBUG, "citserver: ctdl_file_dir is %s", ctdl_file_dir);
77 syslog(LOG_DEBUG, "citserver: ctdl_key_dir is %s", ctdl_key_dir);
78 syslog(LOG_DEBUG, "citserver: ctdl_run_dir is %s", ctdl_run_dir);
79
80 syslog(LOG_INFO, "Opening databases");
83
84 // Load site-specific configuration
85 seed_random_number_generator(); // must be done before config system
86 syslog(LOG_INFO, "Initializing configuration system");
90
91 // If we have an existing database that is older than version 928, reindex the user records.
92 // Unfortunately we cannot do this in serv_upgrade.c because it needs to happen VERY early during startup.
93 int existing_db = CtdlGetConfigInt("MM_hosted_upgrade_level");
94 if ( (existing_db > 0) && (existing_db < 928) ) {
96 }
97
98 // Check floor reference counts
100
101 syslog(LOG_INFO, "Creating base rooms (if necessary)");
102 CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
103 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
104 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
105 CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
106
107 // The "Local System Configuration" room doesn't need to be visible
108 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
109 qrbuf.QRflags2 |= QR2_SYSTEM;
110 CtdlPutRoomLock(&qrbuf);
111 }
112
113 // Aide needs to be public postable, else we're not RFC conformant.
114 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
115 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
116 CtdlPutRoomLock(&qrbuf);
117 }
118
119 syslog(LOG_DEBUG, "master_startup() finished");
120}
121
122
123// Cleanup routine to be called when the server is shutting down. Returns the needed exit code.
124void master_cleanup(int exitcode) {
125 static int already_cleaning_up = 0;
126
127 if (already_cleaning_up) {
128 while (1) {
129 usleep(1000000);
130 }
131 }
132 already_cleaning_up = 1;
133
134 // Close the sockets
136
137 // Close the configuration system
139
140 // Close databases
141 syslog(LOG_INFO, "citserver: closing databases");
143
144 // If the operator requested a halt but not an exit, halt here.
145 if (shutdown_and_halt) {
146 syslog(LOG_ERR, "citserver: Halting server without exiting.");
147 fflush(stdout);
148 fflush(stderr);
149 while (1) {
150 sleep(32767);
151 }
152 }
153
154 // Now go away.
155 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
156 fflush(stdout);
157 fflush(stderr);
158
159 if ((running_as_daemon != 0) && ((exitcode == 0))) {
160 exitcode = CTDLEXIT_SHUTDOWN;
161 }
162
163 ctdl_lockfile(0);
164 exit(exitcode);
165}
166
167
168// returns an asterisk if there are any instant messages waiting, space otherwise.
169char CtdlCheckExpress(struct CitContext *con) {
170 if (con->FirstExpressMessage == NULL) {
171 return (' ');
172 }
173 else {
174 return ('*');
175 }
176}
177
178
180 if (CC->nologin == 1) {
181 cprintf("%d Too many users are already online (maximum is %d)\n",
183 );
185 }
186 else {
187 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
188 strcpy(CC->cs_clientname, "Citadel client protocol");
189 CC->can_receive_im = 1;
190 }
191}
192
193
195 CC->internal_pgm = 1;
196 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
197}
198
199
200// This loop performs all asynchronous functions.
@ KILLME_MAX_SESSIONS_EXCEEDED
#define CTDLEXIT_SHUTDOWN
@ EVT_ASYNC
int create_run_directories(long UID, long GID)
#define ctdl_message_dir
#define ctdl_run_dir
#define ctdl_file_dir
#define ctdl_key_dir
time_t server_startup_time
Definition citserver.c:26
char CtdlCheckExpress(struct CitContext *con)
Definition citserver.c:169
void master_startup(void)
Definition citserver.c:54
void citproto_begin_session()
Definition citserver.c:179
int ScheduledShutdown
Definition citserver.c:25
void master_cleanup(int exitcode)
Definition citserver.c:124
int panic_fd
Definition citserver.c:27
char * unique_session_numbers
Definition citserver.c:24
void do_async_loop(void)
Definition citserver.c:201
void seed_random_number_generator(void)
Definition citserver.c:31
void citproto_begin_admin_session()
Definition citserver.c:194
void ctdl_lockfile(int)
Definition server_main.c:33
char * CtdlGetConfigStr(char *key)
Definition config.c:360
void shutdown_config_system(void)
Definition config.c:300
void initialize_config_system(void)
Definition config.c:212
int CtdlGetConfigInt(char *key)
Definition config.c:386
void validate_config(void)
Definition config.c:39
void context_cleanup(void)
Definition context.c:462
#define CC
Definition context.h:131
void migrate_legacy_control_record(void)
Definition control.c:92
int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name)
Definition room_ops.c:339
unsigned CtdlCreateRoom(char *new_room_name, int new_room_type, char *new_room_pass, int new_room_floor, int really_create, int avoid_access, int new_room_view)
Definition room_ops.c:1024
void CtdlPutRoomLock(struct ctdlroom *qrbuf)
Definition room_ops.c:383
void(* cdb_open_databases)(void)
Definition database.c:27
void(* cdb_close_databases)(void)
Definition database.c:28
void cdb_init_backends(void)
Definition database.c:45
void check_ref_counts(void)
#define CIT_OK
Definition ipcdef.h:2
#define QR2_SMTP_PUBLIC
Definition ipcdef.h:55
#define ERROR
Definition ipcdef.h:5
#define MAX_SESSIONS_EXCEEDED
Definition ipcdef.h:20
#define QR2_SYSTEM
Definition ipcdef.h:51
void PerformSessionHooks(int EventType)
uid_t ctdluid
Definition server_main.c:24
struct ExpressMessage * FirstExpressMessage
Definition context.h:105
unsigned QRflags2
Definition server.h:188
#define AIDEROOM
Definition sysconfig.h:40
#define SYSCONFIGROOM
Definition sysconfig.h:49
void cprintf(const char *format,...)
Definition sysdep.c:363
volatile int shutdown_and_halt
Definition sysdep.c:39
volatile int running_as_daemon
Definition sysdep.c:41
void ForEachUser(void(*CallBack)(char *, void *out_data), void *in_data)
Definition user_ops.c:1032
void reindex_user_928(char *username, void *out_data)
Definition user_ops.c:177