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_cdsa_utilities/handletemplates_defs.h>
37 #include <security_cdsa_utilities/u32handleobject.h>
38 #include <security_cdsa_utilities/cssmdb.h>
41 #include <ext/hash_map>
42 using __gnu_cxx::hash_map
;
51 class AuthHostInstance
;
54 // A Session object represents one or more Connections that are known to
55 // belong to the same authentication domain. Informally this means just
56 // about "the same user", for the right definition of "user." The upshot
57 // is that global credentials can be shared by Connections of one Session
58 // with a modicum of security, and so Sessions are the natural nexus of
59 // single-sign-on functionality.
61 class Session
: public U32HandleObject
, public PerSession
{
63 typedef MachPlusPlus::Bootstrap Bootstrap
;
65 Session(Bootstrap bootstrap
, Port servicePort
, SessionAttributeBits attrs
= 0);
68 Bootstrap
bootstrapPort() const { return mBootstrap
; }
69 Port
servicePort() const { return mServicePort
; }
71 IFDUMP(virtual void dumpNode());
74 static const SessionAttributeBits settableAttributes
=
75 sessionHasGraphicAccess
| sessionHasTTY
| sessionIsRemote
;
77 SessionAttributeBits
attributes() const { return mAttributes
; }
78 bool attribute(SessionAttributeBits bits
) const { return mAttributes
& bits
; }
80 virtual void setupAttributes(SessionCreationFlags flags
, SessionAttributeBits attrs
);
82 virtual bool haveOriginatorUid() const = 0;
83 virtual uid_t
originatorUid() const = 0;
84 Credential
originatorCredential() const { return mOriginatorCredential
; }
86 virtual CFDataRef
copyUserPrefs() = 0;
88 static std::string kUsername
;
89 static std::string kRealname
;
92 void setAttributes(SessionAttributeBits attrs
) { mAttributes
|= attrs
; }
95 const CredentialSet
&authCredentials() const { return mSessionCreds
; }
98 // For external Authorization clients
100 OSStatus
authCreate(const AuthItemSet
&rights
, const AuthItemSet
&environment
,
101 AuthorizationFlags flags
, AuthorizationBlob
&newHandle
, const audit_token_t
&auditToken
);
102 void authFree(const AuthorizationBlob
&auth
, AuthorizationFlags flags
);
103 static OSStatus
authGetRights(const AuthorizationBlob
&auth
,
104 const AuthItemSet
&requestedRights
, const AuthItemSet
&environment
,
105 AuthorizationFlags flags
, AuthItemSet
&grantedRights
);
106 OSStatus
authGetInfo(const AuthorizationBlob
&auth
, const char *tag
, AuthItemSet
&contextInfo
);
108 OSStatus
authExternalize(const AuthorizationBlob
&auth
, AuthorizationExternalForm
&extForm
);
109 OSStatus
authInternalize(const AuthorizationExternalForm
&extForm
, AuthorizationBlob
&auth
);
111 OSStatus
authorizationdbGet(AuthorizationString inRightName
, CFDictionaryRef
*rightDict
);
112 OSStatus
authorizationdbSet(const AuthorizationBlob
&authBlob
, AuthorizationString inRightName
, CFDictionaryRef rightDict
);
113 OSStatus
authorizationdbRemove(const AuthorizationBlob
&authBlob
, AuthorizationString inRightName
);
116 // Authorization methods for securityd's internal use
118 OSStatus
authCheckRight(string
&rightName
, Connection
&connection
, bool allowUI
);
119 // authCheckRight() with exception-handling and Boolean return semantics
120 bool isRightAuthorized(string
&rightName
, Connection
&connection
, bool allowUI
);
123 struct AuthorizationExternalBlob
{
124 AuthorizationBlob blob
;
129 static AuthorizationToken
&authorization(const AuthorizationBlob
&blob
);
130 OSStatus
authGetRights(AuthorizationToken
&auth
,
131 const AuthItemSet
&requestedRights
, const AuthItemSet
&environment
,
132 AuthorizationFlags flags
, AuthItemSet
&grantedRights
);
133 void mergeCredentials(CredentialSet
&creds
);
136 static Session
&find(Port servPort
);
137 static Session
&find(SecuritySessionId id
);
138 template <class SessionType
> static SessionType
&find(SecuritySessionId id
);
139 static void destroy(Port servPort
);
140 void invalidateSessionAuthHosts(); // invalidate auth hosts in this session
141 static void invalidateAuthHosts(); // invalidate auth hosts in all sessions
143 static void processSystemSleep();
144 void processLockAll();
146 RefPointer
<AuthHostInstance
> authhost(const AuthHostType hostType
= securityAgent
, const bool restart
= false);
149 Bootstrap mBootstrap
; // session bootstrap port
150 Port mServicePort
; // SecurityServer service port for this session
151 SessionAttributeBits mAttributes
; // attribute bits (see AuthSession.h)
153 mutable Mutex mCredsLock
; // lock for mSessionCreds
154 CredentialSet mSessionCreds
; // shared session authorization credentials
156 mutable Mutex mAuthHostLock
;
157 AuthHostInstance
*mSecurityAgent
;
158 AuthHostInstance
*mAuthHost
;
160 CFRef
<CFDataRef
> mSessionAgentPrefs
;
161 Credential mOriginatorCredential
;
166 static PortMap
<Session
> mSessions
;
169 template <class SessionType
>
170 SessionType
&Session::find(SecuritySessionId id
)
172 if (SessionType
*ssn
= dynamic_cast<SessionType
*>(&find(id
)))
175 MacOSError::throwMe(errSessionInvalidId
);
180 // The RootSession is the session (i.e. bootstrap dictionary) of system daemons that are
181 // started early and don't belong to anything more restrictive. The RootSession is considered
183 // Currently, telnet sessions et al also default into this session, but this will change
186 class RootSession
: public Session
{
188 RootSession(Server
&server
, SessionAttributeBits attrs
= 0);
190 bool haveOriginatorUid() const { return true; }
191 uid_t
originatorUid() const { return 0; }
192 CFDataRef
copyUserPrefs() { return NULL
; }
197 // A DynamicSession is the default type of session object. We create one when a new
198 // Connection initializes whose bootstrap port we haven't seen before. These Sessions
199 // are torn down when their bootstrap object disappears (which happens when mach_init
200 // destroys it due to its requestor referent vanishing).
202 class DynamicSession
: private ReceivePort
, public Session
{
204 DynamicSession(TaskPort taskPort
);
207 void setupAttributes(SessionCreationFlags flags
, SessionAttributeBits attrs
);
209 bool haveOriginatorUid() const { return mHaveOriginatorUid
; }
210 uid_t
originatorUid() const;
211 void originatorUid(uid_t uid
);
212 void setUserPrefs(CFDataRef userPrefsDict
);
213 CFDataRef
copyUserPrefs();
216 void checkOriginator(); // fail unless current process is originator
217 void kill(); // augment parent's kill
220 Port mOriginatorTask
; // originating process's task port
221 bool mHaveOriginatorUid
; // originator uid was set by session originator
222 uid_t mOriginatorUid
; // uid as set by session originator