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