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