Slight change to the way we offer XMPP stream features.
[citadel.git] / citadel / modules / jabber / serv_xmpp.c
1 /*
2  * $Id$ 
3  *
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.
7  *
8  */
9
10 #include "sysdep.h"
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <pwd.h>
17 #include <errno.h>
18 #include <sys/types.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <sys/wait.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <ctype.h>
35 #include <libcitadel.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "citserver.h"
39 #include "support.h"
40 #include "config.h"
41 #include "room_ops.h"
42 #include "user_ops.h"
43 #include "policy.h"
44 #include "database.h"
45 #include "msgbase.h"
46 #include "internet_addressing.h"
47 #include "md5.h"
48 #include "ctdl_module.h"
49
50 #ifdef HAVE_EXPAT
51 #include <expat.h>
52 #include "serv_xmpp.h"
53
54
55 /* We have just received a <stream> tag from the client, so send them ours */
56
57 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
58 {
59
60         lprintf(CTDL_DEBUG, "New stream detected.\n");
61
62         while (*attr) {
63                 if (!strcasecmp(attr[0], "to")) {
64                         safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
65                 }
66                 attr += 2;
67         }
68
69         cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
70
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\">");
77
78         /* The features of this stream are... */
79         cprintf("<stream:features>");
80
81         if (!CC->logged_in) {
82                 /* If we're not logged in yet, offer SASL as our feature set */
83                 xmpp_output_auth_mechs();
84         }
85         else {
86                 /* If we've logged in, now offer binding and sessions as our feature set */
87                 cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
88                 cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
89         }
90
91         cprintf("</stream:features>");
92 }
93
94
95 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
96         char el[256];
97         char *sep = NULL;
98         int i;
99
100         /* Axe the namespace, we don't care about it */
101         safestrncpy(el, supplied_el, sizeof el);
102         while (sep = strchr(el, ':'), sep) {
103                 strcpy(el, ++sep);
104         }
105
106         lprintf(CTDL_DEBUG, "XMPP ELEMENT START: <%s>\n", el);
107
108         for (i=0; attr[i] != NULL; i+=2) {
109                 lprintf(CTDL_DEBUG, "                    Attribute '%s' = '%s'\n", attr[i], attr[i+1]);
110         }
111
112         if (!strcasecmp(el, "stream")) {
113                 xmpp_stream_start(data, supplied_el, attr);
114         }
115
116         else if (!strcasecmp(el, "query")) {
117                 XMPP->iq_query_xmlns[0] = 0;
118                 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
119         }
120
121         else if (!strcasecmp(el, "iq")) {
122                 for (i=0; attr[i] != NULL; i+=2) {
123                         if (!strcasecmp(attr[i], "type")) {
124                                 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
125                         }
126                         else if (!strcasecmp(attr[i], "id")) {
127                                 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
128                         }
129                         else if (!strcasecmp(attr[i], "from")) {
130                                 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
131                         }
132                         else if (!strcasecmp(attr[i], "to")) {
133                                 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
134                         }
135                 }
136         }
137
138         else if (!strcasecmp(el, "auth")) {
139                 XMPP->sasl_auth_mech[0] = 0;
140                 for (i=0; attr[i] != NULL; i+=2) {
141                         if (!strcasecmp(attr[i], "mechanism")) {
142                                 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
143                         }
144                 }
145         }
146 }
147
148
149
150 void xmpp_xml_end(void *data, const char *supplied_el) {
151         char el[256];
152         char *sep = NULL;
153
154         /* Axe the namespace, we don't care about it */
155         safestrncpy(el, supplied_el, sizeof el);
156         while (sep = strchr(el, ':'), sep) {
157                 strcpy(el, ++sep);
158         }
159
160         lprintf(CTDL_DEBUG, "XMPP ELEMENT END  : <%s>\n", el);
161         if (XMPP->chardata_len > 0) {
162                 lprintf(CTDL_DEBUG, "          chardata: %s\n", XMPP->chardata);
163         }
164
165         if (!strcasecmp(el, "resource")) {
166                 if (XMPP->chardata_len > 0) {
167                         safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
168                                 sizeof XMPP->iq_client_resource);
169                 }
170         }
171
172         else if (!strcasecmp(el, "iq")) {
173
174                 /*
175                  * iq type="get" (handle queries)
176                  */
177                 if (!strcasecmp(XMPP->iq_type, "get")) {
178
179                         /*
180                          * Query on a namespace
181                          */
182                         if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
183                                 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from,
184                                                 XMPP->iq_to, XMPP->iq_query_xmlns);
185                         }
186
187                         /*
188                          * Unknown queries ... return the XML equivalent of a blank stare
189                          */
190                         else {
191                                 cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
192                                 cprintf("</iq>");
193                         }
194                 }
195
196                 /*
197                  * If this <iq> stanza was a "bind" attempt, process it ...
198                  */
199                 else if ( (!IsEmptyStr(XMPP->iq_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
200
201                         /* Generate the "full JID" of the client resource */
202
203                         // snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
204                         //      "%d@%s/%s",
205                         //      CC->cs_pid,
206                         //      config.c_fqdn,
207                         //      XMPP->iq_client_resource
208                         //);
209
210                         snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
211                                 "%s/%s",
212                                 CC->cs_inet_email,
213                                 XMPP->iq_client_resource
214                         );
215
216                         /* Tell the client what its JID is */
217
218                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
219                         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
220                         cprintf("<jid>%s</jid>", XMPP->client_jid);
221                         cprintf("</bind>");
222                         cprintf("</iq>");
223                 }
224
225                 else if (XMPP->iq_session) {
226                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_id);
227                         cprintf("</iq>");
228                 }
229
230                 else {
231                         cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_id);
232                         cprintf("<error></error>");
233                         cprintf("</iq>");
234                 }
235
236                 /* Now clear these fields out so they don't get used by a future stanza */
237                 XMPP->iq_id[0] = 0;
238                 XMPP->iq_from[0] = 0;
239                 XMPP->iq_to[0] = 0;
240                 XMPP->iq_type[0] = 0;
241                 XMPP->iq_client_resource[0] = 0;
242                 XMPP->iq_session = 0;
243                 XMPP->iq_query_xmlns[0] = 0;
244         }
245
246         else if (!strcasecmp(el, "auth")) {
247
248                 /* Try to authenticate (this function is responsible for the output stanza) */
249                 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
250
251                 /* Now clear these fields out so they don't get used by a future stanza */
252                 XMPP->sasl_auth_mech[0] = 0;
253         }
254
255         else if (!strcasecmp(el, "session")) {
256                 XMPP->iq_session = 1;
257         }
258
259         XMPP->chardata_len = 0;
260         if (XMPP->chardata_alloc > 0) {
261                 XMPP->chardata[0] = 0;
262         }
263 }
264
265
266 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
267 {
268         struct citxmpp *X = XMPP;
269
270         if (X->chardata_alloc == 0) {
271                 X->chardata_alloc = SIZ;
272                 X->chardata = malloc(X->chardata_alloc);
273         }
274         if ((X->chardata_len + len + 1) > X->chardata_alloc) {
275                 X->chardata_alloc = X->chardata_len + len + 1024;
276                 X->chardata = realloc(X->chardata, X->chardata_alloc);
277         }
278         memcpy(&X->chardata[X->chardata_len], s, len);
279         X->chardata_len += len;
280         X->chardata[X->chardata_len] = 0;
281 }
282
283
284 /*
285  * This cleanup function blows away the temporary memory and files used by the XMPP service.
286  */
287 void xmpp_cleanup_function(void) {
288
289         /* Don't do this stuff if this is not a XMPP session! */
290         if (CC->h_command_function != xmpp_command_loop) return;
291
292         lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
293         if (XMPP->chardata != NULL) {
294                 free(XMPP->chardata);
295                 XMPP->chardata = NULL;
296                 XMPP->chardata_len = 0;
297                 XMPP->chardata_alloc = 0;
298         }
299         XML_ParserFree(XMPP->xp);
300         free(XMPP);
301 }
302
303
304
305 /*
306  * Here's where our XMPP session begins its happy day.
307  */
308 void xmpp_greeting(void) {
309         strcpy(CC->cs_clientname, "Jabber session");
310         CC->session_specific_data = malloc(sizeof(struct citxmpp));
311         memset(XMPP, 0, sizeof(struct citxmpp));
312
313         /* XMPP does not use a greeting, but we still have to initialize some things. */
314
315         XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
316         if (XMPP->xp == NULL) {
317                 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
318                 CC->kill_me = 1;
319                 return;
320         }
321
322         XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
323         XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
324         // XML_SetUserData(XMPP->xp, something...);
325 }
326
327
328 /* 
329  * Main command loop for XMPP sessions.
330  */
331 void xmpp_command_loop(void) {
332         char cmdbuf[16];
333         int retval;
334
335         time(&CC->lastcmd);
336         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
337         retval = client_read(cmdbuf, 1);
338         if (retval != 1) {
339                 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
340                 CC->kill_me = 1;
341                 return;
342         }
343
344         /* FIXME ... this is woefully inefficient. */
345
346         XML_Parse(XMPP->xp, cmdbuf, 1, 0);
347 }
348
349 const char *CitadelServiceXMPP="XMPP";
350
351 #endif  /* HAVE_EXPAT */
352
353 CTDL_MODULE_INIT(jabber)
354 {
355 #ifdef HAVE_EXPAT
356         if (!threading) {
357                 /* CtdlRegisterServiceHook(config.c_xmpp_port,          FIXME   */
358                 CtdlRegisterServiceHook(5222,
359                                         NULL,
360                                         xmpp_greeting,
361                                         xmpp_command_loop,
362                                         NULL,
363                                         CitadelServiceXMPP);
364                 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
365         #else
366                 lprintf(CTDL_INFO, "This server is missing the Expat XML parser.  Jabber service will be disabled.\n");
367 #endif
368         }
369
370         /* return our Subversion id for the Log */
371         return "$Id$";
372 }