6497f8101ca4db4aa405ce01437b2e39304856c6
[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
63
64         /* Take apart the authentication string */
65
66         CtdlDecodeBase64(decoded_authstring, authstring, strlen(authstring));
67         safestrncpy(ident, decoded_authstring, sizeof ident);
68         safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
69         safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
70
71
72         /* If there are underscores in either string, change them to spaces.  Some clients
73          * do not allow spaces so we can tell the user to substitute underscores if their
74          * login name contains spaces.
75          */
76         convert_spaces_to_underscores(ident);
77         convert_spaces_to_underscores(user);
78
79         /* Now attempt authentication */
80
81         if (!IsEmptyStr(ident)) {
82                 result = CtdlLoginExistingUser(user, ident);
83         }
84         else {
85                 result = CtdlLoginExistingUser(NULL, user);
86         }
87
88         if (result == login_ok) {
89                 if (CtdlTryPassword(pass) == pass_ok) {
90                         return(0);                              /* success */
91                 }
92         }
93
94         return(1);                                              /* failure */
95 }
96
97
98 /*
99  * Output the list of SASL mechanisms offered by this stream.
100  */
101 void xmpp_output_auth_mechs(void) {
102         cprintf("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
103         cprintf("<mechanism>PLAIN</mechanism>");
104         cprintf("</mechanisms>");
105 }
106
107 /*
108  * Here we go ... client is trying to authenticate.
109  */
110 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
111
112         if (strcasecmp(sasl_auth_mech, "PLAIN")) {
113                 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
114                 cprintf("<invalid-mechanism/>");
115                 cprintf("</failure>");
116                 return;
117         }
118
119         if (CC->logged_in) logout();  /* Client may try to log in twice.  Handle this. */
120
121         if (CC->nologin) {
122                 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
123                 cprintf("<system-shutdown/>");
124                 cprintf("</failure>");
125         }
126
127         else if (xmpp_auth_plain(authstring) == 0) {
128                 cprintf("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
129         }
130
131         else {
132                 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
133                 cprintf("<not-authorized/>");
134                 cprintf("</failure>");
135         }
136 }
137
138
139
140 /*
141  * Non-SASL authentication
142  */
143 void jabber_non_sasl_authenticate(char *iq_id, char *username, char *password, char *resource) {
144         int result;
145
146         if (CC->logged_in) logout();  /* Client may try to log in twice.  Handle this. */
147
148         result = CtdlLoginExistingUser(NULL, username);
149         if (result == login_ok) {
150                 result = CtdlTryPassword(password);
151                 if (result == pass_ok) {
152                         cprintf("<iq type=\"result\" id=\"%s\"></iq>", iq_id);  /* success */
153                         return;
154                 }
155         }
156
157         /* failure */
158         cprintf("<iq type=\"error\" id=\"%s\">", iq_id);
159         cprintf("<error code=\"401\" type=\"auth\">"
160                 "<not-authorized xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
161                 "</error>"
162                 "</iq>"
163         );
164 }