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.
18 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
35 #include <libcitadel.h>
38 #include "citserver.h"
46 #include "internet_addressing.h"
48 #include "ctdl_module.h"
52 #include "serv_xmpp.h"
54 struct xmpp_event *xmpp_queue = NULL;
56 /* We have just received a <stream> tag from the client, so send them ours */
58 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
61 lprintf(CTDL_DEBUG, "New stream detected.\n");
64 if (!strcasecmp(attr[0], "to")) {
65 safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
70 cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
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\">");
79 /* The features of this stream are... */
80 cprintf("<stream:features>");
82 #ifdef HAVE_OPENSSL_XXXX_COMMENTED_OUT
83 /* TLS encryption (but only if it isn't already active) */
84 if (!CC->redirect_ssl) {
85 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
90 /* If we're not logged in yet, offer SASL as our feature set */
91 xmpp_output_auth_mechs();
94 /* If we've logged in, now offer binding and sessions as our feature set */
95 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
96 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
99 cprintf("</stream:features>");
101 CC->is_async = 1; /* XMPP sessions are inherently async-capable */
105 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
110 /* Axe the namespace, we don't care about it */
111 safestrncpy(el, supplied_el, sizeof el);
112 while (sep = strchr(el, ':'), sep) {
116 lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
118 for (i=0; attr[i] != NULL; i+=2) {
119 lprintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
122 if (!strcasecmp(el, "stream")) {
123 xmpp_stream_start(data, supplied_el, attr);
126 else if (!strcasecmp(el, "query")) {
127 XMPP->iq_query_xmlns[0] = 0;
128 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
131 else if (!strcasecmp(el, "iq")) {
132 for (i=0; attr[i] != NULL; i+=2) {
133 if (!strcasecmp(attr[i], "type")) {
134 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
136 else if (!strcasecmp(attr[i], "id")) {
137 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
139 else if (!strcasecmp(attr[i], "from")) {
140 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
142 else if (!strcasecmp(attr[i], "to")) {
143 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
148 else if (!strcasecmp(el, "auth")) {
149 XMPP->sasl_auth_mech[0] = 0;
150 for (i=0; attr[i] != NULL; i+=2) {
151 if (!strcasecmp(attr[i], "mechanism")) {
152 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
157 else if (!strcasecmp(el, "message")) {
158 for (i=0; attr[i] != NULL; i+=2) {
159 if (!strcasecmp(attr[i], "to")) {
160 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
165 else if (!strcasecmp(el, "html")) {
166 ++XMPP->html_tag_level;
172 void xmpp_xml_end(void *data, const char *supplied_el) {
176 /* Axe the namespace, we don't care about it */
177 safestrncpy(el, supplied_el, sizeof el);
178 while (sep = strchr(el, ':'), sep) {
182 lprintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
183 if (XMPP->chardata_len > 0) {
184 lprintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
187 if (!strcasecmp(el, "resource")) {
188 if (XMPP->chardata_len > 0) {
189 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
190 sizeof XMPP->iq_client_resource);
194 else if (!strcasecmp(el, "iq")) {
197 * iq type="get" (handle queries)
199 if (!strcasecmp(XMPP->iq_type, "get")) {
202 * Query on a namespace
204 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
205 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
206 XMPP->iq_to, XMPP->iq_query_xmlns);
210 * Unknown queries ... return the XML equivalent of a blank stare
213 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
219 * If this <iq> stanza was a "bind" attempt, process it ...
221 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
223 /* Generate the "full JID" of the client resource */
225 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
228 XMPP->iq_client_resource
231 /* Tell the client what its JID is */
233 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
234 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
235 cprintf("<jid>%s</jid>", XMPP->client_jid);
240 else if (XMPP->iq_session) {
241 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
246 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
247 cprintf("<error></error>");
251 /* Now clear these fields out so they don't get used by a future stanza */
253 XMPP->iq_from[0] = 0;
255 XMPP->iq_type[0] = 0;
256 XMPP->iq_client_resource[0] = 0;
257 XMPP->iq_session = 0;
258 XMPP->iq_query_xmlns[0] = 0;
261 else if (!strcasecmp(el, "auth")) {
263 /* Try to authenticate (this function is responsible for the output stanza) */
264 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
266 /* Now clear these fields out so they don't get used by a future stanza */
267 XMPP->sasl_auth_mech[0] = 0;
270 else if (!strcasecmp(el, "session")) {
271 XMPP->iq_session = 1;
274 else if (!strcasecmp(el, "presence")) {
276 /* Respond to a <presence> update by firing back with presence information
277 * on the entire wholist. Check this assumption, it's probably wrong.
279 jabber_wholist_presence_dump();
282 else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
283 if (XMPP->message_body != NULL) {
284 free(XMPP->message_body);
285 XMPP->message_body = NULL;
287 if (XMPP->chardata_len > 0) {
288 XMPP->message_body = strdup(XMPP->chardata);
292 else if (!strcasecmp(el, "message")) {
293 jabber_send_message(XMPP->message_to, XMPP->message_body);
294 XMPP->html_tag_level = 0;
297 else if (!strcasecmp(el, "html")) {
298 --XMPP->html_tag_level;
301 else if (!strcasecmp(el, "starttls")) {
303 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
304 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
305 if (!CC->redirect_ssl) CC->kill_me = 1;
307 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
312 XMPP->chardata_len = 0;
313 if (XMPP->chardata_alloc > 0) {
314 XMPP->chardata[0] = 0;
319 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
321 struct citxmpp *X = XMPP;
323 if (X->chardata_alloc == 0) {
324 X->chardata_alloc = SIZ;
325 X->chardata = malloc(X->chardata_alloc);
327 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
328 X->chardata_alloc = X->chardata_len + len + 1024;
329 X->chardata = realloc(X->chardata, X->chardata_alloc);
331 memcpy(&X->chardata[X->chardata_len], s, len);
332 X->chardata_len += len;
333 X->chardata[X->chardata_len] = 0;
338 * This cleanup function blows away the temporary memory and files used by the XMPP service.
340 void xmpp_cleanup_function(void) {
342 /* Don't do this stuff if this is not a XMPP session! */
343 if (CC->h_command_function != xmpp_command_loop) return;
345 lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
346 if (XMPP->chardata != NULL) {
347 free(XMPP->chardata);
348 XMPP->chardata = NULL;
349 XMPP->chardata_len = 0;
350 XMPP->chardata_alloc = 0;
351 if (XMPP->message_body != NULL) {
352 free(XMPP->message_body);
355 XML_ParserFree(XMPP->xp);
362 * Here's where our XMPP session begins its happy day.
364 void xmpp_greeting(void) {
365 strcpy(CC->cs_clientname, "Jabber session");
366 CC->session_specific_data = malloc(sizeof(struct citxmpp));
367 memset(XMPP, 0, sizeof(struct citxmpp));
368 XMPP->last_event_processed = queue_event_seq;
370 /* XMPP does not use a greeting, but we still have to initialize some things. */
372 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
373 if (XMPP->xp == NULL) {
374 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
379 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
380 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
381 // XML_SetUserData(XMPP->xp, something...);
386 * Main command loop for XMPP sessions.
388 void xmpp_command_loop(void) {
393 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
394 retval = client_read(cmdbuf, 1);
396 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
401 /* FIXME ... this is woefully inefficient. */
403 XML_Parse(XMPP->xp, cmdbuf, 1, 0);
408 * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
410 void xmpp_async_loop(void) {
411 xmpp_process_events();
412 jabber_output_incoming_messages();
417 * Login hook for XMPP sessions
419 void xmpp_login_hook(void) {
420 xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
425 * Logout hook for XMPP sessions
427 void xmpp_logout_hook(void) {
428 xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
432 const char *CitadelServiceXMPP="XMPP";
434 #endif /* HAVE_EXPAT */
436 CTDL_MODULE_INIT(jabber)
440 CtdlRegisterServiceHook(5222, /* FIXME change to config.c_xmpp_port */
446 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
447 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
448 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
451 lprintf(CTDL_INFO, "This server is missing the Expat XML parser. Jabber service will be disabled.\n");
455 /* return our Subversion id for the Log */