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"
55 /* We have just received a <stream> tag from the client, so send them ours */
57 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
60 lprintf(CTDL_DEBUG, "New stream detected.\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>");
82 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
85 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
87 /* SASL (but only if we're not already logged in; this is important!) ... */
89 xmpp_output_auth_mechs();
92 /* ...and the ability to close XML tags using angle brackets. We should patent this. */
93 cprintf("</stream:features>");
97 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
102 /* Axe the namespace, we don't care about it */
103 safestrncpy(el, supplied_el, sizeof el);
104 while (sep = strchr(el, ':'), sep) {
108 lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
110 for (i=0; attr[i] != NULL; i+=2) {
111 lprintf(CTDL_DEBUG, " Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
114 if (!strcasecmp(el, "stream")) {
115 xmpp_stream_start(data, supplied_el, attr);
118 else if (!strcasecmp(el, "query")) {
119 XMPP->iq_query_xmlns[0] = 0;
120 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
123 else if (!strcasecmp(el, "iq")) {
124 for (i=0; attr[i] != NULL; i+=2) {
125 if (!strcasecmp(attr[i], "type")) {
126 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
128 else if (!strcasecmp(attr[i], "id")) {
129 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
131 else if (!strcasecmp(attr[i], "from")) {
132 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
134 else if (!strcasecmp(attr[i], "to")) {
135 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
140 else if (!strcasecmp(el, "auth")) {
141 XMPP->sasl_auth_mech[0] = 0;
142 for (i=0; attr[i] != NULL; i+=2) {
143 if (!strcasecmp(attr[i], "mechanism")) {
144 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
152 void xmpp_xml_end(void *data, const char *supplied_el) {
156 /* Axe the namespace, we don't care about it */
157 safestrncpy(el, supplied_el, sizeof el);
158 while (sep = strchr(el, ':'), sep) {
162 lprintf(CTDL_DEBUG, "XMPP ELEMENT END : <%s>\n", el);
163 if (XMPP->chardata_len > 0) {
164 lprintf(CTDL_DEBUG, " chardata: %s\n", XMPP->chardata);
167 if (!strcasecmp(el, "resource")) {
168 if (XMPP->chardata_len > 0) {
169 safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
170 sizeof XMPP->iq_client_resource);
174 else if (!strcasecmp(el, "iq")) {
177 * iq type="get" (handle queries)
179 if (!strcasecmp(XMPP->iq_type, "get")) {
182 * Query on a namespace
184 if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
185 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
186 XMPP->iq_to, XMPP->iq_query_xmlns);
190 * Unknown queries ... return the XML equivalent of a blank stare
193 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
199 * If this <iq> stanza was a "bind" attempt, process it ...
201 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
203 /* Generate the "full JID" of the client resource */
205 snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
209 XMPP->iq_client_resource
212 /* Tell the client what its JID is */
214 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
215 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
216 cprintf("<jid>%s</jid>", XMPP->client_jid);
221 else if (XMPP->iq_session) {
222 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
227 cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
228 cprintf("<error></error>");
232 /* Now clear these fields out so they don't get used by a future stanza */
234 XMPP->iq_from[0] = 0;
236 XMPP->iq_type[0] = 0;
237 XMPP->iq_client_resource[0] = 0;
238 XMPP->iq_session = 0;
239 XMPP->iq_query_xmlns[0] = 0;
242 else if (!strcasecmp(el, "auth")) {
244 /* Try to authenticate (this function is responsible for the output stanza) */
245 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
247 /* Now clear these fields out so they don't get used by a future stanza */
248 XMPP->sasl_auth_mech[0] = 0;
251 else if (!strcasecmp(el, "session")) {
252 XMPP->iq_session = 1;
255 XMPP->chardata_len = 0;
256 if (XMPP->chardata_alloc > 0) {
257 XMPP->chardata[0] = 0;
262 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
264 struct citxmpp *X = XMPP;
266 if (X->chardata_alloc == 0) {
267 X->chardata_alloc = SIZ;
268 X->chardata = malloc(X->chardata_alloc);
270 if ((X->chardata_len + len + 1) > X->chardata_alloc) {
271 X->chardata_alloc = X->chardata_len + len + 1024;
272 X->chardata = realloc(X->chardata, X->chardata_alloc);
274 memcpy(&X->chardata[X->chardata_len], s, len);
275 X->chardata_len += len;
276 X->chardata[X->chardata_len] = 0;
281 * This cleanup function blows away the temporary memory and files used by the XMPP service.
283 void xmpp_cleanup_function(void) {
285 /* Don't do this stuff if this is not a XMPP session! */
286 if (CC->h_command_function != xmpp_command_loop) return;
288 lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
289 if (XMPP->chardata != NULL) {
290 free(XMPP->chardata);
291 XMPP->chardata = NULL;
292 XMPP->chardata_len = 0;
293 XMPP->chardata_alloc = 0;
295 XML_ParserFree(XMPP->xp);
302 * Here's where our XMPP session begins its happy day.
304 void xmpp_greeting(void) {
305 strcpy(CC->cs_clientname, "Jabber session");
306 CC->session_specific_data = malloc(sizeof(struct citxmpp));
307 memset(XMPP, 0, sizeof(struct citxmpp));
309 /* XMPP does not use a greeting, but we still have to initialize some things. */
311 XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
312 if (XMPP->xp == NULL) {
313 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
318 XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
319 XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
320 // XML_SetUserData(XMPP->xp, something...);
325 * Main command loop for XMPP sessions.
327 void xmpp_command_loop(void) {
332 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
333 retval = client_read(cmdbuf, 1);
335 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
340 /* FIXME ... this is woefully inefficient. */
342 XML_Parse(XMPP->xp, cmdbuf, 1, 0);
345 const char *CitadelServiceXMPP="XMPP";
347 #endif /* HAVE_EXPAT */
349 CTDL_MODULE_INIT(jabber)
353 /* CtdlRegisterServiceHook(config.c_xmpp_port, FIXME */
354 CtdlRegisterServiceHook(5222,
360 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
362 lprintf(CTDL_INFO, "This server is missing the Expat XML parser. Jabber service will be disabled.\n");
366 /* return our Subversion id for the Log */