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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
// -*- 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 <sage@newdream.net>
*
* 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 <memory>
#include <sstream>
#include <algorithm>
#include <boost/algorithm/string/replace.hpp>
#include "auth/KeyRing.h"
#include "include/stringify.h"
#include "common/ceph_context.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/Formatter.h"
#define dout_subsys ceph_subsys_auth
#undef dout_prefix
#define dout_prefix *_dout << "auth: "
using std::map;
using std::ostream;
using std::ostringstream;
using std::string;
using ceph::bufferlist;
using ceph::Formatter;
int KeyRing::from_ceph_context(CephContext *cct)
{
const auto& conf = cct->_conf;
string filename;
int ret = ceph_resolve_file_search(conf->keyring, filename);
if (!ret) {
ret = load(cct, filename);
if (ret < 0)
lderr(cct) << "failed to load " << filename
<< ": " << cpp_strerror(ret) << dendl;
} else if (conf->key.empty() && conf->keyfile.empty()) {
lderr(cct) << "unable to find a keyring on " << conf->keyring
<< ": " << cpp_strerror(ret) << dendl;
}
if (!conf->key.empty()) {
EntityAuth ea;
try {
ea.key.decode_base64(conf->key);
add(conf->name, ea);
return 0;
}
catch (ceph::buffer::error& e) {
lderr(cct) << "failed to decode key '" << conf->key << "'" << dendl;
return -EINVAL;
}
}
if (!conf->keyfile.empty()) {
bufferlist bl;
string err;
int r = bl.read_file(conf->keyfile.c_str(), &err);
if (r < 0) {
lderr(cct) << err << dendl;
return r;
}
string k(bl.c_str(), bl.length());
EntityAuth ea;
try {
ea.key.decode_base64(k);
add(conf->name, ea);
}
catch (ceph::buffer::error& e) {
lderr(cct) << "failed to decode key '" << k << "'" << dendl;
return -EINVAL;
}
return 0;
}
return ret;
}
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);
try {
key.decode_base64(l);
} catch (const ceph::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);
bufferlist bl;
encode(l, bl);
caps[caps_entity] = bl;
set_caps(name, caps);
} else if (strcmp(type, "auid") == 0) {
// just ignore it so we can still decode "old" keyrings that have an auid
} else
return -EINVAL;
return 0;
}
void KeyRing::encode_plaintext(bufferlist& bl)
{
std::ostringstream os;
print(os);
string str = os.str();
bl.append(str);
}
void KeyRing::encode_formatted(string label, Formatter *f, bufferlist& bl)
{
f->open_array_section(label.c_str());
for (const auto &[ename, eauth] : keys) {
f->open_object_section("auth_entities");
f->dump_string("entity", ename.to_str().c_str());
f->dump_string("key", stringify(eauth.key));
if (!eauth.pending_key.empty()) {
f->dump_string("pending_key", stringify(eauth.pending_key));
}
f->open_object_section("caps");
for (auto& [sys, capsbl] : eauth.caps) {
auto dataiter = capsbl.cbegin();
string caps;
ceph::decode(caps, dataiter);
f->dump_string(sys.c_str(), caps);
}
f->close_section(); /* caps */
f->close_section(); /* auth_entities */
}
f->close_section(); /* auth_dump */
f->flush(bl);
}
void KeyRing::decode(bufferlist::const_iterator& bli)
{
int ret;
bufferlist bl;
bli.copy_all(bl);
ConfFile cf;
if (cf.parse_bufferlist(&bl, nullptr) != 0) {
throw ceph::buffer::malformed_input("cannot parse buffer");
}
for (auto& [name, section] : cf) {
if (name == "global")
continue;
EntityName ename;
map<string, bufferlist> caps;
if (!ename.from_str(name)) {
ostringstream oss;
oss << "bad entity name in keyring: " << name;
throw ceph::buffer::malformed_input(oss.str().c_str());
}
for (auto& [k, val] : section) {
if (k.empty())
continue;
string key;
std::replace_copy(k.begin(), k.end(), back_inserter(key), '_', ' ');
ret = set_modifier(key.c_str(), val.c_str(), ename, caps);
if (ret < 0) {
ostringstream oss;
oss << "error setting modifier for [" << name << "] type=" << key
<< " val=" << val;
throw ceph::buffer::malformed_input(oss.str().c_str());
}
}
}
}
int KeyRing::load(CephContext *cct, const std::string &filename)
{
if (filename.empty())
return -EINVAL;
bufferlist bl;
std::string err;
int ret = bl.read_file(filename.c_str(), &err);
if (ret < 0) {
lderr(cct) << "error reading file: " << filename << ": " << err << dendl;
return ret;
}
try {
auto iter = bl.cbegin();
decode(iter);
}
catch (const ceph::buffer::error& err) {
lderr(cct) << "error parsing file " << filename << ": " << err.what() << dendl;
return -EIO;
}
ldout(cct, 2) << "KeyRing::load: loaded key file " << filename << dendl;
return 0;
}
void KeyRing::print(ostream& out)
{
for (auto& [ename, eauth] : keys) {
out << "[" << ename << "]" << std::endl;
out << "\tkey = " << eauth.key << std::endl;
if (!eauth.pending_key.empty()) {
out << "\tpending key = " << eauth.pending_key << std::endl;
}
for (auto& [sys, capbl] : eauth.caps) {
auto dataiter = capbl.cbegin();
string caps;
ceph::decode(caps, dataiter);
boost::replace_all(caps, "\"", "\\\"");
out << "\tcaps " << sys << " = \"" << caps << '"' << std::endl;
}
}
}
void KeyRing::import(CephContext *cct, KeyRing& other)
{
for (map<EntityName, EntityAuth>::iterator p = other.keys.begin();
p != other.keys.end();
++p) {
ldout(cct, 10) << " importing " << p->first << dendl;
ldout(cct, 30) << " " << p->second << dendl;
keys[p->first] = p->second;
}
}
|