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