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 * AuthorizationEngine.h
23 * Copyright: (c) 2000 by Apple Computer, Inc., all rights reserved
27 #if !defined(__AuthorizationEngine__)
28 #define __AuthorizationEngine__ 1
30 #include <Security/Authorization.h>
31 #include <Security/refcount.h>
32 #include <Security/osxsigning.h>
33 #include "agentquery.h"
35 #include <CoreFoundation/CFDate.h>
36 #include <CoreFoundation/CFDictionary.h>
38 #include <sys/types.h>
44 class AuthorizationToken
;
46 namespace Authorization
49 class Error
: public CssmCommonError
{
54 virtual CSSM_RETURN
cssmError() const;
55 virtual OSStatus
osStatus() const;
56 virtual const char *what () const;
57 // @@@ Default value should be internal error.
58 static void throwMe(int err
= -1) __attribute((noreturn
));
62 /* Credentials are less than comparable so they can be put in sets or maps. */
63 class CredentialImpl
: public RefCount
66 CredentialImpl(const string
&username
, const uid_t uid
, gid_t gid
, bool shared
);
67 CredentialImpl(const string
&username
, const string
&password
, bool shared
);
70 bool operator < (const CredentialImpl
&other
) const;
72 // Returns true if this credential should be shared.
73 bool isShared() const;
76 void merge(const CredentialImpl
&other
);
78 // The time at which this credential was obtained.
79 CFAbsoluteTime
creationTime() const;
81 // Return true iff this credential is valid.
84 // Make this credential invalid.
87 // We could make Rule a friend but instead we just expose this for now
88 inline const string
& username() const { return mUsername
; }
89 inline const uid_t
uid() const { return mUid
; }
90 inline const gid_t
gid() const { return mGid
; }
94 // The username of the user that provided his password.
95 // This and mShared are what make this credential unique.
96 // @@@ We do not deal with the domain as of yet.
99 // True iff this credential is shared.
102 // Fields below are not used by less than operator
104 // cached pw-data as returned by getpwnam(mUsername)
108 CFAbsoluteTime mCreationTime
;
113 /* Credentials are less than comparable so they can be put in sets or maps. */
114 class Credential
: public RefPointer
<CredentialImpl
>
118 Credential(CredentialImpl
*impl
);
119 Credential(const string
&username
, const uid_t uid
, gid_t gid
, bool shared
);
120 Credential(const string
&username
, const string
&password
, bool shared
);
123 bool operator < (const Credential
&other
) const;
127 class MutableRightSet
;
130 class Right
: protected AuthorizationItem
132 friend MutableRightSet
;
135 static Right
&overlay(AuthorizationItem
&item
);
136 static Right
*overlay(AuthorizationItem
*item
);
138 Right(AuthorizationString name
, size_t valueLength
, const void *value
);
141 bool operator < (const Right
&other
) const;
142 AuthorizationString
rightName() const { return name
; }
143 size_t argumentLength() const { return valueLength
; }
144 const void *argument() const { return value
; }
148 /* A RightSet is a Container and a Back Insertion Sequence, but it is not a Sequence. Also it only
149 implements the const members of Container and Back Insertion Sequence. */
152 friend class MutableRightSet
;
154 // Container required memebers
155 typedef Right value_type
;
156 typedef const Right
&const_reference
;
157 typedef const Right
*const_pointer
;
158 typedef const_pointer const_iterator
;
159 typedef ptrdiff_t difference_type
;
160 typedef size_t size_type
;
162 RightSet(const AuthorizationRights
*rights
= NULL
);
163 RightSet(const RightSet
&other
);
166 size_type
size() const { return mRights
->count
; }
167 size_type
max_size() const { return INT_MAX
; }
168 const_iterator
begin() const { return static_cast<const_pointer
>(mRights
->items
); }
169 const_iterator
end() const { return static_cast<const_pointer
>(&mRights
->items
[mRights
->count
]); }
170 bool empty() const { return size() == 0; }
172 // Back Insertion Sequence required memebers
173 const_reference
back() const;
175 // Other convenience members
176 operator const AuthorizationRights
*() const { return mRights
; }
178 RightSet
&operator = (const RightSet
&other
);
181 static const AuthorizationRights gEmptyRights
;
182 AuthorizationRights
*mRights
;
186 /* A MutableRightSet is a Container and a Back Insertion Sequence, but it is not a Sequence. */
187 class MutableRightSet
: public RightSet
190 // Container required memebers
191 typedef Right
&reference
;
192 typedef Right
*pointer
;
193 typedef pointer iterator
;
195 MutableRightSet(size_t count
= 0, const Right
&element
= Right());
196 MutableRightSet(const RightSet
&other
);
199 MutableRightSet
&operator = (const RightSet
&other
);
201 iterator
begin() { return static_cast<pointer
>(mRights
->items
); }
202 iterator
end() { return static_cast<pointer
>(&mRights
->items
[mRights
->count
]); }
203 void swap(MutableRightSet
&other
);
205 // Back Insertion Sequence required memebers
207 void push_back(const_reference right
);
210 // Other convenience members
211 size_type
capacity() const { return mCapacity
; }
213 void grow(size_type min_capacity
);
219 typedef set
<Credential
> CredentialSet
;
226 Rule(CFTypeRef cfRule
);
227 Rule(const Rule
&other
);
228 Rule
&operator = (const Rule
&other
);
231 OSStatus
evaluate(const Right
&inRight
, const AuthorizationEnvironment
*environment
,
232 AuthorizationFlags flags
, CFAbsoluteTime now
,
233 const CredentialSet
*inCredentials
, CredentialSet
&credentials
,
234 const AuthorizationToken
&auth
);
237 OSStatus
evaluate(const Right
&inRight
, const AuthorizationEnvironment
*environment
,
238 CFAbsoluteTime now
, const Credential
&credential
, bool ignoreShared
);
239 OSStatus
obtainCredential(QueryAuthorizeByGroup
&client
, const Right
&inRight
,
240 const AuthorizationEnvironment
*environment
, const char *usernameHint
,
241 Credential
&outCredential
, SecurityAgent::Reason reason
);
251 CFTimeInterval mMaxCredentialAge
;
255 static CFStringRef kUserInGroupID
;
256 static CFStringRef kTimeoutID
;
257 static CFStringRef kSharedID
;
258 static CFStringRef kAllowRootID
;
259 static CFStringRef kDenyID
;
260 static CFStringRef kAllowID
;
264 /* The engine which performs the actual authentication and authorization computations.
266 The implementation of a typical call to AuthorizationCreate would look like:
268 Get the current shared CredentialSet for this session.
269 Call authorizedRights() with inRights and the shared CredentialSet.
270 Compute the difference set between the rights requested and the rights returned from authorizedRights().
271 Call credentialIds() with the rights computed above (for which we have no credentials yet).
272 Call aquireCredentials() for the credentialIds returned from credentialIds()
273 For each credential returned place it in the session (replacing when needed) if shared() returns true.
274 The authorization returned to the user should now refer to the credentials in the session and the non shared ones returned by aquireCredentials().
276 When a call to AuthorizationCopyRights() is made, just call authorizedRights() using the union of the session credentials and the credentials tied to the authorization specified.
278 When a call to AuthorizationCopyInfo() is made, ask the Credential specified by tag for it info and return it.
280 When a call to AuthorizationFree() is made, delete all the non-shared credentials ascociated with the authorization specified. If the kAuthorizationFreeFlagDestroy is set. Also delete the shared credentials ascociated with the authorization specified.
285 Engine(const char *configFile
);
288 OSStatus
authorize(const RightSet
&inRights
, const AuthorizationEnvironment
*environment
,
289 AuthorizationFlags flags
, const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
,
290 MutableRightSet
*outRights
, const AuthorizationToken
&auth
);
292 void updateRules(CFAbsoluteTime now
);
294 void parseRules(CFDictionaryRef rules
);
295 static void parseRuleCallback(const void *key
, const void *value
, void *context
);
296 void parseRule(CFStringRef right
, CFTypeRef rule
);
298 Rule
getRule(const Right
&inRight
) const;
300 char *mRulesFileName
;
301 CFAbsoluteTime mLastChecked
;
302 struct timespec mRulesFileMtimespec
;
304 typedef map
<Right
, Rule
> RightMap
;
305 typedef map
<string
, Rule
> RuleMap
;
310 }; // namespace Authorization
312 #endif /* ! __AuthorizationEngine__ */