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 Server
&server() const;
66 SessionId
sessionId() const { return mAudit
.sessionId(); }
67 CommonCriteria::AuditInfo
&auditInfo() { return mAudit
; }
69 IFDUMP(virtual void dumpNode());
72 static const SessionAttributeBits settableAttributes
=
73 sessionHasGraphicAccess
| sessionHasTTY
| sessionIsRemote
| AU_SESSION_FLAG_HAS_AUTHENTICATED
;
75 SessionAttributeBits
attributes() const { updateAudit(); return mAudit
.ai_flags
; }
76 bool attribute(SessionAttributeBits bits
) const { return attributes() & bits
; }
77 void setAttributes(SessionAttributeBits bits
);
79 virtual void setupAttributes(SessionCreationFlags flags
, SessionAttributeBits attrs
);
81 virtual uid_t
originatorUid() const { updateAudit(); return mAudit
.uid(); }
83 virtual CFDataRef
copyUserPrefs() = 0;
85 static const char kUsername
[];
86 static const char kRealname
[];
89 const CredentialSet
&authCredentials() const { return mSessionCreds
; }
92 // For external Authorization clients
94 OSStatus
authCreate(const AuthItemSet
&rights
, const AuthItemSet
&environment
,
95 AuthorizationFlags flags
, AuthorizationBlob
&newHandle
, const audit_token_t
&auditToken
);
96 void authFree(const AuthorizationBlob
&auth
, AuthorizationFlags flags
);
97 static OSStatus
authGetRights(const AuthorizationBlob
&auth
,
98 const AuthItemSet
&requestedRights
, const AuthItemSet
&environment
,
99 AuthorizationFlags flags
, AuthItemSet
&grantedRights
);
100 OSStatus
authGetInfo(const AuthorizationBlob
&auth
, const char *tag
, AuthItemSet
&contextInfo
);
102 OSStatus
authExternalize(const AuthorizationBlob
&auth
, AuthorizationExternalForm
&extForm
);
103 OSStatus
authInternalize(const AuthorizationExternalForm
&extForm
, AuthorizationBlob
&auth
);
105 OSStatus
authorizationdbGet(AuthorizationString inRightName
, CFDictionaryRef
*rightDict
);
106 OSStatus
authorizationdbSet(const AuthorizationBlob
&authBlob
, AuthorizationString inRightName
, CFDictionaryRef rightDict
);
107 OSStatus
authorizationdbRemove(const AuthorizationBlob
&authBlob
, AuthorizationString inRightName
);
110 // Authorization methods for securityd's internal use
112 OSStatus
authCheckRight(string
&rightName
, Connection
&connection
, bool allowUI
);
113 // authCheckRight() with exception-handling and Boolean return semantics
114 bool isRightAuthorized(string
&rightName
, Connection
&connection
, bool allowUI
);
117 void updateAudit() const;
120 struct AuthorizationExternalBlob
{
121 AuthorizationBlob blob
;
126 static AuthorizationToken
&authorization(const AuthorizationBlob
&blob
);
127 OSStatus
authGetRights(AuthorizationToken
&auth
,
128 const AuthItemSet
&requestedRights
, const AuthItemSet
&environment
,
129 AuthorizationFlags flags
, AuthItemSet
&grantedRights
);
130 void mergeCredentials(CredentialSet
&creds
);
133 void invalidateSessionAuthHosts(); // invalidate auth hosts in this session
134 static void invalidateAuthHosts(); // invalidate auth hosts in all sessions
136 static void processSystemSleep();
137 void processLockAll();
139 RefPointer
<AuthHostInstance
> authhost(const AuthHostType hostType
= securityAgent
, const bool restart
= false);
142 mutable CommonCriteria::AuditInfo mAudit
;
144 mutable Mutex mCredsLock
; // lock for mSessionCreds
145 CredentialSet mSessionCreds
; // shared session authorization credentials
147 mutable Mutex mAuthHostLock
;
148 AuthHostInstance
*mSecurityAgent
;
149 AuthHostInstance
*mAuthHost
;
151 CFRef
<CFDataRef
> mSessionAgentPrefs
;
152 Credential mOriginatorCredential
;
157 static Session
&find(SessionId id
, bool create
); // find and optionally create
158 template <class SessionType
> static SessionType
&find(SecuritySessionId id
);
159 static void destroy(SessionId id
);
162 typedef std::map
<SessionId
, RefPointer
<Session
> > SessionMap
;
163 static SessionMap mSessions
;
164 static Mutex mSessionLock
;
168 template <class SessionType
>
169 SessionType
&Session::find(SecuritySessionId id
)
171 if (SessionType
*ssn
= dynamic_cast<SessionType
*>(&find(id
, false)))
174 MacOSError::throwMe(errSessionInvalidId
);
179 // The RootSession is the session of all code that originates from system startup processing
180 // and does not belong to any particular login origin. (Or, if you prefer, whose login origin
181 // is the system itself.)
183 class RootSession
: public Session
{
185 RootSession(uint64_t attributes
, Server
&server
);
187 CFDataRef
copyUserPrefs() { return NULL
; }
192 // A DynamicSession object represents a session that is dynamically constructed
193 // when we first encounter it. These sessions are actually created in client
194 // space using the audit session APIs.
195 // We tear down a DynamicSession when the system reports (via kevents) that the
196 // kernel audit session object has been destroyed.
198 class DynamicSession
: private ReceivePort
, public Session
{
200 DynamicSession(const CommonCriteria::AuditInfo
&audit
);
202 void setUserPrefs(CFDataRef userPrefsDict
);
203 CFDataRef
copyUserPrefs();