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