]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
19 #include "include/types.h"
20 #include "include/rados/librados.hpp"
23 using namespace librados
;
25 void buf_to_hex(const unsigned char *buf
, int len
, char *str
)
28 for (int i
= 0; i
< len
; i
++) {
29 sprintf(&str
[i
*2], "%02x", (int)buf
[i
]);
42 void encode(bufferlist
& bl
) const {
43 bl
.append((const char *)id
, ID_SIZE
);
45 void decode(bufferlist::const_iterator
& iter
) {
46 iter
.copy(ID_SIZE
, (char *)id
);
49 WRITE_CLASS_ENCODER(ACLID
)
51 typedef __u32 ACLFlags
;
54 inline bool operator<(const ACLID
& l
, const ACLID
& r
)
56 return (memcmp(&l
, &r
, ID_SIZE
) < 0);
65 map
<ACLID
, ACLFlags
> acls_map
;
69 void encode(bufferlist
& bl
) const {
73 void decode(bufferlist::const_iterator
& bl
) {
78 int read_acl(ACLID
& id
, ACLFlags
*flags
);
79 void set_acl(ACLID
& id
, ACLFlags flags
);
81 WRITE_CLASS_ENCODER(ObjectACLs
)
83 int ObjectACLs::read_acl(ACLID
& id
, ACLFlags
*flags
)
88 map
<ACLID
, ACLFlags
>::iterator iter
= acls_map
.find(id
);
90 if (iter
== acls_map
.end())
93 *flags
= iter
->second
;
98 void ObjectACLs::set_acl(ACLID
& id
, ACLFlags flags
)
100 acls_map
[id
] = flags
;
108 map
<ACLID
, ACLEntity
> groups
;
111 typedef map
<ACLID
, ACLEntity
> tACLIDEntityMap
;
113 static map
<ACLID
, ACLEntity
> users
;
114 static map
<ACLID
, ACLEntity
> groups
;
116 void get_user(ACLID
& aclid
, ACLEntity
*entity
)
125 int main(int argc
, const char **argv
)
128 if (rados
.init(NULL
) < 0) {
129 cerr
<< "couldn't initialize rados!" << std::endl
;
132 if (rados
.conf_read_file(NULL
)) {
133 cerr
<< "couldn't read Ceph configuration file!" << std::endl
;
136 if (rados
.connect() < 0) {
137 cerr
<< "couldn't connect to cluster!" << std::endl
;
146 snprintf(buf
, 128, "%s", ctime(&tm
));
147 bl
.append(buf
, strlen(buf
));
149 const char *oid
= "bar";
152 int r
= rados
.ioctx_create("data", io_ctx
);
153 cout
<< "open io_ctx result = " << r
<< " pool = " << io_ctx
.get_pool_name() << std::endl
;
157 snprintf(id
.id
, sizeof(id
.id
), "%.8x", 0x1234);
158 cout
<< "id=" << id
.id
<< std::endl
;
160 r
= io_ctx
.exec(oid
, "acl", "get", bl
, bl2
);
161 cout
<< "exec(acl get) returned " << r
162 << " len=" << bl2
.length() << std::endl
;
165 auto iter
= bl2
.cbegin();
169 oa
.set_acl(id
, ACL_RD
);
172 r
= io_ctx
.exec(oid
, "acl", "set", bl
, bl2
);
173 cout
<< "exec(acl set) returned " << r
174 << " len=" << bl2
.length() << std::endl
;
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
;
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
;