Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / radosacl.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 <stdlib.h>
16 #include <time.h>
17 #include <errno.h>
18
19 #include "include/types.h"
20 #include "include/rados/librados.hpp"
21
22 using namespace librados;
23
24 void buf_to_hex(const unsigned char *buf, int len, char *str)
25 {
26   str[0] = '\0';
27   for (int i = 0; i < len; i++) {
28     sprintf(&str[i*2], "%02x", (int)buf[i]);
29   }
30 }
31
32
33 #define ID_SIZE 8
34
35 #define ACL_RD  0x1
36 #define ACL_WR  0x2
37
38 struct ACLID {
39   char id[ID_SIZE + 1];
40
41   void encode(bufferlist& bl) const {
42     bl.append((const char *)id, ID_SIZE);
43   }
44   void decode(bufferlist::iterator& iter) {
45     iter.copy(ID_SIZE, (char *)id);
46   }
47 };
48 WRITE_CLASS_ENCODER(ACLID)
49
50 typedef __u32 ACLFlags;
51
52
53 inline bool operator<(const ACLID& l, const ACLID& r)
54 {
55   return (memcmp(&l, &r, ID_SIZE) < 0);
56 }
57
58 struct ACLPair {
59   ACLID id;
60   ACLFlags flags;
61 };
62
63 class ObjectACLs {
64   map<ACLID, ACLFlags> acls_map;
65
66 public:
67
68   void encode(bufferlist& bl) const {
69     ::encode(acls_map, bl);
70   }
71   void decode(bufferlist::iterator& bl) {
72     ::decode(acls_map, bl);
73   }
74
75   int read_acl(ACLID& id, ACLFlags *flags);
76   void set_acl(ACLID& id, ACLFlags flags);
77 };
78 WRITE_CLASS_ENCODER(ObjectACLs)
79
80 int ObjectACLs::read_acl(ACLID& id, ACLFlags *flags)
81 {
82   if (!flags)
83     return -EINVAL;
84
85   map<ACLID, ACLFlags>::iterator iter = acls_map.find(id);
86
87   if (iter == acls_map.end())
88     return -ENOENT;
89
90   *flags = iter->second;
91
92   return 0;
93 }
94
95 void ObjectACLs::set_acl(ACLID& id, ACLFlags flags)
96 {
97   acls_map[id] = flags;
98 }
99
100
101
102 class ACLEntity
103 {
104   string name;
105   map<ACLID, ACLEntity> groups;
106 };
107
108 typedef map<ACLID, ACLEntity> tACLIDEntityMap;
109
110 static map<ACLID, ACLEntity> users;
111 static map<ACLID, ACLEntity> groups;
112
113 void get_user(ACLID& aclid, ACLEntity *entity)
114 {
115   //users.find(aclid);
116 }
117
118
119
120
121
122 int main(int argc, const char **argv) 
123 {
124   Rados rados;
125   if (rados.init(NULL) < 0) {
126      cerr << "couldn't initialize rados!" << std::endl;
127      exit(1);
128   }
129   if (rados.conf_read_file(NULL)) {
130      cerr << "couldn't read Ceph configuration file!" << std::endl;
131      exit(1);
132   }
133   if (rados.connect() < 0) {
134      cerr << "couldn't connect to cluster!" << std::endl;
135      exit(1);
136   }
137
138   time_t tm;
139   bufferlist bl, bl2;
140   char buf[128];
141
142   time(&tm);
143   snprintf(buf, 128, "%s", ctime(&tm));
144   bl.append(buf, strlen(buf));
145
146   const char *oid = "bar";
147
148   IoCtx io_ctx;
149   int r = rados.ioctx_create("data", io_ctx);
150   cout << "open io_ctx result = " << r << " pool = " << io_ctx.get_pool_name() << std::endl;
151
152   ACLID id;
153
154   snprintf(id.id, sizeof(id.id), "%.8x", 0x1234);
155   cout << "id=" << id.id << std::endl;
156
157   r = io_ctx.exec(oid, "acl", "get", bl, bl2);
158   cout << "exec(acl get) returned " << r
159        << " len=" << bl2.length() << std::endl;
160   ObjectACLs oa;
161   if (r >= 0) {
162     bufferlist::iterator iter = bl2.begin();
163     oa.decode(iter);
164   }
165
166   oa.set_acl(id, ACL_RD);
167   bl.clear();
168   oa.encode(bl);
169   r = io_ctx.exec(oid, "acl", "set", bl, bl2);
170   cout << "exec(acl set) returned " << r
171        << " len=" << bl2.length() << std::endl;
172
173   const unsigned char *md5 = (const unsigned char *)bl2.c_str();
174   char md5_str[bl2.length()*2 + 1];
175   buf_to_hex(md5, bl2.length(), md5_str);
176   cout << "md5 result=" << md5_str << std::endl;
177
178   int size = io_ctx.read(oid, bl2, 128, 0);
179   cout << "read result=" << bl2.c_str() << std::endl;
180   cout << "size=" << size << std::endl;
181
182   return 0;
183 }
184