ForEachUser() now uses a linked list
[citadel.git] / citadel / modules / xmpp / xmpp_sasl_service.c
1 /*
2  * Barebones SASL authentication service for XMPP (Jabber) clients.
3  *
4  * Note: RFC3920 says we "must" support DIGEST-MD5 but we only support PLAIN.
5  *
6  * Copyright (c) 2007-2019 by Art Cancro
7  *
8  * This program is open source software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 3.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include "sysdep.h"
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <sys/types.h>
26
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
29 # include <time.h>
30 #else
31 # if HAVE_SYS_TIME_H
32 #  include <sys/time.h>
33 # else
34 #  include <time.h>
35 # endif
36 #endif
37
38 #include <sys/wait.h>
39 #include <string.h>
40 #include <limits.h>
41 #include <ctype.h>
42 #include <expat.h>
43 #include <libcitadel.h>
44 #include "citadel.h"
45 #include "server.h"
46 #include "citserver.h"
47 #include "support.h"
48 #include "config.h"
49 #include "user_ops.h"
50 #include "internet_addressing.h"
51 #include "ctdl_module.h"
52 #include "serv_xmpp.h"
53
54
55 /*
56  * PLAIN authentication.  Returns zero on success, nonzero on failure.
57  */
58 int xmpp_auth_plain(char *authstring)
59 {
60         char decoded_authstring[1024];
61         char ident[256];
62         char user[256];
63         char pass[256];
64         int result;
65         long len;
66
67         /* Take apart the authentication string */
68         memset(pass, 0, sizeof(pass));
69
70         CtdlDecodeBase64(decoded_authstring, authstring, strlen(authstring));
71         safestrncpy(ident, decoded_authstring, sizeof ident);
72         safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
73         len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
74         if (len < 0) {
75                 len = -len;
76         }
77
78         if (!IsEmptyStr(ident)) {
79                 result = CtdlLoginExistingUser(ident);
80         }
81         else {
82                 result = CtdlLoginExistingUser(user);
83         }
84
85         if (result == login_ok) {
86                 if (CtdlTryPassword(pass, len) == pass_ok) {
87                         return(0);                              /* success */
88                 }
89         }
90
91         return(1);                                              /* failure */
92 }
93
94
95 /*
96  * Output the list of SASL mechanisms offered by this stream.
97  */
98 void xmpp_output_auth_mechs(void) {
99         cprintf("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
100         cprintf("<mechanism>PLAIN</mechanism>");
101         cprintf("</mechanisms>");
102 }
103
104
105 /*
106  * Here we go ... client is trying to authenticate.
107  */
108 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
109
110         if (strcasecmp(sasl_auth_mech, "PLAIN")) {
111                 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
112                 cprintf("<invalid-mechanism/>");
113                 cprintf("</failure>");
114                 return;
115         }
116
117         if (CC->logged_in) {
118                 CtdlUserLogout();  /* Client may try to log in twice.  Handle this. */
119         }
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  * Non-SASL authentication
141  */
142 void xmpp_non_sasl_authenticate(char *iq_id, char *username, char *password) {
143         int result;
144         char xmlbuf[256];
145
146         if (CC->logged_in) {
147                 CtdlUserLogout();  /* Client may try to log in twice.  Handle this. */
148         }
149
150         result = CtdlLoginExistingUser(username);
151         if (result == login_ok) {
152                 result = CtdlTryPassword(password, strlen(password));
153                 if (result == pass_ok) {
154                         cprintf("<iq type=\"result\" id=\"%s\"></iq>", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));   /* success */
155                         return;
156                 }
157         }
158
159         /* failure */
160         cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
161         cprintf("<error code=\"401\" type=\"auth\">"
162                 "<not-authorized xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
163                 "</error>"
164                 "</iq>"
165         );
166 }