]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_rest_swift.cc
update sources to 12.2.7
[ceph.git] / ceph / src / rgw / rgw_rest_swift.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <boost/algorithm/string/predicate.hpp>
5 #include <boost/format.hpp>
6 #include <boost/optional.hpp>
7 #include <boost/utility/in_place_factory.hpp>
8
9 #include "include/assert.h"
10 #include "ceph_ver.h"
11
12 #include "common/Formatter.h"
13 #include "common/utf8.h"
14 #include "common/ceph_json.h"
15
16 #include "rgw_rest_swift.h"
17 #include "rgw_acl_swift.h"
18 #include "rgw_cors_swift.h"
19 #include "rgw_formats.h"
20 #include "rgw_client_io.h"
21
22 #include "rgw_auth.h"
23 #include "rgw_swift_auth.h"
24
25 #include "rgw_request.h"
26 #include "rgw_process.h"
27
28 #include <array>
29 #include <sstream>
30 #include <memory>
31
32 #include <boost/utility/string_ref.hpp>
33
34 #define dout_context g_ceph_context
35 #define dout_subsys ceph_subsys_rgw
36
37 int RGWListBuckets_ObjStore_SWIFT::get_params()
38 {
39 prefix = s->info.args.get("prefix");
40 marker = s->info.args.get("marker");
41 end_marker = s->info.args.get("end_marker");
42 wants_reversed = s->info.args.exists("reverse");
43
44 if (wants_reversed) {
45 std::swap(marker, end_marker);
46 }
47
48 std::string limit_str = s->info.args.get("limit");
49 if (!limit_str.empty()) {
50 std::string err;
51 long l = strict_strtol(limit_str.c_str(), 10, &err);
52 if (!err.empty()) {
53 return -EINVAL;
54 }
55
56 if (l > (long)limit_max || l < 0) {
57 return -ERR_PRECONDITION_FAILED;
58 }
59
60 limit = (uint64_t)l;
61 }
62
63 if (s->cct->_conf->rgw_swift_need_stats) {
64 bool stats, exists;
65 int r = s->info.args.get_bool("stats", &stats, &exists);
66
67 if (r < 0) {
68 return r;
69 }
70
71 if (exists) {
72 need_stats = stats;
73 }
74 } else {
75 need_stats = false;
76 }
77
78 return 0;
79 }
80
81 static void dump_account_metadata(struct req_state * const s,
82 const RGWUsageStats& global_stats,
83 const std::map<std::string, RGWUsageStats> policies_stats,
84 /* const */map<string, bufferlist>& attrs,
85 const RGWQuotaInfo& quota,
86 const RGWAccessControlPolicy_SWIFTAcct &policy)
87 {
88 /* Adding X-Timestamp to keep align with Swift API */
89 dump_header(s, "X-Timestamp", ceph_clock_now());
90
91 dump_header(s, "X-Account-Container-Count", global_stats.buckets_count);
92 dump_header(s, "X-Account-Object-Count", global_stats.objects_count);
93 dump_header(s, "X-Account-Bytes-Used", global_stats.bytes_used);
94 dump_header(s, "X-Account-Bytes-Used-Actual", global_stats.bytes_used_rounded);
95
96 for (const auto& kv : policies_stats) {
97 const auto& policy_name = camelcase_dash_http_attr(kv.first);
98 const auto& policy_stats = kv.second;
99
100 dump_header_infixed(s, "X-Account-Storage-Policy-", policy_name,
101 "-Container-Count", policy_stats.buckets_count);
102 dump_header_infixed(s, "X-Account-Storage-Policy-", policy_name,
103 "-Object-Count", policy_stats.objects_count);
104 dump_header_infixed(s, "X-Account-Storage-Policy-", policy_name,
105 "-Bytes-Used", policy_stats.bytes_used);
106 dump_header_infixed(s, "X-Account-Storage-Policy-", policy_name,
107 "-Bytes-Used-Actual", policy_stats.bytes_used_rounded);
108 }
109
110 /* Dump TempURL-related stuff */
111 if (s->perm_mask == RGW_PERM_FULL_CONTROL) {
112 auto iter = s->user->temp_url_keys.find(0);
113 if (iter != std::end(s->user->temp_url_keys) && ! iter->second.empty()) {
114 dump_header(s, "X-Account-Meta-Temp-Url-Key", iter->second);
115 }
116
117 iter = s->user->temp_url_keys.find(1);
118 if (iter != std::end(s->user->temp_url_keys) && ! iter->second.empty()) {
119 dump_header(s, "X-Account-Meta-Temp-Url-Key-2", iter->second);
120 }
121 }
122
123 /* Dump quota headers. */
124 if (quota.enabled) {
125 if (quota.max_size >= 0) {
126 dump_header(s, "X-Account-Meta-Quota-Bytes", quota.max_size);
127 }
128
129 /* Limit on the number of objects in a given account is a RadosGW's
130 * extension. Swift's account quota WSGI filter doesn't support it. */
131 if (quota.max_objects >= 0) {
132 dump_header(s, "X-Account-Meta-Quota-Count", quota.max_objects);
133 }
134 }
135
136 /* Dump user-defined metadata items and generic attrs. */
137 const size_t PREFIX_LEN = sizeof(RGW_ATTR_META_PREFIX) - 1;
138 map<string, bufferlist>::iterator iter;
139 for (iter = attrs.lower_bound(RGW_ATTR_PREFIX); iter != attrs.end(); ++iter) {
140 const char *name = iter->first.c_str();
141 map<string, string>::const_iterator geniter = rgw_to_http_attrs.find(name);
142
143 if (geniter != rgw_to_http_attrs.end()) {
144 dump_header(s, geniter->second, iter->second);
145 } else if (strncmp(name, RGW_ATTR_META_PREFIX, PREFIX_LEN) == 0) {
146 dump_header_prefixed(s, "X-Account-Meta-",
147 camelcase_dash_http_attr(name + PREFIX_LEN),
148 iter->second);
149 }
150 }
151
152 /* Dump account ACLs */
153 auto account_acls = policy.to_str();
154 if (account_acls) {
155 dump_header(s, "X-Account-Access-Control", std::move(*account_acls));
156 }
157 }
158
159 void RGWListBuckets_ObjStore_SWIFT::send_response_begin(bool has_buckets)
160 {
161 if (op_ret) {
162 set_req_state_err(s, op_ret);
163 } else if (!has_buckets && s->format == RGW_FORMAT_PLAIN) {
164 op_ret = STATUS_NO_CONTENT;
165 set_req_state_err(s, op_ret);
166 }
167
168 if (! s->cct->_conf->rgw_swift_enforce_content_length) {
169 /* Adding account stats in the header to keep align with Swift API */
170 dump_account_metadata(s,
171 global_stats,
172 policies_stats,
173 attrs,
174 user_quota,
175 static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
176 dump_errno(s);
177 end_header(s, NULL, NULL, NO_CONTENT_LENGTH, true);
178 }
179
180 if (! op_ret) {
181 dump_start(s);
182 s->formatter->open_array_section_with_attrs("account",
183 FormatterAttrs("name", s->user->display_name.c_str(), NULL));
184
185 sent_data = true;
186 }
187 }
188
189 void RGWListBuckets_ObjStore_SWIFT::handle_listing_chunk(RGWUserBuckets&& buckets)
190 {
191 if (wants_reversed) {
192 /* Just store in the reversal buffer. Its content will be handled later,
193 * in send_response_end(). */
194 reverse_buffer.emplace(std::begin(reverse_buffer), std::move(buckets));
195 } else {
196 return send_response_data(buckets);
197 }
198 }
199
200 void RGWListBuckets_ObjStore_SWIFT::send_response_data(RGWUserBuckets& buckets)
201 {
202 if (! sent_data) {
203 return;
204 }
205
206 /* Take care of the prefix parameter of Swift API. There is no business
207 * in applying the filter earlier as we really need to go through all
208 * entries regardless of it (the headers like X-Account-Container-Count
209 * aren't affected by specifying prefix). */
210 const std::map<std::string, RGWBucketEnt>& m = buckets.get_buckets();
211 for (auto iter = m.lower_bound(prefix);
212 iter != m.end() && boost::algorithm::starts_with(iter->first, prefix);
213 ++iter) {
214 dump_bucket_entry(iter->second);
215 }
216 }
217
218 void RGWListBuckets_ObjStore_SWIFT::dump_bucket_entry(const RGWBucketEnt& obj)
219 {
220 s->formatter->open_object_section("container");
221 s->formatter->dump_string("name", obj.bucket.name);
222
223 if (need_stats) {
224 s->formatter->dump_int("count", obj.count);
225 s->formatter->dump_int("bytes", obj.size);
226 }
227
228 s->formatter->close_section();
229
230 if (! s->cct->_conf->rgw_swift_enforce_content_length) {
231 rgw_flush_formatter(s, s->formatter);
232 }
233 }
234
235 void RGWListBuckets_ObjStore_SWIFT::send_response_data_reversed(RGWUserBuckets& buckets)
236 {
237 if (! sent_data) {
238 return;
239 }
240
241 /* Take care of the prefix parameter of Swift API. There is no business
242 * in applying the filter earlier as we really need to go through all
243 * entries regardless of it (the headers like X-Account-Container-Count
244 * aren't affected by specifying prefix). */
245 std::map<std::string, RGWBucketEnt>& m = buckets.get_buckets();
246
247 auto iter = m.rbegin();
248 for (/* initialized above */;
249 iter != m.rend() && !boost::algorithm::starts_with(iter->first, prefix);
250 ++iter) {
251 /* NOP */;
252 }
253
254 for (/* iter carried */;
255 iter != m.rend() && boost::algorithm::starts_with(iter->first, prefix);
256 ++iter) {
257 dump_bucket_entry(iter->second);
258 }
259 }
260
261 void RGWListBuckets_ObjStore_SWIFT::send_response_end()
262 {
263 if (wants_reversed) {
264 for (auto& buckets : reverse_buffer) {
265 send_response_data_reversed(buckets);
266 }
267 }
268
269 if (sent_data) {
270 s->formatter->close_section();
271 }
272
273 if (s->cct->_conf->rgw_swift_enforce_content_length) {
274 /* Adding account stats in the header to keep align with Swift API */
275 dump_account_metadata(s,
276 global_stats,
277 policies_stats,
278 attrs,
279 user_quota,
280 static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
281 dump_errno(s);
282 end_header(s, nullptr, nullptr, s->formatter->get_len(), true);
283 }
284
285 if (sent_data || s->cct->_conf->rgw_swift_enforce_content_length) {
286 rgw_flush_formatter_and_reset(s, s->formatter);
287 }
288 }
289
290 int RGWListBucket_ObjStore_SWIFT::get_params()
291 {
292 prefix = s->info.args.get("prefix");
293 marker = s->info.args.get("marker");
294 end_marker = s->info.args.get("end_marker");
295 max_keys = s->info.args.get("limit");
296 op_ret = parse_max_keys();
297 if (op_ret < 0) {
298 return op_ret;
299 }
300 if (max > default_max)
301 return -ERR_PRECONDITION_FAILED;
302
303 delimiter = s->info.args.get("delimiter");
304
305 string path_args;
306 if (s->info.args.exists("path")) { // should handle empty path
307 path_args = s->info.args.get("path");
308 if (!delimiter.empty() || !prefix.empty()) {
309 return -EINVAL;
310 }
311 prefix = path_args;
312 delimiter="/";
313
314 path = prefix;
315 if (path.size() && path[path.size() - 1] != '/')
316 path.append("/");
317
318 int len = prefix.size();
319 int delim_size = delimiter.size();
320
321 if (len >= delim_size) {
322 if (prefix.substr(len - delim_size).compare(delimiter) != 0)
323 prefix.append(delimiter);
324 }
325 }
326
327 return 0;
328 }
329
330 static void dump_container_metadata(struct req_state *,
331 const RGWBucketEnt&,
332 const RGWQuotaInfo&,
333 const RGWBucketWebsiteConf&);
334
335 void RGWListBucket_ObjStore_SWIFT::send_response()
336 {
337 vector<rgw_bucket_dir_entry>::iterator iter = objs.begin();
338 map<string, bool>::iterator pref_iter = common_prefixes.begin();
339
340 dump_start(s);
341 dump_container_metadata(s, bucket, bucket_quota,
342 s->bucket_info.website_conf);
343
344 s->formatter->open_array_section_with_attrs("container", FormatterAttrs("name", s->bucket.name.c_str(), NULL));
345
346 while (iter != objs.end() || pref_iter != common_prefixes.end()) {
347 bool do_pref = false;
348 bool do_objs = false;
349 rgw_obj_key key;
350 if (iter != objs.end()) {
351 key = iter->key;
352 }
353 if (pref_iter == common_prefixes.end())
354 do_objs = true;
355 else if (iter == objs.end())
356 do_pref = true;
357 else if (!key.empty() && key.name.compare(pref_iter->first) == 0) {
358 do_objs = true;
359 ++pref_iter;
360 } else if (!key.empty() && key.name.compare(pref_iter->first) <= 0)
361 do_objs = true;
362 else
363 do_pref = true;
364
365 if (do_objs && (marker.empty() || marker < key)) {
366 if (key.name.compare(path) == 0)
367 goto next;
368
369 s->formatter->open_object_section("object");
370 s->formatter->dump_string("name", key.name);
371 s->formatter->dump_string("hash", iter->meta.etag);
372 s->formatter->dump_int("bytes", iter->meta.accounted_size);
373 if (!iter->meta.user_data.empty())
374 s->formatter->dump_string("user_custom_data", iter->meta.user_data);
375 string single_content_type = iter->meta.content_type;
376 if (iter->meta.content_type.size()) {
377 // content type might hold multiple values, just dump the last one
378 ssize_t pos = iter->meta.content_type.rfind(',');
379 if (pos > 0) {
380 ++pos;
381 while (single_content_type[pos] == ' ')
382 ++pos;
383 single_content_type = single_content_type.substr(pos);
384 }
385 s->formatter->dump_string("content_type", single_content_type);
386 }
387 dump_time(s, "last_modified", &iter->meta.mtime);
388 s->formatter->close_section();
389 }
390
391 if (do_pref && (marker.empty() || pref_iter->first.compare(marker.name) > 0)) {
392 const string& name = pref_iter->first;
393 if (name.compare(delimiter) == 0)
394 goto next;
395
396 s->formatter->open_object_section_with_attrs("subdir", FormatterAttrs("name", name.c_str(), NULL));
397
398 /* swift is a bit inconsistent here */
399 switch (s->format) {
400 case RGW_FORMAT_XML:
401 s->formatter->dump_string("name", name);
402 break;
403 default:
404 s->formatter->dump_string("subdir", name);
405 }
406 s->formatter->close_section();
407 }
408 next:
409 if (do_objs)
410 ++iter;
411 else
412 ++pref_iter;
413 }
414
415 s->formatter->close_section();
416
417 int64_t content_len = 0;
418 if (! op_ret) {
419 content_len = s->formatter->get_len();
420 if (content_len == 0) {
421 op_ret = STATUS_NO_CONTENT;
422 }
423 } else if (op_ret > 0) {
424 op_ret = 0;
425 }
426
427 set_req_state_err(s, op_ret);
428 dump_errno(s);
429 end_header(s, this, NULL, content_len);
430 if (op_ret < 0) {
431 return;
432 }
433
434 rgw_flush_formatter_and_reset(s, s->formatter);
435 }
436
437 static void dump_container_metadata(struct req_state *s,
438 const RGWBucketEnt& bucket,
439 const RGWQuotaInfo& quota,
440 const RGWBucketWebsiteConf& ws_conf)
441 {
442 /* Adding X-Timestamp to keep align with Swift API */
443 dump_header(s, "X-Timestamp", utime_t(s->bucket_info.creation_time));
444
445 dump_header(s, "X-Container-Object-Count", bucket.count);
446 dump_header(s, "X-Container-Bytes-Used", bucket.size);
447 dump_header(s, "X-Container-Bytes-Used-Actual", bucket.size_rounded);
448
449 if (s->object.empty()) {
450 auto swift_policy = \
451 static_cast<RGWAccessControlPolicy_SWIFT*>(s->bucket_acl.get());
452 std::string read_acl, write_acl;
453 swift_policy->to_str(read_acl, write_acl);
454
455 if (read_acl.size()) {
456 dump_header(s, "X-Container-Read", read_acl);
457 }
458 if (write_acl.size()) {
459 dump_header(s, "X-Container-Write", write_acl);
460 }
461 if (!s->bucket_info.placement_rule.empty()) {
462 dump_header(s, "X-Storage-Policy", s->bucket_info.placement_rule);
463 }
464
465 /* Dump user-defined metadata items and generic attrs. */
466 const size_t PREFIX_LEN = sizeof(RGW_ATTR_META_PREFIX) - 1;
467 map<string, bufferlist>::iterator iter;
468 for (iter = s->bucket_attrs.lower_bound(RGW_ATTR_PREFIX);
469 iter != s->bucket_attrs.end();
470 ++iter) {
471 const char *name = iter->first.c_str();
472 map<string, string>::const_iterator geniter = rgw_to_http_attrs.find(name);
473
474 if (geniter != rgw_to_http_attrs.end()) {
475 dump_header(s, geniter->second, iter->second);
476 } else if (strncmp(name, RGW_ATTR_META_PREFIX, PREFIX_LEN) == 0) {
477 dump_header_prefixed(s, "X-Container-Meta-",
478 camelcase_dash_http_attr(name + PREFIX_LEN),
479 iter->second);
480 }
481 }
482 }
483
484 /* Dump container versioning info. */
485 if (! s->bucket_info.swift_ver_location.empty()) {
486 dump_header(s, "X-Versions-Location",
487 url_encode(s->bucket_info.swift_ver_location));
488 }
489
490 /* Dump quota headers. */
491 if (quota.enabled) {
492 if (quota.max_size >= 0) {
493 dump_header(s, "X-Container-Meta-Quota-Bytes", quota.max_size);
494 }
495
496 if (quota.max_objects >= 0) {
497 dump_header(s, "X-Container-Meta-Quota-Count", quota.max_objects);
498 }
499 }
500
501 /* Dump Static Website headers. */
502 if (! ws_conf.index_doc_suffix.empty()) {
503 dump_header(s, "X-Container-Meta-Web-Index", ws_conf.index_doc_suffix);
504 }
505
506 if (! ws_conf.error_doc.empty()) {
507 dump_header(s, "X-Container-Meta-Web-Error", ws_conf.error_doc);
508 }
509
510 if (! ws_conf.subdir_marker.empty()) {
511 dump_header(s, "X-Container-Meta-Web-Directory-Type",
512 ws_conf.subdir_marker);
513 }
514
515 if (! ws_conf.listing_css_doc.empty()) {
516 dump_header(s, "X-Container-Meta-Web-Listings-CSS",
517 ws_conf.listing_css_doc);
518 }
519
520 if (ws_conf.listing_enabled) {
521 dump_header(s, "X-Container-Meta-Web-Listings", "true");
522 }
523 }
524
525 void RGWStatAccount_ObjStore_SWIFT::execute()
526 {
527 RGWStatAccount_ObjStore::execute();
528 op_ret = rgw_get_user_attrs_by_uid(store, s->user->user_id, attrs);
529 }
530
531 void RGWStatAccount_ObjStore_SWIFT::send_response()
532 {
533 if (op_ret >= 0) {
534 op_ret = STATUS_NO_CONTENT;
535 dump_account_metadata(s,
536 global_stats,
537 policies_stats,
538 attrs,
539 user_quota,
540 static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
541 }
542
543 set_req_state_err(s, op_ret);
544 dump_errno(s);
545
546 end_header(s, NULL, NULL, 0, true);
547
548 dump_start(s);
549 }
550
551 void RGWStatBucket_ObjStore_SWIFT::send_response()
552 {
553 if (op_ret >= 0) {
554 op_ret = STATUS_NO_CONTENT;
555 dump_container_metadata(s, bucket, bucket_quota,
556 s->bucket_info.website_conf);
557 }
558
559 set_req_state_err(s, op_ret);
560 dump_errno(s);
561
562 end_header(s, this, NULL, 0, true);
563 dump_start(s);
564 }
565
566 static int get_swift_container_settings(req_state * const s,
567 RGWRados * const store,
568 RGWAccessControlPolicy * const policy,
569 bool * const has_policy,
570 uint32_t * rw_mask,
571 RGWCORSConfiguration * const cors_config,
572 bool * const has_cors)
573 {
574 const char * const read_list = s->info.env->get("HTTP_X_CONTAINER_READ");
575 const char * const write_list = s->info.env->get("HTTP_X_CONTAINER_WRITE");
576
577 *has_policy = false;
578
579 if (read_list || write_list) {
580 RGWAccessControlPolicy_SWIFT swift_policy(s->cct);
581 const auto r = swift_policy.create(store,
582 s->user->user_id,
583 s->user->display_name,
584 read_list,
585 write_list,
586 *rw_mask);
587 if (r < 0) {
588 return r;
589 }
590
591 *policy = swift_policy;
592 *has_policy = true;
593 }
594
595 *has_cors = false;
596
597 /*Check and update CORS configuration*/
598 const char *allow_origins = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_ORIGIN");
599 const char *allow_headers = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_HEADERS");
600 const char *expose_headers = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_EXPOSE_HEADERS");
601 const char *max_age = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_MAX_AGE");
602 if (allow_origins) {
603 RGWCORSConfiguration_SWIFT *swift_cors = new RGWCORSConfiguration_SWIFT;
604 int r = swift_cors->create_update(allow_origins, allow_headers, expose_headers, max_age);
605 if (r < 0) {
606 dout(0) << "Error creating/updating the cors configuration" << dendl;
607 delete swift_cors;
608 return r;
609 }
610 *has_cors = true;
611 *cors_config = *swift_cors;
612 cors_config->dump();
613 delete swift_cors;
614 }
615
616 return 0;
617 }
618
619 #define ACCT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_ACCOUNT_META_"
620 #define ACCT_PUT_ATTR_PREFIX "HTTP_X_ACCOUNT_META_"
621 #define CONT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_CONTAINER_META_"
622 #define CONT_PUT_ATTR_PREFIX "HTTP_X_CONTAINER_META_"
623
624 static void get_rmattrs_from_headers(const req_state * const s,
625 const char * const put_prefix,
626 const char * const del_prefix,
627 set<string>& rmattr_names)
628 {
629 const size_t put_prefix_len = strlen(put_prefix);
630 const size_t del_prefix_len = strlen(del_prefix);
631
632 for (const auto& kv : s->info.env->get_map()) {
633 size_t prefix_len = 0;
634 const char * const p = kv.first.c_str();
635
636 if (strncasecmp(p, del_prefix, del_prefix_len) == 0) {
637 /* Explicitly requested removal. */
638 prefix_len = del_prefix_len;
639 } else if ((strncasecmp(p, put_prefix, put_prefix_len) == 0)
640 && kv.second.empty()) {
641 /* Removal requested by putting an empty value. */
642 prefix_len = put_prefix_len;
643 }
644
645 if (prefix_len > 0) {
646 string name(RGW_ATTR_META_PREFIX);
647 name.append(lowercase_dash_http_attr(p + prefix_len));
648 rmattr_names.insert(name);
649 }
650 }
651 }
652
653 static int get_swift_versioning_settings(
654 req_state * const s,
655 boost::optional<std::string>& swift_ver_location)
656 {
657 /* Removing the Swift's versions location has lower priority than setting
658 * a new one. That's the reason why we're handling it first. */
659 const std::string vlocdel =
660 s->info.env->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
661 if (vlocdel.size()) {
662 swift_ver_location = boost::in_place(std::string());
663 }
664
665 if (s->info.env->exists("HTTP_X_VERSIONS_LOCATION")) {
666 /* If the Swift's versioning is globally disabled but someone wants to
667 * enable it for a given container, new version of Swift will generate
668 * the precondition failed error. */
669 if (! s->cct->_conf->rgw_swift_versioning_enabled) {
670 return -ERR_PRECONDITION_FAILED;
671 }
672
673 swift_ver_location = s->info.env->get("HTTP_X_VERSIONS_LOCATION", "");
674 }
675
676 return 0;
677 }
678
679 int RGWCreateBucket_ObjStore_SWIFT::get_params()
680 {
681 bool has_policy;
682 uint32_t policy_rw_mask = 0;
683
684 int r = get_swift_container_settings(s, store, &policy, &has_policy,
685 &policy_rw_mask, &cors_config, &has_cors);
686 if (r < 0) {
687 return r;
688 }
689
690 if (!has_policy) {
691 policy.create_default(s->user->user_id, s->user->display_name);
692 }
693
694 location_constraint = store->get_zonegroup().api_name;
695 get_rmattrs_from_headers(s, CONT_PUT_ATTR_PREFIX,
696 CONT_REMOVE_ATTR_PREFIX, rmattr_names);
697 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
698
699 return get_swift_versioning_settings(s, swift_ver_location);
700 }
701
702 static inline int handle_metadata_errors(req_state* const s, const int op_ret)
703 {
704 if (op_ret == -EFBIG) {
705 /* Handle the custom error message of exceeding maximum custom attribute
706 * (stored as xattr) size. */
707 const auto error_message = boost::str(
708 boost::format("Metadata value longer than %lld")
709 % s->cct->_conf->get_val<size_t>("rgw_max_attr_size"));
710 set_req_state_err(s, EINVAL, error_message);
711 return -EINVAL;
712 } else if (op_ret == -E2BIG) {
713 const auto error_message = boost::str(
714 boost::format("Too many metadata items; max %lld")
715 % s->cct->_conf->get_val<size_t>("rgw_max_attrs_num_in_req"));
716 set_req_state_err(s, EINVAL, error_message);
717 return -EINVAL;
718 }
719
720 return op_ret;
721 }
722
723 void RGWCreateBucket_ObjStore_SWIFT::send_response()
724 {
725 const auto meta_ret = handle_metadata_errors(s, op_ret);
726 if (meta_ret != op_ret) {
727 op_ret = meta_ret;
728 } else {
729 if (!op_ret) {
730 op_ret = STATUS_CREATED;
731 } else if (op_ret == -ERR_BUCKET_EXISTS) {
732 op_ret = STATUS_ACCEPTED;
733 }
734 set_req_state_err(s, op_ret);
735 }
736
737 dump_errno(s);
738 /* Propose ending HTTP header with 0 Content-Length header. */
739 end_header(s, NULL, NULL, 0);
740 rgw_flush_formatter_and_reset(s, s->formatter);
741 }
742
743 void RGWDeleteBucket_ObjStore_SWIFT::send_response()
744 {
745 int r = op_ret;
746 if (!r)
747 r = STATUS_NO_CONTENT;
748
749 set_req_state_err(s, r);
750 dump_errno(s);
751 end_header(s, this, NULL, 0);
752 rgw_flush_formatter_and_reset(s, s->formatter);
753 }
754
755 static int get_delete_at_param(req_state *s, boost::optional<real_time> &delete_at)
756 {
757 /* Handle Swift object expiration. */
758 real_time delat_proposal;
759 string x_delete = s->info.env->get("HTTP_X_DELETE_AFTER", "");
760
761 if (x_delete.empty()) {
762 x_delete = s->info.env->get("HTTP_X_DELETE_AT", "");
763 } else {
764 /* X-Delete-After HTTP is present. It means we need add its value
765 * to the current time. */
766 delat_proposal = real_clock::now();
767 }
768
769 if (x_delete.empty()) {
770 delete_at = boost::none;
771 if (s->info.env->exists("HTTP_X_REMOVE_DELETE_AT")) {
772 delete_at = boost::in_place(real_time());
773 }
774 return 0;
775 }
776 string err;
777 long ts = strict_strtoll(x_delete.c_str(), 10, &err);
778
779 if (!err.empty()) {
780 return -EINVAL;
781 }
782
783 delat_proposal += make_timespan(ts);
784 if (delat_proposal < real_clock::now()) {
785 return -EINVAL;
786 }
787
788 delete_at = delat_proposal;
789
790 return 0;
791 }
792
793 int RGWPutObj_ObjStore_SWIFT::verify_permission()
794 {
795 op_ret = RGWPutObj_ObjStore::verify_permission();
796
797 /* We have to differentiate error codes depending on whether user is
798 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
799 * (403 Forbidden). */
800 if (s->auth.identity->is_anonymous() && op_ret == -EACCES) {
801 return -EPERM;
802 } else {
803 return op_ret;
804 }
805 }
806
807 int RGWPutObj_ObjStore_SWIFT::get_params()
808 {
809 if (s->has_bad_meta) {
810 return -EINVAL;
811 }
812
813 if (!s->length) {
814 const char *encoding = s->info.env->get("HTTP_TRANSFER_ENCODING");
815 if (!encoding || strcmp(encoding, "chunked") != 0) {
816 ldout(s->cct, 20) << "neither length nor chunked encoding" << dendl;
817 return -ERR_LENGTH_REQUIRED;
818 }
819
820 chunked_upload = true;
821 }
822
823 supplied_etag = s->info.env->get("HTTP_ETAG");
824
825 if (!s->generic_attrs.count(RGW_ATTR_CONTENT_TYPE)) {
826 ldout(s->cct, 5) << "content type wasn't provided, trying to guess" << dendl;
827 const char *suffix = strrchr(s->object.name.c_str(), '.');
828 if (suffix) {
829 suffix++;
830 if (*suffix) {
831 string suffix_str(suffix);
832 const char *mime = rgw_find_mime_by_ext(suffix_str);
833 if (mime) {
834 s->generic_attrs[RGW_ATTR_CONTENT_TYPE] = mime;
835 }
836 }
837 }
838 }
839
840 policy.create_default(s->user->user_id, s->user->display_name);
841
842 int r = get_delete_at_param(s, delete_at);
843 if (r < 0) {
844 ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
845 return r;
846 }
847
848 if (!s->cct->_conf->rgw_swift_custom_header.empty()) {
849 string custom_header = s->cct->_conf->rgw_swift_custom_header;
850 if (s->info.env->exists(custom_header.c_str())) {
851 user_data = s->info.env->get(custom_header.c_str());
852 }
853 }
854
855 dlo_manifest = s->info.env->get("HTTP_X_OBJECT_MANIFEST");
856 bool exists;
857 string multipart_manifest = s->info.args.get("multipart-manifest", &exists);
858 if (exists) {
859 if (multipart_manifest != "put") {
860 ldout(s->cct, 5) << "invalid multipart-manifest http param: " << multipart_manifest << dendl;
861 return -EINVAL;
862 }
863
864 #define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
865 uint64_t max_len = s->cct->_conf->rgw_max_slo_entries * MAX_SLO_ENTRY_SIZE;
866
867 slo_info = new RGWSLOInfo;
868
869 int r = rgw_rest_get_json_input_keep_data(s->cct, s, slo_info->entries, max_len, &slo_info->raw_data, &slo_info->raw_data_len);
870 if (r < 0) {
871 ldout(s->cct, 5) << "failed to read input for slo r=" << r << dendl;
872 return r;
873 }
874
875 if ((int64_t)slo_info->entries.size() > s->cct->_conf->rgw_max_slo_entries) {
876 ldout(s->cct, 5) << "too many entries in slo request: " << slo_info->entries.size() << dendl;
877 return -EINVAL;
878 }
879
880 MD5 etag_sum;
881 uint64_t total_size = 0;
882 for (const auto& entry : slo_info->entries) {
883 etag_sum.Update((const byte *)entry.etag.c_str(),
884 entry.etag.length());
885 total_size += entry.size_bytes;
886
887 ldout(s->cct, 20) << "slo_part: " << entry.path
888 << " size=" << entry.size_bytes
889 << " etag=" << entry.etag
890 << dendl;
891 }
892 complete_etag(etag_sum, &lo_etag);
893 slo_info->total_size = total_size;
894
895 ofs = slo_info->raw_data_len;
896 }
897
898 return RGWPutObj_ObjStore::get_params();
899 }
900
901 void RGWPutObj_ObjStore_SWIFT::send_response()
902 {
903 const auto meta_ret = handle_metadata_errors(s, op_ret);
904 if (meta_ret) {
905 op_ret = meta_ret;
906 } else {
907 if (!op_ret) {
908 op_ret = STATUS_CREATED;
909 }
910 set_req_state_err(s, op_ret);
911 }
912
913 if (! lo_etag.empty()) {
914 /* Static Large Object of Swift API has two etags represented by
915 * following members:
916 * - etag - for the manifest itself (it will be stored in xattrs),
917 * - lo_etag - for the content composited from SLO's segments.
918 * The value is calculated basing on segments' etags.
919 * In response for PUT request we have to expose the second one.
920 * The first one may be obtained by GET with "multipart-manifest=get"
921 * in query string on a given SLO. */
922 dump_etag(s, lo_etag, true /* quoted */);
923 } else {
924 dump_etag(s, etag);
925 }
926
927 dump_last_modified(s, mtime);
928 set_req_state_err(s, op_ret);
929 dump_errno(s);
930 end_header(s, this);
931 rgw_flush_formatter_and_reset(s, s->formatter);
932 }
933
934 static int get_swift_account_settings(req_state * const s,
935 RGWRados * const store,
936 RGWAccessControlPolicy_SWIFTAcct * const policy,
937 bool * const has_policy)
938 {
939 *has_policy = false;
940
941 const char * const acl_attr = s->info.env->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
942 if (acl_attr) {
943 RGWAccessControlPolicy_SWIFTAcct swift_acct_policy(s->cct);
944 const bool r = swift_acct_policy.create(store,
945 s->user->user_id,
946 s->user->display_name,
947 string(acl_attr));
948 if (r != true) {
949 return -EINVAL;
950 }
951
952 *policy = swift_acct_policy;
953 *has_policy = true;
954 }
955
956 return 0;
957 }
958
959 int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
960 {
961 if (s->has_bad_meta) {
962 return -EINVAL;
963 }
964
965 int ret = get_swift_account_settings(s,
966 store,
967 // FIXME: we need to carry unique_ptr in generic class
968 // and allocate appropriate ACL class in the ctor
969 static_cast<RGWAccessControlPolicy_SWIFTAcct *>(&policy),
970 &has_policy);
971 if (ret < 0) {
972 return ret;
973 }
974
975 get_rmattrs_from_headers(s, ACCT_PUT_ATTR_PREFIX, ACCT_REMOVE_ATTR_PREFIX,
976 rmattr_names);
977 return 0;
978 }
979
980 void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
981 {
982 const auto meta_ret = handle_metadata_errors(s, op_ret);
983 if (meta_ret != op_ret) {
984 op_ret = meta_ret;
985 } else {
986 if (!op_ret) {
987 op_ret = STATUS_NO_CONTENT;
988 }
989 set_req_state_err(s, op_ret);
990 }
991
992 dump_errno(s);
993 end_header(s, this);
994 rgw_flush_formatter_and_reset(s, s->formatter);
995 }
996
997 int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
998 {
999 if (s->has_bad_meta) {
1000 return -EINVAL;
1001 }
1002
1003 int r = get_swift_container_settings(s, store, &policy, &has_policy,
1004 &policy_rw_mask, &cors_config, &has_cors);
1005 if (r < 0) {
1006 return r;
1007 }
1008
1009 get_rmattrs_from_headers(s, CONT_PUT_ATTR_PREFIX, CONT_REMOVE_ATTR_PREFIX,
1010 rmattr_names);
1011 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
1012
1013 return get_swift_versioning_settings(s, swift_ver_location);
1014 }
1015
1016 void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
1017 {
1018 const auto meta_ret = handle_metadata_errors(s, op_ret);
1019 if (meta_ret != op_ret) {
1020 op_ret = meta_ret;
1021 } else {
1022 if (!op_ret && (op_ret != -EINVAL)) {
1023 op_ret = STATUS_NO_CONTENT;
1024 }
1025 set_req_state_err(s, op_ret);
1026 }
1027
1028 dump_errno(s);
1029 end_header(s, this);
1030 rgw_flush_formatter_and_reset(s, s->formatter);
1031 }
1032
1033 int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
1034 {
1035 if (s->has_bad_meta) {
1036 return -EINVAL;
1037 }
1038
1039 /* Handle Swift object expiration. */
1040 int r = get_delete_at_param(s, delete_at);
1041 if (r < 0) {
1042 ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
1043 return r;
1044 }
1045
1046 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
1047 dlo_manifest = s->info.env->get("HTTP_X_OBJECT_MANIFEST");
1048
1049 return 0;
1050 }
1051
1052 void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
1053 {
1054 const auto meta_ret = handle_metadata_errors(s, op_ret);
1055 if (meta_ret != op_ret) {
1056 op_ret = meta_ret;
1057 } else {
1058 if (!op_ret) {
1059 op_ret = STATUS_ACCEPTED;
1060 }
1061 set_req_state_err(s, op_ret);
1062 }
1063
1064 if (!s->is_err()) {
1065 dump_content_length(s, 0);
1066 }
1067
1068 dump_errno(s);
1069 end_header(s, this);
1070 rgw_flush_formatter_and_reset(s, s->formatter);
1071 }
1072
1073 static void bulkdelete_respond(const unsigned num_deleted,
1074 const unsigned int num_unfound,
1075 const std::list<RGWBulkDelete::fail_desc_t>& failures,
1076 const int prot_flags, /* in */
1077 ceph::Formatter& formatter) /* out */
1078 {
1079 formatter.open_object_section("delete");
1080
1081 string resp_status;
1082 string resp_body;
1083
1084 if (!failures.empty()) {
1085 int reason = ERR_INVALID_REQUEST;
1086 for (const auto fail_desc : failures) {
1087 if (-ENOENT != fail_desc.err && -EACCES != fail_desc.err) {
1088 reason = fail_desc.err;
1089 }
1090 }
1091 rgw_err err;
1092 set_req_state_err(err, reason, prot_flags);
1093 dump_errno(err, resp_status);
1094 } else if (0 == num_deleted && 0 == num_unfound) {
1095 /* 400 Bad Request */
1096 dump_errno(400, resp_status);
1097 resp_body = "Invalid bulk delete.";
1098 } else {
1099 /* 200 OK */
1100 dump_errno(200, resp_status);
1101 }
1102
1103 encode_json("Number Deleted", num_deleted, &formatter);
1104 encode_json("Number Not Found", num_unfound, &formatter);
1105 encode_json("Response Body", resp_body, &formatter);
1106 encode_json("Response Status", resp_status, &formatter);
1107
1108 formatter.open_array_section("Errors");
1109 for (const auto fail_desc : failures) {
1110 formatter.open_array_section("object");
1111
1112 stringstream ss_name;
1113 ss_name << fail_desc.path;
1114 encode_json("Name", ss_name.str(), &formatter);
1115
1116 rgw_err err;
1117 set_req_state_err(err, fail_desc.err, prot_flags);
1118 string status;
1119 dump_errno(err, status);
1120 encode_json("Status", status, &formatter);
1121 formatter.close_section();
1122 }
1123 formatter.close_section();
1124
1125 formatter.close_section();
1126 }
1127
1128 int RGWDeleteObj_ObjStore_SWIFT::verify_permission()
1129 {
1130 op_ret = RGWDeleteObj_ObjStore::verify_permission();
1131
1132 /* We have to differentiate error codes depending on whether user is
1133 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1134 * (403 Forbidden). */
1135 if (s->auth.identity->is_anonymous() && op_ret == -EACCES) {
1136 return -EPERM;
1137 } else {
1138 return op_ret;
1139 }
1140 }
1141
1142 int RGWDeleteObj_ObjStore_SWIFT::get_params()
1143 {
1144 const string& mm = s->info.args.get("multipart-manifest");
1145 multipart_delete = (mm.compare("delete") == 0);
1146
1147 return RGWDeleteObj_ObjStore::get_params();
1148 }
1149
1150 void RGWDeleteObj_ObjStore_SWIFT::send_response()
1151 {
1152 int r = op_ret;
1153
1154 if (multipart_delete) {
1155 r = 0;
1156 } else if(!r) {
1157 r = STATUS_NO_CONTENT;
1158 }
1159
1160 set_req_state_err(s, r);
1161 dump_errno(s);
1162
1163 if (multipart_delete) {
1164 end_header(s, this /* RGWOp */, nullptr /* contype */,
1165 CHUNKED_TRANSFER_ENCODING);
1166
1167 if (deleter) {
1168 bulkdelete_respond(deleter->get_num_deleted(),
1169 deleter->get_num_unfound(),
1170 deleter->get_failures(),
1171 s->prot_flags,
1172 *s->formatter);
1173 } else if (-ENOENT == op_ret) {
1174 bulkdelete_respond(0, 1, {}, s->prot_flags, *s->formatter);
1175 } else {
1176 RGWBulkDelete::acct_path_t path;
1177 path.bucket_name = s->bucket_name;
1178 path.obj_key = s->object;
1179
1180 RGWBulkDelete::fail_desc_t fail_desc;
1181 fail_desc.err = op_ret;
1182 fail_desc.path = path;
1183
1184 bulkdelete_respond(0, 0, { fail_desc }, s->prot_flags, *s->formatter);
1185 }
1186 } else {
1187 end_header(s, this);
1188 }
1189
1190 rgw_flush_formatter_and_reset(s, s->formatter);
1191
1192 }
1193
1194 static void get_contype_from_attrs(map<string, bufferlist>& attrs,
1195 string& content_type)
1196 {
1197 map<string, bufferlist>::iterator iter = attrs.find(RGW_ATTR_CONTENT_TYPE);
1198 if (iter != attrs.end()) {
1199 content_type = iter->second.c_str();
1200 }
1201 }
1202
1203 static void dump_object_metadata(struct req_state * const s,
1204 map<string, bufferlist> attrs)
1205 {
1206 map<string, string> response_attrs;
1207
1208 for (auto kv : attrs) {
1209 const char * name = kv.first.c_str();
1210 const auto aiter = rgw_to_http_attrs.find(name);
1211
1212 if (aiter != std::end(rgw_to_http_attrs)) {
1213 response_attrs[aiter->second] = kv.second.c_str();
1214 } else if (strcmp(name, RGW_ATTR_SLO_UINDICATOR) == 0) {
1215 // this attr has an extra length prefix from ::encode() in prior versions
1216 dump_header(s, "X-Object-Meta-Static-Large-Object", "True");
1217 } else if (strncmp(name, RGW_ATTR_META_PREFIX,
1218 sizeof(RGW_ATTR_META_PREFIX)-1) == 0) {
1219 name += sizeof(RGW_ATTR_META_PREFIX) - 1;
1220 dump_header_prefixed(s, "X-Object-Meta-",
1221 camelcase_dash_http_attr(name), kv.second);
1222 }
1223 }
1224
1225 /* Handle override and fallback for Content-Disposition HTTP header.
1226 * At the moment this will be used only by TempURL of the Swift API. */
1227 const auto cditer = rgw_to_http_attrs.find(RGW_ATTR_CONTENT_DISP);
1228 if (cditer != std::end(rgw_to_http_attrs)) {
1229 const auto& name = cditer->second;
1230
1231 if (!s->content_disp.override.empty()) {
1232 response_attrs[name] = s->content_disp.override;
1233 } else if (!s->content_disp.fallback.empty()
1234 && response_attrs.find(name) == std::end(response_attrs)) {
1235 response_attrs[name] = s->content_disp.fallback;
1236 }
1237 }
1238
1239 for (const auto kv : response_attrs) {
1240 dump_header(s, kv.first, kv.second);
1241 }
1242
1243 const auto iter = attrs.find(RGW_ATTR_DELETE_AT);
1244 if (iter != std::end(attrs)) {
1245 utime_t delete_at;
1246 try {
1247 ::decode(delete_at, iter->second);
1248 if (!delete_at.is_zero()) {
1249 dump_header(s, "X-Delete-At", delete_at.sec());
1250 }
1251 } catch (buffer::error& err) {
1252 ldout(s->cct, 0) << "ERROR: cannot decode object's " RGW_ATTR_DELETE_AT
1253 " attr, ignoring"
1254 << dendl;
1255 }
1256 }
1257 }
1258
1259 int RGWCopyObj_ObjStore_SWIFT::init_dest_policy()
1260 {
1261 dest_policy.create_default(s->user->user_id, s->user->display_name);
1262
1263 return 0;
1264 }
1265
1266 int RGWCopyObj_ObjStore_SWIFT::get_params()
1267 {
1268 if_mod = s->info.env->get("HTTP_IF_MODIFIED_SINCE");
1269 if_unmod = s->info.env->get("HTTP_IF_UNMODIFIED_SINCE");
1270 if_match = s->info.env->get("HTTP_COPY_IF_MATCH");
1271 if_nomatch = s->info.env->get("HTTP_COPY_IF_NONE_MATCH");
1272
1273 src_tenant_name = s->src_tenant_name;
1274 src_bucket_name = s->src_bucket_name;
1275 src_object = s->src_object;
1276 dest_tenant_name = s->bucket_tenant;
1277 dest_bucket_name = s->bucket_name;
1278 dest_object = s->object.name;
1279
1280 const char * const fresh_meta = s->info.env->get("HTTP_X_FRESH_METADATA");
1281 if (fresh_meta && strcasecmp(fresh_meta, "TRUE") == 0) {
1282 attrs_mod = RGWRados::ATTRSMOD_REPLACE;
1283 } else {
1284 attrs_mod = RGWRados::ATTRSMOD_MERGE;
1285 }
1286
1287 int r = get_delete_at_param(s, delete_at);
1288 if (r < 0) {
1289 ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
1290 return r;
1291 }
1292
1293 return 0;
1294 }
1295
1296 void RGWCopyObj_ObjStore_SWIFT::send_partial_response(off_t ofs)
1297 {
1298 if (! sent_header) {
1299 if (! op_ret)
1300 op_ret = STATUS_CREATED;
1301 set_req_state_err(s, op_ret);
1302 dump_errno(s);
1303 end_header(s, this);
1304
1305 /* Send progress information. Note that this diverge from the original swift
1306 * spec. We do this in order to keep connection alive.
1307 */
1308 if (op_ret == 0) {
1309 s->formatter->open_array_section("progress");
1310 }
1311 sent_header = true;
1312 } else {
1313 s->formatter->dump_int("ofs", (uint64_t)ofs);
1314 }
1315 rgw_flush_formatter(s, s->formatter);
1316 }
1317
1318 void RGWCopyObj_ObjStore_SWIFT::dump_copy_info()
1319 {
1320 /* Dump X-Copied-From. */
1321 dump_header(s, "X-Copied-From", url_encode(src_bucket.name) +
1322 "/" + url_encode(src_object.name));
1323
1324 /* Dump X-Copied-From-Account. */
1325 /* XXX tenant */
1326 dump_header(s, "X-Copied-From-Account", url_encode(s->user->user_id.id));
1327
1328 /* Dump X-Copied-From-Last-Modified. */
1329 dump_time_header(s, "X-Copied-From-Last-Modified", src_mtime);
1330 }
1331
1332 void RGWCopyObj_ObjStore_SWIFT::send_response()
1333 {
1334 if (! sent_header) {
1335 string content_type;
1336 if (! op_ret)
1337 op_ret = STATUS_CREATED;
1338 set_req_state_err(s, op_ret);
1339 dump_errno(s);
1340 dump_etag(s, etag);
1341 dump_last_modified(s, mtime);
1342 dump_copy_info();
1343 get_contype_from_attrs(attrs, content_type);
1344 dump_object_metadata(s, attrs);
1345 end_header(s, this, !content_type.empty() ? content_type.c_str()
1346 : "binary/octet-stream");
1347 } else {
1348 s->formatter->close_section();
1349 rgw_flush_formatter(s, s->formatter);
1350 }
1351 }
1352
1353 int RGWGetObj_ObjStore_SWIFT::verify_permission()
1354 {
1355 op_ret = RGWGetObj_ObjStore::verify_permission();
1356
1357 /* We have to differentiate error codes depending on whether user is
1358 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1359 * (403 Forbidden). */
1360 if (s->auth.identity->is_anonymous() && op_ret == -EACCES) {
1361 return -EPERM;
1362 } else {
1363 return op_ret;
1364 }
1365 }
1366
1367 int RGWGetObj_ObjStore_SWIFT::get_params()
1368 {
1369 const string& mm = s->info.args.get("multipart-manifest");
1370 skip_manifest = (mm.compare("get") == 0);
1371
1372 return RGWGetObj_ObjStore::get_params();
1373 }
1374
1375 int RGWGetObj_ObjStore_SWIFT::send_response_data_error()
1376 {
1377 std::string error_content;
1378 op_ret = error_handler(op_ret, &error_content);
1379 if (! op_ret) {
1380 /* The error handler has taken care of the error. */
1381 return 0;
1382 }
1383
1384 bufferlist error_bl;
1385 error_bl.append(error_content);
1386 return send_response_data(error_bl, 0, error_bl.length());
1387 }
1388
1389 int RGWGetObj_ObjStore_SWIFT::send_response_data(bufferlist& bl,
1390 const off_t bl_ofs,
1391 const off_t bl_len)
1392 {
1393 string content_type;
1394
1395 if (sent_header) {
1396 goto send_data;
1397 }
1398
1399 if (custom_http_ret) {
1400 set_req_state_err(s, 0);
1401 dump_errno(s, custom_http_ret);
1402 } else {
1403 set_req_state_err(s, (partial_content && !op_ret) ? STATUS_PARTIAL_CONTENT
1404 : op_ret);
1405 dump_errno(s);
1406
1407 if (s->is_err()) {
1408 end_header(s, NULL);
1409 return 0;
1410 }
1411 }
1412
1413 if (range_str) {
1414 dump_range(s, ofs, end, s->obj_size);
1415 }
1416
1417 if (s->is_err()) {
1418 end_header(s, NULL);
1419 return 0;
1420 }
1421
1422 dump_content_length(s, total_len);
1423 dump_last_modified(s, lastmod);
1424 dump_header(s, "X-Timestamp", utime_t(lastmod));
1425 if (is_slo) {
1426 dump_header(s, "X-Static-Large-Object", "True");
1427 }
1428
1429 if (! op_ret) {
1430 if (! lo_etag.empty()) {
1431 dump_etag(s, lo_etag, true /* quoted */);
1432 } else {
1433 auto iter = attrs.find(RGW_ATTR_ETAG);
1434 if (iter != attrs.end()) {
1435 dump_etag(s, iter->second);
1436 }
1437 }
1438
1439 get_contype_from_attrs(attrs, content_type);
1440 dump_object_metadata(s, attrs);
1441 }
1442
1443 end_header(s, this, !content_type.empty() ? content_type.c_str()
1444 : "binary/octet-stream");
1445
1446 sent_header = true;
1447
1448 send_data:
1449 if (get_data && !op_ret) {
1450 const auto r = dump_body(s, bl.c_str() + bl_ofs, bl_len);
1451 if (r < 0) {
1452 return r;
1453 }
1454 }
1455 rgw_flush_formatter_and_reset(s, s->formatter);
1456
1457 return 0;
1458 }
1459
1460 void RGWOptionsCORS_ObjStore_SWIFT::send_response()
1461 {
1462 string hdrs, exp_hdrs;
1463 uint32_t max_age = CORS_MAX_AGE_INVALID;
1464 /*EACCES means, there is no CORS registered yet for the bucket
1465 *ENOENT means, there is no match of the Origin in the list of CORSRule
1466 */
1467 if (op_ret == -ENOENT)
1468 op_ret = -EACCES;
1469 if (op_ret < 0) {
1470 set_req_state_err(s, op_ret);
1471 dump_errno(s);
1472 end_header(s, NULL);
1473 return;
1474 }
1475 get_response_params(hdrs, exp_hdrs, &max_age);
1476 dump_errno(s);
1477 dump_access_control(s, origin, req_meth, hdrs.c_str(), exp_hdrs.c_str(),
1478 max_age);
1479 end_header(s, NULL);
1480 }
1481
1482 int RGWBulkDelete_ObjStore_SWIFT::get_data(
1483 list<RGWBulkDelete::acct_path_t>& items, bool * const is_truncated)
1484 {
1485 constexpr size_t MAX_LINE_SIZE = 2048;
1486
1487 RGWClientIOStreamBuf ciosb(static_cast<RGWRestfulIO&>(*(s->cio)),
1488 size_t(s->cct->_conf->rgw_max_chunk_size));
1489 istream cioin(&ciosb);
1490
1491 char buf[MAX_LINE_SIZE];
1492 while (cioin.getline(buf, sizeof(buf))) {
1493 string path_str(buf);
1494
1495 ldout(s->cct, 20) << "extracted Bulk Delete entry: " << path_str << dendl;
1496
1497 RGWBulkDelete::acct_path_t path;
1498
1499 /* We need to skip all slashes at the beginning in order to preserve
1500 * compliance with Swift. */
1501 const size_t start_pos = path_str.find_first_not_of('/');
1502
1503 if (string::npos != start_pos) {
1504 /* Seperator is the first slash after the leading ones. */
1505 const size_t sep_pos = path_str.find('/', start_pos);
1506
1507 if (string::npos != sep_pos) {
1508 path.bucket_name = url_decode(path_str.substr(start_pos,
1509 sep_pos - start_pos));
1510 path.obj_key = url_decode(path_str.substr(sep_pos + 1));
1511 } else {
1512 /* It's guaranteed here that bucket name is at least one character
1513 * long and is different than slash. */
1514 path.bucket_name = url_decode(path_str.substr(start_pos));
1515 }
1516
1517 items.push_back(path);
1518 }
1519
1520 if (items.size() == MAX_CHUNK_ENTRIES) {
1521 *is_truncated = true;
1522 return 0;
1523 }
1524 }
1525
1526 *is_truncated = false;
1527 return 0;
1528 }
1529
1530 void RGWBulkDelete_ObjStore_SWIFT::send_response()
1531 {
1532 set_req_state_err(s, op_ret);
1533 dump_errno(s);
1534 end_header(s, this /* RGWOp */, nullptr /* contype */,
1535 CHUNKED_TRANSFER_ENCODING);
1536
1537 bulkdelete_respond(deleter->get_num_deleted(),
1538 deleter->get_num_unfound(),
1539 deleter->get_failures(),
1540 s->prot_flags,
1541 *s->formatter);
1542 rgw_flush_formatter_and_reset(s, s->formatter);
1543 }
1544
1545
1546 std::unique_ptr<RGWBulkUploadOp::StreamGetter>
1547 RGWBulkUploadOp_ObjStore_SWIFT::create_stream()
1548 {
1549 class SwiftStreamGetter : public StreamGetter {
1550 const size_t conlen;
1551 size_t curpos;
1552 req_state* const s;
1553
1554 public:
1555 SwiftStreamGetter(req_state* const s, const size_t conlen)
1556 : conlen(conlen),
1557 curpos(0),
1558 s(s) {
1559 }
1560
1561 ssize_t get_at_most(size_t want, ceph::bufferlist& dst) override {
1562 /* maximum requested by a caller */
1563 /* data provided by client */
1564 /* RadosGW's limit. */
1565 const size_t max_chunk_size = \
1566 static_cast<size_t>(s->cct->_conf->rgw_max_chunk_size);
1567 const size_t max_to_read = std::min({ want, conlen - curpos, max_chunk_size });
1568
1569 ldout(s->cct, 20) << "bulk_upload: get_at_most max_to_read="
1570 << max_to_read
1571 << ", dst.c_str()=" << reinterpret_cast<intptr_t>(dst.c_str()) << dendl;
1572
1573 bufferptr bp(max_to_read);
1574 const auto read_len = recv_body(s, bp.c_str(), max_to_read);
1575 dst.append(bp, 0, read_len);
1576 //const auto read_len = recv_body(s, dst.c_str(), max_to_read);
1577 if (read_len < 0) {
1578 return read_len;
1579 }
1580
1581 curpos += read_len;
1582 return curpos > s->cct->_conf->rgw_max_put_size ? -ERR_TOO_LARGE
1583 : read_len;
1584 }
1585
1586 ssize_t get_exactly(size_t want, ceph::bufferlist& dst) override {
1587 ldout(s->cct, 20) << "bulk_upload: get_exactly want=" << want << dendl;
1588
1589 /* FIXME: do this in a loop. */
1590 const auto ret = get_at_most(want, dst);
1591 ldout(s->cct, 20) << "bulk_upload: get_exactly ret=" << ret << dendl;
1592 if (ret < 0) {
1593 return ret;
1594 } else if (static_cast<size_t>(ret) != want) {
1595 return -EINVAL;
1596 } else {
1597 return want;
1598 }
1599 }
1600 };
1601
1602 if (! s->length) {
1603 op_ret = -EINVAL;
1604 return nullptr;
1605 } else {
1606 ldout(s->cct, 20) << "bulk upload: create_stream for length="
1607 << s->length << dendl;
1608
1609 const size_t conlen = atoll(s->length);
1610 return std::unique_ptr<SwiftStreamGetter>(new SwiftStreamGetter(s, conlen));
1611 }
1612 }
1613
1614 void RGWBulkUploadOp_ObjStore_SWIFT::send_response()
1615 {
1616 set_req_state_err(s, op_ret);
1617 dump_errno(s);
1618 end_header(s, this /* RGWOp */, nullptr /* contype */,
1619 CHUNKED_TRANSFER_ENCODING);
1620 rgw_flush_formatter_and_reset(s, s->formatter);
1621
1622 s->formatter->open_object_section("delete");
1623
1624 std::string resp_status;
1625 std::string resp_body;
1626
1627 if (! failures.empty()) {
1628 rgw_err err;
1629
1630 const auto last_err = { failures.back().err };
1631 if (boost::algorithm::contains(last_err, terminal_errors)) {
1632 /* The terminal errors are affecting the status of the whole upload. */
1633 set_req_state_err(err, failures.back().err, s->prot_flags);
1634 } else {
1635 set_req_state_err(err, ERR_INVALID_REQUEST, s->prot_flags);
1636 }
1637
1638 dump_errno(err, resp_status);
1639 } else if (0 == num_created && failures.empty()) {
1640 /* Nothing created, nothing failed. This means the archive contained no
1641 * entity we could understand (regular file or directory). We need to
1642 * send 400 Bad Request to an HTTP client in the internal status field. */
1643 dump_errno(400, resp_status);
1644 resp_body = "Invalid Tar File: No Valid Files";
1645 } else {
1646 /* 200 OK */
1647 dump_errno(201, resp_status);
1648 }
1649
1650 encode_json("Number Files Created", num_created, s->formatter);
1651 encode_json("Response Body", resp_body, s->formatter);
1652 encode_json("Response Status", resp_status, s->formatter);
1653
1654 s->formatter->open_array_section("Errors");
1655 for (const auto& fail_desc : failures) {
1656 s->formatter->open_array_section("object");
1657
1658 encode_json("Name", fail_desc.path, s->formatter);
1659
1660 rgw_err err;
1661 set_req_state_err(err, fail_desc.err, s->prot_flags);
1662 std::string status;
1663 dump_errno(err, status);
1664 encode_json("Status", status, s->formatter);
1665
1666 s->formatter->close_section();
1667 }
1668 s->formatter->close_section();
1669
1670 s->formatter->close_section();
1671 rgw_flush_formatter_and_reset(s, s->formatter);
1672 }
1673
1674
1675 void RGWGetCrossDomainPolicy_ObjStore_SWIFT::send_response()
1676 {
1677 set_req_state_err(s, op_ret);
1678 dump_errno(s);
1679 end_header(s, this, "application/xml");
1680
1681 std::stringstream ss;
1682
1683 ss << R"(<?xml version="1.0"?>)" << "\n"
1684 << R"(<!DOCTYPE cross-domain-policy SYSTEM )"
1685 << R"("http://www.adobe.com/xml/dtds/cross-domain-policy.dtd" >)" << "\n"
1686 << R"(<cross-domain-policy>)" << "\n"
1687 << g_conf->rgw_cross_domain_policy << "\n"
1688 << R"(</cross-domain-policy>)";
1689
1690 dump_body(s, ss.str());
1691 }
1692
1693 void RGWGetHealthCheck_ObjStore_SWIFT::send_response()
1694 {
1695 set_req_state_err(s, op_ret);
1696 dump_errno(s);
1697 end_header(s, this, "application/xml");
1698
1699 if (op_ret) {
1700 static constexpr char DISABLED[] = "DISABLED BY FILE";
1701 dump_body(s, DISABLED, strlen(DISABLED));
1702 }
1703 }
1704
1705 const vector<pair<string, RGWInfo_ObjStore_SWIFT::info>> RGWInfo_ObjStore_SWIFT::swift_info =
1706 {
1707 {"bulk_delete", {false, nullptr}},
1708 {"container_quotas", {false, nullptr}},
1709 {"swift", {false, RGWInfo_ObjStore_SWIFT::list_swift_data}},
1710 {"tempurl", { false, RGWInfo_ObjStore_SWIFT::list_tempurl_data}},
1711 {"slo", {false, RGWInfo_ObjStore_SWIFT::list_slo_data}},
1712 {"account_quotas", {false, nullptr}},
1713 {"staticweb", {false, nullptr}},
1714 {"tempauth", {false, RGWInfo_ObjStore_SWIFT::list_tempauth_data}},
1715 };
1716
1717 void RGWInfo_ObjStore_SWIFT::execute()
1718 {
1719 bool is_admin_info_enabled = false;
1720
1721 const string& swiftinfo_sig = s->info.args.get("swiftinfo_sig");
1722 const string& swiftinfo_expires = s->info.args.get("swiftinfo_expires");
1723
1724 if (!swiftinfo_sig.empty() &&
1725 !swiftinfo_expires.empty() &&
1726 !is_expired(swiftinfo_expires, s->cct)) {
1727 is_admin_info_enabled = true;
1728 }
1729
1730 s->formatter->open_object_section("info");
1731
1732 for (const auto& pair : swift_info) {
1733 if(!is_admin_info_enabled && pair.second.is_admin_info)
1734 continue;
1735
1736 if (!pair.second.list_data) {
1737 s->formatter->open_object_section((pair.first).c_str());
1738 s->formatter->close_section();
1739 }
1740 else {
1741 pair.second.list_data(*(s->formatter), *(s->cct->_conf), *store);
1742 }
1743 }
1744
1745 s->formatter->close_section();
1746 }
1747
1748 void RGWInfo_ObjStore_SWIFT::send_response()
1749 {
1750 if (op_ret < 0) {
1751 op_ret = STATUS_NO_CONTENT;
1752 }
1753 set_req_state_err(s, op_ret);
1754 dump_errno(s);
1755 end_header(s, this);
1756 rgw_flush_formatter_and_reset(s, s->formatter);
1757 }
1758
1759 void RGWInfo_ObjStore_SWIFT::list_swift_data(Formatter& formatter,
1760 const md_config_t& config,
1761 RGWRados& store)
1762 {
1763 formatter.open_object_section("swift");
1764 formatter.dump_int("max_file_size", config.rgw_max_put_size);
1765 formatter.dump_int("container_listing_limit", RGW_LIST_BUCKETS_LIMIT_MAX);
1766
1767 string ceph_version(CEPH_GIT_NICE_VER);
1768 formatter.dump_string("version", ceph_version);
1769
1770 const size_t max_attr_name_len = \
1771 g_conf->get_val<size_t>("rgw_max_attr_name_len");
1772 if (max_attr_name_len) {
1773 const size_t meta_name_limit = \
1774 max_attr_name_len - strlen(RGW_ATTR_PREFIX RGW_AMZ_META_PREFIX);
1775 formatter.dump_int("max_meta_name_length", meta_name_limit);
1776 }
1777
1778 const size_t meta_value_limit = g_conf->get_val<size_t>("rgw_max_attr_size");
1779 if (meta_value_limit) {
1780 formatter.dump_int("max_meta_value_length", meta_value_limit);
1781 }
1782
1783 const size_t meta_num_limit = \
1784 g_conf->get_val<size_t>("rgw_max_attrs_num_in_req");
1785 if (meta_num_limit) {
1786 formatter.dump_int("max_meta_count", meta_num_limit);
1787 }
1788
1789 formatter.open_array_section("policies");
1790 RGWZoneGroup& zonegroup = store.get_zonegroup();
1791
1792 for (const auto& placement_targets : zonegroup.placement_targets) {
1793 formatter.open_object_section("policy");
1794 if (placement_targets.second.name.compare(zonegroup.default_placement) == 0)
1795 formatter.dump_bool("default", true);
1796 formatter.dump_string("name", placement_targets.second.name.c_str());
1797 formatter.close_section();
1798 }
1799 formatter.close_section();
1800
1801 formatter.dump_int("max_object_name_size", RGWHandler_REST::MAX_OBJ_NAME_LEN);
1802 formatter.dump_bool("strict_cors_mode", true);
1803 formatter.dump_int("max_container_name_length", RGWHandler_REST::MAX_BUCKET_NAME_LEN);
1804 formatter.close_section();
1805 }
1806
1807 void RGWInfo_ObjStore_SWIFT::list_tempauth_data(Formatter& formatter,
1808 const md_config_t& config,
1809 RGWRados& store)
1810 {
1811 formatter.open_object_section("tempauth");
1812 formatter.dump_bool("account_acls", true);
1813 formatter.close_section();
1814 }
1815 void RGWInfo_ObjStore_SWIFT::list_tempurl_data(Formatter& formatter,
1816 const md_config_t& config,
1817 RGWRados& store)
1818 {
1819 formatter.open_object_section("tempurl");
1820 formatter.open_array_section("methods");
1821 formatter.dump_string("methodname", "GET");
1822 formatter.dump_string("methodname", "HEAD");
1823 formatter.dump_string("methodname", "PUT");
1824 formatter.dump_string("methodname", "POST");
1825 formatter.dump_string("methodname", "DELETE");
1826 formatter.close_section();
1827 formatter.close_section();
1828 }
1829
1830 void RGWInfo_ObjStore_SWIFT::list_slo_data(Formatter& formatter,
1831 const md_config_t& config,
1832 RGWRados& store)
1833 {
1834 formatter.open_object_section("slo");
1835 formatter.dump_int("max_manifest_segments", config.rgw_max_slo_entries);
1836 formatter.close_section();
1837 }
1838
1839 bool RGWInfo_ObjStore_SWIFT::is_expired(const std::string& expires, CephContext* cct)
1840 {
1841 string err;
1842 const utime_t now = ceph_clock_now();
1843 const uint64_t expiration = (uint64_t)strict_strtoll(expires.c_str(),
1844 10, &err);
1845 if (!err.empty()) {
1846 ldout(cct, 5) << "failed to parse siginfo_expires: " << err << dendl;
1847 return true;
1848 }
1849
1850 if (expiration <= (uint64_t)now.sec()) {
1851 ldout(cct, 5) << "siginfo expired: " << expiration << " <= " << now.sec() << dendl;
1852 return true;
1853 }
1854
1855 return false;
1856 }
1857
1858
1859 void RGWFormPost::init(RGWRados* const store,
1860 req_state* const s,
1861 RGWHandler* const dialect_handler)
1862 {
1863 prefix = std::move(s->object.name);
1864 s->object = rgw_obj_key();
1865
1866 return RGWPostObj_ObjStore::init(store, s, dialect_handler);
1867 }
1868
1869 std::size_t RGWFormPost::get_max_file_size() /*const*/
1870 {
1871 std::string max_str = get_part_str(ctrl_parts, "max_file_size", "0");
1872
1873 std::string err;
1874 const std::size_t max_file_size =
1875 static_cast<uint64_t>(strict_strtoll(max_str.c_str(), 10, &err));
1876
1877 if (! err.empty()) {
1878 ldout(s->cct, 5) << "failed to parse FormPost's max_file_size: " << err
1879 << dendl;
1880 return 0;
1881 }
1882
1883 return max_file_size;
1884 }
1885
1886 bool RGWFormPost::is_non_expired()
1887 {
1888 std::string expires = get_part_str(ctrl_parts, "expires", "0");
1889
1890 std::string err;
1891 const uint64_t expires_timestamp =
1892 static_cast<uint64_t>(strict_strtoll(expires.c_str(), 10, &err));
1893
1894 if (! err.empty()) {
1895 dout(5) << "failed to parse FormPost's expires: " << err << dendl;
1896 return false;
1897 }
1898
1899 const utime_t now = ceph_clock_now();
1900 if (expires_timestamp <= static_cast<uint64_t>(now.sec())) {
1901 dout(5) << "FormPost form expired: "
1902 << expires_timestamp << " <= " << now.sec() << dendl;
1903 return false;
1904 }
1905
1906 return true;
1907 }
1908
1909 bool RGWFormPost::is_integral()
1910 {
1911 const std::string form_signature = get_part_str(ctrl_parts, "signature");
1912
1913 try {
1914 get_owner_info(s, *s->user);
1915 s->auth.identity = rgw::auth::transform_old_authinfo(s);
1916 } catch (...) {
1917 ldout(s->cct, 5) << "cannot get user_info of account's owner" << dendl;
1918 return false;
1919 }
1920
1921 for (const auto& kv : s->user->temp_url_keys) {
1922 const int temp_url_key_num = kv.first;
1923 const string& temp_url_key = kv.second;
1924
1925 if (temp_url_key.empty()) {
1926 continue;
1927 }
1928
1929 SignatureHelper sig_helper;
1930 sig_helper.calc(temp_url_key,
1931 s->info.request_uri,
1932 get_part_str(ctrl_parts, "redirect"),
1933 get_part_str(ctrl_parts, "max_file_size", "0"),
1934 get_part_str(ctrl_parts, "max_file_count", "0"),
1935 get_part_str(ctrl_parts, "expires", "0"));
1936
1937 const auto local_sig = sig_helper.get_signature();
1938
1939 ldout(s->cct, 20) << "FormPost signature [" << temp_url_key_num << "]"
1940 << " (calculated): " << local_sig << dendl;
1941
1942 if (sig_helper.is_equal_to(form_signature)) {
1943 return true;
1944 } else {
1945 ldout(s->cct, 5) << "FormPost's signature mismatch: "
1946 << local_sig << " != " << form_signature << dendl;
1947 }
1948 }
1949
1950 return false;
1951 }
1952
1953 void RGWFormPost::get_owner_info(const req_state* const s,
1954 RGWUserInfo& owner_info) const
1955 {
1956 /* We cannot use req_state::bucket_name because it isn't available
1957 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
1958 const string& bucket_name = s->init_state.url_bucket;
1959
1960 /* TempURL in Formpost only requires that bucket name is specified. */
1961 if (bucket_name.empty()) {
1962 throw -EPERM;
1963 }
1964
1965 string bucket_tenant;
1966 if (!s->account_name.empty()) {
1967 RGWUserInfo uinfo;
1968 bool found = false;
1969
1970 const rgw_user uid(s->account_name);
1971 if (uid.tenant.empty()) {
1972 const rgw_user tenanted_uid(uid.id, uid.id);
1973
1974 if (rgw_get_user_info_by_uid(store, tenanted_uid, uinfo) >= 0) {
1975 /* Succeeded. */
1976 bucket_tenant = uinfo.user_id.tenant;
1977 found = true;
1978 }
1979 }
1980
1981 if (!found && rgw_get_user_info_by_uid(store, uid, uinfo) < 0) {
1982 throw -EPERM;
1983 } else {
1984 bucket_tenant = uinfo.user_id.tenant;
1985 }
1986 }
1987
1988 /* Need to get user info of bucket owner. */
1989 RGWBucketInfo bucket_info;
1990 int ret = store->get_bucket_info(*static_cast<RGWObjectCtx *>(s->obj_ctx),
1991 bucket_tenant, bucket_name,
1992 bucket_info, nullptr);
1993 if (ret < 0) {
1994 throw ret;
1995 }
1996
1997 ldout(s->cct, 20) << "temp url user (bucket owner): " << bucket_info.owner
1998 << dendl;
1999
2000 if (rgw_get_user_info_by_uid(store, bucket_info.owner, owner_info) < 0) {
2001 throw -EPERM;
2002 }
2003 }
2004
2005 int RGWFormPost::get_params()
2006 {
2007 /* The parentt class extracts boundary info from the Content-Type. */
2008 int ret = RGWPostObj_ObjStore::get_params();
2009 if (ret < 0) {
2010 return ret;
2011 }
2012
2013 policy.create_default(s->user->user_id, s->user->display_name);
2014
2015 /* Let's start parsing the HTTP body by parsing each form part step-
2016 * by-step till encountering the first part with file data. */
2017 do {
2018 struct post_form_part part;
2019 ret = read_form_part_header(&part, stream_done);
2020 if (ret < 0) {
2021 return ret;
2022 }
2023
2024 if (s->cct->_conf->subsys.should_gather(ceph_subsys_rgw, 20)) {
2025 ldout(s->cct, 20) << "read part header -- part.name="
2026 << part.name << dendl;
2027
2028 for (const auto& pair : part.fields) {
2029 ldout(s->cct, 20) << "field.name=" << pair.first << dendl;
2030 ldout(s->cct, 20) << "field.val=" << pair.second.val << dendl;
2031 ldout(s->cct, 20) << "field.params:" << dendl;
2032
2033 for (const auto& param_pair : pair.second.params) {
2034 ldout(s->cct, 20) << " " << param_pair.first
2035 << " -> " << param_pair.second << dendl;
2036 }
2037 }
2038 }
2039
2040 if (stream_done) {
2041 /* Unexpected here. */
2042 err_msg = "Malformed request";
2043 return -EINVAL;
2044 }
2045
2046 const auto field_iter = part.fields.find("Content-Disposition");
2047 if (std::end(part.fields) != field_iter &&
2048 std::end(field_iter->second.params) != field_iter->second.params.find("filename")) {
2049 /* First data part ahead. */
2050 current_data_part = std::move(part);
2051
2052 /* Stop the iteration. We can assume that all control parts have been
2053 * already parsed. The rest of HTTP body should contain data parts
2054 * only. They will be picked up by ::get_data(). */
2055 break;
2056 } else {
2057 /* Control part ahead. Receive, parse and store for later usage. */
2058 bool boundary;
2059 ret = read_data(part.data, s->cct->_conf->rgw_max_chunk_size,
2060 boundary, stream_done);
2061 if (ret < 0) {
2062 return ret;
2063 } else if (! boundary) {
2064 err_msg = "Couldn't find boundary";
2065 return -EINVAL;
2066 }
2067
2068 ctrl_parts[part.name] = std::move(part);
2069 }
2070 } while (! stream_done);
2071
2072 min_len = 0;
2073 max_len = get_max_file_size();
2074
2075 if (! current_data_part) {
2076 err_msg = "FormPost: no files to process";
2077 return -EINVAL;
2078 }
2079
2080 if (! is_non_expired()) {
2081 err_msg = "FormPost: Form Expired";
2082 return -EPERM;
2083 }
2084
2085 if (! is_integral()) {
2086 err_msg = "FormPost: Invalid Signature";
2087 return -EPERM;
2088 }
2089
2090 return 0;
2091 }
2092
2093 std::string RGWFormPost::get_current_filename() const
2094 {
2095 try {
2096 const auto& field = current_data_part->fields.at("Content-Disposition");
2097 const auto iter = field.params.find("filename");
2098
2099 if (std::end(field.params) != iter) {
2100 return prefix + iter->second;
2101 }
2102 } catch (std::out_of_range&) {
2103 /* NOP */;
2104 }
2105
2106 return prefix;
2107 }
2108
2109 std::string RGWFormPost::get_current_content_type() const
2110 {
2111 try {
2112 const auto& field = current_data_part->fields.at("Content-Type");
2113 return field.val;
2114 } catch (std::out_of_range&) {
2115 /* NOP */;
2116 }
2117
2118 return std::string();
2119 }
2120
2121 bool RGWFormPost::is_next_file_to_upload()
2122 {
2123 if (! stream_done) {
2124 /* We have at least one additional part in the body. */
2125 struct post_form_part part;
2126 int r = read_form_part_header(&part, stream_done);
2127 if (r < 0) {
2128 return false;
2129 }
2130
2131 const auto field_iter = part.fields.find("Content-Disposition");
2132 if (std::end(part.fields) != field_iter) {
2133 const auto& params = field_iter->second.params;
2134 const auto& filename_iter = params.find("filename");
2135
2136 if (std::end(params) != filename_iter && ! filename_iter->second.empty()) {
2137 current_data_part = std::move(part);
2138 return true;
2139 }
2140 }
2141 }
2142
2143 return false;
2144 }
2145
2146 int RGWFormPost::get_data(ceph::bufferlist& bl, bool& again)
2147 {
2148 bool boundary;
2149
2150 int r = read_data(bl, s->cct->_conf->rgw_max_chunk_size,
2151 boundary, stream_done);
2152 if (r < 0) {
2153 return r;
2154 }
2155
2156 /* Tell RGWPostObj::execute() that it has some data to put. */
2157 again = !boundary;
2158
2159 return bl.length();
2160 }
2161
2162 void RGWFormPost::send_response()
2163 {
2164 std::string redirect = get_part_str(ctrl_parts, "redirect");
2165 if (! redirect.empty()) {
2166 op_ret = STATUS_REDIRECT;
2167 }
2168
2169 set_req_state_err(s, op_ret);
2170 s->err.err_code = err_msg;
2171 dump_errno(s);
2172 if (! redirect.empty()) {
2173 dump_redirect(s, redirect);
2174 }
2175 end_header(s, this);
2176 }
2177
2178 bool RGWFormPost::is_formpost_req(req_state* const s)
2179 {
2180 std::string content_type;
2181 std::map<std::string, std::string> params;
2182
2183 parse_boundary_params(s->info.env->get("CONTENT_TYPE", ""),
2184 content_type, params);
2185
2186 return boost::algorithm::iequals(content_type, "multipart/form-data") &&
2187 params.count("boundary") > 0;
2188 }
2189
2190
2191 RGWOp *RGWHandler_REST_Service_SWIFT::op_get()
2192 {
2193 return new RGWListBuckets_ObjStore_SWIFT;
2194 }
2195
2196 RGWOp *RGWHandler_REST_Service_SWIFT::op_head()
2197 {
2198 return new RGWStatAccount_ObjStore_SWIFT;
2199 }
2200
2201 RGWOp *RGWHandler_REST_Service_SWIFT::op_put()
2202 {
2203 if (s->info.args.exists("extract-archive")) {
2204 return new RGWBulkUploadOp_ObjStore_SWIFT;
2205 }
2206 return nullptr;
2207 }
2208
2209 RGWOp *RGWHandler_REST_Service_SWIFT::op_post()
2210 {
2211 if (s->info.args.exists("bulk-delete")) {
2212 return new RGWBulkDelete_ObjStore_SWIFT;
2213 }
2214 return new RGWPutMetadataAccount_ObjStore_SWIFT;
2215 }
2216
2217 RGWOp *RGWHandler_REST_Service_SWIFT::op_delete()
2218 {
2219 if (s->info.args.exists("bulk-delete")) {
2220 return new RGWBulkDelete_ObjStore_SWIFT;
2221 }
2222 return NULL;
2223 }
2224
2225 int RGWSwiftWebsiteHandler::serve_errordoc(const int http_ret,
2226 const std::string error_doc)
2227 {
2228 /* Try to throw it all away. */
2229 s->formatter->reset();
2230
2231 class RGWGetErrorPage : public RGWGetObj_ObjStore_SWIFT {
2232 public:
2233 RGWGetErrorPage(RGWRados* const store,
2234 RGWHandler_REST* const handler,
2235 req_state* const s,
2236 const int http_ret) {
2237 /* Calling a virtual from the base class is safe as the subobject should
2238 * be properly initialized and we haven't overridden the init method. */
2239 init(store, s, handler);
2240 set_get_data(true);
2241 set_custom_http_response(http_ret);
2242 }
2243
2244 int error_handler(const int err_no,
2245 std::string* const error_content) override {
2246 /* Enforce that any error generated while getting the error page will
2247 * not be send to a client. This allows us to recover from the double
2248 * fault situation by sending the original message. */
2249 return 0;
2250 }
2251 } get_errpage_op(store, handler, s, http_ret);
2252
2253 s->object = std::to_string(http_ret) + error_doc;
2254
2255 RGWOp* newop = &get_errpage_op;
2256 RGWRequest req(0);
2257 return rgw_process_authenticated(handler, newop, &req, s, true);
2258 }
2259
2260 int RGWSwiftWebsiteHandler::error_handler(const int err_no,
2261 std::string* const error_content)
2262 {
2263 const auto& ws_conf = s->bucket_info.website_conf;
2264
2265 if (can_be_website_req() && ! ws_conf.error_doc.empty()) {
2266 set_req_state_err(s, err_no);
2267 return serve_errordoc(s->err.http_ret, ws_conf.error_doc);
2268 }
2269
2270 /* Let's go to the default, no-op handler. */
2271 return err_no;
2272 }
2273
2274 bool RGWSwiftWebsiteHandler::is_web_mode() const
2275 {
2276 const boost::string_ref webmode = s->info.env->get("HTTP_X_WEB_MODE", "");
2277 return boost::algorithm::iequals(webmode, "true");
2278 }
2279
2280 bool RGWSwiftWebsiteHandler::can_be_website_req() const
2281 {
2282 /* Static website works only with the GET or HEAD method. Nothing more. */
2283 static const std::set<boost::string_ref> ws_methods = { "GET", "HEAD" };
2284 if (ws_methods.count(s->info.method) == 0) {
2285 return false;
2286 }
2287
2288 /* We also need to handle early failures from the auth system. In such cases
2289 * req_state::auth.identity may be empty. Let's treat that the same way as
2290 * the anonymous access. */
2291 if (! s->auth.identity) {
2292 return true;
2293 }
2294
2295 /* Swift serves websites only for anonymous requests unless client explicitly
2296 * requested this behaviour by supplying X-Web-Mode HTTP header set to true. */
2297 if (s->auth.identity->is_anonymous() || is_web_mode()) {
2298 return true;
2299 }
2300
2301 return false;
2302 }
2303
2304 RGWOp* RGWSwiftWebsiteHandler::get_ws_redirect_op()
2305 {
2306 class RGWMovedPermanently: public RGWOp {
2307 const std::string location;
2308 public:
2309 RGWMovedPermanently(const std::string& location)
2310 : location(location) {
2311 }
2312
2313 int verify_permission() override {
2314 return 0;
2315 }
2316
2317 void execute() override {
2318 op_ret = -ERR_PERMANENT_REDIRECT;
2319 return;
2320 }
2321
2322 void send_response() override {
2323 set_req_state_err(s, op_ret);
2324 dump_errno(s);
2325 dump_content_length(s, 0);
2326 dump_redirect(s, location);
2327 end_header(s, this);
2328 }
2329
2330 const string name() override {
2331 return "RGWMovedPermanently";
2332 }
2333 };
2334
2335 return new RGWMovedPermanently(s->info.request_uri + '/');
2336 }
2337
2338 RGWOp* RGWSwiftWebsiteHandler::get_ws_index_op()
2339 {
2340 /* Retarget to get obj on requested index file. */
2341 if (! s->object.empty()) {
2342 s->object = s->object.name +
2343 s->bucket_info.website_conf.get_index_doc();
2344 } else {
2345 s->object = s->bucket_info.website_conf.get_index_doc();
2346 }
2347
2348 auto getop = new RGWGetObj_ObjStore_SWIFT;
2349 getop->set_get_data(boost::algorithm::equals("GET", s->info.method));
2350
2351 return getop;
2352 }
2353
2354 RGWOp* RGWSwiftWebsiteHandler::get_ws_listing_op()
2355 {
2356 class RGWWebsiteListing : public RGWListBucket_ObjStore_SWIFT {
2357 const std::string prefix_override;
2358
2359 int get_params() override {
2360 prefix = prefix_override;
2361 max = default_max;
2362 delimiter = "/";
2363 return 0;
2364 }
2365
2366 void send_response() override {
2367 /* Generate the header now. */
2368 set_req_state_err(s, op_ret);
2369 dump_errno(s);
2370 dump_container_metadata(s, bucket, bucket_quota,
2371 s->bucket_info.website_conf);
2372 end_header(s, this, "text/html");
2373 if (op_ret < 0) {
2374 return;
2375 }
2376
2377 /* Now it's the time to start generating HTML bucket listing.
2378 * All the crazy stuff with crafting tags will be delegated to
2379 * RGWSwiftWebsiteListingFormatter. */
2380 std::stringstream ss;
2381 RGWSwiftWebsiteListingFormatter htmler(ss, prefix);
2382
2383 const auto& ws_conf = s->bucket_info.website_conf;
2384 htmler.generate_header(s->decoded_uri,
2385 ws_conf.listing_css_doc);
2386
2387 for (const auto& pair : common_prefixes) {
2388 std::string subdir_name = pair.first;
2389 if (! subdir_name.empty()) {
2390 /* To be compliant with Swift we need to remove the trailing
2391 * slash. */
2392 subdir_name.pop_back();
2393 }
2394
2395 htmler.dump_subdir(subdir_name);
2396 }
2397
2398 for (const rgw_bucket_dir_entry& obj : objs) {
2399 if (! common_prefixes.count(obj.key.name + '/')) {
2400 htmler.dump_object(obj);
2401 }
2402 }
2403
2404 htmler.generate_footer();
2405 dump_body(s, ss.str());
2406 }
2407 public:
2408 /* Taking prefix_override by value to leverage std::string r-value ref
2409 * ctor and thus avoid extra memory copying/increasing ref counter. */
2410 RGWWebsiteListing(std::string prefix_override)
2411 : prefix_override(std::move(prefix_override)) {
2412 }
2413 };
2414
2415 std::string prefix = std::move(s->object.name);
2416 s->object = rgw_obj_key();
2417
2418 return new RGWWebsiteListing(std::move(prefix));
2419 }
2420
2421 bool RGWSwiftWebsiteHandler::is_web_dir() const
2422 {
2423 std::string subdir_name = url_decode(s->object.name);
2424
2425 /* Remove character from the subdir name if it is "/". */
2426 if (subdir_name.empty()) {
2427 return false;
2428 } else if (subdir_name.back() == '/') {
2429 subdir_name.pop_back();
2430 }
2431
2432 rgw_obj obj(s->bucket, std::move(subdir_name));
2433
2434 /* First, get attrset of the object we'll try to retrieve. */
2435 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
2436 obj_ctx.obj.set_atomic(obj);
2437 obj_ctx.obj.set_prefetch_data(obj);
2438
2439 RGWObjState* state = nullptr;
2440 if (store->get_obj_state(&obj_ctx, s->bucket_info, obj, &state, false) < 0) {
2441 return false;
2442 }
2443
2444 /* A nonexistent object cannot be a considered as a marker representing
2445 * the emulation of catalog in FS hierarchy. */
2446 if (! state->exists) {
2447 return false;
2448 }
2449
2450 /* Decode the content type. */
2451 std::string content_type;
2452 get_contype_from_attrs(state->attrset, content_type);
2453
2454 const auto& ws_conf = s->bucket_info.website_conf;
2455 const std::string subdir_marker = ws_conf.subdir_marker.empty()
2456 ? "application/directory"
2457 : ws_conf.subdir_marker;
2458 return subdir_marker == content_type && state->size <= 1;
2459 }
2460
2461 bool RGWSwiftWebsiteHandler::is_index_present(const std::string& index)
2462 {
2463 rgw_obj obj(s->bucket, index);
2464
2465 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
2466 obj_ctx.obj.set_atomic(obj);
2467 obj_ctx.obj.set_prefetch_data(obj);
2468
2469 RGWObjState* state = nullptr;
2470 if (store->get_obj_state(&obj_ctx, s->bucket_info, obj, &state, false) < 0) {
2471 return false;
2472 }
2473
2474 /* A nonexistent object cannot be a considered as a viable index. We will
2475 * try to list the bucket or - if this is impossible - return an error. */
2476 return state->exists;
2477 }
2478
2479 int RGWSwiftWebsiteHandler::retarget_bucket(RGWOp* op, RGWOp** new_op)
2480 {
2481 ldout(s->cct, 10) << "Starting retarget" << dendl;
2482 RGWOp* op_override = nullptr;
2483
2484 /* In Swift static web content is served if the request is anonymous or
2485 * has X-Web-Mode HTTP header specified to true. */
2486 if (can_be_website_req()) {
2487 const auto& ws_conf = s->bucket_info.website_conf;
2488 const auto& index = s->bucket_info.website_conf.get_index_doc();
2489
2490 if (s->decoded_uri.back() != '/') {
2491 op_override = get_ws_redirect_op();
2492 } else if (! index.empty() && is_index_present(index)) {
2493 op_override = get_ws_index_op();
2494 } else if (ws_conf.listing_enabled) {
2495 op_override = get_ws_listing_op();
2496 }
2497 }
2498
2499 if (op_override) {
2500 handler->put_op(op);
2501 op_override->init(store, s, handler);
2502
2503 *new_op = op_override;
2504 } else {
2505 *new_op = op;
2506 }
2507
2508 /* Return 404 Not Found is the request has web mode enforced but we static web
2509 * wasn't able to serve it accordingly. */
2510 return ! op_override && is_web_mode() ? -ENOENT : 0;
2511 }
2512
2513 int RGWSwiftWebsiteHandler::retarget_object(RGWOp* op, RGWOp** new_op)
2514 {
2515 ldout(s->cct, 10) << "Starting object retarget" << dendl;
2516 RGWOp* op_override = nullptr;
2517
2518 /* In Swift static web content is served if the request is anonymous or
2519 * has X-Web-Mode HTTP header specified to true. */
2520 if (can_be_website_req() && is_web_dir()) {
2521 const auto& ws_conf = s->bucket_info.website_conf;
2522 const auto& index = s->bucket_info.website_conf.get_index_doc();
2523
2524 if (s->decoded_uri.back() != '/') {
2525 op_override = get_ws_redirect_op();
2526 } else if (! index.empty() && is_index_present(index)) {
2527 op_override = get_ws_index_op();
2528 } else if (ws_conf.listing_enabled) {
2529 op_override = get_ws_listing_op();
2530 }
2531 } else {
2532 /* A regular request or the specified object isn't a subdirectory marker.
2533 * We don't need any re-targeting. Error handling (like sending a custom
2534 * error page) will be performed by error_handler of the actual RGWOp. */
2535 return 0;
2536 }
2537
2538 if (op_override) {
2539 handler->put_op(op);
2540 op_override->init(store, s, handler);
2541
2542 *new_op = op_override;
2543 } else {
2544 *new_op = op;
2545 }
2546
2547 /* Return 404 Not Found if we aren't able to re-target for subdir marker. */
2548 return ! op_override ? -ENOENT : 0;
2549 }
2550
2551
2552 RGWOp *RGWHandler_REST_Bucket_SWIFT::get_obj_op(bool get_data)
2553 {
2554 if (is_acl_op()) {
2555 return new RGWGetACLs_ObjStore_SWIFT;
2556 }
2557
2558 if (get_data)
2559 return new RGWListBucket_ObjStore_SWIFT;
2560 else
2561 return new RGWStatBucket_ObjStore_SWIFT;
2562 }
2563
2564 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_get()
2565 {
2566 return get_obj_op(true);
2567 }
2568
2569 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_head()
2570 {
2571 return get_obj_op(false);
2572 }
2573
2574 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_put()
2575 {
2576 if (is_acl_op()) {
2577 return new RGWPutACLs_ObjStore_SWIFT;
2578 }
2579 if(s->info.args.exists("extract-archive")) {
2580 return new RGWBulkUploadOp_ObjStore_SWIFT;
2581 }
2582 return new RGWCreateBucket_ObjStore_SWIFT;
2583 }
2584
2585 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_delete()
2586 {
2587 return new RGWDeleteBucket_ObjStore_SWIFT;
2588 }
2589
2590 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_post()
2591 {
2592 if (RGWFormPost::is_formpost_req(s)) {
2593 return new RGWFormPost;
2594 } else {
2595 return new RGWPutMetadataBucket_ObjStore_SWIFT;
2596 }
2597 }
2598
2599 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_options()
2600 {
2601 return new RGWOptionsCORS_ObjStore_SWIFT;
2602 }
2603
2604
2605 RGWOp *RGWHandler_REST_Obj_SWIFT::get_obj_op(bool get_data)
2606 {
2607 if (is_acl_op()) {
2608 return new RGWGetACLs_ObjStore_SWIFT;
2609 }
2610
2611 RGWGetObj_ObjStore_SWIFT *get_obj_op = new RGWGetObj_ObjStore_SWIFT;
2612 get_obj_op->set_get_data(get_data);
2613 return get_obj_op;
2614 }
2615
2616 RGWOp *RGWHandler_REST_Obj_SWIFT::op_get()
2617 {
2618 return get_obj_op(true);
2619 }
2620
2621 RGWOp *RGWHandler_REST_Obj_SWIFT::op_head()
2622 {
2623 return get_obj_op(false);
2624 }
2625
2626 RGWOp *RGWHandler_REST_Obj_SWIFT::op_put()
2627 {
2628 if (is_acl_op()) {
2629 return new RGWPutACLs_ObjStore_SWIFT;
2630 }
2631 if(s->info.args.exists("extract-archive")) {
2632 return new RGWBulkUploadOp_ObjStore_SWIFT;
2633 }
2634 if (s->init_state.src_bucket.empty())
2635 return new RGWPutObj_ObjStore_SWIFT;
2636 else
2637 return new RGWCopyObj_ObjStore_SWIFT;
2638 }
2639
2640 RGWOp *RGWHandler_REST_Obj_SWIFT::op_delete()
2641 {
2642 return new RGWDeleteObj_ObjStore_SWIFT;
2643 }
2644
2645 RGWOp *RGWHandler_REST_Obj_SWIFT::op_post()
2646 {
2647 if (RGWFormPost::is_formpost_req(s)) {
2648 return new RGWFormPost;
2649 } else {
2650 return new RGWPutMetadataObject_ObjStore_SWIFT;
2651 }
2652 }
2653
2654 RGWOp *RGWHandler_REST_Obj_SWIFT::op_copy()
2655 {
2656 return new RGWCopyObj_ObjStore_SWIFT;
2657 }
2658
2659 RGWOp *RGWHandler_REST_Obj_SWIFT::op_options()
2660 {
2661 return new RGWOptionsCORS_ObjStore_SWIFT;
2662 }
2663
2664
2665 int RGWHandler_REST_SWIFT::authorize()
2666 {
2667 return rgw::auth::Strategy::apply(auth_strategy, s);
2668 }
2669
2670 int RGWHandler_REST_SWIFT::postauth_init()
2671 {
2672 struct req_init_state* t = &s->init_state;
2673
2674 /* XXX Stub this until Swift Auth sets account into URL. */
2675 s->bucket_tenant = s->user->user_id.tenant;
2676 s->bucket_name = t->url_bucket;
2677
2678 dout(10) << "s->object=" <<
2679 (!s->object.empty() ? s->object : rgw_obj_key("<NULL>"))
2680 << " s->bucket="
2681 << rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name)
2682 << dendl;
2683
2684 int ret;
2685 ret = rgw_validate_tenant_name(s->bucket_tenant);
2686 if (ret)
2687 return ret;
2688 ret = validate_bucket_name(s->bucket_name);
2689 if (ret)
2690 return ret;
2691 ret = validate_object_name(s->object.name);
2692 if (ret)
2693 return ret;
2694
2695 if (!t->src_bucket.empty()) {
2696 /*
2697 * We don't allow cross-tenant copy at present. It requires account
2698 * names in the URL for Swift.
2699 */
2700 s->src_tenant_name = s->user->user_id.tenant;
2701 s->src_bucket_name = t->src_bucket;
2702
2703 ret = validate_bucket_name(s->src_bucket_name);
2704 if (ret < 0) {
2705 return ret;
2706 }
2707 ret = validate_object_name(s->src_object.name);
2708 if (ret < 0) {
2709 return ret;
2710 }
2711 }
2712
2713 return 0;
2714 }
2715
2716 int RGWHandler_REST_SWIFT::validate_bucket_name(const string& bucket)
2717 {
2718 const size_t len = bucket.size();
2719
2720 if (len > MAX_BUCKET_NAME_LEN) {
2721 /* Bucket Name too long. Generate custom error message and bind it
2722 * to an R-value reference. */
2723 const auto msg = boost::str(
2724 boost::format("Container name length of %lld longer than %lld")
2725 % len % int(MAX_BUCKET_NAME_LEN));
2726 set_req_state_err(s, ERR_INVALID_BUCKET_NAME, msg);
2727 return -ERR_INVALID_BUCKET_NAME;
2728 }
2729
2730 const auto ret = RGWHandler_REST::validate_bucket_name(bucket);
2731 if (ret < 0) {
2732 return ret;
2733 }
2734
2735 if (len == 0)
2736 return 0;
2737
2738 if (bucket[0] == '.')
2739 return -ERR_INVALID_BUCKET_NAME;
2740
2741 if (check_utf8(bucket.c_str(), len))
2742 return -ERR_INVALID_UTF8;
2743
2744 const char *s = bucket.c_str();
2745
2746 for (size_t i = 0; i < len; ++i, ++s) {
2747 if (*(unsigned char *)s == 0xff)
2748 return -ERR_INVALID_BUCKET_NAME;
2749 }
2750
2751 return 0;
2752 }
2753
2754 static void next_tok(string& str, string& tok, char delim)
2755 {
2756 if (str.size() == 0) {
2757 tok = "";
2758 return;
2759 }
2760 tok = str;
2761 int pos = str.find(delim);
2762 if (pos > 0) {
2763 tok = str.substr(0, pos);
2764 str = str.substr(pos + 1);
2765 } else {
2766 str = "";
2767 }
2768 }
2769
2770 int RGWHandler_REST_SWIFT::init_from_header(struct req_state* const s,
2771 const std::string& frontend_prefix)
2772 {
2773 string req;
2774 string first;
2775
2776 s->prot_flags |= RGW_REST_SWIFT;
2777
2778 char reqbuf[frontend_prefix.length() + s->decoded_uri.length() + 1];
2779 sprintf(reqbuf, "%s%s", frontend_prefix.c_str(), s->decoded_uri.c_str());
2780 const char *req_name = reqbuf;
2781
2782 const char *p;
2783
2784 if (*req_name == '?') {
2785 p = req_name;
2786 } else {
2787 p = s->info.request_params.c_str();
2788 }
2789
2790 s->info.args.set(p);
2791 s->info.args.parse();
2792
2793 /* Skip the leading slash of URL hierarchy. */
2794 if (req_name[0] != '/') {
2795 return 0;
2796 } else {
2797 req_name++;
2798 }
2799
2800 if ('\0' == req_name[0]) {
2801 return g_conf->rgw_swift_url_prefix == "/" ? -ERR_BAD_URL : 0;
2802 }
2803
2804 req = req_name;
2805
2806 size_t pos = req.find('/');
2807 if (std::string::npos != pos && g_conf->rgw_swift_url_prefix != "/") {
2808 bool cut_url = g_conf->rgw_swift_url_prefix.length();
2809 first = req.substr(0, pos);
2810
2811 if (first.compare(g_conf->rgw_swift_url_prefix) == 0) {
2812 if (cut_url) {
2813 /* Rewind to the "v1/..." part. */
2814 next_tok(req, first, '/');
2815 }
2816 }
2817 } else if (req.compare(g_conf->rgw_swift_url_prefix) == 0) {
2818 s->formatter = new RGWFormatter_Plain;
2819 return -ERR_BAD_URL;
2820 } else {
2821 first = req;
2822 }
2823
2824 std::string tenant_path;
2825 if (! g_conf->rgw_swift_tenant_name.empty()) {
2826 tenant_path = "/AUTH_";
2827 tenant_path.append(g_conf->rgw_swift_tenant_name);
2828 }
2829
2830 /* verify that the request_uri conforms with what's expected */
2831 char buf[g_conf->rgw_swift_url_prefix.length() + 16 + tenant_path.length()];
2832 int blen;
2833 if (g_conf->rgw_swift_url_prefix == "/") {
2834 blen = sprintf(buf, "/v1%s", tenant_path.c_str());
2835 } else {
2836 blen = sprintf(buf, "/%s/v1%s",
2837 g_conf->rgw_swift_url_prefix.c_str(), tenant_path.c_str());
2838 }
2839
2840 if (strncmp(reqbuf, buf, blen) != 0) {
2841 return -ENOENT;
2842 }
2843
2844 int ret = allocate_formatter(s, RGW_FORMAT_PLAIN, true);
2845 if (ret < 0)
2846 return ret;
2847
2848 string ver;
2849
2850 next_tok(req, ver, '/');
2851
2852 if (!tenant_path.empty() || g_conf->rgw_swift_account_in_url) {
2853 string account_name;
2854 next_tok(req, account_name, '/');
2855
2856 /* Erase all pre-defined prefixes like "AUTH_" or "KEY_". */
2857 const vector<string> skipped_prefixes = { "AUTH_", "KEY_" };
2858
2859 for (const auto pfx : skipped_prefixes) {
2860 const size_t comp_len = min(account_name.length(), pfx.length());
2861 if (account_name.compare(0, comp_len, pfx) == 0) {
2862 /* Prefix is present. Drop it. */
2863 account_name = account_name.substr(comp_len);
2864 break;
2865 }
2866 }
2867
2868 if (account_name.empty()) {
2869 return -ERR_PRECONDITION_FAILED;
2870 } else {
2871 s->account_name = account_name;
2872 }
2873 }
2874
2875 next_tok(req, first, '/');
2876
2877 dout(10) << "ver=" << ver << " first=" << first << " req=" << req << dendl;
2878 if (first.size() == 0)
2879 return 0;
2880
2881 s->info.effective_uri = "/" + first;
2882
2883 // Save bucket to tide us over until token is parsed.
2884 s->init_state.url_bucket = first;
2885
2886 if (req.size()) {
2887 s->object =
2888 rgw_obj_key(req, s->info.env->get("HTTP_X_OBJECT_VERSION_ID", "")); /* rgw swift extension */
2889 s->info.effective_uri.append("/" + s->object.name);
2890 }
2891
2892 return 0;
2893 }
2894
2895 int RGWHandler_REST_SWIFT::init(RGWRados* store, struct req_state* s,
2896 rgw::io::BasicClient *cio)
2897 {
2898 struct req_init_state *t = &s->init_state;
2899
2900 s->dialect = "swift";
2901
2902 std::string copy_source =
2903 url_decode(s->info.env->get("HTTP_X_COPY_FROM", ""));
2904 if (! copy_source.empty()) {
2905 bool result = RGWCopyObj::parse_copy_location(copy_source, t->src_bucket,
2906 s->src_object);
2907 if (!result)
2908 return -ERR_BAD_URL;
2909 }
2910
2911 if (s->op == OP_COPY) {
2912 std::string req_dest =
2913 url_decode(s->info.env->get("HTTP_DESTINATION", ""));
2914 if (req_dest.empty())
2915 return -ERR_BAD_URL;
2916
2917 std::string dest_bucket_name;
2918 rgw_obj_key dest_obj_key;
2919 bool result =
2920 RGWCopyObj::parse_copy_location(req_dest, dest_bucket_name,
2921 dest_obj_key);
2922 if (!result)
2923 return -ERR_BAD_URL;
2924
2925 std::string dest_object = dest_obj_key.name;
2926
2927 /* convert COPY operation into PUT */
2928 t->src_bucket = t->url_bucket;
2929 s->src_object = s->object;
2930 t->url_bucket = dest_bucket_name;
2931 s->object = rgw_obj_key(dest_object);
2932 s->op = OP_PUT;
2933 }
2934
2935 return RGWHandler_REST::init(store, s, cio);
2936 }
2937
2938 RGWHandler_REST*
2939 RGWRESTMgr_SWIFT::get_handler(struct req_state* const s,
2940 const rgw::auth::StrategyRegistry& auth_registry,
2941 const std::string& frontend_prefix)
2942 {
2943 int ret = RGWHandler_REST_SWIFT::init_from_header(s, frontend_prefix);
2944 if (ret < 0) {
2945 ldout(s->cct, 10) << "init_from_header returned err=" << ret << dendl;
2946 return nullptr;
2947 }
2948
2949 const auto& auth_strategy = auth_registry.get_swift();
2950
2951 if (s->init_state.url_bucket.empty()) {
2952 return new RGWHandler_REST_Service_SWIFT(auth_strategy);
2953 }
2954
2955 if (s->object.empty()) {
2956 return new RGWHandler_REST_Bucket_SWIFT(auth_strategy);
2957 }
2958
2959 return new RGWHandler_REST_Obj_SWIFT(auth_strategy);
2960 }
2961
2962 RGWHandler_REST* RGWRESTMgr_SWIFT_Info::get_handler(
2963 struct req_state* const s,
2964 const rgw::auth::StrategyRegistry& auth_registry,
2965 const std::string& frontend_prefix)
2966 {
2967 s->prot_flags |= RGW_REST_SWIFT;
2968 const auto& auth_strategy = auth_registry.get_swift();
2969 return new RGWHandler_REST_SWIFT_Info(auth_strategy);
2970 }