2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // cssmaclpod - enhanced PodWrappers for ACL-related CSSM data structures
25 #include <Security/utilities.h>
26 #include <Security/cssmlist.h>
27 #include <Security/cssmalloc.h>
29 #ifdef _CPP_CSSMACLPOD
36 // a nicer name for an authorization tag
37 typedef CSSM_ACL_AUTHORIZATION_TAG AclAuthorization
;
38 typedef set
<AclAuthorization
> AclAuthorizationSet
;
42 // Enhanced POD Wrappers for the public ACL-related CSSM structures
44 class AuthorizationGroup
: public PodWrapper
<AuthorizationGroup
, CSSM_AUTHORIZATIONGROUP
> {
46 AuthorizationGroup() { NumberOfAuthTags
= 0; }
47 AuthorizationGroup(AclAuthorization auth
);
48 explicit AuthorizationGroup(const AclAuthorizationSet
&, CssmAllocator
&alloc
);
50 bool empty() const { return NumberOfAuthTags
== 0; }
51 unsigned int count() const { return NumberOfAuthTags
; }
52 CSSM_ACL_AUTHORIZATION_TAG
operator [] (unsigned ix
) const
53 { assert(ix
< count()); return AuthTags
[ix
]; }
55 bool contains(CSSM_ACL_AUTHORIZATION_TAG tag
) const;
56 operator AclAuthorizationSet () const;
59 class AclOwnerPrototype
;
61 class AclEntryPrototype
: public PodWrapper
<AclEntryPrototype
, CSSM_ACL_ENTRY_PROTOTYPE
> {
63 AclEntryPrototype() { memset(this, 0, sizeof(*this)); }
64 AclEntryPrototype(const AclOwnerPrototype
&proto
);
65 AclEntryPrototype(const CSSM_LIST
&subj
, bool delegate
= false)
66 { memset(this, 0, sizeof(*this)); TypedSubject
= subj
; Delegate
= delegate
; }
68 TypedList
&subject() { return TypedList::overlay(TypedSubject
); }
69 const TypedList
&subject() const { return TypedList::overlay(TypedSubject
); }
70 bool delegate() const { return Delegate
; }
71 char *tag() { return EntryTag
; }
72 const char *tag() const { return EntryTag
; }
73 AuthorizationGroup
&authorization() { return AuthorizationGroup::overlay(Authorization
); }
74 const AuthorizationGroup
&authorization() const
75 { return AuthorizationGroup::overlay(Authorization
); }
78 class AclOwnerPrototype
: public PodWrapper
<AclOwnerPrototype
, CSSM_ACL_OWNER_PROTOTYPE
> {
80 AclOwnerPrototype() { }
81 explicit AclOwnerPrototype(const AclEntryPrototype
&proto
)
82 { TypedSubject
= proto
.subject(); Delegate
= proto
.delegate(); }
84 TypedList
&subject() { return TypedList::overlay(TypedSubject
); }
85 const TypedList
&subject() const { return TypedList::overlay(TypedSubject
); }
86 bool delegate() const { return Delegate
; }
89 class AclEntryInfo
: public PodWrapper
<AclEntryInfo
, CSSM_ACL_ENTRY_INFO
> {
91 AclEntryPrototype
&proto() { return AclEntryPrototype::overlay(EntryPublicInfo
); }
92 const AclEntryPrototype
&proto()
93 const { return AclEntryPrototype::overlay(EntryPublicInfo
); }
95 operator AclEntryPrototype
&() { return proto(); }
96 operator const AclEntryPrototype
&() const { return proto(); }
98 CSSM_ACL_HANDLE
&handle() { return EntryHandle
; }
99 const CSSM_ACL_HANDLE
&handle() const { return EntryHandle
; }
102 class AclEntryInput
: public PodWrapper
<AclEntryInput
, CSSM_ACL_ENTRY_INPUT
> {
104 AclEntryInput() { memset(this, 0, sizeof(*this)); }
105 AclEntryInput(const AclEntryPrototype
&prot
)
106 { Prototype
= prot
; Callback
= NULL
; CallerContext
= NULL
; }
108 AclEntryPrototype
&proto() { return AclEntryPrototype::overlay(Prototype
); }
109 const AclEntryPrototype
&proto() const { return AclEntryPrototype::overlay(Prototype
); }
110 //@@@ not supporting callback features (yet)
113 class AclEdit
: public PodWrapper
<AclEdit
, CSSM_ACL_EDIT
> {
115 AclEdit(CSSM_ACL_EDIT_MODE m
, CSSM_ACL_HANDLE h
, const AclEntryInput
*data
)
116 { EditMode
= m
; OldEntryHandle
= h
; NewEntry
= data
; }
117 AclEdit(const AclEntryInput
&add
)
118 { EditMode
= CSSM_ACL_EDIT_MODE_ADD
; OldEntryHandle
= CSSM_INVALID_HANDLE
; NewEntry
= &add
; }
119 AclEdit(CSSM_ACL_HANDLE h
, const AclEntryInput
&modify
)
120 { EditMode
= CSSM_ACL_EDIT_MODE_REPLACE
; OldEntryHandle
= h
; NewEntry
= &modify
; }
121 AclEdit(CSSM_ACL_HANDLE h
)
122 { EditMode
= CSSM_ACL_EDIT_MODE_DELETE
; OldEntryHandle
= h
; NewEntry
= NULL
; }
124 CSSM_ACL_EDIT_MODE
mode() const { return EditMode
; }
125 CSSM_ACL_HANDLE
handle() const { return OldEntryHandle
; }
126 const AclEntryInput
*newEntry() const { return AclEntryInput::overlay(NewEntry
); }
131 // Allocating versions of Acl structures
133 class AutoAclOwnerPrototype
{
134 NOCOPY(AutoAclOwnerPrototype
)
136 // allocator can be set after construction
137 AutoAclOwnerPrototype(CssmAllocator
*allocator
= NULL
) : mAllocator(allocator
) { }
138 ~AutoAclOwnerPrototype();
140 operator CSSM_ACL_OWNER_PROTOTYPE
*() { return mAclOwnerPrototype
; }
142 void allocator(CssmAllocator
&allocator
);
145 AclOwnerPrototype
*mAclOwnerPrototype
;
146 CssmAllocator
*mAllocator
;
150 class AutoAclEntryInfoList
{
151 NOCOPY(AutoAclEntryInfoList
)
153 // allocator can be set after construction
154 AutoAclEntryInfoList(CssmAllocator
*allocator
= NULL
)
155 : mAclEntryInfo(NULL
), mNumberOfAclEntries(0), mAllocator(allocator
) { }
156 ~AutoAclEntryInfoList();
158 operator CSSM_ACL_ENTRY_INFO_PTR
*() { return &CSSM_ACL_ENTRY_INFO_PTR(mAclEntryInfo
); }
159 operator uint32
*() { return &mNumberOfAclEntries
; }
161 void allocator(CssmAllocator
&allocator
);
163 const AclEntryInfo
&at(uint32 ix
) const { return mAclEntryInfo
[ix
]; }
164 const AclEntryInfo
&operator[](uint32 ix
) const
165 { assert(ix
< mNumberOfAclEntries
); return mAclEntryInfo
[ix
]; }
167 uint32
size() const { return mNumberOfAclEntries
; }
170 AclEntryInfo
*mAclEntryInfo
;
171 uint32 mNumberOfAclEntries
;
172 CssmAllocator
*mAllocator
;
177 // Walkers for the CSSM API structure types
179 namespace DataWalkers
{
182 template <class Action
>
183 AclEntryInput
*walk(Action
&operate
, AclEntryInput
* &input
)
186 walk(operate
, *input
);
190 template <class Action
>
191 void walk(Action
&operate
, AclEntryInput
&input
)
192 { walk(operate
, input
.proto()); }
195 template <class Action
>
196 void walk(Action
&operate
, AclEntryInfo
&info
)
197 { walk(operate
, info
.proto()); }
199 template <class Action
>
200 void walk(Action
&operate
, const AclEntryInfo
&info
)
201 { walk(operate
, const_cast<AclEntryInfo
&>(info
)); }
204 template <class Action
>
205 void walk(Action
&operate
, AclEntryPrototype
&proto
)
207 walk(operate
, proto
.subject());
208 operate(proto
.Authorization
.AuthTags
,
209 sizeof(CSSM_ACL_AUTHORIZATION_TAG
) * proto
.Authorization
.NumberOfAuthTags
);
210 //@@@ ignoring validity period
213 template <class Action
>
214 AclEntryPrototype
*walk(Action
&operate
, AclEntryPrototype
* &proto
)
217 walk(operate
, *proto
);
222 template <class Action
>
223 void walk(Action
&operate
, AclOwnerPrototype
&proto
)
225 walk(operate
, proto
.subject());
228 template <class Action
>
229 AclOwnerPrototype
*walk(Action
&operate
, AclOwnerPrototype
* &proto
)
232 walk(operate
, *proto
);
237 } // end namespace DataWalkers
239 } // end namespace Security
241 #ifdef _CPP_CSSMACLPOD