325 lines
12 KiB
C
325 lines
12 KiB
C
/*
|
|
* This file is part of uIRCd. (https://git.redxen.eu/caskd/uIRCd)
|
|
* Copyright (c) 2019, 2020 Alex-David Denes
|
|
*
|
|
* uIRCd 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 3 of the License, or
|
|
* any later version.
|
|
*
|
|
* uIRCd 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 uIRCd. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "main.h"
|
|
|
|
#include "buffer.h"
|
|
#include "configuration.h"
|
|
#include "connection.h"
|
|
#include "filesystem.h"
|
|
#include "logging.h"
|
|
#include "memory.h"
|
|
#include "signal.h"
|
|
|
|
#include <errno.h> // errno
|
|
#include <fcntl.h> // O_NONBLOCK
|
|
#include <stdio.h> // printf()
|
|
#include <stdlib.h> // EXIT_SUCCESS EXIT_FAILURE
|
|
#include <string.h> // strerror()
|
|
#include <sys/types.h> // time_t size_t
|
|
#include <time.h> // time()
|
|
#include <unistd.h> // getopt() chdir() close()
|
|
|
|
#define UIRC_IRCV3
|
|
#define UIRC_HELPERS
|
|
#include <uirc/uirc.h> // IRC_Message
|
|
|
|
int
|
|
parse_args(int argc, char** argv, Connection* conn)
|
|
{
|
|
int c;
|
|
unsigned long chanindex = 0;
|
|
while ((c = getopt(argc,
|
|
argv,
|
|
"V:a:p:d:t:N:U:R:P:q:c:k:"
|
|
#ifdef UIRCD_FEATURE_LIBCONFIG
|
|
"C:"
|
|
#endif /* UIRCD_FEATURE_LIBCONFIG */
|
|
"vh"))
|
|
!= -1) {
|
|
switch (c) {
|
|
case 'V': {
|
|
loglevel = atoi(optarg);
|
|
LOG(LOG_DEBUG, "Log level was set to %i", loglevel);
|
|
break;
|
|
}
|
|
case 'a': allocate_copy(&conn->data.address, optarg); break;
|
|
case 'p': allocate_copy(&conn->data.service, optarg); break;
|
|
case 'd': allocate_copy(&conn->data.path, optarg); break;
|
|
case 't': {
|
|
conn->data.timeout = (unsigned int) atoi(optarg);
|
|
LOG(LOG_DEBUG, "Timeout was set to %li", conn->data.timeout);
|
|
break;
|
|
}
|
|
case 'N': allocate_copy(&conn->user.nickname, optarg); break;
|
|
case 'U': allocate_copy(&conn->user.username, optarg); break;
|
|
case 'R': allocate_copy(&conn->user.realname, optarg); break;
|
|
case 'P': allocate_copy(&conn->user.password, optarg); break;
|
|
case 'q': allocate_copy(&conn->data.quitmsg, optarg); break;
|
|
case 'c': {
|
|
resize_chanarray(&conn->info.channels);
|
|
chanindex = get_channelindex(optarg, conn->info.channels);
|
|
if (!set_channel(&conn->info.channels[chanindex], optarg, NULL, true)) LOG(LOG_WARN, "Couldn't set channel %s as chan #%lu.", optarg, chanindex);
|
|
break;
|
|
}
|
|
case 'k': {
|
|
if (!set_channel(&conn->info.channels[chanindex], NULL, optarg, true)) LOG(LOG_WARN, "Couldn't set key for channel #%lu.", chanindex);
|
|
break;
|
|
}
|
|
case 'v': printf("uIRCd version " UIRCD_VERSION); return 0;
|
|
case 'h': print_help(); return 0;
|
|
#ifdef UIRCD_FEATURE_LIBCONFIG
|
|
case 'C': parse_configfile(optarg, conn); break;
|
|
#endif /* UIRCD_FEATURE_LIBCONFIG */
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void
|
|
print_help(void)
|
|
{
|
|
struct help {
|
|
char arg;
|
|
char* type;
|
|
char* desc;
|
|
char* def;
|
|
} arg_list[] = { { 'a', "host", "Host to connect to (IP/Hostname)", UIRCD_DEFAULT_ADDR },
|
|
{ 'p', "serv", "Service (port for TCP/UDP)", UIRCD_DEFAULT_PORT },
|
|
{ 'N', "str", "Nickname for registration", UIRCD_DEFAULT_NICK },
|
|
{ 'U', "str", "Username for registration", UIRCD_DEFAULT_USER },
|
|
{ 'R', "str", "Realname for registration", UIRCD_DEFAULT_REAL },
|
|
{ 'P', "str", "Password for registration", NULL },
|
|
{ 'd', "str", "Directory for logs", "current dir" },
|
|
{ 'q', "str", "Quit message", UIRCD_DEFAULT_QUITMSG },
|
|
{ 't', "uint", "Timeout duration", NULL },
|
|
{ 'V', "int", "Log level", "3 [LOG_INFO]" },
|
|
{ 'c', "str", "Channel", NULL },
|
|
{ 'k', "str", "Channel key", NULL },
|
|
#ifdef UIRCD_FEATURE_LIBCONFIG
|
|
{ 'C', "path", "Configuration path", NULL },
|
|
#endif /* UIRCD_FEATURE_LIBCONFIG */
|
|
{ 'v', NULL, "Print version information", NULL },
|
|
{ 'h', NULL, "Print this help message", NULL } };
|
|
printf("usage: uircd -a <host> [ <options> ... ]\n");
|
|
for (size_t i = 0; i < sizeof(arg_list) / sizeof(*arg_list); i++) {
|
|
printf(" -%c", arg_list[i].arg);
|
|
if (arg_list[i].type != NULL) printf(" \t%s", arg_list[i].type);
|
|
if (arg_list[i].desc != NULL) printf(" \t%s", arg_list[i].desc);
|
|
if (arg_list[i].def != NULL) printf(" (default: %s)", arg_list[i].def);
|
|
putchar('\n');
|
|
}
|
|
}
|
|
|
|
int
|
|
main(int argc, char* argv[])
|
|
{
|
|
IRC_Message buffer;
|
|
Connection connection = { 0 };
|
|
time_t ctime; // Current time
|
|
struct timespec sleep = { 0, 50000000L }; // Interval between loops when idle (network buffer is empty)
|
|
struct {
|
|
Buffer_Info recv[2], // Network recv buffer / FIFO read buffer
|
|
send[2], // Network send buffer / FIFO send buffer
|
|
log; // Log writing buffer
|
|
} buf = {
|
|
.recv = { { .fd = -1 }, { .fd = -1 } },
|
|
.send = { { .fd = -1 }, { .fd = -1 } },
|
|
.log = { .fd = -1 },
|
|
};
|
|
|
|
/*
|
|
* Initialisation
|
|
*/
|
|
init_chanarray(&connection.info.channels);
|
|
if (!set_config_defaults(&connection)) {
|
|
LOG(LOG_FATAL, "Couldn't allocate memory for configuration variables. " ERRNOFMT, strerror(errno), errno);
|
|
return EXIT_FAILURE;
|
|
}
|
|
switch (parse_args(argc, argv, &connection)) {
|
|
case 0: return EXIT_SUCCESS;
|
|
case -1: return EXIT_FAILURE;
|
|
}
|
|
if (connection.data.path != NULL) {
|
|
int tmpres;
|
|
// TODO: Add chroot()/jail support by default where supported
|
|
if ((tmpres = chdir(connection.data.path)) != 0) {
|
|
LOG(LOG_ERROR, "Couldn't change log directory to %s. " ERRNOFMT, connection.data.path, strerror(errno), errno);
|
|
return EXIT_FAILURE;
|
|
}
|
|
LOG(LOG_VERBOSE, "Changed root directory to %s.", connection.data.path);
|
|
}
|
|
setup_signals();
|
|
|
|
/*
|
|
* Main loop
|
|
*/
|
|
for (;;) {
|
|
ctime = time(NULL);
|
|
if (connection.info.state == CONN_CLOSED) {
|
|
LOG(LOG_VERBOSE, "%s", "Exiting gracefully.");
|
|
return EXIT_SUCCESS;
|
|
} else if (!run || connection.info.state == CONN_CLOSING) {
|
|
signed long temp;
|
|
if ((temp = Assm_mesg(buf.send[0].buffer, Assm_cmd_QUIT(connection.data.quitmsg), buf.send[0].csize)) > 0) {
|
|
buf.send[0].append_pos = (unsigned long) temp;
|
|
if (flush_buffer(buf.send[0].buffer, buf.send[0].append_pos, buf.send[0].fd) != -1)
|
|
LOG(LOG_VERBOSE, "Sent a QUIT message to " ADDRFMT " containing \"%s\".", connection.data.address, connection.data.service, connection.data.quitmsg);
|
|
}
|
|
reset_buffer(&buf.recv[0], 2); // net rw/FIFO read fd
|
|
reset_buffer(&buf.recv[1], 2); // FIFO read fd
|
|
connection.info.state = CONN_CLOSED;
|
|
LOG(LOG_VERBOSE, "Connection to " ADDRFMT " was closed.", connection.data.address, connection.data.service);
|
|
continue;
|
|
} else if (connection.info.state == CONN_RECONNECTING) {
|
|
connection.info.state = CONN_PENDING;
|
|
if (connection.info.reconinter <= 300) connection.info.reconinter += 5;
|
|
continue;
|
|
} else if (connection.info.state == CONN_PENDING) {
|
|
|
|
// Reconnection throttling
|
|
if (ctime - connection.info.l_connect < connection.info.reconinter) continue;
|
|
|
|
connection.info.l_connect = ctime;
|
|
|
|
/* Reset all state-dependent values to empty */
|
|
reset_buffer(&buf.recv[0], 2);
|
|
reset_buffer(&buf.recv[1], 2);
|
|
reset_buffer(&buf.send[0], 1);
|
|
reset_buffer(&buf.send[1], 1);
|
|
reset_buffer(&buf.log, 1);
|
|
connection.info.l_message = 0;
|
|
|
|
// Connection initialisation and registration
|
|
if ((buf.send[0].fd = init_connection(&connection)) > 0) {
|
|
buf.recv[0].fd = buf.send[1].fd = buf.send[0].fd;
|
|
add_socket_flags(buf.send[0].fd, O_NONBLOCK);
|
|
if (!register_user(&connection, &buf.send[0])) {
|
|
connection.info.state = CONN_RECONNECTING;
|
|
continue;
|
|
}
|
|
connection.info.state = CONN_REGISTERED;
|
|
} else if (buf.send[0].fd == INIT_SOFTFAIL) {
|
|
connection.info.state = CONN_RECONNECTING;
|
|
continue;
|
|
} else if (buf.send[0].fd == INIT_HARDFAIL) {
|
|
connection.info.state = CONN_CLOSED;
|
|
continue;
|
|
}
|
|
} else if (connection.info.state == CONN_IDLE) {
|
|
if (connection.info.state == CONN_IDLE) nanosleep(&sleep, NULL);
|
|
if (connection.data.timeout > 0 && connection.info.l_message < ctime - connection.data.timeout) {
|
|
LOG(LOG_WARN, "Timed out because no message was received since %lu.", connection.info.l_message);
|
|
connection.info.state = CONN_RECONNECTING;
|
|
continue;
|
|
}
|
|
if (buf.recv[1].fd != -1) {
|
|
ssize_t r_bytes, len;
|
|
if ((r_bytes = read_buffer(&buf.recv[1])) == -1) {
|
|
connection.info.state = CONN_RECONNECTING;
|
|
continue;
|
|
}
|
|
|
|
memset((void*) &buffer, '\0', sizeof(IRC_Message));
|
|
if ((len = get_buffer_line(buf.recv[1].buffer)) > 0) {
|
|
LOG(LOG_DEBUG, "Got IRC message: %s", buf.recv[1].buffer);
|
|
if (Tok_mesg(buf.recv[1].buffer, &buffer) == 1) {
|
|
signed long temp;
|
|
if ((temp = Assm_mesg(buf.send[1].buffer, &buffer, buf.send[1].csize)) > 0) {
|
|
if (flush_buffer(buf.send[1].buffer, (size_t) temp, buf.send[1].fd) == -1) {
|
|
connection.info.state = CONN_RECONNECTING;
|
|
continue;
|
|
}
|
|
}
|
|
} else
|
|
LOG(LOG_WARN, "%s", "Received invalid IRC message on FIFO (see RFC2812).");
|
|
|
|
for (long unsigned int x = 0; x < buf.recv[1].csize && *(buf.recv[1].buffer + len + x); x++) *(buf.recv[1].buffer + x) = *(buf.recv[1].buffer + x + len);
|
|
buf.recv[1].append_pos -= (unsigned long) len;
|
|
*(buf.recv[1].buffer + buf.recv[1].append_pos) = '\0';
|
|
} else if (buf.recv[1].append_pos == buf.recv[1].csize - 1) {
|
|
#ifdef UIRCD_RELAXED_RFC
|
|
// TODO: Add resize here
|
|
#else /* UIRCD_RELAXED_RFC */
|
|
LOG(LOG_WARN, "%s.", "FIFO buffer is full and no message could be parsed. Cleared buffer.");
|
|
reset_buffer(&buf.recv[1], false);
|
|
#endif /* UIRCD_RELAXED_RFC */
|
|
}
|
|
} else {
|
|
char pathbuf[UIRCD_LIMITS_PATH];
|
|
if (get_path(pathbuf, sizeof(pathbuf), &connection, NULL, true, true, false) >= 0) {
|
|
if (mkdir_bottomup(pathbuf)) buf.recv[1].fd = makeinput(pathbuf);
|
|
}
|
|
}
|
|
} else if (connection.info.state == CONN_ACTIVE) {
|
|
connection.info.state = CONN_IDLE;
|
|
}
|
|
|
|
ssize_t r_bytes, len;
|
|
if ((r_bytes = read_buffer(&buf.recv[0])) == -1) {
|
|
connection.info.state = CONN_RECONNECTING;
|
|
continue;
|
|
}
|
|
|
|
memset((void*) &buffer, '\0', sizeof(IRC_Message));
|
|
if ((len = get_buffer_line(buf.recv[0].buffer)) > 0) {
|
|
LOG(LOG_DEBUG, "Got IRC message on recvbuffer: %s", buf.recv[0].buffer);
|
|
if (Tok_mesg(buf.recv[0].buffer, &buffer) == 1) {
|
|
connection.info.l_message = ctime;
|
|
|
|
char datebuf[25];
|
|
if (buffer.tags.time.value == NULL) {
|
|
Assm_tag_timestamp(datebuf, sizeof(datebuf), ctime);
|
|
buffer.tags.time.value = datebuf;
|
|
}
|
|
|
|
char logpath[UIRCD_LIMITS_PATH];
|
|
if (Assm_mesg(buf.log.buffer, &buffer, buf.log.csize) > 0) {
|
|
printf("%s\r\n", buf.log.buffer);
|
|
bool match[][3] = { { false, false, false }, { true, false, false }, { true, false, true } };
|
|
for (unsigned long i = 0; i < sizeof(match) / sizeof(*match); i++) {
|
|
if (get_path(logpath, sizeof(logpath), &connection, &buffer, match[i][0], match[i][1], match[i][2]) >= 0) {
|
|
if (mkdir_bottomup(logpath)) write_log(logpath, buf.log.buffer);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!auto_msg_actions(&buffer, &connection, &buf.send[0])) continue;
|
|
|
|
for (long unsigned int x = 0; x < buf.recv[0].csize && *(buf.recv[0].buffer + len + x); x++) *(buf.recv[0].buffer + x) = *(buf.recv[0].buffer + x + len);
|
|
buf.recv[0].append_pos -= (unsigned long) len;
|
|
*(buf.recv[0].buffer + buf.recv[0].append_pos) = '\0';
|
|
connection.info.state = CONN_ACTIVE;
|
|
} else
|
|
LOG(LOG_WARN, "%s", "Received invalid IRC message (see RFC2812).");
|
|
} else if (len == -1) {
|
|
connection.info.state = CONN_RECONNECTING;
|
|
} else if (buf.recv[0].append_pos == buf.recv[0].csize - 1) {
|
|
#ifdef UIRCD_RELAXED_RFC
|
|
// TODO: Add resize here
|
|
#else /* UIRCD_RELAXED_RFC */
|
|
LOG(LOG_WARN, "%s.", "Receive buffer is full and no message could be parsed.");
|
|
connection.info.state = CONN_RECONNECTING;
|
|
#endif /* UIRCD_RELAXED_RFC */
|
|
}
|
|
}
|
|
LOG(LOG_FATAL, "%s", "We shouldn't be here.");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|