97be8d1b1531720a42807666f014b4eabb594b71
[citadel.git] / citadel / server.h
1 /* 
2  * Main declarations file 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 #ifndef SERVER_H
16 #define SERVER_H
17
18 #ifdef __GNUC__
19 #define INLINE __inline__
20 #else
21 #define INLINE
22 #endif
23
24 #include "citadel.h"
25 #ifdef HAVE_OPENSSL
26 #define OPENSSL_NO_KRB5         /* work around redhat b0rken ssl headers */
27 #include <openssl/ssl.h>
28 #endif
29
30 /*
31  * New format for a message in memory
32  */
33 struct CtdlMessage {
34         int cm_magic;                   /* Self-check (NOT SAVED TO DISK) */
35         char cm_anon_type;              /* Anonymous or author-visible */
36         char cm_format_type;            /* Format type */
37         char *cm_fields[256];           /* Data fields */
38         long cm_lengths[256];           /* size of datafields */
39         unsigned int cm_flags;          /* How to handle (NOT SAVED TO DISK) */
40 };
41
42 #define CTDLMESSAGE_MAGIC               0x159d
43 #define CM_SKIP_HOOKS   0x01            /* Don't run server-side handlers */
44
45
46 /* Data structure returned by validate_recipients() */
47 typedef struct __recptypes {
48         int recptypes_magic;
49         int num_local;
50         int num_internet;
51         int num_ignet;
52         int num_room;
53         int num_error;
54         char *errormsg;
55         char *recp_local;
56         char *recp_internet;
57         char *recp_ignet;
58         char *recp_room;
59         char *recp_orgroom;
60         char *display_recp;
61         char *bounce_to;
62         char *envelope_from;
63         char *sending_room;
64 } recptypes;
65
66 #define RECPTYPES_MAGIC 0xfeeb
67
68
69 #define CTDLEXIT_SHUTDOWN       0       /* Normal shutdown; do NOT auto-restart */
70
71 /*
72  * Exit codes 101 through 109 are used for conditions in which
73  * we deliberately do NOT want the service to automatically
74  * restart.
75  */
76 #define CTDLEXIT_CONFIG         101     /* Could not read citadel.config */
77 #define CTDLEXIT_CONTROL        102     /* Could not acquire lock */
78 #define CTDLEXIT_HOME           103     /* Citadel home directory not found */
79 #define CTDLEXIT_OOD            104     /* Out Of Date config - rerun setup */
80 #define CTDLEXIT_DB             105     /* Unable to initialize database */
81 #define CTDLEXIT_LIBCITADEL     106     /* Incorrect version of libcitadel */
82 #define CTDL_EXIT_UNSUP_AUTH    107     /* Unsupported auth mode configured */
83 #define CTDLEXIT_UNUSER         108     /* Could not determine uid to run as */
84 #define CTDLEXIT_CRYPTO         109     /* Problem initializing SSL or TLS */
85
86 /*
87  * Reasons why a session would be terminated (set CC->kill_me to these values)
88  */
89 enum {
90         KILLME_NOT,
91         KILLME_UNKNOWN,
92         KILLME_CLIENT_LOGGED_OUT,
93         KILLME_IDLE,
94         KILLME_CLIENT_DISCONNECTED,
95         KILLME_AUTHFAILED,
96         KILLME_SERVER_SHUTTING_DOWN,
97         KILLME_MAX_SESSIONS_EXCEEDED,
98         KILLME_ADMIN_TERMINATE,
99         KILLME_SELECT_INTERRUPTED,
100         KILLME_SELECT_FAILED,
101         KILLME_WRITE_FAILED,
102         KILLME_SIMULATION_WORKER,
103         KILLME_NOLOGIN,
104         KILLME_NO_CRYPTO,
105         KILLME_READSTRING_FAILED,
106         KILLME_MALLOC_FAILED,
107         KILLME_QUOTA,
108         KILLME_READ_FAILED,
109         KILLME_ILLEGAL_MANAGESIEVE_COMMAND,
110         KILLME_SPAMMER,
111         KILLME_XML_PARSER
112 };
113
114
115 #define CS_STEALTH      1       /* stealth mode */
116 #define CS_CHAT         2       /* chat mode */
117 #define CS_POSTING      4       /* Posting */
118
119
120 extern int ScheduledShutdown;
121 extern uid_t ctdluid;
122
123 struct ExpressMessage {
124         struct ExpressMessage *next;
125         time_t timestamp;       /* When this message was sent */
126         unsigned flags;         /* Special instructions */
127         char sender[256];       /* Name of sending user */
128         char sender_email[256]; /* Email or JID of sending user */
129         char *text;             /* Message text (if applicable) */
130 };
131
132 #define EM_BROADCAST    1       /* Broadcast message */
133 #define EM_GO_AWAY      2       /* Server requests client log off */
134 #define EM_CHAT         4       /* Server requests client enter chat */
135
136 /*
137  * Various things we need to lock and unlock
138  */
139 enum {
140         S_USERS,
141         S_ROOMS,
142         S_SESSION_TABLE,
143         S_FLOORTAB,
144         S_CHATQUEUE,
145         S_CONTROL,
146         S_NETDB,
147         S_SUPPMSGMAIN,
148         S_CONFIG,
149         S_HOUSEKEEPING,
150         S_NTTLIST,
151         S_DIRECTORY,
152         S_NETCONFIGS,
153         S_PUBLIC_CLIENTS,
154         S_FLOORCACHE,
155         S_ATBF,
156         S_JOURNAL_QUEUE,
157         S_RPLIST,
158         S_SIEVELIST,
159         S_CHKPWD,
160         S_LOG,
161         S_NETSPOOL,
162         S_XMPP_QUEUE,
163         S_SCHEDULE_LIST,
164         S_SINGLE_USER,
165         S_LDAP,
166         S_IM_LOGS,
167         MAX_SEMAPHORES
168 };
169
170
171 /*
172  * Upload types
173  */
174 #define UPL_FILE        0
175 #define UPL_NET         1
176 #define UPL_IMAGE       2
177
178
179 /*
180  * message transfer formats
181  */
182 enum {
183         MT_CITADEL,             /* Citadel proprietary */
184         MT_RFC822,              /* RFC822 */
185         MT_MIME,                /* MIME-formatted message */
186         MT_DOWNLOAD,            /* Download a component */
187         MT_SPEW_SECTION         /* Download a component in a single operation */
188 };
189
190 /*
191  * Message format types in the database
192  */
193 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
194 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
195 #define FMT_RFC822      4       /* Standard (headers are in M field) */
196
197
198 /*
199  * Citadel DataBases (define one for each cdb we need to open)
200  */
201 enum {
202         CDB_MSGMAIN,            /* message base                  */
203         CDB_USERS,              /* user file                     */
204         CDB_ROOMS,              /* room index                    */
205         CDB_FLOORTAB,           /* floor index                   */
206         CDB_MSGLISTS,           /* room message lists            */
207         CDB_VISIT,              /* user/room relationships       */
208         CDB_DIRECTORY,          /* address book directory        */
209         CDB_USETABLE,           /* network use table             */
210         CDB_BIGMSGS,            /* larger message bodies         */
211         CDB_FULLTEXT,           /* full text search index        */
212         CDB_EUIDINDEX,          /* locate msgs by EUID           */
213         CDB_USERSBYNUMBER,      /* index of users by number      */
214         CDB_EXTAUTH,            /* associates OpenIDs with users */
215         CDB_CONFIG,             /* system configuration database */
216         MAXCDB                  /* total number of CDB's defined */
217 };
218
219 struct cdbdata {
220         size_t len;
221         char *ptr;
222 };
223
224
225 /*
226  * Event types can't be enum'ed, because they must remain consistent between
227  * builds (to allow for binary modules built somewhere else)
228  */
229 #define EVT_STOP        0       /* Session is terminating */
230 #define EVT_START       1       /* Session is starting */
231 #define EVT_LOGIN       2       /* A user is logging in */
232 #define EVT_NEWROOM     3       /* Changing rooms */
233 #define EVT_LOGOUT      4       /* A user is logging out */
234 #define EVT_SETPASS     5       /* Setting or changing password */
235 #define EVT_CMD         6       /* Called after each server command */
236 #define EVT_RWHO        7       /* An RWHO command is being executed */
237 #define EVT_ASYNC       8       /* Doing asynchronous messages */
238 #define EVT_STEALTH     9       /* Entering stealth mode */
239 #define EVT_UNSTEALTH   10      /* Exiting stealth mode */
240
241 #define EVT_TIMER       50      /* Timer events are called once per minute
242                                    and are not tied to any session */
243 #define EVT_HOUSE       51      /* as needed houskeeping stuff */
244 #define EVT_SHUTDOWN    52      /* Server is shutting down */
245
246 #define EVT_PURGEUSER   100     /* Deleting a user */
247 #define EVT_NEWUSER     102     /* Creating a user */
248
249 #define EVT_BEFOREREAD  200
250 #define EVT_BEFORESAVE  201
251 #define EVT_AFTERSAVE   202
252 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
253 #define EVT_AFTERUSRMBOXSAVE 204 /* called afte a message was saved into a users inbox */
254 /* Priority levels for paging functions (lower is better) */
255 enum {
256         XMSG_PRI_LOCAL,         /* Other users on -this- server */
257         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
258         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
259         MAX_XMSG_PRI
260 };
261
262
263 /* Defines the relationship of a user to a particular room */
264 typedef struct __visit {
265         long v_roomnum;
266         long v_roomgen;
267         long v_usernum;
268         long v_lastseen;
269         unsigned int v_flags;
270         char v_seen[SIZ];
271         char v_answered[SIZ];
272         int v_view;
273 } visit;
274
275 #define V_FORGET        1       /* User has zapped this room        */
276 #define V_LOCKOUT       2       /* User is locked out of this room  */
277 #define V_ACCESS        4       /* Access is granted to this room   */
278
279
280 /* Supplementary data for a message on disk
281  * These are kept separate from the message itself for one of two reasons:
282  * 1. Either their values may change at some point after initial save, or
283  * 2. They are merely caches of data which exist somewhere else, for speed.
284  */
285 struct MetaData {
286         long meta_msgnum;               /* Message number in *local* message base */
287         int meta_refcount;              /* Number of rooms pointing to this msg */
288         char meta_content_type[64];     /* Cached MIME content-type */
289         long meta_rfc822_length;        /* Cache of RFC822-translated msg length */
290         char mimetype[64];              /* if we were able to guess the mimetype for the data */ 
291 };
292
293 /* Calls to AdjRefCount() are queued and deferred, so the user doesn't
294  * have to wait for various disk-intensive operations to complete synchronously.
295  * This is the record format.
296  */
297 struct arcq {
298         long arcq_msgnum;               /* Message number being adjusted */
299         int arcq_delta;                 /* Adjustment ( usually 1 or -1 ) */
300 };
301
302
303 /*
304  * Serialization routines use this struct to return a pointer and a length
305  */
306 struct ser_ret {
307         size_t len;
308         unsigned char *ser;
309 };
310
311
312 /*
313  * The S_USETABLE database is used in several modules now, so we define its format here.
314  */
315 struct UseTable {
316         char ut_msgid[SIZ];
317         time_t ut_timestamp;
318 };
319
320
321
322 /* Preferred field order                                                        */
323 /*               **********                     Important fields                */
324 /*                         ***************      Semi-important fields           */
325 /*                                        **    internal only                   */
326 /*                                          *   Message text (MUST be last)     */
327 ///#define FORDER       "IPTAFONHRDBCEWJGKLQSVXZYU12M"
328
329 typedef enum _MsgField {
330         eAuthor       = 'A',
331         eBig_message  = 'B',
332         eRemoteRoom   = 'C',
333         eDestination  = 'D',
334         eExclusiveID  = 'E',
335         erFc822Addr   = 'F',
336         eHumanNode    = 'H',
337         emessageId    = 'I',
338         eJournal      = 'J',
339         eReplyTo      = 'K',
340         eListID       = 'L',
341         eMesageText   = 'M',
342         eNodeName     = 'N',
343         eOriginalRoom = 'O',
344         eMessagePath  = 'P',
345         eRecipient    = 'R',
346         eSpecialField = 'S',
347         eTimestamp    = 'T',
348         eMsgSubject   = 'U',
349         eenVelopeTo   = 'V',
350         eWeferences   = 'W',
351         eCarbonCopY   = 'Y',
352         eErrorMsg     = '0',
353         eSuppressIdx  = '1',
354         eExtnotify    = '2',
355         eVltMsgNum    = '3'
356 }eMsgField;
357
358 #endif /* SERVER_H */