2015-09-20 17:13:43 -04:00
|
|
|
/*
|
|
|
|
fe-sasl.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 2015 The Lemon Man
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2015-09-09 19:02:44 -04:00
|
|
|
#include "module.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include "irc-cap.h"
|
|
|
|
#include "irc-servers.h"
|
|
|
|
#include "sasl.h"
|
|
|
|
|
|
|
|
#define SASL_TIMEOUT (20 * 1000) // ms
|
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
static gboolean sasl_timeout(IRC_SERVER_REC *server)
|
2015-09-09 19:02:44 -04:00
|
|
|
{
|
|
|
|
/* The authentication timed out, we can't do much beside terminating it */
|
|
|
|
irc_send_cmd_now(server, "AUTHENTICATE *");
|
|
|
|
cap_finish_negotiation(server);
|
|
|
|
|
2016-03-20 16:47:02 -04:00
|
|
|
server->sasl_timeout = 0;
|
2015-09-10 18:58:01 -04:00
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
signal_emit("server sasl failure", 2, server, "The authentication timed out");
|
|
|
|
|
2015-09-10 18:58:01 -04:00
|
|
|
return FALSE;
|
2015-09-09 19:02:44 -04:00
|
|
|
}
|
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
static void sasl_start(IRC_SERVER_REC *server, const char *data, const char *from)
|
2015-09-09 19:02:44 -04:00
|
|
|
{
|
|
|
|
IRC_SERVER_CONNECT_REC *conn;
|
|
|
|
|
|
|
|
conn = server->connrec;
|
|
|
|
|
|
|
|
switch (conn->sasl_mechanism) {
|
|
|
|
case SASL_MECHANISM_PLAIN:
|
|
|
|
irc_send_cmd_now(server, "AUTHENTICATE PLAIN");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SASL_MECHANISM_EXTERNAL:
|
|
|
|
irc_send_cmd_now(server, "AUTHENTICATE EXTERNAL");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
server->sasl_timeout = g_timeout_add(SASL_TIMEOUT, (GSourceFunc) sasl_timeout, server);
|
|
|
|
}
|
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
static void sasl_fail(IRC_SERVER_REC *server, const char *data, const char *from)
|
2015-09-09 19:02:44 -04:00
|
|
|
{
|
2015-09-11 17:20:07 -04:00
|
|
|
char *params, *error;
|
|
|
|
|
2015-09-09 19:02:44 -04:00
|
|
|
/* Stop any pending timeout, if any */
|
2016-03-20 16:47:02 -04:00
|
|
|
if (server->sasl_timeout != 0) {
|
2015-09-10 19:17:18 -04:00
|
|
|
g_source_remove(server->sasl_timeout);
|
2016-03-20 16:47:02 -04:00
|
|
|
server->sasl_timeout = 0;
|
2015-09-10 19:17:18 -04:00
|
|
|
}
|
2015-09-09 19:02:44 -04:00
|
|
|
|
2015-09-11 17:20:07 -04:00
|
|
|
params = event_get_params(data, 2, NULL, &error);
|
|
|
|
|
2016-06-01 16:29:13 -04:00
|
|
|
signal_emit("server sasl failure", 2, server, error);
|
2015-09-09 19:02:44 -04:00
|
|
|
|
|
|
|
/* Terminate the negotiation */
|
|
|
|
cap_finish_negotiation(server);
|
2015-09-11 17:20:07 -04:00
|
|
|
|
|
|
|
g_free(params);
|
2015-09-09 19:02:44 -04:00
|
|
|
}
|
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
static void sasl_already(IRC_SERVER_REC *server, const char *data, const char *from)
|
2015-09-09 19:02:44 -04:00
|
|
|
{
|
2016-03-20 16:47:02 -04:00
|
|
|
if (server->sasl_timeout != 0) {
|
2015-09-10 19:17:18 -04:00
|
|
|
g_source_remove(server->sasl_timeout);
|
2016-03-20 16:47:02 -04:00
|
|
|
server->sasl_timeout = 0;
|
2015-09-10 19:17:18 -04:00
|
|
|
}
|
2015-09-09 19:02:44 -04:00
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
signal_emit("server sasl success", 1, server);
|
|
|
|
|
2015-09-09 19:02:44 -04:00
|
|
|
/* We're already authenticated, do nothing */
|
|
|
|
cap_finish_negotiation(server);
|
|
|
|
}
|
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
static void sasl_success(IRC_SERVER_REC *server, const char *data, const char *from)
|
2015-09-09 19:02:44 -04:00
|
|
|
{
|
2016-03-20 16:47:02 -04:00
|
|
|
if (server->sasl_timeout != 0) {
|
2015-09-10 19:17:18 -04:00
|
|
|
g_source_remove(server->sasl_timeout);
|
2016-03-20 16:47:02 -04:00
|
|
|
server->sasl_timeout = 0;
|
2015-09-10 19:17:18 -04:00
|
|
|
}
|
2015-09-09 19:02:44 -04:00
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
signal_emit("server sasl success", 1, server);
|
|
|
|
|
2015-09-09 19:02:44 -04:00
|
|
|
/* The authentication succeeded, time to finish the CAP negotiation */
|
|
|
|
cap_finish_negotiation(server);
|
|
|
|
}
|
|
|
|
|
2015-09-20 17:13:43 -04:00
|
|
|
static void sasl_step(IRC_SERVER_REC *server, const char *data, const char *from)
|
2015-09-09 19:02:44 -04:00
|
|
|
{
|
|
|
|
IRC_SERVER_CONNECT_REC *conn;
|
|
|
|
GString *req;
|
|
|
|
char *enc_req;
|
|
|
|
|
|
|
|
conn = server->connrec;
|
|
|
|
|
|
|
|
/* Stop the timer */
|
2016-03-20 16:47:02 -04:00
|
|
|
if (server->sasl_timeout != 0) {
|
2015-09-10 19:17:18 -04:00
|
|
|
g_source_remove(server->sasl_timeout);
|
2016-03-20 16:47:02 -04:00
|
|
|
server->sasl_timeout = 0;
|
2015-09-10 19:17:18 -04:00
|
|
|
}
|
2015-09-09 19:02:44 -04:00
|
|
|
|
|
|
|
switch (conn->sasl_mechanism) {
|
|
|
|
case SASL_MECHANISM_PLAIN:
|
2015-09-11 16:09:47 -04:00
|
|
|
/* At this point we assume that conn->sasl_{username, password} are non-NULL.
|
2015-09-09 19:02:44 -04:00
|
|
|
* The PLAIN mechanism expects a NULL-separated string composed by the authorization identity, the
|
|
|
|
* authentication identity and the password.
|
2015-09-11 16:09:47 -04:00
|
|
|
* The authorization identity field is explicitly set to the user provided username.
|
2015-09-09 19:02:44 -04:00
|
|
|
* The whole request is then encoded in base64. */
|
|
|
|
|
|
|
|
req = g_string_new(NULL);
|
|
|
|
|
2015-09-11 16:09:47 -04:00
|
|
|
g_string_append(req, conn->sasl_username);
|
2015-09-09 19:02:44 -04:00
|
|
|
g_string_append_c(req, '\0');
|
|
|
|
g_string_append(req, conn->sasl_username);
|
|
|
|
g_string_append_c(req, '\0');
|
|
|
|
g_string_append(req, conn->sasl_password);
|
|
|
|
|
|
|
|
enc_req = g_base64_encode((const guchar *)req->str, req->len);
|
|
|
|
|
|
|
|
irc_send_cmdv(server, "AUTHENTICATE %s", enc_req);
|
|
|
|
|
|
|
|
g_free(enc_req);
|
|
|
|
g_string_free(req, TRUE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SASL_MECHANISM_EXTERNAL:
|
|
|
|
/* Empty response */
|
2016-03-08 16:00:20 -05:00
|
|
|
irc_send_cmdv(server, "AUTHENTICATE +");
|
2015-09-09 19:02:44 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We expect a response within a reasonable time */
|
|
|
|
server->sasl_timeout = g_timeout_add(SASL_TIMEOUT, (GSourceFunc) sasl_timeout, server);
|
|
|
|
}
|
|
|
|
|
2016-03-11 12:55:31 -05:00
|
|
|
static void sasl_disconnected(IRC_SERVER_REC *server)
|
|
|
|
{
|
|
|
|
g_return_if_fail(server != NULL);
|
|
|
|
|
|
|
|
if (!IS_IRC_SERVER(server)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 16:47:02 -04:00
|
|
|
if (server->sasl_timeout != 0) {
|
2016-03-11 12:55:31 -05:00
|
|
|
g_source_remove(server->sasl_timeout);
|
2016-03-20 16:47:02 -04:00
|
|
|
server->sasl_timeout = 0;
|
2016-03-11 12:55:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:02:44 -04:00
|
|
|
void sasl_init(void)
|
|
|
|
{
|
|
|
|
signal_add_first("server cap ack sasl", (SIGNAL_FUNC) sasl_start);
|
|
|
|
signal_add_first("event authenticate", (SIGNAL_FUNC) sasl_step);
|
2015-09-11 17:17:46 -04:00
|
|
|
signal_add_first("event 903", (SIGNAL_FUNC) sasl_success);
|
2015-09-09 19:02:44 -04:00
|
|
|
signal_add_first("event 902", (SIGNAL_FUNC) sasl_fail);
|
|
|
|
signal_add_first("event 904", (SIGNAL_FUNC) sasl_fail);
|
|
|
|
signal_add_first("event 905", (SIGNAL_FUNC) sasl_fail);
|
2015-09-11 16:23:38 -04:00
|
|
|
signal_add_first("event 906", (SIGNAL_FUNC) sasl_fail);
|
2015-09-09 19:02:44 -04:00
|
|
|
signal_add_first("event 907", (SIGNAL_FUNC) sasl_already);
|
2016-03-11 12:55:31 -05:00
|
|
|
signal_add_first("server disconnected", (SIGNAL_FUNC) sasl_disconnected);
|
2015-09-09 19:02:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void sasl_deinit(void)
|
|
|
|
{
|
|
|
|
signal_remove("server cap ack sasl", (SIGNAL_FUNC) sasl_start);
|
|
|
|
signal_remove("event authenticate", (SIGNAL_FUNC) sasl_step);
|
2015-09-11 17:17:46 -04:00
|
|
|
signal_remove("event 903", (SIGNAL_FUNC) sasl_success);
|
2015-09-09 19:02:44 -04:00
|
|
|
signal_remove("event 902", (SIGNAL_FUNC) sasl_fail);
|
|
|
|
signal_remove("event 904", (SIGNAL_FUNC) sasl_fail);
|
|
|
|
signal_remove("event 905", (SIGNAL_FUNC) sasl_fail);
|
2015-09-11 16:23:38 -04:00
|
|
|
signal_remove("event 906", (SIGNAL_FUNC) sasl_fail);
|
2015-09-09 19:02:44 -04:00
|
|
|
signal_remove("event 907", (SIGNAL_FUNC) sasl_already);
|
2016-03-11 12:55:31 -05:00
|
|
|
signal_remove("server disconnected", (SIGNAL_FUNC) sasl_disconnected);
|
2015-09-09 19:02:44 -04:00
|
|
|
}
|