first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / tentacles / key_repository.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : key_repository                                                    *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2004-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "key_repository.h"
16
17 #include <crypto/blowfish_crypto.h>
18 #include <structures/symbol_table.h>
19
20 using namespace basis;
21 using namespace crypto;
22 using namespace structures;
23
24 namespace octopi {
25
26 #undef LOG
27 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
28
29 //#define DEBUG_KEY_REPOSITORY
30   // uncomment for noisier execution.  beware however, if the uls is in
31   // use, this can cause infinite recursion.
32
33 key_repository::~key_repository() {}
34
35 octenc_key_record *key_repository::lock(const octopus_entity &ent)
36 {
37 #ifdef DEBUG_KEY_REPOSITORY
38   FUNCDEF("lock");
39   LOG(astring("entity sought=") + ent.text_form());
40 #endif
41   octenc_key_record *to_return = NIL;
42   _locker.lock();
43   to_return = _keys.find(ent.mangled_form());
44   if (!to_return) {
45 #ifdef DEBUG_KEY_REPOSITORY
46     LOG(astring("did not find entity=") + ent.text_form());
47 #endif
48     _locker.unlock();
49   } else {
50 #ifdef DEBUG_KEY_REPOSITORY
51     LOG(astring("found entity=") + ent.text_form());
52 #endif
53   }
54   return to_return;
55 }
56
57 void key_repository::unlock(octenc_key_record *to_unlock)
58 {
59   if (!to_unlock) return;  // dolts!  they cannot unlock a non-record.
60   _locker.unlock();
61 }
62
63 outcome key_repository::add(const octopus_entity &ent,
64     const blowfish_crypto &key)
65 {
66 #ifdef DEBUG_KEY_REPOSITORY
67   FUNCDEF("add");
68   LOG(astring("adding key for entity=") + ent.text_form());
69 #endif
70   auto_synchronizer loc(_locker);
71   octenc_key_record rec(ent, key);
72   return _keys.add(ent.mangled_form(), rec);
73 }
74
75 outcome key_repository::whack(const octopus_entity &ent)
76 {
77 #ifdef DEBUG_KEY_REPOSITORY
78   FUNCDEF("whack");
79   LOG(astring("removing key for entity=") + ent.text_form());
80 #endif
81   auto_synchronizer loc(_locker);
82   return _keys.whack(ent.mangled_form());
83 }
84
85 } //namespace.
86