* sysdep.c: worker_thread(): Make scheduling a little more fair to higher
[citadel.git] / citadel / server.h
1 /* $Id$ */
2
3 /* Uncomment this if you want to track memory leaks.
4  * This incurs some overhead, so don't use it unless you're debugging the code!
5  */
6 /* #define DEBUG_MEMORY_LEAKS */
7
8
9 #ifndef SERVER_H
10 #define SERVER_H
11
12 #ifdef __GNUC__
13 #define INLINE __inline__
14 #else
15 #define INLINE
16 #endif
17
18 #include "citadel.h"
19 #ifdef HAVE_OPENSSL
20 #include <openssl/ssl.h>
21 #endif
22
23 /*
24  * New format for a message in memory
25  */
26 struct CtdlMessage {
27         int cm_magic;                   /* Self-check (NOT SAVED TO DISK) */
28         char cm_anon_type;              /* Anonymous or author-visible */
29         char cm_format_type;            /* Format type */
30         char *cm_fields[256];           /* Data fields */
31         unsigned int cm_flags;          /* How to handle (NOT SAVED TO DISK) */
32 };
33
34 #define CTDLMESSAGE_MAGIC               0x159d
35 #define CM_SKIP_HOOKS   0x01            /* Don't run server-side handlers */
36
37
38 /*
39  * Generic per-session variable or data structure storage
40  */
41 struct CtdlSessData {
42         struct CtdlSessData *next;
43         unsigned long sym_id;
44         void *sym_data;
45 };
46
47 /*
48  * Static user data symbol types.  Server extensions can ask for dynamic
49  * extensions to per-session data, but the symbol ID has to be listed here.
50  */
51 enum {
52         SYM_DESIRED_SECTION,            /* Used by the MIME parser */
53         SYM_MA_INFO,                    /* Handles multipart/alternative */
54         SYM_REPL,                       /* Used for replication checking */
55         SYM_CIT_ICAL,                   /* Used by the calendar service */
56         SYM_IMAP,                       /* Used by the IMAP service */
57         SYM_POP3,                       /* Used by the POP3 service */
58         SYM_SMTP,                       /* Used by the SMTP service */
59         SYM_SMTP_RECPS,
60         SYM_SMTP_ROOMS,
61         SYM_VCARD,                      /* vCard handling requires this */
62         SYM_MAX
63 };
64
65
66 /*
67  * Here's the big one... the Citadel context structure.
68  *
69  * This structure keeps track of all information relating to a running 
70  * session on the server.  We keep one of these for each session thread.
71  *
72  * Note that the first element is "*next" so that it may be used without
73  * modification in a linked list.
74  */
75 struct CitContext {
76         struct CitContext *next;        /* Link to next session in the list */
77
78         struct ctdluser user;   /* Database record buffers */
79         struct ctdlroom room;
80
81         int state;              /* thread state (see CON_ values below) */
82         int kill_me;            /* Set to nonzero to flag for termination */
83
84         char curr_user[USERNAME_SIZE];  /* name of current user */
85         int logged_in;          /* logged in */
86         int internal_pgm;       /* authenticated as internal program */
87         char temp[PATH_MAX];    /* temp file name */
88         int nologin;            /* not allowed to log in */
89         int is_local_socket;    /* set to 1 if client is on unix domain sock */
90
91         char net_node[PATH_MAX];/* Is the client another Citadel server? */
92         int client_socket;
93         int cs_pid;             /* session ID */
94         time_t lastcmd;         /* time of last command executed */
95         time_t lastidle;        /* For computing idle time */
96         time_t previous_login;  /* Date/time of previous login */
97         char lastcmdname[5];    /* name of last command executed */
98         unsigned cs_flags;      /* miscellaneous flags */
99         void (*h_command_function) (void) ;     /* service command function */
100         int is_async;           /* Nonzero if client accepts async msgs */
101
102         /* feeping creaturisms... */
103         int cs_clientdev;       /* client developer ID */
104         int cs_clienttyp;       /* client type code */
105         int cs_clientver;       /* client version number */
106         char cs_clientname[32]; /* name of client software */
107         char cs_host[64];       /* host logged in from */
108         char cs_addr[64];       /* address logged in from */
109
110         /* The Internet type of thing */
111         char cs_inet_email[SIZ];/* Return address of outbound Internet mail */
112
113         FILE *download_fp;      /* Fields relating to file transfer */
114         FILE *upload_fp;
115         char upl_file[PATH_MAX];
116         char upl_path[PATH_MAX];
117         char upl_comment[SIZ];
118         char upl_filedir[PATH_MAX];
119         char dl_is_net;
120         char upload_type;
121
122         /* Beginning of cryptography - session nonce */
123         char cs_nonce[NONCE_SIZE];      /* The nonce for this session's next auth transaction */
124
125         /* Redirect this session's output to somewhere else? */
126         FILE *redirect_fp;
127         int redirect_sock;
128 #ifdef HAVE_OPENSSL
129         SSL *ssl;
130         int redirect_ssl;
131 #endif
132
133         int buffering;
134         char *output_buffer;    /* hold output for one big dump */
135         int buffer_len;
136
137         /* A linked list of all express messages sent to us. */
138         struct ExpressMessage *FirstExpressMessage;
139         int disable_exp;        /* Set to 1 to disable incoming pages */
140         int newmail;            /* Other sessions increment this */
141
142         /* Masquerade... */
143         char fake_username[USERNAME_SIZE];      /* Fake username <bc> */ 
144         char fake_postname[USERNAME_SIZE];      /* Fake postname <bc> */
145         char fake_hostname[64];                 /* Fake hostname <bc> */
146         char fake_roomname[ROOMNAMELEN];        /* Fake roomname <bc> */
147
148         char preferred_formats[SIZ];            /* Preferred MIME formats */
149
150         /* Dynamically allocated session data */
151         struct CtdlSessData *FirstSessData;
152 };
153
154 typedef struct CitContext t_context;
155
156 /* Values for CitContext.state */
157 enum {
158         CON_IDLE,               /* This context is doing nothing */
159         CON_READY,              /* This context is ready-to-run */
160         CON_EXECUTING,          /* This context is bound to a thread */
161 };
162
163
164 #define CS_STEALTH      1       /* stealth mode */
165 #define CS_CHAT         2       /* chat mode */
166 #define CS_POSTING      4       /* Posting */
167
168 struct CitContext *MyContext(void);
169 #define CC MyContext()
170
171 extern struct CitContext *ContextList;
172 extern int ScheduledShutdown;
173 extern struct CitControl CitControl;
174
175
176 struct ExpressMessage {
177         struct ExpressMessage *next;
178         time_t timestamp;       /* When this message was sent */
179         unsigned flags;         /* Special instructions */
180         char sender[64];        /* Name of sending user */
181         char *text;             /* Message text (if applicable) */
182 };
183
184 #define EM_BROADCAST    1       /* Broadcast message */
185 #define EM_GO_AWAY      2       /* Server requests client log off */
186 #define EM_CHAT         4       /* Server requests client enter chat */
187
188 struct ChatLine {
189         struct ChatLine *next;
190         int chat_seq;
191         time_t chat_time;
192         char chat_text[SIZ];
193         char chat_username[USERNAME_SIZE];
194         char chat_room[ROOMNAMELEN];
195 };
196
197 /*
198  * Various things we need to lock and unlock
199  */
200 enum {
201         S_USERS,
202         S_ROOMS,
203         S_SESSION_TABLE,
204         S_FLOORTAB,
205         S_CHATQUEUE,
206         S_CONTROL,
207         S_DATABASE,
208         S_NETDB,
209         S_SUPPMSGMAIN,
210         S_I_WANNA_SELECT,
211         S_CONFIG,
212         S_WORKER_LIST,
213         S_HOUSEKEEPING,
214         S_NTTLIST,
215         S_DIRECTORY,
216         S_NETCONFIGS,
217         S_PUBLIC_CLIENTS,
218         S_LDAP,
219         S_FLOORCACHE,
220         MAX_SEMAPHORES
221 };
222
223
224 /*
225  * Upload types
226  */
227 #define UPL_FILE        0
228 #define UPL_NET         1
229 #define UPL_IMAGE       2
230
231
232 /*
233  * message transfer formats
234  */
235 enum {
236         MT_CITADEL,             /* Citadel proprietary */
237         MT_RFC822,              /* RFC822 */
238         MT_MIME,                /* MIME-formatted message */
239         MT_DOWNLOAD             /* Download a component */
240 };
241
242 /*
243  * Message format types in the database
244  */
245 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
246 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
247 #define FMT_RFC822      4       /* Standard (headers are in M field) */
248
249
250 /*
251  * Citadel DataBases (define one for each cdb we need to open)
252  */
253 enum {
254         CDB_MSGMAIN,            /* message base                  */
255         CDB_USERS,              /* user file                     */
256         CDB_ROOMS,              /* room index                    */
257         CDB_FLOORTAB,           /* floor index                   */
258         CDB_MSGLISTS,           /* room message lists            */
259         CDB_VISIT,              /* user/room relationships       */
260         CDB_DIRECTORY,          /* address book directory        */
261         CDB_USETABLE,           /* network use table             */
262         MAXCDB                  /* total number of CDB's defined */
263 };
264
265 struct cdbdata {
266         size_t len;
267         char *ptr;
268 };
269
270
271
272 /* Structures and declarations for function hooks of various types */
273
274 struct LogFunctionHook {
275         struct LogFunctionHook *next;
276         int loglevel;
277         void (*h_function_pointer) (char *);
278 };
279 extern struct LogFunctionHook *LogHookTable;
280
281 struct CleanupFunctionHook {
282         struct CleanupFunctionHook *next;
283         void (*h_function_pointer) (void);
284 };
285 extern struct CleanupFunctionHook *CleanupHookTable;
286
287
288
289
290 /*
291  * SessionFunctionHook extensions are used for any type of hook for which
292  * the context in which it's being called (which is determined by the event
293  * type) will make it obvious for the hook function to know where to look for
294  * pertinent data.
295  */
296 struct SessionFunctionHook {
297         struct SessionFunctionHook *next;
298         void (*h_function_pointer) (void);
299         int eventtype;
300 };
301 extern struct SessionFunctionHook *SessionHookTable;
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
316 #define EVT_TIMER       50      /* Timer events are called once per minute
317                                    and are not tied to any session */
318
319 /*
320  * UserFunctionHook extensions are used for any type of hook which implements
321  * an operation on a user or username (potentially) other than the one
322  * operating the current session.
323  */
324 struct UserFunctionHook {
325         struct UserFunctionHook *next;
326         void (*h_function_pointer) (struct ctdluser *usbuf);
327         int eventtype;
328 };
329 extern struct UserFunctionHook *UserHookTable;
330
331 #define EVT_PURGEUSER   100     /* Deleting a user */
332 #define EVT_NEWUSER     102     /* Creating a user */
333
334 /*
335  * MessageFunctionHook extensions are used for hooks which implement handlers
336  * for various types of message operations (save, read, etc.)
337  */
338 struct MessageFunctionHook {
339         struct MessageFunctionHook *next;
340         int (*h_function_pointer) (struct CtdlMessage *msg);
341         int eventtype;
342 };
343 extern struct MessageFunctionHook *MessageHookTable;
344
345 #define EVT_BEFOREREAD  200
346 #define EVT_BEFORESAVE  201
347 #define EVT_AFTERSAVE   202
348 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
349
350
351
352 /*
353  * NetprocFunctionHook extensions are used for hooks which implement handlers
354  * for incoming network messages.
355  */
356 struct NetprocFunctionHook {
357         struct NetprocFunctionHook *next;
358         int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
359 };
360 extern struct NetprocFunctionHook *NetprocHookTable;
361
362
363 /*
364  * DeleteFunctionHook extensions are used for hooks which get called when a
365  * message is about to be deleted.
366  */
367 struct DeleteFunctionHook {
368         struct DeleteFunctionHook *next;
369         void (*h_function_pointer) (char *target_room, long msgnum);
370 };
371 extern struct DeleteFunctionHook *DeleteHookTable;
372
373
374 /*
375  * ExpressMessageFunctionHook extensions are used for hooks which implement
376  * the sending of an express message through various channels.  Any function
377  * registered should return the number of recipients to whom the message was
378  * successfully transmitted.
379  */
380 struct XmsgFunctionHook {
381         struct XmsgFunctionHook *next;
382         int (*h_function_pointer) (char *, char *, char *);
383         int order;
384 };
385 extern struct XmsgFunctionHook *XmsgHookTable;
386
387 /* Priority levels for paging functions (lower is better) */
388 enum {
389         XMSG_PRI_LOCAL,         /* Other users on -this- server */
390         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
391         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
392         MAX_XMSG_PRI
393 };
394
395
396
397 /*
398  * ServiceFunctionHook extensions are used for hooks which implement various
399  * non-Citadel services (on TCP protocols) directly in the Citadel server.
400  */
401 struct ServiceFunctionHook {
402         struct ServiceFunctionHook *next;
403         int tcp_port;
404         char *sockpath;
405         void (*h_greeting_function) (void) ;
406         void (*h_command_function) (void) ;
407         int msock;
408 };
409 extern struct ServiceFunctionHook *ServiceHookTable;
410
411
412
413 /* Defines the relationship of a user to a particular room */
414 struct visit {
415         long v_roomnum;
416         long v_roomgen;
417         long v_usernum;
418         long v_lastseen;
419         unsigned int v_flags;
420         char v_seen[SIZ];
421         char v_answered[SIZ];
422         int v_view;
423 };
424
425 #define V_FORGET        1       /* User has zapped this room        */
426 #define V_LOCKOUT       2       /* User is locked out of this room  */
427 #define V_ACCESS        4       /* Access is granted to this room   */
428
429
430 /* Supplementary data for a message on disk
431  * (These are kept separately from the message itself because they are
432  * fields whose values may change at some point after the message is saved.)
433  */
434 struct MetaData {
435         long meta_msgnum;       /* Message number in *local* message base */
436         int meta_refcount;      /* Number of rooms which point to this msg */
437         char meta_content_type[64];
438         /* more stuff will be added to this record in the future */
439 };
440
441
442
443 /* Built-in debuggable stuff for checking for memory leaks */
444 #ifdef DEBUG_MEMORY_LEAKS
445
446 #define mallok(howbig)          tracked_malloc(howbig, __FILE__, __LINE__)
447 #define phree(whichptr)                 tracked_free(whichptr)
448 #define reallok(whichptr,howbig)        tracked_realloc(whichptr,howbig)
449 #define strdoop(orig)           tracked_strdup(orig, __FILE__, __LINE__)
450
451 void *tracked_malloc(size_t, char *, int);
452 void tracked_free(void *);
453 void *tracked_realloc(void *, size_t);
454 void dump_tracked(void);
455 char *tracked_strdup(const char *, char *, int);
456
457 struct TheHeap {
458         struct TheHeap *next;
459         char h_file[32];
460         int h_line;
461         void *h_ptr;
462 };
463
464 extern struct TheHeap *heap;
465
466 #else
467
468 #define mallok(howbig)                  malloc(howbig)
469 #define phree(whichptr)                 free(whichptr)
470 #define reallok(whichptr,howbig)        realloc(whichptr,howbig)
471 #define strdoop(orig)                   strdup(orig)
472
473
474 #endif
475
476
477 /* 
478  * Serialization routines use this struct to return a pointer and a length
479  */
480 struct ser_ret {
481         size_t len;
482         char *ser;
483 };
484
485
486 /* Preferred field order */
487 /*               **********                     Important fields */
488 /*                         ***************      Semi-important fields */
489 /*                                        *     Message text (MUST be last) */
490 #define FORDER  "IPTAFONHRDBCEGJKLQSVWXYZUM"
491
492 #endif /* SERVER_H */