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