]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_common.h
import ceph 14.2.5
[ceph.git] / ceph / src / rgw / rgw_common.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 /*
5 * Ceph - scalable distributed file system
6 *
7 * Copyright (C) 2004-2009 Sage Weil <sage@newdream.net>
8 * Copyright (C) 2015 Yehuda Sadeh <yehuda@redhat.com>
9 *
10 * This is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License version 2.1, as published by the Free Software
13 * Foundation. See file COPYING.
14 *
15 */
16
17 #ifndef CEPH_RGW_COMMON_H
18 #define CEPH_RGW_COMMON_H
19
20 #include <array>
21
22 #include <boost/algorithm/string.hpp>
23 #include <boost/utility/string_view.hpp>
24
25 #include "common/ceph_crypto.h"
26 #include "rgw_acl.h"
27 #include "rgw_cors.h"
28 #include "rgw_iam_policy.h"
29 #include "rgw_quota.h"
30 #include "rgw_string.h"
31 #include "common/async/yield_context.h"
32 #include "rgw_website.h"
33 #include "rgw_object_lock.h"
34 #include "cls/version/cls_version_types.h"
35 #include "cls/user/cls_user_types.h"
36 #include "cls/rgw/cls_rgw_types.h"
37 #include "include/rados/librados.hpp"
38
39 namespace ceph {
40 class Formatter;
41 }
42
43 using ceph::crypto::MD5;
44
45
46 #define RGW_ATTR_PREFIX "user.rgw."
47
48 #define RGW_HTTP_RGWX_ATTR_PREFIX "RGWX_ATTR_"
49 #define RGW_HTTP_RGWX_ATTR_PREFIX_OUT "Rgwx-Attr-"
50
51 #define RGW_AMZ_PREFIX "x-amz-"
52 #define RGW_AMZ_META_PREFIX RGW_AMZ_PREFIX "meta-"
53 #define RGW_AMZ_WEBSITE_REDIRECT_LOCATION RGW_AMZ_PREFIX "website-redirect-location"
54 #define RGW_AMZ_TAG_COUNT RGW_AMZ_PREFIX "tagging-count"
55
56 #define RGW_SYS_PARAM_PREFIX "rgwx-"
57
58 #define RGW_ATTR_ACL RGW_ATTR_PREFIX "acl"
59 #define RGW_ATTR_LC RGW_ATTR_PREFIX "lc"
60 #define RGW_ATTR_CORS RGW_ATTR_PREFIX "cors"
61 #define RGW_ATTR_ETAG RGW_ATTR_PREFIX "etag"
62 #define RGW_ATTR_BUCKETS RGW_ATTR_PREFIX "buckets"
63 #define RGW_ATTR_META_PREFIX RGW_ATTR_PREFIX RGW_AMZ_META_PREFIX
64 #define RGW_ATTR_CONTENT_TYPE RGW_ATTR_PREFIX "content_type"
65 #define RGW_ATTR_CACHE_CONTROL RGW_ATTR_PREFIX "cache_control"
66 #define RGW_ATTR_CONTENT_DISP RGW_ATTR_PREFIX "content_disposition"
67 #define RGW_ATTR_CONTENT_ENC RGW_ATTR_PREFIX "content_encoding"
68 #define RGW_ATTR_CONTENT_LANG RGW_ATTR_PREFIX "content_language"
69 #define RGW_ATTR_EXPIRES RGW_ATTR_PREFIX "expires"
70 #define RGW_ATTR_DELETE_AT RGW_ATTR_PREFIX "delete_at"
71 #define RGW_ATTR_ID_TAG RGW_ATTR_PREFIX "idtag"
72 #define RGW_ATTR_TAIL_TAG RGW_ATTR_PREFIX "tail_tag"
73 #define RGW_ATTR_SHADOW_OBJ RGW_ATTR_PREFIX "shadow_name"
74 #define RGW_ATTR_MANIFEST RGW_ATTR_PREFIX "manifest"
75 #define RGW_ATTR_USER_MANIFEST RGW_ATTR_PREFIX "user_manifest"
76 #define RGW_ATTR_AMZ_WEBSITE_REDIRECT_LOCATION RGW_ATTR_PREFIX RGW_AMZ_WEBSITE_REDIRECT_LOCATION
77 #define RGW_ATTR_SLO_MANIFEST RGW_ATTR_PREFIX "slo_manifest"
78 /* Information whether an object is SLO or not must be exposed to
79 * user through custom HTTP header named X-Static-Large-Object. */
80 #define RGW_ATTR_SLO_UINDICATOR RGW_ATTR_META_PREFIX "static-large-object"
81 #define RGW_ATTR_X_ROBOTS_TAG RGW_ATTR_PREFIX "x-robots-tag"
82 #define RGW_ATTR_STORAGE_CLASS RGW_ATTR_PREFIX "storage_class"
83
84 /* S3 Object Lock*/
85 #define RGW_ATTR_OBJECT_LOCK RGW_ATTR_PREFIX "object-lock"
86 #define RGW_ATTR_OBJECT_RETENTION RGW_ATTR_PREFIX "object-retention"
87 #define RGW_ATTR_OBJECT_LEGAL_HOLD RGW_ATTR_PREFIX "object-legal-hold"
88
89
90 #define RGW_ATTR_PG_VER RGW_ATTR_PREFIX "pg_ver"
91 #define RGW_ATTR_SOURCE_ZONE RGW_ATTR_PREFIX "source_zone"
92 #define RGW_ATTR_TAGS RGW_ATTR_PREFIX RGW_AMZ_PREFIX "tagging"
93
94 #define RGW_ATTR_TEMPURL_KEY1 RGW_ATTR_META_PREFIX "temp-url-key"
95 #define RGW_ATTR_TEMPURL_KEY2 RGW_ATTR_META_PREFIX "temp-url-key-2"
96
97 /* Account/container quota of the Swift API. */
98 #define RGW_ATTR_QUOTA_NOBJS RGW_ATTR_META_PREFIX "quota-count"
99 #define RGW_ATTR_QUOTA_MSIZE RGW_ATTR_META_PREFIX "quota-bytes"
100
101 /* Static Web Site of Swift API. */
102 #define RGW_ATTR_WEB_INDEX RGW_ATTR_META_PREFIX "web-index"
103 #define RGW_ATTR_WEB_ERROR RGW_ATTR_META_PREFIX "web-error"
104 #define RGW_ATTR_WEB_LISTINGS RGW_ATTR_META_PREFIX "web-listings"
105 #define RGW_ATTR_WEB_LIST_CSS RGW_ATTR_META_PREFIX "web-listings-css"
106 #define RGW_ATTR_SUBDIR_MARKER RGW_ATTR_META_PREFIX "web-directory-type"
107
108 #define RGW_ATTR_OLH_PREFIX RGW_ATTR_PREFIX "olh."
109
110 #define RGW_ATTR_OLH_INFO RGW_ATTR_OLH_PREFIX "info"
111 #define RGW_ATTR_OLH_VER RGW_ATTR_OLH_PREFIX "ver"
112 #define RGW_ATTR_OLH_ID_TAG RGW_ATTR_OLH_PREFIX "idtag"
113 #define RGW_ATTR_OLH_PENDING_PREFIX RGW_ATTR_OLH_PREFIX "pending."
114
115 #define RGW_ATTR_COMPRESSION RGW_ATTR_PREFIX "compression"
116
117 #define RGW_ATTR_APPEND_PART_NUM RGW_ATTR_PREFIX "append_part_num"
118
119 /* IAM Policy */
120 #define RGW_ATTR_IAM_POLICY RGW_ATTR_PREFIX "iam-policy"
121 #define RGW_ATTR_USER_POLICY RGW_ATTR_PREFIX "user-policy"
122
123 /* RGW File Attributes */
124 #define RGW_ATTR_UNIX_KEY1 RGW_ATTR_PREFIX "unix-key1"
125 #define RGW_ATTR_UNIX1 RGW_ATTR_PREFIX "unix1"
126
127 #define RGW_ATTR_CRYPT_PREFIX RGW_ATTR_PREFIX "crypt."
128 #define RGW_ATTR_CRYPT_MODE RGW_ATTR_CRYPT_PREFIX "mode"
129 #define RGW_ATTR_CRYPT_KEYMD5 RGW_ATTR_CRYPT_PREFIX "keymd5"
130 #define RGW_ATTR_CRYPT_KEYID RGW_ATTR_CRYPT_PREFIX "keyid"
131 #define RGW_ATTR_CRYPT_KEYSEL RGW_ATTR_CRYPT_PREFIX "keysel"
132
133 #define RGW_BUCKETS_OBJ_SUFFIX ".buckets"
134
135 #define RGW_FORMAT_PLAIN 0
136 #define RGW_FORMAT_XML 1
137 #define RGW_FORMAT_JSON 2
138 #define RGW_FORMAT_HTML 3
139
140 #define RGW_CAP_READ 0x1
141 #define RGW_CAP_WRITE 0x2
142 #define RGW_CAP_ALL (RGW_CAP_READ | RGW_CAP_WRITE)
143
144 #define RGW_REST_SWIFT 0x1
145 #define RGW_REST_SWIFT_AUTH 0x2
146 #define RGW_REST_S3 0x4
147 #define RGW_REST_WEBSITE 0x8
148 #define RGW_REST_STS 0x10
149
150 #define RGW_SUSPENDED_USER_AUID (uint64_t)-2
151
152 #define RGW_OP_TYPE_READ 0x01
153 #define RGW_OP_TYPE_WRITE 0x02
154 #define RGW_OP_TYPE_DELETE 0x04
155
156 #define RGW_OP_TYPE_MODIFY (RGW_OP_TYPE_WRITE | RGW_OP_TYPE_DELETE)
157 #define RGW_OP_TYPE_ALL (RGW_OP_TYPE_READ | RGW_OP_TYPE_WRITE | RGW_OP_TYPE_DELETE)
158
159 #define RGW_DEFAULT_MAX_BUCKETS 1000
160
161 #define RGW_DEFER_TO_BUCKET_ACLS_RECURSE 1
162 #define RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL 2
163
164 #define STATUS_CREATED 1900
165 #define STATUS_ACCEPTED 1901
166 #define STATUS_NO_CONTENT 1902
167 #define STATUS_PARTIAL_CONTENT 1903
168 #define STATUS_REDIRECT 1904
169 #define STATUS_NO_APPLY 1905
170 #define STATUS_APPLIED 1906
171
172 #define ERR_INVALID_BUCKET_NAME 2000
173 #define ERR_INVALID_OBJECT_NAME 2001
174 #define ERR_NO_SUCH_BUCKET 2002
175 #define ERR_METHOD_NOT_ALLOWED 2003
176 #define ERR_INVALID_DIGEST 2004
177 #define ERR_BAD_DIGEST 2005
178 #define ERR_UNRESOLVABLE_EMAIL 2006
179 #define ERR_INVALID_PART 2007
180 #define ERR_INVALID_PART_ORDER 2008
181 #define ERR_NO_SUCH_UPLOAD 2009
182 #define ERR_REQUEST_TIMEOUT 2010
183 #define ERR_LENGTH_REQUIRED 2011
184 #define ERR_REQUEST_TIME_SKEWED 2012
185 #define ERR_BUCKET_EXISTS 2013
186 #define ERR_BAD_URL 2014
187 #define ERR_PRECONDITION_FAILED 2015
188 #define ERR_NOT_MODIFIED 2016
189 #define ERR_INVALID_UTF8 2017
190 #define ERR_UNPROCESSABLE_ENTITY 2018
191 #define ERR_TOO_LARGE 2019
192 #define ERR_TOO_MANY_BUCKETS 2020
193 #define ERR_INVALID_REQUEST 2021
194 #define ERR_TOO_SMALL 2022
195 #define ERR_NOT_FOUND 2023
196 #define ERR_PERMANENT_REDIRECT 2024
197 #define ERR_LOCKED 2025
198 #define ERR_QUOTA_EXCEEDED 2026
199 #define ERR_SIGNATURE_NO_MATCH 2027
200 #define ERR_INVALID_ACCESS_KEY 2028
201 #define ERR_MALFORMED_XML 2029
202 #define ERR_USER_EXIST 2030
203 #define ERR_NOT_SLO_MANIFEST 2031
204 #define ERR_EMAIL_EXIST 2032
205 #define ERR_KEY_EXIST 2033
206 #define ERR_INVALID_SECRET_KEY 2034
207 #define ERR_INVALID_KEY_TYPE 2035
208 #define ERR_INVALID_CAP 2036
209 #define ERR_INVALID_TENANT_NAME 2037
210 #define ERR_WEBSITE_REDIRECT 2038
211 #define ERR_NO_SUCH_WEBSITE_CONFIGURATION 2039
212 #define ERR_AMZ_CONTENT_SHA256_MISMATCH 2040
213 #define ERR_NO_SUCH_LC 2041
214 #define ERR_NO_SUCH_USER 2042
215 #define ERR_NO_SUCH_SUBUSER 2043
216 #define ERR_MFA_REQUIRED 2044
217 #define ERR_NO_SUCH_CORS_CONFIGURATION 2045
218 #define ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION 2046
219 #define ERR_INVALID_RETENTION_PERIOD 2047
220 #define ERR_USER_SUSPENDED 2100
221 #define ERR_INTERNAL_ERROR 2200
222 #define ERR_NOT_IMPLEMENTED 2201
223 #define ERR_SERVICE_UNAVAILABLE 2202
224 #define ERR_ROLE_EXISTS 2203
225 #define ERR_MALFORMED_DOC 2204
226 #define ERR_NO_ROLE_FOUND 2205
227 #define ERR_DELETE_CONFLICT 2206
228 #define ERR_NO_SUCH_BUCKET_POLICY 2207
229 #define ERR_INVALID_LOCATION_CONSTRAINT 2208
230 #define ERR_TAG_CONFLICT 2209
231 #define ERR_INVALID_TAG 2210
232 #define ERR_ZERO_IN_URL 2211
233 #define ERR_MALFORMED_ACL_ERROR 2212
234 #define ERR_ZONEGROUP_DEFAULT_PLACEMENT_MISCONFIGURATION 2213
235 #define ERR_INVALID_ENCRYPTION_ALGORITHM 2214
236 #define ERR_INVALID_CORS_RULES_ERROR 2215
237 #define ERR_NO_CORS_FOUND 2216
238 #define ERR_INVALID_WEBSITE_ROUTING_RULES_ERROR 2217
239 #define ERR_RATE_LIMITED 2218
240 #define ERR_POSITION_NOT_EQUAL_TO_LENGTH 2219
241 #define ERR_OBJECT_NOT_APPENDABLE 2220
242 #define ERR_INVALID_BUCKET_STATE 2221
243
244 #define ERR_BUSY_RESHARDING 2300
245 #define ERR_NO_SUCH_ENTITY 2301
246
247 // STS Errors
248 #define ERR_PACKED_POLICY_TOO_LARGE 2400
249 #define ERR_INVALID_IDENTITY_TOKEN 2401
250
251 #ifndef UINT32_MAX
252 #define UINT32_MAX (0xffffffffu)
253 #endif
254
255 struct req_state;
256
257 typedef void *RGWAccessHandle;
258
259 /* size should be the required string size + 1 */
260 int gen_rand_base64(CephContext *cct, char *dest, int size);
261 void gen_rand_alphanumeric(CephContext *cct, char *dest, int size);
262 void gen_rand_alphanumeric_lower(CephContext *cct, char *dest, int size);
263 void gen_rand_alphanumeric_upper(CephContext *cct, char *dest, int size);
264 void gen_rand_alphanumeric_no_underscore(CephContext *cct, char *dest, int size);
265 void gen_rand_alphanumeric_plain(CephContext *cct, char *dest, int size);
266 void gen_rand_alphanumeric_lower(CephContext *cct, string *str, int length);
267
268 enum RGWIntentEvent {
269 DEL_OBJ = 0,
270 DEL_DIR = 1,
271 };
272
273 enum HostStyle {
274 PathStyle = 0,
275 VirtualStyle = 1,
276 };
277
278 /** Store error returns for output at a different point in the program */
279 struct rgw_err {
280 rgw_err();
281 void clear();
282 bool is_clear() const;
283 bool is_err() const;
284 friend std::ostream& operator<<(std::ostream& oss, const rgw_err &err);
285
286 int http_ret;
287 int ret;
288 std::string err_code;
289 std::string message;
290 };
291
292
293
294 /* Helper class used for RGWHTTPArgs parsing */
295 class NameVal
296 {
297 const std::string str;
298 std::string name;
299 std::string val;
300 public:
301 explicit NameVal(const std::string& nv) : str(nv) {}
302
303 int parse();
304
305 std::string& get_name() { return name; }
306 std::string& get_val() { return val; }
307 };
308
309 /** Stores the XML arguments associated with the HTTP request in req_state*/
310 class RGWHTTPArgs {
311 std::string str, empty_str;
312 std::map<std::string, std::string> val_map;
313 std::map<std::string, std::string> sys_val_map;
314 std::map<std::string, std::string> sub_resources;
315 bool has_resp_modifier = false;
316 bool admin_subresource_added = false;
317 public:
318 RGWHTTPArgs() = default;
319 explicit RGWHTTPArgs(const std::string& s) {
320 set(s);
321 parse();
322 }
323
324 /** Set the arguments; as received */
325 void set(const std::string& s) {
326 has_resp_modifier = false;
327 val_map.clear();
328 sub_resources.clear();
329 str = s;
330 }
331 /** parse the received arguments */
332 int parse();
333 void append(const std::string& name, const string& val);
334 /** Get the value for a specific argument parameter */
335 const string& get(const std::string& name, bool *exists = NULL) const;
336 boost::optional<const std::string&>
337 get_optional(const std::string& name) const;
338 int get_bool(const std::string& name, bool *val, bool *exists);
339 int get_bool(const char *name, bool *val, bool *exists);
340 void get_bool(const char *name, bool *val, bool def_val);
341 int get_int(const char *name, int *val, int def_val);
342
343 /** Get the value for specific system argument parameter */
344 std::string sys_get(const std::string& name, bool *exists = nullptr) const;
345
346 /** see if a parameter is contained in this RGWHTTPArgs */
347 bool exists(const char *name) const {
348 return (val_map.find(name) != std::end(val_map));
349 }
350 bool sub_resource_exists(const char *name) const {
351 return (sub_resources.find(name) != std::end(sub_resources));
352 }
353 std::map<std::string, std::string>& get_params() {
354 return val_map;
355 }
356 const std::map<std::string, std::string>& get_sub_resources() const {
357 return sub_resources;
358 }
359 unsigned get_num_params() const {
360 return val_map.size();
361 }
362 bool has_response_modifier() const {
363 return has_resp_modifier;
364 }
365 void set_system() { /* make all system params visible */
366 std::map<std::string, std::string>::iterator iter;
367 for (iter = sys_val_map.begin(); iter != sys_val_map.end(); ++iter) {
368 val_map[iter->first] = iter->second;
369 }
370 }
371 const std::string& get_str() {
372 return str;
373 }
374 }; // RGWHTTPArgs
375
376 const char *rgw_conf_get(const map<string, string, ltstr_nocase>& conf_map, const char *name, const char *def_val);
377 int rgw_conf_get_int(const map<string, string, ltstr_nocase>& conf_map, const char *name, int def_val);
378 bool rgw_conf_get_bool(const map<string, string, ltstr_nocase>& conf_map, const char *name, bool def_val);
379
380 class RGWEnv;
381
382 class RGWConf {
383 friend class RGWEnv;
384 int enable_ops_log;
385 int enable_usage_log;
386 uint8_t defer_to_bucket_acls;
387 void init(CephContext *cct);
388 public:
389 RGWConf()
390 : enable_ops_log(1),
391 enable_usage_log(1),
392 defer_to_bucket_acls(0) {
393 }
394 };
395
396 class RGWEnv {
397 std::map<string, string, ltstr_nocase> env_map;
398 RGWConf conf;
399 public:
400 void init(CephContext *cct);
401 void init(CephContext *cct, char **envp);
402 void set(std::string name, std::string val);
403 const char *get(const char *name, const char *def_val = nullptr) const;
404 int get_int(const char *name, int def_val = 0) const;
405 bool get_bool(const char *name, bool def_val = 0);
406 size_t get_size(const char *name, size_t def_val = 0) const;
407 bool exists(const char *name) const;
408 bool exists_prefix(const char *prefix) const;
409 void remove(const char *name);
410 const std::map<string, string, ltstr_nocase>& get_map() const { return env_map; }
411 int get_enable_ops_log() const {
412 return conf.enable_ops_log;
413 }
414
415 int get_enable_usage_log() const {
416 return conf.enable_usage_log;
417 }
418
419 int get_defer_to_bucket_acls() const {
420 return conf.defer_to_bucket_acls;
421 }
422 };
423
424 // return true if the connection is secure. this either means that the
425 // connection arrived via ssl, or was forwarded as https by a trusted proxy
426 bool rgw_transport_is_secure(CephContext *cct, const RGWEnv& env);
427
428 enum http_op {
429 OP_GET,
430 OP_PUT,
431 OP_DELETE,
432 OP_HEAD,
433 OP_POST,
434 OP_COPY,
435 OP_OPTIONS,
436 OP_UNKNOWN,
437 };
438
439 enum RGWOpType {
440 RGW_OP_UNKNOWN = 0,
441 RGW_OP_GET_OBJ,
442 RGW_OP_LIST_BUCKETS,
443 RGW_OP_STAT_ACCOUNT,
444 RGW_OP_LIST_BUCKET,
445 RGW_OP_GET_BUCKET_LOGGING,
446 RGW_OP_GET_BUCKET_LOCATION,
447 RGW_OP_GET_BUCKET_VERSIONING,
448 RGW_OP_SET_BUCKET_VERSIONING,
449 RGW_OP_GET_BUCKET_WEBSITE,
450 RGW_OP_SET_BUCKET_WEBSITE,
451 RGW_OP_STAT_BUCKET,
452 RGW_OP_CREATE_BUCKET,
453 RGW_OP_DELETE_BUCKET,
454 RGW_OP_PUT_OBJ,
455 RGW_OP_STAT_OBJ,
456 RGW_OP_POST_OBJ,
457 RGW_OP_PUT_METADATA_ACCOUNT,
458 RGW_OP_PUT_METADATA_BUCKET,
459 RGW_OP_PUT_METADATA_OBJECT,
460 RGW_OP_SET_TEMPURL,
461 RGW_OP_DELETE_OBJ,
462 RGW_OP_COPY_OBJ,
463 RGW_OP_GET_ACLS,
464 RGW_OP_PUT_ACLS,
465 RGW_OP_GET_CORS,
466 RGW_OP_PUT_CORS,
467 RGW_OP_DELETE_CORS,
468 RGW_OP_OPTIONS_CORS,
469 RGW_OP_GET_REQUEST_PAYMENT,
470 RGW_OP_SET_REQUEST_PAYMENT,
471 RGW_OP_INIT_MULTIPART,
472 RGW_OP_COMPLETE_MULTIPART,
473 RGW_OP_ABORT_MULTIPART,
474 RGW_OP_LIST_MULTIPART,
475 RGW_OP_LIST_BUCKET_MULTIPARTS,
476 RGW_OP_DELETE_MULTI_OBJ,
477 RGW_OP_BULK_DELETE,
478 RGW_OP_SET_ATTRS,
479 RGW_OP_GET_CROSS_DOMAIN_POLICY,
480 RGW_OP_GET_HEALTH_CHECK,
481 RGW_OP_GET_INFO,
482 RGW_OP_CREATE_ROLE,
483 RGW_OP_DELETE_ROLE,
484 RGW_OP_GET_ROLE,
485 RGW_OP_MODIFY_ROLE,
486 RGW_OP_LIST_ROLES,
487 RGW_OP_PUT_ROLE_POLICY,
488 RGW_OP_GET_ROLE_POLICY,
489 RGW_OP_LIST_ROLE_POLICIES,
490 RGW_OP_DELETE_ROLE_POLICY,
491 RGW_OP_PUT_BUCKET_POLICY,
492 RGW_OP_GET_BUCKET_POLICY,
493 RGW_OP_DELETE_BUCKET_POLICY,
494 RGW_OP_PUT_OBJ_TAGGING,
495 RGW_OP_GET_OBJ_TAGGING,
496 RGW_OP_DELETE_OBJ_TAGGING,
497 RGW_OP_PUT_LC,
498 RGW_OP_GET_LC,
499 RGW_OP_DELETE_LC,
500 RGW_OP_PUT_USER_POLICY,
501 RGW_OP_GET_USER_POLICY,
502 RGW_OP_LIST_USER_POLICIES,
503 RGW_OP_DELETE_USER_POLICY,
504 RGW_OP_PUT_BUCKET_OBJ_LOCK,
505 RGW_OP_GET_BUCKET_OBJ_LOCK,
506 RGW_OP_PUT_OBJ_RETENTION,
507 RGW_OP_GET_OBJ_RETENTION,
508 RGW_OP_PUT_OBJ_LEGAL_HOLD,
509 RGW_OP_GET_OBJ_LEGAL_HOLD,
510 /* rgw specific */
511 RGW_OP_ADMIN_SET_METADATA,
512 RGW_OP_GET_OBJ_LAYOUT,
513 RGW_OP_BULK_UPLOAD,
514 RGW_OP_METADATA_SEARCH,
515 RGW_OP_CONFIG_BUCKET_META_SEARCH,
516 RGW_OP_GET_BUCKET_META_SEARCH,
517 RGW_OP_DEL_BUCKET_META_SEARCH,
518 /* sts specific*/
519 RGW_STS_ASSUME_ROLE,
520 RGW_STS_GET_SESSION_TOKEN,
521 RGW_STS_ASSUME_ROLE_WEB_IDENTITY,
522 /* pubsub */
523 RGW_OP_PUBSUB_TOPIC_CREATE,
524 RGW_OP_PUBSUB_TOPICS_LIST,
525 RGW_OP_PUBSUB_TOPIC_GET,
526 RGW_OP_PUBSUB_TOPIC_DELETE,
527 RGW_OP_PUBSUB_SUB_CREATE,
528 RGW_OP_PUBSUB_SUB_GET,
529 RGW_OP_PUBSUB_SUB_DELETE,
530 RGW_OP_PUBSUB_SUB_PULL,
531 RGW_OP_PUBSUB_SUB_ACK,
532 RGW_OP_PUBSUB_NOTIF_CREATE,
533 RGW_OP_PUBSUB_NOTIF_DELETE,
534 RGW_OP_PUBSUB_NOTIF_LIST,
535 };
536
537 class RGWAccessControlPolicy;
538 class JSONObj;
539
540 struct RGWAccessKey {
541 string id; // AccessKey
542 string key; // SecretKey
543 string subuser;
544
545 RGWAccessKey() {}
546 RGWAccessKey(std::string _id, std::string _key)
547 : id(std::move(_id)), key(std::move(_key)) {}
548
549 void encode(bufferlist& bl) const {
550 ENCODE_START(2, 2, bl);
551 encode(id, bl);
552 encode(key, bl);
553 encode(subuser, bl);
554 ENCODE_FINISH(bl);
555 }
556
557 void decode(bufferlist::const_iterator& bl) {
558 DECODE_START_LEGACY_COMPAT_LEN_32(2, 2, 2, bl);
559 decode(id, bl);
560 decode(key, bl);
561 decode(subuser, bl);
562 DECODE_FINISH(bl);
563 }
564 void dump(Formatter *f) const;
565 void dump_plain(Formatter *f) const;
566 void dump(Formatter *f, const string& user, bool swift) const;
567 static void generate_test_instances(list<RGWAccessKey*>& o);
568
569 void decode_json(JSONObj *obj);
570 void decode_json(JSONObj *obj, bool swift);
571 };
572 WRITE_CLASS_ENCODER(RGWAccessKey)
573
574 struct RGWSubUser {
575 string name;
576 uint32_t perm_mask;
577
578 RGWSubUser() : perm_mask(0) {}
579 void encode(bufferlist& bl) const {
580 ENCODE_START(2, 2, bl);
581 encode(name, bl);
582 encode(perm_mask, bl);
583 ENCODE_FINISH(bl);
584 }
585
586 void decode(bufferlist::const_iterator& bl) {
587 DECODE_START_LEGACY_COMPAT_LEN_32(2, 2, 2, bl);
588 decode(name, bl);
589 decode(perm_mask, bl);
590 DECODE_FINISH(bl);
591 }
592 void dump(Formatter *f) const;
593 void dump(Formatter *f, const string& user) const;
594 static void generate_test_instances(list<RGWSubUser*>& o);
595
596 void decode_json(JSONObj *obj);
597 };
598 WRITE_CLASS_ENCODER(RGWSubUser)
599
600 class RGWUserCaps
601 {
602 map<string, uint32_t> caps;
603
604 int get_cap(const string& cap, string& type, uint32_t *perm);
605 int add_cap(const string& cap);
606 int remove_cap(const string& cap);
607 public:
608 static int parse_cap_perm(const string& str, uint32_t *perm);
609 int add_from_string(const string& str);
610 int remove_from_string(const string& str);
611
612 void encode(bufferlist& bl) const {
613 ENCODE_START(1, 1, bl);
614 encode(caps, bl);
615 ENCODE_FINISH(bl);
616 }
617 void decode(bufferlist::const_iterator& bl) {
618 DECODE_START(1, bl);
619 decode(caps, bl);
620 DECODE_FINISH(bl);
621 }
622 int check_cap(const string& cap, uint32_t perm);
623 bool is_valid_cap_type(const string& tp);
624 void dump(Formatter *f) const;
625 void dump(Formatter *f, const char *name) const;
626
627 void decode_json(JSONObj *obj);
628 };
629 WRITE_CLASS_ENCODER(RGWUserCaps)
630
631 void encode_json(const char *name, const obj_version& v, Formatter *f);
632 void encode_json(const char *name, const RGWUserCaps& val, Formatter *f);
633
634 void decode_json_obj(obj_version& v, JSONObj *obj);
635
636
637
638 enum RGWIdentityType
639 {
640 TYPE_NONE=0,
641 TYPE_RGW=1,
642 TYPE_KEYSTONE=2,
643 TYPE_LDAP=3,
644 TYPE_ROLE=4,
645 TYPE_WEB=5,
646 };
647
648 static string RGW_STORAGE_CLASS_STANDARD = "STANDARD";
649
650 struct rgw_placement_rule {
651 std::string name;
652 std::string storage_class;
653
654 rgw_placement_rule() {}
655 rgw_placement_rule(const string& _n, const string& _sc) : name(_n), storage_class(_sc) {}
656 rgw_placement_rule(const rgw_placement_rule& _r, const string& _sc) : name(_r.name) {
657 if (!_sc.empty()) {
658 storage_class = _sc;
659 } else {
660 storage_class = _r.storage_class;
661 }
662 }
663
664 bool empty() const {
665 return name.empty() && storage_class.empty();
666 }
667
668 void inherit_from(const rgw_placement_rule& r) {
669 if (name.empty()) {
670 name = r.name;
671 }
672 if (storage_class.empty()) {
673 storage_class = r.storage_class;
674 }
675 }
676
677 void clear() {
678 name.clear();
679 storage_class.clear();
680 }
681
682 void init(const string& n, const string& c) {
683 name = n;
684 storage_class = c;
685 }
686
687 static const string& get_canonical_storage_class(const string& storage_class) {
688 if (storage_class.empty()) {
689 return RGW_STORAGE_CLASS_STANDARD;
690 }
691 return storage_class;
692 }
693
694 const string& get_storage_class() const {
695 return get_canonical_storage_class(storage_class);
696 }
697
698 int compare(const rgw_placement_rule& r) const {
699 int c = name.compare(r.name);
700 if (c != 0) {
701 return c;
702 }
703 return get_storage_class().compare(r.get_storage_class());
704 }
705
706 bool operator==(const rgw_placement_rule& r) const {
707 return (name == r.name &&
708 get_storage_class() == r.get_storage_class());
709 }
710
711 bool operator!=(const rgw_placement_rule& r) const {
712 return !(*this == r);
713 }
714
715 void encode(bufferlist& bl) const {
716 /* no ENCODE_START/END due to backward compatibility */
717 std::string s = to_str();
718 ceph::encode(s, bl);
719 }
720
721 void decode(bufferlist::const_iterator& bl) {
722 std::string s;
723 ceph::decode(s, bl);
724 from_str(s);
725 }
726
727 std::string to_str() const {
728 if (standard_storage_class()) {
729 return name;
730 }
731 return to_str_explicit();
732 }
733
734 std::string to_str_explicit() const {
735 return name + "/" + storage_class;
736 }
737
738 void from_str(const std::string& s) {
739 size_t pos = s.find("/");
740 if (pos == std::string::npos) {
741 name = s;
742 storage_class.clear();
743 return;
744 }
745 name = s.substr(0, pos);
746 storage_class = s.substr(pos + 1);
747 }
748
749 bool standard_storage_class() const {
750 return storage_class.empty() || storage_class == RGW_STORAGE_CLASS_STANDARD;
751 }
752 };
753 WRITE_CLASS_ENCODER(rgw_placement_rule)
754
755 void encode_json(const char *name, const rgw_placement_rule& val, ceph::Formatter *f);
756 void decode_json_obj(rgw_placement_rule& v, JSONObj *obj);
757
758 inline ostream& operator<<(ostream& out, const rgw_placement_rule& rule) {
759 return out << rule.to_str();
760 }
761 struct RGWUserInfo
762 {
763 rgw_user user_id;
764 string display_name;
765 string user_email;
766 map<string, RGWAccessKey> access_keys;
767 map<string, RGWAccessKey> swift_keys;
768 map<string, RGWSubUser> subusers;
769 __u8 suspended;
770 int32_t max_buckets;
771 uint32_t op_mask;
772 RGWUserCaps caps;
773 __u8 admin;
774 __u8 system;
775 rgw_placement_rule default_placement;
776 list<string> placement_tags;
777 RGWQuotaInfo bucket_quota;
778 map<int, string> temp_url_keys;
779 RGWQuotaInfo user_quota;
780 uint32_t type;
781 set<string> mfa_ids;
782 string assumed_role_arn;
783
784 RGWUserInfo()
785 : suspended(0),
786 max_buckets(RGW_DEFAULT_MAX_BUCKETS),
787 op_mask(RGW_OP_TYPE_ALL),
788 admin(0),
789 system(0),
790 type(TYPE_NONE) {
791 }
792
793 RGWAccessKey* get_key(const string& access_key) {
794 if (access_keys.empty())
795 return nullptr;
796
797 auto k = access_keys.find(access_key);
798 if (k == access_keys.end())
799 return nullptr;
800 else
801 return &(k->second);
802 }
803
804 void encode(bufferlist& bl) const {
805 ENCODE_START(21, 9, bl);
806 encode((uint64_t)0, bl); // old auid
807 string access_key;
808 string secret_key;
809 if (!access_keys.empty()) {
810 map<string, RGWAccessKey>::const_iterator iter = access_keys.begin();
811 const RGWAccessKey& k = iter->second;
812 access_key = k.id;
813 secret_key = k.key;
814 }
815 encode(access_key, bl);
816 encode(secret_key, bl);
817 encode(display_name, bl);
818 encode(user_email, bl);
819 string swift_name;
820 string swift_key;
821 if (!swift_keys.empty()) {
822 map<string, RGWAccessKey>::const_iterator iter = swift_keys.begin();
823 const RGWAccessKey& k = iter->second;
824 swift_name = k.id;
825 swift_key = k.key;
826 }
827 encode(swift_name, bl);
828 encode(swift_key, bl);
829 encode(user_id.id, bl);
830 encode(access_keys, bl);
831 encode(subusers, bl);
832 encode(suspended, bl);
833 encode(swift_keys, bl);
834 encode(max_buckets, bl);
835 encode(caps, bl);
836 encode(op_mask, bl);
837 encode(system, bl);
838 encode(default_placement, bl);
839 encode(placement_tags, bl);
840 encode(bucket_quota, bl);
841 encode(temp_url_keys, bl);
842 encode(user_quota, bl);
843 encode(user_id.tenant, bl);
844 encode(admin, bl);
845 encode(type, bl);
846 encode(mfa_ids, bl);
847 encode(assumed_role_arn, bl);
848 ENCODE_FINISH(bl);
849 }
850 void decode(bufferlist::const_iterator& bl) {
851 DECODE_START_LEGACY_COMPAT_LEN_32(20, 9, 9, bl);
852 if (struct_v >= 2) {
853 uint64_t old_auid;
854 decode(old_auid, bl);
855 }
856 string access_key;
857 string secret_key;
858 decode(access_key, bl);
859 decode(secret_key, bl);
860 if (struct_v < 6) {
861 RGWAccessKey k;
862 k.id = access_key;
863 k.key = secret_key;
864 access_keys[access_key] = k;
865 }
866 decode(display_name, bl);
867 decode(user_email, bl);
868 /* We populate swift_keys map later nowadays, but we have to decode. */
869 string swift_name;
870 string swift_key;
871 if (struct_v >= 3) decode(swift_name, bl);
872 if (struct_v >= 4) decode(swift_key, bl);
873 if (struct_v >= 5)
874 decode(user_id.id, bl);
875 else
876 user_id.id = access_key;
877 if (struct_v >= 6) {
878 decode(access_keys, bl);
879 decode(subusers, bl);
880 }
881 suspended = 0;
882 if (struct_v >= 7) {
883 decode(suspended, bl);
884 }
885 if (struct_v >= 8) {
886 decode(swift_keys, bl);
887 }
888 if (struct_v >= 10) {
889 decode(max_buckets, bl);
890 } else {
891 max_buckets = RGW_DEFAULT_MAX_BUCKETS;
892 }
893 if (struct_v >= 11) {
894 decode(caps, bl);
895 }
896 if (struct_v >= 12) {
897 decode(op_mask, bl);
898 } else {
899 op_mask = RGW_OP_TYPE_ALL;
900 }
901 if (struct_v >= 13) {
902 decode(system, bl);
903 decode(default_placement, bl);
904 decode(placement_tags, bl); /* tags of allowed placement rules */
905 }
906 if (struct_v >= 14) {
907 decode(bucket_quota, bl);
908 }
909 if (struct_v >= 15) {
910 decode(temp_url_keys, bl);
911 }
912 if (struct_v >= 16) {
913 decode(user_quota, bl);
914 }
915 if (struct_v >= 17) {
916 decode(user_id.tenant, bl);
917 } else {
918 user_id.tenant.clear();
919 }
920 if (struct_v >= 18) {
921 decode(admin, bl);
922 }
923 if (struct_v >= 19) {
924 decode(type, bl);
925 }
926 if (struct_v >= 20) {
927 decode(mfa_ids, bl);
928 }
929 if (struct_v >= 21) {
930 decode(assumed_role_arn, bl);
931 }
932 DECODE_FINISH(bl);
933 }
934 void dump(Formatter *f) const;
935 static void generate_test_instances(list<RGWUserInfo*>& o);
936
937 void decode_json(JSONObj *obj);
938 };
939 WRITE_CLASS_ENCODER(RGWUserInfo)
940
941 struct rgw_pool {
942 std::string name;
943 std::string ns;
944
945 rgw_pool() = default;
946 rgw_pool(const rgw_pool& _p) : name(_p.name), ns(_p.ns) {}
947 rgw_pool(rgw_pool&&) = default;
948 rgw_pool(const string& _s) {
949 from_str(_s);
950 }
951 rgw_pool(const string& _name, const string& _ns) : name(_name), ns(_ns) {}
952
953 string to_str() const;
954 void from_str(const string& s);
955
956 void init(const string& _s) {
957 from_str(_s);
958 }
959
960 bool empty() const {
961 return name.empty();
962 }
963
964 int compare(const rgw_pool& p) const {
965 int r = name.compare(p.name);
966 if (r != 0) {
967 return r;
968 }
969 return ns.compare(p.ns);
970 }
971
972 void encode(bufferlist& bl) const {
973 ENCODE_START(10, 10, bl);
974 encode(name, bl);
975 encode(ns, bl);
976 ENCODE_FINISH(bl);
977 }
978
979 void decode_from_bucket(bufferlist::const_iterator& bl);
980
981 void decode(bufferlist::const_iterator& bl) {
982 DECODE_START_LEGACY_COMPAT_LEN(10, 3, 3, bl);
983
984 decode(name, bl);
985
986 if (struct_v < 10) {
987
988 /*
989 * note that rgw_pool can be used where rgw_bucket was used before
990 * therefore we inherit rgw_bucket's old versions. However, we only
991 * need the first field from rgw_bucket. unless we add more fields
992 * in which case we'll need to look at struct_v, and check the actual
993 * version. Anything older than 10 needs to be treated as old rgw_bucket
994 */
995
996 } else {
997 decode(ns, bl);
998 }
999
1000 DECODE_FINISH(bl);
1001 }
1002
1003 rgw_pool& operator=(const rgw_pool&) = default;
1004
1005 bool operator==(const rgw_pool& p) const {
1006 return (compare(p) == 0);
1007 }
1008 bool operator!=(const rgw_pool& p) const {
1009 return !(*this == p);
1010 }
1011 bool operator<(const rgw_pool& p) const {
1012 int r = name.compare(p.name);
1013 if (r == 0) {
1014 return (ns.compare(p.ns) < 0);
1015 }
1016 return (r < 0);
1017 }
1018 };
1019 WRITE_CLASS_ENCODER(rgw_pool)
1020
1021 struct rgw_data_placement_target {
1022 rgw_pool data_pool;
1023 rgw_pool data_extra_pool;
1024 rgw_pool index_pool;
1025
1026 rgw_data_placement_target() = default;
1027 rgw_data_placement_target(const rgw_data_placement_target&) = default;
1028 rgw_data_placement_target(rgw_data_placement_target&&) = default;
1029
1030 rgw_data_placement_target(const rgw_pool& data_pool,
1031 const rgw_pool& data_extra_pool,
1032 const rgw_pool& index_pool)
1033 : data_pool(data_pool),
1034 data_extra_pool(data_extra_pool),
1035 index_pool(index_pool) {
1036 }
1037
1038 rgw_data_placement_target&
1039 operator=(const rgw_data_placement_target&) = default;
1040
1041 const rgw_pool& get_data_extra_pool() const {
1042 if (data_extra_pool.empty()) {
1043 return data_pool;
1044 }
1045 return data_extra_pool;
1046 }
1047
1048 int compare(const rgw_data_placement_target& t) {
1049 int c = data_pool.compare(t.data_pool);
1050 if (c != 0) {
1051 return c;
1052 }
1053 c = data_extra_pool.compare(t.data_extra_pool);
1054 if (c != 0) {
1055 return c;
1056 }
1057 return index_pool.compare(t.index_pool);
1058 };
1059
1060 void dump(Formatter *f) const;
1061 void decode_json(JSONObj *obj);
1062 };
1063
1064 inline ostream& operator<<(ostream& out, const rgw_pool& p) {
1065 out << p.to_str();
1066 return out;
1067 }
1068
1069 struct rgw_raw_obj {
1070 rgw_pool pool;
1071 std::string oid;
1072 std::string loc;
1073
1074 rgw_raw_obj() {}
1075 rgw_raw_obj(const rgw_pool& _pool, const std::string& _oid) {
1076 init(_pool, _oid);
1077 }
1078 rgw_raw_obj(const rgw_pool& _pool, const std::string& _oid, const string& _loc) : loc(_loc) {
1079 init(_pool, _oid);
1080 }
1081
1082 void init(const rgw_pool& _pool, const std::string& _oid) {
1083 pool = _pool;
1084 oid = _oid;
1085 }
1086
1087 bool empty() const {
1088 return oid.empty();
1089 }
1090
1091 void encode(bufferlist& bl) const {
1092 ENCODE_START(6, 6, bl);
1093 encode(pool, bl);
1094 encode(oid, bl);
1095 encode(loc, bl);
1096 ENCODE_FINISH(bl);
1097 }
1098
1099 void decode_from_rgw_obj(bufferlist::const_iterator& bl);
1100
1101 void decode(bufferlist::const_iterator& bl) {
1102 unsigned ofs = bl.get_off();
1103 DECODE_START(6, bl);
1104 if (struct_v < 6) {
1105 /*
1106 * this object was encoded as rgw_obj, prior to rgw_raw_obj been split out of it,
1107 * let's decode it as rgw_obj and convert it
1108 */
1109 bl.seek(ofs);
1110 decode_from_rgw_obj(bl);
1111 return;
1112 }
1113 decode(pool, bl);
1114 decode(oid, bl);
1115 decode(loc, bl);
1116 DECODE_FINISH(bl);
1117 }
1118
1119 bool operator<(const rgw_raw_obj& o) const {
1120 int r = pool.compare(o.pool);
1121 if (r == 0) {
1122 r = oid.compare(o.oid);
1123 if (r == 0) {
1124 r = loc.compare(o.loc);
1125 }
1126 }
1127 return (r < 0);
1128 }
1129
1130 bool operator==(const rgw_raw_obj& o) const {
1131 return (pool == o.pool && oid == o.oid && loc == o.loc);
1132 }
1133
1134 void dump(Formatter *f) const;
1135 void decode_json(JSONObj *obj);
1136 };
1137 WRITE_CLASS_ENCODER(rgw_raw_obj)
1138
1139 inline ostream& operator<<(ostream& out, const rgw_raw_obj& o) {
1140 out << o.pool << ":" << o.oid;
1141 return out;
1142 }
1143
1144 struct rgw_bucket {
1145 std::string tenant;
1146 std::string name;
1147 std::string marker;
1148 std::string bucket_id;
1149 rgw_data_placement_target explicit_placement;
1150
1151 std::string oid; /*
1152 * runtime in-memory only info. If not empty, points to the bucket instance object
1153 */
1154
1155 rgw_bucket() { }
1156 // cppcheck-suppress noExplicitConstructor
1157 explicit rgw_bucket(const rgw_user& u, const cls_user_bucket& b) :
1158 tenant(u.tenant),
1159 name(b.name),
1160 marker(b.marker),
1161 bucket_id(b.bucket_id),
1162 explicit_placement(b.explicit_placement.data_pool,
1163 b.explicit_placement.data_extra_pool,
1164 b.explicit_placement.index_pool) {}
1165 rgw_bucket(const rgw_bucket&) = default;
1166 rgw_bucket(rgw_bucket&&) = default;
1167
1168 void convert(cls_user_bucket *b) const {
1169 b->name = name;
1170 b->marker = marker;
1171 b->bucket_id = bucket_id;
1172 b->explicit_placement.data_pool = explicit_placement.data_pool.to_str();
1173 b->explicit_placement.data_extra_pool = explicit_placement.data_extra_pool.to_str();
1174 b->explicit_placement.index_pool = explicit_placement.index_pool.to_str();
1175 }
1176
1177 void encode(bufferlist& bl) const {
1178 ENCODE_START(10, 10, bl);
1179 encode(name, bl);
1180 encode(marker, bl);
1181 encode(bucket_id, bl);
1182 encode(tenant, bl);
1183 bool encode_explicit = !explicit_placement.data_pool.empty();
1184 encode(encode_explicit, bl);
1185 if (encode_explicit) {
1186 encode(explicit_placement.data_pool, bl);
1187 encode(explicit_placement.data_extra_pool, bl);
1188 encode(explicit_placement.index_pool, bl);
1189 }
1190 ENCODE_FINISH(bl);
1191 }
1192 void decode(bufferlist::const_iterator& bl) {
1193 DECODE_START_LEGACY_COMPAT_LEN(10, 3, 3, bl);
1194 decode(name, bl);
1195 if (struct_v < 10) {
1196 decode(explicit_placement.data_pool.name, bl);
1197 }
1198 if (struct_v >= 2) {
1199 decode(marker, bl);
1200 if (struct_v <= 3) {
1201 uint64_t id;
1202 decode(id, bl);
1203 char buf[16];
1204 snprintf(buf, sizeof(buf), "%" PRIu64, id);
1205 bucket_id = buf;
1206 } else {
1207 decode(bucket_id, bl);
1208 }
1209 }
1210 if (struct_v < 10) {
1211 if (struct_v >= 5) {
1212 decode(explicit_placement.index_pool.name, bl);
1213 } else {
1214 explicit_placement.index_pool = explicit_placement.data_pool;
1215 }
1216 if (struct_v >= 7) {
1217 decode(explicit_placement.data_extra_pool.name, bl);
1218 }
1219 }
1220 if (struct_v >= 8) {
1221 decode(tenant, bl);
1222 }
1223 if (struct_v >= 10) {
1224 bool decode_explicit = !explicit_placement.data_pool.empty();
1225 decode(decode_explicit, bl);
1226 if (decode_explicit) {
1227 decode(explicit_placement.data_pool, bl);
1228 decode(explicit_placement.data_extra_pool, bl);
1229 decode(explicit_placement.index_pool, bl);
1230 }
1231 }
1232 DECODE_FINISH(bl);
1233 }
1234
1235 void update_bucket_id(const string& new_bucket_id) {
1236 bucket_id = new_bucket_id;
1237 oid.clear();
1238 }
1239
1240 // format a key for the bucket/instance. pass delim=0 to skip a field
1241 std::string get_key(char tenant_delim = '/',
1242 char id_delim = ':',
1243 size_t reserve = 0) const;
1244
1245 const rgw_pool& get_data_extra_pool() const {
1246 return explicit_placement.get_data_extra_pool();
1247 }
1248
1249 void dump(Formatter *f) const;
1250 void decode_json(JSONObj *obj);
1251 static void generate_test_instances(list<rgw_bucket*>& o);
1252
1253 rgw_bucket& operator=(const rgw_bucket&) = default;
1254
1255 bool operator<(const rgw_bucket& b) const {
1256 if (tenant == b.tenant) {
1257 return name < b.name;
1258 } else {
1259 return tenant < b.tenant;
1260 }
1261 }
1262
1263 bool operator==(const rgw_bucket& b) const {
1264 return (tenant == b.tenant) && (name == b.name) && \
1265 (bucket_id == b.bucket_id);
1266 }
1267 };
1268 WRITE_CLASS_ENCODER(rgw_bucket)
1269
1270 inline ostream& operator<<(ostream& out, const rgw_bucket &b) {
1271 out << b.name << "[" << b.marker << "]";
1272 return out;
1273 }
1274
1275 struct rgw_bucket_shard {
1276 rgw_bucket bucket;
1277 int shard_id;
1278
1279 rgw_bucket_shard() : shard_id(-1) {}
1280 rgw_bucket_shard(const rgw_bucket& _b, int _sid) : bucket(_b), shard_id(_sid) {}
1281
1282 std::string get_key(char tenant_delim = '/', char id_delim = ':',
1283 char shard_delim = ':') const;
1284
1285 bool operator<(const rgw_bucket_shard& b) const {
1286 if (bucket < b.bucket) {
1287 return true;
1288 }
1289 if (b.bucket < bucket) {
1290 return false;
1291 }
1292 return shard_id < b.shard_id;
1293 }
1294 };
1295
1296
1297 struct RGWObjVersionTracker {
1298 obj_version read_version;
1299 obj_version write_version;
1300
1301 obj_version *version_for_read() {
1302 return &read_version;
1303 }
1304
1305 obj_version *version_for_write() {
1306 if (write_version.ver == 0)
1307 return NULL;
1308
1309 return &write_version;
1310 }
1311
1312 obj_version *version_for_check() {
1313 if (read_version.ver == 0)
1314 return NULL;
1315
1316 return &read_version;
1317 }
1318
1319 void prepare_op_for_read(librados::ObjectReadOperation *op);
1320 void prepare_op_for_write(librados::ObjectWriteOperation *op);
1321
1322 void apply_write() {
1323 read_version = write_version;
1324 write_version = obj_version();
1325 }
1326
1327 void clear() {
1328 read_version = obj_version();
1329 write_version = obj_version();
1330 }
1331
1332 void generate_new_write_ver(CephContext *cct);
1333 };
1334
1335 inline ostream& operator<<(ostream& out, const obj_version &v)
1336 {
1337 out << v.tag << ":" << v.ver;
1338 return out;
1339 }
1340
1341 inline ostream& operator<<(ostream& out, const RGWObjVersionTracker &ot)
1342 {
1343 out << "{r=" << ot.read_version << ",w=" << ot.write_version << "}";
1344 return out;
1345 }
1346
1347 enum RGWBucketFlags {
1348 BUCKET_SUSPENDED = 0x1,
1349 BUCKET_VERSIONED = 0x2,
1350 BUCKET_VERSIONS_SUSPENDED = 0x4,
1351 BUCKET_DATASYNC_DISABLED = 0X8,
1352 BUCKET_MFA_ENABLED = 0X10,
1353 BUCKET_OBJ_LOCK_ENABLED = 0X20,
1354 };
1355
1356 enum RGWBucketIndexType {
1357 RGWBIType_Normal = 0,
1358 RGWBIType_Indexless = 1,
1359 };
1360
1361 inline ostream& operator<<(ostream& out, const RGWBucketIndexType &index_type)
1362 {
1363 switch (index_type) {
1364 case RGWBIType_Normal:
1365 return out << "Normal";
1366 case RGWBIType_Indexless:
1367 return out << "Indexless";
1368 default:
1369 return out << "Unknown";
1370 }
1371 }
1372
1373 struct RGWBucketInfo {
1374 enum BIShardsHashType {
1375 MOD = 0
1376 };
1377
1378 rgw_bucket bucket;
1379 rgw_user owner;
1380 uint32_t flags;
1381 string zonegroup;
1382 ceph::real_time creation_time;
1383 rgw_placement_rule placement_rule;
1384 bool has_instance_obj;
1385 RGWObjVersionTracker objv_tracker; /* we don't need to serialize this, for runtime tracking */
1386 obj_version ep_objv; /* entry point object version, for runtime tracking only */
1387 RGWQuotaInfo quota;
1388
1389 // Represents the number of bucket index object shards:
1390 // - value of 0 indicates there is no sharding (this is by default before this
1391 // feature is implemented).
1392 // - value of UINT32_T::MAX indicates this is a blind bucket.
1393 uint32_t num_shards;
1394
1395 // Represents the bucket index shard hash type.
1396 uint8_t bucket_index_shard_hash_type;
1397
1398 // Represents the shard number for blind bucket.
1399 const static uint32_t NUM_SHARDS_BLIND_BUCKET;
1400
1401 bool requester_pays;
1402
1403 bool has_website;
1404 RGWBucketWebsiteConf website_conf;
1405
1406 RGWBucketIndexType index_type = RGWBIType_Normal;
1407
1408 bool swift_versioning;
1409 string swift_ver_location;
1410
1411 map<string, uint32_t> mdsearch_config;
1412
1413
1414
1415 /* resharding */
1416 uint8_t reshard_status;
1417 string new_bucket_instance_id;
1418
1419 RGWObjectLock obj_lock;
1420
1421 void encode(bufferlist& bl) const {
1422 ENCODE_START(20, 4, bl);
1423 encode(bucket, bl);
1424 encode(owner.id, bl);
1425 encode(flags, bl);
1426 encode(zonegroup, bl);
1427 uint64_t ct = real_clock::to_time_t(creation_time);
1428 encode(ct, bl);
1429 encode(placement_rule, bl);
1430 encode(has_instance_obj, bl);
1431 encode(quota, bl);
1432 encode(num_shards, bl);
1433 encode(bucket_index_shard_hash_type, bl);
1434 encode(requester_pays, bl);
1435 encode(owner.tenant, bl);
1436 encode(has_website, bl);
1437 if (has_website) {
1438 encode(website_conf, bl);
1439 }
1440 encode((uint32_t)index_type, bl);
1441 encode(swift_versioning, bl);
1442 if (swift_versioning) {
1443 encode(swift_ver_location, bl);
1444 }
1445 encode(creation_time, bl);
1446 encode(mdsearch_config, bl);
1447 encode(reshard_status, bl);
1448 encode(new_bucket_instance_id, bl);
1449 if (obj_lock_enabled()) {
1450 encode(obj_lock, bl);
1451 }
1452 ENCODE_FINISH(bl);
1453 }
1454 void decode(bufferlist::const_iterator& bl) {
1455 DECODE_START_LEGACY_COMPAT_LEN_32(20, 4, 4, bl);
1456 decode(bucket, bl);
1457 if (struct_v >= 2) {
1458 string s;
1459 decode(s, bl);
1460 owner.from_str(s);
1461 }
1462 if (struct_v >= 3)
1463 decode(flags, bl);
1464 if (struct_v >= 5)
1465 decode(zonegroup, bl);
1466 if (struct_v >= 6) {
1467 uint64_t ct;
1468 decode(ct, bl);
1469 if (struct_v < 17)
1470 creation_time = ceph::real_clock::from_time_t((time_t)ct);
1471 }
1472 if (struct_v >= 7)
1473 decode(placement_rule, bl);
1474 if (struct_v >= 8)
1475 decode(has_instance_obj, bl);
1476 if (struct_v >= 9)
1477 decode(quota, bl);
1478 if (struct_v >= 10)
1479 decode(num_shards, bl);
1480 if (struct_v >= 11)
1481 decode(bucket_index_shard_hash_type, bl);
1482 if (struct_v >= 12)
1483 decode(requester_pays, bl);
1484 if (struct_v >= 13)
1485 decode(owner.tenant, bl);
1486 if (struct_v >= 14) {
1487 decode(has_website, bl);
1488 if (has_website) {
1489 decode(website_conf, bl);
1490 } else {
1491 website_conf = RGWBucketWebsiteConf();
1492 }
1493 }
1494 if (struct_v >= 15) {
1495 uint32_t it;
1496 decode(it, bl);
1497 index_type = (RGWBucketIndexType)it;
1498 } else {
1499 index_type = RGWBIType_Normal;
1500 }
1501 swift_versioning = false;
1502 swift_ver_location.clear();
1503 if (struct_v >= 16) {
1504 decode(swift_versioning, bl);
1505 if (swift_versioning) {
1506 decode(swift_ver_location, bl);
1507 }
1508 }
1509 if (struct_v >= 17) {
1510 decode(creation_time, bl);
1511 }
1512 if (struct_v >= 18) {
1513 decode(mdsearch_config, bl);
1514 }
1515 if (struct_v >= 19) {
1516 decode(reshard_status, bl);
1517 decode(new_bucket_instance_id, bl);
1518 }
1519 if (struct_v >= 20 && obj_lock_enabled()) {
1520 decode(obj_lock, bl);
1521 }
1522 DECODE_FINISH(bl);
1523 }
1524 void dump(Formatter *f) const;
1525 static void generate_test_instances(list<RGWBucketInfo*>& o);
1526
1527 void decode_json(JSONObj *obj);
1528
1529 bool versioned() const { return (flags & BUCKET_VERSIONED) != 0; }
1530 int versioning_status() const { return flags & (BUCKET_VERSIONED | BUCKET_VERSIONS_SUSPENDED | BUCKET_MFA_ENABLED); }
1531 bool versioning_enabled() const { return (versioning_status() & (BUCKET_VERSIONED | BUCKET_VERSIONS_SUSPENDED)) == BUCKET_VERSIONED; }
1532 bool mfa_enabled() const { return (versioning_status() & BUCKET_MFA_ENABLED) != 0; }
1533 bool datasync_flag_enabled() const { return (flags & BUCKET_DATASYNC_DISABLED) == 0; }
1534 bool obj_lock_enabled() const { return (flags & BUCKET_OBJ_LOCK_ENABLED) != 0; }
1535
1536 bool has_swift_versioning() const {
1537 /* A bucket may be versioned through one mechanism only. */
1538 return swift_versioning && !versioned();
1539 }
1540
1541 RGWBucketInfo() : flags(0), has_instance_obj(false), num_shards(0), bucket_index_shard_hash_type(MOD), requester_pays(false),
1542 has_website(false), swift_versioning(false), reshard_status(0) {}
1543 };
1544 WRITE_CLASS_ENCODER(RGWBucketInfo)
1545
1546 struct RGWBucketEntryPoint
1547 {
1548 rgw_bucket bucket;
1549 rgw_user owner;
1550 ceph::real_time creation_time;
1551 bool linked;
1552
1553 bool has_bucket_info;
1554 RGWBucketInfo old_bucket_info;
1555
1556 RGWBucketEntryPoint() : linked(false), has_bucket_info(false) {}
1557
1558 void encode(bufferlist& bl) const {
1559 ENCODE_START(10, 8, bl);
1560 encode(bucket, bl);
1561 encode(owner.id, bl);
1562 encode(linked, bl);
1563 uint64_t ctime = (uint64_t)real_clock::to_time_t(creation_time);
1564 encode(ctime, bl);
1565 encode(owner, bl);
1566 encode(creation_time, bl);
1567 ENCODE_FINISH(bl);
1568 }
1569 void decode(bufferlist::const_iterator& bl) {
1570 auto orig_iter = bl;
1571 DECODE_START_LEGACY_COMPAT_LEN_32(10, 4, 4, bl);
1572 if (struct_v < 8) {
1573 /* ouch, old entry, contains the bucket info itself */
1574 old_bucket_info.decode(orig_iter);
1575 has_bucket_info = true;
1576 return;
1577 }
1578 has_bucket_info = false;
1579 decode(bucket, bl);
1580 decode(owner.id, bl);
1581 decode(linked, bl);
1582 uint64_t ctime;
1583 decode(ctime, bl);
1584 if (struct_v < 10) {
1585 creation_time = real_clock::from_time_t((time_t)ctime);
1586 }
1587 if (struct_v >= 9) {
1588 decode(owner, bl);
1589 }
1590 if (struct_v >= 10) {
1591 decode(creation_time, bl);
1592 }
1593 DECODE_FINISH(bl);
1594 }
1595
1596 void dump(Formatter *f) const;
1597 void decode_json(JSONObj *obj);
1598 };
1599 WRITE_CLASS_ENCODER(RGWBucketEntryPoint)
1600
1601 struct RGWStorageStats
1602 {
1603 RGWObjCategory category;
1604 uint64_t size;
1605 uint64_t size_rounded;
1606 uint64_t size_utilized{0}; //< size after compression, encryption
1607 uint64_t num_objects;
1608
1609 RGWStorageStats()
1610 : category(RGWObjCategory::None),
1611 size(0),
1612 size_rounded(0),
1613 num_objects(0) {}
1614
1615 void dump(Formatter *f) const;
1616 };
1617
1618 class RGWEnv;
1619
1620 /* Namespaced forward declarations. */
1621 namespace rgw {
1622 namespace auth {
1623 namespace s3 {
1624 class AWSBrowserUploadAbstractor;
1625 }
1626 class Completer;
1627 }
1628 namespace io {
1629 class BasicClient;
1630 }
1631 }
1632
1633
1634 struct req_info {
1635 const RGWEnv *env;
1636 RGWHTTPArgs args;
1637 map<string, string> x_meta_map;
1638
1639 string host;
1640 const char *method;
1641 string script_uri;
1642 string request_uri;
1643 string request_uri_aws4;
1644 string effective_uri;
1645 string request_params;
1646 string domain;
1647 string storage_class;
1648
1649 req_info(CephContext *cct, const RGWEnv *env);
1650 void rebuild_from(req_info& src);
1651 void init_meta_info(bool *found_bad_meta);
1652 };
1653
1654 typedef cls_rgw_obj_key rgw_obj_index_key;
1655
1656 struct rgw_obj_key {
1657 string name;
1658 string instance;
1659 string ns;
1660
1661 rgw_obj_key() {}
1662 // cppcheck-suppress noExplicitConstructor
1663 rgw_obj_key(const string& n) : name(n) {}
1664 rgw_obj_key(const string& n, const string& i) : name(n), instance(i) {}
1665 rgw_obj_key(const string& n, const string& i, const string& _ns) : name(n), instance(i), ns(_ns) {}
1666
1667 rgw_obj_key(const rgw_obj_index_key& k) {
1668 parse_index_key(k.name, &name, &ns);
1669 instance = k.instance;
1670 }
1671
1672 static void parse_index_key(const string& key, string *name, string *ns) {
1673 if (key[0] != '_') {
1674 *name = key;
1675 ns->clear();
1676 return;
1677 }
1678 if (key[1] == '_') {
1679 *name = key.substr(1);
1680 ns->clear();
1681 return;
1682 }
1683 ssize_t pos = key.find('_', 1);
1684 if (pos < 0) {
1685 /* shouldn't happen, just use key */
1686 *name = key;
1687 ns->clear();
1688 return;
1689 }
1690
1691 *name = key.substr(pos + 1);
1692 *ns = key.substr(1, pos -1);
1693 }
1694
1695 void set(const string& n) {
1696 name = n;
1697 instance.clear();
1698 ns.clear();
1699 }
1700
1701 void set(const string& n, const string& i) {
1702 name = n;
1703 instance = i;
1704 ns.clear();
1705 }
1706
1707 void set(const string& n, const string& i, const string& _ns) {
1708 name = n;
1709 instance = i;
1710 ns = _ns;
1711 }
1712
1713 bool set(const rgw_obj_index_key& index_key) {
1714 if (!parse_raw_oid(index_key.name, this)) {
1715 return false;
1716 }
1717 instance = index_key.instance;
1718 return true;
1719 }
1720
1721 void set_instance(const string& i) {
1722 instance = i;
1723 }
1724
1725 const string& get_instance() const {
1726 return instance;
1727 }
1728
1729 string get_index_key_name() const {
1730 if (ns.empty()) {
1731 if (name.size() < 1 || name[0] != '_') {
1732 return name;
1733 }
1734 return string("_") + name;
1735 };
1736
1737 char buf[ns.size() + 16];
1738 snprintf(buf, sizeof(buf), "_%s_", ns.c_str());
1739 return string(buf) + name;
1740 };
1741
1742 void get_index_key(rgw_obj_index_key *key) const {
1743 key->name = get_index_key_name();
1744 key->instance = instance;
1745 }
1746
1747 string get_loc() const {
1748 /*
1749 * For backward compatibility. Older versions used to have object locator on all objects,
1750 * however, the name was the effective object locator. This had the same effect as not
1751 * having object locator at all for most objects but the ones that started with underscore as
1752 * these were escaped.
1753 */
1754 if (name[0] == '_' && ns.empty()) {
1755 return name;
1756 }
1757
1758 return string();
1759 }
1760
1761 bool empty() const {
1762 return name.empty();
1763 }
1764
1765 bool have_null_instance() const {
1766 return instance == "null";
1767 }
1768
1769 bool have_instance() const {
1770 return !instance.empty();
1771 }
1772
1773 bool need_to_encode_instance() const {
1774 return have_instance() && !have_null_instance();
1775 }
1776
1777 string get_oid() const {
1778 if (ns.empty() && !need_to_encode_instance()) {
1779 if (name.size() < 1 || name[0] != '_') {
1780 return name;
1781 }
1782 return string("_") + name;
1783 }
1784
1785 string oid = "_";
1786 oid.append(ns);
1787 if (need_to_encode_instance()) {
1788 oid.append(string(":") + instance);
1789 }
1790 oid.append("_");
1791 oid.append(name);
1792 return oid;
1793 }
1794
1795 bool operator==(const rgw_obj_key& k) const {
1796 return (name.compare(k.name) == 0) &&
1797 (instance.compare(k.instance) == 0);
1798 }
1799
1800 bool operator<(const rgw_obj_key& k) const {
1801 int r = name.compare(k.name);
1802 if (r == 0) {
1803 r = instance.compare(k.instance);
1804 }
1805 return (r < 0);
1806 }
1807
1808 bool operator<=(const rgw_obj_key& k) const {
1809 return !(k < *this);
1810 }
1811
1812 static void parse_ns_field(string& ns, string& instance) {
1813 int pos = ns.find(':');
1814 if (pos >= 0) {
1815 instance = ns.substr(pos + 1);
1816 ns = ns.substr(0, pos);
1817 } else {
1818 instance.clear();
1819 }
1820 }
1821
1822 // takes an oid and parses out the namespace (ns), name, and
1823 // instance
1824 static bool parse_raw_oid(const string& oid, rgw_obj_key *key) {
1825 key->instance.clear();
1826 key->ns.clear();
1827 if (oid[0] != '_') {
1828 key->name = oid;
1829 return true;
1830 }
1831
1832 if (oid.size() >= 2 && oid[1] == '_') {
1833 key->name = oid.substr(1);
1834 return true;
1835 }
1836
1837 if (oid.size() < 3) // for namespace, min size would be 3: _x_
1838 return false;
1839
1840 size_t pos = oid.find('_', 2); // oid must match ^_[^_].+$
1841 if (pos == string::npos)
1842 return false;
1843
1844 key->ns = oid.substr(1, pos - 1);
1845 parse_ns_field(key->ns, key->instance);
1846
1847 key->name = oid.substr(pos + 1);
1848 return true;
1849 }
1850
1851 /**
1852 * Translate a namespace-mangled object name to the user-facing name
1853 * existing in the given namespace.
1854 *
1855 * If the object is part of the given namespace, it returns true
1856 * and cuts down the name to the unmangled version. If it is not
1857 * part of the given namespace, it returns false.
1858 */
1859 static bool oid_to_key_in_ns(const string& oid, rgw_obj_key *key, const string& ns) {
1860 bool ret = parse_raw_oid(oid, key);
1861 if (!ret) {
1862 return ret;
1863 }
1864
1865 return (ns == key->ns);
1866 }
1867
1868 /**
1869 * Given a mangled object name and an empty namespace string, this
1870 * function extracts the namespace into the string and sets the object
1871 * name to be the unmangled version.
1872 *
1873 * It returns true after successfully doing so, or
1874 * false if it fails.
1875 */
1876 static bool strip_namespace_from_name(string& name, string& ns, string& instance) {
1877 ns.clear();
1878 instance.clear();
1879 if (name[0] != '_') {
1880 return true;
1881 }
1882
1883 size_t pos = name.find('_', 1);
1884 if (pos == string::npos) {
1885 return false;
1886 }
1887
1888 if (name[1] == '_') {
1889 name = name.substr(1);
1890 return true;
1891 }
1892
1893 size_t period_pos = name.find('.');
1894 if (period_pos < pos) {
1895 return false;
1896 }
1897
1898 ns = name.substr(1, pos-1);
1899 name = name.substr(pos+1, string::npos);
1900
1901 parse_ns_field(ns, instance);
1902 return true;
1903 }
1904
1905 void encode(bufferlist& bl) const {
1906 ENCODE_START(2, 1, bl);
1907 encode(name, bl);
1908 encode(instance, bl);
1909 encode(ns, bl);
1910 ENCODE_FINISH(bl);
1911 }
1912 void decode(bufferlist::const_iterator& bl) {
1913 DECODE_START(2, bl);
1914 decode(name, bl);
1915 decode(instance, bl);
1916 if (struct_v >= 2) {
1917 decode(ns, bl);
1918 }
1919 DECODE_FINISH(bl);
1920 }
1921 void dump(Formatter *f) const;
1922 void decode_json(JSONObj *obj);
1923
1924 string to_str() const {
1925 if (instance.empty()) {
1926 return name;
1927 }
1928 char buf[name.size() + instance.size() + 16];
1929 snprintf(buf, sizeof(buf), "%s[%s]", name.c_str(), instance.c_str());
1930 return buf;
1931 }
1932 };
1933 WRITE_CLASS_ENCODER(rgw_obj_key)
1934
1935 inline ostream& operator<<(ostream& out, const rgw_obj_key &o) {
1936 return out << o.to_str();
1937 }
1938
1939 inline ostream& operator<<(ostream& out, const rgw_obj_index_key &o) {
1940 if (o.instance.empty()) {
1941 return out << o.name;
1942 } else {
1943 return out << o.name << "[" << o.instance << "]";
1944 }
1945 }
1946
1947 struct req_init_state {
1948 /* Keeps [[tenant]:]bucket until we parse the token. */
1949 string url_bucket;
1950 string src_bucket;
1951 };
1952
1953 #include "rgw_auth.h"
1954
1955 class RGWObjectCtx;
1956 class RGWSysObjectCtx;
1957
1958 /** Store all the state necessary to complete and respond to an HTTP request*/
1959 struct req_state : DoutPrefixProvider {
1960 CephContext *cct;
1961 rgw::io::BasicClient *cio{nullptr};
1962 http_op op{OP_UNKNOWN};
1963 RGWOpType op_type{};
1964 bool content_started{false};
1965 int format{0};
1966 ceph::Formatter *formatter{nullptr};
1967 string decoded_uri;
1968 string relative_uri;
1969 const char *length{nullptr};
1970 int64_t content_length{0};
1971 map<string, string> generic_attrs;
1972 rgw_err err;
1973 bool expect_cont{false};
1974 uint64_t obj_size{0};
1975 bool enable_ops_log;
1976 bool enable_usage_log;
1977 uint8_t defer_to_bucket_acls;
1978 uint32_t perm_mask{0};
1979
1980 /* Set once when url_bucket is parsed and not violated thereafter. */
1981 string account_name;
1982
1983 string bucket_tenant;
1984 string bucket_name;
1985
1986 rgw_bucket bucket;
1987 rgw_obj_key object;
1988 string src_tenant_name;
1989 string src_bucket_name;
1990 rgw_obj_key src_object;
1991 ACLOwner bucket_owner;
1992 ACLOwner owner;
1993
1994 string zonegroup_name;
1995 string zonegroup_endpoint;
1996 string bucket_instance_id;
1997 int bucket_instance_shard_id{-1};
1998 string redirect_zone_endpoint;
1999
2000 string redirect;
2001
2002 RGWBucketInfo bucket_info;
2003 real_time bucket_mtime;
2004 std::map<std::string, ceph::bufferlist> bucket_attrs;
2005 bool bucket_exists{false};
2006 rgw_placement_rule dest_placement;
2007
2008 bool has_bad_meta{false};
2009
2010 RGWUserInfo *user;
2011
2012 struct {
2013 /* TODO(rzarzynski): switch out to the static_ptr for both members. */
2014
2015 /* Object having the knowledge about an authenticated identity and allowing
2016 * to apply it during the authorization phase (verify_permission() methods
2017 * of a given RGWOp). Thus, it bounds authentication and authorization steps
2018 * through a well-defined interface. For more details, see rgw_auth.h. */
2019 std::unique_ptr<rgw::auth::Identity> identity;
2020
2021 std::shared_ptr<rgw::auth::Completer> completer;
2022
2023 /* A container for credentials of the S3's browser upload. It's necessary
2024 * because: 1) the ::authenticate() method of auth engines and strategies
2025 * take req_state only; 2) auth strategies live much longer than RGWOps -
2026 * there is no way to pass additional data dependencies through ctors. */
2027 class {
2028 /* Writer. */
2029 friend class RGWPostObj_ObjStore_S3;
2030 /* Reader. */
2031 friend class rgw::auth::s3::AWSBrowserUploadAbstractor;
2032
2033 std::string access_key;
2034 std::string signature;
2035 std::string x_amz_algorithm;
2036 std::string x_amz_credential;
2037 std::string x_amz_date;
2038 std::string x_amz_security_token;
2039 ceph::bufferlist encoded_policy;
2040 } s3_postobj_creds;
2041 } auth;
2042
2043 std::unique_ptr<RGWAccessControlPolicy> user_acl;
2044 std::unique_ptr<RGWAccessControlPolicy> bucket_acl;
2045 std::unique_ptr<RGWAccessControlPolicy> object_acl;
2046
2047 rgw::IAM::Environment env;
2048 boost::optional<rgw::IAM::Policy> iam_policy;
2049 vector<rgw::IAM::Policy> iam_user_policies;
2050
2051 /* Is the request made by an user marked as a system one?
2052 * Being system user means we also have the admin status. */
2053 bool system_request{false};
2054
2055 string canned_acl;
2056 bool has_acl_header{false};
2057 bool local_source{false}; /* source is local */
2058
2059 int prot_flags{0};
2060
2061 /* Content-Disposition override for TempURL of Swift API. */
2062 struct {
2063 string override;
2064 string fallback;
2065 } content_disp;
2066
2067 string host_id;
2068
2069 req_info info;
2070 req_init_state init_state;
2071
2072 using Clock = ceph::coarse_real_clock;
2073 Clock::time_point time;
2074
2075 Clock::duration time_elapsed() const { return Clock::now() - time; }
2076
2077 RGWObjectCtx *obj_ctx{nullptr};
2078 RGWSysObjectCtx *sysobj_ctx{nullptr};
2079 string dialect;
2080 string req_id;
2081 string trans_id;
2082 uint64_t id;
2083
2084 bool mfa_verified{false};
2085
2086 /// optional coroutine context
2087 optional_yield yield{null_yield};
2088
2089 req_state(CephContext* _cct, RGWEnv* e, RGWUserInfo* u, uint64_t id);
2090 ~req_state();
2091
2092 bool is_err() const { return err.is_err(); }
2093
2094 // implements DoutPrefixProvider
2095 std::ostream& gen_prefix(std::ostream& out) const override;
2096 CephContext* get_cct() const override { return cct; }
2097 unsigned get_subsys() const override { return ceph_subsys_rgw; }
2098 };
2099
2100 void set_req_state_err(struct req_state*, int);
2101 void set_req_state_err(struct req_state*, int, const string&);
2102 void set_req_state_err(struct rgw_err&, int, const int);
2103 void dump(struct req_state*);
2104
2105 /** Store basic data on bucket */
2106 struct RGWBucketEnt {
2107 rgw_bucket bucket;
2108 size_t size;
2109 size_t size_rounded;
2110 ceph::real_time creation_time;
2111 uint64_t count;
2112
2113 /* The placement_rule is necessary to calculate per-storage-policy statics
2114 * of the Swift API. Although the info available in RGWBucketInfo, we need
2115 * to duplicate it here to not affect the performance of buckets listing. */
2116 rgw_placement_rule placement_rule;
2117
2118 RGWBucketEnt()
2119 : size(0),
2120 size_rounded(0),
2121 count(0) {
2122 }
2123 RGWBucketEnt(const RGWBucketEnt&) = default;
2124 RGWBucketEnt(RGWBucketEnt&&) = default;
2125 explicit RGWBucketEnt(const rgw_user& u, cls_user_bucket_entry&& e)
2126 : bucket(u, std::move(e.bucket)),
2127 size(e.size),
2128 size_rounded(e.size_rounded),
2129 creation_time(e.creation_time),
2130 count(e.count) {
2131 }
2132
2133 RGWBucketEnt& operator=(const RGWBucketEnt&) = default;
2134
2135 void convert(cls_user_bucket_entry *b) const {
2136 bucket.convert(&b->bucket);
2137 b->size = size;
2138 b->size_rounded = size_rounded;
2139 b->creation_time = creation_time;
2140 b->count = count;
2141 }
2142
2143 void encode(bufferlist& bl) const {
2144 ENCODE_START(7, 5, bl);
2145 uint64_t s = size;
2146 __u32 mt = ceph::real_clock::to_time_t(creation_time);
2147 string empty_str; // originally had the bucket name here, but we encode bucket later
2148 encode(empty_str, bl);
2149 encode(s, bl);
2150 encode(mt, bl);
2151 encode(count, bl);
2152 encode(bucket, bl);
2153 s = size_rounded;
2154 encode(s, bl);
2155 encode(creation_time, bl);
2156 encode(placement_rule, bl);
2157 ENCODE_FINISH(bl);
2158 }
2159 void decode(bufferlist::const_iterator& bl) {
2160 DECODE_START_LEGACY_COMPAT_LEN(7, 5, 5, bl);
2161 __u32 mt;
2162 uint64_t s;
2163 string empty_str; // backward compatibility
2164 decode(empty_str, bl);
2165 decode(s, bl);
2166 decode(mt, bl);
2167 size = s;
2168 if (struct_v < 6) {
2169 creation_time = ceph::real_clock::from_time_t(mt);
2170 }
2171 if (struct_v >= 2)
2172 decode(count, bl);
2173 if (struct_v >= 3)
2174 decode(bucket, bl);
2175 if (struct_v >= 4)
2176 decode(s, bl);
2177 size_rounded = s;
2178 if (struct_v >= 6)
2179 decode(creation_time, bl);
2180 if (struct_v >= 7)
2181 decode(placement_rule, bl);
2182 DECODE_FINISH(bl);
2183 }
2184 void dump(Formatter *f) const;
2185 static void generate_test_instances(list<RGWBucketEnt*>& o);
2186 };
2187 WRITE_CLASS_ENCODER(RGWBucketEnt)
2188
2189 struct rgw_obj {
2190 rgw_bucket bucket;
2191 rgw_obj_key key;
2192
2193 bool in_extra_data{false}; /* in-memory only member, does not serialize */
2194
2195 // Represents the hash index source for this object once it is set (non-empty)
2196 std::string index_hash_source;
2197
2198 rgw_obj() {}
2199 rgw_obj(const rgw_bucket& b, const std::string& name) : bucket(b), key(name) {}
2200 rgw_obj(const rgw_bucket& b, const rgw_obj_key& k) : bucket(b), key(k) {}
2201 rgw_obj(const rgw_bucket& b, const rgw_obj_index_key& k) : bucket(b), key(k) {}
2202
2203 void init(const rgw_bucket& b, const std::string& name) {
2204 bucket = b;
2205 key.set(name);
2206 }
2207 void init(const rgw_bucket& b, const std::string& name, const string& i, const string& n) {
2208 bucket = b;
2209 key.set(name, i, n);
2210 }
2211 void init_ns(const rgw_bucket& b, const std::string& name, const string& n) {
2212 bucket = b;
2213 key.name = name;
2214 key.instance.clear();
2215 key.ns = n;
2216 }
2217
2218 bool empty() const {
2219 return key.empty();
2220 }
2221
2222 void set_key(const rgw_obj_key& k) {
2223 key = k;
2224 }
2225
2226 string get_oid() const {
2227 return key.get_oid();
2228 }
2229
2230 const string& get_hash_object() const {
2231 return index_hash_source.empty() ? key.name : index_hash_source;
2232 }
2233
2234 void set_in_extra_data(bool val) {
2235 in_extra_data = val;
2236 }
2237
2238 bool is_in_extra_data() const {
2239 return in_extra_data;
2240 }
2241
2242 void encode(bufferlist& bl) const {
2243 ENCODE_START(6, 6, bl);
2244 encode(bucket, bl);
2245 encode(key.ns, bl);
2246 encode(key.name, bl);
2247 encode(key.instance, bl);
2248 // encode(placement_id, bl);
2249 ENCODE_FINISH(bl);
2250 }
2251 void decode(bufferlist::const_iterator& bl) {
2252 DECODE_START_LEGACY_COMPAT_LEN(6, 3, 3, bl);
2253 if (struct_v < 6) {
2254 string s;
2255 decode(bucket.name, bl); /* bucket.name */
2256 decode(s, bl); /* loc */
2257 decode(key.ns, bl);
2258 decode(key.name, bl);
2259 if (struct_v >= 2)
2260 decode(bucket, bl);
2261 if (struct_v >= 4)
2262 decode(key.instance, bl);
2263 if (key.ns.empty() && key.instance.empty()) {
2264 if (key.name[0] == '_') {
2265 key.name = key.name.substr(1);
2266 }
2267 } else {
2268 if (struct_v >= 5) {
2269 decode(key.name, bl);
2270 } else {
2271 ssize_t pos = key.name.find('_', 1);
2272 if (pos < 0) {
2273 throw buffer::error();
2274 }
2275 key.name = key.name.substr(pos + 1);
2276 }
2277 }
2278 } else {
2279 decode(bucket, bl);
2280 decode(key.ns, bl);
2281 decode(key.name, bl);
2282 decode(key.instance, bl);
2283 // decode(placement_id, bl);
2284 }
2285 DECODE_FINISH(bl);
2286 }
2287 void dump(Formatter *f) const;
2288 static void generate_test_instances(list<rgw_obj*>& o);
2289
2290 bool operator==(const rgw_obj& o) const {
2291 return (key == o.key) &&
2292 (bucket == o.bucket);
2293 }
2294 bool operator<(const rgw_obj& o) const {
2295 int r = key.name.compare(o.key.name);
2296 if (r == 0) {
2297 r = bucket.bucket_id.compare(o.bucket.bucket_id); /* not comparing bucket.name, if bucket_id is equal so will be bucket.name */
2298 if (r == 0) {
2299 r = key.ns.compare(o.key.ns);
2300 if (r == 0) {
2301 r = key.instance.compare(o.key.instance);
2302 }
2303 }
2304 }
2305
2306 return (r < 0);
2307 }
2308
2309 const rgw_pool& get_explicit_data_pool() {
2310 if (!in_extra_data || bucket.explicit_placement.data_extra_pool.empty()) {
2311 return bucket.explicit_placement.data_pool;
2312 }
2313 return bucket.explicit_placement.data_extra_pool;
2314 }
2315 };
2316 WRITE_CLASS_ENCODER(rgw_obj)
2317
2318 struct rgw_cache_entry_info {
2319 string cache_locator;
2320 uint64_t gen;
2321
2322 rgw_cache_entry_info() : gen(0) {}
2323 };
2324
2325 inline ostream& operator<<(ostream& out, const rgw_obj &o) {
2326 return out << o.bucket.name << ":" << o.get_oid();
2327 }
2328
2329 static inline void buf_to_hex(const unsigned char* const buf,
2330 const size_t len,
2331 char* const str)
2332 {
2333 str[0] = '\0';
2334 for (size_t i = 0; i < len; i++) {
2335 ::sprintf(&str[i*2], "%02x", static_cast<int>(buf[i]));
2336 }
2337 }
2338
2339 template<size_t N> static inline std::array<char, N * 2 + 1>
2340 buf_to_hex(const std::array<unsigned char, N>& buf)
2341 {
2342 static_assert(N > 0, "The input array must be at least one element long");
2343
2344 std::array<char, N * 2 + 1> hex_dest;
2345 buf_to_hex(buf.data(), N, hex_dest.data());
2346 return hex_dest;
2347 }
2348
2349 static inline int hexdigit(char c)
2350 {
2351 if (c >= '0' && c <= '9')
2352 return (c - '0');
2353 c = toupper(c);
2354 if (c >= 'A' && c <= 'F')
2355 return c - 'A' + 0xa;
2356 return -EINVAL;
2357 }
2358
2359 static inline int hex_to_buf(const char *hex, char *buf, int len)
2360 {
2361 int i = 0;
2362 const char *p = hex;
2363 while (*p) {
2364 if (i >= len)
2365 return -EINVAL;
2366 buf[i] = 0;
2367 int d = hexdigit(*p);
2368 if (d < 0)
2369 return d;
2370 buf[i] = d << 4;
2371 p++;
2372 if (!*p)
2373 return -EINVAL;
2374 d = hexdigit(*p);
2375 if (d < 0)
2376 return d;
2377 buf[i] += d;
2378 i++;
2379 p++;
2380 }
2381 return i;
2382 }
2383
2384 static inline int rgw_str_to_bool(const char *s, int def_val)
2385 {
2386 if (!s)
2387 return def_val;
2388
2389 return (strcasecmp(s, "true") == 0 ||
2390 strcasecmp(s, "on") == 0 ||
2391 strcasecmp(s, "yes") == 0 ||
2392 strcasecmp(s, "1") == 0);
2393 }
2394
2395 static inline void append_rand_alpha(CephContext *cct, const string& src, string& dest, int len)
2396 {
2397 dest = src;
2398 char buf[len + 1];
2399 gen_rand_alphanumeric(cct, buf, len);
2400 dest.append("_");
2401 dest.append(buf);
2402 }
2403
2404 static inline const char *rgw_obj_category_name(RGWObjCategory category)
2405 {
2406 switch (category) {
2407 case RGWObjCategory::None:
2408 return "rgw.none";
2409 case RGWObjCategory::Main:
2410 return "rgw.main";
2411 case RGWObjCategory::Shadow:
2412 return "rgw.shadow";
2413 case RGWObjCategory::MultiMeta:
2414 return "rgw.multimeta";
2415 }
2416
2417 return "unknown";
2418 }
2419
2420 static inline uint64_t rgw_rounded_kb(uint64_t bytes)
2421 {
2422 return (bytes + 1023) / 1024;
2423 }
2424
2425 static inline uint64_t rgw_rounded_objsize(uint64_t bytes)
2426 {
2427 return ((bytes + 4095) & ~4095);
2428 }
2429
2430 static inline uint64_t rgw_rounded_objsize_kb(uint64_t bytes)
2431 {
2432 return ((bytes + 4095) & ~4095) / 1024;
2433 }
2434
2435 /* implement combining step, S3 header canonicalization; k is a
2436 * valid header and in lc form */
2437 static inline void add_amz_meta_header(
2438 std::map<std::string, std::string>& x_meta_map,
2439 const std::string& k,
2440 const std::string& v)
2441 {
2442 auto it = x_meta_map.find(k);
2443 if (it != x_meta_map.end()) {
2444 std::string old = it->second;
2445 boost::algorithm::trim_right(old);
2446 old.append(",");
2447 old.append(v);
2448 x_meta_map[k] = old;
2449 } else {
2450 x_meta_map[k] = v;
2451 }
2452 } /* add_amz_meta_header */
2453
2454 extern string rgw_string_unquote(const string& s);
2455 extern void parse_csv_string(const string& ival, vector<string>& ovals);
2456 extern int parse_key_value(string& in_str, string& key, string& val);
2457 extern int parse_key_value(string& in_str, const char *delim, string& key, string& val);
2458
2459 extern boost::optional<std::pair<boost::string_view, boost::string_view>>
2460 parse_key_value(const boost::string_view& in_str,
2461 const boost::string_view& delim);
2462 extern boost::optional<std::pair<boost::string_view, boost::string_view>>
2463 parse_key_value(const boost::string_view& in_str);
2464
2465
2466 /** time parsing */
2467 extern int parse_time(const char *time_str, real_time *time);
2468 extern bool parse_rfc2616(const char *s, struct tm *t);
2469 extern bool parse_iso8601(const char *s, struct tm *t, uint32_t *pns = NULL, bool extended_format = true);
2470 extern string rgw_trim_whitespace(const string& src);
2471 extern boost::string_view rgw_trim_whitespace(const boost::string_view& src);
2472 extern string rgw_trim_quotes(const string& val);
2473
2474 extern void rgw_to_iso8601(const real_time& t, char *dest, int buf_size);
2475 extern void rgw_to_iso8601(const real_time& t, string *dest);
2476 extern std::string rgw_to_asctime(const utime_t& t);
2477
2478 /** Check if the req_state's user has the necessary permissions
2479 * to do the requested action */
2480 rgw::IAM::Effect eval_user_policies(const vector<rgw::IAM::Policy>& user_policies,
2481 const rgw::IAM::Environment& env,
2482 boost::optional<const rgw::auth::Identity&> id,
2483 const uint64_t op,
2484 const rgw::ARN& arn);
2485 bool verify_user_permission(const DoutPrefixProvider* dpp,
2486 struct req_state * const s,
2487 RGWAccessControlPolicy * const user_acl,
2488 const vector<rgw::IAM::Policy>& user_policies,
2489 const rgw::ARN& res,
2490 const uint64_t op);
2491 bool verify_user_permission_no_policy(const DoutPrefixProvider* dpp,
2492 struct req_state * const s,
2493 RGWAccessControlPolicy * const user_acl,
2494 const int perm);
2495 bool verify_user_permission(const DoutPrefixProvider* dpp,
2496 struct req_state * const s,
2497 const rgw::ARN& res,
2498 const uint64_t op);
2499 bool verify_user_permission_no_policy(const DoutPrefixProvider* dpp,
2500 struct req_state * const s,
2501 int perm);
2502 bool verify_bucket_permission(
2503 const DoutPrefixProvider* dpp,
2504 struct req_state * const s,
2505 const rgw_bucket& bucket,
2506 RGWAccessControlPolicy * const user_acl,
2507 RGWAccessControlPolicy * const bucket_acl,
2508 const boost::optional<rgw::IAM::Policy>& bucket_policy,
2509 const vector<rgw::IAM::Policy>& user_policies,
2510 const uint64_t op);
2511 bool verify_bucket_permission(const DoutPrefixProvider* dpp, struct req_state * const s, const uint64_t op);
2512 bool verify_bucket_permission_no_policy(
2513 const DoutPrefixProvider* dpp,
2514 struct req_state * const s,
2515 RGWAccessControlPolicy * const user_acl,
2516 RGWAccessControlPolicy * const bucket_acl,
2517 const int perm);
2518 bool verify_bucket_permission_no_policy(const DoutPrefixProvider* dpp,
2519 struct req_state * const s,
2520 const int perm);
2521 int verify_bucket_owner_or_policy(struct req_state* const s,
2522 const uint64_t op);
2523 extern bool verify_object_permission(
2524 const DoutPrefixProvider* dpp,
2525 struct req_state * const s,
2526 const rgw_obj& obj,
2527 RGWAccessControlPolicy * const user_acl,
2528 RGWAccessControlPolicy * const bucket_acl,
2529 RGWAccessControlPolicy * const object_acl,
2530 const boost::optional<rgw::IAM::Policy>& bucket_policy,
2531 const vector<rgw::IAM::Policy>& user_policies,
2532 const uint64_t op);
2533 extern bool verify_object_permission(const DoutPrefixProvider* dpp, struct req_state *s, uint64_t op);
2534 extern bool verify_object_permission_no_policy(
2535 const DoutPrefixProvider* dpp,
2536 struct req_state * const s,
2537 RGWAccessControlPolicy * const user_acl,
2538 RGWAccessControlPolicy * const bucket_acl,
2539 RGWAccessControlPolicy * const object_acl,
2540 int perm);
2541 extern bool verify_object_permission_no_policy(const DoutPrefixProvider* dpp, struct req_state *s,
2542 int perm);
2543 /** Convert an input URL into a sane object name
2544 * by converting %-escaped strings into characters, etc*/
2545 extern void rgw_uri_escape_char(char c, string& dst);
2546 extern std::string url_decode(const boost::string_view& src_str,
2547 bool in_query = false);
2548 extern void url_encode(const std::string& src, string& dst,
2549 bool encode_slash = true);
2550 extern std::string url_encode(const std::string& src, bool encode_slash = true);
2551 /* destination should be CEPH_CRYPTO_HMACSHA1_DIGESTSIZE bytes long */
2552 extern void calc_hmac_sha1(const char *key, int key_len,
2553 const char *msg, int msg_len, char *dest);
2554
2555 static inline sha1_digest_t
2556 calc_hmac_sha1(const boost::string_view& key, const boost::string_view& msg) {
2557 sha1_digest_t dest;
2558 calc_hmac_sha1(key.data(), key.size(), msg.data(), msg.size(),
2559 reinterpret_cast<char*>(dest.v));
2560 return dest;
2561 }
2562
2563 /* destination should be CEPH_CRYPTO_HMACSHA256_DIGESTSIZE bytes long */
2564 extern void calc_hmac_sha256(const char *key, int key_len,
2565 const char *msg, int msg_len,
2566 char *dest);
2567
2568 static inline sha256_digest_t
2569 calc_hmac_sha256(const char *key, const int key_len,
2570 const char *msg, const int msg_len) {
2571 sha256_digest_t dest;
2572 calc_hmac_sha256(key, key_len, msg, msg_len,
2573 reinterpret_cast<char*>(dest.v));
2574 return dest;
2575 }
2576
2577 static inline sha256_digest_t
2578 calc_hmac_sha256(const boost::string_view& key, const boost::string_view& msg) {
2579 sha256_digest_t dest;
2580 calc_hmac_sha256(key.data(), key.size(),
2581 msg.data(), msg.size(),
2582 reinterpret_cast<char*>(dest.v));
2583 return dest;
2584 }
2585
2586 static inline sha256_digest_t
2587 calc_hmac_sha256(const sha256_digest_t &key,
2588 const boost::string_view& msg) {
2589 sha256_digest_t dest;
2590 calc_hmac_sha256(reinterpret_cast<const char*>(key.v), sha256_digest_t::SIZE,
2591 msg.data(), msg.size(),
2592 reinterpret_cast<char*>(dest.v));
2593 return dest;
2594 }
2595
2596 static inline sha256_digest_t
2597 calc_hmac_sha256(const std::vector<unsigned char>& key,
2598 const boost::string_view& msg) {
2599 sha256_digest_t dest;
2600 calc_hmac_sha256(reinterpret_cast<const char*>(key.data()), key.size(),
2601 msg.data(), msg.size(),
2602 reinterpret_cast<char*>(dest.v));
2603 return dest;
2604 }
2605
2606 template<size_t KeyLenN>
2607 static inline sha256_digest_t
2608 calc_hmac_sha256(const std::array<unsigned char, KeyLenN>& key,
2609 const boost::string_view& msg) {
2610 sha256_digest_t dest;
2611 calc_hmac_sha256(reinterpret_cast<const char*>(key.data()), key.size(),
2612 msg.data(), msg.size(),
2613 reinterpret_cast<char*>(dest.v));
2614 return dest;
2615 }
2616
2617 extern sha256_digest_t calc_hash_sha256(const boost::string_view& msg);
2618
2619 extern ceph::crypto::SHA256* calc_hash_sha256_open_stream();
2620 extern void calc_hash_sha256_update_stream(ceph::crypto::SHA256* hash,
2621 const char* msg,
2622 int len);
2623 extern std::string calc_hash_sha256_close_stream(ceph::crypto::SHA256** phash);
2624 extern std::string calc_hash_sha256_restart_stream(ceph::crypto::SHA256** phash);
2625
2626 extern int rgw_parse_op_type_list(const string& str, uint32_t *perm);
2627
2628 static constexpr uint32_t MATCH_POLICY_ACTION = 0x01;
2629 static constexpr uint32_t MATCH_POLICY_RESOURCE = 0x02;
2630 static constexpr uint32_t MATCH_POLICY_ARN = 0x04;
2631 static constexpr uint32_t MATCH_POLICY_STRING = 0x08;
2632
2633 extern bool match_policy(boost::string_view pattern, boost::string_view input,
2634 uint32_t flag);
2635
2636 extern string camelcase_dash_http_attr(const string& orig);
2637 extern string lowercase_dash_http_attr(const string& orig);
2638
2639 void rgw_setup_saved_curl_handles();
2640 void rgw_release_all_curl_handles();
2641
2642 static inline void rgw_escape_str(const string& s, char esc_char,
2643 char special_char, string *dest)
2644 {
2645 const char *src = s.c_str();
2646 char dest_buf[s.size() * 2 + 1];
2647 char *destp = dest_buf;
2648
2649 for (size_t i = 0; i < s.size(); i++) {
2650 char c = src[i];
2651 if (c == esc_char || c == special_char) {
2652 *destp++ = esc_char;
2653 }
2654 *destp++ = c;
2655 }
2656 *destp++ = '\0';
2657 *dest = dest_buf;
2658 }
2659
2660 static inline ssize_t rgw_unescape_str(const string& s, ssize_t ofs,
2661 char esc_char, char special_char,
2662 string *dest)
2663 {
2664 const char *src = s.c_str();
2665 char dest_buf[s.size() + 1];
2666 char *destp = dest_buf;
2667 bool esc = false;
2668
2669 dest_buf[0] = '\0';
2670
2671 for (size_t i = ofs; i < s.size(); i++) {
2672 char c = src[i];
2673 if (!esc && c == esc_char) {
2674 esc = true;
2675 continue;
2676 }
2677 if (!esc && c == special_char) {
2678 *destp = '\0';
2679 *dest = dest_buf;
2680 return (ssize_t)i + 1;
2681 }
2682 *destp++ = c;
2683 esc = false;
2684 }
2685 *destp = '\0';
2686 *dest = dest_buf;
2687 return string::npos;
2688 }
2689
2690 static inline string rgw_bl_str(ceph::buffer::list& raw)
2691 {
2692 size_t len = raw.length();
2693 string s(raw.c_str(), len);
2694 while (len && !s[len - 1]) {
2695 --len;
2696 s.resize(len);
2697 }
2698 return s;
2699 }
2700
2701 template <typename T>
2702 int decode_bl(bufferlist& bl, T& t)
2703 {
2704 auto iter = bl.cbegin();
2705 try {
2706 decode(t, iter);
2707 } catch (buffer::error& err) {
2708 return -EIO;
2709 }
2710 return 0;
2711 }
2712
2713 #endif