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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
|
// -*- 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 <boost/algorithm/string.hpp>
#include "common/errno.h"
#include "common/Formatter.h"
#include "common/ceph_json.h"
#include "common/RWLock.h"
#include "rgw_rados.h"
#include "rgw_zone.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"
#include "rgw_quota.h"
#include "services/svc_zone.h"
#include "services/svc_sys_obj.h"
#include "services/svc_sys_obj_cache.h"
#define dout_subsys ceph_subsys_rgw
static RGWMetadataHandler *user_meta_handler = NULL;
extern void op_type_to_str(uint32_t mask, char *buf, int len);
/**
* 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();
}
int rgw_user_sync_all_stats(RGWRados *store, const rgw_user& user_id)
{
CephContext *cct = store->ctx();
size_t max_entries = cct->_conf->rgw_list_buckets_max_chunk;
bool is_truncated = false;
string marker;
int ret;
RGWSysObjectCtx obj_ctx = store->svc.sysobj->init_obj_ctx();
do {
RGWUserBuckets user_buckets;
ret = rgw_read_user_buckets(store, user_id, user_buckets, marker,
string(), max_entries, false, &is_truncated);
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;
RGWBucketInfo bucket_info;
ret = store->get_bucket_info(obj_ctx, user_id.tenant, bucket_ent.bucket.name,
bucket_info, nullptr, nullptr);
if (ret < 0) {
ldout(cct, 0) << "ERROR: could not read bucket info: bucket=" << bucket_ent.bucket << " ret=" << ret << dendl;
continue;
}
ret = rgw_bucket_sync_user_stats(store, user_id, bucket_info);
if (ret < 0) {
ldout(cct, 0) << "ERROR: could not sync bucket stats: ret=" << ret << dendl;
return ret;
}
RGWQuotaInfo bucket_quota;
ret = store->check_bucket_shards(bucket_info, bucket_info.bucket, bucket_quota);
if (ret < 0) {
ldout(cct, 0) << "ERROR in check_bucket_shards: " << cpp_strerror(-ret)<< dendl;
}
}
} while (is_truncated);
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;
}
int rgw_user_get_all_buckets_stats(RGWRados *store, const rgw_user& user_id, map<string, cls_user_bucket_entry>&buckets_usage_map)
{
CephContext *cct = store->ctx();
size_t max_entries = cct->_conf->rgw_list_buckets_max_chunk;
bool done;
bool is_truncated;
string marker;
int ret;
do {
RGWUserBuckets user_buckets;
ret = rgw_read_user_buckets(store, user_id, user_buckets, marker,
string(), max_entries, false, &is_truncated);
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 (const auto& i : buckets) {
marker = i.first;
const RGWBucketEnt& bucket_ent = i.second;
cls_user_bucket_entry entry;
ret = store->cls_user_get_bucket_stats(bucket_ent.bucket, entry);
if (ret < 0) {
ldout(cct, 0) << "ERROR: could not get bucket stats: ret=" << ret << dendl;
return ret;
}
buckets_usage_map.emplace(bucket_ent.bucket.name, entry);
}
done = (buckets.size() < max_entries);
} while (!done);
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,
real_time mtime,
bool exclusive,
map<string, bufferlist> *pattrs)
{
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);
string key;
info.user_id.to_str(key);
ret = store->meta_mgr->put_entry(user_meta_handler, key, 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->svc.zone->get_zone_params().user_email_pool, info.user_email,
link_bl, exclusive, NULL, real_time());
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->svc.zone->get_zone_params().user_keys_pool, k.id,
link_bl, exclusive, NULL, real_time());
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->svc.zone->get_zone_params().user_swift_pool, k.id,
link_bl, exclusive, NULL, real_time());
if (ret < 0)
return ret;
}
return ret;
}
struct user_info_entry {
RGWUserInfo info;
RGWObjVersionTracker objv_tracker;
real_time mtime;
};
static RGWChainedCacheImpl<user_info_entry> uinfo_cache;
int rgw_get_user_info_from_index(RGWRados * const store,
const string& key,
const rgw_pool& pool,
RGWUserInfo& info,
RGWObjVersionTracker * const objv_tracker,
real_time * const pmtime)
{
if (auto e = uinfo_cache.find(key)) {
info = e->info;
if (objv_tracker)
*objv_tracker = e->objv_tracker;
if (pmtime)
*pmtime = e->mtime;
return 0;
}
user_info_entry e;
bufferlist bl;
RGWUID uid;
auto obj_ctx = store->svc.sysobj->init_obj_ctx();
int ret = rgw_get_system_obj(store, obj_ctx, pool, key, bl, NULL, &e.mtime);
if (ret < 0)
return ret;
rgw_cache_entry_info cache_info;
auto iter = bl.cbegin();
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;
}
uinfo_cache.put(store->svc.cache, key, &e, { &cache_info });
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,
const rgw_user& uid,
RGWUserInfo& info,
RGWObjVersionTracker * const objv_tracker,
real_time * const pmtime,
rgw_cache_entry_info * const cache_info,
map<string, bufferlist> * const pattrs)
{
bufferlist bl;
RGWUID user_id;
auto obj_ctx = store->svc.sysobj->init_obj_ctx();
string oid = uid.to_str();
int ret = rgw_get_system_obj(store, obj_ctx, store->svc.zone->get_zone_params().user_uid_pool, oid, bl, objv_tracker, pmtime, pattrs, cache_info);
if (ret < 0) {
return ret;
}
auto iter = bl.cbegin();
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, real_time *pmtime)
{
return rgw_get_user_info_from_index(store, email, store->svc.zone->get_zone_params().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 * const store,
const string& swift_name,
RGWUserInfo& info, /* out */
RGWObjVersionTracker * const objv_tracker,
real_time * const pmtime)
{
return rgw_get_user_info_from_index(store, swift_name,
store->svc.zone->get_zone_params().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,
const std::string& access_key,
RGWUserInfo& info,
RGWObjVersionTracker* objv_tracker,
real_time *pmtime)
{
return rgw_get_user_info_from_index(store, access_key,
store->svc.zone->get_zone_params().user_keys_pool,
info, objv_tracker, pmtime);
}
int rgw_get_user_attrs_by_uid(RGWRados *store,
const rgw_user& user_id,
map<string, bufferlist>& attrs,
RGWObjVersionTracker *objv_tracker)
{
auto obj_ctx = store->svc.sysobj->init_obj_ctx();
rgw_raw_obj obj(store->svc.zone->get_zone_params().user_uid_pool, user_id.to_str());
auto src = obj_ctx.get_obj(obj);
return src.rop()
.set_attrs(&attrs)
.set_objv_tracker(objv_tracker)
.stat();
}
int rgw_remove_key_index(RGWRados *store, RGWAccessKey& access_key)
{
rgw_raw_obj obj(store->svc.zone->get_zone_params().user_keys_pool, access_key.id);
auto obj_ctx = store->svc.sysobj->init_obj_ctx();
auto sysobj = obj_ctx.get_obj(obj);
return sysobj.wop().remove();
}
int rgw_remove_uid_index(RGWRados *store, rgw_user& 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;
string oid = uid.to_str();
ret = store->meta_mgr->remove_entry(user_meta_handler, oid, &objv_tracker);
if (ret < 0)
return ret;
return 0;
}
int rgw_remove_email_index(RGWRados *store, string& email)
{
if (email.empty()) {
return 0;
}
rgw_raw_obj obj(store->svc.zone->get_zone_params().user_email_pool, email);
auto obj_ctx = store->svc.sysobj->init_obj_ctx();
auto sysobj = obj_ctx.get_obj(obj);
return sysobj.wop().remove();
}
int rgw_remove_swift_name_index(RGWRados *store, string& swift_name)
{
rgw_raw_obj obj(store->svc.zone->get_zone_params().user_swift_pool, swift_name);
auto obj_ctx = store->svc.sysobj->init_obj_ctx();
auto sysobj = obj_ctx.get_obj(obj);
return sysobj.wop().remove();
}
/**
* 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) {
int ret;
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;
}
}
ldout(store->ctx(), 10) << "removing email index: " << info.user_email << dendl;
ret = rgw_remove_email_index(store, info.user_email);
if (ret < 0 && ret != -ENOENT) {
ldout(store->ctx(), 0) << "ERROR: could not remove email index object for "
<< info.user_email << ", should be fixed (err=" << ret << ")" << dendl;
return ret;
}
string buckets_obj_id;
rgw_get_buckets_obj(info.user_id, buckets_obj_id);
rgw_raw_obj uid_bucks(store->svc.zone->get_zone_params().user_uid_pool, buckets_obj_id);
ldout(store->ctx(), 10) << "removing user buckets index" << dendl;
auto obj_ctx = store->svc.sysobj->init_obj_ctx();
auto sysobj = obj_ctx.get_obj(uid_bucks);
ret = sysobj.wop().remove();
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;
}
string key;
info.user_id.to_str(key);
rgw_raw_obj uid_obj(store->svc.zone->get_zone_params().user_uid_pool, key);
ldout(store->ctx(), 10) << "removing user index: " << info.user_id << dendl;
ret = store->meta_mgr->remove_entry(user_meta_handler, key, &objv_tracker);
if (ret < 0 && ret != -ENOENT && ret != -ECANCELED) {
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, "write-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;
}
int rgw_validate_tenant_name(const string& t)
{
struct tench {
static bool is_good(char ch) {
return isalnum(ch) || ch == '_';
}
};
std::string::const_iterator it =
std::find_if_not(t.begin(), t.end(), tench::is_good);
return (it == t.end())? 0: -ERR_INVALID_TENANT_NAME;
}
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) {
if (old_info.user_id.tenant != new_info.user_id.tenant) {
ldout(store->ctx(), 0) << "ERROR: tenant mismatch: " << old_info.user_id.tenant << " != " << new_info.user_id.tenant << dendl;
return false;
}
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.to_str());
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");
string s;
info.user_id.to_str(s);
f->dump_format("id", "%s:%s", s.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");
string s;
info.user_id.to_str(s);
f->dump_format("user", "%s%s%s", s.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");
string s;
info.user_id.to_str(s);
f->dump_format("user", "%s%s%s", s.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");
encode_json("tenant", info.user_id.tenant, f);
encode_json("user_id", info.user_id.id, f);
encode_json("display_name", info.display_name, f);
encode_json("email", info.user_email, f);
encode_json("suspended", (int)info.suspended, f);
encode_json("max_buckets", (int)info.max_buckets, f);
dump_subusers_info(f, info);
dump_access_keys_info(f, info);
dump_swift_keys_info(f, info);
encode_json("caps", info.caps, f);
char buf[256];
op_type_to_str(info.op_mask, buf, sizeof(buf));
encode_json("op_mask", (const char *)buf, f);
encode_json("system", (bool)info.system, f);
encode_json("admin", (bool)info.admin, f);
encode_json("default_placement", info.default_placement.name, f);
encode_json("default_storage_class", info.default_placement.storage_class, f);
encode_json("placement_tags", info.placement_tags, f);
encode_json("bucket_quota", info.bucket_quota, f);
encode_json("user_quota", info.user_quota, f);
encode_json("temp_url_keys", info.temp_url_keys, f);
string user_source_type;
switch ((RGWIdentityType)info.type) {
case TYPE_RGW:
user_source_type = "rgw";
break;
case TYPE_KEYSTONE:
user_source_type = "keystone";
break;
case TYPE_LDAP:
user_source_type = "ldap";
break;
case TYPE_NONE:
user_source_type = "none";
break;
default:
user_source_type = "none";
break;
}
encode_json("type", user_source_type, f);
encode_json("mfa_ids", info.mfa_ids, 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;
}
rgw_user& 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
if (key_type < 0) {
if (op_state.has_subuser()) {
key_type = KEY_TYPE_SWIFT;
} else {
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 -ERR_INVALID_ACCESS_KEY;
}
// 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 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 -ERR_KEY_EXIST;
}
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 -ERR_KEY_EXIST;
}
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 -ERR_KEY_EXIST;
}
}
}
//key's subuser
if (op_state.has_subuser()) {
//create user and subuser at the same time, user's s3 key should not be set this
if (!op_state.key_type_setbycontext || (key_type == KEY_TYPE_SWIFT)) {
new_key.subuser = op_state.get_subuser();
}
}
//Secret key
if (!gen_secret) {
if (op_state.get_secret_key().empty()) {
set_err_msg(err_msg, "empty secret key");
return -ERR_INVALID_SECRET_KEY;
}
key = op_state.get_secret_key();
} else {
char secret_key_buf[SECRET_KEY_LEN + 1];
gen_rand_alphanumeric_plain(g_ceph_context, secret_key_buf, sizeof(secret_key_buf));
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);
gen_rand_alphanumeric_upper(g_ceph_context, public_id_buf, id_buf_size);
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 -ERR_INVALID_ACCESS_KEY;
}
// 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 -ERR_KEY_EXIST;
}
}
// 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 -ERR_INVALID_ACCESS_KEY;
}
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 -ERR_INVALID_KEY_TYPE;
}
if (!op_state.has_existing_key()) {
set_err_msg(err_msg, "key does not exist");
return -ERR_INVALID_ACCESS_KEY;
}
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];
int key_buf_size = sizeof(secret_key_buf);
gen_rand_alphanumeric_plain(g_ceph_context, secret_key_buf, key_buf_size);
key = secret_key_buf;
}
if (key.empty()) {
set_err_msg(err_msg, "empty secret key");
return -ERR_INVALID_SECRET_KEY;
}
// update the access key with the new secret key
modify_key.key = key;
key_pair.second = modify_key;
if (key_type == KEY_TYPE_S3) {
(*access_keys)[id] = modify_key;
} else if (key_type == KEY_TYPE_SWIFT) {
(*swift_keys)[id] = modify_key;
}
return 0;
}
int RGWAccessKeyPool::execute_add(RGWUserAdminOpState& op_state,
std::string *err_msg, bool defer_user_update)
{
int ret = 0;
std::string subprocess_msg;
int key_op = GENERATE_KEY;
// set the op
if (op_state.has_existing_key())
key_op = MODIFY_KEY;
switch (key_op) {
case GENERATE_KEY:
ret = generate_key(op_state, &subprocess_msg);
break;
case MODIFY_KEY:
ret = modify_key(op_state, &subprocess_msg);
break;
}
if (ret < 0) {
set_err_msg(err_msg, subprocess_msg);
return ret;
}
// store the updated info
if (!defer_user_update)
ret = user->update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
int RGWAccessKeyPool::add(RGWUserAdminOpState& op_state, std::string *err_msg)
{
return add(op_state, err_msg, false);
}
int RGWAccessKeyPool::add(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_user_update)
{
int ret;
std::string subprocess_msg;
ret = check_op(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to parse request, " + subprocess_msg);
return ret;
}
ret = execute_add(op_state, &subprocess_msg, defer_user_update);
if (ret < 0) {
set_err_msg(err_msg, "unable to add access key, " + subprocess_msg);
return ret;
}
return 0;
}
int RGWAccessKeyPool::execute_remove(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_user_update)
{
int ret = 0;
int key_type = op_state.get_key_type();
std::string id = op_state.get_access_key();
map<std::string, RGWAccessKey>::iterator kiter;
map<std::string, RGWAccessKey> *keys_map;
if (!op_state.has_existing_key()) {
set_err_msg(err_msg, "unable to find access key");
return -ERR_INVALID_ACCESS_KEY;
}
if (key_type == KEY_TYPE_S3) {
keys_map = access_keys;
} else if (key_type == KEY_TYPE_SWIFT) {
keys_map = swift_keys;
} else {
keys_map = NULL;
set_err_msg(err_msg, "invalid access key");
return -ERR_INVALID_ACCESS_KEY;
}
kiter = keys_map->find(id);
if (kiter == keys_map->end()) {
set_err_msg(err_msg, "key not found");
return -ERR_INVALID_ACCESS_KEY;
}
rgw_remove_key_index(store, kiter->second);
keys_map->erase(kiter);
if (!defer_user_update)
ret = user->update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
int RGWAccessKeyPool::remove(RGWUserAdminOpState& op_state, std::string *err_msg)
{
return remove(op_state, err_msg, false);
}
int RGWAccessKeyPool::remove(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_user_update)
{
int ret;
std::string subprocess_msg;
ret = check_op(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to parse request, " + subprocess_msg);
return ret;
}
ret = execute_remove(op_state, &subprocess_msg, defer_user_update);
if (ret < 0) {
set_err_msg(err_msg, "unable to remove access key, " + subprocess_msg);
return ret;
}
return 0;
}
// remove all keys associated with a subuser
int RGWAccessKeyPool::remove_subuser_keys(RGWUserAdminOpState& op_state,
std::string *err_msg, bool defer_user_update)
{
int ret = 0;
if (!op_state.is_populated()) {
set_err_msg(err_msg, "user info was not populated");
return -EINVAL;
}
if (!op_state.has_subuser()) {
set_err_msg(err_msg, "no subuser specified");
return -EINVAL;
}
std::string swift_kid = op_state.build_default_swift_kid();
if (swift_kid.empty()) {
set_err_msg(err_msg, "empty swift access key");
return -EINVAL;
}
map<std::string, RGWAccessKey>::iterator kiter;
map<std::string, RGWAccessKey> *keys_map;
// a subuser can have at most one swift key
keys_map = swift_keys;
kiter = keys_map->find(swift_kid);
if (kiter != keys_map->end()) {
rgw_remove_key_index(store, kiter->second);
keys_map->erase(kiter);
}
// a subuser may have multiple s3 key pairs
std::string subuser_str = op_state.get_subuser();
keys_map = access_keys;
RGWUserInfo user_info = op_state.get_user_info();
map<std::string, RGWAccessKey>::iterator user_kiter = user_info.access_keys.begin();
for (; user_kiter != user_info.access_keys.end(); ++user_kiter) {
if (user_kiter->second.subuser == subuser_str) {
kiter = keys_map->find(user_kiter->first);
if (kiter != keys_map->end()) {
rgw_remove_key_index(store, kiter->second);
keys_map->erase(kiter);
}
}
}
if (!defer_user_update)
ret = user->update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
RGWSubUserPool::RGWSubUserPool(RGWUser *usr)
{
subusers_allowed = (usr != NULL);
if (usr)
store = usr->get_store();
else
store = NULL;
user = usr;
subuser_map = NULL;
}
RGWSubUserPool::~RGWSubUserPool()
{
}
int RGWSubUserPool::init(RGWUserAdminOpState& op_state)
{
if (!op_state.is_initialized()) {
subusers_allowed = false;
return -EINVAL;
}
rgw_user& uid = op_state.get_user_id();
if (uid.compare(RGW_USER_ANON_ID) == 0) {
subusers_allowed = false;
return -EACCES;
}
subuser_map = op_state.get_subusers();
if (subuser_map == NULL) {
subusers_allowed = false;
return -EINVAL;
}
subusers_allowed = true;
return 0;
}
bool RGWSubUserPool::exists(std::string subuser)
{
if (subuser.empty())
return false;
if (!subuser_map)
return false;
if (subuser_map->count(subuser))
return true;
return false;
}
int RGWSubUserPool::check_op(RGWUserAdminOpState& op_state,
std::string *err_msg)
{
bool existing = false;
std::string subuser = op_state.get_subuser();
if (!op_state.is_populated()) {
set_err_msg(err_msg, "user info was not populated");
return -EINVAL;
}
if (!subusers_allowed) {
set_err_msg(err_msg, "subusers not allowed for this user");
return -EACCES;
}
if (subuser.empty() && !op_state.will_gen_subuser()) {
set_err_msg(err_msg, "empty subuser name");
return -EINVAL;
}
if (op_state.get_subuser_perm() == RGW_PERM_INVALID) {
set_err_msg(err_msg, "invaild subuser access");
return -EINVAL;
}
//set key type when it not set or set by context
if ((op_state.get_key_type() < 0) || op_state.key_type_setbycontext) {
op_state.set_key_type(KEY_TYPE_SWIFT);
op_state.key_type_setbycontext = true;
}
// check if the subuser exists
if (!subuser.empty())
existing = exists(subuser);
op_state.set_existing_subuser(existing);
return 0;
}
int RGWSubUserPool::execute_add(RGWUserAdminOpState& op_state,
std::string *err_msg, bool defer_user_update)
{
int ret = 0;
std::string subprocess_msg;
RGWSubUser subuser;
std::pair<std::string, RGWSubUser> subuser_pair;
std::string subuser_str = op_state.get_subuser();
subuser_pair.first = subuser_str;
// assumes key should be created
if (op_state.has_key_op()) {
ret = user->keys.add(op_state, &subprocess_msg, true);
if (ret < 0) {
set_err_msg(err_msg, "unable to create subuser key, " + subprocess_msg);
return ret;
}
}
// create the subuser
subuser.name = subuser_str;
if (op_state.has_subuser_perm())
subuser.perm_mask = op_state.get_subuser_perm();
// insert the subuser into user info
subuser_pair.second = subuser;
subuser_map->insert(subuser_pair);
// attempt to save the subuser
if (!defer_user_update)
ret = user->update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
int RGWSubUserPool::add(RGWUserAdminOpState& op_state, std::string *err_msg)
{
return add(op_state, err_msg, false);
}
int RGWSubUserPool::add(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_user_update)
{
std::string subprocess_msg;
int ret;
int32_t key_type = op_state.get_key_type();
ret = check_op(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to parse request, " + subprocess_msg);
return ret;
}
if (key_type == KEY_TYPE_S3 && op_state.get_access_key().empty()) {
op_state.set_gen_access();
}
if (op_state.get_secret_key().empty()) {
op_state.set_gen_secret();
}
ret = execute_add(op_state, &subprocess_msg, defer_user_update);
if (ret < 0) {
set_err_msg(err_msg, "unable to create subuser, " + subprocess_msg);
return ret;
}
return 0;
}
int RGWSubUserPool::execute_remove(RGWUserAdminOpState& op_state,
std::string *err_msg, bool defer_user_update)
{
int ret = 0;
std::string subprocess_msg;
std::string subuser_str = op_state.get_subuser();
map<std::string, RGWSubUser>::iterator siter;
siter = subuser_map->find(subuser_str);
if (siter == subuser_map->end()){
set_err_msg(err_msg, "subuser not found: " + subuser_str);
return -ERR_NO_SUCH_SUBUSER;
}
if (!op_state.has_existing_subuser()) {
set_err_msg(err_msg, "subuser not found: " + subuser_str);
return -ERR_NO_SUCH_SUBUSER;
}
// always purge all associate keys
user->keys.remove_subuser_keys(op_state, &subprocess_msg, true);
// remove the subuser from the user info
subuser_map->erase(siter);
// attempt to save the subuser
if (!defer_user_update)
ret = user->update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
int RGWSubUserPool::remove(RGWUserAdminOpState& op_state, std::string *err_msg)
{
return remove(op_state, err_msg, false);
}
int RGWSubUserPool::remove(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_user_update)
{
std::string subprocess_msg;
int ret;
ret = check_op(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to parse request, " + subprocess_msg);
return ret;
}
ret = execute_remove(op_state, &subprocess_msg, defer_user_update);
if (ret < 0) {
set_err_msg(err_msg, "unable to remove subuser, " + subprocess_msg);
return ret;
}
return 0;
}
int RGWSubUserPool::execute_modify(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_user_update)
{
int ret = 0;
std::string subprocess_msg;
std::map<std::string, RGWSubUser>::iterator siter;
std::pair<std::string, RGWSubUser> subuser_pair;
std::string subuser_str = op_state.get_subuser();
RGWSubUser subuser;
if (!op_state.has_existing_subuser()) {
set_err_msg(err_msg, "subuser does not exist");
return -ERR_NO_SUCH_SUBUSER;
}
subuser_pair.first = subuser_str;
siter = subuser_map->find(subuser_str);
subuser = siter->second;
if (op_state.has_key_op()) {
ret = user->keys.add(op_state, &subprocess_msg, true);
if (ret < 0) {
set_err_msg(err_msg, "unable to create subuser keys, " + subprocess_msg);
return ret;
}
}
if (op_state.has_subuser_perm())
subuser.perm_mask = op_state.get_subuser_perm();
subuser_pair.second = subuser;
subuser_map->erase(siter);
subuser_map->insert(subuser_pair);
// attempt to save the subuser
if (!defer_user_update)
ret = user->update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
int RGWSubUserPool::modify(RGWUserAdminOpState& op_state, std::string *err_msg)
{
return RGWSubUserPool::modify(op_state, err_msg, false);
}
int RGWSubUserPool::modify(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_user_update)
{
std::string subprocess_msg;
int ret;
RGWSubUser subuser;
ret = check_op(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to parse request, " + subprocess_msg);
return ret;
}
ret = execute_modify(op_state, &subprocess_msg, defer_user_update);
if (ret < 0) {
set_err_msg(err_msg, "unable to modify subuser, " + subprocess_msg);
return ret;
}
return 0;
}
RGWUserCapPool::RGWUserCapPool(RGWUser *usr)
{
user = usr;
caps = NULL;
caps_allowed = (user != NULL);
}
RGWUserCapPool::~RGWUserCapPool()
{
}
int RGWUserCapPool::init(RGWUserAdminOpState& op_state)
{
if (!op_state.is_initialized()) {
caps_allowed = false;
return -EINVAL;
}
rgw_user& uid = op_state.get_user_id();
if (uid.compare(RGW_USER_ANON_ID) == 0) {
caps_allowed = false;
return -EACCES;
}
caps = op_state.get_caps_obj();
if (!caps) {
caps_allowed = false;
return -ERR_INVALID_CAP;
}
caps_allowed = true;
return 0;
}
int RGWUserCapPool::add(RGWUserAdminOpState& op_state, std::string *err_msg)
{
return add(op_state, err_msg, false);
}
int RGWUserCapPool::add(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_save)
{
int ret = 0;
std::string caps_str = op_state.get_caps();
if (!op_state.is_populated()) {
set_err_msg(err_msg, "user info was not populated");
return -EINVAL;
}
if (!caps_allowed) {
set_err_msg(err_msg, "caps not allowed for this user");
return -EACCES;
}
if (caps_str.empty()) {
set_err_msg(err_msg, "empty user caps");
return -ERR_INVALID_CAP;
}
int r = caps->add_from_string(caps_str);
if (r < 0) {
set_err_msg(err_msg, "unable to add caps: " + caps_str);
return r;
}
if (!defer_save)
ret = user->update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
int RGWUserCapPool::remove(RGWUserAdminOpState& op_state, std::string *err_msg)
{
return remove(op_state, err_msg, false);
}
int RGWUserCapPool::remove(RGWUserAdminOpState& op_state, std::string *err_msg, bool defer_save)
{
int ret = 0;
std::string caps_str = op_state.get_caps();
if (!op_state.is_populated()) {
set_err_msg(err_msg, "user info was not populated");
return -EINVAL;
}
if (!caps_allowed) {
set_err_msg(err_msg, "caps not allowed for this user");
return -EACCES;
}
if (caps_str.empty()) {
set_err_msg(err_msg, "empty user caps");
return -ERR_INVALID_CAP;
}
int r = caps->remove_from_string(caps_str);
if (r < 0) {
set_err_msg(err_msg, "unable to remove caps: " + caps_str);
return r;
}
if (!defer_save)
ret = user->update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
RGWUser::RGWUser() : store(NULL), info_stored(false), caps(this), keys(this), subusers(this)
{
init_default();
}
int RGWUser::init(RGWRados *storage, RGWUserAdminOpState& op_state)
{
init_default();
int ret = init_storage(storage);
if (ret < 0)
return ret;
ret = init(op_state);
if (ret < 0)
return ret;
return 0;
}
RGWUser::~RGWUser()
{
}
void RGWUser::init_default()
{
// use anonymous user info as a placeholder
rgw_get_anon_user(old_info);
user_id = RGW_USER_ANON_ID;
clear_populated();
}
int RGWUser::init_storage(RGWRados *storage)
{
if (!storage) {
return -EINVAL;
}
store = storage;
clear_populated();
/* API wrappers */
keys = RGWAccessKeyPool(this);
caps = RGWUserCapPool(this);
subusers = RGWSubUserPool(this);
return 0;
}
int RGWUser::init(RGWUserAdminOpState& op_state)
{
bool found = false;
std::string swift_user;
user_id = op_state.get_user_id();
std::string user_email = op_state.get_user_email();
std::string access_key = op_state.get_access_key();
std::string subuser = op_state.get_subuser();
int key_type = op_state.get_key_type();
if (key_type == KEY_TYPE_SWIFT) {
swift_user = op_state.get_access_key();
access_key.clear();
}
RGWUserInfo user_info;
clear_populated();
if (user_id.empty() && !subuser.empty()) {
size_t pos = subuser.find(':');
if (pos != string::npos) {
user_id = subuser.substr(0, pos);
op_state.set_user_id(user_id);
}
}
if (!user_id.empty() && (user_id.compare(RGW_USER_ANON_ID) != 0)) {
found = (rgw_get_user_info_by_uid(store, user_id, user_info, &op_state.objv) >= 0);
op_state.found_by_uid = found;
}
if (store->ctx()->_conf.get_val<bool>("rgw_user_unique_email")) {
if (!user_email.empty() && !found) {
found = (rgw_get_user_info_by_email(store, user_email, user_info, &op_state.objv) >= 0);
op_state.found_by_email = found;
}
}
if (!swift_user.empty() && !found) {
found = (rgw_get_user_info_by_swift(store, swift_user, user_info, &op_state.objv) >= 0);
op_state.found_by_key = found;
}
if (!access_key.empty() && !found) {
found = (rgw_get_user_info_by_access_key(store, access_key, user_info, &op_state.objv) >= 0);
op_state.found_by_key = found;
}
op_state.set_existing_user(found);
if (found) {
op_state.set_user_info(user_info);
op_state.set_populated();
old_info = user_info;
set_populated();
}
if (user_id.empty()) {
user_id = user_info.user_id;
}
op_state.set_initialized();
// this may have been called by a helper object
int ret = init_members(op_state);
if (ret < 0)
return ret;
return 0;
}
int RGWUser::init_members(RGWUserAdminOpState& op_state)
{
int ret = 0;
ret = keys.init(op_state);
if (ret < 0)
return ret;
ret = subusers.init(op_state);
if (ret < 0)
return ret;
ret = caps.init(op_state);
if (ret < 0)
return ret;
return 0;
}
int RGWUser::update(RGWUserAdminOpState& op_state, std::string *err_msg)
{
int ret;
std::string subprocess_msg;
RGWUserInfo user_info = op_state.get_user_info();
if (!store) {
set_err_msg(err_msg, "couldn't initialize storage");
return -EINVAL;
}
if (is_populated()) {
ret = rgw_store_user_info(store, user_info, &old_info, &op_state.objv, real_time(), false);
if (ret < 0) {
set_err_msg(err_msg, "unable to store user info");
return ret;
}
ret = remove_old_indexes(store, old_info, user_info, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to remove old user info, " + subprocess_msg);
return ret;
}
} else {
ret = rgw_store_user_info(store, user_info, NULL, &op_state.objv, real_time(), false);
if (ret < 0) {
set_err_msg(err_msg, "unable to store user info");
return ret;
}
}
old_info = user_info;
set_populated();
return 0;
}
int RGWUser::check_op(RGWUserAdminOpState& op_state, std::string *err_msg)
{
bool same_id;
bool populated;
rgw_user& op_id = op_state.get_user_id();
RGWUserInfo user_info;
same_id = (user_id.compare(op_id) == 0);
populated = is_populated();
if (op_id.compare(RGW_USER_ANON_ID) == 0) {
set_err_msg(err_msg, "unable to perform operations on the anonymous user");
return -EINVAL;
}
if (populated && !same_id) {
set_err_msg(err_msg, "user id mismatch, operation id: " + op_id.to_str()
+ " does not match: " + user_id.to_str());
return -EINVAL;
}
int ret = rgw_validate_tenant_name(op_id.tenant);
if (ret) {
set_err_msg(err_msg,
"invalid tenant only alphanumeric and _ characters are allowed");
return ret;
}
//set key type when it not set or set by context
if ((op_state.get_key_type() < 0) || op_state.key_type_setbycontext) {
op_state.set_key_type(KEY_TYPE_S3);
op_state.key_type_setbycontext = true;
}
return 0;
}
int RGWUser::execute_add(RGWUserAdminOpState& op_state, std::string *err_msg)
{
std::string subprocess_msg;
int ret = 0;
bool defer_user_update = true;
RGWUserInfo user_info;
rgw_user& uid = op_state.get_user_id();
std::string user_email = op_state.get_user_email();
std::string display_name = op_state.get_display_name();
// fail if the user exists already
if (op_state.has_existing_user()) {
if (!op_state.exclusive &&
(user_email.empty() ||
boost::iequals(user_email, old_info.user_email)) &&
old_info.display_name == display_name) {
return execute_modify(op_state, err_msg);
}
if (op_state.found_by_email) {
set_err_msg(err_msg, "email: " + user_email +
" is the email address an existing user");
ret = -ERR_EMAIL_EXIST;
} else if (op_state.found_by_key) {
set_err_msg(err_msg, "duplicate key provided");
ret = -ERR_KEY_EXIST;
} else {
set_err_msg(err_msg, "user: " + op_state.user_id.to_str() + " exists");
ret = -EEXIST;
}
return ret;
}
// fail if the user_info has already been populated
if (op_state.is_populated()) {
set_err_msg(err_msg, "cannot overwrite already populated user");
return -EEXIST;
}
// fail if the display name was not included
if (display_name.empty()) {
set_err_msg(err_msg, "no display name specified");
return -EINVAL;
}
// set the user info
user_id = uid;
user_info.user_id = user_id;
user_info.display_name = display_name;
user_info.type = TYPE_RGW;
if (!user_email.empty())
user_info.user_email = user_email;
CephContext *cct = store->ctx();
if (op_state.max_buckets_specified) {
user_info.max_buckets = op_state.get_max_buckets();
} else {
user_info.max_buckets =
cct->_conf.get_val<int64_t>("rgw_user_max_buckets");
}
user_info.suspended = op_state.get_suspension_status();
user_info.admin = op_state.admin;
user_info.system = op_state.system;
if (op_state.op_mask_specified)
user_info.op_mask = op_state.get_op_mask();
if (op_state.has_bucket_quota()) {
user_info.bucket_quota = op_state.get_bucket_quota();
} else {
rgw_apply_default_bucket_quota(user_info.bucket_quota, cct->_conf);
}
if (op_state.temp_url_key_specified) {
map<int, string>::iterator iter;
for (iter = op_state.temp_url_keys.begin();
iter != op_state.temp_url_keys.end(); ++iter) {
user_info.temp_url_keys[iter->first] = iter->second;
}
}
if (op_state.has_user_quota()) {
user_info.user_quota = op_state.get_user_quota();
} else {
rgw_apply_default_user_quota(user_info.user_quota, cct->_conf);
}
// update the request
op_state.set_user_info(user_info);
op_state.set_populated();
// update the helper objects
ret = init_members(op_state);
if (ret < 0) {
set_err_msg(err_msg, "unable to initialize user");
return ret;
}
// see if we need to add an access key
if (op_state.has_key_op()) {
ret = keys.add(op_state, &subprocess_msg, defer_user_update);
if (ret < 0) {
set_err_msg(err_msg, "unable to create access key, " + subprocess_msg);
return ret;
}
}
// see if we need to add some caps
if (op_state.has_caps_op()) {
ret = caps.add(op_state, &subprocess_msg, defer_user_update);
if (ret < 0) {
set_err_msg(err_msg, "unable to add user capabilities, " + subprocess_msg);
return ret;
}
}
ret = update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
int RGWUser::add(RGWUserAdminOpState& op_state, std::string *err_msg)
{
std::string subprocess_msg;
int ret;
ret = check_op(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to parse parameters, " + subprocess_msg);
return ret;
}
ret = execute_add(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to create user, " + subprocess_msg);
return ret;
}
return 0;
}
int RGWUser::execute_remove(RGWUserAdminOpState& op_state, std::string *err_msg)
{
int ret;
bool purge_data = op_state.will_purge_data();
rgw_user& uid = op_state.get_user_id();
RGWUserInfo user_info = op_state.get_user_info();
if (!op_state.has_existing_user()) {
set_err_msg(err_msg, "user does not exist");
return -ENOENT;
}
bool is_truncated = false;
string marker;
CephContext *cct = store->ctx();
size_t max_buckets = cct->_conf->rgw_list_buckets_max_chunk;
do {
RGWUserBuckets buckets;
ret = rgw_read_user_buckets(store, uid, buckets, marker, string(),
max_buckets, false, &is_truncated);
if (ret < 0) {
set_err_msg(err_msg, "unable to read user bucket info");
return ret;
}
map<std::string, RGWBucketEnt>& m = buckets.get_buckets();
if (!m.empty() && !purge_data) {
set_err_msg(err_msg, "must specify purge data to remove user with buckets");
return -EEXIST; // change to code that maps to 409: conflict
}
std::map<std::string, RGWBucketEnt>::iterator it;
for (it = m.begin(); it != m.end(); ++it) {
ret = rgw_remove_bucket(store, ((*it).second).bucket, true);
if (ret < 0) {
set_err_msg(err_msg, "unable to delete user data");
return ret;
}
marker = it->first;
}
} while (is_truncated);
ret = rgw_delete_user(store, user_info, op_state.objv);
if (ret < 0) {
set_err_msg(err_msg, "unable to remove user from RADOS");
return ret;
}
op_state.clear_populated();
clear_populated();
return 0;
}
int RGWUser::remove(RGWUserAdminOpState& op_state, std::string *err_msg)
{
std::string subprocess_msg;
int ret;
ret = check_op(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to parse parameters, " + subprocess_msg);
return ret;
}
ret = execute_remove(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to remove user, " + subprocess_msg);
return ret;
}
return 0;
}
int RGWUser::execute_modify(RGWUserAdminOpState& op_state, std::string *err_msg)
{
bool populated = op_state.is_populated();
int ret = 0;
std::string subprocess_msg;
std::string op_email = op_state.get_user_email();
std::string display_name = op_state.get_display_name();
RGWUserInfo user_info;
RGWUserInfo duplicate_check;
// ensure that the user info has been populated or is populate-able
if (!op_state.has_existing_user() && !populated) {
set_err_msg(err_msg, "user not found");
return -ENOENT;
}
// if the user hasn't already been populated...attempt to
if (!populated) {
ret = init(op_state);
if (ret < 0) {
set_err_msg(err_msg, "unable to retrieve user info");
return ret;
}
}
// ensure that we can modify the user's attributes
if (user_id.compare(RGW_USER_ANON_ID) == 0) {
set_err_msg(err_msg, "unable to modify anonymous user's info");
return -EACCES;
}
user_info = old_info;
std::string old_email = old_info.user_email;
if (!op_email.empty()) {
// make sure we are not adding a duplicate email
if (old_email.compare(op_email) != 0) {
ret = rgw_get_user_info_by_email(store, op_email, duplicate_check);
if (ret >= 0 && duplicate_check.user_id.compare(user_id) != 0) {
set_err_msg(err_msg, "cannot add duplicate email");
return -ERR_EMAIL_EXIST;
}
}
user_info.user_email = op_email;
} else if (op_email.empty() && op_state.user_email_specified) {
ldout(store->ctx(), 10) << "removing email index: " << user_info.user_email << dendl;
ret = rgw_remove_email_index(store, user_info.user_email);
if (ret < 0 && ret != -ENOENT) {
ldout(store->ctx(), 0) << "ERROR: could not remove " << user_info.user_id << " index (err=" << ret << ")" << dendl;
return ret;
}
user_info.user_email = "";
}
// update the remaining user info
if (!display_name.empty())
user_info.display_name = display_name;
if (op_state.max_buckets_specified)
user_info.max_buckets = op_state.get_max_buckets();
if (op_state.admin_specified)
user_info.admin = op_state.admin;
if (op_state.system_specified)
user_info.system = op_state.system;
if (op_state.temp_url_key_specified) {
map<int, string>::iterator iter;
for (iter = op_state.temp_url_keys.begin();
iter != op_state.temp_url_keys.end(); ++iter) {
user_info.temp_url_keys[iter->first] = iter->second;
}
}
if (op_state.op_mask_specified)
user_info.op_mask = op_state.get_op_mask();
if (op_state.has_bucket_quota())
user_info.bucket_quota = op_state.get_bucket_quota();
if (op_state.has_user_quota())
user_info.user_quota = op_state.get_user_quota();
if (op_state.has_suspension_op()) {
__u8 suspended = op_state.get_suspension_status();
user_info.suspended = suspended;
RGWUserBuckets buckets;
if (user_id.empty()) {
set_err_msg(err_msg, "empty user id passed...aborting");
return -EINVAL;
}
bool is_truncated = false;
string marker;
CephContext *cct = store->ctx();
size_t max_buckets = cct->_conf->rgw_list_buckets_max_chunk;
do {
ret = rgw_read_user_buckets(store, user_id, buckets, marker, string(),
max_buckets, false, &is_truncated);
if (ret < 0) {
set_err_msg(err_msg, "could not get buckets for uid: " + user_id.to_str());
return ret;
}
map<string, RGWBucketEnt>& m = buckets.get_buckets();
map<string, RGWBucketEnt>::iterator iter;
vector<rgw_bucket> bucket_names;
for (iter = m.begin(); iter != m.end(); ++iter) {
RGWBucketEnt obj = iter->second;
bucket_names.push_back(obj.bucket);
marker = iter->first;
}
ret = store->set_buckets_enabled(bucket_names, !suspended);
if (ret < 0) {
set_err_msg(err_msg, "failed to modify bucket");
return ret;
}
} while (is_truncated);
}
if (op_state.mfa_ids_specified) {
user_info.mfa_ids = op_state.mfa_ids;
}
op_state.set_user_info(user_info);
// if we're supposed to modify keys, do so
if (op_state.has_key_op()) {
ret = keys.add(op_state, &subprocess_msg, true);
if (ret < 0) {
set_err_msg(err_msg, "unable to create or modify keys, " + subprocess_msg);
return ret;
}
}
ret = update(op_state, err_msg);
if (ret < 0)
return ret;
return 0;
}
int RGWUser::modify(RGWUserAdminOpState& op_state, std::string *err_msg)
{
std::string subprocess_msg;
int ret;
ret = check_op(op_state, &subprocess_msg);
if (ret < 0) {
if (is_populated() && (user_id.compare(op_state.get_user_id()) != 0)) {
set_err_msg(err_msg, "unable to create user " + user_id.to_str()
+ " because user id " + op_state.get_user_id().to_str()
+ " already exists with email "
+ op_state.get_user_email());
} else {
set_err_msg(err_msg, "unable to parse parameters, " + subprocess_msg);
}
return ret;
}
ret = execute_modify(op_state, &subprocess_msg);
if (ret < 0) {
set_err_msg(err_msg, "unable to modify user, " + subprocess_msg);
return ret;
}
return 0;
}
int RGWUser::info(RGWUserAdminOpState& op_state, RGWUserInfo& fetched_info, std::string *err_msg)
{
int ret = init(op_state);
if (ret < 0) {
set_err_msg(err_msg, "unable to fetch user info");
return ret;
}
fetched_info = op_state.get_user_info();
return 0;
}
int RGWUser::info(RGWUserInfo& fetched_info, std::string *err_msg)
{
if (!is_populated()) {
set_err_msg(err_msg, "no user info saved");
return -EINVAL;
}
fetched_info = old_info;
return 0;
}
int RGWUser::list(RGWUserAdminOpState& op_state, RGWFormatterFlusher& flusher)
{
Formatter *formatter = flusher.get_formatter();
void *handle = nullptr;
std::string metadata_key = "user";
if (op_state.max_entries > 1000) {
op_state.max_entries = 1000;
}
int ret = store->meta_mgr->list_keys_init(metadata_key, op_state.marker, &handle);
if (ret < 0) {
return ret;
}
bool truncated = false;
uint64_t count = 0;
uint64_t left = 0;
flusher.start(0);
// open the result object section
formatter->open_object_section("result");
// open the user id list array section
formatter->open_array_section("keys");
do {
std::list<std::string> keys;
left = op_state.max_entries - count;
ret = store->meta_mgr->list_keys_next(handle, left, keys, &truncated);
if (ret < 0 && ret != -ENOENT) {
return ret;
} if (ret != -ENOENT) {
for (std::list<std::string>::iterator iter = keys.begin(); iter != keys.end(); ++iter) {
formatter->dump_string("key", *iter);
++count;
}
}
} while (truncated && left > 0);
// close user id list section
formatter->close_section();
formatter->dump_bool("truncated", truncated);
formatter->dump_int("count", count);
if (truncated) {
formatter->dump_string("marker", store->meta_mgr->get_marker(handle));
}
// close result object section
formatter->close_section();
store->meta_mgr->list_keys_complete(handle);
flusher.flush();
return 0;
}
int RGWUserAdminOp_User::list(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUser user;
int ret = user.init_storage(store);
if (ret < 0)
return ret;
ret = user.list(op_state, flusher);
if (ret < 0)
return ret;
return 0;
}
int RGWUserAdminOp_User::info(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
if (!op_state.has_existing_user())
return -ERR_NO_SUCH_USER;
Formatter *formatter = flusher.get_formatter();
ret = user.info(info, NULL);
if (ret < 0)
return ret;
if (op_state.sync_stats) {
ret = rgw_user_sync_all_stats(store, info.user_id);
if (ret < 0) {
return ret;
}
}
RGWStorageStats stats;
RGWStorageStats *arg_stats = NULL;
if (op_state.fetch_stats) {
int ret = store->get_user_stats(info.user_id, stats);
if (ret < 0 && ret != -ENOENT) {
return ret;
}
arg_stats = &stats;
}
if (formatter) {
flusher.start(0);
dump_user_info(formatter, info, arg_stats);
flusher.flush();
}
return 0;
}
int RGWUserAdminOp_User::create(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
Formatter *formatter = flusher.get_formatter();
ret = user.add(op_state, NULL);
if (ret < 0) {
if (ret == -EEXIST)
ret = -ERR_USER_EXIST;
return ret;
}
ret = user.info(info, NULL);
if (ret < 0)
return ret;
if (formatter) {
flusher.start(0);
dump_user_info(formatter, info);
flusher.flush();
}
return 0;
}
int RGWUserAdminOp_User::modify(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
Formatter *formatter = flusher.get_formatter();
ret = user.modify(op_state, NULL);
if (ret < 0) {
if (ret == -ENOENT)
ret = -ERR_NO_SUCH_USER;
return ret;
}
ret = user.info(info, NULL);
if (ret < 0)
return ret;
if (formatter) {
flusher.start(0);
dump_user_info(formatter, info);
flusher.flush();
}
return 0;
}
int RGWUserAdminOp_User::remove(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
ret = user.remove(op_state, NULL);
if (ret == -ENOENT)
ret = -ERR_NO_SUCH_USER;
return ret;
}
int RGWUserAdminOp_Subuser::create(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
if (!op_state.has_existing_user())
return -ERR_NO_SUCH_USER;
Formatter *formatter = flusher.get_formatter();
ret = user.subusers.add(op_state, NULL);
if (ret < 0)
return ret;
ret = user.info(info, NULL);
if (ret < 0)
return ret;
if (formatter) {
flusher.start(0);
dump_subusers_info(formatter, info);
flusher.flush();
}
return 0;
}
int RGWUserAdminOp_Subuser::modify(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
if (!op_state.has_existing_user())
return -ERR_NO_SUCH_USER;
Formatter *formatter = flusher.get_formatter();
ret = user.subusers.modify(op_state, NULL);
if (ret < 0)
return ret;
ret = user.info(info, NULL);
if (ret < 0)
return ret;
if (formatter) {
flusher.start(0);
dump_subusers_info(formatter, info);
flusher.flush();
}
return 0;
}
int RGWUserAdminOp_Subuser::remove(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
if (!op_state.has_existing_user())
return -ERR_NO_SUCH_USER;
ret = user.subusers.remove(op_state, NULL);
if (ret < 0)
return ret;
return 0;
}
int RGWUserAdminOp_Key::create(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
if (!op_state.has_existing_user())
return -ERR_NO_SUCH_USER;
Formatter *formatter = flusher.get_formatter();
ret = user.keys.add(op_state, NULL);
if (ret < 0)
return ret;
ret = user.info(info, NULL);
if (ret < 0)
return ret;
if (formatter) {
flusher.start(0);
int key_type = op_state.get_key_type();
if (key_type == KEY_TYPE_SWIFT)
dump_swift_keys_info(formatter, info);
else if (key_type == KEY_TYPE_S3)
dump_access_keys_info(formatter, info);
flusher.flush();
}
return 0;
}
int RGWUserAdminOp_Key::remove(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
if (!op_state.has_existing_user())
return -ERR_NO_SUCH_USER;
ret = user.keys.remove(op_state, NULL);
if (ret < 0)
return ret;
return 0;
}
int RGWUserAdminOp_Caps::add(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
if (!op_state.has_existing_user())
return -ERR_NO_SUCH_USER;
Formatter *formatter = flusher.get_formatter();
ret = user.caps.add(op_state, NULL);
if (ret < 0)
return ret;
ret = user.info(info, NULL);
if (ret < 0)
return ret;
if (formatter) {
flusher.start(0);
info.caps.dump(formatter);
flusher.flush();
}
return 0;
}
int RGWUserAdminOp_Caps::remove(RGWRados *store, RGWUserAdminOpState& op_state,
RGWFormatterFlusher& flusher)
{
RGWUserInfo info;
RGWUser user;
int ret = user.init(store, op_state);
if (ret < 0)
return ret;
if (!op_state.has_existing_user())
return -ERR_NO_SUCH_USER;
Formatter *formatter = flusher.get_formatter();
ret = user.caps.remove(op_state, NULL);
if (ret < 0)
return ret;
ret = user.info(info, NULL);
if (ret < 0)
return ret;
if (formatter) {
flusher.start(0);
info.caps.dump(formatter);
flusher.flush();
}
return 0;
}
struct RGWUserCompleteInfo {
RGWUserInfo info;
map<string, bufferlist> attrs;
bool has_attrs;
RGWUserCompleteInfo()
: has_attrs(false)
{}
void dump(Formatter * const f) const {
info.dump(f);
encode_json("attrs", attrs, f);
}
void decode_json(JSONObj *obj) {
decode_json_obj(info, obj);
has_attrs = JSONDecoder::decode_json("attrs", attrs, obj);
}
};
class RGWUserMetadataObject : public RGWMetadataObject {
RGWUserCompleteInfo uci;
public:
RGWUserMetadataObject(const RGWUserCompleteInfo& _uci, obj_version& v, real_time m)
: uci(_uci) {
objv = v;
mtime = m;
}
void dump(Formatter *f) const override {
uci.dump(f);
}
};
class RGWUserMetadataHandler : public RGWMetadataHandler {
public:
string get_type() override { return "user"; }
int get(RGWRados *store, string& entry, RGWMetadataObject **obj) override {
RGWUserCompleteInfo uci;
RGWObjVersionTracker objv_tracker;
real_time mtime;
rgw_user uid(entry);
int ret = rgw_get_user_info_by_uid(store, uid, uci.info, &objv_tracker,
&mtime, NULL, &uci.attrs);
if (ret < 0) {
return ret;
}
RGWUserMetadataObject *mdo = new RGWUserMetadataObject(uci, objv_tracker.read_version, mtime);
*obj = mdo;
return 0;
}
int put(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker,
real_time mtime, JSONObj *obj, sync_type_t sync_mode) override {
RGWUserCompleteInfo uci;
try {
decode_json_obj(uci, obj);
} catch (JSONDecoder::err& e) {
return -EINVAL;
}
map<string, bufferlist> *pattrs = NULL;
if (uci.has_attrs) {
pattrs = &uci.attrs;
}
rgw_user uid(entry);
RGWUserInfo old_info;
real_time orig_mtime;
int ret = rgw_get_user_info_by_uid(store, uid, old_info, &objv_tracker, &orig_mtime);
if (ret < 0 && ret != -ENOENT)
return ret;
// are we actually going to perform this put, or is it too old?
if (ret != -ENOENT &&
!check_versions(objv_tracker.read_version, orig_mtime,
objv_tracker.write_version, mtime, sync_mode)) {
return STATUS_NO_APPLY;
}
ret = rgw_store_user_info(store, uci.info, &old_info, &objv_tracker, mtime, false, pattrs);
if (ret < 0) {
return ret;
}
return STATUS_APPLIED;
}
struct list_keys_info {
RGWRados *store;
RGWListRawObjsCtx ctx;
};
int remove(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker) override {
RGWUserInfo info;
rgw_user uid(entry);
int ret = rgw_get_user_info_by_uid(store, uid, info, &objv_tracker);
if (ret < 0)
return ret;
return rgw_delete_user(store, info, objv_tracker);
}
void get_pool_and_oid(RGWRados *store, const string& key, rgw_pool& pool, string& oid) override {
oid = key;
pool = store->svc.zone->get_zone_params().user_uid_pool;
}
int list_keys_init(RGWRados *store, const string& marker, void **phandle) override
{
auto info = std::make_unique<list_keys_info>();
info->store = store;
int ret = store->list_raw_objects_init(store->svc.zone->get_zone_params().user_uid_pool, marker,
&info->ctx);
if (ret < 0) {
return ret;
}
*phandle = (void *)info.release();
return 0;
}
int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated) override {
list_keys_info *info = static_cast<list_keys_info *>(handle);
string no_filter;
keys.clear();
RGWRados *store = info->store;
list<string> unfiltered_keys;
int ret = store->list_raw_objects_next(no_filter, max, info->ctx,
unfiltered_keys, truncated);
if (ret < 0 && ret != -ENOENT)
return ret;
if (ret == -ENOENT) {
if (truncated)
*truncated = false;
return 0;
}
// now filter out the buckets entries
list<string>::iterator iter;
for (iter = unfiltered_keys.begin(); iter != unfiltered_keys.end(); ++iter) {
string& k = *iter;
if (k.find(".buckets") == string::npos) {
keys.push_back(k);
}
}
return 0;
}
void list_keys_complete(void *handle) override {
list_keys_info *info = static_cast<list_keys_info *>(handle);
delete info;
}
string get_marker(void *handle) override {
list_keys_info *info = static_cast<list_keys_info *>(handle);
return info->store->list_raw_objs_get_cursor(info->ctx);
}
};
void rgw_user_init(RGWRados *store)
{
uinfo_cache.init(store->svc.cache);
user_meta_handler = new RGWUserMetadataHandler;
store->meta_mgr->register_handler(user_meta_handler);
}
|