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