bdb7a7ebeb5997643390311811d63b7b2199b046
[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
46
47
48
49 /*
50  * Here's the big one... the Citadel context structure.
51  *
52  * This structure keeps track of all information relating to a running 
53  * session on the server.  We keep one of these for each session thread.
54  *
55  * Note that the first element is "*next" so that it may be used without
56  * modification in a linked list.
57  */
58 struct CitContext {
59         struct CitContext *prev;        /* Link to previous session in list */
60         struct CitContext *next;        /* Link to next session in the list */
61
62         int state;              /* thread state (see CON_ values below) */
63         int kill_me;            /* Set to nonzero to flag for termination */
64         int client_socket;
65         int cs_pid;             /* session ID */
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
76         char net_node[32]       ;/* Is the client another Citadel server? */
77         time_t previous_login;  /* Date/time of previous login */
78         char lastcmdname[5];    /* name of last command executed */
79         unsigned cs_flags;      /* miscellaneous flags */
80         void (*h_command_function) (void) ;     /* service command function */
81         void (*h_async_function) (void) ;       /* do async msgs function */
82         int is_async;           /* Nonzero if client accepts async msgs */
83         int async_waiting;      /* Nonzero if there are async msgs waiting */
84         int input_waiting;      /* Nonzero if there is client input waiting */
85
86         /* feeping creaturisms... */
87         int cs_clientdev;       /* client developer ID */
88         int cs_clienttyp;       /* client type code */
89         int cs_clientver;       /* client version number */
90         char cs_clientname[32]; /* name of client software */
91         char cs_host[64];       /* host logged in from */
92         char cs_addr[64];       /* address logged in from */
93
94         /* The Internet type of thing */
95         char cs_inet_email[128];/* Return address of outbound Internet mail */
96         char cs_inet_fn[128];   /* Friendly-name of outbound Internet mail */
97
98         FILE *download_fp;      /* Fields relating to file transfer */
99         char download_desired_section[128];
100         FILE *upload_fp;
101         char upl_file[256];
102         char upl_path[PATH_MAX];
103         char upl_comment[256];
104         char upl_filedir[PATH_MAX];
105         char dl_is_net;
106         char upload_type;
107
108         struct ctdluser user;   /* Database record buffers */
109         struct ctdlroom room;
110
111         /* Beginning of cryptography - session nonce */
112         char cs_nonce[NONCE_SIZE];      /* The nonce for this session's next auth transaction */
113
114         /* Redirect this session's output to a memory buffer? */
115         char *redirect_buffer;          /* the buffer */
116         size_t redirect_len;            /* length of data in buffer */
117         size_t redirect_alloc;          /* length of allocated buffer */
118 #ifdef HAVE_OPENSSL
119         SSL *ssl;
120         int redirect_ssl;
121 #endif
122
123         int buffering;
124         char *output_buffer;    /* hold output for one big dump */
125         int buffer_len;
126
127         /* A linked list of all instant messages sent to us. */
128         struct ExpressMessage *FirstExpressMessage;
129         int disable_exp;        /* Set to 1 to disable incoming pages */
130         int newmail;            /* Other sessions increment this */
131
132         /* Masqueraded values in the 'who is online' list */
133         char fake_username[USERNAME_SIZE];
134         char fake_hostname[64];
135         char fake_roomname[ROOMNAMELEN];
136
137         char preferred_formats[256];            /* Preferred MIME formats */
138
139         /* Dynamically allocated session data */
140         struct citimap *IMAP;
141         struct citpop3 *POP3;
142         struct citsmtp *SMTP;
143         struct citmgsve *MGSVE; /**< Managesieve Session struct */
144         char *SMTP_RECPS;
145         char *SMTP_ROOMS;
146         struct cit_ical *CIT_ICAL;              /* calendaring data */
147         struct ma_info *ma;                     /* multipart/alternative data */
148 };
149
150 typedef struct CitContext t_context;
151
152 /*
153  * Values for CitContext.state
154  * 
155  * A session that is doing nothing is in CON_IDLE state.  When activity
156  * is detected on the socket, it goes to CON_READY, indicating that it
157  * needs to have a worker thread bound to it.  When a thread binds to
158  * the session, it goes to CON_EXECUTING and does its thing.  When the
159  * transaction is finished, the thread sets it back to CON_IDLE and lets
160  * it go.
161  */
162 enum {
163         CON_IDLE,               /* This context is doing nothing */
164         CON_READY,              /* This context needs attention */
165         CON_EXECUTING           /* This context is bound to a thread */
166 };
167
168
169 #define CS_STEALTH      1       /* stealth mode */
170 #define CS_CHAT         2       /* chat mode */
171 #define CS_POSTING      4       /* Posting */
172
173 struct CitContext *MyContext(void);
174 #define CC MyContext()
175
176 /*
177  * This is the control record for the message base... 
178  */
179 struct CitControl {
180         long MMhighest;                 /* highest message number in file   */
181         unsigned MMflags;               /* Global system flags              */
182         long MMnextuser;                /* highest user number on system    */
183         long MMnextroom;                /* highest room number on system    */
184         int version;                    /* Server-hosted upgrade level      */
185         int fulltext_wordbreaker;       /* ID of wordbreaker in use         */
186         long MMfulltext;                /* highest message number indexed   */
187 };
188
189 extern struct CitContext *ContextList;
190 extern int ScheduledShutdown;
191 extern struct CitControl CitControl;
192
193
194 struct ExpressMessage {
195         struct ExpressMessage *next;
196         time_t timestamp;       /* When this message was sent */
197         unsigned flags;         /* Special instructions */
198         char sender[64];        /* Name of sending user */
199         char *text;             /* Message text (if applicable) */
200 };
201
202 #define EM_BROADCAST    1       /* Broadcast message */
203 #define EM_GO_AWAY      2       /* Server requests client log off */
204 #define EM_CHAT         4       /* Server requests client enter chat */
205
206 struct ChatLine {
207         struct ChatLine *next;
208         int chat_seq;
209         time_t chat_time;
210         char chat_text[SIZ];
211         char chat_username[USERNAME_SIZE];
212         char chat_room[ROOMNAMELEN];
213 };
214
215 /*
216  * Various things we need to lock and unlock
217  */
218 enum {
219         S_USERS,
220         S_ROOMS,
221         S_SESSION_TABLE,
222         S_FLOORTAB,
223         S_CHATQUEUE,
224         S_CONTROL,
225         S_NETDB,
226         S_SUPPMSGMAIN,
227         S_CONFIG,
228         S_WORKER_LIST,
229         S_HOUSEKEEPING,
230         S_NTTLIST,
231         S_DIRECTORY,
232         S_NETCONFIGS,
233         S_PUBLIC_CLIENTS,
234         S_LDAP,
235         S_FLOORCACHE,
236         S_DEBUGMEMLEAKS,
237         S_ATBF,
238         S_JOURNAL_QUEUE,
239         S_RPLIST,
240         S_SIEVELIST,
241         MAX_SEMAPHORES
242 };
243
244
245 /*
246  * Upload types
247  */
248 #define UPL_FILE        0
249 #define UPL_NET         1
250 #define UPL_IMAGE       2
251
252
253 /*
254  * message transfer formats
255  */
256 enum {
257         MT_CITADEL,             /* Citadel proprietary */
258         MT_RFC822,              /* RFC822 */
259         MT_MIME,                /* MIME-formatted message */
260         MT_DOWNLOAD,            /* Download a component */
261         MT_SPEW_SECTION         /* Download a component in a single operation */
262 };
263
264 /*
265  * Message format types in the database
266  */
267 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
268 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
269 #define FMT_RFC822      4       /* Standard (headers are in M field) */
270
271
272 /*
273  * Citadel DataBases (define one for each cdb we need to open)
274  */
275 enum {
276         CDB_MSGMAIN,            /* message base                  */
277         CDB_USERS,              /* user file                     */
278         CDB_ROOMS,              /* room index                    */
279         CDB_FLOORTAB,           /* floor index                   */
280         CDB_MSGLISTS,           /* room message lists            */
281         CDB_VISIT,              /* user/room relationships       */
282         CDB_DIRECTORY,          /* address book directory        */
283         CDB_USETABLE,           /* network use table             */
284         CDB_BIGMSGS,            /* larger message bodies         */
285         CDB_FULLTEXT,           /* full text search index        */
286         CDB_EUIDINDEX,          /* locate msgs by EUID           */
287         MAXCDB                  /* total number of CDB's defined */
288 };
289
290 struct cdbdata {
291         size_t len;
292         char *ptr;
293 };
294
295
296
297 /* Structures and declarations for function hooks of various types */
298
299 struct LogFunctionHook {
300         struct LogFunctionHook *next;
301         int loglevel;
302         void (*h_function_pointer) (char *);
303 };
304 extern struct LogFunctionHook *LogHookTable;
305
306 struct CleanupFunctionHook {
307         struct CleanupFunctionHook *next;
308         void (*h_function_pointer) (void);
309 };
310 extern struct CleanupFunctionHook *CleanupHookTable;
311
312 struct FixedOutputHook {
313         struct FixedOutputHook *next;
314         char content_type[64];
315         void (*h_function_pointer) (char *, int);
316 };
317 extern struct FixedOutputHook *FixedOutputTable;
318
319
320
321 /*
322  * SessionFunctionHook extensions are used for any type of hook for which
323  * the context in which it's being called (which is determined by the event
324  * type) will make it obvious for the hook function to know where to look for
325  * pertinent data.
326  */
327 struct SessionFunctionHook {
328         struct SessionFunctionHook *next;
329         void (*h_function_pointer) (void);
330         int eventtype;
331 };
332 extern struct SessionFunctionHook *SessionHookTable;
333
334 /* 
335  * Event types can't be enum'ed, because they must remain consistent between
336  * builds (to allow for binary modules built somewhere else)
337  */
338 #define EVT_STOP        0       /* Session is terminating */
339 #define EVT_START       1       /* Session is starting */
340 #define EVT_LOGIN       2       /* A user is logging in */
341 #define EVT_NEWROOM     3       /* Changing rooms */
342 #define EVT_LOGOUT      4       /* A user is logging out */
343 #define EVT_SETPASS     5       /* Setting or changing password */
344 #define EVT_CMD         6       /* Called after each server command */
345 #define EVT_RWHO        7       /* An RWHO command is being executed */
346 #define EVT_ASYNC       8       /* Doing asynchronous messages */
347
348 #define EVT_TIMER       50      /* Timer events are called once per minute
349                                    and are not tied to any session */
350
351 /*
352  * UserFunctionHook extensions are used for any type of hook which implements
353  * an operation on a user or username (potentially) other than the one
354  * operating the current session.
355  */
356 struct UserFunctionHook {
357         struct UserFunctionHook *next;
358         void (*h_function_pointer) (struct ctdluser *usbuf);
359         int eventtype;
360 };
361 extern struct UserFunctionHook *UserHookTable;
362
363 #define EVT_PURGEUSER   100     /* Deleting a user */
364 #define EVT_NEWUSER     102     /* Creating a user */
365
366 /*
367  * MessageFunctionHook extensions are used for hooks which implement handlers
368  * for various types of message operations (save, read, etc.)
369  */
370 struct MessageFunctionHook {
371         struct MessageFunctionHook *next;
372         int (*h_function_pointer) (struct CtdlMessage *msg);
373         int eventtype;
374 };
375 extern struct MessageFunctionHook *MessageHookTable;
376
377 #define EVT_BEFOREREAD  200
378 #define EVT_BEFORESAVE  201
379 #define EVT_AFTERSAVE   202
380 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
381
382
383
384 /*
385  * NetprocFunctionHook extensions are used for hooks which implement handlers
386  * for incoming network messages.
387  */
388 struct NetprocFunctionHook {
389         struct NetprocFunctionHook *next;
390         int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
391 };
392 extern struct NetprocFunctionHook *NetprocHookTable;
393
394
395 /*
396  * DeleteFunctionHook extensions are used for hooks which get called when a
397  * message is about to be deleted.
398  */
399 struct DeleteFunctionHook {
400         struct DeleteFunctionHook *next;
401         void (*h_function_pointer) (char *target_room, long msgnum);
402 };
403 extern struct DeleteFunctionHook *DeleteHookTable;
404
405
406 /*
407  * ExpressMessageFunctionHook extensions are used for hooks which implement
408  * the sending of an instant message through various channels.  Any function
409  * registered should return the number of recipients to whom the message was
410  * successfully transmitted.
411  */
412 struct XmsgFunctionHook {
413         struct XmsgFunctionHook *next;
414         int (*h_function_pointer) (char *, char *, char *);
415         int order;
416 };
417 extern struct XmsgFunctionHook *XmsgHookTable;
418
419 /* Priority levels for paging functions (lower is better) */
420 enum {
421         XMSG_PRI_LOCAL,         /* Other users on -this- server */
422         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
423         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
424         MAX_XMSG_PRI
425 };
426
427
428
429 /*
430  * ServiceFunctionHook extensions are used for hooks which implement various
431  * non-Citadel services (on TCP protocols) directly in the Citadel server.
432  */
433 struct ServiceFunctionHook {
434         struct ServiceFunctionHook *next;
435         int tcp_port;
436         char *sockpath;
437         void (*h_greeting_function) (void) ;
438         void (*h_command_function) (void) ;
439         void (*h_async_function) (void) ;
440         int msock;
441 };
442 extern struct ServiceFunctionHook *ServiceHookTable;
443
444
445
446 /* Defines the relationship of a user to a particular room */
447 struct visit {
448         long v_roomnum;
449         long v_roomgen;
450         long v_usernum;
451         long v_lastseen;
452         unsigned int v_flags;
453         char v_seen[SIZ];
454         char v_answered[SIZ];
455         int v_view;
456 };
457
458 #define V_FORGET        1       /* User has zapped this room        */
459 #define V_LOCKOUT       2       /* User is locked out of this room  */
460 #define V_ACCESS        4       /* Access is granted to this room   */
461
462
463 /* Supplementary data for a message on disk
464  * These are kept separate from the message itself for one of two reasons:
465  * 1. Either their values may change at some point after initial save, or
466  * 2. They are merely caches of data which exist somewhere else, for speed.
467  */
468 struct MetaData {
469         long meta_msgnum;               /* Message number in *local* message base */
470         int meta_refcount;              /* Number of rooms pointing to this msg */
471         char meta_content_type[64];     /* Cached MIME content-type */
472         long meta_rfc822_length;        /* Cache of RFC822-translated msg length */
473 };
474
475 /* Calls to AdjRefCount() are queued and deferred, so the user doesn't
476  * have to wait for various disk-intensive operations to complete synchronously.
477  * This is the record format.
478  */
479 struct arcq {
480         long arcq_msgnum;               /* Message number being adjusted */
481         int arcq_delta;                 /* Adjustment ( usually 1 or -1 ) */
482 };
483
484
485 /* 
486  * Serialization routines use this struct to return a pointer and a length
487  */
488 struct ser_ret {
489         size_t len;
490         unsigned char *ser;
491 };
492
493
494 /* Preferred field order */
495 /*               **********                     Important fields */
496 /*                         ***************      Semi-important fields */
497 /*                                        *     Message text (MUST be last) */
498 #define FORDER  "IPTAFONHRDBCEJGKLQSVWXZYUM"
499
500 #endif /* SERVER_H */