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