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