do_hosekeeping is now a seperate thread. This releases worker threads
[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 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 /*
31  * New format for a message in memory
32  */
33 struct CtdlMessage {
34         int cm_magic;                   /* Self-check (NOT SAVED TO DISK) */
35         char cm_anon_type;              /* Anonymous or author-visible */
36         char cm_format_type;            /* Format type */
37         char *cm_fields[256];           /* Data fields */
38         unsigned int cm_flags;          /* How to handle (NOT SAVED TO DISK) */
39 };
40
41 #define CTDLMESSAGE_MAGIC               0x159d
42 #define CM_SKIP_HOOKS   0x01            /* Don't run server-side handlers */
43
44
45
46 /*
47  * Exit codes 101 through 109 are used for conditions in which
48  * we deliberately do NOT want the service to automatically
49  * restart.
50  */
51 #define CTDLEXIT_CONFIG         101     /* Could not read citadel.config */
52 #define CTDLEXIT_CONTROL        102     /* Could not acquire lock */
53 #define CTDLEXIT_HOME           103     /* Citadel home directory not found */
54 #define CTDLEXIT_OOD            104     /* Out Of Date config - rerun setup */
55 #define CTDLEXIT_DB             105     /* Unable to initialize database */
56
57
58
59
60 /*
61  * Here's the big one... the Citadel context structure.
62  *
63  * This structure keeps track of all information relating to a running 
64  * session on the server.  We keep one of these for each session thread.
65  *
66  */
67 struct CitContext {
68         struct CitContext *prev;        /* Link to previous session in list */
69         struct CitContext *next;        /* Link to next session in the list */
70
71         int state;              /* thread state (see CON_ values below) */
72         int kill_me;            /* Set to nonzero to flag for termination */
73         int client_socket;
74         int cs_pid;             /* session ID */
75         time_t lastcmd;         /* time of last command executed */
76         time_t lastidle;        /* For computing idle time */
77
78         char curr_user[USERNAME_SIZE];  /* name of current user */
79         int logged_in;          /* logged in */
80         int internal_pgm;       /* authenticated as internal program */
81         int nologin;            /* not allowed to log in */
82         int is_local_socket;    /* set to 1 if client is on unix domain sock */
83         int curr_view;          /* The view type for the current user/room */
84         int is_master;          /* Is this session logged in using the master user? */
85
86         char net_node[32]       ;/* Is the client another Citadel server? */
87         time_t previous_login;  /* Date/time of previous login */
88         char lastcmdname[5];    /* name of last command executed */
89         unsigned cs_flags;      /* miscellaneous flags */
90         void (*h_command_function) (void) ;     /* service command function */
91         void (*h_async_function) (void) ;       /* do async msgs function */
92         void (*h_greeting_function) (void) ;    /* service startup 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         /* Client information */
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[128];                /* Return address of outbound Internet mail */
107         char cs_inet_other_emails[1024];        /* User's other valid Internet email addresses */
108         char cs_inet_fn[128];                   /* Friendly-name of outbound Internet mail */
109
110         FILE *download_fp;      /* Fields relating to file transfer */
111         char download_desired_section[128];
112         FILE *upload_fp;
113         char upl_file[256];
114         char upl_path[PATH_MAX];
115         char upl_comment[256];
116         char upl_filedir[PATH_MAX];
117         char dl_is_net;
118         char upload_type;
119
120         struct ctdluser user;   /* Database record buffers */
121         struct ctdlroom room;
122
123         /* Beginning of cryptography - session nonce */
124         char cs_nonce[NONCE_SIZE];      /* The nonce for this session's next auth transaction */
125
126         /* Redirect this session's output to a memory buffer? */
127         char *redirect_buffer;          /* the buffer */
128         size_t redirect_len;            /* length of data in buffer */
129         size_t redirect_alloc;          /* length of allocated buffer */
130 #ifdef HAVE_OPENSSL
131         SSL *ssl;
132         int redirect_ssl;
133 #endif
134
135         int buffering;
136         char *output_buffer;    /* hold output for one big dump */
137         int buffer_len;
138
139         /* A linked list of all instant messages sent to us. */
140         struct ExpressMessage *FirstExpressMessage;
141         int disable_exp;        /* Set to 1 to disable incoming pages */
142         int newmail;            /* Other sessions increment this */
143
144         /* Masqueraded values in the 'who is online' list */
145         char fake_username[USERNAME_SIZE];
146         char fake_hostname[64];
147         char fake_roomname[ROOMNAMELEN];
148
149         /* Preferred MIME formats */
150         char preferred_formats[256];
151         int msg4_dont_decode;
152
153         /* Dynamically allocated session data */
154         char *session_specific_data;            /* Used by individual protocol modules */
155         struct cit_ical *CIT_ICAL;              /* calendaring data */
156         struct ma_info *ma;                     /* multipart/alternative data */
157         const char* ServiceName;                /**< whats our actual purpose? */
158         
159         struct timeval client_expires_at;               /** When this client will expire */
160 };
161
162 typedef struct CitContext t_context;
163
164 /*
165  * Values for CitContext.state
166  * 
167  * A session that is doing nothing is in CON_IDLE state.  When activity
168  * is detected on the socket, it goes to CON_READY, indicating that it
169  * needs to have a worker thread bound to it.  When a thread binds to
170  * the session, it goes to CON_EXECUTING and does its thing.  When the
171  * transaction is finished, the thread sets it back to CON_IDLE and lets
172  * it go.
173  */
174 enum {
175         CON_IDLE,               /* This context is doing nothing */
176         CON_START,              /* This context is starting up */
177         CON_READY,              /* This context needs attention */
178         CON_EXECUTING           /* This context is bound to a thread */
179 };
180
181
182 #define CS_STEALTH      1       /* stealth mode */
183 #define CS_CHAT         2       /* chat mode */
184 #define CS_POSTING      4       /* Posting */
185
186 struct CitContext *MyContext(void);
187 #define CC MyContext()
188
189 /*
190  * This is the control record for the message base... 
191  */
192 struct CitControl {
193         long MMhighest;                 /* highest message number in file   */
194         unsigned MMflags;               /* Global system flags              */
195         long MMnextuser;                /* highest user number on system    */
196         long MMnextroom;                /* highest room number on system    */
197         int version;                    /* Server-hosted upgrade level      */
198         int fulltext_wordbreaker;       /* ID of wordbreaker in use         */
199         long MMfulltext;                /* highest message number indexed   */
200 };
201
202 extern struct CitContext *ContextList;
203 extern int ScheduledShutdown;
204 extern struct CitControl CitControl;
205
206 struct ExpressMessage {
207         struct ExpressMessage *next;
208         time_t timestamp;       /* When this message was sent */
209         unsigned flags;         /* Special instructions */
210         char sender[256];       /* Name of sending user */
211         char sender_email[256]; /* Email or JID of sending user */
212         char *text;             /* Message text (if applicable) */
213 };
214
215 #define EM_BROADCAST    1       /* Broadcast message */
216 #define EM_GO_AWAY      2       /* Server requests client log off */
217 #define EM_CHAT         4       /* Server requests client enter chat */
218
219 struct ChatLine {
220         struct ChatLine *next;
221         int chat_seq;
222         time_t chat_time;
223         char chat_text[SIZ];
224         char chat_username[USERNAME_SIZE];
225         char chat_room[ROOMNAMELEN];
226 };
227
228 /*
229  * Various things we need to lock and unlock
230  */
231 enum {
232         S_USERS,
233         S_ROOMS,
234         S_SESSION_TABLE,
235         S_FLOORTAB,
236         S_CHATQUEUE,
237         S_CONTROL,
238         S_NETDB,
239         S_SUPPMSGMAIN,
240         S_CONFIG,
241         S_HOUSEKEEPING,
242         S_NTTLIST,
243         S_DIRECTORY,
244         S_NETCONFIGS,
245         S_PUBLIC_CLIENTS,
246         S_LDAP,
247         S_FLOORCACHE,
248         S_DEBUGMEMLEAKS,
249         S_ATBF,
250         S_JOURNAL_QUEUE,
251         S_RPLIST,
252         S_SIEVELIST,
253         S_CHKPWD,
254         S_LOG,
255         S_NETSPOOL,
256         S_THREAD_LIST,
257         S_XMPP_QUEUE,
258         S_SCHEDULE_LIST,
259         MAX_SEMAPHORES
260 };
261
262
263 /*
264  * Upload types
265  */
266 #define UPL_FILE        0
267 #define UPL_NET         1
268 #define UPL_IMAGE       2
269
270
271 /*
272  * message transfer formats
273  */
274 enum {
275         MT_CITADEL,             /* Citadel proprietary */
276         MT_RFC822,              /* RFC822 */
277         MT_MIME,                /* MIME-formatted message */
278         MT_DOWNLOAD,            /* Download a component */
279         MT_SPEW_SECTION         /* Download a component in a single operation */
280 };
281
282 /*
283  * Message format types in the database
284  */
285 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
286 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
287 #define FMT_RFC822      4       /* Standard (headers are in M field) */
288
289
290 /*
291  * Citadel DataBases (define one for each cdb we need to open)
292  */
293 enum {
294         CDB_MSGMAIN,            /* message base                  */
295         CDB_USERS,              /* user file                     */
296         CDB_ROOMS,              /* room index                    */
297         CDB_FLOORTAB,           /* floor index                   */
298         CDB_MSGLISTS,           /* room message lists            */
299         CDB_VISIT,              /* user/room relationships       */
300         CDB_DIRECTORY,          /* address book directory        */
301         CDB_USETABLE,           /* network use table             */
302         CDB_BIGMSGS,            /* larger message bodies         */
303         CDB_FULLTEXT,           /* full text search index        */
304         CDB_EUIDINDEX,          /* locate msgs by EUID           */
305         MAXCDB                  /* total number of CDB's defined */
306 };
307
308 struct cdbdata {
309         size_t len;
310         char *ptr;
311 };
312
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 #define EVT_STEALTH     9       /* Entering stealth mode */
328 #define EVT_UNSTEALTH   10      /* Exiting stealth mode */
329
330 #define EVT_TIMER       50      /* Timer events are called once per minute
331                                    and are not tied to any session */
332 #define EVT_HOUSE       51      /* as needed houskeeping stuff */
333
334 #define EVT_PURGEUSER   100     /* Deleting a user */
335 #define EVT_NEWUSER     102     /* Creating a user */
336
337 #define EVT_BEFOREREAD  200
338 #define EVT_BEFORESAVE  201
339 #define EVT_AFTERSAVE   202
340 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
341 /* Priority levels for paging functions (lower is better) */
342 enum {
343         XMSG_PRI_LOCAL,         /* Other users on -this- server */
344         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
345         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
346         MAX_XMSG_PRI
347 };
348
349
350 /* Defines the relationship of a user to a particular room */
351 struct visit {
352         long v_roomnum;
353         long v_roomgen;
354         long v_usernum;
355         long v_lastseen;
356         unsigned int v_flags;
357         char v_seen[SIZ];
358         char v_answered[SIZ];
359         int v_view;
360 };
361
362 #define V_FORGET        1       /* User has zapped this room        */
363 #define V_LOCKOUT       2       /* User is locked out of this room  */
364 #define V_ACCESS        4       /* Access is granted to this room   */
365
366
367 /* Supplementary data for a message on disk
368  * These are kept separate from the message itself for one of two reasons:
369  * 1. Either their values may change at some point after initial save, or
370  * 2. They are merely caches of data which exist somewhere else, for speed.
371  */
372 struct MetaData {
373         long meta_msgnum;               /* Message number in *local* message base */
374         int meta_refcount;              /* Number of rooms pointing to this msg */
375         char meta_content_type[64];     /* Cached MIME content-type */
376         long meta_rfc822_length;        /* Cache of RFC822-translated msg length */
377 };
378
379 /* Calls to AdjRefCount() are queued and deferred, so the user doesn't
380  * have to wait for various disk-intensive operations to complete synchronously.
381  * This is the record format.
382  */
383 struct arcq {
384         long arcq_msgnum;               /* Message number being adjusted */
385         int arcq_delta;                 /* Adjustment ( usually 1 or -1 ) */
386 };
387
388
389 /* 
390  * Serialization routines use this struct to return a pointer and a length
391  */
392 struct ser_ret {
393         size_t len;
394         unsigned char *ser;
395 };
396
397
398 /*
399  * The S_USETABLE database is used in several modules now, so we define its format here.
400  */
401 struct UseTable {
402         char ut_msgid[SIZ];
403         time_t ut_timestamp;
404 };
405
406
407
408 /* Preferred field order                                                        */
409 /*               **********                     Important fields                */
410 /*                         ***************      Semi-important fields           */
411 /*                                        *     Message text (MUST be last)     */
412 #define FORDER  "IPTAFONHRDBCEJGKLQSVWXZYUM"
413
414 #endif /* SERVER_H */