1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// tokenaccess - access management to a TokenDatabase's Token's TokenDaemon's tokend
27//
28#ifndef _H_TOKENACCESS
29#define _H_TOKENACCESS
30
31#include "tokendatabase.h"
32#include "tokenkey.h"
33#include "server.h"
34
35
36//
37// Turn a Key into a TokenKey, when we know that it's that
38//
39inline TokenKey &myKey(Key &key)
40{
41	return safer_cast<TokenKey &>(key);
42}
43
44
45//
46// The common access/retry/management framework for calls that go to the actual daemon.
47//
48class Access : public Token::Access {
49public:
50	Access(Token &token) : Token::Access(token), mIteration(0)
51	{ Server::active().longTermActivity(); }
52	template <class Whatever>
53	Access(Token &token, Whatever &it) : Token::Access(token)
54	{ add(it); Server::active().longTermActivity(); }
55
56	void operator () (const CssmError &err);
57	using Token::Access::operator ();
58
59	void add(TokenAcl &acl)		{ mAcls.insert(&acl); }
60	void add(TokenAcl *acl)		{ if (acl) mAcls.insert(acl); }
61	void add(AclSource &src)	{ add(dynamic_cast<TokenAcl&>(src.acl())); }
62	void add(AclSource *src)	{ if (src) add(*src); }
63	void add(Key &key)			{ mAcls.insert(&myKey(key)); }
64
65private:
66	set<TokenAcl *> mAcls;		// TokenAcl subclasses to clear on retry
67	unsigned int mIteration;	// iteration count (try, retry, give up)
68};
69
70
71//
72// A nice little macro bracket to apply it.
73// You must declare an Access called 'access' before doing
74//	TRY
75//		some actions
76//		GUARD(a call to tokend)
77//	DONE
78//
79#define TRY			for (;;) {
80#define GUARD		try {
81#define DONE		return; \
82	} catch (const CssmError &error) { \
83		access(error); \
84	} }
85
86
87#endif //_H_TOKENACCESS
88