/
rgw_op.cc
3135 lines (2555 loc) · 79.8 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
#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_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_client_io.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;
#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;
int pos = name.find(MP_META_SUFFIX, len - 5);
if (pos <= 0)
return false;
pos = name.rfind('.', pos - 1);
if (pos < 0)
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;
int pos = s.find("bytes=");
if (pos < 0) {
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 < 0)
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 void format_xattr(std::string &xattr)
{
/* If the extended attribute is not valid UTF-8, we encode it using quoted-printable
* encoding.
*/
if ((check_utf8(xattr.c_str(), xattr.length()) != 0) ||
(check_for_control_characters(xattr.c_str(), xattr.length()) != 0)) {
static const char MIME_PREFIX_STR[] = "=?UTF-8?Q?";
static const int MIME_PREFIX_LEN = sizeof(MIME_PREFIX_STR) - 1;
static const char MIME_SUFFIX_STR[] = "?=";
static const int MIME_SUFFIX_LEN = sizeof(MIME_SUFFIX_STR) - 1;
int mlen = mime_encode_as_qp(xattr.c_str(), NULL, 0);
char *mime = new char[MIME_PREFIX_LEN + mlen + MIME_SUFFIX_LEN + 1];
strcpy(mime, MIME_PREFIX_STR);
mime_encode_as_qp(xattr.c_str(), mime + MIME_PREFIX_LEN, mlen);
strcpy(mime + MIME_PREFIX_LEN + (mlen - 1), MIME_SUFFIX_STR);
xattr.assign(mime);
delete [] mime;
}
}
/**
* Get the HTTP request metadata out of the req_state as a
* map(<attr_name, attr_contents>, where attr_name is RGW_ATTR_PREFIX.HTTP_NAME)
* s: The request state
* attrs: will be filled up with attrs mapped as <attr_name, attr_contents>
*
*/
static void rgw_get_request_metadata(CephContext *cct, struct req_info& info, map<string, bufferlist>& attrs)
{
map<string, string>::iterator iter;
for (iter = info.x_meta_map.begin(); iter != info.x_meta_map.end(); ++iter) {
const string &name(iter->first);
string &xattr(iter->second);
ldout(cct, 10) << "x>> " << name << ":" << xattr << dendl;
format_xattr(xattr);
string attr_name(RGW_ATTR_PREFIX);
attr_name.append(name);
map<string, bufferlist>::value_type v(attr_name, bufferlist());
std::pair < map<string, bufferlist>::iterator, bool > rval(attrs.insert(v));
bufferlist& bl(rval.first->second);
bl.append(xattr.c_str(), xattr.size() + 1);
}
}
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) << "Read AccessControlPolicy";
s3policy->to_xml(*_dout);
*_dout << dendl;
}
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, void *ctx,
RGWBucketInfo& bucket_info, map<string, bufferlist>& bucket_attrs,
RGWAccessControlPolicy *policy, rgw_obj& obj)
{
bufferlist bl;
int ret = 0;
ret = store->get_attr(ctx, obj, 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.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, 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,
uint64_t *obj_size, RGWObjVersionTracker *objv_tracker)
{
void *handle;
int ret = store->prepare_get_obj(s->obj_ctx, obj, NULL, NULL, &attrs, NULL,
NULL, NULL, NULL, NULL, NULL, obj_size, objv_tracker, &handle, &s->err);
store->finish_get_obj(&handle);
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, string& object)
{
string upload_id;
upload_id = s->info.args.get("uploadId");
string oid = object;
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 (!oid.empty() && !upload_id.empty()) {
RGWMPObj mp(oid, upload_id);
oid = mp.get_meta();
obj.init_ns(bucket, oid, mp_ns);
obj.set_in_extra_data(true);
} else {
obj.init(bucket, oid);
}
int ret = get_policy_from_attr(s->cct, store, s->obj_ctx, bucket_info, bucket_attrs, policy, obj);
if (ret == -ENOENT && object.size()) {
/* 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;
string& owner = bucket_policy.get_owner().get_id();
if (!s->system_request && owner.compare(s->user.user_id) != 0 &&
!bucket_policy.verify_permission(s->user.user_id, 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 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.
*/
static int rgw_build_policies(RGWRados *store, struct req_state *s, bool only_bucket, bool prefetch_data)
{
int ret = 0;
string obj_str;
RGWUserInfo bucket_owner_info;
s->bucket_instance_id = s->info.args.get(RGW_SYS_PARAM_PREFIX "bucket-instance");
s->bucket_acl = new RGWAccessControlPolicy(s->cct);
if (s->copy_source) { /* check if copy source is within the current domain */
const char *src = s->copy_source;
if (*src == '/')
++src;
string copy_source_str(src);
int pos = copy_source_str.find('/');
if (pos > 0)
copy_source_str = copy_source_str.substr(0, pos);
RGWBucketInfo source_info;
ret = store->get_bucket_info(s->obj_ctx, copy_source_str, source_info, NULL);
if (ret == 0) {
string& region = source_info.region;
s->local_source = store->region.equals(region);
}
}
if (!s->bucket_name_str.empty()) {
s->bucket_exists = true;
if (s->bucket_instance_id.empty()) {
ret = store->get_bucket_info(s->obj_ctx, s->bucket_name_str, s->bucket_info, NULL, &s->bucket_attrs);
} else {
ret = store->get_bucket_instance_info(s->obj_ctx, s->bucket_instance_id, s->bucket_info, NULL, &s->bucket_attrs);
}
if (ret < 0) {
if (ret != -ENOENT) {
ldout(s->cct, 0) << "NOTICE: couldn't get bucket from bucket_name (name=" << s->bucket_name_str << ")" << dendl;
return ret;
}
s->bucket_exists = false;
}
s->bucket = s->bucket_info.bucket;
if (s->bucket_exists) {
string no_obj;
ret = read_policy(store, s, s->bucket_info, s->bucket_attrs, s->bucket_acl, s->bucket, no_obj);
} 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();
string& region = s->bucket_info.region;
if (s->bucket_exists && !store->region.equals(region)) {
ldout(s->cct, 0) << "NOTICE: request for data in a different region (" << region << " != " << store->region.name << ")" << dendl;
/* we now need to make sure that the operation actually requires copy source, that is
* it's a copy operation
*/
if (store->region.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_str.empty()) {
return -ERR_PERMANENT_REDIRECT;
}
}
}
/* we're passed only_bucket = true when we specifically need the bucket's
acls, that happens on write operations */
if (!only_bucket && !s->object_str.empty()) {
if (!s->bucket_exists) {
return -ERR_NO_SUCH_BUCKET;
}
s->object_acl = new RGWAccessControlPolicy(s->cct);
obj_str = s->object_str;
rgw_obj obj(s->bucket, obj_str);
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, obj_str);
}
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.init(s->bucket, s->object_str);
store->set_atomic(s->obj_ctx, obj);
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->zone.is_master) {
ldout(s->cct, 5) << "NOTICE: modify request to a non-master zone by a non-system user, permission denied" << dendl;
return -EPERM;
}
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_str.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->region_map.bucket_quota;
}
if (uinfo->user_quota.enabled) {
user_quota = uinfo->user_quota;
} else {
user_quota = store->region_map.user_quota;
}
return 0;
}
static bool validate_cors_rule_method(RGWCORSRule *rule, const char *req_meth) {
uint8_t flags = 0;
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;
int ret = read_bucket_cors();
if (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;
/* 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, RGWObjEnt& ent, RGWAccessControlPolicy *bucket_policy, off_t start_ofs, off_t end_ofs)
{
ldout(s->cct, 0) << "user manifest obj=" << ent.name << dendl;
void *handle = NULL;
off_t cur_ofs = start_ofs;
off_t cur_end = end_ofs;
utime_t start_time = s->time;
rgw_obj part(bucket, ent.name);
map<string, bufferlist> attrs;
uint64_t obj_size;
void *obj_ctx = store->create_context(s);
RGWAccessControlPolicy obj_policy(s->cct);
ldout(s->cct, 20) << "reading obj=" << part << " ofs=" << cur_ofs << " end=" << cur_end << dendl;
store->set_atomic(obj_ctx, part);
store->set_prefetch_data(obj_ctx, part);
ret = store->prepare_get_obj(obj_ctx, part, &cur_ofs, &cur_end, &attrs, NULL,
NULL, NULL, NULL, NULL, NULL, &obj_size, NULL, &handle, &s->err);
if (ret < 0)
goto done_err;
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;
ret = -EIO;
goto done_err;
}
ret = rgw_policy_from_attrset(s->cct, attrs, &obj_policy);
if (ret < 0)
goto done_err;
if (!verify_object_permission(s, bucket_policy, &obj_policy, RGW_PERM_READ)) {
ret = -EPERM;
goto done_err;
}
perfcounter->inc(l_rgw_get_b, cur_end - cur_ofs);
while (cur_ofs <= cur_end) {
bufferlist bl;
ret = store->get_obj(obj_ctx, NULL, &handle, part, bl, cur_ofs, cur_end);
if (ret < 0)
goto done_err;
off_t len = bl.length();
cur_ofs += len;
ofs += len;
ret = 0;
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);
}
store->destroy_context(obj_ctx);
obj_ctx = NULL;
store->finish_get_obj(&handle);
return 0;
done_err:
if (obj_ctx)
store->destroy_context(obj_ctx);
return ret;
}
static int iterate_user_manifest_parts(CephContext *cct, RGWRados *store, off_t ofs, off_t end,
rgw_bucket& bucket, string& obj_prefix, RGWAccessControlPolicy *bucket_policy,
uint64_t *ptotal_len,
int (*cb)(rgw_bucket& bucket, RGWObjEnt& ent, RGWAccessControlPolicy *bucket_policy,
off_t start_ofs, off_t end_ofs, void *param), void *cb_param)
{
uint64_t obj_ofs = 0, len_count = 0;
bool found_start = false, found_end = false;
string delim;
string marker;
bool is_truncated;
string no_ns;
map<string, bool> common_prefixes;
vector<RGWObjEnt> objs;
utime_t start_time = ceph_clock_now(cct);
do {
#define MAX_LIST_OBJS 100
int r = store->list_objects(bucket, MAX_LIST_OBJS, obj_prefix, delim, marker, NULL,
objs, common_prefixes,
true, no_ns, true, &is_truncated, NULL);
if (r < 0)
return r;
vector<RGWObjEnt>::iterator viter;
for (viter = objs.begin(); viter != objs.end() && !found_end; ++viter) {
RGWObjEnt& ent = *viter;
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) {
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;
}
}
marker = ent.name;
start_time = ceph_clock_now(cct);
}
} while (is_truncated && !found_end);
if (ptotal_len)
*ptotal_len = len_count;
return 0;
}
static int get_obj_user_manifest_iterate_cb(rgw_bucket& bucket, RGWObjEnt& ent, RGWAccessControlPolicy *bucket_policy, off_t start_ofs, off_t end_ofs,
void *param)
{
RGWGetObj *op = (RGWGetObj *)param;
return op->read_user_manifest_part(bucket, ent, bucket_policy, start_ofs, end_ofs);
}
int RGWGetObj::handle_user_manifest(const char *prefix)
{
ldout(s->cct, 2) << "RGWGetObj::handle_user_manifest() prefix=" << prefix << dendl;
string prefix_str = prefix;
int pos = prefix_str.find('/');
if (pos < 0)
return -EINVAL;
string bucket_name = prefix_str.substr(0, pos);
string obj_prefix = prefix_str.substr(pos + 1);
rgw_bucket bucket;
RGWAccessControlPolicy _bucket_policy(s->cct);
RGWAccessControlPolicy *bucket_policy;
if (bucket_name.compare(s->bucket.name) != 0) {
RGWBucketInfo bucket_info;
map<string, bufferlist> bucket_attrs;
int r = store->get_bucket_info(NULL, bucket_name, bucket_info, NULL, &bucket_attrs);
if (r < 0) {
ldout(s->cct, 0) << "could not get bucket info for bucket=" << bucket_name << dendl;
return r;
}
bucket = bucket_info.bucket;
string no_obj;
bucket_policy = &_bucket_policy;
r = read_policy(store, s, bucket_info, bucket_attrs, bucket_policy, bucket, no_obj);
if (r < 0) {
ldout(s->cct, 0) << "failed to read bucket policy" << dendl;
return r;
}
} else {
bucket = s->bucket;
bucket_policy = s->bucket_acl;
}
/* dry run to find out total length */
int r = iterate_user_manifest_parts(s->cct, store, ofs, end, bucket, obj_prefix, bucket_policy, &total_len, NULL, NULL);
if (r < 0)
return r;
s->obj_size = total_len;
r = iterate_user_manifest_parts(s->cct, store, ofs, end, bucket, obj_prefix, bucket_policy, NULL, get_obj_user_manifest_iterate_cb, (void *)this);
if (r < 0)
return r;
return 0;
}
class RGWGetObj_CB : public RGWGetDataCB
{
RGWGetObj *op;
public:
RGWGetObj_CB(RGWGetObj *_op) : op(_op) {}
virtual ~RGWGetObj_CB() {}
int handle_data(bufferlist& bl, off_t bl_ofs, off_t bl_len) {
return op->get_data_cb(bl, bl_ofs, bl_len);
}
};
int RGWGetObj::get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len)
{
/* garbage collection related handling */
utime_t start_time = ceph_clock_now(s->cct);
if (start_time > gc_invalidate_time) {
int r = store->defer_gc(s->obj_ctx, obj);
if (r < 0) {
dout(0) << "WARNING: could not defer gc entry for obj" << dendl;
}
gc_invalidate_time = start_time;
gc_invalidate_time += (s->cct->_conf->rgw_gc_obj_min_wait / 2);
}
return send_response_data(bl, bl_ofs, bl_len);
}
void RGWGetObj::pre_exec()
{
rgw_bucket_object_pre_exec(s);
}
void RGWGetObj::execute()
{
void *handle = NULL;
utime_t start_time = s->time;
bufferlist bl;
gc_invalidate_time = ceph_clock_now(s->cct);
gc_invalidate_time += (s->cct->_conf->rgw_gc_obj_min_wait / 2);
RGWGetObj_CB cb(this);
map<string, bufferlist>::iterator attr_iter;
perfcounter->inc(l_rgw_get);
off_t new_ofs, new_end;
ret = get_params();
if (ret < 0)
goto done_err;
ret = init_common();
if (ret < 0)
goto done_err;
new_ofs = ofs;
new_end = end;
ret = store->prepare_get_obj(s->obj_ctx, obj, &new_ofs, &new_end, &attrs, mod_ptr,
unmod_ptr, &lastmod, if_match, if_nomatch, &total_len, &s->obj_size, NULL, &handle, &s->err);
if (ret < 0)
goto done_err;
attr_iter = attrs.find(RGW_ATTR_USER_MANIFEST);
if (attr_iter != attrs.end()) {
ret = handle_user_manifest(attr_iter->second.c_str());
if (ret < 0) {
ldout(s->cct, 0) << "ERROR: failed to handle user manifest ret=" << ret << dendl;
}
return;
}
ofs = new_ofs;
end = new_end;
start = ofs;
if (!get_data || ofs > end)
goto done_err;
perfcounter->inc(l_rgw_get_b, end - ofs);
ret = store->get_obj_iterate(s->obj_ctx, &handle, obj, ofs, end, &cb);
perfcounter->tinc(l_rgw_get_lat,
(ceph_clock_now(s->cct) - start_time));
if (ret < 0) {
goto done_err;
}
store->finish_get_obj(&handle);
done_err:
send_response_data(bl, 0, 0);
store->finish_get_obj(&handle);
}
int RGWGetObj::init_common()
{
if (range_str) {
int r = parse_range(range_str, ofs, end, &partial_content);
if (r < 0)
return r;
}
if (if_mod) {
if (parse_time(if_mod, &mod_time) < 0)
return -EINVAL;
mod_ptr = &mod_time;
}
if (if_unmod) {
if (parse_time(if_unmod, &unmod_time) < 0)
return -EINVAL;
unmod_ptr = &unmod_time;
}
return 0;
}
int RGWListBuckets::verify_permission()
{
return 0;
}
void RGWListBuckets::execute()
{
bool done;
bool started = false;
uint64_t total_count = 0;
uint64_t max_buckets = s->cct->_conf->rgw_list_buckets_max_chunk;
ret = get_params();
if (ret < 0)
goto send_end;
do {
RGWUserBuckets buckets;
uint64_t read_count;
if (limit > 0)
read_count = min(limit - total_count, (uint64_t)max_buckets);
else
read_count = max_buckets;
ret = rgw_read_user_buckets(store, s->user.user_id, buckets,
marker, read_count, should_get_stats());
if (!started) {
send_response_begin(buckets.count() > 0);
started = true;
}
if (ret < 0) {
/* hmm.. something wrong here.. the user was authenticated, so it
should exist */
ldout(s->cct, 10) << "WARNING: failed on rgw_get_user_buckets uid=" << s->user.user_id << dendl;
break;
}
map<string, RGWBucketEnt>& m = buckets.get_buckets();
total_count += m.size();
done = (m.size() < read_count || (limit > 0 && total_count == limit));
if (!m.empty()) {
send_response_data(buckets);
map<string, RGWBucketEnt>::reverse_iterator riter = m.rbegin();
marker = riter->first;
}
} while (!done);
send_end:
if (!started) {
send_response_begin(false);
}
send_response_end();
}
int RGWStatAccount::verify_permission()
{
return 0;
}
void RGWStatAccount::execute()