Skip to content

Commit 03221b1

Browse files
committed
certctl: Create output directories
In a pkgbase world, we cannot assume that these directories exist; we must create them ourselves. Fixes: c340ef2 ("certctl: Reimplement in C") Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D52121
1 parent 7d6b5a9 commit 03221b1

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

usr.sbin/certctl/certctl.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ static char *bundle_dest;
100100

101101
static FILE *mlf;
102102

103+
/*
104+
* Create a directory and its parents as needed.
105+
*/
106+
static void
107+
mkdirp(const char *dir)
108+
{
109+
struct stat sb;
110+
const char *sep;
111+
char *parent;
112+
113+
if (stat(dir, &sb) == 0)
114+
return;
115+
if ((sep = strrchr(dir, '/')) != NULL) {
116+
parent = xasprintf("%.*s", (int)(sep - dir), dir);
117+
mkdirp(parent);
118+
free(parent);
119+
}
120+
info("creating %s", dir);
121+
if (mkdir(dir, 0755) != 0)
122+
err(1, "mkdir %s", dir);
123+
}
124+
103125
/*
104126
* Remove duplicate and trailing slashes from a path.
105127
*/
@@ -685,7 +707,7 @@ save_trusted(void)
685707
{
686708
int ret;
687709

688-
/* save untrusted certs */
710+
mkdirp(trusted_dest);
689711
ret = write_certs(trusted_dest, &trusted);
690712
return (ret);
691713
}
@@ -700,6 +722,7 @@ save_untrusted(void)
700722
{
701723
int ret;
702724

725+
mkdirp(untrusted_dest);
703726
ret = write_certs(untrusted_dest, &untrusted);
704727
return (ret);
705728
}
@@ -721,6 +744,7 @@ save_bundle(void)
721744
} else {
722745
dir = xasprintf("%.*s", (int)(sep - bundle_dest), bundle_dest);
723746
file = sep + 1;
747+
mkdirp(dir);
724748
}
725749
ret = write_bundle(dir, file, &trusted);
726750
free(dir);
@@ -995,17 +1019,17 @@ set_defaults(void)
9951019

9961020
if ((value = getenv("TRUSTDESTDIR")) != NULL ||
9971021
(value = getenv("CERTDESTDIR")) != NULL)
998-
trusted_dest = xstrdup(value);
1022+
trusted_dest = normalize_path(value);
9991023
else
10001024
trusted_dest = expand_path(TRUSTED_PATH);
10011025

10021026
if ((value = getenv("UNTRUSTDESTDIR")) != NULL)
1003-
untrusted_dest = xstrdup(value);
1027+
untrusted_dest = normalize_path(value);
10041028
else
10051029
untrusted_dest = expand_path(UNTRUSTED_PATH);
10061030

10071031
if ((value = getenv("BUNDLE")) != NULL)
1008-
bundle_dest = xstrdup(value);
1032+
bundle_dest = normalize_path(value);
10091033
else
10101034
bundle_dest = expand_path(BUNDLE_PATH);
10111035

usr.sbin/certctl/tests/certctl_test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ certctl_setup()
7676
mkdir -p ${DESTDIR}${DISTBASE}/usr/share/certs/untrusted
7777
mkdir -p ${DESTDIR}/usr/local/share/certs
7878

79-
# Create output directories
80-
mkdir -p ${DESTDIR}${DISTBASE}/etc/ssl/certs
81-
mkdir -p ${DESTDIR}${DISTBASE}/etc/ssl/untrusted
79+
# Do not create output directories; certctl will take care of it
80+
#mkdir -p ${DESTDIR}${DISTBASE}/etc/ssl/certs
81+
#mkdir -p ${DESTDIR}${DISTBASE}/etc/ssl/untrusted
8282

8383
# Generate a random key
8484
keyname="testkey"

0 commit comments

Comments
 (0)