12616070d67e0b4a390fd02a63e5a616a9b8c440
[citadel.git] / citadel / server / server.h
1 // Main declarations file for the Citadel server
2 //
3 // Copyright (c) 1987-2023 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 #ifndef SERVER_H
9 #define SERVER_H
10
11 #ifdef __GNUC__
12 #define INLINE __inline__
13 #else
14 #define INLINE
15 #endif
16
17 #include "citadel.h"
18 #ifdef HAVE_OPENSSL
19 #define OPENSSL_NO_KRB5                 // work around redhat b0rken ssl headers
20 #include <openssl/ssl.h>
21 #endif
22
23 // New format for a message in memory
24 struct CtdlMessage {
25         int cm_magic;                   // Self-check (NOT SAVED TO DISK)
26         char cm_anon_type;              // Anonymous or author-visible
27         char cm_format_type;            // Format type
28         char *cm_fields[256];           // Data fields
29         long cm_lengths[256];           // size of datafields
30         unsigned int cm_flags;          // How to handle (NOT SAVED TO DISK)
31 };
32
33 #define CTDLMESSAGE_MAGIC       0x159d
34 #define CM_SKIP_HOOKS           0x01    // Don't run server-side handlers
35
36
37 // Data structure returned by validate_recipients()
38 struct recptypes {
39         int recptypes_magic;
40         int num_local;
41         int num_internet;
42         int num_room;
43         int num_error;
44         char *errormsg;
45         char *recp_local;
46         char *recp_internet;
47         char *recp_room;
48         char *recp_orgroom;
49         char *display_recp;
50         char *bounce_to;
51         char *envelope_from;
52         char *sending_room;
53 };
54
55 #define RECPTYPES_MAGIC 0xfeeb
56
57 #define CTDLEXIT_SHUTDOWN       0       // Normal shutdown; do NOT auto-restart
58
59 // Exit codes 101 through 109 are used for conditions in which
60 // we deliberately do NOT want the service to automatically
61 // restart.
62 #define CTDLEXIT_CONFIG         101     // Could not read system configuration
63 #define CTDLEXIT_HOME           103     // Citadel home directory not found
64 #define CTDLEXIT_DB             105     // Unable to initialize database
65 #define CTDLEXIT_LIBCITADEL     106     // Incorrect version of libcitadel
66 #define CTDL_EXIT_UNSUP_AUTH    107     // Unsupported auth mode configured
67 #define CTDLEXIT_UNUSER         108     // Could not determine uid to run as
68 #define CTDLEXIT_CRYPTO         109     // Problem initializing SSL or TLS
69
70 // Any other exit is likely to be from an unexpected abort (segfault etc)
71 // and we want to try restarting.
72
73
74
75 // Reasons why a session would be terminated (set CC->kill_me to these values)
76 enum {
77         KILLME_NOT,
78         KILLME_UNKNOWN,
79         KILLME_CLIENT_LOGGED_OUT,
80         KILLME_IDLE,
81         KILLME_CLIENT_DISCONNECTED,
82         KILLME_AUTHFAILED,
83         KILLME_SERVER_SHUTTING_DOWN,
84         KILLME_MAX_SESSIONS_EXCEEDED,
85         KILLME_ADMIN_TERMINATE,
86         KILLME_SELECT_INTERRUPTED,
87         KILLME_SELECT_FAILED,
88         KILLME_WRITE_FAILED,
89         KILLME_SIMULATION_WORKER,
90         KILLME_NOLOGIN,
91         KILLME_NO_CRYPTO,
92         KILLME_READSTRING_FAILED,
93         KILLME_MALLOC_FAILED,
94         KILLME_QUOTA,
95         KILLME_READ_FAILED,
96         KILLME_SPAMMER,
97         KILLME_XML_PARSER
98 };
99
100
101 #define CS_STEALTH      1       // stealth mode
102 #define CS_CHAT         2       // chat mode
103 #define CS_POSTING      4       // posting
104
105 extern int ScheduledShutdown;
106 extern uid_t ctdluid;
107 extern int sanity_diag_mode;
108
109 struct ExpressMessage {
110         struct ExpressMessage *next;
111         time_t timestamp;       // When this message was sent
112         unsigned flags;         // Special instructions
113         char sender[256];       // Name of sending user
114         char sender_email[256]; // Email or JID of sending user
115         char *text;             // Message text (if applicable)
116 };
117
118 #define EM_BROADCAST    1       // Broadcast message
119 #define EM_GO_AWAY      2       // Server requests client log off
120 #define EM_CHAT         4       // Server requests client enter chat
121
122 // Various things we need to lock and unlock
123 enum {
124         S_USERS,
125         S_ROOMS,
126         S_SESSION_TABLE,
127         S_FLOORTAB,
128         S_CHATQUEUE,
129         S_CONTROL,
130         S_SUPPMSGMAIN,
131         S_CONFIG,
132         S_HOUSEKEEPING,
133         S_NETCONFIGS,
134         S_FLOORCACHE,
135         S_ATBF,
136         S_JOURNAL_QUEUE,
137         S_CHKPWD,
138         S_XMPP_QUEUE,
139         S_SINGLE_USER,
140         S_IM_LOGS,
141         S_OPENSSL,
142         S_SMTPQUEUE,
143         MAX_SEMAPHORES
144 };
145
146
147 // message transfer formats
148 enum {
149         MT_CITADEL,             // Citadel proprietary
150         MT_RFC822,              // RFC822
151         MT_MIME,                // MIME-formatted message
152         MT_DOWNLOAD,            // Download a component
153         MT_SPEW_SECTION         // Download a component in a single operation
154 };
155
156 // Message format types in the database
157 #define FMT_CITADEL     0       // Citadel vari-format (proprietary)
158 #define FMT_FIXED       1       // Fixed format (proprietary)
159 #define FMT_RFC822      4       // Standard (headers are in M field)
160
161
162 // citadel database tables (define one for each cdb we need to open)
163 enum {
164         CDB_MSGMAIN,            // message base
165         CDB_USERS,              // user file
166         CDB_ROOMS,              // room index
167         CDB_FLOORTAB,           // floor index
168         CDB_MSGLISTS,           // room message lists
169         CDB_VISIT,              // user/room relationships
170         CDB_DIRECTORY,          // address book directory
171         CDB_USETABLE,           // network use table
172         CDB_BIGMSGS,            // larger message bodies
173         CDB_FULLTEXT,           // full text search index
174         CDB_EUIDINDEX,          // locate msgs by EUID
175         CDB_USERSBYNUMBER,      // index of users by number
176         CDB_EXTAUTH,            // associates OpenIDs with users
177         CDB_CONFIG,             // system configuration database
178         MAXCDB                  // total number of CDB's defined
179 };
180
181 struct cdbdata {
182         size_t len;
183         char *ptr;
184 };
185
186
187 // Event types for hooks
188 enum {
189         EVT_STOP,               // Session is terminating
190         EVT_START,              // Session is starting
191         EVT_LOGIN,              // A user is logging in
192         EVT_NEWROOM,            // Changing rooms
193         EVT_LOGOUT,             // A user is logging out
194         EVT_SETPASS,            // Setting or changing password
195         EVT_CMD,                // Called after each server command
196         EVT_RWHO,               // An RWHO command is being executed
197         EVT_ASYNC,              // Doing asynchronous messages
198         EVT_STEALTH,            // Entering stealth mode
199         EVT_UNSTEALTH,          // Exiting stealth mode
200         EVT_TIMER,              // Timer events are called once per minute and are not tied to any session
201         EVT_HOUSE,              // as needed houskeeping stuff
202         EVT_SHUTDOWN,           // Server is shutting down
203         EVT_PURGEUSER,          // Deleting a user
204         EVT_NEWUSER,            // Creating a user
205         EVT_BEFORESAVE,
206         EVT_AFTERSAVE,
207         EVT_SMTPSCAN,           // called before submitting a msg from SMTP
208         EVT_AFTERUSRMBOXSAVE    // called afte a message was saved into a users inbox
209 };
210
211
212 /* Priority levels for paging functions (lower is better) */
213 enum {
214         XMSG_PRI_LOCAL,         // Other users on -this- server
215         XMSG_PRI_REMOTE,        // Other users on a Citadel network
216         XMSG_PRI_FOREIGN,       // Contacts on foreign instant message hosts
217         MAX_XMSG_PRI
218 };
219
220
221 // Defines the relationship of a user to a particular room
222 typedef struct __visit {
223         long v_roomnum;
224         long v_roomgen;
225         long v_usernum;
226         long v_lastseen;
227         unsigned int v_flags;
228         char v_seen[SIZ];
229         char v_answered[SIZ];
230         int v_view;
231 } visit;
232
233 #define V_FORGET        1       // User has zapped this room
234 #define V_LOCKOUT       2       // User is locked out of this room
235 #define V_ACCESS        4       // Access is granted to this room
236
237
238 // Supplementary data for a message on disk
239 // These are kept separate from the message itself for one of two reasons:
240 // 1. Either their values may change at some point after initial save, or
241 // 2. They are merely caches of data which exist somewhere else, for speed.
242 // DO NOT PUT BIG DATA IN HERE ... we need this struct to be tiny for lots of quick r/w
243 struct MetaData {
244         long meta_msgnum;               // Message number in *local* message base
245         int meta_refcount;              // Number of rooms pointing to this msg
246         char meta_content_type[64];     // Cached MIME content-type
247         long meta_rfc822_length;        // Cache of RFC822-translated msg length
248 };
249
250
251 // Calls to AdjRefCount() are queued and deferred, so the user doesn't
252 // have to wait for various disk-intensive operations to complete synchronously.
253 // This is the record format.
254 struct arcq {
255         long arcq_msgnum;               // Message number being adjusted
256         int arcq_delta;                 // Adjustment ( usually 1 or -1 )
257 };
258
259
260 // Serialization routines use this struct to return a pointer and a length
261 struct ser_ret {
262         size_t len;
263         unsigned char *ser;
264 };
265
266
267 // The S_USETABLE database is used in several modules now, so we define its format here.
268 struct UseTable {
269         char ut_msgid[SIZ];
270         time_t ut_timestamp;
271 };
272
273
274 // These one-byte field headers are found in the Citadel message store.
275 typedef enum _MsgField {
276         eAuthor       = 'A',
277         eBig_message  = 'B',
278         eExclusiveID  = 'E',
279         erFc822Addr   = 'F',
280         emessageId    = 'I',
281         eJournal      = 'J',
282         eReplyTo      = 'K',
283         eListID       = 'L',
284         eMesageText   = 'M',
285         eOriginalRoom = 'O',
286         eMessagePath  = 'P',
287         eRecipient    = 'R',
288         eTimestamp    = 'T',
289         eMsgSubject   = 'U',
290         eenVelopeTo   = 'V',
291         eWeferences   = 'W',
292         eCarbonCopY   = 'Y',
293         eErrorMsg     = '0',
294         eSuppressIdx  = '1',
295         eExtnotify    = '2',
296         eVltMsgNum    = '3'
297 } eMsgField;
298
299
300 // User records.
301 typedef struct ctdluser ctdluser;
302 struct ctdluser {                       // User record
303         int version;                    // Citadel version which created this record
304         uid_t uid;                      // Associate with a unix account?
305         char password[32];              // password
306         unsigned flags;                 // See US_ flags below
307         long timescalled;               // Total number of logins
308         long posted;                    // Number of messages ever submitted
309         cit_uint8_t axlevel;            // Access level
310         long usernum;                   // User number (never recycled)
311         time_t lastcall;                // Date/time of most recent login
312         int USuserpurge;                // Purge time (in days) for user
313         char fullname[64];              // Display name (primary identifier)
314         long msgnum_bio;                // msgnum of user's profile (bio)
315         long msgnum_pic;                // msgnum of user's avatar (photo)
316         char emailaddrs[512];           // Internet email addresses
317         long msgnum_inboxrules;         // msgnum of user's inbox filtering rules
318         long lastproc_inboxrules;       // msgnum of last message filtered
319 };
320
321
322 // Room records.
323 struct ctdlroom {
324         char QRname[ROOMNAMELEN];       // Name of room
325         char QRpasswd[10];              // Only valid if it's a private rm
326         long QRroomaide;                // User number of room aide
327         long QRhighest;                 // Highest message NUMBER in room
328         time_t QRgen;                   // Generation number of room
329         unsigned QRflags;               // See flag values below
330         char QRdirname[15];             // Directory name, if applicable
331         long msgnum_info;               // msgnum of room banner (info file)
332         char QRfloor;                   // Which floor this room is on
333         time_t QRmtime;                 // Date/time of last post
334         struct ExpirePolicy QRep;       // Message expiration policy
335         long QRnumber;                  // Globally unique room number
336         char QRorder;                   // Sort key for room listing order
337         unsigned QRflags2;              // Additional flags
338         int QRdefaultview;              // How to display the contents
339         long msgnum_pic;                // msgnum of room picture or icon
340 };
341
342 // Private rooms are always flagged with QR_PRIVATE.  If neither QR_PASSWORDED
343 // or QR_GUESSNAME is set, then it is invitation-only.  Passworded rooms are
344 // flagged with both QR_PRIVATE and QR_PASSWORDED while guess-name rooms are
345 // flagged with both QR_PRIVATE and QR_GUESSNAME.  NEVER set all three flags.
346
347 // Floor record.  The floor number is implicit in its location in the file.
348 struct floor {
349         unsigned short f_flags;         // flags
350         char f_name[256];               // name of floor
351         int f_ref_count;                // reference count
352         struct ExpirePolicy f_ep;       // default expiration policy
353 };
354
355 #define F_INUSE         1               // floor is in use
356
357
358 #endif // SERVER_H