]> git.proxmox.com Git - ceph.git/blob - ceph/src/cls/otp/cls_otp_client.cc
import 15.2.0 Octopus source
[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 namespace librados;
21
22 #include "cls/otp/cls_otp_ops.h"
23 #include "cls/otp/cls_otp_client.h"
24
25 #include "common/random_string.h" /* for gen_rand_alphanumeric */
26
27 namespace rados {
28 namespace cls {
29 namespace otp {
30
31 void OTP::create(librados::ObjectWriteOperation *rados_op,
32 const otp_info_t& config) {
33 cls_otp_set_otp_op op;
34 op.entries.push_back(config);
35 bufferlist in;
36 encode(op, in);
37 rados_op->exec("otp", "otp_set", in);
38 }
39
40 void OTP::set(librados::ObjectWriteOperation *rados_op,
41 const list<otp_info_t>& entries) {
42 cls_otp_set_otp_op op;
43 op.entries = entries;
44 bufferlist in;
45 encode(op, in);
46 rados_op->exec("otp", "otp_set", in);
47 }
48
49 void OTP::remove(librados::ObjectWriteOperation *rados_op,
50 const string& id) {
51 cls_otp_remove_otp_op op;
52 op.ids.push_back(id);
53 bufferlist in;
54 encode(op, in);
55 rados_op->exec("otp", "otp_remove", in);
56 }
57
58 int OTP::check(CephContext *cct, librados::IoCtx& ioctx, const string& oid,
59 const string& id, const string& val, otp_check_t *result) {
60 cls_otp_check_otp_op op;
61 op.id = id;
62 op.val = val;
63 #define TOKEN_LEN 16
64 op.token = gen_rand_alphanumeric(cct, TOKEN_LEN);
65
66 bufferlist in;
67 bufferlist out;
68 encode(op, in);
69 int r = ioctx.exec(oid, "otp", "otp_check", in, out);
70 if (r < 0) {
71 return r;
72 }
73
74 cls_otp_get_result_op op2;
75 op2.token = op.token;
76 bufferlist in2;
77 bufferlist out2;
78 encode(op2, in2);
79 r = ioctx.exec(oid, "otp", "otp_get_result", in, out);
80 if (r < 0) {
81 return r;
82 }
83
84 auto iter = out.cbegin();
85 cls_otp_get_result_reply ret;
86 try {
87 decode(ret, iter);
88 } catch (buffer::error& err) {
89 return -EBADMSG;
90 }
91
92 *result = ret.result;
93
94 return 0;
95 }
96
97 int OTP::get(librados::ObjectReadOperation *rop,
98 librados::IoCtx& ioctx, const string& oid,
99 const list<string> *ids, bool get_all, list<otp_info_t> *result) {
100 librados::ObjectReadOperation _rop;
101 if (!rop) {
102 rop = &_rop;
103 }
104 cls_otp_get_otp_op op;
105 if (ids) {
106 op.ids = *ids;
107 }
108 op.get_all = get_all;
109 bufferlist in;
110 bufferlist out;
111 int op_ret;
112 encode(op, in);
113 rop->exec("otp", "otp_get", in, &out, &op_ret);
114 int r = ioctx.operate(oid, rop, nullptr);
115 if (r < 0) {
116 return r;
117 }
118 if (op_ret < 0) {
119 return op_ret;
120 }
121
122 cls_otp_get_otp_reply ret;
123 auto iter = out.cbegin();
124 try {
125 decode(ret, iter);
126 } catch (buffer::error& err) {
127 return -EBADMSG;
128 }
129
130 *result = ret.found_entries;;
131
132 return 0;
133 }
134
135 int OTP::get(librados::ObjectReadOperation *op,
136 librados::IoCtx& ioctx, const string& oid,
137 const string& id, otp_info_t *result) {
138 list<string> ids{ id };
139 list<otp_info_t> ret;
140
141 int r = get(op, ioctx, oid, &ids, false, &ret);
142 if (r < 0) {
143 return r;
144 }
145 if (ret.empty()) {
146 return -ENOENT;
147 }
148 *result = ret.front();
149
150 return 0;
151 }
152
153 int OTP::get_all(librados::ObjectReadOperation *op, librados::IoCtx& ioctx, const string& oid,
154 list<otp_info_t> *result) {
155 return get(op, ioctx, oid, nullptr, true, result);
156 }
157
158 int OTP::get_current_time(librados::IoCtx& ioctx, const string& oid,
159 ceph::real_time *result) {
160 cls_otp_get_current_time_op op;
161 bufferlist in;
162 bufferlist out;
163 int op_ret;
164 encode(op, in);
165 ObjectReadOperation rop;
166 rop.exec("otp", "get_current_time", in, &out, &op_ret);
167 int r = ioctx.operate(oid, &rop, nullptr);
168 if (r < 0) {
169 return r;
170 }
171 if (op_ret < 0) {
172 return op_ret;
173 }
174
175 cls_otp_get_current_time_reply ret;
176 auto iter = out.cbegin();
177 try {
178 decode(ret, iter);
179 } catch (buffer::error& err) {
180 return -EBADMSG;
181 }
182
183 *result = ret.time;
184
185 return 0;
186 }
187 } // namespace otp
188 } // namespace cls
189 } // namespace rados
190