-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdMacaroonsAuthz.cc
430 lines (369 loc) · 13.9 KB
/
XrdMacaroonsAuthz.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <stdexcept>
#include <sstream>
#include <ctime>
#include "macaroons.h"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdSec/XrdSecEntity.hh"
#include "XrdSec/XrdSecEntityAttr.hh"
#include "XrdMacaroonsHandler.hh"
#include "XrdMacaroonsAuthz.hh"
using namespace Macaroons;
namespace {
class AuthzCheck
{
public:
AuthzCheck(const char *req_path, const Access_Operation req_oper, ssize_t m_max_duration, XrdSysError &log);
const std::string &GetSecName() const {return m_sec_name;}
static int verify_before_s(void *authz_ptr,
const unsigned char *pred,
size_t pred_sz);
static int verify_activity_s(void *authz_ptr,
const unsigned char *pred,
size_t pred_sz);
static int verify_path_s(void *authz_ptr,
const unsigned char *pred,
size_t pred_sz);
static int verify_name_s(void *authz_ptr,
const unsigned char *pred,
size_t pred_sz);
private:
int verify_before(const unsigned char *pred, size_t pred_sz);
int verify_activity(const unsigned char *pred, size_t pred_sz);
int verify_path(const unsigned char *pred, size_t pred_sz);
int verify_name(const unsigned char *pred, size_t pred_sz);
ssize_t m_max_duration;
XrdSysError &m_log;
const std::string m_path;
std::string m_desired_activity;
std::string m_sec_name;
Access_Operation m_oper;
time_t m_now;
};
static XrdAccPrivs AddPriv(Access_Operation op, XrdAccPrivs privs)
{
int new_privs = privs;
switch (op) {
case AOP_Any:
break;
case AOP_Chmod:
new_privs |= static_cast<int>(XrdAccPriv_Chmod);
break;
case AOP_Chown:
new_privs |= static_cast<int>(XrdAccPriv_Chown);
break;
case AOP_Excl_Create: // fallthrough
case AOP_Create:
new_privs |= static_cast<int>(XrdAccPriv_Create);
break;
case AOP_Delete:
new_privs |= static_cast<int>(XrdAccPriv_Delete);
break;
case AOP_Excl_Insert: // fallthrough
case AOP_Insert:
new_privs |= static_cast<int>(XrdAccPriv_Insert);
break;
case AOP_Lock:
new_privs |= static_cast<int>(XrdAccPriv_Lock);
break;
case AOP_Mkdir:
new_privs |= static_cast<int>(XrdAccPriv_Mkdir);
break;
case AOP_Read:
new_privs |= static_cast<int>(XrdAccPriv_Read);
break;
case AOP_Readdir:
new_privs |= static_cast<int>(XrdAccPriv_Readdir);
break;
case AOP_Rename:
new_privs |= static_cast<int>(XrdAccPriv_Rename);
break;
case AOP_Stat:
new_privs |= static_cast<int>(XrdAccPriv_Lookup);
break;
case AOP_Update:
new_privs |= static_cast<int>(XrdAccPriv_Update);
break;
};
return static_cast<XrdAccPrivs>(new_privs);
}
}
Authz::Authz(XrdSysLogger *log, char const *config, XrdAccAuthorize *chain)
: m_max_duration(86400),
m_chain(chain),
m_log(log, "macarons_"),
m_authz_behavior(static_cast<int>(Handler::AuthzBehavior::PASSTHROUGH))
{
Handler::AuthzBehavior behavior(Handler::AuthzBehavior::PASSTHROUGH);
XrdOucEnv env;
if (!Handler::Config(config, &env, &m_log, m_location, m_secret, m_max_duration, behavior))
{
throw std::runtime_error("Macaroon authorization config failed.");
}
m_authz_behavior = static_cast<int>(behavior);
}
XrdAccPrivs
Authz::OnMissing(const XrdSecEntity *Entity, const char *path,
const Access_Operation oper, XrdOucEnv *env)
{
switch (m_authz_behavior) {
case Handler::AuthzBehavior::PASSTHROUGH:
return m_chain ? m_chain->Access(Entity, path, oper, env) : XrdAccPriv_None;
case Handler::AuthzBehavior::ALLOW:
return AddPriv(oper, XrdAccPriv_None);;
case Handler::AuthzBehavior::DENY:
return XrdAccPriv_None;
}
// Code should be unreachable.
return XrdAccPriv_None;
}
XrdAccPrivs
Authz::Access(const XrdSecEntity *Entity, const char *path,
const Access_Operation oper, XrdOucEnv *env)
{
const char *authz = env ? env->Get("authz") : nullptr;
// We don't allow any testing to occur in this authz module, preventing
// a macaroon to be used to receive further macaroons.
if (oper == AOP_Any)
{
return m_chain ? m_chain->Access(Entity, path, oper, env) : XrdAccPriv_None;
}
if (!authz || strncmp(authz, "Bearer%20", 9))
{
//m_log.Emsg("Access", "No bearer token present");
return OnMissing(Entity, path, oper, env);
}
authz += 9;
macaroon_returncode mac_err = MACAROON_SUCCESS;
struct macaroon* macaroon = macaroon_deserialize(
authz,
&mac_err);
if (!macaroon)
{
// Do not log - might be other token type!
//m_log.Emsg("Access", "Failed to parse the macaroon");
return OnMissing(Entity, path, oper, env);
}
struct macaroon_verifier *verifier = macaroon_verifier_create();
if (!verifier)
{
m_log.Emsg("Access", "Failed to create a new macaroon verifier");
return XrdAccPriv_None;
}
if (!path)
{
m_log.Emsg("Access", "Request with no provided path.");
macaroon_verifier_destroy(verifier);
return XrdAccPriv_None;
}
AuthzCheck check_helper(path, oper, m_max_duration, m_log);
if (macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_before_s, &check_helper, &mac_err) ||
macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_activity_s, &check_helper, &mac_err) ||
macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_name_s, &check_helper, &mac_err) ||
macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_path_s, &check_helper, &mac_err))
{
m_log.Emsg("Access", "Failed to configure caveat verifier:");
macaroon_verifier_destroy(verifier);
return XrdAccPriv_None;
}
const unsigned char *macaroon_loc;
size_t location_sz;
macaroon_location(macaroon, &macaroon_loc, &location_sz);
if (strncmp(reinterpret_cast<const char *>(macaroon_loc), m_location.c_str(), location_sz))
{
std::string location_str(reinterpret_cast<const char *>(macaroon_loc), location_sz);
m_log.Emsg("Access", "Macaroon is for incorrect location", location_str.c_str());
macaroon_verifier_destroy(verifier);
macaroon_destroy(macaroon);
return m_chain ? m_chain->Access(Entity, path, oper, env) : XrdAccPriv_None;
}
if (macaroon_verify(verifier, macaroon,
reinterpret_cast<const unsigned char *>(m_secret.c_str()),
m_secret.size(),
NULL, 0, // discharge macaroons
&mac_err))
{
m_log.Log(LogMask::Debug, "Access", "Macaroon verification failed");
macaroon_verifier_destroy(verifier);
macaroon_destroy(macaroon);
return m_chain ? m_chain->Access(Entity, path, oper, env) : XrdAccPriv_None;
}
macaroon_verifier_destroy(verifier);
const unsigned char *macaroon_id;
size_t id_sz;
macaroon_identifier(macaroon, &macaroon_id, &id_sz);
std::string macaroon_id_str(reinterpret_cast<const char *>(macaroon_id), id_sz);
m_log.Log(LogMask::Info, "Access", "Macaroon verification successful; ID", macaroon_id_str.c_str());
macaroon_destroy(macaroon);
// Copy the name, if present into the macaroon, into the credential object.
if (Entity && check_helper.GetSecName().size()) {
const std::string &username = check_helper.GetSecName();
m_log.Log(LogMask::Debug, "Access", "Setting the request name to", username.c_str());
Entity->eaAPI->Add("request.name", username);
}
// We passed verification - give the correct privilege.
return AddPriv(oper, XrdAccPriv_None);
}
AuthzCheck::AuthzCheck(const char *req_path, const Access_Operation req_oper, ssize_t max_duration, XrdSysError &log)
: m_max_duration(max_duration),
m_log(log),
m_path(NormalizeSlashes(req_path)),
m_oper(req_oper),
m_now(time(NULL))
{
switch (m_oper)
{
case AOP_Any:
break;
case AOP_Chmod:
case AOP_Chown:
m_desired_activity = "UPDATE_METADATA";
break;
case AOP_Insert:
case AOP_Lock:
case AOP_Mkdir:
case AOP_Update:
case AOP_Create:
m_desired_activity = "MANAGE";
break;
case AOP_Rename:
case AOP_Excl_Create:
case AOP_Excl_Insert:
m_desired_activity = "UPLOAD";
break;
case AOP_Delete:
m_desired_activity = "DELETE";
break;
case AOP_Read:
m_desired_activity = "DOWNLOAD";
break;
case AOP_Readdir:
m_desired_activity = "LIST";
break;
case AOP_Stat:
m_desired_activity = "READ_METADATA";
};
}
int
AuthzCheck::verify_before_s(void *authz_ptr,
const unsigned char *pred,
size_t pred_sz)
{
return static_cast<AuthzCheck*>(authz_ptr)->verify_before(pred, pred_sz);
}
int
AuthzCheck::verify_activity_s(void *authz_ptr,
const unsigned char *pred,
size_t pred_sz)
{
return static_cast<AuthzCheck*>(authz_ptr)->verify_activity(pred, pred_sz);
}
int
AuthzCheck::verify_path_s(void *authz_ptr,
const unsigned char *pred,
size_t pred_sz)
{
return static_cast<AuthzCheck*>(authz_ptr)->verify_path(pred, pred_sz);
}
int
AuthzCheck::verify_name_s(void *authz_ptr,
const unsigned char *pred,
size_t pred_sz)
{
return static_cast<AuthzCheck*>(authz_ptr)->verify_name(pred, pred_sz);
}
int
AuthzCheck::verify_before(const unsigned char * pred, size_t pred_sz)
{
std::string pred_str(reinterpret_cast<const char *>(pred), pred_sz);
if (strncmp("before:", pred_str.c_str(), 7))
{
return 1;
}
m_log.Log(LogMask::Debug, "AuthzCheck", "running verify before", pred_str.c_str());
struct tm caveat_tm;
if (strptime(&pred_str[7], "%Y-%m-%dT%H:%M:%SZ", &caveat_tm) == nullptr)
{
m_log.Log(LogMask::Debug, "AuthzCheck", "failed to parse time string", &pred_str[7]);
return 1;
}
caveat_tm.tm_isdst = -1;
time_t caveat_time = timegm(&caveat_tm);
if (-1 == caveat_time)
{
m_log.Log(LogMask::Debug, "AuthzCheck", "failed to generate unix time", &pred_str[7]);
return 1;
}
if ((m_max_duration > 0) && (caveat_time > m_now + m_max_duration))
{
m_log.Log(LogMask::Warning, "AuthzCheck", "Max token age is greater than configured max duration; rejecting");
return 1;
}
int result = (m_now >= caveat_time);
if (!result) m_log.Log(LogMask::Debug, "AuthzCheck", "verify before successful");
else m_log.Log(LogMask::Debug, "AuthzCheck", "verify before failed");
return result;
}
int
AuthzCheck::verify_activity(const unsigned char * pred, size_t pred_sz)
{
if (!m_desired_activity.size()) {return 1;}
std::string pred_str(reinterpret_cast<const char *>(pred), pred_sz);
if (strncmp("activity:", pred_str.c_str(), 9)) {return 1;}
m_log.Log(LogMask::Debug, "AuthzCheck", "running verify activity", pred_str.c_str());
std::stringstream ss(pred_str.substr(9));
for (std::string activity; std::getline(ss, activity, ','); )
{
// Any allowed activity also implies "READ_METADATA"
if (m_desired_activity == "READ_METADATA") {return 0;}
if ((activity == m_desired_activity) || ((m_desired_activity == "UPLOAD") && (activity == "MANAGE")))
{
m_log.Log(LogMask::Debug, "AuthzCheck", "macaroon has desired activity", activity.c_str());
return 0;
}
}
m_log.Log(LogMask::Info, "AuthzCheck", "macaroon does NOT have desired activity", m_desired_activity.c_str());
return 1;
}
int
AuthzCheck::verify_path(const unsigned char * pred, size_t pred_sz)
{
std::string pred_str_raw(reinterpret_cast<const char *>(pred), pred_sz);
if (strncmp("path:", pred_str_raw.c_str(), 5)) {return 1;}
std::string pred_str = NormalizeSlashes(pred_str_raw.substr(5));
m_log.Log(LogMask::Debug, "AuthzCheck", "running verify path", pred_str.c_str());
if ((m_path.find("/./") != std::string::npos) ||
(m_path.find("/../") != std::string::npos))
{
m_log.Log(LogMask::Info, "AuthzCheck", "invalid requested path", m_path.c_str());
return 1;
}
int result = strncmp(pred_str.c_str(), m_path.c_str(), pred_str.size());
if (!result)
{
m_log.Log(LogMask::Debug, "AuthzCheck", "path request verified for", m_path.c_str());
}
// READ_METADATA permission for /foo/bar automatically implies permission
// to READ_METADATA for /foo.
else if (m_oper == AOP_Stat)
{
result = strncmp(m_path.c_str(), pred_str.c_str(), m_path.size());
if (!result) {m_log.Log(LogMask::Debug, "AuthzCheck", "READ_METADATA path request verified for", m_path.c_str());}
else {m_log.Log(LogMask::Debug, "AuthzCheck", "READ_METADATA path request NOT allowed", m_path.c_str());}
}
else
{
m_log.Log(LogMask::Debug, "AuthzCheck", "path request NOT allowed", m_path.c_str());
}
return result;
}
int
AuthzCheck::verify_name(const unsigned char * pred, size_t pred_sz)
{
std::string pred_str(reinterpret_cast<const char *>(pred), pred_sz);
if (strncmp("name:", pred_str.c_str(), 5)) {return 1;}
if (pred_str.size() < 6) {return 1;}
m_log.Log(LogMask::Debug, "AuthzCheck", "Verifying macaroon with", pred_str.c_str());
// Make a copy of the name for the XrdSecEntity; this will be used later.
m_sec_name = pred_str.substr(5);
return 0;
}