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