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