libical, expat, and libsieve are now *required*.
[citadel.git] / citadel / modules / jabber / xmpp_sasl_service.c
1 /*
2  * $Id$ 
3  *
4  * Barebones SASL authentication service for XMPP (Jabber) clients.
5  *
6  * Note: RFC3920 says we "must" support DIGEST-MD5 but we only support PLAIN.
7  *
8  * Copyright (c) 2007 by Art Cancro
9  * This code is released under the terms of the GNU General Public License.
10  *
11  */
12
13 #include "sysdep.h"
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <sys/types.h>
22
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
25 # include <time.h>
26 #else
27 # if HAVE_SYS_TIME_H
28 #  include <sys/time.h>
29 # else
30 #  include <time.h>
31 # endif
32 #endif
33
34 #include <sys/wait.h>
35 #include <string.h>
36 #include <limits.h>
37 #include <ctype.h>
38 #include <expat.h>
39 #include <libcitadel.h>
40 #include "citadel.h"
41 #include "server.h"
42 #include "citserver.h"
43 #include "support.h"
44 #include "config.h"
45 #include "user_ops.h"
46 #include "internet_addressing.h"
47 #include "md5.h"
48 #include "ctdl_module.h"
49 #include "serv_xmpp.h"
50
51
52 /*
53  * PLAIN authentication.  Returns zero on success, nonzero on failure.
54  */
55 int xmpp_auth_plain(char *authstring)
56 {
57         char decoded_authstring[1024];
58         char ident[256];
59         char user[256];
60         char pass[256];
61         int result;
62         char *ptr = NULL;
63
64
65         /* Take apart the authentication string */
66
67         CtdlDecodeBase64(decoded_authstring, authstring, strlen(authstring));
68         safestrncpy(ident, decoded_authstring, sizeof ident);
69         safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
70         safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
71
72
73         /* If there are underscores in either string, change them to spaces.  Some clients
74          * do not allow spaces so we can tell the user to substitute underscores if their
75          * login name contains spaces.
76          */
77         convert_spaces_to_underscores(ident);
78         convert_spaces_to_underscores(user);
79
80         /* Now attempt authentication */
81
82         if (!IsEmptyStr(ident)) {
83                 result = CtdlLoginExistingUser(user, ident);
84         }
85         else {
86                 result = CtdlLoginExistingUser(NULL, user);
87         }
88
89         if (result == login_ok) {
90                 if (CtdlTryPassword(pass) == pass_ok) {
91                         return(0);                              /* success */
92                 }
93         }
94
95         return(1);                                              /* failure */
96 }
97
98
99 /*
100  * Output the list of SASL mechanisms offered by this stream.
101  */
102 void xmpp_output_auth_mechs(void) {
103         cprintf("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
104         cprintf("<mechanism>PLAIN</mechanism>");
105         cprintf("</mechanisms>");
106 }
107
108 /*
109  * Here we go ... client is trying to authenticate.
110  */
111 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
112
113         if (strcasecmp(sasl_auth_mech, "PLAIN")) {
114                 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
115                 cprintf("<invalid-mechanism/>");
116                 cprintf("</failure>");
117                 return;
118         }
119
120         if (CC->logged_in) logout(CC);  /* Client may try to log in twice.  Handle this. */
121
122         if (xmpp_auth_plain(authstring) == 0) {
123                 cprintf("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
124         }
125
126         else {
127                 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
128                 cprintf("<not-authorized/>");
129                 cprintf("</failure>");
130         }
131 }
132
133
134
135 /*
136  * Non-SASL authentication
137  */
138 void jabber_non_sasl_authenticate(char *iq_id, char *username, char *password, char *resource) {
139         int result;
140
141         if (CC->logged_in) logout(CC);  /* Client may try to log in twice.  Handle this. */
142
143         result = CtdlLoginExistingUser(NULL, username);
144         if (result == login_ok) {
145                 result = CtdlTryPassword(password);
146                 if (result == pass_ok) {
147                         cprintf("<iq type=\"result\" id=\"%s\"></iq>", iq_id);  /* success */
148                         return;
149                 }
150         }
151
152         /* failure */
153         cprintf("<iq type=\"error\" id=\"%s\">", iq_id);
154         cprintf("<error code=\"401\" type=\"auth\">"
155                 "<not-authorized xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
156                 "</error>"
157                 "</iq>"
158         );
159 }