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
|
2020-08-04 16:19:14 +00:00
|
|
|
* combining it with OpenSSL (or a modified version of that library),
|
2020-06-08 21:48:07 +00:00
|
|
|
* 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
|
2020-08-04 16:19:14 +00:00
|
|
|
* source code for the parts of OpenSSL used as well as that of the
|
2020-06-08 21:48:07 +00:00
|
|
|
* covered work.
|
2020-02-01 06:18:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
2021-06-20 22:18:33 +00:00
|
|
|
#include <errno.h>
|
2021-06-24 21:38:18 +00:00
|
|
|
#include <fcntl.h>
|
2020-02-01 06:18:01 +00:00
|
|
|
#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;
|
|
|
|
|
2021-01-10 00:11:57 +00:00
|
|
|
void ircConfig(
|
|
|
|
bool insecure, const char *trust, const char *cert, const char *priv
|
|
|
|
) {
|
2020-02-01 06:18:01 +00:00
|
|
|
struct tls_config *config = tls_config_new();
|
|
|
|
if (!config) errx(EX_SOFTWARE, "tls_config_new");
|
|
|
|
|
2021-06-20 14:42:10 +00:00
|
|
|
int error;
|
2020-02-01 06:18:01 +00:00
|
|
|
if (insecure) {
|
|
|
|
tls_config_insecure_noverifycert(config);
|
|
|
|
tls_config_insecure_noverifyname(config);
|
|
|
|
}
|
2021-01-10 00:11:57 +00:00
|
|
|
if (trust) {
|
|
|
|
tls_config_insecure_noverifyname(config);
|
|
|
|
const char *dirs = NULL;
|
|
|
|
for (const char *path; NULL != (path = configPath(&dirs, trust));) {
|
|
|
|
error = tls_config_set_ca_file(config, path);
|
|
|
|
if (!error) break;
|
|
|
|
}
|
|
|
|
if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config));
|
|
|
|
}
|
2020-02-01 06:18:01 +00:00
|
|
|
|
2021-06-10 19:23:33 +00:00
|
|
|
// Explicitly load the default CA cert file on OpenBSD now so it doesn't
|
|
|
|
// need to be unveiled. Other systems might use a CA directory, so avoid
|
|
|
|
// changing the default behavior.
|
|
|
|
#ifdef __OpenBSD__
|
2021-06-10 01:32:09 +00:00
|
|
|
if (!insecure && !trust) {
|
|
|
|
const char *ca = tls_default_ca_cert_file();
|
|
|
|
error = tls_config_set_ca_file(config, ca);
|
|
|
|
if (error) errx(EX_OSFILE, "%s: %s", ca, tls_config_error(config));
|
|
|
|
}
|
2021-06-10 19:23:33 +00:00
|
|
|
#endif
|
2021-06-10 01:32:09 +00:00
|
|
|
|
2020-02-01 06:18:01 +00:00
|
|
|
if (cert) {
|
2020-08-21 20:47:54 +00:00
|
|
|
const char *dirs = NULL;
|
|
|
|
for (const char *path; NULL != (path = configPath(&dirs, cert));) {
|
2020-08-20 18:56:13 +00:00
|
|
|
if (priv) {
|
|
|
|
error = tls_config_set_cert_file(config, path);
|
|
|
|
} else {
|
|
|
|
error = tls_config_set_keypair_file(config, path, path);
|
|
|
|
}
|
|
|
|
if (!error) break;
|
2020-02-06 07:21:04 +00:00
|
|
|
}
|
2020-08-20 18:56:13 +00:00
|
|
|
if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config));
|
|
|
|
}
|
|
|
|
if (priv) {
|
2020-08-21 20:47:54 +00:00
|
|
|
const char *dirs = NULL;
|
|
|
|
for (const char *path; NULL != (path = configPath(&dirs, priv));) {
|
2020-08-20 18:56:13 +00:00
|
|
|
error = tls_config_set_key_file(config, path);
|
|
|
|
if (!error) break;
|
2020-02-01 06:18:01 +00:00
|
|
|
}
|
2020-08-20 18:56:13 +00:00
|
|
|
if (error) errx(EX_NOINPUT, "%s: %s", priv, tls_config_error(config));
|
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;
|
2021-06-20 22:18:33 +00:00
|
|
|
if (error && errno == EINTR) break; // connect continues asynchronously
|
2020-02-01 06:18:01 +00:00
|
|
|
|
|
|
|
close(sock);
|
|
|
|
sock = -1;
|
|
|
|
}
|
|
|
|
if (sock < 0) err(EX_UNAVAILABLE, "%s:%s", host, port);
|
|
|
|
freeaddrinfo(head);
|
|
|
|
|
2021-06-24 21:38:18 +00:00
|
|
|
fcntl(sock, F_SETFD, FD_CLOEXEC);
|
2020-02-01 06:18:01 +00:00
|
|
|
error = tls_connect_socket(client, sock, host);
|
|
|
|
if (error) errx(EX_PROTOCOL, "tls_connect: %s", tls_error(client));
|
|
|
|
|
2021-06-20 20:48:53 +00:00
|
|
|
do {
|
|
|
|
error = tls_handshake(client);
|
|
|
|
} while (error == TLS_WANT_POLLIN || error == TLS_WANT_POLLOUT);
|
2020-02-01 06:18:01 +00:00
|
|
|
if (error) errx(EX_PROTOCOL, "tls_handshake: %s", tls_error(client));
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2021-01-10 23:00:41 +00:00
|
|
|
void ircPrintCert(void) {
|
2021-01-10 00:11:57 +00:00
|
|
|
size_t len;
|
|
|
|
const byte *pem = tls_peer_cert_chain_pem(client, &len);
|
2021-01-10 23:00:41 +00:00
|
|
|
printf("subject= %s\n", tls_peer_cert_subject(client));
|
|
|
|
fwrite(pem, len, 1, stdout);
|
2021-01-10 00:11:57 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|