b7357925707b7051f4d15bdef7358ebeb5cdc060
[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         else {
94                 /* If we've logged in, now offer binding and sessions as 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
99         cprintf("</stream:features>");
100
101         CC->is_async = 1;               /* XMPP sessions are inherently async-capable */
102 }
103
104
105 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
106         char el[256];
107         char *sep = NULL;
108         int i;
109
110         /* Axe the namespace, we don't care about it */
111         safestrncpy(el, supplied_el, sizeof el);
112         while (sep = strchr(el, ':'), sep) {
113                 strcpy(el, ++sep);
114         }
115
116         lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
117
118         for (i=0; attr[i] != NULL; i+=2) {
119                 lprintf(CTDL_DEBUG, "                    Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
120         }
121
122         if (!strcasecmp(el, "stream")) {
123                 xmpp_stream_start(data, supplied_el, attr);
124         }
125
126         else if (!strcasecmp(el, "query")) {
127                 XMPP->iq_query_xmlns[0] = 0;
128                 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
129         }
130
131         else if (!strcasecmp(el, "iq")) {
132                 for (i=0; attr[i] != NULL; i+=2) {
133                         if (!strcasecmp(attr[i], "type")) {
134                                 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
135                         }
136                         else if (!strcasecmp(attr[i], "id")) {
137                                 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
138                         }
139                         else if (!strcasecmp(attr[i], "from")) {
140                                 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
141                         }
142                         else if (!strcasecmp(attr[i], "to")) {
143                                 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
144                         }
145                 }
146         }
147
148         else if (!strcasecmp(el, "auth")) {
149                 XMPP->sasl_auth_mech[0] = 0;
150                 for (i=0; attr[i] != NULL; i+=2) {
151                         if (!strcasecmp(attr[i], "mechanism")) {
152                                 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
153                         }
154                 }
155         }
156
157         else if (!strcasecmp(el, "message")) {
158                 for (i=0; attr[i] != NULL; i+=2) {
159                         if (!strcasecmp(attr[i], "to")) {
160                                 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
161                         }
162                 }
163         }
164
165         else if (!strcasecmp(el, "html")) {
166                 ++XMPP->html_tag_level;
167         }
168 }
169
170
171
172 void xmpp_xml_end(void *data, const char *supplied_el) {
173         char el[256];
174         char *sep = NULL;
175
176         /* Axe the namespace, we don't care about it */
177         safestrncpy(el, supplied_el, sizeof el);
178         while (sep = strchr(el, ':'), sep) {
179                 strcpy(el, ++sep);
180         }
181
182         lprintf(CTDL_DEBUG, "XMPP ELEMENT END  : <%s>\n", el);
183         if (XMPP->chardata_len > 0) {
184                 lprintf(CTDL_DEBUG, "          chardata: %s\n", XMPP->chardata);
185         }
186
187         if (!strcasecmp(el, "resource")) {
188                 if (XMPP->chardata_len > 0) {
189                         safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
190                                 sizeof XMPP->iq_client_resource);
191                 }
192         }
193
194         else if (!strcasecmp(el, "iq")) {
195
196                 /*
197                  * iq type="get" (handle queries)
198                  */
199                 if (!strcasecmp(XMPP->iq_type, "get")) {
200
201                         /*
202                          * Query on a namespace
203                          */
204                         if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
205                                 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
206                                                 XMPP->iq_to, XMPP->iq_query_xmlns);
207                         }
208
209                         /*
210                          * Unknown queries ... return the XML equivalent of a blank stare
211                          */
212                         else {
213                                 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
214                                 cprintf("</iq>");
215                         }
216                 }
217
218                 /*
219                  * If this <iq> stanza was a "bind" attempt, process it ...
220                  */
221                 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
222
223                         /* Generate the "full JID" of the client resource */
224
225                         snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
226                                 "%s/%s",
227                                 CC->cs_inet_email,
228                                 XMPP->iq_client_resource
229                         );
230
231                         /* Tell the client what its JID is */
232
233                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
234                         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
235                         cprintf("<jid>%s</jid>", XMPP->client_jid);
236                         cprintf("</bind>");
237                         cprintf("</iq>");
238                 }
239
240                 else if (XMPP->iq_session) {
241                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
242                         cprintf("</iq>");
243                 }
244
245                 else {
246                         cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
247                         cprintf("<error></error>");
248                         cprintf("</iq>");
249                 }
250
251                 /* Now clear these fields out so they don't get used by a future stanza */
252                 XMPP->iq_id[0] = 0;
253                 XMPP->iq_from[0] = 0;
254                 XMPP->iq_to[0] = 0;
255                 XMPP->iq_type[0] = 0;
256                 XMPP->iq_client_resource[0] = 0;
257                 XMPP->iq_session = 0;
258                 XMPP->iq_query_xmlns[0] = 0;
259         }
260
261         else if (!strcasecmp(el, "auth")) {
262
263                 /* Try to authenticate (this function is responsible for the output stanza) */
264                 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
265
266                 /* Now clear these fields out so they don't get used by a future stanza */
267                 XMPP->sasl_auth_mech[0] = 0;
268         }
269
270         else if (!strcasecmp(el, "session")) {
271                 XMPP->iq_session = 1;
272         }
273
274         else if (!strcasecmp(el, "presence")) {
275
276                 /* Respond to a <presence> update by firing back with presence information
277                  * on the entire wholist.  Check this assumption, it's probably wrong.
278                  */
279                 jabber_wholist_presence_dump();
280         }
281
282         else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
283                 if (XMPP->message_body != NULL) {
284                         free(XMPP->message_body);
285                         XMPP->message_body = NULL;
286                 }
287                 if (XMPP->chardata_len > 0) {
288                         XMPP->message_body = strdup(XMPP->chardata);
289                 }
290         }
291
292         else if (!strcasecmp(el, "message")) {
293                 jabber_send_message(XMPP->message_to, XMPP->message_body);
294                 XMPP->html_tag_level = 0;
295         }
296
297         else if (!strcasecmp(el, "html")) {
298                 --XMPP->html_tag_level;
299         }
300
301         else if (!strcasecmp(el, "starttls")) {
302 #ifdef HAVE_OPENSSL
303         cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
304         CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
305         if (!CC->redirect_ssl) CC->kill_me = 1;
306 #else
307         cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
308         CC->kill_me = 1;
309 #endif
310         }
311
312         XMPP->chardata_len = 0;
313         if (XMPP->chardata_alloc > 0) {
314                 XMPP->chardata[0] = 0;
315         }
316 }
317
318
319 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
320 {
321         struct citxmpp *X = XMPP;
322
323         if (X->chardata_alloc == 0) {
324                 X->chardata_alloc = SIZ;
325                 X->chardata = malloc(X->chardata_alloc);
326         }
327         if ((X->chardata_len + len + 1) > X->chardata_alloc) {
328                 X->chardata_alloc = X->chardata_len + len + 1024;
329                 X->chardata = realloc(X->chardata, X->chardata_alloc);
330         }
331         memcpy(&X->chardata[X->chardata_len], s, len);
332         X->chardata_len += len;
333         X->chardata[X->chardata_len] = 0;
334 }
335
336
337 /*
338  * This cleanup function blows away the temporary memory and files used by the XMPP service.
339  */
340 void xmpp_cleanup_function(void) {
341
342         /* Don't do this stuff if this is not a XMPP session! */
343         if (CC->h_command_function != xmpp_command_loop) return;
344
345         lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
346         if (XMPP->chardata != NULL) {
347                 free(XMPP->chardata);
348                 XMPP->chardata = NULL;
349                 XMPP->chardata_len = 0;
350                 XMPP->chardata_alloc = 0;
351                 if (XMPP->message_body != NULL) {
352                         free(XMPP->message_body);
353                 }
354         }
355         XML_ParserFree(XMPP->xp);
356         free(XMPP);
357 }
358
359
360
361 /*
362  * Here's where our XMPP session begins its happy day.
363  */
364 void xmpp_greeting(void) {
365         strcpy(CC->cs_clientname, "Jabber session");
366         CC->session_specific_data = malloc(sizeof(struct citxmpp));
367         memset(XMPP, 0, sizeof(struct citxmpp));
368         XMPP->last_event_processed = queue_event_seq;
369
370         /* XMPP does not use a greeting, but we still have to initialize some things. */
371
372         XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
373         if (XMPP->xp == NULL) {
374                 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
375                 CC->kill_me = 1;
376                 return;
377         }
378
379         XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
380         XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
381         // XML_SetUserData(XMPP->xp, something...);
382 }
383
384
385 /* 
386  * Main command loop for XMPP sessions.
387  */
388 void xmpp_command_loop(void) {
389         char cmdbuf[16];
390         int retval;
391
392         time(&CC->lastcmd);
393         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
394         retval = client_read(cmdbuf, 1);
395         if (retval != 1) {
396                 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
397                 CC->kill_me = 1;
398                 return;
399         }
400
401         /* FIXME ... this is woefully inefficient. */
402
403         XML_Parse(XMPP->xp, cmdbuf, 1, 0);
404 }
405
406
407 /*
408  * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
409  */
410 void xmpp_async_loop(void) {
411         xmpp_process_events();
412         jabber_output_incoming_messages();
413 }
414
415
416 /*
417  * Login hook for XMPP sessions
418  */
419 void xmpp_login_hook(void) {
420         xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
421 }
422
423
424 /*
425  * Logout hook for XMPP sessions
426  */
427 void xmpp_logout_hook(void) {
428         xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
429 }
430
431
432 const char *CitadelServiceXMPP="XMPP";
433
434 #endif  /* HAVE_EXPAT */
435
436 CTDL_MODULE_INIT(jabber)
437 {
438 #ifdef HAVE_EXPAT
439         if (!threading) {
440                 CtdlRegisterServiceHook(5222,                   /* FIXME change to config.c_xmpp_port */
441                                         NULL,
442                                         xmpp_greeting,
443                                         xmpp_command_loop,
444                                         xmpp_async_loop,
445                                         CitadelServiceXMPP);
446                 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
447                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
448                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
449                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH);
450                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH);
451         #else
452                 lprintf(CTDL_INFO, "This server is missing the Expat XML parser.  Jabber service will be disabled.\n");
453 #endif
454         }
455
456         /* return our Subversion id for the Log */
457         return "$Id$";
458 }