2 * Barebones SASL authentication service for XMPP (Jabber) clients.
4 * Note: RFC3920 says we "must" support DIGEST-MD5 but we only support PLAIN.
6 * Copyright (c) 2007-2019 by Art Cancro
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.
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.
25 #include <sys/types.h>
32 #include <libcitadel.h>
35 #include "citserver.h"
39 #include "internet_addressing.h"
40 #include "ctdl_module.h"
41 #include "serv_xmpp.h"
45 * PLAIN authentication. Returns zero on success, nonzero on failure.
47 int xmpp_auth_plain(char *authstring)
49 char decoded_authstring[1024];
56 /* Take apart the authentication string */
57 memset(pass, 0, sizeof(pass));
59 CtdlDecodeBase64(decoded_authstring, authstring, strlen(authstring));
60 safestrncpy(ident, decoded_authstring, sizeof ident);
61 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
62 len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
67 if (!IsEmptyStr(ident)) {
68 result = CtdlLoginExistingUser(ident);
71 result = CtdlLoginExistingUser(user);
74 if (result == login_ok) {
75 if (CtdlTryPassword(pass, len) == pass_ok) {
76 return(0); /* success */
80 return(1); /* failure */
85 * Output the list of SASL mechanisms offered by this stream.
87 void xmpp_output_auth_mechs(void) {
88 cprintf("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
89 cprintf("<mechanism>PLAIN</mechanism>");
90 cprintf("</mechanisms>");
95 * Here we go ... client is trying to authenticate.
97 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
99 if (strcasecmp(sasl_auth_mech, "PLAIN")) {
100 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
101 cprintf("<invalid-mechanism/>");
102 cprintf("</failure>");
107 CtdlUserLogout(); /* Client may try to log in twice. Handle this. */
111 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
112 cprintf("<system-shutdown/>");
113 cprintf("</failure>");
116 else if (xmpp_auth_plain(authstring) == 0) {
117 cprintf("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
121 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
122 cprintf("<not-authorized/>");
123 cprintf("</failure>");
129 * Non-SASL authentication
131 void xmpp_non_sasl_authenticate(char *iq_id, char *username, char *password) {
136 CtdlUserLogout(); /* Client may try to log in twice. Handle this. */
139 result = CtdlLoginExistingUser(username);
140 if (result == login_ok) {
141 result = CtdlTryPassword(password, strlen(password));
142 if (result == pass_ok) {
143 cprintf("<iq type=\"result\" id=\"%s\"></iq>", xmlesc(xmlbuf, iq_id, sizeof xmlbuf)); /* success */
149 cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
150 cprintf("<error code=\"401\" type=\"auth\">"
151 "<not-authorized xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"