2014-07-09 01:54:50 +02:00
|
|
|
/*
|
|
|
|
* kike.c: the experimental IRC daemon
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014, Přemysl Janouch <p.janouch@gmail.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PROGRAM_NAME "kike"
|
|
|
|
#define PROGRAM_VERSION "alpha"
|
|
|
|
|
|
|
|
#include "common.c"
|
2014-07-13 23:47:29 +02:00
|
|
|
#include <nl_types.h>
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
// --- Configuration (application-specific) ------------------------------------
|
|
|
|
|
|
|
|
static struct config_item g_config_table[] =
|
|
|
|
{
|
|
|
|
{ "server_name", NULL, "Server name" },
|
2014-07-13 21:27:38 +02:00
|
|
|
{ "motd", NULL, "MOTD filename" },
|
2014-07-13 23:47:29 +02:00
|
|
|
{ "catalog", NULL, "catgets localization catalog" },
|
2014-07-13 21:27:38 +02:00
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
{ "bind_host", NULL, "Address of the IRC server" },
|
|
|
|
{ "bind_port", "6667", "Port of the IRC server" },
|
|
|
|
{ "ssl_cert", NULL, "Server SSL certificate (PEM)" },
|
|
|
|
{ "ssl_key", NULL, "Server SSL private key (PEM)" },
|
|
|
|
|
|
|
|
{ "max_connections", NULL, "Maximum client connections" },
|
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
// --- Signals -----------------------------------------------------------------
|
|
|
|
|
|
|
|
static int g_signal_pipe[2]; ///< A pipe used to signal... signals
|
|
|
|
|
|
|
|
/// Program termination has been requested by a signal
|
|
|
|
static volatile sig_atomic_t g_termination_requested;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sigterm_handler (int signum)
|
|
|
|
{
|
|
|
|
(void) signum;
|
|
|
|
|
|
|
|
g_termination_requested = true;
|
|
|
|
|
|
|
|
int original_errno = errno;
|
|
|
|
if (write (g_signal_pipe[1], "t", 1) == -1)
|
|
|
|
soft_assert (errno == EAGAIN);
|
|
|
|
errno = original_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_signal_handlers (void)
|
|
|
|
{
|
|
|
|
if (pipe (g_signal_pipe) == -1)
|
2014-07-16 23:23:03 +02:00
|
|
|
exit_fatal ("%s: %s", "pipe", strerror (errno));
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
set_cloexec (g_signal_pipe[0]);
|
|
|
|
set_cloexec (g_signal_pipe[1]);
|
|
|
|
|
|
|
|
// So that the pipe cannot overflow; it would make write() block within
|
|
|
|
// the signal handler, which is something we really don't want to happen.
|
|
|
|
// The same holds true for read().
|
|
|
|
set_blocking (g_signal_pipe[0], false);
|
|
|
|
set_blocking (g_signal_pipe[1], false);
|
|
|
|
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sigemptyset (&sa.sa_mask);
|
|
|
|
sa.sa_handler = sigterm_handler;
|
|
|
|
if (sigaction (SIGINT, &sa, NULL) == -1
|
|
|
|
|| sigaction (SIGTERM, &sa, NULL) == -1)
|
2014-07-16 23:23:03 +02:00
|
|
|
exit_fatal ("%s: %s", "sigaction", strerror (errno));
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-13 03:26:33 +02:00
|
|
|
// --- IRC token validation ----------------------------------------------------
|
|
|
|
|
2014-07-14 00:10:59 +02:00
|
|
|
// Use the enum only if applicable and a simple boolean isn't sufficient.
|
|
|
|
|
2014-07-13 03:26:33 +02:00
|
|
|
enum validation_result
|
|
|
|
{
|
|
|
|
VALIDATION_OK,
|
|
|
|
VALIDATION_ERROR_EMPTY,
|
|
|
|
VALIDATION_ERROR_TOO_LONG,
|
|
|
|
VALIDATION_ERROR_INVALID
|
|
|
|
};
|
|
|
|
|
|
|
|
// Everything as per RFC 2812
|
2014-07-14 02:35:38 +02:00
|
|
|
#define IRC_MAX_NICKNAME 9
|
|
|
|
#define IRC_MAX_HOSTNAME 63
|
|
|
|
#define IRC_MAX_MESSAGE_LENGTH 510
|
2014-07-13 03:26:33 +02:00
|
|
|
|
2014-07-13 04:30:23 +02:00
|
|
|
static bool
|
|
|
|
irc_regex_match (const char *regex, const char *s)
|
|
|
|
{
|
|
|
|
static struct str_map cache;
|
|
|
|
static bool initialized;
|
2014-07-13 03:26:33 +02:00
|
|
|
|
2014-07-13 04:30:23 +02:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
regex_cache_init (&cache);
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct error *e = NULL;
|
|
|
|
bool result = regex_cache_match (&cache, regex,
|
|
|
|
REG_EXTENDED | REG_NOSUB, s, &e);
|
|
|
|
hard_assert (!e);
|
|
|
|
return result;
|
|
|
|
}
|
2014-07-13 03:26:33 +02:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
irc_validate_to_str (enum validation_result result)
|
|
|
|
{
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case VALIDATION_OK: return "success";
|
|
|
|
case VALIDATION_ERROR_EMPTY: return "the value is empty";
|
|
|
|
case VALIDATION_ERROR_INVALID: return "invalid format";
|
|
|
|
case VALIDATION_ERROR_TOO_LONG: return "the value is too long";
|
|
|
|
default: abort ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 04:30:23 +02:00
|
|
|
// Anything to keep it as short as possible
|
|
|
|
#define SN "[0-9A-Za-z][-0-9A-Za-z]*[0-9A-Za-z]*"
|
|
|
|
#define N4 "[0-9]{1,3}"
|
|
|
|
#define N6 "[0-9ABCDEFabcdef]{1,}"
|
|
|
|
|
|
|
|
#define LE "A-Za-z"
|
2014-07-15 23:28:00 +02:00
|
|
|
#define SP "][\\\\`_^{|}"
|
2014-07-13 03:26:33 +02:00
|
|
|
|
|
|
|
static enum validation_result
|
|
|
|
irc_validate_hostname (const char *hostname)
|
|
|
|
{
|
|
|
|
if (!*hostname)
|
|
|
|
return VALIDATION_ERROR_EMPTY;
|
2014-07-13 04:30:23 +02:00
|
|
|
if (!irc_regex_match ("^" SN "(\\." SN ")*$", hostname))
|
2014-07-13 03:26:33 +02:00
|
|
|
return VALIDATION_ERROR_INVALID;
|
2014-07-14 02:35:38 +02:00
|
|
|
if (strlen (hostname) > IRC_MAX_HOSTNAME)
|
2014-07-13 03:26:33 +02:00
|
|
|
return VALIDATION_ERROR_TOO_LONG;
|
|
|
|
return VALIDATION_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_is_valid_hostaddr (const char *hostaddr)
|
|
|
|
{
|
2014-07-13 04:30:23 +02:00
|
|
|
if (irc_regex_match ("^" N4 "\\." N4 "\\." N4 "\\." N4 "$", hostaddr)
|
|
|
|
|| irc_regex_match ("^" N6 ":" N6 ":" N6 ":" N6 ":"
|
|
|
|
N6 ":" N6 ":" N6 ":" N6 "$", hostaddr)
|
|
|
|
|| irc_regex_match ("^0:0:0:0:0:(0|[Ff]{4}):"
|
|
|
|
N4 "\\." N4 "\\." N4 "\\." N4 "$", hostaddr))
|
2014-07-13 03:26:33 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_is_valid_host (const char *host)
|
|
|
|
{
|
|
|
|
return irc_validate_hostname (host) == VALIDATION_OK
|
|
|
|
|| irc_is_valid_hostaddr (host);
|
|
|
|
}
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
static bool
|
|
|
|
irc_is_valid_user (const char *user)
|
|
|
|
{
|
|
|
|
return irc_regex_match ("^[^\r\n @]+$", user);
|
|
|
|
}
|
|
|
|
|
2014-07-13 03:26:33 +02:00
|
|
|
static bool
|
|
|
|
irc_validate_nickname (const char *nickname)
|
|
|
|
{
|
|
|
|
if (!*nickname)
|
|
|
|
return VALIDATION_ERROR_EMPTY;
|
2014-07-15 23:28:00 +02:00
|
|
|
if (!irc_regex_match ("^[" SP LE "][" SP LE "0-9-]*$", nickname))
|
2014-07-13 03:26:33 +02:00
|
|
|
return VALIDATION_ERROR_INVALID;
|
2014-07-14 02:35:38 +02:00
|
|
|
if (strlen (nickname) > IRC_MAX_NICKNAME)
|
2014-07-13 03:26:33 +02:00
|
|
|
return VALIDATION_ERROR_TOO_LONG;
|
|
|
|
return VALIDATION_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SN
|
|
|
|
#undef N4
|
|
|
|
#undef N6
|
|
|
|
|
|
|
|
#undef LE
|
|
|
|
#undef SP
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
// --- Application data --------------------------------------------------------
|
|
|
|
|
2014-07-14 00:30:46 +02:00
|
|
|
#define IRC_SUPPORTED_USER_MODES "aiwros"
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
IRC_USER_MODE_INVISIBLE = (1 << 0),
|
|
|
|
IRC_USER_MODE_RX_WALLOPS = (1 << 1),
|
|
|
|
IRC_USER_MODE_RESTRICTED = (1 << 2),
|
|
|
|
IRC_USER_MODE_OPERATOR = (1 << 3),
|
|
|
|
IRC_USER_MODE_RX_SERVER_NOTICES = (1 << 4)
|
|
|
|
};
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
struct client
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-18 19:51:15 +02:00
|
|
|
LIST_HEADER (client)
|
2014-07-09 01:54:50 +02:00
|
|
|
struct server_context *ctx; ///< Server context
|
|
|
|
|
|
|
|
int socket_fd; ///< The TCP socket
|
|
|
|
struct str read_buffer; ///< Unprocessed input
|
|
|
|
struct str write_buffer; ///< Output yet to be sent out
|
|
|
|
|
|
|
|
unsigned initialized : 1; ///< Has any data been received yet?
|
2014-07-14 02:35:38 +02:00
|
|
|
unsigned registered : 1; ///< The user has registered
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
unsigned ssl_rx_want_tx : 1; ///< SSL_read() wants to write
|
|
|
|
unsigned ssl_tx_want_rx : 1; ///< SSL_write() wants to read
|
|
|
|
SSL *ssl; ///< SSL connection
|
|
|
|
|
|
|
|
char *nickname; ///< IRC nickname (main identifier)
|
|
|
|
char *username; ///< IRC username
|
2014-07-13 05:38:05 +02:00
|
|
|
char *realname; ///< IRC realname (e-mail)
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
char *hostname; ///< Hostname shown to the network
|
|
|
|
|
|
|
|
unsigned mode; ///< User's mode
|
|
|
|
char *away_message; ///< Away message
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
client_init (struct client *self)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
|
|
|
|
self->socket_fd = -1;
|
|
|
|
str_init (&self->read_buffer);
|
|
|
|
str_init (&self->write_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
client_free (struct client *self)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
|
|
|
if (!soft_assert (self->socket_fd == -1))
|
|
|
|
xclose (self->socket_fd);
|
|
|
|
if (self->ssl)
|
|
|
|
SSL_free (self->ssl);
|
|
|
|
|
|
|
|
str_free (&self->read_buffer);
|
|
|
|
str_free (&self->write_buffer);
|
|
|
|
|
|
|
|
free (self->nickname);
|
|
|
|
free (self->username);
|
2014-07-13 05:38:05 +02:00
|
|
|
free (self->realname);
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
free (self->hostname);
|
|
|
|
free (self->away_message);
|
|
|
|
}
|
|
|
|
|
2014-07-17 22:54:16 +02:00
|
|
|
static char *
|
|
|
|
client_get_mode (struct client *self)
|
|
|
|
{
|
|
|
|
struct str mode;
|
|
|
|
str_init (&mode);
|
|
|
|
|
|
|
|
if (self->away_message) str_append_c (&mode, 'a');
|
|
|
|
|
|
|
|
unsigned m = self->mode;
|
|
|
|
if (m & IRC_USER_MODE_INVISIBLE) str_append_c (&mode, 'i');
|
|
|
|
if (m & IRC_USER_MODE_RX_WALLOPS) str_append_c (&mode, 'w');
|
|
|
|
if (m & IRC_USER_MODE_RESTRICTED) str_append_c (&mode, 'r');
|
|
|
|
if (m & IRC_USER_MODE_OPERATOR) str_append_c (&mode, 'o');
|
|
|
|
if (m & IRC_USER_MODE_RX_SERVER_NOTICES) str_append_c (&mode, 's');
|
|
|
|
|
|
|
|
if (mode.len)
|
|
|
|
return str_steal (&mode);
|
|
|
|
|
|
|
|
str_free (&mode);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-14 00:30:46 +02:00
|
|
|
#define IRC_SUPPORTED_CHAN_MODES "ov" "imnqpst" "kl"
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
IRC_CHAN_MODE_INVITE_ONLY = (1 << 0),
|
|
|
|
IRC_CHAN_MODE_MODERATED = (1 << 1),
|
|
|
|
IRC_CHAN_MODE_NO_OUTSIDE_MSGS = (1 << 2),
|
2014-07-14 00:30:46 +02:00
|
|
|
IRC_CHAN_MODE_QUIET = (1 << 3),
|
2014-07-09 01:54:50 +02:00
|
|
|
IRC_CHAN_MODE_PRIVATE = (1 << 4),
|
2014-07-14 00:30:46 +02:00
|
|
|
IRC_CHAN_MODE_SECRET = (1 << 5),
|
|
|
|
IRC_CHAN_MODE_PROTECTED_TOPIC = (1 << 6),
|
|
|
|
|
|
|
|
IRC_CHAN_MODE_OPERATOR = (1 << 7),
|
|
|
|
IRC_CHAN_MODE_VOICE = (1 << 8)
|
|
|
|
};
|
|
|
|
|
|
|
|
struct channel_user
|
|
|
|
{
|
2014-07-18 19:51:15 +02:00
|
|
|
LIST_HEADER (channel_user)
|
2014-07-14 00:30:46 +02:00
|
|
|
|
|
|
|
unsigned modes;
|
|
|
|
char nickname[];
|
2014-07-09 01:54:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct channel
|
|
|
|
{
|
|
|
|
struct server_context *ctx; ///< Server context
|
|
|
|
|
|
|
|
char *name; ///< Channel name
|
|
|
|
unsigned modes; ///< Channel modes
|
|
|
|
char *key; ///< Channel key
|
|
|
|
long user_limit; ///< User limit or -1
|
|
|
|
|
2014-07-14 00:30:46 +02:00
|
|
|
struct channel_user *users; ///< Channel users
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
struct str_vector ban_list; ///< Ban list
|
|
|
|
struct str_vector exception_list; ///< Exceptions from bans
|
|
|
|
struct str_vector invite_list; ///< Exceptions from +I
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
channel_init (struct channel *self)
|
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
|
|
|
|
str_vector_init (&self->ban_list);
|
|
|
|
str_vector_init (&self->exception_list);
|
|
|
|
str_vector_init (&self->invite_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
channel_free (struct channel *self)
|
|
|
|
{
|
|
|
|
free (self->name);
|
|
|
|
free (self->key);
|
|
|
|
|
2014-07-14 00:30:46 +02:00
|
|
|
struct channel_user *link, *tmp;
|
|
|
|
for (link = self->users; link; link = tmp)
|
|
|
|
{
|
|
|
|
tmp = link->next;
|
|
|
|
free (link);
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
str_vector_free (&self->ban_list);
|
|
|
|
str_vector_free (&self->exception_list);
|
|
|
|
str_vector_free (&self->invite_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct server_context
|
|
|
|
{
|
|
|
|
struct str_map config; ///< Server configuration
|
|
|
|
|
|
|
|
int listen_fd; ///< Listening socket FD
|
2014-07-14 20:54:47 +02:00
|
|
|
struct client *clients; ///< Clients
|
2014-07-12 21:59:17 +02:00
|
|
|
SSL_CTX *ssl_ctx; ///< SSL context
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-13 04:59:22 +02:00
|
|
|
char *server_name; ///< Our server name
|
2014-07-14 20:54:47 +02:00
|
|
|
struct str_map users; ///< Maps nicknames to clients
|
2014-07-09 01:54:50 +02:00
|
|
|
struct str_map channels; ///< Maps channel names to data
|
2014-07-14 02:35:38 +02:00
|
|
|
struct str_map handlers; ///< Message handlers
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
struct poller poller; ///< Manages polled description
|
2014-07-12 21:59:17 +02:00
|
|
|
bool quitting; ///< User requested quitting
|
2014-07-09 01:54:50 +02:00
|
|
|
bool polling; ///< The event loop is running
|
2014-07-13 21:27:38 +02:00
|
|
|
|
|
|
|
struct str_vector motd; ///< MOTD (none if empty)
|
2014-07-13 23:47:29 +02:00
|
|
|
nl_catd catalog; ///< Message catalog for server msgs
|
2014-07-09 01:54:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_context_init (struct server_context *self)
|
|
|
|
{
|
|
|
|
str_map_init (&self->config);
|
|
|
|
self->config.free = free;
|
|
|
|
load_config_defaults (&self->config, g_config_table);
|
|
|
|
|
|
|
|
self->listen_fd = -1;
|
|
|
|
self->clients = NULL;
|
|
|
|
|
2014-07-13 04:59:22 +02:00
|
|
|
self->server_name = NULL;
|
2014-07-09 01:54:50 +02:00
|
|
|
str_map_init (&self->users);
|
2014-07-16 21:23:30 +02:00
|
|
|
self->users.key_xfrm = irc_strxfrm;
|
2014-07-09 01:54:50 +02:00
|
|
|
// TODO: set channel_free() as the free function?
|
|
|
|
str_map_init (&self->channels);
|
2014-07-16 21:23:30 +02:00
|
|
|
self->channels.key_xfrm = irc_strxfrm;
|
2014-07-14 02:35:38 +02:00
|
|
|
str_map_init (&self->handlers);
|
2014-07-16 21:23:30 +02:00
|
|
|
self->handlers.key_xfrm = irc_strxfrm;
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
poller_init (&self->poller);
|
2014-07-12 21:59:17 +02:00
|
|
|
self->quitting = false;
|
2014-07-09 01:54:50 +02:00
|
|
|
self->polling = false;
|
2014-07-13 21:27:38 +02:00
|
|
|
|
|
|
|
str_vector_init (&self->motd);
|
2014-07-13 23:47:29 +02:00
|
|
|
self->catalog = (nl_catd) -1;
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_context_free (struct server_context *self)
|
|
|
|
{
|
|
|
|
str_map_free (&self->config);
|
|
|
|
|
|
|
|
if (self->listen_fd != -1)
|
|
|
|
xclose (self->listen_fd);
|
2014-07-12 21:59:17 +02:00
|
|
|
if (self->ssl_ctx)
|
|
|
|
SSL_CTX_free (self->ssl_ctx);
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
// TODO: terminate the connections properly before this is called
|
2014-07-14 20:54:47 +02:00
|
|
|
struct client *link, *tmp;
|
2014-07-09 01:54:50 +02:00
|
|
|
for (link = self->clients; link; link = tmp)
|
|
|
|
{
|
|
|
|
tmp = link->next;
|
2014-07-14 20:54:47 +02:00
|
|
|
client_free (link);
|
2014-07-09 01:54:50 +02:00
|
|
|
free (link);
|
|
|
|
}
|
|
|
|
|
2014-07-13 04:59:22 +02:00
|
|
|
free (self->server_name);
|
2014-07-09 01:54:50 +02:00
|
|
|
str_map_free (&self->users);
|
|
|
|
str_map_free (&self->channels);
|
2014-07-14 02:35:38 +02:00
|
|
|
str_map_free (&self->handlers);
|
2014-07-09 01:54:50 +02:00
|
|
|
poller_free (&self->poller);
|
2014-07-13 21:27:38 +02:00
|
|
|
|
|
|
|
str_vector_free (&self->motd);
|
2014-07-13 23:47:29 +02:00
|
|
|
if (self->catalog != (nl_catd) -1)
|
|
|
|
catclose (self->catalog);
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Main program ------------------------------------------------------------
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (struct client *c, const char *reason)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 02:35:38 +02:00
|
|
|
// TODO: multicast a QUIT message with `reason' || "Client exited"
|
|
|
|
(void) reason;
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
// TODO: do further cleanup if the client has successfully registered etc.
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
struct server_context *ctx = c->ctx;
|
|
|
|
ssize_t i = poller_find_by_fd (&ctx->poller, c->socket_fd);
|
2014-07-14 02:35:38 +02:00
|
|
|
if (i != -1)
|
|
|
|
poller_remove_at_index (&ctx->poller, i);
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
xclose (c->socket_fd);
|
|
|
|
c->socket_fd = -1;
|
|
|
|
client_free (c);
|
|
|
|
LIST_UNLINK (ctx->clients, c);
|
|
|
|
free (c);
|
2014-07-14 02:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_str (struct client *c, const struct str *s)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
|
|
|
// TODO: kill the connection above some "SendQ" threshold (careful!)
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
str_append_data (&c->write_buffer, s->str,
|
2014-07-14 02:35:38 +02:00
|
|
|
s->len > IRC_MAX_MESSAGE_LENGTH ? IRC_MAX_MESSAGE_LENGTH : s->len);
|
2014-07-14 20:54:47 +02:00
|
|
|
str_append (&c->write_buffer, "\r\n");
|
2014-07-14 02:35:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
static void irc_send (struct client *c,
|
2014-07-14 02:35:38 +02:00
|
|
|
const char *format, ...) ATTRIBUTE_PRINTF (2, 3);
|
|
|
|
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send (struct client *c, const char *format, ...)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
|
|
|
struct str tmp;
|
|
|
|
str_init (&tmp);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, format);
|
|
|
|
str_append_vprintf (&tmp, format, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_str (c, &tmp);
|
2014-07-14 02:35:38 +02:00
|
|
|
str_free (&tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
irc_get_text (struct server_context *ctx, int id, const char *def)
|
|
|
|
{
|
|
|
|
if (!soft_assert (def != NULL))
|
|
|
|
def = "";
|
|
|
|
if (ctx->catalog == (nl_catd) -1)
|
|
|
|
return def;
|
|
|
|
return catgets (ctx->catalog, 1, id, def);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- IRC command handling ----------------------------------------------------
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
IRC_RPL_WELCOME = 1,
|
|
|
|
IRC_RPL_YOURHOST = 2,
|
|
|
|
IRC_RPL_CREATED = 3,
|
|
|
|
IRC_RPL_MYINFO = 4,
|
|
|
|
|
2014-07-17 23:16:49 +02:00
|
|
|
IRC_RPL_LUSERCLIENT = 251,
|
|
|
|
IRC_RPL_LUSEROP = 252,
|
|
|
|
IRC_RPL_LUSERUNKNOWN = 253,
|
|
|
|
IRC_RPL_LUSERCHANNELS = 254,
|
|
|
|
IRC_RPL_LUSERME = 255,
|
|
|
|
|
2014-07-17 23:51:34 +02:00
|
|
|
IRC_RPL_VERSION = 351,
|
2014-07-14 02:35:38 +02:00
|
|
|
IRC_RPL_MOTD = 372,
|
|
|
|
IRC_RPL_MOTDSTART = 375,
|
|
|
|
IRC_RPL_ENDOFMOTD = 376,
|
2014-07-17 23:51:34 +02:00
|
|
|
IRC_RPL_TIME = 391,
|
2014-07-14 02:35:38 +02:00
|
|
|
|
2014-07-17 23:51:34 +02:00
|
|
|
IRC_ERR_NOSUCHSERVER = 402,
|
2014-07-14 02:35:38 +02:00
|
|
|
IRC_ERR_NOORIGIN = 409,
|
|
|
|
IRC_ERR_UNKNOWNCOMMAND = 421,
|
|
|
|
IRC_ERR_NOMOTD = 422,
|
|
|
|
IRC_ERR_NONICKNAMEGIVEN = 431,
|
|
|
|
IRC_ERR_ERRONEOUSNICKNAME = 432,
|
|
|
|
IRC_ERR_NICKNAMEINUSE = 433,
|
|
|
|
IRC_ERR_NOTREGISTERED = 451,
|
|
|
|
IRC_ERR_NEEDMOREPARAMS = 461,
|
|
|
|
IRC_ERR_ALREADYREGISTERED = 462
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *g_default_replies[] =
|
|
|
|
{
|
|
|
|
[IRC_RPL_WELCOME] = ":Welcome to the Internet Relay Network %s!%s@%s",
|
|
|
|
[IRC_RPL_YOURHOST] = ":Your host is %s, running version %s",
|
|
|
|
[IRC_RPL_CREATED] = ":This server was created %s",
|
|
|
|
[IRC_RPL_MYINFO] = "%s %s %s %s",
|
|
|
|
|
2014-07-17 23:16:49 +02:00
|
|
|
[IRC_RPL_LUSERCLIENT] = ":There are %d users and %d services on %d servers",
|
|
|
|
[IRC_RPL_LUSEROP] = "%d :operator(s) online",
|
|
|
|
[IRC_RPL_LUSERUNKNOWN] = "%d :unknown connection(s)",
|
|
|
|
[IRC_RPL_LUSERCHANNELS] = "%d :channels formed",
|
|
|
|
[IRC_RPL_LUSERME] = ":I have %d clients and %d servers",
|
|
|
|
|
2014-07-17 23:51:34 +02:00
|
|
|
[IRC_RPL_VERSION] = "%s.%d %s :%s",
|
2014-07-14 02:35:38 +02:00
|
|
|
[IRC_RPL_MOTD] = ":- %s",
|
|
|
|
[IRC_RPL_MOTDSTART] = ":- %s Message of the day - ",
|
|
|
|
[IRC_RPL_ENDOFMOTD] = ":End of MOTD command",
|
2014-07-17 23:51:34 +02:00
|
|
|
[IRC_RPL_TIME] = "%s :%s",
|
2014-07-14 02:35:38 +02:00
|
|
|
|
2014-07-17 23:51:34 +02:00
|
|
|
[IRC_ERR_NOSUCHSERVER] = "%s :No such server",
|
2014-07-14 02:35:38 +02:00
|
|
|
[IRC_ERR_NOORIGIN] = ":No origin specified",
|
|
|
|
[IRC_ERR_UNKNOWNCOMMAND] = "%s: Unknown command",
|
|
|
|
[IRC_ERR_NOMOTD] = ":MOTD File is missing",
|
|
|
|
[IRC_ERR_NONICKNAMEGIVEN] = ":No nickname given",
|
|
|
|
[IRC_ERR_ERRONEOUSNICKNAME] = "%s :Erroneous nickname",
|
|
|
|
[IRC_ERR_NICKNAMEINUSE] = "%s :Nickname is already in use",
|
2014-07-17 23:17:20 +02:00
|
|
|
[IRC_ERR_NOTREGISTERED] = ":You have not registered",
|
2014-07-14 02:35:38 +02:00
|
|
|
[IRC_ERR_NEEDMOREPARAMS] = "%s :Not enough parameters",
|
|
|
|
[IRC_ERR_ALREADYREGISTERED] = ":Unauthorized command (already registered)",
|
|
|
|
};
|
|
|
|
|
|
|
|
// XXX: this way we cannot typecheck the arguments, so we must be careful
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (struct client *c, int id, ...)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
|
|
|
struct str tmp;
|
|
|
|
str_init (&tmp);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, id);
|
|
|
|
str_append_printf (&tmp, ":%s %03d %s ",
|
2014-07-14 20:54:47 +02:00
|
|
|
c->ctx->server_name, id, c->nickname ? c->nickname : "");
|
2014-07-14 02:35:38 +02:00
|
|
|
str_append_vprintf (&tmp,
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_get_text (c->ctx, id, g_default_replies[id]), ap);
|
2014-07-14 02:35:38 +02:00
|
|
|
va_end (ap);
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_str (c, &tmp);
|
2014-07-14 02:35:38 +02:00
|
|
|
str_free (&tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_motd (struct client *c)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
struct server_context *ctx = c->ctx;
|
2014-07-14 02:35:38 +02:00
|
|
|
if (!ctx->motd.len)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_NOMOTD);
|
2014-07-14 02:35:38 +02:00
|
|
|
return;
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
2014-07-14 02:35:38 +02:00
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_RPL_MOTDSTART, ctx->server_name);
|
2014-07-14 02:35:38 +02:00
|
|
|
for (size_t i = 0; i < ctx->motd.len; i++)
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_RPL_MOTD, ctx->motd.vector[i]);
|
|
|
|
irc_send_reply (c, IRC_RPL_ENDOFMOTD);
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
static void
|
2014-07-17 23:16:49 +02:00
|
|
|
irc_send_lusers (struct client *c)
|
|
|
|
{
|
|
|
|
int n_users = 0, n_services = 0, n_opers = 0, n_unknown = 0;
|
|
|
|
for (struct client *link = c->ctx->clients; link; link = link->next)
|
|
|
|
{
|
|
|
|
if (link->registered)
|
|
|
|
n_users++;
|
|
|
|
else
|
|
|
|
n_unknown++;
|
|
|
|
if (link->mode & IRC_USER_MODE_OPERATOR)
|
|
|
|
n_opers++;
|
|
|
|
}
|
|
|
|
|
2014-07-18 01:03:21 +02:00
|
|
|
int n_channels = c->ctx->channels.len;
|
2014-07-17 23:16:49 +02:00
|
|
|
|
|
|
|
irc_send_reply (c, IRC_RPL_LUSERCLIENT,
|
|
|
|
n_users, n_services, 1 /* servers total */);
|
|
|
|
if (n_opers)
|
|
|
|
irc_send_reply (c, IRC_RPL_LUSEROP, n_opers);
|
|
|
|
if (n_unknown)
|
|
|
|
irc_send_reply (c, IRC_RPL_LUSERUNKNOWN, n_unknown);
|
|
|
|
if (n_channels)
|
|
|
|
irc_send_reply (c, IRC_RPL_LUSERCHANNELS, n_channels);
|
|
|
|
irc_send_reply (c, IRC_RPL_LUSERME,
|
|
|
|
n_users + n_services + n_unknown, 0 /* peer servers */);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-07-17 23:51:34 +02:00
|
|
|
irc_is_this_me (struct server_context *ctx, const char *target)
|
|
|
|
{
|
|
|
|
return !fnmatch (target, ctx->server_name, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_try_finish_registration (struct client *c)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-17 22:54:16 +02:00
|
|
|
struct server_context *ctx = c->ctx;
|
2014-07-14 20:54:47 +02:00
|
|
|
if (!c->nickname || !c->username || !c->realname)
|
2014-07-14 02:35:38 +02:00
|
|
|
return;
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
c->registered = true;
|
|
|
|
irc_send_reply (c, IRC_RPL_WELCOME, c->nickname, c->username, c->hostname);
|
2014-07-14 02:35:38 +02:00
|
|
|
|
2014-07-17 22:54:16 +02:00
|
|
|
irc_send_reply (c, IRC_RPL_YOURHOST, ctx->server_name, PROGRAM_VERSION);
|
2014-07-14 02:35:38 +02:00
|
|
|
// The purpose of this message eludes me
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_RPL_CREATED, __DATE__);
|
2014-07-17 22:54:16 +02:00
|
|
|
irc_send_reply (c, IRC_RPL_MYINFO, ctx->server_name, PROGRAM_VERSION,
|
2014-07-14 02:35:38 +02:00
|
|
|
IRC_SUPPORTED_USER_MODES, IRC_SUPPORTED_CHAN_MODES);
|
|
|
|
|
2014-07-17 23:16:49 +02:00
|
|
|
irc_send_lusers (c);
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_motd (c);
|
2014-07-17 22:54:16 +02:00
|
|
|
|
|
|
|
char *mode = client_get_mode (c);
|
|
|
|
if (mode)
|
|
|
|
irc_send (c, ":%s MODE %s :+%s", c->nickname, c->nickname, mode);
|
|
|
|
free (mode);
|
2014-07-14 02:35:38 +02:00
|
|
|
}
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_handle_pass (const struct irc_message *msg, struct client *c)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->registered)
|
|
|
|
irc_send_reply (c, IRC_ERR_ALREADYREGISTERED);
|
2014-07-14 02:35:38 +02:00
|
|
|
else if (msg->params.len < 1)
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_NEEDMOREPARAMS, msg->command);
|
2014-07-14 02:35:38 +02:00
|
|
|
|
|
|
|
// We have SSL client certificates for this purpose; ignoring
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_handle_nick (const struct irc_message *msg, struct client *c)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
struct server_context *ctx = c->ctx;
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->registered)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_ALREADYREGISTERED);
|
2014-07-14 02:35:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (msg->params.len < 1)
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_NONICKNAMEGIVEN);
|
2014-07-14 02:35:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
const char *nickname = msg->params.vector[0];
|
|
|
|
if (irc_validate_nickname (nickname) != VALIDATION_OK)
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_ERRONEOUSNICKNAME, nickname);
|
2014-07-14 02:35:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->nickname)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
str_map_set (&ctx->users, c->nickname, NULL);
|
|
|
|
free (c->nickname);
|
2014-07-14 02:35:38 +02:00
|
|
|
}
|
2014-07-17 22:54:49 +02:00
|
|
|
if (str_map_find (&ctx->users, nickname))
|
|
|
|
{
|
|
|
|
irc_send_reply (c, IRC_ERR_NICKNAMEINUSE, nickname);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
// Allocate the nickname
|
2014-07-14 20:54:47 +02:00
|
|
|
c->nickname = xstrdup (nickname);
|
|
|
|
str_map_set (&ctx->users, nickname, c);
|
2014-07-14 02:35:38 +02:00
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_try_finish_registration (c);
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_handle_user (const struct irc_message *msg, struct client *c)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->registered)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_ALREADYREGISTERED);
|
2014-07-14 02:35:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (msg->params.len < 4)
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_NEEDMOREPARAMS, msg->command);
|
2014-07-14 02:35:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
const char *username = msg->params.vector[0];
|
|
|
|
const char *mode = msg->params.vector[1];
|
|
|
|
const char *realname = msg->params.vector[3];
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
// Unfortunately the protocol doesn't give us any means of rejecting it
|
|
|
|
if (!irc_is_valid_user (username))
|
|
|
|
username = "xxx";
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
free (c->username);
|
|
|
|
c->username = xstrdup (username);
|
|
|
|
free (c->realname);
|
|
|
|
c->realname = xstrdup (realname);
|
2014-07-14 02:35:38 +02:00
|
|
|
|
|
|
|
unsigned long m;
|
|
|
|
if (xstrtoul (&m, mode, 10))
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
if (m & 4) c->mode |= IRC_USER_MODE_RX_WALLOPS;
|
|
|
|
if (m & 8) c->mode |= IRC_USER_MODE_INVISIBLE;
|
2014-07-14 02:35:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_try_finish_registration (c);
|
2014-07-14 02:35:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 23:51:34 +02:00
|
|
|
static void
|
|
|
|
irc_handle_lusers (const struct irc_message *msg, struct client *c)
|
|
|
|
{
|
|
|
|
if (msg->params.len > 1 && !irc_is_this_me (c->ctx, msg->params.vector[1]))
|
|
|
|
irc_send_reply (c, IRC_ERR_NOSUCHSERVER, msg->params.vector[1]);
|
|
|
|
else
|
|
|
|
irc_send_lusers (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_motd (const struct irc_message *msg, struct client *c)
|
|
|
|
{
|
|
|
|
if (msg->params.len > 0 && !irc_is_this_me (c->ctx, msg->params.vector[0]))
|
|
|
|
irc_send_reply (c, IRC_ERR_NOSUCHSERVER, msg->params.vector[0]);
|
|
|
|
else
|
|
|
|
irc_send_motd (c);
|
|
|
|
}
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_handle_ping (const struct irc_message *msg, struct client *c)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
|
|
|
// XXX: the RFC is pretty incomprehensible about the exact usage
|
2014-07-17 23:51:34 +02:00
|
|
|
if (msg->params.len > 1 && !irc_is_this_me (c->ctx, msg->params.vector[1]))
|
|
|
|
irc_send_reply (c, IRC_ERR_NOSUCHSERVER, msg->params.vector[1]);
|
|
|
|
else if (msg->params.len < 1)
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_NOORIGIN);
|
2014-07-14 02:35:38 +02:00
|
|
|
else
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_send (c, ":%s PONG :%s",
|
|
|
|
c->ctx->server_name, msg->params.vector[0]);
|
2014-07-14 02:35:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 23:51:34 +02:00
|
|
|
static void
|
|
|
|
irc_handle_time (const struct irc_message *msg, struct client *c)
|
|
|
|
{
|
|
|
|
if (msg->params.len > 0 && !irc_is_this_me (c->ctx, msg->params.vector[0]))
|
|
|
|
irc_send_reply (c, IRC_ERR_NOSUCHSERVER, msg->params.vector[0]);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
time_t now = time (NULL);
|
|
|
|
struct tm tm;
|
|
|
|
strftime (buf, sizeof buf, "%a %b %d %Y %T", localtime_r (&now, &tm));
|
|
|
|
irc_send_reply (c, IRC_RPL_TIME, c->ctx->server_name, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_version (const struct irc_message *msg, struct client *c)
|
|
|
|
{
|
|
|
|
if (msg->params.len > 0 && !irc_is_this_me (c->ctx, msg->params.vector[0]))
|
|
|
|
irc_send_reply (c, IRC_ERR_NOSUCHSERVER, msg->params.vector[0]);
|
|
|
|
else
|
|
|
|
irc_send_reply (c, IRC_RPL_VERSION, PROGRAM_VERSION, g_debug_mode,
|
|
|
|
c->ctx->server_name, PROGRAM_NAME " " PROGRAM_VERSION);
|
|
|
|
}
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct irc_command
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
bool requires_registration;
|
2014-07-14 20:54:47 +02:00
|
|
|
void (*handler) (const struct irc_message *, struct client *);
|
2014-07-14 02:35:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_register_handlers (struct server_context *ctx)
|
|
|
|
{
|
2014-07-17 23:51:34 +02:00
|
|
|
// TODO: add an index for IRC_ERR_NOSUCHSERVER validation?
|
2014-07-14 02:35:38 +02:00
|
|
|
static const struct irc_command message_handlers[] =
|
|
|
|
{
|
2014-07-17 23:51:34 +02:00
|
|
|
{ "PASS", false, irc_handle_pass },
|
|
|
|
{ "NICK", false, irc_handle_nick },
|
|
|
|
{ "USER", false, irc_handle_user },
|
|
|
|
|
|
|
|
{ "LUSERS", true, irc_handle_lusers },
|
|
|
|
{ "MOTD", true, irc_handle_motd },
|
|
|
|
{ "PING", true, irc_handle_ping },
|
|
|
|
{ "TIME", true, irc_handle_time },
|
|
|
|
{ "VERSION", true, irc_handle_version },
|
2014-07-14 02:35:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < N_ELEMENTS (message_handlers); i++)
|
|
|
|
{
|
|
|
|
const struct irc_command *cmd = &message_handlers[i];
|
2014-07-15 23:28:00 +02:00
|
|
|
str_map_set (&ctx->handlers, cmd->name, (void *) cmd);
|
2014-07-14 02:35:38 +02:00
|
|
|
}
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_process_message (const struct irc_message *msg,
|
|
|
|
const char *raw, void *user_data)
|
|
|
|
{
|
2014-07-14 02:35:38 +02:00
|
|
|
(void) raw;
|
|
|
|
|
|
|
|
// XXX: we may want to discard everything following a QUIT etc.
|
2014-07-14 20:54:47 +02:00
|
|
|
// We can set a flag within the client object.
|
2014-07-14 02:35:38 +02:00
|
|
|
// TODO: see RFC 2812 :!
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
struct client *c = user_data;
|
|
|
|
struct irc_command *cmd = str_map_find (&c->ctx->handlers, msg->command);
|
2014-07-14 02:35:38 +02:00
|
|
|
if (!cmd)
|
2014-07-15 23:28:00 +02:00
|
|
|
irc_send_reply (c, IRC_ERR_UNKNOWNCOMMAND, msg->command);
|
2014-07-14 20:54:47 +02:00
|
|
|
else if (cmd->requires_registration && !c->registered)
|
|
|
|
irc_send_reply (c, IRC_ERR_NOTREGISTERED);
|
2014-07-14 02:35:38 +02:00
|
|
|
else
|
2014-07-14 20:54:47 +02:00
|
|
|
cmd->handler (msg, c);
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
// --- Network I/O -------------------------------------------------------------
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
static bool
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_try_read (struct client *c)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
struct str *buf = &c->read_buffer;
|
2014-07-12 23:06:39 +02:00
|
|
|
ssize_t n_read;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
str_ensure_space (buf, 512);
|
2014-07-14 20:54:47 +02:00
|
|
|
n_read = recv (c->socket_fd, buf->str + buf->len,
|
2014-07-12 23:06:39 +02:00
|
|
|
buf->alloc - buf->len - 1 /* null byte */, 0);
|
|
|
|
|
|
|
|
if (n_read > 0)
|
|
|
|
{
|
|
|
|
buf->str[buf->len += n_read] = '\0';
|
|
|
|
// TODO: discard characters above the 512 character limit
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_process_buffer (buf, irc_process_message, c);
|
2014-07-12 23:06:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (n_read == 0)
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (c, NULL);
|
2014-07-12 23:06:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return true;
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
print_debug ("%s: %s: %s", __func__, "recv", strerror (errno));
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (c, strerror (errno));
|
2014-07-12 23:06:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_try_read_ssl (struct client *c)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->ssl_tx_want_rx)
|
2014-07-09 01:54:50 +02:00
|
|
|
return true;
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
struct str *buf = &c->read_buffer;
|
|
|
|
c->ssl_rx_want_tx = false;
|
2014-07-09 01:54:50 +02:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
str_ensure_space (buf, 512);
|
2014-07-14 20:54:47 +02:00
|
|
|
int n_read = SSL_read (c->ssl, buf->str + buf->len,
|
2014-07-09 01:54:50 +02:00
|
|
|
buf->alloc - buf->len - 1 /* null byte */);
|
|
|
|
|
|
|
|
const char *error_info = NULL;
|
2014-07-14 20:54:47 +02:00
|
|
|
switch (xssl_get_error (c->ssl, n_read, &error_info))
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
buf->str[buf->len += n_read] = '\0';
|
|
|
|
// TODO: discard characters above the 512 character limit
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_process_buffer (buf, irc_process_message, c);
|
2014-07-09 01:54:50 +02:00
|
|
|
continue;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (c, NULL);
|
2014-07-09 01:54:50 +02:00
|
|
|
return false;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
return true;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2014-07-14 20:54:47 +02:00
|
|
|
c->ssl_rx_want_tx = true;
|
2014-07-12 22:54:35 +02:00
|
|
|
return true;
|
2014-07-09 01:54:50 +02:00
|
|
|
case XSSL_ERROR_TRY_AGAIN:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
print_debug ("%s: %s: %s", __func__, "SSL_read", error_info);
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (c, error_info);
|
2014-07-09 01:54:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_try_write (struct client *c)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
struct str *buf = &c->write_buffer;
|
2014-07-12 23:13:13 +02:00
|
|
|
ssize_t n_written;
|
|
|
|
|
|
|
|
while (buf->len)
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
n_written = send (c->socket_fd, buf->str, buf->len, 0);
|
2014-07-12 23:13:13 +02:00
|
|
|
if (n_written >= 0)
|
|
|
|
{
|
|
|
|
str_remove_slice (buf, 0, n_written);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return true;
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
print_debug ("%s: %s: %s", __func__, "send", strerror (errno));
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (c, strerror (errno));
|
2014-07-12 23:13:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-12 22:54:35 +02:00
|
|
|
return true;
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_try_write_ssl (struct client *c)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->ssl_rx_want_tx)
|
2014-07-09 01:54:50 +02:00
|
|
|
return true;
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
struct str *buf = &c->write_buffer;
|
|
|
|
c->ssl_tx_want_rx = false;
|
2014-07-09 01:54:50 +02:00
|
|
|
while (buf->len)
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
int n_written = SSL_write (c->ssl, buf->str, buf->len);
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
const char *error_info = NULL;
|
2014-07-14 20:54:47 +02:00
|
|
|
switch (xssl_get_error (c->ssl, n_written, &error_info))
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
str_remove_slice (buf, 0, n_written);
|
|
|
|
continue;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (c, NULL);
|
2014-07-09 01:54:50 +02:00
|
|
|
return false;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
return true;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2014-07-14 20:54:47 +02:00
|
|
|
c->ssl_tx_want_rx = true;
|
2014-07-12 22:54:35 +02:00
|
|
|
return true;
|
2014-07-09 01:54:50 +02:00
|
|
|
case XSSL_ERROR_TRY_AGAIN:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
print_debug ("%s: %s: %s", __func__, "SSL_write", error_info);
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (c, error_info);
|
2014-07-09 01:54:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
static bool
|
2014-07-14 20:54:47 +02:00
|
|
|
irc_autodetect_ssl (struct client *c)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
|
|
|
// Trivial SSL/TLS autodetection. The first block of data returned by
|
|
|
|
// recv() must be at least three bytes long for this to work reliably,
|
|
|
|
// but that should not pose a problem in practice.
|
|
|
|
//
|
|
|
|
// SSL2: 1xxx xxxx | xxxx xxxx | <1>
|
|
|
|
// (message length) (client hello)
|
|
|
|
// SSL3/TLS: <22> | <3> | xxxx xxxx
|
|
|
|
// (handshake)| (protocol version)
|
|
|
|
//
|
|
|
|
// Such byte sequences should never occur at the beginning of regular IRC
|
|
|
|
// communication, which usually begins with USER/NICK/PASS/SERVICE.
|
|
|
|
|
|
|
|
char buf[3];
|
|
|
|
start:
|
2014-07-14 20:54:47 +02:00
|
|
|
switch (recv (c->socket_fd, buf, sizeof buf, MSG_PEEK))
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
if ((buf[0] & 0x80) && buf[2] == 1)
|
|
|
|
return true;
|
|
|
|
case 2:
|
|
|
|
if (buf[0] == 22 && buf[1] == 3)
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (buf[0] == 22)
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-07-14 20:54:47 +02:00
|
|
|
client_initialize_ssl (struct client *c)
|
2014-07-14 02:35:38 +02:00
|
|
|
{
|
|
|
|
// SSL support not enabled
|
2014-07-14 20:54:47 +02:00
|
|
|
if (!c->ctx->ssl_ctx)
|
2014-07-14 02:35:38 +02:00
|
|
|
return false;
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
c->ssl = SSL_new (c->ctx->ssl_ctx);
|
|
|
|
if (!c->ssl)
|
2014-07-14 02:35:38 +02:00
|
|
|
goto error_ssl_1;
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
if (!SSL_set_fd (c->ssl, c->socket_fd))
|
2014-07-14 02:35:38 +02:00
|
|
|
goto error_ssl_2;
|
2014-07-14 20:54:47 +02:00
|
|
|
SSL_set_accept_state (c->ssl);
|
2014-07-14 02:35:38 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
error_ssl_2:
|
2014-07-14 20:54:47 +02:00
|
|
|
SSL_free (c->ssl);
|
|
|
|
c->ssl = NULL;
|
2014-07-14 02:35:38 +02:00
|
|
|
error_ssl_1:
|
|
|
|
// XXX: these error strings are really nasty; also there could be
|
|
|
|
// multiple errors on the OpenSSL stack.
|
|
|
|
print_debug ("%s: %s: %s", "could not initialize SSL",
|
2014-07-14 20:54:47 +02:00
|
|
|
c->hostname, ERR_error_string (ERR_get_error (), NULL));
|
2014-07-14 02:35:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
static void
|
|
|
|
on_irc_client_ready (const struct pollfd *pfd, void *user_data)
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
struct client *c = user_data;
|
|
|
|
if (!c->initialized)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
|
|
|
hard_assert (pfd->events == POLLIN);
|
2014-07-14 20:54:47 +02:00
|
|
|
if (irc_autodetect_ssl (c) && !client_initialize_ssl (c))
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
client_kill (c, NULL);
|
2014-07-09 01:54:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-07-14 20:54:47 +02:00
|
|
|
c->initialized = true;
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int new_events = 0;
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->ssl)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
|
|
|
// Reads may want to write, writes may want to read, poll() may
|
|
|
|
// return unexpected things in `revents'... let's try both
|
2014-07-14 20:54:47 +02:00
|
|
|
if (!irc_try_read_ssl (c) || !irc_try_write_ssl (c))
|
2014-07-12 22:54:35 +02:00
|
|
|
return;
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
new_events |= POLLIN;
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->write_buffer.len || c->ssl_rx_want_tx)
|
2014-07-09 01:54:50 +02:00
|
|
|
new_events |= POLLOUT;
|
|
|
|
|
|
|
|
// While we're waiting for an opposite event, we ignore the original
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->ssl_rx_want_tx) new_events &= ~POLLIN;
|
|
|
|
if (c->ssl_tx_want_rx) new_events &= ~POLLOUT;
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-14 20:54:47 +02:00
|
|
|
if (!irc_try_read (c) || !irc_try_write (c))
|
2014-07-12 22:54:35 +02:00
|
|
|
return;
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
new_events |= POLLIN;
|
2014-07-14 20:54:47 +02:00
|
|
|
if (c->write_buffer.len)
|
2014-07-09 01:54:50 +02:00
|
|
|
new_events |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
hard_assert (new_events != 0);
|
|
|
|
if (pfd->events != new_events)
|
2014-07-14 20:54:47 +02:00
|
|
|
poller_set (&c->ctx->poller, c->socket_fd, new_events,
|
|
|
|
(poller_dispatcher_func) on_irc_client_ready, c);
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-14 20:54:47 +02:00
|
|
|
on_irc_client_available (const struct pollfd *pfd, void *user_data)
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
|
|
|
(void) pfd;
|
|
|
|
struct server_context *ctx = user_data;
|
|
|
|
|
|
|
|
// TODO: stop accepting new connections when `max_connections' is reached
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// XXX: `struct sockaddr_storage' is not the most portable thing
|
|
|
|
struct sockaddr_storage peer;
|
|
|
|
socklen_t peer_len = sizeof peer;
|
|
|
|
|
|
|
|
int fd = accept (ctx->listen_fd, (struct sockaddr *) &peer, &peer_len);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (errno == ECONNABORTED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO: handle resource exhaustion (EMFILE, ENFILE) specially
|
|
|
|
// (stop accepting new connections and wait until we close some).
|
|
|
|
// FIXME: handle this better, bring the server down cleanly.
|
2014-07-16 23:23:03 +02:00
|
|
|
exit_fatal ("%s: %s", "accept", strerror (errno));
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char host[NI_MAXHOST] = "unknown", port[NI_MAXSERV] = "unknown";
|
|
|
|
int err = getnameinfo ((struct sockaddr *) &peer, peer_len,
|
2014-07-15 23:28:00 +02:00
|
|
|
host, sizeof host, port, sizeof port, NI_NUMERICSERV);
|
2014-07-09 01:54:50 +02:00
|
|
|
if (err)
|
|
|
|
print_debug ("%s: %s", "getnameinfo", gai_strerror (err));
|
|
|
|
print_debug ("accepted connection from %s:%s", host, port);
|
|
|
|
|
2014-07-14 20:54:47 +02:00
|
|
|
struct client *c = xmalloc (sizeof *c);
|
|
|
|
client_init (c);
|
2014-07-15 23:28:00 +02:00
|
|
|
c->ctx = ctx;
|
2014-07-14 20:54:47 +02:00
|
|
|
c->socket_fd = fd;
|
|
|
|
c->hostname = xstrdup (host);
|
|
|
|
LIST_PREPEND (ctx->clients, c);
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
// TODO: set a timeout on the socket, something like 3 minutes, then we
|
|
|
|
// should terminate the connection.
|
2014-07-15 23:28:00 +02:00
|
|
|
set_blocking (fd, false);
|
|
|
|
poller_set (&ctx->poller, fd, POLLIN,
|
2014-07-14 20:54:47 +02:00
|
|
|
(poller_dispatcher_func) on_irc_client_ready, c);
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 02:35:38 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static int
|
|
|
|
irc_ssl_verify_callback (int verify_ok, X509_STORE_CTX *ctx)
|
|
|
|
{
|
|
|
|
(void) verify_ok;
|
|
|
|
(void) ctx;
|
|
|
|
|
|
|
|
// We only want to provide additional privileges based on the client's
|
|
|
|
// certificate, so let's not terminate the connection because of a failure.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:59:17 +02:00
|
|
|
static bool
|
2014-07-17 08:43:21 +02:00
|
|
|
irc_initialize_ssl (struct server_context *ctx, struct error **e)
|
2014-07-12 21:59:17 +02:00
|
|
|
{
|
|
|
|
const char *ssl_cert = str_map_find (&ctx->config, "ssl_cert");
|
|
|
|
const char *ssl_key = str_map_find (&ctx->config, "ssl_key");
|
|
|
|
|
|
|
|
// Only try to enable SSL support if the user configures it; it is not
|
|
|
|
// a failure if no one has requested it.
|
|
|
|
if (!ssl_cert && !ssl_key)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!ssl_cert)
|
2014-07-17 08:43:21 +02:00
|
|
|
error_set (e, "no SSL certificate set");
|
|
|
|
else if (!ssl_key)
|
|
|
|
error_set (e, "no SSL private key set");
|
2014-07-14 22:12:04 +02:00
|
|
|
if (!ssl_cert || !ssl_key)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char *cert_path = resolve_config_filename (ssl_cert);
|
|
|
|
char *key_path = resolve_config_filename (ssl_key);
|
|
|
|
if (!cert_path)
|
2014-07-17 08:43:21 +02:00
|
|
|
error_set (e, "%s: %s", "cannot open file", ssl_cert);
|
|
|
|
else if (!key_path)
|
|
|
|
error_set (e, "%s: %s", "cannot open file", ssl_key);
|
2014-07-14 22:12:04 +02:00
|
|
|
if (!cert_path || !key_path)
|
2014-07-12 21:59:17 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ctx->ssl_ctx = SSL_CTX_new (SSLv23_server_method ());
|
|
|
|
if (!ctx->ssl_ctx)
|
2014-07-16 22:06:36 +02:00
|
|
|
{
|
|
|
|
// XXX: these error strings are really nasty; also there could be
|
|
|
|
// multiple errors on the OpenSSL stack.
|
2014-07-17 08:43:21 +02:00
|
|
|
error_set (e, "%s: %s", "could not initialize SSL",
|
2014-07-16 22:06:36 +02:00
|
|
|
ERR_error_string (ERR_get_error (), NULL));
|
2014-07-12 21:59:17 +02:00
|
|
|
goto error_ssl_1;
|
2014-07-16 22:06:36 +02:00
|
|
|
}
|
2014-07-12 21:59:17 +02:00
|
|
|
SSL_CTX_set_verify (ctx->ssl_ctx,
|
|
|
|
SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, irc_ssl_verify_callback);
|
|
|
|
// XXX: maybe we should call SSL_CTX_set_options() for some workarounds
|
|
|
|
|
|
|
|
// XXX: perhaps we should read the files ourselves for better messages
|
2014-07-14 22:12:04 +02:00
|
|
|
if (!SSL_CTX_use_certificate_chain_file (ctx->ssl_ctx, cert_path))
|
2014-07-12 21:59:17 +02:00
|
|
|
{
|
2014-07-17 08:43:21 +02:00
|
|
|
error_set (e, "%s: %s", "setting the SSL client certificate failed",
|
2014-07-12 21:59:17 +02:00
|
|
|
ERR_error_string (ERR_get_error (), NULL));
|
|
|
|
goto error_ssl_2;
|
|
|
|
}
|
2014-07-14 22:12:04 +02:00
|
|
|
if (!SSL_CTX_use_PrivateKey_file (ctx->ssl_ctx, key_path, SSL_FILETYPE_PEM))
|
2014-07-12 21:59:17 +02:00
|
|
|
{
|
2014-07-17 08:43:21 +02:00
|
|
|
error_set (e, "%s: %s", "setting the SSL private key failed",
|
2014-07-12 21:59:17 +02:00
|
|
|
ERR_error_string (ERR_get_error (), NULL));
|
|
|
|
goto error_ssl_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: SSL_CTX_check_private_key()? It has probably already been checked
|
|
|
|
// by SSL_CTX_use_PrivateKey_file() above.
|
|
|
|
|
|
|
|
// Gah, spare me your awkward semantics, I just want to push data!
|
|
|
|
// XXX: do we want SSL_MODE_AUTO_RETRY as well? I guess not.
|
|
|
|
SSL_CTX_set_mode (ctx->ssl_ctx,
|
|
|
|
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_ENABLE_PARTIAL_WRITE);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error_ssl_2:
|
|
|
|
SSL_CTX_free (ctx->ssl_ctx);
|
|
|
|
ctx->ssl_ctx = NULL;
|
|
|
|
error_ssl_1:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-13 23:47:29 +02:00
|
|
|
static bool
|
|
|
|
irc_initialize_catalog (struct server_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
hard_assert (ctx->catalog == (nl_catd) -1);
|
|
|
|
const char *catalog = str_map_find (&ctx->config, "catalog");
|
|
|
|
if (!catalog)
|
|
|
|
return true;
|
|
|
|
|
2014-07-14 22:12:04 +02:00
|
|
|
char *path = resolve_config_filename (catalog);
|
|
|
|
if (!path)
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e, "%s: %s", "cannot open file", catalog);
|
2014-07-14 22:12:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ctx->catalog = catopen (path, NL_CAT_LOCALE);
|
|
|
|
free (path);
|
|
|
|
|
2014-07-13 23:47:29 +02:00
|
|
|
if (ctx->catalog == (nl_catd) -1)
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e, "%s: %s",
|
2014-07-13 23:47:29 +02:00
|
|
|
"failed reading the message catalog file", strerror (errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-13 21:27:38 +02:00
|
|
|
static bool
|
|
|
|
irc_initialize_motd (struct server_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
hard_assert (ctx->motd.len == 0);
|
|
|
|
const char *motd = str_map_find (&ctx->config, "motd");
|
|
|
|
if (!motd)
|
|
|
|
return true;
|
|
|
|
|
2014-07-14 22:12:04 +02:00
|
|
|
char *path = resolve_config_filename (motd);
|
|
|
|
if (!path)
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e, "%s: %s", "cannot open file", motd);
|
2014-07-14 22:12:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
FILE *fp = fopen (path, "r");
|
|
|
|
free (path);
|
|
|
|
|
2014-07-13 21:27:38 +02:00
|
|
|
if (!fp)
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e, "%s: %s",
|
2014-07-14 22:12:04 +02:00
|
|
|
"failed reading the MOTD file", strerror (errno));
|
2014-07-13 21:27:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct str line;
|
|
|
|
str_init (&line);
|
|
|
|
while (read_line (fp, &line))
|
|
|
|
str_vector_add_owned (&ctx->motd, str_steal (&line));
|
|
|
|
str_free (&line);
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-13 04:59:22 +02:00
|
|
|
static bool
|
|
|
|
irc_initialize_server_name (struct server_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
enum validation_result res;
|
|
|
|
const char *server_name = str_map_find (&ctx->config, "server_name");
|
|
|
|
if (server_name)
|
|
|
|
{
|
|
|
|
res = irc_validate_hostname (server_name);
|
|
|
|
if (res != VALIDATION_OK)
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e, "invalid configuration value for `%s': %s",
|
|
|
|
"server_name", irc_validate_to_str (res));
|
2014-07-13 04:59:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ctx->server_name = xstrdup (server_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char hostname[HOST_NAME_MAX];
|
|
|
|
if (gethostname (hostname, sizeof hostname))
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e, "%s: %s",
|
|
|
|
"getting the hostname failed", strerror (errno));
|
2014-07-13 04:59:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
res = irc_validate_hostname (hostname);
|
|
|
|
if (res != VALIDATION_OK)
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e,
|
2014-07-13 04:59:22 +02:00
|
|
|
"`%s' is not set and the hostname (`%s') cannot be used: %s",
|
|
|
|
"server_name", hostname, irc_validate_to_str (res));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ctx->server_name = xstrdup (hostname);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
static bool
|
|
|
|
irc_listen (struct server_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
const char *bind_host = str_map_find (&ctx->config, "bind_host");
|
|
|
|
const char *bind_port = str_map_find (&ctx->config, "bind_port");
|
|
|
|
hard_assert (bind_port != NULL); // We have a default value for this
|
|
|
|
|
|
|
|
struct addrinfo gai_hints, *gai_result, *gai_iter;
|
|
|
|
memset (&gai_hints, 0, sizeof gai_hints);
|
|
|
|
|
|
|
|
gai_hints.ai_socktype = SOCK_STREAM;
|
|
|
|
gai_hints.ai_flags = AI_PASSIVE;
|
|
|
|
|
|
|
|
int err = getaddrinfo (bind_host, bind_port, &gai_hints, &gai_result);
|
|
|
|
if (err)
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e, "%s: %s: %s",
|
2014-07-09 01:54:50 +02:00
|
|
|
"network setup failed", "getaddrinfo", gai_strerror (err));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
char real_host[NI_MAXHOST], real_port[NI_MAXSERV];
|
|
|
|
|
|
|
|
for (gai_iter = gai_result; gai_iter; gai_iter = gai_iter->ai_next)
|
|
|
|
{
|
|
|
|
sockfd = socket (gai_iter->ai_family,
|
|
|
|
gai_iter->ai_socktype, gai_iter->ai_protocol);
|
|
|
|
if (sockfd == -1)
|
|
|
|
continue;
|
|
|
|
set_cloexec (sockfd);
|
|
|
|
|
|
|
|
int yes = 1;
|
|
|
|
soft_assert (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE,
|
|
|
|
&yes, sizeof yes) != -1);
|
|
|
|
soft_assert (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&yes, sizeof yes) != -1);
|
|
|
|
|
|
|
|
real_host[0] = real_port[0] = '\0';
|
|
|
|
err = getnameinfo (gai_iter->ai_addr, gai_iter->ai_addrlen,
|
|
|
|
real_host, sizeof real_host, real_port, sizeof real_port,
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
if (err)
|
|
|
|
print_debug ("%s: %s", "getnameinfo", gai_strerror (err));
|
|
|
|
|
|
|
|
if (bind (sockfd, gai_iter->ai_addr, gai_iter->ai_addrlen))
|
2014-07-14 00:10:59 +02:00
|
|
|
print_error ("bind to %s:%s failed: %s",
|
2014-07-09 01:54:50 +02:00
|
|
|
real_host, real_port, strerror (errno));
|
|
|
|
else if (listen (sockfd, 16 /* arbitrary number */))
|
2014-07-14 00:10:59 +02:00
|
|
|
print_error ("listen at %s:%s failed: %s",
|
2014-07-09 01:54:50 +02:00
|
|
|
real_host, real_port, strerror (errno));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
xclose (sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo (gai_result);
|
|
|
|
|
|
|
|
if (!gai_iter)
|
|
|
|
{
|
2014-07-15 22:54:39 +02:00
|
|
|
error_set (e, "network setup failed");
|
2014-07-09 01:54:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-15 23:28:00 +02:00
|
|
|
set_blocking (sockfd, false);
|
2014-07-09 01:54:50 +02:00
|
|
|
ctx->listen_fd = sockfd;
|
|
|
|
poller_set (&ctx->poller, ctx->listen_fd, POLLIN,
|
2014-07-14 20:54:47 +02:00
|
|
|
(poller_dispatcher_func) on_irc_client_available, ctx);
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
print_status ("listening at %s:%s", real_host, real_port);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_signal_pipe_readable (const struct pollfd *fd, struct server_context *ctx)
|
|
|
|
{
|
|
|
|
char *dummy;
|
|
|
|
(void) read (fd->fd, &dummy, 1);
|
|
|
|
|
2014-07-12 21:59:17 +02:00
|
|
|
// TODO: send ERROR messages to anyone, wait for the messages to get
|
|
|
|
// dispatched for a few seconds, RST the rest and quit.
|
2014-07-09 01:54:50 +02:00
|
|
|
if (g_termination_requested && !ctx->quitting)
|
|
|
|
{
|
2014-07-12 21:59:17 +02:00
|
|
|
#if 0
|
2014-07-09 01:54:50 +02:00
|
|
|
initiate_quit (ctx);
|
|
|
|
#endif
|
2014-07-12 21:59:17 +02:00
|
|
|
}
|
2014-07-09 01:54:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 23:26:50 +02:00
|
|
|
static void
|
|
|
|
daemonize (void)
|
|
|
|
{
|
|
|
|
// TODO: create and lock a PID file?
|
|
|
|
print_status ("daemonizing...");
|
|
|
|
|
|
|
|
if (chdir ("/"))
|
|
|
|
exit_fatal ("%s: %s", "chdir", strerror (errno));
|
|
|
|
|
|
|
|
pid_t pid;
|
|
|
|
if ((pid = fork ()) < 0)
|
|
|
|
exit_fatal ("%s: %s", "fork", strerror (errno));
|
|
|
|
else if (pid)
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
|
|
|
|
setsid ();
|
|
|
|
signal (SIGHUP, SIG_IGN);
|
|
|
|
|
|
|
|
if ((pid = fork ()) < 0)
|
|
|
|
exit_fatal ("%s: %s", "fork", strerror (errno));
|
|
|
|
else if (pid)
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
|
|
|
|
openlog (PROGRAM_NAME, LOG_NDELAY | LOG_NOWAIT | LOG_PID, 0);
|
|
|
|
g_log_message_real = log_message_syslog;
|
|
|
|
|
|
|
|
// XXX: we may close our own descriptors this way, crippling ourselves
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
xclose (i);
|
|
|
|
|
|
|
|
int tty = open ("/dev/null", O_RDWR);
|
|
|
|
if (tty != 0 || dup (0) != 1 || dup (0) != 2)
|
|
|
|
exit_fatal ("failed to reopen FD's: %s", strerror (errno));
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:54:50 +02:00
|
|
|
static void
|
|
|
|
print_usage (const char *program_name)
|
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"Usage: %s [OPTION]...\n"
|
|
|
|
"Experimental IRC server.\n"
|
|
|
|
"\n"
|
|
|
|
" -d, --debug run in debug mode (do not daemonize)\n"
|
|
|
|
" -h, --help display this help and exit\n"
|
|
|
|
" -V, --version output version information and exit\n"
|
|
|
|
" --write-default-cfg [filename]\n"
|
|
|
|
" write a default configuration file and exit\n",
|
|
|
|
program_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char *invocation_name = argv[0];
|
|
|
|
|
|
|
|
static struct option opts[] =
|
|
|
|
{
|
|
|
|
{ "debug", no_argument, NULL, 'd' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "write-default-cfg", optional_argument, NULL, 'w' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int c, opt_index;
|
|
|
|
|
|
|
|
c = getopt_long (argc, argv, "dhV", opts, &opt_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
g_debug_mode = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
print_usage (invocation_name);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'V':
|
|
|
|
printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'w':
|
2014-07-14 22:12:04 +02:00
|
|
|
call_write_default_config (optarg, g_config_table);
|
2014-07-09 01:54:50 +02:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
default:
|
2014-07-16 23:23:03 +02:00
|
|
|
print_error ("wrong options");
|
2014-07-09 01:54:50 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print_status (PROGRAM_NAME " " PROGRAM_VERSION " starting");
|
|
|
|
setup_signal_handlers ();
|
|
|
|
|
|
|
|
SSL_library_init ();
|
|
|
|
atexit (EVP_cleanup);
|
|
|
|
SSL_load_error_strings ();
|
|
|
|
// XXX: ERR_load_BIO_strings()? Anything else?
|
|
|
|
atexit (ERR_free_strings);
|
|
|
|
|
|
|
|
struct server_context ctx;
|
|
|
|
server_context_init (&ctx);
|
2014-07-14 02:35:38 +02:00
|
|
|
irc_register_handlers (&ctx);
|
2014-07-09 01:54:50 +02:00
|
|
|
|
2014-07-13 23:50:44 +02:00
|
|
|
struct error *e = NULL;
|
2014-07-09 01:54:50 +02:00
|
|
|
if (!read_config_file (&ctx.config, &e))
|
|
|
|
{
|
2014-07-16 23:23:03 +02:00
|
|
|
print_error ("error loading configuration: %s", e->message);
|
2014-07-09 01:54:50 +02:00
|
|
|
error_free (e);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
poller_set (&ctx.poller, g_signal_pipe[0], POLLIN,
|
|
|
|
(poller_dispatcher_func) on_signal_pipe_readable, &ctx);
|
|
|
|
|
2014-07-17 08:43:21 +02:00
|
|
|
if (!irc_initialize_ssl (&ctx, &e)
|
|
|
|
|| !irc_initialize_server_name (&ctx, &e)
|
2014-07-14 00:10:59 +02:00
|
|
|
|| !irc_initialize_motd (&ctx, &e)
|
|
|
|
|| !irc_initialize_catalog (&ctx, &e)
|
2014-07-13 23:47:29 +02:00
|
|
|
|| !irc_listen (&ctx, &e))
|
2014-07-09 01:54:50 +02:00
|
|
|
{
|
|
|
|
print_error ("%s", e->message);
|
|
|
|
error_free (e);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-07-15 22:23:53 +02:00
|
|
|
if (!g_debug_mode)
|
2014-07-16 23:26:50 +02:00
|
|
|
daemonize ();
|
2014-07-09 01:54:50 +02:00
|
|
|
|
|
|
|
ctx.polling = true;
|
|
|
|
while (ctx.polling)
|
|
|
|
poller_run (&ctx.poller);
|
|
|
|
|
|
|
|
server_context_free (&ctx);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|