diff --git a/include/util.h b/include/util.h
index 8ec9ac5d94092a75813a3e083140fbb88079c29d..6c241f41a175c3d27390ecb6ca5b041bd637cb2b 100644
--- a/include/util.h
+++ b/include/util.h
@@ -9,6 +9,7 @@ const char *path;
};
char *getpath(const struct pathspec *paths, size_t npaths);
+void posix_dirname(char *path, char *dname);
int mkdirs(char *path, mode_t mode);
int download_resp(FILE *out, struct gemini_response resp, const char *path,
char *url);
diff --git a/src/gmnlm.c b/src/gmnlm.c
index 12a6c971b7ae7d18bcdafc3b2b81f55539340095..0ab21849e186a99c84d0f94c59ef6c821e91b97d 100644
--- a/src/gmnlm.c
+++ b/src/gmnlm.c
@@ -562,6 +562,13 @@ assert(host);
n = snprintf(certpath, sizeof(certpath), path_fmt, host, "crt");
assert(n < sizeof(certpath));
n = snprintf(keypath, sizeof(keypath), path_fmt, host, "key");
+ char dname[PATH_MAX + 1];
+ posix_dirname(certpath, dname);
+ if (mkdirs(dname, 0755) != 0) {
+ fprintf(stderr, "Error creating directory %s: %s\n",
+ dname, strerror(errno));
+ break;
+ }
assert(n < sizeof(keypath));
fprintf(stderr, "The server requested a client certificate.\n"
"Presently, this process is not automated.\n"
diff --git a/src/util.c b/src/util.c
index 1cb0bf42b6319e6bd1b0cf0cc94e28627e4f9c68..8441b584ff199ffd81472539a59acce5d0a18f42 100644
--- a/src/util.c
+++ b/src/util.c
@@ -11,7 +11,7 @@ #include <string.h>
#include <sys/stat.h>
#include "util.h"
-static void
+void
posix_dirname(char *path, char *dname)
{
char p[PATH_MAX+1];