Skip to content

Commit 8f5e4e4

Browse files
authored
Merge pull request #566 from nomis/umask
Add "objectstore.umask" configuration option for file/directory creation
2 parents 378b5b8 + de5bb8a commit 8f5e4e4

40 files changed

+396
-219
lines changed

configure.ac

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ AC_DEFINE_UNQUOTED(
167167
["$softhsmtokendir"],
168168
[The default location of the token directory]
169169
)
170+
AC_DEFINE_UNQUOTED(
171+
[DEFAULT_UMASK],
172+
[0077],
173+
[The default file mode creation mask]
174+
)
170175
AC_DEFINE_UNQUOTED(
171176
[DEFAULT_OBJECTSTORE_BACKEND],
172177
["file"],

src/bin/util/softhsm2-util.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,9 @@ bool deleteToken(char* serial, char* token)
548548
bool rv = true;
549549
std::string basedir = Configuration::i()->getString("directories.tokendir", DEFAULT_TOKENDIR);
550550
std::string tokendir;
551+
int umask = Configuration::i()->getInt("objectstore.umask", DEFAULT_UMASK);
551552

552-
rv = findTokenDirectory(basedir, tokendir, serial, token);
553+
rv = findTokenDirectory(basedir, tokendir, umask, serial, token);
553554

554555
if (rv)
555556
{
@@ -634,7 +635,7 @@ void finalizeSoftHSM()
634635
}
635636

636637
// Find the token directory
637-
bool findTokenDirectory(std::string basedir, std::string& tokendir, char* serial, char* label)
638+
bool findTokenDirectory(std::string basedir, std::string& tokendir, int umask, char* serial, char* label)
638639
{
639640
if (serial == NULL && label == NULL)
640641
{
@@ -693,7 +694,7 @@ bool findTokenDirectory(std::string basedir, std::string& tokendir, char* serial
693694
memset(paddedTokenLabel, ' ', sizeof(paddedTokenLabel));
694695

695696
// Create a token instance
696-
ObjectStoreToken* token = ObjectStoreToken::accessToken(basedir, *i);
697+
ObjectStoreToken* token = ObjectStoreToken::accessToken(basedir, *i, umask);
697698

698699
if (!token->isValid())
699700
{

src/bin/util/softhsm2-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void usage();
4343
bool checkSetup();
4444
int initToken(CK_SLOT_ID slotID, char* label, char* soPIN, char* userPIN);
4545
bool deleteToken(char* serial, char* token);
46-
bool findTokenDirectory(std::string basedir, std::string& tokendir, char* serial, char* label);
46+
bool findTokenDirectory(std::string basedir, std::string& tokendir, int umask, char* serial, char* label);
4747
bool rmdir(std::string path);
4848
bool rm(std::string path);
4949
int showSlots();

src/lib/SoftHSM.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ CK_RV SoftHSM::C_Initialize(CK_VOID_PTR pInitArgs)
545545
sessionObjectStore = new SessionObjectStore();
546546

547547
// Load the object store
548-
objectStore = new ObjectStore(Configuration::i()->getString("directories.tokendir", DEFAULT_TOKENDIR));
548+
objectStore = new ObjectStore(Configuration::i()->getString("directories.tokendir", DEFAULT_TOKENDIR),
549+
Configuration::i()->getInt("objectstore.umask", DEFAULT_UMASK));
549550
if (!objectStore->isValid())
550551
{
551552
WARNING_MSG("Could not load the object store");

src/lib/common/Configuration.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ std::auto_ptr<Configuration> Configuration::instance(NULL);
4646
const struct config Configuration::valid_config[] = {
4747
{ "directories.tokendir", CONFIG_TYPE_STRING },
4848
{ "objectstore.backend", CONFIG_TYPE_STRING },
49+
{ "objectstore.umask", CONFIG_TYPE_INT_OCTAL },
4950
{ "log.level", CONFIG_TYPE_STRING },
5051
{ "slots.removable", CONFIG_TYPE_BOOL },
5152
{ "slots.mechanisms", CONFIG_TYPE_STRING },
@@ -107,7 +108,14 @@ int Configuration::getInt(std::string key, int ifEmpty /* = 0 */)
107108
}
108109
else
109110
{
110-
WARNING_MSG("Missing %s in configuration. Using default value: %i", key.c_str(), ifEmpty);
111+
if (getType(key) == CONFIG_TYPE_INT_OCTAL)
112+
{
113+
WARNING_MSG("Missing %s in configuration. Using default value: 0%o", key.c_str(), ifEmpty);
114+
}
115+
else
116+
{
117+
WARNING_MSG("Missing %s in configuration. Using default value: %i", key.c_str(), ifEmpty);
118+
}
111119
return ifEmpty;
112120
}
113121
}

src/lib/common/Configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum
4343
CONFIG_TYPE_UNSUPPORTED,
4444
CONFIG_TYPE_STRING,
4545
CONFIG_TYPE_INT,
46+
CONFIG_TYPE_INT_OCTAL,
4647
CONFIG_TYPE_BOOL
4748
};
4849

src/lib/common/SimpleConfigLoader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ bool SimpleConfigLoader::loadConfiguration()
155155
case CONFIG_TYPE_INT:
156156
Configuration::i()->setInt(stringName, atoi(stringValue.c_str()));
157157
break;
158+
case CONFIG_TYPE_INT_OCTAL:
159+
Configuration::i()->setInt(stringName, strtol(stringValue.c_str(), NULL, 8));
160+
break;
158161
case CONFIG_TYPE_BOOL:
159162
bool boolValue;
160163
if (string2bool(stringValue, &boolValue))

src/lib/common/softhsm2.conf.5.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ objectstore.backend = file
4646
.fi
4747
.RE
4848
.LP
49+
.SH OBJECTSTORE.UMASK
50+
The file mode creation mask used by SoftHSM when creating files or directories. This value is in octal.
51+
This is applied in addition to the process umask and cannot override it.
52+
.LP
53+
.RS
54+
.nf
55+
objectstore.umask = 0077
56+
.fi
57+
.RE
58+
.LP
4959
.SH LOG.LEVEL
5060
The log level which can be set to ERROR, WARNING, INFO or DEBUG.
5161
.LP

src/lib/common/softhsm2.conf.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
directories.tokendir = @softhsmtokendir@
44
objectstore.backend = file
5+
objectstore.umask = 0077
56

67
# ERROR, WARNING, INFO, DEBUG
78
log.level = ERROR

src/lib/object_store/DB.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ bool DB::Result::nextRow()
704704
* Connection
705705
**************************/
706706

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

719-
return new Connection(dbdir,dbname);
719+
return new Connection(dbdir, dbname, umask);
720720
}
721721

722-
DB::Connection::Connection(const std::string &dbdir, const std::string &dbname)
722+
DB::Connection::Connection(const std::string &dbdir, const std::string &dbname, int umask)
723723
: _dbdir(dbdir)
724724
, _dbpath(dbdir + OS_PATHSEP + dbname)
725725
, _db(NULL)
726+
, _umask(umask)
726727
{
727728
}
728729

@@ -815,7 +816,7 @@ bool DB::Connection::connect(const char *
815816
)
816817
{
817818
// Create and set file permissions if the DB does not exist.
818-
int fd = open(_dbpath.c_str(), O_CREAT, S_IRUSR | S_IWUSR);
819+
int fd = open(_dbpath.c_str(), O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) & ~_umask);
819820
if (fd == -1)
820821
{
821822
DB::logError("Could not open database: %s (errno %i)",

0 commit comments

Comments
 (0)