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