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>
38 #include <libcitadel.h>
41 #include "citserver.h"
45 #include "internet_addressing.h"
47 #include "ctdl_module.h"
51 #include "serv_xmpp.h"
55 * PLAIN authentication. Returns zero on success, nonzero on failure.
57 int xmpp_auth_plain(char *authstring)
59 char decoded_authstring[1024];
67 /* Take apart the authentication string */
69 CtdlDecodeBase64(decoded_authstring, authstring, strlen(authstring));
70 safestrncpy(ident, decoded_authstring, sizeof ident);
71 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
72 safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
75 /* If there are underscores in either string, change them to spaces. Some clients
76 * do not allow spaces so we can tell the user to substitute underscores if their
77 * login name contains spaces.
79 while (ptr=strstr(ident, "_")) {
83 while (ptr=strstr(user, "_")) {
87 /* Now attempt authentication */
89 if (!IsEmptyStr(ident)) {
90 result = CtdlLoginExistingUser(user, ident);
93 result = CtdlLoginExistingUser(NULL, user);
96 if (result == login_ok) {
97 if (CtdlTryPassword(pass) == pass_ok) {
98 return(0); /* success */
102 return(1); /* failure */
107 * Output the list of SASL mechanisms offered by this stream.
109 void xmpp_output_auth_mechs(void) {
110 cprintf("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
111 cprintf("<mechanism>PLAIN</mechanism>");
112 cprintf("</mechanisms>");
116 * Here we go ... client is trying to authenticate.
118 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
120 if (strcasecmp(sasl_auth_mech, "PLAIN")) {
121 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
122 cprintf("<invalid-mechanism/>");
123 cprintf("</failure>");
127 if (CC->logged_in) logout(CC); /* Client may try to log in twice. Handle this. */
129 if (xmpp_auth_plain(authstring) == 0) {
130 cprintf("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
134 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
135 cprintf("<not-authorized/>");
136 cprintf("</failure>");
143 * Non-SASL authentication
145 void jabber_non_sasl_authenticate(char *iq_id, char *username, char *password, char *resource) {
148 if (CC->logged_in) logout(CC); /* Client may try to log in twice. Handle this. */
150 result = CtdlLoginExistingUser(NULL, username);
151 if (result == login_ok) {
152 result = CtdlTryPassword(password);
153 if (result == pass_ok) {
154 cprintf("<iq type=\"result\" id=\"%s\"></iq>", iq_id); /* success */
160 cprintf("<iq type=\"error\" id=\"%s\">", iq_id);
161 cprintf("<error code=\"401\" type=\"auth\">"
162 "<not-authorized xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
170 #endif /* HAVE_EXPAT */