privfile: common sk/pk file creation/saving code
This commit is contained in:
parent
7ec0823834
commit
4f2680134e
|
@ -283,6 +283,8 @@ static std::string get_user_dir()
|
|||
return "." CCR_CONFDIR; //fallback for absolutely desolate systems
|
||||
}
|
||||
|
||||
#include "privfile.h"
|
||||
#include <fstream>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/file.h>
|
||||
|
@ -290,8 +292,6 @@ static std::string get_user_dir()
|
|||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
/*
|
||||
* prepares the user directory with empty files and similar stuff.
|
||||
*
|
||||
|
@ -302,33 +302,12 @@ static std::string get_user_dir()
|
|||
static bool ensure_empty_sencode_file (const std::string&fn,
|
||||
const std::string&ident)
|
||||
{
|
||||
struct stat st;
|
||||
if (stat (fn.c_str(), &st)) {
|
||||
if (errno != ENOENT)
|
||||
return false;
|
||||
sencode_list l;
|
||||
sencode_bytes b (ident);
|
||||
l.items.push_back (&b);
|
||||
std::string emptyfile = l.encode();
|
||||
|
||||
//if it simply doesn't exist, create it
|
||||
sencode_list l;
|
||||
sencode_bytes b (ident);
|
||||
l.items.push_back (&b);
|
||||
std::string emptyfile = l.encode();
|
||||
|
||||
int fd;
|
||||
fd = creat (fn.c_str(), S_IRUSR | S_IWUSR);
|
||||
if (fd < 0) return false;
|
||||
ssize_t res = write (fd, emptyfile.c_str(),
|
||||
emptyfile.length());
|
||||
if (close (fd)) return false;
|
||||
if ( (size_t) res != emptyfile.length()) return false;
|
||||
|
||||
} else {
|
||||
if (!S_ISREG (st.st_mode))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (access (fn.c_str(), R_OK | W_OK)) return false;
|
||||
|
||||
return true;
|
||||
return put_private_file (fn, emptyfile, true);
|
||||
}
|
||||
|
||||
static bool prepare_user_dir (const std::string&dir)
|
||||
|
|
|
@ -20,10 +20,11 @@
|
|||
|
||||
#include "symkey.h"
|
||||
|
||||
#include "sc.h"
|
||||
#include "hash.h"
|
||||
#include "str_match.h"
|
||||
#include "iohelpers.h"
|
||||
#include "privfile.h"
|
||||
#include "sc.h"
|
||||
#include "str_match.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
|
@ -176,14 +177,6 @@ bool symkey::save (const std::string&fn, const std::string&withlock,
|
|||
data = tmp;
|
||||
}
|
||||
|
||||
std::ofstream sk_out;
|
||||
sk_out.open (fn == "-" ? "/dev/stdout" : fn.c_str(),
|
||||
std::ios::out | std::ios::binary);
|
||||
if (!sk_out) {
|
||||
err ("error: can't open symkey file for writing");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (armor) {
|
||||
std::vector<std::string> parts;
|
||||
parts.resize (1);
|
||||
|
@ -191,18 +184,13 @@ bool symkey::save (const std::string&fn, const std::string&withlock,
|
|||
data = envelope_format (ENVELOPE_SYMKEY, parts, r);
|
||||
}
|
||||
|
||||
sk_out << data;
|
||||
if (!sk_out.good()) {
|
||||
bool to_stdout = (fn == "-");
|
||||
if (!put_private_file (to_stdout ? "/dev/stdout" : fn,
|
||||
data, !to_stdout)) {
|
||||
err ("error: can't write to symkey file");
|
||||
return false;
|
||||
}
|
||||
|
||||
sk_out.close();
|
||||
if (!sk_out.good()) {
|
||||
err ("error: couldn't close symkey file");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue