4 * XMPP (Jabber) service for the Citadel system
5 * Copyright (c) 2007-2009 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"
59 #include "internet_addressing.h"
61 #include "ctdl_module.h"
62 #include "serv_xmpp.h"
64 struct xmpp_event *xmpp_queue = NULL;
66 /* We have just received a <stream> tag from the client, so send them ours */
68 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
71 if (!strcasecmp(attr[0], "to")) {
72 safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
77 cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
79 cprintf("<stream:stream ");
80 cprintf("from=\"%s\" ", XMPP->server_name);
81 cprintf("id=\"%08x\" ", CC->cs_pid);
82 cprintf("version=\"1.0\" ");
83 cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
84 cprintf("xmlns=\"jabber:client\">");
86 /* The features of this stream are... */
87 cprintf("<stream:features>");
89 #ifdef HAVE_OPENSSL_XXXX_COMMENTED_OUT
90 /* TLS encryption (but only if it isn't already active) */
91 if (!CC->redirect_ssl) {
92 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
97 /* If we're not logged in yet, offer SASL as our feature set */
98 xmpp_output_auth_mechs();
100 /* Also offer non-SASL authentication */
101 cprintf("<auth xmlns=\"http://jabber.org/features/iq-auth\"/>");
104 /* Offer binding and sessions as part of our feature set */
105 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
106 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
108 cprintf("</stream:features>");
110 CC->is_async = 1; /* XMPP sessions are inherently async-capable */
114 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
119 /* Axe the namespace, we don't care about it */
120 safestrncpy(el, supplied_el, sizeof el);
121 while (sep = strchr(el, ':'), sep) {
126 CtdlLogPrintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
127 for (i=0; attr[i] != NULL; i+=2) {
128 CtdlLogPrintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
130 uncomment for more verbosity */
132 if (!strcasecmp(el, "stream")) {
133 xmpp_stream_start(data, supplied_el, attr);
136 else if (!strcasecmp(el, "query")) {
137 XMPP->iq_query_xmlns[0] = 0;
138 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
141 else if (!strcasecmp(el, "bind")) {
142 XMPP->bind_requested = 1;
145 else if (!strcasecmp(el, "iq")) {
146 for (i=0; attr[i] != NULL; i+=2) {
147 if (!strcasecmp(attr[i], "type")) {
148 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
150 else if (!strcasecmp(attr[i], "id")) {
151 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
153 else if (!strcasecmp(attr[i], "from")) {
154 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
156 else if (!strcasecmp(attr[i], "to")) {
157 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
162 else if (!strcasecmp(el, "auth")) {
163 XMPP->sasl_auth_mech[0] = 0;
164 for (i=0; attr[i] != NULL; i+=2) {
165 if (!strcasecmp(attr[i], "mechanism")) {
166 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
171 else if (!strcasecmp(el, "message")) {
172 for (i=0; attr[i] != NULL; i+=2) {
173 if (!strcasecmp(attr[i], "to")) {
174 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
179 else if (!strcasecmp(el, "html")) {
180 ++XMPP->html_tag_level;
186 void xmpp_xml_end(void *data, const char *supplied_el) {
190 /* Axe the namespace, we don't care about it */
191 safestrncpy(el, supplied_el, sizeof el);
192 while (sep = strchr(el, ':'), sep) {
197 CtdlLogPrintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
198 if (XMPP->chardata_len > 0) {
199 CtdlLogPrintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
201 uncomment for more verbosity */
203 if (!strcasecmp(el, "resource")) {
204 if (XMPP->chardata_len > 0) {
205 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
206 sizeof XMPP->iq_client_resource);
207 striplt(XMPP->iq_client_resource);
211 if (!strcasecmp(el, "username")) { /* NON SASL ONLY */
212 if (XMPP->chardata_len > 0) {
213 safestrncpy(XMPP->iq_client_username, XMPP->chardata,
214 sizeof XMPP->iq_client_username);
215 striplt(XMPP->iq_client_username);
219 if (!strcasecmp(el, "password")) { /* NON SASL ONLY */
220 if (XMPP->chardata_len > 0) {
221 safestrncpy(XMPP->iq_client_password, XMPP->chardata,
222 sizeof XMPP->iq_client_password);
223 striplt(XMPP->iq_client_password);
227 else if (!strcasecmp(el, "iq")) {
230 * iq type="get" (handle queries)
232 if (!strcasecmp(XMPP->iq_type, "get")) {
235 * Query on a namespace
237 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
238 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
239 XMPP->iq_to, XMPP->iq_query_xmlns);
243 * Unknown queries ... return the XML equivalent of a blank stare
246 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
252 * Non SASL authentication
255 (!strcasecmp(XMPP->iq_type, "set"))
256 && (!strcasecmp(XMPP->iq_query_xmlns, "jabber:iq:auth:query"))
259 jabber_non_sasl_authenticate(
261 XMPP->iq_client_username,
262 XMPP->iq_client_password,
263 XMPP->iq_client_resource
268 * If this <iq> stanza was a "bind" attempt, process it ...
271 (XMPP->bind_requested)
272 && (!IsEmptyStr(XMPP->iq_id))
273 && (!IsEmptyStr(XMPP->iq_client_resource))
277 /* Generate the "full JID" of the client resource */
279 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
282 XMPP->iq_client_resource
285 /* Tell the client what its JID is */
287 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
288 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
289 cprintf("<jid>%s</jid>", XMPP->client_jid);
294 else if (XMPP->iq_session) {
295 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
300 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
301 cprintf("<error></error>");
305 /* Now clear these fields out so they don't get used by a future stanza */
307 XMPP->iq_from[0] = 0;
309 XMPP->iq_type[0] = 0;
310 XMPP->iq_client_resource[0] = 0;
311 XMPP->iq_session = 0;
312 XMPP->iq_query_xmlns[0] = 0;
313 XMPP->bind_requested = 0;
316 else if (!strcasecmp(el, "auth")) {
318 /* Try to authenticate (this function is responsible for the output stanza) */
319 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
321 /* Now clear these fields out so they don't get used by a future stanza */
322 XMPP->sasl_auth_mech[0] = 0;
325 else if (!strcasecmp(el, "session")) {
326 XMPP->iq_session = 1;
329 else if (!strcasecmp(el, "presence")) {
331 /* Respond to a <presence> update by firing back with presence information
332 * on the entire wholist. Check this assumption, it's probably wrong.
334 jabber_wholist_presence_dump();
337 else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
338 if (XMPP->message_body != NULL) {
339 free(XMPP->message_body);
340 XMPP->message_body = NULL;
342 if (XMPP->chardata_len > 0) {
343 XMPP->message_body = strdup(XMPP->chardata);
347 else if (!strcasecmp(el, "message")) {
348 jabber_send_message(XMPP->message_to, XMPP->message_body);
349 XMPP->html_tag_level = 0;
352 else if (!strcasecmp(el, "html")) {
353 --XMPP->html_tag_level;
356 else if (!strcasecmp(el, "starttls")) {
358 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
359 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
360 if (!CC->redirect_ssl) CC->kill_me = 1;
362 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
367 XMPP->chardata_len = 0;
368 if (XMPP->chardata_alloc > 0) {
369 XMPP->chardata[0] = 0;
374 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
376 struct citxmpp *X = XMPP;
378 if (X->chardata_alloc == 0) {
379 X->chardata_alloc = SIZ;
380 X->chardata = malloc(X->chardata_alloc);
382 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
383 X->chardata_alloc = X->chardata_len + len + 1024;
384 X->chardata = realloc(X->chardata, X->chardata_alloc);
386 memcpy(&X->chardata[X->chardata_len], s, len);
387 X->chardata_len += len;
388 X->chardata[X->chardata_len] = 0;
393 * This cleanup function blows away the temporary memory and files used by the XMPP service.
395 void xmpp_cleanup_function(void) {
397 /* Don't do this stuff if this is not a XMPP session! */
398 if (CC->h_command_function != xmpp_command_loop) return;
400 if (XMPP->chardata != NULL) {
401 free(XMPP->chardata);
402 XMPP->chardata = NULL;
403 XMPP->chardata_len = 0;
404 XMPP->chardata_alloc = 0;
405 if (XMPP->message_body != NULL) {
406 free(XMPP->message_body);
409 XML_ParserFree(XMPP->xp);
416 * Here's where our XMPP session begins its happy day.
418 void xmpp_greeting(void) {
419 strcpy(CC->cs_clientname, "Jabber session");
420 CC->session_specific_data = malloc(sizeof(struct citxmpp));
421 memset(XMPP, 0, sizeof(struct citxmpp));
422 XMPP->last_event_processed = queue_event_seq;
424 /* XMPP does not use a greeting, but we still have to initialize some things. */
426 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
427 if (XMPP->xp == NULL) {
428 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
433 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
434 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
435 // XML_SetUserData(XMPP->xp, something...);
437 CC->can_receive_im = 1; /* This protocol is capable of receiving instant messages */
442 * Main command loop for XMPP sessions.
444 void xmpp_command_loop(void) {
449 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
450 retval = client_read(cmdbuf, 1);
452 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
457 /* FIXME ... this is woefully inefficient. */
459 XML_Parse(XMPP->xp, cmdbuf, 1, 0);
464 * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
466 void xmpp_async_loop(void) {
467 xmpp_process_events();
468 jabber_output_incoming_messages();
473 * Login hook for XMPP sessions
475 void xmpp_login_hook(void) {
476 xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
481 * Logout hook for XMPP sessions
483 void xmpp_logout_hook(void) {
484 xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
488 const char *CitadelServiceXMPP="XMPP";
490 CTDL_MODULE_INIT(jabber)
493 CtdlRegisterServiceHook(config.c_xmpp_c2s_port,
499 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
500 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
501 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
502 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH);
503 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH);
506 /* return our Subversion id for the Log */