* Removed userspace buffering. Everyone has TCP_CORK or TCP_NOPUSH nowadays.
[citadel.git] / citadel / server.h
1 /* $Id$ */
2
3
4 #ifndef SERVER_H
5 #define SERVER_H
6
7 #ifdef __GNUC__
8 #define INLINE __inline__
9 #else
10 #define INLINE
11 #endif
12
13 #include "citadel.h"
14 #ifdef HAVE_OPENSSL
15 #define OPENSSL_NO_KRB5         /* work around redhat b0rken ssl headers */
16 #include <openssl/ssl.h>
17 #endif
18
19 /*
20  * New format for a message in memory
21  */
22 struct CtdlMessage {
23         int cm_magic;                   /* Self-check (NOT SAVED TO DISK) */
24         char cm_anon_type;              /* Anonymous or author-visible */
25         char cm_format_type;            /* Format type */
26         char *cm_fields[256];           /* Data fields */
27         unsigned int cm_flags;          /* How to handle (NOT SAVED TO DISK) */
28 };
29
30 #define CTDLMESSAGE_MAGIC               0x159d
31 #define CM_SKIP_HOOKS   0x01            /* Don't run server-side handlers */
32
33
34
35 /*
36  * Exit codes 101 through 109 are used for conditions in which
37  * we deliberately do NOT want the service to automatically
38  * restart.
39  */
40 #define CTDLEXIT_CONFIG         101     /* Could not read citadel.config */
41 #define CTDLEXIT_CONTROL        102     /* Could not acquire lock */
42 #define CTDLEXIT_HOME           103     /* Citadel home directory not found */
43 #define CTDLEXIT_OOD            104     /* Out Of Date config - rerun setup */
44 #define CTDLEXIT_DB             105     /* Unable to initialize database */
45 #define CTDLEXIT_LIBCITADEL     106     /* Incorrect version of libcitadel */
46
47
48
49
50 /*
51  * Here's the big one... the Citadel context structure.
52  *
53  * This structure keeps track of all information relating to a running 
54  * session on the server.  We keep one of these for each session thread.
55  *
56  */
57 struct CitContext {
58         struct CitContext *prev;        /* Link to previous session in list */
59         struct CitContext *next;        /* Link to next session in the list */
60
61         int state;              /* thread state (see CON_ values below) */
62         int kill_me;            /* Set to nonzero to flag for termination */
63         int client_socket;
64         int cs_pid;             /* session ID */
65         int dont_term;          /* for special activities like artv so we don't get killed */
66         time_t lastcmd;         /* time of last command executed */
67         time_t lastidle;        /* For computing idle time */
68
69         char curr_user[USERNAME_SIZE];  /* name of current user */
70         int logged_in;          /* logged in */
71         int internal_pgm;       /* authenticated as internal program */
72         int nologin;            /* not allowed to log in */
73         int is_local_socket;    /* set to 1 if client is on unix domain sock */
74         int curr_view;          /* The view type for the current user/room */
75         int is_master;          /* Is this session logged in using the master user? */
76
77         char net_node[32]       ;/* Is the client another Citadel server? */
78         time_t previous_login;  /* Date/time of previous login */
79         char lastcmdname[5];    /* name of last command executed */
80         unsigned cs_flags;      /* miscellaneous flags */
81         void (*h_command_function) (void) ;     /* service command function */
82         void (*h_async_function) (void) ;       /* do async msgs function */
83         int is_async;           /* Nonzero if client accepts async msgs */
84         int async_waiting;      /* Nonzero if there are async msgs waiting */
85         int input_waiting;      /* Nonzero if there is client input waiting */
86
87         /* Client information */
88         int cs_clientdev;       /* client developer ID */
89         int cs_clienttyp;       /* client type code */
90         int cs_clientver;       /* client version number */
91         char cs_clientname[32]; /* name of client software */
92         char cs_host[64];       /* host logged in from */
93         char cs_addr[64];       /* address logged in from */
94
95         /* The Internet type of thing */
96         char cs_inet_email[128];                /* Return address of outbound Internet mail */
97         char cs_inet_other_emails[1024];        /* User's other valid Internet email addresses */
98         char cs_inet_fn[128];                   /* Friendly-name of outbound Internet mail */
99
100         FILE *download_fp;      /* Fields relating to file transfer */
101         char download_desired_section[128];
102         FILE *upload_fp;
103         char upl_file[256];
104         char upl_path[PATH_MAX];
105         char upl_comment[256];
106         char upl_filedir[PATH_MAX];
107         char upl_mimetype[64];
108         char dl_is_net;
109         char upload_type;
110
111         struct ctdluser user;   /* Database record buffers */
112         struct ctdlroom room;
113
114         /* Beginning of cryptography - session nonce */
115         char cs_nonce[NONCE_SIZE];      /* The nonce for this session's next auth transaction */
116
117         /* Redirect this session's output to a memory buffer? */
118         char *redirect_buffer;          /* the buffer */
119         size_t redirect_len;            /* length of data in buffer */
120         size_t redirect_alloc;          /* length of allocated buffer */
121 #ifdef HAVE_OPENSSL
122         SSL *ssl;
123         int redirect_ssl;
124 #endif
125
126         /* A linked list of all instant messages sent to us. */
127         struct ExpressMessage *FirstExpressMessage;
128         int disable_exp;        /* Set to 1 to disable incoming pages */
129         int newmail;            /* Other sessions increment this */
130
131         /* Masqueraded values in the 'who is online' list */
132         char fake_username[USERNAME_SIZE];
133         char fake_hostname[64];
134         char fake_roomname[ROOMNAMELEN];
135
136         /* Preferred MIME formats */
137         char preferred_formats[256];
138         int msg4_dont_decode;
139
140         /* Dynamically allocated session data */
141         char *session_specific_data;            /* Used by individual protocol modules */
142         struct cit_ical *CIT_ICAL;              /* calendaring data */
143         struct ma_info *ma;                     /* multipart/alternative data */
144         const char *ServiceName;                /* readable purpose of this session */
145         void *openid_data;                      /* Data stored by the OpenID module */
146 };
147
148 typedef struct CitContext t_context;
149
150 /*
151  * Values for CitContext.state
152  * 
153  * A session that is doing nothing is in CON_IDLE state.  When activity
154  * is detected on the socket, it goes to CON_READY, indicating that it
155  * needs to have a worker thread bound to it.  When a thread binds to
156  * the session, it goes to CON_EXECUTING and does its thing.  When the
157  * transaction is finished, the thread sets it back to CON_IDLE and lets
158  * it go.
159  */
160 enum {
161         CON_IDLE,               /* This context is doing nothing */
162         CON_READY,              /* This context needs attention */
163         CON_EXECUTING           /* This context is bound to a thread */
164 };
165
166
167 #define CS_STEALTH      1       /* stealth mode */
168 #define CS_CHAT         2       /* chat mode */
169 #define CS_POSTING      4       /* Posting */
170
171 struct CitContext *MyContext(void);
172 #define CC MyContext()
173
174 /*
175  * This is the control record for the message base... 
176  */
177 struct CitControl {
178         long MMhighest;                 /* highest message number in file   */
179         unsigned MMflags;               /* Global system flags              */
180         long MMnextuser;                /* highest user number on system    */
181         long MMnextroom;                /* highest room number on system    */
182         int version;                    /* Server-hosted upgrade level      */
183         int fulltext_wordbreaker;       /* ID of wordbreaker in use         */
184         long MMfulltext;                /* highest message number indexed   */
185         int MMdbversion;                /* Version of Berkeley DB used on previous server run */
186 };
187
188 extern struct CitContext *ContextList;
189 extern int ScheduledShutdown;
190 extern struct CitControl CitControl;
191
192 struct ExpressMessage {
193         struct ExpressMessage *next;
194         time_t timestamp;       /* When this message was sent */
195         unsigned flags;         /* Special instructions */
196         char sender[256];       /* Name of sending user */
197         char sender_email[256]; /* Email or JID of sending user */
198         char *text;             /* Message text (if applicable) */
199 };
200
201 #define EM_BROADCAST    1       /* Broadcast message */
202 #define EM_GO_AWAY      2       /* Server requests client log off */
203 #define EM_CHAT         4       /* Server requests client enter chat */
204
205 struct ChatLine {
206         struct ChatLine *next;
207         int chat_seq;
208         time_t chat_time;
209         char chat_text[SIZ];
210         char chat_username[USERNAME_SIZE];
211         char chat_room[ROOMNAMELEN];
212 };
213
214 /*
215  * Various things we need to lock and unlock
216  */
217 enum {
218         S_USERS,
219         S_ROOMS,
220         S_SESSION_TABLE,
221         S_FLOORTAB,
222         S_CHATQUEUE,
223         S_CONTROL,
224         S_NETDB,
225         S_SUPPMSGMAIN,
226         S_CONFIG,
227         S_HOUSEKEEPING,
228         S_NTTLIST,
229         S_DIRECTORY,
230         S_NETCONFIGS,
231         S_PUBLIC_CLIENTS,
232         S_FLOORCACHE,
233         S_DEBUGMEMLEAKS,
234         S_ATBF,
235         S_JOURNAL_QUEUE,
236         S_RPLIST,
237         S_SIEVELIST,
238         S_CHKPWD,
239         S_LOG,
240         S_NETSPOOL,
241         S_THREAD_LIST,
242         S_XMPP_QUEUE,
243         S_SCHEDULE_LIST,
244         S_SINGLE_USER,
245         S_LDAP,
246         MAX_SEMAPHORES
247 };
248
249
250 /*
251  * Upload types
252  */
253 #define UPL_FILE        0
254 #define UPL_NET         1
255 #define UPL_IMAGE       2
256
257
258 /*
259  * message transfer formats
260  */
261 enum {
262         MT_CITADEL,             /* Citadel proprietary */
263         MT_RFC822,              /* RFC822 */
264         MT_MIME,                /* MIME-formatted message */
265         MT_DOWNLOAD,            /* Download a component */
266         MT_SPEW_SECTION         /* Download a component in a single operation */
267 };
268
269 /*
270  * Message format types in the database
271  */
272 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
273 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
274 #define FMT_RFC822      4       /* Standard (headers are in M field) */
275
276
277 /*
278  * Citadel DataBases (define one for each cdb we need to open)
279  */
280 enum {
281         CDB_MSGMAIN,            /* message base                  */
282         CDB_USERS,              /* user file                     */
283         CDB_ROOMS,              /* room index                    */
284         CDB_FLOORTAB,           /* floor index                   */
285         CDB_MSGLISTS,           /* room message lists            */
286         CDB_VISIT,              /* user/room relationships       */
287         CDB_DIRECTORY,          /* address book directory        */
288         CDB_USETABLE,           /* network use table             */
289         CDB_BIGMSGS,            /* larger message bodies         */
290         CDB_FULLTEXT,           /* full text search index        */
291         CDB_EUIDINDEX,          /* locate msgs by EUID           */
292         CDB_USERSBYNUMBER,      /* index of users by number      */
293         CDB_OPENID,             /* associates OpenIDs with users */
294         MAXCDB                  /* total number of CDB's defined */
295 };
296
297 struct cdbdata {
298         size_t len;
299         char *ptr;
300 };
301
302
303 /* 
304  * Event types can't be enum'ed, because they must remain consistent between
305  * builds (to allow for binary modules built somewhere else)
306  */
307 #define EVT_STOP        0       /* Session is terminating */
308 #define EVT_START       1       /* Session is starting */
309 #define EVT_LOGIN       2       /* A user is logging in */
310 #define EVT_NEWROOM     3       /* Changing rooms */
311 #define EVT_LOGOUT      4       /* A user is logging out */
312 #define EVT_SETPASS     5       /* Setting or changing password */
313 #define EVT_CMD         6       /* Called after each server command */
314 #define EVT_RWHO        7       /* An RWHO command is being executed */
315 #define EVT_ASYNC       8       /* Doing asynchronous messages */
316 #define EVT_STEALTH     9       /* Entering stealth mode */
317 #define EVT_UNSTEALTH   10      /* Exiting stealth mode */
318
319 #define EVT_TIMER       50      /* Timer events are called once per minute
320                                    and are not tied to any session */
321 #define EVT_HOUSE       51      /* as needed houskeeping stuff */
322
323 #define EVT_PURGEUSER   100     /* Deleting a user */
324 #define EVT_NEWUSER     102     /* Creating a user */
325
326 #define EVT_BEFOREREAD  200
327 #define EVT_BEFORESAVE  201
328 #define EVT_AFTERSAVE   202
329 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
330 /* Priority levels for paging functions (lower is better) */
331 enum {
332         XMSG_PRI_LOCAL,         /* Other users on -this- server */
333         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
334         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
335         MAX_XMSG_PRI
336 };
337
338
339 /* Defines the relationship of a user to a particular room */
340 struct visit {
341         long v_roomnum;
342         long v_roomgen;
343         long v_usernum;
344         long v_lastseen;
345         unsigned int v_flags;
346         char v_seen[SIZ];
347         char v_answered[SIZ];
348         int v_view;
349 };
350
351 #define V_FORGET        1       /* User has zapped this room        */
352 #define V_LOCKOUT       2       /* User is locked out of this room  */
353 #define V_ACCESS        4       /* Access is granted to this room   */
354
355
356 /* Supplementary data for a message on disk
357  * These are kept separate from the message itself for one of two reasons:
358  * 1. Either their values may change at some point after initial save, or
359  * 2. They are merely caches of data which exist somewhere else, for speed.
360  */
361 struct MetaData {
362         long meta_msgnum;               /* Message number in *local* message base */
363         int meta_refcount;              /* Number of rooms pointing to this msg */
364         char meta_content_type[64];     /* Cached MIME content-type */
365         long meta_rfc822_length;        /* Cache of RFC822-translated msg length */
366         char mimetype[64];              /* if we were able to guess the mimetype for the data */ 
367 };
368
369 /* Calls to AdjRefCount() are queued and deferred, so the user doesn't
370  * have to wait for various disk-intensive operations to complete synchronously.
371  * This is the record format.
372  */
373 struct arcq {
374         long arcq_msgnum;               /* Message number being adjusted */
375         int arcq_delta;                 /* Adjustment ( usually 1 or -1 ) */
376 };
377
378
379 /* 
380  * Serialization routines use this struct to return a pointer and a length
381  */
382 struct ser_ret {
383         size_t len;
384         unsigned char *ser;
385 };
386
387
388 /*
389  * The S_USETABLE database is used in several modules now, so we define its format here.
390  */
391 struct UseTable {
392         char ut_msgid[SIZ];
393         time_t ut_timestamp;
394 };
395
396
397
398 /* Preferred field order                                                        */
399 /*               **********                     Important fields                */
400 /*                         ***************      Semi-important fields           */
401 /*                                        *     Message text (MUST be last)     */
402 #define FORDER  "IPTAFONHRDBCEWJGKLQSVXZYUM"
403
404 #endif /* SERVER_H */