2f2dc419a33cd292c8cf7781b4fe7d39cf331328
[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 <libcitadel.h>
39 #include "citadel.h"
40 #include "server.h"
41 #include "citserver.h"
42 #include "support.h"
43 #include "config.h"
44 #include "user_ops.h"
45 #include "internet_addressing.h"
46 #include "md5.h"
47 #include "ctdl_module.h"
48
49 #ifdef HAVE_EXPAT
50 #include <expat.h>
51 #include "serv_xmpp.h"
52
53
54 /*
55  * PLAIN authentication.  Returns zero on success, nonzero on failure.
56  */
57 int xmpp_auth_plain(char *authstring)
58 {
59         char decoded_authstring[1024];
60         char ident[256];
61         char user[256];
62         char pass[256];
63         int result;
64
65         CtdlDecodeBase64(decoded_authstring, authstring, strlen(authstring));
66         safestrncpy(ident, decoded_authstring, sizeof ident);
67         safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
68         safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
69
70         if (!IsEmptyStr(ident)) {
71                 result = CtdlLoginExistingUser(user, ident);
72         }
73         else {
74                 result = CtdlLoginExistingUser(NULL, user);
75         }
76
77         if (result == login_ok) {
78                 if (CtdlTryPassword(pass) == pass_ok) {
79                         return(0);                              /* success */
80                 }
81         }
82
83         return(1);                                              /* failure */
84 }
85
86
87 /*
88  * Output the list of SASL mechanisms offered by this stream.
89  */
90 void xmpp_output_auth_mechs(void) {
91         cprintf("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
92         cprintf("<mechanism>PLAIN</mechanism>");
93         cprintf("</mechanisms>");
94 }
95
96 /*
97  * Here we go ... client is trying to authenticate.
98  */
99 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
100
101         if (strcasecmp(sasl_auth_mech, "PLAIN")) {
102                 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
103                 cprintf("<invalid-mechanism/>");
104                 cprintf("</failure>");
105                 return;
106         }
107
108         if (CC->logged_in) logout(CC);  /* Client may try to log in twice.  Handle this. */
109
110         if (xmpp_auth_plain(authstring) == 0) {
111                 cprintf("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
112         }
113
114         else {
115                 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
116                 cprintf("<not-authorized/>");
117                 cprintf("</failure>");
118         }
119 }
120
121
122
123 /*
124  * Non-SASL authentication
125  */
126 void jabber_non_sasl_authenticate(char *iq_id, char *username, char *password, char *resource) {
127         int result;
128
129         if (CC->logged_in) logout(CC);  /* Client may try to log in twice.  Handle this. */
130
131         result = CtdlLoginExistingUser(NULL, username);
132         if (result == login_ok) {
133                 result = CtdlTryPassword(password);
134                 if (result == pass_ok) {
135                         cprintf("<iq type=\"result\" id=\"%s\"></iq>", iq_id);  /* success */
136                         return;
137                 }
138         }
139
140         /* failure */
141         cprintf("<iq type=\"error\" id=\"%s\">", iq_id);
142         cprintf("<error code=\"401\" type=\"auth\">"
143                 "<not-authorized xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
144                 "</error>"
145                 "</iq>"
146         );
147 }
148
149
150
151 #endif  /* HAVE_EXPAT */