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
);
80 virtual bool haveOriginatorUid() const = 0;
81 virtual uid_t
originatorUid() const = 0;
82 virtual CFDataRef
copyUserPrefs() = 0;
85 void setAttributes(SessionAttributeBits attrs
) { mAttributes
|= attrs
; }
88 const CredentialSet
&authCredentials() const { return mSessionCreds
; }
90 OSStatus
authCreate(const AuthItemSet
&rights
, const AuthItemSet
&environment
,
91 AuthorizationFlags flags
, AuthorizationBlob
&newHandle
, const audit_token_t
&auditToken
);
92 void authFree(const AuthorizationBlob
&auth
, AuthorizationFlags flags
);
93 OSStatus
authGetRights(const AuthorizationBlob
&auth
,
94 const AuthItemSet
&requestedRights
, const AuthItemSet
&environment
,
95 AuthorizationFlags flags
, AuthItemSet
&grantedRights
);
96 OSStatus
authGetInfo(const AuthorizationBlob
&auth
, const char *tag
, AuthItemSet
&contextInfo
);
98 OSStatus
authExternalize(const AuthorizationBlob
&auth
, AuthorizationExternalForm
&extForm
);
99 OSStatus
authInternalize(const AuthorizationExternalForm
&extForm
, AuthorizationBlob
&auth
);
101 OSStatus
authorizationdbGet(AuthorizationString inRightName
, CFDictionaryRef
*rightDict
);
102 OSStatus
authorizationdbSet(const AuthorizationBlob
&authBlob
, AuthorizationString inRightName
, CFDictionaryRef rightDict
);
103 OSStatus
authorizationdbRemove(const AuthorizationBlob
&authBlob
, AuthorizationString inRightName
);
106 struct AuthorizationExternalBlob
{
107 AuthorizationBlob blob
;
112 AuthorizationToken
&authorization(const AuthorizationBlob
&blob
);
113 void mergeCredentials(CredentialSet
&creds
);
116 static Session
&find(Port servPort
);
117 static Session
&find(SecuritySessionId id
);
118 template <class SessionType
> static SessionType
&find(SecuritySessionId id
);
119 static void destroy(Port servPort
);
121 static void processSystemSleep();
122 void processLockAll();
124 RefPointer
<AuthHostInstance
> authhost(const AuthHostType hostType
= securityAgent
, const bool restart
= false);
127 Bootstrap mBootstrap
; // session bootstrap port
128 Port mServicePort
; // SecurityServer service port for this session
129 SessionAttributeBits mAttributes
; // attribute bits (see AuthSession.h)
131 mutable Mutex mCredsLock
; // lock for mSessionCreds
132 CredentialSet mSessionCreds
; // shared session authorization credentials
134 mutable Mutex mAuthHostLock
;
135 AuthHostInstance
*mSecurityAgent
;
136 AuthHostInstance
*mAuthHost
;
138 CFRef
<CFDataRef
> mSessionAgentPrefs
;
143 static PortMap
<Session
> mSessions
;
146 template <class SessionType
>
147 SessionType
&Session::find(SecuritySessionId id
)
149 if (SessionType
*ssn
= dynamic_cast<SessionType
*>(&find(id
)))
152 MacOSError::throwMe(errSessionInvalidId
);
157 // The RootSession is the session (i.e. bootstrap dictionary) of system daemons that are
158 // started early and don't belong to anything more restrictive. The RootSession is considered
160 // Currently, telnet sessions et al also default into this session, but this will change
163 class RootSession
: public Session
{
165 RootSession(Server
&server
, SessionAttributeBits attrs
= 0);
167 bool haveOriginatorUid() const { return true; }
168 uid_t
originatorUid() const;
169 CFDataRef
copyUserPrefs();
174 // A DynamicSession is the default type of session object. We create one when a new
175 // Connection initializes whose bootstrap port we haven't seen before. These Sessions
176 // are torn down when their bootstrap object disappears (which happens when mach_init
177 // destroys it due to its requestor referent vanishing).
179 class DynamicSession
: private ReceivePort
, public Session
{
181 DynamicSession(TaskPort taskPort
);
184 void setupAttributes(SessionCreationFlags flags
, SessionAttributeBits attrs
);
186 bool haveOriginatorUid() const { return mHaveOriginatorUid
; }
187 uid_t
originatorUid() const;
188 void originatorUid(uid_t uid
);
189 void setUserPrefs(CFDataRef userPrefsDict
);
190 CFDataRef
copyUserPrefs();
193 void checkOriginator(); // fail unless current process is originator
194 void kill(); // augment parent's kill
197 Port mOriginatorTask
; // originating process's task port
198 bool mHaveOriginatorUid
; // originator uid was set by session originator
199 uid_t mOriginatorUid
; // uid as set by session originator