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.cpp
23 * Created by Michael Brouwer on Thu Oct 12 2000.
24 * Copyright (c) 2000 Apple Computer Inc. All rights reserved.
27 #include "AuthorizationEngine.h"
28 #include <Security/AuthorizationWalkers.h>
29 #include "AuthorizationPriv.h"
30 #include "AuthorizationDB.h"
33 #include "authority.h"
35 #include <Security/AuthorizationTags.h>
36 #include <Security/logging.h>
37 #include <Security/cfutilities.h>
38 #include <Security/debugging.h>
42 #include <CoreFoundation/CFData.h>
43 #include <CoreFoundation/CFNumber.h>
44 #include <CoreFoundation/CFPropertyList.h>
50 namespace Authorization
{
54 // Errors to be thrown
56 Error::Error(int err
) : error(err
)
60 const char *Error::what() const throw()
61 { return "Authorization error"; }
63 CSSM_RETURN
Error::cssmError() const throw()
64 { return error
; } // @@@ eventually...
66 OSStatus
Error::osStatus() const throw()
69 void Error::throwMe(int err
) { throw Error(err
); }
74 Engine::Engine(const char *configFile
) : mAuthdb(configFile
)
84 @function AuthorizationEngine::authorize
88 @param inRights (input) List of rights being requested for authorization.
89 @param environment (optional/input) Environment containing information to be used during evaluation.
90 @param flags (input) Optional flags @@@ see AuthorizationCreate for a description.
91 @param inCredentials (input) Credentials already held by the caller.
92 @param outCredentials (output/optional) Credentials obtained, used or refreshed during this call to authorize the requested rights.
93 @param outRights (output/optional) Subset of inRights which were actually authorized.
95 @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
98 Engine::authorize(const AuthItemSet
&inRights
, const AuthItemSet
&environment
,
99 AuthorizationFlags flags
, const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
,
100 AuthItemSet
&outRights
, AuthorizationToken
&auth
)
102 CredentialSet credentials
;
103 OSStatus status
= errAuthorizationSuccess
;
105 // Get current time of day.
106 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
108 // Update rules from database if needed
111 // Check if a credential was passed into the environment and we were asked to extend the rights
112 if (flags
& kAuthorizationFlagExtendRights
)
114 string username
, password
;
116 for (AuthItemSet::iterator item
= environment
.begin(); item
!= environment
.end(); item
++)
118 if (!strcmp((*item
)->name(), kAuthorizationEnvironmentUsername
))
119 username
= (*item
)->stringValue();
120 else if (!strcmp((*item
)->name(), kAuthorizationEnvironmentPassword
))
121 password
= (*item
)->stringValue();
122 else if (!strcmp((*item
)->name(), kAuthorizationEnvironmentShared
))
126 if (username
.length())
128 // Let's create a credential from the passed in username and password.
129 Credential
newCredential(username
, password
, shared
);
130 // If it's valid insert it into the credentials list. Normally this is
131 // only done if it actually authorizes a requested right, but for this
132 // special case (environment) we do it even when no rights are being requested.
133 if (newCredential
->isValid())
134 credentials
.insert(newCredential
);
138 // generate hints for every authorization
139 AuthItemSet environmentToClient
= environment
;
141 AuthItemSet::const_iterator end
= inRights
.end();
142 for (AuthItemSet::const_iterator it
= inRights
.begin(); it
!= end
; ++it
)
144 // Get the rule for each right we are trying to obtain.
145 const Rule
&toplevelRule
= mAuthdb
.getRule(*it
);
146 OSStatus result
= toplevelRule
->evaluate(*it
, toplevelRule
, environmentToClient
, flags
, now
, inCredentials
, credentials
, auth
);
147 secdebug("autheval", "evaluate rule %s for right %s returned %ld.", toplevelRule
->name().c_str(), (*it
)->name(), result
);
150 CodeSigning::OSXCode
*processCode
= Server::connection().process
.clientCode();
151 string processName
= processCode
? processCode
->canonicalPath() : "unknown";
152 CodeSigning::OSXCode
*authCreatorCode
= auth
.creatorCode();
153 string authCreatorName
= authCreatorCode
? authCreatorCode
->canonicalPath() : "unknown";
155 if (result
== errAuthorizationSuccess
)
156 Syslog::info("Succeeded authorizing right %s by process %s for authorization created by %s.", (*it
)->name(), processName
.c_str(), authCreatorName
.c_str());
157 else if (result
== errAuthorizationDenied
)
158 Syslog::notice("Failed to authorize right %s by process %s for authorization created by %s.", (*it
)->name(), processName
.c_str(), authCreatorName
.c_str());
161 if (result
== errAuthorizationSuccess
)
162 outRights
.insert(*it
);
163 else if (result
== errAuthorizationDenied
|| result
== errAuthorizationInteractionNotAllowed
)
165 // add creator pid to authorization token
166 if (!(flags
& kAuthorizationFlagPartialRights
))
172 else if (result
== errAuthorizationCanceled
)
179 Syslog::error("Engine::authorize: Rule::evaluate returned %ld returning errAuthorizationInternal", result
);
180 status
= errAuthorizationInternal
;
186 outCredentials
->swap(credentials
);
192 Engine::verifyModification(string inRightName
, bool remove
,
193 const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
, AuthorizationToken
&auth
)
197 // meta rights are constructed as follows:
198 // we don't allow setting of wildcard rights, so you can only be more specific
199 // note that you should never restrict things with a wildcard right without disallowing
200 // changes to the entire domain. ie.
201 // system.privilege. -> never
202 // config.add.system.privilege. -> never
203 // config.modify.system.privilege. -> never
204 // config.delete.system.privilege. -> never
205 // For now we don't allow any configuration of configuration rules
206 // config.config. -> never
208 string rightnameToCheck
;
210 // @@@ verify right name is is not NULL or zero length
211 if (inRightName
.length() == 0)
212 return errAuthorizationDenied
;
214 // @@@ make sure it isn't a wildcard right by checking trailing "."
215 if ( *(inRightName
.rbegin()) == '.')
216 return errAuthorizationDenied
;
218 // @@@ make sure it isn't a configure right by checking it doesn't start with config.
219 if (inRightName
.find(kConfigRight
, 0) != string::npos
)
221 // special handling of meta right change:
222 // config.add. config.modify. config.remove. config.{}.
223 // check for config.<right> (which always starts with config.config.)
224 rightnameToCheck
= string(kConfigRight
) + inRightName
;
228 // regular check of rights
229 bool existingRule
= mAuthdb
.existRule(inRightName
);
233 rightnameToCheck
= string(kAuthorizationConfigRightModify
) + inRightName
;
235 rightnameToCheck
= string(kAuthorizationConfigRightAdd
) + inRightName
;
240 rightnameToCheck
= string(kAuthorizationConfigRightRemove
) + inRightName
;
243 secdebug("engine", "rule %s doesn't exist.", inRightName
.c_str());
244 return errAuthorizationSuccess
; // doesn't exist, done
250 AuthItemSet rights
, environment
, outRights
;
251 rights
.insert(AuthItemRef(rightnameToCheck
.c_str()));
252 secdebug("engine", "authorizing %s for db modification.", rightnameToCheck
.c_str());
253 return authorize(rights
, environment
, kAuthorizationFlagDefaults
| kAuthorizationFlagInteractionAllowed
| kAuthorizationFlagExtendRights
, inCredentials
, outCredentials
, outRights
, auth
);
257 Engine::getRule(string
&inRightName
, CFDictionaryRef
*outRuleDefinition
)
259 // Get current time of day.
260 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
262 // Update rules from database if needed
265 CFDictionaryRef definition
= mAuthdb
.getRuleDefinition(inRightName
);
268 if (outRuleDefinition
)
269 *outRuleDefinition
= definition
;
271 CFRelease(definition
);
273 return errAuthorizationSuccess
;
276 return errAuthorizationDenied
;
280 Engine::setRule(const char *inRightName
, CFDictionaryRef inRuleDefinition
, const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
, AuthorizationToken
&auth
)
282 // Get current time of day.
283 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
285 // Update rules from database if needed
288 // Validate rule by constructing it from the passed dictionary
289 if (!mAuthdb
.validateRule(inRightName
, inRuleDefinition
))
290 return errAuthorizationDenied
; // @@@ separate error for this?
292 OSStatus result
= verifyModification(inRightName
, false /*setting, not removing*/, inCredentials
, outCredentials
, auth
);
293 if (result
!= errAuthorizationSuccess
)
296 // set the rule for the right and save the database
297 mAuthdb
.setRule(inRightName
, inRuleDefinition
);
299 return errAuthorizationSuccess
;
303 Engine::removeRule(const char *inRightName
, const CredentialSet
*inCredentials
, CredentialSet
*outCredentials
, AuthorizationToken
&auth
)
305 // Get current time of day.
306 CFAbsoluteTime now
= CFAbsoluteTimeGetCurrent();
308 // Update rules from database if needed
311 OSStatus result
= verifyModification(inRightName
, true /*removing*/, inCredentials
, outCredentials
, auth
);
312 if (result
!= errAuthorizationSuccess
)
315 // set the rule for the right and save the database
316 mAuthdb
.removeRule(inRightName
);
318 return errAuthorizationSuccess
;
321 } // end namespace Authorization