forked from ceph/ceph
/
rgw_op.cc
4889 lines (4033 loc) · 129 KB
/
rgw_op.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <errno.h>
#include <stdlib.h>
#include <sstream>
#include "common/Clock.h"
#include "common/armor.h"
#include "common/mime.h"
#include "common/utf8.h"
#include "common/ceph_json.h"
#include "rgw_rados.h"
#include "rgw_op.h"
#include "rgw_rest.h"
#include "rgw_acl.h"
#include "rgw_acl_s3.h"
#include "rgw_acl_swift.h"
#include "rgw_user.h"
#include "rgw_bucket.h"
#include "rgw_log.h"
#include "rgw_multi.h"
#include "rgw_multi_del.h"
#include "rgw_cors.h"
#include "rgw_cors_s3.h"
#include "rgw_rest_conn.h"
#include "rgw_rest_s3.h"
#include "rgw_client_io.h"
#include "include/assert.h"
#define dout_subsys ceph_subsys_rgw
using namespace std;
using ceph::crypto::MD5;
static string mp_ns = RGW_OBJ_NS_MULTIPART;
static string shadow_ns = RGW_OBJ_NS_SHADOW;
static int forward_request_to_master(struct req_state *s, obj_version *objv, RGWRados *store, bufferlist& in_data, JSONParser *jp);
#define MULTIPART_UPLOAD_ID_PREFIX_LEGACY "2/"
#define MULTIPART_UPLOAD_ID_PREFIX "2~" // must contain a unique char that may not come up in gen_rand_alpha()
class MultipartMetaFilter : public RGWAccessListFilter {
public:
MultipartMetaFilter() {}
bool filter(string& name, string& key) {
int len = name.size();
if (len < 6)
return false;
size_t pos = name.find(MP_META_SUFFIX, len - 5);
if (pos == string::npos)
return false;
pos = name.rfind('.', pos - 1);
if (pos == string::npos)
return false;
key = name.substr(0, pos);
return true;
}
};
static MultipartMetaFilter mp_filter;
static int parse_range(const char *range, off_t& ofs, off_t& end, bool *partial_content)
{
int r = -ERANGE;
string s(range);
string ofs_str;
string end_str;
*partial_content = false;
size_t pos = s.find("bytes=");
if (pos == string::npos) {
pos = 0;
while (isspace(s[pos]))
pos++;
int end = pos;
while (isalpha(s[end]))
end++;
if (strncasecmp(s.c_str(), "bytes", end - pos) != 0)
return 0;
while (isspace(s[end]))
end++;
if (s[end] != '=')
return 0;
s = s.substr(end + 1);
} else {
s = s.substr(pos + 6); /* size of("bytes=") */
}
pos = s.find('-');
if (pos == string::npos)
goto done;
*partial_content = true;
ofs_str = s.substr(0, pos);
end_str = s.substr(pos + 1);
if (end_str.length()) {
end = atoll(end_str.c_str());
if (end < 0)
goto done;
}
if (ofs_str.length()) {
ofs = atoll(ofs_str.c_str());
} else { // RFC2616 suffix-byte-range-spec
ofs = -end;
end = -1;
}
if (end >= 0 && end < ofs)
goto done;
r = 0;
done:
return r;
}
static int decode_policy(CephContext *cct,
bufferlist& bl,
RGWAccessControlPolicy *policy)
{
bufferlist::iterator iter = bl.begin();
try {
policy->decode(iter);
} catch (buffer::error& err) {
ldout(cct, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl;
return -EIO;
}
if (cct->_conf->subsys.should_gather(ceph_subsys_rgw, 15)) {
RGWAccessControlPolicy_S3 *s3policy = static_cast<RGWAccessControlPolicy_S3 *>(policy);
ldout(cct, 15) << __func__ << " Read AccessControlPolicy";
s3policy->to_xml(*_dout);
*_dout << dendl;
}
return 0;
}
static int get_user_policy_from_attr(CephContext * const cct,
RGWRados * const store,
map<string, bufferlist>& attrs,
RGWAccessControlPolicy& policy /* out */)
{
auto aiter = attrs.find(RGW_ATTR_ACL);
if (aiter != attrs.end()) {
int ret = decode_policy(cct, aiter->second, &policy);
if (ret < 0) {
return ret;
}
} else {
return -ENOENT;
}
return 0;
}
static int get_bucket_policy_from_attr(CephContext *cct,
RGWRados *store,
void *ctx,
RGWBucketInfo& bucket_info,
map<string, bufferlist>& bucket_attrs,
RGWAccessControlPolicy *policy,
rgw_obj& obj)
{
map<string, bufferlist>::iterator aiter = bucket_attrs.find(RGW_ATTR_ACL);
if (aiter != bucket_attrs.end()) {
int ret = decode_policy(cct, aiter->second, policy);
if (ret < 0)
return ret;
} else {
ldout(cct, 0) << "WARNING: couldn't find acl header for bucket, generating default" << dendl;
RGWUserInfo uinfo;
/* object exists, but policy is broken */
int r = rgw_get_user_info_by_uid(store, bucket_info.owner, uinfo);
if (r < 0)
return r;
policy->create_default(bucket_info.owner, uinfo.display_name);
}
return 0;
}
static int get_obj_policy_from_attr(CephContext *cct,
RGWRados *store,
RGWObjectCtx& obj_ctx,
RGWBucketInfo& bucket_info,
map<string, bufferlist>& bucket_attrs,
RGWAccessControlPolicy *policy,
rgw_obj& obj)
{
bufferlist bl;
int ret = 0;
RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
RGWRados::Object::Read rop(&op_target);
ret = rop.get_attr(RGW_ATTR_ACL, bl);
if (ret >= 0) {
ret = decode_policy(cct, bl, policy);
if (ret < 0)
return ret;
} else if (ret == -ENODATA) {
/* object exists, but policy is broken */
ldout(cct, 0) << "WARNING: couldn't find acl header for object, generating default" << dendl;
RGWUserInfo uinfo;
ret = rgw_get_user_info_by_uid(store, bucket_info.owner, uinfo);
if (ret < 0)
return ret;
policy->create_default(bucket_info.owner, uinfo.display_name);
}
return ret;
}
/**
* Get the AccessControlPolicy for an object off of disk.
* policy: must point to a valid RGWACL, and will be filled upon return.
* bucket: name of the bucket containing the object.
* object: name of the object to get the ACL for.
* Returns: 0 on success, -ERR# otherwise.
*/
static int get_policy_from_attr(CephContext *cct,
RGWRados *store,
void *ctx,
RGWBucketInfo& bucket_info,
map<string, bufferlist>& bucket_attrs,
RGWAccessControlPolicy *policy,
rgw_obj& obj)
{
if (obj.bucket.name.empty()) {
return 0;
}
if (obj.get_object().empty()) {
rgw_obj instance_obj;
store->get_bucket_instance_obj(bucket_info.bucket, instance_obj);
return get_bucket_policy_from_attr(cct, store, ctx, bucket_info, bucket_attrs,
policy, instance_obj);
}
return get_obj_policy_from_attr(cct, store, *static_cast<RGWObjectCtx *>(ctx), bucket_info, bucket_attrs,
policy, obj);
}
static int get_obj_attrs(RGWRados *store, struct req_state *s, rgw_obj& obj, map<string, bufferlist>& attrs)
{
RGWRados::Object op_target(store, s->bucket_info, *static_cast<RGWObjectCtx *>(s->obj_ctx), obj);
RGWRados::Object::Read read_op(&op_target);
read_op.params.attrs = &attrs;
read_op.params.perr = &s->err;
return read_op.prepare(NULL, NULL);
}
static int get_system_obj_attrs(RGWRados *store, struct req_state *s, rgw_obj& obj, map<string, bufferlist>& attrs,
uint64_t *obj_size, RGWObjVersionTracker *objv_tracker)
{
RGWRados::SystemObject src(store, *static_cast<RGWObjectCtx *>(s->obj_ctx), obj);
RGWRados::SystemObject::Read rop(&src);
rop.stat_params.attrs = &attrs;
rop.stat_params.obj_size = obj_size;
int ret = rop.stat(objv_tracker);
return ret;
}
static int read_policy(RGWRados *store,
struct req_state *s,
RGWBucketInfo& bucket_info,
map<string, bufferlist>& bucket_attrs,
RGWAccessControlPolicy *policy,
rgw_bucket& bucket,
rgw_obj_key& object)
{
string upload_id;
upload_id = s->info.args.get("uploadId");
rgw_obj obj;
if (!s->system_request && bucket_info.flags & BUCKET_SUSPENDED) {
ldout(s->cct, 0) << "NOTICE: bucket " << bucket_info.bucket.name << " is suspended" << dendl;
return -ERR_USER_SUSPENDED;
}
if (!object.empty() && !upload_id.empty()) {
/* multipart upload */
RGWMPObj mp(object.name, upload_id);
string oid = mp.get_meta();
obj.init_ns(bucket, oid, mp_ns);
obj.set_in_extra_data(true);
} else {
obj = rgw_obj(bucket, object);
}
int ret = get_policy_from_attr(s->cct, store, s->obj_ctx, bucket_info, bucket_attrs, policy, obj);
if (ret == -ENOENT && !object.empty()) {
/* object does not exist checking the bucket's ACL to make sure
that we send a proper error code */
RGWAccessControlPolicy bucket_policy(s->cct);
string no_object;
rgw_obj no_obj(bucket, no_object);
ret = get_policy_from_attr(s->cct, store, s->obj_ctx, bucket_info, bucket_attrs, &bucket_policy, no_obj);
if (ret < 0) {
return ret;
}
const rgw_user& bucket_owner = bucket_policy.get_owner().get_id();
if (bucket_owner.compare(s->user->user_id) != 0 &&
!s->auth_identity->is_admin_of(bucket_owner) &&
!bucket_policy.verify_permission(*s->auth_identity, s->perm_mask,
RGW_PERM_READ)) {
ret = -EACCES;
} else {
ret = -ENOENT;
}
} else if (ret == -ENOENT) {
ret = -ERR_NO_SUCH_BUCKET;
}
return ret;
}
/**
* Get the AccessControlPolicy for an user, bucket or object off of disk.
* s: The req_state to draw information from.
* only_bucket: If true, reads the user and bucket ACLs rather than the object ACL.
* Returns: 0 on success, -ERR# otherwise.
*/
int rgw_build_bucket_policies(RGWRados* store, struct req_state* s)
{
int ret = 0;
rgw_obj_key obj;
RGWUserInfo bucket_owner_info;
RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
string bi = s->info.args.get(RGW_SYS_PARAM_PREFIX "bucket-instance");
if (!bi.empty()) {
ret = rgw_bucket_parse_bucket_instance(bi, &s->bucket_instance_id, &s->bucket_instance_shard_id);
if (ret < 0) {
return ret;
}
}
if(s->dialect.compare("s3") == 0) {
s->bucket_acl = new RGWAccessControlPolicy_S3(s->cct);
} else if(s->dialect.compare("swift") == 0) {
s->user_acl = std::unique_ptr<RGWAccessControlPolicy>(
new RGWAccessControlPolicy_SWIFTAcct(s->cct));
s->bucket_acl = new RGWAccessControlPolicy_SWIFT(s->cct);
} else {
s->bucket_acl = new RGWAccessControlPolicy(s->cct);
}
/* check if copy source is within the current domain */
if (!s->src_bucket_name.empty()) {
RGWBucketInfo source_info;
if (s->bucket_instance_id.empty()) {
ret = store->get_bucket_info(obj_ctx, s->src_tenant_name, s->src_bucket_name, source_info, NULL);
} else {
ret = store->get_bucket_instance_info(obj_ctx, s->bucket_instance_id, source_info, NULL, NULL);
}
if (ret == 0) {
string& zonegroup = source_info.zonegroup;
s->local_source = store->get_zonegroup().equals(zonegroup);
}
}
struct {
rgw_user uid;
std::string display_name;
} acct_acl_user = {
s->user->user_id,
s->user->display_name,
};
if (!s->bucket_name.empty()) {
s->bucket_exists = true;
if (s->bucket_instance_id.empty()) {
ret = store->get_bucket_info(obj_ctx, s->bucket_tenant, s->bucket_name, s->bucket_info, NULL, &s->bucket_attrs);
} else {
ret = store->get_bucket_instance_info(obj_ctx, s->bucket_instance_id, s->bucket_info, NULL, &s->bucket_attrs);
}
if (ret < 0) {
if (ret != -ENOENT) {
string bucket_log;
rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name, bucket_log);
ldout(s->cct, 0) << "NOTICE: couldn't get bucket from bucket_name (name=" << bucket_log << ")" << dendl;
return ret;
}
s->bucket_exists = false;
}
s->bucket = s->bucket_info.bucket;
if (s->bucket_exists) {
rgw_obj_key no_obj;
ret = read_policy(store, s, s->bucket_info, s->bucket_attrs, s->bucket_acl, s->bucket, no_obj);
acct_acl_user = {
s->bucket_info.owner,
s->bucket_acl->get_owner().get_display_name(),
};
} else {
s->bucket_acl->create_default(s->user->user_id, s->user->display_name);
ret = -ERR_NO_SUCH_BUCKET;
}
s->bucket_owner = s->bucket_acl->get_owner();
RGWZoneGroup zonegroup;
int r = store->get_zonegroup(s->bucket_info.zonegroup, zonegroup);
if (!r) {
if (!zonegroup.endpoints.empty()) {
s->zonegroup_endpoint = zonegroup.endpoints.front();
}
s->zonegroup_name = zonegroup.get_name();
}
if (r < 0 && ret == 0) {
ret = r;
}
if (s->bucket_exists && !store->get_zonegroup().equals(s->bucket_info.zonegroup)) {
ldout(s->cct, 0) << "NOTICE: request for data in a different zonegroup (" << s->bucket_info.zonegroup << " != " << store->get_zonegroup().get_id() << ")" << dendl;
/* we now need to make sure that the operation actually requires copy source, that is
* it's a copy operation
*/
if (store->get_zonegroup().is_master && s->op == OP_DELETE && s->system_request) {
/*If the operation is delete and if this is the master, don't redirect*/
} else if (!s->local_source ||
(s->op != OP_PUT && s->op != OP_COPY) ||
s->object.empty()) {
return -ERR_PERMANENT_REDIRECT;
}
}
}
/* handle user ACL only for those APIs which support it */
if (s->user_acl) {
map<string, bufferlist> uattrs;
ret = rgw_get_user_attrs_by_uid(store, acct_acl_user.uid, uattrs);
if (!ret) {
ret = get_user_policy_from_attr(s->cct, store, uattrs, *s->user_acl);
}
if (-ENOENT == ret) {
/* In already existing clusters users won't have ACL. In such case
* assuming that only account owner has the rights seems to be
* reasonable. That allows to have only one verification logic.
* NOTE: there is small compatibility kludge for global, empty tenant:
* 1. if we try to reach an existing bucket, its owner is considered
* as account owner.
* 2. otherwise account owner is identity stored in s->user->user_id. */
s->user_acl->create_default(acct_acl_user.uid,
acct_acl_user.display_name);
ret = 0;
} else {
ldout(s->cct, 0) << "NOTICE: couldn't get user attrs for handling ACL (user_id="
<< s->user->user_id
<< ", ret="
<< ret
<< ")" << dendl;
return ret;
}
}
return ret;
}
/**
* Get the AccessControlPolicy for a bucket or object off of disk.
* s: The req_state to draw information from.
* only_bucket: If true, reads the bucket ACL rather than the object ACL.
* Returns: 0 on success, -ERR# otherwise.
*/
int rgw_build_object_policies(RGWRados *store, struct req_state *s,
bool prefetch_data)
{
int ret = 0;
if (!s->object.empty()) {
if (!s->bucket_exists) {
return -ERR_NO_SUCH_BUCKET;
}
s->object_acl = new RGWAccessControlPolicy(s->cct);
rgw_obj obj(s->bucket, s->object);
store->set_atomic(s->obj_ctx, obj);
if (prefetch_data) {
store->set_prefetch_data(s->obj_ctx, obj);
}
ret = read_policy(store, s, s->bucket_info, s->bucket_attrs, s->object_acl, s->bucket, s->object);
}
return ret;
}
static void rgw_bucket_object_pre_exec(struct req_state *s)
{
if (s->expect_cont)
dump_continue(s);
dump_bucket_from_state(s);
}
int RGWGetObj::verify_permission()
{
obj = rgw_obj(s->bucket, s->object);
store->set_atomic(s->obj_ctx, obj);
if (get_data) {
store->set_prefetch_data(s->obj_ctx, obj);
}
if (!verify_object_permission(s, RGW_PERM_READ)) {
return -EACCES;
}
return 0;
}
int RGWOp::verify_op_mask()
{
uint32_t required_mask = op_mask();
ldout(s->cct, 20) << "required_mask= " << required_mask
<< " user.op_mask=" << s->user->op_mask << dendl;
if ((s->user->op_mask & required_mask) != required_mask) {
return -EPERM;
}
if (!s->system_request && (required_mask & RGW_OP_TYPE_MODIFY) && store->get_zone().is_read_only()) {
ldout(s->cct, 5) << "NOTICE: modify request to a read-only zone by a non-system user, permission denied" << dendl;
return -EPERM;
}
return 0;
}
int RGWOp::do_aws4_auth_completion()
{
int ret;
if (s->aws4_auth_needs_complete) {
/* complete */
ret = RGW_Auth_S3::authorize_aws4_auth_complete(store, s);
s->aws4_auth_needs_complete = false;
if (ret) {
return ret;
}
/* verify signature */
if (s->aws4_auth->signature != s->aws4_auth->new_signature) {
ret = -ERR_SIGNATURE_NO_MATCH;
ldout(s->cct, 20) << "delayed aws4 auth failed" << dendl;
return ret;
}
/* authorization ok */
dout(10) << "v4 auth ok" << dendl;
}
return 0;
}
int RGWOp::init_quota()
{
/* no quota enforcement for system requests */
if (s->system_request)
return 0;
/* init quota related stuff */
if (!(s->user->op_mask & RGW_OP_TYPE_MODIFY)) {
return 0;
}
/* only interested in object related ops */
if (s->object.empty()) {
return 0;
}
RGWUserInfo owner_info;
RGWUserInfo *uinfo;
if (s->user->user_id == s->bucket_owner.get_id()) {
uinfo = s->user;
} else {
int r = rgw_get_user_info_by_uid(store, s->bucket_info.owner, owner_info);
if (r < 0)
return r;
uinfo = &owner_info;
}
if (s->bucket_info.quota.enabled) {
bucket_quota = s->bucket_info.quota;
} else if (uinfo->bucket_quota.enabled) {
bucket_quota = uinfo->bucket_quota;
} else {
bucket_quota = store->get_bucket_quota();
}
if (uinfo->user_quota.enabled) {
user_quota = uinfo->user_quota;
} else {
user_quota = store->get_user_quota();
}
return 0;
}
static bool validate_cors_rule_method(RGWCORSRule *rule, const char *req_meth) {
uint8_t flags = 0;
if (!req_meth) {
dout(5) << "req_meth is null" << dendl;
return false;
}
if (strcmp(req_meth, "GET") == 0) flags = RGW_CORS_GET;
else if (strcmp(req_meth, "POST") == 0) flags = RGW_CORS_POST;
else if (strcmp(req_meth, "PUT") == 0) flags = RGW_CORS_PUT;
else if (strcmp(req_meth, "DELETE") == 0) flags = RGW_CORS_DELETE;
else if (strcmp(req_meth, "HEAD") == 0) flags = RGW_CORS_HEAD;
if ((rule->get_allowed_methods() & flags) == flags) {
dout(10) << "Method " << req_meth << " is supported" << dendl;
} else {
dout(5) << "Method " << req_meth << " is not supported" << dendl;
return false;
}
return true;
}
int RGWOp::read_bucket_cors()
{
bufferlist bl;
map<string, bufferlist>::iterator aiter = s->bucket_attrs.find(RGW_ATTR_CORS);
if (aiter == s->bucket_attrs.end()) {
ldout(s->cct, 20) << "no CORS configuration attr found" << dendl;
cors_exist = false;
return 0; /* no CORS configuration found */
}
cors_exist = true;
bl = aiter->second;
bufferlist::iterator iter = bl.begin();
try {
bucket_cors.decode(iter);
} catch (buffer::error& err) {
ldout(s->cct, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl;
return -EIO;
}
if (s->cct->_conf->subsys.should_gather(ceph_subsys_rgw, 15)) {
RGWCORSConfiguration_S3 *s3cors = static_cast<RGWCORSConfiguration_S3 *>(&bucket_cors);
ldout(s->cct, 15) << "Read RGWCORSConfiguration";
s3cors->to_xml(*_dout);
*_dout << dendl;
}
return 0;
}
/** CORS 6.2.6.
* If any of the header field-names is not a ASCII case-insensitive match for
* any of the values in list of headers do not set any additional headers and
* terminate this set of steps.
* */
static void get_cors_response_headers(RGWCORSRule *rule, const char *req_hdrs, string& hdrs, string& exp_hdrs, unsigned *max_age) {
if (req_hdrs) {
list<string> hl;
get_str_list(req_hdrs, hl);
for(list<string>::iterator it = hl.begin(); it != hl.end(); ++it) {
if (!rule->is_header_allowed((*it).c_str(), (*it).length())) {
dout(5) << "Header " << (*it) << " is not registered in this rule" << dendl;
} else {
if (hdrs.length() > 0) hdrs.append(",");
hdrs.append((*it));
}
}
}
rule->format_exp_headers(exp_hdrs);
*max_age = rule->get_max_age();
}
/**
* Generate the CORS header response
*
* This is described in the CORS standard, section 6.2.
*/
bool RGWOp::generate_cors_headers(string& origin, string& method, string& headers, string& exp_headers, unsigned *max_age)
{
/* CORS 6.2.1. */
const char *orig = s->info.env->get("HTTP_ORIGIN");
if (!orig) {
return false;
}
/* Custom: */
origin = orig;
op_ret = read_bucket_cors();
if (op_ret < 0) {
return false;
}
if (!cors_exist) {
dout(2) << "No CORS configuration set yet for this bucket" << dendl;
return false;
}
/* CORS 6.2.2. */
RGWCORSRule *rule = bucket_cors.host_name_rule(orig);
if (!rule)
return false;
/*
* Set the Allowed-Origin header to a asterisk if this is allowed in the rule
* and no Authorization was send by the client
*
* The origin parameter specifies a URI that may access the resource. The browser must enforce this.
* For requests without credentials, the server may specify "*" as a wildcard,
* thereby allowing any origin to access the resource.
*/
const char *authorization = s->info.env->get("HTTP_AUTHORIZATION");
if (!authorization && rule->has_wildcard_origin())
origin = "*";
/* CORS 6.2.3. */
const char *req_meth = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_METHOD");
if (!req_meth) {
req_meth = s->info.method;
}
if (req_meth) {
method = req_meth;
/* CORS 6.2.5. */
if (!validate_cors_rule_method(rule, req_meth)) {
return false;
}
}
/* CORS 6.2.4. */
const char *req_hdrs = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_HEADERS");
/* CORS 6.2.6. */
get_cors_response_headers(rule, req_hdrs, headers, exp_headers, max_age);
return true;
}
int RGWGetObj::read_user_manifest_part(rgw_bucket& bucket,
const RGWObjEnt& ent,
RGWAccessControlPolicy * const bucket_policy,
const off_t start_ofs,
const off_t end_ofs)
{
ldout(s->cct, 20) << "user manifest obj=" << ent.key.name << "[" << ent.key.instance << "]" << dendl;
int64_t cur_ofs = start_ofs;
int64_t cur_end = end_ofs;
utime_t start_time = s->time;
rgw_obj part(bucket, ent.key);
map<string, bufferlist> attrs;
uint64_t obj_size;
RGWObjectCtx obj_ctx(store);
RGWAccessControlPolicy obj_policy(s->cct);
ldout(s->cct, 20) << "reading obj=" << part << " ofs=" << cur_ofs << " end=" << cur_end << dendl;
obj_ctx.set_atomic(part);
store->set_prefetch_data(&obj_ctx, part);
RGWRados::Object op_target(store, s->bucket_info, obj_ctx, part);
RGWRados::Object::Read read_op(&op_target);
read_op.conds.if_match = ent.etag.c_str();
read_op.params.attrs = &attrs;
read_op.params.obj_size = &obj_size;
read_op.params.perr = &s->err;
op_ret = read_op.prepare(&cur_ofs, &cur_end);
if (op_ret < 0)
return op_ret;
if (obj_size != ent.size) {
// hmm.. something wrong, object not as expected, abort!
ldout(s->cct, 0) << "ERROR: expected obj_size=" << obj_size << ", actual read size=" << ent.size << dendl;
return -EIO;
}
op_ret = rgw_policy_from_attrset(s->cct, attrs, &obj_policy);
if (op_ret < 0)
return op_ret;
/* We can use global user_acl because LOs cannot have segments
* stored inside different accounts. */
if (!verify_object_permission(s, s->user_acl.get(), bucket_policy,
&obj_policy, RGW_PERM_READ)) {
return -EPERM;
}
perfcounter->inc(l_rgw_get_b, cur_end - cur_ofs);
while (cur_ofs <= cur_end) {
bufferlist bl;
op_ret = read_op.read(cur_ofs, cur_end, bl);
if (op_ret < 0)
return op_ret;
off_t len = bl.length();
cur_ofs += len;
op_ret = 0; /* XXX redundant? */
perfcounter->tinc(l_rgw_get_lat,
(ceph_clock_now(s->cct) - start_time));
send_response_data(bl, 0, len);
start_time = ceph_clock_now(s->cct);
}
return 0;
}
static int iterate_user_manifest_parts(CephContext * const cct,
RGWRados * const store,
const off_t ofs,
const off_t end,
RGWBucketInfo *pbucket_info,
const string& obj_prefix,
RGWAccessControlPolicy * const bucket_policy,
uint64_t * const ptotal_len,
uint64_t * const pobj_size,
string * const pobj_sum,
int (*cb)(rgw_bucket& bucket,
const RGWObjEnt& ent,
RGWAccessControlPolicy * const bucket_policy,
off_t start_ofs,
off_t end_ofs,
void *param),
void * const cb_param)
{
rgw_bucket& bucket = pbucket_info->bucket;
uint64_t obj_ofs = 0, len_count = 0;
bool found_start = false, found_end = false, handled_end = false;
string delim;
bool is_truncated;
vector<RGWObjEnt> objs;
utime_t start_time = ceph_clock_now(cct);
RGWRados::Bucket target(store, *pbucket_info);
RGWRados::Bucket::List list_op(&target);
list_op.params.prefix = obj_prefix;
list_op.params.delim = delim;
MD5 etag_sum;
do {
#define MAX_LIST_OBJS 100
int r = list_op.list_objects(MAX_LIST_OBJS, &objs, NULL, &is_truncated);
if (r < 0) {
return r;
}
for (RGWObjEnt& ent : objs) {
uint64_t cur_total_len = obj_ofs;
uint64_t start_ofs = 0, end_ofs = ent.size;
if (!found_start && cur_total_len + ent.size > (uint64_t)ofs) {
start_ofs = ofs - obj_ofs;
found_start = true;
}
obj_ofs += ent.size;
if (pobj_sum) {
etag_sum.Update((const byte *)ent.etag.c_str(),
ent.etag.length());
}
if (!found_end && obj_ofs > (uint64_t)end) {
end_ofs = end - cur_total_len + 1;
found_end = true;
}
perfcounter->tinc(l_rgw_get_lat,
(ceph_clock_now(cct) - start_time));
if (found_start && !handled_end) {
len_count += end_ofs - start_ofs;
if (cb) {
r = cb(bucket, ent, bucket_policy, start_ofs, end_ofs, cb_param);
if (r < 0) {
return r;
}
}
}
handled_end = found_end;
start_time = ceph_clock_now(cct);
}
} while (is_truncated);
if (ptotal_len) {
*ptotal_len = len_count;
}
if (pobj_size) {
*pobj_size = obj_ofs;
}
if (pobj_sum) {
complete_etag(etag_sum, pobj_sum);
}
return 0;
}
struct rgw_slo_part {
RGWAccessControlPolicy *bucket_policy;
rgw_bucket bucket;
string obj_name;
uint64_t size;
string etag;
rgw_slo_part() : bucket_policy(NULL), size(0) {}
};
static int iterate_slo_parts(CephContext *cct,
RGWRados *store,
off_t ofs,
off_t end,
map<uint64_t, rgw_slo_part>& slo_parts,
int (*cb)(rgw_bucket& bucket,
const RGWObjEnt& ent,
RGWAccessControlPolicy *bucket_policy,
off_t start_ofs,
off_t end_ofs,
void *param),
void *cb_param)
{
bool found_start = false, found_end = false;
if (slo_parts.empty()) {
return 0;
}
utime_t start_time = ceph_clock_now(cct);
map<uint64_t, rgw_slo_part>::iterator iter = slo_parts.upper_bound(ofs);
if (iter != slo_parts.begin()) {
--iter;
}
uint64_t obj_ofs = iter->first;
for (; iter != slo_parts.end() && !found_end; ++iter) {
rgw_slo_part& part = iter->second;
RGWObjEnt ent;
ent.key.name = part.obj_name;
ent.size = part.size;
ent.etag = part.etag;
uint64_t cur_total_len = obj_ofs;
uint64_t start_ofs = 0, end_ofs = ent.size;
if (!found_start && cur_total_len + ent.size > (uint64_t)ofs) {
start_ofs = ofs - obj_ofs;
found_start = true;
}
obj_ofs += ent.size;
if (!found_end && obj_ofs > (uint64_t)end) {
end_ofs = end - cur_total_len + 1;
found_end = true;
}
perfcounter->tinc(l_rgw_get_lat,
(ceph_clock_now(cct) - start_time));
if (found_start) {
if (cb) {
int r = cb(part.bucket, ent, part.bucket_policy, start_ofs, end_ofs, cb_param);
if (r < 0)
return r;
}
}
start_time = ceph_clock_now(cct);