xmpp non sasl authenticate does not need the resource name
[citadel.git] / citadel / modules / xmpp / serv_xmpp.c
1 /*
2  * XMPP (Jabber) service for the Citadel system
3  * Copyright (c) 2007-2018 by Art Cancro and citadel.org
4  *
5  * This program is open source software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "sysdep.h"
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
32 # include <time.h>
33 #else
34 # if HAVE_SYS_TIME_H
35 #  include <sys/time.h>
36 # else
37 #  include <time.h>
38 # endif
39 #endif
40
41 #include <sys/wait.h>
42 #include <string.h>
43 #include <limits.h>
44 #include <ctype.h>
45 #include <libcitadel.h>
46 #include <expat.h>
47 #include "citadel.h"
48 #include "server.h"
49 #include "citserver.h"
50 #include "support.h"
51 #include "config.h"
52 #include "user_ops.h"
53 #include "database.h"
54 #include "msgbase.h"
55 #include "internet_addressing.h"
56 #include "md5.h"
57 #include "ctdl_module.h"
58 #include "serv_xmpp.h"
59
60 /* uncomment for more verbosity - it will log all received XML tags */
61 #define XMPP_XML_DEBUG
62
63 /* XML_StopParser is present in expat 2.x */
64 #if XML_MAJOR_VERSION > 1
65 #define HAVE_XML_STOPPARSER
66 #endif
67
68 struct xmpp_event *xmpp_queue = NULL;
69
70
71 #ifdef HAVE_XML_STOPPARSER
72 /* Stop the parser if an entity declaration is hit. */
73 static void xmpp_entity_declaration(void *userData, const XML_Char *entityName,
74                                 int is_parameter_entity, const XML_Char *value,
75                                 int value_length, const XML_Char *base,
76                                 const XML_Char *systemId, const XML_Char *publicId,
77                                 const XML_Char *notationName
78 ) {
79         syslog(LOG_WARNING, "xmpp: illegal entity declaration encountered; stopping parser.");
80         XML_StopParser(XMPP->xp, XML_FALSE);
81 }
82 #endif
83
84
85 /*
86  * Given a source string and a target buffer, returns the string
87  * properly escaped for insertion into an XML stream.  Returns a
88  * pointer to the target buffer for convenience.
89  */
90 static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
91 {
92         int n = 0;
93         unsigned char test = (1<<7);
94
95         if ((*CharS & 0xC0) != 0xC0) 
96                 return 1;
97
98         while ((n < 8) && 
99                ((test & ((unsigned char)*CharS)) != 0)) 
100         {
101                 test = test >> 1;
102                 n ++;
103         }
104         if ((n > 6) || ((CharE - CharS) < n))
105                 n = 0;
106         return n;
107 }
108
109
110 char *xmlesc(char *buf, char *str, int bufsiz)
111 {
112         int IsUtf8Sequence;
113         char *ptr, *pche;
114         unsigned char ch;
115         int inlen;
116         int len = 0;
117
118         if (!buf) return(NULL);
119         buf[0] = 0;
120         len = 0;
121         if (!str) {
122                 return(buf);
123         }
124         inlen = strlen(str);
125         pche = str + inlen;
126
127         for (ptr=str; *ptr; ptr++) {
128                 ch = *ptr;
129                 if (ch == '<') {
130                         strcpy(&buf[len], "&lt;");
131                         len += 4;
132                 }
133                 else if (ch == '>') {
134                         strcpy(&buf[len], "&gt;");
135                         len += 4;
136                 }
137                 else if (ch == '&') {
138                         strcpy(&buf[len], "&amp;");
139                         len += 5;
140                 }
141                 else if ((ch >= 0x20) && (ch <= 0x7F)) {
142                         buf[len++] = ch;
143                         buf[len] = 0;
144                 }
145                 else if (ch < 0x20) {
146                         /* we probably shouldn't be doing this */
147                         buf[len++] = '_';
148                         buf[len] = 0;
149                 }
150                 else {
151                         IsUtf8Sequence =  Ctdl_GetUtf8SequenceLength(ptr, pche);
152                         if (IsUtf8Sequence)
153                         {
154                                 while ((IsUtf8Sequence > 0) && 
155                                        (ptr < pche))
156                                 {
157                                         buf[len] = *ptr;
158                                         ptr ++;
159                                         --IsUtf8Sequence;
160                                 }
161                         }
162                         else
163                         {
164                                 char oct[10];
165                                 sprintf(oct, "&#%o;", ch);
166                                 strcpy(&buf[len], oct);
167                                 len += strlen(oct);
168                         }
169                 }
170                 if ((len + 6) > bufsiz) {
171                         return(buf);
172                 }
173         }
174         return(buf);
175 }
176
177
178 /*
179  * We have just received a <stream> tag from the client, so send them ours
180  */
181 void xmpp_stream_start(void *data, const char *supplied_el, const char **attr)
182 {
183         char xmlbuf[256];
184
185         while (*attr) {
186                 if (!strcasecmp(attr[0], "to")) {
187                         safestrncpy(XMPP->server_name, attr[1], sizeof XMPP->server_name);
188                 }
189                 attr += 2;
190         }
191
192         cprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
193
194         cprintf("<stream:stream ");
195         cprintf("from=\"%s\" ", xmlesc(xmlbuf, XMPP->server_name, sizeof xmlbuf));
196         cprintf("id=\"%08x\" ", CC->cs_pid);
197         cprintf("version=\"1.0\" ");
198         cprintf("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
199         cprintf("xmlns=\"jabber:client\">");
200
201         /* The features of this stream are... */
202         cprintf("<stream:features>");
203
204         /*
205          * TLS encryption (but only if it isn't already active)
206          */ 
207 #ifdef HAVE_OPENSSL
208         if (!CC->redirect_ssl) {
209                 cprintf("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>");
210         }
211 #endif
212
213         if (!CC->logged_in) {
214                 /* If we're not logged in yet, offer SASL as our feature set */
215                 xmpp_output_auth_mechs();
216
217                 /* Also offer non-SASL authentication */
218                 cprintf("<auth xmlns=\"http://jabber.org/features/iq-auth\"/>");
219         }
220
221         /* Offer binding and sessions as part of our feature set */
222         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>");
223         cprintf("<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>");
224
225         cprintf("</stream:features>");
226
227         CC->is_async = 1;               /* XMPP sessions are inherently async-capable */
228 }
229
230
231 void xmpp_xml_start(void *data, const char *supplied_el, const char **attr) {
232         char el[256];
233         char *sep = NULL;
234         int i;
235
236         /* Axe the namespace, we don't care about it */
237         safestrncpy(el, supplied_el, sizeof el);
238         while (sep = strchr(el, ':'), sep) {
239                 strcpy(el, ++sep);
240         }
241
242 #ifdef XMPP_XML_DEBUG
243         syslog(LOG_DEBUG, "xmpp: ELEMENT START: <%s>", el);
244         for (i=0; attr[i] != NULL; i+=2) {
245                 syslog(LOG_DEBUG, "xmpp: Attribute '%s' = '%s'", attr[i], attr[i+1]);
246         }
247 #endif
248
249         if (!strcasecmp(el, "stream")) {
250                 xmpp_stream_start(data, supplied_el, attr);
251         }
252
253         else if (!strcasecmp(el, "query")) {
254                 XMPP->iq_query_xmlns[0] = 0;
255                 safestrncpy(XMPP->iq_query_xmlns, supplied_el, sizeof XMPP->iq_query_xmlns);
256         }
257
258         else if (!strcasecmp(el, "bind")) {
259                 XMPP->bind_requested = 1;
260         }
261
262         else if (!strcasecmp(el, "iq")) {
263                 for (i=0; attr[i] != NULL; i+=2) {
264                         if (!strcasecmp(attr[i], "type")) {
265                                 safestrncpy(XMPP->iq_type, attr[i+1], sizeof XMPP->iq_type);
266                         }
267                         else if (!strcasecmp(attr[i], "id")) {
268                                 safestrncpy(XMPP->iq_id, attr[i+1], sizeof XMPP->iq_id);
269                         }
270                         else if (!strcasecmp(attr[i], "from")) {
271                                 safestrncpy(XMPP->iq_from, attr[i+1], sizeof XMPP->iq_from);
272                         }
273                         else if (!strcasecmp(attr[i], "to")) {
274                                 safestrncpy(XMPP->iq_to, attr[i+1], sizeof XMPP->iq_to);
275                         }
276                 }
277         }
278
279         else if (!strcasecmp(el, "auth")) {
280                 XMPP->sasl_auth_mech[0] = 0;
281                 for (i=0; attr[i] != NULL; i+=2) {
282                         if (!strcasecmp(attr[i], "mechanism")) {
283                                 safestrncpy(XMPP->sasl_auth_mech, attr[i+1], sizeof XMPP->sasl_auth_mech);
284                         }
285                 }
286         }
287
288         else if (!strcasecmp(el, "message")) {
289                 for (i=0; attr[i] != NULL; i+=2) {
290                         if (!strcasecmp(attr[i], "to")) {
291                                 safestrncpy(XMPP->message_to, attr[i+1], sizeof XMPP->message_to);
292                         }
293                 }
294         }
295
296         else if (!strcasecmp(el, "html")) {
297                 ++XMPP->html_tag_level;
298         }
299 }
300
301
302
303 void xmpp_xml_end(void *data, const char *supplied_el) {
304         char el[256];
305         char *sep = NULL;
306         char xmlbuf[256];
307
308         /* Axe the namespace, we don't care about it */
309         safestrncpy(el, supplied_el, sizeof el);
310         while (sep = strchr(el, ':'), sep) {
311                 strcpy(el, ++sep);
312         }
313
314 #ifdef XMPP_XML_DEBUG
315         syslog(LOG_DEBUG, "xmpp: ELEMENT END  : <%s>", el);
316         if (XMPP->chardata_len > 0) {
317                 syslog(LOG_DEBUG, "xmpp: chardata: %s", XMPP->chardata);
318         }
319 #endif
320
321         if (!strcasecmp(el, "resource")) {
322                 if (XMPP->chardata_len > 0) {
323                         safestrncpy(XMPP->iq_client_resource, XMPP->chardata, sizeof XMPP->iq_client_resource);
324                         striplt(XMPP->iq_client_resource);
325                 }
326         }
327
328         else if (!strcasecmp(el, "username")) {         /* NON SASL ONLY */
329                 if (XMPP->chardata_len > 0) {
330                         safestrncpy(XMPP->iq_client_username, XMPP->chardata, sizeof XMPP->iq_client_username);
331                         striplt(XMPP->iq_client_username);
332                 }
333         }
334
335         else if (!strcasecmp(el, "password")) {         /* NON SASL ONLY */
336                 if (XMPP->chardata_len > 0) {
337                         safestrncpy(XMPP->iq_client_password, XMPP->chardata, sizeof XMPP->iq_client_password);
338                         striplt(XMPP->iq_client_password);
339                 }
340         }
341
342         else if (!strcasecmp(el, "iq")) {
343
344                 /*
345                  * iq type="get" (handle queries)
346                  */
347                 if (!strcasecmp(XMPP->iq_type, "get")) {
348
349                         /*
350                          * Query on a namespace
351                          */
352                         if (!IsEmptyStr(XMPP->iq_query_xmlns)) {
353                                 xmpp_query_namespace(XMPP->iq_id, XMPP->iq_from, XMPP->iq_to, XMPP->iq_query_xmlns);
354                         }
355
356                         /*
357                          * ping ( http://xmpp.org/extensions/xep-0199.html )
358                          */
359                         else if (XMPP->ping_requested) {
360                                 cprintf("<iq type=\"result\" ");
361                                 if (!IsEmptyStr(XMPP->iq_from)) {
362                                         cprintf("to=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_from, sizeof xmlbuf));
363                                 }
364                                 if (!IsEmptyStr(XMPP->iq_to)) {
365                                         cprintf("from=\"%s\" ", xmlesc(xmlbuf, XMPP->iq_to, sizeof xmlbuf));
366                                 }
367                                 cprintf("id=\"%s\"/>", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
368                         }
369
370                         /*
371                          * Unknown query ... return the XML equivalent of a blank stare
372                          */
373                         else {
374                                 syslog(LOG_DEBUG, "xmpp: Unknown query <%s> - returning <service-unavailable/>", el);
375                                 cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
376                                 cprintf("<error code=\"503\" type=\"cancel\">"
377                                         "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
378                                         "</error>"
379                                 );
380                                 cprintf("</iq>");
381                         }
382                 }
383
384                 /*
385                  * Non SASL authentication
386                  */
387                 else if (
388                         (!strcasecmp(XMPP->iq_type, "set"))
389                         && (!strcasecmp(XMPP->iq_query_xmlns, "jabber:iq:auth:query"))
390                         ) {
391
392                         xmpp_non_sasl_authenticate(
393                                 XMPP->iq_id,
394                                 XMPP->iq_client_username,
395                                 XMPP->iq_client_password
396                         );
397                 }       
398
399                 /*
400                  * If this <iq> stanza was a "bind" attempt, process it ...
401                  */
402                 else if (
403                         (XMPP->bind_requested)
404                         && (!IsEmptyStr(XMPP->iq_id))
405                         && (CC->logged_in)
406                 ) {
407
408                         /* If the client has not specified a client resource, generate one */
409
410                         if (IsEmptyStr(XMPP->iq_client_resource)) {
411                                 snprintf(XMPP->iq_client_resource, sizeof XMPP->iq_client_resource, "%d", CC->cs_pid);
412                         }
413
414                         /* Generate the "full JID" of the client resource */
415                         snprintf(XMPP->client_jid, sizeof XMPP->client_jid, "%s/%s", CC->cs_inet_email, XMPP->iq_client_resource);
416
417                         /* Tell the client what its JID is */
418
419                         cprintf("<iq type=\"result\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
420                         cprintf("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
421                         cprintf("<jid>%s</jid>", xmlesc(xmlbuf, XMPP->client_jid, sizeof xmlbuf));
422                         cprintf("</bind>");
423                         cprintf("</iq>");
424                 }
425
426                 else if (XMPP->iq_session) {
427                         cprintf("<iq type=\"result\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
428                         cprintf("</iq>");
429                 }
430
431                 else {
432                         cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, XMPP->iq_id, sizeof xmlbuf));
433                         cprintf("<error>Don't know how to do '%s'!</error>", xmlesc(xmlbuf, XMPP->iq_type, sizeof xmlbuf));
434                         cprintf("</iq>");
435                         syslog(LOG_DEBUG, "XMPP: don't know how to do iq_type='%s' with iq_query_xmlns='%s'", XMPP->iq_type, XMPP->iq_query_xmlns);
436                 }
437
438                 /* Now clear these fields out so they don't get used by a future stanza */
439                 XMPP->iq_id[0] = 0;
440                 XMPP->iq_from[0] = 0;
441                 XMPP->iq_to[0] = 0;
442                 XMPP->iq_type[0] = 0;
443                 XMPP->iq_client_resource[0] = 0;
444                 XMPP->iq_session = 0;
445                 XMPP->iq_query_xmlns[0] = 0;
446                 XMPP->bind_requested = 0;
447                 XMPP->ping_requested = 0;
448         }
449
450         else if (!strcasecmp(el, "auth")) {
451
452                 /* Try to authenticate (this function is responsible for the output stanza) */
453                 xmpp_sasl_auth(XMPP->sasl_auth_mech, (XMPP->chardata != NULL ? XMPP->chardata : "") );
454
455                 /* Now clear these fields out so they don't get used by a future stanza */
456                 XMPP->sasl_auth_mech[0] = 0;
457         }
458
459         else if (!strcasecmp(el, "session")) {
460                 XMPP->iq_session = 1;
461         }
462
463         else if (!strcasecmp(el, "presence")) {
464
465                 /* Respond to a <presence> update by firing back with presence information
466                  * on the entire wholist.  Check this assumption, it's probably wrong.
467                  */
468                 xmpp_wholist_presence_dump();
469         }
470
471         else if ( (!strcasecmp(el, "body")) && (XMPP->html_tag_level == 0) ) {
472                 if (XMPP->message_body != NULL) {
473                         free(XMPP->message_body);
474                         XMPP->message_body = NULL;
475                 }
476                 if (XMPP->chardata_len > 0) {
477                         XMPP->message_body = strdup(XMPP->chardata);
478                 }
479         }
480
481         else if (!strcasecmp(el, "message")) {
482                 xmpp_send_message(XMPP->message_to, XMPP->message_body);
483                 XMPP->html_tag_level = 0;
484         }
485
486         else if (!strcasecmp(el, "html")) {
487                 --XMPP->html_tag_level;
488         }
489
490         else if (!strcasecmp(el, "starttls")) {
491 #ifdef HAVE_OPENSSL
492                 cprintf("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
493                 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
494                 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO;
495 #else
496                 cprintf("<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
497                 CC->kill_me = KILLME_NO_CRYPTO;
498 #endif
499         }
500
501         else if (!strcasecmp(el, "ping")) {
502                 XMPP->ping_requested = 1;
503         }
504
505         else if (!strcasecmp(el, "stream")) {
506                 syslog(LOG_DEBUG, "xmpp: client shut down their stream");
507                 xmpp_massacre_roster();
508                 cprintf("</stream>\n");
509                 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
510         }
511
512         else if (!strcasecmp(el, "query")) {
513                 /* already processed , no further action needed here */
514         }
515
516         else if (!strcasecmp(el, "bind")) {
517                 /* already processed , no further action needed here */
518         }
519
520         else {
521                 syslog(LOG_DEBUG, "xmpp: ignoring unknown tag <%s>", el);
522         }
523
524         XMPP->chardata_len = 0;
525         if (XMPP->chardata_alloc > 0) {
526                 XMPP->chardata[0] = 0;
527         }
528 }
529
530
531 void xmpp_xml_chardata(void *data, const XML_Char *s, int len)
532 {
533         citxmpp *X = XMPP;
534
535         if (X->chardata_alloc == 0) {
536                 X->chardata_alloc = SIZ;
537                 X->chardata = malloc(X->chardata_alloc);
538         }
539         if ((X->chardata_len + len + 1) > X->chardata_alloc) {
540                 X->chardata_alloc = X->chardata_len + len + 1024;
541                 X->chardata = realloc(X->chardata, X->chardata_alloc);
542         }
543         memcpy(&X->chardata[X->chardata_len], s, len);
544         X->chardata_len += len;
545         X->chardata[X->chardata_len] = 0;
546 }
547
548
549 /*
550  * This cleanup function blows away the temporary memory and files used by the XMPP service.
551  */
552 void xmpp_cleanup_function(void) {
553
554         /* Don't do this stuff if this is not a XMPP session! */
555         if (CC->h_command_function != xmpp_command_loop) return;
556
557         if (XMPP->chardata != NULL) {
558                 free(XMPP->chardata);
559                 XMPP->chardata = NULL;
560                 XMPP->chardata_len = 0;
561                 XMPP->chardata_alloc = 0;
562                 if (XMPP->message_body != NULL) {
563                         free(XMPP->message_body);
564                 }
565         }
566         XML_ParserFree(XMPP->xp);
567         free(XMPP);
568 }
569
570
571 /*
572  * Here's where our XMPP session begins its happy day.
573  */
574 void xmpp_greeting(void) {
575         client_set_inbound_buf(4);
576         strcpy(CC->cs_clientname, "XMPP session");
577         CC->session_specific_data = malloc(sizeof(citxmpp));
578         memset(XMPP, 0, sizeof(citxmpp));
579         XMPP->last_event_processed = queue_event_seq;
580
581         /* XMPP does not use a greeting, but we still have to initialize some things. */
582
583         XMPP->xp = XML_ParserCreateNS("UTF-8", ':');
584         if (XMPP->xp == NULL) {
585                 syslog(LOG_ERR, "xmpp: cannot create XML parser");
586                 CC->kill_me = KILLME_XML_PARSER;
587                 return;
588         }
589
590         XML_SetElementHandler(XMPP->xp, xmpp_xml_start, xmpp_xml_end);
591         XML_SetCharacterDataHandler(XMPP->xp, xmpp_xml_chardata);
592         // XML_SetUserData(XMPP->xp, something...);
593
594         /* Prevent the "billion laughs" attack against expat by disabling
595          * internal entity expansion.  With 2.x, forcibly stop the parser
596          * if an entity is declared - this is safer and a more obvious
597          * failure mode.  With older versions, simply prevent expansion
598          * of such entities. */
599 #ifdef HAVE_XML_STOPPARSER
600         XML_SetEntityDeclHandler(XMPP->xp, xmpp_entity_declaration);
601 #else
602         XML_SetDefaultHandler(XMPP->xp, NULL);
603 #endif
604
605         CC->can_receive_im = 1;         /* This protocol is capable of receiving instant messages */
606 }
607
608
609 /* 
610  * Main command loop for XMPP sessions.
611  */
612 void xmpp_command_loop(void) {
613         int rc;
614         StrBuf *stream_input = NewStrBuf();
615
616         time(&CC->lastcmd);
617         rc = client_read_random_blob(stream_input, 30);
618         if (rc > 0) {
619                 XML_Parse(XMPP->xp, ChrPtr(stream_input), rc, 0);
620         }
621         else {
622                 syslog(LOG_ERR, "xmpp: client disconnected: ending session.");
623                 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
624         }
625         FreeStrBuf(&stream_input);
626 }
627
628
629 /*
630  * Async loop for XMPP sessions (handles the transmission of unsolicited stanzas)
631  */
632 void xmpp_async_loop(void) {
633         xmpp_process_events();
634         xmpp_output_incoming_messages();
635 }
636
637
638 /*
639  * Login hook for XMPP sessions
640  */
641 void xmpp_login_hook(void) {
642         xmpp_queue_event(XMPP_EVT_LOGIN, CC->cs_inet_email);
643 }
644
645
646 /*
647  * Logout hook for XMPP sessions
648  */
649 void xmpp_logout_hook(void) {
650         xmpp_queue_event(XMPP_EVT_LOGOUT, CC->cs_inet_email);
651 }
652
653
654 const char *CitadelServiceXMPP="XMPP";
655 extern void xmpp_cleanup_events(void);
656 CTDL_MODULE_INIT(xmpp)
657 {
658         if (!threading) {
659                 CtdlRegisterServiceHook(CtdlGetConfigInt("c_xmpp_c2s_port"),
660                                         NULL,
661                                         xmpp_greeting,
662                                         xmpp_command_loop,
663                                         xmpp_async_loop,
664                                         CitadelServiceXMPP
665                 );
666                 CtdlRegisterSessionHook(xmpp_cleanup_function, EVT_STOP, PRIO_STOP + 70);
667                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_LOGIN, PRIO_LOGIN + 90);
668                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 90);
669                 CtdlRegisterSessionHook(xmpp_login_hook, EVT_UNSTEALTH, PRIO_UNSTEALTH + 1);
670                 CtdlRegisterSessionHook(xmpp_logout_hook, EVT_STEALTH, PRIO_STEALTH + 1);
671                 CtdlRegisterCleanupHook(xmpp_cleanup_events);
672
673         }
674
675         /* return our module name for the log */
676         return "xmpp";
677 }