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