about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-02-06 02:21:04 -0500
committerJune McEnroe <june@causal.agency>2020-02-06 02:21:04 -0500
commit8b3bf897c2b7a14ff6a4c096b9969eaeb695a9e0 (patch)
treeab4bfc7820e081da7ca18a1605d332d7185aed6e
parentHandle TOPIC and replies (diff)
downloadcatgirl-8b3bf897c2b7a14ff6a4c096b9969eaeb695a9e0.tar.gz
catgirl-8b3bf897c2b7a14ff6a4c096b9969eaeb695a9e0.zip
Search for cert and priv in config dirs
-rw-r--r--chat.c14
-rw-r--r--chat.h2
-rw-r--r--irc.c37
3 files changed, 48 insertions, 5 deletions
diff --git a/chat.c b/chat.c
index 115fe38..c487722 100644
--- a/chat.c
+++ b/chat.c
@@ -111,7 +111,19 @@ int main(int argc, char *argv[]) {
 	set(&self.chanTypes, "#&");
 	set(&self.prefixes, "@+");
 
-	ircConfig(insecure, cert, priv);
+	FILE *certFile = NULL;
+	FILE *privFile = NULL;
+	if (cert) {
+		certFile = configOpen(cert, "r");
+		if (!certFile) err(EX_NOINPUT, "%s", cert);
+	}
+	if (priv) {
+		privFile = configOpen(priv, "r");
+		if (!privFile) err(EX_NOINPUT, "%s", priv);
+	}
+	ircConfig(insecure, certFile, privFile);
+	if (certFile) fclose(certFile);
+	if (privFile) fclose(privFile);
 
 	uiInit();
 	uiShowID(Network);
diff --git a/chat.h b/chat.h
index 57d4ba6..112530d 100644
--- a/chat.h
+++ b/chat.h
@@ -105,7 +105,7 @@ struct Message {
 	char *params[ParamCap];
 };
 
-void ircConfig(bool insecure, const char *cert, const char *priv);
+void ircConfig(bool insecure, FILE *cert, FILE *priv);
 int ircConnect(const char *host, const char *port);
 void ircRecv(void);
 void ircSend(const char *ptr, size_t len);
diff --git a/irc.c b/irc.c
index 2d6f00b..05f8f9d 100644
--- a/irc.c
+++ b/irc.c
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sysexits.h>
 #include <tls.h>
 #include <unistd.h>
@@ -31,7 +32,22 @@
 
 struct tls *client;
 
-void ircConfig(bool insecure, const char *cert, const char *priv) {
+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) {
 	struct tls_config *config = tls_config_new();
 	if (!config) errx(EX_SOFTWARE, "tls_config_new");
 
@@ -49,13 +65,28 @@ void ircConfig(bool insecure, const char *cert, const char *priv) {
 	}
 
 	if (cert) {
-		error = tls_config_set_keypair_file(config, cert, (priv ? priv : cert));
+		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);
 		if (error) {
 			errx(
-				EX_SOFTWARE, "tls_config_set_keypair_file: %s",
+				EX_CONFIG, "tls_config_set_key_mem: %s",
 				tls_config_error(config)
 			);
 		}
+		explicit_bzero(buf, len);
+		free(buf);
 	}
 
 	client = tls_client();