]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_op.cc
import ceph 16.2.7
[ceph.git] / ceph / src / rgw / rgw_op.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
3
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <system_error>
7 #include <unistd.h>
8
9 #include <sstream>
10 #include <string_view>
11
12 #include <boost/algorithm/string/predicate.hpp>
13 #include <boost/optional.hpp>
14 #include <boost/utility/in_place_factory.hpp>
15
16 #include "include/scope_guard.h"
17 #include "common/Clock.h"
18 #include "common/armor.h"
19 #include "common/errno.h"
20 #include "common/mime.h"
21 #include "common/utf8.h"
22 #include "common/ceph_json.h"
23 #include "common/static_ptr.h"
24
25 #include "rgw_rados.h"
26 #include "rgw_zone.h"
27 #include "rgw_op.h"
28 #include "rgw_rest.h"
29 #include "rgw_acl.h"
30 #include "rgw_acl_s3.h"
31 #include "rgw_acl_swift.h"
32 #include "rgw_aio_throttle.h"
33 #include "rgw_user.h"
34 #include "rgw_bucket.h"
35 #include "rgw_log.h"
36 #include "rgw_multi.h"
37 #include "rgw_multi_del.h"
38 #include "rgw_cors.h"
39 #include "rgw_cors_s3.h"
40 #include "rgw_rest_conn.h"
41 #include "rgw_rest_s3.h"
42 #include "rgw_tar.h"
43 #include "rgw_client_io.h"
44 #include "rgw_compression.h"
45 #include "rgw_role.h"
46 #include "rgw_tag_s3.h"
47 #include "rgw_putobj_processor.h"
48 #include "rgw_crypt.h"
49 #include "rgw_perf_counters.h"
50 #include "rgw_notify.h"
51 #include "rgw_notify_event_type.h"
52 #include "rgw_sal_rados.h"
53
54 #include "services/svc_zone.h"
55 #include "services/svc_quota.h"
56 #include "services/svc_sys_obj.h"
57
58 #include "cls/lock/cls_lock_client.h"
59 #include "cls/rgw/cls_rgw_client.h"
60
61
62 #include "include/ceph_assert.h"
63
64 #include "compressor/Compressor.h"
65
66 #ifdef WITH_LTTNG
67 #define TRACEPOINT_DEFINE
68 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
69 #include "tracing/rgw_op.h"
70 #undef TRACEPOINT_PROBE_DYNAMIC_LINKAGE
71 #undef TRACEPOINT_DEFINE
72 #else
73 #define tracepoint(...)
74 #endif
75
76 #define dout_context g_ceph_context
77 #define dout_subsys ceph_subsys_rgw
78
79 using namespace librados;
80 using ceph::crypto::MD5;
81 using boost::optional;
82 using boost::none;
83
84 using rgw::ARN;
85 using rgw::IAM::Effect;
86 using rgw::IAM::Policy;
87
88 static string mp_ns = RGW_OBJ_NS_MULTIPART;
89 static string shadow_ns = RGW_OBJ_NS_SHADOW;
90
91 static void forward_req_info(CephContext *cct, req_info& info, const std::string& bucket_name);
92
93 static MultipartMetaFilter mp_filter;
94
95 // this probably should belong in the rgw_iam_policy_keywords, I'll get it to it
96 // at some point
97 static constexpr auto S3_EXISTING_OBJTAG = "s3:ExistingObjectTag";
98
99 int RGWGetObj::parse_range(void)
100 {
101 int r = -ERANGE;
102 string rs(range_str);
103 string ofs_str;
104 string end_str;
105
106 ignore_invalid_range = s->cct->_conf->rgw_ignore_get_invalid_range;
107 partial_content = false;
108
109 size_t pos = rs.find("bytes=");
110 if (pos == string::npos) {
111 pos = 0;
112 while (isspace(rs[pos]))
113 pos++;
114 int end = pos;
115 while (isalpha(rs[end]))
116 end++;
117 if (strncasecmp(rs.c_str(), "bytes", end - pos) != 0)
118 return 0;
119 while (isspace(rs[end]))
120 end++;
121 if (rs[end] != '=')
122 return 0;
123 rs = rs.substr(end + 1);
124 } else {
125 rs = rs.substr(pos + 6); /* size of("bytes=") */
126 }
127 pos = rs.find('-');
128 if (pos == string::npos)
129 goto done;
130
131 partial_content = true;
132
133 ofs_str = rs.substr(0, pos);
134 end_str = rs.substr(pos + 1);
135 if (end_str.length()) {
136 end = atoll(end_str.c_str());
137 if (end < 0)
138 goto done;
139 }
140
141 if (ofs_str.length()) {
142 ofs = atoll(ofs_str.c_str());
143 } else { // RFC2616 suffix-byte-range-spec
144 ofs = -end;
145 end = -1;
146 }
147
148 if (end >= 0 && end < ofs)
149 goto done;
150
151 range_parsed = true;
152 return 0;
153
154 done:
155 if (ignore_invalid_range) {
156 partial_content = false;
157 ofs = 0;
158 end = -1;
159 range_parsed = false; // allow retry
160 r = 0;
161 }
162
163 return r;
164 }
165
166 static int decode_policy(const DoutPrefixProvider *dpp,
167 CephContext *cct,
168 bufferlist& bl,
169 RGWAccessControlPolicy *policy)
170 {
171 auto iter = bl.cbegin();
172 try {
173 policy->decode(iter);
174 } catch (buffer::error& err) {
175 ldpp_dout(dpp, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl;
176 return -EIO;
177 }
178 if (cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
179 ldpp_dout(dpp, 15) << __func__ << " Read AccessControlPolicy";
180 RGWAccessControlPolicy_S3 *s3policy = static_cast<RGWAccessControlPolicy_S3 *>(policy);
181 s3policy->to_xml(*_dout);
182 *_dout << dendl;
183 }
184 return 0;
185 }
186
187
188 static int get_user_policy_from_attr(const DoutPrefixProvider *dpp,
189 CephContext * const cct,
190 rgw::sal::RGWRadosStore * const store,
191 map<string, bufferlist>& attrs,
192 RGWAccessControlPolicy& policy /* out */)
193 {
194 auto aiter = attrs.find(RGW_ATTR_ACL);
195 if (aiter != attrs.end()) {
196 int ret = decode_policy(dpp, cct, aiter->second, &policy);
197 if (ret < 0) {
198 return ret;
199 }
200 } else {
201 return -ENOENT;
202 }
203
204 return 0;
205 }
206
207 /**
208 * Get the AccessControlPolicy for an object off of disk.
209 * policy: must point to a valid RGWACL, and will be filled upon return.
210 * bucket: name of the bucket containing the object.
211 * object: name of the object to get the ACL for.
212 * Returns: 0 on success, -ERR# otherwise.
213 */
214 int rgw_op_get_bucket_policy_from_attr(const DoutPrefixProvider *dpp,
215 CephContext *cct,
216 rgw::sal::RGWStore *store,
217 RGWBucketInfo& bucket_info,
218 map<string, bufferlist>& bucket_attrs,
219 RGWAccessControlPolicy *policy,
220 optional_yield y)
221 {
222 map<string, bufferlist>::iterator aiter = bucket_attrs.find(RGW_ATTR_ACL);
223
224 if (aiter != bucket_attrs.end()) {
225 int ret = decode_policy(dpp, cct, aiter->second, policy);
226 if (ret < 0)
227 return ret;
228 } else {
229 ldpp_dout(dpp, 0) << "WARNING: couldn't find acl header for bucket, generating default" << dendl;
230 std::unique_ptr<rgw::sal::RGWUser> user = store->get_user(bucket_info.owner);
231 /* object exists, but policy is broken */
232 int r = user->load_by_id(dpp, y);
233 if (r < 0)
234 return r;
235
236 policy->create_default(bucket_info.owner, user->get_display_name());
237 }
238 return 0;
239 }
240
241 static int get_obj_policy_from_attr(const DoutPrefixProvider *dpp,
242 CephContext *cct,
243 rgw::sal::RGWStore *store,
244 RGWObjectCtx& obj_ctx,
245 RGWBucketInfo& bucket_info,
246 map<string, bufferlist>& bucket_attrs,
247 RGWAccessControlPolicy *policy,
248 string *storage_class,
249 rgw::sal::RGWObject* obj,
250 optional_yield y)
251 {
252 bufferlist bl;
253 int ret = 0;
254
255 std::unique_ptr<rgw::sal::RGWObject::ReadOp> rop = obj->get_read_op(&obj_ctx);
256
257 ret = rop->get_attr(dpp, RGW_ATTR_ACL, bl, y);
258 if (ret >= 0) {
259 ret = decode_policy(dpp, cct, bl, policy);
260 if (ret < 0)
261 return ret;
262 } else if (ret == -ENODATA) {
263 /* object exists, but policy is broken */
264 ldpp_dout(dpp, 0) << "WARNING: couldn't find acl header for object, generating default" << dendl;
265 std::unique_ptr<rgw::sal::RGWUser> user = store->get_user(bucket_info.owner);
266 ret = user->load_by_id(dpp, y);
267 if (ret < 0)
268 return ret;
269
270 policy->create_default(bucket_info.owner, user->get_display_name());
271 }
272
273 if (storage_class) {
274 bufferlist scbl;
275 int r = rop->get_attr(dpp, RGW_ATTR_STORAGE_CLASS, scbl, y);
276 if (r >= 0) {
277 *storage_class = scbl.to_str();
278 } else {
279 storage_class->clear();
280 }
281 }
282
283 return ret;
284 }
285
286
287 static boost::optional<Policy> get_iam_policy_from_attr(CephContext* cct,
288 map<string, bufferlist>& attrs,
289 const string& tenant) {
290 auto i = attrs.find(RGW_ATTR_IAM_POLICY);
291 if (i != attrs.end()) {
292 return Policy(cct, tenant, i->second);
293 } else {
294 return none;
295 }
296 }
297
298 static boost::optional<PublicAccessBlockConfiguration>
299 get_public_access_conf_from_attr(const map<string, bufferlist>& attrs)
300 {
301 if (auto aiter = attrs.find(RGW_ATTR_PUBLIC_ACCESS);
302 aiter != attrs.end()) {
303 bufferlist::const_iterator iter{&aiter->second};
304 PublicAccessBlockConfiguration access_conf;
305 try {
306 access_conf.decode(iter);
307 } catch (const buffer::error& e) {
308 return boost::none;
309 }
310 return access_conf;
311 }
312 return boost::none;
313 }
314
315 vector<Policy> get_iam_user_policy_from_attr(CephContext* cct,
316 rgw::sal::RGWRadosStore* store,
317 map<string, bufferlist>& attrs,
318 const string& tenant) {
319 vector<Policy> policies;
320 if (auto it = attrs.find(RGW_ATTR_USER_POLICY); it != attrs.end()) {
321 bufferlist out_bl = attrs[RGW_ATTR_USER_POLICY];
322 map<string, string> policy_map;
323 decode(policy_map, out_bl);
324 for (auto& it : policy_map) {
325 bufferlist bl = bufferlist::static_from_string(it.second);
326 Policy p(cct, tenant, bl);
327 policies.push_back(std::move(p));
328 }
329 }
330 return policies;
331 }
332
333 static int get_obj_head(const DoutPrefixProvider *dpp,
334 struct req_state *s,
335 rgw::sal::RGWObject* obj,
336 bufferlist *pbl)
337 {
338 std::unique_ptr<rgw::sal::RGWObject::ReadOp> read_op = obj->get_read_op(s->obj_ctx);
339 obj->set_prefetch_data(s->obj_ctx);
340
341 int ret = read_op->prepare(s->yield, dpp);
342 if (ret < 0) {
343 return ret;
344 }
345
346 if (!pbl) {
347 return 0;
348 }
349
350 ret = read_op->read(0, s->cct->_conf->rgw_max_chunk_size, *pbl, s->yield, dpp);
351
352 return 0;
353 }
354
355 struct multipart_upload_info
356 {
357 rgw_placement_rule dest_placement;
358
359 void encode(bufferlist& bl) const {
360 ENCODE_START(1, 1, bl);
361 encode(dest_placement, bl);
362 ENCODE_FINISH(bl);
363 }
364
365 void decode(bufferlist::const_iterator& bl) {
366 DECODE_START(1, bl);
367 decode(dest_placement, bl);
368 DECODE_FINISH(bl);
369 }
370 };
371 WRITE_CLASS_ENCODER(multipart_upload_info)
372
373 static int get_multipart_info(const DoutPrefixProvider *dpp, struct req_state *s,
374 rgw::sal::RGWObject* obj,
375 multipart_upload_info *upload_info)
376 {
377 bufferlist header;
378
379 bufferlist headbl;
380 bufferlist *pheadbl = (upload_info ? &headbl : nullptr);
381
382 int op_ret = get_obj_head(dpp, s, obj, pheadbl);
383 if (op_ret < 0) {
384 if (op_ret == -ENOENT) {
385 return -ERR_NO_SUCH_UPLOAD;
386 }
387 return op_ret;
388 }
389
390 if (upload_info && headbl.length() > 0) {
391 auto hiter = headbl.cbegin();
392 try {
393 decode(*upload_info, hiter);
394 } catch (buffer::error& err) {
395 ldpp_dout(s, 0) << "ERROR: failed to decode multipart upload info" << dendl;
396 return -EIO;
397 }
398 }
399
400 return 0;
401 }
402
403 static int get_multipart_info(const DoutPrefixProvider *dpp, struct req_state *s,
404 const string& meta_oid,
405 multipart_upload_info *upload_info)
406 {
407 map<string, bufferlist>::iterator iter;
408 bufferlist header;
409
410 std::unique_ptr<rgw::sal::RGWObject> meta_obj;
411 meta_obj = s->bucket->get_object(rgw_obj_key(meta_oid, string(), mp_ns));
412 meta_obj->set_in_extra_data(true);
413
414 return get_multipart_info(dpp, s, meta_obj.get(), upload_info);
415 }
416
417 static int read_bucket_policy(const DoutPrefixProvider *dpp,
418 rgw::sal::RGWStore *store,
419 struct req_state *s,
420 RGWBucketInfo& bucket_info,
421 map<string, bufferlist>& bucket_attrs,
422 RGWAccessControlPolicy *policy,
423 rgw_bucket& bucket,
424 optional_yield y)
425 {
426 if (!s->system_request && bucket_info.flags & BUCKET_SUSPENDED) {
427 ldpp_dout(dpp, 0) << "NOTICE: bucket " << bucket_info.bucket.name
428 << " is suspended" << dendl;
429 return -ERR_USER_SUSPENDED;
430 }
431
432 if (bucket.name.empty()) {
433 return 0;
434 }
435
436 int ret = rgw_op_get_bucket_policy_from_attr(dpp, s->cct, store, bucket_info, bucket_attrs, policy, y);
437 if (ret == -ENOENT) {
438 ret = -ERR_NO_SUCH_BUCKET;
439 }
440
441 return ret;
442 }
443
444 static int read_obj_policy(const DoutPrefixProvider *dpp,
445 rgw::sal::RGWStore *store,
446 struct req_state *s,
447 RGWBucketInfo& bucket_info,
448 map<string, bufferlist>& bucket_attrs,
449 RGWAccessControlPolicy* acl,
450 string *storage_class,
451 boost::optional<Policy>& policy,
452 rgw::sal::RGWBucket* bucket,
453 rgw::sal::RGWObject* object,
454 optional_yield y,
455 bool copy_src=false)
456 {
457 string upload_id;
458 upload_id = s->info.args.get("uploadId");
459 std::unique_ptr<rgw::sal::RGWObject> mpobj;
460 rgw_obj obj;
461
462 if (!s->system_request && bucket_info.flags & BUCKET_SUSPENDED) {
463 ldpp_dout(dpp, 0) << "NOTICE: bucket " << bucket_info.bucket.name
464 << " is suspended" << dendl;
465 return -ERR_USER_SUSPENDED;
466 }
467
468 // when getting policy info for copy-source obj, upload_id makes no sense.
469 // 'copy_src' is used to make this function backward compatible.
470 if (!upload_id.empty() && !copy_src) {
471 /* multipart upload */
472 RGWMPObj mp(object->get_name(), upload_id);
473 string oid = mp.get_meta();
474 mpobj = bucket->get_object(rgw_obj_key(oid, string(), mp_ns));
475 mpobj->set_in_extra_data(true);
476 object = mpobj.get();
477 }
478 policy = get_iam_policy_from_attr(s->cct, bucket_attrs, bucket->get_tenant());
479
480 RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx);
481 int ret = get_obj_policy_from_attr(dpp, s->cct, store, *obj_ctx,
482 bucket_info, bucket_attrs, acl, storage_class, object, s->yield);
483 if (ret == -ENOENT) {
484 /* object does not exist checking the bucket's ACL to make sure
485 that we send a proper error code */
486 RGWAccessControlPolicy bucket_policy(s->cct);
487 ret = rgw_op_get_bucket_policy_from_attr(dpp, s->cct, store, bucket_info, bucket_attrs, &bucket_policy, y);
488 if (ret < 0) {
489 return ret;
490 }
491 const rgw_user& bucket_owner = bucket_policy.get_owner().get_id();
492 if (bucket_owner.compare(s->user->get_id()) != 0 &&
493 ! s->auth.identity->is_admin_of(bucket_owner)) {
494 auto r = eval_identity_or_session_policies(s->iam_user_policies, s->env,
495 *s->auth.identity, rgw::IAM::s3ListBucket,
496 ARN(bucket->get_key()));
497 if (r == Effect::Allow)
498 return -ENOENT;
499 if (r == Effect::Deny)
500 return -EACCES;
501 if (policy) {
502 r = policy->eval(s->env, *s->auth.identity, rgw::IAM::s3ListBucket, ARN(bucket->get_key()));
503 if (r == Effect::Allow)
504 return -ENOENT;
505 if (r == Effect::Deny)
506 return -EACCES;
507 }
508 if (! s->session_policies.empty()) {
509 r = eval_identity_or_session_policies(s->session_policies, s->env,
510 *s->auth.identity, rgw::IAM::s3ListBucket,
511 ARN(bucket->get_key()));
512 if (r == Effect::Allow)
513 return -ENOENT;
514 if (r == Effect::Deny)
515 return -EACCES;
516 }
517 if (! bucket_policy.verify_permission(s, *s->auth.identity, s->perm_mask, RGW_PERM_READ))
518 ret = -EACCES;
519 else
520 ret = -ENOENT;
521 } else {
522 ret = -ENOENT;
523 }
524 }
525
526 return ret;
527 }
528
529 /**
530 * Get the AccessControlPolicy for an user, bucket or object off of disk.
531 * s: The req_state to draw information from.
532 * only_bucket: If true, reads the user and bucket ACLs rather than the object ACL.
533 * Returns: 0 on success, -ERR# otherwise.
534 */
535 int rgw_build_bucket_policies(const DoutPrefixProvider *dpp, rgw::sal::RGWRadosStore* store, struct req_state* s, optional_yield y)
536 {
537 int ret = 0;
538 auto obj_ctx = store->svc()->sysobj->init_obj_ctx();
539
540 string bi = s->info.args.get(RGW_SYS_PARAM_PREFIX "bucket-instance");
541 if (!bi.empty()) {
542 // note: overwrites s->bucket_name, may include a tenant/
543 ret = rgw_bucket_parse_bucket_instance(bi, &s->bucket_name, &s->bucket_instance_id, &s->bucket_instance_shard_id);
544 if (ret < 0) {
545 return ret;
546 }
547 }
548
549 if(s->dialect.compare("s3") == 0) {
550 s->bucket_acl = std::make_unique<RGWAccessControlPolicy_S3>(s->cct);
551 } else if(s->dialect.compare("swift") == 0) {
552 /* We aren't allocating the account policy for those operations using
553 * the Swift's infrastructure that don't really need req_state::user.
554 * Typical example here is the implementation of /info. */
555 if (!s->user->get_id().empty()) {
556 s->user_acl = std::make_unique<RGWAccessControlPolicy_SWIFTAcct>(s->cct);
557 }
558 s->bucket_acl = std::make_unique<RGWAccessControlPolicy_SWIFT>(s->cct);
559 } else {
560 s->bucket_acl = std::make_unique<RGWAccessControlPolicy>(s->cct);
561 }
562
563 /* check if copy source is within the current domain */
564 if (!s->src_bucket_name.empty()) {
565 std::unique_ptr<rgw::sal::RGWBucket> src_bucket;
566 ret = store->get_bucket(dpp, nullptr, s->src_tenant_name, s->src_bucket_name, &src_bucket, y);
567 if (ret == 0) {
568 ret = src_bucket->load_by_name(dpp, s->src_tenant_name, s->src_bucket_name,
569 s->bucket_instance_id, &obj_ctx, s->yield);
570 }
571 if (ret == 0) {
572 string& zonegroup = src_bucket->get_info().zonegroup;
573 s->local_source = store->get_zonegroup().equals(zonegroup);
574 }
575 }
576
577 struct {
578 rgw_user uid;
579 std::string display_name;
580 } acct_acl_user = {
581 s->user->get_id(),
582 s->user->get_display_name(),
583 };
584
585 if (!s->bucket_name.empty()) {
586 s->bucket_exists = true;
587
588 ret = store->get_bucket(dpp, s->user.get(), rgw_bucket(rgw_bucket_key(s->bucket_tenant, s->bucket_name, s->bucket_instance_id)), &s->bucket, y);
589 if (ret < 0) {
590 if (ret != -ENOENT) {
591 string bucket_log;
592 bucket_log = rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name);
593 ldpp_dout(dpp, 0) << "NOTICE: couldn't get bucket from bucket_name (name="
594 << bucket_log << ")" << dendl;
595 return ret;
596 }
597 s->bucket_exists = false;
598 return -ERR_NO_SUCH_BUCKET;
599 }
600 if (!rgw::sal::RGWObject::empty(s->object.get())) {
601 s->object->set_bucket(s->bucket.get());
602 }
603
604 s->bucket_mtime = s->bucket->get_modification_time();
605 s->bucket_attrs = s->bucket->get_attrs();
606 ret = read_bucket_policy(dpp, store, s, s->bucket->get_info(),
607 s->bucket->get_attrs(),
608 s->bucket_acl.get(), s->bucket->get_key(), y);
609 acct_acl_user = {
610 s->bucket->get_info().owner,
611 s->bucket_acl->get_owner().get_display_name(),
612 };
613
614 s->bucket_owner = s->bucket_acl->get_owner();
615
616 RGWZoneGroup zonegroup;
617 int r = store->get_zonegroup(s->bucket->get_info().zonegroup, zonegroup);
618 if (!r) {
619 if (!zonegroup.endpoints.empty()) {
620 s->zonegroup_endpoint = zonegroup.endpoints.front();
621 } else {
622 // use zonegroup's master zone endpoints
623 auto z = zonegroup.zones.find(zonegroup.master_zone);
624 if (z != zonegroup.zones.end() && !z->second.endpoints.empty()) {
625 s->zonegroup_endpoint = z->second.endpoints.front();
626 }
627 }
628 s->zonegroup_name = zonegroup.get_name();
629 }
630 if (r < 0 && ret == 0) {
631 ret = r;
632 }
633
634 if (!store->get_zonegroup().equals(s->bucket->get_info().zonegroup)) {
635 ldpp_dout(dpp, 0) << "NOTICE: request for data in a different zonegroup ("
636 << s->bucket->get_info().zonegroup << " != "
637 << store->get_zonegroup().get_id() << ")" << dendl;
638 /* we now need to make sure that the operation actually requires copy source, that is
639 * it's a copy operation
640 */
641 if (store->get_zonegroup().is_master_zonegroup() && s->system_request) {
642 /*If this is the master, don't redirect*/
643 } else if (s->op_type == RGW_OP_GET_BUCKET_LOCATION ) {
644 /* If op is get bucket location, don't redirect */
645 } else if (!s->local_source ||
646 (s->op != OP_PUT && s->op != OP_COPY) ||
647 rgw::sal::RGWObject::empty(s->object.get())) {
648 return -ERR_PERMANENT_REDIRECT;
649 }
650 }
651
652 /* init dest placement */
653 s->dest_placement.storage_class = s->info.storage_class;
654 s->dest_placement.inherit_from(s->bucket->get_placement_rule());
655
656 if (!store->svc()->zone->get_zone_params().valid_placement(s->dest_placement)) {
657 ldpp_dout(dpp, 0) << "NOTICE: invalid dest placement: " << s->dest_placement.to_str() << dendl;
658 return -EINVAL;
659 }
660
661 s->bucket_access_conf = get_public_access_conf_from_attr(s->bucket->get_attrs());
662 }
663
664 /* handle user ACL only for those APIs which support it */
665 if (s->user_acl) {
666 map<string, bufferlist> uattrs;
667 ret = store->ctl()->user->get_attrs_by_uid(dpp, acct_acl_user.uid, &uattrs, s->yield);
668 if (!ret) {
669 ret = get_user_policy_from_attr(dpp, s->cct, store, uattrs, *s->user_acl);
670 }
671 if (-ENOENT == ret) {
672 /* In already existing clusters users won't have ACL. In such case
673 * assuming that only account owner has the rights seems to be
674 * reasonable. That allows to have only one verification logic.
675 * NOTE: there is small compatibility kludge for global, empty tenant:
676 * 1. if we try to reach an existing bucket, its owner is considered
677 * as account owner.
678 * 2. otherwise account owner is identity stored in s->user->user_id. */
679 s->user_acl->create_default(acct_acl_user.uid,
680 acct_acl_user.display_name);
681 ret = 0;
682 } else if (ret < 0) {
683 ldpp_dout(dpp, 0) << "NOTICE: couldn't get user attrs for handling ACL "
684 "(user_id=" << s->user->get_id() << ", ret=" << ret << ")" << dendl;
685 return ret;
686 }
687 }
688 // We don't need user policies in case of STS token returned by AssumeRole,
689 // hence the check for user type
690 if (! s->user->get_id().empty() && s->auth.identity->get_identity_type() != TYPE_ROLE) {
691 try {
692 map<string, bufferlist> uattrs;
693 if (ret = store->ctl()->user->get_attrs_by_uid(dpp, s->user->get_id(), &uattrs, s->yield); ! ret) {
694 auto user_policies = get_iam_user_policy_from_attr(s->cct, store, uattrs, s->user->get_tenant());
695 s->iam_user_policies.insert(s->iam_user_policies.end(),
696 std::make_move_iterator(user_policies.begin()),
697 std::make_move_iterator(user_policies.end()));
698 } else {
699 if (ret == -ENOENT)
700 ret = 0;
701 else ret = -EACCES;
702 }
703 } catch (const std::exception& e) {
704 ldpp_dout(dpp, -1) << "Error reading IAM User Policy: " << e.what() << dendl;
705 ret = -EACCES;
706 }
707 }
708
709 try {
710 s->iam_policy = get_iam_policy_from_attr(s->cct, s->bucket_attrs, s->bucket_tenant);
711 } catch (const std::exception& e) {
712 // Really this is a can't happen condition. We parse the policy
713 // when it's given to us, so perhaps we should abort or otherwise
714 // raise bloody murder.
715 ldpp_dout(dpp, 0) << "Error reading IAM Policy: " << e.what() << dendl;
716 ret = -EACCES;
717 }
718
719 bool success = store->svc()->zone->get_redirect_zone_endpoint(&s->redirect_zone_endpoint);
720 if (success) {
721 ldpp_dout(dpp, 20) << "redirect_zone_endpoint=" << s->redirect_zone_endpoint << dendl;
722 }
723
724 return ret;
725 }
726
727 /**
728 * Get the AccessControlPolicy for a bucket or object off of disk.
729 * s: The req_state to draw information from.
730 * only_bucket: If true, reads the bucket ACL rather than the object ACL.
731 * Returns: 0 on success, -ERR# otherwise.
732 */
733 int rgw_build_object_policies(const DoutPrefixProvider *dpp, rgw::sal::RGWRadosStore *store, struct req_state *s,
734 bool prefetch_data, optional_yield y)
735 {
736 int ret = 0;
737
738 if (!rgw::sal::RGWObject::empty(s->object.get())) {
739 if (!s->bucket_exists) {
740 return -ERR_NO_SUCH_BUCKET;
741 }
742 s->object_acl = std::make_unique<RGWAccessControlPolicy>(s->cct);
743
744 s->object->set_bucket(s->bucket.get());
745
746 s->object->set_atomic(s->obj_ctx);
747 if (prefetch_data) {
748 s->object->set_prefetch_data(s->obj_ctx);
749 }
750 ret = read_obj_policy(dpp, store, s, s->bucket->get_info(), s->bucket_attrs,
751 s->object_acl.get(), nullptr, s->iam_policy, s->bucket.get(),
752 s->object.get(), y);
753 }
754
755 return ret;
756 }
757
758 void rgw_add_to_iam_environment(rgw::IAM::Environment& e, std::string_view key, std::string_view val){
759 // This variant just adds non empty key pairs to IAM env., values can be empty
760 // in certain cases like tagging
761 if (!key.empty())
762 e.emplace(key,val);
763 }
764
765 static int rgw_iam_add_tags_from_bl(struct req_state* s, bufferlist& bl){
766 RGWObjTags& tagset = s->tagset;
767 try {
768 auto bliter = bl.cbegin();
769 tagset.decode(bliter);
770 } catch (buffer::error& err) {
771 ldpp_dout(s, 0) << "ERROR: caught buffer::error, couldn't decode TagSet" << dendl;
772 return -EIO;
773 }
774
775 for (const auto& tag: tagset.get_tags()){
776 rgw_add_to_iam_environment(s->env, "s3:ExistingObjectTag/" + tag.first, tag.second);
777 }
778 return 0;
779 }
780
781 static int rgw_iam_add_existing_objtags(const DoutPrefixProvider *dpp, rgw::sal::RGWRadosStore* store, struct req_state* s, std::uint64_t action) {
782 s->object->set_atomic(s->obj_ctx);
783 int op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, dpp);
784 if (op_ret < 0)
785 return op_ret;
786 rgw::sal::RGWAttrs attrs = s->object->get_attrs();
787 auto tags = attrs.find(RGW_ATTR_TAGS);
788 if (tags != attrs.end()){
789 return rgw_iam_add_tags_from_bl(s, tags->second);
790 }
791 return 0;
792 }
793
794 static void rgw_add_grant_to_iam_environment(rgw::IAM::Environment& e, struct req_state *s){
795
796 using header_pair_t = std::pair <const char*, const char*>;
797 static const std::initializer_list <header_pair_t> acl_header_conditionals {
798 {"HTTP_X_AMZ_GRANT_READ", "s3:x-amz-grant-read"},
799 {"HTTP_X_AMZ_GRANT_WRITE", "s3:x-amz-grant-write"},
800 {"HTTP_X_AMZ_GRANT_READ_ACP", "s3:x-amz-grant-read-acp"},
801 {"HTTP_X_AMZ_GRANT_WRITE_ACP", "s3:x-amz-grant-write-acp"},
802 {"HTTP_X_AMZ_GRANT_FULL_CONTROL", "s3:x-amz-grant-full-control"}
803 };
804
805 if (s->has_acl_header){
806 for (const auto& c: acl_header_conditionals){
807 auto hdr = s->info.env->get(c.first);
808 if(hdr) {
809 e[c.second] = hdr;
810 }
811 }
812 }
813 }
814
815 void rgw_build_iam_environment(rgw::sal::RGWRadosStore* store,
816 struct req_state* s)
817 {
818 const auto& m = s->info.env->get_map();
819 auto t = ceph::real_clock::now();
820 s->env.emplace("aws:CurrentTime", std::to_string(ceph::real_clock::to_time_t(t)));
821 s->env.emplace("aws:EpochTime", ceph::to_iso_8601(t));
822 // TODO: This is fine for now, but once we have STS we'll need to
823 // look and see. Also this won't work with the IdentityApplier
824 // model, since we need to know the actual credential.
825 s->env.emplace("aws:PrincipalType", "User");
826
827 auto i = m.find("HTTP_REFERER");
828 if (i != m.end()) {
829 s->env.emplace("aws:Referer", i->second);
830 }
831
832 if (rgw_transport_is_secure(s->cct, *s->info.env)) {
833 s->env.emplace("aws:SecureTransport", "true");
834 }
835
836 const auto remote_addr_param = s->cct->_conf->rgw_remote_addr_param;
837 if (remote_addr_param.length()) {
838 i = m.find(remote_addr_param);
839 } else {
840 i = m.find("REMOTE_ADDR");
841 }
842 if (i != m.end()) {
843 const string* ip = &(i->second);
844 string temp;
845 if (remote_addr_param == "HTTP_X_FORWARDED_FOR") {
846 const auto comma = ip->find(',');
847 if (comma != string::npos) {
848 temp.assign(*ip, 0, comma);
849 ip = &temp;
850 }
851 }
852 s->env.emplace("aws:SourceIp", *ip);
853 }
854
855 i = m.find("HTTP_USER_AGENT"); {
856 if (i != m.end())
857 s->env.emplace("aws:UserAgent", i->second);
858 }
859
860 if (s->user) {
861 // What to do about aws::userid? One can have multiple access
862 // keys so that isn't really suitable. Do we have a durable
863 // identifier that can persist through name changes?
864 s->env.emplace("aws:username", s->user->get_id().id);
865 }
866
867 i = m.find("HTTP_X_AMZ_SECURITY_TOKEN");
868 if (i != m.end()) {
869 s->env.emplace("sts:authentication", "true");
870 } else {
871 s->env.emplace("sts:authentication", "false");
872 }
873 }
874
875 void rgw_bucket_object_pre_exec(struct req_state *s)
876 {
877 if (s->expect_cont)
878 dump_continue(s);
879
880 dump_bucket_from_state(s);
881 }
882
883 // So! Now and then when we try to update bucket information, the
884 // bucket has changed during the course of the operation. (Or we have
885 // a cache consistency problem that Watch/Notify isn't ruling out
886 // completely.)
887 //
888 // When this happens, we need to update the bucket info and try
889 // again. We have, however, to try the right *part* again. We can't
890 // simply re-send, since that will obliterate the previous update.
891 //
892 // Thus, callers of this function should include everything that
893 // merges information to be changed into the bucket information as
894 // well as the call to set it.
895 //
896 // The called function must return an integer, negative on error. In
897 // general, they should just return op_ret.
898 namespace {
899 template<typename F>
900 int retry_raced_bucket_write(const DoutPrefixProvider *dpp, rgw::sal::RGWBucket* b, const F& f) {
901 auto r = f();
902 for (auto i = 0u; i < 15u && r == -ECANCELED; ++i) {
903 r = b->try_refresh_info(dpp, nullptr);
904 if (r >= 0) {
905 r = f();
906 }
907 }
908 return r;
909 }
910 }
911
912
913 int RGWGetObj::verify_permission(optional_yield y)
914 {
915 s->object->set_atomic(s->obj_ctx);
916
917 if (prefetch_data()) {
918 s->object->set_prefetch_data(s->obj_ctx);
919 }
920
921 if (torrent.get_flag()) {
922 if (s->object->get_instance().empty()) {
923 action = rgw::IAM::s3GetObjectTorrent;
924 } else {
925 action = rgw::IAM::s3GetObjectVersionTorrent;
926 }
927 } else {
928 if (s->object->get_instance().empty()) {
929 action = rgw::IAM::s3GetObject;
930 } else {
931 action = rgw::IAM::s3GetObjectVersion;
932 }
933 if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG))
934 rgw_iam_add_existing_objtags(this, store, s, action);
935 if (! s->iam_user_policies.empty()) {
936 for (auto& user_policy : s->iam_user_policies) {
937 if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG))
938 rgw_iam_add_existing_objtags(this, store, s, action);
939 }
940 }
941 }
942
943 if (!verify_object_permission(this, s, action)) {
944 return -EACCES;
945 }
946
947 if (s->bucket->get_info().obj_lock_enabled()) {
948 get_retention = verify_object_permission(this, s, rgw::IAM::s3GetObjectRetention);
949 get_legal_hold = verify_object_permission(this, s, rgw::IAM::s3GetObjectLegalHold);
950 }
951
952 return 0;
953 }
954
955 int RGWOp::verify_op_mask()
956 {
957 uint32_t required_mask = op_mask();
958
959 ldpp_dout(this, 20) << "required_mask= " << required_mask
960 << " user.op_mask=" << s->user->get_info().op_mask << dendl;
961
962 if ((s->user->get_info().op_mask & required_mask) != required_mask) {
963 return -EPERM;
964 }
965
966 if (!s->system_request && (required_mask & RGW_OP_TYPE_MODIFY) && !store->svc()->zone->zone_is_writeable()) {
967 ldpp_dout(this, 5) << "NOTICE: modify request to a read-only zone by a "
968 "non-system user, permission denied" << dendl;
969 return -EPERM;
970 }
971
972 return 0;
973 }
974
975 int RGWGetObjTags::verify_permission(optional_yield y)
976 {
977 auto iam_action = s->object->get_instance().empty()?
978 rgw::IAM::s3GetObjectTagging:
979 rgw::IAM::s3GetObjectVersionTagging;
980 // TODO since we are parsing the bl now anyway, we probably change
981 // the send_response function to accept RGWObjTag instead of a bl
982 if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){
983 rgw_iam_add_existing_objtags(this, store, s, iam_action);
984 }
985 if (! s->iam_user_policies.empty()) {
986 for (auto& user_policy : s->iam_user_policies) {
987 if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) {
988 rgw_iam_add_existing_objtags(this, store, s, iam_action);
989 }
990 }
991 }
992 if (!verify_object_permission(this, s,iam_action))
993 return -EACCES;
994
995 return 0;
996 }
997
998 void RGWGetObjTags::pre_exec()
999 {
1000 rgw_bucket_object_pre_exec(s);
1001 }
1002
1003 void RGWGetObjTags::execute(optional_yield y)
1004 {
1005 rgw::sal::RGWAttrs attrs;
1006
1007 s->object->set_atomic(s->obj_ctx);
1008
1009 op_ret = s->object->get_obj_attrs(s->obj_ctx, y, this);
1010 if (op_ret < 0) {
1011 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << s->object
1012 << " ret=" << op_ret << dendl;
1013 return;
1014 }
1015
1016 attrs = s->object->get_attrs();
1017 auto tags = attrs.find(RGW_ATTR_TAGS);
1018 if(tags != attrs.end()){
1019 has_tags = true;
1020 tags_bl.append(tags->second);
1021 }
1022 send_response_data(tags_bl);
1023 }
1024
1025 int RGWPutObjTags::verify_permission(optional_yield y)
1026 {
1027 auto iam_action = s->object->get_instance().empty() ?
1028 rgw::IAM::s3PutObjectTagging:
1029 rgw::IAM::s3PutObjectVersionTagging;
1030
1031 if(s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){
1032 rgw_iam_add_existing_objtags(this, store, s, iam_action);
1033 }
1034 if (! s->iam_user_policies.empty()) {
1035 for (auto& user_policy : s->iam_user_policies) {
1036 if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) {
1037 rgw_iam_add_existing_objtags(this, store, s, iam_action);
1038 }
1039 }
1040 }
1041 if (!verify_object_permission(this, s,iam_action))
1042 return -EACCES;
1043 return 0;
1044 }
1045
1046 void RGWPutObjTags::execute(optional_yield y)
1047 {
1048 op_ret = get_params(y);
1049 if (op_ret < 0)
1050 return;
1051
1052 if (rgw::sal::RGWObject::empty(s->object.get())){
1053 op_ret= -EINVAL; // we only support tagging on existing objects
1054 return;
1055 }
1056
1057 s->object->set_atomic(s->obj_ctx);
1058 op_ret = s->object->modify_obj_attrs(s->obj_ctx, RGW_ATTR_TAGS, tags_bl, y, this);
1059 if (op_ret == -ECANCELED){
1060 op_ret = -ERR_TAG_CONFLICT;
1061 }
1062 }
1063
1064 void RGWDeleteObjTags::pre_exec()
1065 {
1066 rgw_bucket_object_pre_exec(s);
1067 }
1068
1069
1070 int RGWDeleteObjTags::verify_permission(optional_yield y)
1071 {
1072 if (!rgw::sal::RGWObject::empty(s->object.get())) {
1073 auto iam_action = s->object->get_instance().empty() ?
1074 rgw::IAM::s3DeleteObjectTagging:
1075 rgw::IAM::s3DeleteObjectVersionTagging;
1076
1077 if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){
1078 rgw_iam_add_existing_objtags(this, store, s, iam_action);
1079 }
1080 if (! s->iam_user_policies.empty()) {
1081 for (auto& user_policy : s->iam_user_policies) {
1082 if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) {
1083 rgw_iam_add_existing_objtags(this, store, s, iam_action);
1084 }
1085 }
1086 }
1087 if (!verify_object_permission(this, s, iam_action))
1088 return -EACCES;
1089 }
1090 return 0;
1091 }
1092
1093 void RGWDeleteObjTags::execute(optional_yield y)
1094 {
1095 if (rgw::sal::RGWObject::empty(s->object.get()))
1096 return;
1097
1098 op_ret = s->object->delete_obj_attrs(this, s->obj_ctx, RGW_ATTR_TAGS, y);
1099 }
1100
1101 int RGWGetBucketTags::verify_permission(optional_yield y)
1102 {
1103
1104 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketTagging)) {
1105 return -EACCES;
1106 }
1107
1108 return 0;
1109 }
1110
1111 void RGWGetBucketTags::pre_exec()
1112 {
1113 rgw_bucket_object_pre_exec(s);
1114 }
1115
1116 void RGWGetBucketTags::execute(optional_yield y)
1117 {
1118 auto iter = s->bucket_attrs.find(RGW_ATTR_TAGS);
1119 if (iter != s->bucket_attrs.end()) {
1120 has_tags = true;
1121 tags_bl.append(iter->second);
1122 } else {
1123 op_ret = -ERR_NO_SUCH_TAG_SET;
1124 }
1125 send_response_data(tags_bl);
1126 }
1127
1128 int RGWPutBucketTags::verify_permission(optional_yield y) {
1129 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketTagging);
1130 }
1131
1132 void RGWPutBucketTags::execute(optional_yield y)
1133 {
1134
1135 op_ret = get_params(this, y);
1136 if (op_ret < 0)
1137 return;
1138
1139 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
1140 if (op_ret < 0) {
1141 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
1142 }
1143
1144 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this, y] {
1145 rgw::sal::RGWAttrs attrs = s->bucket->get_attrs();
1146 attrs[RGW_ATTR_TAGS] = tags_bl;
1147 return s->bucket->set_instance_attrs(this, attrs, y);
1148 });
1149
1150 }
1151
1152 void RGWDeleteBucketTags::pre_exec()
1153 {
1154 rgw_bucket_object_pre_exec(s);
1155 }
1156
1157 int RGWDeleteBucketTags::verify_permission(optional_yield y)
1158 {
1159 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketTagging);
1160 }
1161
1162 void RGWDeleteBucketTags::execute(optional_yield y)
1163 {
1164 bufferlist in_data;
1165 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
1166 if (op_ret < 0) {
1167 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
1168 return;
1169 }
1170
1171 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this, y] {
1172 rgw::sal::RGWAttrs attrs = s->bucket->get_attrs();
1173 attrs.erase(RGW_ATTR_TAGS);
1174 op_ret = s->bucket->set_instance_attrs(this, attrs, y);
1175 if (op_ret < 0) {
1176 ldpp_dout(this, 0) << "RGWDeleteBucketTags() failed to remove RGW_ATTR_TAGS on bucket="
1177 << s->bucket->get_name()
1178 << " returned err= " << op_ret << dendl;
1179 }
1180 return op_ret;
1181 });
1182 }
1183
1184 int RGWGetBucketReplication::verify_permission(optional_yield y)
1185 {
1186 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetReplicationConfiguration)) {
1187 return -EACCES;
1188 }
1189
1190 return 0;
1191 }
1192
1193 void RGWGetBucketReplication::pre_exec()
1194 {
1195 rgw_bucket_object_pre_exec(s);
1196 }
1197
1198 void RGWGetBucketReplication::execute(optional_yield y)
1199 {
1200 send_response_data();
1201 }
1202
1203 int RGWPutBucketReplication::verify_permission(optional_yield y) {
1204 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutReplicationConfiguration);
1205 }
1206
1207 void RGWPutBucketReplication::execute(optional_yield y) {
1208
1209 op_ret = get_params(y);
1210 if (op_ret < 0)
1211 return;
1212
1213 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
1214 if (op_ret < 0) {
1215 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
1216 return;
1217 }
1218
1219 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
1220 auto sync_policy = (s->bucket->get_info().sync_policy ? *s->bucket->get_info().sync_policy : rgw_sync_policy_info());
1221
1222 for (auto& group : sync_policy_groups) {
1223 sync_policy.groups[group.id] = group;
1224 }
1225
1226 s->bucket->get_info().set_sync_policy(std::move(sync_policy));
1227
1228 int ret = s->bucket->put_instance_info(this, false, real_time());
1229 if (ret < 0) {
1230 ldpp_dout(this, 0) << "ERROR: put_bucket_instance_info (bucket=" << s->bucket << ") returned ret=" << ret << dendl;
1231 return ret;
1232 }
1233
1234 return 0;
1235 });
1236 }
1237
1238 void RGWDeleteBucketReplication::pre_exec()
1239 {
1240 rgw_bucket_object_pre_exec(s);
1241 }
1242
1243 int RGWDeleteBucketReplication::verify_permission(optional_yield y)
1244 {
1245 return verify_bucket_owner_or_policy(s, rgw::IAM::s3DeleteReplicationConfiguration);
1246 }
1247
1248 void RGWDeleteBucketReplication::execute(optional_yield y)
1249 {
1250 bufferlist in_data;
1251 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
1252 if (op_ret < 0) {
1253 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
1254 return;
1255 }
1256
1257 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
1258 if (!s->bucket->get_info().sync_policy) {
1259 return 0;
1260 }
1261
1262 rgw_sync_policy_info sync_policy = *s->bucket->get_info().sync_policy;
1263
1264 update_sync_policy(&sync_policy);
1265
1266 s->bucket->get_info().set_sync_policy(std::move(sync_policy));
1267
1268 int ret = s->bucket->put_instance_info(this, false, real_time());
1269 if (ret < 0) {
1270 ldpp_dout(this, 0) << "ERROR: put_bucket_instance_info (bucket=" << s->bucket << ") returned ret=" << ret << dendl;
1271 return ret;
1272 }
1273
1274 return 0;
1275 });
1276 }
1277
1278 int RGWOp::do_aws4_auth_completion()
1279 {
1280 ldpp_dout(this, 5) << "NOTICE: call to do_aws4_auth_completion" << dendl;
1281 if (s->auth.completer) {
1282 if (!s->auth.completer->complete()) {
1283 return -ERR_AMZ_CONTENT_SHA256_MISMATCH;
1284 } else {
1285 ldpp_dout(this, 10) << "v4 auth ok -- do_aws4_auth_completion" << dendl;
1286 }
1287
1288 /* TODO(rzarzynski): yes, we're really called twice on PUTs. Only first
1289 * call passes, so we disable second one. This is old behaviour, sorry!
1290 * Plan for tomorrow: seek and destroy. */
1291 s->auth.completer = nullptr;
1292 }
1293
1294 return 0;
1295 }
1296
1297 int RGWOp::init_quota()
1298 {
1299 /* no quota enforcement for system requests */
1300 if (s->system_request)
1301 return 0;
1302
1303 /* init quota related stuff */
1304 if (!(s->user->get_info().op_mask & RGW_OP_TYPE_MODIFY)) {
1305 return 0;
1306 }
1307
1308 /* only interested in object related ops */
1309 if (rgw::sal::RGWBucket::empty(s->bucket.get())
1310 || rgw::sal::RGWObject::empty(s->object.get())) {
1311 return 0;
1312 }
1313
1314 rgw::sal::RGWRadosUser owner_user(store, s->bucket->get_info().owner);
1315 rgw::sal::RGWUser* user;
1316
1317 if (s->user->get_id() == s->bucket_owner.get_id()) {
1318 user = s->user.get();
1319 } else {
1320 int r = owner_user.load_by_id(this, s->yield);
1321 if (r < 0)
1322 return r;
1323 user = &owner_user;
1324 }
1325
1326 if (s->bucket->get_info().quota.enabled) {
1327 bucket_quota = s->bucket->get_info().quota;
1328 } else if (user->get_info().bucket_quota.enabled) {
1329 bucket_quota = user->get_info().bucket_quota;
1330 } else {
1331 bucket_quota = store->svc()->quota->get_bucket_quota();
1332 }
1333
1334 if (user->get_info().user_quota.enabled) {
1335 user_quota = user->get_info().user_quota;
1336 } else {
1337 user_quota = store->svc()->quota->get_user_quota();
1338 }
1339
1340 return 0;
1341 }
1342
1343 static bool validate_cors_rule_method(RGWCORSRule *rule, const char *req_meth) {
1344 uint8_t flags = 0;
1345
1346 if (!req_meth) {
1347 dout(5) << "req_meth is null" << dendl;
1348 return false;
1349 }
1350
1351 if (strcmp(req_meth, "GET") == 0) flags = RGW_CORS_GET;
1352 else if (strcmp(req_meth, "POST") == 0) flags = RGW_CORS_POST;
1353 else if (strcmp(req_meth, "PUT") == 0) flags = RGW_CORS_PUT;
1354 else if (strcmp(req_meth, "DELETE") == 0) flags = RGW_CORS_DELETE;
1355 else if (strcmp(req_meth, "HEAD") == 0) flags = RGW_CORS_HEAD;
1356
1357 if (rule->get_allowed_methods() & flags) {
1358 dout(10) << "Method " << req_meth << " is supported" << dendl;
1359 } else {
1360 dout(5) << "Method " << req_meth << " is not supported" << dendl;
1361 return false;
1362 }
1363
1364 return true;
1365 }
1366
1367 static bool validate_cors_rule_header(RGWCORSRule *rule, const char *req_hdrs) {
1368 if (req_hdrs) {
1369 vector<string> hdrs;
1370 get_str_vec(req_hdrs, hdrs);
1371 for (const auto& hdr : hdrs) {
1372 if (!rule->is_header_allowed(hdr.c_str(), hdr.length())) {
1373 dout(5) << "Header " << hdr << " is not registered in this rule" << dendl;
1374 return false;
1375 }
1376 }
1377 }
1378 return true;
1379 }
1380
1381 int RGWOp::read_bucket_cors()
1382 {
1383 bufferlist bl;
1384
1385 map<string, bufferlist>::iterator aiter = s->bucket_attrs.find(RGW_ATTR_CORS);
1386 if (aiter == s->bucket_attrs.end()) {
1387 ldpp_dout(this, 20) << "no CORS configuration attr found" << dendl;
1388 cors_exist = false;
1389 return 0; /* no CORS configuration found */
1390 }
1391
1392 cors_exist = true;
1393
1394 bl = aiter->second;
1395
1396 auto iter = bl.cbegin();
1397 try {
1398 bucket_cors.decode(iter);
1399 } catch (buffer::error& err) {
1400 ldpp_dout(this, 0) << "ERROR: could not decode CORS, caught buffer::error" << dendl;
1401 return -EIO;
1402 }
1403 if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
1404 RGWCORSConfiguration_S3 *s3cors = static_cast<RGWCORSConfiguration_S3 *>(&bucket_cors);
1405 ldpp_dout(this, 15) << "Read RGWCORSConfiguration";
1406 s3cors->to_xml(*_dout);
1407 *_dout << dendl;
1408 }
1409 return 0;
1410 }
1411
1412 /** CORS 6.2.6.
1413 * If any of the header field-names is not a ASCII case-insensitive match for
1414 * any of the values in list of headers do not set any additional headers and
1415 * terminate this set of steps.
1416 * */
1417 static void get_cors_response_headers(RGWCORSRule *rule, const char *req_hdrs, string& hdrs, string& exp_hdrs, unsigned *max_age) {
1418 if (req_hdrs) {
1419 list<string> hl;
1420 get_str_list(req_hdrs, hl);
1421 for(list<string>::iterator it = hl.begin(); it != hl.end(); ++it) {
1422 if (!rule->is_header_allowed((*it).c_str(), (*it).length())) {
1423 dout(5) << "Header " << (*it) << " is not registered in this rule" << dendl;
1424 } else {
1425 if (hdrs.length() > 0) hdrs.append(",");
1426 hdrs.append((*it));
1427 }
1428 }
1429 }
1430 rule->format_exp_headers(exp_hdrs);
1431 *max_age = rule->get_max_age();
1432 }
1433
1434 /**
1435 * Generate the CORS header response
1436 *
1437 * This is described in the CORS standard, section 6.2.
1438 */
1439 bool RGWOp::generate_cors_headers(string& origin, string& method, string& headers, string& exp_headers, unsigned *max_age)
1440 {
1441 /* CORS 6.2.1. */
1442 const char *orig = s->info.env->get("HTTP_ORIGIN");
1443 if (!orig) {
1444 return false;
1445 }
1446
1447 /* Custom: */
1448 origin = orig;
1449 int temp_op_ret = read_bucket_cors();
1450 if (temp_op_ret < 0) {
1451 op_ret = temp_op_ret;
1452 return false;
1453 }
1454
1455 if (!cors_exist) {
1456 ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl;
1457 return false;
1458 }
1459
1460 /* CORS 6.2.2. */
1461 RGWCORSRule *rule = bucket_cors.host_name_rule(orig);
1462 if (!rule)
1463 return false;
1464
1465 /*
1466 * Set the Allowed-Origin header to a asterisk if this is allowed in the rule
1467 * and no Authorization was send by the client
1468 *
1469 * The origin parameter specifies a URI that may access the resource. The browser must enforce this.
1470 * For requests without credentials, the server may specify "*" as a wildcard,
1471 * thereby allowing any origin to access the resource.
1472 */
1473 const char *authorization = s->info.env->get("HTTP_AUTHORIZATION");
1474 if (!authorization && rule->has_wildcard_origin())
1475 origin = "*";
1476
1477 /* CORS 6.2.3. */
1478 const char *req_meth = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_METHOD");
1479 if (!req_meth) {
1480 req_meth = s->info.method;
1481 }
1482
1483 if (req_meth) {
1484 method = req_meth;
1485 /* CORS 6.2.5. */
1486 if (!validate_cors_rule_method(rule, req_meth)) {
1487 return false;
1488 }
1489 }
1490
1491 /* CORS 6.2.4. */
1492 const char *req_hdrs = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_HEADERS");
1493
1494 /* CORS 6.2.6. */
1495 get_cors_response_headers(rule, req_hdrs, headers, exp_headers, max_age);
1496
1497 return true;
1498 }
1499
1500 int RGWGetObj::read_user_manifest_part(rgw::sal::RGWBucket* bucket,
1501 const rgw_bucket_dir_entry& ent,
1502 RGWAccessControlPolicy * const bucket_acl,
1503 const boost::optional<Policy>& bucket_policy,
1504 const off_t start_ofs,
1505 const off_t end_ofs,
1506 bool swift_slo)
1507 {
1508 ldpp_dout(this, 20) << "user manifest obj=" << ent.key.name
1509 << "[" << ent.key.instance << "]" << dendl;
1510 RGWGetObj_CB cb(this);
1511 RGWGetObj_Filter* filter = &cb;
1512 boost::optional<RGWGetObj_Decompress> decompress;
1513
1514 int64_t cur_ofs = start_ofs;
1515 int64_t cur_end = end_ofs;
1516
1517 std::unique_ptr<rgw::sal::RGWObject> part = bucket->get_object(ent.key);
1518
1519 RGWObjectCtx obj_ctx(store);
1520 RGWAccessControlPolicy obj_policy(s->cct);
1521
1522 ldpp_dout(this, 20) << "reading obj=" << part << " ofs=" << cur_ofs
1523 << " end=" << cur_end << dendl;
1524
1525 part->set_atomic(&obj_ctx);
1526 part->set_prefetch_data(&obj_ctx);
1527
1528 std::unique_ptr<rgw::sal::RGWObject::ReadOp> read_op = part->get_read_op(&obj_ctx);
1529
1530 if (!swift_slo) {
1531 /* SLO etag is optional */
1532 read_op->params.if_match = ent.meta.etag.c_str();
1533 }
1534
1535 op_ret = read_op->prepare(s->yield, this);
1536 if (op_ret < 0)
1537 return op_ret;
1538 op_ret = part->range_to_ofs(ent.meta.accounted_size, cur_ofs, cur_end);
1539 if (op_ret < 0)
1540 return op_ret;
1541 bool need_decompress;
1542 op_ret = rgw_compression_info_from_attrset(part->get_attrs(), need_decompress, cs_info);
1543 if (op_ret < 0) {
1544 ldpp_dout(this, 0) << "ERROR: failed to decode compression info" << dendl;
1545 return -EIO;
1546 }
1547
1548 if (need_decompress)
1549 {
1550 if (cs_info.orig_size != ent.meta.accounted_size) {
1551 // hmm.. something wrong, object not as expected, abort!
1552 ldpp_dout(this, 0) << "ERROR: expected cs_info.orig_size=" << cs_info.orig_size
1553 << ", actual read size=" << ent.meta.size << dendl;
1554 return -EIO;
1555 }
1556 decompress.emplace(s->cct, &cs_info, partial_content, filter);
1557 filter = &*decompress;
1558 }
1559 else
1560 {
1561 if (part->get_obj_size() != ent.meta.size) {
1562 // hmm.. something wrong, object not as expected, abort!
1563 ldpp_dout(this, 0) << "ERROR: expected obj_size=" << part->get_obj_size()
1564 << ", actual read size=" << ent.meta.size << dendl;
1565 return -EIO;
1566 }
1567 }
1568
1569 op_ret = rgw_policy_from_attrset(s, s->cct, part->get_attrs(), &obj_policy);
1570 if (op_ret < 0)
1571 return op_ret;
1572
1573 /* We can use global user_acl because LOs cannot have segments
1574 * stored inside different accounts. */
1575 if (s->system_request) {
1576 ldpp_dout(this, 2) << "overriding permissions due to system operation" << dendl;
1577 } else if (s->auth.identity->is_admin_of(s->user->get_id())) {
1578 ldpp_dout(this, 2) << "overriding permissions due to admin operation" << dendl;
1579 } else if (!verify_object_permission(this, s, part->get_obj(), s->user_acl.get(),
1580 bucket_acl, &obj_policy, bucket_policy,
1581 s->iam_user_policies, s->session_policies, action)) {
1582 return -EPERM;
1583 }
1584 if (ent.meta.size == 0) {
1585 return 0;
1586 }
1587
1588 perfcounter->inc(l_rgw_get_b, cur_end - cur_ofs);
1589 filter->fixup_range(cur_ofs, cur_end);
1590 op_ret = read_op->iterate(this, cur_ofs, cur_end, filter, s->yield);
1591 if (op_ret >= 0)
1592 op_ret = filter->flush();
1593 return op_ret;
1594 }
1595
1596 static int iterate_user_manifest_parts(const DoutPrefixProvider *dpp,
1597 CephContext * const cct,
1598 rgw::sal::RGWStore* const store,
1599 const off_t ofs,
1600 const off_t end,
1601 rgw::sal::RGWBucket* bucket,
1602 const string& obj_prefix,
1603 RGWAccessControlPolicy * const bucket_acl,
1604 const boost::optional<Policy>& bucket_policy,
1605 uint64_t * const ptotal_len,
1606 uint64_t * const pobj_size,
1607 string * const pobj_sum,
1608 int (*cb)(rgw::sal::RGWBucket* bucket,
1609 const rgw_bucket_dir_entry& ent,
1610 RGWAccessControlPolicy * const bucket_acl,
1611 const boost::optional<Policy>& bucket_policy,
1612 off_t start_ofs,
1613 off_t end_ofs,
1614 void *param,
1615 bool swift_slo),
1616 void * const cb_param,
1617 optional_yield y)
1618 {
1619 uint64_t obj_ofs = 0, len_count = 0;
1620 bool found_start = false, found_end = false, handled_end = false;
1621 string delim;
1622
1623 utime_t start_time = ceph_clock_now();
1624
1625 rgw::sal::RGWBucket::ListParams params;
1626 params.prefix = obj_prefix;
1627 params.delim = delim;
1628
1629 rgw::sal::RGWBucket::ListResults results;
1630 MD5 etag_sum;
1631 do {
1632 static constexpr auto MAX_LIST_OBJS = 100u;
1633 int r = bucket->list(dpp, params, MAX_LIST_OBJS, results, y);
1634 if (r < 0) {
1635 return r;
1636 }
1637
1638 for (rgw_bucket_dir_entry& ent : results.objs) {
1639 const uint64_t cur_total_len = obj_ofs;
1640 const uint64_t obj_size = ent.meta.accounted_size;
1641 uint64_t start_ofs = 0, end_ofs = obj_size;
1642
1643 if ((ptotal_len || cb) && !found_start && cur_total_len + obj_size > (uint64_t)ofs) {
1644 start_ofs = ofs - obj_ofs;
1645 found_start = true;
1646 }
1647
1648 obj_ofs += obj_size;
1649 if (pobj_sum) {
1650 etag_sum.Update((const unsigned char *)ent.meta.etag.c_str(),
1651 ent.meta.etag.length());
1652 }
1653
1654 if ((ptotal_len || cb) && !found_end && obj_ofs > (uint64_t)end) {
1655 end_ofs = end - cur_total_len + 1;
1656 found_end = true;
1657 }
1658
1659 perfcounter->tinc(l_rgw_get_lat,
1660 (ceph_clock_now() - start_time));
1661
1662 if (found_start && !handled_end) {
1663 len_count += end_ofs - start_ofs;
1664
1665 if (cb) {
1666 r = cb(bucket, ent, bucket_acl, bucket_policy, start_ofs, end_ofs,
1667 cb_param, false /* swift_slo */);
1668 if (r < 0) {
1669 return r;
1670 }
1671 }
1672 }
1673
1674 handled_end = found_end;
1675 start_time = ceph_clock_now();
1676 }
1677 } while (results.is_truncated);
1678
1679 if (ptotal_len) {
1680 *ptotal_len = len_count;
1681 }
1682 if (pobj_size) {
1683 *pobj_size = obj_ofs;
1684 }
1685 if (pobj_sum) {
1686 complete_etag(etag_sum, pobj_sum);
1687 }
1688
1689 return 0;
1690 }
1691
1692 struct rgw_slo_part {
1693 RGWAccessControlPolicy *bucket_acl = nullptr;
1694 Policy* bucket_policy = nullptr;
1695 rgw::sal::RGWBucket* bucket;
1696 string obj_name;
1697 uint64_t size = 0;
1698 string etag;
1699 };
1700
1701 static int iterate_slo_parts(CephContext *cct,
1702 rgw::sal::RGWStore*store,
1703 off_t ofs,
1704 off_t end,
1705 map<uint64_t, rgw_slo_part>& slo_parts,
1706 int (*cb)(rgw::sal::RGWBucket* bucket,
1707 const rgw_bucket_dir_entry& ent,
1708 RGWAccessControlPolicy *bucket_acl,
1709 const boost::optional<Policy>& bucket_policy,
1710 off_t start_ofs,
1711 off_t end_ofs,
1712 void *param,
1713 bool swift_slo),
1714 void *cb_param)
1715 {
1716 bool found_start = false, found_end = false;
1717
1718 if (slo_parts.empty()) {
1719 return 0;
1720 }
1721
1722 utime_t start_time = ceph_clock_now();
1723
1724 map<uint64_t, rgw_slo_part>::iterator iter = slo_parts.upper_bound(ofs);
1725 if (iter != slo_parts.begin()) {
1726 --iter;
1727 }
1728
1729 uint64_t obj_ofs = iter->first;
1730
1731 for (; iter != slo_parts.end() && !found_end; ++iter) {
1732 rgw_slo_part& part = iter->second;
1733 rgw_bucket_dir_entry ent;
1734
1735 ent.key.name = part.obj_name;
1736 ent.meta.accounted_size = ent.meta.size = part.size;
1737 ent.meta.etag = part.etag;
1738
1739 uint64_t cur_total_len = obj_ofs;
1740 uint64_t start_ofs = 0, end_ofs = ent.meta.size - 1;
1741
1742 if (!found_start && cur_total_len + ent.meta.size > (uint64_t)ofs) {
1743 start_ofs = ofs - obj_ofs;
1744 found_start = true;
1745 }
1746
1747 obj_ofs += ent.meta.size;
1748
1749 if (!found_end && obj_ofs > (uint64_t)end) {
1750 end_ofs = end - cur_total_len;
1751 found_end = true;
1752 }
1753
1754 perfcounter->tinc(l_rgw_get_lat,
1755 (ceph_clock_now() - start_time));
1756
1757 if (found_start) {
1758 if (cb) {
1759 dout(20) << "iterate_slo_parts()"
1760 << " obj=" << part.obj_name
1761 << " start_ofs=" << start_ofs
1762 << " end_ofs=" << end_ofs
1763 << dendl;
1764
1765 // SLO is a Swift thing, and Swift has no knowledge of S3 Policies.
1766 int r = cb(part.bucket, ent, part.bucket_acl,
1767 (part.bucket_policy ?
1768 boost::optional<Policy>(*part.bucket_policy) : none),
1769 start_ofs, end_ofs, cb_param, true /* swift_slo */);
1770 if (r < 0)
1771 return r;
1772 }
1773 }
1774
1775 start_time = ceph_clock_now();
1776 }
1777
1778 return 0;
1779 }
1780
1781 static int get_obj_user_manifest_iterate_cb(rgw::sal::RGWBucket* bucket,
1782 const rgw_bucket_dir_entry& ent,
1783 RGWAccessControlPolicy * const bucket_acl,
1784 const boost::optional<Policy>& bucket_policy,
1785 const off_t start_ofs,
1786 const off_t end_ofs,
1787 void * const param,
1788 bool swift_slo = false)
1789 {
1790 RGWGetObj *op = static_cast<RGWGetObj *>(param);
1791 return op->read_user_manifest_part(
1792 bucket, ent, bucket_acl, bucket_policy, start_ofs, end_ofs, swift_slo);
1793 }
1794
1795 int RGWGetObj::handle_user_manifest(const char *prefix, optional_yield y)
1796 {
1797 const std::string_view prefix_view(prefix);
1798 ldpp_dout(this, 2) << "RGWGetObj::handle_user_manifest() prefix="
1799 << prefix_view << dendl;
1800
1801 const size_t pos = prefix_view.find('/');
1802 if (pos == string::npos) {
1803 return -EINVAL;
1804 }
1805
1806 const std::string bucket_name = url_decode(prefix_view.substr(0, pos));
1807 const std::string obj_prefix = url_decode(prefix_view.substr(pos + 1));
1808
1809 RGWAccessControlPolicy _bucket_acl(s->cct);
1810 RGWAccessControlPolicy *bucket_acl;
1811 boost::optional<Policy> _bucket_policy;
1812 boost::optional<Policy>* bucket_policy;
1813 RGWBucketInfo bucket_info;
1814 std::unique_ptr<rgw::sal::RGWBucket> ubucket;
1815 rgw::sal::RGWBucket *pbucket = NULL;
1816 int r = 0;
1817
1818 if (bucket_name.compare(s->bucket->get_name()) != 0) {
1819 map<string, bufferlist> bucket_attrs;
1820 r = store->get_bucket(this, s->user.get(), s->user->get_tenant(), bucket_name, &ubucket, y);
1821 if (r < 0) {
1822 ldpp_dout(this, 0) << "could not get bucket info for bucket="
1823 << bucket_name << dendl;
1824 return r;
1825 }
1826 bucket_acl = &_bucket_acl;
1827 r = read_bucket_policy(this, store, s, ubucket->get_info(), bucket_attrs, bucket_acl, ubucket->get_key(), y);
1828 if (r < 0) {
1829 ldpp_dout(this, 0) << "failed to read bucket policy" << dendl;
1830 return r;
1831 }
1832 _bucket_policy = get_iam_policy_from_attr(s->cct, bucket_attrs, s->user->get_tenant());
1833 bucket_policy = &_bucket_policy;
1834 pbucket = ubucket.get();
1835 } else {
1836 pbucket = s->bucket.get();
1837 bucket_acl = s->bucket_acl.get();
1838 bucket_policy = &s->iam_policy;
1839 }
1840
1841 /* dry run to find out:
1842 * - total length (of the parts we are going to send to client),
1843 * - overall DLO's content size,
1844 * - md5 sum of overall DLO's content (for etag of Swift API). */
1845 r = iterate_user_manifest_parts(this, s->cct, store, ofs, end,
1846 pbucket, obj_prefix, bucket_acl, *bucket_policy,
1847 nullptr, &s->obj_size, &lo_etag,
1848 nullptr /* cb */, nullptr /* cb arg */, y);
1849 if (r < 0) {
1850 return r;
1851 }
1852 s->object->set_obj_size(s->obj_size);
1853
1854 r = RGWRados::Object::Read::range_to_ofs(s->obj_size, ofs, end);
1855 if (r < 0) {
1856 return r;
1857 }
1858
1859 r = iterate_user_manifest_parts(this, s->cct, store, ofs, end,
1860 pbucket, obj_prefix, bucket_acl, *bucket_policy,
1861 &total_len, nullptr, nullptr,
1862 nullptr, nullptr, y);
1863 if (r < 0) {
1864 return r;
1865 }
1866
1867 if (!get_data) {
1868 bufferlist bl;
1869 send_response_data(bl, 0, 0);
1870 return 0;
1871 }
1872
1873 r = iterate_user_manifest_parts(this, s->cct, store, ofs, end,
1874 pbucket, obj_prefix, bucket_acl, *bucket_policy,
1875 nullptr, nullptr, nullptr,
1876 get_obj_user_manifest_iterate_cb, (void *)this, y);
1877 if (r < 0) {
1878 return r;
1879 }
1880
1881 if (!total_len) {
1882 bufferlist bl;
1883 send_response_data(bl, 0, 0);
1884 }
1885
1886 return r;
1887 }
1888
1889 int RGWGetObj::handle_slo_manifest(bufferlist& bl, optional_yield y)
1890 {
1891 RGWSLOInfo slo_info;
1892 auto bliter = bl.cbegin();
1893 try {
1894 decode(slo_info, bliter);
1895 } catch (buffer::error& err) {
1896 ldpp_dout(this, 0) << "ERROR: failed to decode slo manifest" << dendl;
1897 return -EIO;
1898 }
1899 ldpp_dout(this, 2) << "RGWGetObj::handle_slo_manifest()" << dendl;
1900
1901 vector<RGWAccessControlPolicy> allocated_acls;
1902 map<string, pair<RGWAccessControlPolicy *, boost::optional<Policy>>> policies;
1903 map<string, std::unique_ptr<rgw::sal::RGWBucket>> buckets;
1904
1905 map<uint64_t, rgw_slo_part> slo_parts;
1906
1907 MD5 etag_sum;
1908 total_len = 0;
1909
1910 for (const auto& entry : slo_info.entries) {
1911 const string& path = entry.path;
1912
1913 /* If the path starts with slashes, strip them all. */
1914 const size_t pos_init = path.find_first_not_of('/');
1915 /* According to the documentation of std::string::find following check
1916 * is not necessary as we should get the std::string::npos propagation
1917 * here. This might be true with the accuracy to implementation's bugs.
1918 * See following question on SO:
1919 * http://stackoverflow.com/questions/1011790/why-does-stdstring-findtext-stdstringnpos-not-return-npos
1920 */
1921 if (pos_init == string::npos) {
1922 return -EINVAL;
1923 }
1924
1925 const size_t pos_sep = path.find('/', pos_init);
1926 if (pos_sep == string::npos) {
1927 return -EINVAL;
1928 }
1929
1930 string bucket_name = path.substr(pos_init, pos_sep - pos_init);
1931 string obj_name = path.substr(pos_sep + 1);
1932
1933 rgw::sal::RGWBucket* bucket;
1934 RGWAccessControlPolicy *bucket_acl;
1935 Policy* bucket_policy;
1936
1937 if (bucket_name.compare(s->bucket->get_name()) != 0) {
1938 const auto& piter = policies.find(bucket_name);
1939 if (piter != policies.end()) {
1940 bucket_acl = piter->second.first;
1941 bucket_policy = piter->second.second.get_ptr();
1942 bucket = buckets[bucket_name].get();
1943 } else {
1944 allocated_acls.push_back(RGWAccessControlPolicy(s->cct));
1945 RGWAccessControlPolicy& _bucket_acl = allocated_acls.back();
1946
1947 std::unique_ptr<rgw::sal::RGWBucket> tmp_bucket;
1948 auto obj_ctx = store->svc()->sysobj->init_obj_ctx();
1949 int r = store->get_bucket(this, s->user.get(), s->user->get_tenant(), bucket_name, &tmp_bucket, y);
1950 if (r < 0) {
1951 ldpp_dout(this, 0) << "could not get bucket info for bucket="
1952 << bucket_name << dendl;
1953 return r;
1954 }
1955 bucket = tmp_bucket.get();
1956 bucket_acl = &_bucket_acl;
1957 r = read_bucket_policy(this, store, s, tmp_bucket->get_info(), tmp_bucket->get_attrs(), bucket_acl,
1958 tmp_bucket->get_key(), y);
1959 if (r < 0) {
1960 ldpp_dout(this, 0) << "failed to read bucket ACL for bucket "
1961 << bucket << dendl;
1962 return r;
1963 }
1964 auto _bucket_policy = get_iam_policy_from_attr(
1965 s->cct, tmp_bucket->get_attrs(), tmp_bucket->get_tenant());
1966 bucket_policy = _bucket_policy.get_ptr();
1967 buckets[bucket_name].swap(tmp_bucket);
1968 policies[bucket_name] = make_pair(bucket_acl, _bucket_policy);
1969 }
1970 } else {
1971 bucket = s->bucket.get();
1972 bucket_acl = s->bucket_acl.get();
1973 bucket_policy = s->iam_policy.get_ptr();
1974 }
1975
1976 rgw_slo_part part;
1977 part.bucket_acl = bucket_acl;
1978 part.bucket_policy = bucket_policy;
1979 part.bucket = bucket;
1980 part.obj_name = obj_name;
1981 part.size = entry.size_bytes;
1982 part.etag = entry.etag;
1983 ldpp_dout(this, 20) << "slo_part: bucket=" << part.bucket
1984 << " obj=" << part.obj_name
1985 << " size=" << part.size
1986 << " etag=" << part.etag
1987 << dendl;
1988
1989 etag_sum.Update((const unsigned char *)entry.etag.c_str(),
1990 entry.etag.length());
1991
1992 slo_parts[total_len] = part;
1993 total_len += part.size;
1994 } /* foreach entry */
1995
1996 complete_etag(etag_sum, &lo_etag);
1997
1998 s->obj_size = slo_info.total_size;
1999 s->object->set_obj_size(slo_info.total_size);
2000 ldpp_dout(this, 20) << "s->obj_size=" << s->obj_size << dendl;
2001
2002 int r = RGWRados::Object::Read::range_to_ofs(total_len, ofs, end);
2003 if (r < 0) {
2004 return r;
2005 }
2006
2007 total_len = end - ofs + 1;
2008 ldpp_dout(this, 20) << "Requested: ofs=" << ofs
2009 << " end=" << end
2010 << " total=" << total_len
2011 << dendl;
2012
2013 r = iterate_slo_parts(s->cct, store, ofs, end, slo_parts,
2014 get_obj_user_manifest_iterate_cb, (void *)this);
2015 if (r < 0) {
2016 return r;
2017 }
2018
2019 return 0;
2020 }
2021
2022 int RGWGetObj::get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len)
2023 {
2024 /* garbage collection related handling:
2025 * defer_gc disabled for https://tracker.ceph.com/issues/47866 */
2026 return send_response_data(bl, bl_ofs, bl_len);
2027 }
2028
2029 bool RGWGetObj::prefetch_data()
2030 {
2031 /* HEAD request, stop prefetch*/
2032 if (!get_data || s->info.env->exists("HTTP_X_RGW_AUTH")) {
2033 return false;
2034 }
2035
2036 range_str = s->info.env->get("HTTP_RANGE");
2037 // TODO: add range prefetch
2038 if (range_str) {
2039 parse_range();
2040 return false;
2041 }
2042
2043 return get_data;
2044 }
2045
2046 void RGWGetObj::pre_exec()
2047 {
2048 rgw_bucket_object_pre_exec(s);
2049 }
2050
2051 static inline void rgw_cond_decode_objtags(
2052 struct req_state *s,
2053 const std::map<std::string, buffer::list> &attrs)
2054 {
2055 const auto& tags = attrs.find(RGW_ATTR_TAGS);
2056 if (tags != attrs.end()) {
2057 try {
2058 bufferlist::const_iterator iter{&tags->second};
2059 s->tagset.decode(iter);
2060 } catch (buffer::error& err) {
2061 ldpp_dout(s, 0)
2062 << "ERROR: caught buffer::error, couldn't decode TagSet" << dendl;
2063 }
2064 }
2065 }
2066
2067 void RGWGetObj::execute(optional_yield y)
2068 {
2069 bufferlist bl;
2070 gc_invalidate_time = ceph_clock_now();
2071 gc_invalidate_time += (s->cct->_conf->rgw_gc_obj_min_wait / 2);
2072
2073 bool need_decompress;
2074 int64_t ofs_x, end_x;
2075
2076 RGWGetObj_CB cb(this);
2077 RGWGetObj_Filter* filter = (RGWGetObj_Filter *)&cb;
2078 boost::optional<RGWGetObj_Decompress> decompress;
2079 std::unique_ptr<RGWGetObj_Filter> decrypt;
2080 map<string, bufferlist>::iterator attr_iter;
2081
2082 perfcounter->inc(l_rgw_get);
2083
2084 std::unique_ptr<rgw::sal::RGWObject::ReadOp> read_op(s->object->get_read_op(s->obj_ctx));
2085
2086 op_ret = get_params(y);
2087 if (op_ret < 0)
2088 goto done_err;
2089
2090 op_ret = init_common();
2091 if (op_ret < 0)
2092 goto done_err;
2093
2094 read_op->params.mod_ptr = mod_ptr;
2095 read_op->params.unmod_ptr = unmod_ptr;
2096 read_op->params.high_precision_time = s->system_request; /* system request need to use high precision time */
2097 read_op->params.mod_zone_id = mod_zone_id;
2098 read_op->params.mod_pg_ver = mod_pg_ver;
2099 read_op->params.if_match = if_match;
2100 read_op->params.if_nomatch = if_nomatch;
2101 read_op->params.lastmod = &lastmod;
2102
2103 op_ret = read_op->prepare(s->yield, this);
2104 if (op_ret < 0)
2105 goto done_err;
2106 version_id = s->object->get_instance();
2107 s->obj_size = s->object->get_obj_size();
2108 attrs = s->object->get_attrs();
2109
2110 /* STAT ops don't need data, and do no i/o */
2111 if (get_type() == RGW_OP_STAT_OBJ) {
2112 return;
2113 }
2114 if (s->info.env->exists("HTTP_X_RGW_AUTH")) {
2115 op_ret = 0;
2116 goto done_err;
2117 }
2118 /* start gettorrent */
2119 if (torrent.get_flag())
2120 {
2121 attr_iter = attrs.find(RGW_ATTR_CRYPT_MODE);
2122 if (attr_iter != attrs.end() && attr_iter->second.to_str() == "SSE-C-AES256") {
2123 ldpp_dout(this, 0) << "ERROR: torrents are not supported for objects "
2124 "encrypted with SSE-C" << dendl;
2125 op_ret = -EINVAL;
2126 goto done_err;
2127 }
2128 torrent.init(s, store);
2129 rgw_obj obj = s->object->get_obj();
2130 op_ret = torrent.get_torrent_file(s->object.get(), total_len, bl, obj);
2131 if (op_ret < 0)
2132 {
2133 ldpp_dout(this, 0) << "ERROR: failed to get_torrent_file ret= " << op_ret
2134 << dendl;
2135 goto done_err;
2136 }
2137 op_ret = send_response_data(bl, 0, total_len);
2138 if (op_ret < 0)
2139 {
2140 ldpp_dout(this, 0) << "ERROR: failed to send_response_data ret= " << op_ret << dendl;
2141 goto done_err;
2142 }
2143 return;
2144 }
2145 /* end gettorrent */
2146
2147 op_ret = rgw_compression_info_from_attrset(attrs, need_decompress, cs_info);
2148 if (op_ret < 0) {
2149 ldpp_dout(this, 0) << "ERROR: failed to decode compression info, cannot decompress" << dendl;
2150 goto done_err;
2151 }
2152 if (need_decompress) {
2153 s->obj_size = cs_info.orig_size;
2154 s->object->set_obj_size(cs_info.orig_size);
2155 decompress.emplace(s->cct, &cs_info, partial_content, filter);
2156 filter = &*decompress;
2157 }
2158
2159 attr_iter = attrs.find(RGW_ATTR_USER_MANIFEST);
2160 if (attr_iter != attrs.end() && !skip_manifest) {
2161 op_ret = handle_user_manifest(attr_iter->second.c_str(), y);
2162 if (op_ret < 0) {
2163 ldpp_dout(this, 0) << "ERROR: failed to handle user manifest ret="
2164 << op_ret << dendl;
2165 goto done_err;
2166 }
2167 return;
2168 }
2169
2170 attr_iter = attrs.find(RGW_ATTR_SLO_MANIFEST);
2171 if (attr_iter != attrs.end() && !skip_manifest) {
2172 is_slo = true;
2173 op_ret = handle_slo_manifest(attr_iter->second, y);
2174 if (op_ret < 0) {
2175 ldpp_dout(this, 0) << "ERROR: failed to handle slo manifest ret=" << op_ret
2176 << dendl;
2177 goto done_err;
2178 }
2179 return;
2180 }
2181
2182 // for range requests with obj size 0
2183 if (range_str && !(s->obj_size)) {
2184 total_len = 0;
2185 op_ret = -ERANGE;
2186 goto done_err;
2187 }
2188
2189 op_ret = s->object->range_to_ofs(s->obj_size, ofs, end);
2190 if (op_ret < 0)
2191 goto done_err;
2192 total_len = (ofs <= end ? end + 1 - ofs : 0);
2193
2194 /* Check whether the object has expired. Swift API documentation
2195 * stands that we should return 404 Not Found in such case. */
2196 if (need_object_expiration() && s->object->is_expired()) {
2197 op_ret = -ENOENT;
2198 goto done_err;
2199 }
2200
2201 /* Decode S3 objtags, if any */
2202 rgw_cond_decode_objtags(s, attrs);
2203
2204 start = ofs;
2205
2206 attr_iter = attrs.find(RGW_ATTR_MANIFEST);
2207 op_ret = this->get_decrypt_filter(&decrypt, filter,
2208 attr_iter != attrs.end() ? &(attr_iter->second) : nullptr);
2209 if (decrypt != nullptr) {
2210 filter = decrypt.get();
2211 }
2212 if (op_ret < 0) {
2213 goto done_err;
2214 }
2215
2216 if (!get_data || ofs > end) {
2217 send_response_data(bl, 0, 0);
2218 return;
2219 }
2220
2221 perfcounter->inc(l_rgw_get_b, end - ofs);
2222
2223 ofs_x = ofs;
2224 end_x = end;
2225 filter->fixup_range(ofs_x, end_x);
2226 op_ret = read_op->iterate(this, ofs_x, end_x, filter, s->yield);
2227
2228 if (op_ret >= 0)
2229 op_ret = filter->flush();
2230
2231 perfcounter->tinc(l_rgw_get_lat, s->time_elapsed());
2232 if (op_ret < 0) {
2233 goto done_err;
2234 }
2235
2236 op_ret = send_response_data(bl, 0, 0);
2237 if (op_ret < 0) {
2238 goto done_err;
2239 }
2240 return;
2241
2242 done_err:
2243 send_response_data_error(y);
2244 }
2245
2246 int RGWGetObj::init_common()
2247 {
2248 if (range_str) {
2249 /* range parsed error when prefetch */
2250 if (!range_parsed) {
2251 int r = parse_range();
2252 if (r < 0)
2253 return r;
2254 }
2255 }
2256 if (if_mod) {
2257 if (parse_time(if_mod, &mod_time) < 0)
2258 return -EINVAL;
2259 mod_ptr = &mod_time;
2260 }
2261
2262 if (if_unmod) {
2263 if (parse_time(if_unmod, &unmod_time) < 0)
2264 return -EINVAL;
2265 unmod_ptr = &unmod_time;
2266 }
2267
2268 return 0;
2269 }
2270
2271 int RGWListBuckets::verify_permission(optional_yield y)
2272 {
2273 rgw::Partition partition = rgw::Partition::aws;
2274 rgw::Service service = rgw::Service::s3;
2275
2276 string tenant;
2277 if (s->auth.identity->get_identity_type() == TYPE_ROLE) {
2278 tenant = s->auth.identity->get_role_tenant();
2279 } else {
2280 tenant = s->user->get_tenant();
2281 }
2282
2283 if (!verify_user_permission(this, s, ARN(partition, service, "", tenant, "*"), rgw::IAM::s3ListAllMyBuckets)) {
2284 return -EACCES;
2285 }
2286
2287 return 0;
2288 }
2289
2290 int RGWGetUsage::verify_permission(optional_yield y)
2291 {
2292 if (s->auth.identity->is_anonymous()) {
2293 return -EACCES;
2294 }
2295
2296 return 0;
2297 }
2298
2299 void RGWListBuckets::execute(optional_yield y)
2300 {
2301 bool done;
2302 bool started = false;
2303 uint64_t total_count = 0;
2304
2305 const uint64_t max_buckets = s->cct->_conf->rgw_list_buckets_max_chunk;
2306
2307 op_ret = get_params(y);
2308 if (op_ret < 0) {
2309 goto send_end;
2310 }
2311
2312 if (supports_account_metadata()) {
2313 op_ret = store->ctl()->user->get_attrs_by_uid(this, s->user->get_id(), &attrs, s->yield);
2314 if (op_ret < 0) {
2315 goto send_end;
2316 }
2317 }
2318
2319 is_truncated = false;
2320 do {
2321 rgw::sal::RGWBucketList buckets;
2322 uint64_t read_count;
2323 if (limit >= 0) {
2324 read_count = min(limit - total_count, max_buckets);
2325 } else {
2326 read_count = max_buckets;
2327 }
2328
2329 op_ret = s->user->list_buckets(this, marker, end_marker, read_count, should_get_stats(), buckets, y);
2330
2331 if (op_ret < 0) {
2332 /* hmm.. something wrong here.. the user was authenticated, so it
2333 should exist */
2334 ldpp_dout(this, 10) << "WARNING: failed on rgw_get_user_buckets uid="
2335 << s->user->get_id() << dendl;
2336 break;
2337 }
2338
2339 /* We need to have stats for all our policies - even if a given policy
2340 * isn't actually used in a given account. In such situation its usage
2341 * stats would be simply full of zeros. */
2342 for (const auto& policy : store->get_zonegroup().placement_targets) {
2343 policies_stats.emplace(policy.second.name,
2344 decltype(policies_stats)::mapped_type());
2345 }
2346
2347 std::map<std::string, std::unique_ptr<rgw::sal::RGWBucket>>& m = buckets.get_buckets();
2348 for (const auto& kv : m) {
2349 const auto& bucket = kv.second;
2350
2351 global_stats.bytes_used += bucket->get_size();
2352 global_stats.bytes_used_rounded += bucket->get_size_rounded();
2353 global_stats.objects_count += bucket->get_count();
2354
2355 /* operator[] still can create a new entry for storage policy seen
2356 * for first time. */
2357 auto& policy_stats = policies_stats[bucket->get_placement_rule().to_str()];
2358 policy_stats.bytes_used += bucket->get_size();
2359 policy_stats.bytes_used_rounded += bucket->get_size_rounded();
2360 policy_stats.buckets_count++;
2361 policy_stats.objects_count += bucket->get_count();
2362 }
2363 global_stats.buckets_count += m.size();
2364 total_count += m.size();
2365
2366 done = (m.size() < read_count || (limit >= 0 && total_count >= (uint64_t)limit));
2367
2368 if (!started) {
2369 send_response_begin(buckets.count() > 0);
2370 started = true;
2371 }
2372
2373 if (read_count > 0 &&
2374 !m.empty()) {
2375 auto riter = m.rbegin();
2376 marker = riter->first;
2377
2378 handle_listing_chunk(std::move(buckets));
2379 }
2380 } while (is_truncated && !done);
2381
2382 send_end:
2383 if (!started) {
2384 send_response_begin(false);
2385 }
2386 send_response_end();
2387 }
2388
2389 void RGWGetUsage::execute(optional_yield y)
2390 {
2391 uint64_t start_epoch = 0;
2392 uint64_t end_epoch = (uint64_t)-1;
2393 op_ret = get_params(y);
2394 if (op_ret < 0)
2395 return;
2396
2397 if (!start_date.empty()) {
2398 op_ret = utime_t::parse_date(start_date, &start_epoch, NULL);
2399 if (op_ret < 0) {
2400 ldpp_dout(this, 0) << "ERROR: failed to parse start date" << dendl;
2401 return;
2402 }
2403 }
2404
2405 if (!end_date.empty()) {
2406 op_ret = utime_t::parse_date(end_date, &end_epoch, NULL);
2407 if (op_ret < 0) {
2408 ldpp_dout(this, 0) << "ERROR: failed to parse end date" << dendl;
2409 return;
2410 }
2411 }
2412
2413 uint32_t max_entries = 1000;
2414
2415 bool is_truncated = true;
2416
2417 RGWUsageIter usage_iter;
2418
2419 while (s->bucket && is_truncated) {
2420 op_ret = s->bucket->read_usage(this, start_epoch, end_epoch, max_entries, &is_truncated,
2421 usage_iter, usage);
2422 if (op_ret == -ENOENT) {
2423 op_ret = 0;
2424 is_truncated = false;
2425 }
2426
2427 if (op_ret < 0) {
2428 return;
2429 }
2430 }
2431
2432 op_ret = rgw_user_sync_all_stats(this, store, s->user->get_id(), y);
2433 if (op_ret < 0) {
2434 ldpp_dout(this, 0) << "ERROR: failed to sync user stats" << dendl;
2435 return;
2436 }
2437
2438 op_ret = rgw_user_get_all_buckets_stats(this, store, s->user->get_id(), buckets_usage, y);
2439 if (op_ret < 0) {
2440 ldpp_dout(this, 0) << "ERROR: failed to get user's buckets stats" << dendl;
2441 return;
2442 }
2443
2444 op_ret = store->ctl()->user->read_stats(this, s->user->get_id(), &stats, y);
2445 if (op_ret < 0) {
2446 ldpp_dout(this, 0) << "ERROR: can't read user header" << dendl;
2447 return;
2448 }
2449
2450 return;
2451 }
2452
2453 int RGWStatAccount::verify_permission(optional_yield y)
2454 {
2455 if (!verify_user_permission_no_policy(this, s, RGW_PERM_READ)) {
2456 return -EACCES;
2457 }
2458
2459 return 0;
2460 }
2461
2462 void RGWStatAccount::execute(optional_yield y)
2463 {
2464 string marker;
2465 rgw::sal::RGWBucketList buckets;
2466 uint64_t max_buckets = s->cct->_conf->rgw_list_buckets_max_chunk;
2467 const string *lastmarker;
2468
2469 do {
2470
2471 lastmarker = nullptr;
2472 op_ret = rgw_read_user_buckets(this, store, s->user->get_id(), buckets, marker,
2473 string(), max_buckets, true, y);
2474 if (op_ret < 0) {
2475 /* hmm.. something wrong here.. the user was authenticated, so it
2476 should exist */
2477 ldpp_dout(this, 10) << "WARNING: failed on rgw_read_user_buckets uid="
2478 << s->user->get_id() << " ret=" << op_ret << dendl;
2479 break;
2480 } else {
2481 /* We need to have stats for all our policies - even if a given policy
2482 * isn't actually used in a given account. In such situation its usage
2483 * stats would be simply full of zeros. */
2484 for (const auto& policy : store->get_zonegroup().placement_targets) {
2485 policies_stats.emplace(policy.second.name,
2486 decltype(policies_stats)::mapped_type());
2487 }
2488
2489 std::map<std::string, std::unique_ptr<rgw::sal::RGWBucket>>& m = buckets.get_buckets();
2490 for (const auto& kv : m) {
2491 const auto& bucket = kv.second;
2492 lastmarker = &kv.first;
2493
2494 global_stats.bytes_used += bucket->get_size();
2495 global_stats.bytes_used_rounded += bucket->get_size_rounded();
2496 global_stats.objects_count += bucket->get_count();
2497
2498 /* operator[] still can create a new entry for storage policy seen
2499 * for first time. */
2500 auto& policy_stats = policies_stats[bucket->get_placement_rule().to_str()];
2501 policy_stats.bytes_used += bucket->get_size();
2502 policy_stats.bytes_used_rounded += bucket->get_size_rounded();
2503 policy_stats.buckets_count++;
2504 policy_stats.objects_count += bucket->get_count();
2505 }
2506 global_stats.buckets_count += m.size();
2507
2508 }
2509 if (!lastmarker) {
2510 ldpp_dout(this, -1) << "ERROR: rgw_read_user_buckets, stasis at marker="
2511 << marker << " uid=" << s->user->get_id() << dendl;
2512 break;
2513 }
2514 marker = *lastmarker;
2515 } while (buckets.is_truncated());
2516 }
2517
2518 int RGWGetBucketVersioning::verify_permission(optional_yield y)
2519 {
2520 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketVersioning);
2521 }
2522
2523 void RGWGetBucketVersioning::pre_exec()
2524 {
2525 rgw_bucket_object_pre_exec(s);
2526 }
2527
2528 void RGWGetBucketVersioning::execute(optional_yield y)
2529 {
2530 if (! s->bucket_exists) {
2531 op_ret = -ERR_NO_SUCH_BUCKET;
2532 return;
2533 }
2534
2535 versioned = s->bucket->versioned();
2536 versioning_enabled = s->bucket->versioning_enabled();
2537 mfa_enabled = s->bucket->get_info().mfa_enabled();
2538 }
2539
2540 int RGWSetBucketVersioning::verify_permission(optional_yield y)
2541 {
2542 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketVersioning);
2543 }
2544
2545 void RGWSetBucketVersioning::pre_exec()
2546 {
2547 rgw_bucket_object_pre_exec(s);
2548 }
2549
2550 void RGWSetBucketVersioning::execute(optional_yield y)
2551 {
2552 op_ret = get_params(y);
2553 if (op_ret < 0)
2554 return;
2555
2556 if (! s->bucket_exists) {
2557 op_ret = -ERR_NO_SUCH_BUCKET;
2558 return;
2559 }
2560
2561 if (s->bucket->get_info().obj_lock_enabled() && versioning_status != VersioningEnabled) {
2562 s->err.message = "bucket versioning cannot be disabled on buckets with object lock enabled";
2563 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
2564 op_ret = -ERR_INVALID_BUCKET_STATE;
2565 return;
2566 }
2567
2568 bool cur_mfa_status = s->bucket->get_info().mfa_enabled();
2569
2570 mfa_set_status &= (mfa_status != cur_mfa_status);
2571
2572 if (mfa_set_status &&
2573 !s->mfa_verified) {
2574 op_ret = -ERR_MFA_REQUIRED;
2575 return;
2576 }
2577 //if mfa is enabled for bucket, make sure mfa code is validated in case versioned status gets changed
2578 if (cur_mfa_status) {
2579 bool req_versioning_status = false;
2580 //if requested versioning status is not the same as the one set for the bucket, return error
2581 if (versioning_status == VersioningEnabled) {
2582 req_versioning_status = (s->bucket->get_info().flags & BUCKET_VERSIONS_SUSPENDED) != 0;
2583 } else if (versioning_status == VersioningSuspended) {
2584 req_versioning_status = (s->bucket->get_info().flags & BUCKET_VERSIONS_SUSPENDED) == 0;
2585 }
2586 if (req_versioning_status && !s->mfa_verified) {
2587 op_ret = -ERR_MFA_REQUIRED;
2588 return;
2589 }
2590 }
2591
2592 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
2593 if (op_ret < 0) {
2594 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
2595 return;
2596 }
2597
2598 bool modified = mfa_set_status;
2599
2600 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [&] {
2601 if (mfa_set_status) {
2602 if (mfa_status) {
2603 s->bucket->get_info().flags |= BUCKET_MFA_ENABLED;
2604 } else {
2605 s->bucket->get_info().flags &= ~BUCKET_MFA_ENABLED;
2606 }
2607 }
2608
2609 if (versioning_status == VersioningEnabled) {
2610 s->bucket->get_info().flags |= BUCKET_VERSIONED;
2611 s->bucket->get_info().flags &= ~BUCKET_VERSIONS_SUSPENDED;
2612 modified = true;
2613 } else if (versioning_status == VersioningSuspended) {
2614 s->bucket->get_info().flags |= (BUCKET_VERSIONED | BUCKET_VERSIONS_SUSPENDED);
2615 modified = true;
2616 } else {
2617 return op_ret;
2618 }
2619 s->bucket->set_attrs(rgw::sal::RGWAttrs(s->bucket_attrs));
2620 return s->bucket->put_instance_info(this, false, real_time());
2621 });
2622
2623 if (!modified) {
2624 return;
2625 }
2626
2627 if (op_ret < 0) {
2628 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
2629 << " returned err=" << op_ret << dendl;
2630 return;
2631 }
2632 }
2633
2634 int RGWGetBucketWebsite::verify_permission(optional_yield y)
2635 {
2636 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketWebsite);
2637 }
2638
2639 void RGWGetBucketWebsite::pre_exec()
2640 {
2641 rgw_bucket_object_pre_exec(s);
2642 }
2643
2644 void RGWGetBucketWebsite::execute(optional_yield y)
2645 {
2646 if (!s->bucket->get_info().has_website) {
2647 op_ret = -ERR_NO_SUCH_WEBSITE_CONFIGURATION;
2648 }
2649 }
2650
2651 int RGWSetBucketWebsite::verify_permission(optional_yield y)
2652 {
2653 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketWebsite);
2654 }
2655
2656 void RGWSetBucketWebsite::pre_exec()
2657 {
2658 rgw_bucket_object_pre_exec(s);
2659 }
2660
2661 void RGWSetBucketWebsite::execute(optional_yield y)
2662 {
2663 op_ret = get_params(y);
2664
2665 if (op_ret < 0)
2666 return;
2667
2668 if (!s->bucket_exists) {
2669 op_ret = -ERR_NO_SUCH_BUCKET;
2670 return;
2671 }
2672
2673 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
2674 if (op_ret < 0) {
2675 ldpp_dout(this, 0) << " forward_request_to_master returned ret=" << op_ret << dendl;
2676 return;
2677 }
2678
2679 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
2680 s->bucket->get_info().has_website = true;
2681 s->bucket->get_info().website_conf = website_conf;
2682 op_ret = s->bucket->put_instance_info(this, false, real_time());
2683 return op_ret;
2684 });
2685
2686 if (op_ret < 0) {
2687 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
2688 << " returned err=" << op_ret << dendl;
2689 return;
2690 }
2691 }
2692
2693 int RGWDeleteBucketWebsite::verify_permission(optional_yield y)
2694 {
2695 return verify_bucket_owner_or_policy(s, rgw::IAM::s3DeleteBucketWebsite);
2696 }
2697
2698 void RGWDeleteBucketWebsite::pre_exec()
2699 {
2700 rgw_bucket_object_pre_exec(s);
2701 }
2702
2703 void RGWDeleteBucketWebsite::execute(optional_yield y)
2704 {
2705 if (!s->bucket_exists) {
2706 op_ret = -ERR_NO_SUCH_BUCKET;
2707 return;
2708 }
2709
2710 bufferlist in_data;
2711
2712 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
2713 if (op_ret < 0) {
2714 ldpp_dout(this, 0) << "NOTICE: forward_to_master failed on bucket=" << s->bucket->get_name()
2715 << "returned err=" << op_ret << dendl;
2716 return;
2717 }
2718 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
2719 s->bucket->get_info().has_website = false;
2720 s->bucket->get_info().website_conf = RGWBucketWebsiteConf();
2721 op_ret = s->bucket->put_instance_info(this, false, real_time());
2722 return op_ret;
2723 });
2724 if (op_ret < 0) {
2725 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket
2726 << " returned err=" << op_ret << dendl;
2727 return;
2728 }
2729 }
2730
2731 int RGWStatBucket::verify_permission(optional_yield y)
2732 {
2733 // This (a HEAD request on a bucket) is governed by the s3:ListBucket permission.
2734 if (!verify_bucket_permission(this, s, rgw::IAM::s3ListBucket)) {
2735 return -EACCES;
2736 }
2737
2738 return 0;
2739 }
2740
2741 void RGWStatBucket::pre_exec()
2742 {
2743 rgw_bucket_object_pre_exec(s);
2744 }
2745
2746 void RGWStatBucket::execute(optional_yield y)
2747 {
2748 if (!s->bucket_exists) {
2749 op_ret = -ERR_NO_SUCH_BUCKET;
2750 return;
2751 }
2752
2753 op_ret = store->get_bucket(this, s->user.get(), s->bucket->get_key(), &bucket, y);
2754 if (op_ret) {
2755 return;
2756 }
2757 op_ret = bucket->update_container_stats(s);
2758 }
2759
2760 int RGWListBucket::verify_permission(optional_yield y)
2761 {
2762 op_ret = get_params(y);
2763 if (op_ret < 0) {
2764 return op_ret;
2765 }
2766 if (!prefix.empty())
2767 s->env.emplace("s3:prefix", prefix);
2768
2769 if (!delimiter.empty())
2770 s->env.emplace("s3:delimiter", delimiter);
2771
2772 s->env.emplace("s3:max-keys", std::to_string(max));
2773
2774 if (!verify_bucket_permission(this,
2775 s,
2776 list_versions ?
2777 rgw::IAM::s3ListBucketVersions :
2778 rgw::IAM::s3ListBucket)) {
2779 return -EACCES;
2780 }
2781
2782 return 0;
2783 }
2784
2785 int RGWListBucket::parse_max_keys()
2786 {
2787 // Bound max value of max-keys to configured value for security
2788 // Bound min value of max-keys to '0'
2789 // Some S3 clients explicitly send max-keys=0 to detect if the bucket is
2790 // empty without listing any items.
2791 return parse_value_and_bound(max_keys, max, 0,
2792 g_conf().get_val<uint64_t>("rgw_max_listing_results"),
2793 default_max);
2794 }
2795
2796 void RGWListBucket::pre_exec()
2797 {
2798 rgw_bucket_object_pre_exec(s);
2799 }
2800
2801 void RGWListBucket::execute(optional_yield y)
2802 {
2803 if (!s->bucket_exists) {
2804 op_ret = -ERR_NO_SUCH_BUCKET;
2805 return;
2806 }
2807
2808 if (allow_unordered && !delimiter.empty()) {
2809 ldpp_dout(this, 0) <<
2810 "ERROR: unordered bucket listing requested with a delimiter" << dendl;
2811 op_ret = -EINVAL;
2812 return;
2813 }
2814
2815 if (need_container_stats()) {
2816 op_ret = s->bucket->update_container_stats(s);
2817 }
2818
2819 rgw::sal::RGWBucket::ListParams params;
2820 params.prefix = prefix;
2821 params.delim = delimiter;
2822 params.marker = marker;
2823 params.end_marker = end_marker;
2824 params.list_versions = list_versions;
2825 params.allow_unordered = allow_unordered;
2826 params.shard_id = shard_id;
2827
2828 rgw::sal::RGWBucket::ListResults results;
2829
2830 op_ret = s->bucket->list(this, params, max, results, y);
2831 if (op_ret >= 0) {
2832 next_marker = results.next_marker;
2833 is_truncated = results.is_truncated;
2834 objs = std::move(results.objs);
2835 common_prefixes = std::move(results.common_prefixes);
2836 }
2837 }
2838
2839 int RGWGetBucketLogging::verify_permission(optional_yield y)
2840 {
2841 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketLogging);
2842 }
2843
2844 int RGWGetBucketLocation::verify_permission(optional_yield y)
2845 {
2846 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketLocation);
2847 }
2848
2849 int RGWCreateBucket::verify_permission(optional_yield y)
2850 {
2851 /* This check is mostly needed for S3 that doesn't support account ACL.
2852 * Swift doesn't allow to delegate any permission to an anonymous user,
2853 * so it will become an early exit in such case. */
2854 if (s->auth.identity->is_anonymous()) {
2855 return -EACCES;
2856 }
2857
2858 rgw_bucket bucket;
2859 bucket.name = s->bucket_name;
2860 bucket.tenant = s->bucket_tenant;
2861 ARN arn = ARN(bucket);
2862 if (!verify_user_permission(this, s, arn, rgw::IAM::s3CreateBucket)) {
2863 return -EACCES;
2864 }
2865
2866 if (s->user->get_tenant() != s->bucket_tenant) {
2867 //AssumeRole is meant for cross account access
2868 if (s->auth.identity->get_identity_type() != TYPE_ROLE) {
2869 ldpp_dout(this, 10) << "user cannot create a bucket in a different tenant"
2870 << " (user_id.tenant=" << s->user->get_tenant()
2871 << " requested=" << s->bucket_tenant << ")"
2872 << dendl;
2873 return -EACCES;
2874 }
2875 }
2876
2877 if (s->user->get_max_buckets() < 0) {
2878 return -EPERM;
2879 }
2880
2881 if (s->user->get_max_buckets()) {
2882 rgw::sal::RGWBucketList buckets;
2883 string marker;
2884 op_ret = rgw_read_user_buckets(this, store, s->user->get_id(), buckets,
2885 marker, string(), s->user->get_max_buckets(),
2886 false, y);
2887 if (op_ret < 0) {
2888 return op_ret;
2889 }
2890
2891 if ((int)buckets.count() >= s->user->get_max_buckets()) {
2892 return -ERR_TOO_MANY_BUCKETS;
2893 }
2894 }
2895
2896 return 0;
2897 }
2898
2899 void RGWCreateBucket::pre_exec()
2900 {
2901 rgw_bucket_object_pre_exec(s);
2902 }
2903
2904 static void prepare_add_del_attrs(const map<string, bufferlist>& orig_attrs,
2905 map<string, bufferlist>& out_attrs,
2906 map<string, bufferlist>& out_rmattrs)
2907 {
2908 for (const auto& kv : orig_attrs) {
2909 const string& name = kv.first;
2910
2911 /* Check if the attr is user-defined metadata item. */
2912 if (name.compare(0, sizeof(RGW_ATTR_META_PREFIX) - 1,
2913 RGW_ATTR_META_PREFIX) == 0) {
2914 /* For the objects all existing meta attrs have to be removed. */
2915 out_rmattrs[name] = kv.second;
2916 } else if (out_attrs.find(name) == std::end(out_attrs)) {
2917 out_attrs[name] = kv.second;
2918 }
2919 }
2920 }
2921
2922 /* Fuse resource metadata basing on original attributes in @orig_attrs, set
2923 * of _custom_ attribute names to remove in @rmattr_names and attributes in
2924 * @out_attrs. Place results in @out_attrs.
2925 *
2926 * NOTE: it's supposed that all special attrs already present in @out_attrs
2927 * will be preserved without any change. Special attributes are those which
2928 * names start with RGW_ATTR_META_PREFIX. They're complement to custom ones
2929 * used for X-Account-Meta-*, X-Container-Meta-*, X-Amz-Meta and so on. */
2930 static void prepare_add_del_attrs(const map<string, bufferlist>& orig_attrs,
2931 const set<string>& rmattr_names,
2932 map<string, bufferlist>& out_attrs)
2933 {
2934 for (const auto& kv : orig_attrs) {
2935 const string& name = kv.first;
2936
2937 /* Check if the attr is user-defined metadata item. */
2938 if (name.compare(0, strlen(RGW_ATTR_META_PREFIX),
2939 RGW_ATTR_META_PREFIX) == 0) {
2940 /* For the buckets all existing meta attrs are preserved,
2941 except those that are listed in rmattr_names. */
2942 if (rmattr_names.find(name) != std::end(rmattr_names)) {
2943 const auto aiter = out_attrs.find(name);
2944
2945 if (aiter != std::end(out_attrs)) {
2946 out_attrs.erase(aiter);
2947 }
2948 } else {
2949 /* emplace() won't alter the map if the key is already present.
2950 * This behaviour is fully intensional here. */
2951 out_attrs.emplace(kv);
2952 }
2953 } else if (out_attrs.find(name) == std::end(out_attrs)) {
2954 out_attrs[name] = kv.second;
2955 }
2956 }
2957 }
2958
2959
2960 static void populate_with_generic_attrs(const req_state * const s,
2961 map<string, bufferlist>& out_attrs)
2962 {
2963 for (const auto& kv : s->generic_attrs) {
2964 bufferlist& attrbl = out_attrs[kv.first];
2965 const string& val = kv.second;
2966 attrbl.clear();
2967 attrbl.append(val.c_str(), val.size() + 1);
2968 }
2969 }
2970
2971
2972 static int filter_out_quota_info(std::map<std::string, bufferlist>& add_attrs,
2973 const std::set<std::string>& rmattr_names,
2974 RGWQuotaInfo& quota,
2975 bool * quota_extracted = nullptr)
2976 {
2977 bool extracted = false;
2978
2979 /* Put new limit on max objects. */
2980 auto iter = add_attrs.find(RGW_ATTR_QUOTA_NOBJS);
2981 std::string err;
2982 if (std::end(add_attrs) != iter) {
2983 quota.max_objects =
2984 static_cast<int64_t>(strict_strtoll(iter->second.c_str(), 10, &err));
2985 if (!err.empty()) {
2986 return -EINVAL;
2987 }
2988 add_attrs.erase(iter);
2989 extracted = true;
2990 }
2991
2992 /* Put new limit on bucket (container) size. */
2993 iter = add_attrs.find(RGW_ATTR_QUOTA_MSIZE);
2994 if (iter != add_attrs.end()) {
2995 quota.max_size =
2996 static_cast<int64_t>(strict_strtoll(iter->second.c_str(), 10, &err));
2997 if (!err.empty()) {
2998 return -EINVAL;
2999 }
3000 add_attrs.erase(iter);
3001 extracted = true;
3002 }
3003
3004 for (const auto& name : rmattr_names) {
3005 /* Remove limit on max objects. */
3006 if (name.compare(RGW_ATTR_QUOTA_NOBJS) == 0) {
3007 quota.max_objects = -1;
3008 extracted = true;
3009 }
3010
3011 /* Remove limit on max bucket size. */
3012 if (name.compare(RGW_ATTR_QUOTA_MSIZE) == 0) {
3013 quota.max_size = -1;
3014 extracted = true;
3015 }
3016 }
3017
3018 /* Swift requries checking on raw usage instead of the 4 KiB rounded one. */
3019 quota.check_on_raw = true;
3020 quota.enabled = quota.max_size > 0 || quota.max_objects > 0;
3021
3022 if (quota_extracted) {
3023 *quota_extracted = extracted;
3024 }
3025
3026 return 0;
3027 }
3028
3029
3030 static void filter_out_website(std::map<std::string, ceph::bufferlist>& add_attrs,
3031 const std::set<std::string>& rmattr_names,
3032 RGWBucketWebsiteConf& ws_conf)
3033 {
3034 std::string lstval;
3035
3036 /* Let's define a mapping between each custom attribute and the memory where
3037 * attribute's value should be stored. The memory location is expressed by
3038 * a non-const reference. */
3039 const auto mapping = {
3040 std::make_pair(RGW_ATTR_WEB_INDEX, std::ref(ws_conf.index_doc_suffix)),
3041 std::make_pair(RGW_ATTR_WEB_ERROR, std::ref(ws_conf.error_doc)),
3042 std::make_pair(RGW_ATTR_WEB_LISTINGS, std::ref(lstval)),
3043 std::make_pair(RGW_ATTR_WEB_LIST_CSS, std::ref(ws_conf.listing_css_doc)),
3044 std::make_pair(RGW_ATTR_SUBDIR_MARKER, std::ref(ws_conf.subdir_marker))
3045 };
3046
3047 for (const auto& kv : mapping) {
3048 const char * const key = kv.first;
3049 auto& target = kv.second;
3050
3051 auto iter = add_attrs.find(key);
3052
3053 if (std::end(add_attrs) != iter) {
3054 /* The "target" is a reference to ws_conf. */
3055 target = iter->second.c_str();
3056 add_attrs.erase(iter);
3057 }
3058
3059 if (rmattr_names.count(key)) {
3060 target = std::string();
3061 }
3062 }
3063
3064 if (! lstval.empty()) {
3065 ws_conf.listing_enabled = boost::algorithm::iequals(lstval, "true");
3066 }
3067 }
3068
3069
3070 void RGWCreateBucket::execute(optional_yield y)
3071 {
3072 buffer::list aclbl;
3073 buffer::list corsbl;
3074 string bucket_name = rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name);
3075 rgw_raw_obj obj(store->svc()->zone->get_zone_params().domain_root, bucket_name);
3076
3077 op_ret = get_params(y);
3078 if (op_ret < 0)
3079 return;
3080
3081 if (!relaxed_region_enforcement &&
3082 !location_constraint.empty() &&
3083 !store->svc()->zone->has_zonegroup_api(location_constraint)) {
3084 ldpp_dout(this, 0) << "location constraint (" << location_constraint << ")"
3085 << " can't be found." << dendl;
3086 op_ret = -ERR_INVALID_LOCATION_CONSTRAINT;
3087 s->err.message = "The specified location-constraint is not valid";
3088 return;
3089 }
3090
3091 if (!relaxed_region_enforcement && !store->get_zonegroup().is_master_zonegroup() && !location_constraint.empty() &&
3092 store->get_zonegroup().api_name != location_constraint) {
3093 ldpp_dout(this, 0) << "location constraint (" << location_constraint << ")"
3094 << " doesn't match zonegroup" << " (" << store->get_zonegroup().api_name << ")"
3095 << dendl;
3096 op_ret = -ERR_INVALID_LOCATION_CONSTRAINT;
3097 s->err.message = "The specified location-constraint is not valid";
3098 return;
3099 }
3100
3101 const auto& zonegroup = store->get_zonegroup();
3102 if (!placement_rule.name.empty() &&
3103 !zonegroup.placement_targets.count(placement_rule.name)) {
3104 ldpp_dout(this, 0) << "placement target (" << placement_rule.name << ")"
3105 << " doesn't exist in the placement targets of zonegroup"
3106 << " (" << store->get_zonegroup().api_name << ")" << dendl;
3107 op_ret = -ERR_INVALID_LOCATION_CONSTRAINT;
3108 s->err.message = "The specified placement target does not exist";
3109 return;
3110 }
3111
3112 /* we need to make sure we read bucket info, it's not read before for this
3113 * specific request */
3114 op_ret = store->get_bucket(this, s->user.get(), s->bucket_tenant, s->bucket_name, &s->bucket, y);
3115 if (op_ret < 0 && op_ret != -ENOENT)
3116 return;
3117 s->bucket_exists = (op_ret != -ENOENT);
3118
3119 if (s->bucket_exists) {
3120 if (!s->system_request &&
3121 store->svc()->zone->get_zonegroup().get_id() != s->bucket->get_info().zonegroup) {
3122 op_ret = -EEXIST;
3123 return;
3124 }
3125 /* Initialize info from req_state */
3126 info = s->bucket->get_info();
3127 }
3128
3129 s->bucket_owner.set_id(s->user->get_id()); /* XXX dang use s->bucket->owner */
3130 s->bucket_owner.set_name(s->user->get_display_name());
3131
3132 string zonegroup_id;
3133
3134 if (s->system_request) {
3135 zonegroup_id = s->info.args.get(RGW_SYS_PARAM_PREFIX "zonegroup");
3136 if (zonegroup_id.empty()) {
3137 zonegroup_id = store->get_zonegroup().get_id();
3138 }
3139 } else {
3140 zonegroup_id = store->get_zonegroup().get_id();
3141 }
3142
3143 /* Encode special metadata first as we're using std::map::emplace under
3144 * the hood. This method will add the new items only if the map doesn't
3145 * contain such keys yet. */
3146 policy.encode(aclbl);
3147 emplace_attr(RGW_ATTR_ACL, std::move(aclbl));
3148
3149 if (has_cors) {
3150 cors_config.encode(corsbl);
3151 emplace_attr(RGW_ATTR_CORS, std::move(corsbl));
3152 }
3153
3154 RGWQuotaInfo quota_info;
3155 const RGWQuotaInfo * pquota_info = nullptr;
3156 if (need_metadata_upload()) {
3157 /* It's supposed that following functions WILL NOT change any special
3158 * attributes (like RGW_ATTR_ACL) if they are already present in attrs. */
3159 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs, false);
3160 if (op_ret < 0) {
3161 return;
3162 }
3163 prepare_add_del_attrs(s->bucket_attrs, rmattr_names, attrs);
3164 populate_with_generic_attrs(s, attrs);
3165
3166 op_ret = filter_out_quota_info(attrs, rmattr_names, quota_info);
3167 if (op_ret < 0) {
3168 return;
3169 } else {
3170 pquota_info = &quota_info;
3171 }
3172
3173 /* Web site of Swift API. */
3174 filter_out_website(attrs, rmattr_names, info.website_conf);
3175 info.has_website = !info.website_conf.is_empty();
3176 }
3177
3178 rgw_bucket tmp_bucket;
3179 tmp_bucket.tenant = s->bucket_tenant; /* ignored if bucket exists */
3180 tmp_bucket.name = s->bucket_name;
3181
3182 /* Handle updates of the metadata for Swift's object versioning. */
3183 if (swift_ver_location) {
3184 info.swift_ver_location = *swift_ver_location;
3185 info.swift_versioning = (! swift_ver_location->empty());
3186 }
3187
3188 /* We're replacing bucket with the newly created one */
3189 ldpp_dout(this, 10) << "user=" << s->user << " bucket=" << tmp_bucket << dendl;
3190 op_ret = store->create_bucket(this, *s->user, tmp_bucket, zonegroup_id,
3191 placement_rule,
3192 info.swift_ver_location,
3193 pquota_info, policy, attrs, info, ep_objv,
3194 true, obj_lock_enabled, &s->bucket_exists, s->info,
3195 &s->bucket, y);
3196
3197 /* continue if EEXIST and create_bucket will fail below. this way we can
3198 * recover from a partial create by retrying it. */
3199 ldpp_dout(this, 20) << "rgw_create_bucket returned ret=" << op_ret << " bucket=" << s->bucket.get() << dendl;
3200
3201 if (op_ret)
3202 return;
3203
3204 const bool existed = s->bucket_exists;
3205 if (existed) {
3206 /* bucket already existed, might have raced with another bucket creation, or
3207 * might be partial bucket creation that never completed. Read existing bucket
3208 * info, verify that the reported bucket owner is the current user.
3209 * If all is ok then update the user's list of buckets.
3210 * Otherwise inform client about a name conflict.
3211 */
3212 if (s->bucket->get_info().owner.compare(s->user->get_id()) != 0) {
3213 op_ret = -EEXIST;
3214 return;
3215 }
3216 }
3217
3218 op_ret = store->ctl()->bucket->link_bucket(s->user->get_id(), s->bucket->get_key(),
3219 s->bucket->get_creation_time(), y, s, false);
3220 if (op_ret && !existed && op_ret != -EEXIST) {
3221 /* if it exists (or previously existed), don't remove it! */
3222 op_ret = store->ctl()->bucket->unlink_bucket(s->user->get_id(), s->bucket->get_key(), y, this);
3223 if (op_ret < 0) {
3224 ldpp_dout(this, 0) << "WARNING: failed to unlink bucket: ret=" << op_ret
3225 << dendl;
3226 }
3227 } else if (op_ret == -EEXIST || (op_ret == 0 && existed)) {
3228 op_ret = -ERR_BUCKET_EXISTS;
3229 }
3230
3231 if (need_metadata_upload() && existed) {
3232 /* OK, it looks we lost race with another request. As it's required to
3233 * handle metadata fusion and upload, the whole operation becomes very
3234 * similar in nature to PutMetadataBucket. However, as the attrs may
3235 * changed in the meantime, we have to refresh. */
3236 short tries = 0;
3237 do {
3238 map<string, bufferlist> battrs;
3239
3240 op_ret = s->bucket->get_bucket_info(this, y);
3241 if (op_ret < 0) {
3242 return;
3243 } else if (!s->bucket->is_owner(s->user.get())) {
3244 /* New bucket doesn't belong to the account we're operating on. */
3245 op_ret = -EEXIST;
3246 return;
3247 } else {
3248 s->bucket_attrs = s->bucket->get_attrs();
3249 }
3250
3251 attrs.clear();
3252
3253 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs, false);
3254 if (op_ret < 0) {
3255 return;
3256 }
3257 prepare_add_del_attrs(s->bucket_attrs, rmattr_names, attrs);
3258 populate_with_generic_attrs(s, attrs);
3259 op_ret = filter_out_quota_info(attrs, rmattr_names, s->bucket->get_info().quota);
3260 if (op_ret < 0) {
3261 return;
3262 }
3263
3264 /* Handle updates of the metadata for Swift's object versioning. */
3265 if (swift_ver_location) {
3266 s->bucket->get_info().swift_ver_location = *swift_ver_location;
3267 s->bucket->get_info().swift_versioning = (! swift_ver_location->empty());
3268 }
3269
3270 /* Web site of Swift API. */
3271 filter_out_website(attrs, rmattr_names, s->bucket->get_info().website_conf);
3272 s->bucket->get_info().has_website = !s->bucket->get_info().website_conf.is_empty();
3273
3274 /* This will also set the quota on the bucket. */
3275 op_ret = store->ctl()->bucket->set_bucket_instance_attrs(s->bucket->get_info(), attrs,
3276 &s->bucket->get_info().objv_tracker,
3277 y, this);
3278 } while (op_ret == -ECANCELED && tries++ < 20);
3279
3280 /* Restore the proper return code. */
3281 if (op_ret >= 0) {
3282 op_ret = -ERR_BUCKET_EXISTS;
3283 }
3284 }
3285 }
3286
3287 int RGWDeleteBucket::verify_permission(optional_yield y)
3288 {
3289 if (!verify_bucket_permission(this, s, rgw::IAM::s3DeleteBucket)) {
3290 return -EACCES;
3291 }
3292
3293 return 0;
3294 }
3295
3296 void RGWDeleteBucket::pre_exec()
3297 {
3298 rgw_bucket_object_pre_exec(s);
3299 }
3300
3301 void RGWDeleteBucket::execute(optional_yield y)
3302 {
3303 if (s->bucket_name.empty()) {
3304 op_ret = -EINVAL;
3305 return;
3306 }
3307
3308 if (!s->bucket_exists) {
3309 ldpp_dout(this, 0) << "ERROR: bucket " << s->bucket_name << " not found" << dendl;
3310 op_ret = -ERR_NO_SUCH_BUCKET;
3311 return;
3312 }
3313 RGWObjVersionTracker ot;
3314 ot.read_version = s->bucket->get_version();
3315
3316 if (s->system_request) {
3317 string tag = s->info.args.get(RGW_SYS_PARAM_PREFIX "tag");
3318 string ver_str = s->info.args.get(RGW_SYS_PARAM_PREFIX "ver");
3319 if (!tag.empty()) {
3320 ot.read_version.tag = tag;
3321 uint64_t ver;
3322 string err;
3323 ver = strict_strtol(ver_str.c_str(), 10, &err);
3324 if (!err.empty()) {
3325 ldpp_dout(this, 0) << "failed to parse ver param" << dendl;
3326 op_ret = -EINVAL;
3327 return;
3328 }
3329 ot.read_version.ver = ver;
3330 }
3331 }
3332
3333 op_ret = s->bucket->sync_user_stats(this, y);
3334 if ( op_ret < 0) {
3335 ldpp_dout(this, 1) << "WARNING: failed to sync user stats before bucket delete: op_ret= " << op_ret << dendl;
3336 }
3337
3338 op_ret = s->bucket->check_empty(this, y);
3339 if (op_ret < 0) {
3340 return;
3341 }
3342
3343 bufferlist in_data;
3344 op_ret = store->forward_request_to_master(this, s->user.get(), &ot.read_version, in_data, nullptr, s->info, y);
3345 if (op_ret < 0) {
3346 if (op_ret == -ENOENT) {
3347 /* adjust error, we want to return with NoSuchBucket and not
3348 * NoSuchKey */
3349 op_ret = -ERR_NO_SUCH_BUCKET;
3350 }
3351 return;
3352 }
3353
3354 op_ret = s->bucket->remove_bucket(this, false, false, nullptr, y);
3355 if (op_ret < 0 && op_ret == -ECANCELED) {
3356 // lost a race, either with mdlog sync or another delete bucket operation.
3357 // in either case, we've already called ctl.bucket->unlink_bucket()
3358 op_ret = 0;
3359 }
3360 return;
3361 }
3362
3363 int RGWPutObj::init_processing(optional_yield y) {
3364 copy_source = url_decode(s->info.env->get("HTTP_X_AMZ_COPY_SOURCE", ""));
3365 copy_source_range = s->info.env->get("HTTP_X_AMZ_COPY_SOURCE_RANGE");
3366 size_t pos;
3367 int ret;
3368
3369 /* handle x-amz-copy-source */
3370 std::string_view cs_view(copy_source);
3371 if (! cs_view.empty()) {
3372 if (cs_view[0] == '/')
3373 cs_view.remove_prefix(1);
3374 copy_source_bucket_name = std::string(cs_view);
3375 pos = copy_source_bucket_name.find("/");
3376 if (pos == std::string::npos) {
3377 ret = -EINVAL;
3378 ldpp_dout(this, 5) << "x-amz-copy-source bad format" << dendl;
3379 return ret;
3380 }
3381 copy_source_object_name =
3382 copy_source_bucket_name.substr(pos + 1, copy_source_bucket_name.size());
3383 copy_source_bucket_name = copy_source_bucket_name.substr(0, pos);
3384 #define VERSION_ID_STR "?versionId="
3385 pos = copy_source_object_name.find(VERSION_ID_STR);
3386 if (pos == std::string::npos) {
3387 copy_source_object_name = url_decode(copy_source_object_name);
3388 } else {
3389 copy_source_version_id =
3390 copy_source_object_name.substr(pos + sizeof(VERSION_ID_STR) - 1);
3391 copy_source_object_name =
3392 url_decode(copy_source_object_name.substr(0, pos));
3393 }
3394 pos = copy_source_bucket_name.find(":");
3395 if (pos == std::string::npos) {
3396 // if tenant is not specified in x-amz-copy-source, use tenant of the requester
3397 copy_source_tenant_name = s->user->get_tenant();
3398 } else {
3399 copy_source_tenant_name = copy_source_bucket_name.substr(0, pos);
3400 copy_source_bucket_name = copy_source_bucket_name.substr(pos + 1, copy_source_bucket_name.size());
3401 if (copy_source_bucket_name.empty()) {
3402 ret = -EINVAL;
3403 ldpp_dout(this, 5) << "source bucket name is empty" << dendl;
3404 return ret;
3405 }
3406 }
3407 std::unique_ptr<rgw::sal::RGWBucket> bucket;
3408 ret = store->get_bucket(this, s->user.get(), copy_source_tenant_name, copy_source_bucket_name,
3409 &bucket, y);
3410 if (ret < 0) {
3411 ldpp_dout(this, 5) << __func__ << "(): get_bucket() returned ret=" << ret << dendl;
3412 return ret;
3413 }
3414
3415 ret = bucket->get_bucket_info(this, y);
3416 if (ret < 0) {
3417 ldpp_dout(this, 5) << __func__ << "(): get_bucket_info() returned ret=" << ret << dendl;
3418 return ret;
3419 }
3420 copy_source_bucket_info = bucket->get_info();
3421
3422 /* handle x-amz-copy-source-range */
3423 if (copy_source_range) {
3424 string range = copy_source_range;
3425 pos = range.find("bytes=");
3426 if (pos == std::string::npos || pos != 0) {
3427 ret = -EINVAL;
3428 ldpp_dout(this, 5) << "x-amz-copy-source-range bad format" << dendl;
3429 return ret;
3430 }
3431 /* 6 is the length of "bytes=" */
3432 range = range.substr(pos + 6);
3433 pos = range.find("-");
3434 if (pos == std::string::npos) {
3435 ret = -EINVAL;
3436 ldpp_dout(this, 5) << "x-amz-copy-source-range bad format" << dendl;
3437 return ret;
3438 }
3439 string first = range.substr(0, pos);
3440 string last = range.substr(pos + 1);
3441 if (first.find_first_not_of("0123456789") != std::string::npos ||
3442 last.find_first_not_of("0123456789") != std::string::npos) {
3443 ldpp_dout(this, 5) << "x-amz-copy-source-range bad format not an integer" << dendl;
3444 ret = -EINVAL;
3445 return ret;
3446 }
3447 copy_source_range_fst = strtoull(first.c_str(), NULL, 10);
3448 copy_source_range_lst = strtoull(last.c_str(), NULL, 10);
3449 if (copy_source_range_fst > copy_source_range_lst) {
3450 ret = -ERANGE;
3451 ldpp_dout(this, 5) << "x-amz-copy-source-range bad format first number bigger than second" << dendl;
3452 return ret;
3453 }
3454 }
3455
3456 } /* copy_source */
3457 return RGWOp::init_processing(y);
3458 }
3459
3460 int RGWPutObj::verify_permission(optional_yield y)
3461 {
3462 if (! copy_source.empty()) {
3463
3464 RGWAccessControlPolicy cs_acl(s->cct);
3465 boost::optional<Policy> policy;
3466 map<string, bufferlist> cs_attrs;
3467 std::unique_ptr<rgw::sal::RGWBucket> cs_bucket;
3468 int ret = store->get_bucket(NULL, copy_source_bucket_info, &cs_bucket);
3469 if (ret < 0)
3470 return ret;
3471
3472 std::unique_ptr<rgw::sal::RGWObject> cs_object =
3473 cs_bucket->get_object(rgw_obj_key(copy_source_object_name, copy_source_version_id));
3474
3475 cs_object->set_atomic(s->obj_ctx);
3476 cs_object->set_prefetch_data(s->obj_ctx);
3477
3478 /* check source object permissions */
3479 if (read_obj_policy(this, store, s, copy_source_bucket_info, cs_attrs, &cs_acl, nullptr,
3480 policy, cs_bucket.get(), cs_object.get(), y, true) < 0) {
3481 return -EACCES;
3482 }
3483
3484 /* admin request overrides permission checks */
3485 if (! s->auth.identity->is_admin_of(cs_acl.get_owner().get_id())) {
3486 if (policy || ! s->iam_user_policies.empty()) {
3487 auto usr_policy_res = Effect::Pass;
3488 for (auto& user_policy : s->iam_user_policies) {
3489 if (usr_policy_res = user_policy.eval(s->env, *s->auth.identity,
3490 cs_object->get_instance().empty() ?
3491 rgw::IAM::s3GetObject :
3492 rgw::IAM::s3GetObjectVersion,
3493 rgw::ARN(cs_object->get_obj())); usr_policy_res == Effect::Deny)
3494 return -EACCES;
3495 else if (usr_policy_res == Effect::Allow)
3496 break;
3497 }
3498 rgw::IAM::Effect e = Effect::Pass;
3499 if (policy) {
3500 e = policy->eval(s->env, *s->auth.identity,
3501 cs_object->get_instance().empty() ?
3502 rgw::IAM::s3GetObject :
3503 rgw::IAM::s3GetObjectVersion,
3504 rgw::ARN(cs_object->get_obj()));
3505 }
3506 if (e == Effect::Deny) {
3507 return -EACCES;
3508 } else if (usr_policy_res == Effect::Pass && e == Effect::Pass &&
3509 !cs_acl.verify_permission(this, *s->auth.identity, s->perm_mask,
3510 RGW_PERM_READ)) {
3511 return -EACCES;
3512 }
3513 } else if (!cs_acl.verify_permission(this, *s->auth.identity, s->perm_mask,
3514 RGW_PERM_READ)) {
3515 return -EACCES;
3516 }
3517 }
3518 }
3519
3520 if (s->bucket_access_conf && s->bucket_access_conf->block_public_acls()) {
3521 if (s->canned_acl.compare("public-read") ||
3522 s->canned_acl.compare("public-read-write") ||
3523 s->canned_acl.compare("authenticated-read"))
3524 return -EACCES;
3525 }
3526
3527 auto op_ret = get_params(y);
3528 if (op_ret < 0) {
3529 ldpp_dout(this, 20) << "get_params() returned ret=" << op_ret << dendl;
3530 return op_ret;
3531 }
3532
3533 if (s->iam_policy || ! s->iam_user_policies.empty()) {
3534 rgw_add_grant_to_iam_environment(s->env, s);
3535
3536 rgw_add_to_iam_environment(s->env, "s3:x-amz-acl", s->canned_acl);
3537
3538 if (obj_tags != nullptr && obj_tags->count() > 0){
3539 auto tags = obj_tags->get_tags();
3540 for (const auto& kv: tags){
3541 rgw_add_to_iam_environment(s->env, "s3:RequestObjectTag/"+kv.first, kv.second);
3542 }
3543 }
3544
3545 constexpr auto encrypt_attr = "x-amz-server-side-encryption";
3546 constexpr auto s3_encrypt_attr = "s3:x-amz-server-side-encryption";
3547 auto enc_header = s->info.x_meta_map.find(encrypt_attr);
3548 if (enc_header != s->info.x_meta_map.end()){
3549 rgw_add_to_iam_environment(s->env, s3_encrypt_attr, enc_header->second);
3550 }
3551
3552 constexpr auto kms_attr = "x-amz-server-side-encryption-aws-kms-key-id";
3553 constexpr auto s3_kms_attr = "s3:x-amz-server-side-encryption-aws-kms-key-id";
3554 auto kms_header = s->info.x_meta_map.find(kms_attr);
3555 if (kms_header != s->info.x_meta_map.end()){
3556 rgw_add_to_iam_environment(s->env, s3_kms_attr, kms_header->second);
3557 }
3558
3559 /* Object needs a bucket from this point */
3560 s->object->set_bucket(s->bucket.get());
3561
3562 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
3563 boost::none,
3564 rgw::IAM::s3PutObject,
3565 s->object->get_obj());
3566 if (identity_policy_res == Effect::Deny)
3567 return -EACCES;
3568
3569 rgw::IAM::Effect e = Effect::Pass;
3570 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
3571 if (s->iam_policy) {
3572 e = s->iam_policy->eval(s->env, *s->auth.identity,
3573 rgw::IAM::s3PutObject,
3574 s->object->get_obj(),
3575 princ_type);
3576 }
3577 if (e == Effect::Deny) {
3578 return -EACCES;
3579 }
3580
3581 if (!s->session_policies.empty()) {
3582 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
3583 boost::none,
3584 rgw::IAM::s3PutObject,
3585 s->object->get_obj());
3586 if (session_policy_res == Effect::Deny) {
3587 return -EACCES;
3588 }
3589 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
3590 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
3591 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) ||
3592 (session_policy_res == Effect::Allow && e == Effect::Allow))
3593 return 0;
3594 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
3595 //Intersection of session policy and identity policy plus bucket policy
3596 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) || e == Effect::Allow)
3597 return 0;
3598 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
3599 if (session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow)
3600 return 0;
3601 }
3602 return -EACCES;
3603 }
3604 if (e == Effect::Allow || identity_policy_res == Effect::Allow) {
3605 return 0;
3606 }
3607 }
3608
3609 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
3610 return -EACCES;
3611 }
3612
3613 return 0;
3614 }
3615
3616
3617 void RGWPutObj::pre_exec()
3618 {
3619 rgw_bucket_object_pre_exec(s);
3620 }
3621
3622 class RGWPutObj_CB : public RGWGetObj_Filter
3623 {
3624 RGWPutObj *op;
3625 public:
3626 explicit RGWPutObj_CB(RGWPutObj *_op) : op(_op) {}
3627 ~RGWPutObj_CB() override {}
3628
3629 int handle_data(bufferlist& bl, off_t bl_ofs, off_t bl_len) override {
3630 return op->get_data_cb(bl, bl_ofs, bl_len);
3631 }
3632 };
3633
3634 int RGWPutObj::get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len)
3635 {
3636 bufferlist bl_tmp;
3637 bl.begin(bl_ofs).copy(bl_len, bl_tmp);
3638
3639 bl_aux.append(bl_tmp);
3640
3641 return bl_len;
3642 }
3643
3644 int RGWPutObj::get_data(const off_t fst, const off_t lst, bufferlist& bl)
3645 {
3646 RGWPutObj_CB cb(this);
3647 RGWGetObj_Filter* filter = &cb;
3648 boost::optional<RGWGetObj_Decompress> decompress;
3649 std::unique_ptr<RGWGetObj_Filter> decrypt;
3650 RGWCompressionInfo cs_info;
3651 map<string, bufferlist> attrs;
3652 int ret = 0;
3653
3654 uint64_t obj_size;
3655 int64_t new_ofs, new_end;
3656
3657 new_ofs = fst;
3658 new_end = lst;
3659
3660 std::unique_ptr<rgw::sal::RGWBucket> bucket;
3661 ret = store->get_bucket(nullptr, copy_source_bucket_info, &bucket);
3662 if (ret < 0)
3663 return ret;
3664
3665 std::unique_ptr<rgw::sal::RGWObject> obj = bucket->get_object(rgw_obj_key(copy_source_object_name, copy_source_version_id));
3666 std::unique_ptr<rgw::sal::RGWObject::ReadOp> read_op(obj->get_read_op(s->obj_ctx));
3667
3668 ret = read_op->prepare(s->yield, this);
3669 if (ret < 0)
3670 return ret;
3671
3672 obj_size = obj->get_obj_size();
3673
3674 bool need_decompress;
3675 op_ret = rgw_compression_info_from_attrset(obj->get_attrs(), need_decompress, cs_info);
3676 if (op_ret < 0) {
3677 ldpp_dout(this, 0) << "ERROR: failed to decode compression info" << dendl;
3678 return -EIO;
3679 }
3680
3681 bool partial_content = true;
3682 if (need_decompress)
3683 {
3684 obj_size = cs_info.orig_size;
3685 decompress.emplace(s->cct, &cs_info, partial_content, filter);
3686 filter = &*decompress;
3687 }
3688
3689 auto attr_iter = obj->get_attrs().find(RGW_ATTR_MANIFEST);
3690 op_ret = this->get_decrypt_filter(&decrypt,
3691 filter,
3692 obj->get_attrs(),
3693 attr_iter != obj->get_attrs().end() ? &(attr_iter->second) : nullptr);
3694 if (decrypt != nullptr) {
3695 filter = decrypt.get();
3696 }
3697 if (op_ret < 0) {
3698 return op_ret;
3699 }
3700
3701 ret = obj->range_to_ofs(obj_size, new_ofs, new_end);
3702 if (ret < 0)
3703 return ret;
3704
3705 filter->fixup_range(new_ofs, new_end);
3706 ret = read_op->iterate(this, new_ofs, new_end, filter, s->yield);
3707
3708 if (ret >= 0)
3709 ret = filter->flush();
3710
3711 bl.claim_append(bl_aux);
3712
3713 return ret;
3714 }
3715
3716 // special handling for compression type = "random" with multipart uploads
3717 static CompressorRef get_compressor_plugin(const req_state *s,
3718 const std::string& compression_type)
3719 {
3720 if (compression_type != "random") {
3721 return Compressor::create(s->cct, compression_type);
3722 }
3723
3724 bool is_multipart{false};
3725 const auto& upload_id = s->info.args.get("uploadId", &is_multipart);
3726
3727 if (!is_multipart) {
3728 return Compressor::create(s->cct, compression_type);
3729 }
3730
3731 // use a hash of the multipart upload id so all parts use the same plugin
3732 const auto alg = std::hash<std::string>{}(upload_id) % Compressor::COMP_ALG_LAST;
3733 if (alg == Compressor::COMP_ALG_NONE) {
3734 return nullptr;
3735 }
3736 return Compressor::create(s->cct, alg);
3737 }
3738
3739 void RGWPutObj::execute(optional_yield y)
3740 {
3741 char supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1];
3742 char supplied_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1];
3743 char calc_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1];
3744 unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE];
3745 MD5 hash;
3746 bufferlist bl, aclbl, bs;
3747 int len;
3748
3749 off_t fst;
3750 off_t lst;
3751
3752 bool need_calc_md5 = (dlo_manifest == NULL) && (slo_info == NULL);
3753 perfcounter->inc(l_rgw_put);
3754 // report latency on return
3755 auto put_lat = make_scope_guard([&] {
3756 perfcounter->tinc(l_rgw_put_lat, s->time_elapsed());
3757 });
3758
3759 op_ret = -EINVAL;
3760 if (rgw::sal::RGWObject::empty(s->object.get())) {
3761 return;
3762 }
3763
3764 if (!s->bucket_exists) {
3765 op_ret = -ERR_NO_SUCH_BUCKET;
3766 return;
3767 }
3768
3769 /* Object needs a bucket from this point */
3770 s->object->set_bucket(s->bucket.get());
3771
3772 op_ret = get_system_versioning_params(s, &olh_epoch, &version_id);
3773 if (op_ret < 0) {
3774 ldpp_dout(this, 20) << "get_system_versioning_params() returned ret="
3775 << op_ret << dendl;
3776 return;
3777 }
3778
3779 if (supplied_md5_b64) {
3780 need_calc_md5 = true;
3781
3782 ldpp_dout(this, 15) << "supplied_md5_b64=" << supplied_md5_b64 << dendl;
3783 op_ret = ceph_unarmor(supplied_md5_bin, &supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1],
3784 supplied_md5_b64, supplied_md5_b64 + strlen(supplied_md5_b64));
3785 ldpp_dout(this, 15) << "ceph_armor ret=" << op_ret << dendl;
3786 if (op_ret != CEPH_CRYPTO_MD5_DIGESTSIZE) {
3787 op_ret = -ERR_INVALID_DIGEST;
3788 return;
3789 }
3790
3791 buf_to_hex((const unsigned char *)supplied_md5_bin, CEPH_CRYPTO_MD5_DIGESTSIZE, supplied_md5);
3792 ldpp_dout(this, 15) << "supplied_md5=" << supplied_md5 << dendl;
3793 }
3794
3795 if (!chunked_upload) { /* with chunked upload we don't know how big is the upload.
3796 we also check sizes at the end anyway */
3797 op_ret = s->bucket->check_quota(user_quota, bucket_quota, s->content_length, y);
3798 if (op_ret < 0) {
3799 ldpp_dout(this, 20) << "check_quota() returned ret=" << op_ret << dendl;
3800 return;
3801 }
3802 }
3803
3804 if (supplied_etag) {
3805 strncpy(supplied_md5, supplied_etag, sizeof(supplied_md5) - 1);
3806 supplied_md5[sizeof(supplied_md5) - 1] = '\0';
3807 }
3808
3809 const bool multipart = !multipart_upload_id.empty();
3810 auto& obj_ctx = *static_cast<RGWObjectCtx*>(s->obj_ctx);
3811
3812 /* Handle object versioning of Swift API. */
3813 if (! multipart) {
3814 op_ret = s->object->swift_versioning_copy(s->obj_ctx, this, s->yield);
3815 if (op_ret < 0) {
3816 return;
3817 }
3818 }
3819
3820 // make reservation for notification if needed
3821 rgw::notify::reservation_t res(this, store, s, s->object.get());
3822 const auto event_type = rgw::notify::ObjectCreatedPut;
3823 op_ret = rgw::notify::publish_reserve(this, event_type, res, obj_tags.get());
3824 if (op_ret < 0) {
3825 return;
3826 }
3827
3828 // create the object processor
3829 auto aio = rgw::make_throttle(s->cct->_conf->rgw_put_obj_min_window_size,
3830 s->yield);
3831 using namespace rgw::putobj;
3832 constexpr auto max_processor_size = std::max({sizeof(MultipartObjectProcessor),
3833 sizeof(AtomicObjectProcessor),
3834 sizeof(AppendObjectProcessor)});
3835 ceph::static_ptr<ObjectProcessor, max_processor_size> processor;
3836
3837 rgw_placement_rule *pdest_placement;
3838
3839 multipart_upload_info upload_info;
3840 if (multipart) {
3841 RGWMPObj mp(s->object->get_name(), multipart_upload_id);
3842
3843 op_ret = get_multipart_info(this, s, mp.get_meta(), &upload_info);
3844 if (op_ret < 0) {
3845 if (op_ret != -ENOENT) {
3846 ldpp_dout(this, 0) << "ERROR: get_multipart_info returned " << op_ret << ": " << cpp_strerror(-op_ret) << dendl;
3847 } else {// -ENOENT: raced with upload complete/cancel, no need to spam log
3848 ldpp_dout(this, 20) << "failed to get multipart info (returned " << op_ret << ": " << cpp_strerror(-op_ret) << "): probably raced with upload complete / cancel" << dendl;
3849 }
3850 return;
3851 }
3852 pdest_placement = &upload_info.dest_placement;
3853 ldpp_dout(this, 20) << "dest_placement for part=" << upload_info.dest_placement << dendl;
3854 processor.emplace<MultipartObjectProcessor>(
3855 &*aio, store, s->bucket.get(), pdest_placement,
3856 s->owner.get_id(), obj_ctx, s->object->clone(),
3857 multipart_upload_id, multipart_part_num, multipart_part_str,
3858 this, s->yield);
3859 } else if(append) {
3860 if (s->bucket->versioned()) {
3861 op_ret = -ERR_INVALID_BUCKET_STATE;
3862 return;
3863 }
3864 pdest_placement = &s->dest_placement;
3865 processor.emplace<AppendObjectProcessor>(
3866 &*aio, store, s->bucket.get(), pdest_placement, s->bucket_owner.get_id(),
3867 obj_ctx, s->object->clone(),
3868 s->req_id, position, &cur_accounted_size, this, s->yield);
3869 } else {
3870 if (s->bucket->versioning_enabled()) {
3871 if (!version_id.empty()) {
3872 s->object->set_instance(version_id);
3873 } else {
3874 s->object->gen_rand_obj_instance_name();
3875 version_id = s->object->get_instance();
3876 }
3877 }
3878 pdest_placement = &s->dest_placement;
3879 processor.emplace<AtomicObjectProcessor>(
3880 &*aio, store, s->bucket.get(), pdest_placement,
3881 s->bucket_owner.get_id(), obj_ctx, s->object->clone(),
3882 olh_epoch, s->req_id, this, s->yield);
3883 }
3884
3885 op_ret = processor->prepare(s->yield);
3886 if (op_ret < 0) {
3887 ldpp_dout(this, 20) << "processor->prepare() returned ret=" << op_ret
3888 << dendl;
3889 return;
3890 }
3891
3892 if ((! copy_source.empty()) && !copy_source_range) {
3893 rgw::sal::RGWRadosObject obj(store, rgw_obj_key(copy_source_object_name, copy_source_version_id));
3894 rgw::sal::RGWRadosBucket bucket(store, copy_source_bucket_info);
3895
3896 RGWObjState *astate;
3897 op_ret = obj.get_obj_state(this, &obj_ctx, bucket, &astate, s->yield);
3898 if (op_ret < 0) {
3899 ldpp_dout(this, 0) << "ERROR: get copy source obj state returned with error" << op_ret << dendl;
3900 return;
3901 }
3902 if (!astate->exists){
3903 op_ret = -ENOENT;
3904 return;
3905 }
3906 lst = astate->accounted_size - 1;
3907 } else {
3908 lst = copy_source_range_lst;
3909 }
3910
3911 fst = copy_source_range_fst;
3912
3913 // no filters by default
3914 DataProcessor *filter = processor.get();
3915
3916 const auto& compression_type = store->svc()->zone->get_zone_params().get_compression_type(*pdest_placement);
3917 CompressorRef plugin;
3918 boost::optional<RGWPutObj_Compress> compressor;
3919
3920 std::unique_ptr<DataProcessor> encrypt;
3921
3922 if (!append) { // compression and encryption only apply to full object uploads
3923 op_ret = get_encrypt_filter(&encrypt, filter);
3924 if (op_ret < 0) {
3925 return;
3926 }
3927 if (encrypt != nullptr) {
3928 filter = &*encrypt;
3929 } else if (compression_type != "none") {
3930 plugin = get_compressor_plugin(s, compression_type);
3931 if (!plugin) {
3932 ldpp_dout(this, 1) << "Cannot load plugin for compression type "
3933 << compression_type << dendl;
3934 } else {
3935 compressor.emplace(s->cct, plugin, filter);
3936 filter = &*compressor;
3937 }
3938 }
3939 }
3940 tracepoint(rgw_op, before_data_transfer, s->req_id.c_str());
3941 do {
3942 bufferlist data;
3943 if (fst > lst)
3944 break;
3945 if (copy_source.empty()) {
3946 len = get_data(data);
3947 } else {
3948 uint64_t cur_lst = min(fst + s->cct->_conf->rgw_max_chunk_size - 1, lst);
3949 op_ret = get_data(fst, cur_lst, data);
3950 if (op_ret < 0)
3951 return;
3952 len = data.length();
3953 s->content_length += len;
3954 fst += len;
3955 }
3956 if (len < 0) {
3957 op_ret = len;
3958 ldpp_dout(this, 20) << "get_data() returned ret=" << op_ret << dendl;
3959 return;
3960 } else if (len == 0) {
3961 break;
3962 }
3963
3964 if (need_calc_md5) {
3965 hash.Update((const unsigned char *)data.c_str(), data.length());
3966 }
3967
3968 /* update torrrent */
3969 torrent.update(data);
3970
3971 op_ret = filter->process(std::move(data), ofs);
3972 if (op_ret < 0) {
3973 ldpp_dout(this, 20) << "processor->process() returned ret="
3974 << op_ret << dendl;
3975 return;
3976 }
3977
3978 ofs += len;
3979 } while (len > 0);
3980 tracepoint(rgw_op, after_data_transfer, s->req_id.c_str(), ofs);
3981
3982 // flush any data in filters
3983 op_ret = filter->process({}, ofs);
3984 if (op_ret < 0) {
3985 return;
3986 }
3987
3988 if (!chunked_upload && ofs != s->content_length) {
3989 op_ret = -ERR_REQUEST_TIMEOUT;
3990 return;
3991 }
3992 s->obj_size = ofs;
3993 s->object->set_obj_size(ofs);
3994
3995 perfcounter->inc(l_rgw_put_b, s->obj_size);
3996
3997 op_ret = do_aws4_auth_completion();
3998 if (op_ret < 0) {
3999 return;
4000 }
4001
4002 op_ret = s->bucket->check_quota(user_quota, bucket_quota, s->obj_size, y);
4003 if (op_ret < 0) {
4004 ldpp_dout(this, 20) << "second check_quota() returned op_ret=" << op_ret << dendl;
4005 return;
4006 }
4007
4008 hash.Final(m);
4009
4010 if (compressor && compressor->is_compressed()) {
4011 bufferlist tmp;
4012 RGWCompressionInfo cs_info;
4013 cs_info.compression_type = plugin->get_type_name();
4014 cs_info.orig_size = s->obj_size;
4015 cs_info.compressor_message = compressor->get_compressor_message();
4016 cs_info.blocks = move(compressor->get_compression_blocks());
4017 encode(cs_info, tmp);
4018 attrs[RGW_ATTR_COMPRESSION] = tmp;
4019 ldpp_dout(this, 20) << "storing " << RGW_ATTR_COMPRESSION
4020 << " with type=" << cs_info.compression_type
4021 << ", orig_size=" << cs_info.orig_size
4022 << ", blocks=" << cs_info.blocks.size() << dendl;
4023 }
4024
4025 buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, calc_md5);
4026
4027 etag = calc_md5;
4028
4029 if (supplied_md5_b64 && strcmp(calc_md5, supplied_md5)) {
4030 op_ret = -ERR_BAD_DIGEST;
4031 return;
4032 }
4033
4034 policy.encode(aclbl);
4035 emplace_attr(RGW_ATTR_ACL, std::move(aclbl));
4036
4037 if (dlo_manifest) {
4038 op_ret = encode_dlo_manifest_attr(dlo_manifest, attrs);
4039 if (op_ret < 0) {
4040 ldpp_dout(this, 0) << "bad user manifest: " << dlo_manifest << dendl;
4041 return;
4042 }
4043 }
4044
4045 if (slo_info) {
4046 bufferlist manifest_bl;
4047 encode(*slo_info, manifest_bl);
4048 emplace_attr(RGW_ATTR_SLO_MANIFEST, std::move(manifest_bl));
4049 }
4050
4051 if (supplied_etag && etag.compare(supplied_etag) != 0) {
4052 op_ret = -ERR_UNPROCESSABLE_ENTITY;
4053 return;
4054 }
4055 bl.append(etag.c_str(), etag.size());
4056 emplace_attr(RGW_ATTR_ETAG, std::move(bl));
4057
4058 populate_with_generic_attrs(s, attrs);
4059 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs);
4060 if (op_ret < 0) {
4061 return;
4062 }
4063 encode_delete_at_attr(delete_at, attrs);
4064 encode_obj_tags_attr(obj_tags.get(), attrs);
4065 rgw_cond_decode_objtags(s, attrs);
4066
4067 /* Add a custom metadata to expose the information whether an object
4068 * is an SLO or not. Appending the attribute must be performed AFTER
4069 * processing any input from user in order to prohibit overwriting. */
4070 if (slo_info) {
4071 bufferlist slo_userindicator_bl;
4072 slo_userindicator_bl.append("True", 4);
4073 emplace_attr(RGW_ATTR_SLO_UINDICATOR, std::move(slo_userindicator_bl));
4074 }
4075 if (obj_legal_hold) {
4076 bufferlist obj_legal_hold_bl;
4077 obj_legal_hold->encode(obj_legal_hold_bl);
4078 emplace_attr(RGW_ATTR_OBJECT_LEGAL_HOLD, std::move(obj_legal_hold_bl));
4079 }
4080 if (obj_retention) {
4081 bufferlist obj_retention_bl;
4082 obj_retention->encode(obj_retention_bl);
4083 emplace_attr(RGW_ATTR_OBJECT_RETENTION, std::move(obj_retention_bl));
4084 }
4085
4086 tracepoint(rgw_op, processor_complete_enter, s->req_id.c_str());
4087 op_ret = processor->complete(s->obj_size, etag, &mtime, real_time(), attrs,
4088 (delete_at ? *delete_at : real_time()), if_match, if_nomatch,
4089 (user_data.empty() ? nullptr : &user_data), nullptr, nullptr,
4090 s->yield);
4091 tracepoint(rgw_op, processor_complete_exit, s->req_id.c_str());
4092
4093 /* produce torrent */
4094 if (s->cct->_conf->rgw_torrent_flag && (ofs == torrent.get_data_len()))
4095 {
4096 torrent.init(s, store);
4097 torrent.set_create_date(mtime);
4098 op_ret = torrent.complete(y);
4099 if (0 != op_ret)
4100 {
4101 ldpp_dout(this, 0) << "ERROR: torrent.handle_data() returned " << op_ret << dendl;
4102 return;
4103 }
4104 }
4105
4106 // send request to notification manager
4107 const auto ret = rgw::notify::publish_commit(s->object.get(), s->obj_size, mtime, etag, event_type, res, this);
4108 if (ret < 0) {
4109 ldpp_dout(this, 1) << "ERROR: publishing notification failed, with error: " << ret << dendl;
4110 // too late to rollback operation, hence op_ret is not set here
4111 }
4112 }
4113
4114 int RGWPostObj::verify_permission(optional_yield y)
4115 {
4116 return 0;
4117 }
4118
4119 void RGWPostObj::pre_exec()
4120 {
4121 rgw_bucket_object_pre_exec(s);
4122 }
4123
4124 void RGWPostObj::execute(optional_yield y)
4125 {
4126 boost::optional<RGWPutObj_Compress> compressor;
4127 CompressorRef plugin;
4128 char supplied_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1];
4129
4130 /* Read in the data from the POST form. */
4131 op_ret = get_params(y);
4132 if (op_ret < 0) {
4133 return;
4134 }
4135
4136 op_ret = verify_params();
4137 if (op_ret < 0) {
4138 return;
4139 }
4140
4141 if (s->iam_policy || ! s->iam_user_policies.empty() || !s->session_policies.empty()) {
4142 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
4143 boost::none,
4144 rgw::IAM::s3PutObject,
4145 s->object->get_obj());
4146 if (identity_policy_res == Effect::Deny) {
4147 op_ret = -EACCES;
4148 return;
4149 }
4150
4151 rgw::IAM::Effect e = Effect::Pass;
4152 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
4153 if (s->iam_policy) {
4154 e = s->iam_policy->eval(s->env, *s->auth.identity,
4155 rgw::IAM::s3PutObject,
4156 s->object->get_obj(),
4157 princ_type);
4158 }
4159 if (e == Effect::Deny) {
4160 op_ret = -EACCES;
4161 return;
4162 }
4163
4164 if (!s->session_policies.empty()) {
4165 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
4166 boost::none,
4167 rgw::IAM::s3PutObject,
4168 s->object->get_obj());
4169 if (session_policy_res == Effect::Deny) {
4170 op_ret = -EACCES;
4171 return;
4172 }
4173 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
4174 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
4175 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) ||
4176 (session_policy_res == Effect::Allow && e == Effect::Allow)) {
4177 op_ret = 0;
4178 return;
4179 }
4180 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
4181 //Intersection of session policy and identity policy plus bucket policy
4182 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) || e == Effect::Allow) {
4183 op_ret = 0;
4184 return;
4185 }
4186 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
4187 if (session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) {
4188 op_ret = 0;
4189 return;
4190 }
4191 }
4192 op_ret = -EACCES;
4193 return;
4194 }
4195 if (identity_policy_res == Effect::Pass && e == Effect::Pass && !verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
4196 op_ret = -EACCES;
4197 return;
4198 }
4199 } else if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
4200 op_ret = -EACCES;
4201 return;
4202 }
4203
4204 // make reservation for notification if needed
4205 rgw::notify::reservation_t res(this, store, s, s->object.get());
4206 const auto event_type = rgw::notify::ObjectCreatedPost;
4207 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
4208 if (op_ret < 0) {
4209 return;
4210 }
4211
4212 /* Start iteration over data fields. It's necessary as Swift's FormPost
4213 * is capable to handle multiple files in single form. */
4214 do {
4215 char calc_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1];
4216 unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE];
4217 MD5 hash;
4218 ceph::buffer::list bl, aclbl;
4219 int len = 0;
4220
4221 op_ret = s->bucket->check_quota(user_quota, bucket_quota, s->content_length, y);
4222 if (op_ret < 0) {
4223 return;
4224 }
4225
4226 if (supplied_md5_b64) {
4227 char supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1];
4228 ldpp_dout(this, 15) << "supplied_md5_b64=" << supplied_md5_b64 << dendl;
4229 op_ret = ceph_unarmor(supplied_md5_bin, &supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1],
4230 supplied_md5_b64, supplied_md5_b64 + strlen(supplied_md5_b64));
4231 ldpp_dout(this, 15) << "ceph_armor ret=" << op_ret << dendl;
4232 if (op_ret != CEPH_CRYPTO_MD5_DIGESTSIZE) {
4233 op_ret = -ERR_INVALID_DIGEST;
4234 return;
4235 }
4236
4237 buf_to_hex((const unsigned char *)supplied_md5_bin, CEPH_CRYPTO_MD5_DIGESTSIZE, supplied_md5);
4238 ldpp_dout(this, 15) << "supplied_md5=" << supplied_md5 << dendl;
4239 }
4240
4241 std::unique_ptr<rgw::sal::RGWObject> obj =
4242 s->bucket->get_object(rgw_obj_key(get_current_filename()));
4243 if (s->bucket->versioning_enabled()) {
4244 obj->gen_rand_obj_instance_name();
4245 }
4246
4247 auto aio = rgw::make_throttle(s->cct->_conf->rgw_put_obj_min_window_size,
4248 s->yield);
4249
4250 using namespace rgw::putobj;
4251 AtomicObjectProcessor processor(&*aio, store, s->bucket.get(),
4252 &s->dest_placement,
4253 s->bucket_owner.get_id(),
4254 *static_cast<RGWObjectCtx*>(s->obj_ctx),
4255 std::move(obj), 0, s->req_id, this, s->yield);
4256 op_ret = processor.prepare(s->yield);
4257 if (op_ret < 0) {
4258 return;
4259 }
4260
4261 /* No filters by default. */
4262 DataProcessor *filter = &processor;
4263
4264 std::unique_ptr<DataProcessor> encrypt;
4265 op_ret = get_encrypt_filter(&encrypt, filter);
4266 if (op_ret < 0) {
4267 return;
4268 }
4269 if (encrypt != nullptr) {
4270 filter = encrypt.get();
4271 } else {
4272 const auto& compression_type = store->svc()->zone->get_zone_params().get_compression_type(
4273 s->dest_placement);
4274 if (compression_type != "none") {
4275 plugin = Compressor::create(s->cct, compression_type);
4276 if (!plugin) {
4277 ldpp_dout(this, 1) << "Cannot load plugin for compression type "
4278 << compression_type << dendl;
4279 } else {
4280 compressor.emplace(s->cct, plugin, filter);
4281 filter = &*compressor;
4282 }
4283 }
4284 }
4285
4286 bool again;
4287 do {
4288 ceph::bufferlist data;
4289 len = get_data(data, again);
4290
4291 if (len < 0) {
4292 op_ret = len;
4293 return;
4294 }
4295
4296 if (!len) {
4297 break;
4298 }
4299
4300 hash.Update((const unsigned char *)data.c_str(), data.length());
4301 op_ret = filter->process(std::move(data), ofs);
4302
4303 ofs += len;
4304
4305 if (ofs > max_len) {
4306 op_ret = -ERR_TOO_LARGE;
4307 return;
4308 }
4309 } while (again);
4310
4311 // flush
4312 op_ret = filter->process({}, ofs);
4313 if (op_ret < 0) {
4314 return;
4315 }
4316
4317 if (len < min_len) {
4318 op_ret = -ERR_TOO_SMALL;
4319 return;
4320 }
4321
4322 s->obj_size = ofs;
4323 s->object->set_obj_size(ofs);
4324
4325
4326 op_ret = s->bucket->check_quota(user_quota, bucket_quota, s->obj_size, y);
4327 if (op_ret < 0) {
4328 return;
4329 }
4330
4331 hash.Final(m);
4332 buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, calc_md5);
4333
4334 etag = calc_md5;
4335
4336 if (supplied_md5_b64 && strcmp(calc_md5, supplied_md5)) {
4337 op_ret = -ERR_BAD_DIGEST;
4338 return;
4339 }
4340
4341 bl.append(etag.c_str(), etag.size());
4342 emplace_attr(RGW_ATTR_ETAG, std::move(bl));
4343
4344 policy.encode(aclbl);
4345 emplace_attr(RGW_ATTR_ACL, std::move(aclbl));
4346
4347 const std::string content_type = get_current_content_type();
4348 if (! content_type.empty()) {
4349 ceph::bufferlist ct_bl;
4350 ct_bl.append(content_type.c_str(), content_type.size() + 1);
4351 emplace_attr(RGW_ATTR_CONTENT_TYPE, std::move(ct_bl));
4352 }
4353
4354 if (compressor && compressor->is_compressed()) {
4355 ceph::bufferlist tmp;
4356 RGWCompressionInfo cs_info;
4357 cs_info.compression_type = plugin->get_type_name();
4358 cs_info.orig_size = s->obj_size;
4359 cs_info.compressor_message = compressor->get_compressor_message();
4360 cs_info.blocks = move(compressor->get_compression_blocks());
4361 encode(cs_info, tmp);
4362 emplace_attr(RGW_ATTR_COMPRESSION, std::move(tmp));
4363 }
4364
4365 op_ret = processor.complete(s->obj_size, etag, nullptr, real_time(), attrs,
4366 (delete_at ? *delete_at : real_time()),
4367 nullptr, nullptr, nullptr, nullptr, nullptr,
4368 s->yield);
4369 if (op_ret < 0) {
4370 return;
4371 }
4372 } while (is_next_file_to_upload());
4373
4374 // send request to notification manager
4375 const auto ret = rgw::notify::publish_commit(s->object.get(), ofs, ceph::real_clock::now(), etag, event_type, res, this);
4376 if (ret < 0) {
4377 ldpp_dout(this, 1) << "ERROR: publishing notification failed, with error: " << ret << dendl;
4378 // too late to rollback operation, hence op_ret is not set here
4379 }
4380 }
4381
4382
4383 void RGWPutMetadataAccount::filter_out_temp_url(map<string, bufferlist>& add_attrs,
4384 const set<string>& rmattr_names,
4385 map<int, string>& temp_url_keys)
4386 {
4387 map<string, bufferlist>::iterator iter;
4388
4389 iter = add_attrs.find(RGW_ATTR_TEMPURL_KEY1);
4390 if (iter != add_attrs.end()) {
4391 temp_url_keys[0] = iter->second.c_str();
4392 add_attrs.erase(iter);
4393 }
4394
4395 iter = add_attrs.find(RGW_ATTR_TEMPURL_KEY2);
4396 if (iter != add_attrs.end()) {
4397 temp_url_keys[1] = iter->second.c_str();
4398 add_attrs.erase(iter);
4399 }
4400
4401 for (const string& name : rmattr_names) {
4402 if (name.compare(RGW_ATTR_TEMPURL_KEY1) == 0) {
4403 temp_url_keys[0] = string();
4404 }
4405 if (name.compare(RGW_ATTR_TEMPURL_KEY2) == 0) {
4406 temp_url_keys[1] = string();
4407 }
4408 }
4409 }
4410
4411 int RGWPutMetadataAccount::init_processing(optional_yield y)
4412 {
4413 /* First, go to the base class. At the time of writing the method was
4414 * responsible only for initializing the quota. This isn't necessary
4415 * here as we are touching metadata only. I'm putting this call only
4416 * for the future. */
4417 op_ret = RGWOp::init_processing(y);
4418 if (op_ret < 0) {
4419 return op_ret;
4420 }
4421
4422 op_ret = get_params(y);
4423 if (op_ret < 0) {
4424 return op_ret;
4425 }
4426
4427 op_ret = store->ctl()->user->get_attrs_by_uid(this, s->user->get_id(), &orig_attrs,
4428 s->yield,
4429 &acct_op_tracker);
4430 if (op_ret < 0) {
4431 return op_ret;
4432 }
4433
4434 if (has_policy) {
4435 bufferlist acl_bl;
4436 policy.encode(acl_bl);
4437 attrs.emplace(RGW_ATTR_ACL, std::move(acl_bl));
4438 }
4439
4440 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs, false);
4441 if (op_ret < 0) {
4442 return op_ret;
4443 }
4444 prepare_add_del_attrs(orig_attrs, rmattr_names, attrs);
4445 populate_with_generic_attrs(s, attrs);
4446
4447 /* Try extract the TempURL-related stuff now to allow verify_permission
4448 * evaluate whether we need FULL_CONTROL or not. */
4449 filter_out_temp_url(attrs, rmattr_names, temp_url_keys);
4450
4451 /* The same with quota except a client needs to be reseller admin. */
4452 op_ret = filter_out_quota_info(attrs, rmattr_names, new_quota,
4453 &new_quota_extracted);
4454 if (op_ret < 0) {
4455 return op_ret;
4456 }
4457
4458 return 0;
4459 }
4460
4461 int RGWPutMetadataAccount::verify_permission(optional_yield y)
4462 {
4463 if (s->auth.identity->is_anonymous()) {
4464 return -EACCES;
4465 }
4466
4467 if (!verify_user_permission_no_policy(this, s, RGW_PERM_WRITE)) {
4468 return -EACCES;
4469 }
4470
4471 /* Altering TempURL keys requires FULL_CONTROL. */
4472 if (!temp_url_keys.empty() && s->perm_mask != RGW_PERM_FULL_CONTROL) {
4473 return -EPERM;
4474 }
4475
4476 /* We are failing this intensionally to allow system user/reseller admin
4477 * override in rgw_process.cc. This is the way to specify a given RGWOp
4478 * expect extra privileges. */
4479 if (new_quota_extracted) {
4480 return -EACCES;
4481 }
4482
4483 return 0;
4484 }
4485
4486 void RGWPutMetadataAccount::execute(optional_yield y)
4487 {
4488 /* Params have been extracted earlier. See init_processing(). */
4489 RGWUserInfo new_uinfo;
4490 op_ret = store->ctl()->user->get_info_by_uid(this, s->user->get_id(), &new_uinfo, s->yield,
4491 RGWUserCtl::GetParams()
4492 .set_objv_tracker(&acct_op_tracker));
4493 if (op_ret < 0) {
4494 return;
4495 }
4496
4497 /* Handle the TempURL-related stuff. */
4498 if (!temp_url_keys.empty()) {
4499 for (auto& pair : temp_url_keys) {
4500 new_uinfo.temp_url_keys[pair.first] = std::move(pair.second);
4501 }
4502 }
4503
4504 /* Handle the quota extracted at the verify_permission step. */
4505 if (new_quota_extracted) {
4506 new_uinfo.user_quota = std::move(new_quota);
4507 }
4508
4509 /* We are passing here the current (old) user info to allow the function
4510 * optimize-out some operations. */
4511 op_ret = store->ctl()->user->store_info(this, new_uinfo, s->yield,
4512 RGWUserCtl::PutParams()
4513 .set_old_info(&s->user->get_info())
4514 .set_objv_tracker(&acct_op_tracker)
4515 .set_attrs(&attrs));
4516 }
4517
4518 int RGWPutMetadataBucket::verify_permission(optional_yield y)
4519 {
4520 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
4521 return -EACCES;
4522 }
4523
4524 return 0;
4525 }
4526
4527 void RGWPutMetadataBucket::pre_exec()
4528 {
4529 rgw_bucket_object_pre_exec(s);
4530 }
4531
4532 void RGWPutMetadataBucket::execute(optional_yield y)
4533 {
4534 op_ret = get_params(y);
4535 if (op_ret < 0) {
4536 return;
4537 }
4538
4539 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs, false);
4540 if (op_ret < 0) {
4541 return;
4542 }
4543
4544 if (!placement_rule.empty() &&
4545 placement_rule != s->bucket->get_placement_rule()) {
4546 op_ret = -EEXIST;
4547 return;
4548 }
4549
4550 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
4551 /* Encode special metadata first as we're using std::map::emplace under
4552 * the hood. This method will add the new items only if the map doesn't
4553 * contain such keys yet. */
4554 if (has_policy) {
4555 if (s->dialect.compare("swift") == 0) {
4556 auto old_policy = \
4557 static_cast<RGWAccessControlPolicy_SWIFT*>(s->bucket_acl.get());
4558 auto new_policy = static_cast<RGWAccessControlPolicy_SWIFT*>(&policy);
4559 new_policy->filter_merge(policy_rw_mask, old_policy);
4560 policy = *new_policy;
4561 }
4562 buffer::list bl;
4563 policy.encode(bl);
4564 emplace_attr(RGW_ATTR_ACL, std::move(bl));
4565 }
4566
4567 if (has_cors) {
4568 buffer::list bl;
4569 cors_config.encode(bl);
4570 emplace_attr(RGW_ATTR_CORS, std::move(bl));
4571 }
4572
4573 /* It's supposed that following functions WILL NOT change any
4574 * special attributes (like RGW_ATTR_ACL) if they are already
4575 * present in attrs. */
4576 prepare_add_del_attrs(s->bucket_attrs, rmattr_names, attrs);
4577 populate_with_generic_attrs(s, attrs);
4578
4579 /* According to the Swift's behaviour and its container_quota
4580 * WSGI middleware implementation: anyone with write permissions
4581 * is able to set the bucket quota. This stays in contrast to
4582 * account quotas that can be set only by clients holding
4583 * reseller admin privileges. */
4584 op_ret = filter_out_quota_info(attrs, rmattr_names, s->bucket->get_info().quota);
4585 if (op_ret < 0) {
4586 return op_ret;
4587 }
4588
4589 if (swift_ver_location) {
4590 s->bucket->get_info().swift_ver_location = *swift_ver_location;
4591 s->bucket->get_info().swift_versioning = (!swift_ver_location->empty());
4592 }
4593
4594 /* Web site of Swift API. */
4595 filter_out_website(attrs, rmattr_names, s->bucket->get_info().website_conf);
4596 s->bucket->get_info().has_website = !s->bucket->get_info().website_conf.is_empty();
4597
4598 /* Setting attributes also stores the provided bucket info. Due
4599 * to this fact, the new quota settings can be serialized with
4600 * the same call. */
4601 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
4602 return op_ret;
4603 });
4604 }
4605
4606 int RGWPutMetadataObject::verify_permission(optional_yield y)
4607 {
4608 // This looks to be something specific to Swift. We could add
4609 // operations like swift:PutMetadataObject to the Policy Engine.
4610 if (!verify_object_permission_no_policy(this, s, RGW_PERM_WRITE)) {
4611 return -EACCES;
4612 }
4613
4614 return 0;
4615 }
4616
4617 void RGWPutMetadataObject::pre_exec()
4618 {
4619 rgw_bucket_object_pre_exec(s);
4620 }
4621
4622 void RGWPutMetadataObject::execute(optional_yield y)
4623 {
4624 rgw_obj target_obj;
4625 rgw::sal::RGWAttrs attrs, rmattrs;
4626
4627 s->object->set_atomic(s->obj_ctx);
4628
4629 op_ret = get_params(y);
4630 if (op_ret < 0) {
4631 return;
4632 }
4633
4634 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs);
4635 if (op_ret < 0) {
4636 return;
4637 }
4638
4639 /* check if obj exists, read orig attrs */
4640 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, s, &target_obj);
4641 if (op_ret < 0) {
4642 return;
4643 }
4644
4645 /* Check whether the object has expired. Swift API documentation
4646 * stands that we should return 404 Not Found in such case. */
4647 if (need_object_expiration() && s->object->is_expired()) {
4648 op_ret = -ENOENT;
4649 return;
4650 }
4651
4652 /* Filter currently existing attributes. */
4653 prepare_add_del_attrs(s->object->get_attrs(), attrs, rmattrs);
4654 populate_with_generic_attrs(s, attrs);
4655 encode_delete_at_attr(delete_at, attrs);
4656
4657 if (dlo_manifest) {
4658 op_ret = encode_dlo_manifest_attr(dlo_manifest, attrs);
4659 if (op_ret < 0) {
4660 ldpp_dout(this, 0) << "bad user manifest: " << dlo_manifest << dendl;
4661 return;
4662 }
4663 }
4664
4665 op_ret = s->object->set_obj_attrs(this, s->obj_ctx, &attrs, &rmattrs, s->yield, &target_obj);
4666 }
4667
4668 int RGWDeleteObj::handle_slo_manifest(bufferlist& bl, optional_yield y)
4669 {
4670 RGWSLOInfo slo_info;
4671 auto bliter = bl.cbegin();
4672 try {
4673 decode(slo_info, bliter);
4674 } catch (buffer::error& err) {
4675 ldpp_dout(this, 0) << "ERROR: failed to decode slo manifest" << dendl;
4676 return -EIO;
4677 }
4678
4679 try {
4680 deleter = std::unique_ptr<RGWBulkDelete::Deleter>(\
4681 new RGWBulkDelete::Deleter(this, store, s));
4682 } catch (const std::bad_alloc&) {
4683 return -ENOMEM;
4684 }
4685
4686 list<RGWBulkDelete::acct_path_t> items;
4687 for (const auto& iter : slo_info.entries) {
4688 const string& path_str = iter.path;
4689
4690 const size_t sep_pos = path_str.find('/', 1 /* skip first slash */);
4691 if (std::string_view::npos == sep_pos) {
4692 return -EINVAL;
4693 }
4694
4695 RGWBulkDelete::acct_path_t path;
4696
4697 path.bucket_name = url_decode(path_str.substr(1, sep_pos - 1));
4698 path.obj_key = url_decode(path_str.substr(sep_pos + 1));
4699
4700 items.push_back(path);
4701 }
4702
4703 /* Request removal of the manifest object itself. */
4704 RGWBulkDelete::acct_path_t path;
4705 path.bucket_name = s->bucket_name;
4706 path.obj_key = s->object->get_key();
4707 items.push_back(path);
4708
4709 int ret = deleter->delete_chunk(items, y);
4710 if (ret < 0) {
4711 return ret;
4712 }
4713
4714 return 0;
4715 }
4716
4717 int RGWDeleteObj::verify_permission(optional_yield y)
4718 {
4719 int op_ret = get_params(y);
4720 if (op_ret) {
4721 return op_ret;
4722 }
4723 if (s->iam_policy || ! s->iam_user_policies.empty() || ! s->session_policies.empty()) {
4724 if (s->bucket->get_info().obj_lock_enabled() && bypass_governance_mode) {
4725 auto r = eval_identity_or_session_policies(s->iam_user_policies, s->env, boost::none,
4726 rgw::IAM::s3BypassGovernanceRetention, ARN(s->bucket->get_key(), s->object->get_name()));
4727 if (r == Effect::Deny) {
4728 bypass_perm = false;
4729 } else if (r == Effect::Pass && s->iam_policy) {
4730 r = s->iam_policy->eval(s->env, *s->auth.identity, rgw::IAM::s3BypassGovernanceRetention,
4731 ARN(s->bucket->get_key(), s->object->get_name()));
4732 if (r == Effect::Deny) {
4733 bypass_perm = false;
4734 }
4735 } else if (r == Effect::Pass && !s->session_policies.empty()) {
4736 r = eval_identity_or_session_policies(s->session_policies, s->env, boost::none,
4737 rgw::IAM::s3BypassGovernanceRetention, ARN(s->bucket->get_key(), s->object->get_name()));
4738 if (r == Effect::Deny) {
4739 bypass_perm = false;
4740 }
4741 }
4742 }
4743 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
4744 boost::none,
4745 s->object->get_instance().empty() ?
4746 rgw::IAM::s3DeleteObject :
4747 rgw::IAM::s3DeleteObjectVersion,
4748 ARN(s->bucket->get_key(), s->object->get_name()));
4749 if (identity_policy_res == Effect::Deny) {
4750 return -EACCES;
4751 }
4752
4753 rgw::IAM::Effect r = Effect::Pass;
4754 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
4755 if (s->iam_policy) {
4756 r = s->iam_policy->eval(s->env, *s->auth.identity,
4757 s->object->get_instance().empty() ?
4758 rgw::IAM::s3DeleteObject :
4759 rgw::IAM::s3DeleteObjectVersion,
4760 ARN(s->bucket->get_key(), s->object->get_name()),
4761 princ_type);
4762 }
4763 if (r == Effect::Deny)
4764 return -EACCES;
4765
4766 if (!s->session_policies.empty()) {
4767 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
4768 boost::none,
4769 s->object->get_instance().empty() ?
4770 rgw::IAM::s3DeleteObject :
4771 rgw::IAM::s3DeleteObjectVersion,
4772 ARN(s->bucket->get_key(), s->object->get_name()));
4773 if (session_policy_res == Effect::Deny) {
4774 return -EACCES;
4775 }
4776 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
4777 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
4778 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) ||
4779 (session_policy_res == Effect::Allow && r == Effect::Allow)) {
4780 return 0;
4781 }
4782 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
4783 //Intersection of session policy and identity policy plus bucket policy
4784 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) || r == Effect::Allow) {
4785 return 0;
4786 }
4787 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
4788 if (session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) {
4789 return 0;
4790 }
4791 }
4792 return -EACCES;
4793 }
4794 if (r == Effect::Allow || identity_policy_res == Effect::Allow)
4795 return 0;
4796 }
4797
4798 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
4799 return -EACCES;
4800 }
4801
4802 if (s->bucket->get_info().mfa_enabled() &&
4803 !s->object->get_instance().empty() &&
4804 !s->mfa_verified) {
4805 ldpp_dout(this, 5) << "NOTICE: object delete request with a versioned object, mfa auth not provided" << dendl;
4806 return -ERR_MFA_REQUIRED;
4807 }
4808
4809 return 0;
4810 }
4811
4812 void RGWDeleteObj::pre_exec()
4813 {
4814 rgw_bucket_object_pre_exec(s);
4815 }
4816
4817 void RGWDeleteObj::execute(optional_yield y)
4818 {
4819 if (!s->bucket_exists) {
4820 op_ret = -ERR_NO_SUCH_BUCKET;
4821 return;
4822 }
4823 s->object->set_bucket(s->bucket.get());
4824
4825 if (!rgw::sal::RGWObject::empty(s->object.get())) {
4826 uint64_t obj_size = 0;
4827 std::string etag;
4828 RGWObjectCtx* obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx);
4829 {
4830 RGWObjState* astate = nullptr;
4831 bool check_obj_lock = s->object->have_instance() && s->bucket->get_info().obj_lock_enabled();
4832
4833 op_ret = s->object->get_obj_state(this, obj_ctx, *s->bucket.get(), &astate, s->yield, true);
4834 if (op_ret < 0) {
4835 if (need_object_expiration() || multipart_delete) {
4836 return;
4837 }
4838
4839 if (check_obj_lock) {
4840 /* check if obj exists, read orig attrs */
4841 if (op_ret == -ENOENT) {
4842 /* object maybe delete_marker, skip check_obj_lock*/
4843 check_obj_lock = false;
4844 } else {
4845 return;
4846 }
4847 }
4848 } else {
4849 obj_size = astate->size;
4850 etag = astate->attrset[RGW_ATTR_ETAG].to_str();
4851 }
4852
4853 // ignore return value from get_obj_attrs in all other cases
4854 op_ret = 0;
4855
4856 if (check_obj_lock) {
4857 ceph_assert(astate);
4858 int object_lock_response = verify_object_lock(this, astate->attrset, bypass_perm, bypass_governance_mode);
4859 if (object_lock_response != 0) {
4860 op_ret = object_lock_response;
4861 if (op_ret == -EACCES) {
4862 s->err.message = "forbidden by object lock";
4863 }
4864 return;
4865 }
4866 }
4867
4868 if (multipart_delete) {
4869 if (!astate) {
4870 op_ret = -ERR_NOT_SLO_MANIFEST;
4871 return;
4872 }
4873
4874 const auto slo_attr = astate->attrset.find(RGW_ATTR_SLO_MANIFEST);
4875
4876 if (slo_attr != astate->attrset.end()) {
4877 op_ret = handle_slo_manifest(slo_attr->second, y);
4878 if (op_ret < 0) {
4879 ldpp_dout(this, 0) << "ERROR: failed to handle slo manifest ret=" << op_ret << dendl;
4880 }
4881 } else {
4882 op_ret = -ERR_NOT_SLO_MANIFEST;
4883 }
4884
4885 return;
4886 }
4887 }
4888
4889 // make reservation for notification if needed
4890 rgw::notify::reservation_t res(this, store, s, s->object.get());
4891 const auto versioned_object = s->bucket->versioning_enabled();
4892 const auto event_type = versioned_object && s->object->get_instance().empty() ?
4893 rgw::notify::ObjectRemovedDeleteMarkerCreated : rgw::notify::ObjectRemovedDelete;
4894 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
4895 if (op_ret < 0) {
4896 return;
4897 }
4898
4899 s->object->set_atomic(s->obj_ctx);
4900
4901 bool ver_restored = false;
4902 op_ret = s->object->swift_versioning_restore(s->obj_ctx, ver_restored, this);
4903 if (op_ret < 0) {
4904 return;
4905 }
4906
4907 if (!ver_restored) {
4908 uint64_t epoch = 0;
4909
4910 /* Swift's versioning mechanism hasn't found any previous version of
4911 * the object that could be restored. This means we should proceed
4912 * with the regular delete path. */
4913 op_ret = get_system_versioning_params(s, &epoch, &version_id);
4914 if (op_ret < 0) {
4915 return;
4916 }
4917
4918 op_ret = s->object->delete_object(this, obj_ctx, s->owner, s->bucket_owner, unmod_since,
4919 s->system_request, epoch, version_id, s->yield);
4920 if (op_ret >= 0) {
4921 delete_marker = s->object->get_delete_marker();
4922 }
4923
4924 /* Check whether the object has expired. Swift API documentation
4925 * stands that we should return 404 Not Found in such case. */
4926 if (need_object_expiration() && s->object->is_expired()) {
4927 op_ret = -ENOENT;
4928 return;
4929 }
4930 }
4931
4932 if (op_ret == -ECANCELED) {
4933 op_ret = 0;
4934 }
4935 if (op_ret == -ERR_PRECONDITION_FAILED && no_precondition_error) {
4936 op_ret = 0;
4937 }
4938
4939 // send request to notification manager
4940 const auto ret = rgw::notify::publish_commit(s->object.get(), obj_size, ceph::real_clock::now(), etag, event_type, res, this);
4941 if (ret < 0) {
4942 ldpp_dout(this, 1) << "ERROR: publishing notification failed, with error: " << ret << dendl;
4943 // too late to rollback operation, hence op_ret is not set here
4944 }
4945 } else {
4946 op_ret = -EINVAL;
4947 }
4948 }
4949
4950 bool RGWCopyObj::parse_copy_location(const std::string_view& url_src,
4951 string& bucket_name,
4952 rgw_obj_key& key,
4953 req_state* s)
4954 {
4955 std::string_view name_str;
4956 std::string_view params_str;
4957
4958 // search for ? before url-decoding so we don't accidentally match %3F
4959 size_t pos = url_src.find('?');
4960 if (pos == string::npos) {
4961 name_str = url_src;
4962 } else {
4963 name_str = url_src.substr(0, pos);
4964 params_str = url_src.substr(pos + 1);
4965 }
4966
4967 std::string_view dec_src{name_str};
4968 if (dec_src[0] == '/')
4969 dec_src.remove_prefix(1);
4970
4971 pos = dec_src.find('/');
4972 if (pos == string::npos)
4973 return false;
4974
4975 bucket_name = url_decode(dec_src.substr(0, pos));
4976 key.name = url_decode(dec_src.substr(pos + 1));
4977
4978 if (key.name.empty()) {
4979 return false;
4980 }
4981
4982 if (! params_str.empty()) {
4983 RGWHTTPArgs args;
4984 args.set(std::string(params_str));
4985 args.parse(s);
4986
4987 key.instance = args.get("versionId", NULL);
4988 }
4989
4990 return true;
4991 }
4992
4993 int RGWCopyObj::verify_permission(optional_yield y)
4994 {
4995 RGWAccessControlPolicy src_acl(s->cct);
4996 boost::optional<Policy> src_policy;
4997 op_ret = get_params(y);
4998 if (op_ret < 0)
4999 return op_ret;
5000
5001 op_ret = get_system_versioning_params(s, &olh_epoch, &version_id);
5002 if (op_ret < 0) {
5003 return op_ret;
5004 }
5005
5006 /* This is a bit of a hack; create an empty bucket, then load it below. */
5007 op_ret = store->get_bucket(s->user.get(), RGWBucketInfo(), &src_bucket);
5008 if (op_ret < 0) {
5009 if (op_ret == -ENOENT) {
5010 op_ret = -ERR_NO_SUCH_BUCKET;
5011 }
5012 return op_ret;
5013 }
5014
5015 op_ret = src_bucket->load_by_name(this, src_tenant_name, src_bucket_name, s->bucket_instance_id,
5016 s->sysobj_ctx, s->yield);
5017 if (op_ret < 0) {
5018 if (op_ret == -ENOENT) {
5019 op_ret = -ERR_NO_SUCH_BUCKET;
5020 }
5021 return op_ret;
5022 }
5023
5024 src_object->set_bucket(src_bucket.get());
5025 /* get buckets info (source and dest) */
5026 if (s->local_source && source_zone.empty()) {
5027 src_object->set_atomic(s->obj_ctx);
5028 src_object->set_prefetch_data(s->obj_ctx);
5029
5030 rgw_placement_rule src_placement;
5031
5032 /* check source object permissions */
5033 op_ret = read_obj_policy(this, store, s, src_bucket->get_info(), src_bucket->get_attrs(), &src_acl, &src_placement.storage_class,
5034 src_policy, src_bucket.get(), src_object.get(), y);
5035 if (op_ret < 0) {
5036 return op_ret;
5037 }
5038
5039 /* follow up on previous checks that required reading source object head */
5040 if (need_to_check_storage_class) {
5041 src_placement.inherit_from(src_bucket->get_placement_rule());
5042
5043 op_ret = check_storage_class(src_placement);
5044 if (op_ret < 0) {
5045 return op_ret;
5046 }
5047 }
5048
5049 /* admin request overrides permission checks */
5050 if (!s->auth.identity->is_admin_of(src_acl.get_owner().get_id())) {
5051 if (src_policy || ! s->iam_user_policies.empty() || !s->session_policies.empty()) {
5052 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
5053 boost::none,
5054 src_object->get_instance().empty() ?
5055 rgw::IAM::s3GetObject :
5056 rgw::IAM::s3GetObjectVersion,
5057 ARN(src_object->get_obj()));
5058 if (identity_policy_res == Effect::Deny) {
5059 return -EACCES;
5060 }
5061 auto e = Effect::Pass;
5062 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
5063 if (src_policy) {
5064 e = src_policy->eval(s->env, *s->auth.identity,
5065 src_object->get_instance().empty() ?
5066 rgw::IAM::s3GetObject :
5067 rgw::IAM::s3GetObjectVersion,
5068 ARN(src_object->get_obj()),
5069 princ_type);
5070 }
5071 if (e == Effect::Deny) {
5072 return -EACCES;
5073 }
5074 if (!s->session_policies.empty()) {
5075 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
5076 boost::none,
5077 src_object->get_instance().empty() ?
5078 rgw::IAM::s3GetObject :
5079 rgw::IAM::s3GetObjectVersion,
5080 ARN(src_object->get_obj()));
5081 if (session_policy_res == Effect::Deny) {
5082 return -EACCES;
5083 }
5084 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
5085 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
5086 if ((session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) &&
5087 (session_policy_res != Effect::Allow || e != Effect::Allow)) {
5088 return -EACCES;
5089 }
5090 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
5091 //Intersection of session policy and identity policy plus bucket policy
5092 if ((session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) && e != Effect::Allow) {
5093 return -EACCES;
5094 }
5095 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
5096 if (session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) {
5097 return -EACCES;
5098 }
5099 }
5100 }
5101 if (identity_policy_res == Effect::Pass && e == Effect::Pass &&
5102 !src_acl.verify_permission(this, *s->auth.identity, s->perm_mask,
5103 RGW_PERM_READ)) {
5104 return -EACCES;
5105 }
5106 } else if (!src_acl.verify_permission(this, *s->auth.identity,
5107 s->perm_mask,
5108 RGW_PERM_READ)) {
5109 return -EACCES;
5110 }
5111 }
5112 }
5113
5114 RGWAccessControlPolicy dest_bucket_policy(s->cct);
5115
5116 if (src_bucket_name.compare(dest_bucket_name) == 0) { /* will only happen if s->local_source
5117 or intra region sync */
5118 dest_bucket = src_bucket->clone();
5119 } else {
5120 op_ret = store->get_bucket(s->user.get(), RGWBucketInfo(), &dest_bucket);
5121 if (op_ret < 0) {
5122 if (op_ret == -ENOENT) {
5123 ldpp_dout(this, 0) << "ERROR: Destination Bucket not found for user: " << s->user->get_id().to_str() << dendl;
5124 op_ret = -ERR_NO_SUCH_BUCKET;
5125 }
5126 return op_ret;
5127 }
5128 op_ret = dest_bucket->load_by_name(this, dest_tenant_name, dest_bucket_name, std::string(),
5129 s->sysobj_ctx, s->yield);
5130 if (op_ret < 0) {
5131 if (op_ret == -ENOENT) {
5132 op_ret = -ERR_NO_SUCH_BUCKET;
5133 }
5134 return op_ret;
5135 }
5136 }
5137
5138 dest_object = store->get_object(rgw_obj_key(dest_obj_name));
5139 dest_object->set_bucket(dest_bucket.get());
5140 dest_object->set_atomic(s->obj_ctx);
5141
5142 /* check dest bucket permissions */
5143 op_ret = read_bucket_policy(this, store, s, dest_bucket->get_info(),
5144 dest_bucket->get_attrs(),
5145 &dest_bucket_policy, dest_bucket->get_key(), y);
5146 if (op_ret < 0) {
5147 return op_ret;
5148 }
5149 auto dest_iam_policy = get_iam_policy_from_attr(s->cct, dest_bucket->get_attrs(), dest_bucket->get_tenant());
5150 /* admin request overrides permission checks */
5151 if (! s->auth.identity->is_admin_of(dest_policy.get_owner().get_id())){
5152 if (dest_iam_policy != boost::none || ! s->iam_user_policies.empty() || !s->session_policies.empty()) {
5153 rgw_add_to_iam_environment(s->env, "s3:x-amz-copy-source", copy_source);
5154 if (md_directive)
5155 rgw_add_to_iam_environment(s->env, "s3:x-amz-metadata-directive",
5156 *md_directive);
5157
5158 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies,
5159 s->env, boost::none,
5160 rgw::IAM::s3PutObject,
5161 ARN(dest_object->get_obj()));
5162 if (identity_policy_res == Effect::Deny) {
5163 return -EACCES;
5164 }
5165 auto e = Effect::Pass;
5166 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
5167 if (dest_iam_policy) {
5168 e = dest_iam_policy->eval(s->env, *s->auth.identity,
5169 rgw::IAM::s3PutObject,
5170 ARN(dest_object->get_obj()),
5171 princ_type);
5172 }
5173 if (e == Effect::Deny) {
5174 return -EACCES;
5175 }
5176 if (!s->session_policies.empty()) {
5177 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env, boost::none, rgw::IAM::s3PutObject, ARN(dest_object->get_obj()));
5178 if (session_policy_res == Effect::Deny) {
5179 return false;
5180 }
5181 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
5182 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
5183 if ((session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) &&
5184 (session_policy_res != Effect::Allow || e == Effect::Allow)) {
5185 return -EACCES;
5186 }
5187 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
5188 //Intersection of session policy and identity policy plus bucket policy
5189 if ((session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) && e != Effect::Allow) {
5190 return -EACCES;
5191 }
5192 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
5193 if (session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) {
5194 return -EACCES;
5195 }
5196 }
5197 }
5198 if (identity_policy_res == Effect::Pass && e == Effect::Pass &&
5199 ! dest_bucket_policy.verify_permission(this,
5200 *s->auth.identity,
5201 s->perm_mask,
5202 RGW_PERM_WRITE)){
5203 return -EACCES;
5204 }
5205 } else if (! dest_bucket_policy.verify_permission(this, *s->auth.identity, s->perm_mask,
5206 RGW_PERM_WRITE)) {
5207 return -EACCES;
5208 }
5209
5210 }
5211
5212 op_ret = init_dest_policy();
5213 if (op_ret < 0) {
5214 return op_ret;
5215 }
5216
5217 return 0;
5218 }
5219
5220
5221 int RGWCopyObj::init_common()
5222 {
5223 if (if_mod) {
5224 if (parse_time(if_mod, &mod_time) < 0) {
5225 op_ret = -EINVAL;
5226 return op_ret;
5227 }
5228 mod_ptr = &mod_time;
5229 }
5230
5231 if (if_unmod) {
5232 if (parse_time(if_unmod, &unmod_time) < 0) {
5233 op_ret = -EINVAL;
5234 return op_ret;
5235 }
5236 unmod_ptr = &unmod_time;
5237 }
5238
5239 bufferlist aclbl;
5240 dest_policy.encode(aclbl);
5241 emplace_attr(RGW_ATTR_ACL, std::move(aclbl));
5242
5243 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs);
5244 if (op_ret < 0) {
5245 return op_ret;
5246 }
5247 populate_with_generic_attrs(s, attrs);
5248
5249 return 0;
5250 }
5251
5252 static void copy_obj_progress_cb(off_t ofs, void *param)
5253 {
5254 RGWCopyObj *op = static_cast<RGWCopyObj *>(param);
5255 op->progress_cb(ofs);
5256 }
5257
5258 void RGWCopyObj::progress_cb(off_t ofs)
5259 {
5260 if (!s->cct->_conf->rgw_copy_obj_progress)
5261 return;
5262
5263 if (ofs - last_ofs < s->cct->_conf->rgw_copy_obj_progress_every_bytes)
5264 return;
5265
5266 send_partial_response(ofs);
5267
5268 last_ofs = ofs;
5269 }
5270
5271 void RGWCopyObj::pre_exec()
5272 {
5273 rgw_bucket_object_pre_exec(s);
5274 }
5275
5276 void RGWCopyObj::execute(optional_yield y)
5277 {
5278 if (init_common() < 0)
5279 return;
5280
5281 if (! s->object->get_bucket()) {
5282 s->bucket = src_object->get_bucket()->clone();
5283 s->object->set_bucket(s->bucket.get());
5284 }
5285
5286 // make reservation for notification if needed
5287 rgw::notify::reservation_t res(this, store, s, s->object.get());
5288 const auto event_type = rgw::notify::ObjectCreatedCopy;
5289 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
5290 if (op_ret < 0) {
5291 return;
5292 }
5293
5294 if ( ! version_id.empty()) {
5295 dest_object->set_instance(version_id);
5296 } else if (dest_bucket->versioning_enabled()) {
5297 dest_object->gen_rand_obj_instance_name();
5298 }
5299
5300 src_object->set_atomic(s->obj_ctx);
5301 dest_object->set_atomic(s->obj_ctx);
5302
5303 encode_delete_at_attr(delete_at, attrs);
5304
5305 uint64_t obj_size = 0;
5306 {
5307 // get src object size (cached in obj_ctx from verify_permission())
5308 RGWObjState* astate = nullptr;
5309 op_ret = src_object->get_obj_state(this, s->obj_ctx, *src_bucket, &astate,
5310 s->yield, true);
5311 if (op_ret < 0) {
5312 return;
5313 }
5314 obj_size = astate->size;
5315
5316 if (!s->system_request) { // no quota enforcement for system requests
5317 // enforce quota against the destination bucket owner
5318 op_ret = dest_bucket->check_quota(user_quota, bucket_quota,
5319 astate->accounted_size, y);
5320 if (op_ret < 0) {
5321 return;
5322 }
5323 }
5324 }
5325
5326 bool high_precision_time = (s->system_request);
5327
5328 /* Handle object versioning of Swift API. In case of copying to remote this
5329 * should fail gently (op_ret == 0) as the dst_obj will not exist here. */
5330 op_ret = dest_object->swift_versioning_copy(s->obj_ctx, this, s->yield);
5331 if (op_ret < 0) {
5332 return;
5333 }
5334
5335 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
5336 op_ret = src_object->copy_object(obj_ctx,
5337 s->user.get(),
5338 &s->info,
5339 source_zone,
5340 dest_object.get(),
5341 dest_bucket.get(),
5342 src_bucket.get(),
5343 s->dest_placement,
5344 &src_mtime,
5345 &mtime,
5346 mod_ptr,
5347 unmod_ptr,
5348 high_precision_time,
5349 if_match,
5350 if_nomatch,
5351 attrs_mod,
5352 copy_if_newer,
5353 attrs,
5354 RGWObjCategory::Main,
5355 olh_epoch,
5356 delete_at,
5357 (version_id.empty() ? NULL : &version_id),
5358 &s->req_id, /* use req_id as tag */
5359 &etag,
5360 copy_obj_progress_cb, (void *)this,
5361 this,
5362 s->yield);
5363
5364 // send request to notification manager
5365 const auto ret = rgw::notify::publish_commit(s->object.get(), obj_size, mtime, etag, event_type, res, this);
5366 if (ret < 0) {
5367 ldpp_dout(this, 1) << "ERROR: publishing notification failed, with error: " << ret << dendl;
5368 // too late to rollback operation, hence op_ret is not set here
5369 }
5370 }
5371
5372 int RGWGetACLs::verify_permission(optional_yield y)
5373 {
5374 bool perm;
5375 if (!rgw::sal::RGWObject::empty(s->object.get())) {
5376 auto iam_action = s->object->get_instance().empty() ?
5377 rgw::IAM::s3GetObjectAcl :
5378 rgw::IAM::s3GetObjectVersionAcl;
5379
5380 if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){
5381 rgw_iam_add_existing_objtags(this, store, s, iam_action);
5382 }
5383 if (! s->iam_user_policies.empty()) {
5384 for (auto& user_policy : s->iam_user_policies) {
5385 if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) {
5386 rgw_iam_add_existing_objtags(this, store, s, iam_action);
5387 }
5388 }
5389 }
5390 perm = verify_object_permission(this, s, iam_action);
5391 } else {
5392 if (!s->bucket_exists) {
5393 return -ERR_NO_SUCH_BUCKET;
5394 }
5395 perm = verify_bucket_permission(this, s, rgw::IAM::s3GetBucketAcl);
5396 }
5397 if (!perm)
5398 return -EACCES;
5399
5400 return 0;
5401 }
5402
5403 void RGWGetACLs::pre_exec()
5404 {
5405 rgw_bucket_object_pre_exec(s);
5406 }
5407
5408 void RGWGetACLs::execute(optional_yield y)
5409 {
5410 stringstream ss;
5411 RGWAccessControlPolicy* const acl = \
5412 (!rgw::sal::RGWObject::empty(s->object.get()) ? s->object_acl.get() : s->bucket_acl.get());
5413 RGWAccessControlPolicy_S3* const s3policy = \
5414 static_cast<RGWAccessControlPolicy_S3*>(acl);
5415 s3policy->to_xml(ss);
5416 acls = ss.str();
5417 }
5418
5419
5420
5421 int RGWPutACLs::verify_permission(optional_yield y)
5422 {
5423 bool perm;
5424
5425 rgw_add_to_iam_environment(s->env, "s3:x-amz-acl", s->canned_acl);
5426
5427 rgw_add_grant_to_iam_environment(s->env, s);
5428 if (!rgw::sal::RGWObject::empty(s->object.get())) {
5429 auto iam_action = s->object->get_instance().empty() ? rgw::IAM::s3PutObjectAcl : rgw::IAM::s3PutObjectVersionAcl;
5430 op_ret = rgw_iam_add_existing_objtags(this, store, s, iam_action);
5431 perm = verify_object_permission(this, s, iam_action);
5432 } else {
5433 perm = verify_bucket_permission(this, s, rgw::IAM::s3PutBucketAcl);
5434 }
5435 if (!perm)
5436 return -EACCES;
5437
5438 return 0;
5439 }
5440
5441 int RGWGetLC::verify_permission(optional_yield y)
5442 {
5443 bool perm;
5444 perm = verify_bucket_permission(this, s, rgw::IAM::s3GetLifecycleConfiguration);
5445 if (!perm)
5446 return -EACCES;
5447
5448 return 0;
5449 }
5450
5451 int RGWPutLC::verify_permission(optional_yield y)
5452 {
5453 bool perm;
5454 perm = verify_bucket_permission(this, s, rgw::IAM::s3PutLifecycleConfiguration);
5455 if (!perm)
5456 return -EACCES;
5457
5458 return 0;
5459 }
5460
5461 int RGWDeleteLC::verify_permission(optional_yield y)
5462 {
5463 bool perm;
5464 perm = verify_bucket_permission(this, s, rgw::IAM::s3PutLifecycleConfiguration);
5465 if (!perm)
5466 return -EACCES;
5467
5468 return 0;
5469 }
5470
5471 void RGWPutACLs::pre_exec()
5472 {
5473 rgw_bucket_object_pre_exec(s);
5474 }
5475
5476 void RGWGetLC::pre_exec()
5477 {
5478 rgw_bucket_object_pre_exec(s);
5479 }
5480
5481 void RGWPutLC::pre_exec()
5482 {
5483 rgw_bucket_object_pre_exec(s);
5484 }
5485
5486 void RGWDeleteLC::pre_exec()
5487 {
5488 rgw_bucket_object_pre_exec(s);
5489 }
5490
5491 void RGWPutACLs::execute(optional_yield y)
5492 {
5493 bufferlist bl;
5494
5495 RGWAccessControlPolicy_S3 *policy = NULL;
5496 RGWACLXMLParser_S3 parser(s->cct);
5497 RGWAccessControlPolicy_S3 new_policy(s->cct);
5498 stringstream ss;
5499
5500 op_ret = 0; /* XXX redundant? */
5501
5502 if (!parser.init()) {
5503 op_ret = -EINVAL;
5504 return;
5505 }
5506
5507
5508 RGWAccessControlPolicy* const existing_policy = \
5509 (rgw::sal::RGWObject::empty(s->object.get()) ? s->bucket_acl.get() : s->object_acl.get());
5510
5511 owner = existing_policy->get_owner();
5512
5513 op_ret = get_params(y);
5514 if (op_ret < 0) {
5515 if (op_ret == -ERANGE) {
5516 ldpp_dout(this, 4) << "The size of request xml data is larger than the max limitation, data size = "
5517 << s->length << dendl;
5518 op_ret = -ERR_MALFORMED_XML;
5519 s->err.message = "The XML you provided was larger than the maximum " +
5520 std::to_string(s->cct->_conf->rgw_max_put_param_size) +
5521 " bytes allowed.";
5522 }
5523 return;
5524 }
5525
5526 char* buf = data.c_str();
5527 ldpp_dout(this, 15) << "read len=" << data.length() << " data=" << (buf ? buf : "") << dendl;
5528
5529 if (!s->canned_acl.empty() && data.length() > 0) {
5530 op_ret = -EINVAL;
5531 return;
5532 }
5533
5534 if (!s->canned_acl.empty() || s->has_acl_header) {
5535 op_ret = get_policy_from_state(store, s, ss);
5536 if (op_ret < 0)
5537 return;
5538
5539 data.clear();
5540 data.append(ss.str());
5541 }
5542
5543 if (!parser.parse(data.c_str(), data.length(), 1)) {
5544 op_ret = -EINVAL;
5545 return;
5546 }
5547 policy = static_cast<RGWAccessControlPolicy_S3 *>(parser.find_first("AccessControlPolicy"));
5548 if (!policy) {
5549 op_ret = -EINVAL;
5550 return;
5551 }
5552
5553 const RGWAccessControlList& req_acl = policy->get_acl();
5554 const multimap<string, ACLGrant>& req_grant_map = req_acl.get_grant_map();
5555 #define ACL_GRANTS_MAX_NUM 100
5556 int max_num = s->cct->_conf->rgw_acl_grants_max_num;
5557 if (max_num < 0) {
5558 max_num = ACL_GRANTS_MAX_NUM;
5559 }
5560
5561 int grants_num = req_grant_map.size();
5562 if (grants_num > max_num) {
5563 ldpp_dout(this, 4) << "An acl can have up to " << max_num
5564 << " grants, request acl grants num: " << grants_num << dendl;
5565 op_ret = -ERR_LIMIT_EXCEEDED;
5566 s->err.message = "The request is rejected, because the acl grants number you requested is larger than the maximum "
5567 + std::to_string(max_num)
5568 + " grants allowed in an acl.";
5569 return;
5570 }
5571
5572 // forward bucket acl requests to meta master zone
5573 if ((rgw::sal::RGWObject::empty(s->object.get()))) {
5574 bufferlist in_data;
5575 // include acl data unless it was generated from a canned_acl
5576 if (s->canned_acl.empty()) {
5577 in_data.append(data);
5578 }
5579 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
5580 if (op_ret < 0) {
5581 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5582 return;
5583 }
5584 }
5585
5586 if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
5587 ldpp_dout(this, 15) << "Old AccessControlPolicy";
5588 policy->to_xml(*_dout);
5589 *_dout << dendl;
5590 }
5591
5592 op_ret = policy->rebuild(this, store->ctl()->user, &owner, new_policy, s->err.message);
5593 if (op_ret < 0)
5594 return;
5595
5596 if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
5597 ldpp_dout(this, 15) << "New AccessControlPolicy:";
5598 new_policy.to_xml(*_dout);
5599 *_dout << dendl;
5600 }
5601
5602 if (s->bucket_access_conf &&
5603 s->bucket_access_conf->block_public_acls() &&
5604 new_policy.is_public(this)) {
5605 op_ret = -EACCES;
5606 return;
5607 }
5608 new_policy.encode(bl);
5609 map<string, bufferlist> attrs;
5610
5611 if (!rgw::sal::RGWObject::empty(s->object.get())) {
5612 s->object->set_atomic(s->obj_ctx);
5613 //if instance is empty, we should modify the latest object
5614 op_ret = s->object->modify_obj_attrs(s->obj_ctx, RGW_ATTR_ACL, bl, s->yield, this);
5615 } else {
5616 map<string,bufferlist> attrs = s->bucket_attrs;
5617 attrs[RGW_ATTR_ACL] = bl;
5618 op_ret = store->ctl()->bucket->set_bucket_instance_attrs(s->bucket->get_info(), attrs,
5619 &s->bucket->get_info().objv_tracker,
5620 s->yield, this);
5621 }
5622 if (op_ret == -ECANCELED) {
5623 op_ret = 0; /* lost a race, but it's ok because acls are immutable */
5624 }
5625 }
5626
5627 void RGWPutLC::execute(optional_yield y)
5628 {
5629 bufferlist bl;
5630
5631 RGWLifecycleConfiguration_S3 config(s->cct);
5632 RGWXMLParser parser;
5633 RGWLifecycleConfiguration_S3 new_config(s->cct);
5634
5635 content_md5 = s->info.env->get("HTTP_CONTENT_MD5");
5636 if (content_md5 == nullptr) {
5637 op_ret = -ERR_INVALID_REQUEST;
5638 s->err.message = "Missing required header for this request: Content-MD5";
5639 ldpp_dout(this, 5) << s->err.message << dendl;
5640 return;
5641 }
5642
5643 std::string content_md5_bin;
5644 try {
5645 content_md5_bin = rgw::from_base64(std::string_view(content_md5));
5646 } catch (...) {
5647 s->err.message = "Request header Content-MD5 contains character "
5648 "that is not base64 encoded.";
5649 ldpp_dout(this, 5) << s->err.message << dendl;
5650 op_ret = -ERR_BAD_DIGEST;
5651 return;
5652 }
5653
5654 if (!parser.init()) {
5655 op_ret = -EINVAL;
5656 return;
5657 }
5658
5659 op_ret = get_params(y);
5660 if (op_ret < 0)
5661 return;
5662
5663 char* buf = data.c_str();
5664 ldpp_dout(this, 15) << "read len=" << data.length() << " data=" << (buf ? buf : "") << dendl;
5665
5666 MD5 data_hash;
5667 unsigned char data_hash_res[CEPH_CRYPTO_MD5_DIGESTSIZE];
5668 data_hash.Update(reinterpret_cast<const unsigned char*>(buf), data.length());
5669 data_hash.Final(data_hash_res);
5670
5671 if (memcmp(data_hash_res, content_md5_bin.c_str(), CEPH_CRYPTO_MD5_DIGESTSIZE) != 0) {
5672 op_ret = -ERR_BAD_DIGEST;
5673 s->err.message = "The Content-MD5 you specified did not match what we received.";
5674 ldpp_dout(this, 5) << s->err.message
5675 << " Specified content md5: " << content_md5
5676 << ", calculated content md5: " << data_hash_res
5677 << dendl;
5678 return;
5679 }
5680
5681 if (!parser.parse(buf, data.length(), 1)) {
5682 op_ret = -ERR_MALFORMED_XML;
5683 return;
5684 }
5685
5686 try {
5687 RGWXMLDecoder::decode_xml("LifecycleConfiguration", config, &parser);
5688 } catch (RGWXMLDecoder::err& err) {
5689 ldpp_dout(this, 5) << "Bad lifecycle configuration: " << err << dendl;
5690 op_ret = -ERR_MALFORMED_XML;
5691 return;
5692 }
5693
5694 op_ret = config.rebuild(new_config);
5695 if (op_ret < 0)
5696 return;
5697
5698 if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
5699 XMLFormatter xf;
5700 new_config.dump_xml(&xf);
5701 stringstream ss;
5702 xf.flush(ss);
5703 ldpp_dout(this, 15) << "New LifecycleConfiguration:" << ss.str() << dendl;
5704 }
5705
5706 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
5707 if (op_ret < 0) {
5708 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5709 return;
5710 }
5711
5712 op_ret = store->get_rgwlc()->set_bucket_config(s->bucket->get_info(), s->bucket_attrs, &new_config);
5713 if (op_ret < 0) {
5714 return;
5715 }
5716 return;
5717 }
5718
5719 void RGWDeleteLC::execute(optional_yield y)
5720 {
5721 bufferlist data;
5722 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
5723 if (op_ret < 0) {
5724 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5725 return;
5726 }
5727
5728 op_ret = store->get_rgwlc()->remove_bucket_config(s->bucket->get_info(), s->bucket_attrs);
5729 if (op_ret < 0) {
5730 return;
5731 }
5732 return;
5733 }
5734
5735 int RGWGetCORS::verify_permission(optional_yield y)
5736 {
5737 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketCORS);
5738 }
5739
5740 void RGWGetCORS::execute(optional_yield y)
5741 {
5742 op_ret = read_bucket_cors();
5743 if (op_ret < 0)
5744 return ;
5745
5746 if (!cors_exist) {
5747 ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl;
5748 op_ret = -ERR_NO_CORS_FOUND;
5749 return;
5750 }
5751 }
5752
5753 int RGWPutCORS::verify_permission(optional_yield y)
5754 {
5755 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketCORS);
5756 }
5757
5758 void RGWPutCORS::execute(optional_yield y)
5759 {
5760 rgw_raw_obj obj;
5761
5762 op_ret = get_params(y);
5763 if (op_ret < 0)
5764 return;
5765
5766 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
5767 if (op_ret < 0) {
5768 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5769 return;
5770 }
5771
5772 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
5773 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
5774 attrs[RGW_ATTR_CORS] = cors_bl;
5775 return s->bucket->set_instance_attrs(this, attrs, s->yield);
5776 });
5777 }
5778
5779 int RGWDeleteCORS::verify_permission(optional_yield y)
5780 {
5781 // No separate delete permission
5782 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketCORS);
5783 }
5784
5785 void RGWDeleteCORS::execute(optional_yield y)
5786 {
5787 bufferlist data;
5788 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
5789 if (op_ret < 0) {
5790 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5791 return;
5792 }
5793
5794 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
5795 op_ret = read_bucket_cors();
5796 if (op_ret < 0)
5797 return op_ret;
5798
5799 if (!cors_exist) {
5800 ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl;
5801 op_ret = -ENOENT;
5802 return op_ret;
5803 }
5804
5805 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
5806 attrs.erase(RGW_ATTR_CORS);
5807 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
5808 if (op_ret < 0) {
5809 ldpp_dout(this, 0) << "RGWLC::RGWDeleteCORS() failed to set attrs on bucket=" << s->bucket->get_name()
5810 << " returned err=" << op_ret << dendl;
5811 }
5812 return op_ret;
5813 });
5814 }
5815
5816 void RGWOptionsCORS::get_response_params(string& hdrs, string& exp_hdrs, unsigned *max_age) {
5817 get_cors_response_headers(rule, req_hdrs, hdrs, exp_hdrs, max_age);
5818 }
5819
5820 int RGWOptionsCORS::validate_cors_request(RGWCORSConfiguration *cc) {
5821 rule = cc->host_name_rule(origin);
5822 if (!rule) {
5823 ldpp_dout(this, 10) << "There is no cors rule present for " << origin << dendl;
5824 return -ENOENT;
5825 }
5826
5827 if (!validate_cors_rule_method(rule, req_meth)) {
5828 return -ENOENT;
5829 }
5830
5831 if (!validate_cors_rule_header(rule, req_hdrs)) {
5832 return -ENOENT;
5833 }
5834
5835 return 0;
5836 }
5837
5838 void RGWOptionsCORS::execute(optional_yield y)
5839 {
5840 op_ret = read_bucket_cors();
5841 if (op_ret < 0)
5842 return;
5843
5844 origin = s->info.env->get("HTTP_ORIGIN");
5845 if (!origin) {
5846 ldpp_dout(this, 0) << "Missing mandatory Origin header" << dendl;
5847 op_ret = -EINVAL;
5848 return;
5849 }
5850 req_meth = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_METHOD");
5851 if (!req_meth) {
5852 ldpp_dout(this, 0) << "Missing mandatory Access-control-request-method header" << dendl;
5853 op_ret = -EINVAL;
5854 return;
5855 }
5856 if (!cors_exist) {
5857 ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl;
5858 op_ret = -ENOENT;
5859 return;
5860 }
5861 req_hdrs = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_HEADERS");
5862 op_ret = validate_cors_request(&bucket_cors);
5863 if (!rule) {
5864 origin = req_meth = NULL;
5865 return;
5866 }
5867 return;
5868 }
5869
5870 int RGWGetRequestPayment::verify_permission(optional_yield y)
5871 {
5872 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketRequestPayment);
5873 }
5874
5875 void RGWGetRequestPayment::pre_exec()
5876 {
5877 rgw_bucket_object_pre_exec(s);
5878 }
5879
5880 void RGWGetRequestPayment::execute(optional_yield y)
5881 {
5882 requester_pays = s->bucket->get_info().requester_pays;
5883 }
5884
5885 int RGWSetRequestPayment::verify_permission(optional_yield y)
5886 {
5887 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketRequestPayment);
5888 }
5889
5890 void RGWSetRequestPayment::pre_exec()
5891 {
5892 rgw_bucket_object_pre_exec(s);
5893 }
5894
5895 void RGWSetRequestPayment::execute(optional_yield y)
5896 {
5897
5898 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
5899 if (op_ret < 0) {
5900 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5901 return;
5902 }
5903
5904 op_ret = get_params(y);
5905
5906 if (op_ret < 0)
5907 return;
5908
5909 s->bucket->get_info().requester_pays = requester_pays;
5910 op_ret = s->bucket->put_instance_info(this, false, real_time());
5911 if (op_ret < 0) {
5912 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
5913 << " returned err=" << op_ret << dendl;
5914 return;
5915 }
5916 s->bucket_attrs = s->bucket->get_attrs();
5917 }
5918
5919 int RGWInitMultipart::verify_permission(optional_yield y)
5920 {
5921 if (s->iam_policy || ! s->iam_user_policies.empty() || !s->session_policies.empty()) {
5922 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
5923 boost::none,
5924 rgw::IAM::s3PutObject,
5925 s->object->get_obj());
5926 if (identity_policy_res == Effect::Deny) {
5927 return -EACCES;
5928 }
5929
5930 rgw::IAM::Effect e = Effect::Pass;
5931 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
5932 if (s->iam_policy) {
5933 e = s->iam_policy->eval(s->env, *s->auth.identity,
5934 rgw::IAM::s3PutObject,
5935 s->object->get_obj(),
5936 princ_type);
5937 }
5938 if (e == Effect::Deny) {
5939 return -EACCES;
5940 }
5941
5942 if (!s->session_policies.empty()) {
5943 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
5944 boost::none,
5945 rgw::IAM::s3PutObject,
5946 s->object->get_obj());
5947 if (session_policy_res == Effect::Deny) {
5948 return -EACCES;
5949 }
5950 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
5951 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
5952 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) ||
5953 (session_policy_res == Effect::Allow && e == Effect::Allow)) {
5954 return 0;
5955 }
5956 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
5957 //Intersection of session policy and identity policy plus bucket policy
5958 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) || e == Effect::Allow) {
5959 return 0;
5960 }
5961 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
5962 if (session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) {
5963 return 0;
5964 }
5965 }
5966 return -EACCES;
5967 }
5968 if (e == Effect::Allow || identity_policy_res == Effect::Allow) {
5969 return 0;
5970 }
5971 }
5972
5973 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
5974 return -EACCES;
5975 }
5976
5977 return 0;
5978 }
5979
5980 void RGWInitMultipart::pre_exec()
5981 {
5982 rgw_bucket_object_pre_exec(s);
5983 }
5984
5985 void RGWInitMultipart::execute(optional_yield y)
5986 {
5987 bufferlist aclbl;
5988 rgw::sal::RGWAttrs attrs;
5989
5990 if (get_params(y) < 0)
5991 return;
5992
5993 if (rgw::sal::RGWObject::empty(s->object.get()))
5994 return;
5995
5996 policy.encode(aclbl);
5997 attrs[RGW_ATTR_ACL] = aclbl;
5998
5999 populate_with_generic_attrs(s, attrs);
6000
6001 /* select encryption mode */
6002 op_ret = prepare_encryption(attrs);
6003 if (op_ret != 0)
6004 return;
6005
6006 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs);
6007 if (op_ret < 0) {
6008 return;
6009 }
6010
6011 // make reservation for notification if needed
6012 rgw::notify::reservation_t res(this, store, s, s->object.get());
6013 const auto event_type = rgw::notify::ObjectCreatedPost;
6014 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
6015 if (op_ret < 0) {
6016 return;
6017 }
6018
6019 do {
6020 char buf[33];
6021 std::unique_ptr<rgw::sal::RGWObject> obj;
6022 gen_rand_alphanumeric(s->cct, buf, sizeof(buf) - 1);
6023 upload_id = MULTIPART_UPLOAD_ID_PREFIX; /* v2 upload id */
6024 upload_id.append(buf);
6025
6026 string tmp_obj_name;
6027 RGWMPObj mp(s->object->get_name(), upload_id);
6028 tmp_obj_name = mp.get_meta();
6029
6030 obj = s->bucket->get_object(rgw_obj_key(tmp_obj_name, string(), mp_ns));
6031 // the meta object will be indexed with 0 size, we c
6032 obj->set_in_extra_data(true);
6033 obj->set_hash_source(s->object->get_name());
6034
6035 std::unique_ptr<rgw::sal::RGWObject::WriteOp> obj_op = obj->get_write_op(s->obj_ctx);
6036
6037 obj_op->params.versioning_disabled = true; /* no versioning for multipart meta */
6038 obj_op->params.owner = s->owner;
6039 obj_op->params.category = RGWObjCategory::MultiMeta;
6040 obj_op->params.flags = PUT_OBJ_CREATE_EXCL;
6041 obj_op->params.mtime = &mtime;
6042 obj_op->params.attrs = &attrs;
6043
6044 multipart_upload_info upload_info;
6045 upload_info.dest_placement = s->dest_placement;
6046
6047 bufferlist bl;
6048 encode(upload_info, bl);
6049 obj_op->params.data = &bl;
6050
6051 op_ret = obj_op->prepare(s->yield);
6052
6053 op_ret = obj_op->write_meta(this, bl.length(), 0, s->yield);
6054 } while (op_ret == -EEXIST);
6055
6056 // send request to notification manager
6057 const auto ret = rgw::notify::publish_commit(s->object.get(), s->obj_size, ceph::real_clock::now(), attrs[RGW_ATTR_ETAG].to_str(), event_type, res, this);
6058 if (ret < 0) {
6059 ldpp_dout(this, 1) << "ERROR: publishing notification failed, with error: " << ret << dendl;
6060 // too late to rollback operation, hence op_ret is not set here
6061 }
6062 }
6063
6064 int RGWCompleteMultipart::verify_permission(optional_yield y)
6065 {
6066 if (s->iam_policy || ! s->iam_user_policies.empty() || ! s->session_policies.empty()) {
6067 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
6068 boost::none,
6069 rgw::IAM::s3PutObject,
6070 s->object->get_obj());
6071 if (identity_policy_res == Effect::Deny) {
6072 return -EACCES;
6073 }
6074
6075 rgw::IAM::Effect e = Effect::Pass;
6076 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
6077 if (s->iam_policy) {
6078 e = s->iam_policy->eval(s->env, *s->auth.identity,
6079 rgw::IAM::s3PutObject,
6080 s->object->get_obj(),
6081 princ_type);
6082 }
6083 if (e == Effect::Deny) {
6084 return -EACCES;
6085 }
6086
6087 if (!s->session_policies.empty()) {
6088 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
6089 boost::none,
6090 rgw::IAM::s3PutObject,
6091 s->object->get_obj());
6092 if (session_policy_res == Effect::Deny) {
6093 return -EACCES;
6094 }
6095 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
6096 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
6097 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) ||
6098 (session_policy_res == Effect::Allow && e == Effect::Allow)) {
6099 return 0;
6100 }
6101 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
6102 //Intersection of session policy and identity policy plus bucket policy
6103 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) || e == Effect::Allow) {
6104 return 0;
6105 }
6106 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
6107 if (session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) {
6108 return 0;
6109 }
6110 }
6111 return -EACCES;
6112 }
6113 if (e == Effect::Allow || identity_policy_res == Effect::Allow) {
6114 return 0;
6115 }
6116 }
6117
6118 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
6119 return -EACCES;
6120 }
6121
6122 return 0;
6123 }
6124
6125 void RGWCompleteMultipart::pre_exec()
6126 {
6127 rgw_bucket_object_pre_exec(s);
6128 }
6129
6130 void RGWCompleteMultipart::execute(optional_yield y)
6131 {
6132 RGWMultiCompleteUpload *parts;
6133 map<int, string>::iterator iter;
6134 RGWMultiXMLParser parser;
6135 string meta_oid;
6136 map<uint32_t, RGWUploadPartInfo> obj_parts;
6137 map<uint32_t, RGWUploadPartInfo>::iterator obj_iter;
6138 rgw::sal::RGWAttrs attrs;
6139 off_t ofs = 0;
6140 MD5 hash;
6141 char final_etag[CEPH_CRYPTO_MD5_DIGESTSIZE];
6142 char final_etag_str[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 16];
6143 bufferlist etag_bl;
6144 std::unique_ptr<rgw::sal::RGWObject> meta_obj;
6145 std::unique_ptr<rgw::sal::RGWObject> target_obj;
6146 RGWMPObj mp;
6147 RGWObjManifest manifest;
6148 uint64_t olh_epoch = 0;
6149
6150 op_ret = get_params(y);
6151 if (op_ret < 0)
6152 return;
6153 op_ret = get_system_versioning_params(s, &olh_epoch, &version_id);
6154 if (op_ret < 0) {
6155 return;
6156 }
6157
6158 if (!data.length()) {
6159 op_ret = -ERR_MALFORMED_XML;
6160 return;
6161 }
6162
6163 if (!parser.init()) {
6164 op_ret = -EIO;
6165 return;
6166 }
6167
6168 if (!parser.parse(data.c_str(), data.length(), 1)) {
6169 op_ret = -ERR_MALFORMED_XML;
6170 return;
6171 }
6172
6173 parts = static_cast<RGWMultiCompleteUpload *>(parser.find_first("CompleteMultipartUpload"));
6174 if (!parts || parts->parts.empty()) {
6175 op_ret = -ERR_MALFORMED_XML;
6176 return;
6177 }
6178
6179 if ((int)parts->parts.size() >
6180 s->cct->_conf->rgw_multipart_part_upload_limit) {
6181 op_ret = -ERANGE;
6182 return;
6183 }
6184
6185 mp.init(s->object->get_name(), upload_id);
6186
6187 meta_oid = mp.get_meta();
6188
6189 int total_parts = 0;
6190 int handled_parts = 0;
6191 int max_parts = 1000;
6192 int marker = 0;
6193 bool truncated;
6194 RGWCompressionInfo cs_info;
6195 bool compressed = false;
6196 uint64_t accounted_size = 0;
6197
6198 uint64_t min_part_size = s->cct->_conf->rgw_multipart_min_part_size;
6199
6200 list<rgw_obj_index_key> remove_objs; /* objects to be removed from index listing */
6201
6202 bool versioned_object = s->bucket->versioning_enabled();
6203
6204 iter = parts->parts.begin();
6205
6206 meta_obj = s->bucket->get_object(rgw_obj_key(meta_oid, string(), mp_ns));
6207 meta_obj->set_in_extra_data(true);
6208 meta_obj->set_hash_source(s->object->get_name());
6209
6210 /*take a cls lock on meta_obj to prevent racing completions (or retries)
6211 from deleting the parts*/
6212 int max_lock_secs_mp =
6213 s->cct->_conf.get_val<int64_t>("rgw_mp_lock_max_time");
6214 utime_t dur(max_lock_secs_mp, 0);
6215
6216 serializer = meta_obj->get_serializer(this, "RGWCompleteMultipart");
6217 op_ret = serializer->try_lock(this, dur, y);
6218 if (op_ret < 0) {
6219 ldpp_dout(this, 0) << "failed to acquire lock" << dendl;
6220 op_ret = -ERR_INTERNAL_ERROR;
6221 s->err.message = "This multipart completion is already in progress";
6222 return;
6223 }
6224
6225 op_ret = meta_obj->get_obj_attrs(s->obj_ctx, s->yield, this);
6226 if (op_ret < 0) {
6227 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << meta_obj
6228 << " ret=" << op_ret << dendl;
6229 return;
6230 }
6231 attrs = meta_obj->get_attrs();
6232
6233 // make reservation for notification if needed
6234 rgw::notify::reservation_t res(this, store, s, meta_obj.get(), &s->object->get_name());
6235 const auto event_type = rgw::notify::ObjectCreatedCompleteMultipartUpload;
6236 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
6237 if (op_ret < 0) {
6238 return;
6239 }
6240
6241 do {
6242 op_ret = list_multipart_parts(this, store, s, upload_id, meta_oid, max_parts,
6243 marker, obj_parts, &marker, &truncated);
6244 if (op_ret == -ENOENT) {
6245 op_ret = -ERR_NO_SUCH_UPLOAD;
6246 }
6247 if (op_ret < 0)
6248 return;
6249
6250 total_parts += obj_parts.size();
6251 if (!truncated && total_parts != (int)parts->parts.size()) {
6252 ldpp_dout(this, 0) << "NOTICE: total parts mismatch: have: " << total_parts
6253 << " expected: " << parts->parts.size() << dendl;
6254 op_ret = -ERR_INVALID_PART;
6255 return;
6256 }
6257
6258 for (obj_iter = obj_parts.begin(); iter != parts->parts.end() && obj_iter != obj_parts.end(); ++iter, ++obj_iter, ++handled_parts) {
6259 uint64_t part_size = obj_iter->second.accounted_size;
6260 if (handled_parts < (int)parts->parts.size() - 1 &&
6261 part_size < min_part_size) {
6262 op_ret = -ERR_TOO_SMALL;
6263 return;
6264 }
6265
6266 char petag[CEPH_CRYPTO_MD5_DIGESTSIZE];
6267 if (iter->first != (int)obj_iter->first) {
6268 ldpp_dout(this, 0) << "NOTICE: parts num mismatch: next requested: "
6269 << iter->first << " next uploaded: "
6270 << obj_iter->first << dendl;
6271 op_ret = -ERR_INVALID_PART;
6272 return;
6273 }
6274 string part_etag = rgw_string_unquote(iter->second);
6275 if (part_etag.compare(obj_iter->second.etag) != 0) {
6276 ldpp_dout(this, 0) << "NOTICE: etag mismatch: part: " << iter->first
6277 << " etag: " << iter->second << dendl;
6278 op_ret = -ERR_INVALID_PART;
6279 return;
6280 }
6281
6282 hex_to_buf(obj_iter->second.etag.c_str(), petag,
6283 CEPH_CRYPTO_MD5_DIGESTSIZE);
6284 hash.Update((const unsigned char *)petag, sizeof(petag));
6285
6286 RGWUploadPartInfo& obj_part = obj_iter->second;
6287
6288 /* update manifest for part */
6289 string oid = mp.get_part(obj_iter->second.num);
6290 rgw_obj src_obj;
6291 src_obj.init_ns(s->bucket->get_key(), oid, mp_ns);
6292
6293 if (obj_part.manifest.empty()) {
6294 ldpp_dout(this, 0) << "ERROR: empty manifest for object part: obj="
6295 << src_obj << dendl;
6296 op_ret = -ERR_INVALID_PART;
6297 return;
6298 } else {
6299 manifest.append(this, obj_part.manifest, store->svc()->zone);
6300 }
6301
6302 bool part_compressed = (obj_part.cs_info.compression_type != "none");
6303 if ((handled_parts > 0) &&
6304 ((part_compressed != compressed) ||
6305 (cs_info.compression_type != obj_part.cs_info.compression_type))) {
6306 ldpp_dout(this, 0) << "ERROR: compression type was changed during multipart upload ("
6307 << cs_info.compression_type << ">>" << obj_part.cs_info.compression_type << ")" << dendl;
6308 op_ret = -ERR_INVALID_PART;
6309 return;
6310 }
6311
6312 if (part_compressed) {
6313 int64_t new_ofs; // offset in compression data for new part
6314 if (cs_info.blocks.size() > 0)
6315 new_ofs = cs_info.blocks.back().new_ofs + cs_info.blocks.back().len;
6316 else
6317 new_ofs = 0;
6318 for (const auto& block : obj_part.cs_info.blocks) {
6319 compression_block cb;
6320 cb.old_ofs = block.old_ofs + cs_info.orig_size;
6321 cb.new_ofs = new_ofs;
6322 cb.len = block.len;
6323 cs_info.blocks.push_back(cb);
6324 new_ofs = cb.new_ofs + cb.len;
6325 }
6326 if (!compressed)
6327 cs_info.compression_type = obj_part.cs_info.compression_type;
6328 cs_info.orig_size += obj_part.cs_info.orig_size;
6329 compressed = true;
6330 }
6331
6332 rgw_obj_index_key remove_key;
6333 src_obj.key.get_index_key(&remove_key);
6334
6335 remove_objs.push_back(remove_key);
6336
6337 ofs += obj_part.size;
6338 accounted_size += obj_part.accounted_size;
6339 }
6340 } while (truncated);
6341
6342 hash.Final((unsigned char *)final_etag);
6343
6344 buf_to_hex((unsigned char *)final_etag, sizeof(final_etag), final_etag_str);
6345 snprintf(&final_etag_str[CEPH_CRYPTO_MD5_DIGESTSIZE * 2], sizeof(final_etag_str) - CEPH_CRYPTO_MD5_DIGESTSIZE * 2,
6346 "-%lld", (long long)parts->parts.size());
6347 etag = final_etag_str;
6348 ldpp_dout(this, 10) << "calculated etag: " << final_etag_str << dendl;
6349
6350 etag_bl.append(final_etag_str, strlen(final_etag_str));
6351
6352 attrs[RGW_ATTR_ETAG] = etag_bl;
6353
6354 if (compressed) {
6355 // write compression attribute to full object
6356 bufferlist tmp;
6357 encode(cs_info, tmp);
6358 attrs[RGW_ATTR_COMPRESSION] = tmp;
6359 }
6360
6361 target_obj = s->bucket->get_object(rgw_obj_key(s->object->get_name()));
6362 if (versioned_object) {
6363 if (!version_id.empty()) {
6364 target_obj->set_instance(version_id);
6365 } else {
6366 target_obj->gen_rand_obj_instance_name();
6367 version_id = target_obj->get_instance();
6368 }
6369 }
6370
6371 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
6372
6373 target_obj->set_atomic(&obj_ctx);
6374
6375 std::unique_ptr<rgw::sal::RGWObject::WriteOp> obj_op = target_obj->get_write_op(&obj_ctx);
6376
6377 obj_op->params.manifest = &manifest;
6378 obj_op->params.remove_objs = &remove_objs;
6379
6380 obj_op->params.ptag = &s->req_id; /* use req_id as operation tag */
6381 obj_op->params.owner = s->owner;
6382 obj_op->params.flags = PUT_OBJ_CREATE;
6383 obj_op->params.modify_tail = true;
6384 obj_op->params.completeMultipart = true;
6385 obj_op->params.olh_epoch = olh_epoch;
6386 obj_op->params.attrs = &attrs;
6387
6388 op_ret = obj_op->prepare(s->yield);
6389 if (op_ret < 0) {
6390 return;
6391 }
6392
6393 op_ret = obj_op->write_meta(this, ofs, accounted_size, s->yield);
6394 if (op_ret < 0) {
6395 return;
6396 }
6397
6398 // remove the upload meta object
6399 string version_id;
6400
6401 // remove the upload meta object ; the meta object is not versioned
6402 // when the bucket is, as that would add an unneeded delete marker
6403 int r = meta_obj->delete_object(this, s->obj_ctx, ACLOwner(), ACLOwner(), ceph::real_time(), false, 0,
6404 version_id, null_yield, true /* prevent versioning*/ );
6405 if (r >= 0) {
6406 /* serializer's exclusive lock is released */
6407 serializer->clear_locked();
6408 } else {
6409 ldpp_dout(this, 0) << "WARNING: failed to remove object " << meta_obj << dendl;
6410 }
6411
6412 // send request to notification manager
6413 const auto ret = rgw::notify::publish_commit(s->object.get(), ofs, ceph::real_clock::now(), final_etag_str, event_type, res, this);
6414 if (ret < 0) {
6415 ldpp_dout(this, 1) << "ERROR: publishing notification failed, with error: " << ret << dendl;
6416 // too late to rollback operation, hence op_ret is not set here
6417 }
6418 } // RGWCompleteMultipart::execute
6419
6420 void RGWCompleteMultipart::complete()
6421 {
6422 /* release exclusive lock iff not already */
6423 if (unlikely(serializer && serializer->locked)) {
6424 int r = serializer->unlock();
6425 if (r < 0) {
6426 ldpp_dout(this, 0) << "WARNING: failed to unlock " << serializer->oid << dendl;
6427 }
6428 }
6429 send_response();
6430 }
6431
6432 int RGWAbortMultipart::verify_permission(optional_yield y)
6433 {
6434 if (s->iam_policy || ! s->iam_user_policies.empty() || !s->session_policies.empty()) {
6435 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
6436 boost::none,
6437 rgw::IAM::s3AbortMultipartUpload,
6438 s->object->get_obj());
6439 if (identity_policy_res == Effect::Deny) {
6440 return -EACCES;
6441 }
6442
6443 rgw::IAM::Effect e = Effect::Pass;
6444 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
6445 if (s->iam_policy) {
6446 e = s->iam_policy->eval(s->env, *s->auth.identity,
6447 rgw::IAM::s3AbortMultipartUpload,
6448 s->object->get_obj(), princ_type);
6449 }
6450
6451 if (e == Effect::Deny) {
6452 return -EACCES;
6453 }
6454
6455 if (!s->session_policies.empty()) {
6456 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
6457 boost::none,
6458 rgw::IAM::s3PutObject,
6459 s->object->get_obj());
6460 if (session_policy_res == Effect::Deny) {
6461 return -EACCES;
6462 }
6463 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
6464 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
6465 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) ||
6466 (session_policy_res == Effect::Allow && e == Effect::Allow)) {
6467 return 0;
6468 }
6469 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
6470 //Intersection of session policy and identity policy plus bucket policy
6471 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) || e == Effect::Allow) {
6472 return 0;
6473 }
6474 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
6475 if (session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) {
6476 return 0;
6477 }
6478 }
6479 return -EACCES;
6480 }
6481 if (e == Effect::Allow || identity_policy_res == Effect::Allow) {
6482 return 0;
6483 }
6484 }
6485
6486 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
6487 return -EACCES;
6488 }
6489
6490 return 0;
6491 }
6492
6493 void RGWAbortMultipart::pre_exec()
6494 {
6495 rgw_bucket_object_pre_exec(s);
6496 }
6497
6498 void RGWAbortMultipart::execute(optional_yield y)
6499 {
6500 op_ret = -EINVAL;
6501 string upload_id;
6502 string meta_oid;
6503 upload_id = s->info.args.get("uploadId");
6504 rgw_obj meta_obj;
6505 RGWMPObj mp;
6506
6507 if (upload_id.empty() || rgw::sal::RGWObject::empty(s->object.get()))
6508 return;
6509
6510 mp.init(s->object->get_name(), upload_id);
6511 meta_oid = mp.get_meta();
6512
6513 op_ret = get_multipart_info(this, s, meta_oid, nullptr);
6514 if (op_ret < 0)
6515 return;
6516
6517 RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx);
6518 op_ret = abort_multipart_upload(this, store, s->cct, obj_ctx, s->bucket->get_info(), mp);
6519 }
6520
6521 int RGWListMultipart::verify_permission(optional_yield y)
6522 {
6523 if (!verify_object_permission(this, s, rgw::IAM::s3ListMultipartUploadParts))
6524 return -EACCES;
6525
6526 return 0;
6527 }
6528
6529 void RGWListMultipart::pre_exec()
6530 {
6531 rgw_bucket_object_pre_exec(s);
6532 }
6533
6534 void RGWListMultipart::execute(optional_yield y)
6535 {
6536 string meta_oid;
6537 RGWMPObj mp;
6538
6539 op_ret = get_params(y);
6540 if (op_ret < 0)
6541 return;
6542
6543 mp.init(s->object->get_name(), upload_id);
6544 meta_oid = mp.get_meta();
6545
6546 op_ret = get_multipart_info(this, s, meta_oid, nullptr);
6547 if (op_ret < 0)
6548 return;
6549
6550 op_ret = list_multipart_parts(this, store, s, upload_id, meta_oid, max_parts,
6551 marker, parts, NULL, &truncated);
6552 }
6553
6554 int RGWListBucketMultiparts::verify_permission(optional_yield y)
6555 {
6556 if (!verify_bucket_permission(this,
6557 s,
6558 rgw::IAM::s3ListBucketMultipartUploads))
6559 return -EACCES;
6560
6561 return 0;
6562 }
6563
6564 void RGWListBucketMultiparts::pre_exec()
6565 {
6566 rgw_bucket_object_pre_exec(s);
6567 }
6568
6569 void RGWListBucketMultiparts::execute(optional_yield y)
6570 {
6571 vector<rgw_bucket_dir_entry> objs;
6572 string marker_meta;
6573
6574 op_ret = get_params(y);
6575 if (op_ret < 0)
6576 return;
6577
6578 if (s->prot_flags & RGW_REST_SWIFT) {
6579 string path_args;
6580 path_args = s->info.args.get("path");
6581 if (!path_args.empty()) {
6582 if (!delimiter.empty() || !prefix.empty()) {
6583 op_ret = -EINVAL;
6584 return;
6585 }
6586 prefix = path_args;
6587 delimiter="/";
6588 }
6589 }
6590 marker_meta = marker.get_meta();
6591
6592 op_ret = list_bucket_multiparts(this, store, s->bucket->get_info(), prefix, marker_meta, delimiter,
6593 max_uploads, &objs, &common_prefixes, &is_truncated);
6594 if (op_ret < 0) {
6595 return;
6596 }
6597
6598 if (!objs.empty()) {
6599 vector<rgw_bucket_dir_entry>::iterator iter;
6600 RGWMultipartUploadEntry entry;
6601 for (iter = objs.begin(); iter != objs.end(); ++iter) {
6602 rgw_obj_key key(iter->key);
6603 if (!entry.mp.from_meta(key.name))
6604 continue;
6605 entry.obj = *iter;
6606 uploads.push_back(entry);
6607 }
6608 next_marker = entry;
6609 }
6610 }
6611
6612 void RGWGetHealthCheck::execute(optional_yield y)
6613 {
6614 if (!g_conf()->rgw_healthcheck_disabling_path.empty() &&
6615 (::access(g_conf()->rgw_healthcheck_disabling_path.c_str(), F_OK) == 0)) {
6616 /* Disabling path specified & existent in the filesystem. */
6617 op_ret = -ERR_SERVICE_UNAVAILABLE; /* 503 */
6618 } else {
6619 op_ret = 0; /* 200 OK */
6620 }
6621 }
6622
6623 int RGWDeleteMultiObj::verify_permission(optional_yield y)
6624 {
6625 int op_ret = get_params(y);
6626 if (op_ret) {
6627 return op_ret;
6628 }
6629
6630 if (s->iam_policy || ! s->iam_user_policies.empty() || ! s->session_policies.empty()) {
6631 if (s->bucket->get_info().obj_lock_enabled() && bypass_governance_mode) {
6632 auto r = eval_identity_or_session_policies(s->iam_user_policies, s->env, boost::none,
6633 rgw::IAM::s3BypassGovernanceRetention, ARN(s->bucket->get_key()));
6634 if (r == Effect::Deny) {
6635 bypass_perm = false;
6636 } else if (r == Effect::Pass && s->iam_policy) {
6637 r = s->iam_policy->eval(s->env, *s->auth.identity, rgw::IAM::s3BypassGovernanceRetention,
6638 ARN(s->bucket->get_key()));
6639 if (r == Effect::Deny) {
6640 bypass_perm = false;
6641 }
6642 } else if (r == Effect::Pass && !s->session_policies.empty()) {
6643 r = eval_identity_or_session_policies(s->session_policies, s->env, boost::none,
6644 rgw::IAM::s3BypassGovernanceRetention, ARN(s->bucket->get_key()));
6645 if (r == Effect::Deny) {
6646 bypass_perm = false;
6647 }
6648 }
6649 }
6650
6651 bool not_versioned = rgw::sal::RGWObject::empty(s->object.get()) || s->object->get_instance().empty();
6652
6653 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
6654 boost::none,
6655 not_versioned ?
6656 rgw::IAM::s3DeleteObject :
6657 rgw::IAM::s3DeleteObjectVersion,
6658 ARN(s->bucket->get_key()));
6659 if (identity_policy_res == Effect::Deny) {
6660 return -EACCES;
6661 }
6662
6663 rgw::IAM::Effect r = Effect::Pass;
6664 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
6665 if (s->iam_policy) {
6666 r = s->iam_policy->eval(s->env, *s->auth.identity,
6667 not_versioned ?
6668 rgw::IAM::s3DeleteObject :
6669 rgw::IAM::s3DeleteObjectVersion,
6670 ARN(s->bucket->get_key()),
6671 princ_type);
6672 }
6673 if (r == Effect::Deny)
6674 return -EACCES;
6675
6676 if (!s->session_policies.empty()) {
6677 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
6678 boost::none,
6679 not_versioned ?
6680 rgw::IAM::s3DeleteObject :
6681 rgw::IAM::s3DeleteObjectVersion,
6682 ARN(s->bucket->get_key()));
6683 if (session_policy_res == Effect::Deny) {
6684 return -EACCES;
6685 }
6686 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
6687 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
6688 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) ||
6689 (session_policy_res == Effect::Allow && r == Effect::Allow)) {
6690 return 0;
6691 }
6692 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
6693 //Intersection of session policy and identity policy plus bucket policy
6694 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) || r == Effect::Allow) {
6695 return 0;
6696 }
6697 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
6698 if (session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) {
6699 return 0;
6700 }
6701 }
6702 return -EACCES;
6703 }
6704 if (r == Effect::Allow || identity_policy_res == Effect::Allow)
6705 return 0;
6706 }
6707
6708 acl_allowed = verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE);
6709 if (!acl_allowed)
6710 return -EACCES;
6711
6712 return 0;
6713 }
6714
6715 void RGWDeleteMultiObj::pre_exec()
6716 {
6717 rgw_bucket_object_pre_exec(s);
6718 }
6719
6720 void RGWDeleteMultiObj::execute(optional_yield y)
6721 {
6722 RGWMultiDelDelete *multi_delete;
6723 vector<rgw_obj_key>::iterator iter;
6724 RGWMultiDelXMLParser parser;
6725 RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx);
6726 char* buf;
6727
6728 buf = data.c_str();
6729 if (!buf) {
6730 op_ret = -EINVAL;
6731 goto error;
6732 }
6733
6734 if (!parser.init()) {
6735 op_ret = -EINVAL;
6736 goto error;
6737 }
6738
6739 if (!parser.parse(buf, data.length(), 1)) {
6740 op_ret = -EINVAL;
6741 goto error;
6742 }
6743
6744 multi_delete = static_cast<RGWMultiDelDelete *>(parser.find_first("Delete"));
6745 if (!multi_delete) {
6746 op_ret = -EINVAL;
6747 goto error;
6748 } else {
6749 #define DELETE_MULTI_OBJ_MAX_NUM 1000
6750 int max_num = s->cct->_conf->rgw_delete_multi_obj_max_num;
6751 if (max_num < 0) {
6752 max_num = DELETE_MULTI_OBJ_MAX_NUM;
6753 }
6754 int multi_delete_object_num = multi_delete->objects.size();
6755 if (multi_delete_object_num > max_num) {
6756 op_ret = -ERR_MALFORMED_XML;
6757 goto error;
6758 }
6759 }
6760
6761 if (multi_delete->is_quiet())
6762 quiet = true;
6763
6764 if (s->bucket->get_info().mfa_enabled()) {
6765 bool has_versioned = false;
6766 for (auto i : multi_delete->objects) {
6767 if (!i.instance.empty()) {
6768 has_versioned = true;
6769 break;
6770 }
6771 }
6772 if (has_versioned && !s->mfa_verified) {
6773 ldpp_dout(this, 5) << "NOTICE: multi-object delete request with a versioned object, mfa auth not provided" << dendl;
6774 op_ret = -ERR_MFA_REQUIRED;
6775 goto error;
6776 }
6777 }
6778
6779 begin_response();
6780 if (multi_delete->objects.empty()) {
6781 goto done;
6782 }
6783
6784 for (iter = multi_delete->objects.begin();
6785 iter != multi_delete->objects.end();
6786 ++iter) {
6787 std::string version_id;
6788 std::unique_ptr<rgw::sal::RGWObject> obj = bucket->get_object(*iter);
6789 if (s->iam_policy || ! s->iam_user_policies.empty() || !s->session_policies.empty()) {
6790 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
6791 boost::none,
6792 iter->instance.empty() ?
6793 rgw::IAM::s3DeleteObject :
6794 rgw::IAM::s3DeleteObjectVersion,
6795 ARN(obj->get_obj()));
6796 if (identity_policy_res == Effect::Deny) {
6797 send_partial_response(*iter, false, "", -EACCES);
6798 continue;
6799 }
6800
6801 rgw::IAM::Effect e = Effect::Pass;
6802 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
6803 if (s->iam_policy) {
6804 e = s->iam_policy->eval(s->env,
6805 *s->auth.identity,
6806 iter->instance.empty() ?
6807 rgw::IAM::s3DeleteObject :
6808 rgw::IAM::s3DeleteObjectVersion,
6809 ARN(obj->get_obj()),
6810 princ_type);
6811 }
6812 if (e == Effect::Deny) {
6813 send_partial_response(*iter, false, "", -EACCES);
6814 continue;
6815 }
6816
6817 if (!s->session_policies.empty()) {
6818 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
6819 boost::none,
6820 iter->instance.empty() ?
6821 rgw::IAM::s3DeleteObject :
6822 rgw::IAM::s3DeleteObjectVersion,
6823 ARN(obj->get_obj()));
6824 if (session_policy_res == Effect::Deny) {
6825 send_partial_response(*iter, false, "", -EACCES);
6826 continue;
6827 }
6828 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
6829 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
6830 if ((session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) &&
6831 (session_policy_res != Effect::Allow || e != Effect::Allow)) {
6832 send_partial_response(*iter, false, "", -EACCES);
6833 continue;
6834 }
6835 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
6836 //Intersection of session policy and identity policy plus bucket policy
6837 if ((session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) && e != Effect::Allow) {
6838 send_partial_response(*iter, false, "", -EACCES);
6839 continue;
6840 }
6841 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
6842 if (session_policy_res != Effect::Allow || identity_policy_res != Effect::Allow) {
6843 send_partial_response(*iter, false, "", -EACCES);
6844 continue;
6845 }
6846 }
6847 send_partial_response(*iter, false, "", -EACCES);
6848 continue;
6849 }
6850
6851 if ((identity_policy_res == Effect::Pass && e == Effect::Pass && !acl_allowed)) {
6852 send_partial_response(*iter, false, "", -EACCES);
6853 continue;
6854 }
6855 }
6856
6857 uint64_t obj_size = 0;
6858 std::string etag;
6859
6860 if (!rgw::sal::RGWObject::empty(obj.get())) {
6861 RGWObjState* astate = nullptr;
6862 bool check_obj_lock = obj->have_instance() && bucket->get_info().obj_lock_enabled();
6863 const auto ret = obj->get_obj_state(this, obj_ctx, *bucket, &astate, s->yield, true);
6864
6865 if (ret < 0) {
6866 if (ret == -ENOENT) {
6867 // object maybe delete_marker, skip check_obj_lock
6868 check_obj_lock = false;
6869 } else {
6870 // Something went wrong.
6871 send_partial_response(*iter, false, "", ret);
6872 continue;
6873 }
6874 } else {
6875 obj_size = astate->size;
6876 etag = astate->attrset[RGW_ATTR_ETAG].to_str();
6877 }
6878
6879 if (check_obj_lock) {
6880 ceph_assert(astate);
6881 int object_lock_response = verify_object_lock(this, astate->attrset, bypass_perm, bypass_governance_mode);
6882 if (object_lock_response != 0) {
6883 send_partial_response(*iter, false, "", object_lock_response);
6884 continue;
6885 }
6886 }
6887 }
6888
6889 // make reservation for notification if needed
6890 const auto versioned_object = s->bucket->versioning_enabled();
6891 rgw::notify::reservation_t res(this, store, s, obj.get());
6892 const auto event_type = versioned_object && obj->get_instance().empty() ?
6893 rgw::notify::ObjectRemovedDeleteMarkerCreated : rgw::notify::ObjectRemovedDelete;
6894 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
6895 if (op_ret < 0) {
6896 send_partial_response(*iter, false, "", op_ret);
6897 continue;
6898 }
6899
6900 obj->set_atomic(obj_ctx);
6901
6902 op_ret = obj->delete_object(this, obj_ctx, s->owner, s->bucket_owner, ceph::real_time(),
6903 false, 0, version_id, s->yield);
6904 if (op_ret == -ENOENT) {
6905 op_ret = 0;
6906 }
6907
6908 send_partial_response(*iter, obj->get_delete_marker(), version_id, op_ret);
6909
6910 // send request to notification manager
6911 const auto ret = rgw::notify::publish_commit(obj.get(), obj_size, ceph::real_clock::now(), etag, event_type, res, this);
6912 if (ret < 0) {
6913 ldpp_dout(this, 1) << "ERROR: publishing notification failed, with error: " << ret << dendl;
6914 // too late to rollback operation, hence op_ret is not set here
6915 }
6916 }
6917
6918 /* set the return code to zero, errors at this point will be
6919 dumped to the response */
6920 op_ret = 0;
6921
6922 done:
6923 // will likely segfault if begin_response() has not been called
6924 end_response();
6925 return;
6926
6927 error:
6928 send_status();
6929 return;
6930
6931 }
6932
6933 bool RGWBulkDelete::Deleter::verify_permission(RGWBucketInfo& binfo,
6934 map<string, bufferlist>& battrs,
6935 ACLOwner& bucket_owner /* out */,
6936 optional_yield y)
6937 {
6938 RGWAccessControlPolicy bacl(store->ctx());
6939 int ret = read_bucket_policy(dpp, store, s, binfo, battrs, &bacl, binfo.bucket, y);
6940 if (ret < 0) {
6941 return false;
6942 }
6943
6944 auto policy = get_iam_policy_from_attr(s->cct, battrs, binfo.bucket.tenant);
6945
6946 bucket_owner = bacl.get_owner();
6947
6948 /* We can use global user_acl because each BulkDelete request is allowed
6949 * to work on entities from a single account only. */
6950 return verify_bucket_permission(dpp, s, binfo.bucket, s->user_acl.get(),
6951 &bacl, policy, s->iam_user_policies, s->session_policies, rgw::IAM::s3DeleteBucket);
6952 }
6953
6954 bool RGWBulkDelete::Deleter::delete_single(const acct_path_t& path, optional_yield y)
6955 {
6956 std::unique_ptr<rgw::sal::RGWBucket> bucket;
6957 ACLOwner bowner;
6958 RGWObjVersionTracker ot;
6959
6960 int ret = store->get_bucket(dpp, s->user.get(), s->user->get_tenant(), path.bucket_name, &bucket, y);
6961 if (ret < 0) {
6962 goto binfo_fail;
6963 }
6964
6965 ret = bucket->get_bucket_info(dpp, s->yield);
6966 if (ret < 0) {
6967 goto binfo_fail;
6968 }
6969
6970 if (!verify_permission(bucket->get_info(), bucket->get_attrs(), bowner, y)) {
6971 ret = -EACCES;
6972 goto auth_fail;
6973 }
6974
6975 if (!path.obj_key.empty()) {
6976 ACLOwner bucket_owner;
6977 std::string version_id;
6978
6979 bucket_owner.set_id(bucket->get_info().owner);
6980 std::unique_ptr<rgw::sal::RGWObject> obj = bucket->get_object(path.obj_key);
6981 obj->set_atomic(s->obj_ctx);
6982
6983 ret = obj->delete_object(dpp, s->obj_ctx, bowner, bucket_owner, ceph::real_time(), false, 0,
6984 version_id, s->yield);
6985 if (ret < 0) {
6986 goto delop_fail;
6987 }
6988 } else {
6989 ret = bucket->remove_bucket(dpp, false, true, &s->info, s->yield);
6990 if (ret < 0) {
6991 goto delop_fail;
6992 }
6993 }
6994
6995 num_deleted++;
6996 return true;
6997
6998
6999 binfo_fail:
7000 if (-ENOENT == ret) {
7001 ldpp_dout(dpp, 20) << "cannot find bucket = " << path.bucket_name << dendl;
7002 num_unfound++;
7003 } else {
7004 ldpp_dout(dpp, 20) << "cannot get bucket info, ret = " << ret << dendl;
7005
7006 fail_desc_t failed_item = {
7007 .err = ret,
7008 .path = path
7009 };
7010 failures.push_back(failed_item);
7011 }
7012 return false;
7013
7014 auth_fail:
7015 ldpp_dout(dpp, 20) << "wrong auth for " << path << dendl;
7016 {
7017 fail_desc_t failed_item = {
7018 .err = ret,
7019 .path = path
7020 };
7021 failures.push_back(failed_item);
7022 }
7023 return false;
7024
7025 delop_fail:
7026 if (-ENOENT == ret) {
7027 ldpp_dout(dpp, 20) << "cannot find entry " << path << dendl;
7028 num_unfound++;
7029 } else {
7030 fail_desc_t failed_item = {
7031 .err = ret,
7032 .path = path
7033 };
7034 failures.push_back(failed_item);
7035 }
7036 return false;
7037 }
7038
7039 bool RGWBulkDelete::Deleter::delete_chunk(const std::list<acct_path_t>& paths, optional_yield y)
7040 {
7041 ldpp_dout(dpp, 20) << "in delete_chunk" << dendl;
7042 for (auto path : paths) {
7043 ldpp_dout(dpp, 20) << "bulk deleting path: " << path << dendl;
7044 delete_single(path, y);
7045 }
7046
7047 return true;
7048 }
7049
7050 int RGWBulkDelete::verify_permission(optional_yield y)
7051 {
7052 return 0;
7053 }
7054
7055 void RGWBulkDelete::pre_exec()
7056 {
7057 rgw_bucket_object_pre_exec(s);
7058 }
7059
7060 void RGWBulkDelete::execute(optional_yield y)
7061 {
7062 deleter = std::unique_ptr<Deleter>(new Deleter(this, store, s));
7063
7064 bool is_truncated = false;
7065 do {
7066 list<RGWBulkDelete::acct_path_t> items;
7067
7068 int ret = get_data(items, &is_truncated);
7069 if (ret < 0) {
7070 return;
7071 }
7072
7073 ret = deleter->delete_chunk(items, y);
7074 } while (!op_ret && is_truncated);
7075
7076 return;
7077 }
7078
7079
7080 constexpr std::array<int, 2> RGWBulkUploadOp::terminal_errors;
7081
7082 int RGWBulkUploadOp::verify_permission(optional_yield y)
7083 {
7084 if (s->auth.identity->is_anonymous()) {
7085 return -EACCES;
7086 }
7087
7088 if (! verify_user_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7089 return -EACCES;
7090 }
7091
7092 if (s->user->get_tenant() != s->bucket_tenant) {
7093 ldpp_dout(this, 10) << "user cannot create a bucket in a different tenant"
7094 << " (user_id.tenant=" << s->user->get_tenant()
7095 << " requested=" << s->bucket_tenant << ")" << dendl;
7096 return -EACCES;
7097 }
7098
7099 if (s->user->get_max_buckets() < 0) {
7100 return -EPERM;
7101 }
7102
7103 return 0;
7104 }
7105
7106 void RGWBulkUploadOp::pre_exec()
7107 {
7108 rgw_bucket_object_pre_exec(s);
7109 }
7110
7111 boost::optional<std::pair<std::string, rgw_obj_key>>
7112 RGWBulkUploadOp::parse_path(const std::string_view& path)
7113 {
7114 /* We need to skip all slashes at the beginning in order to preserve
7115 * compliance with Swift. */
7116 const size_t start_pos = path.find_first_not_of('/');
7117
7118 if (std::string_view::npos != start_pos) {
7119 /* Seperator is the first slash after the leading ones. */
7120 const size_t sep_pos = path.substr(start_pos).find('/');
7121
7122 if (std::string_view::npos != sep_pos) {
7123 const auto bucket_name = path.substr(start_pos, sep_pos - start_pos);
7124 const auto obj_name = path.substr(sep_pos + 1);
7125
7126 return std::make_pair(std::string(bucket_name),
7127 rgw_obj_key(std::string(obj_name)));
7128 } else {
7129 /* It's guaranteed here that bucket name is at least one character
7130 * long and is different than slash. */
7131 return std::make_pair(std::string(path.substr(start_pos)),
7132 rgw_obj_key());
7133 }
7134 }
7135
7136 return none;
7137 }
7138
7139 std::pair<std::string, std::string>
7140 RGWBulkUploadOp::handle_upload_path(struct req_state *s)
7141 {
7142 std::string bucket_path, file_prefix;
7143 if (! s->init_state.url_bucket.empty()) {
7144 file_prefix = bucket_path = s->init_state.url_bucket + "/";
7145 if (!rgw::sal::RGWObject::empty(s->object.get())) {
7146 const std::string& object_name = s->object->get_name();
7147
7148 /* As rgw_obj_key::empty() already verified emptiness of s->object->get_name(),
7149 * we can safely examine its last element. */
7150 if (object_name.back() == '/') {
7151 file_prefix.append(object_name);
7152 } else {
7153 file_prefix.append(object_name).append("/");
7154 }
7155 }
7156 }
7157 return std::make_pair(bucket_path, file_prefix);
7158 }
7159
7160 int RGWBulkUploadOp::handle_dir_verify_permission(optional_yield y)
7161 {
7162 if (s->user->get_max_buckets() > 0) {
7163 rgw::sal::RGWBucketList buckets;
7164 std::string marker;
7165 op_ret = rgw_read_user_buckets(this, store, s->user->get_user(), buckets,
7166 marker, std::string(), s->user->get_max_buckets(),
7167 false, y);
7168 if (op_ret < 0) {
7169 return op_ret;
7170 }
7171
7172 if (buckets.count() >= static_cast<size_t>(s->user->get_max_buckets())) {
7173 return -ERR_TOO_MANY_BUCKETS;
7174 }
7175 }
7176
7177 return 0;
7178 }
7179
7180 static void forward_req_info(CephContext *cct, req_info& info, const std::string& bucket_name)
7181 {
7182 /* the request of container or object level will contain bucket name.
7183 * only at account level need to append the bucket name */
7184 if (info.script_uri.find(bucket_name) != std::string::npos) {
7185 return;
7186 }
7187
7188 ldout(cct, 20) << "append the bucket: "<< bucket_name << " to req_info" << dendl;
7189 info.script_uri.append("/").append(bucket_name);
7190 info.request_uri_aws4 = info.request_uri = info.script_uri;
7191 info.effective_uri = "/" + bucket_name;
7192 }
7193
7194 void RGWBulkUploadOp::init(rgw::sal::RGWRadosStore* const store,
7195 struct req_state* const s,
7196 RGWHandler* const h)
7197 {
7198 RGWOp::init(store, s, h);
7199 dir_ctx.emplace(store->svc()->sysobj->init_obj_ctx());
7200 }
7201
7202 int RGWBulkUploadOp::handle_dir(const std::string_view path, optional_yield y)
7203 {
7204 ldpp_dout(this, 20) << "got directory=" << path << dendl;
7205
7206 op_ret = handle_dir_verify_permission(y);
7207 if (op_ret < 0) {
7208 return op_ret;
7209 }
7210
7211 std::string bucket_name;
7212 rgw_obj_key object_junk;
7213 std::tie(bucket_name, object_junk) = *parse_path(path);
7214
7215 rgw_raw_obj obj(store->svc()->zone->get_zone_params().domain_root,
7216 rgw_make_bucket_entry_name(s->bucket_tenant, bucket_name));
7217
7218 /* we need to make sure we read bucket info, it's not read before for this
7219 * specific request */
7220 std::unique_ptr<rgw::sal::RGWBucket> bucket;
7221
7222 /* Create metadata: ACLs. */
7223 std::map<std::string, ceph::bufferlist> attrs;
7224 RGWAccessControlPolicy policy;
7225 policy.create_default(s->user->get_id(), s->user->get_display_name());
7226 ceph::bufferlist aclbl;
7227 policy.encode(aclbl);
7228 attrs.emplace(RGW_ATTR_ACL, std::move(aclbl));
7229
7230 obj_version objv, ep_objv;
7231 bool bucket_exists;
7232 RGWQuotaInfo quota_info;
7233 const RGWQuotaInfo* pquota_info = nullptr;
7234 RGWBucketInfo out_info;
7235 string swift_ver_location;
7236 rgw_bucket new_bucket;
7237 req_info info = s->info;
7238 new_bucket.tenant = s->bucket_tenant; /* ignored if bucket exists */
7239 new_bucket.name = bucket_name;
7240 rgw_placement_rule placement_rule;
7241 placement_rule.storage_class = s->info.storage_class;
7242 forward_req_info(s->cct, info, bucket_name);
7243
7244 op_ret = store->create_bucket(this, *s->user, new_bucket,
7245 store->get_zonegroup().get_id(),
7246 placement_rule, swift_ver_location,
7247 pquota_info, policy, attrs,
7248 out_info, ep_objv,
7249 true, false, &bucket_exists,
7250 info, &bucket, y);
7251 /* continue if EEXIST and create_bucket will fail below. this way we can
7252 * recover from a partial create by retrying it. */
7253 ldpp_dout(this, 20) << "rgw_create_bucket returned ret=" << op_ret
7254 << ", bucket=" << bucket << dendl;
7255
7256 if (op_ret && op_ret != -EEXIST) {
7257 return op_ret;
7258 }
7259
7260 const bool existed = (op_ret == -EEXIST);
7261 if (existed) {
7262 /* bucket already existed, might have raced with another bucket creation, or
7263 * might be partial bucket creation that never completed. Read existing bucket
7264 * info, verify that the reported bucket owner is the current user.
7265 * If all is ok then update the user's list of buckets.
7266 * Otherwise inform client about a name conflict.
7267 */
7268 if (out_info.owner.compare(s->user->get_id()) != 0) {
7269 op_ret = -EEXIST;
7270 ldpp_dout(this, 20) << "conflicting bucket name" << dendl;
7271 return op_ret;
7272 }
7273 new_bucket = out_info.bucket;
7274 }
7275
7276 op_ret = store->ctl()->bucket->link_bucket(s->user->get_id(), new_bucket,
7277 out_info.creation_time,
7278 s->yield, s, false);
7279 if (op_ret && !existed && op_ret != -EEXIST) {
7280 /* if it exists (or previously existed), don't remove it! */
7281 op_ret = store->ctl()->bucket->unlink_bucket(s->user->get_id(), new_bucket, s->yield, this);
7282 if (op_ret < 0) {
7283 ldpp_dout(this, 0) << "WARNING: failed to unlink bucket: ret=" << op_ret << dendl;
7284 }
7285 } else if (op_ret == -EEXIST || (op_ret == 0 && existed)) {
7286 ldpp_dout(this, 20) << "containers already exists" << dendl;
7287 op_ret = -ERR_BUCKET_EXISTS;
7288 }
7289
7290 return op_ret;
7291 }
7292
7293
7294 bool RGWBulkUploadOp::handle_file_verify_permission(RGWBucketInfo& binfo,
7295 const rgw_obj& obj,
7296 std::map<std::string, ceph::bufferlist>& battrs,
7297 ACLOwner& bucket_owner /* out */,
7298 optional_yield y)
7299 {
7300 RGWAccessControlPolicy bacl(store->ctx());
7301 op_ret = read_bucket_policy(this, store, s, binfo, battrs, &bacl, binfo.bucket, y);
7302 if (op_ret < 0) {
7303 ldpp_dout(this, 20) << "cannot read_policy() for bucket" << dendl;
7304 return false;
7305 }
7306
7307 auto policy = get_iam_policy_from_attr(s->cct, battrs, binfo.bucket.tenant);
7308
7309 bucket_owner = bacl.get_owner();
7310 if (policy || ! s->iam_user_policies.empty() || !s->session_policies.empty()) {
7311 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
7312 boost::none,
7313 rgw::IAM::s3PutObject, obj);
7314 if (identity_policy_res == Effect::Deny) {
7315 return false;
7316 }
7317
7318 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
7319 auto e = policy->eval(s->env, *s->auth.identity,
7320 rgw::IAM::s3PutObject, obj, princ_type);
7321 if (e == Effect::Deny) {
7322 return false;
7323 }
7324
7325 if (!s->session_policies.empty()) {
7326 auto session_policy_res = eval_identity_or_session_policies(s->session_policies, s->env,
7327 boost::none,
7328 rgw::IAM::s3PutObject, obj);
7329 if (session_policy_res == Effect::Deny) {
7330 return false;
7331 }
7332 if (princ_type == rgw::IAM::PolicyPrincipal::Role) {
7333 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
7334 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) ||
7335 (session_policy_res == Effect::Allow && e == Effect::Allow)) {
7336 return true;
7337 }
7338 } else if (princ_type == rgw::IAM::PolicyPrincipal::Session) {
7339 //Intersection of session policy and identity policy plus bucket policy
7340 if ((session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) || e == Effect::Allow) {
7341 return true;
7342 }
7343 } else if (princ_type == rgw::IAM::PolicyPrincipal::Other) {// there was no match in the bucket policy
7344 if (session_policy_res == Effect::Allow && identity_policy_res == Effect::Allow) {
7345 return true;
7346 }
7347 }
7348 return false;
7349 }
7350 if (e == Effect::Allow || identity_policy_res == Effect::Allow) {
7351 return true;
7352 }
7353 }
7354
7355 return verify_bucket_permission_no_policy(this, s, s->user_acl.get(),
7356 &bacl, RGW_PERM_WRITE);
7357 }
7358
7359 int RGWBulkUploadOp::handle_file(const std::string_view path,
7360 const size_t size,
7361 AlignedStreamGetter& body, optional_yield y)
7362 {
7363
7364 ldpp_dout(this, 20) << "got file=" << path << ", size=" << size << dendl;
7365
7366 if (size > static_cast<size_t>(s->cct->_conf->rgw_max_put_size)) {
7367 op_ret = -ERR_TOO_LARGE;
7368 return op_ret;
7369 }
7370
7371 std::string bucket_name;
7372 rgw_obj_key object;
7373 std::tie(bucket_name, object) = *parse_path(path);
7374
7375 auto& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
7376 std::unique_ptr<rgw::sal::RGWBucket> bucket;
7377 ACLOwner bowner;
7378
7379 op_ret = store->get_bucket(this, s->user.get(), rgw_bucket(rgw_bucket_key(s->user->get_tenant(), bucket_name)), &bucket, y);
7380 if (op_ret == -ENOENT) {
7381 ldpp_dout(this, 20) << "non existent directory=" << bucket_name << dendl;
7382 } else if (op_ret < 0) {
7383 return op_ret;
7384 }
7385
7386 std::unique_ptr<rgw::sal::RGWObject> obj = bucket->get_object(object);
7387
7388 if (! handle_file_verify_permission(bucket->get_info(),
7389 obj->get_obj(),
7390 bucket->get_attrs(), bowner, y)) {
7391 ldpp_dout(this, 20) << "object creation unauthorized" << dendl;
7392 op_ret = -EACCES;
7393 return op_ret;
7394 }
7395
7396 op_ret = bucket->check_quota(user_quota, bucket_quota, size, y);
7397 if (op_ret < 0) {
7398 return op_ret;
7399 }
7400
7401 if (bucket->versioning_enabled()) {
7402 obj->gen_rand_obj_instance_name();
7403 }
7404
7405 rgw_placement_rule dest_placement = s->dest_placement;
7406 dest_placement.inherit_from(bucket->get_placement_rule());
7407
7408 auto aio = rgw::make_throttle(s->cct->_conf->rgw_put_obj_min_window_size,
7409 s->yield);
7410
7411 using namespace rgw::putobj;
7412 AtomicObjectProcessor processor(&*aio, store, bucket.get(), &s->dest_placement, bowner.get_id(),
7413 obj_ctx, std::move(obj), 0, s->req_id, this, s->yield);
7414
7415 op_ret = processor.prepare(s->yield);
7416 if (op_ret < 0) {
7417 ldpp_dout(this, 20) << "cannot prepare processor due to ret=" << op_ret << dendl;
7418 return op_ret;
7419 }
7420
7421 /* No filters by default. */
7422 DataProcessor *filter = &processor;
7423
7424 const auto& compression_type = store->svc()->zone->get_zone_params().get_compression_type(
7425 dest_placement);
7426 CompressorRef plugin;
7427 boost::optional<RGWPutObj_Compress> compressor;
7428 if (compression_type != "none") {
7429 plugin = Compressor::create(s->cct, compression_type);
7430 if (! plugin) {
7431 ldpp_dout(this, 1) << "Cannot load plugin for rgw_compression_type "
7432 << compression_type << dendl;
7433 } else {
7434 compressor.emplace(s->cct, plugin, filter);
7435 filter = &*compressor;
7436 }
7437 }
7438
7439 /* Upload file content. */
7440 ssize_t len = 0;
7441 size_t ofs = 0;
7442 MD5 hash;
7443 do {
7444 ceph::bufferlist data;
7445 len = body.get_at_most(s->cct->_conf->rgw_max_chunk_size, data);
7446
7447 ldpp_dout(this, 20) << "body=" << data.c_str() << dendl;
7448 if (len < 0) {
7449 op_ret = len;
7450 return op_ret;
7451 } else if (len > 0) {
7452 hash.Update((const unsigned char *)data.c_str(), data.length());
7453 op_ret = filter->process(std::move(data), ofs);
7454 if (op_ret < 0) {
7455 ldpp_dout(this, 20) << "filter->process() returned ret=" << op_ret << dendl;
7456 return op_ret;
7457 }
7458
7459 ofs += len;
7460 }
7461
7462 } while (len > 0);
7463
7464 // flush
7465 op_ret = filter->process({}, ofs);
7466 if (op_ret < 0) {
7467 return op_ret;
7468 }
7469
7470 if (ofs != size) {
7471 ldpp_dout(this, 10) << "real file size different from declared" << dendl;
7472 op_ret = -EINVAL;
7473 return op_ret;
7474 }
7475
7476 op_ret = bucket->check_quota(user_quota, bucket_quota, size, y);
7477 if (op_ret < 0) {
7478 ldpp_dout(this, 20) << "quota exceeded for path=" << path << dendl;
7479 return op_ret;
7480 }
7481
7482 char calc_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1];
7483 unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE];
7484 hash.Final(m);
7485 buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, calc_md5);
7486
7487 /* Create metadata: ETAG. */
7488 std::map<std::string, ceph::bufferlist> attrs;
7489 std::string etag = calc_md5;
7490 ceph::bufferlist etag_bl;
7491 etag_bl.append(etag.c_str(), etag.size() + 1);
7492 attrs.emplace(RGW_ATTR_ETAG, std::move(etag_bl));
7493
7494 /* Create metadata: ACLs. */
7495 RGWAccessControlPolicy policy;
7496 policy.create_default(s->user->get_id(), s->user->get_display_name());
7497 ceph::bufferlist aclbl;
7498 policy.encode(aclbl);
7499 attrs.emplace(RGW_ATTR_ACL, std::move(aclbl));
7500
7501 /* Create metadata: compression info. */
7502 if (compressor && compressor->is_compressed()) {
7503 ceph::bufferlist tmp;
7504 RGWCompressionInfo cs_info;
7505 cs_info.compression_type = plugin->get_type_name();
7506 cs_info.orig_size = s->obj_size;
7507 cs_info.compressor_message = compressor->get_compressor_message();
7508 cs_info.blocks = std::move(compressor->get_compression_blocks());
7509 encode(cs_info, tmp);
7510 attrs.emplace(RGW_ATTR_COMPRESSION, std::move(tmp));
7511 }
7512
7513 /* Complete the transaction. */
7514 op_ret = processor.complete(size, etag, nullptr, ceph::real_time(),
7515 attrs, ceph::real_time() /* delete_at */,
7516 nullptr, nullptr, nullptr, nullptr, nullptr,
7517 s->yield);
7518 if (op_ret < 0) {
7519 ldpp_dout(this, 20) << "processor::complete returned op_ret=" << op_ret << dendl;
7520 }
7521
7522 return op_ret;
7523 }
7524
7525 void RGWBulkUploadOp::execute(optional_yield y)
7526 {
7527 ceph::bufferlist buffer(64 * 1024);
7528
7529 ldpp_dout(this, 20) << "start" << dendl;
7530
7531 /* Create an instance of stream-abstracting class. Having this indirection
7532 * allows for easy introduction of decompressors like gzip and bzip2. */
7533 auto stream = create_stream();
7534 if (! stream) {
7535 return;
7536 }
7537
7538 /* Handling the $UPLOAD_PATH accordingly to the Swift's Bulk middleware. See:
7539 * https://github.com/openstack/swift/blob/2.13.0/swift/common/middleware/bulk.py#L31-L41 */
7540 std::string bucket_path, file_prefix;
7541 std::tie(bucket_path, file_prefix) = handle_upload_path(s);
7542
7543 auto status = rgw::tar::StatusIndicator::create();
7544 do {
7545 op_ret = stream->get_exactly(rgw::tar::BLOCK_SIZE, buffer);
7546 if (op_ret < 0) {
7547 ldpp_dout(this, 2) << "cannot read header" << dendl;
7548 return;
7549 }
7550
7551 /* We need to re-interpret the buffer as a TAR block. Exactly two blocks
7552 * must be tracked to detect out end-of-archive. It occurs when both of
7553 * them are empty (zeroed). Tracing this particular inter-block dependency
7554 * is responsibility of the rgw::tar::StatusIndicator class. */
7555 boost::optional<rgw::tar::HeaderView> header;
7556 std::tie(status, header) = rgw::tar::interpret_block(status, buffer);
7557
7558 if (! status.empty() && header) {
7559 /* This specific block isn't empty (entirely zeroed), so we can parse
7560 * it as a TAR header and dispatch. At the moment we do support only
7561 * regular files and directories. Everything else (symlinks, devices)
7562 * will be ignored but won't cease the whole upload. */
7563 switch (header->get_filetype()) {
7564 case rgw::tar::FileType::NORMAL_FILE: {
7565 ldpp_dout(this, 2) << "handling regular file" << dendl;
7566
7567 std::string_view filename;
7568 if (bucket_path.empty())
7569 filename = header->get_filename();
7570 else
7571 filename = file_prefix + std::string(header->get_filename());
7572 auto body = AlignedStreamGetter(0, header->get_filesize(),
7573 rgw::tar::BLOCK_SIZE, *stream);
7574 op_ret = handle_file(filename,
7575 header->get_filesize(),
7576 body, y);
7577 if (! op_ret) {
7578 /* Only regular files counts. */
7579 num_created++;
7580 } else {
7581 failures.emplace_back(op_ret, std::string(filename));
7582 }
7583 break;
7584 }
7585 case rgw::tar::FileType::DIRECTORY: {
7586 ldpp_dout(this, 2) << "handling regular directory" << dendl;
7587
7588 std::string_view dirname = bucket_path.empty() ? header->get_filename() : bucket_path;
7589 op_ret = handle_dir(dirname, y);
7590 if (op_ret < 0 && op_ret != -ERR_BUCKET_EXISTS) {
7591 failures.emplace_back(op_ret, std::string(dirname));
7592 }
7593 break;
7594 }
7595 default: {
7596 /* Not recognized. Skip. */
7597 op_ret = 0;
7598 break;
7599 }
7600 }
7601
7602 /* In case of any problems with sub-request authorization Swift simply
7603 * terminates whole upload immediately. */
7604 if (boost::algorithm::contains(std::initializer_list<int>{ op_ret },
7605 terminal_errors)) {
7606 ldpp_dout(this, 2) << "terminating due to ret=" << op_ret << dendl;
7607 break;
7608 }
7609 } else {
7610 ldpp_dout(this, 2) << "an empty block" << dendl;
7611 op_ret = 0;
7612 }
7613
7614 buffer.clear();
7615 } while (! status.eof());
7616
7617 return;
7618 }
7619
7620 RGWBulkUploadOp::AlignedStreamGetter::~AlignedStreamGetter()
7621 {
7622 const size_t aligned_legnth = length + (-length % alignment);
7623 ceph::bufferlist junk;
7624
7625 DecoratedStreamGetter::get_exactly(aligned_legnth - position, junk);
7626 }
7627
7628 ssize_t RGWBulkUploadOp::AlignedStreamGetter::get_at_most(const size_t want,
7629 ceph::bufferlist& dst)
7630 {
7631 const size_t max_to_read = std::min(want, length - position);
7632 const auto len = DecoratedStreamGetter::get_at_most(max_to_read, dst);
7633 if (len > 0) {
7634 position += len;
7635 }
7636 return len;
7637 }
7638
7639 ssize_t RGWBulkUploadOp::AlignedStreamGetter::get_exactly(const size_t want,
7640 ceph::bufferlist& dst)
7641 {
7642 const auto len = DecoratedStreamGetter::get_exactly(want, dst);
7643 if (len > 0) {
7644 position += len;
7645 }
7646 return len;
7647 }
7648
7649 int RGWGetAttrs::verify_permission(optional_yield y)
7650 {
7651 s->object->set_atomic(s->obj_ctx);
7652
7653 auto iam_action = s->object->get_instance().empty() ?
7654 rgw::IAM::s3GetObject :
7655 rgw::IAM::s3GetObjectVersion;
7656
7657 if (!verify_object_permission(this, s, iam_action)) {
7658 return -EACCES;
7659 }
7660
7661 return 0;
7662 }
7663
7664 void RGWGetAttrs::pre_exec()
7665 {
7666 rgw_bucket_object_pre_exec(s);
7667 }
7668
7669 void RGWGetAttrs::execute(optional_yield y)
7670 {
7671 op_ret = get_params();
7672 if (op_ret < 0)
7673 return;
7674
7675 s->object->set_atomic(s->obj_ctx);
7676
7677 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, this);
7678 if (op_ret < 0) {
7679 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << s->object
7680 << " ret=" << op_ret << dendl;
7681 return;
7682 }
7683
7684 /* XXX RGWObject::get_obj_attrs() does not support filtering (yet) */
7685 auto& obj_attrs = s->object->get_attrs();
7686 if (attrs.size() != 0) {
7687 /* return only attrs requested */
7688 for (auto& att : attrs) {
7689 auto iter = obj_attrs.find(att.first);
7690 if (iter != obj_attrs.end()) {
7691 att.second = iter->second;
7692 }
7693 }
7694 } else {
7695 /* return all attrs */
7696 for (auto& att : obj_attrs) {
7697 attrs.insert(get_attrs_t::value_type(att.first, att.second));;
7698 }
7699 }
7700
7701 return;
7702 }
7703
7704 int RGWRMAttrs::verify_permission(optional_yield y)
7705 {
7706 // This looks to be part of the RGW-NFS machinery and has no S3 or
7707 // Swift equivalent.
7708 bool perm;
7709 if (!rgw::sal::RGWObject::empty(s->object.get())) {
7710 perm = verify_object_permission_no_policy(this, s, RGW_PERM_WRITE);
7711 } else {
7712 perm = verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE);
7713 }
7714 if (!perm)
7715 return -EACCES;
7716
7717 return 0;
7718 }
7719
7720 void RGWRMAttrs::pre_exec()
7721 {
7722 rgw_bucket_object_pre_exec(s);
7723 }
7724
7725 void RGWRMAttrs::execute(optional_yield y)
7726 {
7727 op_ret = get_params();
7728 if (op_ret < 0)
7729 return;
7730
7731 s->object->set_atomic(s->obj_ctx);
7732
7733 op_ret = s->object->set_obj_attrs(this, s->obj_ctx, nullptr, &attrs, y);
7734 if (op_ret < 0) {
7735 ldpp_dout(this, 0) << "ERROR: failed to delete obj attrs, obj=" << s->object
7736 << " ret=" << op_ret << dendl;
7737 }
7738 return;
7739 }
7740
7741 int RGWSetAttrs::verify_permission(optional_yield y)
7742 {
7743 // This looks to be part of the RGW-NFS machinery and has no S3 or
7744 // Swift equivalent.
7745 bool perm;
7746 if (!rgw::sal::RGWObject::empty(s->object.get())) {
7747 perm = verify_object_permission_no_policy(this, s, RGW_PERM_WRITE);
7748 } else {
7749 perm = verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE);
7750 }
7751 if (!perm)
7752 return -EACCES;
7753
7754 return 0;
7755 }
7756
7757 void RGWSetAttrs::pre_exec()
7758 {
7759 rgw_bucket_object_pre_exec(s);
7760 }
7761
7762 void RGWSetAttrs::execute(optional_yield y)
7763 {
7764 op_ret = get_params(y);
7765 if (op_ret < 0)
7766 return;
7767
7768 if (!rgw::sal::RGWObject::empty(s->object.get())) {
7769 rgw::sal::RGWAttrs a(attrs);
7770 op_ret = s->object->set_obj_attrs(this, s->obj_ctx, &a, nullptr, y);
7771 } else {
7772 op_ret = store->ctl()->bucket->set_bucket_instance_attrs(
7773 s->bucket->get_info(), attrs, &s->bucket->get_info().objv_tracker,
7774 s->yield, this);
7775 }
7776
7777 } /* RGWSetAttrs::execute() */
7778
7779 void RGWGetObjLayout::pre_exec()
7780 {
7781 rgw_bucket_object_pre_exec(s);
7782 }
7783
7784 void RGWGetObjLayout::execute(optional_yield y)
7785 {
7786 /* Make sure bucket is correct */
7787 s->object->set_bucket(s->bucket.get());
7788
7789 std::unique_ptr<rgw::sal::RGWObject::ReadOp> stat_op(s->object->get_read_op(s->obj_ctx));
7790
7791
7792 op_ret = stat_op->prepare(y, this);
7793 if (op_ret < 0) {
7794 return;
7795 }
7796
7797 head_obj = stat_op->result.head_obj;
7798
7799 op_ret = stat_op->get_manifest(this, &manifest, y);
7800 }
7801
7802
7803 int RGWConfigBucketMetaSearch::verify_permission(optional_yield y)
7804 {
7805 if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) {
7806 return -EACCES;
7807 }
7808
7809 return 0;
7810 }
7811
7812 void RGWConfigBucketMetaSearch::pre_exec()
7813 {
7814 rgw_bucket_object_pre_exec(s);
7815 }
7816
7817 void RGWConfigBucketMetaSearch::execute(optional_yield y)
7818 {
7819 op_ret = get_params(y);
7820 if (op_ret < 0) {
7821 ldpp_dout(this, 20) << "NOTICE: get_params() returned ret=" << op_ret << dendl;
7822 return;
7823 }
7824
7825 s->bucket->get_info().mdsearch_config = mdsearch_config;
7826
7827 op_ret = s->bucket->put_instance_info(this, false, real_time());
7828 if (op_ret < 0) {
7829 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
7830 << " returned err=" << op_ret << dendl;
7831 return;
7832 }
7833 s->bucket_attrs = s->bucket->get_attrs();
7834 }
7835
7836 int RGWGetBucketMetaSearch::verify_permission(optional_yield y)
7837 {
7838 if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) {
7839 return -EACCES;
7840 }
7841
7842 return 0;
7843 }
7844
7845 void RGWGetBucketMetaSearch::pre_exec()
7846 {
7847 rgw_bucket_object_pre_exec(s);
7848 }
7849
7850 int RGWDelBucketMetaSearch::verify_permission(optional_yield y)
7851 {
7852 if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) {
7853 return -EACCES;
7854 }
7855
7856 return 0;
7857 }
7858
7859 void RGWDelBucketMetaSearch::pre_exec()
7860 {
7861 rgw_bucket_object_pre_exec(s);
7862 }
7863
7864 void RGWDelBucketMetaSearch::execute(optional_yield y)
7865 {
7866 s->bucket->get_info().mdsearch_config.clear();
7867
7868 op_ret = s->bucket->put_instance_info(this, false, real_time());
7869 if (op_ret < 0) {
7870 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
7871 << " returned err=" << op_ret << dendl;
7872 return;
7873 }
7874 s->bucket_attrs = s->bucket->get_attrs();
7875 }
7876
7877
7878 RGWHandler::~RGWHandler()
7879 {
7880 }
7881
7882 int RGWHandler::init(rgw::sal::RGWRadosStore *_store,
7883 struct req_state *_s,
7884 rgw::io::BasicClient *cio)
7885 {
7886 store = _store;
7887 s = _s;
7888
7889 return 0;
7890 }
7891
7892 int RGWHandler::do_init_permissions(const DoutPrefixProvider *dpp, optional_yield y)
7893 {
7894 int ret = rgw_build_bucket_policies(dpp, store, s, y);
7895 if (ret < 0) {
7896 ldpp_dout(dpp, 10) << "init_permissions on " << s->bucket
7897 << " failed, ret=" << ret << dendl;
7898 return ret==-ENODATA ? -EACCES : ret;
7899 }
7900
7901 rgw_build_iam_environment(store, s);
7902 return ret;
7903 }
7904
7905 int RGWHandler::do_read_permissions(RGWOp *op, bool only_bucket, optional_yield y)
7906 {
7907 if (only_bucket) {
7908 /* already read bucket info */
7909 return 0;
7910 }
7911 int ret = rgw_build_object_policies(op, store, s, op->prefetch_data(), y);
7912
7913 if (ret < 0) {
7914 ldpp_dout(op, 10) << "read_permissions on " << s->bucket << ":"
7915 << s->object << " only_bucket=" << only_bucket
7916 << " ret=" << ret << dendl;
7917 if (ret == -ENODATA)
7918 ret = -EACCES;
7919 if (s->auth.identity->is_anonymous() && ret == -EACCES)
7920 ret = -EPERM;
7921 }
7922
7923 return ret;
7924 }
7925
7926 int RGWOp::error_handler(int err_no, string *error_content, optional_yield y) {
7927 return dialect_handler->error_handler(err_no, error_content, y);
7928 }
7929
7930 int RGWHandler::error_handler(int err_no, string *error_content, optional_yield) {
7931 // This is the do-nothing error handler
7932 return err_no;
7933 }
7934
7935 std::ostream& RGWOp::gen_prefix(std::ostream& out) const
7936 {
7937 // append <dialect>:<op name> to the prefix
7938 return s->gen_prefix(out) << s->dialect << ':' << name() << ' ';
7939 }
7940
7941 void RGWDefaultResponseOp::send_response() {
7942 if (op_ret) {
7943 set_req_state_err(s, op_ret);
7944 }
7945 dump_errno(s);
7946 end_header(s);
7947 }
7948
7949 void RGWPutBucketPolicy::send_response()
7950 {
7951 if (!op_ret) {
7952 /* A successful Put Bucket Policy should return a 204 on success */
7953 op_ret = STATUS_NO_CONTENT;
7954 }
7955 if (op_ret) {
7956 set_req_state_err(s, op_ret);
7957 }
7958 dump_errno(s);
7959 end_header(s);
7960 }
7961
7962 int RGWPutBucketPolicy::verify_permission(optional_yield y)
7963 {
7964 if (!verify_bucket_permission(this, s, rgw::IAM::s3PutBucketPolicy)) {
7965 return -EACCES;
7966 }
7967
7968 return 0;
7969 }
7970
7971 int RGWPutBucketPolicy::get_params(optional_yield y)
7972 {
7973 const auto max_size = s->cct->_conf->rgw_max_put_param_size;
7974 // At some point when I have more time I want to make a version of
7975 // rgw_rest_read_all_input that doesn't use malloc.
7976 std::tie(op_ret, data) = rgw_rest_read_all_input(s, max_size, false);
7977
7978 // And throws exceptions.
7979 return op_ret;
7980 }
7981
7982 void RGWPutBucketPolicy::execute(optional_yield y)
7983 {
7984 op_ret = get_params(y);
7985 if (op_ret < 0) {
7986 return;
7987 }
7988
7989 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
7990 if (op_ret < 0) {
7991 ldpp_dout(this, 20) << "forward_request_to_master returned ret=" << op_ret << dendl;
7992 return;
7993 }
7994
7995 try {
7996 const Policy p(s->cct, s->bucket_tenant, data);
7997 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
7998 if (s->bucket_access_conf &&
7999 s->bucket_access_conf->block_public_policy() &&
8000 rgw::IAM::is_public(p)) {
8001 op_ret = -EACCES;
8002 return;
8003 }
8004
8005 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [&p, this, &attrs] {
8006 attrs[RGW_ATTR_IAM_POLICY].clear();
8007 attrs[RGW_ATTR_IAM_POLICY].append(p.text);
8008 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
8009 return op_ret;
8010 });
8011 } catch (rgw::IAM::PolicyParseException& e) {
8012 ldpp_dout(this, 20) << "failed to parse policy: " << e.what() << dendl;
8013 op_ret = -EINVAL;
8014 }
8015 }
8016
8017 void RGWGetBucketPolicy::send_response()
8018 {
8019 if (op_ret) {
8020 set_req_state_err(s, op_ret);
8021 }
8022 dump_errno(s);
8023 end_header(s, this, "application/json");
8024 dump_body(s, policy);
8025 }
8026
8027 int RGWGetBucketPolicy::verify_permission(optional_yield y)
8028 {
8029 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketPolicy)) {
8030 return -EACCES;
8031 }
8032
8033 return 0;
8034 }
8035
8036 void RGWGetBucketPolicy::execute(optional_yield y)
8037 {
8038 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
8039 auto aiter = attrs.find(RGW_ATTR_IAM_POLICY);
8040 if (aiter == attrs.end()) {
8041 ldpp_dout(this, 0) << "can't find bucket IAM POLICY attr bucket_name = "
8042 << s->bucket_name << dendl;
8043 op_ret = -ERR_NO_SUCH_BUCKET_POLICY;
8044 s->err.message = "The bucket policy does not exist";
8045 return;
8046 } else {
8047 policy = attrs[RGW_ATTR_IAM_POLICY];
8048
8049 if (policy.length() == 0) {
8050 ldpp_dout(this, 10) << "The bucket policy does not exist, bucket: "
8051 << s->bucket_name << dendl;
8052 op_ret = -ERR_NO_SUCH_BUCKET_POLICY;
8053 s->err.message = "The bucket policy does not exist";
8054 return;
8055 }
8056 }
8057 }
8058
8059 void RGWDeleteBucketPolicy::send_response()
8060 {
8061 if (op_ret) {
8062 set_req_state_err(s, op_ret);
8063 }
8064 dump_errno(s);
8065 end_header(s);
8066 }
8067
8068 int RGWDeleteBucketPolicy::verify_permission(optional_yield y)
8069 {
8070 if (!verify_bucket_permission(this, s, rgw::IAM::s3DeleteBucketPolicy)) {
8071 return -EACCES;
8072 }
8073
8074 return 0;
8075 }
8076
8077 void RGWDeleteBucketPolicy::execute(optional_yield y)
8078 {
8079 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
8080 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
8081 attrs.erase(RGW_ATTR_IAM_POLICY);
8082 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
8083 return op_ret;
8084 });
8085 }
8086
8087 void RGWPutBucketObjectLock::pre_exec()
8088 {
8089 rgw_bucket_object_pre_exec(s);
8090 }
8091
8092 int RGWPutBucketObjectLock::verify_permission(optional_yield y)
8093 {
8094 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketObjectLockConfiguration);
8095 }
8096
8097 void RGWPutBucketObjectLock::execute(optional_yield y)
8098 {
8099 if (!s->bucket->get_info().obj_lock_enabled()) {
8100 s->err.message = "object lock configuration can't be set if bucket object lock not enabled";
8101 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
8102 op_ret = -ERR_INVALID_BUCKET_STATE;
8103 return;
8104 }
8105
8106 RGWXMLDecoder::XMLParser parser;
8107 if (!parser.init()) {
8108 ldpp_dout(this, 0) << "ERROR: failed to initialize parser" << dendl;
8109 op_ret = -EINVAL;
8110 return;
8111 }
8112 op_ret = get_params(y);
8113 if (op_ret < 0) {
8114 return;
8115 }
8116 if (!parser.parse(data.c_str(), data.length(), 1)) {
8117 op_ret = -ERR_MALFORMED_XML;
8118 return;
8119 }
8120
8121 try {
8122 RGWXMLDecoder::decode_xml("ObjectLockConfiguration", obj_lock, &parser, true);
8123 } catch (RGWXMLDecoder::err& err) {
8124 ldpp_dout(this, 5) << "unexpected xml:" << err << dendl;
8125 op_ret = -ERR_MALFORMED_XML;
8126 return;
8127 }
8128 if (obj_lock.has_rule() && !obj_lock.retention_period_valid()) {
8129 s->err.message = "retention period must be a positive integer value";
8130 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
8131 op_ret = -ERR_INVALID_RETENTION_PERIOD;
8132 return;
8133 }
8134
8135 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
8136 if (op_ret < 0) {
8137 ldpp_dout(this, 20) << __func__ << "forward_request_to_master returned ret=" << op_ret << dendl;
8138 return;
8139 }
8140
8141 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
8142 s->bucket->get_info().obj_lock = obj_lock;
8143 op_ret = s->bucket->put_instance_info(this, false, real_time());
8144 return op_ret;
8145 });
8146 return;
8147 }
8148
8149 void RGWGetBucketObjectLock::pre_exec()
8150 {
8151 rgw_bucket_object_pre_exec(s);
8152 }
8153
8154 int RGWGetBucketObjectLock::verify_permission(optional_yield y)
8155 {
8156 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketObjectLockConfiguration);
8157 }
8158
8159 void RGWGetBucketObjectLock::execute(optional_yield y)
8160 {
8161 if (!s->bucket->get_info().obj_lock_enabled()) {
8162 op_ret = -ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION;
8163 return;
8164 }
8165 }
8166
8167 int RGWPutObjRetention::verify_permission(optional_yield y)
8168 {
8169 if (!verify_object_permission(this, s, rgw::IAM::s3PutObjectRetention)) {
8170 return -EACCES;
8171 }
8172 op_ret = get_params(y);
8173 if (op_ret) {
8174 return op_ret;
8175 }
8176 if (bypass_governance_mode) {
8177 bypass_perm = verify_object_permission(this, s, rgw::IAM::s3BypassGovernanceRetention);
8178 }
8179 return 0;
8180 }
8181
8182 void RGWPutObjRetention::pre_exec()
8183 {
8184 rgw_bucket_object_pre_exec(s);
8185 }
8186
8187 void RGWPutObjRetention::execute(optional_yield y)
8188 {
8189 if (!s->bucket->get_info().obj_lock_enabled()) {
8190 s->err.message = "object retention can't be set if bucket object lock not configured";
8191 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
8192 op_ret = -ERR_INVALID_REQUEST;
8193 return;
8194 }
8195
8196 RGWXMLDecoder::XMLParser parser;
8197 if (!parser.init()) {
8198 ldpp_dout(this, 0) << "ERROR: failed to initialize parser" << dendl;
8199 op_ret = -EINVAL;
8200 return;
8201 }
8202
8203 if (!parser.parse(data.c_str(), data.length(), 1)) {
8204 op_ret = -ERR_MALFORMED_XML;
8205 return;
8206 }
8207
8208 try {
8209 RGWXMLDecoder::decode_xml("Retention", obj_retention, &parser, true);
8210 } catch (RGWXMLDecoder::err& err) {
8211 ldpp_dout(this, 5) << "unexpected xml:" << err << dendl;
8212 op_ret = -ERR_MALFORMED_XML;
8213 return;
8214 }
8215
8216 if (ceph::real_clock::to_time_t(obj_retention.get_retain_until_date()) < ceph_clock_now()) {
8217 s->err.message = "the retain-until date must be in the future";
8218 ldpp_dout(this, 0) << "ERROR: " << s->err.message << dendl;
8219 op_ret = -EINVAL;
8220 return;
8221 }
8222 bufferlist bl;
8223 obj_retention.encode(bl);
8224
8225 //check old retention
8226 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, this);
8227 if (op_ret < 0) {
8228 ldpp_dout(this, 0) << "ERROR: get obj attr error"<< dendl;
8229 return;
8230 }
8231 rgw::sal::RGWAttrs attrs = s->object->get_attrs();
8232 auto aiter = attrs.find(RGW_ATTR_OBJECT_RETENTION);
8233 if (aiter != attrs.end()) {
8234 RGWObjectRetention old_obj_retention;
8235 try {
8236 decode(old_obj_retention, aiter->second);
8237 } catch (buffer::error& err) {
8238 ldpp_dout(this, 0) << "ERROR: failed to decode RGWObjectRetention" << dendl;
8239 op_ret = -EIO;
8240 return;
8241 }
8242 if (ceph::real_clock::to_time_t(obj_retention.get_retain_until_date()) < ceph::real_clock::to_time_t(old_obj_retention.get_retain_until_date())) {
8243 if (old_obj_retention.get_mode().compare("GOVERNANCE") != 0 || !bypass_perm || !bypass_governance_mode) {
8244 s->err.message = "proposed retain-until date shortens an existing retention period and governance bypass check failed";
8245 op_ret = -EACCES;
8246 return;
8247 }
8248 }
8249 }
8250
8251 op_ret = s->object->modify_obj_attrs(s->obj_ctx, RGW_ATTR_OBJECT_RETENTION, bl, s->yield, this);
8252
8253 return;
8254 }
8255
8256 int RGWGetObjRetention::verify_permission(optional_yield y)
8257 {
8258 if (!verify_object_permission(this, s, rgw::IAM::s3GetObjectRetention)) {
8259 return -EACCES;
8260 }
8261 return 0;
8262 }
8263
8264 void RGWGetObjRetention::pre_exec()
8265 {
8266 rgw_bucket_object_pre_exec(s);
8267 }
8268
8269 void RGWGetObjRetention::execute(optional_yield y)
8270 {
8271 if (!s->bucket->get_info().obj_lock_enabled()) {
8272 s->err.message = "bucket object lock not configured";
8273 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
8274 op_ret = -ERR_INVALID_REQUEST;
8275 return;
8276 }
8277 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, this);
8278 if (op_ret < 0) {
8279 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << s->object
8280 << " ret=" << op_ret << dendl;
8281 return;
8282 }
8283 rgw::sal::RGWAttrs attrs = s->object->get_attrs();
8284 auto aiter = attrs.find(RGW_ATTR_OBJECT_RETENTION);
8285 if (aiter == attrs.end()) {
8286 op_ret = -ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION;
8287 return;
8288 }
8289
8290 bufferlist::const_iterator iter{&aiter->second};
8291 try {
8292 obj_retention.decode(iter);
8293 } catch (const buffer::error& e) {
8294 ldpp_dout(this, 0) << __func__ << "decode object retention config failed" << dendl;
8295 op_ret = -EIO;
8296 return;
8297 }
8298 return;
8299 }
8300
8301 int RGWPutObjLegalHold::verify_permission(optional_yield y)
8302 {
8303 if (!verify_object_permission(this, s, rgw::IAM::s3PutObjectLegalHold)) {
8304 return -EACCES;
8305 }
8306 return 0;
8307 }
8308
8309 void RGWPutObjLegalHold::pre_exec()
8310 {
8311 rgw_bucket_object_pre_exec(s);
8312 }
8313
8314 void RGWPutObjLegalHold::execute(optional_yield y) {
8315 if (!s->bucket->get_info().obj_lock_enabled()) {
8316 s->err.message = "object legal hold can't be set if bucket object lock not enabled";
8317 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
8318 op_ret = -ERR_INVALID_REQUEST;
8319 return;
8320 }
8321
8322 RGWXMLDecoder::XMLParser parser;
8323 if (!parser.init()) {
8324 ldpp_dout(this, 0) << "ERROR: failed to initialize parser" << dendl;
8325 op_ret = -EINVAL;
8326 return;
8327 }
8328
8329 op_ret = get_params(y);
8330 if (op_ret < 0)
8331 return;
8332
8333 if (!parser.parse(data.c_str(), data.length(), 1)) {
8334 op_ret = -ERR_MALFORMED_XML;
8335 return;
8336 }
8337
8338 try {
8339 RGWXMLDecoder::decode_xml("LegalHold", obj_legal_hold, &parser, true);
8340 } catch (RGWXMLDecoder::err &err) {
8341 ldpp_dout(this, 5) << "unexpected xml:" << err << dendl;
8342 op_ret = -ERR_MALFORMED_XML;
8343 return;
8344 }
8345 bufferlist bl;
8346 obj_legal_hold.encode(bl);
8347 //if instance is empty, we should modify the latest object
8348 op_ret = s->object->modify_obj_attrs(s->obj_ctx, RGW_ATTR_OBJECT_LEGAL_HOLD, bl, s->yield, this);
8349 return;
8350 }
8351
8352 int RGWGetObjLegalHold::verify_permission(optional_yield y)
8353 {
8354 if (!verify_object_permission(this, s, rgw::IAM::s3GetObjectLegalHold)) {
8355 return -EACCES;
8356 }
8357 return 0;
8358 }
8359
8360 void RGWGetObjLegalHold::pre_exec()
8361 {
8362 rgw_bucket_object_pre_exec(s);
8363 }
8364
8365 void RGWGetObjLegalHold::execute(optional_yield y)
8366 {
8367 if (!s->bucket->get_info().obj_lock_enabled()) {
8368 s->err.message = "bucket object lock not configured";
8369 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
8370 op_ret = -ERR_INVALID_REQUEST;
8371 return;
8372 }
8373 map<string, bufferlist> attrs;
8374 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, this);
8375 if (op_ret < 0) {
8376 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << s->object
8377 << " ret=" << op_ret << dendl;
8378 return;
8379 }
8380 auto aiter = s->object->get_attrs().find(RGW_ATTR_OBJECT_LEGAL_HOLD);
8381 if (aiter == s->object->get_attrs().end()) {
8382 op_ret = -ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION;
8383 return;
8384 }
8385
8386 bufferlist::const_iterator iter{&aiter->second};
8387 try {
8388 obj_legal_hold.decode(iter);
8389 } catch (const buffer::error& e) {
8390 ldpp_dout(this, 0) << __func__ << "decode object legal hold config failed" << dendl;
8391 op_ret = -EIO;
8392 return;
8393 }
8394 return;
8395 }
8396
8397 void RGWGetClusterStat::execute(optional_yield y)
8398 {
8399 op_ret = store->cluster_stat(stats_op);
8400 }
8401
8402 int RGWGetBucketPolicyStatus::verify_permission(optional_yield y)
8403 {
8404 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketPolicyStatus)) {
8405 return -EACCES;
8406 }
8407
8408 return 0;
8409 }
8410
8411 void RGWGetBucketPolicyStatus::execute(optional_yield y)
8412 {
8413 isPublic = (s->iam_policy && rgw::IAM::is_public(*s->iam_policy)) || s->bucket_acl->is_public(this);
8414 }
8415
8416 int RGWPutBucketPublicAccessBlock::verify_permission(optional_yield y)
8417 {
8418 if (!verify_bucket_permission(this, s, rgw::IAM::s3PutBucketPublicAccessBlock)) {
8419 return -EACCES;
8420 }
8421
8422 return 0;
8423 }
8424
8425 int RGWPutBucketPublicAccessBlock::get_params(optional_yield y)
8426 {
8427 const auto max_size = s->cct->_conf->rgw_max_put_param_size;
8428 std::tie(op_ret, data) = rgw_rest_read_all_input(s, max_size, false);
8429 return op_ret;
8430 }
8431
8432 void RGWPutBucketPublicAccessBlock::execute(optional_yield y)
8433 {
8434 RGWXMLDecoder::XMLParser parser;
8435 if (!parser.init()) {
8436 ldpp_dout(this, 0) << "ERROR: failed to initialize parser" << dendl;
8437 op_ret = -EINVAL;
8438 return;
8439 }
8440
8441 op_ret = get_params(y);
8442 if (op_ret < 0)
8443 return;
8444
8445 if (!parser.parse(data.c_str(), data.length(), 1)) {
8446 ldpp_dout(this, 0) << "ERROR: malformed XML" << dendl;
8447 op_ret = -ERR_MALFORMED_XML;
8448 return;
8449 }
8450
8451 try {
8452 RGWXMLDecoder::decode_xml("PublicAccessBlockConfiguration", access_conf, &parser, true);
8453 } catch (RGWXMLDecoder::err &err) {
8454 ldpp_dout(this, 5) << "unexpected xml:" << err << dendl;
8455 op_ret = -ERR_MALFORMED_XML;
8456 return;
8457 }
8458
8459 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
8460 if (op_ret < 0) {
8461 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
8462 return;
8463 }
8464
8465 bufferlist bl;
8466 access_conf.encode(bl);
8467 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this, &bl] {
8468 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
8469 attrs[RGW_ATTR_PUBLIC_ACCESS] = bl;
8470 return s->bucket->set_instance_attrs(this, attrs, s->yield);
8471 });
8472
8473 }
8474
8475 int RGWGetBucketPublicAccessBlock::verify_permission(optional_yield y)
8476 {
8477 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketPolicy)) {
8478 return -EACCES;
8479 }
8480
8481 return 0;
8482 }
8483
8484 void RGWGetBucketPublicAccessBlock::execute(optional_yield y)
8485 {
8486 auto attrs = s->bucket_attrs;
8487 if (auto aiter = attrs.find(RGW_ATTR_PUBLIC_ACCESS);
8488 aiter == attrs.end()) {
8489 ldpp_dout(this, 0) << "can't find bucket IAM POLICY attr bucket_name = "
8490 << s->bucket_name << dendl;
8491 // return the default;
8492 return;
8493 } else {
8494 bufferlist::const_iterator iter{&aiter->second};
8495 try {
8496 access_conf.decode(iter);
8497 } catch (const buffer::error& e) {
8498 ldpp_dout(this, 0) << __func__ << "decode access_conf failed" << dendl;
8499 op_ret = -EIO;
8500 return;
8501 }
8502 }
8503 }
8504
8505
8506 void RGWDeleteBucketPublicAccessBlock::send_response()
8507 {
8508 if (op_ret) {
8509 set_req_state_err(s, op_ret);
8510 }
8511 dump_errno(s);
8512 end_header(s);
8513 }
8514
8515 int RGWDeleteBucketPublicAccessBlock::verify_permission(optional_yield y)
8516 {
8517 if (!verify_bucket_permission(this, s, rgw::IAM::s3PutBucketPublicAccessBlock)) {
8518 return -EACCES;
8519 }
8520
8521 return 0;
8522 }
8523
8524 void RGWDeleteBucketPublicAccessBlock::execute(optional_yield y)
8525 {
8526 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
8527 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
8528 attrs.erase(RGW_ATTR_PUBLIC_ACCESS);
8529 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
8530 return op_ret;
8531 });
8532 }