-//
-// Dynamic sessions use the audit session context of the first-contact client caller.
-//
-DynamicSession::DynamicSession(const AuditInfo &audit)
- : Session(audit, Server::active())
-{
-}
-
-
-//
-// Authorization operations
-//
-OSStatus Session::authCreate(const AuthItemSet &rights,
- const AuthItemSet &environment,
- AuthorizationFlags flags,
- AuthorizationBlob &newHandle,
- const audit_token_t &auditToken)
-{
- // invoke the authorization computation engine
- CredentialSet resultCreds;
-
- // this will acquire the object lock, so we delay acquiring it (@@@ no longer needed)
- auto_ptr<AuthorizationToken> auth(new AuthorizationToken(*this, resultCreds, auditToken, (flags&kAuthorizationFlagLeastPrivileged)));
-
- SECURITYD_AUTH_CREATE(this, auth.get());
-
- // Make a copy of the mSessionCreds
- CredentialSet sessionCreds;
- {
- StLock<Mutex> _(mCredsLock);
- sessionCreds = mSessionCreds;
- }
-
- AuthItemSet outRights;
- OSStatus result = Server::authority().authorize(rights, environment, flags,
- &sessionCreds, &resultCreds, outRights, *auth);
- newHandle = auth->handle();
-
- // merge resulting creds into shared pool
- if ((flags & kAuthorizationFlagExtendRights) &&
- !(flags & kAuthorizationFlagDestroyRights))
- {
- StLock<Mutex> _(mCredsLock);
- mergeCredentials(resultCreds);
- auth->mergeCredentials(resultCreds);
- }
-
- // Make sure that this isn't done until the auth(AuthorizationToken) is guaranteed to
- // not be destroyed anymore since it's destructor asserts it has no processes
- Server::process().addAuthorization(auth.get());
- auth.release();
- return result;
-}
-
-void Session::authFree(const AuthorizationBlob &authBlob, AuthorizationFlags flags)
-{
- AuthorizationToken::Deleter deleter(authBlob);
- AuthorizationToken &auth = deleter;
- Process &process = Server::process();
- process.checkAuthorization(&auth);
-
- if (flags & kAuthorizationFlagDestroyRights) {
- // explicitly invalidate all shared credentials and remove them from the session
- for (CredentialSet::const_iterator it = auth.begin(); it != auth.end(); it++)
- if ((*it)->isShared())
- (*it)->invalidate();
- }
-
- // now get rid of the authorization itself
- if (process.removeAuthorization(&auth))
- deleter.remove();
-}
-
-OSStatus Session::authGetRights(const AuthorizationBlob &authBlob,
- const AuthItemSet &rights, const AuthItemSet &environment,
- AuthorizationFlags flags,
- AuthItemSet &grantedRights)
-{
- AuthorizationToken &auth = authorization(authBlob);
- return auth.session().authGetRights(auth, rights, environment, flags, grantedRights);
-}
-
-OSStatus Session::authGetRights(AuthorizationToken &auth,
- const AuthItemSet &rights, const AuthItemSet &environment,
- AuthorizationFlags flags,
- AuthItemSet &grantedRights)
-{
- CredentialSet resultCreds;
- CredentialSet effective;
- {
- StLock<Mutex> _(mCredsLock);
- effective = auth.effectiveCreds();
- }
- OSStatus result = Server::authority().authorize(rights, environment, flags,
- &effective, &resultCreds, grantedRights, auth);
-
- // merge resulting creds into shared pool
- if ((flags & kAuthorizationFlagExtendRights) && !(flags & kAuthorizationFlagDestroyRights))
- {
- StLock<Mutex> _(mCredsLock);
- mergeCredentials(resultCreds);
- auth.mergeCredentials(resultCreds);
- }
-
- secdebug("SSauth", "Authorization %p copyRights asked for %d got %d",
- &auth, int(rights.size()), int(grantedRights.size()));
- return result;
-}
-
-OSStatus Session::authGetInfo(const AuthorizationBlob &authBlob,
- const char *tag,
- AuthItemSet &contextInfo)
-{
- AuthorizationToken &auth = authorization(authBlob);
- secdebug("SSauth", "Authorization %p get-info", &auth);
- contextInfo = auth.infoSet(tag);
- return noErr;
-}
-
-OSStatus Session::authExternalize(const AuthorizationBlob &authBlob,
- AuthorizationExternalForm &extForm)
-{
- const AuthorizationToken &auth = authorization(authBlob);
- StLock<Mutex> _(*this);
- if (auth.mayExternalize(Server::process())) {
- memset(&extForm, 0, sizeof(extForm));
- AuthorizationExternalBlob &extBlob =
- reinterpret_cast<AuthorizationExternalBlob &>(extForm);
- extBlob.blob = auth.handle();
- extBlob.session = this->sessionId();
- secdebug("SSauth", "Authorization %p externalized", &auth);
- return noErr;
- } else
- return errAuthorizationExternalizeNotAllowed;
-}
-
-OSStatus Session::authInternalize(const AuthorizationExternalForm &extForm,
- AuthorizationBlob &authBlob)
-{
- // interpret the external form
- const AuthorizationExternalBlob &extBlob =
- reinterpret_cast<const AuthorizationExternalBlob &>(extForm);
-
- // locate source authorization
- AuthorizationToken &sourceAuth = AuthorizationToken::find(extBlob.blob);
-
- // check for permission and do it
- if (sourceAuth.mayInternalize(Server::process(), true)) {
- StLock<Mutex> _(*this);
- authBlob = extBlob.blob;
- Server::process().addAuthorization(&sourceAuth);
- secdebug("SSauth", "Authorization %p internalized", &sourceAuth);
- return noErr;
- } else
- return errAuthorizationInternalizeNotAllowed;
-}
-
-