2 * Copyright (c) 2000-2004 Apple Computer, 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/handleobject.h>
37 #include <security_cdsa_utilities/cssmdb.h>
40 #include <ext/hash_map>
41 using __gnu_cxx::hash_map
;
50 class AuthHostInstance
;
53 // A Session object represents one or more Connections that are known to
54 // belong to the same authentication domain. Informally this means just
55 // about "the same user", for the right definition of "user." The upshot
56 // is that global credentials can be shared by Connections of one Session
57 // with a modicum of security, and so Sessions are the natural nexus of
58 // single-sign-on functionality.
60 class Session
: public HandleObject
, public PerSession
{
62 typedef MachPlusPlus::Bootstrap Bootstrap
;
64 Session(Bootstrap bootstrap
, Port servicePort
, SessionAttributeBits attrs
= 0);
67 Bootstrap
bootstrapPort() const { return mBootstrap
; }
68 Port
servicePort() const { return mServicePort
; }
70 IFDUMP(virtual void dumpNode());
73 static const SessionAttributeBits settableAttributes
=
74 sessionHasGraphicAccess
| sessionHasTTY
| sessionIsRemote
;
76 SessionAttributeBits
attributes() const { return mAttributes
; }
77 bool attribute(SessionAttributeBits bits
) const { return mAttributes
& bits
; }
79 virtual void setupAttributes(SessionCreationFlags flags
, SessionAttributeBits attrs
);
81 virtual bool haveOriginatorUid() const = 0;
82 virtual uid_t
originatorUid() const = 0;
83 Credential
originatorCredential() const { return mOriginatorCredential
; }
85 virtual CFDataRef
copyUserPrefs() = 0;
87 static std::string kUsername
;
88 static std::string kRealname
;
91 void setAttributes(SessionAttributeBits attrs
) { mAttributes
|= attrs
; }
94 const CredentialSet
&authCredentials() const { return mSessionCreds
; }
96 OSStatus
authCreate(const AuthItemSet
&rights
, const AuthItemSet
&environment
,
97 AuthorizationFlags flags
, AuthorizationBlob
&newHandle
, const audit_token_t
&auditToken
);
98 void authFree(const AuthorizationBlob
&auth
, AuthorizationFlags flags
);
99 static OSStatus
authGetRights(const AuthorizationBlob
&auth
,
100 const AuthItemSet
&requestedRights
, const AuthItemSet
&environment
,
101 AuthorizationFlags flags
, AuthItemSet
&grantedRights
);
102 OSStatus
authGetInfo(const AuthorizationBlob
&auth
, const char *tag
, AuthItemSet
&contextInfo
);
104 OSStatus
authExternalize(const AuthorizationBlob
&auth
, AuthorizationExternalForm
&extForm
);
105 OSStatus
authInternalize(const AuthorizationExternalForm
&extForm
, AuthorizationBlob
&auth
);
107 OSStatus
authorizationdbGet(AuthorizationString inRightName
, CFDictionaryRef
*rightDict
);
108 OSStatus
authorizationdbSet(const AuthorizationBlob
&authBlob
, AuthorizationString inRightName
, CFDictionaryRef rightDict
);
109 OSStatus
authorizationdbRemove(const AuthorizationBlob
&authBlob
, AuthorizationString inRightName
);
112 struct AuthorizationExternalBlob
{
113 AuthorizationBlob blob
;
118 static AuthorizationToken
&authorization(const AuthorizationBlob
&blob
);
119 OSStatus
authGetRights(AuthorizationToken
&auth
,
120 const AuthItemSet
&requestedRights
, const AuthItemSet
&environment
,
121 AuthorizationFlags flags
, AuthItemSet
&grantedRights
);
122 void mergeCredentials(CredentialSet
&creds
);
125 static Session
&find(Port servPort
);
126 static Session
&find(SecuritySessionId id
);
127 template <class SessionType
> static SessionType
&find(SecuritySessionId id
);
128 static void destroy(Port servPort
);
130 static void processSystemSleep();
131 void processLockAll();
133 RefPointer
<AuthHostInstance
> authhost(const AuthHostType hostType
= securityAgent
, const bool restart
= false);
136 Bootstrap mBootstrap
; // session bootstrap port
137 Port mServicePort
; // SecurityServer service port for this session
138 SessionAttributeBits mAttributes
; // attribute bits (see AuthSession.h)
140 mutable Mutex mCredsLock
; // lock for mSessionCreds
141 CredentialSet mSessionCreds
; // shared session authorization credentials
143 mutable Mutex mAuthHostLock
;
144 AuthHostInstance
*mSecurityAgent
;
145 AuthHostInstance
*mAuthHost
;
147 CFRef
<CFDataRef
> mSessionAgentPrefs
;
148 Credential mOriginatorCredential
;
153 static PortMap
<Session
> mSessions
;
156 template <class SessionType
>
157 SessionType
&Session::find(SecuritySessionId id
)
159 if (SessionType
*ssn
= dynamic_cast<SessionType
*>(&find(id
)))
162 MacOSError::throwMe(errSessionInvalidId
);
167 // The RootSession is the session (i.e. bootstrap dictionary) of system daemons that are
168 // started early and don't belong to anything more restrictive. The RootSession is considered
170 // Currently, telnet sessions et al also default into this session, but this will change
173 class RootSession
: public Session
{
175 RootSession(Server
&server
, SessionAttributeBits attrs
= 0);
177 bool haveOriginatorUid() const { return true; }
178 uid_t
originatorUid() const { return 0; }
179 CFDataRef
copyUserPrefs() { return NULL
; }
184 // A DynamicSession is the default type of session object. We create one when a new
185 // Connection initializes whose bootstrap port we haven't seen before. These Sessions
186 // are torn down when their bootstrap object disappears (which happens when mach_init
187 // destroys it due to its requestor referent vanishing).
189 class DynamicSession
: private ReceivePort
, public Session
{
191 DynamicSession(TaskPort taskPort
);
194 void setupAttributes(SessionCreationFlags flags
, SessionAttributeBits attrs
);
196 bool haveOriginatorUid() const { return mHaveOriginatorUid
; }
197 uid_t
originatorUid() const;
198 void originatorUid(uid_t uid
);
199 void setUserPrefs(CFDataRef userPrefsDict
);
200 CFDataRef
copyUserPrefs();
203 void checkOriginator(); // fail unless current process is originator
204 void kill(); // augment parent's kill
207 Port mOriginatorTask
; // originating process's task port
208 bool mHaveOriginatorUid
; // originator uid was set by session originator
209 uid_t mOriginatorUid
; // uid as set by session originator