]> git.proxmox.com Git - ceph.git/blob - ceph/src/cls/otp/cls_otp_client.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / cls / otp / cls_otp_client.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include "include/types.h"
16 #include "msg/msg_types.h"
17 #include "include/rados/librados.hpp"
18 #include "include/utime.h"
19
20 using std::list;
21 using std::string;
22 using namespace librados;
23
24 #include "cls/otp/cls_otp_ops.h"
25 #include "cls/otp/cls_otp_client.h"
26
27 #include "common/random_string.h" /* for gen_rand_alphanumeric */
28
29 namespace rados {
30 namespace cls {
31 namespace otp {
32
33 void OTP::create(librados::ObjectWriteOperation *rados_op,
34 const otp_info_t& config) {
35 cls_otp_set_otp_op op;
36 op.entries.push_back(config);
37 bufferlist in;
38 encode(op, in);
39 rados_op->exec("otp", "otp_set", in);
40 }
41
42 void OTP::set(librados::ObjectWriteOperation *rados_op,
43 const list<otp_info_t>& entries) {
44 cls_otp_set_otp_op op;
45 op.entries = entries;
46 bufferlist in;
47 encode(op, in);
48 rados_op->exec("otp", "otp_set", in);
49 }
50
51 void OTP::remove(librados::ObjectWriteOperation *rados_op,
52 const string& id) {
53 cls_otp_remove_otp_op op;
54 op.ids.push_back(id);
55 bufferlist in;
56 encode(op, in);
57 rados_op->exec("otp", "otp_remove", in);
58 }
59
60 int OTP::check(CephContext *cct, librados::IoCtx& ioctx, const string& oid,
61 const string& id, const string& val, otp_check_t *result) {
62 cls_otp_check_otp_op op;
63 op.id = id;
64 op.val = val;
65 #define TOKEN_LEN 16
66 op.token = gen_rand_alphanumeric(cct, TOKEN_LEN);
67
68 bufferlist in;
69 bufferlist out;
70 encode(op, in);
71 int r = ioctx.exec(oid, "otp", "otp_check", in, out);
72 if (r < 0) {
73 return r;
74 }
75
76 cls_otp_get_result_op op2;
77 op2.token = op.token;
78 bufferlist in2;
79 bufferlist out2;
80 encode(op2, in2);
81 r = ioctx.exec(oid, "otp", "otp_get_result", in, out);
82 if (r < 0) {
83 return r;
84 }
85
86 auto iter = out.cbegin();
87 cls_otp_get_result_reply ret;
88 try {
89 decode(ret, iter);
90 } catch (ceph::buffer::error& err) {
91 return -EBADMSG;
92 }
93
94 *result = ret.result;
95
96 return 0;
97 }
98
99 int OTP::get(librados::ObjectReadOperation *rop,
100 librados::IoCtx& ioctx, const string& oid,
101 const list<string> *ids, bool get_all, list<otp_info_t> *result) {
102 librados::ObjectReadOperation _rop;
103 if (!rop) {
104 rop = &_rop;
105 }
106 cls_otp_get_otp_op op;
107 if (ids) {
108 op.ids = *ids;
109 }
110 op.get_all = get_all;
111 bufferlist in;
112 bufferlist out;
113 int op_ret;
114 encode(op, in);
115 rop->exec("otp", "otp_get", in, &out, &op_ret);
116 int r = ioctx.operate(oid, rop, nullptr);
117 if (r < 0) {
118 return r;
119 }
120 if (op_ret < 0) {
121 return op_ret;
122 }
123
124 cls_otp_get_otp_reply ret;
125 auto iter = out.cbegin();
126 try {
127 decode(ret, iter);
128 } catch (ceph::buffer::error& err) {
129 return -EBADMSG;
130 }
131
132 *result = ret.found_entries;;
133
134 return 0;
135 }
136
137 int OTP::get(librados::ObjectReadOperation *op,
138 librados::IoCtx& ioctx, const string& oid,
139 const string& id, otp_info_t *result) {
140 list<string> ids{ id };
141 list<otp_info_t> ret;
142
143 int r = get(op, ioctx, oid, &ids, false, &ret);
144 if (r < 0) {
145 return r;
146 }
147 if (ret.empty()) {
148 return -ENOENT;
149 }
150 *result = ret.front();
151
152 return 0;
153 }
154
155 int OTP::get_all(librados::ObjectReadOperation *op, librados::IoCtx& ioctx, const string& oid,
156 list<otp_info_t> *result) {
157 return get(op, ioctx, oid, nullptr, true, result);
158 }
159
160 int OTP::get_current_time(librados::IoCtx& ioctx, const string& oid,
161 ceph::real_time *result) {
162 cls_otp_get_current_time_op op;
163 bufferlist in;
164 bufferlist out;
165 int op_ret;
166 encode(op, in);
167 ObjectReadOperation rop;
168 rop.exec("otp", "get_current_time", in, &out, &op_ret);
169 int r = ioctx.operate(oid, &rop, nullptr);
170 if (r < 0) {
171 return r;
172 }
173 if (op_ret < 0) {
174 return op_ret;
175 }
176
177 cls_otp_get_current_time_reply ret;
178 auto iter = out.cbegin();
179 try {
180 decode(ret, iter);
181 } catch (ceph::buffer::error& err) {
182 return -EBADMSG;
183 }
184
185 *result = ret.time;
186
187 return 0;
188 }
189 } // namespace otp
190 } // namespace cls
191 } // namespace rados