| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
ObjectStore.h |
| 29 |
|
| 30 |
The object store manages the separate tokens that the SoftHSM supports. Each |
| 31 |
token is organised as a directory containing files that are contain the |
| 32 |
token's objects. The object store is initialised with a root directory from |
| 33 |
which it enumerates the tokens. |
| 34 |
*****************************************************************************/ |
| 35 |
|
| 36 |
#include "config.h" |
| 37 |
#include "log.h" |
| 38 |
#include "ObjectStore.h" |
| 39 |
#include "Directory.h" |
| 40 |
#include "ObjectStoreToken.h" |
| 41 |
#include "OSPathSep.h" |
| 42 |
#include "UUID.h" |
| 43 |
#include <stdio.h> |
| 44 |
|
| 45 |
// Constructor |
| 46 |
ObjectStore::ObjectStore(std::string inStorePath) |
| 47 |
{ |
| 48 |
storePath = inStorePath; |
| 49 |
valid = false; |
| 50 |
storeMutex = MutexFactory::i()->getMutex(); |
| 51 |
|
| 52 |
MutexLocker lock(storeMutex); |
| 53 |
|
| 54 |
// Find all tokens in the specified path |
| 55 |
Directory storeDir(storePath); |
| 56 |
|
| 57 |
if (!storeDir.isValid()) |
| 58 |
{ |
| 59 |
WARNING_MSG("Failed to enumerate object store in %s", storePath.c_str()); |
| 60 |
|
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
// Assume that all subdirectories are tokens |
| 65 |
std::vector<std::string> dirs = storeDir.getSubDirs(); |
| 66 |
|
| 67 |
for (std::vector<std::string>::iterator i = dirs.begin(); i != dirs.end(); i++) |
| 68 |
{ |
| 69 |
// Create a token instance |
| 70 |
ObjectStoreToken* token = ObjectStoreToken::accessToken(storePath, *i); |
| 71 |
|
| 72 |
if (!token->isValid()) |
| 73 |
{ |
| 74 |
ERROR_MSG("Failed to open token %s", i->c_str()); |
| 75 |
|
| 76 |
delete token; |
| 77 |
|
| 78 |
// Silently ignore tokens that we do not have access to |
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
tokens.push_back(token); |
| 83 |
allTokens.push_back(token); |
| 84 |
} |
| 85 |
|
| 86 |
valid = true; |
| 87 |
} |
| 88 |
|
| 89 |
// Destructor |
| 90 |
ObjectStore::~ObjectStore() |
| 91 |
{ |
| 92 |
{ |
| 93 |
MutexLocker lock(storeMutex); |
| 94 |
|
| 95 |
// Clean up |
| 96 |
tokens.clear(); |
| 97 |
|
| 98 |
for (std::vector<ObjectStoreToken*>::iterator i = allTokens.begin(); i != allTokens.end(); i++) |
| 99 |
{ |
| 100 |
delete *i; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
MutexFactory::i()->recycleMutex(storeMutex); |
| 105 |
} |
| 106 |
|
| 107 |
// Check if the object store is valid |
| 108 |
bool ObjectStore::isValid() |
| 109 |
{ |
| 110 |
return valid; |
| 111 |
} |
| 112 |
|
| 113 |
// Return the number of tokens that is present |
| 114 |
size_t ObjectStore::getTokenCount() |
| 115 |
{ |
| 116 |
MutexLocker lock(storeMutex); |
| 117 |
|
| 118 |
return tokens.size(); |
| 119 |
} |
| 120 |
|
| 121 |
// Return a pointer to the n-th token (counting starts at 0) |
| 122 |
ObjectStoreToken* ObjectStore::getToken(size_t whichToken) |
| 123 |
{ |
| 124 |
MutexLocker lock(storeMutex); |
| 125 |
|
| 126 |
if (whichToken >= tokens.size()) |
| 127 |
{ |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
|
| 131 |
return tokens[whichToken]; |
| 132 |
} |
| 133 |
|
| 134 |
// Create a new token |
| 135 |
ObjectStoreToken* ObjectStore::newToken(const ByteString& label) |
| 136 |
{ |
| 137 |
MutexLocker lock(storeMutex); |
| 138 |
|
| 139 |
// Generate a UUID for the token |
| 140 |
std::string tokenUUID = UUID::newUUID(); |
| 141 |
|
| 142 |
// Convert the UUID to a serial number |
| 143 |
std::string serialNumber = tokenUUID.substr(19, 4) + tokenUUID.substr(24); |
| 144 |
ByteString serial((const unsigned char*) serialNumber.c_str(), serialNumber.size()); |
| 145 |
|
| 146 |
// Create the token |
| 147 |
ObjectStoreToken* newToken = ObjectStoreToken::createToken(storePath, tokenUUID, label, serial); |
| 148 |
|
| 149 |
if (newToken != NULL) |
| 150 |
{ |
| 151 |
tokens.push_back(newToken); |
| 152 |
allTokens.push_back(newToken); |
| 153 |
} |
| 154 |
|
| 155 |
return newToken; |
| 156 |
} |
| 157 |
|
| 158 |
// Destroy a token |
| 159 |
bool ObjectStore::destroyToken(ObjectStoreToken *token) |
| 160 |
{ |
| 161 |
MutexLocker lock(storeMutex); |
| 162 |
|
| 163 |
// Find the token |
| 164 |
for (std::vector<ObjectStoreToken*>::iterator i = tokens.begin(); i != tokens.end(); i++) |
| 165 |
{ |
| 166 |
if (*i == token) |
| 167 |
{ |
| 168 |
// Found the token, now destroy the token |
| 169 |
if (!token->clearToken()) |
| 170 |
{ |
| 171 |
ERROR_MSG("Failed to clear token instance"); |
| 172 |
|
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
// And remove it from the vector |
| 177 |
tokens.erase(i); |
| 178 |
|
| 179 |
return true; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
ERROR_MSG("Could not find the token instance to destroy"); |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
|