summaryrefslogtreecommitdiffstats
path: root/src/tools/radosacl.cc
blob: 3b071705b018c96ffb8ab0a3d3383f1a58bf94b3 (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
// -*- 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 <stdlib.h>
#include <time.h>
#include <errno.h>

#include "include/types.h"
#include "include/rados/librados.hpp"

using namespace librados;

void buf_to_hex(const unsigned char *buf, int len, char *str)
{
  str[0] = '\0';
  for (int i = 0; i < len; i++) {
    sprintf(&str[i*2], "%02x", (int)buf[i]);
  }
}


#define ID_SIZE 8

#define ACL_RD	0x1
#define ACL_WR	0x2

struct ACLID {
  char id[ID_SIZE + 1];

  void encode(bufferlist& bl) const {
    bl.append((const char *)id, ID_SIZE);
  }
  void decode(bufferlist::const_iterator& iter) {
    iter.copy(ID_SIZE, (char *)id);
  }
};
WRITE_CLASS_ENCODER(ACLID)

typedef __u32 ACLFlags;


inline bool operator<(const ACLID& l, const ACLID& r)
{
  return (memcmp(&l, &r, ID_SIZE) < 0);
}

struct ACLPair {
  ACLID id;
  ACLFlags flags;
};

class ObjectACLs {
  map<ACLID, ACLFlags> acls_map;

public:

  void encode(bufferlist& bl) const {
    using ceph::encode;
    encode(acls_map, bl);
  }
  void decode(bufferlist::const_iterator& bl) {
    using ceph::decode;
    decode(acls_map, bl);
  }

  int read_acl(ACLID& id, ACLFlags *flags);
  void set_acl(ACLID& id, ACLFlags flags);
};
WRITE_CLASS_ENCODER(ObjectACLs)

int ObjectACLs::read_acl(ACLID& id, ACLFlags *flags)
{
  if (!flags)
    return -EINVAL;

  map<ACLID, ACLFlags>::iterator iter = acls_map.find(id);

  if (iter == acls_map.end())
    return -ENOENT;

  *flags = iter->second;

  return 0;
}

void ObjectACLs::set_acl(ACLID& id, ACLFlags flags)
{
  acls_map[id] = flags;
}



class ACLEntity
{
  string name;
  map<ACLID, ACLEntity> groups;
};

typedef map<ACLID, ACLEntity> tACLIDEntityMap;

static map<ACLID, ACLEntity> users;
static map<ACLID, ACLEntity> groups;

void get_user(ACLID& aclid, ACLEntity *entity)
{
  //users.find(aclid);
}





int main(int argc, const char **argv) 
{
  Rados rados;
  if (rados.init(NULL) < 0) {
     cerr << "couldn't initialize rados!" << std::endl;
     exit(1);
  }
  if (rados.conf_read_file(NULL)) {
     cerr << "couldn't read Ceph configuration file!" << std::endl;
     exit(1);
  }
  if (rados.connect() < 0) {
     cerr << "couldn't connect to cluster!" << std::endl;
     exit(1);
  }

  time_t tm;
  bufferlist bl, bl2;
  char buf[128];

  time(&tm);
  snprintf(buf, 128, "%s", ctime(&tm));
  bl.append(buf, strlen(buf));

  const char *oid = "bar";

  IoCtx io_ctx;
  int r = rados.ioctx_create("data", io_ctx);
  cout << "open io_ctx result = " << r << " pool = " << io_ctx.get_pool_name() << std::endl;

  ACLID id;

  snprintf(id.id, sizeof(id.id), "%.8x", 0x1234);
  cout << "id=" << id.id << std::endl;

  r = io_ctx.exec(oid, "acl", "get", bl, bl2);
  cout << "exec(acl get) returned " << r
       << " len=" << bl2.length() << std::endl;
  ObjectACLs oa;
  if (r >= 0) {
    auto iter = bl2.cbegin();
    oa.decode(iter);
  }

  oa.set_acl(id, ACL_RD);
  bl.clear();
  oa.encode(bl);
  r = io_ctx.exec(oid, "acl", "set", bl, bl2);
  cout << "exec(acl set) returned " << r
       << " len=" << bl2.length() << std::endl;

  const unsigned char *md5 = (const unsigned char *)bl2.c_str();
  char md5_str[bl2.length()*2 + 1];
  buf_to_hex(md5, bl2.length(), md5_str);
  cout << "md5 result=" << md5_str << std::endl;

  int size = io_ctx.read(oid, bl2, 128, 0);
  cout << "read result=" << bl2.c_str() << std::endl;
  cout << "size=" << size << std::endl;

  return 0;
}