Change around the order of features offered as
[citadel.git] / citadel / modules / jabber / serv_xmpp.c
1 /*
2  * $Id$ 
3  *
4  * XMPP (Jabber) service for the Citadel system
5  * Copyright (c) 2007 by Art Cancro
6  * This code is released under the terms of the GNU General Public License.
7  *
8  */
9
10 #include "sysdep.h"
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <pwd.h>
17 #include <errno.h>
18 #include <sys/types.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <sys/wait.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <ctype.h>
35 #include <libcitadel.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "citserver.h"
39 #include "support.h"
40 #include "config.h"
41 #include "room_ops.h"
42 #include "user_ops.h"
43 #include "policy.h"
44 #include "database.h"
45 #include "msgbase.h"
46 #include "internet_addressing.h"
47 #include "md5.h"
48 #include "ctdl_module.h"
49
50 #ifdef HAVE_EXPAT
51 #include <expat.h>
52 #include "serv_xmpp.h"
53
54 struct xmpp_event *xmpp_queue = NULL;
55
56 /* We have just received a <stream> tag from the client, so send them ours */
57
58 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
59 {
60
61         lprintf(CTDL_DEBUG, "New stream detected.\n");
62
63         while (*attr) {
64                 if (!strcasecmp(attr[0], "to")) {
65                         safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
66                 }
67                 attr += 2;
68         }
69
70         cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
71
72         cprintf("<stream:stream ");
73         cprintf("from=\"%s\" ", XMPP->server_name);
74         cprintf("id=\"%08x\" ", CC->cs_pid);
75         cprintf("version=\"1.0\" ");
76         cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
77         cprintf("xmlns=\"jabber:client\">");
78
79         /* The features of this stream are... */
80         cprintf("<stream:features>");
81
82 #ifdef HAVE_OPENSSL_XXXX_COMMENTED_OUT
83         /* TLS encryption (but only if it isn't already active) */
84         if (!CC->redirect_ssl) {
85                 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
86         }
87 #endif
88
89         if (!CC->logged_in) {
90                 /* If we're not logged in yet, offer SASL as our feature set */
91                 xmpp_output_auth_mechs();
92         }
93
94         /* Offer binding and sessions as part of our feature set */
95         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
96         cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
97
98         cprintf("</stream:features>");
99
100         CC->is_async = 1;               /* XMPP sessions are inherently async-capable */
101 }
102
103
104 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
105         char el[256];
106         char *sep = NULL;
107         int i;
108
109         /* Axe the namespace, we don't care about it */
110         safestrncpy(el, supplied_el, sizeof el);
111         while (sep = strchr(el, ':'), sep) {
112                 strcpy(el, ++sep);
113         }
114
115         lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
116
117         for (i=0; attr[i] != NULL; i+=2) {
118                 lprintf(CTDL_DEBUG, "                    Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
119         }
120
121         if (!strcasecmp(el, "stream")) {
122                 xmpp_stream_start(data, supplied_el, attr);
123         }
124
125         else if (!strcasecmp(el, "query")) {
126                 XMPP->iq_query_xmlns[0] = 0;
127                 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
128         }
129
130         else if (!strcasecmp(el, "iq")) {
131                 for (i=0; attr[i] != NULL; i+=2) {
132                         if (!strcasecmp(attr[i], "type")) {
133                                 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
134                         }
135                         else if (!strcasecmp(attr[i], "id")) {
136                                 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
137                         }
138                         else if (!strcasecmp(attr[i], "from")) {
139                                 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
140                         }
141                         else if (!strcasecmp(attr[i], "to")) {
142                                 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
143                         }
144                 }
145         }
146
147         else if (!strcasecmp(el, "auth")) {
148                 XMPP->sasl_auth_mech[0] = 0;
149                 for (i=0; attr[i] != NULL; i+=2) {
150                         if (!strcasecmp(attr[i], "mechanism")) {
151                                 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
152                         }
153                 }
154         }
155
156         else if (!strcasecmp(el, "message")) {
157                 for (i=0; attr[i] != NULL; i+=2) {
158                         if (!strcasecmp(attr[i], "to")) {
159                                 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
160                         }
161                 }
162         }
163
164         else if (!strcasecmp(el, "html")) {
165                 ++XMPP->html_tag_level;
166         }
167 }
168
169
170
171 void xmpp_xml_end(void *data, const char *supplied_el) {
172         char el[256];
173         char *sep = NULL;
174
175         /* Axe the namespace, we don't care about it */
176         safestrncpy(el, supplied_el, sizeof el);
177         while (sep = strchr(el, ':'), sep) {
178                 strcpy(el, ++sep);
179         }
180
181         lprintf(CTDL_DEBUG, "XMPP ELEMENT END  : <%s>\n", el);
182         if (XMPP->chardata_len > 0) {
183                 lprintf(CTDL_DEBUG, "          chardata: %s\n", XMPP->chardata);
184         }
185
186         if (!strcasecmp(el, "resource")) {
187                 if (XMPP->chardata_len > 0) {
188                         safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
189                                 sizeof XMPP->iq_client_resource);
190                 }
191         }
192
193         else if (!strcasecmp(el, "iq")) {
194
195                 /*
196                  * iq type="get" (handle queries)
197                  */
198                 if (!strcasecmp(XMPP->iq_type, "get")) {
199
200                         /*
201                          * Query on a namespace
202                          */
203                         if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
204                                 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
205                                                 XMPP->iq_to, XMPP->iq_query_xmlns);
206                         }
207
208                         /*
209                          * Unknown queries ... return the XML equivalent of a blank stare
210                          */
211                         else {
212                                 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
213                                 cprintf("</iq>");
214                         }
215                 }
216
217                 /*
218                  * If this <iq> stanza was a "bind" attempt, process it ...
219                  */
220                 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
221
222                         /* Generate the "full JID" of the client resource */
223
224                         snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
225                                 "%s/%s",
226                                 CC->cs_inet_email,
227                                 XMPP->iq_client_resource
228                         );
229
230                         /* Tell the client what its JID is */
231
232                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
233                         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
234                         cprintf("<jid>%s</jid>", XMPP->client_jid);
235                         cprintf("</bind>");
236                         cprintf("</iq>");
237                 }
238
239                 else if (XMPP->iq_session) {
240                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
241                         cprintf("</iq>");
242                 }
243
244                 else {
245                         cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
246                         cprintf("<error></error>");
247                         cprintf("</iq>");
248                 }
249
250                 /* Now clear these fields out so they don't get used by a future stanza */
251                 XMPP->iq_id[0] = 0;
252                 XMPP->iq_from[0] = 0;
253                 XMPP->iq_to[0] = 0;
254                 XMPP->iq_type[0] = 0;
255                 XMPP->iq_client_resource[0] = 0;
256                 XMPP->iq_session = 0;
257                 XMPP->iq_query_xmlns[0] = 0;
258         }
259
260         else if (!strcasecmp(el, "auth")) {
261
262                 /* Try to authenticate (this function is responsible for the output stanza) */
263                 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
264
265                 /* Now clear these fields out so they don't get used by a future stanza */
266                 XMPP->sasl_auth_mech[0] = 0;
267         }
268
269         else if (!strcasecmp(el, "session")) {
270                 XMPP->iq_session = 1;
271         }
272
273         else if (!strcasecmp(el, "presence")) {
274
275                 /* Respond to a <presence> update by firing back with presence information
276                  * on the entire wholist.  Check this assumption, it's probably wrong.
277                  */
278                 jabber_wholist_presence_dump();
279         }
280
281         else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
282                 if (XMPP->message_body != NULL) {
283                         free(XMPP->message_body);
284                         XMPP->message_body = NULL;
285                 }
286                 if (XMPP->chardata_len > 0) {
287                         XMPP->message_body = strdup(XMPP->chardata);
288                 }
289         }
290
291         else if (!strcasecmp(el, "message")) {
292                 jabber_send_message(XMPP->message_to, XMPP->message_body);
293                 XMPP->html_tag_level = 0;
294         }
295
296         else if (!strcasecmp(el, "html")) {
297                 --XMPP->html_tag_level;
298         }
299
300         else if (!strcasecmp(el, "starttls")) {
301 #ifdef HAVE_OPENSSL
302         cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
303         CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
304         if (!CC->redirect_ssl) CC->kill_me = 1;
305 #else
306         cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
307         CC->kill_me = 1;
308 #endif
309         }
310
311         XMPP->chardata_len = 0;
312         if (XMPP->chardata_alloc > 0) {
313                 XMPP->chardata[0] = 0;
314         }
315 }
316
317
318 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
319 {
320         struct citxmpp *X = XMPP;
321
322         if (X->chardata_alloc == 0) {
323                 X->chardata_alloc = SIZ;
324                 X->chardata = malloc(X->chardata_alloc);
325         }
326         if ((X->chardata_len + len + 1) > X->chardata_alloc) {
327                 X->chardata_alloc = X->chardata_len + len + 1024;
328                 X->chardata = realloc(X->chardata, X->chardata_alloc);
329         }
330         memcpy(&X->chardata[X->chardata_len], s, len);
331         X->chardata_len += len;
332         X->chardata[X->chardata_len] = 0;
333 }
334
335
336 /*
337  * This cleanup function blows away the temporary memory and files used by the XMPP service.
338  */
339 void xmpp_cleanup_function(void) {
340
341         /* Don't do this stuff if this is not a XMPP session! */
342         if (CC->h_command_function != xmpp_command_loop) return;
343
344         lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
345         if (XMPP->chardata != NULL) {
346                 free(XMPP->chardata);
347                 XMPP->chardata = NULL;
348                 XMPP->chardata_len = 0;
349                 XMPP->chardata_alloc = 0;
350                 if (XMPP->message_body != NULL) {
351                         free(XMPP->message_body);
352                 }
353         }
354         XML_ParserFree(XMPP->xp);
355         free(XMPP);
356 }
357
358
359
360 /*
361  * Here's where our XMPP session begins its happy day.
362  */
363 void xmpp_greeting(void) {
364         strcpy(CC->cs_clientname, "Jabber session");
365         CC->session_specific_data = malloc(sizeof(struct citxmpp));
366         memset(XMPP, 0, sizeof(struct citxmpp));
367         XMPP->last_event_processed = queue_event_seq;
368
369         /* XMPP does not use a greeting, but we still have to initialize some things. */
370
371         XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
372         if (XMPP->xp == NULL) {
373                 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
374                 CC->kill_me = 1;
375                 return;
376         }
377
378         XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
379         XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
380         // XML_SetUserData(XMPP->xp, something...);
381 }
382
383
384 /* 
385  * Main command loop for XMPP sessions.
386  */
387 void xmpp_command_loop(void) {
388         char cmdbuf[16];
389         int retval;
390
391         time(&CC->lastcmd);
392         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
393         retval = client_read(cmdbuf, 1);
394         if (retval != 1) {
395                 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
396                 CC->kill_me = 1;
397                 return;
398         }
399
400         /* FIXME ... this is woefully inefficient. */
401
402         XML_Parse(XMPP->xp, cmdbuf, 1, 0);
403 }
404
405
406 /*
407  * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
408  */
409 void xmpp_async_loop(void) {
410         xmpp_process_events();
411         jabber_output_incoming_messages();
412 }
413
414
415 /*
416  * Login hook for XMPP sessions
417  */
418 void xmpp_login_hook(void) {
419         xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
420 }
421
422
423 /*
424  * Logout hook for XMPP sessions
425  */
426 void xmpp_logout_hook(void) {
427         xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
428 }
429
430
431 const char *CitadelServiceXMPP="XMPP";
432
433 #endif  /* HAVE_EXPAT */
434
435 CTDL_MODULE_INIT(jabber)
436 {
437 #ifdef HAVE_EXPAT
438         if (!threading) {
439                 CtdlRegisterServiceHook(config.c_xmpp_c2s_port,
440                                         NULL,
441                                         xmpp_greeting,
442                                         xmpp_command_loop,
443                                         xmpp_async_loop,
444                                         CitadelServiceXMPP);
445                 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
446                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
447                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
448                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH);
449                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH);
450         #else
451                 lprintf(CTDL_INFO, "This server is missing the Expat XML parser.  Jabber service will be disabled.\n");
452 #endif
453         }
454
455         /* return our Subversion id for the Log */
456         return "$Id$";
457 }