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 std::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
);
49 explicit AuthorizationGroup(const AclAuthorizationSet
&, CssmAllocator
&alloc
);
50 void destroy(CssmAllocator
&alloc
);
52 bool empty() const { return NumberOfAuthTags
== 0; }
53 unsigned int count() const { return NumberOfAuthTags
; }
54 CSSM_ACL_AUTHORIZATION_TAG
operator [] (unsigned ix
) const
55 { assert(ix
< count()); return AuthTags
[ix
]; }
57 bool contains(CSSM_ACL_AUTHORIZATION_TAG tag
) const;
58 operator AclAuthorizationSet () const;
61 class AclOwnerPrototype
;
63 class AclEntryPrototype
: public PodWrapper
<AclEntryPrototype
, CSSM_ACL_ENTRY_PROTOTYPE
> {
65 AclEntryPrototype() { clearPod(); }
66 explicit AclEntryPrototype(const AclOwnerPrototype
&proto
);
67 AclEntryPrototype(const CSSM_LIST
&subj
, bool delegate
= false)
68 { clearPod(); TypedSubject
= subj
; Delegate
= delegate
; }
70 TypedList
&subject() { return TypedList::overlay(TypedSubject
); }
71 const TypedList
&subject() const { return TypedList::overlay(TypedSubject
); }
72 bool delegate() const { return Delegate
; }
73 char *tag() { return EntryTag
; }
74 const char *tag() const { return EntryTag
; }
75 void tag(const char *tagString
);
76 AuthorizationGroup
&authorization() { return AuthorizationGroup::overlay(Authorization
); }
77 const AuthorizationGroup
&authorization() const
78 { return AuthorizationGroup::overlay(Authorization
); }
81 class AclOwnerPrototype
: public PodWrapper
<AclOwnerPrototype
, CSSM_ACL_OWNER_PROTOTYPE
> {
83 AclOwnerPrototype() { clearPod(); }
84 explicit AclOwnerPrototype(const AclEntryPrototype
&proto
)
85 { TypedSubject
= proto
.subject(); Delegate
= proto
.delegate(); }
86 AclOwnerPrototype(const CSSM_LIST
&subj
, bool delegate
= false)
87 { TypedSubject
= subj
; Delegate
= delegate
; }
89 TypedList
&subject() { return TypedList::overlay(TypedSubject
); }
90 const TypedList
&subject() const { return TypedList::overlay(TypedSubject
); }
91 bool delegate() const { return Delegate
; }
94 class AclEntryInfo
: public PodWrapper
<AclEntryInfo
, CSSM_ACL_ENTRY_INFO
> {
96 AclEntryPrototype
&proto() { return AclEntryPrototype::overlay(EntryPublicInfo
); }
97 const AclEntryPrototype
&proto()
98 const { return AclEntryPrototype::overlay(EntryPublicInfo
); }
100 operator AclEntryPrototype
&() { return proto(); }
101 operator const AclEntryPrototype
&() const { return proto(); }
103 CSSM_ACL_HANDLE
&handle() { return EntryHandle
; }
104 const CSSM_ACL_HANDLE
&handle() const { return EntryHandle
; }
107 class AclEntryInput
: public PodWrapper
<AclEntryInput
, CSSM_ACL_ENTRY_INPUT
> {
109 AclEntryInput() { clearPod(); }
110 AclEntryInput(const AclEntryPrototype
&prot
)
111 { Prototype
= prot
; Callback
= NULL
; CallerContext
= NULL
; }
113 AclEntryPrototype
&proto() { return AclEntryPrototype::overlay(Prototype
); }
114 const AclEntryPrototype
&proto() const { return AclEntryPrototype::overlay(Prototype
); }
115 //@@@ not supporting callback features (yet)
118 class AclEdit
: public PodWrapper
<AclEdit
, CSSM_ACL_EDIT
> {
120 AclEdit(CSSM_ACL_EDIT_MODE m
, CSSM_ACL_HANDLE h
, const AclEntryInput
*data
)
121 { EditMode
= m
; OldEntryHandle
= h
; NewEntry
= data
; }
122 AclEdit(const AclEntryInput
&add
)
123 { EditMode
= CSSM_ACL_EDIT_MODE_ADD
; OldEntryHandle
= CSSM_INVALID_HANDLE
; NewEntry
= &add
; }
124 AclEdit(CSSM_ACL_HANDLE h
, const AclEntryInput
&modify
)
125 { EditMode
= CSSM_ACL_EDIT_MODE_REPLACE
; OldEntryHandle
= h
; NewEntry
= &modify
; }
126 AclEdit(CSSM_ACL_HANDLE h
)
127 { EditMode
= CSSM_ACL_EDIT_MODE_DELETE
; OldEntryHandle
= h
; NewEntry
= NULL
; }
129 CSSM_ACL_EDIT_MODE
mode() const { return EditMode
; }
130 CSSM_ACL_HANDLE
handle() const { return OldEntryHandle
; }
131 const AclEntryInput
*newEntry() const { return AclEntryInput::overlay(NewEntry
); }
136 // Allocating versions of Acl structures
138 class AutoAclOwnerPrototype
{
139 NOCOPY(AutoAclOwnerPrototype
)
141 // allocator can be set after construction
142 AutoAclOwnerPrototype(CssmAllocator
*allocator
= NULL
)
143 : mAclOwnerPrototype(NULL
), mAllocator(allocator
) { }
144 ~AutoAclOwnerPrototype();
146 operator CSSM_ACL_OWNER_PROTOTYPE
*() { return make(); }
147 AclOwnerPrototype
&operator * () { return *make(); }
149 void allocator(CssmAllocator
&allocator
);
152 AclOwnerPrototype
*mAclOwnerPrototype
;
153 CssmAllocator
*mAllocator
;
155 AclOwnerPrototype
*make();
159 class AutoAclEntryInfoList
{
160 NOCOPY(AutoAclEntryInfoList
)
162 // allocator can be set after construction
163 AutoAclEntryInfoList(CssmAllocator
*allocator
= NULL
)
164 : mAclEntryInfo(NULL
), mNumberOfAclEntries(0), mAllocator(allocator
) { }
165 ~AutoAclEntryInfoList();
167 operator CSSM_ACL_ENTRY_INFO_PTR
*() { return &CSSM_ACL_ENTRY_INFO_PTR(mAclEntryInfo
); }
168 operator uint32
*() { return &mNumberOfAclEntries
; }
170 void allocator(CssmAllocator
&allocator
);
172 const AclEntryInfo
&at(uint32 ix
) const { return mAclEntryInfo
[ix
]; }
173 const AclEntryInfo
&operator[](uint32 ix
) const
174 { assert(ix
< mNumberOfAclEntries
); return mAclEntryInfo
[ix
]; }
175 AclEntryInfo
&operator[](uint32 ix
)
176 { assert(ix
< mNumberOfAclEntries
); return mAclEntryInfo
[ix
]; }
178 uint32
size() const { return mNumberOfAclEntries
; } // obsolete
179 uint32
count() const { return mNumberOfAclEntries
; }
180 AclEntryInfo
*entries() const { return mAclEntryInfo
; }
183 AclEntryInfo
*mAclEntryInfo
;
184 uint32 mNumberOfAclEntries
;
185 CssmAllocator
*mAllocator
;
188 class AutoAuthorizationGroup
: public AuthorizationGroup
{
190 AutoAuthorizationGroup(CssmAllocator
&alloc
) : allocator(alloc
) { }
191 explicit AutoAuthorizationGroup(const AclAuthorizationSet
&set
,
192 CssmAllocator
&alloc
) : AuthorizationGroup(set
, alloc
), allocator(alloc
) { }
193 ~AutoAuthorizationGroup() { destroy(allocator
); }
195 CssmAllocator
&allocator
;
200 // Walkers for the CSSM API structure types
202 namespace DataWalkers
{
205 template <class Action
>
206 AclEntryInput
*walk(Action
&operate
, AclEntryInput
* &input
)
209 walk(operate
, *input
);
213 template <class Action
>
214 void walk(Action
&operate
, AclEntryInput
&input
)
215 { walk(operate
, input
.proto()); }
218 template <class Action
>
219 void walk(Action
&operate
, AclEntryInfo
&info
)
220 { walk(operate
, info
.proto()); }
222 template <class Action
>
223 void walk(Action
&operate
, const AclEntryInfo
&info
)
224 { walk(operate
, const_cast<AclEntryInfo
&>(info
)); }
227 template <class Action
>
228 void walk(Action
&operate
, AclEntryPrototype
&proto
)
230 walk(operate
, proto
.subject());
231 operate(proto
.Authorization
.AuthTags
,
232 sizeof(CSSM_ACL_AUTHORIZATION_TAG
) * proto
.Authorization
.NumberOfAuthTags
);
233 //@@@ ignoring validity period
236 template <class Action
>
237 AclEntryPrototype
*walk(Action
&operate
, AclEntryPrototype
* &proto
)
240 walk(operate
, *proto
);
245 template <class Action
>
246 void walk(Action
&operate
, AclOwnerPrototype
&proto
)
248 walk(operate
, proto
.subject());
251 template <class Action
>
252 AclOwnerPrototype
*walk(Action
&operate
, AclOwnerPrototype
* &proto
)
255 walk(operate
, *proto
);
260 } // end namespace DataWalkers
262 } // end namespace Security
264 #ifdef _CPP_CSSMACLPOD