]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include <array> | |
37b3c998 | 5 | #include <algorithm> |
7c673cae | 6 | |
d2e6a577 | 7 | #include <boost/utility/string_view.hpp> |
7c673cae | 8 | #include <boost/container/static_vector.hpp> |
d2e6a577 FG |
9 | #include <boost/algorithm/string/predicate.hpp> |
10 | #include <boost/algorithm/string.hpp> | |
7c673cae FG |
11 | |
12 | #include "rgw_swift_auth.h" | |
13 | #include "rgw_rest.h" | |
14 | ||
15 | #include "common/ceph_crypto.h" | |
16 | #include "common/Clock.h" | |
17 | ||
18 | #include "auth/Crypto.h" | |
19 | ||
20 | #include "rgw_client_io.h" | |
21 | #include "rgw_http_client.h" | |
22 | #include "include/str_list.h" | |
23 | ||
24 | #define dout_context g_ceph_context | |
25 | #define dout_subsys ceph_subsys_rgw | |
26 | ||
27 | #define DEFAULT_SWIFT_PREFIX "/swift" | |
28 | ||
29 | using namespace ceph::crypto; | |
30 | ||
31 | ||
32 | namespace rgw { | |
33 | namespace auth { | |
34 | namespace swift { | |
35 | ||
36 | /* TempURL: applier */ | |
37 | void TempURLApplier::modify_request_state(req_state* s) const /* in/out */ | |
38 | { | |
39 | bool inline_exists = false; | |
40 | const std::string& filename = s->info.args.get("filename"); | |
41 | ||
42 | s->info.args.get("inline", &inline_exists); | |
43 | if (inline_exists) { | |
44 | s->content_disp.override = "inline"; | |
45 | } else if (!filename.empty()) { | |
46 | std::string fenc; | |
47 | url_encode(filename, fenc); | |
48 | s->content_disp.override = "attachment; filename=\"" + fenc + "\""; | |
49 | } else { | |
50 | std::string fenc; | |
51 | url_encode(s->object.name, fenc); | |
52 | s->content_disp.fallback = "attachment; filename=\"" + fenc + "\""; | |
53 | } | |
54 | ||
55 | ldout(s->cct, 20) << "finished applying changes to req_state for TempURL: " | |
56 | << " content_disp override " << s->content_disp.override | |
57 | << " content_disp fallback " << s->content_disp.fallback | |
58 | << dendl; | |
59 | ||
60 | } | |
61 | ||
62 | /* TempURL: engine */ | |
63 | bool TempURLEngine::is_applicable(const req_state* const s) const noexcept | |
64 | { | |
65 | return s->info.args.exists("temp_url_sig") || | |
66 | s->info.args.exists("temp_url_expires"); | |
67 | } | |
68 | ||
69 | void TempURLEngine::get_owner_info(const req_state* const s, | |
70 | RGWUserInfo& owner_info) const | |
71 | { | |
72 | /* We cannot use req_state::bucket_name because it isn't available | |
73 | * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */ | |
74 | const string& bucket_name = s->init_state.url_bucket; | |
75 | ||
76 | /* TempURL requires that bucket and object names are specified. */ | |
77 | if (bucket_name.empty() || s->object.empty()) { | |
78 | throw -EPERM; | |
79 | } | |
80 | ||
81 | /* TempURL case is completely different than the Keystone auth - you may | |
82 | * get account name only through extraction from URL. In turn, knowledge | |
83 | * about account is neccessary to obtain its bucket tenant. Without that, | |
84 | * the access would be limited to accounts with empty tenant. */ | |
85 | string bucket_tenant; | |
86 | if (!s->account_name.empty()) { | |
87 | RGWUserInfo uinfo; | |
88 | bool found = false; | |
89 | ||
90 | const rgw_user uid(s->account_name); | |
91 | if (uid.tenant.empty()) { | |
92 | const rgw_user tenanted_uid(uid.id, uid.id); | |
93 | ||
94 | if (rgw_get_user_info_by_uid(store, tenanted_uid, uinfo) >= 0) { | |
95 | /* Succeeded. */ | |
96 | bucket_tenant = uinfo.user_id.tenant; | |
97 | found = true; | |
98 | } | |
99 | } | |
100 | ||
101 | if (!found && rgw_get_user_info_by_uid(store, uid, uinfo) < 0) { | |
102 | throw -EPERM; | |
103 | } else { | |
104 | bucket_tenant = uinfo.user_id.tenant; | |
105 | } | |
106 | } | |
107 | ||
108 | /* Need to get user info of bucket owner. */ | |
109 | RGWBucketInfo bucket_info; | |
110 | int ret = store->get_bucket_info(*static_cast<RGWObjectCtx *>(s->obj_ctx), | |
111 | bucket_tenant, bucket_name, | |
112 | bucket_info, nullptr); | |
113 | if (ret < 0) { | |
114 | throw ret; | |
115 | } | |
116 | ||
117 | ldout(cct, 20) << "temp url user (bucket owner): " << bucket_info.owner | |
118 | << dendl; | |
119 | ||
120 | if (rgw_get_user_info_by_uid(store, bucket_info.owner, owner_info) < 0) { | |
121 | throw -EPERM; | |
122 | } | |
123 | } | |
124 | ||
125 | bool TempURLEngine::is_expired(const std::string& expires) const | |
126 | { | |
127 | string err; | |
128 | const utime_t now = ceph_clock_now(); | |
129 | const uint64_t expiration = (uint64_t)strict_strtoll(expires.c_str(), | |
130 | 10, &err); | |
131 | if (!err.empty()) { | |
132 | dout(5) << "failed to parse temp_url_expires: " << err << dendl; | |
133 | return true; | |
134 | } | |
135 | ||
136 | if (expiration <= (uint64_t)now.sec()) { | |
137 | dout(5) << "temp url expired: " << expiration << " <= " << now.sec() << dendl; | |
138 | return true; | |
139 | } | |
140 | ||
141 | return false; | |
142 | } | |
143 | ||
37b3c998 TL |
144 | bool TempURLEngine::is_disallowed_header_present(const req_info& info) const |
145 | { | |
146 | static const auto headers = { | |
147 | "HTTP_X_OBJECT_MANIFEST", | |
148 | }; | |
149 | ||
150 | return std::any_of(std::begin(headers), std::end(headers), | |
151 | [&info](const char* header) { | |
152 | return info.env->exists(header); | |
153 | }); | |
154 | } | |
155 | ||
156 | std::string extract_swift_subuser(const std::string& swift_user_name) | |
157 | { | |
7c673cae FG |
158 | size_t pos = swift_user_name.find(':'); |
159 | if (std::string::npos == pos) { | |
160 | return swift_user_name; | |
161 | } else { | |
162 | return swift_user_name.substr(pos + 1); | |
163 | } | |
164 | } | |
165 | ||
166 | class TempURLEngine::SignatureHelper | |
167 | { | |
168 | private: | |
169 | static constexpr uint32_t output_size = | |
170 | CEPH_CRYPTO_HMACSHA1_DIGESTSIZE * 2 + 1; | |
171 | ||
172 | unsigned char dest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; // 20 | |
173 | char dest_str[output_size]; | |
174 | ||
175 | public: | |
176 | SignatureHelper() = default; | |
177 | ||
178 | const char* calc(const std::string& key, | |
d2e6a577 FG |
179 | const boost::string_view& method, |
180 | const boost::string_view& path, | |
7c673cae FG |
181 | const std::string& expires) { |
182 | ||
183 | using ceph::crypto::HMACSHA1; | |
184 | using UCHARPTR = const unsigned char*; | |
185 | ||
186 | HMACSHA1 hmac((UCHARPTR) key.c_str(), key.size()); | |
187 | hmac.Update((UCHARPTR) method.data(), method.size()); | |
188 | hmac.Update((UCHARPTR) "\n", 1); | |
189 | hmac.Update((UCHARPTR) expires.c_str(), expires.size()); | |
190 | hmac.Update((UCHARPTR) "\n", 1); | |
191 | hmac.Update((UCHARPTR) path.data(), path.size()); | |
192 | hmac.Final(dest); | |
193 | ||
194 | buf_to_hex((UCHARPTR) dest, sizeof(dest), dest_str); | |
195 | ||
196 | return dest_str; | |
197 | } | |
198 | ||
199 | bool is_equal_to(const std::string& rhs) const { | |
200 | /* never allow out-of-range exception */ | |
201 | if (rhs.size() < (output_size - 1)) { | |
202 | return false; | |
203 | } | |
204 | return rhs.compare(0 /* pos */, output_size, dest_str) == 0; | |
205 | } | |
206 | ||
207 | }; /* TempURLEngine::SignatureHelper */ | |
208 | ||
d2e6a577 FG |
209 | class TempURLEngine::PrefixableSignatureHelper |
210 | : private TempURLEngine::SignatureHelper { | |
211 | using base_t = SignatureHelper; | |
212 | ||
213 | const boost::string_view decoded_uri; | |
214 | const boost::string_view object_name; | |
215 | boost::string_view no_obj_uri; | |
216 | ||
217 | const boost::optional<const std::string&> prefix; | |
218 | ||
219 | public: | |
3efd9988 | 220 | PrefixableSignatureHelper(const std::string& _decoded_uri, |
d2e6a577 FG |
221 | const std::string& object_name, |
222 | const boost::optional<const std::string&> prefix) | |
3efd9988 | 223 | : decoded_uri(_decoded_uri), |
d2e6a577 FG |
224 | object_name(object_name), |
225 | prefix(prefix) { | |
3efd9988 FG |
226 | /* Transform: v1/acct/cont/obj - > v1/acct/cont/ |
227 | * | |
228 | * NOTE(rzarzynski): we really want to substr() on boost::string_view, | |
229 | * not std::string. Otherwise we would end with no_obj_uri referencing | |
230 | * a temporary. */ | |
d2e6a577 FG |
231 | no_obj_uri = \ |
232 | decoded_uri.substr(0, decoded_uri.length() - object_name.length()); | |
233 | } | |
234 | ||
235 | const char* calc(const std::string& key, | |
236 | const boost::string_view& method, | |
237 | const boost::string_view& path, | |
238 | const std::string& expires) { | |
239 | if (!prefix) { | |
240 | return base_t::calc(key, method, path, expires); | |
241 | } else { | |
242 | const auto prefixed_path = \ | |
243 | string_cat_reserve("prefix:", no_obj_uri, *prefix); | |
244 | return base_t::calc(key, method, prefixed_path, expires); | |
245 | } | |
246 | } | |
247 | ||
248 | bool is_equal_to(const std::string& rhs) const { | |
249 | bool is_auth_ok = base_t::is_equal_to(rhs); | |
250 | ||
251 | if (prefix && is_auth_ok) { | |
252 | const auto prefix_uri = string_cat_reserve(no_obj_uri, *prefix); | |
253 | is_auth_ok = boost::algorithm::starts_with(decoded_uri, prefix_uri); | |
254 | } | |
255 | ||
256 | return is_auth_ok; | |
257 | } | |
258 | }; /* TempURLEngine::PrefixableSignatureHelper */ | |
259 | ||
7c673cae FG |
260 | TempURLEngine::result_t |
261 | TempURLEngine::authenticate(const req_state* const s) const | |
262 | { | |
263 | if (! is_applicable(s)) { | |
264 | return result_t::deny(); | |
265 | } | |
266 | ||
d2e6a577 FG |
267 | /* NOTE(rzarzynski): RGWHTTPArgs::get(), in contrast to RGWEnv::get(), |
268 | * never returns nullptr. If the requested parameter is absent, we will | |
269 | * get the empty string. */ | |
270 | const std::string& temp_url_sig = s->info.args.get("temp_url_sig"); | |
271 | const std::string& temp_url_expires = s->info.args.get("temp_url_expires"); | |
7c673cae FG |
272 | |
273 | if (temp_url_sig.empty() || temp_url_expires.empty()) { | |
274 | return result_t::deny(); | |
275 | } | |
276 | ||
d2e6a577 FG |
277 | /* Though, for prefixed tempurls we need to differentiate between empty |
278 | * prefix and lack of prefix. Empty prefix means allowance for whole | |
279 | * container. */ | |
280 | const boost::optional<const std::string&> temp_url_prefix = \ | |
281 | s->info.args.get_optional("temp_url_prefix"); | |
282 | ||
7c673cae FG |
283 | RGWUserInfo owner_info; |
284 | try { | |
285 | get_owner_info(s, owner_info); | |
286 | } catch (...) { | |
287 | ldout(cct, 5) << "cannot get user_info of account's owner" << dendl; | |
288 | return result_t::reject(); | |
289 | } | |
290 | ||
291 | if (owner_info.temp_url_keys.empty()) { | |
292 | ldout(cct, 5) << "user does not have temp url key set, aborting" << dendl; | |
293 | return result_t::reject(); | |
294 | } | |
295 | ||
296 | if (is_expired(temp_url_expires)) { | |
297 | ldout(cct, 5) << "temp url link expired" << dendl; | |
224ce89b | 298 | return result_t::reject(-EPERM); |
7c673cae FG |
299 | } |
300 | ||
37b3c998 TL |
301 | if (is_disallowed_header_present(s->info)) { |
302 | ldout(cct, 5) << "temp url rejected due to disallowed header" << dendl; | |
303 | return result_t::reject(-EINVAL); | |
304 | } | |
305 | ||
7c673cae FG |
306 | /* We need to verify two paths because of compliance with Swift, Tempest |
307 | * and old versions of RadosGW. The second item will have the prefix | |
308 | * of Swift API entry point removed. */ | |
309 | ||
310 | /* XXX can we search this ONCE? */ | |
311 | const size_t pos = g_conf->rgw_swift_url_prefix.find_last_not_of('/') + 1; | |
d2e6a577 FG |
312 | const boost::string_view ref_uri = s->decoded_uri; |
313 | const std::array<boost::string_view, 2> allowed_paths = { | |
7c673cae FG |
314 | ref_uri, |
315 | ref_uri.substr(pos + 1) | |
316 | }; | |
317 | ||
318 | /* Account owner calculates the signature also against a HTTP method. */ | |
d2e6a577 | 319 | boost::container::static_vector<boost::string_view, 3> allowed_methods; |
7c673cae FG |
320 | if (strcmp("HEAD", s->info.method) == 0) { |
321 | /* HEAD requests are specially handled. */ | |
322 | /* TODO: after getting a newer boost (with static_vector supporting | |
323 | * initializers lists), get back to the good notation: | |
324 | * allowed_methods = {"HEAD", "GET", "PUT" }; | |
325 | * Just for now let's use emplace_back to construct the vector. */ | |
326 | allowed_methods.emplace_back("HEAD"); | |
327 | allowed_methods.emplace_back("GET"); | |
328 | allowed_methods.emplace_back("PUT"); | |
329 | } else if (strlen(s->info.method) > 0) { | |
330 | allowed_methods.emplace_back(s->info.method); | |
331 | } | |
332 | ||
333 | /* Need to try each combination of keys, allowed path and methods. */ | |
d2e6a577 FG |
334 | PrefixableSignatureHelper sig_helper { |
335 | s->decoded_uri, | |
336 | s->object.name, | |
337 | temp_url_prefix | |
338 | }; | |
339 | ||
7c673cae FG |
340 | for (const auto& kv : owner_info.temp_url_keys) { |
341 | const int temp_url_key_num = kv.first; | |
342 | const string& temp_url_key = kv.second; | |
343 | ||
344 | if (temp_url_key.empty()) { | |
345 | continue; | |
346 | } | |
347 | ||
348 | for (const auto& path : allowed_paths) { | |
349 | for (const auto& method : allowed_methods) { | |
350 | const char* const local_sig = sig_helper.calc(temp_url_key, method, | |
351 | path, temp_url_expires); | |
352 | ||
353 | ldout(s->cct, 20) << "temp url signature [" << temp_url_key_num | |
354 | << "] (calculated): " << local_sig | |
355 | << dendl; | |
356 | ||
357 | if (sig_helper.is_equal_to(temp_url_sig)) { | |
358 | auto apl = apl_factory->create_apl_turl(cct, s, owner_info); | |
359 | return result_t::grant(std::move(apl)); | |
360 | } else { | |
361 | ldout(s->cct, 5) << "temp url signature mismatch: " << local_sig | |
362 | << " != " << temp_url_sig << dendl; | |
363 | } | |
364 | } | |
365 | } | |
366 | } | |
367 | ||
368 | return result_t::reject(); | |
369 | } | |
370 | ||
371 | ||
372 | /* External token */ | |
373 | bool ExternalTokenEngine::is_applicable(const std::string& token) const noexcept | |
374 | { | |
375 | if (token.empty()) { | |
376 | return false; | |
377 | } else if (g_conf->rgw_swift_auth_url.empty()) { | |
378 | return false; | |
379 | } else { | |
380 | return true; | |
381 | } | |
382 | } | |
383 | ||
384 | ExternalTokenEngine::result_t | |
385 | ExternalTokenEngine::authenticate(const std::string& token, | |
386 | const req_state* const s) const | |
387 | { | |
388 | if (! is_applicable(token)) { | |
389 | return result_t::deny(); | |
390 | } | |
391 | ||
392 | std::string auth_url = g_conf->rgw_swift_auth_url; | |
31f18b77 | 393 | if (auth_url.back() != '/') { |
7c673cae FG |
394 | auth_url.append("/"); |
395 | } | |
396 | ||
397 | auth_url.append("token"); | |
398 | char url_buf[auth_url.size() + 1 + token.length() + 1]; | |
399 | sprintf(url_buf, "%s/%s", auth_url.c_str(), token.c_str()); | |
400 | ||
401 | RGWHTTPHeadersCollector validator(cct, { "X-Auth-Groups", "X-Auth-Ttl" }); | |
402 | ||
403 | ldout(cct, 10) << "rgw_swift_validate_token url=" << url_buf << dendl; | |
404 | ||
405 | int ret = validator.process(url_buf); | |
406 | if (ret < 0) { | |
407 | throw ret; | |
408 | } | |
409 | ||
410 | std::string swift_user; | |
411 | try { | |
412 | std::vector<std::string> swift_groups; | |
413 | get_str_vec(validator.get_header_value("X-Auth-Groups"), | |
414 | ",", swift_groups); | |
415 | ||
416 | if (0 == swift_groups.size()) { | |
31f18b77 | 417 | return result_t::deny(-EPERM); |
7c673cae FG |
418 | } else { |
419 | swift_user = std::move(swift_groups[0]); | |
420 | } | |
421 | } catch (std::out_of_range) { | |
422 | /* The X-Auth-Groups header isn't present in the response. */ | |
31f18b77 | 423 | return result_t::deny(-EPERM); |
7c673cae FG |
424 | } |
425 | ||
426 | if (swift_user.empty()) { | |
31f18b77 | 427 | return result_t::deny(-EPERM); |
7c673cae FG |
428 | } |
429 | ||
430 | ldout(cct, 10) << "swift user=" << swift_user << dendl; | |
431 | ||
432 | RGWUserInfo tmp_uinfo; | |
433 | ret = rgw_get_user_info_by_swift(store, swift_user, tmp_uinfo); | |
434 | if (ret < 0) { | |
435 | ldout(cct, 0) << "NOTICE: couldn't map swift user" << dendl; | |
436 | throw ret; | |
437 | } | |
438 | ||
439 | auto apl = apl_factory->create_apl_local(cct, s, tmp_uinfo, | |
440 | extract_swift_subuser(swift_user)); | |
441 | return result_t::grant(std::move(apl)); | |
442 | } | |
443 | ||
444 | static int build_token(const string& swift_user, | |
445 | const string& key, | |
446 | const uint64_t nonce, | |
447 | const utime_t& expiration, | |
448 | bufferlist& bl) | |
449 | { | |
450 | ::encode(swift_user, bl); | |
451 | ::encode(nonce, bl); | |
452 | ::encode(expiration, bl); | |
453 | ||
454 | bufferptr p(CEPH_CRYPTO_HMACSHA1_DIGESTSIZE); | |
455 | ||
456 | char buf[bl.length() * 2 + 1]; | |
457 | buf_to_hex((const unsigned char *)bl.c_str(), bl.length(), buf); | |
458 | dout(20) << "build_token token=" << buf << dendl; | |
459 | ||
460 | char k[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; | |
461 | memset(k, 0, sizeof(k)); | |
462 | const char *s = key.c_str(); | |
463 | for (int i = 0; i < (int)key.length(); i++, s++) { | |
464 | k[i % CEPH_CRYPTO_HMACSHA1_DIGESTSIZE] |= *s; | |
465 | } | |
466 | calc_hmac_sha1(k, sizeof(k), bl.c_str(), bl.length(), p.c_str()); | |
467 | ||
468 | bl.append(p); | |
469 | ||
470 | return 0; | |
471 | ||
472 | } | |
473 | ||
474 | static int encode_token(CephContext *cct, string& swift_user, string& key, | |
475 | bufferlist& bl) | |
476 | { | |
477 | uint64_t nonce; | |
478 | ||
479 | int ret = get_random_bytes((char *)&nonce, sizeof(nonce)); | |
480 | if (ret < 0) | |
481 | return ret; | |
482 | ||
483 | utime_t expiration = ceph_clock_now(); | |
484 | expiration += cct->_conf->rgw_swift_token_expiration; | |
485 | ||
486 | return build_token(swift_user, key, nonce, expiration, bl); | |
487 | } | |
488 | ||
489 | ||
490 | /* AUTH_rgwtk (signed token): engine */ | |
491 | bool SignedTokenEngine::is_applicable(const std::string& token) const noexcept | |
492 | { | |
493 | if (token.empty()) { | |
494 | return false; | |
495 | } else { | |
496 | return token.compare(0, 10, "AUTH_rgwtk") == 0; | |
497 | } | |
498 | } | |
499 | ||
500 | SignedTokenEngine::result_t | |
501 | SignedTokenEngine::authenticate(const std::string& token, | |
502 | const req_state* const s) const | |
503 | { | |
504 | if (! is_applicable(token)) { | |
31f18b77 | 505 | return result_t::deny(-EPERM); |
7c673cae FG |
506 | } |
507 | ||
508 | /* Effective token string is the part after the prefix. */ | |
509 | const std::string etoken = token.substr(strlen("AUTH_rgwtk")); | |
510 | const size_t etoken_len = etoken.length(); | |
511 | ||
512 | if (etoken_len & 1) { | |
513 | ldout(cct, 0) << "NOTICE: failed to verify token: odd token length=" | |
514 | << etoken_len << dendl; | |
515 | throw -EINVAL; | |
516 | } | |
517 | ||
518 | ceph::bufferptr p(etoken_len/2); | |
519 | int ret = hex_to_buf(etoken.c_str(), p.c_str(), etoken_len); | |
520 | if (ret < 0) { | |
521 | throw ret; | |
522 | } | |
523 | ||
524 | ceph::bufferlist tok_bl; | |
525 | tok_bl.append(p); | |
526 | ||
527 | uint64_t nonce; | |
528 | utime_t expiration; | |
529 | std::string swift_user; | |
530 | ||
531 | try { | |
532 | /*const*/ auto iter = tok_bl.begin(); | |
533 | ||
534 | ::decode(swift_user, iter); | |
535 | ::decode(nonce, iter); | |
536 | ::decode(expiration, iter); | |
537 | } catch (buffer::error& err) { | |
538 | ldout(cct, 0) << "NOTICE: failed to decode token" << dendl; | |
539 | throw -EINVAL; | |
540 | } | |
541 | ||
542 | const utime_t now = ceph_clock_now(); | |
543 | if (expiration < now) { | |
544 | ldout(cct, 0) << "NOTICE: old timed out token was used now=" << now | |
545 | << " token.expiration=" << expiration | |
546 | << dendl; | |
31f18b77 | 547 | return result_t::deny(-EPERM); |
7c673cae FG |
548 | } |
549 | ||
550 | RGWUserInfo user_info; | |
551 | ret = rgw_get_user_info_by_swift(store, swift_user, user_info); | |
552 | if (ret < 0) { | |
553 | throw ret; | |
554 | } | |
555 | ||
556 | ldout(cct, 10) << "swift_user=" << swift_user << dendl; | |
557 | ||
558 | const auto siter = user_info.swift_keys.find(swift_user); | |
559 | if (siter == std::end(user_info.swift_keys)) { | |
31f18b77 | 560 | return result_t::deny(-EPERM); |
7c673cae FG |
561 | } |
562 | ||
563 | const auto swift_key = siter->second; | |
564 | ||
565 | bufferlist local_tok_bl; | |
566 | ret = build_token(swift_user, swift_key.key, nonce, expiration, local_tok_bl); | |
567 | if (ret < 0) { | |
568 | throw ret; | |
569 | } | |
570 | ||
571 | if (local_tok_bl.length() != tok_bl.length()) { | |
572 | ldout(cct, 0) << "NOTICE: tokens length mismatch:" | |
573 | << " tok_bl.length()=" << tok_bl.length() | |
574 | << " local_tok_bl.length()=" << local_tok_bl.length() | |
575 | << dendl; | |
31f18b77 | 576 | return result_t::deny(-EPERM); |
7c673cae FG |
577 | } |
578 | ||
579 | if (memcmp(local_tok_bl.c_str(), tok_bl.c_str(), | |
580 | local_tok_bl.length()) != 0) { | |
581 | char buf[local_tok_bl.length() * 2 + 1]; | |
582 | ||
583 | buf_to_hex(reinterpret_cast<const unsigned char *>(local_tok_bl.c_str()), | |
584 | local_tok_bl.length(), buf); | |
585 | ||
586 | ldout(cct, 0) << "NOTICE: tokens mismatch tok=" << buf << dendl; | |
31f18b77 | 587 | return result_t::deny(-EPERM); |
7c673cae FG |
588 | } |
589 | ||
590 | auto apl = apl_factory->create_apl_local(cct, s, user_info, | |
591 | extract_swift_subuser(swift_user)); | |
592 | return result_t::grant(std::move(apl)); | |
593 | } | |
594 | ||
595 | } /* namespace swift */ | |
596 | } /* namespace auth */ | |
597 | } /* namespace rgw */ | |
598 | ||
599 | ||
600 | void RGW_SWIFT_Auth_Get::execute() | |
601 | { | |
602 | int ret = -EPERM; | |
603 | ||
604 | const char *key = s->info.env->get("HTTP_X_AUTH_KEY"); | |
605 | const char *user = s->info.env->get("HTTP_X_AUTH_USER"); | |
606 | ||
607 | s->prot_flags |= RGW_REST_SWIFT; | |
608 | ||
609 | string user_str; | |
610 | RGWUserInfo info; | |
611 | bufferlist bl; | |
612 | RGWAccessKey *swift_key; | |
613 | map<string, RGWAccessKey>::iterator siter; | |
614 | ||
615 | string swift_url = g_conf->rgw_swift_url; | |
616 | string swift_prefix = g_conf->rgw_swift_url_prefix; | |
617 | string tenant_path; | |
618 | ||
619 | /* | |
620 | * We did not allow an empty Swift prefix before, but we want it now. | |
621 | * So, we take rgw_swift_url_prefix = "/" to yield the empty prefix. | |
622 | * The rgw_swift_url_prefix = "" is the default and yields "/swift" | |
623 | * in a backwards-compatible way. | |
624 | */ | |
625 | if (swift_prefix.size() == 0) { | |
626 | swift_prefix = DEFAULT_SWIFT_PREFIX; | |
627 | } else if (swift_prefix == "/") { | |
628 | swift_prefix.clear(); | |
629 | } else { | |
630 | if (swift_prefix[0] != '/') { | |
631 | swift_prefix.insert(0, "/"); | |
632 | } | |
633 | } | |
634 | ||
635 | if (swift_url.size() == 0) { | |
636 | bool add_port = false; | |
637 | const char *server_port = s->info.env->get("SERVER_PORT_SECURE"); | |
638 | const char *protocol; | |
639 | if (server_port) { | |
640 | add_port = (strcmp(server_port, "443") != 0); | |
641 | protocol = "https"; | |
642 | } else { | |
643 | server_port = s->info.env->get("SERVER_PORT"); | |
644 | add_port = (strcmp(server_port, "80") != 0); | |
645 | protocol = "http"; | |
646 | } | |
647 | const char *host = s->info.env->get("HTTP_HOST"); | |
648 | if (!host) { | |
649 | dout(0) << "NOTICE: server is misconfigured, missing rgw_swift_url_prefix or rgw_swift_url, HTTP_HOST is not set" << dendl; | |
650 | ret = -EINVAL; | |
651 | goto done; | |
652 | } | |
653 | swift_url = protocol; | |
654 | swift_url.append("://"); | |
655 | swift_url.append(host); | |
656 | if (add_port && !strchr(host, ':')) { | |
657 | swift_url.append(":"); | |
658 | swift_url.append(server_port); | |
659 | } | |
660 | } | |
661 | ||
662 | if (!key || !user) | |
663 | goto done; | |
664 | ||
665 | user_str = user; | |
666 | ||
667 | if ((ret = rgw_get_user_info_by_swift(store, user_str, info)) < 0) | |
668 | { | |
669 | ret = -EACCES; | |
670 | goto done; | |
671 | } | |
672 | ||
673 | siter = info.swift_keys.find(user_str); | |
674 | if (siter == info.swift_keys.end()) { | |
675 | ret = -EPERM; | |
676 | goto done; | |
677 | } | |
678 | swift_key = &siter->second; | |
679 | ||
680 | if (swift_key->key.compare(key) != 0) { | |
681 | dout(0) << "NOTICE: RGW_SWIFT_Auth_Get::execute(): bad swift key" << dendl; | |
682 | ret = -EPERM; | |
683 | goto done; | |
684 | } | |
685 | ||
686 | if (!g_conf->rgw_swift_tenant_name.empty()) { | |
687 | tenant_path = "/AUTH_"; | |
688 | tenant_path.append(g_conf->rgw_swift_tenant_name); | |
689 | } else if (g_conf->rgw_swift_account_in_url) { | |
690 | tenant_path = "/AUTH_"; | |
691 | tenant_path.append(info.user_id.to_str()); | |
692 | } | |
693 | ||
694 | dump_header(s, "X-Storage-Url", swift_url + swift_prefix + "/v1" + | |
695 | tenant_path); | |
696 | ||
697 | using rgw::auth::swift::encode_token; | |
698 | if ((ret = encode_token(s->cct, swift_key->id, swift_key->key, bl)) < 0) | |
699 | goto done; | |
700 | ||
701 | { | |
702 | static constexpr size_t PREFIX_LEN = sizeof("AUTH_rgwtk") - 1; | |
703 | char token_val[PREFIX_LEN + bl.length() * 2 + 1]; | |
704 | ||
705 | snprintf(token_val, PREFIX_LEN + 1, "AUTH_rgwtk"); | |
706 | buf_to_hex((const unsigned char *)bl.c_str(), bl.length(), | |
707 | token_val + PREFIX_LEN); | |
708 | ||
709 | dump_header(s, "X-Storage-Token", token_val); | |
710 | dump_header(s, "X-Auth-Token", token_val); | |
711 | } | |
712 | ||
713 | ret = STATUS_NO_CONTENT; | |
714 | ||
715 | done: | |
716 | set_req_state_err(s, ret); | |
717 | dump_errno(s); | |
718 | end_header(s); | |
719 | } | |
720 | ||
721 | int RGWHandler_SWIFT_Auth::init(RGWRados *store, struct req_state *state, | |
722 | rgw::io::BasicClient *cio) | |
723 | { | |
724 | state->dialect = "swift-auth"; | |
725 | state->formatter = new JSONFormatter; | |
726 | state->format = RGW_FORMAT_JSON; | |
727 | ||
728 | return RGWHandler::init(store, state, cio); | |
729 | } | |
730 | ||
731 | int RGWHandler_SWIFT_Auth::authorize() | |
732 | { | |
733 | return 0; | |
734 | } | |
735 | ||
736 | RGWOp *RGWHandler_SWIFT_Auth::op_get() | |
737 | { | |
738 | return new RGW_SWIFT_Auth_Get; | |
739 | } | |
740 |