1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
18
19 #define INDEX_KEYSIZE 177
20
21 namespace sipwitch {
22
24 {
25 public:
26 key(
const char *keyid,
const char *keyhash);
27
30 };
31
32 static memalloc private_cache;
34 static condlock_t private_lock;
35
36 key::key(
const char *keyid,
const char *keyhash) :
38 {
39 id = private_cache.dup(keyid);
40 hash = private_cache.dup(keyhash);
41 }
42
44 {
45 private_lock.modify();
46 memset(private_paths, 0, sizeof(private_paths));
47 private_cache.purge();
48 private_lock.commit();
50 }
51
53 {
54 assert(id != NULL);
55
56 private_lock.access();
58 linked_pointer<key> keys = private_paths[path];
59 while(is(keys)) {
60 if(String::equal(id, keys->id))
61 return keys->hash;
62 keys.next();
63 }
64 private_lock.release();
65 return NULL;
66 }
67
69 {
70 if(id)
71 private_lock.release();
72 }
73
75 {
76 assert(id != NULL && hash != NULL);
77
78 void *mp;
79 size_t len = strlen(hash);
80
81 private_lock.access();
83 linked_pointer<key> keys = private_paths[path];
84 while(is(keys)) {
85 if(String::equal(id, keys->id)) {
86 if(len == strlen(keys->hash)) {
87 String::set(keys->hash, ++len, hash);
88 private_lock.commit();
89 return true;
90 }
91 return false;
92 }
93 keys.next();
94 }
95 mp = private_cache.alloc(
sizeof(
key));
96 new(mp)
key(
id, hash);
97 private_lock.commit();
98 return true;
99 }
100
102 {
103 FILE *fp;
104 char buffer[256];
105 char *cp, *ep;
106
107 dir::create(DEFAULT_VARPATH "/lib/sipwitch/digests", fsys::GROUP_PRIVATE);
109 fp = fopen(*path, "r");
110
111 if(!fp)
112 return;
113
114 while(NULL != fgets(buffer, sizeof(buffer), fp)) {
115 if(feof(fp))
116 break;
117
118 cp = strchr(buffer, ':');
119 if(!cp)
120 continue;
121
122 *(cp++) = 0;
123
124 ep = strchr(cp, '\r');
125 if(!ep)
126 ep = strchr(cp, '\n');
127
128 if(ep)
129 *ep = 0;
130
132 }
133 fclose(fp);
134 }
135
136 } // end namespace
key(const char *keyid, const char *keyhash)
static bool set(const char *id, const char *hash)
static const char * getRealm(void)
static const char * get(const char *id)
static void release(const char *hash)