1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
8 #include <boost/tokenizer.hpp>
9 #include <boost/algorithm/string.hpp>
10 #include <boost/utility/string_view.hpp>
12 #include "json_spirit/json_spirit.h"
13 #include "common/ceph_json.h"
16 #include "rgw_common.h"
18 #include "rgw_string.h"
19 #include "rgw_rados.h"
20 #include "rgw_http_errors.h"
22 #include "common/ceph_crypto.h"
23 #include "common/armor.h"
24 #include "common/errno.h"
25 #include "common/Clock.h"
26 #include "common/Formatter.h"
27 #include "common/perf_counters.h"
28 #include "common/strtol.h"
29 #include "include/str_list.h"
30 #include "auth/Crypto.h"
31 #include "rgw_crypt_sanitize.h"
35 #define dout_context g_ceph_context
36 #define dout_subsys ceph_subsys_rgw
39 using boost::optional
;
42 using rgw::IAM::Effect
;
43 using rgw::IAM::op_to_perm
;
44 using rgw::IAM::Policy
;
46 PerfCounters
*perfcounter
= NULL
;
48 const uint32_t RGWBucketInfo::NUM_SHARDS_BLIND_BUCKET(UINT32_MAX
);
50 rgw_http_errors
rgw_http_s3_errors({
52 { STATUS_CREATED
, {201, "Created" }},
53 { STATUS_ACCEPTED
, {202, "Accepted" }},
54 { STATUS_NO_CONTENT
, {204, "NoContent" }},
55 { STATUS_PARTIAL_CONTENT
, {206, "" }},
56 { ERR_PERMANENT_REDIRECT
, {301, "PermanentRedirect" }},
57 { ERR_WEBSITE_REDIRECT
, {301, "WebsiteRedirect" }},
58 { STATUS_REDIRECT
, {303, "" }},
59 { ERR_NOT_MODIFIED
, {304, "NotModified" }},
60 { EINVAL
, {400, "InvalidArgument" }},
61 { ERR_INVALID_REQUEST
, {400, "InvalidRequest" }},
62 { ERR_INVALID_DIGEST
, {400, "InvalidDigest" }},
63 { ERR_BAD_DIGEST
, {400, "BadDigest" }},
64 { ERR_INVALID_LOCATION_CONSTRAINT
, {400, "InvalidLocationConstraint" }},
65 { ERR_INVALID_BUCKET_NAME
, {400, "InvalidBucketName" }},
66 { ERR_INVALID_OBJECT_NAME
, {400, "InvalidObjectName" }},
67 { ERR_UNRESOLVABLE_EMAIL
, {400, "UnresolvableGrantByEmailAddress" }},
68 { ERR_INVALID_PART
, {400, "InvalidPart" }},
69 { ERR_INVALID_PART_ORDER
, {400, "InvalidPartOrder" }},
70 { ERR_REQUEST_TIMEOUT
, {400, "RequestTimeout" }},
71 { ERR_TOO_LARGE
, {400, "EntityTooLarge" }},
72 { ERR_TOO_SMALL
, {400, "EntityTooSmall" }},
73 { ERR_TOO_MANY_BUCKETS
, {400, "TooManyBuckets" }},
74 { ERR_MALFORMED_XML
, {400, "MalformedXML" }},
75 { ERR_AMZ_CONTENT_SHA256_MISMATCH
, {400, "XAmzContentSHA256Mismatch" }},
76 { ERR_LENGTH_REQUIRED
, {411, "MissingContentLength" }},
77 { EACCES
, {403, "AccessDenied" }},
78 { EPERM
, {403, "AccessDenied" }},
79 { ERR_SIGNATURE_NO_MATCH
, {403, "SignatureDoesNotMatch" }},
80 { ERR_INVALID_ACCESS_KEY
, {403, "InvalidAccessKeyId" }},
81 { ERR_USER_SUSPENDED
, {403, "UserSuspended" }},
82 { ERR_REQUEST_TIME_SKEWED
, {403, "RequestTimeTooSkewed" }},
83 { ERR_QUOTA_EXCEEDED
, {403, "QuotaExceeded" }},
84 { ENOENT
, {404, "NoSuchKey" }},
85 { ERR_NO_SUCH_BUCKET
, {404, "NoSuchBucket" }},
86 { ERR_NO_SUCH_WEBSITE_CONFIGURATION
, {404, "NoSuchWebsiteConfiguration" }},
87 { ERR_NO_SUCH_UPLOAD
, {404, "NoSuchUpload" }},
88 { ERR_NOT_FOUND
, {404, "Not Found"}},
89 { ERR_NO_SUCH_LC
, {404, "NoSuchLifecycleConfiguration"}},
90 { ERR_NO_SUCH_BUCKET_POLICY
, {404, "NoSuchBucketPolicy"}},
91 { ERR_METHOD_NOT_ALLOWED
, {405, "MethodNotAllowed" }},
92 { ETIMEDOUT
, {408, "RequestTimeout" }},
93 { EEXIST
, {409, "BucketAlreadyExists" }},
94 { ERR_USER_EXIST
, {409, "UserAlreadyExists" }},
95 { ERR_EMAIL_EXIST
, {409, "EmailExists" }},
96 { ERR_KEY_EXIST
, {409, "KeyExists"}},
97 { ERR_INVALID_SECRET_KEY
, {400, "InvalidSecretKey"}},
98 { ERR_INVALID_KEY_TYPE
, {400, "InvalidKeyType"}},
99 { ERR_INVALID_CAP
, {400, "InvalidCapability"}},
100 { ERR_INVALID_TENANT_NAME
, {400, "InvalidTenantName" }},
101 { ENOTEMPTY
, {409, "BucketNotEmpty" }},
102 { ERR_PRECONDITION_FAILED
, {412, "PreconditionFailed" }},
103 { ERANGE
, {416, "InvalidRange" }},
104 { ERR_UNPROCESSABLE_ENTITY
, {422, "UnprocessableEntity" }},
105 { ERR_LOCKED
, {423, "Locked" }},
106 { ERR_INTERNAL_ERROR
, {500, "InternalError" }},
107 { ERR_NOT_IMPLEMENTED
, {501, "NotImplemented" }},
108 { ERR_SERVICE_UNAVAILABLE
, {503, "ServiceUnavailable"}},
111 rgw_http_errors
rgw_http_swift_errors({
112 { EACCES
, {403, "AccessDenied" }},
113 { EPERM
, {401, "AccessDenied" }},
114 { ERR_USER_SUSPENDED
, {401, "UserSuspended" }},
115 { ERR_INVALID_UTF8
, {412, "Invalid UTF8" }},
116 { ERR_BAD_URL
, {412, "Bad URL" }},
117 { ERR_NOT_SLO_MANIFEST
, {400, "Not an SLO manifest" }},
118 { ERR_QUOTA_EXCEEDED
, {413, "QuotaExceeded" }},
121 int rgw_perf_start(CephContext
*cct
)
123 PerfCountersBuilder
plb(cct
, cct
->_conf
->name
.to_str(), l_rgw_first
, l_rgw_last
);
125 plb
.add_u64_counter(l_rgw_req
, "req", "Requests");
126 plb
.add_u64_counter(l_rgw_failed_req
, "failed_req", "Aborted requests");
128 plb
.add_u64_counter(l_rgw_get
, "get", "Gets");
129 plb
.add_u64_counter(l_rgw_get_b
, "get_b", "Size of gets");
130 plb
.add_time_avg(l_rgw_get_lat
, "get_initial_lat", "Get latency");
131 plb
.add_u64_counter(l_rgw_put
, "put", "Puts");
132 plb
.add_u64_counter(l_rgw_put_b
, "put_b", "Size of puts");
133 plb
.add_time_avg(l_rgw_put_lat
, "put_initial_lat", "Put latency");
135 plb
.add_u64(l_rgw_qlen
, "qlen", "Queue length");
136 plb
.add_u64(l_rgw_qactive
, "qactive", "Active requests queue");
138 plb
.add_u64_counter(l_rgw_cache_hit
, "cache_hit", "Cache hits");
139 plb
.add_u64_counter(l_rgw_cache_miss
, "cache_miss", "Cache miss");
141 plb
.add_u64_counter(l_rgw_keystone_token_cache_hit
, "keystone_token_cache_hit", "Keystone token cache hits");
142 plb
.add_u64_counter(l_rgw_keystone_token_cache_miss
, "keystone_token_cache_miss", "Keystone token cache miss");
144 perfcounter
= plb
.create_perf_counters();
145 cct
->get_perfcounters_collection()->add(perfcounter
);
149 void rgw_perf_stop(CephContext
*cct
)
152 cct
->get_perfcounters_collection()->remove(perfcounter
);
156 using namespace ceph::crypto
;
175 return (http_ret
== 200);
181 return !(http_ret
>= 200 && http_ret
<= 399);
184 // The requestURI transferred from the frontend can be abs_path or absoluteURI
185 // If it is absoluteURI, we should adjust it to abs_path for the following
186 // S3 authorization and some other processes depending on the requestURI
187 // The absoluteURI can start with "http://", "https://", "ws://" or "wss://"
188 static string
get_abs_path(const string
& request_uri
) {
189 const static string ABS_PREFIXS
[] = {"http://", "https://", "ws://", "wss://"};
191 for (int i
= 0; i
< 4; ++i
) {
192 if (boost::algorithm::starts_with(request_uri
, ABS_PREFIXS
[i
])) {
197 if (!isAbs
) { // it is not a valid absolute uri
200 size_t beg_pos
= request_uri
.find("://") + 3;
201 size_t len
= request_uri
.size();
202 beg_pos
= request_uri
.find('/', beg_pos
);
203 if (beg_pos
== string::npos
) return request_uri
;
204 return request_uri
.substr(beg_pos
, len
- beg_pos
);
207 req_info::req_info(CephContext
*cct
, const class RGWEnv
*env
) : env(env
) {
208 method
= env
->get("REQUEST_METHOD", "");
209 script_uri
= env
->get("SCRIPT_URI", cct
->_conf
->rgw_script_uri
.c_str());
210 request_uri
= env
->get("REQUEST_URI", cct
->_conf
->rgw_request_uri
.c_str());
211 if (request_uri
[0] != '/') {
212 request_uri
= get_abs_path(request_uri
);
214 auto pos
= request_uri
.find('?');
215 if (pos
!= string::npos
) {
216 request_params
= request_uri
.substr(pos
+ 1);
217 request_uri
= request_uri
.substr(0, pos
);
219 request_params
= env
->get("QUERY_STRING", "");
221 host
= env
->get("HTTP_HOST", "");
223 // strip off any trailing :port from host (added by CrossFTP and maybe others)
224 size_t colon_offset
= host
.find_last_of(':');
225 if (colon_offset
!= string::npos
) {
226 bool all_digits
= true;
227 for (unsigned i
= colon_offset
+ 1; i
< host
.size(); ++i
) {
228 if (!isdigit(host
[i
])) {
234 host
.resize(colon_offset
);
239 void req_info::rebuild_from(req_info
& src
)
242 script_uri
= src
.script_uri
;
244 if (src
.effective_uri
.empty()) {
245 request_uri
= src
.request_uri
;
247 request_uri
= src
.effective_uri
;
249 effective_uri
.clear();
252 x_meta_map
= src
.x_meta_map
;
253 x_meta_map
.erase("x-amz-date");
257 req_state::req_state(CephContext
* _cct
, RGWEnv
* e
, RGWUserInfo
* u
)
258 : cct(_cct
), cio(NULL
), op(OP_UNKNOWN
), user(u
), has_acl_header(false),
261 enable_ops_log
= e
->conf
.enable_ops_log
;
262 enable_usage_log
= e
->conf
.enable_usage_log
;
263 defer_to_bucket_acls
= e
->conf
.defer_to_bucket_acls
;
264 content_started
= false;
274 system_request
= false;
276 time
= ceph_clock_now();
278 bucket_instance_shard_id
= -1;
280 bucket_exists
= false;
281 has_bad_meta
= false;
283 local_source
= false;
288 req_state::~req_state() {
294 bool search_err(rgw_http_errors
& errs
, int err_no
, bool is_website_redirect
, int& http_ret
, string
& code
)
296 auto r
= errs
.find(err_no
);
297 if (r
!= errs
.end()) {
298 if (! is_website_redirect
)
299 http_ret
= r
->second
.first
;
300 code
= r
->second
.second
;
306 void set_req_state_err(struct rgw_err
& err
, /* out */
308 const int prot_flags
) /* in */
314 bool is_website_redirect
= false;
316 if (prot_flags
& RGW_REST_SWIFT
) {
317 if (search_err(rgw_http_swift_errors
, err_no
, is_website_redirect
, err
.http_ret
, err
.err_code
))
321 //Default to searching in s3 errors
322 is_website_redirect
|= (prot_flags
& RGW_REST_WEBSITE
)
323 && err_no
== ERR_WEBSITE_REDIRECT
&& err
.is_clear();
324 if (search_err(rgw_http_s3_errors
, err_no
, is_website_redirect
, err
.http_ret
, err
.err_code
))
326 dout(0) << "WARNING: set_req_state_err err_no=" << err_no
327 << " resorting to 500" << dendl
;
330 err
.err_code
= "UnknownError";
333 void set_req_state_err(struct req_state
* s
, int err_no
, const string
& err_msg
)
336 set_req_state_err(s
, err_no
);
337 s
->err
.message
= err_msg
;
341 void set_req_state_err(struct req_state
* s
, int err_no
)
344 set_req_state_err(s
->err
, err_no
, s
->prot_flags
);
348 void dump(struct req_state
* s
)
350 if (s
->format
!= RGW_FORMAT_HTML
)
351 s
->formatter
->open_object_section("Error");
352 if (!s
->err
.err_code
.empty())
353 s
->formatter
->dump_string("Code", s
->err
.err_code
);
354 if (!s
->err
.message
.empty())
355 s
->formatter
->dump_string("Message", s
->err
.message
);
356 if (!s
->bucket_name
.empty()) // TODO: connect to expose_bucket
357 s
->formatter
->dump_string("BucketName", s
->bucket_name
);
358 if (!s
->trans_id
.empty()) // TODO: connect to expose_bucket or another toggle
359 s
->formatter
->dump_string("RequestId", s
->trans_id
);
360 s
->formatter
->dump_string("HostId", s
->host_id
);
361 if (s
->format
!= RGW_FORMAT_HTML
)
362 s
->formatter
->close_section();
370 #define STR_LEN_ENTRY(s) { s, sizeof(s) - 1 }
372 struct str_len meta_prefixes
[] = { STR_LEN_ENTRY("HTTP_X_AMZ"),
373 STR_LEN_ENTRY("HTTP_X_GOOG"),
374 STR_LEN_ENTRY("HTTP_X_DHO"),
375 STR_LEN_ENTRY("HTTP_X_RGW"),
376 STR_LEN_ENTRY("HTTP_X_OBJECT"),
377 STR_LEN_ENTRY("HTTP_X_CONTAINER"),
378 STR_LEN_ENTRY("HTTP_X_ACCOUNT"),
382 void req_info::init_meta_info(bool *found_bad_meta
)
386 for (const auto& kv
: env
->get_map()) {
388 const string
& header_name
= kv
.first
;
389 const string
& val
= kv
.second
;
390 for (int prefix_num
= 0; (prefix
= meta_prefixes
[prefix_num
].str
) != NULL
; prefix_num
++) {
391 int len
= meta_prefixes
[prefix_num
].len
;
392 const char *p
= header_name
.c_str();
393 if (strncmp(p
, prefix
, len
) == 0) {
394 dout(10) << "meta>> " << p
<< dendl
;
395 const char *name
= p
+len
; /* skip the prefix */
396 int name_len
= header_name
.size() - len
;
398 if (found_bad_meta
&& strncmp(name
, "_META_", name_len
) == 0)
399 *found_bad_meta
= true;
401 char name_low
[meta_prefixes
[0].len
+ name_len
+ 1];
402 snprintf(name_low
, meta_prefixes
[0].len
- 5 + name_len
+ 1, "%s%s", meta_prefixes
[0].str
+ 5 /* skip HTTP_ */, name
); // normalize meta prefix
404 for (j
= 0; name_low
[j
]; j
++) {
405 if (name_low
[j
] != '_')
406 name_low
[j
] = tolower(name_low
[j
]);
412 auto it
= x_meta_map
.find(name_low
);
413 if (it
!= x_meta_map
.end()) {
414 string old
= it
->second
;
415 boost::algorithm::trim_right(old
);
418 x_meta_map
[name_low
] = old
;
420 x_meta_map
[name_low
] = val
;
425 for (const auto& kv
: x_meta_map
) {
426 dout(10) << "x>> " << kv
.first
<< ":" << rgw::crypt_sanitize::x_meta_map
{kv
.first
, kv
.second
} << dendl
;
430 std::ostream
& operator<<(std::ostream
& oss
, const rgw_err
&err
)
432 oss
<< "rgw_err(http_ret=" << err
.http_ret
<< ", err_code='" << err
.err_code
<< "') ";
436 string
rgw_string_unquote(const string
& s
)
438 if (s
[0] != '"' || s
.size() < 2)
442 for (len
= s
.size(); len
> 2; --len
) {
443 if (s
[len
- 1] != ' ')
450 return s
.substr(1, len
- 2);
453 static bool check_str_end(const char *s
)
466 static bool check_gmt_end(const char *s
)
471 while (isspace(*s
)) {
475 /* check for correct timezone */
476 if ((strncmp(s
, "GMT", 3) != 0) &&
477 (strncmp(s
, "UTC", 3) != 0)) {
484 static bool parse_rfc850(const char *s
, struct tm
*t
)
486 memset(t
, 0, sizeof(*t
));
487 return check_gmt_end(strptime(s
, "%A, %d-%b-%y %H:%M:%S ", t
));
490 static bool parse_asctime(const char *s
, struct tm
*t
)
492 memset(t
, 0, sizeof(*t
));
493 return check_str_end(strptime(s
, "%a %b %d %H:%M:%S %Y", t
));
496 static bool parse_rfc1123(const char *s
, struct tm
*t
)
498 memset(t
, 0, sizeof(*t
));
499 return check_gmt_end(strptime(s
, "%a, %d %b %Y %H:%M:%S ", t
));
502 static bool parse_rfc1123_alt(const char *s
, struct tm
*t
)
504 memset(t
, 0, sizeof(*t
));
505 return check_str_end(strptime(s
, "%a, %d %b %Y %H:%M:%S %z", t
));
508 bool parse_rfc2616(const char *s
, struct tm
*t
)
510 return parse_rfc850(s
, t
) || parse_asctime(s
, t
) || parse_rfc1123(s
, t
) || parse_rfc1123_alt(s
,t
);
513 bool parse_iso8601(const char *s
, struct tm
*t
, uint32_t *pns
, bool extended_format
)
515 memset(t
, 0, sizeof(*t
));
521 if (extended_format
) {
522 p
= strptime(s
, "%Y-%m-%dT%T", t
);
524 p
= strptime(s
, "%Y-%m-%d %T", t
);
527 p
= strptime(s
, "%Y%m%dT%H%M%S", t
);
530 dout(0) << "parse_iso8601 failed" << dendl
;
533 const boost::string_view str
= rgw_trim_whitespace(boost::string_view(p
));
534 int len
= str
.size();
536 if (len
== 0 || (len
== 1 && str
[0] == 'Z'))
544 boost::string_view nsstr
= str
.substr(1, len
- 2);
545 int r
= stringtoul(nsstr
.to_string(), &ms
);
553 if (nsstr
.size() > 9) {
554 nsstr
= nsstr
.substr(0, 9);
557 uint64_t mul_table
[] = { 0,
569 *pns
= ms
* mul_table
[nsstr
.size()];
574 int parse_key_value(string
& in_str
, const char *delim
, string
& key
, string
& val
)
579 auto pos
= in_str
.find(delim
);
580 if (pos
== string::npos
)
583 key
= rgw_trim_whitespace(in_str
.substr(0, pos
));
584 val
= rgw_trim_whitespace(in_str
.substr(pos
+ 1));
589 int parse_key_value(string
& in_str
, string
& key
, string
& val
)
591 return parse_key_value(in_str
, "=", key
,val
);
594 boost::optional
<std::pair
<boost::string_view
, boost::string_view
>>
595 parse_key_value(const boost::string_view
& in_str
,
596 const boost::string_view
& delim
)
598 const size_t pos
= in_str
.find(delim
);
599 if (pos
== boost::string_view::npos
) {
603 const auto key
= rgw_trim_whitespace(in_str
.substr(0, pos
));
604 const auto val
= rgw_trim_whitespace(in_str
.substr(pos
+ 1));
606 return std::make_pair(key
, val
);
609 boost::optional
<std::pair
<boost::string_view
, boost::string_view
>>
610 parse_key_value(const boost::string_view
& in_str
)
612 return parse_key_value(in_str
, "=");
615 int parse_time(const char *time_str
, real_time
*time
)
620 if (!parse_rfc2616(time_str
, &tm
) && !parse_iso8601(time_str
, &tm
, &ns
)) {
624 time_t sec
= internal_timegm(&tm
);
625 *time
= utime_t(sec
, ns
).to_real_time();
630 #define TIME_BUF_SIZE 128
632 void rgw_to_iso8601(const real_time
& t
, char *dest
, int buf_size
)
636 char buf
[TIME_BUF_SIZE
];
638 time_t epoch
= ut
.sec();
639 struct tm
*tmp
= gmtime_r(&epoch
, &result
);
643 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%T", tmp
) == 0)
646 snprintf(dest
, buf_size
, "%s.%03dZ", buf
, (int)(ut
.usec() / 1000));
649 void rgw_to_iso8601(const real_time
& t
, string
*dest
)
651 char buf
[TIME_BUF_SIZE
];
652 rgw_to_iso8601(t
, buf
, sizeof(buf
));
657 * calculate the sha1 value of a given msg and key
659 void calc_hmac_sha1(const char *key
, int key_len
,
660 const char *msg
, int msg_len
, char *dest
)
661 /* destination should be CEPH_CRYPTO_HMACSHA1_DIGESTSIZE bytes long */
663 HMACSHA1
hmac((const unsigned char *)key
, key_len
);
664 hmac
.Update((const unsigned char *)msg
, msg_len
);
665 hmac
.Final((unsigned char *)dest
);
669 * calculate the sha256 value of a given msg and key
671 void calc_hmac_sha256(const char *key
, int key_len
,
672 const char *msg
, int msg_len
, char *dest
)
674 char hash_sha256
[CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
];
676 HMACSHA256
hmac((const unsigned char *)key
, key_len
);
677 hmac
.Update((const unsigned char *)msg
, msg_len
);
678 hmac
.Final((unsigned char *)hash_sha256
);
680 memcpy(dest
, hash_sha256
, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
);
683 using ceph::crypto::SHA256
;
686 * calculate the sha256 hash value of a given msg
688 sha256_digest_t
calc_hash_sha256(const boost::string_view
& msg
)
690 std::array
<unsigned char, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
> hash
;
693 hasher
.Update(reinterpret_cast<const unsigned char*>(msg
.data()), msg
.size());
694 hasher
.Final(hash
.data());
699 SHA256
* calc_hash_sha256_open_stream()
704 void calc_hash_sha256_update_stream(SHA256
*hash
, const char *msg
, int len
)
706 hash
->Update((const unsigned char *)msg
, len
);
709 string
calc_hash_sha256_close_stream(SHA256
**phash
)
711 SHA256
*hash
= *phash
;
713 hash
= calc_hash_sha256_open_stream();
715 char hash_sha256
[CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
];
717 hash
->Final((unsigned char *)hash_sha256
);
719 char hex_str
[(CEPH_CRYPTO_SHA256_DIGESTSIZE
* 2) + 1];
720 buf_to_hex((unsigned char *)hash_sha256
, CEPH_CRYPTO_SHA256_DIGESTSIZE
, hex_str
);
725 return std::string(hex_str
);
728 std::string
calc_hash_sha256_restart_stream(SHA256
**phash
)
730 const auto hash
= calc_hash_sha256_close_stream(phash
);
731 *phash
= calc_hash_sha256_open_stream();
736 int gen_rand_base64(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
739 char tmp_dest
[size
+ 4]; /* so that there's space for the extra '=' characters, and some */
742 ret
= get_random_bytes(buf
, sizeof(buf
));
744 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
748 ret
= ceph_armor(tmp_dest
, &tmp_dest
[sizeof(tmp_dest
)],
749 (const char *)buf
, ((const char *)buf
) + ((size
- 1) * 3 + 4 - 1) / 4);
751 lderr(cct
) << "ceph_armor failed" << dendl
;
754 tmp_dest
[ret
] = '\0';
755 memcpy(dest
, tmp_dest
, size
);
761 static const char alphanum_upper_table
[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
763 int gen_rand_alphanumeric_upper(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
765 int ret
= get_random_bytes(dest
, size
);
767 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
772 for (i
=0; i
<size
- 1; i
++) {
773 int pos
= (unsigned)dest
[i
];
774 dest
[i
] = alphanum_upper_table
[pos
% (sizeof(alphanum_upper_table
) - 1)];
781 static const char alphanum_lower_table
[]="0123456789abcdefghijklmnopqrstuvwxyz";
783 int gen_rand_alphanumeric_lower(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
785 int ret
= get_random_bytes(dest
, size
);
787 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
792 for (i
=0; i
<size
- 1; i
++) {
793 int pos
= (unsigned)dest
[i
];
794 dest
[i
] = alphanum_lower_table
[pos
% (sizeof(alphanum_lower_table
) - 1)];
801 int gen_rand_alphanumeric_lower(CephContext
*cct
, string
*str
, int length
)
803 char buf
[length
+ 1];
804 int ret
= gen_rand_alphanumeric_lower(cct
, buf
, sizeof(buf
));
812 // this is basically a modified base64 charset, url friendly
813 static const char alphanum_table
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
815 int gen_rand_alphanumeric(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
817 int ret
= get_random_bytes(dest
, size
);
819 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
824 for (i
=0; i
<size
- 1; i
++) {
825 int pos
= (unsigned)dest
[i
];
826 dest
[i
] = alphanum_table
[pos
& 63];
833 static const char alphanum_no_underscore_table
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
835 int gen_rand_alphanumeric_no_underscore(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
837 int ret
= get_random_bytes(dest
, size
);
839 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
844 for (i
=0; i
<size
- 1; i
++) {
845 int pos
= (unsigned)dest
[i
];
846 dest
[i
] = alphanum_no_underscore_table
[pos
& 63];
853 static const char alphanum_plain_table
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
855 int gen_rand_alphanumeric_plain(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
857 int ret
= get_random_bytes(dest
, size
);
859 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
864 for (i
=0; i
<size
- 1; i
++) {
865 int pos
= (unsigned)dest
[i
];
866 dest
[i
] = alphanum_plain_table
[pos
% (sizeof(alphanum_plain_table
) - 1)];
875 auto delim_pos
= str
.find('=');
878 if (delim_pos
== string::npos
) {
883 name
= str
.substr(0, delim_pos
);
884 val
= str
.substr(delim_pos
+ 1);
890 int RGWHTTPArgs::parse()
902 int fpos
= str
.find('&', pos
);
907 std::string nameval
= url_decode(str
.substr(pos
, fpos
- pos
), true);
908 NameVal
nv(std::move(nameval
));
909 int ret
= nv
.parse();
911 string
& name
= nv
.get_name();
912 string
& val
= nv
.get_val();
923 void RGWHTTPArgs::append(const string
& name
, const string
& val
)
925 if (name
.compare(0, sizeof(RGW_SYS_PARAM_PREFIX
) - 1, RGW_SYS_PARAM_PREFIX
) == 0) {
926 sys_val_map
[name
] = val
;
931 if ((name
.compare("acl") == 0) ||
932 (name
.compare("cors") == 0) ||
933 (name
.compare("location") == 0) ||
934 (name
.compare("logging") == 0) ||
935 (name
.compare("usage") == 0) ||
936 (name
.compare("lifecycle") == 0) ||
937 (name
.compare("delete") == 0) ||
938 (name
.compare("uploads") == 0) ||
939 (name
.compare("partNumber") == 0) ||
940 (name
.compare("uploadId") == 0) ||
941 (name
.compare("versionId") == 0) ||
942 (name
.compare("start-date") == 0) ||
943 (name
.compare("end-date") == 0) ||
944 (name
.compare("versions") == 0) ||
945 (name
.compare("versioning") == 0) ||
946 (name
.compare("website") == 0) ||
947 (name
.compare("requestPayment") == 0) ||
948 (name
.compare("torrent") == 0)) {
949 sub_resources
[name
] = val
;
950 } else if (name
[0] == 'r') { // root of all evil
951 if ((name
.compare("response-content-type") == 0) ||
952 (name
.compare("response-content-language") == 0) ||
953 (name
.compare("response-expires") == 0) ||
954 (name
.compare("response-cache-control") == 0) ||
955 (name
.compare("response-content-disposition") == 0) ||
956 (name
.compare("response-content-encoding") == 0)) {
957 sub_resources
[name
] = val
;
958 has_resp_modifier
= true;
960 } else if ((name
.compare("subuser") == 0) ||
961 (name
.compare("key") == 0) ||
962 (name
.compare("caps") == 0) ||
963 (name
.compare("index") == 0) ||
964 (name
.compare("policy") == 0) ||
965 (name
.compare("quota") == 0) ||
966 (name
.compare("object") == 0)) {
968 if (!admin_subresource_added
) {
969 sub_resources
[name
] = "";
970 admin_subresource_added
= true;
975 const string
& RGWHTTPArgs::get(const string
& name
, bool *exists
) const
977 auto iter
= val_map
.find(name
);
978 bool e
= (iter
!= std::end(val_map
));
986 int RGWHTTPArgs::get_bool(const string
& name
, bool *val
, bool *exists
)
988 map
<string
, string
>::iterator iter
;
989 iter
= val_map
.find(name
);
990 bool e
= (iter
!= val_map
.end());
995 const char *s
= iter
->second
.c_str();
997 if (strcasecmp(s
, "false") == 0) {
999 } else if (strcasecmp(s
, "true") == 0) {
1009 int RGWHTTPArgs::get_bool(const char *name
, bool *val
, bool *exists
)
1012 return get_bool(s
, val
, exists
);
1015 void RGWHTTPArgs::get_bool(const char *name
, bool *val
, bool def_val
)
1017 bool exists
= false;
1018 if ((get_bool(name
, val
, &exists
) < 0) ||
1024 string
RGWHTTPArgs::sys_get(const string
& name
, bool * const exists
) const
1026 const auto iter
= sys_val_map
.find(name
);
1027 const bool e
= (iter
!= sys_val_map
.end());
1033 return e
? iter
->second
: string();
1036 bool verify_user_permission(struct req_state
* const s
,
1037 RGWAccessControlPolicy
* const user_acl
,
1040 /* S3 doesn't support account ACLs. */
1044 if ((perm
& (int)s
->perm_mask
) != perm
)
1047 return user_acl
->verify_permission(*s
->auth
.identity
, perm
, perm
);
1050 bool verify_user_permission(struct req_state
* const s
,
1053 return verify_user_permission(s
, s
->user_acl
.get(), perm
);
1056 bool verify_requester_payer_permission(struct req_state
*s
)
1058 if (!s
->bucket_info
.requester_pays
)
1061 if (s
->auth
.identity
->is_owner_of(s
->bucket_info
.owner
))
1064 if (s
->auth
.identity
->is_anonymous()) {
1068 const char *request_payer
= s
->info
.env
->get("HTTP_X_AMZ_REQUEST_PAYER");
1069 if (!request_payer
) {
1071 request_payer
= s
->info
.args
.get("x-amz-request-payer", &exists
).c_str();
1077 if (strcasecmp(request_payer
, "requester") == 0) {
1084 bool verify_bucket_permission(struct req_state
* const s
,
1085 const rgw_bucket
& bucket
,
1086 RGWAccessControlPolicy
* const user_acl
,
1087 RGWAccessControlPolicy
* const bucket_acl
,
1088 const optional
<Policy
>& bucket_policy
,
1091 if (!verify_requester_payer_permission(s
))
1094 if (bucket_policy
) {
1095 auto r
= bucket_policy
->eval(s
->env
, *s
->auth
.identity
, op
, ARN(bucket
));
1096 if (r
== Effect::Allow
)
1097 // It looks like S3 ACLs only GRANT permissions rather than
1098 // denying them, so this should be safe.
1100 else if (r
== Effect::Deny
)
1104 const auto perm
= op_to_perm(op
);
1106 return verify_bucket_permission_no_policy(s
, user_acl
, bucket_acl
, perm
);
1109 bool verify_bucket_permission_no_policy(struct req_state
* const s
,
1110 RGWAccessControlPolicy
* const user_acl
,
1111 RGWAccessControlPolicy
* const bucket_acl
,
1117 if ((perm
& (int)s
->perm_mask
) != perm
)
1120 if (bucket_acl
->verify_permission(*s
->auth
.identity
, perm
, perm
,
1121 s
->info
.env
->get("HTTP_REFERER")))
1127 return user_acl
->verify_permission(*s
->auth
.identity
, perm
, perm
);
1130 bool verify_bucket_permission_no_policy(struct req_state
* const s
, const int perm
)
1132 if (!verify_requester_payer_permission(s
))
1135 return verify_bucket_permission_no_policy(s
,
1141 bool verify_bucket_permission(struct req_state
* const s
, const uint64_t op
)
1143 return verify_bucket_permission(s
,
1151 static inline bool check_deferred_bucket_perms(struct req_state
* const s
,
1152 const rgw_bucket
& bucket
,
1153 RGWAccessControlPolicy
* const user_acl
,
1154 RGWAccessControlPolicy
* const bucket_acl
,
1155 const optional
<Policy
>& bucket_policy
,
1156 const uint8_t deferred_check
,
1159 return (s
->defer_to_bucket_acls
== deferred_check \
1160 && verify_bucket_permission(s
, bucket
, user_acl
, bucket_acl
, bucket_policy
, op
));
1163 static inline bool check_deferred_bucket_only_acl(struct req_state
* const s
,
1164 RGWAccessControlPolicy
* const user_acl
,
1165 RGWAccessControlPolicy
* const bucket_acl
,
1166 const uint8_t deferred_check
,
1169 return (s
->defer_to_bucket_acls
== deferred_check \
1170 && verify_bucket_permission_no_policy(s
, user_acl
, bucket_acl
, perm
));
1173 bool verify_object_permission(struct req_state
* const s
,
1175 RGWAccessControlPolicy
* const user_acl
,
1176 RGWAccessControlPolicy
* const bucket_acl
,
1177 RGWAccessControlPolicy
* const object_acl
,
1178 const optional
<Policy
>& bucket_policy
,
1181 if (!verify_requester_payer_permission(s
))
1184 if (bucket_policy
) {
1185 auto r
= bucket_policy
->eval(s
->env
, *s
->auth
.identity
, op
, ARN(obj
));
1186 if (r
== Effect::Allow
)
1187 // It looks like S3 ACLs only GRANT permissions rather than
1188 // denying them, so this should be safe.
1190 else if (r
== Effect::Deny
)
1194 const auto perm
= op_to_perm(op
);
1196 if (check_deferred_bucket_perms(s
, obj
.bucket
, user_acl
, bucket_acl
, bucket_policy
,
1197 RGW_DEFER_TO_BUCKET_ACLS_RECURSE
, op
) ||
1198 check_deferred_bucket_perms(s
, obj
.bucket
, user_acl
, bucket_acl
, bucket_policy
,
1199 RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL
, rgw::IAM::s3All
)) {
1207 bool ret
= object_acl
->verify_permission(*s
->auth
.identity
, s
->perm_mask
, perm
);
1212 if (!s
->cct
->_conf
->rgw_enforce_swift_acls
)
1215 if ((perm
& (int)s
->perm_mask
) != perm
)
1219 if (perm
& (RGW_PERM_READ
| RGW_PERM_READ_ACP
))
1220 swift_perm
|= RGW_PERM_READ_OBJS
;
1221 if (perm
& RGW_PERM_WRITE
)
1222 swift_perm
|= RGW_PERM_WRITE_OBJS
;
1227 /* we already verified the user mask above, so we pass swift_perm as the mask here,
1228 otherwise the mask might not cover the swift permissions bits */
1229 if (bucket_acl
->verify_permission(*s
->auth
.identity
, swift_perm
, swift_perm
,
1230 s
->info
.env
->get("HTTP_REFERER")))
1236 return user_acl
->verify_permission(*s
->auth
.identity
, swift_perm
, swift_perm
);
1239 bool verify_object_permission_no_policy(struct req_state
* const s
,
1240 RGWAccessControlPolicy
* const user_acl
,
1241 RGWAccessControlPolicy
* const bucket_acl
,
1242 RGWAccessControlPolicy
* const object_acl
,
1245 if (check_deferred_bucket_only_acl(s
, user_acl
, bucket_acl
, RGW_DEFER_TO_BUCKET_ACLS_RECURSE
, perm
) ||
1246 check_deferred_bucket_only_acl(s
, user_acl
, bucket_acl
, RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL
, RGW_PERM_FULL_CONTROL
)) {
1254 bool ret
= object_acl
->verify_permission(*s
->auth
.identity
, s
->perm_mask
, perm
);
1259 if (!s
->cct
->_conf
->rgw_enforce_swift_acls
)
1262 if ((perm
& (int)s
->perm_mask
) != perm
)
1266 if (perm
& (RGW_PERM_READ
| RGW_PERM_READ_ACP
))
1267 swift_perm
|= RGW_PERM_READ_OBJS
;
1268 if (perm
& RGW_PERM_WRITE
)
1269 swift_perm
|= RGW_PERM_WRITE_OBJS
;
1274 /* we already verified the user mask above, so we pass swift_perm as the mask here,
1275 otherwise the mask might not cover the swift permissions bits */
1276 if (bucket_acl
->verify_permission(*s
->auth
.identity
, swift_perm
, swift_perm
,
1277 s
->info
.env
->get("HTTP_REFERER")))
1283 return user_acl
->verify_permission(*s
->auth
.identity
, swift_perm
, swift_perm
);
1286 bool verify_object_permission_no_policy(struct req_state
*s
, int perm
)
1288 if (!verify_requester_payer_permission(s
))
1291 return verify_object_permission_no_policy(s
, s
->user_acl
.get(),
1292 s
->bucket_acl
, s
->object_acl
,
1296 bool verify_object_permission(struct req_state
*s
, uint64_t op
)
1298 return verify_object_permission(s
,
1299 rgw_obj(s
->bucket
, s
->object
),
1313 memset(table
, -1, sizeof(table
));
1315 for (i
= '0'; i
<='9'; i
++)
1317 for (i
= 'A'; i
<='F'; i
++)
1318 table
[i
] = i
- 'A' + 0xa;
1319 for (i
= 'a'; i
<='f'; i
++)
1320 table
[i
] = i
- 'a' + 0xa;
1323 char to_num(char c
) {
1324 return table
[(int)c
];
1328 static char hex_to_num(char c
)
1330 static HexTable hex_table
;
1331 return hex_table
.to_num(c
);
1334 std::string
url_decode(const boost::string_view
& src_str
, bool in_query
)
1336 std::string dest_str
;
1337 dest_str
.reserve(src_str
.length() + 1);
1339 for (auto src
= std::begin(src_str
); src
!= std::end(src_str
); ++src
) {
1341 if (!in_query
|| *src
!= '+') {
1345 dest_str
.push_back(*src
);
1347 dest_str
.push_back(' ');
1350 /* 3 == strlen("%%XX") */
1351 if (std::distance(src
, std::end(src_str
)) < 3) {
1356 const char c1
= hex_to_num(*src
++);
1357 const char c2
= hex_to_num(*src
);
1358 if (c1
< 0 || c2
< 0) {
1359 return std::string();
1361 dest_str
.push_back(c1
<< 4 | c2
);
1369 void rgw_uri_escape_char(char c
, string
& dst
)
1372 snprintf(buf
, sizeof(buf
), "%%%.2X", (int)(unsigned char)c
);
1376 static bool char_needs_url_encoding(char c
)
1378 if (c
<= 0x20 || c
>= 0x7f)
1408 void url_encode(const string
& src
, string
& dst
)
1410 const char *p
= src
.c_str();
1411 for (unsigned i
= 0; i
< src
.size(); i
++, p
++) {
1412 if (char_needs_url_encoding(*p
)) {
1413 rgw_uri_escape_char(*p
, dst
);
1421 std::string
url_encode(const std::string
& src
)
1424 url_encode(src
, dst
);
1429 string
rgw_trim_whitespace(const string
& src
)
1436 for (; start
!= (int)src
.size(); start
++) {
1437 if (!isspace(src
[start
]))
1441 int end
= src
.size() - 1;
1446 for (; end
> start
; end
--) {
1447 if (!isspace(src
[end
]))
1451 return src
.substr(start
, end
- start
+ 1);
1454 boost::string_view
rgw_trim_whitespace(const boost::string_view
& src
)
1456 boost::string_view res
= src
;
1458 while (res
.size() > 0 && std::isspace(res
.front())) {
1459 res
.remove_prefix(1);
1461 while (res
.size() > 0 && std::isspace(res
.back())) {
1462 res
.remove_suffix(1);
1467 string
rgw_trim_quotes(const string
& val
)
1469 string s
= rgw_trim_whitespace(val
);
1474 int end
= s
.size() - 1;
1475 int quotes_count
= 0;
1477 if (s
[start
] == '"') {
1481 if (s
[end
] == '"') {
1485 if (quotes_count
== 2) {
1486 return s
.substr(start
, end
- start
+ 1);
1491 struct rgw_name_to_flag
{
1492 const char *type_name
;
1496 static int parse_list_of_flags(struct rgw_name_to_flag
*mapping
,
1497 const string
& str
, uint32_t *perm
)
1500 get_str_list(str
, strs
);
1501 list
<string
>::iterator iter
;
1503 for (iter
= strs
.begin(); iter
!= strs
.end(); ++iter
) {
1505 for (int i
= 0; mapping
[i
].type_name
; i
++) {
1506 if (s
.compare(mapping
[i
].type_name
) == 0)
1507 v
|= mapping
[i
].flag
;
1515 static struct rgw_name_to_flag cap_names
[] = { {"*", RGW_CAP_ALL
},
1516 {"read", RGW_CAP_READ
},
1517 {"write", RGW_CAP_WRITE
},
1520 int RGWUserCaps::parse_cap_perm(const string
& str
, uint32_t *perm
)
1522 return parse_list_of_flags(cap_names
, str
, perm
);
1525 int RGWUserCaps::get_cap(const string
& cap
, string
& type
, uint32_t *pperm
)
1527 int pos
= cap
.find('=');
1529 type
= rgw_trim_whitespace(cap
.substr(0, pos
));
1532 if (!is_valid_cap_type(type
))
1533 return -ERR_INVALID_CAP
;
1537 if (pos
< (int)cap
.size() - 1) {
1538 cap_perm
= cap
.substr(pos
+ 1);
1539 int r
= RGWUserCaps::parse_cap_perm(cap_perm
, &perm
);
1549 int RGWUserCaps::add_cap(const string
& cap
)
1554 int r
= get_cap(cap
, type
, &perm
);
1563 int RGWUserCaps::remove_cap(const string
& cap
)
1568 int r
= get_cap(cap
, type
, &perm
);
1572 map
<string
, uint32_t>::iterator iter
= caps
.find(type
);
1573 if (iter
== caps
.end())
1576 uint32_t& old_perm
= iter
->second
;
1584 int RGWUserCaps::add_from_string(const string
& str
)
1588 auto end
= str
.find(';', start
);
1589 if (end
== string::npos
)
1592 int r
= add_cap(str
.substr(start
, end
- start
));
1597 } while (start
< (int)str
.size());
1602 int RGWUserCaps::remove_from_string(const string
& str
)
1606 auto end
= str
.find(';', start
);
1607 if (end
== string::npos
)
1610 int r
= remove_cap(str
.substr(start
, end
- start
));
1615 } while (start
< (int)str
.size());
1620 void RGWUserCaps::dump(Formatter
*f
) const
1625 void RGWUserCaps::dump(Formatter
*f
, const char *name
) const
1627 f
->open_array_section(name
);
1628 map
<string
, uint32_t>::const_iterator iter
;
1629 for (iter
= caps
.begin(); iter
!= caps
.end(); ++iter
)
1631 f
->open_object_section("cap");
1632 f
->dump_string("type", iter
->first
);
1633 uint32_t perm
= iter
->second
;
1635 for (int i
=0; cap_names
[i
].type_name
; i
++) {
1636 if ((perm
& cap_names
[i
].flag
) == cap_names
[i
].flag
) {
1637 if (perm_str
.size())
1638 perm_str
.append(", ");
1640 perm_str
.append(cap_names
[i
].type_name
);
1641 perm
&= ~cap_names
[i
].flag
;
1644 if (perm_str
.empty())
1645 perm_str
= "<none>";
1647 f
->dump_string("perm", perm_str
);
1658 void decode_json(JSONObj
*obj
) {
1659 JSONDecoder::decode_json("type", type
, obj
);
1661 JSONDecoder::decode_json("perm", perm_str
, obj
);
1662 if (RGWUserCaps::parse_cap_perm(perm_str
, &perm
) < 0) {
1663 throw JSONDecoder::err("failed to parse permissions");
1668 void RGWUserCaps::decode_json(JSONObj
*obj
)
1670 list
<RGWUserCap
> caps_list
;
1671 decode_json_obj(caps_list
, obj
);
1673 list
<RGWUserCap
>::iterator iter
;
1674 for (iter
= caps_list
.begin(); iter
!= caps_list
.end(); ++iter
) {
1675 RGWUserCap
& cap
= *iter
;
1676 caps
[cap
.type
] = cap
.perm
;
1680 int RGWUserCaps::check_cap(const string
& cap
, uint32_t perm
)
1682 map
<string
, uint32_t>::iterator iter
= caps
.find(cap
);
1684 if ((iter
== caps
.end()) ||
1685 (iter
->second
& perm
) != perm
) {
1692 bool RGWUserCaps::is_valid_cap_type(const string
& tp
)
1694 static const char *cap_type
[] = { "user",
1705 for (unsigned int i
= 0; i
< sizeof(cap_type
) / sizeof(char *); ++i
) {
1706 if (tp
.compare(cap_type
[i
]) == 0) {
1714 static ssize_t
unescape_str(const string
& s
, ssize_t ofs
, char esc_char
, char special_char
, string
*dest
)
1716 const char *src
= s
.c_str();
1717 char dest_buf
[s
.size() + 1];
1718 char *destp
= dest_buf
;
1723 for (size_t i
= ofs
; i
< s
.size(); i
++) {
1725 if (!esc
&& c
== esc_char
) {
1729 if (!esc
&& c
== special_char
) {
1732 return (ssize_t
)i
+ 1;
1739 return string::npos
;
1742 static void escape_str(const string
& s
, char esc_char
, char special_char
, string
*dest
)
1744 const char *src
= s
.c_str();
1745 char dest_buf
[s
.size() * 2 + 1];
1746 char *destp
= dest_buf
;
1748 for (size_t i
= 0; i
< s
.size(); i
++) {
1750 if (c
== esc_char
|| c
== special_char
) {
1751 *destp
++ = esc_char
;
1759 void rgw_pool::from_str(const string
& s
)
1761 size_t pos
= unescape_str(s
, 0, '\\', ':', &name
);
1762 if (pos
!= string::npos
) {
1763 pos
= unescape_str(s
, pos
, '\\', ':', &ns
);
1764 /* ignore return; if pos != string::npos it means that we had a colon
1765 * in the middle of ns that wasn't escaped, we're going to stop there
1770 string
rgw_pool::to_str() const
1773 escape_str(name
, '\\', ':', &esc_name
);
1778 escape_str(ns
, '\\', ':', &esc_ns
);
1779 return esc_name
+ ":" + esc_ns
;
1782 void rgw_raw_obj::decode_from_rgw_obj(bufferlist::iterator
& bl
)
1785 ::decode(old_obj
, bl
);
1787 get_obj_bucket_and_oid_loc(old_obj
, oid
, loc
);
1788 pool
= old_obj
.get_explicit_data_pool();
1791 std::string
rgw_bucket::get_key(char tenant_delim
, char id_delim
) const
1793 static constexpr size_t shard_len
{12}; // ":4294967295\0"
1794 const size_t max_len
= tenant
.size() + sizeof(tenant_delim
) +
1795 name
.size() + sizeof(id_delim
) + bucket_id
.size() + shard_len
;
1798 key
.reserve(max_len
);
1799 if (!tenant
.empty() && tenant_delim
) {
1801 key
.append(1, tenant_delim
);
1804 if (!bucket_id
.empty() && id_delim
) {
1805 key
.append(1, id_delim
);
1806 key
.append(bucket_id
);
1811 std::string
rgw_bucket_shard::get_key(char tenant_delim
, char id_delim
,
1812 char shard_delim
) const
1814 auto key
= bucket
.get_key(tenant_delim
, id_delim
);
1815 if (shard_id
>= 0 && shard_delim
) {
1816 key
.append(1, shard_delim
);
1817 key
.append(std::to_string(shard_id
));
1822 static struct rgw_name_to_flag op_type_mapping
[] = { {"*", RGW_OP_TYPE_ALL
},
1823 {"read", RGW_OP_TYPE_READ
},
1824 {"write", RGW_OP_TYPE_WRITE
},
1825 {"delete", RGW_OP_TYPE_DELETE
},
1829 int rgw_parse_op_type_list(const string
& str
, uint32_t *perm
)
1831 return parse_list_of_flags(op_type_mapping
, str
, perm
);
1834 static int match_internal(boost::string_ref pattern
, boost::string_ref input
, int (*function
)(const char&, const char&))
1836 boost::string_ref::iterator it1
= pattern
.begin();
1837 boost::string_ref::iterator it2
= input
.begin();
1839 if (it1
== pattern
.end() && it2
== input
.end())
1841 if (it1
== pattern
.end() || it2
== input
.end())
1843 if (*it1
== '*' && (it1
+ 1) == pattern
.end() && it2
!= input
.end())
1845 if (*it1
== '*' && (it1
+ 1) == pattern
.end() && it2
== input
.end())
1847 if (function(*it1
, *it2
) || *it1
== '?') {
1853 if (function(*(it1
+ 1), *it2
))
1864 static int matchcase(const char& c1
, const char& c2
)
1871 static int matchignorecase(const char& c1
, const char& c2
)
1873 if (tolower(c1
) == tolower(c2
))
1878 int match(const string
& pattern
, const string
& input
, uint32_t flag
)
1880 auto last_pos_input
= 0, last_pos_pattern
= 0;
1883 auto cur_pos_input
= input
.find(":", last_pos_input
);
1884 auto cur_pos_pattern
= pattern
.find(":", last_pos_pattern
);
1886 string substr_input
= input
.substr(last_pos_input
, cur_pos_input
);
1887 string substr_pattern
= pattern
.substr(last_pos_pattern
, cur_pos_pattern
);
1890 if (substr_pattern
== "*") {
1892 } else if (flag
& MATCH_POLICY_ACTION
|| flag
& MATCH_POLICY_ARN
) {
1893 res
= match_internal(substr_pattern
, substr_input
, &matchignorecase
);
1895 res
= match_internal(substr_pattern
, substr_input
, &matchcase
);
1900 if (cur_pos_pattern
== string::npos
&& cur_pos_input
== string::npos
)
1902 else if ((cur_pos_pattern
== string::npos
&& cur_pos_input
!= string::npos
) ||
1903 (cur_pos_pattern
!= string::npos
&& cur_pos_input
== string::npos
))
1906 last_pos_pattern
= cur_pos_pattern
+ 1;
1907 last_pos_input
= cur_pos_input
+ 1;