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