-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyRing.cc
218 lines (183 loc) · 4.76 KB
/
KeyRing.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2009 Sage Weil <[email protected]>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <errno.h>
#include <map>
#include "common/config.h"
#include "common/debug.h"
#include "include/str_list.h"
#include "common/ConfUtils.h"
#include "Crypto.h"
#include "auth/KeyRing.h"
#define DOUT_SUBSYS auth
#undef dout_prefix
#define dout_prefix *_dout << "auth: "
using namespace std;
KeyRing g_keyring;
static string remove_quotes(string& s)
{
int n = s.size();
/* remove quotes from string */
if (s[0] == '"' && s[n - 1] == '"')
return s.substr(1, n-2);
return s;
}
int KeyRing::set_modifier(const char *type, const char *val, EntityName& name, map<string, bufferlist>& caps)
{
if (!val)
return -EINVAL;
if (strcmp(type, "key") == 0) {
CryptoKey key;
string l(val);
l = remove_quotes(l);
try {
key.decode_base64(l);
} catch (const buffer::error& err) {
return -EINVAL;
}
set_key(name, key);
} else if (strncmp(type, "caps ", 5) == 0) {
const char *caps_entity = type + 5;
if (!*caps_entity)
return -EINVAL;
string l(val);
l = remove_quotes(l);
bufferlist bl;
::encode(l, bl);
caps[caps_entity] = bl;
set_caps(name, caps);
} else if (strcmp(type, "auid") == 0) {
uint64_t auid = strtoull(val, NULL, 0);
set_uid(name, auid);
} else
return -EINVAL;
return 0;
}
void KeyRing::decode_plaintext(bufferlist::iterator& bli)
{
int ret;
bufferlist::iterator iter = bli;
// find out the size of the buffer
char c;
int len = 0;
try {
do {
::decode(c, iter);
len++;
} while (c);
} catch (buffer::error& err) {
}
char *orig_src = new char[len + 1];
orig_src[len] = '\0';
iter = bli;
int i;
for (i = 0; i < len; i++) {
::decode(c, iter);
orig_src[i] = c;
}
bufferlist bl;
bl.append(orig_src, len);
ConfFile cf(&bl);
cf.set_global(false);
if (!cf.parse()) {
derr << "cannot parse buffer" << dendl;
goto done_err;
}
for (std::list<ConfSection*>::const_iterator p =
cf.get_section_list().begin();
p != cf.get_section_list().end(); ++p) {
string name = (*p)->get_name();
EntityName ename;
map<string, bufferlist> caps;
if (!ename.from_str(name)) {
derr << "bad entity name: " << name << dendl;
goto done_err;
}
ConfList& cl = (*p)->get_list();
ConfList::iterator cli;
for (cli = cl.begin(); cli != cl.end(); ++ cli) {
ConfLine *line = *cli;
const char *type = line->get_var();
if (!type)
continue;
ret = set_modifier(type, line->get_val(), ename, caps);
if (ret < 0) {
derr << "error setting modifier for [" << name << "] type=" << type << " val=" << line->get_val() << dendl;
goto done_err;
}
}
}
delete[] orig_src;
return;
done_err:
delete[] orig_src;
throw buffer::error();
}
void KeyRing::decode(bufferlist::iterator& bl) {
__u8 struct_v;
bufferlist::iterator start_pos = bl;
try {
::decode(struct_v, bl);
::decode(keys, bl);
} catch (buffer::error& err) {
keys.clear();
decode_plaintext(start_pos);
}
}
int KeyRing::load(const char *filename)
{
if (!filename)
return -EINVAL;
bufferlist bl;
int ret = bl.read_file(filename, true);
if (ret < 0) {
derr << "error reading file: " << filename << dendl;
return ret;
}
try {
bufferlist::iterator iter = bl.begin();
decode(iter);
}
catch (const buffer::error& err) {
derr << "error parsing file " << filename << dendl;
}
dout(2) << "KeyRing::load: loaded key file " << filename << dendl;
return 0;
}
void KeyRing::print(ostream& out)
{
for (map<EntityName, EntityAuth>::iterator p = keys.begin();
p != keys.end();
++p) {
out << "[" << p->first << "]" << std::endl;
out << "\tkey = " << p->second.key << std::endl;
out << "\tauid = " << p->second.auid << std::endl;
for (map<string, bufferlist>::iterator q = p->second.caps.begin();
q != p->second.caps.end();
++q) {
bufferlist::iterator dataiter = q->second.begin();
string caps;
::decode(caps, dataiter);
out << "\tcaps " << q->first << " = \"" << caps << '"' << std::endl;
}
}
}
void KeyRing::import(KeyRing& other)
{
for (map<EntityName, EntityAuth>::iterator p = other.keys.begin();
p != other.keys.end();
++p) {
dout(10) << " importing " << p->first << " " << p->second << dendl;
keys[p->first] = p->second;
}
}