2020-02-01 06:18:01 +00:00
|
|
|
/* Copyright (C) 2020 C. McEnroe <june@causal.agency>
|
|
|
|
*
|
|
|
|
* 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 3 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, see <https://www.gnu.org/licenses/>.
|
2020-06-08 21:48:07 +00:00
|
|
|
*
|
|
|
|
* Additional permission under GNU GPL version 3 section 7:
|
|
|
|
*
|
|
|
|
* If you modify this Program, or any covered work, by linking or
|
|
|
|
* combining it with LibreSSL (or a modified version of that library),
|
|
|
|
* containing parts covered by the terms of the OpenSSL License and the
|
|
|
|
* original SSLeay license, the licensors of this Program grant you
|
|
|
|
* additional permission to convey the resulting work. Corresponding
|
|
|
|
* Source for a non-source form of such a combination shall include the
|
|
|
|
* source code for the parts of LibreSSL used as well as that of the
|
|
|
|
* covered work.
|
2020-02-01 06:18:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2020-02-06 07:21:04 +00:00
|
|
|
#include <sys/stat.h>
|
2020-02-01 06:18:01 +00:00
|
|
|
#include <sysexits.h>
|
|
|
|
#include <tls.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "chat.h"
|
|
|
|
|
|
|
|
struct tls *client;
|
|
|
|
|
2020-02-06 07:21:04 +00:00
|
|
|
static byte *readFile(size_t *len, FILE *file) {
|
|
|
|
struct stat stat;
|
|
|
|
int error = fstat(fileno(file), &stat);
|
|
|
|
if (error) err(EX_IOERR, "fstat");
|
|
|
|
|
|
|
|
byte *buf = malloc(stat.st_size);
|
|
|
|
if (!buf) err(EX_OSERR, "malloc");
|
|
|
|
|
|
|
|
rewind(file);
|
|
|
|
*len = fread(buf, 1, stat.st_size, file);
|
|
|
|
if (ferror(file)) err(EX_IOERR, "fread");
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ircConfig(bool insecure, FILE *cert, FILE *priv) {
|
2020-02-01 06:18:01 +00:00
|
|
|
struct tls_config *config = tls_config_new();
|
|
|
|
if (!config) errx(EX_SOFTWARE, "tls_config_new");
|
|
|
|
|
|
|
|
int error = tls_config_set_ciphers(config, "compat");
|
|
|
|
if (error) {
|
|
|
|
errx(
|
|
|
|
EX_SOFTWARE, "tls_config_set_ciphers: %s",
|
|
|
|
tls_config_error(config)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (insecure) {
|
|
|
|
tls_config_insecure_noverifycert(config);
|
|
|
|
tls_config_insecure_noverifyname(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cert) {
|
2020-02-06 07:21:04 +00:00
|
|
|
size_t len;
|
|
|
|
byte *buf = readFile(&len, cert);
|
|
|
|
error = tls_config_set_cert_mem(config, buf, len);
|
|
|
|
if (error) {
|
|
|
|
errx(
|
|
|
|
EX_CONFIG, "tls_config_set_cert_mem: %s",
|
|
|
|
tls_config_error(config)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (priv) {
|
|
|
|
free(buf);
|
|
|
|
buf = readFile(&len, priv);
|
|
|
|
}
|
|
|
|
error = tls_config_set_key_mem(config, buf, len);
|
2020-02-01 06:18:01 +00:00
|
|
|
if (error) {
|
|
|
|
errx(
|
2020-02-06 07:21:04 +00:00
|
|
|
EX_CONFIG, "tls_config_set_key_mem: %s",
|
2020-02-01 06:18:01 +00:00
|
|
|
tls_config_error(config)
|
|
|
|
);
|
|
|
|
}
|
2020-02-06 07:21:04 +00:00
|
|
|
explicit_bzero(buf, len);
|
|
|
|
free(buf);
|
2020-02-01 06:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client = tls_client();
|
|
|
|
if (!client) errx(EX_SOFTWARE, "tls_client");
|
|
|
|
|
|
|
|
error = tls_configure(client, config);
|
|
|
|
if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client));
|
|
|
|
tls_config_free(config);
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:02:37 +00:00
|
|
|
int ircConnect(const char *bindHost, const char *host, const char *port) {
|
2020-02-01 06:18:01 +00:00
|
|
|
assert(client);
|
|
|
|
|
2020-02-12 01:02:37 +00:00
|
|
|
int error;
|
|
|
|
int sock = -1;
|
2020-02-01 06:18:01 +00:00
|
|
|
struct addrinfo *head;
|
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_family = AF_UNSPEC,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_protocol = IPPROTO_TCP,
|
|
|
|
};
|
2020-02-12 01:02:37 +00:00
|
|
|
|
|
|
|
if (bindHost) {
|
|
|
|
error = getaddrinfo(bindHost, NULL, &hints, &head);
|
2020-02-17 19:50:27 +00:00
|
|
|
if (error) errx(EX_NOHOST, "%s: %s", bindHost, gai_strerror(error));
|
2020-02-12 01:02:37 +00:00
|
|
|
|
|
|
|
for (struct addrinfo *ai = head; ai; ai = ai->ai_next) {
|
|
|
|
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
|
|
|
if (sock < 0) err(EX_OSERR, "socket");
|
|
|
|
|
|
|
|
error = bind(sock, ai->ai_addr, ai->ai_addrlen);
|
|
|
|
if (!error) {
|
|
|
|
hints.ai_family = ai->ai_family;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sock);
|
|
|
|
sock = -1;
|
|
|
|
}
|
|
|
|
if (sock < 0) err(EX_UNAVAILABLE, "%s", bindHost);
|
|
|
|
freeaddrinfo(head);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = getaddrinfo(host, port, &hints, &head);
|
2020-02-01 06:18:01 +00:00
|
|
|
if (error) errx(EX_NOHOST, "%s:%s: %s", host, port, gai_strerror(error));
|
|
|
|
|
|
|
|
for (struct addrinfo *ai = head; ai; ai = ai->ai_next) {
|
2020-02-12 01:02:37 +00:00
|
|
|
if (sock < 0) {
|
|
|
|
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
|
|
|
if (sock < 0) err(EX_OSERR, "socket");
|
|
|
|
}
|
2020-02-01 06:18:01 +00:00
|
|
|
|
|
|
|
error = connect(sock, ai->ai_addr, ai->ai_addrlen);
|
|
|
|
if (!error) break;
|
|
|
|
|
|
|
|
close(sock);
|
|
|
|
sock = -1;
|
|
|
|
}
|
|
|
|
if (sock < 0) err(EX_UNAVAILABLE, "%s:%s", host, port);
|
|
|
|
freeaddrinfo(head);
|
|
|
|
|
|
|
|
error = tls_connect_socket(client, sock, host);
|
|
|
|
if (error) errx(EX_PROTOCOL, "tls_connect: %s", tls_error(client));
|
|
|
|
|
|
|
|
error = tls_handshake(client);
|
|
|
|
if (error) errx(EX_PROTOCOL, "tls_handshake: %s", tls_error(client));
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2020-02-17 04:05:43 +00:00
|
|
|
enum { MessageCap = 8191 + 512 };
|
|
|
|
|
|
|
|
static void debug(const char *pre, const char *line) {
|
2020-02-01 07:26:35 +00:00
|
|
|
if (!self.debug) return;
|
|
|
|
size_t len = strcspn(line, "\r\n");
|
2020-02-02 00:37:48 +00:00
|
|
|
uiFormat(
|
2020-02-17 04:05:43 +00:00
|
|
|
Debug, Cold, NULL, "\3%02d%s\3\t%.*s",
|
|
|
|
Gray, pre, (int)len, line
|
2020-02-02 00:37:48 +00:00
|
|
|
);
|
2020-02-01 07:26:35 +00:00
|
|
|
if (!isatty(STDERR_FILENO)) {
|
2020-02-17 04:05:43 +00:00
|
|
|
fprintf(stderr, "%s %.*s\n", pre, (int)len, line);
|
2020-02-01 07:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 06:18:01 +00:00
|
|
|
void ircSend(const char *ptr, size_t len) {
|
|
|
|
assert(client);
|
|
|
|
while (len) {
|
|
|
|
ssize_t ret = tls_write(client, ptr, len);
|
|
|
|
if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue;
|
|
|
|
if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(client));
|
|
|
|
ptr += ret;
|
|
|
|
len -= ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ircFormat(const char *format, ...) {
|
2020-02-17 04:05:43 +00:00
|
|
|
char buf[MessageCap];
|
2020-02-01 06:18:01 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
int len = vsnprintf(buf, sizeof(buf), format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
assert((size_t)len < sizeof(buf));
|
2020-02-17 04:05:43 +00:00
|
|
|
debug("<<", buf);
|
2020-02-01 06:18:01 +00:00
|
|
|
ircSend(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *TagNames[TagCap] = {
|
|
|
|
#define X(name, id) [id] = name,
|
|
|
|
ENUM_TAG
|
|
|
|
#undef X
|
|
|
|
};
|
|
|
|
|
|
|
|
static void unescape(char *tag) {
|
|
|
|
for (;;) {
|
|
|
|
tag = strchr(tag, '\\');
|
|
|
|
if (!tag) break;
|
|
|
|
switch (tag[1]) {
|
|
|
|
break; case ':': tag[1] = ';';
|
|
|
|
break; case 's': tag[1] = ' ';
|
|
|
|
break; case 'r': tag[1] = '\r';
|
|
|
|
break; case 'n': tag[1] = '\n';
|
|
|
|
}
|
|
|
|
memmove(tag, &tag[1], strlen(&tag[1]) + 1);
|
|
|
|
if (tag[0]) tag = &tag[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct Message parse(char *line) {
|
|
|
|
struct Message msg = { .cmd = NULL };
|
|
|
|
|
|
|
|
if (line[0] == '@') {
|
|
|
|
char *tags = 1 + strsep(&line, " ");
|
|
|
|
while (tags) {
|
|
|
|
char *tag = strsep(&tags, ";");
|
|
|
|
char *key = strsep(&tag, "=");
|
2020-02-16 03:19:55 +00:00
|
|
|
for (uint i = 0; i < TagCap; ++i) {
|
2020-02-01 06:18:01 +00:00
|
|
|
if (strcmp(key, TagNames[i])) continue;
|
|
|
|
unescape(tag);
|
|
|
|
msg.tags[i] = tag;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] == ':') {
|
|
|
|
char *origin = 1 + strsep(&line, " ");
|
|
|
|
msg.nick = strsep(&origin, "!");
|
|
|
|
msg.user = strsep(&origin, "@");
|
|
|
|
msg.host = origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.cmd = strsep(&line, " ");
|
2020-02-16 03:19:55 +00:00
|
|
|
for (uint i = 0; line && i < ParamCap; ++i) {
|
2020-02-01 06:18:01 +00:00
|
|
|
if (line[0] == ':') {
|
|
|
|
msg.params[i] = &line[1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
msg.params[i] = strsep(&line, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ircRecv(void) {
|
2020-02-17 04:05:43 +00:00
|
|
|
static char buf[MessageCap];
|
2020-02-01 06:18:01 +00:00
|
|
|
static size_t len = 0;
|
|
|
|
|
|
|
|
assert(client);
|
|
|
|
ssize_t ret = tls_read(client, &buf[len], sizeof(buf) - len);
|
|
|
|
if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) return;
|
|
|
|
if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(client));
|
|
|
|
if (!ret) errx(EX_PROTOCOL, "server closed connection");
|
|
|
|
len += ret;
|
|
|
|
|
|
|
|
char *crlf;
|
|
|
|
char *line = buf;
|
|
|
|
for (;;) {
|
|
|
|
crlf = memmem(line, &buf[len] - line, "\r\n", 2);
|
|
|
|
if (!crlf) break;
|
|
|
|
*crlf = '\0';
|
2020-02-17 04:05:43 +00:00
|
|
|
debug(">>", line);
|
2020-06-24 17:36:24 +00:00
|
|
|
struct Message msg = parse(line);
|
|
|
|
handle(&msg);
|
2020-02-01 06:18:01 +00:00
|
|
|
line = crlf + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= line - buf;
|
|
|
|
memmove(buf, line, len);
|
|
|
|
}
|
2020-02-13 06:01:23 +00:00
|
|
|
|
|
|
|
void ircClose(void) {
|
2020-07-23 18:20:29 +00:00
|
|
|
tls_close(client);
|
2020-02-13 06:01:23 +00:00
|
|
|
tls_free(client);
|
|
|
|
}
|