summaryrefslogtreecommitdiffstats
path: root/src/cls/otp/cls_otp_client.cc
blob: 0ba55571f8d1dae9dd2fdd8687edfcd8973e4df6 (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#include "include/types.h"
#include "msg/msg_types.h"
#include "include/rados/librados.hpp"
#include "include/utime.h"

using std::list;
using std::string;
using namespace librados;

#include "cls/otp/cls_otp_ops.h"
#include "cls/otp/cls_otp_client.h"

#include "common/random_string.h" /* for gen_rand_alphanumeric */

namespace rados {
  namespace cls {
    namespace otp {

      void OTP::create(librados::ObjectWriteOperation *rados_op,
                       const otp_info_t& config) {
        cls_otp_set_otp_op op;
        op.entries.push_back(config);
        bufferlist in;
        encode(op, in);
        rados_op->exec("otp", "otp_set", in);
      }

      void OTP::set(librados::ObjectWriteOperation *rados_op,
                       const list<otp_info_t>& entries) {
        cls_otp_set_otp_op op;
        op.entries = entries;
        bufferlist in;
        encode(op, in);
        rados_op->exec("otp", "otp_set", in);
      }

      void OTP::remove(librados::ObjectWriteOperation *rados_op,
                       const string& id) {
        cls_otp_remove_otp_op op;
        op.ids.push_back(id);
        bufferlist in;
        encode(op, in);
        rados_op->exec("otp", "otp_remove", in);
      }

      int OTP::check(CephContext *cct, librados::IoCtx& ioctx, const string& oid,
                     const string& id, const string& val, otp_check_t *result) {
        cls_otp_check_otp_op op;
        op.id = id;
        op.val = val;
#define TOKEN_LEN 16
        op.token = gen_rand_alphanumeric(cct, TOKEN_LEN);
        
        bufferlist in;
        bufferlist out;
        encode(op, in);
        int r = ioctx.exec(oid, "otp", "otp_check", in, out);
        if (r < 0) {
          return r;
        }

        cls_otp_get_result_op op2;
        op2.token = op.token;
        bufferlist in2;
        bufferlist out2;
        encode(op2, in2);
        r = ioctx.exec(oid, "otp", "otp_get_result", in, out);
        if (r < 0) {
          return r;
        }

        auto iter = out.cbegin();
        cls_otp_get_result_reply ret;
        try {
          decode(ret, iter);
        } catch (ceph::buffer::error& err) {
	  return -EBADMSG;
        }

        *result = ret.result;

        return 0;
      }

      int OTP::get(librados::ObjectReadOperation *rop,
                   librados::IoCtx& ioctx, const string& oid,
                   const list<string> *ids, bool get_all, list<otp_info_t> *result) {
        librados::ObjectReadOperation _rop;
        if (!rop) {
          rop = &_rop;
        }
        cls_otp_get_otp_op op;
        if (ids) {
          op.ids = *ids;
        }
        op.get_all = get_all;
        bufferlist in;
        bufferlist out;
        int op_ret;
        encode(op, in);
        rop->exec("otp", "otp_get", in, &out, &op_ret);
        int r = ioctx.operate(oid, rop, nullptr);
        if (r < 0) {
          return r;
        }
        if (op_ret < 0) {
          return op_ret;
        }

        cls_otp_get_otp_reply ret;
        auto iter = out.cbegin();
        try {
          decode(ret, iter);
        } catch (ceph::buffer::error& err) {
	  return -EBADMSG;
        }

        *result = ret.found_entries;;

        return 0;
      }

      int OTP::get(librados::ObjectReadOperation *op,
                   librados::IoCtx& ioctx, const string& oid,
                    const string& id, otp_info_t *result) {
        list<string> ids{ id };
        list<otp_info_t> ret;

        int r = get(op, ioctx, oid, &ids, false, &ret);
        if (r < 0) {
          return r;
        }
        if (ret.empty()) {
          return -ENOENT;
        }
        *result = ret.front();

        return 0;
      }

      int OTP::get_all(librados::ObjectReadOperation *op, librados::IoCtx& ioctx, const string& oid,
                       list<otp_info_t> *result) {
        return get(op, ioctx, oid, nullptr, true, result);
      }

      int OTP::get_current_time(librados::IoCtx& ioctx, const string& oid,
                                ceph::real_time *result) {
        cls_otp_get_current_time_op op;
        bufferlist in;
        bufferlist out;
        int op_ret;
        encode(op, in);
        ObjectReadOperation rop;
        rop.exec("otp", "get_current_time", in, &out, &op_ret);
        int r = ioctx.operate(oid, &rop, nullptr);
        if (r < 0) {
          return r;
        }
        if (op_ret < 0) {
          return op_ret;
        }

        cls_otp_get_current_time_reply ret;
        auto iter = out.cbegin();
        try {
          decode(ret, iter);
        } catch (ceph::buffer::error& err) {
	  return -EBADMSG;
        }

        *result = ret.time;

        return 0;
      }
    } // namespace otp
  } // namespace cls
} // namespace rados