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