d9bcb207e4da1242574930b0b5076e5a98d135f6
[citadel.git] / citadel / server.h
1 /* 
2  * Main declarations file for the Citadel server
3  *
4  * Copyright (c) 1987-2015 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
84 /*
85  * Reasons why a session would be terminated (set CC->kill_me to these values)
86  */
87 enum {
88         KILLME_NOT,
89         KILLME_UNKNOWN,
90         KILLME_CLIENT_LOGGED_OUT,
91         KILLME_IDLE,
92         KILLME_CLIENT_DISCONNECTED,
93         KILLME_AUTHFAILED,
94         KILLME_SERVER_SHUTTING_DOWN,
95         KILLME_MAX_SESSIONS_EXCEEDED,
96         KILLME_ADMIN_TERMINATE,
97         KILLME_SELECT_INTERRUPTED,
98         KILLME_SELECT_FAILED,
99         KILLME_WRITE_FAILED,
100         KILLME_SIMULATION_WORKER,
101         KILLME_NOLOGIN,
102         KILLME_NO_CRYPTO,
103         KILLME_READSTRING_FAILED,
104         KILLME_MALLOC_FAILED,
105         KILLME_QUOTA,
106         KILLME_READ_FAILED,
107         KILLME_ILLEGAL_MANAGESIEVE_COMMAND,
108         KILLME_SPAMMER,
109         KILLME_XML_PARSER
110 };
111
112
113 #define CS_STEALTH      1       /* stealth mode */
114 #define CS_CHAT         2       /* chat mode */
115 #define CS_POSTING      4       /* Posting */
116
117
118 /*
119  * This is the control record for the message base... 
120  */
121 struct CitControl {
122         long MMhighest;                 /* highest message number in file   */
123         unsigned MMflags;               /* Global system flags              */
124         long MMnextuser;                /* highest user number on system    */
125         long MMnextroom;                /* highest room number on system    */
126         int MM_hosted_upgrade_level;    /* Server-hosted upgrade level      */
127         int MM_fulltext_wordbreaker;    /* ID of wordbreaker in use         */
128         long MMfulltext;                /* highest message number indexed   */
129         int MMdbversion;                /* Version of Berkeley DB used on previous server run */
130 };
131
132 extern int ScheduledShutdown;
133 extern struct CitControl CitControl;
134
135 struct ExpressMessage {
136         struct ExpressMessage *next;
137         time_t timestamp;       /* When this message was sent */
138         unsigned flags;         /* Special instructions */
139         char sender[256];       /* Name of sending user */
140         char sender_email[256]; /* Email or JID of sending user */
141         char *text;             /* Message text (if applicable) */
142 };
143
144 #define EM_BROADCAST    1       /* Broadcast message */
145 #define EM_GO_AWAY      2       /* Server requests client log off */
146 #define EM_CHAT         4       /* Server requests client enter chat */
147
148 /*
149  * Various things we need to lock and unlock
150  */
151 enum {
152         S_USERS,
153         S_ROOMS,
154         S_SESSION_TABLE,
155         S_FLOORTAB,
156         S_CHATQUEUE,
157         S_CONTROL,
158         S_NETDB,
159         S_SUPPMSGMAIN,
160         S_CONFIG,
161         S_HOUSEKEEPING,
162         S_NTTLIST,
163         S_DIRECTORY,
164         S_NETCONFIGS,
165         S_PUBLIC_CLIENTS,
166         S_FLOORCACHE,
167         S_ATBF,
168         S_JOURNAL_QUEUE,
169         S_RPLIST,
170         S_SIEVELIST,
171         S_CHKPWD,
172         S_LOG,
173         S_NETSPOOL,
174         S_XMPP_QUEUE,
175         S_SCHEDULE_LIST,
176         S_SINGLE_USER,
177         S_LDAP,
178         S_IM_LOGS,
179         MAX_SEMAPHORES
180 };
181
182
183 /*
184  * Upload types
185  */
186 #define UPL_FILE        0
187 #define UPL_NET         1
188 #define UPL_IMAGE       2
189
190
191 /*
192  * message transfer formats
193  */
194 enum {
195         MT_CITADEL,             /* Citadel proprietary */
196         MT_RFC822,              /* RFC822 */
197         MT_MIME,                /* MIME-formatted message */
198         MT_DOWNLOAD,            /* Download a component */
199         MT_SPEW_SECTION         /* Download a component in a single operation */
200 };
201
202 /*
203  * Message format types in the database
204  */
205 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
206 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
207 #define FMT_RFC822      4       /* Standard (headers are in M field) */
208
209
210 /*
211  * Citadel DataBases (define one for each cdb we need to open)
212  */
213 enum {
214         CDB_MSGMAIN,            /* message base                  */
215         CDB_USERS,              /* user file                     */
216         CDB_ROOMS,              /* room index                    */
217         CDB_FLOORTAB,           /* floor index                   */
218         CDB_MSGLISTS,           /* room message lists            */
219         CDB_VISIT,              /* user/room relationships       */
220         CDB_DIRECTORY,          /* address book directory        */
221         CDB_USETABLE,           /* network use table             */
222         CDB_BIGMSGS,            /* larger message bodies         */
223         CDB_FULLTEXT,           /* full text search index        */
224         CDB_EUIDINDEX,          /* locate msgs by EUID           */
225         CDB_USERSBYNUMBER,      /* index of users by number      */
226         CDB_OPENID,             /* associates OpenIDs with users */
227         CDB_CONFIG,             /* global system configuration   */
228         MAXCDB                  /* total number of CDB's defined */
229 };
230
231 struct cdbdata {
232         size_t len;
233         char *ptr;
234 };
235
236
237 /*
238  * Event types can't be enum'ed, because they must remain consistent between
239  * builds (to allow for binary modules built somewhere else)
240  */
241 #define EVT_STOP        0       /* Session is terminating */
242 #define EVT_START       1       /* Session is starting */
243 #define EVT_LOGIN       2       /* A user is logging in */
244 #define EVT_NEWROOM     3       /* Changing rooms */
245 #define EVT_LOGOUT      4       /* A user is logging out */
246 #define EVT_SETPASS     5       /* Setting or changing password */
247 #define EVT_CMD         6       /* Called after each server command */
248 #define EVT_RWHO        7       /* An RWHO command is being executed */
249 #define EVT_ASYNC       8       /* Doing asynchronous messages */
250 #define EVT_STEALTH     9       /* Entering stealth mode */
251 #define EVT_UNSTEALTH   10      /* Exiting stealth mode */
252
253 #define EVT_TIMER       50      /* Timer events are called once per minute
254                                    and are not tied to any session */
255 #define EVT_HOUSE       51      /* as needed houskeeping stuff */
256 #define EVT_SHUTDOWN    52      /* Server is shutting down */
257
258 #define EVT_PURGEUSER   100     /* Deleting a user */
259 #define EVT_NEWUSER     102     /* Creating a user */
260
261 #define EVT_BEFOREREAD  200
262 #define EVT_BEFORESAVE  201
263 #define EVT_AFTERSAVE   202
264 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
265 #define EVT_AFTERUSRMBOXSAVE 204 /* called afte a message was saved into a users inbox */
266 /* Priority levels for paging functions (lower is better) */
267 enum {
268         XMSG_PRI_LOCAL,         /* Other users on -this- server */
269         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
270         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
271         MAX_XMSG_PRI
272 };
273
274
275 /* Defines the relationship of a user to a particular room */
276 typedef struct __visit {
277         long v_roomnum;
278         long v_roomgen;
279         long v_usernum;
280         long v_lastseen;
281         unsigned int v_flags;
282         char v_seen[SIZ];
283         char v_answered[SIZ];
284         int v_view;
285 } visit;
286
287 #define V_FORGET        1       /* User has zapped this room        */
288 #define V_LOCKOUT       2       /* User is locked out of this room  */
289 #define V_ACCESS        4       /* Access is granted to this room   */
290
291
292 /* Supplementary data for a message on disk
293  * These are kept separate from the message itself for one of two reasons:
294  * 1. Either their values may change at some point after initial save, or
295  * 2. They are merely caches of data which exist somewhere else, for speed.
296  */
297 struct MetaData {
298         long meta_msgnum;               /* Message number in *local* message base */
299         int meta_refcount;              /* Number of rooms pointing to this msg */
300         char meta_content_type[64];     /* Cached MIME content-type */
301         long meta_rfc822_length;        /* Cache of RFC822-translated msg length */
302         char mimetype[64];              /* if we were able to guess the mimetype for the data */ 
303 };
304
305 /* Calls to AdjRefCount() are queued and deferred, so the user doesn't
306  * have to wait for various disk-intensive operations to complete synchronously.
307  * This is the record format.
308  */
309 struct arcq {
310         long arcq_msgnum;               /* Message number being adjusted */
311         int arcq_delta;                 /* Adjustment ( usually 1 or -1 ) */
312 };
313
314
315 /*
316  * Serialization routines use this struct to return a pointer and a length
317  */
318 struct ser_ret {
319         size_t len;
320         unsigned char *ser;
321 };
322
323
324 /*
325  * The S_USETABLE database is used in several modules now, so we define its format here.
326  */
327 struct UseTable {
328         char ut_msgid[SIZ];
329         time_t ut_timestamp;
330 };
331
332
333
334 /* Preferred field order                                                        */
335 /*               **********                     Important fields                */
336 /*                         ***************      Semi-important fields           */
337 /*                                        **    internal only                   */
338 /*                                          *   Message text (MUST be last)     */
339 ///#define FORDER       "IPTAFONHRDBCEWJGKLQSVXZYU12M"
340
341 typedef enum _MsgField {
342         eAuthor       = 'A',
343         eBig_message  = 'B',
344         eRemoteRoom   = 'C',
345         eDestination  = 'D',
346         eExclusiveID  = 'E',
347         erFc822Addr   = 'F',
348         eHumanNode    = 'H',
349         emessageId    = 'I',
350         eJournal      = 'J',
351         eReplyTo      = 'K',
352         eListID       = 'L',
353         eMesageText   = 'M',
354         eNodeName     = 'N',
355         eOriginalRoom = 'O',
356         eMessagePath  = 'P',
357         eRecipient    = 'R',
358         eSpecialField = 'S',
359         eTimestamp    = 'T',
360         eMsgSubject   = 'U',
361         eenVelopeTo   = 'V',
362         eWeferences   = 'W',
363         eCarbonCopY   = 'Y',
364         eErrorMsg     = '0',
365         eSuppressIdx  = '1',
366         eExtnotify    = '2',
367         eVltMsgNum    = '3'
368 }eMsgField;
369
370 #endif /* SERVER_H */