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