/
rgw_user.cc
2594 lines (2063 loc) · 63.1 KB
/
rgw_user.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <errno.h>
#include <string>
#include <map>
#include "common/errno.h"
#include "common/Formatter.h"
#include "common/ceph_json.h"
#include "common/RWLock.h"
#include "rgw_rados.h"
#include "rgw_acl.h"
#include "include/types.h"
#include "rgw_user.h"
#include "rgw_string.h"
// until everything is moved from rgw_common
#include "rgw_common.h"
#include "rgw_bucket.h"
#define dout_subsys ceph_subsys_rgw
using namespace std;
static RGWMetadataHandler *user_meta_handler = NULL;
/**
* Get the anonymous (ie, unauthenticated) user info.
*/
void rgw_get_anon_user(RGWUserInfo& info)
{
info.user_id = RGW_USER_ANON_ID;
info.display_name.clear();
info.access_keys.clear();
}
bool rgw_user_is_authenticated(RGWUserInfo& info)
{
return (info.user_id != RGW_USER_ANON_ID);
}
int rgw_user_sync_all_stats(RGWRados *store, const string& user_id)
{
CephContext *cct = store->ctx();
size_t max_entries = cct->_conf->rgw_list_buckets_max_chunk;
bool done;
string marker;
int ret;
do {
RGWUserBuckets user_buckets;
ret = rgw_read_user_buckets(store, user_id, user_buckets, marker, max_entries, false);
if (ret < 0) {
ldout(cct, 0) << "failed to read user buckets: ret=" << ret << dendl;
return ret;
}
map<string, RGWBucketEnt>& buckets = user_buckets.get_buckets();
for (map<string, RGWBucketEnt>::iterator i = buckets.begin();
i != buckets.end();
++i) {
marker = i->first;
RGWBucketEnt& bucket_ent = i->second;
ret = rgw_bucket_sync_user_stats(store, user_id, bucket_ent.bucket);
if (ret < 0) {
ldout(cct, 0) << "ERROR: could not sync bucket stats: ret=" << ret << dendl;
return ret;
}
}
done = (buckets.size() < max_entries);
} while (!done);
ret = store->complete_sync_user_stats(user_id);
if (ret < 0) {
cerr << "ERROR: failed to complete syncing user stats: ret=" << ret << std::endl;
return ret;
}
return 0;
}
/**
* Save the given user information to storage.
* Returns: 0 on success, -ERR# on failure.
*/
int rgw_store_user_info(RGWRados *store,
RGWUserInfo& info,
RGWUserInfo *old_info,
RGWObjVersionTracker *objv_tracker,
time_t mtime,
bool exclusive,
map<string, bufferlist> *pattrs)
{
bufferlist bl;
info.encode(bl);
int ret;
RGWObjVersionTracker ot;
if (objv_tracker) {
ot = *objv_tracker;
}
if (ot.write_version.tag.empty()) {
if (ot.read_version.tag.empty()) {
ot.generate_new_write_ver(store->ctx());
} else {
ot.write_version = ot.read_version;
ot.write_version.ver++;
}
}
map<string, RGWAccessKey>::iterator iter;
for (iter = info.swift_keys.begin(); iter != info.swift_keys.end(); ++iter) {
if (old_info && old_info->swift_keys.count(iter->first) != 0)
continue;
RGWAccessKey& k = iter->second;
/* check if swift mapping exists */
RGWUserInfo inf;
int r = rgw_get_user_info_by_swift(store, k.id, inf);
if (r >= 0 && inf.user_id.compare(info.user_id) != 0) {
ldout(store->ctx(), 0) << "WARNING: can't store user info, swift id (" << k.id
<< ") already mapped to another user (" << info.user_id << ")" << dendl;
return -EEXIST;
}
}
if (!info.access_keys.empty()) {
/* check if access keys already exist */
RGWUserInfo inf;
map<string, RGWAccessKey>::iterator iter = info.access_keys.begin();
for (; iter != info.access_keys.end(); ++iter) {
RGWAccessKey& k = iter->second;
if (old_info && old_info->access_keys.count(iter->first) != 0)
continue;
int r = rgw_get_user_info_by_access_key(store, k.id, inf);
if (r >= 0 && inf.user_id.compare(info.user_id) != 0) {
ldout(store->ctx(), 0) << "WARNING: can't store user info, access key already mapped to another user" << dendl;
return -EEXIST;
}
}
}
RGWUID ui;
ui.user_id = info.user_id;
bufferlist link_bl;
::encode(ui, link_bl);
bufferlist data_bl;
::encode(ui, data_bl);
::encode(info, data_bl);
ret = store->meta_mgr->put_entry(user_meta_handler, info.user_id, data_bl, exclusive, &ot, mtime, pattrs);
if (ret < 0)
return ret;
if (!info.user_email.empty()) {
if (!old_info ||
old_info->user_email.compare(info.user_email) != 0) { /* only if new index changed */
ret = rgw_put_system_obj(store, store->zone.user_email_pool, info.user_email,
link_bl.c_str(), link_bl.length(), exclusive, NULL, 0);
if (ret < 0)
return ret;
}
}
if (!info.access_keys.empty()) {
map<string, RGWAccessKey>::iterator iter = info.access_keys.begin();
for (; iter != info.access_keys.end(); ++iter) {
RGWAccessKey& k = iter->second;
if (old_info && old_info->access_keys.count(iter->first) != 0)
continue;
ret = rgw_put_system_obj(store, store->zone.user_keys_pool, k.id,
link_bl.c_str(), link_bl.length(), exclusive,
NULL, 0);
if (ret < 0)
return ret;
}
}
map<string, RGWAccessKey>::iterator siter;
for (siter = info.swift_keys.begin(); siter != info.swift_keys.end(); ++siter) {
RGWAccessKey& k = siter->second;
if (old_info && old_info->swift_keys.count(siter->first) != 0)
continue;
ret = rgw_put_system_obj(store, store->zone.user_swift_pool, k.id,
link_bl.c_str(), link_bl.length(), exclusive,
NULL, 0);
if (ret < 0)
return ret;
}
return ret;
}
int rgw_store_user_attrs(RGWRados *const store,
string& user_id,
map<string, bufferlist>& attrs,
map<string, bufferlist>* const rmattrs,
RGWObjVersionTracker * const objv_tracker)
{
rgw_obj obj(store->zone.user_uid_pool, user_id);
return store->meta_mgr->set_attrs(user_meta_handler, user_id, obj,
attrs, rmattrs, objv_tracker);
}
struct user_info_entry {
RGWUserInfo info;
RGWObjVersionTracker objv_tracker;
time_t mtime;
};
static RGWChainedCacheImpl<user_info_entry> uinfo_cache;
int rgw_get_user_info_from_index(RGWRados *store, string& key, rgw_bucket& bucket, RGWUserInfo& info,
RGWObjVersionTracker *objv_tracker, time_t *pmtime)
{
user_info_entry e;
if (uinfo_cache.find(key, &e)) {
info = e.info;
if (objv_tracker)
*objv_tracker = e.objv_tracker;
if (pmtime)
*pmtime = e.mtime;
return 0;
}
bufferlist bl;
RGWUID uid;
RGWObjectCtx obj_ctx(store);
int ret = rgw_get_system_obj(store, obj_ctx, bucket, key, bl, NULL, &e.mtime);
if (ret < 0)
return ret;
rgw_cache_entry_info cache_info;
bufferlist::iterator iter = bl.begin();
try {
::decode(uid, iter);
int ret = rgw_get_user_info_by_uid(store, uid.user_id, e.info, &e.objv_tracker, NULL, &cache_info);
if (ret < 0) {
return ret;
}
} catch (buffer::error& err) {
ldout(store->ctx(), 0) << "ERROR: failed to decode user info, caught buffer::error" << dendl;
return -EIO;
}
list<rgw_cache_entry_info *> cache_info_entries;
cache_info_entries.push_back(&cache_info);
uinfo_cache.put(store, key, &e, cache_info_entries);
info = e.info;
if (objv_tracker)
*objv_tracker = e.objv_tracker;
if (pmtime)
*pmtime = e.mtime;
return 0;
}
/**
* Given a uid, finds the user info associated with it.
* returns: 0 on success, -ERR# on failure (including nonexistence)
*/
int rgw_get_user_info_by_uid(RGWRados *store,
string& uid,
RGWUserInfo& info,
RGWObjVersionTracker *objv_tracker,
time_t *pmtime,
rgw_cache_entry_info *cache_info,
map<string, bufferlist> *pattrs)
{
bufferlist bl;
RGWUID user_id;
RGWObjectCtx obj_ctx(store);
int ret = rgw_get_system_obj(store, obj_ctx, store->zone.user_uid_pool, uid, bl, objv_tracker, pmtime, pattrs, cache_info);
if (ret < 0) {
return ret;
}
bufferlist::iterator iter = bl.begin();
try {
::decode(user_id, iter);
if (user_id.user_id.compare(uid) != 0) {
lderr(store->ctx()) << "ERROR: rgw_get_user_info_by_uid(): user id mismatch: " << user_id.user_id << " != " << uid << dendl;
return -EIO;
}
if (!iter.end()) {
::decode(info, iter);
}
} catch (buffer::error& err) {
ldout(store->ctx(), 0) << "ERROR: failed to decode user info, caught buffer::error" << dendl;
return -EIO;
}
return 0;
}
/**
* Given an email, finds the user info associated with it.
* returns: 0 on success, -ERR# on failure (including nonexistence)
*/
int rgw_get_user_info_by_email(RGWRados *store, string& email, RGWUserInfo& info,
RGWObjVersionTracker *objv_tracker, time_t *pmtime)
{
return rgw_get_user_info_from_index(store, email, store->zone.user_email_pool, info, objv_tracker, pmtime);
}
/**
* Given an swift username, finds the user_info associated with it.
* returns: 0 on success, -ERR# on failure (including nonexistence)
*/
extern int rgw_get_user_info_by_swift(RGWRados *store, string& swift_name, RGWUserInfo& info,
RGWObjVersionTracker *objv_tracker, time_t *pmtime)
{
return rgw_get_user_info_from_index(store, swift_name, store->zone.user_swift_pool, info, objv_tracker, pmtime);
}
/**
* Given an access key, finds the user info associated with it.
* returns: 0 on success, -ERR# on failure (including nonexistence)
*/
extern int rgw_get_user_info_by_access_key(RGWRados *store, string& access_key, RGWUserInfo& info,
RGWObjVersionTracker *objv_tracker, time_t *pmtime)
{
return rgw_get_user_info_from_index(store, access_key, store->zone.user_keys_pool, info, objv_tracker, pmtime);
}
int rgw_get_user_attrs_by_uid(RGWRados *store,
const string& user_id,
map<string, bufferlist>& attrs,
RGWObjVersionTracker *objv_tracker)
{
RGWObjectCtx obj_ctx(store);
rgw_obj obj(store->zone.user_uid_pool, user_id);
RGWRados::SystemObject src(store, obj_ctx, obj);
RGWRados::SystemObject::Read rop(&src);
rop.stat_params.attrs = &attrs;
return rop.stat(objv_tracker);
}
int rgw_remove_key_index(RGWRados *store, RGWAccessKey& access_key)
{
rgw_obj obj(store->zone.user_keys_pool, access_key.id);
int ret = store->delete_system_obj(obj);
return ret;
}
int rgw_remove_uid_index(RGWRados *store, string& uid)
{
RGWObjVersionTracker objv_tracker;
RGWUserInfo info;
int ret = rgw_get_user_info_by_uid(store, uid, info, &objv_tracker, NULL);
if (ret < 0)
return ret;
ret = store->meta_mgr->remove_entry(user_meta_handler, uid, &objv_tracker);
if (ret < 0)
return ret;
return 0;
}
int rgw_remove_email_index(RGWRados *store, string& email)
{
rgw_obj obj(store->zone.user_email_pool, email);
int ret = store->delete_system_obj(obj);
return ret;
}
int rgw_remove_swift_name_index(RGWRados *store, string& swift_name)
{
rgw_obj obj(store->zone.user_swift_pool, swift_name);
int ret = store->delete_system_obj(obj);
return ret;
}
/**
* delete a user's presence from the RGW system.
* First remove their bucket ACLs, then delete them
* from the user and user email pools. This leaves the pools
* themselves alone, as well as any ACLs embedded in object xattrs.
*/
int rgw_delete_user(RGWRados *store, RGWUserInfo& info, RGWObjVersionTracker& objv_tracker) {
string marker;
vector<rgw_bucket> buckets_vec;
bool done;
int ret;
CephContext *cct = store->ctx();
size_t max_buckets = cct->_conf->rgw_list_buckets_max_chunk;
do {
RGWUserBuckets user_buckets;
ret = rgw_read_user_buckets(store, info.user_id, user_buckets, marker, max_buckets, false);
if (ret < 0)
return ret;
map<string, RGWBucketEnt>& buckets = user_buckets.get_buckets();
for (map<string, RGWBucketEnt>::iterator i = buckets.begin();
i != buckets.end();
++i) {
RGWBucketEnt& bucket = i->second;
buckets_vec.push_back(bucket.bucket);
marker = i->first;
}
done = (buckets.size() < max_buckets);
} while (!done);
map<string, RGWAccessKey>::iterator kiter = info.access_keys.begin();
for (; kiter != info.access_keys.end(); ++kiter) {
ldout(store->ctx(), 10) << "removing key index: " << kiter->first << dendl;
ret = rgw_remove_key_index(store, kiter->second);
if (ret < 0 && ret != -ENOENT) {
ldout(store->ctx(), 0) << "ERROR: could not remove " << kiter->first << " (access key object), should be fixed (err=" << ret << ")" << dendl;
return ret;
}
}
map<string, RGWAccessKey>::iterator siter = info.swift_keys.begin();
for (; siter != info.swift_keys.end(); ++siter) {
RGWAccessKey& k = siter->second;
ldout(store->ctx(), 10) << "removing swift subuser index: " << k.id << dendl;
/* check if swift mapping exists */
ret = rgw_remove_swift_name_index(store, k.id);
if (ret < 0 && ret != -ENOENT) {
ldout(store->ctx(), 0) << "ERROR: could not remove " << k.id << " (swift name object), should be fixed (err=" << ret << ")" << dendl;
return ret;
}
}
rgw_obj email_obj(store->zone.user_email_pool, info.user_email);
ldout(store->ctx(), 10) << "removing email index: " << info.user_email << dendl;
ret = store->delete_system_obj(email_obj);
if (ret < 0 && ret != -ENOENT) {
ldout(store->ctx(), 0) << "ERROR: could not remove " << info.user_id << ":" << email_obj << ", should be fixed (err=" << ret << ")" << dendl;
return ret;
}
string buckets_obj_id;
rgw_get_buckets_obj(info.user_id, buckets_obj_id);
rgw_obj uid_bucks(store->zone.user_uid_pool, buckets_obj_id);
ldout(store->ctx(), 10) << "removing user buckets index" << dendl;
ret = store->delete_system_obj(uid_bucks);
if (ret < 0 && ret != -ENOENT) {
ldout(store->ctx(), 0) << "ERROR: could not remove " << info.user_id << ":" << uid_bucks << ", should be fixed (err=" << ret << ")" << dendl;
return ret;
}
rgw_obj uid_obj(store->zone.user_uid_pool, info.user_id);
ldout(store->ctx(), 10) << "removing user index: " << info.user_id << dendl;
ret = store->meta_mgr->remove_entry(user_meta_handler, info.user_id, &objv_tracker);
if (ret < 0 && ret != -ENOENT) {
ldout(store->ctx(), 0) << "ERROR: could not remove " << info.user_id << ":" << uid_obj << ", should be fixed (err=" << ret << ")" << dendl;
return ret;
}
return 0;
}
static bool char_is_unreserved_url(char c)
{
if (isalnum(c))
return true;
switch (c) {
case '-':
case '.':
case '_':
case '~':
return true;
default:
return false;
}
}
struct rgw_flags_desc {
uint32_t mask;
const char *str;
};
static struct rgw_flags_desc rgw_perms[] = {
{ RGW_PERM_FULL_CONTROL, "full-control" },
{ RGW_PERM_READ | RGW_PERM_WRITE, "read-write" },
{ RGW_PERM_READ, "read" },
{ RGW_PERM_WRITE, "write" },
{ RGW_PERM_READ_ACP, "read-acp" },
{ RGW_PERM_WRITE_ACP, "read-acp" },
{ 0, NULL }
};
void rgw_perm_to_str(uint32_t mask, char *buf, int len)
{
const char *sep = "";
int pos = 0;
if (!mask) {
snprintf(buf, len, "<none>");
return;
}
while (mask) {
uint32_t orig_mask = mask;
for (int i = 0; rgw_perms[i].mask; i++) {
struct rgw_flags_desc *desc = &rgw_perms[i];
if ((mask & desc->mask) == desc->mask) {
pos += snprintf(buf + pos, len - pos, "%s%s", sep, desc->str);
if (pos == len)
return;
sep = ", ";
mask &= ~desc->mask;
if (!mask)
return;
}
}
if (mask == orig_mask) // no change
break;
}
}
uint32_t rgw_str_to_perm(const char *str)
{
if (strcasecmp(str, "") == 0)
return RGW_PERM_NONE;
else if (strcasecmp(str, "read") == 0)
return RGW_PERM_READ;
else if (strcasecmp(str, "write") == 0)
return RGW_PERM_WRITE;
else if (strcasecmp(str, "readwrite") == 0)
return RGW_PERM_READ | RGW_PERM_WRITE;
else if (strcasecmp(str, "full") == 0)
return RGW_PERM_FULL_CONTROL;
return RGW_PERM_INVALID;
}
static bool validate_access_key(string& key)
{
const char *p = key.c_str();
while (*p) {
if (!char_is_unreserved_url(*p))
return false;
p++;
}
return true;
}
static void set_err_msg(std::string *sink, std::string msg)
{
if (sink && !msg.empty())
*sink = msg;
}
static bool remove_old_indexes(RGWRados *store,
RGWUserInfo& old_info, RGWUserInfo& new_info, std::string *err_msg)
{
int ret;
bool success = true;
if (!old_info.user_id.empty() && old_info.user_id.compare(new_info.user_id) != 0) {
ret = rgw_remove_uid_index(store, old_info.user_id);
if (ret < 0 && ret != -ENOENT) {
set_err_msg(err_msg, "ERROR: could not remove index for uid " + old_info.user_id);
success = false;
}
}
if (!old_info.user_email.empty() &&
old_info.user_email.compare(new_info.user_email) != 0) {
ret = rgw_remove_email_index(store, old_info.user_email);
if (ret < 0 && ret != -ENOENT) {
set_err_msg(err_msg, "ERROR: could not remove index for email " + old_info.user_email);
success = false;
}
}
map<string, RGWAccessKey>::iterator old_iter;
for (old_iter = old_info.swift_keys.begin(); old_iter != old_info.swift_keys.end(); ++old_iter) {
RGWAccessKey& swift_key = old_iter->second;
map<string, RGWAccessKey>::iterator new_iter = new_info.swift_keys.find(swift_key.id);
if (new_iter == new_info.swift_keys.end()) {
ret = rgw_remove_swift_name_index(store, swift_key.id);
if (ret < 0 && ret != -ENOENT) {
set_err_msg(err_msg, "ERROR: could not remove index for swift_name " + swift_key.id);
success = false;
}
}
}
return success;
}
/*
* Dump either the full user info or a subset to a formatter.
*
* NOTE: It is the caller's respnsibility to ensure that the
* formatter is flushed at the correct time.
*/
static void dump_subusers_info(Formatter *f, RGWUserInfo &info)
{
map<string, RGWSubUser>::iterator uiter;
f->open_array_section("subusers");
for (uiter = info.subusers.begin(); uiter != info.subusers.end(); ++uiter) {
RGWSubUser& u = uiter->second;
f->open_object_section("user");
f->dump_format("id", "%s:%s", info.user_id.c_str(), u.name.c_str());
char buf[256];
rgw_perm_to_str(u.perm_mask, buf, sizeof(buf));
f->dump_string("permissions", buf);
f->close_section();
}
f->close_section();
}
static void dump_access_keys_info(Formatter *f, RGWUserInfo &info)
{
map<string, RGWAccessKey>::iterator kiter;
f->open_array_section("keys");
for (kiter = info.access_keys.begin(); kiter != info.access_keys.end(); ++kiter) {
RGWAccessKey& k = kiter->second;
const char *sep = (k.subuser.empty() ? "" : ":");
const char *subuser = (k.subuser.empty() ? "" : k.subuser.c_str());
f->open_object_section("key");
f->dump_format("user", "%s%s%s", info.user_id.c_str(), sep, subuser);
f->dump_string("access_key", k.id);
f->dump_string("secret_key", k.key);
f->close_section();
}
f->close_section();
}
static void dump_swift_keys_info(Formatter *f, RGWUserInfo &info)
{
map<string, RGWAccessKey>::iterator kiter;
f->open_array_section("swift_keys");
for (kiter = info.swift_keys.begin(); kiter != info.swift_keys.end(); ++kiter) {
RGWAccessKey& k = kiter->second;
const char *sep = (k.subuser.empty() ? "" : ":");
const char *subuser = (k.subuser.empty() ? "" : k.subuser.c_str());
f->open_object_section("key");
f->dump_format("user", "%s%s%s", info.user_id.c_str(), sep, subuser);
f->dump_string("secret_key", k.key);
f->close_section();
}
f->close_section();
}
static void dump_user_info(Formatter *f, RGWUserInfo &info,
RGWStorageStats *stats = NULL)
{
f->open_object_section("user_info");
f->dump_string("user_id", info.user_id);
f->dump_string("display_name", info.display_name);
f->dump_string("email", info.user_email);
f->dump_int("suspended", (int)info.suspended);
f->dump_int("max_buckets", (int)info.max_buckets);
dump_subusers_info(f, info);
dump_access_keys_info(f, info);
dump_swift_keys_info(f, info);
info.caps.dump(f);
if (stats) {
encode_json("stats", *stats, f);
}
f->close_section();
}
RGWAccessKeyPool::RGWAccessKeyPool(RGWUser* usr)
{
user = usr;
swift_keys = NULL;
access_keys = NULL;
if (!user) {
keys_allowed = false;
store = NULL;
return;
}
keys_allowed = true;
store = user->get_store();
}
RGWAccessKeyPool::~RGWAccessKeyPool()
{
}
int RGWAccessKeyPool::init(RGWUserAdminOpState& op_state)
{
if (!op_state.is_initialized()) {
keys_allowed = false;
return -EINVAL;
}
std::string uid = op_state.get_user_id();
if (uid.compare(RGW_USER_ANON_ID) == 0) {
keys_allowed = false;
return -EACCES;
}
swift_keys = op_state.get_swift_keys();
access_keys = op_state.get_access_keys();
keys_allowed = true;
return 0;
}
/*
* Do a fairly exhaustive search for an existing key matching the parameters
* given. Also handles the case where no key type was specified and updates
* the operation state if needed.
*/
bool RGWAccessKeyPool::check_existing_key(RGWUserAdminOpState& op_state)
{
bool existing_key = false;
int key_type = op_state.get_key_type();
std::string kid = op_state.get_access_key();
std::map<std::string, RGWAccessKey>::iterator kiter;
std::string swift_kid = op_state.build_default_swift_kid();
RGWUserInfo dup_info;
if (kid.empty() && swift_kid.empty())
return false;
switch (key_type) {
case KEY_TYPE_SWIFT:
kiter = swift_keys->find(swift_kid);
existing_key = (kiter != swift_keys->end());
if (existing_key)
op_state.set_access_key(swift_kid);
break;
case KEY_TYPE_S3:
kiter = access_keys->find(kid);
existing_key = (kiter != access_keys->end());
break;
default:
kiter = access_keys->find(kid);
existing_key = (kiter != access_keys->end());
if (existing_key) {
op_state.set_key_type(KEY_TYPE_S3);
break;
}
kiter = swift_keys->find(kid);
existing_key = (kiter != swift_keys->end());
if (existing_key) {
op_state.set_key_type(KEY_TYPE_SWIFT);
break;
}
// handle the case where the access key was not provided in user:key format
if (swift_kid.empty())
return false;
kiter = swift_keys->find(swift_kid);
existing_key = (kiter != swift_keys->end());
if (existing_key) {
op_state.set_access_key(swift_kid);
op_state.set_key_type(KEY_TYPE_SWIFT);
}
}
op_state.set_existing_key(existing_key);
return existing_key;
}
int RGWAccessKeyPool::check_op(RGWUserAdminOpState& op_state,
std::string *err_msg)
{
RGWUserInfo dup_info;
if (!op_state.is_populated()) {
set_err_msg(err_msg, "user info was not populated");
return -EINVAL;
}
if (!keys_allowed) {
set_err_msg(err_msg, "keys not allowed for this user");
return -EACCES;
}
int32_t key_type = op_state.get_key_type();
// if a key type wasn't specified set it to s3
if (key_type < 0)
key_type = KEY_TYPE_S3;
op_state.set_key_type(key_type);
/* see if the access key was specified */
if (key_type == KEY_TYPE_S3 && !op_state.will_gen_access() &&
op_state.get_access_key().empty()) {
set_err_msg(err_msg, "empty access key");
return -EINVAL;
}
// don't check for secret key because we may be doing a removal
check_existing_key(op_state);
return 0;
}
// Generate a new random key
int RGWAccessKeyPool::generate_key(RGWUserAdminOpState& op_state, std::string *err_msg)
{
std::string id;
std::string key;
std::pair<std::string, RGWAccessKey> key_pair;
RGWAccessKey new_key;
RGWUserInfo duplicate_check;
int ret = 0;
int key_type = op_state.get_key_type();
bool gen_access = op_state.will_gen_access();
bool gen_secret = op_state.will_gen_secret();
if (!keys_allowed) {
set_err_msg(err_msg, "access keys not allowed for this user");
return -EACCES;
}
if (op_state.has_existing_key()) {
set_err_msg(err_msg, "cannot create existing key");
return -EEXIST;
}
if (!gen_access) {
id = op_state.get_access_key();
}
if (!id.empty()) {
switch (key_type) {
case KEY_TYPE_SWIFT:
if (rgw_get_user_info_by_swift(store, id, duplicate_check) >= 0) {
set_err_msg(err_msg, "existing swift key in RGW system:" + id);
return -EEXIST;
}
break;
case KEY_TYPE_S3:
if (rgw_get_user_info_by_access_key(store, id, duplicate_check) >= 0) {
set_err_msg(err_msg, "existing S3 key in RGW system:" + id);
return -EEXIST;
}
}
}
if (op_state.has_subuser())
new_key.subuser = op_state.get_subuser();
if (!gen_secret) {
key = op_state.get_secret_key();
} else if (gen_secret) {
char secret_key_buf[SECRET_KEY_LEN + 1];
ret = gen_rand_alphanumeric_plain(g_ceph_context, secret_key_buf, sizeof(secret_key_buf));
if (ret < 0) {
set_err_msg(err_msg, "unable to generate secret key");
return ret;
}
key = secret_key_buf;
}
// Generate the access key
if (key_type == KEY_TYPE_S3 && gen_access) {
char public_id_buf[PUBLIC_ID_LEN + 1];
do {
int id_buf_size = sizeof(public_id_buf);
ret = gen_rand_alphanumeric_upper(g_ceph_context,
public_id_buf, id_buf_size);
if (ret < 0) {
set_err_msg(err_msg, "unable to generate access key");
return ret;
}
id = public_id_buf;
if (!validate_access_key(id))
continue;
} while (!rgw_get_user_info_by_access_key(store, id, duplicate_check));
}
if (key_type == KEY_TYPE_SWIFT) {
id = op_state.build_default_swift_kid();
if (id.empty()) {
set_err_msg(err_msg, "empty swift access key");
return -EINVAL;
}
// check that the access key doesn't exist
if (rgw_get_user_info_by_swift(store, id, duplicate_check) >= 0) {
set_err_msg(err_msg, "cannot create existing swift key");
return -EEXIST;
}
}
// finally create the new key
new_key.id = id;
new_key.key = key;
key_pair.first = id;
key_pair.second = new_key;
if (key_type == KEY_TYPE_S3) {
access_keys->insert(key_pair);
} else if (key_type == KEY_TYPE_SWIFT) {
swift_keys->insert(key_pair);
}
return 0;
}
// modify an existing key
int RGWAccessKeyPool::modify_key(RGWUserAdminOpState& op_state, std::string *err_msg)
{
std::string id;
std::string key = op_state.get_secret_key();
int key_type = op_state.get_key_type();
RGWAccessKey modify_key;
pair<string, RGWAccessKey> key_pair;
map<std::string, RGWAccessKey>::iterator kiter;
switch (key_type) {
case KEY_TYPE_S3:
id = op_state.get_access_key();
if (id.empty()) {
set_err_msg(err_msg, "no access key specified");
return -EINVAL;
}
break;
case KEY_TYPE_SWIFT:
id = op_state.build_default_swift_kid();
if (id.empty()) {
set_err_msg(err_msg, "no subuser specified");
return -EINVAL;
}
break;
default:
set_err_msg(err_msg, "invalid key type");
return -EINVAL;
}
if (!op_state.has_existing_key()) {
set_err_msg(err_msg, "key does not exist");
return -EINVAL;
}
key_pair.first = id;
if (key_type == KEY_TYPE_SWIFT) {
modify_key.id = id;
modify_key.subuser = op_state.get_subuser();
} else if (key_type == KEY_TYPE_S3) {
kiter = access_keys->find(id);
if (kiter != access_keys->end()) {
modify_key = kiter->second;
}
}
if (op_state.will_gen_secret()) {
char secret_key_buf[SECRET_KEY_LEN + 1];