[373] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * userdb.cpp -- functions to handle a user database
|
---|
| 4 | * Copyright (C) 1999 DigiLib Systems Limited, New Zealand
|
---|
| 5 | *
|
---|
[533] | 6 | * A component of the Greenstone digital library software
|
---|
| 7 | * from the New Zealand Digital Library Project at the
|
---|
| 8 | * University of Waikato, New Zealand.
|
---|
[373] | 9 | *
|
---|
[533] | 10 | * This program is free software; you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * This program is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with this program; if not, write to the Free Software
|
---|
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | *
|
---|
[373] | 24 | *********************************************************************/
|
---|
| 25 |
|
---|
[997] | 26 | #include "gsdlconf.h"
|
---|
[373] | 27 | #include "userdb.h"
|
---|
[15586] | 28 | #include "gdbmclass.h"
|
---|
[373] | 29 | #include "gsdltimes.h"
|
---|
| 30 | #include "fileutil.h"
|
---|
[516] | 31 | #include <stdlib.h>
|
---|
[373] | 32 |
|
---|
| 33 | // include crypt
|
---|
[1000] | 34 | #if defined(__WIN32__)
|
---|
| 35 | #include "crypt.h"
|
---|
| 36 | #else
|
---|
[997] | 37 | #if defined(HAVE_CRYPT_H)
|
---|
| 38 | #include <crypt.h>
|
---|
| 39 | #else
|
---|
[533] | 40 | #include <unistd.h>
|
---|
[997] | 41 | #endif
|
---|
[1000] | 42 | #endif
|
---|
[373] | 43 |
|
---|
[13844] | 44 | //==========================================//
|
---|
| 45 | // userinfo_t functions (Start) //
|
---|
| 46 | //==========================================//
|
---|
| 47 | userinfo_t::userinfo_t()
|
---|
| 48 | {
|
---|
| 49 | clear();
|
---|
| 50 | }
|
---|
[373] | 51 |
|
---|
[13844] | 52 | userinfo_t::~userinfo_t(){};
|
---|
| 53 |
|
---|
| 54 | void userinfo_t::clear () {
|
---|
| 55 | username.clear();
|
---|
| 56 | password.clear();
|
---|
| 57 | enabled = false;
|
---|
| 58 | groups.clear();
|
---|
| 59 | comment.clear();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | userinfo_t &userinfo_t::operator=(const userinfo_t &x) {
|
---|
| 63 | username = x.username;
|
---|
| 64 | password = x.password;
|
---|
| 65 | enabled = x.enabled;
|
---|
| 66 | groups = x.groups;
|
---|
| 67 | comment = x.comment;
|
---|
| 68 |
|
---|
| 69 | return *this;
|
---|
| 70 | }
|
---|
| 71 | //==========================================//
|
---|
| 72 | // userinfo_t functions (END) //
|
---|
| 73 | //==========================================//
|
---|
| 74 |
|
---|
| 75 | //==========================================//
|
---|
| 76 | // userdbclass functions (Start) //
|
---|
| 77 | //==========================================//
|
---|
| 78 | userdbclass::userdbclass(const text_t &userdbfilename)
|
---|
| 79 | {
|
---|
[14269] | 80 | storeduserdbfilename = userdbfilename;
|
---|
[15586] | 81 | userdb = new gdbmclass();
|
---|
| 82 | activated = (!userdb->opendatabase(storeduserdbfilename, DB_READER, 1000, true)) ? false : true;
|
---|
[14269] | 83 | if (activated == false)
|
---|
| 84 | {
|
---|
[15586] | 85 | activated = (!userdb->opendatabase(storeduserdbfilename, DB_WRITER_CREATE, 1000, true)) ? false : true;
|
---|
[14269] | 86 | if (activated == true)
|
---|
| 87 | {
|
---|
[15586] | 88 | userdb->closedatabase();
|
---|
| 89 | activated = (!userdb->opendatabase(storeduserdbfilename, DB_READER, 1000, true)) ? false : true;
|
---|
[14269] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[13844] | 93 | external_db = false;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | userdbclass::~userdbclass()
|
---|
| 97 | {
|
---|
[15586] | 98 | if (external_db == false)
|
---|
| 99 | {
|
---|
| 100 | userdb->closedatabase();
|
---|
| 101 | delete userdb;
|
---|
| 102 | }
|
---|
[13844] | 103 | }
|
---|
| 104 |
|
---|
[373] | 105 | // few useful functions
|
---|
[13844] | 106 | text_t userdbclass::crypt_text (const text_t &text)
|
---|
| 107 | {
|
---|
[373] | 108 | static const char *salt = "Tp";
|
---|
| 109 | text_t crypt_password;
|
---|
[13844] | 110 |
|
---|
[7381] | 111 | if (text.empty()) return g_EmptyText;
|
---|
[13844] | 112 |
|
---|
[373] | 113 | // encrypt the password
|
---|
| 114 | char *text_cstr = text.getcstr();
|
---|
[7381] | 115 | if (text_cstr == NULL) return g_EmptyText;
|
---|
[373] | 116 | crypt_password = crypt(text_cstr, salt);
|
---|
[7381] | 117 | delete []text_cstr;
|
---|
[373] | 118 |
|
---|
| 119 | return crypt_password;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | // username_ok tests to make sure a username is ok. a username
|
---|
| 123 | // must be at least 2 characters long, but no longer than 30
|
---|
| 124 | // characters long. it can contain the characters a-z A-Z 0-9
|
---|
| 125 | // . and _
|
---|
[13844] | 126 | bool userdbclass::username_ok (const text_t &username)
|
---|
| 127 | {
|
---|
[373] | 128 | if (username.size() < 2 || username.size() > 30) return false;
|
---|
| 129 |
|
---|
| 130 | text_t::const_iterator here = username.begin();
|
---|
| 131 | text_t::const_iterator end = username.end();
|
---|
[13844] | 132 | while (here != end)
|
---|
| 133 | {
|
---|
| 134 | if ((*here >= 'a' && *here <= 'z') ||
|
---|
| 135 | (*here >= 'A' && *here <= 'Z') ||
|
---|
| 136 | (*here >= '0' && *here <= '9') ||
|
---|
| 137 | *here == '.' ||
|
---|
| 138 | *here == '_')
|
---|
| 139 | {
|
---|
| 140 | // ok
|
---|
| 141 | } else return false;
|
---|
| 142 | ++here;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[373] | 145 | return true;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | // password_ok tests to make sure a password is ok. a password
|
---|
| 149 | // must be at least 3 characters long but no longer than 8 characters
|
---|
| 150 | // long. it can contain any character in the range 0x20-0x7e
|
---|
[13844] | 151 | bool userdbclass::password_ok (const text_t &password)
|
---|
| 152 | {
|
---|
[14999] | 153 | // DB: 20/02/08. Why mustn't the password exceed 8 chars,
|
---|
| 154 | // windows 3.1 complicance? Even then this doesn't make sense to me
|
---|
| 155 | // as storing this in GDBM wouldn't trigger a 8-char limit
|
---|
[373] | 156 |
|
---|
[14999] | 157 | // Have increased this to 128. Not because I think someone will type
|
---|
| 158 | // in something that long, but some encryptions schemes (e.g. wireless
|
---|
| 159 | // networks) work with such lengths and someone one day might like
|
---|
| 160 | // to use such a generated password in Greenstone.
|
---|
| 161 | //
|
---|
| 162 |
|
---|
| 163 | if (password.size() < 3 || password.size() > 128) return false;
|
---|
| 164 |
|
---|
[373] | 165 | text_t::const_iterator here = password.begin();
|
---|
| 166 | text_t::const_iterator end = password.end();
|
---|
| 167 | while (here != end) {
|
---|
[13844] | 168 | if (*here >= 0x20 && *here <= 0x7e)
|
---|
| 169 | {
|
---|
| 170 | // ok
|
---|
| 171 | } else return false;
|
---|
[9620] | 172 | ++here;
|
---|
[373] | 173 | }
|
---|
| 174 |
|
---|
| 175 | return true;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[13844] | 178 | // removes spaces from user groups
|
---|
| 179 | text_t userdbclass::format_user_groups(const text_t user_groups)
|
---|
| 180 | {
|
---|
| 181 | text_t new_groups = g_EmptyText;
|
---|
| 182 | text_t::const_iterator here = user_groups.begin();
|
---|
| 183 | text_t::const_iterator end = user_groups.end();
|
---|
| 184 | while (here != end) {
|
---|
| 185 | if (*here != ' '&& *here != '\t' && *here != '\n')
|
---|
| 186 | {
|
---|
| 187 | new_groups.push_back(*here);
|
---|
| 188 | }
|
---|
| 189 | ++here;
|
---|
| 190 | }
|
---|
| 191 | return new_groups;
|
---|
[373] | 192 | }
|
---|
| 193 |
|
---|
| 194 | // functions dealing with user databases
|
---|
| 195 | // returns true on success (in which case userinfo will contain
|
---|
| 196 | // the information for this user)
|
---|
[13844] | 197 | // @return 0 success
|
---|
| 198 | // @return -1 database is not currently connected
|
---|
| 199 | // @return -2 not defined username
|
---|
| 200 | int userdbclass::get_user_info (const text_t &username, userinfo_t &userinfo)
|
---|
| 201 | {
|
---|
| 202 | // Let's make sure the connection has been established.
|
---|
| 203 | if (activated == true)
|
---|
| 204 | {
|
---|
[373] | 205 | userinfo.clear();
|
---|
[13844] | 206 | infodbclass info;
|
---|
| 207 | // See if we can get the user's infomration
|
---|
[15586] | 208 | if (userdb->getinfo (username, info))
|
---|
[13844] | 209 | {
|
---|
| 210 | userinfo.username = info["username"];
|
---|
| 211 | userinfo.password = info["password"];
|
---|
| 212 | userinfo.enabled = (info["enabled"] == "true");
|
---|
| 213 | userinfo.groups = info["groups"];
|
---|
| 214 | userinfo.comment = info["comment"];
|
---|
| 215 | return ERRNO_SUCCEED;
|
---|
| 216 | }
|
---|
| 217 | // If we failed to retrieve the users information, we should check if the username is admin or not.
|
---|
| 218 | // If it is the admin user, let's create a new account for the admin user.
|
---|
| 219 | else if (username == "admin")
|
---|
| 220 | {
|
---|
| 221 | userinfo.clear();
|
---|
| 222 | userinfo.username = "admin";
|
---|
| 223 | userinfo.password = crypt_text("admin");
|
---|
| 224 | userinfo.enabled = true;
|
---|
[15087] | 225 | userinfo.groups = "administrator,all-collections-editor";
|
---|
[13844] | 226 | userinfo.comment = "change the password for this account as soon as possible";
|
---|
| 227 | // Return the set result.
|
---|
| 228 | return set_user_info (username, userinfo);
|
---|
| 229 | }
|
---|
| 230 | // The username is not found, return false
|
---|
| 231 | return ERRNO_USERNOTFOUND;
|
---|
[373] | 232 | }
|
---|
[13844] | 233 | // Failed to connect to the database, return false.
|
---|
| 234 | return ERRNO_CONNECTIONFAILED;
|
---|
[373] | 235 | }
|
---|
| 236 |
|
---|
| 237 | // returns true on success
|
---|
[13844] | 238 | int userdbclass::set_user_info (const text_t &username, const userinfo_t &userinfo)
|
---|
| 239 | {
|
---|
| 240 | // Let's make sure the connection has been established.
|
---|
| 241 | if (activated == true)
|
---|
| 242 | {
|
---|
| 243 | infodbclass info;
|
---|
| 244 | info["username"] = userinfo.username;
|
---|
| 245 | info["password"] = userinfo.password;
|
---|
| 246 | info["enabled"] = userinfo.enabled ? "true" : "false";
|
---|
| 247 | info["groups"] = userinfo.groups;
|
---|
| 248 | info["comment"] = userinfo.comment;
|
---|
[15586] | 249 | userdb->closedatabase();
|
---|
| 250 | userdb->opendatabase(storeduserdbfilename, DB_WRITER_CREATE, 1000, true);
|
---|
| 251 | int result = (userdb->setinfo (username, info)) ? ERRNO_SUCCEED : ERRNO_DBACTIONFAILED;
|
---|
| 252 | userdb->closedatabase();
|
---|
| 253 | userdb->opendatabase(storeduserdbfilename, DB_READER, 1000, true);
|
---|
[14269] | 254 | return result;
|
---|
[13844] | 255 | }
|
---|
| 256 | return ERRNO_CONNECTIONFAILED;
|
---|
[373] | 257 | }
|
---|
| 258 |
|
---|
[13844] | 259 | // returns true if the user's password is correct.
|
---|
| 260 | int userdbclass::check_passwd (const text_t &username, const text_t &password)
|
---|
| 261 | {
|
---|
| 262 | userinfo_t thisuser;
|
---|
| 263 | int returned = get_user_info(username, thisuser);
|
---|
| 264 | if(returned != ERRNO_SUCCEED) return returned;
|
---|
| 265 | // couple of basic checks
|
---|
| 266 | if (thisuser.username.empty() || thisuser.password.empty() ||
|
---|
| 267 | password.empty()) return ERRNO_MISSINGPASSWORD;
|
---|
[373] | 268 |
|
---|
[13844] | 269 | text_t crypt_password = crypt_text(password);
|
---|
| 270 | return (thisuser.password == crypt_password) ? ERRNO_SUCCEED : ERRNO_PASSWORDMISMATCH;
|
---|
| 271 | }
|
---|
[373] | 272 |
|
---|
[13844] | 273 | int userdbclass::add_user (const userinfo_t &userinfo)
|
---|
| 274 | {
|
---|
| 275 | // Let's make sure the connection has been established.
|
---|
| 276 | if (activated == true)
|
---|
| 277 | {
|
---|
| 278 | infodbclass info;
|
---|
[15586] | 279 | if (userdb->getinfo (userinfo.username, info))
|
---|
[13844] | 280 | {
|
---|
| 281 | // There is an existing username already
|
---|
| 282 | return ERRNO_EXISTINGUSERNAME;
|
---|
| 283 | }
|
---|
| 284 | else
|
---|
| 285 | {
|
---|
| 286 | return set_user_info(userinfo.username, userinfo);
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 | return ERRNO_CONNECTIONFAILED;
|
---|
[373] | 290 | }
|
---|
| 291 |
|
---|
[13844] | 292 | int userdbclass::edit_user (const userinfo_t &userinfo)
|
---|
| 293 | {
|
---|
| 294 | // Let's make sure the connection has been established.
|
---|
| 295 | if (activated == true)
|
---|
| 296 | {
|
---|
| 297 | infodbclass info;
|
---|
[15586] | 298 | if (userdb->getinfo (userinfo.username, info))
|
---|
[13844] | 299 | {
|
---|
| 300 | return set_user_info(userinfo.username, userinfo);
|
---|
| 301 | }
|
---|
| 302 | else
|
---|
| 303 | {
|
---|
| 304 | // The user does not exist in the database.
|
---|
| 305 | return ERRNO_USERNOTFOUND;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | return ERRNO_CONNECTIONFAILED;
|
---|
[373] | 309 | }
|
---|
| 310 |
|
---|
[13844] | 311 | int userdbclass::delete_user (const text_t &username)
|
---|
| 312 | {
|
---|
| 313 | // Let's make sure the connection has been established.
|
---|
| 314 | if (activated == true)
|
---|
| 315 | {
|
---|
[15586] | 316 | userdb->closedatabase();
|
---|
| 317 | userdb->opendatabase(storeduserdbfilename, DB_WRITER_CREATE, 1000, true);
|
---|
| 318 | userdb->deletekey (username);
|
---|
| 319 | userdb->closedatabase();
|
---|
| 320 | userdb->opendatabase(storeduserdbfilename, DB_READER, 1000, true);
|
---|
[13844] | 321 | return ERRNO_SUCCEED;
|
---|
| 322 | }
|
---|
| 323 | return ERRNO_CONNECTIONFAILED;
|
---|
| 324 | }
|
---|
[373] | 325 |
|
---|
[13844] | 326 | // gets all the users' information in the database. returns true
|
---|
| 327 | // on success
|
---|
| 328 | int userdbclass::get_all_users(userinfo_tarray &userinfo_array)
|
---|
| 329 | {
|
---|
| 330 | // Let's make sure the connection has been established.
|
---|
| 331 | if (activated == true)
|
---|
| 332 | {
|
---|
| 333 | userinfo_array.erase(userinfo_array.begin(), userinfo_array.end());
|
---|
[15630] | 334 | text_tarray userlist = userdb->getkeys();
|
---|
| 335 | text_tarray::iterator user_iterator = userlist.begin();
|
---|
| 336 | while (user_iterator != userlist.end())
|
---|
| 337 | {
|
---|
[13844] | 338 | userinfo_t one_userinfo;
|
---|
[15630] | 339 | int returned = get_user_info(*user_iterator, one_userinfo);
|
---|
[13844] | 340 | if (returned != ERRNO_SUCCEED) return returned;
|
---|
| 341 | userinfo_array.push_back(one_userinfo);
|
---|
[15630] | 342 | user_iterator++;
|
---|
[13844] | 343 | }
|
---|
| 344 | return ERRNO_SUCCEED;
|
---|
| 345 | }
|
---|
| 346 | return ERRNO_CONNECTIONFAILED;
|
---|
[373] | 347 | }
|
---|
| 348 |
|
---|
| 349 | // gets a list of all the users in the database. returns true
|
---|
| 350 | // on success
|
---|
[13844] | 351 | int userdbclass::get_user_list (text_tarray &userlist)
|
---|
| 352 | {
|
---|
| 353 | // Let's make sure the connection has been established.
|
---|
| 354 | if (activated == true)
|
---|
| 355 | {
|
---|
| 356 | userlist.erase (userlist.begin(), userlist.end());
|
---|
[15630] | 357 | userlist = userdb->getkeys();
|
---|
[13844] | 358 | return ERRNO_SUCCEED;
|
---|
| 359 | }
|
---|
| 360 | return ERRNO_CONNECTIONFAILED;
|
---|
[373] | 361 | }
|
---|
[13844] | 362 | //==========================================//
|
---|
| 363 | // userdbclass functions (End) //
|
---|
| 364 | //==========================================//
|
---|
[373] | 365 |
|
---|
[13844] | 366 | //==========================================//
|
---|
| 367 | // keydbclass functions (Start) //
|
---|
| 368 | //==========================================//
|
---|
| 369 | keydbclass::keydbclass(const text_t &keydbfilename)
|
---|
| 370 | {
|
---|
[14269] | 371 | storedkeydbfilename = keydbfilename;
|
---|
[15586] | 372 | keydb = new gdbmclass();
|
---|
| 373 | activated = keydb->opendatabase(storedkeydbfilename, DB_READER, 1000, true);
|
---|
[14269] | 374 | if (activated == false)
|
---|
| 375 | {
|
---|
[15586] | 376 | activated = keydb->opendatabase(storedkeydbfilename, DB_WRITER_CREATE, 1000, true);
|
---|
[14269] | 377 | if (activated == true)
|
---|
| 378 | {
|
---|
[15586] | 379 | keydb->closedatabase();
|
---|
| 380 | activated = keydb->opendatabase(storedkeydbfilename, DB_READER, 1000, true);
|
---|
[14269] | 381 | }
|
---|
| 382 | }
|
---|
[13844] | 383 | external_db = false;
|
---|
| 384 | }
|
---|
[373] | 385 |
|
---|
[15586] | 386 | keydbclass::~keydbclass()
|
---|
[13844] | 387 | {
|
---|
[15586] | 388 | if (external_db == false)
|
---|
| 389 | {
|
---|
| 390 | keydb->closedatabase();
|
---|
| 391 | delete keydb;
|
---|
| 392 | }
|
---|
[373] | 393 | }
|
---|
| 394 |
|
---|
| 395 | // generates a random key for the user, stores it in the database and
|
---|
| 396 | // returns it so that it can be used in page generation
|
---|
| 397 | // returns "" on failure
|
---|
[13844] | 398 | text_t keydbclass::generate_key (const text_t &username)
|
---|
| 399 | {
|
---|
| 400 | // Let's make sure the connection has been established.
|
---|
| 401 | if (activated == true)
|
---|
| 402 | {
|
---|
| 403 | static const char *numconvert = "0123456789abcdefghijklmnopqrstuvwxyz"
|
---|
| 404 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
| 405 |
|
---|
| 406 | // loop looking for a suitable new key
|
---|
| 407 | text_t userkey;
|
---|
| 408 | text_t crypt_userkey;
|
---|
| 409 | do {
|
---|
| 410 | // convert to base 62 :-)
|
---|
| 411 | int userkey_int = rand ();
|
---|
| 412 | while (userkey_int > 0)
|
---|
| 413 | {
|
---|
| 414 | userkey.push_back (numconvert[userkey_int%62]);
|
---|
| 415 | userkey_int /= 62;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | // make sure this key is not in the database
|
---|
| 419 | crypt_userkey = userdbclass::crypt_text(userkey);
|
---|
[15586] | 420 | if (keydb->exists (crypt_userkey)) userkey.clear();
|
---|
[13844] | 421 | } while (userkey.empty());
|
---|
| 422 |
|
---|
| 423 | // enter the key into the database
|
---|
| 424 | infodbclass keydata;
|
---|
| 425 | keydata["user"] = username;
|
---|
| 426 | keydata["time"] = time2text(time(NULL));
|
---|
| 427 |
|
---|
[15586] | 428 | keydb->closedatabase();
|
---|
| 429 | keydb->opendatabase(storedkeydbfilename, DB_WRITER_CREATE, 1000, true);
|
---|
| 430 | if (!keydb->setinfo (crypt_userkey, keydata))
|
---|
[13844] | 431 | {
|
---|
| 432 | userkey.clear(); // failed
|
---|
| 433 | }
|
---|
[15586] | 434 | keydb->closedatabase();
|
---|
| 435 | keydb->opendatabase(storedkeydbfilename, DB_READER, 1000, true);
|
---|
[13844] | 436 |
|
---|
| 437 | return userkey;
|
---|
[373] | 438 | }
|
---|
[13844] | 439 | return "";
|
---|
[373] | 440 | }
|
---|
| 441 |
|
---|
| 442 | // checks to see if there is a key for this particular user in the
|
---|
| 443 | // database that hasn't decayed. a short decay is used when group
|
---|
| 444 | // is set to administrator
|
---|
[13844] | 445 | bool keydbclass::check_key (const userinfo_t &thisuser, const text_t &key, const text_t &group, int keydecay)
|
---|
| 446 | {
|
---|
| 447 | // Let's make sure the connection has been established.
|
---|
| 448 | if (activated == true)
|
---|
| 449 | {
|
---|
| 450 | if (thisuser.username.empty() || key.empty()) return false;
|
---|
| 451 |
|
---|
| 452 | // the keydecay is set to 5 minute for things requiring the
|
---|
| 453 | // administrator
|
---|
| 454 | // if (group == "administrator") keydecay = 300;
|
---|
| 455 |
|
---|
| 456 | // success if there is a key in the key database that is owned by this
|
---|
| 457 | // user whose creation time is less that keydecay
|
---|
| 458 | text_t crypt_key = userdbclass::crypt_text(key);
|
---|
| 459 | infodbclass info;
|
---|
[15586] | 460 | if (keydb->getinfo (crypt_key, info)) {
|
---|
[13844] | 461 | if (info["user"] == thisuser.username) {
|
---|
| 462 | time_t keycreation = text2time (info["time"]);
|
---|
| 463 | if (keycreation != (time_t)-1 && difftime (text2time(time2text(time(NULL))),
|
---|
| 464 | keycreation) <= keydecay) {
|
---|
| 465 | // succeeded, update the key's time
|
---|
| 466 | info["time"] = time2text(time(NULL));
|
---|
[15586] | 467 | keydb->closedatabase();
|
---|
| 468 | keydb->opendatabase(storedkeydbfilename, DB_WRITER_CREATE, 1000, true);
|
---|
| 469 | keydb->setinfo (crypt_key, info);
|
---|
| 470 | keydb->closedatabase();
|
---|
| 471 | keydb->opendatabase(storedkeydbfilename, DB_READER, 1000, true);
|
---|
[13844] | 472 | return true;
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
[373] | 475 | }
|
---|
| 476 | }
|
---|
[13844] | 477 | return false;
|
---|
[373] | 478 | }
|
---|
| 479 |
|
---|
| 480 | // remove_old_keys will remove all keys created more than keydecay ago.
|
---|
| 481 | // use sparingly, it can be quite an expensive function
|
---|
[13844] | 482 | void keydbclass::remove_old_keys (int keydecay)
|
---|
| 483 | {
|
---|
| 484 | // Let's make sure the connection has been established.
|
---|
| 485 | if (activated == true)
|
---|
| 486 | {
|
---|
| 487 | // get a list of keys created more than keydecay seconds agon
|
---|
| 488 | text_tarray oldkeys;
|
---|
| 489 | infodbclass info;
|
---|
| 490 | time_t timenow = text2time(time2text(time(NULL)));
|
---|
| 491 | time_t keycreation = (time_t)-1;
|
---|
[15630] | 492 |
|
---|
| 493 | text_tarray keylist = keydb->getkeys();
|
---|
| 494 | text_tarray::iterator key_iterator = keylist.begin();
|
---|
| 495 | while (key_iterator != keylist.end())
|
---|
| 496 | {
|
---|
| 497 | if (keydb->getinfo (*key_iterator, info)) {
|
---|
[13844] | 498 | keycreation = text2time (info["time"]);
|
---|
| 499 | if (keycreation != (time_t)-1 && difftime (timenow, keycreation) > keydecay) {
|
---|
| 500 | // found an old key
|
---|
[15630] | 501 | oldkeys.push_back(*key_iterator);
|
---|
[13844] | 502 | }
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[15630] | 505 | key_iterator++;
|
---|
[373] | 506 | }
|
---|
[13844] | 507 |
|
---|
[15207] | 508 | // delete the old keys
|
---|
| 509 | if (oldkeys.size() > 0) {
|
---|
[15586] | 510 | keydb->closedatabase();
|
---|
| 511 | keydb->opendatabase(storedkeydbfilename, DB_WRITER_CREATE, 1000, true);
|
---|
[15207] | 512 | text_tarray::iterator keys_here = oldkeys.begin();
|
---|
| 513 | text_tarray::iterator keys_end = oldkeys.end();
|
---|
| 514 | while (keys_here != keys_end) {
|
---|
[15586] | 515 | keydb->deletekey(*keys_here);
|
---|
[15207] | 516 | ++keys_here;
|
---|
| 517 | }
|
---|
[15586] | 518 | keydb->closedatabase();
|
---|
| 519 | keydb->opendatabase(storedkeydbfilename, DB_READER, 1000, true);
|
---|
[13844] | 520 | }
|
---|
[373] | 521 | }
|
---|
| 522 | }
|
---|
[13844] | 523 | //==========================================//
|
---|
| 524 | // keydbclass functions (End) //
|
---|
| 525 | //==========================================//
|
---|