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