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)
60 lprintf(CTDL_DEBUG, "New XMPP stream.\n");
63 if (!strcasecmp(attr[0], "to")) {
64 safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
69 cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
71 cprintf("<stream:stream ");
72 cprintf("from=\"%s\" ", XMPP->server_name);
73 cprintf("id=\"%08x\" ", CC->cs_pid);
74 cprintf("version=\"1.0\" ");
75 cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
76 cprintf("xmlns=\"jabber:client\">");
78 /* The features of this stream are... */
79 cprintf("<stream:features>");
81 #ifdef HAVE_OPENSSL_XXXX_COMMENTED_OUT
82 /* TLS encryption (but only if it isn't already active) */
83 if (!CC->redirect_ssl) {
84 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
89 /* If we're not logged in yet, offer SASL as our feature set */
90 xmpp_output_auth_mechs();
92 /* Also offer non-SASL authentication
93 cprintf("<auth xmlns=\"http://jabber.org/features/iq-auth\"/>"); */
96 /* Offer binding and sessions as part of our feature set */
97 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
98 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
100 cprintf("</stream:features>");
102 CC->is_async = 1; /* XMPP sessions are inherently async-capable */
106 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
111 /* Axe the namespace, we don't care about it */
112 safestrncpy(el, supplied_el, sizeof el);
113 while (sep = strchr(el, ':'), sep) {
117 lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
119 for (i=0; attr[i] != NULL; i+=2) {
120 lprintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
123 if (!strcasecmp(el, "stream")) {
124 xmpp_stream_start(data, supplied_el, attr);
127 else if (!strcasecmp(el, "query")) {
128 XMPP->iq_query_xmlns[0] = 0;
129 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
132 else if (!strcasecmp(el, "bind")) {
133 XMPP->bind_requested = 1;
136 else if (!strcasecmp(el, "iq")) {
137 for (i=0; attr[i] != NULL; i+=2) {
138 if (!strcasecmp(attr[i], "type")) {
139 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
141 else if (!strcasecmp(attr[i], "id")) {
142 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
144 else if (!strcasecmp(attr[i], "from")) {
145 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
147 else if (!strcasecmp(attr[i], "to")) {
148 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
153 else if (!strcasecmp(el, "auth")) {
154 XMPP->sasl_auth_mech[0] = 0;
155 for (i=0; attr[i] != NULL; i+=2) {
156 if (!strcasecmp(attr[i], "mechanism")) {
157 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
162 else if (!strcasecmp(el, "message")) {
163 for (i=0; attr[i] != NULL; i+=2) {
164 if (!strcasecmp(attr[i], "to")) {
165 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
170 else if (!strcasecmp(el, "html")) {
171 ++XMPP->html_tag_level;
177 void xmpp_xml_end(void *data, const char *supplied_el) {
181 /* Axe the namespace, we don't care about it */
182 safestrncpy(el, supplied_el, sizeof el);
183 while (sep = strchr(el, ':'), sep) {
187 lprintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
188 if (XMPP->chardata_len > 0) {
189 lprintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
192 if (!strcasecmp(el, "resource")) {
193 if (XMPP->chardata_len > 0) {
194 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
195 sizeof XMPP->iq_client_resource);
199 else if (!strcasecmp(el, "iq")) {
202 * iq type="get" (handle queries)
204 if (!strcasecmp(XMPP->iq_type, "get")) {
207 * Query on a namespace
209 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
210 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
211 XMPP->iq_to, XMPP->iq_query_xmlns);
215 * Unknown queries ... return the XML equivalent of a blank stare
218 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
224 * If this <iq> stanza was a "bind" attempt, process it ...
227 (XMPP->bind_requested)
228 && (!IsEmptyStr(XMPP->iq_id))
229 && (!IsEmptyStr(XMPP->iq_client_resource))
233 /* Generate the "full JID" of the client resource */
235 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
238 XMPP->iq_client_resource
241 /* Tell the client what its JID is */
243 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
244 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
245 cprintf("<jid>%s</jid>", XMPP->client_jid);
250 else if (XMPP->iq_session) {
251 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
256 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
257 cprintf("<error></error>");
261 /* Now clear these fields out so they don't get used by a future stanza */
263 XMPP->iq_from[0] = 0;
265 XMPP->iq_type[0] = 0;
266 XMPP->iq_client_resource[0] = 0;
267 XMPP->iq_session = 0;
268 XMPP->iq_query_xmlns[0] = 0;
269 XMPP->bind_requested = 0;
272 else if (!strcasecmp(el, "auth")) {
274 /* Try to authenticate (this function is responsible for the output stanza) */
275 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
277 /* Now clear these fields out so they don't get used by a future stanza */
278 XMPP->sasl_auth_mech[0] = 0;
281 else if (!strcasecmp(el, "session")) {
282 XMPP->iq_session = 1;
285 else if (!strcasecmp(el, "presence")) {
287 /* Respond to a <presence> update by firing back with presence information
288 * on the entire wholist. Check this assumption, it's probably wrong.
290 jabber_wholist_presence_dump();
293 else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
294 if (XMPP->message_body != NULL) {
295 free(XMPP->message_body);
296 XMPP->message_body = NULL;
298 if (XMPP->chardata_len > 0) {
299 XMPP->message_body = strdup(XMPP->chardata);
303 else if (!strcasecmp(el, "message")) {
304 jabber_send_message(XMPP->message_to, XMPP->message_body);
305 XMPP->html_tag_level = 0;
308 else if (!strcasecmp(el, "html")) {
309 --XMPP->html_tag_level;
312 else if (!strcasecmp(el, "starttls")) {
314 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
315 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
316 if (!CC->redirect_ssl) CC->kill_me = 1;
318 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
323 XMPP->chardata_len = 0;
324 if (XMPP->chardata_alloc > 0) {
325 XMPP->chardata[0] = 0;
330 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
332 struct citxmpp *X = XMPP;
334 if (X->chardata_alloc == 0) {
335 X->chardata_alloc = SIZ;
336 X->chardata = malloc(X->chardata_alloc);
338 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
339 X->chardata_alloc = X->chardata_len + len + 1024;
340 X->chardata = realloc(X->chardata, X->chardata_alloc);
342 memcpy(&X->chardata[X->chardata_len], s, len);
343 X->chardata_len += len;
344 X->chardata[X->chardata_len] = 0;
349 * This cleanup function blows away the temporary memory and files used by the XMPP service.
351 void xmpp_cleanup_function(void) {
353 /* Don't do this stuff if this is not a XMPP session! */
354 if (CC->h_command_function != xmpp_command_loop) return;
356 lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
357 if (XMPP->chardata != NULL) {
358 free(XMPP->chardata);
359 XMPP->chardata = NULL;
360 XMPP->chardata_len = 0;
361 XMPP->chardata_alloc = 0;
362 if (XMPP->message_body != NULL) {
363 free(XMPP->message_body);
366 XML_ParserFree(XMPP->xp);
373 * Here's where our XMPP session begins its happy day.
375 void xmpp_greeting(void) {
376 strcpy(CC->cs_clientname, "Jabber session");
377 CC->session_specific_data = malloc(sizeof(struct citxmpp));
378 memset(XMPP, 0, sizeof(struct citxmpp));
379 XMPP->last_event_processed = queue_event_seq;
381 /* XMPP does not use a greeting, but we still have to initialize some things. */
383 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
384 if (XMPP->xp == NULL) {
385 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
390 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
391 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
392 // XML_SetUserData(XMPP->xp, something...);
397 * Main command loop for XMPP sessions.
399 void xmpp_command_loop(void) {
404 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
405 retval = client_read(cmdbuf, 1);
407 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
412 /* FIXME ... this is woefully inefficient. */
414 XML_Parse(XMPP->xp, cmdbuf, 1, 0);
419 * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
421 void xmpp_async_loop(void) {
422 xmpp_process_events();
423 jabber_output_incoming_messages();
428 * Login hook for XMPP sessions
430 void xmpp_login_hook(void) {
431 xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
436 * Logout hook for XMPP sessions
438 void xmpp_logout_hook(void) {
439 xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
443 const char *CitadelServiceXMPP="XMPP";
445 #endif /* HAVE_EXPAT */
447 CTDL_MODULE_INIT(jabber)
451 CtdlRegisterServiceHook(config.c_xmpp_c2s_port,
457 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
458 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN);
459 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT);
460 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH);
461 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH);
463 lprintf(CTDL_INFO, "This server is missing the Expat XML parser. Jabber service will be disabled.\n");
467 /* return our Subversion id for the Log */