Skip to content

Commit

Permalink
Merge pull request #566 from nomis/umask
Browse files Browse the repository at this point in the history
Add "objectstore.umask" configuration option for file/directory creation
  • Loading branch information
halderen authored Jan 19, 2021
2 parents 378b5b8 + de5bb8a commit 8f5e4e4
Show file tree
Hide file tree
Showing 40 changed files with 396 additions and 219 deletions.
5 changes: 5 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ AC_DEFINE_UNQUOTED(
["$softhsmtokendir"],
[The default location of the token directory]
)
AC_DEFINE_UNQUOTED(
[DEFAULT_UMASK],
[0077],
[The default file mode creation mask]
)
AC_DEFINE_UNQUOTED(
[DEFAULT_OBJECTSTORE_BACKEND],
["file"],
Expand Down
7 changes: 4 additions & 3 deletions src/bin/util/softhsm2-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,9 @@ bool deleteToken(char* serial, char* token)
bool rv = true;
std::string basedir = Configuration::i()->getString("directories.tokendir", DEFAULT_TOKENDIR);
std::string tokendir;
int umask = Configuration::i()->getInt("objectstore.umask", DEFAULT_UMASK);

rv = findTokenDirectory(basedir, tokendir, serial, token);
rv = findTokenDirectory(basedir, tokendir, umask, serial, token);

if (rv)
{
Expand Down Expand Up @@ -634,7 +635,7 @@ void finalizeSoftHSM()
}

// Find the token directory
bool findTokenDirectory(std::string basedir, std::string& tokendir, char* serial, char* label)
bool findTokenDirectory(std::string basedir, std::string& tokendir, int umask, char* serial, char* label)
{
if (serial == NULL && label == NULL)
{
Expand Down Expand Up @@ -693,7 +694,7 @@ bool findTokenDirectory(std::string basedir, std::string& tokendir, char* serial
memset(paddedTokenLabel, ' ', sizeof(paddedTokenLabel));

// Create a token instance
ObjectStoreToken* token = ObjectStoreToken::accessToken(basedir, *i);
ObjectStoreToken* token = ObjectStoreToken::accessToken(basedir, *i, umask);

if (!token->isValid())
{
Expand Down
2 changes: 1 addition & 1 deletion src/bin/util/softhsm2-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void usage();
bool checkSetup();
int initToken(CK_SLOT_ID slotID, char* label, char* soPIN, char* userPIN);
bool deleteToken(char* serial, char* token);
bool findTokenDirectory(std::string basedir, std::string& tokendir, char* serial, char* label);
bool findTokenDirectory(std::string basedir, std::string& tokendir, int umask, char* serial, char* label);
bool rmdir(std::string path);
bool rm(std::string path);
int showSlots();
Expand Down
3 changes: 2 additions & 1 deletion src/lib/SoftHSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,8 @@ CK_RV SoftHSM::C_Initialize(CK_VOID_PTR pInitArgs)
sessionObjectStore = new SessionObjectStore();

// Load the object store
objectStore = new ObjectStore(Configuration::i()->getString("directories.tokendir", DEFAULT_TOKENDIR));
objectStore = new ObjectStore(Configuration::i()->getString("directories.tokendir", DEFAULT_TOKENDIR),
Configuration::i()->getInt("objectstore.umask", DEFAULT_UMASK));
if (!objectStore->isValid())
{
WARNING_MSG("Could not load the object store");
Expand Down
10 changes: 9 additions & 1 deletion src/lib/common/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ std::auto_ptr<Configuration> Configuration::instance(NULL);
const struct config Configuration::valid_config[] = {
{ "directories.tokendir", CONFIG_TYPE_STRING },
{ "objectstore.backend", CONFIG_TYPE_STRING },
{ "objectstore.umask", CONFIG_TYPE_INT_OCTAL },
{ "log.level", CONFIG_TYPE_STRING },
{ "slots.removable", CONFIG_TYPE_BOOL },
{ "slots.mechanisms", CONFIG_TYPE_STRING },
Expand Down Expand Up @@ -107,7 +108,14 @@ int Configuration::getInt(std::string key, int ifEmpty /* = 0 */)
}
else
{
WARNING_MSG("Missing %s in configuration. Using default value: %i", key.c_str(), ifEmpty);
if (getType(key) == CONFIG_TYPE_INT_OCTAL)
{
WARNING_MSG("Missing %s in configuration. Using default value: 0%o", key.c_str(), ifEmpty);
}
else
{
WARNING_MSG("Missing %s in configuration. Using default value: %i", key.c_str(), ifEmpty);
}
return ifEmpty;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/common/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum
CONFIG_TYPE_UNSUPPORTED,
CONFIG_TYPE_STRING,
CONFIG_TYPE_INT,
CONFIG_TYPE_INT_OCTAL,
CONFIG_TYPE_BOOL
};

Expand Down
3 changes: 3 additions & 0 deletions src/lib/common/SimpleConfigLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ bool SimpleConfigLoader::loadConfiguration()
case CONFIG_TYPE_INT:
Configuration::i()->setInt(stringName, atoi(stringValue.c_str()));
break;
case CONFIG_TYPE_INT_OCTAL:
Configuration::i()->setInt(stringName, strtol(stringValue.c_str(), NULL, 8));
break;
case CONFIG_TYPE_BOOL:
bool boolValue;
if (string2bool(stringValue, &boolValue))
Expand Down
10 changes: 10 additions & 0 deletions src/lib/common/softhsm2.conf.5.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ objectstore.backend = file
.fi
.RE
.LP
.SH OBJECTSTORE.UMASK
The file mode creation mask used by SoftHSM when creating files or directories. This value is in octal.
This is applied in addition to the process umask and cannot override it.
.LP
.RS
.nf
objectstore.umask = 0077
.fi
.RE
.LP
.SH LOG.LEVEL
The log level which can be set to ERROR, WARNING, INFO or DEBUG.
.LP
Expand Down
1 change: 1 addition & 0 deletions src/lib/common/softhsm2.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

directories.tokendir = @softhsmtokendir@
objectstore.backend = file
objectstore.umask = 0077

# ERROR, WARNING, INFO, DEBUG
log.level = ERROR
Expand Down
9 changes: 5 additions & 4 deletions src/lib/object_store/DB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ bool DB::Result::nextRow()
* Connection
**************************/

DB::Connection *DB::Connection::Create(const std::string &dbdir, const std::string &dbname)
DB::Connection *DB::Connection::Create(const std::string &dbdir, const std::string &dbname, int umask)
{
if (dbdir.length() == 0) {
DB::logError("Connection::Create: database directory parameter dbdir is empty");
Expand All @@ -716,13 +716,14 @@ DB::Connection *DB::Connection::Create(const std::string &dbdir, const std::stri
return NULL;
}

return new Connection(dbdir,dbname);
return new Connection(dbdir, dbname, umask);
}

DB::Connection::Connection(const std::string &dbdir, const std::string &dbname)
DB::Connection::Connection(const std::string &dbdir, const std::string &dbname, int umask)
: _dbdir(dbdir)
, _dbpath(dbdir + OS_PATHSEP + dbname)
, _db(NULL)
, _umask(umask)
{
}

Expand Down Expand Up @@ -815,7 +816,7 @@ bool DB::Connection::connect(const char *
)
{
// Create and set file permissions if the DB does not exist.
int fd = open(_dbpath.c_str(), O_CREAT, S_IRUSR | S_IWUSR);
int fd = open(_dbpath.c_str(), O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) & ~_umask);
if (fd == -1)
{
DB::logError("Could not open database: %s (errno %i)",
Expand Down
5 changes: 3 additions & 2 deletions src/lib/object_store/DB.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Result : public Statement {
// Responsible for connection to the database and for managing prepared statements.
class Connection {
public:
static Connection *Create(const std::string &dbdir, const std::string &dbname);
static Connection *Create(const std::string &dbdir, const std::string &dbname, int umask);
virtual ~Connection();

// value that was passed into dbdir when this connection was created.
Expand Down Expand Up @@ -176,8 +176,9 @@ class Connection {
std::string _dbdir;
std::string _dbpath;
sqlite3 *_db;
int _umask;

Connection(const std::string &dbdir, const std::string &dbname);
Connection(const std::string &dbdir, const std::string &dbname, int umask);

// disable evil constructors
Connection(const Connection &);
Expand Down
24 changes: 14 additions & 10 deletions src/lib/object_store/DBToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const char * const DBTOKEN_FILE = "sqlite3.db";
const long long DBTOKEN_OBJECT_TOKENINFO = 1;

// Constructor for creating a new token.
DBToken::DBToken(const std::string &baseDir, const std::string &tokenName, const ByteString &label, const ByteString &serial)
DBToken::DBToken(const std::string &baseDir, const std::string &tokenName, int umask, const ByteString &label, const ByteString &serial)
: _connection(NULL), _tokenMutex(NULL)
{
std::string tokenDir = baseDir + OS_PATHSEP + tokenName;
Expand All @@ -75,7 +75,11 @@ DBToken::DBToken(const std::string &baseDir, const std::string &tokenName, const
}

// First create the directory for the token, we expect basePath to already exist
if (mkdir(tokenDir.c_str(), S_IFDIR | S_IRWXU))
#ifndef _WIN32
if (::mkdir(tokenDir.c_str(), S_IFDIR | ((S_IRWXU | S_IRWXG | S_IRWXO) & ~umask)))
#else
if (_mkdir(tokenDir.c_str()))
#endif
{
// Allow the directory to exists already.
if (errno != EEXIST)
Expand All @@ -86,7 +90,7 @@ DBToken::DBToken(const std::string &baseDir, const std::string &tokenName, const
}

// Create
_connection = DB::Connection::Create(tokenDir, DBTOKEN_FILE);
_connection = DB::Connection::Create(tokenDir, DBTOKEN_FILE, umask);
if (_connection == NULL)
{
ERROR_MSG("Failed to create a database connection for \"%s\"", tokenPath.c_str());
Expand Down Expand Up @@ -166,7 +170,7 @@ DBToken::DBToken(const std::string &baseDir, const std::string &tokenName, const
}

// Constructor for accessing an existing token.
DBToken::DBToken(const std::string &baseDir, const std::string &tokenName)
DBToken::DBToken(const std::string &baseDir, const std::string &tokenName, int umask)
: _connection(NULL), _tokenMutex(NULL)
{
std::string tokenDir = baseDir + OS_PATHSEP + tokenName;
Expand All @@ -182,7 +186,7 @@ DBToken::DBToken(const std::string &baseDir, const std::string &tokenName)
fclose(f);

// Create a database connection.
_connection = DB::Connection::Create(tokenDir, DBTOKEN_FILE);
_connection = DB::Connection::Create(tokenDir, DBTOKEN_FILE, umask);
if (_connection == NULL)
{
ERROR_MSG("Failed to create a database connection for \"%s\"", tokenPath.c_str());
Expand Down Expand Up @@ -220,7 +224,7 @@ DBToken::DBToken(const std::string &baseDir, const std::string &tokenName)
// Success!
}

DBToken *DBToken::createToken(const std::string basePath, const std::string tokenDir, const ByteString &label, const ByteString &serial)
DBToken *DBToken::createToken(const std::string basePath, const std::string tokenDir, int umask, const ByteString &label, const ByteString &serial)
{
Directory baseDir(basePath);

Expand All @@ -230,12 +234,12 @@ DBToken *DBToken::createToken(const std::string basePath, const std::string toke
}

// Create the token directory
if (!baseDir.mkdir(tokenDir))
if (!baseDir.mkdir(tokenDir, umask))
{
return NULL;
}

DBToken *token = new DBToken(basePath, tokenDir, label, serial);
DBToken *token = new DBToken(basePath, tokenDir, umask, label, serial);
if (!token->isValid())
{
baseDir.rmdir(tokenDir);
Expand All @@ -249,9 +253,9 @@ DBToken *DBToken::createToken(const std::string basePath, const std::string toke
return token;
}

DBToken *DBToken::accessToken(const std::string &basePath, const std::string &tokenDir)
DBToken *DBToken::accessToken(const std::string &basePath, const std::string &tokenDir, int umask)
{
return new DBToken(basePath, tokenDir);
return new DBToken(basePath, tokenDir, umask);
}

// Destructor
Expand Down
8 changes: 4 additions & 4 deletions src/lib/object_store/DBToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ class DBToken : public ObjectStoreToken
{
public:
// Constructor to create a new token
DBToken(const std::string &baseDir, const std::string &tokenName, const ByteString& label, const ByteString& serial);
DBToken(const std::string &baseDir, const std::string &tokenName, int umask, const ByteString& label, const ByteString& serial);

// Constructor to access an existing token
DBToken(const std::string &baseDir, const std::string &tokenName);
DBToken(const std::string &baseDir, const std::string &tokenName, int umask);

// Create a new token
static DBToken* createToken(const std::string basePath, const std::string tokenDir, const ByteString& label, const ByteString& serial);
static DBToken* createToken(const std::string basePath, const std::string tokenDir, int umask, const ByteString& label, const ByteString& serial);

// Access an existing token
static DBToken* accessToken(const std::string &basePath, const std::string &tokenDir);
static DBToken* accessToken(const std::string &basePath, const std::string &tokenDir, int umask);

// Destructor
virtual ~DBToken();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/object_store/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ bool Directory::refresh()
}

// Create a new subdirectory
bool Directory::mkdir(std::string name)
bool Directory::mkdir(std::string name, int umask)
{
std::string fullPath = path + OS_PATHSEP + name;

#ifndef _WIN32
int rv = ::mkdir(fullPath.c_str(), S_IFDIR | S_IRWXU);
int rv = ::mkdir(fullPath.c_str(), S_IFDIR | ((S_IRWXU | S_IRWXG | S_IRWXO) & ~umask));
#else
int rv = _mkdir(fullPath.c_str());
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/lib/object_store/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Directory
bool refresh();

// Create a new subdirectory
bool mkdir(std::string name);
bool mkdir(std::string name, int umask);

// Delete a subdirectory in the directory
bool rmdir(std::string name, bool doRefresh = false);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/object_store/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ enum AttributeKind {
//
// N.B.: the create flag only has a function when a file is opened read/write
// N.B.: the truncate flag only has a function when the create one is true
File::File(std::string inPath, bool forRead /* = true */, bool forWrite /* = false */, bool create /* = false */, bool truncate /* = true */)
File::File(std::string inPath, int umask, bool forRead /* = true */, bool forWrite /* = false */, bool create /* = false */, bool truncate /* = true */)
{
stream = NULL;

Expand All @@ -88,7 +88,7 @@ File::File(std::string inPath, bool forRead /* = true */, bool forWrite /* = fal
if (forRead && forWrite && create) flags |= O_CREAT;
if (forRead && forWrite && create && truncate) flags |= O_TRUNC;
// Open the file
fd = open(path.c_str(), flags, 0600);
fd = open(path.c_str(), flags, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) & ~umask);
if (fd == -1)
{
ERROR_MSG("Could not open the file (%s): %s", strerror(errno), path.c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/lib/object_store/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class File
{
public:
// Constructor
File(std::string inPath, bool forRead = true, bool forWrite = false, bool create = false, bool truncate = true);
File(std::string inPath, int umask, bool forRead = true, bool forWrite = false, bool create = false, bool truncate = true);

// Destructor
virtual ~File();
Expand Down
13 changes: 7 additions & 6 deletions src/lib/object_store/Generation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
#include "Generation.h"

// Factory
Generation* Generation::create(const std::string path, bool isToken /* = false */)
Generation* Generation::create(const std::string path, int umask, bool isToken /* = false */)
{
Generation* gen = new Generation(path, isToken);
Generation* gen = new Generation(path, umask, isToken);
if ((gen != NULL) && isToken && (gen->genMutex == NULL))
{
delete gen;
Expand Down Expand Up @@ -92,7 +92,7 @@ bool Generation::wasUpdated()
{
MutexLocker lock(genMutex);

File genFile(path);
File genFile(path, umask);

if (!genFile.isValid())
{
Expand All @@ -118,7 +118,7 @@ bool Generation::wasUpdated()
}
else
{
File objectFile(path);
File objectFile(path, umask);

if (!objectFile.isValid())
{
Expand Down Expand Up @@ -151,7 +151,7 @@ void Generation::commit()
{
MutexLocker lock(genMutex);

File genFile(path, true, true, true, false);
File genFile(path, umask, true, true, true, false);

if (!genFile.isValid())
{
Expand Down Expand Up @@ -241,9 +241,10 @@ void Generation::rollback()
}

// Constructor
Generation::Generation(const std::string inPath, bool inIsToken)
Generation::Generation(const std::string inPath, int inUmask, bool inIsToken)
{
path = inPath;
umask = inUmask;
isToken = inIsToken;
pendingUpdate = false;
currentValue = 0;
Expand Down
Loading

0 comments on commit 8f5e4e4

Please sign in to comment.