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 /* Offer binding and sessions as part of 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\"/>");
98 cprintf("</stream:features>");
100 CC->is_async = 1; /* XMPP sessions are inherently async-capable */
104 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
109 /* Axe the namespace, we don't care about it */
110 safestrncpy(el, supplied_el, sizeof el);
111 while (sep = strchr(el, ':'), sep) {
115 lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
117 for (i=0; attr[i] != NULL; i+=2) {
118 lprintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
121 if (!strcasecmp(el, "stream")) {
122 xmpp_stream_start(data, supplied_el, attr);
125 else if (!strcasecmp(el, "query")) {
126 XMPP->iq_query_xmlns[0] = 0;
127 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
130 else if (!strcasecmp(el, "iq")) {
131 for (i=0; attr[i] != NULL; i+=2) {
132 if (!strcasecmp(attr[i], "type")) {
133 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
135 else if (!strcasecmp(attr[i], "id")) {
136 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
138 else if (!strcasecmp(attr[i], "from")) {
139 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
141 else if (!strcasecmp(attr[i], "to")) {
142 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
147 else if (!strcasecmp(el, "auth")) {
148 XMPP->sasl_auth_mech[0] = 0;
149 for (i=0; attr[i] != NULL; i+=2) {
150 if (!strcasecmp(attr[i], "mechanism")) {
151 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
156 else if (!strcasecmp(el, "message")) {
157 for (i=0; attr[i] != NULL; i+=2) {
158 if (!strcasecmp(attr[i], "to")) {
159 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
164 else if (!strcasecmp(el, "html")) {
165 ++XMPP->html_tag_level;
171 void xmpp_xml_end(void *data, const char *supplied_el) {
175 /* Axe the namespace, we don't care about it */
176 safestrncpy(el, supplied_el, sizeof el);
177 while (sep = strchr(el, ':'), sep) {
181 lprintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
182 if (XMPP->chardata_len > 0) {
183 lprintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
186 if (!strcasecmp(el, "resource")) {
187 if (XMPP->chardata_len > 0) {
188 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
189 sizeof XMPP->iq_client_resource);
193 else if (!strcasecmp(el, "iq")) {
196 * iq type="get" (handle queries)
198 if (!strcasecmp(XMPP->iq_type, "get")) {
201 * Query on a namespace
203 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
204 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
205 XMPP->iq_to, XMPP->iq_query_xmlns);
209 * Unknown queries ... return the XML equivalent of a blank stare
212 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
218 * If this <iq> stanza was a "bind" attempt, process it ...
220 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
222 /* Generate the "full JID" of the client resource */
224 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
227 XMPP->iq_client_resource
230 /* Tell the client what its JID is */
232 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
233 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
234 cprintf("<jid>%s</jid>", XMPP->client_jid);
239 else if (XMPP->iq_session) {
240 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
245 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
246 cprintf("<error></error>");
250 /* Now clear these fields out so they don't get used by a future stanza */
252 XMPP->iq_from[0] = 0;
254 XMPP->iq_type[0] = 0;
255 XMPP->iq_client_resource[0] = 0;
256 XMPP->iq_session = 0;
257 XMPP->iq_query_xmlns[0] = 0;
260 else if (!strcasecmp(el, "auth")) {
262 /* Try to authenticate (this function is responsible for the output stanza) */
263 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
265 /* Now clear these fields out so they don't get used by a future stanza */
266 XMPP->sasl_auth_mech[0] = 0;
269 else if (!strcasecmp(el, "session")) {
270 XMPP->iq_session = 1;
273 else if (!strcasecmp(el, "presence")) {
275 /* Respond to a <presence> update by firing back with presence information
276 * on the entire wholist. Check this assumption, it's probably wrong.
278 jabber_wholist_presence_dump();
281 else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
282 if (XMPP->message_body != NULL) {
283 free(XMPP->message_body);
284 XMPP->message_body = NULL;
286 if (XMPP->chardata_len > 0) {
287 XMPP->message_body = strdup(XMPP->chardata);
291 else if (!strcasecmp(el, "message")) {
292 jabber_send_message(XMPP->message_to, XMPP->message_body);
293 XMPP->html_tag_level = 0;
296 else if (!strcasecmp(el, "html")) {
297 --XMPP->html_tag_level;
300 else if (!strcasecmp(el, "starttls")) {
302 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
303 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
304 if (!CC->redirect_ssl) CC->kill_me = 1;
306 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
311 XMPP->chardata_len = 0;
312 if (XMPP->chardata_alloc > 0) {
313 XMPP->chardata[0] = 0;
318 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
320 struct citxmpp *X = XMPP;
322 if (X->chardata_alloc == 0) {
323 X->chardata_alloc = SIZ;
324 X->chardata = malloc(X->chardata_alloc);
326 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
327 X->chardata_alloc = X->chardata_len + len + 1024;
328 X->chardata = realloc(X->chardata, X->chardata_alloc);
330 memcpy(&X->chardata[X->chardata_len], s, len);
331 X->chardata_len += len;
332 X->chardata[X->chardata_len] = 0;
337 * This cleanup function blows away the temporary memory and files used by the XMPP service.
339 void xmpp_cleanup_function(void) {
341 /* Don't do this stuff if this is not a XMPP session! */
342 if (CC->h_command_function != xmpp_command_loop) return;
344 lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
345 if (XMPP->chardata != NULL) {
346 free(XMPP->chardata);
347 XMPP->chardata = NULL;
348 XMPP->chardata_len = 0;
349 XMPP->chardata_alloc = 0;
350 if (XMPP->message_body != NULL) {
351 free(XMPP->message_body);
354 XML_ParserFree(XMPP->xp);
361 * Here's where our XMPP session begins its happy day.
363 void xmpp_greeting(void) {
364 strcpy(CC->cs_clientname, "Jabber session");
365 CC->session_specific_data = malloc(sizeof(struct citxmpp));
366 memset(XMPP, 0, sizeof(struct citxmpp));
367 XMPP->last_event_processed = queue_event_seq;
369 /* XMPP does not use a greeting, but we still have to initialize some things. */
371 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
372 if (XMPP->xp == NULL) {
373 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
378 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
379 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
380 // XML_SetUserData(XMPP->xp, something...);
385 * Main command loop for XMPP sessions.
387 void xmpp_command_loop(void) {
392 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
393 retval = client_read(cmdbuf, 1);
395 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
400 /* FIXME ... this is woefully inefficient. */
402 XML_Parse(XMPP->xp, cmdbuf, 1, 0);
407 * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
409 void xmpp_async_loop(void) {
410 xmpp_process_events();
411 jabber_output_incoming_messages();
416 * Login hook for XMPP sessions
418 void xmpp_login_hook(void) {
419 xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
424 * Logout hook for XMPP sessions
426 void xmpp_logout_hook(void) {
427 xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
431 const char *CitadelServiceXMPP="XMPP";
433 #endif /* HAVE_EXPAT */
435 CTDL_MODULE_INIT(jabber)
439 CtdlRegisterServiceHook(config.c_xmpp_c2s_port,
445 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
446 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
447 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
448 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH);
449 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH);
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 */