Fix race condition that caused segfaults in imap and xmpp as seen on
[citadel.git] / citadel / context.h
1 /* $Id: sysdep_decls.h 7265 2009-03-25 23:18:46Z dothebart $ */
2
3 #ifndef CONTEXT_H
4 #define CONTEXT_H
5
6 #include <stdarg.h>
7 #include "sysdep.h"
8 #include "server.h"
9 #include "sysdep_decls.h"
10 #include "threads.h"
11
12
13 /*
14  * Here's the big one... the Citadel context structure.
15  *
16  * This structure keeps track of all information relating to a running 
17  * session on the server.  We keep one of these for each session thread.
18  *
19  */
20 struct CitContext {
21         struct CitContext *prev;        /* Link to previous session in list */
22         struct CitContext *next;        /* Link to next session in the list */
23
24         int state;              /* thread state (see CON_ values below) */
25         int kill_me;            /* Set to nonzero to flag for termination */
26         int client_socket;
27         int cs_pid;             /* session ID */
28         int dont_term;          /* for special activities like artv so we don't get killed */
29         time_t lastcmd;         /* time of last command executed */
30         time_t lastidle;        /* For computing idle time */
31
32         char curr_user[USERNAME_SIZE];  /* name of current user */
33         int logged_in;          /* logged in */
34         int internal_pgm;       /* authenticated as internal program */
35         int nologin;            /* not allowed to log in */
36         int is_local_socket;    /* set to 1 if client is on unix domain sock */
37         int curr_view;          /* The view type for the current user/room */
38         int is_master;          /* Is this session logged in using the master user? */
39
40         char net_node[32]       ;/* Is the client another Citadel server? */
41         time_t previous_login;  /* Date/time of previous login */
42         char lastcmdname[5];    /* name of last command executed */
43         unsigned cs_flags;      /* miscellaneous flags */
44         void (*h_command_function) (void) ;     /* service command function */
45         void (*h_async_function) (void) ;       /* do async msgs function */
46         void (*h_greeting_function) (void) ;    /* greeting function for session startup */
47         int is_async;           /* Nonzero if client accepts async msgs */
48         int async_waiting;      /* Nonzero if there are async msgs waiting */
49         int input_waiting;      /* Nonzero if there is client input waiting */
50         int can_receive_im;     /* Session is capable of receiving instant messages */
51
52         /* Client information */
53         int cs_clientdev;       /* client developer ID */
54         int cs_clienttyp;       /* client type code */
55         int cs_clientver;       /* client version number */
56         uid_t cs_UDSclientUID;  /* the uid of the client when talking via UDS */
57         char cs_clientname[32]; /* name of client software */
58         char cs_host[64];       /* host logged in from */
59         char cs_addr[64];       /* address logged in from */
60
61         /* The Internet type of thing */
62         char cs_inet_email[128];                /* Return address of outbound Internet mail */
63         char cs_inet_other_emails[1024];        /* User's other valid Internet email addresses */
64         char cs_inet_fn[128];                   /* Friendly-name of outbound Internet mail */
65
66         FILE *download_fp;      /* Fields relating to file transfer */
67         char download_desired_section[128];
68         FILE *upload_fp;
69         char upl_file[256];
70         char upl_path[PATH_MAX];
71         char upl_comment[256];
72         char upl_filedir[PATH_MAX];
73         char upl_mimetype[64];
74         char dl_is_net;
75         char upload_type;
76
77         struct ctdluser user;   /* Database record buffers */
78         struct ctdlroom room;
79
80         /* Beginning of cryptography - session nonce */
81         char cs_nonce[NONCE_SIZE];      /* The nonce for this session's next auth transaction */
82
83         /* Redirect this session's output to a memory buffer? */
84         char *redirect_buffer;          /* the buffer */
85         size_t redirect_len;            /* length of data in buffer */
86         size_t redirect_alloc;          /* length of allocated buffer */
87 #ifdef HAVE_OPENSSL
88         SSL *ssl;
89         int redirect_ssl;
90 #endif
91
92         /* A linked list of all instant messages sent to us. */
93         struct ExpressMessage *FirstExpressMessage;
94         int disable_exp;        /* Set to 1 to disable incoming pages */
95         int newmail;            /* Other sessions increment this */
96
97         /* Masqueraded values in the 'who is online' list */
98         char fake_username[USERNAME_SIZE];
99         char fake_hostname[64];
100         char fake_roomname[ROOMNAMELEN];
101
102         /* Preferred MIME formats */
103         char preferred_formats[256];
104         int msg4_dont_decode;
105
106         /* Dynamically allocated session data */
107         char *session_specific_data;            /* Used by individual protocol modules */
108         struct cit_ical *CIT_ICAL;              /* calendaring data */
109         struct ma_info *ma;                     /* multipart/alternative data */
110         const char *ServiceName;                /* readable purpose of this session */
111         void *openid_data;                      /* Data stored by the OpenID module */
112         char *ldap_dn;                          /* DN of user when using AUTHMODE_LDAP */
113 };
114
115 typedef struct CitContext CitContext;
116
117 /*
118  * Values for CitContext.state
119  * 
120  * A session that is doing nothing is in CON_IDLE state.  When activity
121  * is detected on the socket, it goes to CON_READY, indicating that it
122  * needs to have a worker thread bound to it.  When a thread binds to
123  * the session, it goes to CON_EXECUTING and does its thing.  When the
124  * transaction is finished, the thread sets it back to CON_IDLE and lets
125  * it go.
126  */
127 enum {
128         CON_IDLE,               /* This context is doing nothing */
129         CON_GREETING,           /* This context needs to output its greeting */
130         CON_STARTING,           /* This context is outputting its greeting */
131         CON_READY,              /* This context needs attention */
132         CON_EXECUTING           /* This context is bound to a thread */
133 };
134
135 #define CC MyContext()
136
137
138 extern citthread_key_t MyConKey;                        /* TSD key for MyContext() */
139 extern int num_sessions;
140 extern CitContext masterCC;
141 extern CitContext *ContextList;
142
143 CitContext *MyContext (void);
144 void RemoveContext (struct CitContext *);
145 CitContext *CreateNewContext (void);
146 void context_cleanup(void);
147 void kill_session (int session_to_kill);
148 INLINE void become_session(struct CitContext *which_con);
149 void InitializeMasterCC(void);
150 void dead_session_purge(int force);
151
152 /* Deprecated, user CtdlBumpNewMailCounter() instead */
153 void BumpNewMailCounter(long) __attribute__ ((deprecated));
154
155 void terminate_idle_sessions(void);
156 int CtdlTerminateOtherSession (int session_num);
157 /* bits returned by CtdlTerminateOtherSession */
158 #define TERM_FOUND      0x01
159 #define TERM_ALLOWED    0x02
160 #define TERM_KILLED     0x03
161 #define TERM_NOTALLOWED -1
162 #endif /* CONTEXT_H */