2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 * AuthorizationEngine.cpp
29 * Created by Michael Brouwer on Thu Oct 12 2000.
32 #include "AuthorizationEngine.h"
33 #include <security_cdsa_utilities/AuthorizationWalkers.h>
34 #include <Security/AuthorizationPriv.h>
35 #include <Security/AuthorizationDB.h>
38 #include "authority.h"
40 #include <Security/AuthorizationTags.h>
41 #include <Security/AuthorizationTagsPriv.h>
42 #include <security_utilities/logging.h>
43 #include <security_utilities/cfutilities.h>
44 #include <security_utilities/debugging.h>
45 //#include "session.h"
48 #include <CoreFoundation/CFData.h>
49 #include <CoreFoundation/CFNumber.h>
50 #include <CoreFoundation/CFPropertyList.h>
56 namespace Authorization
{
60 // Errors to be thrown
62 Error::Error(int err
) : error(err
)
66 const char *Error::what() const throw()
67 { return "Authorization error"; }
69 int Error::unixError() const throw()
70 { return error
; } // @@@ eventually...
72 OSStatus
Error::osStatus() const throw()
75 void Error::throwMe(int err
) { throw Error(err
); }
80 Engine::Engine(const char *configFile
) : mAuthdb(configFile
)
90 @function AuthorizationEngine::authorize
94 @param inRights (input) List of rights being requested for authorization.
95 @param environment (optional/input) Environment containing information to be used during evaluation.
96 @param flags (input) Optional flags @@@ see AuthorizationCreate for a description.
97 @param inCredentials (input) Credentials already held by the caller.
98 @param outCredentials (output/optional) Credentials obtained, used or refreshed during this call to authorize the requested rights.
99 @param outRights (output/optional) Subset of inRights which were actually authorized.
101 @results Returns errAuthorizationSuccess if all rights requested are authorized, or if the kAuthorizationFlagPartialRights flag was specified. Might return other status values like errAuthorizationDenied, errAuthorizationCanceled or errAuthorizationInteractionNotAllowed
104 Engine::authorize(const AuthItemSet
&inRights
, const AuthItemSet
&environment
,
105 AuthorizationFlags flags
, const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
,
106 AuthItemSet
&outRights
, AuthorizationToken
&auth
)
108 CredentialSet credentials
;
109 OSStatus status
= errAuthorizationSuccess
;
111 // Get current time of day.
112 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
114 // Update rules from database if needed
117 // Check if a credential was passed into the environment and we were asked to extend the rights
118 if (flags
& kAuthorizationFlagExtendRights
)
120 string username
, password
;
122 for (AuthItemSet::iterator item
= environment
.begin(); item
!= environment
.end(); item
++)
124 if (!strcmp((*item
)->name(), kAuthorizationEnvironmentUsername
))
125 username
= (*item
)->stringValue();
126 else if (!strcmp((*item
)->name(), kAuthorizationEnvironmentPassword
))
127 password
= (*item
)->stringValue();
128 else if (!strcmp((*item
)->name(), kAuthorizationEnvironmentShared
))
132 if (username
.length())
134 // Let's create a credential from the passed in username and password.
135 Credential
newCredential(username
, password
, shared
);
136 // If it's valid insert it into the credentials list. Normally this is
137 // only done if it actually authorizes a requested right, but for this
138 // special case (environment) we do it even when no rights are being requested.
139 if (newCredential
->isValid())
140 credentials
.insert(newCredential
);
144 // generate hints for every authorization
145 AuthItemSet environmentToClient
= environment
;
147 AuthItemSet::const_iterator end
= inRights
.end();
148 for (AuthItemSet::const_iterator it
= inRights
.begin(); it
!= end
; ++it
)
150 // Get the rule for each right we are trying to obtain.
151 const Rule
&toplevelRule
= mAuthdb
.getRule(*it
);
152 OSStatus result
= toplevelRule
->evaluate(*it
, toplevelRule
, environmentToClient
, flags
, now
, inCredentials
, credentials
, auth
);
153 secdebug("autheval", "evaluate rule %s for right %s returned %ld.", toplevelRule
->name().c_str(), (*it
)->name(), result
);
156 RefPointer
<OSXCode
> processCode
= Server::process().clientCode();
157 string processName
= processCode
? processCode
->canonicalPath() : "unknown";
158 RefPointer
<OSXCode
> authCreatorCode
= auth
.creatorCode();
159 string authCreatorName
= authCreatorCode
? authCreatorCode
->canonicalPath() : "unknown";
161 if (result
== errAuthorizationSuccess
)
162 Syslog::info("Succeeded authorizing right %s by process %s for authorization created by %s.", (*it
)->name(), processName
.c_str(), authCreatorName
.c_str());
163 else if (result
== errAuthorizationDenied
)
164 Syslog::notice("Failed to authorize right %s by process %s for authorization created by %s.", (*it
)->name(), processName
.c_str(), authCreatorName
.c_str());
167 if (result
== errAuthorizationSuccess
)
168 outRights
.insert(*it
);
169 else if (result
== errAuthorizationDenied
|| result
== errAuthorizationInteractionNotAllowed
)
171 // add creator pid to authorization token
172 if (!(flags
& kAuthorizationFlagPartialRights
))
178 else if (result
== errAuthorizationCanceled
)
185 Syslog::error("Engine::authorize: Rule::evaluate returned %ld returning errAuthorizationInternal", result
);
186 status
= errAuthorizationInternal
;
192 outCredentials
->swap(credentials
);
198 Engine::verifyModification(string inRightName
, bool remove
,
199 const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
, AuthorizationToken
&auth
)
203 // meta rights are constructed as follows:
204 // we don't allow setting of wildcard rights, so you can only be more specific
205 // note that you should never restrict things with a wildcard right without disallowing
206 // changes to the entire domain. ie.
207 // system.privilege. -> never
208 // config.add.system.privilege. -> never
209 // config.modify.system.privilege. -> never
210 // config.delete.system.privilege. -> never
211 // For now we don't allow any configuration of configuration rules
212 // config.config. -> never
214 string rightnameToCheck
;
216 // @@@ verify right name is is not NULL or zero length
217 if (inRightName
.length() == 0)
218 return errAuthorizationDenied
;
220 // @@@ make sure it isn't a wildcard right by checking trailing "."
221 if ( *(inRightName
.rbegin()) == '.')
222 return errAuthorizationDenied
;
224 // @@@ make sure it isn't a configure right by checking it doesn't start with config.
225 if (inRightName
.find(kConfigRight
, 0) != string::npos
)
227 // special handling of meta right change:
228 // config.add. config.modify. config.remove. config.{}.
229 // check for config.<right> (which always starts with config.config.)
230 rightnameToCheck
= string(kConfigRight
) + inRightName
;
234 // regular check of rights
235 bool existingRule
= mAuthdb
.existRule(inRightName
);
239 rightnameToCheck
= string(kAuthorizationConfigRightModify
) + inRightName
;
241 rightnameToCheck
= string(kAuthorizationConfigRightAdd
) + inRightName
;
246 rightnameToCheck
= string(kAuthorizationConfigRightRemove
) + inRightName
;
249 secdebug("engine", "rule %s doesn't exist.", inRightName
.c_str());
250 return errAuthorizationSuccess
; // doesn't exist, done
256 AuthItemSet rights
, environment
, outRights
;
257 rights
.insert(AuthItemRef(rightnameToCheck
.c_str()));
258 secdebug("engine", "authorizing %s for db modification.", rightnameToCheck
.c_str());
259 return authorize(rights
, environment
, kAuthorizationFlagDefaults
| kAuthorizationFlagInteractionAllowed
| kAuthorizationFlagExtendRights
, inCredentials
, outCredentials
, outRights
, auth
);
263 Engine::getRule(string
&inRightName
, CFDictionaryRef
*outRuleDefinition
)
265 // Get current time of day.
266 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
268 // Update rules from database if needed
271 CFDictionaryRef definition
= mAuthdb
.getRuleDefinition(inRightName
);
274 if (outRuleDefinition
)
275 *outRuleDefinition
= definition
;
277 CFRelease(definition
);
279 return errAuthorizationSuccess
;
282 return errAuthorizationDenied
;
286 Engine::setRule(const char *inRightName
, CFDictionaryRef inRuleDefinition
, const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
, AuthorizationToken
&auth
)
288 // Get current time of day.
289 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
291 // Update rules from database if needed
294 // Validate rule by constructing it from the passed dictionary
295 if (!mAuthdb
.validateRule(inRightName
, inRuleDefinition
))
296 return errAuthorizationDenied
; // @@@ separate error for this?
298 OSStatus result
= verifyModification(inRightName
, false /*setting, not removing*/, inCredentials
, outCredentials
, auth
);
299 if (result
!= errAuthorizationSuccess
)
302 // set the rule for the right and save the database
303 mAuthdb
.setRule(inRightName
, inRuleDefinition
);
305 return errAuthorizationSuccess
;
309 Engine::removeRule(const char *inRightName
, const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
, AuthorizationToken
&auth
)
311 // Get current time of day.
312 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
314 // Update rules from database if needed
317 OSStatus result
= verifyModification(inRightName
, true /*removing*/, inCredentials
, outCredentials
, auth
);
318 if (result
!= errAuthorizationSuccess
)
321 // set the rule for the right and save the database
322 mAuthdb
.removeRule(inRightName
);
324 return errAuthorizationSuccess
;
327 } // end namespace Authorization