check permissions for server.json

This commit is contained in:
badaix 2017-07-01 11:15:37 +02:00
parent f6209d7dce
commit 2955b20e9d
7 changed files with 230 additions and 163 deletions

View file

@ -19,6 +19,7 @@
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fstream>
#include <cerrno>
#include "common/snapException.h"
@ -34,13 +35,30 @@ Config::Config()
if (getenv("HOME") == NULL)
dir = "/var/lib/snapserver/";
else
dir = getenv("HOME") + string("/.config/snapserver/");
dir = getenv("HOME");
if (!dir.empty() && (dir.back() != '/'))
dir += "/";
if (dir.find("/var/lib/snapserver") == string::npos)
dir += ".config/snapserver/";
int status = mkdirRecursive(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if ((status != 0) && (errno != EEXIST))
throw SnapException("failed to create settings directory: \"" + dir + "\": " + cpt::to_string(errno));
filename_ = dir + "server.json";
logO << "Settings file: " << filename_ << "\n";
logS(kLogNotice) << "Settings file: \"" << filename_ << "\"\n";
int fd;
if ((fd = open(filename_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
{
if (errno == EACCES)
throw std::runtime_error("failed to open file \"" + filename_ + "\", permission denied");
else
throw std::runtime_error("failed to open file \"" + filename_ + "\"");
}
close(fd);
try
{