]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_op.cc
import ceph 16.2.7
[ceph.git] / ceph / src / rgw / rgw_op.cc
CommitLineData
7c673cae 1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
9f95a23c 2// vim: ts=8 sw=2 smarttab ft=cpp
7c673cae
FG
3
4#include <errno.h>
5#include <stdlib.h>
31f18b77 6#include <system_error>
7c673cae
FG
7#include <unistd.h>
8
9#include <sstream>
f67539c2 10#include <string_view>
7c673cae
FG
11
12#include <boost/algorithm/string/predicate.hpp>
13#include <boost/optional.hpp>
31f18b77 14#include <boost/utility/in_place_factory.hpp>
7c673cae 15
11fdf7f2 16#include "include/scope_guard.h"
7c673cae
FG
17#include "common/Clock.h"
18#include "common/armor.h"
31f18b77 19#include "common/errno.h"
7c673cae
FG
20#include "common/mime.h"
21#include "common/utf8.h"
22#include "common/ceph_json.h"
11fdf7f2 23#include "common/static_ptr.h"
7c673cae
FG
24
25#include "rgw_rados.h"
11fdf7f2 26#include "rgw_zone.h"
7c673cae
FG
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"
11fdf7f2 32#include "rgw_aio_throttle.h"
7c673cae
FG
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"
224ce89b 46#include "rgw_tag_s3.h"
11fdf7f2
TL
47#include "rgw_putobj_processor.h"
48#include "rgw_crypt.h"
49#include "rgw_perf_counters.h"
eafe8130
TL
50#include "rgw_notify.h"
51#include "rgw_notify_event_type.h"
f67539c2 52#include "rgw_sal_rados.h"
11fdf7f2
TL
53
54#include "services/svc_zone.h"
55#include "services/svc_quota.h"
56#include "services/svc_sys_obj.h"
57
7c673cae
FG
58#include "cls/lock/cls_lock_client.h"
59#include "cls/rgw/cls_rgw_client.h"
60
61
11fdf7f2 62#include "include/ceph_assert.h"
7c673cae
FG
63
64#include "compressor/Compressor.h"
65
11fdf7f2
TL
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
7c673cae
FG
75
76#define dout_context g_ceph_context
77#define dout_subsys ceph_subsys_rgw
78
7c673cae
FG
79using namespace librados;
80using ceph::crypto::MD5;
31f18b77
FG
81using boost::optional;
82using boost::none;
7c673cae 83
eafe8130 84using rgw::ARN;
31f18b77
FG
85using rgw::IAM::Effect;
86using rgw::IAM::Policy;
87
7c673cae
FG
88static string mp_ns = RGW_OBJ_NS_MULTIPART;
89static string shadow_ns = RGW_OBJ_NS_SHADOW;
90
91static void forward_req_info(CephContext *cct, req_info& info, const std::string& bucket_name);
7c673cae
FG
92
93static MultipartMetaFilter mp_filter;
94
11fdf7f2
TL
95// this probably should belong in the rgw_iam_policy_keywords, I'll get it to it
96// at some point
97static constexpr auto S3_EXISTING_OBJTAG = "s3:ExistingObjectTag";
98
28e407b8 99int RGWGetObj::parse_range(void)
7c673cae
FG
100{
101 int r = -ERANGE;
28e407b8 102 string rs(range_str);
7c673cae
FG
103 string ofs_str;
104 string end_str;
105
28e407b8
AA
106 ignore_invalid_range = s->cct->_conf->rgw_ignore_get_invalid_range;
107 partial_content = false;
7c673cae 108
28e407b8 109 size_t pos = rs.find("bytes=");
7c673cae
FG
110 if (pos == string::npos) {
111 pos = 0;
28e407b8 112 while (isspace(rs[pos]))
7c673cae
FG
113 pos++;
114 int end = pos;
28e407b8 115 while (isalpha(rs[end]))
7c673cae 116 end++;
28e407b8 117 if (strncasecmp(rs.c_str(), "bytes", end - pos) != 0)
7c673cae 118 return 0;
28e407b8 119 while (isspace(rs[end]))
7c673cae 120 end++;
28e407b8 121 if (rs[end] != '=')
7c673cae 122 return 0;
28e407b8 123 rs = rs.substr(end + 1);
7c673cae 124 } else {
28e407b8 125 rs = rs.substr(pos + 6); /* size of("bytes=") */
7c673cae 126 }
28e407b8 127 pos = rs.find('-');
7c673cae
FG
128 if (pos == string::npos)
129 goto done;
130
28e407b8 131 partial_content = true;
7c673cae 132
28e407b8
AA
133 ofs_str = rs.substr(0, pos);
134 end_str = rs.substr(pos + 1);
7c673cae
FG
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
28e407b8
AA
151 range_parsed = true;
152 return 0;
153
7c673cae 154done:
28e407b8
AA
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
7c673cae
FG
163 return r;
164}
165
b3b6e05e
TL
166static int decode_policy(const DoutPrefixProvider *dpp,
167 CephContext *cct,
7c673cae
FG
168 bufferlist& bl,
169 RGWAccessControlPolicy *policy)
170{
11fdf7f2 171 auto iter = bl.cbegin();
7c673cae
FG
172 try {
173 policy->decode(iter);
174 } catch (buffer::error& err) {
b3b6e05e 175 ldpp_dout(dpp, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl;
7c673cae
FG
176 return -EIO;
177 }
11fdf7f2 178 if (cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
b3b6e05e 179 ldpp_dout(dpp, 15) << __func__ << " Read AccessControlPolicy";
11fdf7f2 180 RGWAccessControlPolicy_S3 *s3policy = static_cast<RGWAccessControlPolicy_S3 *>(policy);
7c673cae
FG
181 s3policy->to_xml(*_dout);
182 *_dout << dendl;
183 }
184 return 0;
185}
186
187
b3b6e05e
TL
188static int get_user_policy_from_attr(const DoutPrefixProvider *dpp,
189 CephContext * const cct,
9f95a23c 190 rgw::sal::RGWRadosStore * const store,
31f18b77
FG
191 map<string, bufferlist>& attrs,
192 RGWAccessControlPolicy& policy /* out */)
7c673cae
FG
193{
194 auto aiter = attrs.find(RGW_ATTR_ACL);
195 if (aiter != attrs.end()) {
b3b6e05e 196 int ret = decode_policy(dpp, cct, aiter->second, &policy);
7c673cae
FG
197 if (ret < 0) {
198 return ret;
199 }
200 } else {
201 return -ENOENT;
202 }
203
204 return 0;
205}
206
9f95a23c
TL
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 */
b3b6e05e
TL
214int rgw_op_get_bucket_policy_from_attr(const DoutPrefixProvider *dpp,
215 CephContext *cct,
f67539c2 216 rgw::sal::RGWStore *store,
9f95a23c
TL
217 RGWBucketInfo& bucket_info,
218 map<string, bufferlist>& bucket_attrs,
f67539c2
TL
219 RGWAccessControlPolicy *policy,
220 optional_yield y)
7c673cae
FG
221{
222 map<string, bufferlist>::iterator aiter = bucket_attrs.find(RGW_ATTR_ACL);
223
224 if (aiter != bucket_attrs.end()) {
b3b6e05e 225 int ret = decode_policy(dpp, cct, aiter->second, policy);
7c673cae
FG
226 if (ret < 0)
227 return ret;
228 } else {
b3b6e05e 229 ldpp_dout(dpp, 0) << "WARNING: couldn't find acl header for bucket, generating default" << dendl;
f67539c2 230 std::unique_ptr<rgw::sal::RGWUser> user = store->get_user(bucket_info.owner);
7c673cae 231 /* object exists, but policy is broken */
b3b6e05e 232 int r = user->load_by_id(dpp, y);
7c673cae
FG
233 if (r < 0)
234 return r;
235
f67539c2 236 policy->create_default(bucket_info.owner, user->get_display_name());
7c673cae
FG
237 }
238 return 0;
239}
240
b3b6e05e
TL
241static int get_obj_policy_from_attr(const DoutPrefixProvider *dpp,
242 CephContext *cct,
f67539c2 243 rgw::sal::RGWStore *store,
31f18b77
FG
244 RGWObjectCtx& obj_ctx,
245 RGWBucketInfo& bucket_info,
246 map<string, bufferlist>& bucket_attrs,
247 RGWAccessControlPolicy *policy,
11fdf7f2 248 string *storage_class,
f67539c2 249 rgw::sal::RGWObject* obj,
9f95a23c 250 optional_yield y)
7c673cae
FG
251{
252 bufferlist bl;
253 int ret = 0;
254
f67539c2 255 std::unique_ptr<rgw::sal::RGWObject::ReadOp> rop = obj->get_read_op(&obj_ctx);
7c673cae 256
b3b6e05e 257 ret = rop->get_attr(dpp, RGW_ATTR_ACL, bl, y);
7c673cae 258 if (ret >= 0) {
b3b6e05e 259 ret = decode_policy(dpp, cct, bl, policy);
7c673cae
FG
260 if (ret < 0)
261 return ret;
262 } else if (ret == -ENODATA) {
263 /* object exists, but policy is broken */
b3b6e05e 264 ldpp_dout(dpp, 0) << "WARNING: couldn't find acl header for object, generating default" << dendl;
f67539c2 265 std::unique_ptr<rgw::sal::RGWUser> user = store->get_user(bucket_info.owner);
b3b6e05e 266 ret = user->load_by_id(dpp, y);
7c673cae
FG
267 if (ret < 0)
268 return ret;
269
f67539c2 270 policy->create_default(bucket_info.owner, user->get_display_name());
7c673cae 271 }
11fdf7f2
TL
272
273 if (storage_class) {
274 bufferlist scbl;
b3b6e05e 275 int r = rop->get_attr(dpp, RGW_ATTR_STORAGE_CLASS, scbl, y);
11fdf7f2
TL
276 if (r >= 0) {
277 *storage_class = scbl.to_str();
278 } else {
279 storage_class->clear();
280 }
281 }
282
7c673cae
FG
283 return ret;
284}
285
286
11fdf7f2 287static boost::optional<Policy> get_iam_policy_from_attr(CephContext* cct,
11fdf7f2
TL
288 map<string, bufferlist>& attrs,
289 const string& tenant) {
31f18b77
FG
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 }
7c673cae
FG
296}
297
9f95a23c
TL
298static boost::optional<PublicAccessBlockConfiguration>
299get_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
11fdf7f2 315vector<Policy> get_iam_user_policy_from_attr(CephContext* cct,
9f95a23c 316 rgw::sal::RGWRadosStore* store,
11fdf7f2
TL
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
b3b6e05e
TL
333static int get_obj_head(const DoutPrefixProvider *dpp,
334 struct req_state *s,
f67539c2
TL
335 rgw::sal::RGWObject* obj,
336 bufferlist *pbl)
7c673cae 337{
f67539c2
TL
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);
7c673cae 340
b3b6e05e 341 int ret = read_op->prepare(s->yield, dpp);
11fdf7f2
TL
342 if (ret < 0) {
343 return ret;
344 }
345
346 if (!pbl) {
347 return 0;
348 }
349
b3b6e05e 350 ret = read_op->read(0, s->cct->_conf->rgw_max_chunk_size, *pbl, s->yield, dpp);
11fdf7f2
TL
351
352 return 0;
353}
354
355struct 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};
371WRITE_CLASS_ENCODER(multipart_upload_info)
372
b3b6e05e 373static int get_multipart_info(const DoutPrefixProvider *dpp, struct req_state *s,
f67539c2 374 rgw::sal::RGWObject* obj,
11fdf7f2
TL
375 multipart_upload_info *upload_info)
376{
377 bufferlist header;
378
379 bufferlist headbl;
380 bufferlist *pheadbl = (upload_info ? &headbl : nullptr);
381
b3b6e05e 382 int op_ret = get_obj_head(dpp, s, obj, pheadbl);
11fdf7f2
TL
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
11fdf7f2
TL
400 return 0;
401}
402
b3b6e05e 403static int get_multipart_info(const DoutPrefixProvider *dpp, struct req_state *s,
11fdf7f2 404 const string& meta_oid,
11fdf7f2
TL
405 multipart_upload_info *upload_info)
406{
407 map<string, bufferlist>::iterator iter;
408 bufferlist header;
409
f67539c2
TL
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);
9f95a23c 413
b3b6e05e 414 return get_multipart_info(dpp, s, meta_obj.get(), upload_info);
7c673cae
FG
415}
416
b3b6e05e
TL
417static int read_bucket_policy(const DoutPrefixProvider *dpp,
418 rgw::sal::RGWStore *store,
7c673cae
FG
419 struct req_state *s,
420 RGWBucketInfo& bucket_info,
421 map<string, bufferlist>& bucket_attrs,
422 RGWAccessControlPolicy *policy,
f67539c2
TL
423 rgw_bucket& bucket,
424 optional_yield y)
7c673cae
FG
425{
426 if (!s->system_request && bucket_info.flags & BUCKET_SUSPENDED) {
b3b6e05e 427 ldpp_dout(dpp, 0) << "NOTICE: bucket " << bucket_info.bucket.name
11fdf7f2 428 << " is suspended" << dendl;
7c673cae
FG
429 return -ERR_USER_SUSPENDED;
430 }
431
432 if (bucket.name.empty()) {
433 return 0;
434 }
435
b3b6e05e 436 int ret = rgw_op_get_bucket_policy_from_attr(dpp, s->cct, store, bucket_info, bucket_attrs, policy, y);
7c673cae
FG
437 if (ret == -ENOENT) {
438 ret = -ERR_NO_SUCH_BUCKET;
439 }
440
441 return ret;
442}
443
b3b6e05e
TL
444static int read_obj_policy(const DoutPrefixProvider *dpp,
445 rgw::sal::RGWStore *store,
7c673cae
FG
446 struct req_state *s,
447 RGWBucketInfo& bucket_info,
448 map<string, bufferlist>& bucket_attrs,
31f18b77 449 RGWAccessControlPolicy* acl,
11fdf7f2
TL
450 string *storage_class,
451 boost::optional<Policy>& policy,
f67539c2
TL
452 rgw::sal::RGWBucket* bucket,
453 rgw::sal::RGWObject* object,
454 optional_yield y,
455 bool copy_src=false)
7c673cae
FG
456{
457 string upload_id;
458 upload_id = s->info.args.get("uploadId");
f67539c2 459 std::unique_ptr<rgw::sal::RGWObject> mpobj;
7c673cae
FG
460 rgw_obj obj;
461
462 if (!s->system_request && bucket_info.flags & BUCKET_SUSPENDED) {
b3b6e05e 463 ldpp_dout(dpp, 0) << "NOTICE: bucket " << bucket_info.bucket.name
11fdf7f2 464 << " is suspended" << dendl;
7c673cae
FG
465 return -ERR_USER_SUSPENDED;
466 }
467
f67539c2
TL
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) {
7c673cae 471 /* multipart upload */
f67539c2 472 RGWMPObj mp(object->get_name(), upload_id);
7c673cae 473 string oid = mp.get_meta();
f67539c2
TL
474 mpobj = bucket->get_object(rgw_obj_key(oid, string(), mp_ns));
475 mpobj->set_in_extra_data(true);
476 object = mpobj.get();
7c673cae 477 }
f67539c2 478 policy = get_iam_policy_from_attr(s->cct, bucket_attrs, bucket->get_tenant());
31f18b77 479
7c673cae 480 RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx);
b3b6e05e 481 int ret = get_obj_policy_from_attr(dpp, s->cct, store, *obj_ctx,
f67539c2 482 bucket_info, bucket_attrs, acl, storage_class, object, s->yield);
7c673cae
FG
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);
b3b6e05e 487 ret = rgw_op_get_bucket_policy_from_attr(dpp, s->cct, store, bucket_info, bucket_attrs, &bucket_policy, y);
7c673cae
FG
488 if (ret < 0) {
489 return ret;
490 }
7c673cae 491 const rgw_user& bucket_owner = bucket_policy.get_owner().get_id();
9f95a23c 492 if (bucket_owner.compare(s->user->get_id()) != 0 &&
81eedcae 493 ! s->auth.identity->is_admin_of(bucket_owner)) {
522d829b
TL
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;
81eedcae 501 if (policy) {
522d829b
TL
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()));
81eedcae
TL
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;
7c673cae
FG
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 */
b3b6e05e 535int rgw_build_bucket_policies(const DoutPrefixProvider *dpp, rgw::sal::RGWRadosStore* store, struct req_state* s, optional_yield y)
7c673cae
FG
536{
537 int ret = 0;
9f95a23c 538 auto obj_ctx = store->svc()->sysobj->init_obj_ctx();
7c673cae
FG
539
540 string bi = s->info.args.get(RGW_SYS_PARAM_PREFIX "bucket-instance");
541 if (!bi.empty()) {
522d829b
TL
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);
7c673cae
FG
544 if (ret < 0) {
545 return ret;
546 }
547 }
548
549 if(s->dialect.compare("s3") == 0) {
11fdf7f2 550 s->bucket_acl = std::make_unique<RGWAccessControlPolicy_S3>(s->cct);
7c673cae
FG
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. */
9f95a23c 555 if (!s->user->get_id().empty()) {
11fdf7f2 556 s->user_acl = std::make_unique<RGWAccessControlPolicy_SWIFTAcct>(s->cct);
7c673cae 557 }
11fdf7f2 558 s->bucket_acl = std::make_unique<RGWAccessControlPolicy_SWIFT>(s->cct);
7c673cae 559 } else {
11fdf7f2 560 s->bucket_acl = std::make_unique<RGWAccessControlPolicy>(s->cct);
7c673cae
FG
561 }
562
563 /* check if copy source is within the current domain */
564 if (!s->src_bucket_name.empty()) {
f67539c2 565 std::unique_ptr<rgw::sal::RGWBucket> src_bucket;
b3b6e05e 566 ret = store->get_bucket(dpp, nullptr, s->src_tenant_name, s->src_bucket_name, &src_bucket, y);
f67539c2 567 if (ret == 0) {
b3b6e05e 568 ret = src_bucket->load_by_name(dpp, s->src_tenant_name, s->src_bucket_name,
f67539c2 569 s->bucket_instance_id, &obj_ctx, s->yield);
7c673cae
FG
570 }
571 if (ret == 0) {
f67539c2
TL
572 string& zonegroup = src_bucket->get_info().zonegroup;
573 s->local_source = store->get_zonegroup().equals(zonegroup);
7c673cae
FG
574 }
575 }
576
577 struct {
578 rgw_user uid;
579 std::string display_name;
580 } acct_acl_user = {
9f95a23c
TL
581 s->user->get_id(),
582 s->user->get_display_name(),
7c673cae
FG
583 };
584
585 if (!s->bucket_name.empty()) {
586 s->bucket_exists = true;
9f95a23c 587
b3b6e05e 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);
7c673cae
FG
589 if (ret < 0) {
590 if (ret != -ENOENT) {
f67539c2
TL
591 string bucket_log;
592 bucket_log = rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name);
b3b6e05e 593 ldpp_dout(dpp, 0) << "NOTICE: couldn't get bucket from bucket_name (name="
f67539c2
TL
594 << bucket_log << ")" << dendl;
595 return ret;
7c673cae
FG
596 }
597 s->bucket_exists = false;
9f95a23c 598 return -ERR_NO_SUCH_BUCKET;
7c673cae 599 }
b3b6e05e
TL
600 if (!rgw::sal::RGWObject::empty(s->object.get())) {
601 s->object->set_bucket(s->bucket.get());
602 }
603
f67539c2
TL
604 s->bucket_mtime = s->bucket->get_modification_time();
605 s->bucket_attrs = s->bucket->get_attrs();
b3b6e05e 606 ret = read_bucket_policy(dpp, store, s, s->bucket->get_info(),
f67539c2
TL
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
7c673cae
FG
614 s->bucket_owner = s->bucket_acl->get_owner();
615
616 RGWZoneGroup zonegroup;
f67539c2 617 int r = store->get_zonegroup(s->bucket->get_info().zonegroup, zonegroup);
7c673cae
FG
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
f67539c2 634 if (!store->get_zonegroup().equals(s->bucket->get_info().zonegroup)) {
b3b6e05e 635 ldpp_dout(dpp, 0) << "NOTICE: request for data in a different zonegroup ("
f67539c2
TL
636 << s->bucket->get_info().zonegroup << " != "
637 << store->get_zonegroup().get_id() << ")" << dendl;
7c673cae
FG
638 /* we now need to make sure that the operation actually requires copy source, that is
639 * it's a copy operation
640 */
f67539c2 641 if (store->get_zonegroup().is_master_zonegroup() && s->system_request) {
7c673cae 642 /*If this is the master, don't redirect*/
3efd9988
FG
643 } else if (s->op_type == RGW_OP_GET_BUCKET_LOCATION ) {
644 /* If op is get bucket location, don't redirect */
7c673cae
FG
645 } else if (!s->local_source ||
646 (s->op != OP_PUT && s->op != OP_COPY) ||
f67539c2 647 rgw::sal::RGWObject::empty(s->object.get())) {
7c673cae
FG
648 return -ERR_PERMANENT_REDIRECT;
649 }
650 }
11fdf7f2 651
f67539c2
TL
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());
11fdf7f2 655
f67539c2 656 if (!store->svc()->zone->get_zone_params().valid_placement(s->dest_placement)) {
b3b6e05e 657 ldpp_dout(dpp, 0) << "NOTICE: invalid dest placement: " << s->dest_placement.to_str() << dendl;
f67539c2 658 return -EINVAL;
11fdf7f2 659 }
9f95a23c 660
f67539c2 661 s->bucket_access_conf = get_public_access_conf_from_attr(s->bucket->get_attrs());
7c673cae
FG
662 }
663
664 /* handle user ACL only for those APIs which support it */
665 if (s->user_acl) {
666 map<string, bufferlist> uattrs;
b3b6e05e 667 ret = store->ctl()->user->get_attrs_by_uid(dpp, acct_acl_user.uid, &uattrs, s->yield);
7c673cae 668 if (!ret) {
b3b6e05e 669 ret = get_user_policy_from_attr(dpp, s->cct, store, uattrs, *s->user_acl);
7c673cae
FG
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;
11fdf7f2 682 } else if (ret < 0) {
b3b6e05e 683 ldpp_dout(dpp, 0) << "NOTICE: couldn't get user attrs for handling ACL "
9f95a23c 684 "(user_id=" << s->user->get_id() << ", ret=" << ret << ")" << dendl;
7c673cae
FG
685 return ret;
686 }
687 }
11fdf7f2
TL
688 // We don't need user policies in case of STS token returned by AssumeRole,
689 // hence the check for user type
9f95a23c 690 if (! s->user->get_id().empty() && s->auth.identity->get_identity_type() != TYPE_ROLE) {
11fdf7f2
TL
691 try {
692 map<string, bufferlist> uattrs;
b3b6e05e 693 if (ret = store->ctl()->user->get_attrs_by_uid(dpp, s->user->get_id(), &uattrs, s->yield); ! ret) {
9f95a23c 694 auto user_policies = get_iam_user_policy_from_attr(s->cct, store, uattrs, s->user->get_tenant());
f67539c2
TL
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()));
11fdf7f2
TL
698 } else {
699 if (ret == -ENOENT)
700 ret = 0;
701 else ret = -EACCES;
702 }
703 } catch (const std::exception& e) {
b3b6e05e 704 ldpp_dout(dpp, -1) << "Error reading IAM User Policy: " << e.what() << dendl;
11fdf7f2
TL
705 ret = -EACCES;
706 }
707 }
7c673cae 708
31f18b77 709 try {
f67539c2 710 s->iam_policy = get_iam_policy_from_attr(s->cct, s->bucket_attrs, s->bucket_tenant);
31f18b77
FG
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.
b3b6e05e 715 ldpp_dout(dpp, 0) << "Error reading IAM Policy: " << e.what() << dendl;
31f18b77
FG
716 ret = -EACCES;
717 }
7c673cae 718
9f95a23c 719 bool success = store->svc()->zone->get_redirect_zone_endpoint(&s->redirect_zone_endpoint);
11fdf7f2 720 if (success) {
b3b6e05e 721 ldpp_dout(dpp, 20) << "redirect_zone_endpoint=" << s->redirect_zone_endpoint << dendl;
11fdf7f2
TL
722 }
723
7c673cae
FG
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 */
b3b6e05e 733int rgw_build_object_policies(const DoutPrefixProvider *dpp, rgw::sal::RGWRadosStore *store, struct req_state *s,
f67539c2 734 bool prefetch_data, optional_yield y)
7c673cae
FG
735{
736 int ret = 0;
737
f67539c2 738 if (!rgw::sal::RGWObject::empty(s->object.get())) {
7c673cae
FG
739 if (!s->bucket_exists) {
740 return -ERR_NO_SUCH_BUCKET;
741 }
11fdf7f2 742 s->object_acl = std::make_unique<RGWAccessControlPolicy>(s->cct);
f67539c2
TL
743
744 s->object->set_bucket(s->bucket.get());
7c673cae 745
f67539c2 746 s->object->set_atomic(s->obj_ctx);
7c673cae 747 if (prefetch_data) {
f67539c2 748 s->object->set_prefetch_data(s->obj_ctx);
7c673cae 749 }
b3b6e05e 750 ret = read_obj_policy(dpp, store, s, s->bucket->get_info(), s->bucket_attrs,
f67539c2
TL
751 s->object_acl.get(), nullptr, s->iam_policy, s->bucket.get(),
752 s->object.get(), y);
7c673cae
FG
753 }
754
755 return ret;
756}
757
11fdf7f2
TL
758void 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
765static int rgw_iam_add_tags_from_bl(struct req_state* s, bufferlist& bl){
9f95a23c 766 RGWObjTags& tagset = s->tagset;
11fdf7f2
TL
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
b3b6e05e 781static int rgw_iam_add_existing_objtags(const DoutPrefixProvider *dpp, rgw::sal::RGWRadosStore* store, struct req_state* s, std::uint64_t action) {
f67539c2 782 s->object->set_atomic(s->obj_ctx);
b3b6e05e 783 int op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, dpp);
11fdf7f2
TL
784 if (op_ret < 0)
785 return op_ret;
f67539c2 786 rgw::sal::RGWAttrs attrs = s->object->get_attrs();
11fdf7f2
TL
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
794static 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
9f95a23c 815void rgw_build_iam_environment(rgw::sal::RGWRadosStore* store,
11fdf7f2 816 struct req_state* s)
31f18b77 817{
31f18b77
FG
818 const auto& m = s->info.env->get_map();
819 auto t = ceph::real_clock::now();
11fdf7f2
TL
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));
31f18b77
FG
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.
11fdf7f2 825 s->env.emplace("aws:PrincipalType", "User");
31f18b77
FG
826
827 auto i = m.find("HTTP_REFERER");
828 if (i != m.end()) {
11fdf7f2 829 s->env.emplace("aws:Referer", i->second);
31f18b77
FG
830 }
831
f64942e4 832 if (rgw_transport_is_secure(s->cct, *s->info.env)) {
11fdf7f2 833 s->env.emplace("aws:SecureTransport", "true");
31f18b77
FG
834 }
835
b32b8144
FG
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 }
31f18b77 842 if (i != m.end()) {
b32b8144
FG
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 }
11fdf7f2 852 s->env.emplace("aws:SourceIp", *ip);
31f18b77
FG
853 }
854
855 i = m.find("HTTP_USER_AGENT"); {
856 if (i != m.end())
11fdf7f2 857 s->env.emplace("aws:UserAgent", i->second);
31f18b77
FG
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?
9f95a23c 864 s->env.emplace("aws:username", s->user->get_id().id);
11fdf7f2
TL
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");
31f18b77 872 }
31f18b77
FG
873}
874
875void rgw_bucket_object_pre_exec(struct req_state *s)
7c673cae
FG
876{
877 if (s->expect_cont)
878 dump_continue(s);
879
880 dump_bucket_from_state(s);
881}
882
b32b8144
FG
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.
898namespace {
899template<typename F>
b3b6e05e 900int retry_raced_bucket_write(const DoutPrefixProvider *dpp, rgw::sal::RGWBucket* b, const F& f) {
b32b8144
FG
901 auto r = f();
902 for (auto i = 0u; i < 15u && r == -ECANCELED; ++i) {
b3b6e05e 903 r = b->try_refresh_info(dpp, nullptr);
b32b8144
FG
904 if (r >= 0) {
905 r = f();
906 }
907 }
908 return r;
909}
910}
911
912
f67539c2 913int RGWGetObj::verify_permission(optional_yield y)
7c673cae 914{
f67539c2
TL
915 s->object->set_atomic(s->obj_ctx);
916
917 if (prefetch_data()) {
918 s->object->set_prefetch_data(s->obj_ctx);
7c673cae
FG
919 }
920
31f18b77 921 if (torrent.get_flag()) {
f67539c2 922 if (s->object->get_instance().empty()) {
31f18b77
FG
923 action = rgw::IAM::s3GetObjectTorrent;
924 } else {
925 action = rgw::IAM::s3GetObjectVersionTorrent;
926 }
927 } else {
f67539c2 928 if (s->object->get_instance().empty()) {
31f18b77
FG
929 action = rgw::IAM::s3GetObject;
930 } else {
931 action = rgw::IAM::s3GetObjectVersion;
932 }
11fdf7f2 933 if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG))
b3b6e05e 934 rgw_iam_add_existing_objtags(this, store, s, action);
11fdf7f2
TL
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))
b3b6e05e 938 rgw_iam_add_existing_objtags(this, store, s, action);
11fdf7f2
TL
939 }
940 }
31f18b77
FG
941 }
942
11fdf7f2 943 if (!verify_object_permission(this, s, action)) {
7c673cae
FG
944 return -EACCES;
945 }
946
f67539c2 947 if (s->bucket->get_info().obj_lock_enabled()) {
eafe8130
TL
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
7c673cae
FG
952 return 0;
953}
954
7c673cae
FG
955int RGWOp::verify_op_mask()
956{
957 uint32_t required_mask = op_mask();
958
11fdf7f2 959 ldpp_dout(this, 20) << "required_mask= " << required_mask
9f95a23c 960 << " user.op_mask=" << s->user->get_info().op_mask << dendl;
7c673cae 961
9f95a23c 962 if ((s->user->get_info().op_mask & required_mask) != required_mask) {
7c673cae
FG
963 return -EPERM;
964 }
965
9f95a23c 966 if (!s->system_request && (required_mask & RGW_OP_TYPE_MODIFY) && !store->svc()->zone->zone_is_writeable()) {
11fdf7f2
TL
967 ldpp_dout(this, 5) << "NOTICE: modify request to a read-only zone by a "
968 "non-system user, permission denied" << dendl;
7c673cae
FG
969 return -EPERM;
970 }
971
972 return 0;
973}
974
f67539c2 975int RGWGetObjTags::verify_permission(optional_yield y)
224ce89b 976{
f67539c2 977 auto iam_action = s->object->get_instance().empty()?
11fdf7f2
TL
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)){
b3b6e05e 983 rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2
TL
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)) {
b3b6e05e 988 rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2
TL
989 }
990 }
991 }
992 if (!verify_object_permission(this, s,iam_action))
224ce89b
WB
993 return -EACCES;
994
995 return 0;
996}
997
998void RGWGetObjTags::pre_exec()
999{
1000 rgw_bucket_object_pre_exec(s);
1001}
1002
f67539c2 1003void RGWGetObjTags::execute(optional_yield y)
224ce89b 1004{
f67539c2 1005 rgw::sal::RGWAttrs attrs;
224ce89b 1006
f67539c2 1007 s->object->set_atomic(s->obj_ctx);
224ce89b 1008
b3b6e05e 1009 op_ret = s->object->get_obj_attrs(s->obj_ctx, y, this);
b5b8bbf5 1010 if (op_ret < 0) {
f67539c2 1011 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << s->object
11fdf7f2 1012 << " ret=" << op_ret << dendl;
b5b8bbf5
FG
1013 return;
1014 }
1015
f67539c2 1016 attrs = s->object->get_attrs();
224ce89b
WB
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
f67539c2 1025int RGWPutObjTags::verify_permission(optional_yield y)
224ce89b 1026{
f67539c2 1027 auto iam_action = s->object->get_instance().empty() ?
11fdf7f2
TL
1028 rgw::IAM::s3PutObjectTagging:
1029 rgw::IAM::s3PutObjectVersionTagging;
1030
1031 if(s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){
b3b6e05e 1032 rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2
TL
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)) {
b3b6e05e 1037 rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2
TL
1038 }
1039 }
1040 }
1041 if (!verify_object_permission(this, s,iam_action))
224ce89b
WB
1042 return -EACCES;
1043 return 0;
1044}
1045
f67539c2 1046void RGWPutObjTags::execute(optional_yield y)
224ce89b 1047{
f67539c2 1048 op_ret = get_params(y);
224ce89b
WB
1049 if (op_ret < 0)
1050 return;
1051
f67539c2 1052 if (rgw::sal::RGWObject::empty(s->object.get())){
224ce89b
WB
1053 op_ret= -EINVAL; // we only support tagging on existing objects
1054 return;
1055 }
1056
f67539c2 1057 s->object->set_atomic(s->obj_ctx);
b3b6e05e 1058 op_ret = s->object->modify_obj_attrs(s->obj_ctx, RGW_ATTR_TAGS, tags_bl, y, this);
224ce89b
WB
1059 if (op_ret == -ECANCELED){
1060 op_ret = -ERR_TAG_CONFLICT;
1061 }
1062}
1063
1064void RGWDeleteObjTags::pre_exec()
1065{
1066 rgw_bucket_object_pre_exec(s);
1067}
1068
1069
f67539c2 1070int RGWDeleteObjTags::verify_permission(optional_yield y)
224ce89b 1071{
f67539c2
TL
1072 if (!rgw::sal::RGWObject::empty(s->object.get())) {
1073 auto iam_action = s->object->get_instance().empty() ?
11fdf7f2
TL
1074 rgw::IAM::s3DeleteObjectTagging:
1075 rgw::IAM::s3DeleteObjectVersionTagging;
1076
1077 if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){
b3b6e05e 1078 rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2
TL
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)) {
b3b6e05e 1083 rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2
TL
1084 }
1085 }
1086 }
1087 if (!verify_object_permission(this, s, iam_action))
224ce89b
WB
1088 return -EACCES;
1089 }
1090 return 0;
1091}
1092
f67539c2 1093void RGWDeleteObjTags::execute(optional_yield y)
224ce89b 1094{
f67539c2 1095 if (rgw::sal::RGWObject::empty(s->object.get()))
224ce89b
WB
1096 return;
1097
b3b6e05e 1098 op_ret = s->object->delete_obj_attrs(this, s->obj_ctx, RGW_ATTR_TAGS, y);
9f95a23c
TL
1099}
1100
f67539c2 1101int RGWGetBucketTags::verify_permission(optional_yield y)
9f95a23c
TL
1102{
1103
1104 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketTagging)) {
1105 return -EACCES;
1106 }
1107
1108 return 0;
1109}
1110
1111void RGWGetBucketTags::pre_exec()
1112{
1113 rgw_bucket_object_pre_exec(s);
1114}
1115
f67539c2 1116void RGWGetBucketTags::execute(optional_yield y)
9f95a23c
TL
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
f67539c2 1128int RGWPutBucketTags::verify_permission(optional_yield y) {
9f95a23c
TL
1129 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketTagging);
1130}
1131
f67539c2
TL
1132void RGWPutBucketTags::execute(optional_yield y)
1133{
9f95a23c 1134
b3b6e05e 1135 op_ret = get_params(this, y);
9f95a23c
TL
1136 if (op_ret < 0)
1137 return;
1138
b3b6e05e 1139 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
1140 if (op_ret < 0) {
1141 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
9f95a23c
TL
1142 }
1143
b3b6e05e 1144 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this, y] {
f67539c2 1145 rgw::sal::RGWAttrs attrs = s->bucket->get_attrs();
9f95a23c 1146 attrs[RGW_ATTR_TAGS] = tags_bl;
b3b6e05e 1147 return s->bucket->set_instance_attrs(this, attrs, y);
9f95a23c
TL
1148 });
1149
1150}
1151
1152void RGWDeleteBucketTags::pre_exec()
1153{
1154 rgw_bucket_object_pre_exec(s);
1155}
1156
f67539c2 1157int RGWDeleteBucketTags::verify_permission(optional_yield y)
9f95a23c
TL
1158{
1159 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketTagging);
1160}
1161
f67539c2 1162void RGWDeleteBucketTags::execute(optional_yield y)
9f95a23c 1163{
f67539c2 1164 bufferlist in_data;
b3b6e05e 1165 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
1166 if (op_ret < 0) {
1167 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
1168 return;
9f95a23c
TL
1169 }
1170
b3b6e05e 1171 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this, y] {
f67539c2 1172 rgw::sal::RGWAttrs attrs = s->bucket->get_attrs();
9f95a23c 1173 attrs.erase(RGW_ATTR_TAGS);
b3b6e05e 1174 op_ret = s->bucket->set_instance_attrs(this, attrs, y);
9f95a23c
TL
1175 if (op_ret < 0) {
1176 ldpp_dout(this, 0) << "RGWDeleteBucketTags() failed to remove RGW_ATTR_TAGS on bucket="
f67539c2 1177 << s->bucket->get_name()
9f95a23c
TL
1178 << " returned err= " << op_ret << dendl;
1179 }
1180 return op_ret;
1181 });
1182}
1183
f67539c2 1184int RGWGetBucketReplication::verify_permission(optional_yield y)
9f95a23c
TL
1185{
1186 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetReplicationConfiguration)) {
1187 return -EACCES;
1188 }
1189
1190 return 0;
1191}
1192
1193void RGWGetBucketReplication::pre_exec()
1194{
1195 rgw_bucket_object_pre_exec(s);
1196}
1197
f67539c2 1198void RGWGetBucketReplication::execute(optional_yield y)
9f95a23c
TL
1199{
1200 send_response_data();
1201}
1202
f67539c2 1203int RGWPutBucketReplication::verify_permission(optional_yield y) {
9f95a23c
TL
1204 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutReplicationConfiguration);
1205}
1206
f67539c2 1207void RGWPutBucketReplication::execute(optional_yield y) {
9f95a23c 1208
f67539c2 1209 op_ret = get_params(y);
9f95a23c
TL
1210 if (op_ret < 0)
1211 return;
1212
b3b6e05e 1213 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
1214 if (op_ret < 0) {
1215 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
1216 return;
9f95a23c
TL
1217 }
1218
b3b6e05e 1219 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
f67539c2 1220 auto sync_policy = (s->bucket->get_info().sync_policy ? *s->bucket->get_info().sync_policy : rgw_sync_policy_info());
9f95a23c
TL
1221
1222 for (auto& group : sync_policy_groups) {
1223 sync_policy.groups[group.id] = group;
1224 }
1225
f67539c2 1226 s->bucket->get_info().set_sync_policy(std::move(sync_policy));
9f95a23c 1227
b3b6e05e 1228 int ret = s->bucket->put_instance_info(this, false, real_time());
9f95a23c 1229 if (ret < 0) {
f67539c2 1230 ldpp_dout(this, 0) << "ERROR: put_bucket_instance_info (bucket=" << s->bucket << ") returned ret=" << ret << dendl;
9f95a23c
TL
1231 return ret;
1232 }
1233
1234 return 0;
1235 });
1236}
1237
1238void RGWDeleteBucketReplication::pre_exec()
1239{
1240 rgw_bucket_object_pre_exec(s);
1241}
1242
f67539c2 1243int RGWDeleteBucketReplication::verify_permission(optional_yield y)
9f95a23c
TL
1244{
1245 return verify_bucket_owner_or_policy(s, rgw::IAM::s3DeleteReplicationConfiguration);
1246}
1247
f67539c2 1248void RGWDeleteBucketReplication::execute(optional_yield y)
9f95a23c 1249{
f67539c2 1250 bufferlist in_data;
b3b6e05e 1251 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
1252 if (op_ret < 0) {
1253 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
1254 return;
9f95a23c
TL
1255 }
1256
b3b6e05e 1257 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
f67539c2 1258 if (!s->bucket->get_info().sync_policy) {
9f95a23c
TL
1259 return 0;
1260 }
1261
f67539c2 1262 rgw_sync_policy_info sync_policy = *s->bucket->get_info().sync_policy;
9f95a23c
TL
1263
1264 update_sync_policy(&sync_policy);
1265
f67539c2 1266 s->bucket->get_info().set_sync_policy(std::move(sync_policy));
9f95a23c 1267
b3b6e05e 1268 int ret = s->bucket->put_instance_info(this, false, real_time());
9f95a23c 1269 if (ret < 0) {
f67539c2 1270 ldpp_dout(this, 0) << "ERROR: put_bucket_instance_info (bucket=" << s->bucket << ") returned ret=" << ret << dendl;
9f95a23c
TL
1271 return ret;
1272 }
1273
1274 return 0;
1275 });
224ce89b
WB
1276}
1277
7c673cae
FG
1278int RGWOp::do_aws4_auth_completion()
1279{
11fdf7f2 1280 ldpp_dout(this, 5) << "NOTICE: call to do_aws4_auth_completion" << dendl;
31f18b77
FG
1281 if (s->auth.completer) {
1282 if (!s->auth.completer->complete()) {
1283 return -ERR_AMZ_CONTENT_SHA256_MISMATCH;
1284 } else {
11fdf7f2 1285 ldpp_dout(this, 10) << "v4 auth ok -- do_aws4_auth_completion" << dendl;
7c673cae 1286 }
31f18b77
FG
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;
7c673cae
FG
1292 }
1293
1294 return 0;
1295}
1296
1297int 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 */
9f95a23c 1304 if (!(s->user->get_info().op_mask & RGW_OP_TYPE_MODIFY)) {
7c673cae
FG
1305 return 0;
1306 }
1307
1308 /* only interested in object related ops */
f67539c2
TL
1309 if (rgw::sal::RGWBucket::empty(s->bucket.get())
1310 || rgw::sal::RGWObject::empty(s->object.get())) {
7c673cae
FG
1311 return 0;
1312 }
1313
f67539c2
TL
1314 rgw::sal::RGWRadosUser owner_user(store, s->bucket->get_info().owner);
1315 rgw::sal::RGWUser* user;
7c673cae 1316
9f95a23c 1317 if (s->user->get_id() == s->bucket_owner.get_id()) {
f67539c2 1318 user = s->user.get();
7c673cae 1319 } else {
b3b6e05e 1320 int r = owner_user.load_by_id(this, s->yield);
7c673cae
FG
1321 if (r < 0)
1322 return r;
9f95a23c 1323 user = &owner_user;
7c673cae
FG
1324 }
1325
f67539c2
TL
1326 if (s->bucket->get_info().quota.enabled) {
1327 bucket_quota = s->bucket->get_info().quota;
9f95a23c
TL
1328 } else if (user->get_info().bucket_quota.enabled) {
1329 bucket_quota = user->get_info().bucket_quota;
7c673cae 1330 } else {
9f95a23c 1331 bucket_quota = store->svc()->quota->get_bucket_quota();
7c673cae
FG
1332 }
1333
9f95a23c
TL
1334 if (user->get_info().user_quota.enabled) {
1335 user_quota = user->get_info().user_quota;
7c673cae 1336 } else {
9f95a23c 1337 user_quota = store->svc()->quota->get_user_quota();
7c673cae
FG
1338 }
1339
1340 return 0;
1341}
1342
1343static 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
1adf2230 1357 if (rule->get_allowed_methods() & flags) {
7c673cae
FG
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
b32b8144
FG
1367static 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
7c673cae
FG
1381int 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()) {
11fdf7f2 1387 ldpp_dout(this, 20) << "no CORS configuration attr found" << dendl;
7c673cae
FG
1388 cors_exist = false;
1389 return 0; /* no CORS configuration found */
1390 }
1391
1392 cors_exist = true;
1393
1394 bl = aiter->second;
1395
11fdf7f2 1396 auto iter = bl.cbegin();
7c673cae
FG
1397 try {
1398 bucket_cors.decode(iter);
1399 } catch (buffer::error& err) {
f67539c2 1400 ldpp_dout(this, 0) << "ERROR: could not decode CORS, caught buffer::error" << dendl;
7c673cae
FG
1401 return -EIO;
1402 }
11fdf7f2 1403 if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
7c673cae 1404 RGWCORSConfiguration_S3 *s3cors = static_cast<RGWCORSConfiguration_S3 *>(&bucket_cors);
11fdf7f2 1405 ldpp_dout(this, 15) << "Read RGWCORSConfiguration";
7c673cae
FG
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 * */
1417static 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 */
1439bool 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;
9f95a23c
TL
1449 int temp_op_ret = read_bucket_cors();
1450 if (temp_op_ret < 0) {
1451 op_ret = temp_op_ret;
7c673cae
FG
1452 return false;
1453 }
1454
1455 if (!cors_exist) {
11fdf7f2 1456 ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl;
7c673cae
FG
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
f67539c2 1500int RGWGetObj::read_user_manifest_part(rgw::sal::RGWBucket* bucket,
7c673cae 1501 const rgw_bucket_dir_entry& ent,
31f18b77 1502 RGWAccessControlPolicy * const bucket_acl,
11fdf7f2 1503 const boost::optional<Policy>& bucket_policy,
7c673cae 1504 const off_t start_ofs,
11fdf7f2
TL
1505 const off_t end_ofs,
1506 bool swift_slo)
7c673cae 1507{
11fdf7f2
TL
1508 ldpp_dout(this, 20) << "user manifest obj=" << ent.key.name
1509 << "[" << ent.key.instance << "]" << dendl;
7c673cae 1510 RGWGetObj_CB cb(this);
11fdf7f2 1511 RGWGetObj_Filter* filter = &cb;
7c673cae
FG
1512 boost::optional<RGWGetObj_Decompress> decompress;
1513
1514 int64_t cur_ofs = start_ofs;
1515 int64_t cur_end = end_ofs;
1516
f67539c2 1517 std::unique_ptr<rgw::sal::RGWObject> part = bucket->get_object(ent.key);
7c673cae 1518
7c673cae
FG
1519 RGWObjectCtx obj_ctx(store);
1520 RGWAccessControlPolicy obj_policy(s->cct);
1521
11fdf7f2
TL
1522 ldpp_dout(this, 20) << "reading obj=" << part << " ofs=" << cur_ofs
1523 << " end=" << cur_end << dendl;
7c673cae 1524
f67539c2
TL
1525 part->set_atomic(&obj_ctx);
1526 part->set_prefetch_data(&obj_ctx);
7c673cae 1527
f67539c2 1528 std::unique_ptr<rgw::sal::RGWObject::ReadOp> read_op = part->get_read_op(&obj_ctx);
7c673cae 1529
11fdf7f2
TL
1530 if (!swift_slo) {
1531 /* SLO etag is optional */
f67539c2 1532 read_op->params.if_match = ent.meta.etag.c_str();
11fdf7f2 1533 }
7c673cae 1534
b3b6e05e 1535 op_ret = read_op->prepare(s->yield, this);
7c673cae
FG
1536 if (op_ret < 0)
1537 return op_ret;
f67539c2 1538 op_ret = part->range_to_ofs(ent.meta.accounted_size, cur_ofs, cur_end);
7c673cae
FG
1539 if (op_ret < 0)
1540 return op_ret;
1541 bool need_decompress;
f67539c2 1542 op_ret = rgw_compression_info_from_attrset(part->get_attrs(), need_decompress, cs_info);
7c673cae 1543 if (op_ret < 0) {
11fdf7f2
TL
1544 ldpp_dout(this, 0) << "ERROR: failed to decode compression info" << dendl;
1545 return -EIO;
7c673cae
FG
1546 }
1547
1548 if (need_decompress)
1549 {
224ce89b 1550 if (cs_info.orig_size != ent.meta.accounted_size) {
7c673cae 1551 // hmm.. something wrong, object not as expected, abort!
11fdf7f2
TL
1552 ldpp_dout(this, 0) << "ERROR: expected cs_info.orig_size=" << cs_info.orig_size
1553 << ", actual read size=" << ent.meta.size << dendl;
7c673cae
FG
1554 return -EIO;
1555 }
1556 decompress.emplace(s->cct, &cs_info, partial_content, filter);
1557 filter = &*decompress;
1558 }
1559 else
1560 {
f67539c2 1561 if (part->get_obj_size() != ent.meta.size) {
7c673cae 1562 // hmm.. something wrong, object not as expected, abort!
f67539c2 1563 ldpp_dout(this, 0) << "ERROR: expected obj_size=" << part->get_obj_size()
11fdf7f2 1564 << ", actual read size=" << ent.meta.size << dendl;
7c673cae
FG
1565 return -EIO;
1566 }
1567 }
1568
b3b6e05e 1569 op_ret = rgw_policy_from_attrset(s, s->cct, part->get_attrs(), &obj_policy);
7c673cae
FG
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) {
11fdf7f2 1576 ldpp_dout(this, 2) << "overriding permissions due to system operation" << dendl;
9f95a23c 1577 } else if (s->auth.identity->is_admin_of(s->user->get_id())) {
11fdf7f2 1578 ldpp_dout(this, 2) << "overriding permissions due to admin operation" << dendl;
f67539c2
TL
1579 } else if (!verify_object_permission(this, s, part->get_obj(), s->user_acl.get(),
1580 bucket_acl, &obj_policy, bucket_policy,
522d829b 1581 s->iam_user_policies, s->session_policies, action)) {
7c673cae
FG
1582 return -EPERM;
1583 }
7c673cae
FG
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);
b3b6e05e 1590 op_ret = read_op->iterate(this, cur_ofs, cur_end, filter, s->yield);
7c673cae
FG
1591 if (op_ret >= 0)
1592 op_ret = filter->flush();
1593 return op_ret;
1594}
1595
b3b6e05e
TL
1596static int iterate_user_manifest_parts(const DoutPrefixProvider *dpp,
1597 CephContext * const cct,
f67539c2 1598 rgw::sal::RGWStore* const store,
7c673cae
FG
1599 const off_t ofs,
1600 const off_t end,
f67539c2 1601 rgw::sal::RGWBucket* bucket,
7c673cae 1602 const string& obj_prefix,
31f18b77 1603 RGWAccessControlPolicy * const bucket_acl,
11fdf7f2 1604 const boost::optional<Policy>& bucket_policy,
7c673cae
FG
1605 uint64_t * const ptotal_len,
1606 uint64_t * const pobj_size,
1607 string * const pobj_sum,
f67539c2 1608 int (*cb)(rgw::sal::RGWBucket* bucket,
7c673cae 1609 const rgw_bucket_dir_entry& ent,
31f18b77 1610 RGWAccessControlPolicy * const bucket_acl,
11fdf7f2 1611 const boost::optional<Policy>& bucket_policy,
7c673cae
FG
1612 off_t start_ofs,
1613 off_t end_ofs,
11fdf7f2
TL
1614 void *param,
1615 bool swift_slo),
f67539c2
TL
1616 void * const cb_param,
1617 optional_yield y)
7c673cae 1618{
7c673cae
FG
1619 uint64_t obj_ofs = 0, len_count = 0;
1620 bool found_start = false, found_end = false, handled_end = false;
1621 string delim;
7c673cae
FG
1622
1623 utime_t start_time = ceph_clock_now();
1624
f67539c2
TL
1625 rgw::sal::RGWBucket::ListParams params;
1626 params.prefix = obj_prefix;
1627 params.delim = delim;
7c673cae 1628
f67539c2 1629 rgw::sal::RGWBucket::ListResults results;
7c673cae
FG
1630 MD5 etag_sum;
1631 do {
f67539c2 1632 static constexpr auto MAX_LIST_OBJS = 100u;
b3b6e05e 1633 int r = bucket->list(dpp, params, MAX_LIST_OBJS, results, y);
7c673cae
FG
1634 if (r < 0) {
1635 return r;
1636 }
1637
f67539c2 1638 for (rgw_bucket_dir_entry& ent : results.objs) {
224ce89b
WB
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;
7c673cae 1642
224ce89b 1643 if ((ptotal_len || cb) && !found_start && cur_total_len + obj_size > (uint64_t)ofs) {
7c673cae
FG
1644 start_ofs = ofs - obj_ofs;
1645 found_start = true;
1646 }
1647
224ce89b 1648 obj_ofs += obj_size;
7c673cae 1649 if (pobj_sum) {
11fdf7f2 1650 etag_sum.Update((const unsigned char *)ent.meta.etag.c_str(),
7c673cae
FG
1651 ent.meta.etag.length());
1652 }
1653
31f18b77 1654 if ((ptotal_len || cb) && !found_end && obj_ofs > (uint64_t)end) {
7c673cae
FG
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) {
11fdf7f2
TL
1666 r = cb(bucket, ent, bucket_acl, bucket_policy, start_ofs, end_ofs,
1667 cb_param, false /* swift_slo */);
7c673cae
FG
1668 if (r < 0) {
1669 return r;
1670 }
1671 }
1672 }
1673
1674 handled_end = found_end;
1675 start_time = ceph_clock_now();
1676 }
f67539c2 1677 } while (results.is_truncated);
7c673cae
FG
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
1692struct rgw_slo_part {
31f18b77
FG
1693 RGWAccessControlPolicy *bucket_acl = nullptr;
1694 Policy* bucket_policy = nullptr;
f67539c2 1695 rgw::sal::RGWBucket* bucket;
7c673cae 1696 string obj_name;
31f18b77 1697 uint64_t size = 0;
7c673cae 1698 string etag;
7c673cae
FG
1699};
1700
1701static int iterate_slo_parts(CephContext *cct,
f67539c2 1702 rgw::sal::RGWStore*store,
7c673cae
FG
1703 off_t ofs,
1704 off_t end,
1705 map<uint64_t, rgw_slo_part>& slo_parts,
f67539c2 1706 int (*cb)(rgw::sal::RGWBucket* bucket,
7c673cae 1707 const rgw_bucket_dir_entry& ent,
31f18b77 1708 RGWAccessControlPolicy *bucket_acl,
11fdf7f2 1709 const boost::optional<Policy>& bucket_policy,
7c673cae
FG
1710 off_t start_ofs,
1711 off_t end_ofs,
11fdf7f2
TL
1712 void *param,
1713 bool swift_slo),
7c673cae
FG
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;
224ce89b 1736 ent.meta.accounted_size = ent.meta.size = part.size;
7c673cae
FG
1737 ent.meta.etag = part.etag;
1738
1739 uint64_t cur_total_len = obj_ofs;
eafe8130 1740 uint64_t start_ofs = 0, end_ofs = ent.meta.size - 1;
7c673cae
FG
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) {
eafe8130 1750 end_ofs = end - cur_total_len;
7c673cae
FG
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) {
eafe8130
TL
1759 dout(20) << "iterate_slo_parts()"
1760 << " obj=" << part.obj_name
1761 << " start_ofs=" << start_ofs
1762 << " end_ofs=" << end_ofs
1763 << dendl;
1764
31f18b77
FG
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 ?
11fdf7f2
TL
1768 boost::optional<Policy>(*part.bucket_policy) : none),
1769 start_ofs, end_ofs, cb_param, true /* swift_slo */);
31f18b77 1770 if (r < 0)
7c673cae
FG
1771 return r;
1772 }
1773 }
1774
1775 start_time = ceph_clock_now();
1776 }
1777
1778 return 0;
1779}
1780
f67539c2 1781static int get_obj_user_manifest_iterate_cb(rgw::sal::RGWBucket* bucket,
7c673cae 1782 const rgw_bucket_dir_entry& ent,
31f18b77 1783 RGWAccessControlPolicy * const bucket_acl,
11fdf7f2 1784 const boost::optional<Policy>& bucket_policy,
7c673cae
FG
1785 const off_t start_ofs,
1786 const off_t end_ofs,
11fdf7f2
TL
1787 void * const param,
1788 bool swift_slo = false)
7c673cae
FG
1789{
1790 RGWGetObj *op = static_cast<RGWGetObj *>(param);
11fdf7f2
TL
1791 return op->read_user_manifest_part(
1792 bucket, ent, bucket_acl, bucket_policy, start_ofs, end_ofs, swift_slo);
7c673cae
FG
1793}
1794
f67539c2 1795int RGWGetObj::handle_user_manifest(const char *prefix, optional_yield y)
7c673cae 1796{
f67539c2 1797 const std::string_view prefix_view(prefix);
11fdf7f2 1798 ldpp_dout(this, 2) << "RGWGetObj::handle_user_manifest() prefix="
31f18b77 1799 << prefix_view << dendl;
7c673cae 1800
31f18b77
FG
1801 const size_t pos = prefix_view.find('/');
1802 if (pos == string::npos) {
7c673cae 1803 return -EINVAL;
31f18b77 1804 }
7c673cae 1805
31f18b77
FG
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));
7c673cae 1808
31f18b77
FG
1809 RGWAccessControlPolicy _bucket_acl(s->cct);
1810 RGWAccessControlPolicy *bucket_acl;
11fdf7f2
TL
1811 boost::optional<Policy> _bucket_policy;
1812 boost::optional<Policy>* bucket_policy;
7c673cae 1813 RGWBucketInfo bucket_info;
f67539c2
TL
1814 std::unique_ptr<rgw::sal::RGWBucket> ubucket;
1815 rgw::sal::RGWBucket *pbucket = NULL;
1816 int r = 0;
7c673cae 1817
f67539c2 1818 if (bucket_name.compare(s->bucket->get_name()) != 0) {
7c673cae 1819 map<string, bufferlist> bucket_attrs;
b3b6e05e 1820 r = store->get_bucket(this, s->user.get(), s->user->get_tenant(), bucket_name, &ubucket, y);
7c673cae 1821 if (r < 0) {
11fdf7f2 1822 ldpp_dout(this, 0) << "could not get bucket info for bucket="
7c673cae
FG
1823 << bucket_name << dendl;
1824 return r;
1825 }
31f18b77 1826 bucket_acl = &_bucket_acl;
b3b6e05e 1827 r = read_bucket_policy(this, store, s, ubucket->get_info(), bucket_attrs, bucket_acl, ubucket->get_key(), y);
7c673cae 1828 if (r < 0) {
11fdf7f2 1829 ldpp_dout(this, 0) << "failed to read bucket policy" << dendl;
7c673cae
FG
1830 return r;
1831 }
f67539c2 1832 _bucket_policy = get_iam_policy_from_attr(s->cct, bucket_attrs, s->user->get_tenant());
31f18b77 1833 bucket_policy = &_bucket_policy;
f67539c2 1834 pbucket = ubucket.get();
7c673cae 1835 } else {
f67539c2 1836 pbucket = s->bucket.get();
c07f9fc5 1837 bucket_acl = s->bucket_acl.get();
31f18b77 1838 bucket_policy = &s->iam_policy;
7c673cae
FG
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). */
b3b6e05e 1845 r = iterate_user_manifest_parts(this, s->cct, store, ofs, end,
f67539c2 1846 pbucket, obj_prefix, bucket_acl, *bucket_policy,
31f18b77 1847 nullptr, &s->obj_size, &lo_etag,
f67539c2 1848 nullptr /* cb */, nullptr /* cb arg */, y);
7c673cae
FG
1849 if (r < 0) {
1850 return r;
1851 }
f67539c2 1852 s->object->set_obj_size(s->obj_size);
7c673cae 1853
31f18b77
FG
1854 r = RGWRados::Object::Read::range_to_ofs(s->obj_size, ofs, end);
1855 if (r < 0) {
1856 return r;
1857 }
1858
b3b6e05e 1859 r = iterate_user_manifest_parts(this, s->cct, store, ofs, end,
f67539c2 1860 pbucket, obj_prefix, bucket_acl, *bucket_policy,
31f18b77 1861 &total_len, nullptr, nullptr,
f67539c2 1862 nullptr, nullptr, y);
31f18b77
FG
1863 if (r < 0) {
1864 return r;
1865 }
1866
7c673cae
FG
1867 if (!get_data) {
1868 bufferlist bl;
1869 send_response_data(bl, 0, 0);
1870 return 0;
1871 }
1872
b3b6e05e 1873 r = iterate_user_manifest_parts(this, s->cct, store, ofs, end,
f67539c2 1874 pbucket, obj_prefix, bucket_acl, *bucket_policy,
7c673cae 1875 nullptr, nullptr, nullptr,
f67539c2 1876 get_obj_user_manifest_iterate_cb, (void *)this, y);
7c673cae
FG
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
f67539c2 1886 return r;
7c673cae
FG
1887}
1888
f67539c2 1889int RGWGetObj::handle_slo_manifest(bufferlist& bl, optional_yield y)
7c673cae
FG
1890{
1891 RGWSLOInfo slo_info;
11fdf7f2 1892 auto bliter = bl.cbegin();
7c673cae 1893 try {
11fdf7f2 1894 decode(slo_info, bliter);
7c673cae 1895 } catch (buffer::error& err) {
11fdf7f2 1896 ldpp_dout(this, 0) << "ERROR: failed to decode slo manifest" << dendl;
7c673cae
FG
1897 return -EIO;
1898 }
11fdf7f2 1899 ldpp_dout(this, 2) << "RGWGetObj::handle_slo_manifest()" << dendl;
7c673cae 1900
31f18b77 1901 vector<RGWAccessControlPolicy> allocated_acls;
11fdf7f2 1902 map<string, pair<RGWAccessControlPolicy *, boost::optional<Policy>>> policies;
f67539c2 1903 map<string, std::unique_ptr<rgw::sal::RGWBucket>> buckets;
7c673cae
FG
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
f67539c2 1933 rgw::sal::RGWBucket* bucket;
31f18b77
FG
1934 RGWAccessControlPolicy *bucket_acl;
1935 Policy* bucket_policy;
7c673cae 1936
f67539c2 1937 if (bucket_name.compare(s->bucket->get_name()) != 0) {
7c673cae
FG
1938 const auto& piter = policies.find(bucket_name);
1939 if (piter != policies.end()) {
31f18b77
FG
1940 bucket_acl = piter->second.first;
1941 bucket_policy = piter->second.second.get_ptr();
f67539c2 1942 bucket = buckets[bucket_name].get();
7c673cae 1943 } else {
31f18b77
FG
1944 allocated_acls.push_back(RGWAccessControlPolicy(s->cct));
1945 RGWAccessControlPolicy& _bucket_acl = allocated_acls.back();
7c673cae 1946
f67539c2 1947 std::unique_ptr<rgw::sal::RGWBucket> tmp_bucket;
9f95a23c 1948 auto obj_ctx = store->svc()->sysobj->init_obj_ctx();
b3b6e05e 1949 int r = store->get_bucket(this, s->user.get(), s->user->get_tenant(), bucket_name, &tmp_bucket, y);
7c673cae 1950 if (r < 0) {
11fdf7f2 1951 ldpp_dout(this, 0) << "could not get bucket info for bucket="
7c673cae
FG
1952 << bucket_name << dendl;
1953 return r;
1954 }
f67539c2 1955 bucket = tmp_bucket.get();
31f18b77 1956 bucket_acl = &_bucket_acl;
b3b6e05e 1957 r = read_bucket_policy(this, store, s, tmp_bucket->get_info(), tmp_bucket->get_attrs(), bucket_acl,
f67539c2 1958 tmp_bucket->get_key(), y);
7c673cae 1959 if (r < 0) {
11fdf7f2 1960 ldpp_dout(this, 0) << "failed to read bucket ACL for bucket "
7c673cae
FG
1961 << bucket << dendl;
1962 return r;
31f18b77
FG
1963 }
1964 auto _bucket_policy = get_iam_policy_from_attr(
f67539c2 1965 s->cct, tmp_bucket->get_attrs(), tmp_bucket->get_tenant());
31f18b77 1966 bucket_policy = _bucket_policy.get_ptr();
f67539c2 1967 buckets[bucket_name].swap(tmp_bucket);
31f18b77 1968 policies[bucket_name] = make_pair(bucket_acl, _bucket_policy);
7c673cae
FG
1969 }
1970 } else {
f67539c2 1971 bucket = s->bucket.get();
c07f9fc5 1972 bucket_acl = s->bucket_acl.get();
31f18b77 1973 bucket_policy = s->iam_policy.get_ptr();
7c673cae
FG
1974 }
1975
1976 rgw_slo_part part;
31f18b77 1977 part.bucket_acl = bucket_acl;
7c673cae
FG
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;
eafe8130 1983 ldpp_dout(this, 20) << "slo_part: bucket=" << part.bucket
7c673cae
FG
1984 << " obj=" << part.obj_name
1985 << " size=" << part.size
1986 << " etag=" << part.etag
1987 << dendl;
1988
11fdf7f2 1989 etag_sum.Update((const unsigned char *)entry.etag.c_str(),
7c673cae
FG
1990 entry.etag.length());
1991
1992 slo_parts[total_len] = part;
1993 total_len += part.size;
11fdf7f2 1994 } /* foreach entry */
7c673cae
FG
1995
1996 complete_etag(etag_sum, &lo_etag);
1997
1998 s->obj_size = slo_info.total_size;
f67539c2 1999 s->object->set_obj_size(slo_info.total_size);
11fdf7f2 2000 ldpp_dout(this, 20) << "s->obj_size=" << s->obj_size << dendl;
7c673cae 2001
31f18b77
FG
2002 int r = RGWRados::Object::Read::range_to_ofs(total_len, ofs, end);
2003 if (r < 0) {
2004 return r;
7c673cae
FG
2005 }
2006
2007 total_len = end - ofs + 1;
eafe8130
TL
2008 ldpp_dout(this, 20) << "Requested: ofs=" << ofs
2009 << " end=" << end
2010 << " total=" << total_len
2011 << dendl;
7c673cae 2012
31f18b77 2013 r = iterate_slo_parts(s->cct, store, ofs, end, slo_parts,
7c673cae
FG
2014 get_obj_user_manifest_iterate_cb, (void *)this);
2015 if (r < 0) {
2016 return r;
2017 }
2018
2019 return 0;
2020}
2021
2022int RGWGetObj::get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len)
2023{
f91f0fd5
TL
2024 /* garbage collection related handling:
2025 * defer_gc disabled for https://tracker.ceph.com/issues/47866 */
7c673cae
FG
2026 return send_response_data(bl, bl_ofs, bl_len);
2027}
2028
2029bool RGWGetObj::prefetch_data()
2030{
2031 /* HEAD request, stop prefetch*/
f6b5b4d7 2032 if (!get_data || s->info.env->exists("HTTP_X_RGW_AUTH")) {
7c673cae
FG
2033 return false;
2034 }
2035
7c673cae 2036 range_str = s->info.env->get("HTTP_RANGE");
1911f103 2037 // TODO: add range prefetch
28e407b8 2038 if (range_str) {
1911f103
TL
2039 parse_range();
2040 return false;
7c673cae
FG
2041 }
2042
1911f103 2043 return get_data;
7c673cae 2044}
28e407b8 2045
7c673cae
FG
2046void RGWGetObj::pre_exec()
2047{
2048 rgw_bucket_object_pre_exec(s);
2049}
2050
9f95a23c
TL
2051static 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) {
b3b6e05e 2061 ldpp_dout(s, 0)
9f95a23c
TL
2062 << "ERROR: caught buffer::error, couldn't decode TagSet" << dendl;
2063 }
2064 }
2065}
2066
f67539c2 2067void RGWGetObj::execute(optional_yield y)
7c673cae 2068{
7c673cae
FG
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);
11fdf7f2 2077 RGWGetObj_Filter* filter = (RGWGetObj_Filter *)&cb;
7c673cae 2078 boost::optional<RGWGetObj_Decompress> decompress;
11fdf7f2 2079 std::unique_ptr<RGWGetObj_Filter> decrypt;
7c673cae
FG
2080 map<string, bufferlist>::iterator attr_iter;
2081
2082 perfcounter->inc(l_rgw_get);
2083
f67539c2 2084 std::unique_ptr<rgw::sal::RGWObject::ReadOp> read_op(s->object->get_read_op(s->obj_ctx));
7c673cae 2085
f67539c2 2086 op_ret = get_params(y);
7c673cae
FG
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
f67539c2
TL
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
b3b6e05e 2103 op_ret = read_op->prepare(s->yield, this);
7c673cae
FG
2104 if (op_ret < 0)
2105 goto done_err;
f67539c2
TL
2106 version_id = s->object->get_instance();
2107 s->obj_size = s->object->get_obj_size();
2108 attrs = s->object->get_attrs();
7c673cae
FG
2109
2110 /* STAT ops don't need data, and do no i/o */
2111 if (get_type() == RGW_OP_STAT_OBJ) {
2112 return;
2113 }
f6b5b4d7
TL
2114 if (s->info.env->exists("HTTP_X_RGW_AUTH")) {
2115 op_ret = 0;
2116 goto done_err;
2117 }
7c673cae
FG
2118 /* start gettorrent */
2119 if (torrent.get_flag())
2120 {
3efd9988
FG
2121 attr_iter = attrs.find(RGW_ATTR_CRYPT_MODE);
2122 if (attr_iter != attrs.end() && attr_iter->second.to_str() == "SSE-C-AES256") {
11fdf7f2 2123 ldpp_dout(this, 0) << "ERROR: torrents are not supported for objects "
28e407b8
AA
2124 "encrypted with SSE-C" << dendl;
2125 op_ret = -EINVAL;
3efd9988
FG
2126 goto done_err;
2127 }
7c673cae 2128 torrent.init(s, store);
f67539c2
TL
2129 rgw_obj obj = s->object->get_obj();
2130 op_ret = torrent.get_torrent_file(s->object.get(), total_len, bl, obj);
7c673cae
FG
2131 if (op_ret < 0)
2132 {
11fdf7f2 2133 ldpp_dout(this, 0) << "ERROR: failed to get_torrent_file ret= " << op_ret
7c673cae
FG
2134 << dendl;
2135 goto done_err;
2136 }
2137 op_ret = send_response_data(bl, 0, total_len);
2138 if (op_ret < 0)
2139 {
11fdf7f2 2140 ldpp_dout(this, 0) << "ERROR: failed to send_response_data ret= " << op_ret << dendl;
7c673cae
FG
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) {
b3b6e05e 2149 ldpp_dout(this, 0) << "ERROR: failed to decode compression info, cannot decompress" << dendl;
7c673cae
FG
2150 goto done_err;
2151 }
2152 if (need_decompress) {
2153 s->obj_size = cs_info.orig_size;
f67539c2 2154 s->object->set_obj_size(cs_info.orig_size);
7c673cae
FG
2155 decompress.emplace(s->cct, &cs_info, partial_content, filter);
2156 filter = &*decompress;
2157 }
7c673cae
FG
2158
2159 attr_iter = attrs.find(RGW_ATTR_USER_MANIFEST);
2160 if (attr_iter != attrs.end() && !skip_manifest) {
f67539c2 2161 op_ret = handle_user_manifest(attr_iter->second.c_str(), y);
7c673cae 2162 if (op_ret < 0) {
11fdf7f2 2163 ldpp_dout(this, 0) << "ERROR: failed to handle user manifest ret="
7c673cae
FG
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;
f67539c2 2173 op_ret = handle_slo_manifest(attr_iter->second, y);
7c673cae 2174 if (op_ret < 0) {
11fdf7f2 2175 ldpp_dout(this, 0) << "ERROR: failed to handle slo manifest ret=" << op_ret
7c673cae
FG
2176 << dendl;
2177 goto done_err;
2178 }
2179 return;
2180 }
2181
31f18b77
FG
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
f67539c2 2189 op_ret = s->object->range_to_ofs(s->obj_size, ofs, end);
31f18b77
FG
2190 if (op_ret < 0)
2191 goto done_err;
2192 total_len = (ofs <= end ? end + 1 - ofs : 0);
2193
7c673cae
FG
2194 /* Check whether the object has expired. Swift API documentation
2195 * stands that we should return 404 Not Found in such case. */
f67539c2 2196 if (need_object_expiration() && s->object->is_expired()) {
7c673cae
FG
2197 op_ret = -ENOENT;
2198 goto done_err;
2199 }
2200
9f95a23c
TL
2201 /* Decode S3 objtags, if any */
2202 rgw_cond_decode_objtags(s, attrs);
2203
7c673cae
FG
2204 start = ofs;
2205
7c673cae
FG
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);
b3b6e05e 2226 op_ret = read_op->iterate(this, ofs_x, end_x, filter, s->yield);
7c673cae
FG
2227
2228 if (op_ret >= 0)
2229 op_ret = filter->flush();
2230
11fdf7f2 2231 perfcounter->tinc(l_rgw_get_lat, s->time_elapsed());
7c673cae
FG
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
2242done_err:
f67539c2 2243 send_response_data_error(y);
7c673cae
FG
2244}
2245
2246int RGWGetObj::init_common()
2247{
2248 if (range_str) {
28e407b8 2249 /* range parsed error when prefetch */
7c673cae 2250 if (!range_parsed) {
28e407b8 2251 int r = parse_range();
7c673cae
FG
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
f67539c2 2271int RGWListBuckets::verify_permission(optional_yield y)
7c673cae 2272{
eafe8130
TL
2273 rgw::Partition partition = rgw::Partition::aws;
2274 rgw::Service service = rgw::Service::s3;
11fdf7f2 2275
f67539c2
TL
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)) {
7c673cae
FG
2284 return -EACCES;
2285 }
2286
2287 return 0;
2288}
2289
f67539c2 2290int RGWGetUsage::verify_permission(optional_yield y)
7c673cae
FG
2291{
2292 if (s->auth.identity->is_anonymous()) {
2293 return -EACCES;
2294 }
2295
2296 return 0;
2297}
2298
f67539c2 2299void RGWListBuckets::execute(optional_yield y)
7c673cae
FG
2300{
2301 bool done;
2302 bool started = false;
2303 uint64_t total_count = 0;
2304
3efd9988 2305 const uint64_t max_buckets = s->cct->_conf->rgw_list_buckets_max_chunk;
7c673cae 2306
f67539c2 2307 op_ret = get_params(y);
7c673cae
FG
2308 if (op_ret < 0) {
2309 goto send_end;
2310 }
2311
2312 if (supports_account_metadata()) {
b3b6e05e 2313 op_ret = store->ctl()->user->get_attrs_by_uid(this, s->user->get_id(), &attrs, s->yield);
7c673cae
FG
2314 if (op_ret < 0) {
2315 goto send_end;
2316 }
2317 }
2318
2319 is_truncated = false;
2320 do {
9f95a23c 2321 rgw::sal::RGWBucketList buckets;
7c673cae
FG
2322 uint64_t read_count;
2323 if (limit >= 0) {
11fdf7f2 2324 read_count = min(limit - total_count, max_buckets);
7c673cae
FG
2325 } else {
2326 read_count = max_buckets;
2327 }
2328
b3b6e05e 2329 op_ret = s->user->list_buckets(this, marker, end_marker, read_count, should_get_stats(), buckets, y);
9f95a23c 2330
7c673cae
FG
2331 if (op_ret < 0) {
2332 /* hmm.. something wrong here.. the user was authenticated, so it
2333 should exist */
11fdf7f2 2334 ldpp_dout(this, 10) << "WARNING: failed on rgw_get_user_buckets uid="
9f95a23c 2335 << s->user->get_id() << dendl;
7c673cae
FG
2336 break;
2337 }
3efd9988
FG
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. */
f67539c2 2342 for (const auto& policy : store->get_zonegroup().placement_targets) {
3efd9988
FG
2343 policies_stats.emplace(policy.second.name,
2344 decltype(policies_stats)::mapped_type());
2345 }
2346
f67539c2 2347 std::map<std::string, std::unique_ptr<rgw::sal::RGWBucket>>& m = buckets.get_buckets();
3efd9988
FG
2348 for (const auto& kv : m) {
2349 const auto& bucket = kv.second;
2350
9f95a23c
TL
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();
3efd9988
FG
2354
2355 /* operator[] still can create a new entry for storage policy seen
2356 * for first time. */
9f95a23c
TL
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();
3efd9988 2360 policy_stats.buckets_count++;
9f95a23c 2361 policy_stats.objects_count += bucket->get_count();
7c673cae 2362 }
3efd9988 2363 global_stats.buckets_count += m.size();
7c673cae
FG
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
9f95a23c
TL
2373 if (read_count > 0 &&
2374 !m.empty()) {
f67539c2 2375 auto riter = m.rbegin();
7c673cae 2376 marker = riter->first;
3efd9988
FG
2377
2378 handle_listing_chunk(std::move(buckets));
7c673cae
FG
2379 }
2380 } while (is_truncated && !done);
2381
2382send_end:
2383 if (!started) {
2384 send_response_begin(false);
2385 }
2386 send_response_end();
2387}
2388
f67539c2 2389void RGWGetUsage::execute(optional_yield y)
7c673cae
FG
2390{
2391 uint64_t start_epoch = 0;
2392 uint64_t end_epoch = (uint64_t)-1;
f67539c2 2393 op_ret = get_params(y);
7c673cae
FG
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) {
11fdf7f2 2400 ldpp_dout(this, 0) << "ERROR: failed to parse start date" << dendl;
7c673cae
FG
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) {
11fdf7f2 2408 ldpp_dout(this, 0) << "ERROR: failed to parse end date" << dendl;
7c673cae
FG
2409 return;
2410 }
2411 }
2412
2413 uint32_t max_entries = 1000;
2414
2415 bool is_truncated = true;
2416
2417 RGWUsageIter usage_iter;
2418
f67539c2 2419 while (s->bucket && is_truncated) {
b3b6e05e 2420 op_ret = s->bucket->read_usage(this, start_epoch, end_epoch, max_entries, &is_truncated,
f67539c2 2421 usage_iter, usage);
7c673cae
FG
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
b3b6e05e 2432 op_ret = rgw_user_sync_all_stats(this, store, s->user->get_id(), y);
7c673cae 2433 if (op_ret < 0) {
11fdf7f2 2434 ldpp_dout(this, 0) << "ERROR: failed to sync user stats" << dendl;
c07f9fc5
FG
2435 return;
2436 }
2437
b3b6e05e 2438 op_ret = rgw_user_get_all_buckets_stats(this, store, s->user->get_id(), buckets_usage, y);
c07f9fc5 2439 if (op_ret < 0) {
11fdf7f2
TL
2440 ldpp_dout(this, 0) << "ERROR: failed to get user's buckets stats" << dendl;
2441 return;
7c673cae 2442 }
c07f9fc5 2443
b3b6e05e 2444 op_ret = store->ctl()->user->read_stats(this, s->user->get_id(), &stats, y);
7c673cae 2445 if (op_ret < 0) {
11fdf7f2 2446 ldpp_dout(this, 0) << "ERROR: can't read user header" << dendl;
c07f9fc5 2447 return;
7c673cae
FG
2448 }
2449
2450 return;
2451}
2452
f67539c2 2453int RGWStatAccount::verify_permission(optional_yield y)
7c673cae 2454{
11fdf7f2 2455 if (!verify_user_permission_no_policy(this, s, RGW_PERM_READ)) {
7c673cae
FG
2456 return -EACCES;
2457 }
2458
2459 return 0;
2460}
2461
f67539c2 2462void RGWStatAccount::execute(optional_yield y)
7c673cae
FG
2463{
2464 string marker;
9f95a23c 2465 rgw::sal::RGWBucketList buckets;
7c673cae 2466 uint64_t max_buckets = s->cct->_conf->rgw_list_buckets_max_chunk;
f6b5b4d7 2467 const string *lastmarker;
7c673cae
FG
2468
2469 do {
7c673cae 2470
f6b5b4d7 2471 lastmarker = nullptr;
b3b6e05e 2472 op_ret = rgw_read_user_buckets(this, store, s->user->get_id(), buckets, marker,
f67539c2 2473 string(), max_buckets, true, y);
7c673cae
FG
2474 if (op_ret < 0) {
2475 /* hmm.. something wrong here.. the user was authenticated, so it
2476 should exist */
f6b5b4d7
TL
2477 ldpp_dout(this, 10) << "WARNING: failed on rgw_read_user_buckets uid="
2478 << s->user->get_id() << " ret=" << op_ret << dendl;
7c673cae
FG
2479 break;
2480 } else {
3efd9988
FG
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. */
f67539c2 2484 for (const auto& policy : store->get_zonegroup().placement_targets) {
3efd9988
FG
2485 policies_stats.emplace(policy.second.name,
2486 decltype(policies_stats)::mapped_type());
2487 }
2488
f67539c2 2489 std::map<std::string, std::unique_ptr<rgw::sal::RGWBucket>>& m = buckets.get_buckets();
3efd9988
FG
2490 for (const auto& kv : m) {
2491 const auto& bucket = kv.second;
f6b5b4d7 2492 lastmarker = &kv.first;
3efd9988 2493
9f95a23c
TL
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();
3efd9988
FG
2497
2498 /* operator[] still can create a new entry for storage policy seen
2499 * for first time. */
9f95a23c
TL
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();
3efd9988 2503 policy_stats.buckets_count++;
9f95a23c 2504 policy_stats.objects_count += bucket->get_count();
7c673cae 2505 }
3efd9988 2506 global_stats.buckets_count += m.size();
7c673cae
FG
2507
2508 }
f6b5b4d7 2509 if (!lastmarker) {
b3b6e05e 2510 ldpp_dout(this, -1) << "ERROR: rgw_read_user_buckets, stasis at marker="
f6b5b4d7
TL
2511 << marker << " uid=" << s->user->get_id() << dendl;
2512 break;
2513 }
2514 marker = *lastmarker;
9f95a23c 2515 } while (buckets.is_truncated());
7c673cae
FG
2516}
2517
f67539c2 2518int RGWGetBucketVersioning::verify_permission(optional_yield y)
7c673cae 2519{
b32b8144 2520 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketVersioning);
7c673cae
FG
2521}
2522
2523void RGWGetBucketVersioning::pre_exec()
2524{
2525 rgw_bucket_object_pre_exec(s);
2526}
2527
f67539c2 2528void RGWGetBucketVersioning::execute(optional_yield y)
7c673cae 2529{
f67539c2
TL
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();
7c673cae
FG
2538}
2539
f67539c2 2540int RGWSetBucketVersioning::verify_permission(optional_yield y)
7c673cae 2541{
b32b8144 2542 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketVersioning);
7c673cae
FG
2543}
2544
2545void RGWSetBucketVersioning::pre_exec()
2546{
2547 rgw_bucket_object_pre_exec(s);
2548}
2549
f67539c2 2550void RGWSetBucketVersioning::execute(optional_yield y)
7c673cae 2551{
f67539c2 2552 op_ret = get_params(y);
7c673cae
FG
2553 if (op_ret < 0)
2554 return;
2555
f67539c2
TL
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) {
522d829b
TL
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;
eafe8130
TL
2564 op_ret = -ERR_INVALID_BUCKET_STATE;
2565 return;
2566 }
2567
f67539c2 2568 bool cur_mfa_status = s->bucket->get_info().mfa_enabled();
11fdf7f2
TL
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 }
92f5a8d4
TL
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) {
f67539c2 2582 req_versioning_status = (s->bucket->get_info().flags & BUCKET_VERSIONS_SUSPENDED) != 0;
92f5a8d4 2583 } else if (versioning_status == VersioningSuspended) {
f67539c2 2584 req_versioning_status = (s->bucket->get_info().flags & BUCKET_VERSIONS_SUSPENDED) == 0;
92f5a8d4
TL
2585 }
2586 if (req_versioning_status && !s->mfa_verified) {
2587 op_ret = -ERR_MFA_REQUIRED;
2588 return;
2589 }
2590 }
11fdf7f2 2591
b3b6e05e 2592 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
2593 if (op_ret < 0) {
2594 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
2595 return;
7c673cae
FG
2596 }
2597
11fdf7f2
TL
2598 bool modified = mfa_set_status;
2599
b3b6e05e 2600 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [&] {
11fdf7f2
TL
2601 if (mfa_set_status) {
2602 if (mfa_status) {
f67539c2 2603 s->bucket->get_info().flags |= BUCKET_MFA_ENABLED;
11fdf7f2 2604 } else {
f67539c2 2605 s->bucket->get_info().flags &= ~BUCKET_MFA_ENABLED;
11fdf7f2
TL
2606 }
2607 }
2608
2609 if (versioning_status == VersioningEnabled) {
f67539c2
TL
2610 s->bucket->get_info().flags |= BUCKET_VERSIONED;
2611 s->bucket->get_info().flags &= ~BUCKET_VERSIONS_SUSPENDED;
11fdf7f2
TL
2612 modified = true;
2613 } else if (versioning_status == VersioningSuspended) {
f67539c2 2614 s->bucket->get_info().flags |= (BUCKET_VERSIONED | BUCKET_VERSIONS_SUSPENDED);
11fdf7f2
TL
2615 modified = true;
2616 } else {
2617 return op_ret;
b32b8144 2618 }
f67539c2 2619 s->bucket->set_attrs(rgw::sal::RGWAttrs(s->bucket_attrs));
b3b6e05e 2620 return s->bucket->put_instance_info(this, false, real_time());
b32b8144 2621 });
7c673cae 2622
11fdf7f2
TL
2623 if (!modified) {
2624 return;
2625 }
2626
7c673cae 2627 if (op_ret < 0) {
f67539c2 2628 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
7c673cae
FG
2629 << " returned err=" << op_ret << dendl;
2630 return;
2631 }
2632}
2633
f67539c2 2634int RGWGetBucketWebsite::verify_permission(optional_yield y)
7c673cae 2635{
b32b8144 2636 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketWebsite);
7c673cae
FG
2637}
2638
2639void RGWGetBucketWebsite::pre_exec()
2640{
2641 rgw_bucket_object_pre_exec(s);
2642}
2643
f67539c2 2644void RGWGetBucketWebsite::execute(optional_yield y)
7c673cae 2645{
f67539c2 2646 if (!s->bucket->get_info().has_website) {
b32b8144 2647 op_ret = -ERR_NO_SUCH_WEBSITE_CONFIGURATION;
7c673cae
FG
2648 }
2649}
2650
f67539c2 2651int RGWSetBucketWebsite::verify_permission(optional_yield y)
7c673cae 2652{
b32b8144 2653 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketWebsite);
7c673cae
FG
2654}
2655
2656void RGWSetBucketWebsite::pre_exec()
2657{
2658 rgw_bucket_object_pre_exec(s);
2659}
2660
f67539c2 2661void RGWSetBucketWebsite::execute(optional_yield y)
7c673cae 2662{
f67539c2 2663 op_ret = get_params(y);
7c673cae
FG
2664
2665 if (op_ret < 0)
2666 return;
2667
522d829b
TL
2668 if (!s->bucket_exists) {
2669 op_ret = -ERR_NO_SUCH_BUCKET;
2670 return;
2671 }
2672
b3b6e05e 2673 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
2674 if (op_ret < 0) {
2675 ldpp_dout(this, 0) << " forward_request_to_master returned ret=" << op_ret << dendl;
2676 return;
31f18b77
FG
2677 }
2678
b3b6e05e 2679 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
f67539c2
TL
2680 s->bucket->get_info().has_website = true;
2681 s->bucket->get_info().website_conf = website_conf;
b3b6e05e 2682 op_ret = s->bucket->put_instance_info(this, false, real_time());
b32b8144
FG
2683 return op_ret;
2684 });
7c673cae 2685
7c673cae 2686 if (op_ret < 0) {
f67539c2 2687 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
11fdf7f2 2688 << " returned err=" << op_ret << dendl;
7c673cae
FG
2689 return;
2690 }
2691}
2692
f67539c2 2693int RGWDeleteBucketWebsite::verify_permission(optional_yield y)
7c673cae 2694{
b32b8144 2695 return verify_bucket_owner_or_policy(s, rgw::IAM::s3DeleteBucketWebsite);
7c673cae
FG
2696}
2697
2698void RGWDeleteBucketWebsite::pre_exec()
2699{
2700 rgw_bucket_object_pre_exec(s);
2701}
2702
f67539c2 2703void RGWDeleteBucketWebsite::execute(optional_yield y)
7c673cae 2704{
522d829b
TL
2705 if (!s->bucket_exists) {
2706 op_ret = -ERR_NO_SUCH_BUCKET;
2707 return;
2708 }
2709
f67539c2 2710 bufferlist in_data;
494da23a 2711
b3b6e05e 2712 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
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;
494da23a 2717 }
b3b6e05e 2718 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
f67539c2
TL
2719 s->bucket->get_info().has_website = false;
2720 s->bucket->get_info().website_conf = RGWBucketWebsiteConf();
b3b6e05e 2721 op_ret = s->bucket->put_instance_info(this, false, real_time());
b32b8144
FG
2722 return op_ret;
2723 });
7c673cae 2724 if (op_ret < 0) {
f67539c2 2725 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket
11fdf7f2 2726 << " returned err=" << op_ret << dendl;
7c673cae
FG
2727 return;
2728 }
2729}
2730
f67539c2 2731int RGWStatBucket::verify_permission(optional_yield y)
7c673cae 2732{
31f18b77 2733 // This (a HEAD request on a bucket) is governed by the s3:ListBucket permission.
11fdf7f2 2734 if (!verify_bucket_permission(this, s, rgw::IAM::s3ListBucket)) {
7c673cae
FG
2735 return -EACCES;
2736 }
2737
2738 return 0;
2739}
2740
2741void RGWStatBucket::pre_exec()
2742{
2743 rgw_bucket_object_pre_exec(s);
2744}
2745
f67539c2 2746void RGWStatBucket::execute(optional_yield y)
7c673cae
FG
2747{
2748 if (!s->bucket_exists) {
2749 op_ret = -ERR_NO_SUCH_BUCKET;
2750 return;
2751 }
2752
b3b6e05e 2753 op_ret = store->get_bucket(this, s->user.get(), s->bucket->get_key(), &bucket, y);
f67539c2
TL
2754 if (op_ret) {
2755 return;
2756 }
b3b6e05e 2757 op_ret = bucket->update_container_stats(s);
7c673cae
FG
2758}
2759
f67539c2 2760int RGWListBucket::verify_permission(optional_yield y)
7c673cae 2761{
f67539c2 2762 op_ret = get_params(y);
31f18b77
FG
2763 if (op_ret < 0) {
2764 return op_ret;
2765 }
b32b8144
FG
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));
31f18b77 2773
11fdf7f2
TL
2774 if (!verify_bucket_permission(this,
2775 s,
31f18b77
FG
2776 list_versions ?
2777 rgw::IAM::s3ListBucketVersions :
2778 rgw::IAM::s3ListBucket)) {
7c673cae
FG
2779 return -EACCES;
2780 }
2781
2782 return 0;
2783}
2784
2785int RGWListBucket::parse_max_keys()
2786{
f64942e4
AA
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,
11fdf7f2 2792 g_conf().get_val<uint64_t>("rgw_max_listing_results"),
f64942e4 2793 default_max);
7c673cae
FG
2794}
2795
2796void RGWListBucket::pre_exec()
2797{
2798 rgw_bucket_object_pre_exec(s);
2799}
2800
f67539c2 2801void RGWListBucket::execute(optional_yield y)
7c673cae
FG
2802{
2803 if (!s->bucket_exists) {
2804 op_ret = -ERR_NO_SUCH_BUCKET;
2805 return;
2806 }
2807
1adf2230 2808 if (allow_unordered && !delimiter.empty()) {
11fdf7f2 2809 ldpp_dout(this, 0) <<
1adf2230
AA
2810 "ERROR: unordered bucket listing requested with a delimiter" << dendl;
2811 op_ret = -EINVAL;
2812 return;
2813 }
2814
7c673cae 2815 if (need_container_stats()) {
b3b6e05e 2816 op_ret = s->bucket->update_container_stats(s);
7c673cae
FG
2817 }
2818
f67539c2
TL
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;
7c673cae 2827
f67539c2 2828 rgw::sal::RGWBucket::ListResults results;
7c673cae 2829
b3b6e05e 2830 op_ret = s->bucket->list(this, params, max, results, y);
31f18b77 2831 if (op_ret >= 0) {
f67539c2
TL
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);
7c673cae
FG
2836 }
2837}
2838
f67539c2 2839int RGWGetBucketLogging::verify_permission(optional_yield y)
7c673cae 2840{
b32b8144 2841 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketLogging);
7c673cae
FG
2842}
2843
f67539c2 2844int RGWGetBucketLocation::verify_permission(optional_yield y)
7c673cae 2845{
b32b8144 2846 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketLocation);
7c673cae
FG
2847}
2848
f67539c2 2849int RGWCreateBucket::verify_permission(optional_yield y)
7c673cae
FG
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
11fdf7f2
TL
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)) {
7c673cae
FG
2863 return -EACCES;
2864 }
2865
9f95a23c 2866 if (s->user->get_tenant() != s->bucket_tenant) {
f67539c2
TL
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 }
7c673cae 2875 }
f67539c2 2876
9f95a23c 2877 if (s->user->get_max_buckets() < 0) {
7c673cae
FG
2878 return -EPERM;
2879 }
2880
9f95a23c
TL
2881 if (s->user->get_max_buckets()) {
2882 rgw::sal::RGWBucketList buckets;
7c673cae 2883 string marker;
b3b6e05e 2884 op_ret = rgw_read_user_buckets(this, store, s->user->get_id(), buckets,
9f95a23c 2885 marker, string(), s->user->get_max_buckets(),
f67539c2 2886 false, y);
7c673cae
FG
2887 if (op_ret < 0) {
2888 return op_ret;
2889 }
2890
9f95a23c 2891 if ((int)buckets.count() >= s->user->get_max_buckets()) {
7c673cae
FG
2892 return -ERR_TOO_MANY_BUCKETS;
2893 }
2894 }
2895
2896 return 0;
2897}
2898
7c673cae
FG
2899void RGWCreateBucket::pre_exec()
2900{
2901 rgw_bucket_object_pre_exec(s);
2902}
2903
2904static 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. */
2930static 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
2960static 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
2972static 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
3030static 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
f67539c2 3070void RGWCreateBucket::execute(optional_yield y)
7c673cae 3071{
7c673cae
FG
3072 buffer::list aclbl;
3073 buffer::list corsbl;
9f95a23c
TL
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);
7c673cae 3076
f67539c2 3077 op_ret = get_params(y);
7c673cae
FG
3078 if (op_ret < 0)
3079 return;
3080
11fdf7f2
TL
3081 if (!relaxed_region_enforcement &&
3082 !location_constraint.empty() &&
9f95a23c 3083 !store->svc()->zone->has_zonegroup_api(location_constraint)) {
11fdf7f2 3084 ldpp_dout(this, 0) << "location constraint (" << location_constraint << ")"
31f18b77
FG
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
f67539c2
TL
3091 if (!relaxed_region_enforcement && !store->get_zonegroup().is_master_zonegroup() && !location_constraint.empty() &&
3092 store->get_zonegroup().api_name != location_constraint) {
11fdf7f2 3093 ldpp_dout(this, 0) << "location constraint (" << location_constraint << ")"
f67539c2 3094 << " doesn't match zonegroup" << " (" << store->get_zonegroup().api_name << ")"
31f18b77
FG
3095 << dendl;
3096 op_ret = -ERR_INVALID_LOCATION_CONSTRAINT;
3097 s->err.message = "The specified location-constraint is not valid";
7c673cae
FG
3098 return;
3099 }
3100
f67539c2 3101 const auto& zonegroup = store->get_zonegroup();
11fdf7f2
TL
3102 if (!placement_rule.name.empty() &&
3103 !zonegroup.placement_targets.count(placement_rule.name)) {
3104 ldpp_dout(this, 0) << "placement target (" << placement_rule.name << ")"
c07f9fc5 3105 << " doesn't exist in the placement targets of zonegroup"
f67539c2 3106 << " (" << store->get_zonegroup().api_name << ")" << dendl;
c07f9fc5
FG
3107 op_ret = -ERR_INVALID_LOCATION_CONSTRAINT;
3108 s->err.message = "The specified placement target does not exist";
3109 return;
3110 }
3111
7c673cae
FG
3112 /* we need to make sure we read bucket info, it's not read before for this
3113 * specific request */
b3b6e05e 3114 op_ret = store->get_bucket(this, s->user.get(), s->bucket_tenant, s->bucket_name, &s->bucket, y);
7c673cae
FG
3115 if (op_ret < 0 && op_ret != -ENOENT)
3116 return;
3117 s->bucket_exists = (op_ret != -ENOENT);
3118
7c673cae 3119 if (s->bucket_exists) {
f67539c2
TL
3120 if (!s->system_request &&
3121 store->svc()->zone->get_zonegroup().get_id() != s->bucket->get_info().zonegroup) {
3122 op_ret = -EEXIST;
7c673cae
FG
3123 return;
3124 }
f67539c2
TL
3125 /* Initialize info from req_state */
3126 info = s->bucket->get_info();
7c673cae
FG
3127 }
3128
f67539c2
TL
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
7c673cae
FG
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()) {
f67539c2 3137 zonegroup_id = store->get_zonegroup().get_id();
7c673cae
FG
3138 }
3139 } else {
f67539c2 3140 zonegroup_id = store->get_zonegroup().get_id();
7c673cae
FG
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. */
b3b6e05e 3159 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs, false);
3efd9988
FG
3160 if (op_ret < 0) {
3161 return;
3162 }
7c673cae
FG
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. */
f67539c2
TL
3174 filter_out_website(attrs, rmattr_names, info.website_conf);
3175 info.has_website = !info.website_conf.is_empty();
7c673cae
FG
3176 }
3177
f67539c2
TL
3178 rgw_bucket tmp_bucket;
3179 tmp_bucket.tenant = s->bucket_tenant; /* ignored if bucket exists */
3180 tmp_bucket.name = s->bucket_name;
7c673cae
FG
3181
3182 /* Handle updates of the metadata for Swift's object versioning. */
3183 if (swift_ver_location) {
f67539c2
TL
3184 info.swift_ver_location = *swift_ver_location;
3185 info.swift_versioning = (! swift_ver_location->empty());
eafe8130
TL
3186 }
3187
f67539c2
TL
3188 /* We're replacing bucket with the newly created one */
3189 ldpp_dout(this, 10) << "user=" << s->user << " bucket=" << tmp_bucket << dendl;
b3b6e05e 3190 op_ret = store->create_bucket(this, *s->user, tmp_bucket, zonegroup_id,
f67539c2
TL
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);
7c673cae 3196
7c673cae
FG
3197 /* continue if EEXIST and create_bucket will fail below. this way we can
3198 * recover from a partial create by retrying it. */
f67539c2 3199 ldpp_dout(this, 20) << "rgw_create_bucket returned ret=" << op_ret << " bucket=" << s->bucket.get() << dendl;
7c673cae 3200
f67539c2 3201 if (op_ret)
7c673cae
FG
3202 return;
3203
f67539c2 3204 const bool existed = s->bucket_exists;
7c673cae
FG
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 */
f67539c2 3212 if (s->bucket->get_info().owner.compare(s->user->get_id()) != 0) {
7c673cae
FG
3213 op_ret = -EEXIST;
3214 return;
3215 }
7c673cae
FG
3216 }
3217
f67539c2 3218 op_ret = store->ctl()->bucket->link_bucket(s->user->get_id(), s->bucket->get_key(),
b3b6e05e 3219 s->bucket->get_creation_time(), y, s, false);
7c673cae
FG
3220 if (op_ret && !existed && op_ret != -EEXIST) {
3221 /* if it exists (or previously existed), don't remove it! */
b3b6e05e 3222 op_ret = store->ctl()->bucket->unlink_bucket(s->user->get_id(), s->bucket->get_key(), y, this);
7c673cae 3223 if (op_ret < 0) {
11fdf7f2 3224 ldpp_dout(this, 0) << "WARNING: failed to unlink bucket: ret=" << op_ret
7c673cae
FG
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 {
7c673cae
FG
3238 map<string, bufferlist> battrs;
3239
b3b6e05e 3240 op_ret = s->bucket->get_bucket_info(this, y);
7c673cae
FG
3241 if (op_ret < 0) {
3242 return;
f67539c2 3243 } else if (!s->bucket->is_owner(s->user.get())) {
7c673cae
FG
3244 /* New bucket doesn't belong to the account we're operating on. */
3245 op_ret = -EEXIST;
3246 return;
3247 } else {
f67539c2 3248 s->bucket_attrs = s->bucket->get_attrs();
7c673cae
FG
3249 }
3250
3251 attrs.clear();
3252
b3b6e05e 3253 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs, false);
3efd9988
FG
3254 if (op_ret < 0) {
3255 return;
3256 }
7c673cae
FG
3257 prepare_add_del_attrs(s->bucket_attrs, rmattr_names, attrs);
3258 populate_with_generic_attrs(s, attrs);
f67539c2 3259 op_ret = filter_out_quota_info(attrs, rmattr_names, s->bucket->get_info().quota);
7c673cae
FG
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) {
f67539c2
TL
3266 s->bucket->get_info().swift_ver_location = *swift_ver_location;
3267 s->bucket->get_info().swift_versioning = (! swift_ver_location->empty());
7c673cae
FG
3268 }
3269
3270 /* Web site of Swift API. */
f67539c2
TL
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();
7c673cae
FG
3273
3274 /* This will also set the quota on the bucket. */
f67539c2
TL
3275 op_ret = store->ctl()->bucket->set_bucket_instance_attrs(s->bucket->get_info(), attrs,
3276 &s->bucket->get_info().objv_tracker,
b3b6e05e 3277 y, this);
7c673cae
FG
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
f67539c2 3287int RGWDeleteBucket::verify_permission(optional_yield y)
7c673cae 3288{
11fdf7f2 3289 if (!verify_bucket_permission(this, s, rgw::IAM::s3DeleteBucket)) {
7c673cae
FG
3290 return -EACCES;
3291 }
3292
3293 return 0;
3294}
3295
3296void RGWDeleteBucket::pre_exec()
3297{
3298 rgw_bucket_object_pre_exec(s);
3299}
3300
f67539c2 3301void RGWDeleteBucket::execute(optional_yield y)
7c673cae 3302{
11fdf7f2
TL
3303 if (s->bucket_name.empty()) {
3304 op_ret = -EINVAL;
7c673cae 3305 return;
11fdf7f2 3306 }
7c673cae
FG
3307
3308 if (!s->bucket_exists) {
11fdf7f2 3309 ldpp_dout(this, 0) << "ERROR: bucket " << s->bucket_name << " not found" << dendl;
7c673cae
FG
3310 op_ret = -ERR_NO_SUCH_BUCKET;
3311 return;
3312 }
3313 RGWObjVersionTracker ot;
f67539c2 3314 ot.read_version = s->bucket->get_version();
7c673cae
FG
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()) {
11fdf7f2 3325 ldpp_dout(this, 0) << "failed to parse ver param" << dendl;
7c673cae
FG
3326 op_ret = -EINVAL;
3327 return;
3328 }
3329 ot.read_version.ver = ver;
3330 }
3331 }
3332
b3b6e05e 3333 op_ret = s->bucket->sync_user_stats(this, y);
7c673cae 3334 if ( op_ret < 0) {
11fdf7f2 3335 ldpp_dout(this, 1) << "WARNING: failed to sync user stats before bucket delete: op_ret= " << op_ret << dendl;
7c673cae 3336 }
f67539c2 3337
b3b6e05e 3338 op_ret = s->bucket->check_empty(this, y);
7c673cae
FG
3339 if (op_ret < 0) {
3340 return;
3341 }
3342
f67539c2 3343 bufferlist in_data;
b3b6e05e 3344 op_ret = store->forward_request_to_master(this, s->user.get(), &ot.read_version, in_data, nullptr, s->info, y);
f67539c2
TL
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;
7c673cae 3350 }
f67539c2 3351 return;
7c673cae
FG
3352 }
3353
a4b75251 3354 op_ret = s->bucket->remove_bucket(this, false, false, nullptr, y);
f67539c2
TL
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;
224ce89b 3359 }
f67539c2
TL
3360 return;
3361}
224ce89b 3362
f67539c2
TL
3363int 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;
b3b6e05e 3408 ret = store->get_bucket(this, s->user.get(), copy_source_tenant_name, copy_source_bucket_name,
f67539c2
TL
3409 &bucket, y);
3410 if (ret < 0) {
3411 ldpp_dout(this, 5) << __func__ << "(): get_bucket() returned ret=" << ret << dendl;
3412 return ret;
3413 }
7c673cae 3414
b3b6e05e 3415 ret = bucket->get_bucket_info(this, y);
f67539c2
TL
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();
7c673cae 3421
f67539c2
TL
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 }
7c673cae 3454 }
f67539c2
TL
3455
3456 } /* copy_source */
3457 return RGWOp::init_processing(y);
7c673cae
FG
3458}
3459
f67539c2 3460int RGWPutObj::verify_permission(optional_yield y)
7c673cae 3461{
3a9019d9 3462 if (! copy_source.empty()) {
7c673cae 3463
31f18b77 3464 RGWAccessControlPolicy cs_acl(s->cct);
11fdf7f2 3465 boost::optional<Policy> policy;
7c673cae 3466 map<string, bufferlist> cs_attrs;
f67539c2
TL
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));
7c673cae 3474
f67539c2
TL
3475 cs_object->set_atomic(s->obj_ctx);
3476 cs_object->set_prefetch_data(s->obj_ctx);
7c673cae
FG
3477
3478 /* check source object permissions */
b3b6e05e 3479 if (read_obj_policy(this, store, s, copy_source_bucket_info, cs_attrs, &cs_acl, nullptr,
f67539c2 3480 policy, cs_bucket.get(), cs_object.get(), y, true) < 0) {
7c673cae
FG
3481 return -EACCES;
3482 }
3483
3484 /* admin request overrides permission checks */
31f18b77 3485 if (! s->auth.identity->is_admin_of(cs_acl.get_owner().get_id())) {
11fdf7f2
TL
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,
f67539c2 3490 cs_object->get_instance().empty() ?
11fdf7f2
TL
3491 rgw::IAM::s3GetObject :
3492 rgw::IAM::s3GetObjectVersion,
f67539c2 3493 rgw::ARN(cs_object->get_obj())); usr_policy_res == Effect::Deny)
11fdf7f2
TL
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,
f67539c2 3501 cs_object->get_instance().empty() ?
31f18b77
FG
3502 rgw::IAM::s3GetObject :
3503 rgw::IAM::s3GetObjectVersion,
f67539c2 3504 rgw::ARN(cs_object->get_obj()));
11fdf7f2 3505 }
31f18b77
FG
3506 if (e == Effect::Deny) {
3507 return -EACCES;
11fdf7f2
TL
3508 } else if (usr_policy_res == Effect::Pass && e == Effect::Pass &&
3509 !cs_acl.verify_permission(this, *s->auth.identity, s->perm_mask,
31f18b77
FG
3510 RGW_PERM_READ)) {
3511 return -EACCES;
3512 }
11fdf7f2 3513 } else if (!cs_acl.verify_permission(this, *s->auth.identity, s->perm_mask,
31f18b77
FG
3514 RGW_PERM_READ)) {
3515 return -EACCES;
3516 }
7c673cae 3517 }
31f18b77 3518 }
7c673cae 3519
9f95a23c
TL
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
f67539c2 3527 auto op_ret = get_params(y);
11fdf7f2
TL
3528 if (op_ret < 0) {
3529 ldpp_dout(this, 20) << "get_params() returned ret=" << op_ret << dendl;
3530 return op_ret;
7c673cae
FG
3531 }
3532
11fdf7f2
TL
3533 if (s->iam_policy || ! s->iam_user_policies.empty()) {
3534 rgw_add_grant_to_iam_environment(s->env, s);
7c673cae 3535
11fdf7f2 3536 rgw_add_to_iam_environment(s->env, "s3:x-amz-acl", s->canned_acl);
7c673cae 3537
11fdf7f2
TL
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 }
7c673cae 3543 }
7c673cae 3544
11fdf7f2
TL
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 }
7c673cae 3551
11fdf7f2
TL
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 }
7c673cae 3558
f67539c2
TL
3559 /* Object needs a bucket from this point */
3560 s->object->set_bucket(s->bucket.get());
3561
522d829b 3562 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
11fdf7f2
TL
3563 boost::none,
3564 rgw::IAM::s3PutObject,
f67539c2 3565 s->object->get_obj());
522d829b 3566 if (identity_policy_res == Effect::Deny)
11fdf7f2 3567 return -EACCES;
7c673cae 3568
11fdf7f2 3569 rgw::IAM::Effect e = Effect::Pass;
522d829b 3570 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
11fdf7f2
TL
3571 if (s->iam_policy) {
3572 e = s->iam_policy->eval(s->env, *s->auth.identity,
3573 rgw::IAM::s3PutObject,
522d829b
TL
3574 s->object->get_obj(),
3575 princ_type);
11fdf7f2 3576 }
522d829b
TL
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 }
11fdf7f2 3602 return -EACCES;
522d829b
TL
3603 }
3604 if (e == Effect::Allow || identity_policy_res == Effect::Allow) {
11fdf7f2
TL
3605 return 0;
3606 }
7c673cae
FG
3607 }
3608
11fdf7f2
TL
3609 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
3610 return -EACCES;
7c673cae
FG
3611 }
3612
11fdf7f2 3613 return 0;
7c673cae
FG
3614}
3615
7c673cae
FG
3616
3617void RGWPutObj::pre_exec()
3618{
3619 rgw_bucket_object_pre_exec(s);
3620}
3621
11fdf7f2 3622class RGWPutObj_CB : public RGWGetObj_Filter
7c673cae
FG
3623{
3624 RGWPutObj *op;
3625public:
11fdf7f2 3626 explicit RGWPutObj_CB(RGWPutObj *_op) : op(_op) {}
7c673cae
FG
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
3634int RGWPutObj::get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len)
3635{
3636 bufferlist bl_tmp;
9f95a23c 3637 bl.begin(bl_ofs).copy(bl_len, bl_tmp);
7c673cae
FG
3638
3639 bl_aux.append(bl_tmp);
3640
3641 return bl_len;
3642}
3643
3644int RGWPutObj::get_data(const off_t fst, const off_t lst, bufferlist& bl)
3645{
3646 RGWPutObj_CB cb(this);
11fdf7f2 3647 RGWGetObj_Filter* filter = &cb;
7c673cae 3648 boost::optional<RGWGetObj_Decompress> decompress;
11fdf7f2 3649 std::unique_ptr<RGWGetObj_Filter> decrypt;
7c673cae
FG
3650 RGWCompressionInfo cs_info;
3651 map<string, bufferlist> attrs;
7c673cae
FG
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
f67539c2
TL
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;
7c673cae 3664
f67539c2
TL
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));
7c673cae 3667
b3b6e05e 3668 ret = read_op->prepare(s->yield, this);
7c673cae
FG
3669 if (ret < 0)
3670 return ret;
3671
f67539c2
TL
3672 obj_size = obj->get_obj_size();
3673
7c673cae 3674 bool need_decompress;
f67539c2 3675 op_ret = rgw_compression_info_from_attrset(obj->get_attrs(), need_decompress, cs_info);
7c673cae 3676 if (op_ret < 0) {
b3b6e05e 3677 ldpp_dout(this, 0) << "ERROR: failed to decode compression info" << dendl;
11fdf7f2 3678 return -EIO;
7c673cae
FG
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
f67539c2 3689 auto attr_iter = obj->get_attrs().find(RGW_ATTR_MANIFEST);
7c673cae
FG
3690 op_ret = this->get_decrypt_filter(&decrypt,
3691 filter,
f67539c2
TL
3692 obj->get_attrs(),
3693 attr_iter != obj->get_attrs().end() ? &(attr_iter->second) : nullptr);
7c673cae
FG
3694 if (decrypt != nullptr) {
3695 filter = decrypt.get();
3696 }
3697 if (op_ret < 0) {
f67539c2 3698 return op_ret;
7c673cae
FG
3699 }
3700
f67539c2 3701 ret = obj->range_to_ofs(obj_size, new_ofs, new_end);
7c673cae
FG
3702 if (ret < 0)
3703 return ret;
3704
3705 filter->fixup_range(new_ofs, new_end);
b3b6e05e 3706 ret = read_op->iterate(this, new_ofs, new_end, filter, s->yield);
7c673cae
FG
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
3717static 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
f67539c2 3739void RGWPutObj::execute(optional_yield y)
7c673cae 3740{
7c673cae
FG
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;
7c673cae
FG
3748
3749 off_t fst;
3750 off_t lst;
7c673cae
FG
3751
3752 bool need_calc_md5 = (dlo_manifest == NULL) && (slo_info == NULL);
3753 perfcounter->inc(l_rgw_put);
11fdf7f2
TL
3754 // report latency on return
3755 auto put_lat = make_scope_guard([&] {
3756 perfcounter->tinc(l_rgw_put_lat, s->time_elapsed());
3757 });
3758
7c673cae 3759 op_ret = -EINVAL;
f67539c2 3760 if (rgw::sal::RGWObject::empty(s->object.get())) {
11fdf7f2 3761 return;
7c673cae
FG
3762 }
3763
3764 if (!s->bucket_exists) {
3765 op_ret = -ERR_NO_SUCH_BUCKET;
3766 return;
3767 }
3768
f67539c2
TL
3769 /* Object needs a bucket from this point */
3770 s->object->set_bucket(s->bucket.get());
7c673cae
FG
3771
3772 op_ret = get_system_versioning_params(s, &olh_epoch, &version_id);
3773 if (op_ret < 0) {
11fdf7f2 3774 ldpp_dout(this, 20) << "get_system_versioning_params() returned ret="
7c673cae 3775 << op_ret << dendl;
11fdf7f2 3776 return;
7c673cae
FG
3777 }
3778
3779 if (supplied_md5_b64) {
3780 need_calc_md5 = true;
3781
11fdf7f2 3782 ldpp_dout(this, 15) << "supplied_md5_b64=" << supplied_md5_b64 << dendl;
7c673cae
FG
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));
11fdf7f2 3785 ldpp_dout(this, 15) << "ceph_armor ret=" << op_ret << dendl;
7c673cae
FG
3786 if (op_ret != CEPH_CRYPTO_MD5_DIGESTSIZE) {
3787 op_ret = -ERR_INVALID_DIGEST;
11fdf7f2 3788 return;
7c673cae
FG
3789 }
3790
3791 buf_to_hex((const unsigned char *)supplied_md5_bin, CEPH_CRYPTO_MD5_DIGESTSIZE, supplied_md5);
11fdf7f2 3792 ldpp_dout(this, 15) << "supplied_md5=" << supplied_md5 << dendl;
7c673cae
FG
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 */
f67539c2 3797 op_ret = s->bucket->check_quota(user_quota, bucket_quota, s->content_length, y);
7c673cae 3798 if (op_ret < 0) {
11fdf7f2
TL
3799 ldpp_dout(this, 20) << "check_quota() returned ret=" << op_ret << dendl;
3800 return;
7c673cae
FG
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
11fdf7f2
TL
3809 const bool multipart = !multipart_upload_id.empty();
3810 auto& obj_ctx = *static_cast<RGWObjectCtx*>(s->obj_ctx);
7c673cae
FG
3811
3812 /* Handle object versioning of Swift API. */
3813 if (! multipart) {
f67539c2 3814 op_ret = s->object->swift_versioning_copy(s->obj_ctx, this, s->yield);
7c673cae 3815 if (op_ret < 0) {
11fdf7f2 3816 return;
7c673cae
FG
3817 }
3818 }
3819
f67539c2 3820 // make reservation for notification if needed
b3b6e05e 3821 rgw::notify::reservation_t res(this, store, s, s->object.get());
f67539c2 3822 const auto event_type = rgw::notify::ObjectCreatedPut;
b3b6e05e 3823 op_ret = rgw::notify::publish_reserve(this, event_type, res, obj_tags.get());
f67539c2
TL
3824 if (op_ret < 0) {
3825 return;
3826 }
3827
11fdf7f2 3828 // create the object processor
9f95a23c
TL
3829 auto aio = rgw::make_throttle(s->cct->_conf->rgw_put_obj_min_window_size,
3830 s->yield);
11fdf7f2
TL
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
eafe8130 3839 multipart_upload_info upload_info;
11fdf7f2 3840 if (multipart) {
f67539c2 3841 RGWMPObj mp(s->object->get_name(), multipart_upload_id);
11fdf7f2 3842
b3b6e05e 3843 op_ret = get_multipart_info(this, s, mp.get_meta(), &upload_info);
11fdf7f2
TL
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>(
f67539c2
TL
3855 &*aio, store, s->bucket.get(), pdest_placement,
3856 s->owner.get_id(), obj_ctx, s->object->clone(),
9f95a23c
TL
3857 multipart_upload_id, multipart_part_num, multipart_part_str,
3858 this, s->yield);
11fdf7f2 3859 } else if(append) {
f67539c2 3860 if (s->bucket->versioned()) {
11fdf7f2
TL
3861 op_ret = -ERR_INVALID_BUCKET_STATE;
3862 return;
3863 }
3864 pdest_placement = &s->dest_placement;
3865 processor.emplace<AppendObjectProcessor>(
f67539c2
TL
3866 &*aio, store, s->bucket.get(), pdest_placement, s->bucket_owner.get_id(),
3867 obj_ctx, s->object->clone(),
9f95a23c 3868 s->req_id, position, &cur_accounted_size, this, s->yield);
11fdf7f2 3869 } else {
f67539c2 3870 if (s->bucket->versioning_enabled()) {
11fdf7f2 3871 if (!version_id.empty()) {
f67539c2 3872 s->object->set_instance(version_id);
11fdf7f2 3873 } else {
f67539c2
TL
3874 s->object->gen_rand_obj_instance_name();
3875 version_id = s->object->get_instance();
11fdf7f2
TL
3876 }
3877 }
3878 pdest_placement = &s->dest_placement;
3879 processor.emplace<AtomicObjectProcessor>(
f67539c2
TL
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);
11fdf7f2
TL
3883 }
3884
9f95a23c 3885 op_ret = processor->prepare(s->yield);
7c673cae 3886 if (op_ret < 0) {
11fdf7f2 3887 ldpp_dout(this, 20) << "processor->prepare() returned ret=" << op_ret
7c673cae 3888 << dendl;
11fdf7f2 3889 return;
7c673cae
FG
3890 }
3891
3a9019d9 3892 if ((! copy_source.empty()) && !copy_source_range) {
f67539c2
TL
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);
3a9019d9
FG
3895
3896 RGWObjState *astate;
b3b6e05e 3897 op_ret = obj.get_obj_state(this, &obj_ctx, bucket, &astate, s->yield);
3a9019d9 3898 if (op_ret < 0) {
11fdf7f2
TL
3899 ldpp_dout(this, 0) << "ERROR: get copy source obj state returned with error" << op_ret << dendl;
3900 return;
3a9019d9
FG
3901 }
3902 if (!astate->exists){
3903 op_ret = -ENOENT;
11fdf7f2 3904 return;
3a9019d9 3905 }
28e407b8 3906 lst = astate->accounted_size - 1;
3a9019d9
FG
3907 } else {
3908 lst = copy_source_range_lst;
3909 }
3910
7c673cae 3911 fst = copy_source_range_fst;
7c673cae 3912
11fdf7f2
TL
3913 // no filters by default
3914 DataProcessor *filter = processor.get();
3915
9f95a23c 3916 const auto& compression_type = store->svc()->zone->get_zone_params().get_compression_type(*pdest_placement);
11fdf7f2
TL
3917 CompressorRef plugin;
3918 boost::optional<RGWPutObj_Compress> compressor;
3919
3920 std::unique_ptr<DataProcessor> encrypt;
eafe8130
TL
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 }
7c673cae
FG
3938 }
3939 }
11fdf7f2 3940 tracepoint(rgw_op, before_data_transfer, s->req_id.c_str());
7c673cae 3941 do {
31f18b77 3942 bufferlist data;
7c673cae
FG
3943 if (fst > lst)
3944 break;
3a9019d9 3945 if (copy_source.empty()) {
31f18b77 3946 len = get_data(data);
7c673cae
FG
3947 } else {
3948 uint64_t cur_lst = min(fst + s->cct->_conf->rgw_max_chunk_size - 1, lst);
31f18b77 3949 op_ret = get_data(fst, cur_lst, data);
7c673cae 3950 if (op_ret < 0)
11fdf7f2 3951 return;
31f18b77 3952 len = data.length();
7c673cae
FG
3953 s->content_length += len;
3954 fst += len;
3955 }
3956 if (len < 0) {
3957 op_ret = len;
11fdf7f2
TL
3958 ldpp_dout(this, 20) << "get_data() returned ret=" << op_ret << dendl;
3959 return;
3960 } else if (len == 0) {
3961 break;
7c673cae
FG
3962 }
3963
7c673cae 3964 if (need_calc_md5) {
11fdf7f2 3965 hash.Update((const unsigned char *)data.c_str(), data.length());
7c673cae
FG
3966 }
3967
31f18b77
FG
3968 /* update torrrent */
3969 torrent.update(data);
7c673cae 3970
11fdf7f2 3971 op_ret = filter->process(std::move(data), ofs);
7c673cae 3972 if (op_ret < 0) {
11fdf7f2
TL
3973 ldpp_dout(this, 20) << "processor->process() returned ret="
3974 << op_ret << dendl;
3975 return;
7c673cae
FG
3976 }
3977
3978 ofs += len;
3979 } while (len > 0);
11fdf7f2 3980 tracepoint(rgw_op, after_data_transfer, s->req_id.c_str(), ofs);
7c673cae 3981
11fdf7f2
TL
3982 // flush any data in filters
3983 op_ret = filter->process({}, ofs);
3984 if (op_ret < 0) {
3985 return;
7c673cae
FG
3986 }
3987
31f18b77 3988 if (!chunked_upload && ofs != s->content_length) {
7c673cae 3989 op_ret = -ERR_REQUEST_TIMEOUT;
11fdf7f2 3990 return;
7c673cae
FG
3991 }
3992 s->obj_size = ofs;
f67539c2 3993 s->object->set_obj_size(ofs);
7c673cae
FG
3994
3995 perfcounter->inc(l_rgw_put_b, s->obj_size);
3996
31f18b77
FG
3997 op_ret = do_aws4_auth_completion();
3998 if (op_ret < 0) {
11fdf7f2 3999 return;
7c673cae 4000 }
31f18b77 4001
f67539c2 4002 op_ret = s->bucket->check_quota(user_quota, bucket_quota, s->obj_size, y);
7c673cae 4003 if (op_ret < 0) {
11fdf7f2
TL
4004 ldpp_dout(this, 20) << "second check_quota() returned op_ret=" << op_ret << dendl;
4005 return;
7c673cae
FG
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;
f67539c2 4015 cs_info.compressor_message = compressor->get_compressor_message();
7c673cae 4016 cs_info.blocks = move(compressor->get_compression_blocks());
11fdf7f2 4017 encode(cs_info, tmp);
7c673cae 4018 attrs[RGW_ATTR_COMPRESSION] = tmp;
11fdf7f2 4019 ldpp_dout(this, 20) << "storing " << RGW_ATTR_COMPRESSION
7c673cae
FG
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;
11fdf7f2 4031 return;
7c673cae
FG
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) {
11fdf7f2
TL
4040 ldpp_dout(this, 0) << "bad user manifest: " << dlo_manifest << dendl;
4041 return;
7c673cae 4042 }
7c673cae
FG
4043 }
4044
4045 if (slo_info) {
4046 bufferlist manifest_bl;
11fdf7f2 4047 encode(*slo_info, manifest_bl);
7c673cae 4048 emplace_attr(RGW_ATTR_SLO_MANIFEST, std::move(manifest_bl));
7c673cae
FG
4049 }
4050
4051 if (supplied_etag && etag.compare(supplied_etag) != 0) {
4052 op_ret = -ERR_UNPROCESSABLE_ENTITY;
11fdf7f2 4053 return;
7c673cae 4054 }
11fdf7f2 4055 bl.append(etag.c_str(), etag.size());
7c673cae
FG
4056 emplace_attr(RGW_ATTR_ETAG, std::move(bl));
4057
4058 populate_with_generic_attrs(s, attrs);
b3b6e05e 4059 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs);
3efd9988 4060 if (op_ret < 0) {
11fdf7f2 4061 return;
3efd9988 4062 }
7c673cae 4063 encode_delete_at_attr(delete_at, attrs);
224ce89b 4064 encode_obj_tags_attr(obj_tags.get(), attrs);
9f95a23c 4065 rgw_cond_decode_objtags(s, attrs);
7c673cae
FG
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;
31f18b77 4072 slo_userindicator_bl.append("True", 4);
7c673cae
FG
4073 emplace_attr(RGW_ATTR_SLO_UINDICATOR, std::move(slo_userindicator_bl));
4074 }
eafe8130
TL
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 }
7c673cae 4085
11fdf7f2 4086 tracepoint(rgw_op, processor_complete_enter, s->req_id.c_str());
7c673cae
FG
4087 op_ret = processor->complete(s->obj_size, etag, &mtime, real_time(), attrs,
4088 (delete_at ? *delete_at : real_time()), if_match, if_nomatch,
9f95a23c
TL
4089 (user_data.empty() ? nullptr : &user_data), nullptr, nullptr,
4090 s->yield);
11fdf7f2 4091 tracepoint(rgw_op, processor_complete_exit, s->req_id.c_str());
7c673cae
FG
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);
f67539c2 4098 op_ret = torrent.complete(y);
7c673cae
FG
4099 if (0 != op_ret)
4100 {
11fdf7f2
TL
4101 ldpp_dout(this, 0) << "ERROR: torrent.handle_data() returned " << op_ret << dendl;
4102 return;
7c673cae
FG
4103 }
4104 }
eafe8130
TL
4105
4106 // send request to notification manager
b3b6e05e 4107 const auto ret = rgw::notify::publish_commit(s->object.get(), s->obj_size, mtime, etag, event_type, res, this);
eafe8130 4108 if (ret < 0) {
f67539c2
TL
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
eafe8130 4111 }
7c673cae
FG
4112}
4113
f67539c2 4114int RGWPostObj::verify_permission(optional_yield y)
7c673cae 4115{
11fdf7f2 4116 return 0;
7c673cae 4117}
11fdf7f2 4118
7c673cae
FG
4119void RGWPostObj::pre_exec()
4120{
4121 rgw_bucket_object_pre_exec(s);
4122}
4123
f67539c2 4124void RGWPostObj::execute(optional_yield y)
7c673cae 4125{
7c673cae
FG
4126 boost::optional<RGWPutObj_Compress> compressor;
4127 CompressorRef plugin;
224ce89b 4128 char supplied_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1];
7c673cae
FG
4129
4130 /* Read in the data from the POST form. */
f67539c2 4131 op_ret = get_params(y);
7c673cae
FG
4132 if (op_ret < 0) {
4133 return;
4134 }
4135
4136 op_ret = verify_params();
4137 if (op_ret < 0) {
4138 return;
4139 }
4140
522d829b
TL
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,
11fdf7f2
TL
4143 boost::none,
4144 rgw::IAM::s3PutObject,
f67539c2 4145 s->object->get_obj());
522d829b 4146 if (identity_policy_res == Effect::Deny) {
11fdf7f2
TL
4147 op_ret = -EACCES;
4148 return;
4149 }
4150
4151 rgw::IAM::Effect e = Effect::Pass;
522d829b 4152 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
11fdf7f2
TL
4153 if (s->iam_policy) {
4154 e = s->iam_policy->eval(s->env, *s->auth.identity,
31f18b77 4155 rgw::IAM::s3PutObject,
522d829b
TL
4156 s->object->get_obj(),
4157 princ_type);
11fdf7f2 4158 }
31f18b77
FG
4159 if (e == Effect::Deny) {
4160 op_ret = -EACCES;
4161 return;
522d829b
TL
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)) {
31f18b77
FG
4196 op_ret = -EACCES;
4197 return;
4198 }
11fdf7f2 4199 } else if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae
FG
4200 op_ret = -EACCES;
4201 return;
4202 }
4203
f67539c2 4204 // make reservation for notification if needed
b3b6e05e 4205 rgw::notify::reservation_t res(this, store, s, s->object.get());
f67539c2 4206 const auto event_type = rgw::notify::ObjectCreatedPost;
b3b6e05e 4207 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
f67539c2
TL
4208 if (op_ret < 0) {
4209 return;
4210 }
4211
4212 /* Start iteration over data fields. It's necessary as Swift's FormPost
7c673cae
FG
4213 * is capable to handle multiple files in single form. */
4214 do {
7c673cae
FG
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
f67539c2 4221 op_ret = s->bucket->check_quota(user_quota, bucket_quota, s->content_length, y);
7c673cae
FG
4222 if (op_ret < 0) {
4223 return;
4224 }
4225
224ce89b
WB
4226 if (supplied_md5_b64) {
4227 char supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1];
11fdf7f2 4228 ldpp_dout(this, 15) << "supplied_md5_b64=" << supplied_md5_b64 << dendl;
224ce89b
WB
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));
11fdf7f2 4231 ldpp_dout(this, 15) << "ceph_armor ret=" << op_ret << dendl;
224ce89b
WB
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);
11fdf7f2
TL
4238 ldpp_dout(this, 15) << "supplied_md5=" << supplied_md5 << dendl;
4239 }
4240
f67539c2
TL
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();
11fdf7f2 4245 }
7c673cae 4246
9f95a23c
TL
4247 auto aio = rgw::make_throttle(s->cct->_conf->rgw_put_obj_min_window_size,
4248 s->yield);
11fdf7f2
TL
4249
4250 using namespace rgw::putobj;
f67539c2 4251 AtomicObjectProcessor processor(&*aio, store, s->bucket.get(),
11fdf7f2
TL
4252 &s->dest_placement,
4253 s->bucket_owner.get_id(),
4254 *static_cast<RGWObjectCtx*>(s->obj_ctx),
f67539c2 4255 std::move(obj), 0, s->req_id, this, s->yield);
9f95a23c 4256 op_ret = processor.prepare(s->yield);
7c673cae
FG
4257 if (op_ret < 0) {
4258 return;
4259 }
4260
11fdf7f2
TL
4261 /* No filters by default. */
4262 DataProcessor *filter = &processor;
4263
4264 std::unique_ptr<DataProcessor> encrypt;
7c673cae
FG
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 {
9f95a23c 4272 const auto& compression_type = store->svc()->zone->get_zone_params().get_compression_type(
11fdf7f2 4273 s->dest_placement);
7c673cae
FG
4274 if (compression_type != "none") {
4275 plugin = Compressor::create(s->cct, compression_type);
4276 if (!plugin) {
11fdf7f2 4277 ldpp_dout(this, 1) << "Cannot load plugin for compression type "
7c673cae
FG
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
11fdf7f2
TL
4300 hash.Update((const unsigned char *)data.c_str(), data.length());
4301 op_ret = filter->process(std::move(data), ofs);
7c673cae
FG
4302
4303 ofs += len;
4304
4305 if (ofs > max_len) {
4306 op_ret = -ERR_TOO_LARGE;
4307 return;
4308 }
4309 } while (again);
4310
11fdf7f2
TL
4311 // flush
4312 op_ret = filter->process({}, ofs);
4313 if (op_ret < 0) {
4314 return;
7c673cae
FG
4315 }
4316
4317 if (len < min_len) {
4318 op_ret = -ERR_TOO_SMALL;
4319 return;
4320 }
4321
4322 s->obj_size = ofs;
f67539c2 4323 s->object->set_obj_size(ofs);
7c673cae 4324
224ce89b 4325
f67539c2 4326 op_ret = s->bucket->check_quota(user_quota, bucket_quota, s->obj_size, y);
7c673cae
FG
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;
11fdf7f2
TL
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());
7c673cae
FG
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;
f67539c2 4359 cs_info.compressor_message = compressor->get_compressor_message();
7c673cae 4360 cs_info.blocks = move(compressor->get_compression_blocks());
11fdf7f2 4361 encode(cs_info, tmp);
7c673cae
FG
4362 emplace_attr(RGW_ATTR_COMPRESSION, std::move(tmp));
4363 }
4364
11fdf7f2
TL
4365 op_ret = processor.complete(s->obj_size, etag, nullptr, real_time(), attrs,
4366 (delete_at ? *delete_at : real_time()),
9f95a23c
TL
4367 nullptr, nullptr, nullptr, nullptr, nullptr,
4368 s->yield);
11fdf7f2
TL
4369 if (op_ret < 0) {
4370 return;
4371 }
7c673cae 4372 } while (is_next_file_to_upload());
eafe8130 4373
f67539c2 4374 // send request to notification manager
b3b6e05e 4375 const auto ret = rgw::notify::publish_commit(s->object.get(), ofs, ceph::real_clock::now(), etag, event_type, res, this);
eafe8130 4376 if (ret < 0) {
f67539c2
TL
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
eafe8130 4379 }
7c673cae
FG
4380}
4381
4382
4383void 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
f67539c2 4411int RGWPutMetadataAccount::init_processing(optional_yield y)
7c673cae
FG
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. */
f67539c2 4417 op_ret = RGWOp::init_processing(y);
7c673cae
FG
4418 if (op_ret < 0) {
4419 return op_ret;
4420 }
4421
f67539c2 4422 op_ret = get_params(y);
7c673cae
FG
4423 if (op_ret < 0) {
4424 return op_ret;
4425 }
4426
b3b6e05e 4427 op_ret = store->ctl()->user->get_attrs_by_uid(this, s->user->get_id(), &orig_attrs,
9f95a23c
TL
4428 s->yield,
4429 &acct_op_tracker);
7c673cae
FG
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
b3b6e05e 4440 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs, false);
3efd9988
FG
4441 if (op_ret < 0) {
4442 return op_ret;
4443 }
7c673cae
FG
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
f67539c2 4461int RGWPutMetadataAccount::verify_permission(optional_yield y)
7c673cae
FG
4462{
4463 if (s->auth.identity->is_anonymous()) {
4464 return -EACCES;
4465 }
4466
11fdf7f2 4467 if (!verify_user_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae
FG
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
f67539c2 4486void RGWPutMetadataAccount::execute(optional_yield y)
7c673cae
FG
4487{
4488 /* Params have been extracted earlier. See init_processing(). */
4489 RGWUserInfo new_uinfo;
b3b6e05e 4490 op_ret = store->ctl()->user->get_info_by_uid(this, s->user->get_id(), &new_uinfo, s->yield,
9f95a23c
TL
4491 RGWUserCtl::GetParams()
4492 .set_objv_tracker(&acct_op_tracker));
7c673cae
FG
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. */
b3b6e05e 4511 op_ret = store->ctl()->user->store_info(this, new_uinfo, s->yield,
9f95a23c
TL
4512 RGWUserCtl::PutParams()
4513 .set_old_info(&s->user->get_info())
4514 .set_objv_tracker(&acct_op_tracker)
4515 .set_attrs(&attrs));
7c673cae
FG
4516}
4517
f67539c2 4518int RGWPutMetadataBucket::verify_permission(optional_yield y)
7c673cae 4519{
11fdf7f2 4520 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae
FG
4521 return -EACCES;
4522 }
4523
4524 return 0;
4525}
4526
4527void RGWPutMetadataBucket::pre_exec()
4528{
4529 rgw_bucket_object_pre_exec(s);
4530}
4531
f67539c2 4532void RGWPutMetadataBucket::execute(optional_yield y)
7c673cae 4533{
f67539c2 4534 op_ret = get_params(y);
7c673cae
FG
4535 if (op_ret < 0) {
4536 return;
4537 }
4538
b3b6e05e 4539 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs, false);
3efd9988
FG
4540 if (op_ret < 0) {
4541 return;
4542 }
7c673cae
FG
4543
4544 if (!placement_rule.empty() &&
f67539c2 4545 placement_rule != s->bucket->get_placement_rule()) {
7c673cae
FG
4546 op_ret = -EEXIST;
4547 return;
4548 }
4549
b3b6e05e 4550 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
b32b8144
FG
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 }
7c673cae 4566
b32b8144
FG
4567 if (has_cors) {
4568 buffer::list bl;
4569 cors_config.encode(bl);
4570 emplace_attr(RGW_ATTR_CORS, std::move(bl));
4571 }
7c673cae 4572
b32b8144
FG
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);
7c673cae 4578
b32b8144
FG
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. */
f67539c2 4584 op_ret = filter_out_quota_info(attrs, rmattr_names, s->bucket->get_info().quota);
b32b8144
FG
4585 if (op_ret < 0) {
4586 return op_ret;
4587 }
7c673cae 4588
b32b8144 4589 if (swift_ver_location) {
f67539c2
TL
4590 s->bucket->get_info().swift_ver_location = *swift_ver_location;
4591 s->bucket->get_info().swift_versioning = (!swift_ver_location->empty());
b32b8144 4592 }
7c673cae 4593
b32b8144 4594 /* Web site of Swift API. */
f67539c2
TL
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();
7c673cae 4597
b32b8144
FG
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. */
b3b6e05e 4601 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
b32b8144
FG
4602 return op_ret;
4603 });
7c673cae
FG
4604}
4605
f67539c2 4606int RGWPutMetadataObject::verify_permission(optional_yield y)
7c673cae 4607{
31f18b77
FG
4608 // This looks to be something specific to Swift. We could add
4609 // operations like swift:PutMetadataObject to the Policy Engine.
11fdf7f2 4610 if (!verify_object_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae
FG
4611 return -EACCES;
4612 }
4613
4614 return 0;
4615}
4616
4617void RGWPutMetadataObject::pre_exec()
4618{
4619 rgw_bucket_object_pre_exec(s);
4620}
4621
f67539c2 4622void RGWPutMetadataObject::execute(optional_yield y)
7c673cae 4623{
eafe8130 4624 rgw_obj target_obj;
f67539c2 4625 rgw::sal::RGWAttrs attrs, rmattrs;
7c673cae 4626
f67539c2 4627 s->object->set_atomic(s->obj_ctx);
7c673cae 4628
f67539c2 4629 op_ret = get_params(y);
7c673cae
FG
4630 if (op_ret < 0) {
4631 return;
4632 }
4633
b3b6e05e 4634 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs);
3efd9988
FG
4635 if (op_ret < 0) {
4636 return;
4637 }
4638
7c673cae 4639 /* check if obj exists, read orig attrs */
b3b6e05e 4640 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, s, &target_obj);
7c673cae
FG
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. */
f67539c2 4647 if (need_object_expiration() && s->object->is_expired()) {
7c673cae
FG
4648 op_ret = -ENOENT;
4649 return;
4650 }
4651
4652 /* Filter currently existing attributes. */
f67539c2 4653 prepare_add_del_attrs(s->object->get_attrs(), attrs, rmattrs);
7c673cae
FG
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) {
11fdf7f2 4660 ldpp_dout(this, 0) << "bad user manifest: " << dlo_manifest << dendl;
7c673cae
FG
4661 return;
4662 }
4663 }
4664
b3b6e05e 4665 op_ret = s->object->set_obj_attrs(this, s->obj_ctx, &attrs, &rmattrs, s->yield, &target_obj);
7c673cae
FG
4666}
4667
f67539c2 4668int RGWDeleteObj::handle_slo_manifest(bufferlist& bl, optional_yield y)
7c673cae
FG
4669{
4670 RGWSLOInfo slo_info;
11fdf7f2 4671 auto bliter = bl.cbegin();
7c673cae 4672 try {
11fdf7f2 4673 decode(slo_info, bliter);
7c673cae 4674 } catch (buffer::error& err) {
11fdf7f2 4675 ldpp_dout(this, 0) << "ERROR: failed to decode slo manifest" << dendl;
7c673cae
FG
4676 return -EIO;
4677 }
4678
4679 try {
4680 deleter = std::unique_ptr<RGWBulkDelete::Deleter>(\
11fdf7f2
TL
4681 new RGWBulkDelete::Deleter(this, store, s));
4682 } catch (const std::bad_alloc&) {
7c673cae
FG
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 */);
f67539c2 4691 if (std::string_view::npos == sep_pos) {
7c673cae
FG
4692 return -EINVAL;
4693 }
4694
4695 RGWBulkDelete::acct_path_t path;
4696
31f18b77
FG
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));
7c673cae
FG
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;
f67539c2 4706 path.obj_key = s->object->get_key();
7c673cae
FG
4707 items.push_back(path);
4708
f67539c2 4709 int ret = deleter->delete_chunk(items, y);
7c673cae
FG
4710 if (ret < 0) {
4711 return ret;
4712 }
4713
4714 return 0;
4715}
4716
f67539c2 4717int RGWDeleteObj::verify_permission(optional_yield y)
7c673cae 4718{
f67539c2 4719 int op_ret = get_params(y);
eafe8130
TL
4720 if (op_ret) {
4721 return op_ret;
4722 }
522d829b 4723 if (s->iam_policy || ! s->iam_user_policies.empty() || ! s->session_policies.empty()) {
f67539c2 4724 if (s->bucket->get_info().obj_lock_enabled() && bypass_governance_mode) {
522d829b 4725 auto r = eval_identity_or_session_policies(s->iam_user_policies, s->env, boost::none,
f67539c2 4726 rgw::IAM::s3BypassGovernanceRetention, ARN(s->bucket->get_key(), s->object->get_name()));
eafe8130
TL
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,
f67539c2 4731 ARN(s->bucket->get_key(), s->object->get_name()));
eafe8130
TL
4732 if (r == Effect::Deny) {
4733 bypass_perm = false;
4734 }
522d829b
TL
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 }
eafe8130
TL
4741 }
4742 }
522d829b 4743 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
11fdf7f2 4744 boost::none,
f67539c2 4745 s->object->get_instance().empty() ?
11fdf7f2
TL
4746 rgw::IAM::s3DeleteObject :
4747 rgw::IAM::s3DeleteObjectVersion,
f67539c2 4748 ARN(s->bucket->get_key(), s->object->get_name()));
522d829b 4749 if (identity_policy_res == Effect::Deny) {
11fdf7f2
TL
4750 return -EACCES;
4751 }
4752
4753 rgw::IAM::Effect r = Effect::Pass;
522d829b 4754 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
11fdf7f2
TL
4755 if (s->iam_policy) {
4756 r = s->iam_policy->eval(s->env, *s->auth.identity,
f67539c2 4757 s->object->get_instance().empty() ?
31f18b77
FG
4758 rgw::IAM::s3DeleteObject :
4759 rgw::IAM::s3DeleteObjectVersion,
522d829b
TL
4760 ARN(s->bucket->get_key(), s->object->get_name()),
4761 princ_type);
11fdf7f2 4762 }
522d829b
TL
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 }
a8e16298 4792 return -EACCES;
522d829b
TL
4793 }
4794 if (r == Effect::Allow || identity_policy_res == Effect::Allow)
11fdf7f2 4795 return 0;
31f18b77
FG
4796 }
4797
11fdf7f2 4798 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae
FG
4799 return -EACCES;
4800 }
4801
f67539c2
TL
4802 if (s->bucket->get_info().mfa_enabled() &&
4803 !s->object->get_instance().empty() &&
11fdf7f2
TL
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
7c673cae
FG
4809 return 0;
4810}
4811
4812void RGWDeleteObj::pre_exec()
4813{
4814 rgw_bucket_object_pre_exec(s);
4815}
4816
f67539c2 4817void RGWDeleteObj::execute(optional_yield y)
7c673cae
FG
4818{
4819 if (!s->bucket_exists) {
4820 op_ret = -ERR_NO_SUCH_BUCKET;
4821 return;
4822 }
f67539c2 4823 s->object->set_bucket(s->bucket.get());
7c673cae 4824
a4b75251
TL
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();
7c673cae 4832
a4b75251
TL
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 }
7c673cae 4838
a4b75251
TL
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();
7c673cae 4851 }
7c673cae 4852
a4b75251
TL
4853 // ignore return value from get_obj_attrs in all other cases
4854 op_ret = 0;
4855
f67539c2 4856 if (check_obj_lock) {
a4b75251
TL
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 }
eafe8130
TL
4864 return;
4865 }
4866 }
9f95a23c 4867
a4b75251
TL
4868 if (multipart_delete) {
4869 if (!astate) {
4870 op_ret = -ERR_NOT_SLO_MANIFEST;
4871 return;
4872 }
eafe8130 4873
a4b75251 4874 const auto slo_attr = astate->attrset.find(RGW_ATTR_SLO_MANIFEST);
7c673cae 4875
a4b75251
TL
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;
7c673cae 4883 }
7c673cae 4884
a4b75251
TL
4885 return;
4886 }
7c673cae
FG
4887 }
4888
f67539c2 4889 // make reservation for notification if needed
b3b6e05e 4890 rgw::notify::reservation_t res(this, store, s, s->object.get());
f67539c2
TL
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;
b3b6e05e 4894 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
f67539c2
TL
4895 if (op_ret < 0) {
4896 return;
4897 }
4898
f67539c2 4899 s->object->set_atomic(s->obj_ctx);
a4b75251 4900
7c673cae 4901 bool ver_restored = false;
f67539c2 4902 op_ret = s->object->swift_versioning_restore(s->obj_ctx, ver_restored, this);
7c673cae
FG
4903 if (op_ret < 0) {
4904 return;
4905 }
4906
4907 if (!ver_restored) {
f67539c2
TL
4908 uint64_t epoch = 0;
4909
7c673cae
FG
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. */
f67539c2 4913 op_ret = get_system_versioning_params(s, &epoch, &version_id);
7c673cae 4914 if (op_ret < 0) {
f67539c2 4915 return;
7c673cae
FG
4916 }
4917
b3b6e05e 4918 op_ret = s->object->delete_object(this, obj_ctx, s->owner, s->bucket_owner, unmod_since,
f67539c2 4919 s->system_request, epoch, version_id, s->yield);
7c673cae 4920 if (op_ret >= 0) {
f67539c2 4921 delete_marker = s->object->get_delete_marker();
7c673cae
FG
4922 }
4923
4924 /* Check whether the object has expired. Swift API documentation
4925 * stands that we should return 404 Not Found in such case. */
f67539c2 4926 if (need_object_expiration() && s->object->is_expired()) {
7c673cae
FG
4927 op_ret = -ENOENT;
4928 return;
4929 }
4930 }
4931
94b18763
FG
4932 if (op_ret == -ECANCELED) {
4933 op_ret = 0;
4934 }
7c673cae
FG
4935 if (op_ret == -ERR_PRECONDITION_FAILED && no_precondition_error) {
4936 op_ret = 0;
4937 }
9f95a23c 4938
f67539c2 4939 // send request to notification manager
a4b75251 4940 const auto ret = rgw::notify::publish_commit(s->object.get(), obj_size, ceph::real_clock::now(), etag, event_type, res, this);
e306af50 4941 if (ret < 0) {
f67539c2
TL
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
e306af50 4944 }
7c673cae
FG
4945 } else {
4946 op_ret = -EINVAL;
4947 }
4948}
4949
f67539c2 4950bool RGWCopyObj::parse_copy_location(const std::string_view& url_src,
3a9019d9 4951 string& bucket_name,
b3b6e05e
TL
4952 rgw_obj_key& key,
4953 req_state* s)
7c673cae 4954{
f67539c2
TL
4955 std::string_view name_str;
4956 std::string_view params_str;
7c673cae 4957
494da23a 4958 // search for ? before url-decoding so we don't accidentally match %3F
7c673cae
FG
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
f67539c2 4967 std::string_view dec_src{name_str};
3a9019d9
FG
4968 if (dec_src[0] == '/')
4969 dec_src.remove_prefix(1);
7c673cae 4970
3a9019d9 4971 pos = dec_src.find('/');
494da23a 4972 if (pos == string::npos)
7c673cae
FG
4973 return false;
4974
494da23a
TL
4975 bucket_name = url_decode(dec_src.substr(0, pos));
4976 key.name = url_decode(dec_src.substr(pos + 1));
7c673cae
FG
4977
4978 if (key.name.empty()) {
4979 return false;
4980 }
4981
3a9019d9 4982 if (! params_str.empty()) {
7c673cae 4983 RGWHTTPArgs args;
f67539c2 4984 args.set(std::string(params_str));
b3b6e05e 4985 args.parse(s);
7c673cae
FG
4986
4987 key.instance = args.get("versionId", NULL);
4988 }
4989
4990 return true;
4991}
4992
f67539c2 4993int RGWCopyObj::verify_permission(optional_yield y)
7c673cae 4994{
31f18b77 4995 RGWAccessControlPolicy src_acl(s->cct);
11fdf7f2 4996 boost::optional<Policy> src_policy;
f67539c2 4997 op_ret = get_params(y);
7c673cae
FG
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 }
7c673cae 5005
f67539c2
TL
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);
7c673cae
FG
5008 if (op_ret < 0) {
5009 if (op_ret == -ENOENT) {
5010 op_ret = -ERR_NO_SUCH_BUCKET;
5011 }
5012 return op_ret;
5013 }
5014
b3b6e05e 5015 op_ret = src_bucket->load_by_name(this, src_tenant_name, src_bucket_name, s->bucket_instance_id,
f67539c2
TL
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 }
7c673cae 5023
f67539c2 5024 src_object->set_bucket(src_bucket.get());
7c673cae
FG
5025 /* get buckets info (source and dest) */
5026 if (s->local_source && source_zone.empty()) {
f67539c2
TL
5027 src_object->set_atomic(s->obj_ctx);
5028 src_object->set_prefetch_data(s->obj_ctx);
7c673cae 5029
11fdf7f2
TL
5030 rgw_placement_rule src_placement;
5031
7c673cae 5032 /* check source object permissions */
b3b6e05e 5033 op_ret = read_obj_policy(this, store, s, src_bucket->get_info(), src_bucket->get_attrs(), &src_acl, &src_placement.storage_class,
f67539c2 5034 src_policy, src_bucket.get(), src_object.get(), y);
7c673cae
FG
5035 if (op_ret < 0) {
5036 return op_ret;
5037 }
5038
11fdf7f2
TL
5039 /* follow up on previous checks that required reading source object head */
5040 if (need_to_check_storage_class) {
f67539c2 5041 src_placement.inherit_from(src_bucket->get_placement_rule());
11fdf7f2
TL
5042
5043 op_ret = check_storage_class(src_placement);
5044 if (op_ret < 0) {
5045 return op_ret;
5046 }
5047 }
5048
7c673cae 5049 /* admin request overrides permission checks */
31f18b77 5050 if (!s->auth.identity->is_admin_of(src_acl.get_owner().get_id())) {
a4b75251
TL
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 }
31f18b77
FG
5071 if (e == Effect::Deny) {
5072 return -EACCES;
a4b75251
TL
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 &&
11fdf7f2 5102 !src_acl.verify_permission(this, *s->auth.identity, s->perm_mask,
31f18b77
FG
5103 RGW_PERM_READ)) {
5104 return -EACCES;
5105 }
11fdf7f2 5106 } else if (!src_acl.verify_permission(this, *s->auth.identity,
31f18b77
FG
5107 s->perm_mask,
5108 RGW_PERM_READ)) {
5109 return -EACCES;
5110 }
7c673cae
FG
5111 }
5112 }
5113
5114 RGWAccessControlPolicy dest_bucket_policy(s->cct);
7c673cae
FG
5115
5116 if (src_bucket_name.compare(dest_bucket_name) == 0) { /* will only happen if s->local_source
5117 or intra region sync */
f67539c2 5118 dest_bucket = src_bucket->clone();
7c673cae 5119 } else {
f67539c2
TL
5120 op_ret = store->get_bucket(s->user.get(), RGWBucketInfo(), &dest_bucket);
5121 if (op_ret < 0) {
5122 if (op_ret == -ENOENT) {
a4b75251 5123 ldpp_dout(this, 0) << "ERROR: Destination Bucket not found for user: " << s->user->get_id().to_str() << dendl;
f67539c2
TL
5124 op_ret = -ERR_NO_SUCH_BUCKET;
5125 }
5126 return op_ret;
5127 }
b3b6e05e 5128 op_ret = dest_bucket->load_by_name(this, dest_tenant_name, dest_bucket_name, std::string(),
f67539c2 5129 s->sysobj_ctx, s->yield);
7c673cae
FG
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
f67539c2
TL
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);
7c673cae
FG
5141
5142 /* check dest bucket permissions */
b3b6e05e 5143 op_ret = read_bucket_policy(this, store, s, dest_bucket->get_info(),
f67539c2
TL
5144 dest_bucket->get_attrs(),
5145 &dest_bucket_policy, dest_bucket->get_key(), y);
7c673cae
FG
5146 if (op_ret < 0) {
5147 return op_ret;
5148 }
f67539c2 5149 auto dest_iam_policy = get_iam_policy_from_attr(s->cct, dest_bucket->get_attrs(), dest_bucket->get_tenant());
7c673cae 5150 /* admin request overrides permission checks */
11fdf7f2 5151 if (! s->auth.identity->is_admin_of(dest_policy.get_owner().get_id())){
a4b75251 5152 if (dest_iam_policy != boost::none || ! s->iam_user_policies.empty() || !s->session_policies.empty()) {
11fdf7f2 5153 rgw_add_to_iam_environment(s->env, "s3:x-amz-copy-source", copy_source);
494da23a
TL
5154 if (md_directive)
5155 rgw_add_to_iam_environment(s->env, "s3:x-amz-metadata-directive",
5156 *md_directive);
11fdf7f2 5157
a4b75251
TL
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 }
11fdf7f2
TL
5173 if (e == Effect::Deny) {
5174 return -EACCES;
a4b75251
TL
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 &&
11fdf7f2
TL
5199 ! dest_bucket_policy.verify_permission(this,
5200 *s->auth.identity,
5201 s->perm_mask,
5202 RGW_PERM_WRITE)){
5203 return -EACCES;
5204 }
92f5a8d4
TL
5205 } else if (! dest_bucket_policy.verify_permission(this, *s->auth.identity, s->perm_mask,
5206 RGW_PERM_WRITE)) {
5207 return -EACCES;
11fdf7f2 5208 }
92f5a8d4 5209
7c673cae
FG
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
5221int 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
b3b6e05e 5243 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs);
3efd9988
FG
5244 if (op_ret < 0) {
5245 return op_ret;
5246 }
7c673cae
FG
5247 populate_with_generic_attrs(s, attrs);
5248
5249 return 0;
5250}
5251
5252static 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
5258void 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
5271void RGWCopyObj::pre_exec()
5272{
5273 rgw_bucket_object_pre_exec(s);
5274}
5275
f67539c2 5276void RGWCopyObj::execute(optional_yield y)
7c673cae
FG
5277{
5278 if (init_common() < 0)
5279 return;
5280
f67539c2
TL
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
b3b6e05e 5287 rgw::notify::reservation_t res(this, store, s, s->object.get());
f67539c2 5288 const auto event_type = rgw::notify::ObjectCreatedCopy;
b3b6e05e 5289 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
f67539c2
TL
5290 if (op_ret < 0) {
5291 return;
5292 }
7c673cae 5293
11fdf7f2 5294 if ( ! version_id.empty()) {
f67539c2
TL
5295 dest_object->set_instance(version_id);
5296 } else if (dest_bucket->versioning_enabled()) {
5297 dest_object->gen_rand_obj_instance_name();
11fdf7f2
TL
5298 }
5299
f67539c2
TL
5300 src_object->set_atomic(s->obj_ctx);
5301 dest_object->set_atomic(s->obj_ctx);
7c673cae
FG
5302
5303 encode_delete_at_attr(delete_at, attrs);
5304
a4b75251
TL
5305 uint64_t obj_size = 0;
5306 {
f6b5b4d7
TL
5307 // get src object size (cached in obj_ctx from verify_permission())
5308 RGWObjState* astate = nullptr;
b3b6e05e 5309 op_ret = src_object->get_obj_state(this, s->obj_ctx, *src_bucket, &astate,
f67539c2 5310 s->yield, true);
f6b5b4d7
TL
5311 if (op_ret < 0) {
5312 return;
5313 }
a4b75251
TL
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,
f67539c2 5319 astate->accounted_size, y);
a4b75251
TL
5320 if (op_ret < 0) {
5321 return;
5322 }
f6b5b4d7
TL
5323 }
5324 }
5325
7c673cae
FG
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. */
f67539c2 5330 op_ret = dest_object->swift_versioning_copy(s->obj_ctx, this, s->yield);
7c673cae
FG
5331 if (op_ret < 0) {
5332 return;
5333 }
5334
f67539c2
TL
5335 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
5336 op_ret = src_object->copy_object(obj_ctx,
5337 s->user.get(),
9f95a23c
TL
5338 &s->info,
5339 source_zone,
f67539c2
TL
5340 dest_object.get(),
5341 dest_bucket.get(),
5342 src_bucket.get(),
9f95a23c
TL
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,
f67539c2
TL
5353 attrs,
5354 RGWObjCategory::Main,
9f95a23c 5355 olh_epoch,
f67539c2 5356 delete_at,
9f95a23c
TL
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
f67539c2 5364 // send request to notification manager
a4b75251 5365 const auto ret = rgw::notify::publish_commit(s->object.get(), obj_size, mtime, etag, event_type, res, this);
eafe8130 5366 if (ret < 0) {
f67539c2
TL
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
eafe8130 5369 }
7c673cae
FG
5370}
5371
f67539c2 5372int RGWGetACLs::verify_permission(optional_yield y)
7c673cae
FG
5373{
5374 bool perm;
f67539c2
TL
5375 if (!rgw::sal::RGWObject::empty(s->object.get())) {
5376 auto iam_action = s->object->get_instance().empty() ?
11fdf7f2
TL
5377 rgw::IAM::s3GetObjectAcl :
5378 rgw::IAM::s3GetObjectVersionAcl;
5379
5380 if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){
b3b6e05e 5381 rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2
TL
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)) {
b3b6e05e 5386 rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2
TL
5387 }
5388 }
5389 }
5390 perm = verify_object_permission(this, s, iam_action);
7c673cae 5391 } else {
a8e16298
TL
5392 if (!s->bucket_exists) {
5393 return -ERR_NO_SUCH_BUCKET;
5394 }
11fdf7f2 5395 perm = verify_bucket_permission(this, s, rgw::IAM::s3GetBucketAcl);
7c673cae
FG
5396 }
5397 if (!perm)
5398 return -EACCES;
5399
5400 return 0;
5401}
5402
5403void RGWGetACLs::pre_exec()
5404{
5405 rgw_bucket_object_pre_exec(s);
5406}
5407
f67539c2 5408void RGWGetACLs::execute(optional_yield y)
7c673cae
FG
5409{
5410 stringstream ss;
c07f9fc5 5411 RGWAccessControlPolicy* const acl = \
f67539c2 5412 (!rgw::sal::RGWObject::empty(s->object.get()) ? s->object_acl.get() : s->bucket_acl.get());
c07f9fc5
FG
5413 RGWAccessControlPolicy_S3* const s3policy = \
5414 static_cast<RGWAccessControlPolicy_S3*>(acl);
7c673cae
FG
5415 s3policy->to_xml(ss);
5416 acls = ss.str();
5417}
5418
5419
5420
f67539c2 5421int RGWPutACLs::verify_permission(optional_yield y)
7c673cae
FG
5422{
5423 bool perm;
11fdf7f2
TL
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);
f67539c2
TL
5428 if (!rgw::sal::RGWObject::empty(s->object.get())) {
5429 auto iam_action = s->object->get_instance().empty() ? rgw::IAM::s3PutObjectAcl : rgw::IAM::s3PutObjectVersionAcl;
b3b6e05e 5430 op_ret = rgw_iam_add_existing_objtags(this, store, s, iam_action);
11fdf7f2 5431 perm = verify_object_permission(this, s, iam_action);
7c673cae 5432 } else {
11fdf7f2 5433 perm = verify_bucket_permission(this, s, rgw::IAM::s3PutBucketAcl);
7c673cae
FG
5434 }
5435 if (!perm)
5436 return -EACCES;
5437
5438 return 0;
5439}
5440
f67539c2 5441int RGWGetLC::verify_permission(optional_yield y)
7c673cae
FG
5442{
5443 bool perm;
11fdf7f2 5444 perm = verify_bucket_permission(this, s, rgw::IAM::s3GetLifecycleConfiguration);
7c673cae
FG
5445 if (!perm)
5446 return -EACCES;
5447
5448 return 0;
5449}
5450
f67539c2 5451int RGWPutLC::verify_permission(optional_yield y)
7c673cae
FG
5452{
5453 bool perm;
11fdf7f2 5454 perm = verify_bucket_permission(this, s, rgw::IAM::s3PutLifecycleConfiguration);
7c673cae
FG
5455 if (!perm)
5456 return -EACCES;
5457
5458 return 0;
5459}
5460
f67539c2 5461int RGWDeleteLC::verify_permission(optional_yield y)
7c673cae
FG
5462{
5463 bool perm;
11fdf7f2 5464 perm = verify_bucket_permission(this, s, rgw::IAM::s3PutLifecycleConfiguration);
7c673cae
FG
5465 if (!perm)
5466 return -EACCES;
5467
5468 return 0;
5469}
5470
5471void RGWPutACLs::pre_exec()
5472{
5473 rgw_bucket_object_pre_exec(s);
5474}
5475
5476void RGWGetLC::pre_exec()
5477{
5478 rgw_bucket_object_pre_exec(s);
5479}
5480
5481void RGWPutLC::pre_exec()
5482{
5483 rgw_bucket_object_pre_exec(s);
5484}
5485
5486void RGWDeleteLC::pre_exec()
5487{
5488 rgw_bucket_object_pre_exec(s);
5489}
5490
f67539c2 5491void RGWPutACLs::execute(optional_yield y)
7c673cae
FG
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;
7c673cae
FG
5499
5500 op_ret = 0; /* XXX redundant? */
5501
5502 if (!parser.init()) {
5503 op_ret = -EINVAL;
5504 return;
5505 }
5506
5507
c07f9fc5 5508 RGWAccessControlPolicy* const existing_policy = \
f67539c2 5509 (rgw::sal::RGWObject::empty(s->object.get()) ? s->bucket_acl.get() : s->object_acl.get());
7c673cae
FG
5510
5511 owner = existing_policy->get_owner();
5512
f67539c2 5513 op_ret = get_params(y);
c07f9fc5
FG
5514 if (op_ret < 0) {
5515 if (op_ret == -ERANGE) {
11fdf7f2 5516 ldpp_dout(this, 4) << "The size of request xml data is larger than the max limitation, data size = "
c07f9fc5
FG
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 }
7c673cae 5523 return;
c07f9fc5 5524 }
7c673cae 5525
11fdf7f2
TL
5526 char* buf = data.c_str();
5527 ldpp_dout(this, 15) << "read len=" << data.length() << " data=" << (buf ? buf : "") << dendl;
7c673cae 5528
11fdf7f2 5529 if (!s->canned_acl.empty() && data.length() > 0) {
7c673cae
FG
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
11fdf7f2
TL
5539 data.clear();
5540 data.append(ss.str());
7c673cae
FG
5541 }
5542
11fdf7f2 5543 if (!parser.parse(data.c_str(), data.length(), 1)) {
7c673cae
FG
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
c07f9fc5
FG
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) {
11fdf7f2
TL
5563 ldpp_dout(this, 4) << "An acl can have up to " << max_num
5564 << " grants, request acl grants num: " << grants_num << dendl;
9f95a23c 5565 op_ret = -ERR_LIMIT_EXCEEDED;
c07f9fc5
FG
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
7c673cae 5572 // forward bucket acl requests to meta master zone
f67539c2 5573 if ((rgw::sal::RGWObject::empty(s->object.get()))) {
7c673cae
FG
5574 bufferlist in_data;
5575 // include acl data unless it was generated from a canned_acl
5576 if (s->canned_acl.empty()) {
11fdf7f2 5577 in_data.append(data);
7c673cae 5578 }
b3b6e05e 5579 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
7c673cae 5580 if (op_ret < 0) {
11fdf7f2 5581 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
7c673cae
FG
5582 return;
5583 }
5584 }
5585
11fdf7f2
TL
5586 if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
5587 ldpp_dout(this, 15) << "Old AccessControlPolicy";
7c673cae
FG
5588 policy->to_xml(*_dout);
5589 *_dout << dendl;
5590 }
5591
b3b6e05e 5592 op_ret = policy->rebuild(this, store->ctl()->user, &owner, new_policy, s->err.message);
7c673cae
FG
5593 if (op_ret < 0)
5594 return;
5595
11fdf7f2
TL
5596 if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) {
5597 ldpp_dout(this, 15) << "New AccessControlPolicy:";
7c673cae
FG
5598 new_policy.to_xml(*_dout);
5599 *_dout << dendl;
5600 }
5601
9f95a23c
TL
5602 if (s->bucket_access_conf &&
5603 s->bucket_access_conf->block_public_acls() &&
b3b6e05e 5604 new_policy.is_public(this)) {
9f95a23c
TL
5605 op_ret = -EACCES;
5606 return;
5607 }
7c673cae 5608 new_policy.encode(bl);
f67539c2
TL
5609 map<string, bufferlist> attrs;
5610
5611 if (!rgw::sal::RGWObject::empty(s->object.get())) {
5612 s->object->set_atomic(s->obj_ctx);
7c673cae 5613 //if instance is empty, we should modify the latest object
b3b6e05e 5614 op_ret = s->object->modify_obj_attrs(s->obj_ctx, RGW_ATTR_ACL, bl, s->yield, this);
7c673cae 5615 } else {
9f95a23c 5616 map<string,bufferlist> attrs = s->bucket_attrs;
7c673cae 5617 attrs[RGW_ATTR_ACL] = bl;
f67539c2
TL
5618 op_ret = store->ctl()->bucket->set_bucket_instance_attrs(s->bucket->get_info(), attrs,
5619 &s->bucket->get_info().objv_tracker,
b3b6e05e 5620 s->yield, this);
7c673cae
FG
5621 }
5622 if (op_ret == -ECANCELED) {
5623 op_ret = 0; /* lost a race, but it's ok because acls are immutable */
5624 }
5625}
5626
f67539c2 5627void RGWPutLC::execute(optional_yield y)
7c673cae
FG
5628{
5629 bufferlist bl;
5630
11fdf7f2
TL
5631 RGWLifecycleConfiguration_S3 config(s->cct);
5632 RGWXMLParser parser;
7c673cae
FG
5633 RGWLifecycleConfiguration_S3 new_config(s->cct);
5634
b32b8144
FG
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";
11fdf7f2 5639 ldpp_dout(this, 5) << s->err.message << dendl;
b32b8144
FG
5640 return;
5641 }
5642
5643 std::string content_md5_bin;
5644 try {
f67539c2 5645 content_md5_bin = rgw::from_base64(std::string_view(content_md5));
b32b8144
FG
5646 } catch (...) {
5647 s->err.message = "Request header Content-MD5 contains character "
5648 "that is not base64 encoded.";
11fdf7f2 5649 ldpp_dout(this, 5) << s->err.message << dendl;
b32b8144
FG
5650 op_ret = -ERR_BAD_DIGEST;
5651 return;
5652 }
5653
7c673cae
FG
5654 if (!parser.init()) {
5655 op_ret = -EINVAL;
5656 return;
5657 }
5658
f67539c2 5659 op_ret = get_params(y);
7c673cae
FG
5660 if (op_ret < 0)
5661 return;
5662
11fdf7f2
TL
5663 char* buf = data.c_str();
5664 ldpp_dout(this, 15) << "read len=" << data.length() << " data=" << (buf ? buf : "") << dendl;
7c673cae 5665
b32b8144
FG
5666 MD5 data_hash;
5667 unsigned char data_hash_res[CEPH_CRYPTO_MD5_DIGESTSIZE];
11fdf7f2 5668 data_hash.Update(reinterpret_cast<const unsigned char*>(buf), data.length());
b32b8144
FG
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.";
11fdf7f2 5674 ldpp_dout(this, 5) << s->err.message
b32b8144
FG
5675 << " Specified content md5: " << content_md5
5676 << ", calculated content md5: " << data_hash_res
5677 << dendl;
5678 return;
5679 }
5680
11fdf7f2 5681 if (!parser.parse(buf, data.length(), 1)) {
7c673cae
FG
5682 op_ret = -ERR_MALFORMED_XML;
5683 return;
5684 }
11fdf7f2
TL
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;
7c673cae
FG
5690 op_ret = -ERR_MALFORMED_XML;
5691 return;
5692 }
5693
f67539c2 5694 op_ret = config.rebuild(new_config);
7c673cae
FG
5695 if (op_ret < 0)
5696 return;
5697
11fdf7f2
TL
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;
7c673cae 5704 }
11fdf7f2 5705
b3b6e05e 5706 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
f67539c2
TL
5707 if (op_ret < 0) {
5708 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5709 return;
494da23a
TL
5710 }
5711
f67539c2 5712 op_ret = store->get_rgwlc()->set_bucket_config(s->bucket->get_info(), s->bucket_attrs, &new_config);
11fdf7f2 5713 if (op_ret < 0) {
7c673cae 5714 return;
11fdf7f2 5715 }
7c673cae
FG
5716 return;
5717}
5718
f67539c2 5719void RGWDeleteLC::execute(optional_yield y)
7c673cae 5720{
f67539c2 5721 bufferlist data;
b3b6e05e 5722 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
f67539c2
TL
5723 if (op_ret < 0) {
5724 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5725 return;
494da23a 5726 }
11fdf7f2 5727
f67539c2 5728 op_ret = store->get_rgwlc()->remove_bucket_config(s->bucket->get_info(), s->bucket_attrs);
11fdf7f2
TL
5729 if (op_ret < 0) {
5730 return;
5731 }
7c673cae
FG
5732 return;
5733}
5734
f67539c2 5735int RGWGetCORS::verify_permission(optional_yield y)
7c673cae 5736{
b32b8144 5737 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketCORS);
7c673cae
FG
5738}
5739
f67539c2 5740void RGWGetCORS::execute(optional_yield y)
7c673cae
FG
5741{
5742 op_ret = read_bucket_cors();
5743 if (op_ret < 0)
5744 return ;
5745
5746 if (!cors_exist) {
11fdf7f2
TL
5747 ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl;
5748 op_ret = -ERR_NO_CORS_FOUND;
7c673cae
FG
5749 return;
5750 }
5751}
5752
f67539c2 5753int RGWPutCORS::verify_permission(optional_yield y)
7c673cae 5754{
b32b8144 5755 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketCORS);
7c673cae
FG
5756}
5757
f67539c2 5758void RGWPutCORS::execute(optional_yield y)
7c673cae
FG
5759{
5760 rgw_raw_obj obj;
5761
f67539c2 5762 op_ret = get_params(y);
7c673cae
FG
5763 if (op_ret < 0)
5764 return;
5765
b3b6e05e 5766 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
5767 if (op_ret < 0) {
5768 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5769 return;
31f18b77
FG
5770 }
5771
b3b6e05e 5772 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
f67539c2 5773 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
b32b8144 5774 attrs[RGW_ATTR_CORS] = cors_bl;
b3b6e05e 5775 return s->bucket->set_instance_attrs(this, attrs, s->yield);
b32b8144 5776 });
7c673cae
FG
5777}
5778
f67539c2 5779int RGWDeleteCORS::verify_permission(optional_yield y)
7c673cae 5780{
b32b8144
FG
5781 // No separate delete permission
5782 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketCORS);
7c673cae
FG
5783}
5784
f67539c2 5785void RGWDeleteCORS::execute(optional_yield y)
7c673cae 5786{
f67539c2 5787 bufferlist data;
b3b6e05e 5788 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
f67539c2
TL
5789 if (op_ret < 0) {
5790 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5791 return;
494da23a
TL
5792 }
5793
b3b6e05e 5794 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
11fdf7f2 5795 op_ret = read_bucket_cors();
b32b8144
FG
5796 if (op_ret < 0)
5797 return op_ret;
5798
11fdf7f2
TL
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;
b32b8144 5803 }
11fdf7f2 5804
f67539c2 5805 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
11fdf7f2 5806 attrs.erase(RGW_ATTR_CORS);
b3b6e05e 5807 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
11fdf7f2 5808 if (op_ret < 0) {
f67539c2 5809 ldpp_dout(this, 0) << "RGWLC::RGWDeleteCORS() failed to set attrs on bucket=" << s->bucket->get_name()
11fdf7f2
TL
5810 << " returned err=" << op_ret << dendl;
5811 }
5812 return op_ret;
b32b8144 5813 });
7c673cae
FG
5814}
5815
5816void 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
5820int RGWOptionsCORS::validate_cors_request(RGWCORSConfiguration *cc) {
5821 rule = cc->host_name_rule(origin);
5822 if (!rule) {
11fdf7f2 5823 ldpp_dout(this, 10) << "There is no cors rule present for " << origin << dendl;
7c673cae
FG
5824 return -ENOENT;
5825 }
5826
5827 if (!validate_cors_rule_method(rule, req_meth)) {
5828 return -ENOENT;
5829 }
b32b8144
FG
5830
5831 if (!validate_cors_rule_header(rule, req_hdrs)) {
5832 return -ENOENT;
5833 }
5834
7c673cae
FG
5835 return 0;
5836}
5837
f67539c2 5838void RGWOptionsCORS::execute(optional_yield y)
7c673cae
FG
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) {
11fdf7f2 5846 ldpp_dout(this, 0) << "Missing mandatory Origin header" << dendl;
7c673cae
FG
5847 op_ret = -EINVAL;
5848 return;
5849 }
5850 req_meth = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_METHOD");
5851 if (!req_meth) {
11fdf7f2 5852 ldpp_dout(this, 0) << "Missing mandatory Access-control-request-method header" << dendl;
7c673cae
FG
5853 op_ret = -EINVAL;
5854 return;
5855 }
5856 if (!cors_exist) {
11fdf7f2 5857 ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl;
7c673cae
FG
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
f67539c2 5870int RGWGetRequestPayment::verify_permission(optional_yield y)
7c673cae 5871{
b32b8144 5872 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketRequestPayment);
7c673cae
FG
5873}
5874
5875void RGWGetRequestPayment::pre_exec()
5876{
5877 rgw_bucket_object_pre_exec(s);
5878}
5879
f67539c2 5880void RGWGetRequestPayment::execute(optional_yield y)
7c673cae 5881{
f67539c2 5882 requester_pays = s->bucket->get_info().requester_pays;
7c673cae
FG
5883}
5884
f67539c2 5885int RGWSetRequestPayment::verify_permission(optional_yield y)
7c673cae 5886{
b32b8144 5887 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketRequestPayment);
7c673cae
FG
5888}
5889
5890void RGWSetRequestPayment::pre_exec()
5891{
5892 rgw_bucket_object_pre_exec(s);
5893}
5894
f67539c2 5895void RGWSetRequestPayment::execute(optional_yield y)
7c673cae 5896{
494da23a 5897
b3b6e05e 5898 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, in_data, nullptr, s->info, y);
f67539c2
TL
5899 if (op_ret < 0) {
5900 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
5901 return;
494da23a
TL
5902 }
5903
f67539c2 5904 op_ret = get_params(y);
7c673cae
FG
5905
5906 if (op_ret < 0)
5907 return;
5908
f67539c2 5909 s->bucket->get_info().requester_pays = requester_pays;
b3b6e05e 5910 op_ret = s->bucket->put_instance_info(this, false, real_time());
7c673cae 5911 if (op_ret < 0) {
f67539c2 5912 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
7c673cae
FG
5913 << " returned err=" << op_ret << dendl;
5914 return;
5915 }
f67539c2 5916 s->bucket_attrs = s->bucket->get_attrs();
7c673cae
FG
5917}
5918
f67539c2 5919int RGWInitMultipart::verify_permission(optional_yield y)
7c673cae 5920{
522d829b
TL
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,
11fdf7f2
TL
5923 boost::none,
5924 rgw::IAM::s3PutObject,
f67539c2 5925 s->object->get_obj());
522d829b 5926 if (identity_policy_res == Effect::Deny) {
11fdf7f2
TL
5927 return -EACCES;
5928 }
5929
5930 rgw::IAM::Effect e = Effect::Pass;
522d829b 5931 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
11fdf7f2
TL
5932 if (s->iam_policy) {
5933 e = s->iam_policy->eval(s->env, *s->auth.identity,
31f18b77 5934 rgw::IAM::s3PutObject,
522d829b
TL
5935 s->object->get_obj(),
5936 princ_type);
11fdf7f2 5937 }
522d829b 5938 if (e == Effect::Deny) {
31f18b77 5939 return -EACCES;
522d829b
TL
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) {
11fdf7f2 5969 return 0;
31f18b77
FG
5970 }
5971 }
5972
11fdf7f2 5973 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae 5974 return -EACCES;
31f18b77 5975 }
7c673cae
FG
5976
5977 return 0;
5978}
5979
5980void RGWInitMultipart::pre_exec()
5981{
5982 rgw_bucket_object_pre_exec(s);
5983}
5984
f67539c2 5985void RGWInitMultipart::execute(optional_yield y)
7c673cae
FG
5986{
5987 bufferlist aclbl;
f67539c2 5988 rgw::sal::RGWAttrs attrs;
7c673cae 5989
f67539c2 5990 if (get_params(y) < 0)
7c673cae
FG
5991 return;
5992
f67539c2 5993 if (rgw::sal::RGWObject::empty(s->object.get()))
7c673cae
FG
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
b3b6e05e 6006 op_ret = rgw_get_request_metadata(this, s->cct, s->info, attrs);
3efd9988
FG
6007 if (op_ret < 0) {
6008 return;
6009 }
7c673cae 6010
f67539c2 6011 // make reservation for notification if needed
b3b6e05e 6012 rgw::notify::reservation_t res(this, store, s, s->object.get());
f67539c2 6013 const auto event_type = rgw::notify::ObjectCreatedPost;
b3b6e05e 6014 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
f67539c2
TL
6015 if (op_ret < 0) {
6016 return;
6017 }
6018
7c673cae
FG
6019 do {
6020 char buf[33];
f67539c2 6021 std::unique_ptr<rgw::sal::RGWObject> obj;
7c673cae
FG
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;
f67539c2 6027 RGWMPObj mp(s->object->get_name(), upload_id);
7c673cae
FG
6028 tmp_obj_name = mp.get_meta();
6029
f67539c2 6030 obj = s->bucket->get_object(rgw_obj_key(tmp_obj_name, string(), mp_ns));
7c673cae 6031 // the meta object will be indexed with 0 size, we c
f67539c2
TL
6032 obj->set_in_extra_data(true);
6033 obj->set_hash_source(s->object->get_name());
7c673cae 6034
f67539c2 6035 std::unique_ptr<rgw::sal::RGWObject::WriteOp> obj_op = obj->get_write_op(s->obj_ctx);
7c673cae 6036
f67539c2
TL
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;
7c673cae 6043
11fdf7f2
TL
6044 multipart_upload_info upload_info;
6045 upload_info.dest_placement = s->dest_placement;
7c673cae 6046
11fdf7f2
TL
6047 bufferlist bl;
6048 encode(upload_info, bl);
f67539c2 6049 obj_op->params.data = &bl;
7c673cae 6050
f67539c2
TL
6051 op_ret = obj_op->prepare(s->yield);
6052
b3b6e05e 6053 op_ret = obj_op->write_meta(this, bl.length(), 0, s->yield);
11fdf7f2 6054 } while (op_ret == -EEXIST);
eafe8130 6055
f67539c2 6056 // send request to notification manager
b3b6e05e 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);
eafe8130 6058 if (ret < 0) {
f67539c2
TL
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
eafe8130 6061 }
7c673cae
FG
6062}
6063
f67539c2 6064int RGWCompleteMultipart::verify_permission(optional_yield y)
7c673cae 6065{
522d829b
TL
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,
11fdf7f2
TL
6068 boost::none,
6069 rgw::IAM::s3PutObject,
f67539c2 6070 s->object->get_obj());
522d829b 6071 if (identity_policy_res == Effect::Deny) {
11fdf7f2
TL
6072 return -EACCES;
6073 }
6074
6075 rgw::IAM::Effect e = Effect::Pass;
522d829b 6076 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
11fdf7f2
TL
6077 if (s->iam_policy) {
6078 e = s->iam_policy->eval(s->env, *s->auth.identity,
31f18b77 6079 rgw::IAM::s3PutObject,
522d829b
TL
6080 s->object->get_obj(),
6081 princ_type);
11fdf7f2 6082 }
522d829b 6083 if (e == Effect::Deny) {
31f18b77 6084 return -EACCES;
522d829b
TL
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) {
11fdf7f2 6114 return 0;
31f18b77
FG
6115 }
6116 }
6117
11fdf7f2 6118 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae 6119 return -EACCES;
31f18b77 6120 }
7c673cae
FG
6121
6122 return 0;
6123}
6124
6125void RGWCompleteMultipart::pre_exec()
6126{
6127 rgw_bucket_object_pre_exec(s);
6128}
6129
f67539c2 6130void RGWCompleteMultipart::execute(optional_yield y)
7c673cae
FG
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;
f67539c2 6138 rgw::sal::RGWAttrs attrs;
7c673cae
FG
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;
f67539c2
TL
6144 std::unique_ptr<rgw::sal::RGWObject> meta_obj;
6145 std::unique_ptr<rgw::sal::RGWObject> target_obj;
7c673cae
FG
6146 RGWMPObj mp;
6147 RGWObjManifest manifest;
6148 uint64_t olh_epoch = 0;
7c673cae 6149
f67539c2 6150 op_ret = get_params(y);
7c673cae
FG
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
11fdf7f2 6158 if (!data.length()) {
7c673cae
FG
6159 op_ret = -ERR_MALFORMED_XML;
6160 return;
6161 }
6162
6163 if (!parser.init()) {
6164 op_ret = -EIO;
6165 return;
6166 }
6167
11fdf7f2 6168 if (!parser.parse(data.c_str(), data.length(), 1)) {
7c673cae
FG
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
f67539c2
TL
6185 mp.init(s->object->get_name(), upload_id);
6186
7c673cae
FG
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
f67539c2 6202 bool versioned_object = s->bucket->versioning_enabled();
7c673cae
FG
6203
6204 iter = parts->parts.begin();
6205
f67539c2
TL
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());
7c673cae 6209
d2e6a577
FG
6210 /*take a cls lock on meta_obj to prevent racing completions (or retries)
6211 from deleting the parts*/
3efd9988 6212 int max_lock_secs_mp =
11fdf7f2 6213 s->cct->_conf.get_val<int64_t>("rgw_mp_lock_max_time");
3efd9988 6214 utime_t dur(max_lock_secs_mp, 0);
d2e6a577 6215
b3b6e05e
TL
6216 serializer = meta_obj->get_serializer(this, "RGWCompleteMultipart");
6217 op_ret = serializer->try_lock(this, dur, y);
d2e6a577 6218 if (op_ret < 0) {
11fdf7f2 6219 ldpp_dout(this, 0) << "failed to acquire lock" << dendl;
d2e6a577
FG
6220 op_ret = -ERR_INTERNAL_ERROR;
6221 s->err.message = "This multipart completion is already in progress";
6222 return;
6223 }
6224
b3b6e05e 6225 op_ret = meta_obj->get_obj_attrs(s->obj_ctx, s->yield, this);
7c673cae 6226 if (op_ret < 0) {
11fdf7f2 6227 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << meta_obj
7c673cae
FG
6228 << " ret=" << op_ret << dendl;
6229 return;
6230 }
f67539c2 6231 attrs = meta_obj->get_attrs();
7c673cae 6232
522d829b
TL
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
7c673cae 6241 do {
b3b6e05e 6242 op_ret = list_multipart_parts(this, store, s, upload_id, meta_oid, max_parts,
7c673cae
FG
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()) {
11fdf7f2 6252 ldpp_dout(this, 0) << "NOTICE: total parts mismatch: have: " << total_parts
7c673cae
FG
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) {
11fdf7f2 6268 ldpp_dout(this, 0) << "NOTICE: parts num mismatch: next requested: "
7c673cae
FG
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) {
11fdf7f2 6276 ldpp_dout(this, 0) << "NOTICE: etag mismatch: part: " << iter->first
7c673cae
FG
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);
11fdf7f2 6284 hash.Update((const unsigned char *)petag, sizeof(petag));
7c673cae
FG
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;
f67539c2 6291 src_obj.init_ns(s->bucket->get_key(), oid, mp_ns);
7c673cae
FG
6292
6293 if (obj_part.manifest.empty()) {
11fdf7f2 6294 ldpp_dout(this, 0) << "ERROR: empty manifest for object part: obj="
7c673cae
FG
6295 << src_obj << dendl;
6296 op_ret = -ERR_INVALID_PART;
6297 return;
6298 } else {
b3b6e05e 6299 manifest.append(this, obj_part.manifest, store->svc()->zone);
7c673cae
FG
6300 }
6301
11fdf7f2 6302 bool part_compressed = (obj_part.cs_info.compression_type != "none");
9f95a23c 6303 if ((handled_parts > 0) &&
11fdf7f2
TL
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 ("
7c673cae
FG
6307 << cs_info.compression_type << ">>" << obj_part.cs_info.compression_type << ")" << dendl;
6308 op_ret = -ERR_INVALID_PART;
11fdf7f2
TL
6309 return;
6310 }
6311
6312 if (part_compressed) {
224ce89b 6313 int64_t new_ofs; // offset in compression data for new part
7c673cae
FG
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);
b3b6e05e 6341
11fdf7f2 6342 hash.Final((unsigned char *)final_etag);
7c673cae
FG
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;
11fdf7f2 6348 ldpp_dout(this, 10) << "calculated etag: " << final_etag_str << dendl;
7c673cae 6349
11fdf7f2 6350 etag_bl.append(final_etag_str, strlen(final_etag_str));
7c673cae
FG
6351
6352 attrs[RGW_ATTR_ETAG] = etag_bl;
6353
6354 if (compressed) {
6355 // write compression attribute to full object
6356 bufferlist tmp;
11fdf7f2 6357 encode(cs_info, tmp);
7c673cae
FG
6358 attrs[RGW_ATTR_COMPRESSION] = tmp;
6359 }
6360
f67539c2 6361 target_obj = s->bucket->get_object(rgw_obj_key(s->object->get_name()));
7c673cae 6362 if (versioned_object) {
11fdf7f2 6363 if (!version_id.empty()) {
f67539c2 6364 target_obj->set_instance(version_id);
11fdf7f2 6365 } else {
f67539c2
TL
6366 target_obj->gen_rand_obj_instance_name();
6367 version_id = target_obj->get_instance();
11fdf7f2 6368 }
7c673cae
FG
6369 }
6370
6371 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
6372
f67539c2 6373 target_obj->set_atomic(&obj_ctx);
7c673cae 6374
f67539c2 6375 std::unique_ptr<rgw::sal::RGWObject::WriteOp> obj_op = target_obj->get_write_op(&obj_ctx);
7c673cae 6376
f67539c2
TL
6377 obj_op->params.manifest = &manifest;
6378 obj_op->params.remove_objs = &remove_objs;
7c673cae 6379
f67539c2
TL
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;
b3b6e05e 6387
f67539c2 6388 op_ret = obj_op->prepare(s->yield);
b3b6e05e 6389 if (op_ret < 0) {
f67539c2 6390 return;
b3b6e05e 6391 }
f67539c2 6392
b3b6e05e
TL
6393 op_ret = obj_op->write_meta(this, ofs, accounted_size, s->yield);
6394 if (op_ret < 0) {
7c673cae 6395 return;
b3b6e05e 6396 }
7c673cae 6397
b3b6e05e 6398 // remove the upload meta object
f67539c2 6399 string version_id;
b3b6e05e
TL
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*/ );
3efd9988
FG
6405 if (r >= 0) {
6406 /* serializer's exclusive lock is released */
f67539c2 6407 serializer->clear_locked();
3efd9988 6408 } else {
11fdf7f2 6409 ldpp_dout(this, 0) << "WARNING: failed to remove object " << meta_obj << dendl;
3efd9988 6410 }
e306af50 6411
f67539c2 6412 // send request to notification manager
b3b6e05e 6413 const auto ret = rgw::notify::publish_commit(s->object.get(), ofs, ceph::real_clock::now(), final_etag_str, event_type, res, this);
eafe8130 6414 if (ret < 0) {
f67539c2
TL
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
eafe8130 6417 }
b3b6e05e 6418} // RGWCompleteMultipart::execute
3efd9988 6419
3efd9988
FG
6420void RGWCompleteMultipart::complete()
6421{
6422 /* release exclusive lock iff not already */
f67539c2
TL
6423 if (unlikely(serializer && serializer->locked)) {
6424 int r = serializer->unlock();
d2e6a577 6425 if (r < 0) {
f67539c2 6426 ldpp_dout(this, 0) << "WARNING: failed to unlock " << serializer->oid << dendl;
d2e6a577 6427 }
7c673cae 6428 }
3efd9988 6429 send_response();
7c673cae
FG
6430}
6431
f67539c2 6432int RGWAbortMultipart::verify_permission(optional_yield y)
7c673cae 6433{
522d829b
TL
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,
11fdf7f2
TL
6436 boost::none,
6437 rgw::IAM::s3AbortMultipartUpload,
f67539c2 6438 s->object->get_obj());
522d829b 6439 if (identity_policy_res == Effect::Deny) {
11fdf7f2
TL
6440 return -EACCES;
6441 }
6442
6443 rgw::IAM::Effect e = Effect::Pass;
522d829b 6444 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
11fdf7f2
TL
6445 if (s->iam_policy) {
6446 e = s->iam_policy->eval(s->env, *s->auth.identity,
31f18b77 6447 rgw::IAM::s3AbortMultipartUpload,
522d829b 6448 s->object->get_obj(), princ_type);
11fdf7f2 6449 }
522d829b
TL
6450
6451 if (e == Effect::Deny) {
31f18b77 6452 return -EACCES;
522d829b
TL
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) {
11fdf7f2 6482 return 0;
522d829b 6483 }
31f18b77
FG
6484 }
6485
11fdf7f2 6486 if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae 6487 return -EACCES;
31f18b77 6488 }
7c673cae
FG
6489
6490 return 0;
6491}
6492
6493void RGWAbortMultipart::pre_exec()
6494{
6495 rgw_bucket_object_pre_exec(s);
6496}
6497
f67539c2 6498void RGWAbortMultipart::execute(optional_yield y)
7c673cae
FG
6499{
6500 op_ret = -EINVAL;
6501 string upload_id;
6502 string meta_oid;
6503 upload_id = s->info.args.get("uploadId");
7c673cae
FG
6504 rgw_obj meta_obj;
6505 RGWMPObj mp;
6506
f67539c2 6507 if (upload_id.empty() || rgw::sal::RGWObject::empty(s->object.get()))
7c673cae
FG
6508 return;
6509
f67539c2 6510 mp.init(s->object->get_name(), upload_id);
7c673cae
FG
6511 meta_oid = mp.get_meta();
6512
b3b6e05e 6513 op_ret = get_multipart_info(this, s, meta_oid, nullptr);
7c673cae
FG
6514 if (op_ret < 0)
6515 return;
6516
6517 RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx);
b3b6e05e 6518 op_ret = abort_multipart_upload(this, store, s->cct, obj_ctx, s->bucket->get_info(), mp);
7c673cae
FG
6519}
6520
f67539c2 6521int RGWListMultipart::verify_permission(optional_yield y)
7c673cae 6522{
11fdf7f2 6523 if (!verify_object_permission(this, s, rgw::IAM::s3ListMultipartUploadParts))
7c673cae
FG
6524 return -EACCES;
6525
6526 return 0;
6527}
6528
6529void RGWListMultipart::pre_exec()
6530{
6531 rgw_bucket_object_pre_exec(s);
6532}
6533
f67539c2 6534void RGWListMultipart::execute(optional_yield y)
7c673cae 6535{
7c673cae
FG
6536 string meta_oid;
6537 RGWMPObj mp;
6538
f67539c2 6539 op_ret = get_params(y);
7c673cae
FG
6540 if (op_ret < 0)
6541 return;
6542
f67539c2 6543 mp.init(s->object->get_name(), upload_id);
7c673cae
FG
6544 meta_oid = mp.get_meta();
6545
b3b6e05e 6546 op_ret = get_multipart_info(this, s, meta_oid, nullptr);
7c673cae
FG
6547 if (op_ret < 0)
6548 return;
6549
b3b6e05e 6550 op_ret = list_multipart_parts(this, store, s, upload_id, meta_oid, max_parts,
7c673cae
FG
6551 marker, parts, NULL, &truncated);
6552}
6553
f67539c2 6554int RGWListBucketMultiparts::verify_permission(optional_yield y)
7c673cae 6555{
11fdf7f2
TL
6556 if (!verify_bucket_permission(this,
6557 s,
28e407b8 6558 rgw::IAM::s3ListBucketMultipartUploads))
7c673cae
FG
6559 return -EACCES;
6560
6561 return 0;
6562}
6563
6564void RGWListBucketMultiparts::pre_exec()
6565{
6566 rgw_bucket_object_pre_exec(s);
6567}
6568
f67539c2 6569void RGWListBucketMultiparts::execute(optional_yield y)
7c673cae
FG
6570{
6571 vector<rgw_bucket_dir_entry> objs;
6572 string marker_meta;
6573
f67539c2 6574 op_ret = get_params(y);
7c673cae
FG
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
b3b6e05e 6592 op_ret = list_bucket_multiparts(this, store, s->bucket->get_info(), prefix, marker_meta, delimiter,
224ce89b
WB
6593 max_uploads, &objs, &common_prefixes, &is_truncated);
6594 if (op_ret < 0) {
6595 return;
6596 }
7c673cae 6597
7c673cae
FG
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
f67539c2 6612void RGWGetHealthCheck::execute(optional_yield y)
7c673cae 6613{
11fdf7f2
TL
6614 if (!g_conf()->rgw_healthcheck_disabling_path.empty() &&
6615 (::access(g_conf()->rgw_healthcheck_disabling_path.c_str(), F_OK) == 0)) {
7c673cae
FG
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
f67539c2 6623int RGWDeleteMultiObj::verify_permission(optional_yield y)
7c673cae 6624{
f67539c2
TL
6625 int op_ret = get_params(y);
6626 if (op_ret) {
6627 return op_ret;
6628 }
6629
522d829b 6630 if (s->iam_policy || ! s->iam_user_policies.empty() || ! s->session_policies.empty()) {
f67539c2 6631 if (s->bucket->get_info().obj_lock_enabled() && bypass_governance_mode) {
522d829b 6632 auto r = eval_identity_or_session_policies(s->iam_user_policies, s->env, boost::none,
f67539c2
TL
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 }
522d829b
TL
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 }
f67539c2
TL
6648 }
6649 }
6650
6651 bool not_versioned = rgw::sal::RGWObject::empty(s->object.get()) || s->object->get_instance().empty();
6652
522d829b 6653 auto identity_policy_res = eval_identity_or_session_policies(s->iam_user_policies, s->env,
11fdf7f2 6654 boost::none,
f67539c2 6655 not_versioned ?
11fdf7f2
TL
6656 rgw::IAM::s3DeleteObject :
6657 rgw::IAM::s3DeleteObjectVersion,
f67539c2 6658 ARN(s->bucket->get_key()));
522d829b 6659 if (identity_policy_res == Effect::Deny) {
11fdf7f2
TL
6660 return -EACCES;
6661 }
6662
6663 rgw::IAM::Effect r = Effect::Pass;
522d829b 6664 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
11fdf7f2
TL
6665 if (s->iam_policy) {
6666 r = s->iam_policy->eval(s->env, *s->auth.identity,
f67539c2 6667 not_versioned ?
11fdf7f2
TL
6668 rgw::IAM::s3DeleteObject :
6669 rgw::IAM::s3DeleteObjectVersion,
522d829b
TL
6670 ARN(s->bucket->get_key()),
6671 princ_type);
11fdf7f2 6672 }
522d829b
TL
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 }
11fdf7f2 6702 return -EACCES;
522d829b
TL
6703 }
6704 if (r == Effect::Allow || identity_policy_res == Effect::Allow)
11fdf7f2
TL
6705 return 0;
6706 }
6707
6708 acl_allowed = verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE);
6709 if (!acl_allowed)
7c673cae
FG
6710 return -EACCES;
6711
6712 return 0;
6713}
6714
6715void RGWDeleteMultiObj::pre_exec()
6716{
6717 rgw_bucket_object_pre_exec(s);
6718}
6719
f67539c2 6720void RGWDeleteMultiObj::execute(optional_yield y)
7c673cae
FG
6721{
6722 RGWMultiDelDelete *multi_delete;
6723 vector<rgw_obj_key>::iterator iter;
6724 RGWMultiDelXMLParser parser;
7c673cae 6725 RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx);
11fdf7f2 6726 char* buf;
7c673cae 6727
11fdf7f2
TL
6728 buf = data.c_str();
6729 if (!buf) {
7c673cae
FG
6730 op_ret = -EINVAL;
6731 goto error;
6732 }
6733
6734 if (!parser.init()) {
6735 op_ret = -EINVAL;
6736 goto error;
6737 }
6738
11fdf7f2 6739 if (!parser.parse(buf, data.length(), 1)) {
7c673cae
FG
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;
11fdf7f2
TL
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 }
7c673cae
FG
6759 }
6760
6761 if (multi_delete->is_quiet())
6762 quiet = true;
6763
f67539c2 6764 if (s->bucket->get_info().mfa_enabled()) {
11fdf7f2
TL
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
7c673cae
FG
6779 begin_response();
6780 if (multi_delete->objects.empty()) {
6781 goto done;
6782 }
6783
6784 for (iter = multi_delete->objects.begin();
11fdf7f2
TL
6785 iter != multi_delete->objects.end();
6786 ++iter) {
f67539c2 6787 std::string version_id;
522d829b
TL
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,
11fdf7f2
TL
6791 boost::none,
6792 iter->instance.empty() ?
6793 rgw::IAM::s3DeleteObject :
6794 rgw::IAM::s3DeleteObjectVersion,
f67539c2 6795 ARN(obj->get_obj()));
522d829b 6796 if (identity_policy_res == Effect::Deny) {
11fdf7f2
TL
6797 send_partial_response(*iter, false, "", -EACCES);
6798 continue;
6799 }
6800
6801 rgw::IAM::Effect e = Effect::Pass;
522d829b 6802 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
11fdf7f2
TL
6803 if (s->iam_policy) {
6804 e = s->iam_policy->eval(s->env,
31f18b77
FG
6805 *s->auth.identity,
6806 iter->instance.empty() ?
6807 rgw::IAM::s3DeleteObject :
6808 rgw::IAM::s3DeleteObjectVersion,
522d829b
TL
6809 ARN(obj->get_obj()),
6810 princ_type);
6811 }
6812 if (e == Effect::Deny) {
6813 send_partial_response(*iter, false, "", -EACCES);
6814 continue;
11fdf7f2 6815 }
522d829b
TL
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)) {
f67539c2
TL
6852 send_partial_response(*iter, false, "", -EACCES);
6853 continue;
31f18b77
FG
6854 }
6855 }
7c673cae 6856
a4b75251
TL
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) {
f67539c2
TL
6867 // object maybe delete_marker, skip check_obj_lock
6868 check_obj_lock = false;
6869 } else {
6870 // Something went wrong.
a4b75251 6871 send_partial_response(*iter, false, "", ret);
f67539c2
TL
6872 continue;
6873 }
a4b75251
TL
6874 } else {
6875 obj_size = astate->size;
6876 etag = astate->attrset[RGW_ATTR_ETAG].to_str();
f67539c2 6877 }
7c673cae 6878
a4b75251
TL
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 }
f67539c2
TL
6886 }
6887 }
a4b75251 6888
f67539c2
TL
6889 // make reservation for notification if needed
6890 const auto versioned_object = s->bucket->versioning_enabled();
b3b6e05e 6891 rgw::notify::reservation_t res(this, store, s, obj.get());
f67539c2
TL
6892 const auto event_type = versioned_object && obj->get_instance().empty() ?
6893 rgw::notify::ObjectRemovedDeleteMarkerCreated : rgw::notify::ObjectRemovedDelete;
b3b6e05e 6894 op_ret = rgw::notify::publish_reserve(this, event_type, res, nullptr);
f67539c2
TL
6895 if (op_ret < 0) {
6896 send_partial_response(*iter, false, "", op_ret);
6897 continue;
6898 }
7c673cae 6899
f67539c2 6900 obj->set_atomic(obj_ctx);
7c673cae 6901
b3b6e05e 6902 op_ret = obj->delete_object(this, obj_ctx, s->owner, s->bucket_owner, ceph::real_time(),
f67539c2 6903 false, 0, version_id, s->yield);
7c673cae
FG
6904 if (op_ret == -ENOENT) {
6905 op_ret = 0;
6906 }
6907
f67539c2 6908 send_partial_response(*iter, obj->get_delete_marker(), version_id, op_ret);
9f95a23c 6909
f67539c2 6910 // send request to notification manager
a4b75251 6911 const auto ret = rgw::notify::publish_commit(obj.get(), obj_size, ceph::real_clock::now(), etag, event_type, res, this);
9f95a23c 6912 if (ret < 0) {
f67539c2
TL
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
9f95a23c 6915 }
7c673cae
FG
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
6922done:
6923 // will likely segfault if begin_response() has not been called
6924 end_response();
7c673cae
FG
6925 return;
6926
6927error:
6928 send_status();
7c673cae
FG
6929 return;
6930
6931}
6932
6933bool RGWBulkDelete::Deleter::verify_permission(RGWBucketInfo& binfo,
6934 map<string, bufferlist>& battrs,
f67539c2
TL
6935 ACLOwner& bucket_owner /* out */,
6936 optional_yield y)
7c673cae
FG
6937{
6938 RGWAccessControlPolicy bacl(store->ctx());
b3b6e05e 6939 int ret = read_bucket_policy(dpp, store, s, binfo, battrs, &bacl, binfo.bucket, y);
7c673cae
FG
6940 if (ret < 0) {
6941 return false;
6942 }
6943
f67539c2 6944 auto policy = get_iam_policy_from_attr(s->cct, battrs, binfo.bucket.tenant);
31f18b77 6945
7c673cae
FG
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. */
11fdf7f2 6950 return verify_bucket_permission(dpp, s, binfo.bucket, s->user_acl.get(),
522d829b 6951 &bacl, policy, s->iam_user_policies, s->session_policies, rgw::IAM::s3DeleteBucket);
7c673cae
FG
6952}
6953
f67539c2 6954bool RGWBulkDelete::Deleter::delete_single(const acct_path_t& path, optional_yield y)
7c673cae 6955{
f67539c2 6956 std::unique_ptr<rgw::sal::RGWBucket> bucket;
7c673cae 6957 ACLOwner bowner;
9f95a23c
TL
6958 RGWObjVersionTracker ot;
6959
b3b6e05e 6960 int ret = store->get_bucket(dpp, s->user.get(), s->user->get_tenant(), path.bucket_name, &bucket, y);
f67539c2
TL
6961 if (ret < 0) {
6962 goto binfo_fail;
6963 }
7c673cae 6964
b3b6e05e 6965 ret = bucket->get_bucket_info(dpp, s->yield);
7c673cae
FG
6966 if (ret < 0) {
6967 goto binfo_fail;
6968 }
6969
f67539c2 6970 if (!verify_permission(bucket->get_info(), bucket->get_attrs(), bowner, y)) {
7c673cae
FG
6971 ret = -EACCES;
6972 goto auth_fail;
6973 }
6974
6975 if (!path.obj_key.empty()) {
f67539c2
TL
6976 ACLOwner bucket_owner;
6977 std::string version_id;
7c673cae 6978
f67539c2
TL
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);
7c673cae 6982
b3b6e05e
TL
6983 ret = obj->delete_object(dpp, s->obj_ctx, bowner, bucket_owner, ceph::real_time(), false, 0,
6984 version_id, s->yield);
7c673cae
FG
6985 if (ret < 0) {
6986 goto delop_fail;
6987 }
6988 } else {
a4b75251 6989 ret = bucket->remove_bucket(dpp, false, true, &s->info, s->yield);
7c673cae
FG
6990 if (ret < 0) {
6991 goto delop_fail;
6992 }
7c673cae
FG
6993 }
6994
6995 num_deleted++;
6996 return true;
6997
6998
6999binfo_fail:
7000 if (-ENOENT == ret) {
b3b6e05e 7001 ldpp_dout(dpp, 20) << "cannot find bucket = " << path.bucket_name << dendl;
7c673cae
FG
7002 num_unfound++;
7003 } else {
b3b6e05e 7004 ldpp_dout(dpp, 20) << "cannot get bucket info, ret = " << ret << dendl;
7c673cae
FG
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
7014auth_fail:
b3b6e05e 7015 ldpp_dout(dpp, 20) << "wrong auth for " << path << dendl;
7c673cae
FG
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
7025delop_fail:
7026 if (-ENOENT == ret) {
b3b6e05e 7027 ldpp_dout(dpp, 20) << "cannot find entry " << path << dendl;
7c673cae
FG
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
f67539c2 7039bool RGWBulkDelete::Deleter::delete_chunk(const std::list<acct_path_t>& paths, optional_yield y)
7c673cae 7040{
b3b6e05e 7041 ldpp_dout(dpp, 20) << "in delete_chunk" << dendl;
7c673cae 7042 for (auto path : paths) {
b3b6e05e 7043 ldpp_dout(dpp, 20) << "bulk deleting path: " << path << dendl;
f67539c2 7044 delete_single(path, y);
7c673cae
FG
7045 }
7046
7047 return true;
7048}
7049
f67539c2 7050int RGWBulkDelete::verify_permission(optional_yield y)
7c673cae
FG
7051{
7052 return 0;
7053}
7054
7055void RGWBulkDelete::pre_exec()
7056{
7057 rgw_bucket_object_pre_exec(s);
7058}
7059
f67539c2 7060void RGWBulkDelete::execute(optional_yield y)
7c673cae 7061{
11fdf7f2 7062 deleter = std::unique_ptr<Deleter>(new Deleter(this, store, s));
7c673cae
FG
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
f67539c2 7073 ret = deleter->delete_chunk(items, y);
7c673cae
FG
7074 } while (!op_ret && is_truncated);
7075
7076 return;
7077}
7078
7079
7080constexpr std::array<int, 2> RGWBulkUploadOp::terminal_errors;
7081
f67539c2 7082int RGWBulkUploadOp::verify_permission(optional_yield y)
7c673cae
FG
7083{
7084 if (s->auth.identity->is_anonymous()) {
7085 return -EACCES;
7086 }
7087
11fdf7f2 7088 if (! verify_user_permission_no_policy(this, s, RGW_PERM_WRITE)) {
7c673cae
FG
7089 return -EACCES;
7090 }
7091
9f95a23c 7092 if (s->user->get_tenant() != s->bucket_tenant) {
11fdf7f2 7093 ldpp_dout(this, 10) << "user cannot create a bucket in a different tenant"
9f95a23c 7094 << " (user_id.tenant=" << s->user->get_tenant()
11fdf7f2 7095 << " requested=" << s->bucket_tenant << ")" << dendl;
7c673cae
FG
7096 return -EACCES;
7097 }
7098
9f95a23c 7099 if (s->user->get_max_buckets() < 0) {
7c673cae
FG
7100 return -EPERM;
7101 }
7102
7103 return 0;
7104}
7105
7106void RGWBulkUploadOp::pre_exec()
7107{
7108 rgw_bucket_object_pre_exec(s);
7109}
7110
7111boost::optional<std::pair<std::string, rgw_obj_key>>
f67539c2 7112RGWBulkUploadOp::parse_path(const std::string_view& path)
7c673cae
FG
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
f67539c2 7118 if (std::string_view::npos != start_pos) {
7c673cae
FG
7119 /* Seperator is the first slash after the leading ones. */
7120 const size_t sep_pos = path.substr(start_pos).find('/');
7121
f67539c2 7122 if (std::string_view::npos != sep_pos) {
7c673cae
FG
7123 const auto bucket_name = path.substr(start_pos, sep_pos - start_pos);
7124 const auto obj_name = path.substr(sep_pos + 1);
7125
f67539c2
TL
7126 return std::make_pair(std::string(bucket_name),
7127 rgw_obj_key(std::string(obj_name)));
7c673cae
FG
7128 } else {
7129 /* It's guaranteed here that bucket name is at least one character
7130 * long and is different than slash. */
f67539c2 7131 return std::make_pair(std::string(path.substr(start_pos)),
7c673cae
FG
7132 rgw_obj_key());
7133 }
7134 }
7135
31f18b77 7136 return none;
7c673cae
FG
7137}
7138
7139std::pair<std::string, std::string>
7140RGWBulkUploadOp::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 + "/";
f67539c2
TL
7145 if (!rgw::sal::RGWObject::empty(s->object.get())) {
7146 const std::string& object_name = s->object->get_name();
7c673cae 7147
f67539c2 7148 /* As rgw_obj_key::empty() already verified emptiness of s->object->get_name(),
7c673cae
FG
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
f67539c2 7160int RGWBulkUploadOp::handle_dir_verify_permission(optional_yield y)
7c673cae 7161{
9f95a23c
TL
7162 if (s->user->get_max_buckets() > 0) {
7163 rgw::sal::RGWBucketList buckets;
7c673cae 7164 std::string marker;
b3b6e05e 7165 op_ret = rgw_read_user_buckets(this, store, s->user->get_user(), buckets,
9f95a23c 7166 marker, std::string(), s->user->get_max_buckets(),
f67539c2 7167 false, y);
7c673cae
FG
7168 if (op_ret < 0) {
7169 return op_ret;
7170 }
7171
9f95a23c 7172 if (buckets.count() >= static_cast<size_t>(s->user->get_max_buckets())) {
7c673cae
FG
7173 return -ERR_TOO_MANY_BUCKETS;
7174 }
7175 }
7176
7177 return 0;
7178}
7179
7180static 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
9f95a23c 7194void RGWBulkUploadOp::init(rgw::sal::RGWRadosStore* const store,
11fdf7f2
TL
7195 struct req_state* const s,
7196 RGWHandler* const h)
7197{
7198 RGWOp::init(store, s, h);
9f95a23c 7199 dir_ctx.emplace(store->svc()->sysobj->init_obj_ctx());
11fdf7f2
TL
7200}
7201
f67539c2 7202int RGWBulkUploadOp::handle_dir(const std::string_view path, optional_yield y)
7c673cae 7203{
11fdf7f2 7204 ldpp_dout(this, 20) << "got directory=" << path << dendl;
7c673cae 7205
f67539c2 7206 op_ret = handle_dir_verify_permission(y);
7c673cae
FG
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
9f95a23c 7215 rgw_raw_obj obj(store->svc()->zone->get_zone_params().domain_root,
7c673cae
FG
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 */
f67539c2 7220 std::unique_ptr<rgw::sal::RGWBucket> bucket;
7c673cae
FG
7221
7222 /* Create metadata: ACLs. */
7223 std::map<std::string, ceph::bufferlist> attrs;
7224 RGWAccessControlPolicy policy;
9f95a23c 7225 policy.create_default(s->user->get_id(), s->user->get_display_name());
7c673cae
FG
7226 ceph::bufferlist aclbl;
7227 policy.encode(aclbl);
7228 attrs.emplace(RGW_ATTR_ACL, std::move(aclbl));
7229
f67539c2
TL
7230 obj_version objv, ep_objv;
7231 bool bucket_exists;
7c673cae 7232 RGWQuotaInfo quota_info;
f67539c2 7233 const RGWQuotaInfo* pquota_info = nullptr;
7c673cae 7234 RGWBucketInfo out_info;
f67539c2
TL
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
b3b6e05e 7244 op_ret = store->create_bucket(this, *s->user, new_bucket,
f67539c2
TL
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);
7c673cae
FG
7251 /* continue if EEXIST and create_bucket will fail below. this way we can
7252 * recover from a partial create by retrying it. */
11fdf7f2
TL
7253 ldpp_dout(this, 20) << "rgw_create_bucket returned ret=" << op_ret
7254 << ", bucket=" << bucket << dendl;
7c673cae
FG
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 */
9f95a23c 7268 if (out_info.owner.compare(s->user->get_id()) != 0) {
7c673cae 7269 op_ret = -EEXIST;
11fdf7f2 7270 ldpp_dout(this, 20) << "conflicting bucket name" << dendl;
7c673cae
FG
7271 return op_ret;
7272 }
f67539c2 7273 new_bucket = out_info.bucket;
7c673cae
FG
7274 }
7275
f67539c2 7276 op_ret = store->ctl()->bucket->link_bucket(s->user->get_id(), new_bucket,
9f95a23c 7277 out_info.creation_time,
b3b6e05e 7278 s->yield, s, false);
7c673cae
FG
7279 if (op_ret && !existed && op_ret != -EEXIST) {
7280 /* if it exists (or previously existed), don't remove it! */
b3b6e05e 7281 op_ret = store->ctl()->bucket->unlink_bucket(s->user->get_id(), new_bucket, s->yield, this);
7c673cae 7282 if (op_ret < 0) {
11fdf7f2 7283 ldpp_dout(this, 0) << "WARNING: failed to unlink bucket: ret=" << op_ret << dendl;
7c673cae
FG
7284 }
7285 } else if (op_ret == -EEXIST || (op_ret == 0 && existed)) {
11fdf7f2 7286 ldpp_dout(this, 20) << "containers already exists" << dendl;
7c673cae
FG
7287 op_ret = -ERR_BUCKET_EXISTS;
7288 }
7289
7290 return op_ret;
7291}
7292
7293
7294bool RGWBulkUploadOp::handle_file_verify_permission(RGWBucketInfo& binfo,
31f18b77 7295 const rgw_obj& obj,
7c673cae 7296 std::map<std::string, ceph::bufferlist>& battrs,
f67539c2
TL
7297 ACLOwner& bucket_owner /* out */,
7298 optional_yield y)
7c673cae
FG
7299{
7300 RGWAccessControlPolicy bacl(store->ctx());
b3b6e05e 7301 op_ret = read_bucket_policy(this, store, s, binfo, battrs, &bacl, binfo.bucket, y);
7c673cae 7302 if (op_ret < 0) {
11fdf7f2 7303 ldpp_dout(this, 20) << "cannot read_policy() for bucket" << dendl;
7c673cae
FG
7304 return false;
7305 }
7306
f67539c2 7307 auto policy = get_iam_policy_from_attr(s->cct, battrs, binfo.bucket.tenant);
31f18b77 7308
7c673cae 7309 bucket_owner = bacl.get_owner();
522d829b
TL
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,
11fdf7f2
TL
7312 boost::none,
7313 rgw::IAM::s3PutObject, obj);
522d829b 7314 if (identity_policy_res == Effect::Deny) {
11fdf7f2
TL
7315 return false;
7316 }
522d829b
TL
7317
7318 rgw::IAM::PolicyPrincipal princ_type = rgw::IAM::PolicyPrincipal::Other;
31f18b77 7319 auto e = policy->eval(s->env, *s->auth.identity,
522d829b
TL
7320 rgw::IAM::s3PutObject, obj, princ_type);
7321 if (e == Effect::Deny) {
31f18b77 7322 return false;
522d829b
TL
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) {
11fdf7f2 7351 return true;
31f18b77
FG
7352 }
7353 }
7354
11fdf7f2 7355 return verify_bucket_permission_no_policy(this, s, s->user_acl.get(),
31f18b77 7356 &bacl, RGW_PERM_WRITE);
7c673cae
FG
7357}
7358
f67539c2 7359int RGWBulkUploadOp::handle_file(const std::string_view path,
7c673cae 7360 const size_t size,
f67539c2 7361 AlignedStreamGetter& body, optional_yield y)
7c673cae
FG
7362{
7363
11fdf7f2 7364 ldpp_dout(this, 20) << "got file=" << path << ", size=" << size << dendl;
7c673cae 7365
11fdf7f2 7366 if (size > static_cast<size_t>(s->cct->_conf->rgw_max_put_size)) {
7c673cae
FG
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);
f67539c2 7376 std::unique_ptr<rgw::sal::RGWBucket> bucket;
7c673cae 7377 ACLOwner bowner;
f67539c2 7378
b3b6e05e 7379 op_ret = store->get_bucket(this, s->user.get(), rgw_bucket(rgw_bucket_key(s->user->get_tenant(), bucket_name)), &bucket, y);
7c673cae 7380 if (op_ret == -ENOENT) {
11fdf7f2 7381 ldpp_dout(this, 20) << "non existent directory=" << bucket_name << dendl;
7c673cae
FG
7382 } else if (op_ret < 0) {
7383 return op_ret;
7384 }
7385
f67539c2
TL
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)) {
11fdf7f2 7391 ldpp_dout(this, 20) << "object creation unauthorized" << dendl;
7c673cae
FG
7392 op_ret = -EACCES;
7393 return op_ret;
7394 }
7395
f67539c2 7396 op_ret = bucket->check_quota(user_quota, bucket_quota, size, y);
7c673cae
FG
7397 if (op_ret < 0) {
7398 return op_ret;
7399 }
7400
f67539c2
TL
7401 if (bucket->versioning_enabled()) {
7402 obj->gen_rand_obj_instance_name();
11fdf7f2 7403 }
7c673cae 7404
11fdf7f2 7405 rgw_placement_rule dest_placement = s->dest_placement;
f67539c2 7406 dest_placement.inherit_from(bucket->get_placement_rule());
7c673cae 7407
9f95a23c
TL
7408 auto aio = rgw::make_throttle(s->cct->_conf->rgw_put_obj_min_window_size,
7409 s->yield);
11fdf7f2
TL
7410
7411 using namespace rgw::putobj;
f67539c2
TL
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);
11fdf7f2 7414
9f95a23c 7415 op_ret = processor.prepare(s->yield);
7c673cae 7416 if (op_ret < 0) {
11fdf7f2 7417 ldpp_dout(this, 20) << "cannot prepare processor due to ret=" << op_ret << dendl;
7c673cae
FG
7418 return op_ret;
7419 }
7420
11fdf7f2
TL
7421 /* No filters by default. */
7422 DataProcessor *filter = &processor;
7423
9f95a23c 7424 const auto& compression_type = store->svc()->zone->get_zone_params().get_compression_type(
11fdf7f2 7425 dest_placement);
7c673cae 7426 CompressorRef plugin;
11fdf7f2 7427 boost::optional<RGWPutObj_Compress> compressor;
7c673cae
FG
7428 if (compression_type != "none") {
7429 plugin = Compressor::create(s->cct, compression_type);
7430 if (! plugin) {
11fdf7f2
TL
7431 ldpp_dout(this, 1) << "Cannot load plugin for rgw_compression_type "
7432 << compression_type << dendl;
7c673cae
FG
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
11fdf7f2 7447 ldpp_dout(this, 20) << "body=" << data.c_str() << dendl;
7c673cae
FG
7448 if (len < 0) {
7449 op_ret = len;
7450 return op_ret;
7451 } else if (len > 0) {
11fdf7f2
TL
7452 hash.Update((const unsigned char *)data.c_str(), data.length());
7453 op_ret = filter->process(std::move(data), ofs);
7c673cae 7454 if (op_ret < 0) {
11fdf7f2 7455 ldpp_dout(this, 20) << "filter->process() returned ret=" << op_ret << dendl;
7c673cae
FG
7456 return op_ret;
7457 }
7458
7459 ofs += len;
7460 }
7461
7462 } while (len > 0);
7463
11fdf7f2
TL
7464 // flush
7465 op_ret = filter->process({}, ofs);
7466 if (op_ret < 0) {
7467 return op_ret;
7468 }
7469
7c673cae 7470 if (ofs != size) {
11fdf7f2 7471 ldpp_dout(this, 10) << "real file size different from declared" << dendl;
7c673cae 7472 op_ret = -EINVAL;
11fdf7f2 7473 return op_ret;
7c673cae
FG
7474 }
7475
f67539c2 7476 op_ret = bucket->check_quota(user_quota, bucket_quota, size, y);
7c673cae 7477 if (op_ret < 0) {
11fdf7f2 7478 ldpp_dout(this, 20) << "quota exceeded for path=" << path << dendl;
7c673cae
FG
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;
9f95a23c 7496 policy.create_default(s->user->get_id(), s->user->get_display_name());
7c673cae
FG
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;
f67539c2 7507 cs_info.compressor_message = compressor->get_compressor_message();
7c673cae 7508 cs_info.blocks = std::move(compressor->get_compression_blocks());
11fdf7f2 7509 encode(cs_info, tmp);
7c673cae
FG
7510 attrs.emplace(RGW_ATTR_COMPRESSION, std::move(tmp));
7511 }
7512
7513 /* Complete the transaction. */
11fdf7f2
TL
7514 op_ret = processor.complete(size, etag, nullptr, ceph::real_time(),
7515 attrs, ceph::real_time() /* delete_at */,
9f95a23c
TL
7516 nullptr, nullptr, nullptr, nullptr, nullptr,
7517 s->yield);
7c673cae 7518 if (op_ret < 0) {
11fdf7f2 7519 ldpp_dout(this, 20) << "processor::complete returned op_ret=" << op_ret << dendl;
7c673cae
FG
7520 }
7521
7522 return op_ret;
7523}
7524
f67539c2 7525void RGWBulkUploadOp::execute(optional_yield y)
7c673cae
FG
7526{
7527 ceph::bufferlist buffer(64 * 1024);
7528
11fdf7f2 7529 ldpp_dout(this, 20) << "start" << dendl;
7c673cae
FG
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) {
11fdf7f2 7547 ldpp_dout(this, 2) << "cannot read header" << dendl;
7c673cae
FG
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: {
11fdf7f2 7565 ldpp_dout(this, 2) << "handling regular file" << dendl;
7c673cae 7566
f67539c2
TL
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(),
7c673cae
FG
7573 rgw::tar::BLOCK_SIZE, *stream);
7574 op_ret = handle_file(filename,
7575 header->get_filesize(),
f67539c2 7576 body, y);
7c673cae
FG
7577 if (! op_ret) {
7578 /* Only regular files counts. */
7579 num_created++;
7580 } else {
f67539c2 7581 failures.emplace_back(op_ret, std::string(filename));
7c673cae
FG
7582 }
7583 break;
7584 }
7585 case rgw::tar::FileType::DIRECTORY: {
11fdf7f2 7586 ldpp_dout(this, 2) << "handling regular directory" << dendl;
7c673cae 7587
f67539c2
TL
7588 std::string_view dirname = bucket_path.empty() ? header->get_filename() : bucket_path;
7589 op_ret = handle_dir(dirname, y);
7c673cae 7590 if (op_ret < 0 && op_ret != -ERR_BUCKET_EXISTS) {
f67539c2 7591 failures.emplace_back(op_ret, std::string(dirname));
7c673cae
FG
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)) {
11fdf7f2 7606 ldpp_dout(this, 2) << "terminating due to ret=" << op_ret << dendl;
7c673cae
FG
7607 break;
7608 }
7609 } else {
11fdf7f2 7610 ldpp_dout(this, 2) << "an empty block" << dendl;
7c673cae
FG
7611 op_ret = 0;
7612 }
7613
7614 buffer.clear();
7615 } while (! status.eof());
7616
7617 return;
7618}
7619
7620RGWBulkUploadOp::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
7628ssize_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
7639ssize_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
f67539c2
TL
7649int 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
7664void RGWGetAttrs::pre_exec()
7665{
7666 rgw_bucket_object_pre_exec(s);
7667}
7668
7669void 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
b3b6e05e 7677 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, this);
f67539c2
TL
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
7704int RGWRMAttrs::verify_permission(optional_yield y)
7c673cae 7705{
31f18b77
FG
7706 // This looks to be part of the RGW-NFS machinery and has no S3 or
7707 // Swift equivalent.
7c673cae 7708 bool perm;
f67539c2 7709 if (!rgw::sal::RGWObject::empty(s->object.get())) {
11fdf7f2 7710 perm = verify_object_permission_no_policy(this, s, RGW_PERM_WRITE);
7c673cae 7711 } else {
11fdf7f2 7712 perm = verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE);
7c673cae
FG
7713 }
7714 if (!perm)
7715 return -EACCES;
7716
7717 return 0;
7718}
7719
f67539c2 7720void RGWRMAttrs::pre_exec()
7c673cae
FG
7721{
7722 rgw_bucket_object_pre_exec(s);
7723}
7724
f67539c2 7725void RGWRMAttrs::execute(optional_yield y)
7c673cae
FG
7726{
7727 op_ret = get_params();
7728 if (op_ret < 0)
7729 return;
7730
f67539c2
TL
7731 s->object->set_atomic(s->obj_ctx);
7732
b3b6e05e 7733 op_ret = s->object->set_obj_attrs(this, s->obj_ctx, nullptr, &attrs, y);
f67539c2
TL
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
7741int 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
7757void RGWSetAttrs::pre_exec()
7758{
7759 rgw_bucket_object_pre_exec(s);
7760}
7761
7762void RGWSetAttrs::execute(optional_yield y)
7763{
7764 op_ret = get_params(y);
7765 if (op_ret < 0)
7766 return;
7c673cae 7767
f67539c2
TL
7768 if (!rgw::sal::RGWObject::empty(s->object.get())) {
7769 rgw::sal::RGWAttrs a(attrs);
b3b6e05e 7770 op_ret = s->object->set_obj_attrs(this, s->obj_ctx, &a, nullptr, y);
7c673cae 7771 } else {
f67539c2
TL
7772 op_ret = store->ctl()->bucket->set_bucket_instance_attrs(
7773 s->bucket->get_info(), attrs, &s->bucket->get_info().objv_tracker,
b3b6e05e 7774 s->yield, this);
7c673cae 7775 }
f67539c2
TL
7776
7777} /* RGWSetAttrs::execute() */
7c673cae
FG
7778
7779void RGWGetObjLayout::pre_exec()
7780{
7781 rgw_bucket_object_pre_exec(s);
7782}
7783
f67539c2 7784void RGWGetObjLayout::execute(optional_yield y)
7c673cae 7785{
f67539c2
TL
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));
7c673cae 7790
f67539c2 7791
b3b6e05e 7792 op_ret = stat_op->prepare(y, this);
7c673cae
FG
7793 if (op_ret < 0) {
7794 return;
7795 }
7796
f67539c2 7797 head_obj = stat_op->result.head_obj;
7c673cae 7798
b3b6e05e 7799 op_ret = stat_op->get_manifest(this, &manifest, y);
7c673cae
FG
7800}
7801
7802
f67539c2 7803int RGWConfigBucketMetaSearch::verify_permission(optional_yield y)
31f18b77
FG
7804{
7805 if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) {
7806 return -EACCES;
7807 }
7808
7809 return 0;
7810}
7811
7812void RGWConfigBucketMetaSearch::pre_exec()
7813{
7814 rgw_bucket_object_pre_exec(s);
7815}
7816
f67539c2 7817void RGWConfigBucketMetaSearch::execute(optional_yield y)
31f18b77 7818{
f67539c2 7819 op_ret = get_params(y);
31f18b77 7820 if (op_ret < 0) {
11fdf7f2 7821 ldpp_dout(this, 20) << "NOTICE: get_params() returned ret=" << op_ret << dendl;
31f18b77
FG
7822 return;
7823 }
7824
f67539c2 7825 s->bucket->get_info().mdsearch_config = mdsearch_config;
31f18b77 7826
b3b6e05e 7827 op_ret = s->bucket->put_instance_info(this, false, real_time());
31f18b77 7828 if (op_ret < 0) {
f67539c2 7829 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
11fdf7f2 7830 << " returned err=" << op_ret << dendl;
31f18b77
FG
7831 return;
7832 }
f67539c2 7833 s->bucket_attrs = s->bucket->get_attrs();
31f18b77
FG
7834}
7835
f67539c2 7836int RGWGetBucketMetaSearch::verify_permission(optional_yield y)
31f18b77
FG
7837{
7838 if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) {
7839 return -EACCES;
7840 }
7841
7842 return 0;
7843}
7844
7845void RGWGetBucketMetaSearch::pre_exec()
7846{
7847 rgw_bucket_object_pre_exec(s);
7848}
7849
f67539c2 7850int RGWDelBucketMetaSearch::verify_permission(optional_yield y)
31f18b77
FG
7851{
7852 if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) {
7853 return -EACCES;
7854 }
7855
7856 return 0;
7857}
7858
7859void RGWDelBucketMetaSearch::pre_exec()
7860{
7861 rgw_bucket_object_pre_exec(s);
7862}
7863
f67539c2 7864void RGWDelBucketMetaSearch::execute(optional_yield y)
31f18b77 7865{
f67539c2 7866 s->bucket->get_info().mdsearch_config.clear();
31f18b77 7867
b3b6e05e 7868 op_ret = s->bucket->put_instance_info(this, false, real_time());
31f18b77 7869 if (op_ret < 0) {
f67539c2 7870 ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket->get_name()
11fdf7f2 7871 << " returned err=" << op_ret << dendl;
31f18b77
FG
7872 return;
7873 }
f67539c2 7874 s->bucket_attrs = s->bucket->get_attrs();
31f18b77
FG
7875}
7876
7877
7c673cae
FG
7878RGWHandler::~RGWHandler()
7879{
7880}
7881
9f95a23c 7882int RGWHandler::init(rgw::sal::RGWRadosStore *_store,
7c673cae
FG
7883 struct req_state *_s,
7884 rgw::io::BasicClient *cio)
7885{
7886 store = _store;
7887 s = _s;
7888
7889 return 0;
7890}
7891
b3b6e05e 7892int RGWHandler::do_init_permissions(const DoutPrefixProvider *dpp, optional_yield y)
7c673cae 7893{
b3b6e05e 7894 int ret = rgw_build_bucket_policies(dpp, store, s, y);
7c673cae 7895 if (ret < 0) {
b3b6e05e 7896 ldpp_dout(dpp, 10) << "init_permissions on " << s->bucket
11fdf7f2
TL
7897 << " failed, ret=" << ret << dendl;
7898 return ret==-ENODATA ? -EACCES : ret;
7c673cae
FG
7899 }
7900
11fdf7f2 7901 rgw_build_iam_environment(store, s);
7c673cae
FG
7902 return ret;
7903}
7904
f67539c2 7905int RGWHandler::do_read_permissions(RGWOp *op, bool only_bucket, optional_yield y)
7c673cae
FG
7906{
7907 if (only_bucket) {
7908 /* already read bucket info */
7909 return 0;
7910 }
b3b6e05e 7911 int ret = rgw_build_object_policies(op, store, s, op->prefetch_data(), y);
7c673cae
FG
7912
7913 if (ret < 0) {
11fdf7f2 7914 ldpp_dout(op, 10) << "read_permissions on " << s->bucket << ":"
7c673cae
FG
7915 << s->object << " only_bucket=" << only_bucket
7916 << " ret=" << ret << dendl;
7917 if (ret == -ENODATA)
7918 ret = -EACCES;
e306af50
TL
7919 if (s->auth.identity->is_anonymous() && ret == -EACCES)
7920 ret = -EPERM;
7c673cae
FG
7921 }
7922
7923 return ret;
7924}
7925
f67539c2
TL
7926int RGWOp::error_handler(int err_no, string *error_content, optional_yield y) {
7927 return dialect_handler->error_handler(err_no, error_content, y);
7c673cae
FG
7928}
7929
f67539c2 7930int RGWHandler::error_handler(int err_no, string *error_content, optional_yield) {
7c673cae
FG
7931 // This is the do-nothing error handler
7932 return err_no;
7933}
31f18b77 7934
11fdf7f2
TL
7935std::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
7941void 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}
31f18b77
FG
7948
7949void RGWPutBucketPolicy::send_response()
7950{
adb31ebb
TL
7951 if (!op_ret) {
7952 /* A successful Put Bucket Policy should return a 204 on success */
7953 op_ret = STATUS_NO_CONTENT;
7954 }
31f18b77
FG
7955 if (op_ret) {
7956 set_req_state_err(s, op_ret);
7957 }
7958 dump_errno(s);
7959 end_header(s);
7960}
7961
f67539c2 7962int RGWPutBucketPolicy::verify_permission(optional_yield y)
31f18b77 7963{
11fdf7f2 7964 if (!verify_bucket_permission(this, s, rgw::IAM::s3PutBucketPolicy)) {
31f18b77
FG
7965 return -EACCES;
7966 }
7967
7968 return 0;
7969}
7970
f67539c2 7971int RGWPutBucketPolicy::get_params(optional_yield y)
31f18b77
FG
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.
11fdf7f2
TL
7976 std::tie(op_ret, data) = rgw_rest_read_all_input(s, max_size, false);
7977
31f18b77
FG
7978 // And throws exceptions.
7979 return op_ret;
7980}
7981
f67539c2 7982void RGWPutBucketPolicy::execute(optional_yield y)
31f18b77 7983{
f67539c2 7984 op_ret = get_params(y);
31f18b77
FG
7985 if (op_ret < 0) {
7986 return;
7987 }
7988
b3b6e05e 7989 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
f67539c2
TL
7990 if (op_ret < 0) {
7991 ldpp_dout(this, 20) << "forward_request_to_master returned ret=" << op_ret << dendl;
7992 return;
31f18b77
FG
7993 }
7994
7995 try {
11fdf7f2 7996 const Policy p(s->cct, s->bucket_tenant, data);
f67539c2 7997 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
9f95a23c
TL
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
b3b6e05e 8005 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [&p, this, &attrs] {
b32b8144
FG
8006 attrs[RGW_ATTR_IAM_POLICY].clear();
8007 attrs[RGW_ATTR_IAM_POLICY].append(p.text);
b3b6e05e 8008 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
b32b8144
FG
8009 return op_ret;
8010 });
31f18b77 8011 } catch (rgw::IAM::PolicyParseException& e) {
11fdf7f2 8012 ldpp_dout(this, 20) << "failed to parse policy: " << e.what() << dendl;
31f18b77
FG
8013 op_ret = -EINVAL;
8014 }
8015}
8016
8017void 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
f67539c2 8027int RGWGetBucketPolicy::verify_permission(optional_yield y)
31f18b77 8028{
11fdf7f2 8029 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketPolicy)) {
31f18b77
FG
8030 return -EACCES;
8031 }
8032
8033 return 0;
8034}
8035
f67539c2 8036void RGWGetBucketPolicy::execute(optional_yield y)
31f18b77 8037{
f67539c2
TL
8038 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
8039 auto aiter = attrs.find(RGW_ATTR_IAM_POLICY);
31f18b77 8040 if (aiter == attrs.end()) {
11fdf7f2
TL
8041 ldpp_dout(this, 0) << "can't find bucket IAM POLICY attr bucket_name = "
8042 << s->bucket_name << dendl;
31f18b77
FG
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) {
11fdf7f2
TL
8050 ldpp_dout(this, 10) << "The bucket policy does not exist, bucket: "
8051 << s->bucket_name << dendl;
31f18b77
FG
8052 op_ret = -ERR_NO_SUCH_BUCKET_POLICY;
8053 s->err.message = "The bucket policy does not exist";
8054 return;
8055 }
8056 }
8057}
8058
8059void 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
f67539c2 8068int RGWDeleteBucketPolicy::verify_permission(optional_yield y)
31f18b77 8069{
11fdf7f2 8070 if (!verify_bucket_permission(this, s, rgw::IAM::s3DeleteBucketPolicy)) {
31f18b77
FG
8071 return -EACCES;
8072 }
8073
8074 return 0;
8075}
8076
f67539c2 8077void RGWDeleteBucketPolicy::execute(optional_yield y)
31f18b77 8078{
b3b6e05e 8079 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
f67539c2 8080 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
b32b8144 8081 attrs.erase(RGW_ATTR_IAM_POLICY);
b3b6e05e 8082 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
b32b8144
FG
8083 return op_ret;
8084 });
31f18b77 8085}
28e407b8 8086
eafe8130
TL
8087void RGWPutBucketObjectLock::pre_exec()
8088{
8089 rgw_bucket_object_pre_exec(s);
8090}
8091
f67539c2 8092int RGWPutBucketObjectLock::verify_permission(optional_yield y)
eafe8130
TL
8093{
8094 return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketObjectLockConfiguration);
8095}
8096
f67539c2 8097void RGWPutBucketObjectLock::execute(optional_yield y)
eafe8130 8098{
f67539c2 8099 if (!s->bucket->get_info().obj_lock_enabled()) {
522d829b
TL
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;
eafe8130
TL
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 }
f67539c2 8112 op_ret = get_params(y);
eafe8130
TL
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) {
b3b6e05e 8124 ldpp_dout(this, 5) << "unexpected xml:" << err << dendl;
eafe8130
TL
8125 op_ret = -ERR_MALFORMED_XML;
8126 return;
8127 }
8128 if (obj_lock.has_rule() && !obj_lock.retention_period_valid()) {
522d829b
TL
8129 s->err.message = "retention period must be a positive integer value";
8130 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
eafe8130
TL
8131 op_ret = -ERR_INVALID_RETENTION_PERIOD;
8132 return;
8133 }
8134
b3b6e05e 8135 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
f67539c2 8136 if (op_ret < 0) {
b3b6e05e 8137 ldpp_dout(this, 20) << __func__ << "forward_request_to_master returned ret=" << op_ret << dendl;
f67539c2 8138 return;
eafe8130
TL
8139 }
8140
b3b6e05e 8141 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
f67539c2 8142 s->bucket->get_info().obj_lock = obj_lock;
b3b6e05e 8143 op_ret = s->bucket->put_instance_info(this, false, real_time());
eafe8130
TL
8144 return op_ret;
8145 });
8146 return;
8147}
8148
8149void RGWGetBucketObjectLock::pre_exec()
8150{
8151 rgw_bucket_object_pre_exec(s);
8152}
8153
f67539c2 8154int RGWGetBucketObjectLock::verify_permission(optional_yield y)
eafe8130
TL
8155{
8156 return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketObjectLockConfiguration);
8157}
8158
f67539c2 8159void RGWGetBucketObjectLock::execute(optional_yield y)
eafe8130 8160{
f67539c2 8161 if (!s->bucket->get_info().obj_lock_enabled()) {
eafe8130
TL
8162 op_ret = -ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION;
8163 return;
8164 }
8165}
8166
f67539c2 8167int RGWPutObjRetention::verify_permission(optional_yield y)
eafe8130
TL
8168{
8169 if (!verify_object_permission(this, s, rgw::IAM::s3PutObjectRetention)) {
8170 return -EACCES;
8171 }
f67539c2 8172 op_ret = get_params(y);
eafe8130
TL
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
8182void RGWPutObjRetention::pre_exec()
8183{
8184 rgw_bucket_object_pre_exec(s);
8185}
8186
f67539c2 8187void RGWPutObjRetention::execute(optional_yield y)
eafe8130 8188{
f67539c2 8189 if (!s->bucket->get_info().obj_lock_enabled()) {
522d829b
TL
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;
eafe8130
TL
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()) {
522d829b
TL
8217 s->err.message = "the retain-until date must be in the future";
8218 ldpp_dout(this, 0) << "ERROR: " << s->err.message << dendl;
eafe8130
TL
8219 op_ret = -EINVAL;
8220 return;
8221 }
8222 bufferlist bl;
8223 obj_retention.encode(bl);
eafe8130
TL
8224
8225 //check old retention
b3b6e05e 8226 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, this);
eafe8130
TL
8227 if (op_ret < 0) {
8228 ldpp_dout(this, 0) << "ERROR: get obj attr error"<< dendl;
8229 return;
8230 }
f67539c2 8231 rgw::sal::RGWAttrs attrs = s->object->get_attrs();
eafe8130
TL
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) {
522d829b 8244 s->err.message = "proposed retain-until date shortens an existing retention period and governance bypass check failed";
eafe8130
TL
8245 op_ret = -EACCES;
8246 return;
8247 }
8248 }
8249 }
8250
b3b6e05e 8251 op_ret = s->object->modify_obj_attrs(s->obj_ctx, RGW_ATTR_OBJECT_RETENTION, bl, s->yield, this);
eafe8130
TL
8252
8253 return;
8254}
8255
f67539c2 8256int RGWGetObjRetention::verify_permission(optional_yield y)
eafe8130
TL
8257{
8258 if (!verify_object_permission(this, s, rgw::IAM::s3GetObjectRetention)) {
8259 return -EACCES;
8260 }
8261 return 0;
8262}
8263
8264void RGWGetObjRetention::pre_exec()
8265{
8266 rgw_bucket_object_pre_exec(s);
8267}
8268
f67539c2 8269void RGWGetObjRetention::execute(optional_yield y)
eafe8130 8270{
f67539c2 8271 if (!s->bucket->get_info().obj_lock_enabled()) {
522d829b
TL
8272 s->err.message = "bucket object lock not configured";
8273 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
eafe8130
TL
8274 op_ret = -ERR_INVALID_REQUEST;
8275 return;
8276 }
b3b6e05e 8277 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, this);
eafe8130 8278 if (op_ret < 0) {
f67539c2 8279 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << s->object
eafe8130
TL
8280 << " ret=" << op_ret << dendl;
8281 return;
8282 }
f67539c2 8283 rgw::sal::RGWAttrs attrs = s->object->get_attrs();
eafe8130
TL
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) {
b3b6e05e 8294 ldpp_dout(this, 0) << __func__ << "decode object retention config failed" << dendl;
eafe8130
TL
8295 op_ret = -EIO;
8296 return;
8297 }
8298 return;
8299}
8300
f67539c2 8301int RGWPutObjLegalHold::verify_permission(optional_yield y)
eafe8130
TL
8302{
8303 if (!verify_object_permission(this, s, rgw::IAM::s3PutObjectLegalHold)) {
8304 return -EACCES;
8305 }
8306 return 0;
8307}
8308
8309void RGWPutObjLegalHold::pre_exec()
8310{
8311 rgw_bucket_object_pre_exec(s);
8312}
8313
f67539c2
TL
8314void RGWPutObjLegalHold::execute(optional_yield y) {
8315 if (!s->bucket->get_info().obj_lock_enabled()) {
522d829b
TL
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;
eafe8130
TL
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
f67539c2 8329 op_ret = get_params(y);
eafe8130
TL
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) {
b3b6e05e 8341 ldpp_dout(this, 5) << "unexpected xml:" << err << dendl;
eafe8130
TL
8342 op_ret = -ERR_MALFORMED_XML;
8343 return;
8344 }
8345 bufferlist bl;
8346 obj_legal_hold.encode(bl);
eafe8130 8347 //if instance is empty, we should modify the latest object
b3b6e05e 8348 op_ret = s->object->modify_obj_attrs(s->obj_ctx, RGW_ATTR_OBJECT_LEGAL_HOLD, bl, s->yield, this);
eafe8130
TL
8349 return;
8350}
8351
f67539c2 8352int RGWGetObjLegalHold::verify_permission(optional_yield y)
eafe8130
TL
8353{
8354 if (!verify_object_permission(this, s, rgw::IAM::s3GetObjectLegalHold)) {
8355 return -EACCES;
8356 }
8357 return 0;
8358}
8359
8360void RGWGetObjLegalHold::pre_exec()
8361{
8362 rgw_bucket_object_pre_exec(s);
8363}
8364
f67539c2 8365void RGWGetObjLegalHold::execute(optional_yield y)
eafe8130 8366{
f67539c2 8367 if (!s->bucket->get_info().obj_lock_enabled()) {
522d829b
TL
8368 s->err.message = "bucket object lock not configured";
8369 ldpp_dout(this, 4) << "ERROR: " << s->err.message << dendl;
eafe8130
TL
8370 op_ret = -ERR_INVALID_REQUEST;
8371 return;
8372 }
eafe8130 8373 map<string, bufferlist> attrs;
b3b6e05e 8374 op_ret = s->object->get_obj_attrs(s->obj_ctx, s->yield, this);
eafe8130 8375 if (op_ret < 0) {
f67539c2 8376 ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << s->object
eafe8130
TL
8377 << " ret=" << op_ret << dendl;
8378 return;
8379 }
f67539c2
TL
8380 auto aiter = s->object->get_attrs().find(RGW_ATTR_OBJECT_LEGAL_HOLD);
8381 if (aiter == s->object->get_attrs().end()) {
eafe8130
TL
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) {
b3b6e05e 8390 ldpp_dout(this, 0) << __func__ << "decode object legal hold config failed" << dendl;
eafe8130
TL
8391 op_ret = -EIO;
8392 return;
8393 }
8394 return;
8395}
8396
f67539c2 8397void RGWGetClusterStat::execute(optional_yield y)
28e407b8 8398{
f67539c2 8399 op_ret = store->cluster_stat(stats_op);
28e407b8
AA
8400}
8401
f67539c2 8402int RGWGetBucketPolicyStatus::verify_permission(optional_yield y)
9f95a23c
TL
8403{
8404 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketPolicyStatus)) {
8405 return -EACCES;
8406 }
8407
8408 return 0;
8409}
8410
f67539c2 8411void RGWGetBucketPolicyStatus::execute(optional_yield y)
9f95a23c 8412{
b3b6e05e 8413 isPublic = (s->iam_policy && rgw::IAM::is_public(*s->iam_policy)) || s->bucket_acl->is_public(this);
9f95a23c
TL
8414}
8415
f67539c2 8416int RGWPutBucketPublicAccessBlock::verify_permission(optional_yield y)
9f95a23c
TL
8417{
8418 if (!verify_bucket_permission(this, s, rgw::IAM::s3PutBucketPublicAccessBlock)) {
8419 return -EACCES;
8420 }
8421
8422 return 0;
8423}
8424
f67539c2 8425int RGWPutBucketPublicAccessBlock::get_params(optional_yield y)
9f95a23c
TL
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
f67539c2 8432void RGWPutBucketPublicAccessBlock::execute(optional_yield y)
9f95a23c
TL
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
f67539c2 8441 op_ret = get_params(y);
9f95a23c
TL
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
b3b6e05e 8459 op_ret = store->forward_request_to_master(this, s->user.get(), nullptr, data, nullptr, s->info, y);
f67539c2
TL
8460 if (op_ret < 0) {
8461 ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl;
8462 return;
9f95a23c
TL
8463 }
8464
8465 bufferlist bl;
8466 access_conf.encode(bl);
b3b6e05e 8467 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this, &bl] {
f67539c2 8468 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
9f95a23c 8469 attrs[RGW_ATTR_PUBLIC_ACCESS] = bl;
b3b6e05e 8470 return s->bucket->set_instance_attrs(this, attrs, s->yield);
9f95a23c
TL
8471 });
8472
8473}
8474
f67539c2 8475int RGWGetBucketPublicAccessBlock::verify_permission(optional_yield y)
9f95a23c
TL
8476{
8477 if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketPolicy)) {
8478 return -EACCES;
8479 }
8480
8481 return 0;
8482}
8483
f67539c2 8484void RGWGetBucketPublicAccessBlock::execute(optional_yield y)
9f95a23c
TL
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
8506void 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
f67539c2 8515int RGWDeleteBucketPublicAccessBlock::verify_permission(optional_yield y)
9f95a23c
TL
8516{
8517 if (!verify_bucket_permission(this, s, rgw::IAM::s3PutBucketPublicAccessBlock)) {
8518 return -EACCES;
8519 }
8520
8521 return 0;
8522}
8523
f67539c2 8524void RGWDeleteBucketPublicAccessBlock::execute(optional_yield y)
9f95a23c 8525{
b3b6e05e 8526 op_ret = retry_raced_bucket_write(this, s->bucket.get(), [this] {
f67539c2 8527 rgw::sal::RGWAttrs attrs(s->bucket_attrs);
9f95a23c 8528 attrs.erase(RGW_ATTR_PUBLIC_ACCESS);
b3b6e05e 8529 op_ret = s->bucket->set_instance_attrs(this, attrs, s->yield);
9f95a23c
TL
8530 return op_ret;
8531 });
8532}