]> code.citadel.org Git - citadel.git/blob - citadel/modules/jabber/serv_xmpp.c
xmpp sasl hacks
[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         /* Binding... */
82         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
83
84         /* Sessions... */
85         cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
86
87         /* A really bad SASL implementation... */
88         xmpp_output_auth_mechs();
89
90         /* ...and the ability to close XML tags using angle brackets.  We should patent this. */
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, "iq")) {
117                 const char *iqtype = NULL;
118                 const char *iqid = NULL;
119                 for (i=0; attr[i] != NULL; i+=2) {
120                         if (!strcasecmp(attr[i], "type")) iqtype = attr[i+1];
121                         if (!strcasecmp(attr[i], "id")) iqid = attr[i+1];
122                 }
123                 if ((iqtype != NULL) && (iqid != NULL)) {
124                         if (!strcasecmp(iqtype, "set")) {
125                                 safestrncpy(XMPP->iq_bind_id, iqid, sizeof XMPP->iq_bind_id);
126                         }
127                 }
128         }
129
130         else if (!strcasecmp(el, "auth")) {
131                 XMPP->sasl_auth_mech[0] = 0;
132                 for (i=0; attr[i] != NULL; i+=2) {
133                         if (!strcasecmp(attr[i], "mechanism")) {
134                                 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
135                         }
136                 }
137         }
138 }
139
140
141
142 void xmpp_xml_end(void *data, const char *supplied_el) {
143         char el[256];
144         char *sep = NULL;
145
146         /* Axe the namespace, we don't care about it */
147         safestrncpy(el, supplied_el, sizeof el);
148         while (sep = strchr(el, ':'), sep) {
149                 strcpy(el, ++sep);
150         }
151
152         lprintf(CTDL_DEBUG, "XMPP ELEMENT END  : <%s>\n", el);
153         if (XMPP->chardata_len > 0) {
154                 lprintf(CTDL_DEBUG, "          chardata: %s\n", XMPP->chardata);
155         }
156
157         if (!strcasecmp(el, "resource")) {
158                 if (XMPP->chardata_len > 0) {
159                         safestrncpy(XMPP->iq_client_resource, XMPP->chardata,
160                                 sizeof XMPP->iq_client_resource);
161                 }
162         }
163
164         else if (!strcasecmp(el, "iq")) {
165
166
167                 /* If this <iq> stanza was a "bind" attempt, process it ... */
168
169                 if ( (!IsEmptyStr(XMPP->iq_bind_id)) && (!IsEmptyStr(XMPP->iq_client_resource)) ) {
170
171                         /* Generate the "full JID" of the client resource */
172
173                         snprintf(XMPP->client_jid, sizeof XMPP->client_jid,
174                                 "%d@%s/%s",
175                                 CC->cs_pid,
176                                 config.c_fqdn,
177                                 XMPP->iq_client_resource
178                         );
179
180                         /* Tell the client what its JID is */
181
182                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_bind_id);
183                         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
184                         cprintf("<jid>%s</jid>", XMPP->client_jid);
185                         cprintf("</bind>");
186                         cprintf("</iq>");
187                 }
188
189                 else if (XMPP->iq_session) {
190                         cprintf("<iq type=\"result\" id=\"%s\">", XMPP->iq_bind_id);
191                         cprintf("</iq>");
192                 }
193
194                 else {
195                         cprintf("<iq type=\"error\" id=\"%s\">", XMPP->iq_bind_id);
196                         cprintf("<error></error>");
197                         cprintf("</iq>");
198                 }
199
200                 /* Now clear these fields out so they don't get used by a future stanza */
201                 XMPP->iq_bind_id[0] = 0;
202                 XMPP->iq_client_resource[0] = 0;
203                 XMPP->iq_session = 1;
204         }
205
206         else if (!strcasecmp(el, "auth")) {
207
208                 /* Try to authenticate (this function is responsible for the output stanza) */
209                 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
210
211                 /* Now clear these fields out so they don't get used by a future stanza */
212                 XMPP->sasl_auth_mech[0] = 0;
213         }
214
215         else if (!strcasecmp(el, "session")) {
216                 XMPP->iq_session = 1;
217         }
218
219         XMPP->chardata_len = 0;
220         if (XMPP->chardata_alloc > 0) {
221                 XMPP->chardata[0] = 0;
222         }
223 }
224
225
226 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
227 {
228         struct citxmpp *X = XMPP;
229
230         if (X->chardata_alloc == 0) {
231                 X->chardata_alloc = SIZ;
232                 X->chardata = malloc(X->chardata_alloc);
233         }
234         if ((X->chardata_len + len + 1) > X->chardata_alloc) {
235                 X->chardata_alloc = X->chardata_len + len + 1024;
236                 X->chardata = realloc(X->chardata, X->chardata_alloc);
237         }
238         memcpy(&X->chardata[X->chardata_len], s, len);
239         X->chardata_len += len;
240         X->chardata[X->chardata_len] = 0;
241 }
242
243
244 /*
245  * This cleanup function blows away the temporary memory and files used by the XMPP service.
246  */
247 void xmpp_cleanup_function(void) {
248
249         /* Don't do this stuff if this is not a XMPP session! */
250         if (CC->h_command_function != xmpp_command_loop) return;
251
252         lprintf(CTDL_DEBUG, "Performing XMPP cleanup hook\n");
253         if (XMPP->chardata != NULL) {
254                 free(XMPP->chardata);
255                 XMPP->chardata = NULL;
256                 XMPP->chardata_len = 0;
257                 XMPP->chardata_alloc = 0;
258         }
259         XML_ParserFree(XMPP->xp);
260         free(XMPP);
261 }
262
263
264
265 /*
266  * Here's where our XMPP session begins its happy day.
267  */
268 void xmpp_greeting(void) {
269         strcpy(CC->cs_clientname, "Jabber session");
270         CC->session_specific_data = malloc(sizeof(struct citxmpp));
271         memset(XMPP, 0, sizeof(struct citxmpp));
272
273         /* XMPP does not use a greeting, but we still have to initialize some things. */
274
275         XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
276         if (XMPP->xp == NULL) {
277                 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
278                 CC->kill_me = 1;
279                 return;
280         }
281
282         XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
283         XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
284         // XML_SetUserData(XMPP->xp, something...);
285 }
286
287
288 /* 
289  * Main command loop for XMPP sessions.
290  */
291 void xmpp_command_loop(void) {
292         char cmdbuf[16];
293         int retval;
294
295         time(&CC->lastcmd);
296         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
297         retval = client_read(cmdbuf, 1);
298         if (retval != 1) {
299                 lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
300                 CC->kill_me = 1;
301                 return;
302         }
303
304         /* FIXME ... this is woefully inefficient. */
305
306         XML_Parse(XMPP->xp, cmdbuf, 1, 0);
307 }
308
309 const char *CitadelServiceXMPP="XMPP";
310
311 #endif  /* HAVE_EXPAT */
312
313 CTDL_MODULE_INIT(jabber)
314 {
315 #ifdef HAVE_EXPAT
316         if (!threading) {
317                 /* CtdlRegisterServiceHook(config.c_xmpp_port,          FIXME   */
318                 CtdlRegisterServiceHook(5222,
319                                         NULL,
320                                         xmpp_greeting,
321                                         xmpp_command_loop,
322                                         NULL,
323                                         CitadelServiceXMPP);
324                 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP);
325         #else
326                 lprintf(CTDL_INFO, "This server is missing the Expat XML parser.  Jabber service will be disabled.\n");
327 #endif
328         }
329
330         /* return our Subversion id for the Log */
331         return "$Id: $";
332 }