]>
git.proxmox.com Git - ceph.git/blob - ceph/src/auth/Auth.h
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-2009 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.
15 #ifndef CEPH_AUTHTYPES_H
16 #define CEPH_AUTHTYPES_H
19 #include "common/entity_name.h"
21 // The _MAX values are a bit wonky here because we are overloading the first
22 // byte of the auth payload to identify both the type of authentication to be
23 // used *and* the encoding version for the authenticator. So, we define a
27 AUTH_MODE_AUTHORIZER
= 1,
28 AUTH_MODE_AUTHORIZER_MAX
= 9,
30 AUTH_MODE_MON_MAX
= 19,
37 map
<string
, bufferlist
> caps
;
39 void encode(bufferlist
& bl
) const {
43 encode((uint64_t)CEPH_AUTH_UID_DEFAULT
, bl
);
47 void decode(bufferlist::const_iterator
& bl
) {
59 WRITE_CLASS_ENCODER(EntityAuth
)
61 static inline ostream
& operator<<(ostream
& out
, const EntityAuth
& a
) {
62 return out
<< "auth(key=" << a
.key
<< ")";
69 AuthCapsInfo() : allow_all(false) {}
71 void encode(bufferlist
& bl
) const {
75 __u8 a
= (__u8
)allow_all
;
79 void decode(bufferlist::const_iterator
& bl
) {
89 WRITE_CLASS_ENCODER(AuthCapsInfo
)
92 * The ticket (if properly validated) authorizes the principal use
93 * services as described by 'caps' during the specified validity
98 uint64_t global_id
; /* global instance id */
99 utime_t created
, renew_after
, expires
;
103 AuthTicket() : global_id(0), flags(0){}
105 void init_timestamps(utime_t now
, double ttl
) {
110 renew_after
+= ttl
/ 2.0;
113 void encode(bufferlist
& bl
) const {
116 encode(struct_v
, bl
);
118 encode(global_id
, bl
);
119 encode((uint64_t)CEPH_AUTH_UID_DEFAULT
, bl
);
125 void decode(bufferlist::const_iterator
& bl
) {
128 decode(struct_v
, bl
);
130 decode(global_id
, bl
);
133 decode(old_auid
, bl
);
141 WRITE_CLASS_ENCODER(AuthTicket
)
145 * abstract authorizer class
147 struct AuthAuthorizer
{
150 CryptoKey session_key
;
152 explicit AuthAuthorizer(__u32 p
) : protocol(p
) {}
153 virtual ~AuthAuthorizer() {}
154 virtual bool verify_reply(bufferlist::const_iterator
& reply
,
155 std::string
*connection_secret
) = 0;
156 virtual bool add_challenge(CephContext
*cct
, const bufferlist
& challenge
) = 0;
159 struct AuthAuthorizerChallenge
{
160 virtual ~AuthAuthorizerChallenge() {}
163 struct AuthConnectionMeta
{
164 uint32_t auth_method
= CEPH_AUTH_UNKNOWN
; //< CEPH_AUTH_*
166 /// client: initial empty, but populated if server said bad method
167 std::vector
<uint32_t> allowed_methods
;
169 int auth_mode
= AUTH_MODE_NONE
; ///< AUTH_MODE_*
171 int con_mode
= 0; ///< negotiated mode
173 bool is_mode_crc() const {
174 return con_mode
== CEPH_CON_MODE_CRC
;
176 bool is_mode_secure() const {
177 return con_mode
== CEPH_CON_MODE_SECURE
;
180 CryptoKey session_key
; ///< per-ticket key
182 size_t get_connection_secret_length() const {
184 case CEPH_CON_MODE_CRC
:
186 case CEPH_CON_MODE_SECURE
:
191 std::string connection_secret
; ///< per-connection key
193 std::unique_ptr
<AuthAuthorizer
> authorizer
;
194 std::unique_ptr
<AuthAuthorizerChallenge
> authorizer_challenge
;
200 #define KEY_ROTATE_NUM 3 /* prev, current, next */
202 struct ExpiringCryptoKey
{
206 void encode(bufferlist
& bl
) const {
209 encode(struct_v
, bl
);
211 encode(expiration
, bl
);
213 void decode(bufferlist::const_iterator
& bl
) {
216 decode(struct_v
, bl
);
218 decode(expiration
, bl
);
221 WRITE_CLASS_ENCODER(ExpiringCryptoKey
)
223 static inline ostream
& operator<<(ostream
& out
, const ExpiringCryptoKey
& c
)
225 return out
<< c
.key
<< " expires " << c
.expiration
;
228 struct RotatingSecrets
{
229 map
<uint64_t, ExpiringCryptoKey
> secrets
;
232 RotatingSecrets() : max_ver(0) {}
234 void encode(bufferlist
& bl
) const {
237 encode(struct_v
, bl
);
241 void decode(bufferlist::const_iterator
& bl
) {
244 decode(struct_v
, bl
);
249 uint64_t add(ExpiringCryptoKey
& key
) {
250 secrets
[++max_ver
] = key
;
251 while (secrets
.size() > KEY_ROTATE_NUM
)
252 secrets
.erase(secrets
.begin());
256 bool need_new_secrets() const {
257 return secrets
.size() < KEY_ROTATE_NUM
;
259 bool need_new_secrets(utime_t now
) const {
260 return secrets
.size() < KEY_ROTATE_NUM
|| current().expiration
<= now
;
263 ExpiringCryptoKey
& previous() {
264 return secrets
.begin()->second
;
266 ExpiringCryptoKey
& current() {
267 map
<uint64_t, ExpiringCryptoKey
>::iterator p
= secrets
.begin();
271 const ExpiringCryptoKey
& current() const {
272 map
<uint64_t, ExpiringCryptoKey
>::const_iterator p
= secrets
.begin();
276 ExpiringCryptoKey
& next() {
277 return secrets
.rbegin()->second
;
280 return secrets
.empty();
285 WRITE_CLASS_ENCODER(RotatingSecrets
)
291 virtual ~KeyStore() {}
292 virtual bool get_secret(const EntityName
& name
, CryptoKey
& secret
) const = 0;
293 virtual bool get_service_secret(uint32_t service_id
, uint64_t secret_id
,
294 CryptoKey
& secret
) const = 0;
297 inline bool auth_principal_needs_rotating_keys(EntityName
& name
)
299 uint32_t ty(name
.get_type());
300 return ((ty
== CEPH_ENTITY_TYPE_OSD
)
301 || (ty
== CEPH_ENTITY_TYPE_MDS
)
302 || (ty
== CEPH_ENTITY_TYPE_MGR
));