2  * Copyright (c) 2000-2004,2008 Apple 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 // session - authentication session domains 
  31 #include "structure.h" 
  33 #include "authority.h" 
  35 #include <Security/AuthSession.h> 
  36 #include <security_utilities/ccaudit.h> 
  37 #include <security_cdsa_utilities/handletemplates_defs.h> 
  38 #include <security_cdsa_utilities/u32handleobject.h> 
  39 #include <security_cdsa_utilities/cssmdb.h> 
  40 #include <bsm/audit.h> 
  41 #include <bsm/audit_session.h> 
  42 #include <sys/event.h> 
  47 class AuthHostInstance
; 
  50 // A Session object represents one or more Connections that are known to 
  51 // belong to the same authentication domain. Informally this means just 
  52 // about "the same user", for the right definition of "user." The upshot 
  53 // is that global credentials can be shared by Connections of one Session 
  54 // with a modicum of security, and so Sessions are the natural nexus of 
  55 // single-sign-on functionality. 
  57 class Session 
: public PerSession 
{ 
  59         typedef au_asid_t SessionId
;                    // internal session identifier (audit session id) 
  61     Session(const CommonCriteria::AuditInfo 
&audit
, Server 
&server
); 
  64         SessionId 
sessionId() const { return mAudit
.sessionId(); } 
  65         CommonCriteria::AuditInfo 
&auditInfo() { return mAudit
; } 
  67         IFDUMP(virtual void dumpNode()); 
  70     static const SessionAttributeBits settableAttributes 
= 
  71         sessionHasGraphicAccess 
| sessionHasTTY 
| sessionIsRemote 
| AU_SESSION_FLAG_HAS_AUTHENTICATED
; 
  73     SessionAttributeBits 
attributes() const                     { updateAudit(); return mAudit
.ai_flags
; } 
  74     bool attribute(SessionAttributeBits bits
) const     { return attributes() & bits
; } 
  75         void setAttributes(SessionAttributeBits bits
); 
  77     virtual void setupAttributes(SessionCreationFlags flags
, SessionAttributeBits attrs
); 
  79         virtual uid_t 
originatorUid() const             { updateAudit(); return mAudit
.uid(); } 
  81         virtual CFDataRef 
copyUserPrefs() = 0; 
  83         static const char kUsername
[]; 
  84     static const char kRealname
[]; 
  87         const CredentialSet 
&authCredentials() const    { return mSessionCreds
; } 
  90     // For external Authorization clients 
  92         OSStatus 
authCreate(const AuthItemSet 
&rights
, const AuthItemSet 
&environment
, 
  93                 AuthorizationFlags flags
, AuthorizationBlob 
&newHandle
, const audit_token_t 
&auditToken
); 
  94         void authFree(const AuthorizationBlob 
&auth
, AuthorizationFlags flags
); 
  95         static OSStatus 
authGetRights(const AuthorizationBlob 
&auth
, 
  96                 const AuthItemSet 
&requestedRights
, const AuthItemSet 
&environment
, 
  97                 AuthorizationFlags flags
, AuthItemSet 
&grantedRights
); 
  98         OSStatus 
authGetInfo(const AuthorizationBlob 
&auth
, const char *tag
, AuthItemSet 
&contextInfo
); 
 100         OSStatus 
authExternalize(const AuthorizationBlob 
&auth
, AuthorizationExternalForm 
&extForm
); 
 101         OSStatus 
authInternalize(const AuthorizationExternalForm 
&extForm
, AuthorizationBlob 
&auth
); 
 103         OSStatus 
authorizationdbGet(AuthorizationString inRightName
, CFDictionaryRef 
*rightDict
); 
 104         OSStatus 
authorizationdbSet(const AuthorizationBlob 
&authBlob
, AuthorizationString inRightName
, CFDictionaryRef rightDict
); 
 105         OSStatus 
authorizationdbRemove(const AuthorizationBlob 
&authBlob
, AuthorizationString inRightName
); 
 108     // Authorization methods for securityd's internal use 
 110     OSStatus 
authCheckRight(string 
&rightName
, Connection 
&connection
, bool allowUI
); 
 111     // authCheckRight() with exception-handling and Boolean return semantics 
 112     bool isRightAuthorized(string 
&rightName
, Connection 
&connection
, bool allowUI
); 
 115         void updateAudit() const; 
 118     struct AuthorizationExternalBlob 
{ 
 119         AuthorizationBlob blob
; 
 124     static AuthorizationToken 
&authorization(const AuthorizationBlob 
&blob
); 
 125         OSStatus 
authGetRights(AuthorizationToken 
&auth
, 
 126                 const AuthItemSet 
&requestedRights
, const AuthItemSet 
&environment
, 
 127                 AuthorizationFlags flags
, AuthItemSet 
&grantedRights
); 
 128         void mergeCredentials(CredentialSet 
&creds
); 
 131     void invalidateSessionAuthHosts();      // invalidate auth hosts in this session 
 132     static void invalidateAuthHosts();      // invalidate auth hosts in all sessions 
 134         static void processSystemSleep(); 
 135         void processLockAll(); 
 137         RefPointer
<AuthHostInstance
> authhost(const AuthHostType hostType 
= securityAgent
, const bool restart 
= false); 
 140         mutable CommonCriteria::AuditInfo mAudit
; 
 142         mutable Mutex mCredsLock
;                               // lock for mSessionCreds 
 143         CredentialSet mSessionCreds
;                    // shared session authorization credentials 
 145         mutable Mutex mAuthHostLock
; 
 146         AuthHostInstance 
*mSecurityAgent
; 
 147         AuthHostInstance 
*mAuthHost
; 
 149         CFRef
<CFDataRef
> mSessionAgentPrefs
; 
 150     Credential mOriginatorCredential
; 
 155         static Session 
&find(SessionId id
, bool create
);        // find and optionally create 
 156     template <class SessionType
> static SessionType 
&find(SecuritySessionId id
); 
 157         static void destroy(SessionId id
); 
 160         typedef std::map
<SessionId
, RefPointer
<Session
> > SessionMap
; 
 161         static SessionMap mSessions
; 
 162         static Mutex mSessionLock
; 
 166 template <class SessionType
> 
 167 SessionType 
&Session::find(SecuritySessionId id
) 
 169         if (SessionType 
*ssn 
= dynamic_cast<SessionType 
*>(&find(id
, false))) 
 172                 MacOSError::throwMe(errSessionInvalidId
); 
 177 // The RootSession is the session of all code that originates from system startup processing 
 178 // and does not belong to any particular login origin. (Or, if you prefer, whose login origin 
 179 // is the system itself.) 
 181 class RootSession 
: public Session 
{ 
 183     RootSession(uint64_t attributes
, Server 
&server
); 
 185         CFDataRef 
copyUserPrefs()           { return NULL
; } 
 190 // A DynamicSession object represents a session that is dynamically constructed 
 191 // when we first encounter it. These sessions are actually created in client 
 192 // space using the audit session APIs. 
 193 // We tear down a DynamicSession when the system reports (via kevents) that the 
 194 // kernel audit session object has been destroyed. 
 196 class DynamicSession 
: private ReceivePort
, public Session 
{ 
 198     DynamicSession(const CommonCriteria::AuditInfo 
&audit
); 
 200         void setUserPrefs(CFDataRef userPrefsDict
); 
 201         CFDataRef 
copyUserPrefs();