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_INVALID_TAG
, {400, "InvalidTag"}},
77 { ERR_LENGTH_REQUIRED
, {411, "MissingContentLength" }},
78 { EACCES
, {403, "AccessDenied" }},
79 { EPERM
, {403, "AccessDenied" }},
80 { ERR_SIGNATURE_NO_MATCH
, {403, "SignatureDoesNotMatch" }},
81 { ERR_INVALID_ACCESS_KEY
, {403, "InvalidAccessKeyId" }},
82 { ERR_USER_SUSPENDED
, {403, "UserSuspended" }},
83 { ERR_REQUEST_TIME_SKEWED
, {403, "RequestTimeTooSkewed" }},
84 { ERR_QUOTA_EXCEEDED
, {403, "QuotaExceeded" }},
85 { ENOENT
, {404, "NoSuchKey" }},
86 { ERR_NO_SUCH_BUCKET
, {404, "NoSuchBucket" }},
87 { ERR_NO_SUCH_WEBSITE_CONFIGURATION
, {404, "NoSuchWebsiteConfiguration" }},
88 { ERR_NO_SUCH_UPLOAD
, {404, "NoSuchUpload" }},
89 { ERR_NOT_FOUND
, {404, "Not Found"}},
90 { ERR_NO_SUCH_LC
, {404, "NoSuchLifecycleConfiguration"}},
91 { ERR_NO_SUCH_BUCKET_POLICY
, {404, "NoSuchBucketPolicy"}},
92 { ERR_NO_SUCH_USER
, {404, "NoSuchUser"}},
93 { ERR_NO_SUCH_SUBUSER
, {404, "NoSuchSubUser"}},
94 { ERR_METHOD_NOT_ALLOWED
, {405, "MethodNotAllowed" }},
95 { ETIMEDOUT
, {408, "RequestTimeout" }},
96 { EEXIST
, {409, "BucketAlreadyExists" }},
97 { ERR_USER_EXIST
, {409, "UserAlreadyExists" }},
98 { ERR_EMAIL_EXIST
, {409, "EmailExists" }},
99 { ERR_KEY_EXIST
, {409, "KeyExists"}},
100 { ERR_TAG_CONFLICT
, {409, "OperationAborted"}},
101 { ERR_INVALID_SECRET_KEY
, {400, "InvalidSecretKey"}},
102 { ERR_INVALID_KEY_TYPE
, {400, "InvalidKeyType"}},
103 { ERR_INVALID_CAP
, {400, "InvalidCapability"}},
104 { ERR_INVALID_TENANT_NAME
, {400, "InvalidTenantName" }},
105 { ENOTEMPTY
, {409, "BucketNotEmpty" }},
106 { ERR_PRECONDITION_FAILED
, {412, "PreconditionFailed" }},
107 { ERANGE
, {416, "InvalidRange" }},
108 { ERR_UNPROCESSABLE_ENTITY
, {422, "UnprocessableEntity" }},
109 { ERR_LOCKED
, {423, "Locked" }},
110 { ERR_INTERNAL_ERROR
, {500, "InternalError" }},
111 { ERR_NOT_IMPLEMENTED
, {501, "NotImplemented" }},
112 { ERR_SERVICE_UNAVAILABLE
, {503, "ServiceUnavailable"}},
113 { ERR_ZERO_IN_URL
, {400, "InvalidRequest" }},
116 rgw_http_errors
rgw_http_swift_errors({
117 { EACCES
, {403, "AccessDenied" }},
118 { EPERM
, {401, "AccessDenied" }},
119 { ERR_USER_SUSPENDED
, {401, "UserSuspended" }},
120 { ERR_INVALID_UTF8
, {412, "Invalid UTF8" }},
121 { ERR_BAD_URL
, {412, "Bad URL" }},
122 { ERR_NOT_SLO_MANIFEST
, {400, "Not an SLO manifest" }},
123 { ERR_QUOTA_EXCEEDED
, {413, "QuotaExceeded" }},
124 /* FIXME(rzarzynski): we need to find a way to apply Swift's error handling
125 * procedures also for ERR_ZERO_IN_URL. This make a problem as the validation
126 * is performed very early, even before setting the req_state::proto_flags. */
127 { ERR_ZERO_IN_URL
, {412, "Invalid UTF8 or contains NULL"}},
130 int rgw_perf_start(CephContext
*cct
)
132 PerfCountersBuilder
plb(cct
, cct
->_conf
->name
.to_str(), l_rgw_first
, l_rgw_last
);
134 plb
.add_u64_counter(l_rgw_req
, "req", "Requests");
135 plb
.add_u64_counter(l_rgw_failed_req
, "failed_req", "Aborted requests");
137 plb
.add_u64_counter(l_rgw_get
, "get", "Gets");
138 plb
.add_u64_counter(l_rgw_get_b
, "get_b", "Size of gets");
139 plb
.add_time_avg(l_rgw_get_lat
, "get_initial_lat", "Get latency");
140 plb
.add_u64_counter(l_rgw_put
, "put", "Puts");
141 plb
.add_u64_counter(l_rgw_put_b
, "put_b", "Size of puts");
142 plb
.add_time_avg(l_rgw_put_lat
, "put_initial_lat", "Put latency");
144 plb
.add_u64(l_rgw_qlen
, "qlen", "Queue length");
145 plb
.add_u64(l_rgw_qactive
, "qactive", "Active requests queue");
147 plb
.add_u64_counter(l_rgw_cache_hit
, "cache_hit", "Cache hits");
148 plb
.add_u64_counter(l_rgw_cache_miss
, "cache_miss", "Cache miss");
150 plb
.add_u64_counter(l_rgw_keystone_token_cache_hit
, "keystone_token_cache_hit", "Keystone token cache hits");
151 plb
.add_u64_counter(l_rgw_keystone_token_cache_miss
, "keystone_token_cache_miss", "Keystone token cache miss");
153 perfcounter
= plb
.create_perf_counters();
154 cct
->get_perfcounters_collection()->add(perfcounter
);
158 void rgw_perf_stop(CephContext
*cct
)
161 cct
->get_perfcounters_collection()->remove(perfcounter
);
165 using namespace ceph::crypto
;
184 return (http_ret
== 200);
190 return !(http_ret
>= 200 && http_ret
<= 399);
193 // The requestURI transferred from the frontend can be abs_path or absoluteURI
194 // If it is absoluteURI, we should adjust it to abs_path for the following
195 // S3 authorization and some other processes depending on the requestURI
196 // The absoluteURI can start with "http://", "https://", "ws://" or "wss://"
197 static string
get_abs_path(const string
& request_uri
) {
198 const static string ABS_PREFIXS
[] = {"http://", "https://", "ws://", "wss://"};
200 for (int i
= 0; i
< 4; ++i
) {
201 if (boost::algorithm::starts_with(request_uri
, ABS_PREFIXS
[i
])) {
206 if (!isAbs
) { // it is not a valid absolute uri
209 size_t beg_pos
= request_uri
.find("://") + 3;
210 size_t len
= request_uri
.size();
211 beg_pos
= request_uri
.find('/', beg_pos
);
212 if (beg_pos
== string::npos
) return request_uri
;
213 return request_uri
.substr(beg_pos
, len
- beg_pos
);
216 req_info::req_info(CephContext
*cct
, const class RGWEnv
*env
) : env(env
) {
217 method
= env
->get("REQUEST_METHOD", "");
218 script_uri
= env
->get("SCRIPT_URI", cct
->_conf
->rgw_script_uri
.c_str());
219 request_uri
= env
->get("REQUEST_URI", cct
->_conf
->rgw_request_uri
.c_str());
220 if (request_uri
[0] != '/') {
221 request_uri
= get_abs_path(request_uri
);
223 auto pos
= request_uri
.find('?');
224 if (pos
!= string::npos
) {
225 request_params
= request_uri
.substr(pos
+ 1);
226 request_uri
= request_uri
.substr(0, pos
);
228 request_params
= env
->get("QUERY_STRING", "");
230 host
= env
->get("HTTP_HOST", "");
232 // strip off any trailing :port from host (added by CrossFTP and maybe others)
233 size_t colon_offset
= host
.find_last_of(':');
234 if (colon_offset
!= string::npos
) {
235 bool all_digits
= true;
236 for (unsigned i
= colon_offset
+ 1; i
< host
.size(); ++i
) {
237 if (!isdigit(host
[i
])) {
243 host
.resize(colon_offset
);
248 void req_info::rebuild_from(req_info
& src
)
251 script_uri
= src
.script_uri
;
253 if (src
.effective_uri
.empty()) {
254 request_uri
= src
.request_uri
;
256 request_uri
= src
.effective_uri
;
258 effective_uri
.clear();
261 x_meta_map
= src
.x_meta_map
;
262 x_meta_map
.erase("x-amz-date");
266 req_state::req_state(CephContext
* _cct
, RGWEnv
* e
, RGWUserInfo
* u
)
267 : cct(_cct
), cio(NULL
), op(OP_UNKNOWN
), user(u
), has_acl_header(false),
270 enable_ops_log
= e
->conf
.enable_ops_log
;
271 enable_usage_log
= e
->conf
.enable_usage_log
;
272 defer_to_bucket_acls
= e
->conf
.defer_to_bucket_acls
;
273 content_started
= false;
283 system_request
= false;
285 time
= ceph_clock_now();
287 bucket_instance_shard_id
= -1;
289 bucket_exists
= false;
290 has_bad_meta
= false;
292 local_source
= false;
297 req_state::~req_state() {
303 bool search_err(rgw_http_errors
& errs
, int err_no
, bool is_website_redirect
, int& http_ret
, string
& code
)
305 auto r
= errs
.find(err_no
);
306 if (r
!= errs
.end()) {
307 if (! is_website_redirect
)
308 http_ret
= r
->second
.first
;
309 code
= r
->second
.second
;
315 void set_req_state_err(struct rgw_err
& err
, /* out */
317 const int prot_flags
) /* in */
323 bool is_website_redirect
= false;
325 if (prot_flags
& RGW_REST_SWIFT
) {
326 if (search_err(rgw_http_swift_errors
, err_no
, is_website_redirect
, err
.http_ret
, err
.err_code
))
330 //Default to searching in s3 errors
331 is_website_redirect
|= (prot_flags
& RGW_REST_WEBSITE
)
332 && err_no
== ERR_WEBSITE_REDIRECT
&& err
.is_clear();
333 if (search_err(rgw_http_s3_errors
, err_no
, is_website_redirect
, err
.http_ret
, err
.err_code
))
335 dout(0) << "WARNING: set_req_state_err err_no=" << err_no
336 << " resorting to 500" << dendl
;
339 err
.err_code
= "UnknownError";
342 void set_req_state_err(struct req_state
* s
, int err_no
, const string
& err_msg
)
345 set_req_state_err(s
, err_no
);
346 s
->err
.message
= err_msg
;
350 void set_req_state_err(struct req_state
* s
, int err_no
)
353 set_req_state_err(s
->err
, err_no
, s
->prot_flags
);
357 void dump(struct req_state
* s
)
359 if (s
->format
!= RGW_FORMAT_HTML
)
360 s
->formatter
->open_object_section("Error");
361 if (!s
->err
.err_code
.empty())
362 s
->formatter
->dump_string("Code", s
->err
.err_code
);
363 if (!s
->err
.message
.empty())
364 s
->formatter
->dump_string("Message", s
->err
.message
);
365 if (!s
->bucket_name
.empty()) // TODO: connect to expose_bucket
366 s
->formatter
->dump_string("BucketName", s
->bucket_name
);
367 if (!s
->trans_id
.empty()) // TODO: connect to expose_bucket or another toggle
368 s
->formatter
->dump_string("RequestId", s
->trans_id
);
369 s
->formatter
->dump_string("HostId", s
->host_id
);
370 if (s
->format
!= RGW_FORMAT_HTML
)
371 s
->formatter
->close_section();
379 #define STR_LEN_ENTRY(s) { s, sizeof(s) - 1 }
381 struct str_len meta_prefixes
[] = { STR_LEN_ENTRY("HTTP_X_AMZ"),
382 STR_LEN_ENTRY("HTTP_X_GOOG"),
383 STR_LEN_ENTRY("HTTP_X_DHO"),
384 STR_LEN_ENTRY("HTTP_X_RGW"),
385 STR_LEN_ENTRY("HTTP_X_OBJECT"),
386 STR_LEN_ENTRY("HTTP_X_CONTAINER"),
387 STR_LEN_ENTRY("HTTP_X_ACCOUNT"),
391 void req_info::init_meta_info(bool *found_bad_meta
)
395 for (const auto& kv
: env
->get_map()) {
397 const string
& header_name
= kv
.first
;
398 const string
& val
= kv
.second
;
399 for (int prefix_num
= 0; (prefix
= meta_prefixes
[prefix_num
].str
) != NULL
; prefix_num
++) {
400 int len
= meta_prefixes
[prefix_num
].len
;
401 const char *p
= header_name
.c_str();
402 if (strncmp(p
, prefix
, len
) == 0) {
403 dout(10) << "meta>> " << p
<< dendl
;
404 const char *name
= p
+len
; /* skip the prefix */
405 int name_len
= header_name
.size() - len
;
407 if (found_bad_meta
&& strncmp(name
, "_META_", name_len
) == 0)
408 *found_bad_meta
= true;
410 char name_low
[meta_prefixes
[0].len
+ name_len
+ 1];
411 snprintf(name_low
, meta_prefixes
[0].len
- 5 + name_len
+ 1, "%s%s", meta_prefixes
[0].str
+ 5 /* skip HTTP_ */, name
); // normalize meta prefix
413 for (j
= 0; name_low
[j
]; j
++) {
414 if (name_low
[j
] != '_')
415 name_low
[j
] = tolower(name_low
[j
]);
421 auto it
= x_meta_map
.find(name_low
);
422 if (it
!= x_meta_map
.end()) {
423 string old
= it
->second
;
424 boost::algorithm::trim_right(old
);
427 x_meta_map
[name_low
] = old
;
429 x_meta_map
[name_low
] = val
;
434 for (const auto& kv
: x_meta_map
) {
435 dout(10) << "x>> " << kv
.first
<< ":" << rgw::crypt_sanitize::x_meta_map
{kv
.first
, kv
.second
} << dendl
;
439 std::ostream
& operator<<(std::ostream
& oss
, const rgw_err
&err
)
441 oss
<< "rgw_err(http_ret=" << err
.http_ret
<< ", err_code='" << err
.err_code
<< "') ";
445 string
rgw_string_unquote(const string
& s
)
447 if (s
[0] != '"' || s
.size() < 2)
451 for (len
= s
.size(); len
> 2; --len
) {
452 if (s
[len
- 1] != ' ')
459 return s
.substr(1, len
- 2);
462 static bool check_str_end(const char *s
)
475 static bool check_gmt_end(const char *s
)
480 while (isspace(*s
)) {
484 /* check for correct timezone */
485 if ((strncmp(s
, "GMT", 3) != 0) &&
486 (strncmp(s
, "UTC", 3) != 0)) {
493 static bool parse_rfc850(const char *s
, struct tm
*t
)
495 memset(t
, 0, sizeof(*t
));
496 return check_gmt_end(strptime(s
, "%A, %d-%b-%y %H:%M:%S ", t
));
499 static bool parse_asctime(const char *s
, struct tm
*t
)
501 memset(t
, 0, sizeof(*t
));
502 return check_str_end(strptime(s
, "%a %b %d %H:%M:%S %Y", t
));
505 static bool parse_rfc1123(const char *s
, struct tm
*t
)
507 memset(t
, 0, sizeof(*t
));
508 return check_gmt_end(strptime(s
, "%a, %d %b %Y %H:%M:%S ", t
));
511 static bool parse_rfc1123_alt(const char *s
, struct tm
*t
)
513 memset(t
, 0, sizeof(*t
));
514 return check_str_end(strptime(s
, "%a, %d %b %Y %H:%M:%S %z", t
));
517 bool parse_rfc2616(const char *s
, struct tm
*t
)
519 return parse_rfc850(s
, t
) || parse_asctime(s
, t
) || parse_rfc1123(s
, t
) || parse_rfc1123_alt(s
,t
);
522 bool parse_iso8601(const char *s
, struct tm
*t
, uint32_t *pns
, bool extended_format
)
524 memset(t
, 0, sizeof(*t
));
530 if (extended_format
) {
531 p
= strptime(s
, "%Y-%m-%dT%T", t
);
533 p
= strptime(s
, "%Y-%m-%d %T", t
);
536 p
= strptime(s
, "%Y%m%dT%H%M%S", t
);
539 dout(0) << "parse_iso8601 failed" << dendl
;
542 const boost::string_view str
= rgw_trim_whitespace(boost::string_view(p
));
543 int len
= str
.size();
545 if (len
== 0 || (len
== 1 && str
[0] == 'Z'))
553 boost::string_view nsstr
= str
.substr(1, len
- 2);
554 int r
= stringtoul(nsstr
.to_string(), &ms
);
562 if (nsstr
.size() > 9) {
563 nsstr
= nsstr
.substr(0, 9);
566 uint64_t mul_table
[] = { 0,
578 *pns
= ms
* mul_table
[nsstr
.size()];
583 int parse_key_value(string
& in_str
, const char *delim
, string
& key
, string
& val
)
588 auto pos
= in_str
.find(delim
);
589 if (pos
== string::npos
)
592 key
= rgw_trim_whitespace(in_str
.substr(0, pos
));
593 val
= rgw_trim_whitespace(in_str
.substr(pos
+ 1));
598 int parse_key_value(string
& in_str
, string
& key
, string
& val
)
600 return parse_key_value(in_str
, "=", key
,val
);
603 boost::optional
<std::pair
<boost::string_view
, boost::string_view
>>
604 parse_key_value(const boost::string_view
& in_str
,
605 const boost::string_view
& delim
)
607 const size_t pos
= in_str
.find(delim
);
608 if (pos
== boost::string_view::npos
) {
612 const auto key
= rgw_trim_whitespace(in_str
.substr(0, pos
));
613 const auto val
= rgw_trim_whitespace(in_str
.substr(pos
+ 1));
615 return std::make_pair(key
, val
);
618 boost::optional
<std::pair
<boost::string_view
, boost::string_view
>>
619 parse_key_value(const boost::string_view
& in_str
)
621 return parse_key_value(in_str
, "=");
624 int parse_time(const char *time_str
, real_time
*time
)
629 if (!parse_rfc2616(time_str
, &tm
) && !parse_iso8601(time_str
, &tm
, &ns
)) {
633 time_t sec
= internal_timegm(&tm
);
634 *time
= utime_t(sec
, ns
).to_real_time();
639 #define TIME_BUF_SIZE 128
641 void rgw_to_iso8601(const real_time
& t
, char *dest
, int buf_size
)
645 char buf
[TIME_BUF_SIZE
];
647 time_t epoch
= ut
.sec();
648 struct tm
*tmp
= gmtime_r(&epoch
, &result
);
652 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%T", tmp
) == 0)
655 snprintf(dest
, buf_size
, "%s.%03dZ", buf
, (int)(ut
.usec() / 1000));
658 void rgw_to_iso8601(const real_time
& t
, string
*dest
)
660 char buf
[TIME_BUF_SIZE
];
661 rgw_to_iso8601(t
, buf
, sizeof(buf
));
666 * calculate the sha1 value of a given msg and key
668 void calc_hmac_sha1(const char *key
, int key_len
,
669 const char *msg
, int msg_len
, char *dest
)
670 /* destination should be CEPH_CRYPTO_HMACSHA1_DIGESTSIZE bytes long */
672 HMACSHA1
hmac((const unsigned char *)key
, key_len
);
673 hmac
.Update((const unsigned char *)msg
, msg_len
);
674 hmac
.Final((unsigned char *)dest
);
678 * calculate the sha256 value of a given msg and key
680 void calc_hmac_sha256(const char *key
, int key_len
,
681 const char *msg
, int msg_len
, char *dest
)
683 char hash_sha256
[CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
];
685 HMACSHA256
hmac((const unsigned char *)key
, key_len
);
686 hmac
.Update((const unsigned char *)msg
, msg_len
);
687 hmac
.Final((unsigned char *)hash_sha256
);
689 memcpy(dest
, hash_sha256
, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
);
692 using ceph::crypto::SHA256
;
695 * calculate the sha256 hash value of a given msg
697 sha256_digest_t
calc_hash_sha256(const boost::string_view
& msg
)
699 std::array
<unsigned char, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
> hash
;
702 hasher
.Update(reinterpret_cast<const unsigned char*>(msg
.data()), msg
.size());
703 hasher
.Final(hash
.data());
708 SHA256
* calc_hash_sha256_open_stream()
713 void calc_hash_sha256_update_stream(SHA256
*hash
, const char *msg
, int len
)
715 hash
->Update((const unsigned char *)msg
, len
);
718 string
calc_hash_sha256_close_stream(SHA256
**phash
)
720 SHA256
*hash
= *phash
;
722 hash
= calc_hash_sha256_open_stream();
724 char hash_sha256
[CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
];
726 hash
->Final((unsigned char *)hash_sha256
);
728 char hex_str
[(CEPH_CRYPTO_SHA256_DIGESTSIZE
* 2) + 1];
729 buf_to_hex((unsigned char *)hash_sha256
, CEPH_CRYPTO_SHA256_DIGESTSIZE
, hex_str
);
734 return std::string(hex_str
);
737 std::string
calc_hash_sha256_restart_stream(SHA256
**phash
)
739 const auto hash
= calc_hash_sha256_close_stream(phash
);
740 *phash
= calc_hash_sha256_open_stream();
745 int gen_rand_base64(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
748 char tmp_dest
[size
+ 4]; /* so that there's space for the extra '=' characters, and some */
751 ret
= get_random_bytes(buf
, sizeof(buf
));
753 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
757 ret
= ceph_armor(tmp_dest
, &tmp_dest
[sizeof(tmp_dest
)],
758 (const char *)buf
, ((const char *)buf
) + ((size
- 1) * 3 + 4 - 1) / 4);
760 lderr(cct
) << "ceph_armor failed" << dendl
;
763 tmp_dest
[ret
] = '\0';
764 memcpy(dest
, tmp_dest
, size
);
770 static const char alphanum_upper_table
[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
772 int gen_rand_alphanumeric_upper(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
774 int ret
= get_random_bytes(dest
, size
);
776 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
781 for (i
=0; i
<size
- 1; i
++) {
782 int pos
= (unsigned)dest
[i
];
783 dest
[i
] = alphanum_upper_table
[pos
% (sizeof(alphanum_upper_table
) - 1)];
790 static const char alphanum_lower_table
[]="0123456789abcdefghijklmnopqrstuvwxyz";
792 int gen_rand_alphanumeric_lower(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
794 int ret
= get_random_bytes(dest
, size
);
796 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
801 for (i
=0; i
<size
- 1; i
++) {
802 int pos
= (unsigned)dest
[i
];
803 dest
[i
] = alphanum_lower_table
[pos
% (sizeof(alphanum_lower_table
) - 1)];
810 int gen_rand_alphanumeric_lower(CephContext
*cct
, string
*str
, int length
)
812 char buf
[length
+ 1];
813 int ret
= gen_rand_alphanumeric_lower(cct
, buf
, sizeof(buf
));
821 // this is basically a modified base64 charset, url friendly
822 static const char alphanum_table
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
824 int gen_rand_alphanumeric(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
826 int ret
= get_random_bytes(dest
, size
);
828 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
833 for (i
=0; i
<size
- 1; i
++) {
834 int pos
= (unsigned)dest
[i
];
835 dest
[i
] = alphanum_table
[pos
& 63];
842 static const char alphanum_no_underscore_table
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
844 int gen_rand_alphanumeric_no_underscore(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
846 int ret
= get_random_bytes(dest
, size
);
848 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
853 for (i
=0; i
<size
- 1; i
++) {
854 int pos
= (unsigned)dest
[i
];
855 dest
[i
] = alphanum_no_underscore_table
[pos
& 63];
862 static const char alphanum_plain_table
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
864 int gen_rand_alphanumeric_plain(CephContext
*cct
, char *dest
, int size
) /* size should be the required string size + 1 */
866 int ret
= get_random_bytes(dest
, size
);
868 lderr(cct
) << "cannot get random bytes: " << cpp_strerror(-ret
) << dendl
;
873 for (i
=0; i
<size
- 1; i
++) {
874 int pos
= (unsigned)dest
[i
];
875 dest
[i
] = alphanum_plain_table
[pos
% (sizeof(alphanum_plain_table
) - 1)];
884 auto delim_pos
= str
.find('=');
887 if (delim_pos
== string::npos
) {
892 name
= str
.substr(0, delim_pos
);
893 val
= str
.substr(delim_pos
+ 1);
899 int RGWHTTPArgs::parse()
911 int fpos
= str
.find('&', pos
);
916 std::string nameval
= url_decode(str
.substr(pos
, fpos
- pos
), true);
917 NameVal
nv(std::move(nameval
));
918 int ret
= nv
.parse();
920 string
& name
= nv
.get_name();
921 string
& val
= nv
.get_val();
932 void RGWHTTPArgs::append(const string
& name
, const string
& val
)
934 if (name
.compare(0, sizeof(RGW_SYS_PARAM_PREFIX
) - 1, RGW_SYS_PARAM_PREFIX
) == 0) {
935 sys_val_map
[name
] = val
;
940 if ((name
.compare("acl") == 0) ||
941 (name
.compare("cors") == 0) ||
942 (name
.compare("location") == 0) ||
943 (name
.compare("logging") == 0) ||
944 (name
.compare("usage") == 0) ||
945 (name
.compare("lifecycle") == 0) ||
946 (name
.compare("delete") == 0) ||
947 (name
.compare("uploads") == 0) ||
948 (name
.compare("partNumber") == 0) ||
949 (name
.compare("uploadId") == 0) ||
950 (name
.compare("versionId") == 0) ||
951 (name
.compare("start-date") == 0) ||
952 (name
.compare("end-date") == 0) ||
953 (name
.compare("versions") == 0) ||
954 (name
.compare("versioning") == 0) ||
955 (name
.compare("website") == 0) ||
956 (name
.compare("requestPayment") == 0) ||
957 (name
.compare("torrent") == 0) ||
958 (name
.compare("tagging") == 0)) {
959 sub_resources
[name
] = val
;
960 } else if (name
[0] == 'r') { // root of all evil
961 if ((name
.compare("response-content-type") == 0) ||
962 (name
.compare("response-content-language") == 0) ||
963 (name
.compare("response-expires") == 0) ||
964 (name
.compare("response-cache-control") == 0) ||
965 (name
.compare("response-content-disposition") == 0) ||
966 (name
.compare("response-content-encoding") == 0)) {
967 sub_resources
[name
] = val
;
968 has_resp_modifier
= true;
970 } else if ((name
.compare("subuser") == 0) ||
971 (name
.compare("key") == 0) ||
972 (name
.compare("caps") == 0) ||
973 (name
.compare("index") == 0) ||
974 (name
.compare("policy") == 0) ||
975 (name
.compare("quota") == 0) ||
976 (name
.compare("object") == 0)) {
978 if (!admin_subresource_added
) {
979 sub_resources
[name
] = "";
980 admin_subresource_added
= true;
985 const string
& RGWHTTPArgs::get(const string
& name
, bool *exists
) const
987 auto iter
= val_map
.find(name
);
988 bool e
= (iter
!= std::end(val_map
));
996 int RGWHTTPArgs::get_bool(const string
& name
, bool *val
, bool *exists
)
998 map
<string
, string
>::iterator iter
;
999 iter
= val_map
.find(name
);
1000 bool e
= (iter
!= val_map
.end());
1005 const char *s
= iter
->second
.c_str();
1007 if (strcasecmp(s
, "false") == 0) {
1009 } else if (strcasecmp(s
, "true") == 0) {
1019 int RGWHTTPArgs::get_bool(const char *name
, bool *val
, bool *exists
)
1022 return get_bool(s
, val
, exists
);
1025 void RGWHTTPArgs::get_bool(const char *name
, bool *val
, bool def_val
)
1027 bool exists
= false;
1028 if ((get_bool(name
, val
, &exists
) < 0) ||
1034 string
RGWHTTPArgs::sys_get(const string
& name
, bool * const exists
) const
1036 const auto iter
= sys_val_map
.find(name
);
1037 const bool e
= (iter
!= sys_val_map
.end());
1043 return e
? iter
->second
: string();
1046 bool verify_user_permission(struct req_state
* const s
,
1047 RGWAccessControlPolicy
* const user_acl
,
1050 /* S3 doesn't support account ACLs. */
1054 if ((perm
& (int)s
->perm_mask
) != perm
)
1057 return user_acl
->verify_permission(*s
->auth
.identity
, perm
, perm
);
1060 bool verify_user_permission(struct req_state
* const s
,
1063 return verify_user_permission(s
, s
->user_acl
.get(), perm
);
1066 bool verify_requester_payer_permission(struct req_state
*s
)
1068 if (!s
->bucket_info
.requester_pays
)
1071 if (s
->auth
.identity
->is_owner_of(s
->bucket_info
.owner
))
1074 if (s
->auth
.identity
->is_anonymous()) {
1078 const char *request_payer
= s
->info
.env
->get("HTTP_X_AMZ_REQUEST_PAYER");
1079 if (!request_payer
) {
1081 request_payer
= s
->info
.args
.get("x-amz-request-payer", &exists
).c_str();
1087 if (strcasecmp(request_payer
, "requester") == 0) {
1094 bool verify_bucket_permission(struct req_state
* const s
,
1095 const rgw_bucket
& bucket
,
1096 RGWAccessControlPolicy
* const user_acl
,
1097 RGWAccessControlPolicy
* const bucket_acl
,
1098 const optional
<Policy
>& bucket_policy
,
1101 if (!verify_requester_payer_permission(s
))
1104 if (bucket_policy
) {
1105 auto r
= bucket_policy
->eval(s
->env
, *s
->auth
.identity
, op
, ARN(bucket
));
1106 if (r
== Effect::Allow
)
1107 // It looks like S3 ACLs only GRANT permissions rather than
1108 // denying them, so this should be safe.
1110 else if (r
== Effect::Deny
)
1114 const auto perm
= op_to_perm(op
);
1116 return verify_bucket_permission_no_policy(s
, user_acl
, bucket_acl
, perm
);
1119 bool verify_bucket_permission_no_policy(struct req_state
* const s
,
1120 RGWAccessControlPolicy
* const user_acl
,
1121 RGWAccessControlPolicy
* const bucket_acl
,
1127 if ((perm
& (int)s
->perm_mask
) != perm
)
1130 if (bucket_acl
->verify_permission(*s
->auth
.identity
, perm
, perm
,
1131 s
->info
.env
->get("HTTP_REFERER")))
1137 return user_acl
->verify_permission(*s
->auth
.identity
, perm
, perm
);
1140 bool verify_bucket_permission_no_policy(struct req_state
* const s
, const int perm
)
1142 if (!verify_requester_payer_permission(s
))
1145 return verify_bucket_permission_no_policy(s
,
1151 bool verify_bucket_permission(struct req_state
* const s
, const uint64_t op
)
1153 return verify_bucket_permission(s
,
1161 static inline bool check_deferred_bucket_perms(struct req_state
* const s
,
1162 const rgw_bucket
& bucket
,
1163 RGWAccessControlPolicy
* const user_acl
,
1164 RGWAccessControlPolicy
* const bucket_acl
,
1165 const optional
<Policy
>& bucket_policy
,
1166 const uint8_t deferred_check
,
1169 return (s
->defer_to_bucket_acls
== deferred_check \
1170 && verify_bucket_permission(s
, bucket
, user_acl
, bucket_acl
, bucket_policy
, op
));
1173 static inline bool check_deferred_bucket_only_acl(struct req_state
* const s
,
1174 RGWAccessControlPolicy
* const user_acl
,
1175 RGWAccessControlPolicy
* const bucket_acl
,
1176 const uint8_t deferred_check
,
1179 return (s
->defer_to_bucket_acls
== deferred_check \
1180 && verify_bucket_permission_no_policy(s
, user_acl
, bucket_acl
, perm
));
1183 bool verify_object_permission(struct req_state
* const s
,
1185 RGWAccessControlPolicy
* const user_acl
,
1186 RGWAccessControlPolicy
* const bucket_acl
,
1187 RGWAccessControlPolicy
* const object_acl
,
1188 const optional
<Policy
>& bucket_policy
,
1191 if (!verify_requester_payer_permission(s
))
1194 if (bucket_policy
) {
1195 auto r
= bucket_policy
->eval(s
->env
, *s
->auth
.identity
, op
, ARN(obj
));
1196 if (r
== Effect::Allow
)
1197 // It looks like S3 ACLs only GRANT permissions rather than
1198 // denying them, so this should be safe.
1200 else if (r
== Effect::Deny
)
1204 const auto perm
= op_to_perm(op
);
1206 if (check_deferred_bucket_perms(s
, obj
.bucket
, user_acl
, bucket_acl
, bucket_policy
,
1207 RGW_DEFER_TO_BUCKET_ACLS_RECURSE
, op
) ||
1208 check_deferred_bucket_perms(s
, obj
.bucket
, user_acl
, bucket_acl
, bucket_policy
,
1209 RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL
, rgw::IAM::s3All
)) {
1217 bool ret
= object_acl
->verify_permission(*s
->auth
.identity
, s
->perm_mask
, perm
);
1222 if (!s
->cct
->_conf
->rgw_enforce_swift_acls
)
1225 if ((perm
& (int)s
->perm_mask
) != perm
)
1229 if (perm
& (RGW_PERM_READ
| RGW_PERM_READ_ACP
))
1230 swift_perm
|= RGW_PERM_READ_OBJS
;
1231 if (perm
& RGW_PERM_WRITE
)
1232 swift_perm
|= RGW_PERM_WRITE_OBJS
;
1237 /* we already verified the user mask above, so we pass swift_perm as the mask here,
1238 otherwise the mask might not cover the swift permissions bits */
1239 if (bucket_acl
->verify_permission(*s
->auth
.identity
, swift_perm
, swift_perm
,
1240 s
->info
.env
->get("HTTP_REFERER")))
1246 return user_acl
->verify_permission(*s
->auth
.identity
, swift_perm
, swift_perm
);
1249 bool verify_object_permission_no_policy(struct req_state
* const s
,
1250 RGWAccessControlPolicy
* const user_acl
,
1251 RGWAccessControlPolicy
* const bucket_acl
,
1252 RGWAccessControlPolicy
* const object_acl
,
1255 if (check_deferred_bucket_only_acl(s
, user_acl
, bucket_acl
, RGW_DEFER_TO_BUCKET_ACLS_RECURSE
, perm
) ||
1256 check_deferred_bucket_only_acl(s
, user_acl
, bucket_acl
, RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL
, RGW_PERM_FULL_CONTROL
)) {
1264 bool ret
= object_acl
->verify_permission(*s
->auth
.identity
, s
->perm_mask
, perm
);
1269 if (!s
->cct
->_conf
->rgw_enforce_swift_acls
)
1272 if ((perm
& (int)s
->perm_mask
) != perm
)
1276 if (perm
& (RGW_PERM_READ
| RGW_PERM_READ_ACP
))
1277 swift_perm
|= RGW_PERM_READ_OBJS
;
1278 if (perm
& RGW_PERM_WRITE
)
1279 swift_perm
|= RGW_PERM_WRITE_OBJS
;
1284 /* we already verified the user mask above, so we pass swift_perm as the mask here,
1285 otherwise the mask might not cover the swift permissions bits */
1286 if (bucket_acl
->verify_permission(*s
->auth
.identity
, swift_perm
, swift_perm
,
1287 s
->info
.env
->get("HTTP_REFERER")))
1293 return user_acl
->verify_permission(*s
->auth
.identity
, swift_perm
, swift_perm
);
1296 bool verify_object_permission_no_policy(struct req_state
*s
, int perm
)
1298 if (!verify_requester_payer_permission(s
))
1301 return verify_object_permission_no_policy(s
, s
->user_acl
.get(),
1302 s
->bucket_acl
, s
->object_acl
,
1306 bool verify_object_permission(struct req_state
*s
, uint64_t op
)
1308 return verify_object_permission(s
,
1309 rgw_obj(s
->bucket
, s
->object
),
1323 memset(table
, -1, sizeof(table
));
1325 for (i
= '0'; i
<='9'; i
++)
1327 for (i
= 'A'; i
<='F'; i
++)
1328 table
[i
] = i
- 'A' + 0xa;
1329 for (i
= 'a'; i
<='f'; i
++)
1330 table
[i
] = i
- 'a' + 0xa;
1333 char to_num(char c
) {
1334 return table
[(int)c
];
1338 static char hex_to_num(char c
)
1340 static HexTable hex_table
;
1341 return hex_table
.to_num(c
);
1344 std::string
url_decode(const boost::string_view
& src_str
, bool in_query
)
1346 std::string dest_str
;
1347 dest_str
.reserve(src_str
.length() + 1);
1349 for (auto src
= std::begin(src_str
); src
!= std::end(src_str
); ++src
) {
1351 if (!in_query
|| *src
!= '+') {
1355 dest_str
.push_back(*src
);
1357 dest_str
.push_back(' ');
1360 /* 3 == strlen("%%XX") */
1361 if (std::distance(src
, std::end(src_str
)) < 3) {
1366 const char c1
= hex_to_num(*src
++);
1367 const char c2
= hex_to_num(*src
);
1368 if (c1
< 0 || c2
< 0) {
1369 return std::string();
1371 dest_str
.push_back(c1
<< 4 | c2
);
1379 void rgw_uri_escape_char(char c
, string
& dst
)
1382 snprintf(buf
, sizeof(buf
), "%%%.2X", (int)(unsigned char)c
);
1386 static bool char_needs_url_encoding(char c
)
1388 if (c
<= 0x20 || c
>= 0x7f)
1418 void url_encode(const string
& src
, string
& dst
)
1420 const char *p
= src
.c_str();
1421 for (unsigned i
= 0; i
< src
.size(); i
++, p
++) {
1422 if (char_needs_url_encoding(*p
)) {
1423 rgw_uri_escape_char(*p
, dst
);
1431 std::string
url_encode(const std::string
& src
)
1434 url_encode(src
, dst
);
1439 string
rgw_trim_whitespace(const string
& src
)
1446 for (; start
!= (int)src
.size(); start
++) {
1447 if (!isspace(src
[start
]))
1451 int end
= src
.size() - 1;
1456 for (; end
> start
; end
--) {
1457 if (!isspace(src
[end
]))
1461 return src
.substr(start
, end
- start
+ 1);
1464 boost::string_view
rgw_trim_whitespace(const boost::string_view
& src
)
1466 boost::string_view res
= src
;
1468 while (res
.size() > 0 && std::isspace(res
.front())) {
1469 res
.remove_prefix(1);
1471 while (res
.size() > 0 && std::isspace(res
.back())) {
1472 res
.remove_suffix(1);
1477 string
rgw_trim_quotes(const string
& val
)
1479 string s
= rgw_trim_whitespace(val
);
1484 int end
= s
.size() - 1;
1485 int quotes_count
= 0;
1487 if (s
[start
] == '"') {
1491 if (s
[end
] == '"') {
1495 if (quotes_count
== 2) {
1496 return s
.substr(start
, end
- start
+ 1);
1501 struct rgw_name_to_flag
{
1502 const char *type_name
;
1506 static int parse_list_of_flags(struct rgw_name_to_flag
*mapping
,
1507 const string
& str
, uint32_t *perm
)
1510 get_str_list(str
, strs
);
1511 list
<string
>::iterator iter
;
1513 for (iter
= strs
.begin(); iter
!= strs
.end(); ++iter
) {
1515 for (int i
= 0; mapping
[i
].type_name
; i
++) {
1516 if (s
.compare(mapping
[i
].type_name
) == 0)
1517 v
|= mapping
[i
].flag
;
1525 static struct rgw_name_to_flag cap_names
[] = { {"*", RGW_CAP_ALL
},
1526 {"read", RGW_CAP_READ
},
1527 {"write", RGW_CAP_WRITE
},
1530 int RGWUserCaps::parse_cap_perm(const string
& str
, uint32_t *perm
)
1532 return parse_list_of_flags(cap_names
, str
, perm
);
1535 int RGWUserCaps::get_cap(const string
& cap
, string
& type
, uint32_t *pperm
)
1537 int pos
= cap
.find('=');
1539 type
= rgw_trim_whitespace(cap
.substr(0, pos
));
1542 if (!is_valid_cap_type(type
))
1543 return -ERR_INVALID_CAP
;
1547 if (pos
< (int)cap
.size() - 1) {
1548 cap_perm
= cap
.substr(pos
+ 1);
1549 int r
= RGWUserCaps::parse_cap_perm(cap_perm
, &perm
);
1559 int RGWUserCaps::add_cap(const string
& cap
)
1564 int r
= get_cap(cap
, type
, &perm
);
1573 int RGWUserCaps::remove_cap(const string
& cap
)
1578 int r
= get_cap(cap
, type
, &perm
);
1582 map
<string
, uint32_t>::iterator iter
= caps
.find(type
);
1583 if (iter
== caps
.end())
1586 uint32_t& old_perm
= iter
->second
;
1594 int RGWUserCaps::add_from_string(const string
& str
)
1598 auto end
= str
.find(';', start
);
1599 if (end
== string::npos
)
1602 int r
= add_cap(str
.substr(start
, end
- start
));
1607 } while (start
< (int)str
.size());
1612 int RGWUserCaps::remove_from_string(const string
& str
)
1616 auto end
= str
.find(';', start
);
1617 if (end
== string::npos
)
1620 int r
= remove_cap(str
.substr(start
, end
- start
));
1625 } while (start
< (int)str
.size());
1630 void RGWUserCaps::dump(Formatter
*f
) const
1635 void RGWUserCaps::dump(Formatter
*f
, const char *name
) const
1637 f
->open_array_section(name
);
1638 map
<string
, uint32_t>::const_iterator iter
;
1639 for (iter
= caps
.begin(); iter
!= caps
.end(); ++iter
)
1641 f
->open_object_section("cap");
1642 f
->dump_string("type", iter
->first
);
1643 uint32_t perm
= iter
->second
;
1645 for (int i
=0; cap_names
[i
].type_name
; i
++) {
1646 if ((perm
& cap_names
[i
].flag
) == cap_names
[i
].flag
) {
1647 if (perm_str
.size())
1648 perm_str
.append(", ");
1650 perm_str
.append(cap_names
[i
].type_name
);
1651 perm
&= ~cap_names
[i
].flag
;
1654 if (perm_str
.empty())
1655 perm_str
= "<none>";
1657 f
->dump_string("perm", perm_str
);
1668 void decode_json(JSONObj
*obj
) {
1669 JSONDecoder::decode_json("type", type
, obj
);
1671 JSONDecoder::decode_json("perm", perm_str
, obj
);
1672 if (RGWUserCaps::parse_cap_perm(perm_str
, &perm
) < 0) {
1673 throw JSONDecoder::err("failed to parse permissions");
1678 void RGWUserCaps::decode_json(JSONObj
*obj
)
1680 list
<RGWUserCap
> caps_list
;
1681 decode_json_obj(caps_list
, obj
);
1683 list
<RGWUserCap
>::iterator iter
;
1684 for (iter
= caps_list
.begin(); iter
!= caps_list
.end(); ++iter
) {
1685 RGWUserCap
& cap
= *iter
;
1686 caps
[cap
.type
] = cap
.perm
;
1690 int RGWUserCaps::check_cap(const string
& cap
, uint32_t perm
)
1692 map
<string
, uint32_t>::iterator iter
= caps
.find(cap
);
1694 if ((iter
== caps
.end()) ||
1695 (iter
->second
& perm
) != perm
) {
1702 bool RGWUserCaps::is_valid_cap_type(const string
& tp
)
1704 static const char *cap_type
[] = { "user",
1715 for (unsigned int i
= 0; i
< sizeof(cap_type
) / sizeof(char *); ++i
) {
1716 if (tp
.compare(cap_type
[i
]) == 0) {
1724 static ssize_t
unescape_str(const string
& s
, ssize_t ofs
, char esc_char
, char special_char
, string
*dest
)
1726 const char *src
= s
.c_str();
1727 char dest_buf
[s
.size() + 1];
1728 char *destp
= dest_buf
;
1733 for (size_t i
= ofs
; i
< s
.size(); i
++) {
1735 if (!esc
&& c
== esc_char
) {
1739 if (!esc
&& c
== special_char
) {
1742 return (ssize_t
)i
+ 1;
1749 return string::npos
;
1752 static void escape_str(const string
& s
, char esc_char
, char special_char
, string
*dest
)
1754 const char *src
= s
.c_str();
1755 char dest_buf
[s
.size() * 2 + 1];
1756 char *destp
= dest_buf
;
1758 for (size_t i
= 0; i
< s
.size(); i
++) {
1760 if (c
== esc_char
|| c
== special_char
) {
1761 *destp
++ = esc_char
;
1769 void rgw_pool::from_str(const string
& s
)
1771 size_t pos
= unescape_str(s
, 0, '\\', ':', &name
);
1772 if (pos
!= string::npos
) {
1773 pos
= unescape_str(s
, pos
, '\\', ':', &ns
);
1774 /* ignore return; if pos != string::npos it means that we had a colon
1775 * in the middle of ns that wasn't escaped, we're going to stop there
1780 string
rgw_pool::to_str() const
1783 escape_str(name
, '\\', ':', &esc_name
);
1788 escape_str(ns
, '\\', ':', &esc_ns
);
1789 return esc_name
+ ":" + esc_ns
;
1792 void rgw_raw_obj::decode_from_rgw_obj(bufferlist::iterator
& bl
)
1795 ::decode(old_obj
, bl
);
1797 get_obj_bucket_and_oid_loc(old_obj
, oid
, loc
);
1798 pool
= old_obj
.get_explicit_data_pool();
1801 std::string
rgw_bucket::get_key(char tenant_delim
, char id_delim
) const
1803 static constexpr size_t shard_len
{12}; // ":4294967295\0"
1804 const size_t max_len
= tenant
.size() + sizeof(tenant_delim
) +
1805 name
.size() + sizeof(id_delim
) + bucket_id
.size() + shard_len
;
1808 key
.reserve(max_len
);
1809 if (!tenant
.empty() && tenant_delim
) {
1811 key
.append(1, tenant_delim
);
1814 if (!bucket_id
.empty() && id_delim
) {
1815 key
.append(1, id_delim
);
1816 key
.append(bucket_id
);
1821 std::string
rgw_bucket_shard::get_key(char tenant_delim
, char id_delim
,
1822 char shard_delim
) const
1824 auto key
= bucket
.get_key(tenant_delim
, id_delim
);
1825 if (shard_id
>= 0 && shard_delim
) {
1826 key
.append(1, shard_delim
);
1827 key
.append(std::to_string(shard_id
));
1832 static struct rgw_name_to_flag op_type_mapping
[] = { {"*", RGW_OP_TYPE_ALL
},
1833 {"read", RGW_OP_TYPE_READ
},
1834 {"write", RGW_OP_TYPE_WRITE
},
1835 {"delete", RGW_OP_TYPE_DELETE
},
1839 int rgw_parse_op_type_list(const string
& str
, uint32_t *perm
)
1841 return parse_list_of_flags(op_type_mapping
, str
, perm
);
1844 static int match_internal(boost::string_ref pattern
, boost::string_ref input
, int (*function
)(const char&, const char&))
1846 boost::string_ref::iterator it1
= pattern
.begin();
1847 boost::string_ref::iterator it2
= input
.begin();
1849 if (it1
== pattern
.end() && it2
== input
.end())
1851 if (it1
== pattern
.end() || it2
== input
.end())
1853 if (*it1
== '*' && (it1
+ 1) == pattern
.end() && it2
!= input
.end())
1855 if (*it1
== '*' && (it1
+ 1) == pattern
.end() && it2
== input
.end())
1857 if (function(*it1
, *it2
) || *it1
== '?') {
1863 if (function(*(it1
+ 1), *it2
))
1874 static int matchcase(const char& c1
, const char& c2
)
1881 static int matchignorecase(const char& c1
, const char& c2
)
1883 if (tolower(c1
) == tolower(c2
))
1888 int match(const string
& pattern
, const string
& input
, uint32_t flag
)
1890 auto last_pos_input
= 0, last_pos_pattern
= 0;
1893 auto cur_pos_input
= input
.find(":", last_pos_input
);
1894 auto cur_pos_pattern
= pattern
.find(":", last_pos_pattern
);
1896 string substr_input
= input
.substr(last_pos_input
, cur_pos_input
);
1897 string substr_pattern
= pattern
.substr(last_pos_pattern
, cur_pos_pattern
);
1900 if (substr_pattern
== "*") {
1902 } else if (flag
& MATCH_POLICY_ACTION
|| flag
& MATCH_POLICY_ARN
) {
1903 res
= match_internal(substr_pattern
, substr_input
, &matchignorecase
);
1905 res
= match_internal(substr_pattern
, substr_input
, &matchcase
);
1910 if (cur_pos_pattern
== string::npos
&& cur_pos_input
== string::npos
)
1912 else if ((cur_pos_pattern
== string::npos
&& cur_pos_input
!= string::npos
) ||
1913 (cur_pos_pattern
!= string::npos
&& cur_pos_input
== string::npos
))
1916 last_pos_pattern
= cur_pos_pattern
+ 1;
1917 last_pos_input
= cur_pos_input
+ 1;