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