4 * XMPP (Jabber) service for the Citadel system
5 * Copyright (c) 2007-2010 by Art Cancro
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
47 #include <libcitadel.h>
51 #include "citserver.h"
58 #include "internet_addressing.h"
60 #include "ctdl_module.h"
61 #include "serv_xmpp.h"
63 struct xmpp_event *xmpp_queue = NULL;
65 /* We have just received a <stream> tag from the client, so send them ours */
67 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
70 if (!strcasecmp(attr[0], "to")) {
71 safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
76 cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
78 cprintf("<stream:stream ");
79 cprintf("from=\"%s\" ", XMPP->server_name);
80 cprintf("id=\"%08x\" ", CC->cs_pid);
81 cprintf("version=\"1.0\" ");
82 cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
83 cprintf("xmlns=\"jabber:client\">");
85 /* The features of this stream are... */
86 cprintf("<stream:features>");
88 #ifdef HAVE_OPENSSL_XXXX_COMMENTED_OUT
89 /* TLS encryption (but only if it isn't already active) */
90 if (!CC->redirect_ssl) {
91 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
96 /* If we're not logged in yet, offer SASL as our feature set */
97 xmpp_output_auth_mechs();
99 /* Also offer non-SASL authentication */
100 cprintf("<auth xmlns=\"http://jabber.org/features/iq-auth\"/>");
103 /* Offer binding and sessions as part of our feature set */
104 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
105 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
107 cprintf("</stream:features>");
109 CC->is_async = 1; /* XMPP sessions are inherently async-capable */
113 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
118 /* Axe the namespace, we don't care about it */
119 safestrncpy(el, supplied_el, sizeof el);
120 while (sep = strchr(el, ':'), sep) {
125 CtdlLogPrintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
126 for (i=0; attr[i] != NULL; i+=2) {
127 CtdlLogPrintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
129 uncomment for more verbosity */
131 if (!strcasecmp(el, "stream")) {
132 xmpp_stream_start(data, supplied_el, attr);
135 else if (!strcasecmp(el, "query")) {
136 XMPP->iq_query_xmlns[0] = 0;
137 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
140 else if (!strcasecmp(el, "bind")) {
141 XMPP->bind_requested = 1;
144 else if (!strcasecmp(el, "iq")) {
145 for (i=0; attr[i] != NULL; i+=2) {
146 if (!strcasecmp(attr[i], "type")) {
147 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
149 else if (!strcasecmp(attr[i], "id")) {
150 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
152 else if (!strcasecmp(attr[i], "from")) {
153 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
155 else if (!strcasecmp(attr[i], "to")) {
156 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
161 else if (!strcasecmp(el, "auth")) {
162 XMPP->sasl_auth_mech[0] = 0;
163 for (i=0; attr[i] != NULL; i+=2) {
164 if (!strcasecmp(attr[i], "mechanism")) {
165 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
170 else if (!strcasecmp(el, "message")) {
171 for (i=0; attr[i] != NULL; i+=2) {
172 if (!strcasecmp(attr[i], "to")) {
173 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
178 else if (!strcasecmp(el, "html")) {
179 ++XMPP->html_tag_level;
185 void xmpp_xml_end(void *data, const char *supplied_el) {
189 /* Axe the namespace, we don't care about it */
190 safestrncpy(el, supplied_el, sizeof el);
191 while (sep = strchr(el, ':'), sep) {
196 CtdlLogPrintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
197 if (XMPP->chardata_len > 0) {
198 CtdlLogPrintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
200 uncomment for more verbosity */
202 if (!strcasecmp(el, "resource")) {
203 if (XMPP->chardata_len > 0) {
204 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
205 sizeof XMPP->iq_client_resource);
206 striplt(XMPP->iq_client_resource);
210 if (!strcasecmp(el, "username")) { /* NON SASL ONLY */
211 if (XMPP->chardata_len > 0) {
212 safestrncpy(XMPP->iq_client_username, XMPP->chardata,
213 sizeof XMPP->iq_client_username);
214 striplt(XMPP->iq_client_username);
218 if (!strcasecmp(el, "password")) { /* NON SASL ONLY */
219 if (XMPP->chardata_len > 0) {
220 safestrncpy(XMPP->iq_client_password, XMPP->chardata,
221 sizeof XMPP->iq_client_password);
222 striplt(XMPP->iq_client_password);
226 else if (!strcasecmp(el, "iq")) {
229 * iq type="get" (handle queries)
231 if (!strcasecmp(XMPP->iq_type, "get")) {
234 * Query on a namespace
236 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
237 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
238 XMPP->iq_to, XMPP->iq_query_xmlns);
242 * ping ( http://xmpp.org/extensions/xep-0199.html )
244 else if (XMPP->ping_requested) {
245 cprintf("<iq type=\"result\" ");
246 if (!IsEmptyStr(XMPP->iq_from)) {
247 cprintf("to=\"%s\" ", XMPP->iq_from);
249 if (!IsEmptyStr(XMPP->iq_to)) {
250 cprintf("from=\"%s\" ", XMPP->iq_to);
252 cprintf("id=\"%s\"/>", XMPP->iq_id);
256 * Unknown queries ... return the XML equivalent of a blank stare
259 CtdlLogPrintf(CTDL_DEBUG, "Unknown query; <service-unavailable/>\n");
260 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
261 cprintf("<error code=\"503\" type=\"cancel\">"
262 "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
270 * Non SASL authentication
273 (!strcasecmp(XMPP->iq_type, "set"))
274 && (!strcasecmp(XMPP->iq_query_xmlns, "jabber:iq:auth:query"))
277 xmpp_non_sasl_authenticate(
279 XMPP->iq_client_username,
280 XMPP->iq_client_password,
281 XMPP->iq_client_resource
286 * If this <iq> stanza was a "bind" attempt, process it ...
289 (XMPP->bind_requested)
290 && (!IsEmptyStr(XMPP->iq_id))
291 && (!IsEmptyStr(XMPP->iq_client_resource))
295 /* Generate the "full JID" of the client resource */
297 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
300 XMPP->iq_client_resource
303 /* Tell the client what its JID is */
305 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
306 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
307 cprintf("<jid>%s</jid>", XMPP->client_jid);
312 else if (XMPP->iq_session) {
313 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
318 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
319 cprintf("<error></error>");
323 /* Now clear these fields out so they don't get used by a future stanza */
325 XMPP->iq_from[0] = 0;
327 XMPP->iq_type[0] = 0;
328 XMPP->iq_client_resource[0] = 0;
329 XMPP->iq_session = 0;
330 XMPP->iq_query_xmlns[0] = 0;
331 XMPP->bind_requested = 0;
332 XMPP->ping_requested = 0;
335 else if (!strcasecmp(el, "auth")) {
337 /* Try to authenticate (this function is responsible for the output stanza) */
338 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
340 /* Now clear these fields out so they don't get used by a future stanza */
341 XMPP->sasl_auth_mech[0] = 0;
344 else if (!strcasecmp(el, "session")) {
345 XMPP->iq_session = 1;
348 else if (!strcasecmp(el, "presence")) {
350 /* Respond to a <presence> update by firing back with presence information
351 * on the entire wholist. Check this assumption, it's probably wrong.
353 xmpp_wholist_presence_dump();
356 else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
357 if (XMPP->message_body != NULL) {
358 free(XMPP->message_body);
359 XMPP->message_body = NULL;
361 if (XMPP->chardata_len > 0) {
362 XMPP->message_body = strdup(XMPP->chardata);
366 else if (!strcasecmp(el, "message")) {
367 xmpp_send_message(XMPP->message_to, XMPP->message_body);
368 XMPP->html_tag_level = 0;
371 else if (!strcasecmp(el, "html")) {
372 --XMPP->html_tag_level;
375 else if (!strcasecmp(el, "starttls")) {
377 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
378 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
379 if (!CC->redirect_ssl) CC->kill_me = 1;
381 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
386 else if (!strcasecmp(el, "ping")) {
387 XMPP->ping_requested = 1;
390 XMPP->chardata_len = 0;
391 if (XMPP->chardata_alloc > 0) {
392 XMPP->chardata[0] = 0;
397 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
399 struct citxmpp *X = XMPP;
401 if (X->chardata_alloc == 0) {
402 X->chardata_alloc = SIZ;
403 X->chardata = malloc(X->chardata_alloc);
405 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
406 X->chardata_alloc = X->chardata_len + len + 1024;
407 X->chardata = realloc(X->chardata, X->chardata_alloc);
409 memcpy(&X->chardata[X->chardata_len], s, len);
410 X->chardata_len += len;
411 X->chardata[X->chardata_len] = 0;
416 * This cleanup function blows away the temporary memory and files used by the XMPP service.
418 void xmpp_cleanup_function(void) {
420 /* Don't do this stuff if this is not a XMPP session! */
421 if (CC->h_command_function != xmpp_command_loop) return;
423 if (XMPP->chardata != NULL) {
424 free(XMPP->chardata);
425 XMPP->chardata = NULL;
426 XMPP->chardata_len = 0;
427 XMPP->chardata_alloc = 0;
428 if (XMPP->message_body != NULL) {
429 free(XMPP->message_body);
432 XML_ParserFree(XMPP->xp);
439 * Here's where our XMPP session begins its happy day.
441 void xmpp_greeting(void) {
442 strcpy(CC->cs_clientname, "XMPP session");
443 CC->session_specific_data = malloc(sizeof(struct citxmpp));
444 memset(XMPP, 0, sizeof(struct citxmpp));
445 XMPP->last_event_processed = queue_event_seq;
447 /* XMPP does not use a greeting, but we still have to initialize some things. */
449 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
450 if (XMPP->xp == NULL) {
451 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
456 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
457 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
458 // XML_SetUserData(XMPP->xp, something...);
460 CC->can_receive_im = 1; /* This protocol is capable of receiving instant messages */
465 * Main command loop for XMPP sessions.
467 void xmpp_command_loop(void) {
472 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
473 retval = client_read(cmdbuf, 1);
475 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
480 /* FIXME ... this is woefully inefficient. */
482 XML_Parse(XMPP->xp, cmdbuf, 1, 0);
487 * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
489 void xmpp_async_loop(void) {
490 xmpp_process_events();
491 xmpp_output_incoming_messages();
496 * Login hook for XMPP sessions
498 void xmpp_login_hook(void) {
499 xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
504 * Logout hook for XMPP sessions
506 void xmpp_logout_hook(void) {
507 xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
511 const char *CitadelServiceXMPP="XMPP";
513 CTDL_MODULE_INIT(xmpp)
516 CtdlRegisterServiceHook(config.c_xmpp_c2s_port,
522 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
523 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
524 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
525 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH);
526 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH);
529 /* return our Subversion id for the Log */