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-2009 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.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
50 #include <libcitadel.h>
53 #include "citserver.h"
57 #include "internet_addressing.h"
59 #include "ctdl_module.h"
60 #include "serv_xmpp.h"
64 * PLAIN authentication. Returns zero on success, nonzero on failure.
66 int xmpp_auth_plain(char *authstring)
68 char decoded_authstring[1024];
76 /* Take apart the authentication string */
77 memset(pass, 0, sizeof(pass));
79 CtdlDecodeBase64(decoded_authstring, authstring, strlen(authstring));
80 safestrncpy(ident, decoded_authstring, sizeof ident);
81 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
82 len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
86 /* If there are underscores in either string, change them to spaces. Some clients
87 * do not allow spaces so we can tell the user to substitute underscores if their
88 * login name contains spaces.
90 convert_spaces_to_underscores(ident);
91 convert_spaces_to_underscores(user);
93 /* Now attempt authentication */
95 if (!IsEmptyStr(ident)) {
96 result = CtdlLoginExistingUser(user, ident);
99 result = CtdlLoginExistingUser(NULL, user);
102 if (result == login_ok) {
103 if (CtdlTryPassword(pass, len) == pass_ok) {
104 return(0); /* success */
108 return(1); /* failure */
113 * Output the list of SASL mechanisms offered by this stream.
115 void xmpp_output_auth_mechs(void) {
116 cprintf("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
117 cprintf("<mechanism>PLAIN</mechanism>");
118 cprintf("</mechanisms>");
122 * Here we go ... client is trying to authenticate.
124 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
126 if (strcasecmp(sasl_auth_mech, "PLAIN")) {
127 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
128 cprintf("<invalid-mechanism/>");
129 cprintf("</failure>");
133 if (CC->logged_in) CtdlUserLogout(); /* Client may try to log in twice. Handle this. */
136 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
137 cprintf("<system-shutdown/>");
138 cprintf("</failure>");
141 else if (xmpp_auth_plain(authstring) == 0) {
142 cprintf("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
146 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
147 cprintf("<not-authorized/>");
148 cprintf("</failure>");
155 * Non-SASL authentication
157 void xmpp_non_sasl_authenticate(char *iq_id, char *username, char *password, char *resource) {
161 if (CC->logged_in) CtdlUserLogout(); /* Client may try to log in twice. Handle this. */
163 result = CtdlLoginExistingUser(NULL, username);
164 if (result == login_ok) {
165 result = CtdlTryPassword(password, strlen(password));
166 if (result == pass_ok) {
167 cprintf("<iq type=\"result\" id=\"%s\"></iq>", xmlesc(xmlbuf, iq_id, sizeof xmlbuf)); /* success */
173 cprintf("<iq type=\"error\" id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
174 cprintf("<error code=\"401\" type=\"auth\">"
175 "<not-authorized xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"