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