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>");
146 * TLS encryption (but only if it isn't already active)
148 * NOTE: if TLS doesn't handshake properly for whatever reason,
149 * comment out these three lines to disable it at the server.
151 if (!CC->redirect_ssl) {
152 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
155 if (!CC->logged_in) {
156 /* If we're not logged in yet, offer SASL as our feature set */
157 xmpp_output_auth_mechs();
159 /* Also offer non-SASL authentication */
160 cprintf("<auth xmlns=\"http://jabber.org/features/iq-auth\"/>");
163 /* Offer binding and sessions as part of our feature set */
164 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
165 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
167 cprintf("</stream:features>");
169 CC->is_async = 1; /* XMPP sessions are inherently async-capable */
173 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
178 /* Axe the namespace, we don't care about it */
179 safestrncpy(el, supplied_el, sizeof el);
180 while (sep = strchr(el, ':'), sep) {
185 syslog(LOG_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
186 for (i=0; attr[i] != NULL; i+=2) {
187 syslog(LOG_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
189 uncomment for more verbosity */
191 if (!strcasecmp(el, "stream")) {
192 xmpp_stream_start(data, supplied_el, attr);
195 else if (!strcasecmp(el, "query")) {
196 XMPP->iq_query_xmlns[0] = 0;
197 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
200 else if (!strcasecmp(el, "bind")) {
201 XMPP->bind_requested = 1;
204 else if (!strcasecmp(el, "iq")) {
205 for (i=0; attr[i] != NULL; i+=2) {
206 if (!strcasecmp(attr[i], "type")) {
207 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
209 else if (!strcasecmp(attr[i], "id")) {
210 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
212 else if (!strcasecmp(attr[i], "from")) {
213 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
215 else if (!strcasecmp(attr[i], "to")) {
216 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
221 else if (!strcasecmp(el, "auth")) {
222 XMPP->sasl_auth_mech[0] = 0;
223 for (i=0; attr[i] != NULL; i+=2) {
224 if (!strcasecmp(attr[i], "mechanism")) {
225 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
230 else if (!strcasecmp(el, "message")) {
231 for (i=0; attr[i] != NULL; i+=2) {
232 if (!strcasecmp(attr[i], "to")) {
233 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
238 else if (!strcasecmp(el, "html")) {
239 ++XMPP->html_tag_level;
245 void xmpp_xml_end(void *data, const char *supplied_el) {
250 /* Axe the namespace, we don't care about it */
251 safestrncpy(el, supplied_el, sizeof el);
252 while (sep = strchr(el, ':'), sep) {
257 syslog(LOG_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
258 if (XMPP->chardata_len > 0) {
259 syslog(LOG_DEBUG, " chardata: %s\n", XMPP->chardata);
261 uncomment for more verbosity */
263 if (!strcasecmp(el, "resource")) {
264 if (XMPP->chardata_len > 0) {
265 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
266 sizeof XMPP->iq_client_resource);
267 striplt(XMPP->iq_client_resource);
271 else if (!strcasecmp(el, "username")) { /* NON SASL ONLY */
272 if (XMPP->chardata_len > 0) {
273 safestrncpy(XMPP->iq_client_username, XMPP->chardata,
274 sizeof XMPP->iq_client_username);
275 striplt(XMPP->iq_client_username);
279 else if (!strcasecmp(el, "password")) { /* NON SASL ONLY */
280 if (XMPP->chardata_len > 0) {
281 safestrncpy(XMPP->iq_client_password, XMPP->chardata,
282 sizeof XMPP->iq_client_password);
283 striplt(XMPP->iq_client_password);
287 else if (!strcasecmp(el, "iq")) {
290 * iq type="get" (handle queries)
292 if (!strcasecmp(XMPP->iq_type, "get")) {
295 * Query on a namespace
297 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
298 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
299 XMPP->iq_to, XMPP->iq_query_xmlns);
303 * ping ( http://xmpp.org/extensions/xep-0199.html )
305 else if (XMPP->ping_requested) {
306 cprintf("<iq type=\"result\" ");
307 if (!IsEmptyStr(XMPP->iq_from)) {
308 cprintf("to=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_from, sizeof xmlbuf));
310 if (!IsEmptyStr(XMPP->iq_to)) {
311 cprintf("from=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_to, sizeof xmlbuf));
313 cprintf("id=\"%s\"/>", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
317 * Unknown query ... return the XML equivalent of a blank stare
321 "Unknown query <%s> - returning <service-unavailable/>\n",
324 cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
325 cprintf("<error code=\"503\" type=\"cancel\">"
326 "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
334 * Non SASL authentication
337 (!strcasecmp(XMPP->iq_type, "set"))
338 && (!strcasecmp(XMPP->iq_query_xmlns, "jabber:iq:auth:query"))
341 xmpp_non_sasl_authenticate(
343 XMPP->iq_client_username,
344 XMPP->iq_client_password,
345 XMPP->iq_client_resource
350 * If this <iq> stanza was a "bind" attempt, process it ...
353 (XMPP->bind_requested)
354 && (!IsEmptyStr(XMPP->iq_id))
355 && (!IsEmptyStr(XMPP->iq_client_resource))
359 /* Generate the "full JID" of the client resource */
361 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
364 XMPP->iq_client_resource
367 /* Tell the client what its JID is */
369 cprintf("<iq type=\"result\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
370 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
371 cprintf("<jid>%s</jid>", xmlesc(xmlbuf, XMPP->client_jid, sizeof xmlbuf));
376 else if (XMPP->iq_session) {
377 cprintf("<iq type=\"result\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
382 cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
383 cprintf("<error>Don't know howto do '%s'!</error>", xmlesc(xmlbuf, XMPP->iq_type, sizeof xmlbuf));
387 /* Now clear these fields out so they don't get used by a future stanza */
389 XMPP->iq_from[0] = 0;
391 XMPP->iq_type[0] = 0;
392 XMPP->iq_client_resource[0] = 0;
393 XMPP->iq_session = 0;
394 XMPP->iq_query_xmlns[0] = 0;
395 XMPP->bind_requested = 0;
396 XMPP->ping_requested = 0;
399 else if (!strcasecmp(el, "auth")) {
401 /* Try to authenticate (this function is responsible for the output stanza) */
402 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
404 /* Now clear these fields out so they don't get used by a future stanza */
405 XMPP->sasl_auth_mech[0] = 0;
408 else if (!strcasecmp(el, "session")) {
409 XMPP->iq_session = 1;
412 else if (!strcasecmp(el, "presence")) {
414 /* Respond to a <presence> update by firing back with presence information
415 * on the entire wholist. Check this assumption, it's probably wrong.
417 xmpp_wholist_presence_dump();
420 else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
421 if (XMPP->message_body != NULL) {
422 free(XMPP->message_body);
423 XMPP->message_body = NULL;
425 if (XMPP->chardata_len > 0) {
426 XMPP->message_body = strdup(XMPP->chardata);
430 else if (!strcasecmp(el, "message")) {
431 xmpp_send_message(XMPP->message_to, XMPP->message_body);
432 XMPP->html_tag_level = 0;
435 else if (!strcasecmp(el, "html")) {
436 --XMPP->html_tag_level;
439 else if (!strcasecmp(el, "starttls")) {
441 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
442 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
443 if (!CC->redirect_ssl) CC->kill_me = 1;
445 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
450 else if (!strcasecmp(el, "ping")) {
451 XMPP->ping_requested = 1;
454 else if (!strcasecmp(el, "stream")) {
455 syslog(LOG_DEBUG, "XMPP client shut down their stream\n");
456 xmpp_massacre_roster();
457 cprintf("</stream>\n");
462 syslog(LOG_DEBUG, "Ignoring unknown tag <%s>\n", el);
465 XMPP->chardata_len = 0;
466 if (XMPP->chardata_alloc > 0) {
467 XMPP->chardata[0] = 0;
472 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
476 if (X->chardata_alloc == 0) {
477 X->chardata_alloc = SIZ;
478 X->chardata = malloc(X->chardata_alloc);
480 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
481 X->chardata_alloc = X->chardata_len + len + 1024;
482 X->chardata = realloc(X->chardata, X->chardata_alloc);
484 memcpy(&X->chardata[X->chardata_len], s, len);
485 X->chardata_len += len;
486 X->chardata[X->chardata_len] = 0;
491 * This cleanup function blows away the temporary memory and files used by the XMPP service.
493 void xmpp_cleanup_function(void) {
495 /* Don't do this stuff if this is not a XMPP session! */
496 if (CC->h_command_function != xmpp_command_loop) return;
498 if (XMPP->chardata != NULL) {
499 free(XMPP->chardata);
500 XMPP->chardata = NULL;
501 XMPP->chardata_len = 0;
502 XMPP->chardata_alloc = 0;
503 if (XMPP->message_body != NULL) {
504 free(XMPP->message_body);
507 XML_ParserFree(XMPP->xp);
514 * Here's where our XMPP session begins its happy day.
516 void xmpp_greeting(void) {
517 client_set_inbound_buf(4);
518 strcpy(CC->cs_clientname, "XMPP session");
519 CC->session_specific_data = malloc(sizeof(citxmpp));
520 memset(XMPP, 0, sizeof(citxmpp));
521 XMPP->last_event_processed = queue_event_seq;
523 /* XMPP does not use a greeting, but we still have to initialize some things. */
525 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
526 if (XMPP->xp == NULL) {
527 syslog(LOG_ALERT, "Cannot create XML parser!\n");
532 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
533 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
534 // XML_SetUserData(XMPP->xp, something...);
536 CC->can_receive_im = 1; /* This protocol is capable of receiving instant messages */
541 * Main command loop for XMPP sessions.
543 void xmpp_command_loop(void) {
545 StrBuf *stream_input = NewStrBuf();
548 rc = client_read_random_blob(stream_input, 30);
550 XML_Parse(XMPP->xp, ChrPtr(stream_input), rc, 0);
553 syslog(LOG_ERR, "Client disconnected: ending session.\n");
556 FreeStrBuf(&stream_input);
561 * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
563 void xmpp_async_loop(void) {
564 xmpp_process_events();
565 xmpp_output_incoming_messages();
570 * Login hook for XMPP sessions
572 void xmpp_login_hook(void) {
573 xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
578 * Logout hook for XMPP sessions
580 void xmpp_logout_hook(void) {
581 xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
585 const char *CitadelServiceXMPP="XMPP";
586 extern void xmpp_cleanup_events(void);
587 CTDL_MODULE_INIT(xmpp)
590 CtdlRegisterServiceHook(config.c_xmpp_c2s_port,
597 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
598 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
599 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
600 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH);
601 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH);
602 CtdlRegisterCleanupHook(xmpp_cleanup_events);
606 /* return our Subversion id for the Log */