4 * Barebones SASL authentication service for XMPP (Jabber) clients.
6 * Note: RFC3920 says we "must" support DIGEST-MD5 but we only support PLAIN.
8 * Copyright (c) 2007 by Art Cancro
9 * This code is released under the terms of the GNU General Public License.
21 #include <sys/types.h>
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
39 #include <libcitadel.h>
42 #include "citserver.h"
46 #include "internet_addressing.h"
48 #include "ctdl_module.h"
49 #include "serv_xmpp.h"
53 * PLAIN authentication. Returns zero on success, nonzero on failure.
55 int xmpp_auth_plain(char *authstring)
57 char decoded_authstring[1024];
64 /* Take apart the authentication string */
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);
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.
76 convert_spaces_to_underscores(ident);
77 convert_spaces_to_underscores(user);
79 /* Now attempt authentication */
81 if (!IsEmptyStr(ident)) {
82 result = CtdlLoginExistingUser(user, ident);
85 result = CtdlLoginExistingUser(NULL, user);
88 if (result == login_ok) {
89 if (CtdlTryPassword(pass) == pass_ok) {
90 return(0); /* success */
94 return(1); /* failure */
99 * Output the list of SASL mechanisms offered by this stream.
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>");
108 * Here we go ... client is trying to authenticate.
110 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
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>");
119 if (CC->logged_in) logout(); /* Client may try to log in twice. Handle this. */
122 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
123 cprintf("<system-shutdown/>");
124 cprintf("</failure>");
127 else if (xmpp_auth_plain(authstring) == 0) {
128 cprintf("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
132 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
133 cprintf("<not-authorized/>");
134 cprintf("</failure>");
141 * Non-SASL authentication
143 void jabber_non_sasl_authenticate(char *iq_id, char *username, char *password, char *resource) {
146 if (CC->logged_in) logout(); /* Client may try to log in twice. Handle this. */
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 */
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\"/>"