1
0
mirror of https://github.com/moparisthebest/hexchat synced 2024-11-27 11:42:15 -05:00
hexchat/plugins/sasl/sasl.c

316 lines
7.7 KiB
C
Raw Normal View History

2012-07-13 16:27:12 -04:00
/* HexChat
2012-01-10 00:15:03 -05:00
* Copyright (c) 2010 <ygrek@autistici.org>
* Copyright (c) 2012 Berke Viktor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2012-01-09 22:00:20 -05:00
/*
2012-10-30 06:47:12 -04:00
* SASL authentication plugin for HexChat
2012-01-09 22:00:20 -05:00
* Extremely primitive: only PLAIN, no error checking
*
* http://ygrek.org.ua/p/cap_sasl.html
*
* Docs:
* http://hg.atheme.org/charybdis/charybdis/file/6144f52a119b/doc/sasl.txt
* http://tools.ietf.org/html/rfc4422
*/
2012-07-17 12:37:29 -04:00
#include <stdio.h>
2012-01-09 22:00:20 -05:00
#include <string.h>
#include <stdlib.h>
#include <assert.h>
2012-07-17 12:37:29 -04:00
#include <glib.h>
2012-01-09 22:00:20 -05:00
2012-10-24 15:33:02 -04:00
#include "hexchat-plugin.h"
2012-01-09 22:00:20 -05:00
2012-10-30 03:42:48 -04:00
static hexchat_plugin *ph; /* plugin handle */
2012-10-26 07:38:13 -04:00
static char name[] = "SASL";
static char desc[] = "SASL authentication plugin for HexChat";
static char version[] = "1.2";
static const char sasl_help[] = "SASL Usage:\n /SASL ADD <login> <password> <network>, enable/update SASL authentication for given network\n /SASL DEL <network>, disable SASL authentication for given network\n /SASL LIST, get the list of SASL-enabled networks\n";
2012-01-09 22:00:20 -05:00
struct sasl_info
{
2012-01-10 00:15:03 -05:00
char const* login;
char const* password;
char const* network;
2012-01-09 22:00:20 -05:00
};
typedef struct sasl_info sasl_info;
static int
2012-01-10 00:15:03 -05:00
add_info (char const* login, char const* password, char const* network)
2012-01-09 22:00:20 -05:00
{
2012-01-10 00:15:03 -05:00
char buffer[512];
2012-01-09 22:00:20 -05:00
2012-01-10 00:15:03 -05:00
sprintf (buffer, "%s:%s", login, password);
2012-10-30 03:42:48 -04:00
return hexchat_pluginpref_set_str (ph, network, buffer);
}
static int
del_info (char const* network)
{
2012-10-30 03:42:48 -04:00
return hexchat_pluginpref_delete (ph, network);
2012-01-09 22:00:20 -05:00
}
2012-01-15 15:15:33 -05:00
static void
print_info ()
{
char list[512];
char* token;
2012-10-30 03:42:48 -04:00
if (hexchat_pluginpref_list (ph, list))
2012-01-15 15:15:33 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\tSASL-enabled networks:", name);
hexchat_printf (ph, "%s\t----------------------", name);
2012-01-15 15:15:33 -05:00
token = strtok (list, ",");
while (token != NULL)
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\t%s", name, token);
2012-01-15 15:15:33 -05:00
token = strtok (NULL, ",");
}
}
else
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\tThere are no SASL-enabled networks currently", name);
2012-01-15 15:15:33 -05:00
}
}
2012-01-10 00:15:03 -05:00
static sasl_info*
find_info (char const* network)
2012-01-09 22:00:20 -05:00
{
2012-01-10 00:15:03 -05:00
char buffer[512];
char* token;
sasl_info* cur = (sasl_info*) malloc (sizeof (sasl_info));
2012-10-30 03:42:48 -04:00
if (hexchat_pluginpref_get_str (ph, network, buffer))
2012-01-10 00:15:03 -05:00
{
token = strtok (buffer, ":");
cur->login = g_strdup (token);
token = strtok (NULL, ":");
cur->password = g_strdup (token);
cur->network = g_strdup (network);
return cur;
}
return NULL;
2012-01-09 22:00:20 -05:00
}
2012-01-10 00:15:03 -05:00
static sasl_info*
get_info (void)
2012-01-09 22:00:20 -05:00
{
2012-01-10 00:15:03 -05:00
const char* name;
2012-10-30 03:42:48 -04:00
name = hexchat_get_info (ph, "network");
2012-01-10 00:15:03 -05:00
if (name)
{
return find_info (name);
}
else
{
return NULL;
}
2012-01-09 22:00:20 -05:00
}
2012-01-10 00:15:03 -05:00
static int
authend_cb (char *word[], char *word_eol[], void *userdata)
2012-01-09 22:00:20 -05:00
{
2012-01-10 00:15:03 -05:00
if (get_info ())
{
2012-01-11 15:35:02 -05:00
/* omit cryptic server message parts */
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\t%s\n", name, ++word_eol[4]);
hexchat_commandf (ph, "QUOTE CAP END");
2012-01-10 00:15:03 -05:00
}
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
2012-01-09 22:00:20 -05:00
}
/*
2012-01-10 00:15:03 -05:00
static int
disconnect_cb (char *word[], void *userdata)
2012-01-09 22:00:20 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "disconnected\n");
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_NONE;
2012-01-09 22:00:20 -05:00
}
*/
2012-01-10 00:15:03 -05:00
static int
server_cb (char *word[], char *word_eol[], void *userdata)
2012-01-09 22:00:20 -05:00
{
2012-01-10 00:15:03 -05:00
size_t len;
char* buf;
char* enc;
sasl_info* p;
2012-01-09 22:00:20 -05:00
2012-01-10 00:15:03 -05:00
if (strcmp ("AUTHENTICATE", word[1]) == 0 && strcmp ("+", word[2]) == 0)
{
p = get_info ();
2012-01-09 22:00:20 -05:00
2012-01-10 00:15:03 -05:00
if (!p)
{
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_NONE;
2012-01-10 00:15:03 -05:00
}
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\tAuthenticating as %s\n", name, p->login);
2012-01-10 00:15:03 -05:00
len = strlen (p->login) * 2 + 2 + strlen (p->password);
buf = (char*) malloc (len + 1);
strcpy (buf, p->login);
strcpy (buf + strlen (p->login) + 1, p->login);
strcpy (buf + strlen (p->login) * 2 + 2, p->password);
enc = g_base64_encode ((unsigned char*) buf, len);
2012-10-30 03:42:48 -04:00
/* hexchat_printf (ph, "AUTHENTICATE %s\}", enc); */
hexchat_commandf (ph, "QUOTE AUTHENTICATE %s", enc);
2012-01-10 00:15:03 -05:00
free (enc);
free (buf);
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
2012-01-10 00:15:03 -05:00
}
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_NONE;
2012-01-09 22:00:20 -05:00
}
2012-01-10 00:15:03 -05:00
static int
cap_cb (char *word[], char *word_eol[], void *userdata)
2012-01-09 22:00:20 -05:00
{
2012-01-10 00:15:03 -05:00
if (get_info ())
{
/* FIXME test sasl cap */
2012-01-11 15:35:02 -05:00
/* this is visible in the rawlog in case someone needs it, otherwise it's just noise */
2012-10-30 03:42:48 -04:00
/* hexchat_printf (ph, "%s\t%s\n", name, word_eol[1]); */
hexchat_commandf (ph, "QUOTE AUTHENTICATE PLAIN");
2012-01-10 00:15:03 -05:00
}
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
2012-01-09 22:00:20 -05:00
}
2012-01-10 00:15:03 -05:00
static int
sasl_cmd_cb (char *word[], char *word_eol[], void *userdata)
2012-01-09 22:00:20 -05:00
{
const char* login;
const char* password;
const char* network;
const char* mode = word[2];
2012-01-09 22:00:20 -05:00
2012-07-17 12:37:29 -04:00
if (!g_ascii_strcasecmp ("ADD", mode))
2012-01-10 00:15:03 -05:00
{
login = word[3];
password = word[4];
network = word_eol[5];
if (!network || !*network) /* only check for the last word, if it's there, the previous ones will be there, too */
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s", sasl_help);
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
}
if (add_info (login, password, network))
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\tEnabled SASL authentication for the \"%s\" network\n", name, network);
}
else
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\tFailed to enable SASL authentication for the \"%s\" network\n", name, network);
}
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
2012-01-10 00:15:03 -05:00
}
2012-07-17 12:37:29 -04:00
else if (!g_ascii_strcasecmp ("DEL", mode))
{
network = word_eol[3];
2012-01-09 22:00:20 -05:00
if (!network || !*network)
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s", sasl_help);
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
}
2012-01-09 22:00:20 -05:00
if (del_info (network))
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\tDisabled SASL authentication for the \"%s\" network\n", name, network);
}
else
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\tFailed to disable SASL authentication for the \"%s\" network\n", name, network);
}
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
}
2012-07-17 12:37:29 -04:00
else if (!g_ascii_strcasecmp ("LIST", mode))
2012-01-15 15:15:33 -05:00
{
print_info ();
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
2012-01-15 15:15:33 -05:00
}
else
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s", sasl_help);
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_ALL;
}
2012-01-10 00:15:03 -05:00
}
2012-01-09 22:00:20 -05:00
2012-01-10 00:15:03 -05:00
static int
connect_cb (char *word[], void *userdata)
{
if (get_info ())
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s\tSASL enabled\n", name);
hexchat_commandf (ph, "QUOTE CAP REQ :sasl");
2012-01-10 00:15:03 -05:00
}
2012-01-09 22:00:20 -05:00
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_NONE;
2012-01-10 00:15:03 -05:00
}
2012-01-09 22:00:20 -05:00
2012-01-10 00:15:03 -05:00
int
2012-10-30 03:42:48 -04:00
hexchat_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
2012-01-10 00:15:03 -05:00
{
2012-10-30 06:47:12 -04:00
/* we need to save this for use with any hexchat_* functions */
2012-01-10 00:15:03 -05:00
ph = plugin_handle;
2012-10-30 06:47:12 -04:00
/* tell HexChat our info */
2012-01-10 00:15:03 -05:00
*plugin_name = name;
*plugin_desc = desc;
*plugin_version = version;
2012-10-30 03:42:48 -04:00
hexchat_hook_command (ph, "SASL", HEXCHAT_PRI_NORM, sasl_cmd_cb, sasl_help, 0);
hexchat_hook_print (ph, "Connected", HEXCHAT_PRI_NORM, connect_cb, NULL);
/* hexchat_hook_print (ph, "Disconnected", HEXCHAT_PRI_NORM, disconnect_cb, NULL); */
hexchat_hook_server (ph, "CAP", HEXCHAT_PRI_NORM, cap_cb, NULL);
hexchat_hook_server (ph, "RAW LINE", HEXCHAT_PRI_NORM, server_cb, NULL);
hexchat_hook_server (ph, "903", HEXCHAT_PRI_NORM, authend_cb, NULL);
hexchat_hook_server (ph, "904", HEXCHAT_PRI_NORM, authend_cb, NULL);
hexchat_hook_server (ph, "905", HEXCHAT_PRI_NORM, authend_cb, NULL);
hexchat_hook_server (ph, "906", HEXCHAT_PRI_NORM, authend_cb, NULL);
hexchat_hook_server (ph, "907", HEXCHAT_PRI_NORM, authend_cb, NULL);
2012-01-10 00:15:03 -05:00
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s plugin loaded\n", name);
2012-01-09 22:00:20 -05:00
2012-01-10 00:15:03 -05:00
return 1;
2012-01-09 22:00:20 -05:00
}
2012-01-10 00:15:03 -05:00
int
2012-10-30 03:42:48 -04:00
hexchat_plugin_deinit (void)
2012-01-09 22:00:20 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s plugin unloaded\n", name);
2012-01-09 22:00:20 -05:00
return 1;
}