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];
65 CtdlDecodeBase64(decoded_authstring, authstring, strlen(authstring));
66 safestrncpy(ident, decoded_authstring, sizeof ident);
67 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
68 safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
70 if (!IsEmptyStr(ident)) {
71 result = CtdlLoginExistingUser(user, ident);
74 result = CtdlLoginExistingUser(NULL, user);
77 if (result == login_ok) {
78 if (CtdlTryPassword(pass) == pass_ok) {
79 return(0); /* success */
83 return(1); /* failure */
88 * Output the list of SASL mechanisms offered by this stream.
90 void xmpp_output_auth_mechs(void) {
91 cprintf("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
92 cprintf("<mechanism>PLAIN</mechanism>");
93 cprintf("</mechanisms>");
97 * Here we go ... client is trying to authenticate.
99 void xmpp_sasl_auth(char *sasl_auth_mech, char *authstring) {
101 if (strcasecmp(sasl_auth_mech, "PLAIN")) {
102 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
103 cprintf("<invalid-mechanism/>");
104 cprintf("</failure>");
108 if (CC->logged_in) logout(CC); /* Client may try to log in twice. Handle this. */
110 if (xmpp_auth_plain(authstring) == 0) {
111 cprintf("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>");
115 cprintf("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
116 cprintf("<not-authorized/>");
117 cprintf("</failure>");
121 #endif /* HAVE_EXPAT */