Skip to content
This repository has been archived by the owner on Dec 1, 2018. It is now read-only.

Commit

Permalink
Initial backing-up of certificates.
Browse files Browse the repository at this point in the history
Suggested by #12
and #9
  • Loading branch information
kristaps committed Jul 28, 2016
1 parent b7910db commit 6dbba42
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ int chngproc(int, const char *, int);
int dnsproc(int);
int revokeproc(int, const char *,
int, int, const char *const *, size_t);
int fileproc(int, const char *);
int fileproc(int, int, const char *);
int keyproc(int, const char *,
const char **, size_t, int);
int netproc(int, int, int, int, int, int, int, int, int,
Expand Down
45 changes: 44 additions & 1 deletion fileproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "extern.h"
Expand Down Expand Up @@ -64,13 +66,15 @@ serialise(const char *tmp, const char *real,
}

int
fileproc(int certsock, const char *certdir)
fileproc(int certsock, int backup, const char *certdir)
{
char *csr, *ch;
size_t chsz, csz;
int rc;
long lval;
enum fileop op;
time_t t;
char file[PATH_MAX];

csr = ch = NULL;
rc = 0;
Expand Down Expand Up @@ -99,6 +103,45 @@ fileproc(int certsock, const char *certdir)
warnx("unknown operation from certproc");
goto out;
}

/*
* If we're backing up, then copy all files (found) by linking
* them to the file followed by the epoch in seconds.
* If we're going to remove, the unlink(2) will cause the
* original to go away.
* If we're going to update, the rename(2) will replace the
* certificate, leaving the backup as the only one.
*/

if (backup) {
t = time(NULL);
snprintf(file, sizeof(file),
"cert-%llu.pem", (unsigned long long)t);
if (-1 == link(CERT_PEM, file) && ENOENT != errno) {
warnx("%s/%s", certdir, CERT_PEM);
goto out;
} else
dodbg("%s/%s: linked to %s",
certdir, CERT_PEM, file);

snprintf(file, sizeof(file),
"chain-%llu.pem", (unsigned long long)t);
if (-1 == link(CHAIN_PEM, file) && ENOENT != errno) {
warnx("%s/%s", certdir, CHAIN_PEM);
goto out;
} else
dodbg("%s/%s: linked to %s",
certdir, CHAIN_PEM, file);

snprintf(file, sizeof(file),
"fullchain-%llu.pem", (unsigned long long)t);
if (-1 == link(FCHAIN_PEM, file) && ENOENT != errno) {
warnx("%s/%s", certdir, FCHAIN_PEM);
goto out;
} else
dodbg("%s/%s: linked to %s",
certdir, FCHAIN_PEM, file);
}

/*
* If revoking certificates, just unlink the files.
Expand Down
11 changes: 7 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,22 @@ main(int argc, char *argv[])
rvk_fds[2];
pid_t pids[COMP__MAX];
int c, rc, newacct, remote, revoke, force,
staging, multidir, newkey;
staging, multidir, newkey, backup;
extern int verbose;
extern enum comp proccomp;
size_t i, altsz, ne;
const char **alts;

alts = NULL;
newacct = remote = revoke = verbose = force =
multidir = staging = newkey = 0;
multidir = staging = newkey = backup = 0;
certdir = keyfile = acctkey = chngdir = NULL;

while (-1 != (c = getopt(argc, argv, "FmnNrstvf:c:C:k:")))
while (-1 != (c = getopt(argc, argv, "bFmnNrstvf:c:C:k:")))
switch (c) {
case ('b'):
backup = 1;
break;
case ('c'):
free(certdir);
if (NULL == (certdir = strdup(optarg)))
Expand Down Expand Up @@ -383,7 +386,7 @@ main(int argc, char *argv[])
free(alts);
close(dns_fds[0]);
close(rvk_fds[0]);
c = fileproc(file_fds[1], certdir);
c = fileproc(file_fds[1], backup, certdir);
/*
* This is different from the other processes in that it
* can return 2 if the certificates were updated.
Expand Down

0 comments on commit 6dbba42

Please sign in to comment.