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
|
return "." CCR_CONFDIR; //fallback for absolutely desolate systems
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "privfile.h"
|
||||||
|
#include <fstream>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
|
@ -290,8 +292,6 @@ static std::string get_user_dir()
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* prepares the user directory with empty files and similar stuff.
|
* 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,
|
static bool ensure_empty_sencode_file (const std::string&fn,
|
||||||
const std::string&ident)
|
const std::string&ident)
|
||||||
{
|
{
|
||||||
struct stat st;
|
sencode_list l;
|
||||||
if (stat (fn.c_str(), &st)) {
|
sencode_bytes b (ident);
|
||||||
if (errno != ENOENT)
|
l.items.push_back (&b);
|
||||||
return false;
|
std::string emptyfile = l.encode();
|
||||||
|
|
||||||
//if it simply doesn't exist, create it
|
return put_private_file (fn, emptyfile, true);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool prepare_user_dir (const std::string&dir)
|
static bool prepare_user_dir (const std::string&dir)
|
||||||
|
|
|
@ -20,10 +20,11 @@
|
||||||
|
|
||||||
#include "symkey.h"
|
#include "symkey.h"
|
||||||
|
|
||||||
#include "sc.h"
|
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "str_match.h"
|
|
||||||
#include "iohelpers.h"
|
#include "iohelpers.h"
|
||||||
|
#include "privfile.h"
|
||||||
|
#include "sc.h"
|
||||||
|
#include "str_match.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
@ -176,14 +177,6 @@ bool symkey::save (const std::string&fn, const std::string&withlock,
|
||||||
data = tmp;
|
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) {
|
if (armor) {
|
||||||
std::vector<std::string> parts;
|
std::vector<std::string> parts;
|
||||||
parts.resize (1);
|
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);
|
data = envelope_format (ENVELOPE_SYMKEY, parts, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
sk_out << data;
|
bool to_stdout = (fn == "-");
|
||||||
if (!sk_out.good()) {
|
if (!put_private_file (to_stdout ? "/dev/stdout" : fn,
|
||||||
|
data, !to_stdout)) {
|
||||||
err ("error: can't write to symkey file");
|
err ("error: can't write to symkey file");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
sk_out.close();
|
|
||||||
if (!sk_out.good()) {
|
|
||||||
err ("error: couldn't close symkey file");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue