2 * XMPP (Jabber) service for the Citadel system
3 * Copyright (c) 2007-2010 by Art Cancro
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
45 #include <libcitadel.h>
49 #include "citserver.h"
55 #include "internet_addressing.h"
57 #include "ctdl_module.h"
58 #include "serv_xmpp.h"
60 struct xmpp_event *xmpp_queue = NULL;
63 * Given a source string and a target buffer, returns the string
64 * properly escaped for insertion into an XML stream. Returns a
65 * pointer to the target buffer for convenience.
67 * BUG: this does not properly handle UTF-8
69 char *xmlesc(char *buf, char *str, int bufsiz)
75 if (!buf) return(NULL);
82 for (ptr=str; *ptr; ptr++) {
85 strcpy(&buf[len], "<");
89 strcpy(&buf[len], ">");
93 strcpy(&buf[len], "&");
96 else if ((ch >= 0x20) && (ch <= 0x7F)) {
100 else if (ch < 0x20) {
101 /* we probably shouldn't be doing this */
107 sprintf(oct, "&#%o;", ch);
108 strcpy(&buf[len], oct);
111 if ((len + 6) > bufsiz) {
120 * We have just received a <stream> tag from the client, so send them ours
122 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
127 if (!strcasecmp(attr[0], "to")) {
128 safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
133 cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
135 cprintf("<stream:stream ");
136 cprintf("from=\"%s\" ", xmlesc(xmlbuf, XMPP->server_name, sizeof xmlbuf));
137 cprintf("id=\"%08x\" ", CC->cs_pid);
138 cprintf("version=\"1.0\" ");
139 cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
140 cprintf("xmlns=\"jabber:client\">");
142 /* The features of this stream are... */
143 cprintf("<stream:features>");
145 #ifdef HAVE_OPENSSL_XXXX_COMMENTED_OUT
146 /* TLS encryption (but only if it isn't already active) */
147 if (!CC->redirect_ssl) {
148 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
152 if (!CC->logged_in) {
153 /* If we're not logged in yet, offer SASL as our feature set */
154 xmpp_output_auth_mechs();
156 /* Also offer non-SASL authentication */
157 cprintf("<auth xmlns=\"http://jabber.org/features/iq-auth\"/>");
160 /* Offer binding and sessions as part of our feature set */
161 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
162 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
164 cprintf("</stream:features>");
166 CC->is_async = 1; /* XMPP sessions are inherently async-capable */
170 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
175 /* Axe the namespace, we don't care about it */
176 safestrncpy(el, supplied_el, sizeof el);
177 while (sep = strchr(el, ':'), sep) {
182 CtdlLogPrintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
183 for (i=0; attr[i] != NULL; i+=2) {
184 CtdlLogPrintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
186 uncomment for more verbosity */
188 if (!strcasecmp(el, "stream")) {
189 xmpp_stream_start(data, supplied_el, attr);
192 else if (!strcasecmp(el, "query")) {
193 XMPP->iq_query_xmlns[0] = 0;
194 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
197 else if (!strcasecmp(el, "bind")) {
198 XMPP->bind_requested = 1;
201 else if (!strcasecmp(el, "iq")) {
202 for (i=0; attr[i] != NULL; i+=2) {
203 if (!strcasecmp(attr[i], "type")) {
204 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
206 else if (!strcasecmp(attr[i], "id")) {
207 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
209 else if (!strcasecmp(attr[i], "from")) {
210 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
212 else if (!strcasecmp(attr[i], "to")) {
213 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
218 else if (!strcasecmp(el, "auth")) {
219 XMPP->sasl_auth_mech[0] = 0;
220 for (i=0; attr[i] != NULL; i+=2) {
221 if (!strcasecmp(attr[i], "mechanism")) {
222 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
227 else if (!strcasecmp(el, "message")) {
228 for (i=0; attr[i] != NULL; i+=2) {
229 if (!strcasecmp(attr[i], "to")) {
230 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
235 else if (!strcasecmp(el, "html")) {
236 ++XMPP->html_tag_level;
242 void xmpp_xml_end(void *data, const char *supplied_el) {
247 /* Axe the namespace, we don't care about it */
248 safestrncpy(el, supplied_el, sizeof el);
249 while (sep = strchr(el, ':'), sep) {
254 CtdlLogPrintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
255 if (XMPP->chardata_len > 0) {
256 CtdlLogPrintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
258 uncomment for more verbosity */
260 if (!strcasecmp(el, "resource")) {
261 if (XMPP->chardata_len > 0) {
262 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
263 sizeof XMPP->iq_client_resource);
264 striplt(XMPP->iq_client_resource);
268 else if (!strcasecmp(el, "username")) { /* NON SASL ONLY */
269 if (XMPP->chardata_len > 0) {
270 safestrncpy(XMPP->iq_client_username, XMPP->chardata,
271 sizeof XMPP->iq_client_username);
272 striplt(XMPP->iq_client_username);
276 else if (!strcasecmp(el, "password")) { /* NON SASL ONLY */
277 if (XMPP->chardata_len > 0) {
278 safestrncpy(XMPP->iq_client_password, XMPP->chardata,
279 sizeof XMPP->iq_client_password);
280 striplt(XMPP->iq_client_password);
284 else if (!strcasecmp(el, "iq")) {
287 * iq type="get" (handle queries)
289 if (!strcasecmp(XMPP->iq_type, "get")) {
292 * Query on a namespace
294 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
295 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
296 XMPP->iq_to, XMPP->iq_query_xmlns);
300 * ping ( http://xmpp.org/extensions/xep-0199.html )
302 else if (XMPP->ping_requested) {
303 cprintf("<iq type=\"result\" ");
304 if (!IsEmptyStr(XMPP->iq_from)) {
305 cprintf("to=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_from, sizeof xmlbuf));
307 if (!IsEmptyStr(XMPP->iq_to)) {
308 cprintf("from=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_to, sizeof xmlbuf));
310 cprintf("id=\"%s\"/>", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
314 * Unknown query ... return the XML equivalent of a blank stare
317 CtdlLogPrintf(CTDL_DEBUG,
318 "Unknown query <%s> - returning <service-unavailable/>\n",
321 cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
322 cprintf("<error code=\"503\" type=\"cancel\">"
323 "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
331 * Non SASL authentication
334 (!strcasecmp(XMPP->iq_type, "set"))
335 && (!strcasecmp(XMPP->iq_query_xmlns, "jabber:iq:auth:query"))
338 xmpp_non_sasl_authenticate(
340 XMPP->iq_client_username,
341 XMPP->iq_client_password,
342 XMPP->iq_client_resource
347 * If this <iq> stanza was a "bind" attempt, process it ...
350 (XMPP->bind_requested)
351 && (!IsEmptyStr(XMPP->iq_id))
352 && (!IsEmptyStr(XMPP->iq_client_resource))
356 /* Generate the "full JID" of the client resource */
358 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
361 XMPP->iq_client_resource
364 /* Tell the client what its JID is */
366 cprintf("<iq type=\"result\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
367 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
368 cprintf("<jid>%s</jid>", xmlesc(xmlbuf, XMPP->client_jid, sizeof xmlbuf));
373 else if (XMPP->iq_session) {
374 cprintf("<iq type=\"result\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
379 cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
380 cprintf("<error>Don't know howto do '%s'!</error>", xmlesc(xmlbuf, XMPP->iq_type, sizeof xmlbuf));
384 /* Now clear these fields out so they don't get used by a future stanza */
386 XMPP->iq_from[0] = 0;
388 XMPP->iq_type[0] = 0;
389 XMPP->iq_client_resource[0] = 0;
390 XMPP->iq_session = 0;
391 XMPP->iq_query_xmlns[0] = 0;
392 XMPP->bind_requested = 0;
393 XMPP->ping_requested = 0;
396 else if (!strcasecmp(el, "auth")) {
398 /* Try to authenticate (this function is responsible for the output stanza) */
399 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
401 /* Now clear these fields out so they don't get used by a future stanza */
402 XMPP->sasl_auth_mech[0] = 0;
405 else if (!strcasecmp(el, "session")) {
406 XMPP->iq_session = 1;
409 else if (!strcasecmp(el, "presence")) {
411 /* Respond to a <presence> update by firing back with presence information
412 * on the entire wholist. Check this assumption, it's probably wrong.
414 xmpp_wholist_presence_dump();
417 else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
418 if (XMPP->message_body != NULL) {
419 free(XMPP->message_body);
420 XMPP->message_body = NULL;
422 if (XMPP->chardata_len > 0) {
423 XMPP->message_body = strdup(XMPP->chardata);
427 else if (!strcasecmp(el, "message")) {
428 xmpp_send_message(XMPP->message_to, XMPP->message_body);
429 XMPP->html_tag_level = 0;
432 else if (!strcasecmp(el, "html")) {
433 --XMPP->html_tag_level;
436 else if (!strcasecmp(el, "starttls")) {
438 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
439 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
440 if (!CC->redirect_ssl) CC->kill_me = 1;
442 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
447 else if (!strcasecmp(el, "ping")) {
448 XMPP->ping_requested = 1;
451 else if (!strcasecmp(el, "stream")) {
452 CtdlLogPrintf(CTDL_DEBUG, "XMPP client shut down their stream\n");
453 xmpp_massacre_roster();
454 cprintf("</stream>\n");
459 CtdlLogPrintf(CTDL_DEBUG, "Ignoring unknown tag <%s>\n", el);
462 XMPP->chardata_len = 0;
463 if (XMPP->chardata_alloc > 0) {
464 XMPP->chardata[0] = 0;
469 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
473 if (X->chardata_alloc == 0) {
474 X->chardata_alloc = SIZ;
475 X->chardata = malloc(X->chardata_alloc);
477 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
478 X->chardata_alloc = X->chardata_len + len + 1024;
479 X->chardata = realloc(X->chardata, X->chardata_alloc);
481 memcpy(&X->chardata[X->chardata_len], s, len);
482 X->chardata_len += len;
483 X->chardata[X->chardata_len] = 0;
488 * This cleanup function blows away the temporary memory and files used by the XMPP service.
490 void xmpp_cleanup_function(void) {
492 /* Don't do this stuff if this is not a XMPP session! */
493 if (CC->h_command_function != xmpp_command_loop) return;
495 if (XMPP->chardata != NULL) {
496 free(XMPP->chardata);
497 XMPP->chardata = NULL;
498 XMPP->chardata_len = 0;
499 XMPP->chardata_alloc = 0;
500 if (XMPP->message_body != NULL) {
501 free(XMPP->message_body);
504 XML_ParserFree(XMPP->xp);
511 * Here's where our XMPP session begins its happy day.
513 void xmpp_greeting(void) {
514 client_set_inbound_buf(4);
515 strcpy(CC->cs_clientname, "XMPP session");
516 CC->session_specific_data = malloc(sizeof(citxmpp));
517 memset(XMPP, 0, sizeof(citxmpp));
518 XMPP->last_event_processed = queue_event_seq;
520 /* XMPP does not use a greeting, but we still have to initialize some things. */
522 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
523 if (XMPP->xp == NULL) {
524 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
529 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
530 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
531 // XML_SetUserData(XMPP->xp, something...);
533 CC->can_receive_im = 1; /* This protocol is capable of receiving instant messages */
538 * Main command loop for XMPP sessions.
540 void xmpp_command_loop(void) {
542 StrBuf *stream_input = NewStrBuf();
545 rc = client_read_random_blob(stream_input, 30);
547 XML_Parse(XMPP->xp, ChrPtr(stream_input), rc, 0);
550 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\n");
553 FreeStrBuf(&stream_input);
558 * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
560 void xmpp_async_loop(void) {
561 xmpp_process_events();
562 xmpp_output_incoming_messages();
567 * Login hook for XMPP sessions
569 void xmpp_login_hook(void) {
570 xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
575 * Logout hook for XMPP sessions
577 void xmpp_logout_hook(void) {
578 xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
582 const char *CitadelServiceXMPP="XMPP";
583 extern void xmpp_cleanup_events(void);
584 CTDL_MODULE_INIT(xmpp)
587 CtdlRegisterServiceHook(config.c_xmpp_c2s_port,
594 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
595 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
596 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
597 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH);
598 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH);
599 CtdlRegisterCleanupHook(xmpp_cleanup_events);
603 /* return our Subversion id for the Log */