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