]> git.saurik.com Git - apple/securityd.git/blob - src/session.h
securityd-29035.tar.gz
[apple/securityd.git] / src / session.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // session - authentication session domains
27 //
28 #ifndef _H_SESSION
29 #define _H_SESSION
30
31 #include "structure.h"
32 #include "acls.h"
33 #include "authority.h"
34 #include "authhost.h"
35 #include <Security/AuthSession.h>
36 #include <security_cdsa_utilities/handleobject.h>
37 #include <security_cdsa_utilities/cssmdb.h>
38
39 #if __GNUC__ > 2
40 #include <ext/hash_map>
41 using __gnu_cxx::hash_map;
42 #else
43 #include <hash_map>
44 #endif
45
46
47 class Key;
48 class Connection;
49 class Server;
50 class AuthHostInstance;
51
52 //
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.
59 //
60 class Session : public HandleObject, public PerSession {
61 public:
62 typedef MachPlusPlus::Bootstrap Bootstrap;
63
64 Session(Bootstrap bootstrap, Port servicePort, SessionAttributeBits attrs = 0);
65 virtual ~Session();
66
67 Bootstrap bootstrapPort() const { return mBootstrap; }
68 Port servicePort() const { return mServicePort; }
69
70 IFDUMP(virtual void dumpNode());
71
72 public:
73 static const SessionAttributeBits settableAttributes =
74 sessionHasGraphicAccess | sessionHasTTY | sessionIsRemote;
75
76 SessionAttributeBits attributes() const { return mAttributes; }
77 bool attribute(SessionAttributeBits bits) const { return mAttributes & bits; }
78
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;
83
84 protected:
85 void setAttributes(SessionAttributeBits attrs) { mAttributes |= attrs; }
86
87 public:
88 const CredentialSet &authCredentials() const { return mSessionCreds; }
89
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);
97
98 OSStatus authExternalize(const AuthorizationBlob &auth, AuthorizationExternalForm &extForm);
99 OSStatus authInternalize(const AuthorizationExternalForm &extForm, AuthorizationBlob &auth);
100
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);
104
105 private:
106 struct AuthorizationExternalBlob {
107 AuthorizationBlob blob;
108 mach_port_t session;
109 };
110
111 protected:
112 AuthorizationToken &authorization(const AuthorizationBlob &blob);
113 void mergeCredentials(CredentialSet &creds);
114
115 public:
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);
120
121 static void processSystemSleep();
122 void processLockAll();
123
124 RefPointer<AuthHostInstance> authhost(const AuthHostType hostType = securityAgent, const bool restart = false);
125
126 protected:
127 Bootstrap mBootstrap; // session bootstrap port
128 Port mServicePort; // SecurityServer service port for this session
129 SessionAttributeBits mAttributes; // attribute bits (see AuthSession.h)
130
131 mutable Mutex mCredsLock; // lock for mSessionCreds
132 CredentialSet mSessionCreds; // shared session authorization credentials
133
134 mutable Mutex mAuthHostLock;
135 AuthHostInstance *mSecurityAgent;
136 AuthHostInstance *mAuthHost;
137
138 CFRef<CFDataRef> mSessionAgentPrefs;
139
140 void kill();
141
142 protected:
143 static PortMap<Session> mSessions;
144 };
145
146 template <class SessionType>
147 SessionType &Session::find(SecuritySessionId id)
148 {
149 if (SessionType *ssn = dynamic_cast<SessionType *>(&find(id)))
150 return *ssn;
151 else
152 MacOSError::throwMe(errSessionInvalidId);
153 }
154
155
156 //
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
159 // immortal.
160 // Currently, telnet sessions et al also default into this session, but this will change
161 // (we hope).
162 //
163 class RootSession : public Session {
164 public:
165 RootSession(Server &server, SessionAttributeBits attrs = 0);
166
167 bool haveOriginatorUid() const { return true; }
168 uid_t originatorUid() const;
169 CFDataRef copyUserPrefs();
170 };
171
172
173 //
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).
178 //
179 class DynamicSession : private ReceivePort, public Session {
180 public:
181 DynamicSession(TaskPort taskPort);
182 ~DynamicSession();
183
184 void setupAttributes(SessionCreationFlags flags, SessionAttributeBits attrs);
185
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();
191
192 protected:
193 void checkOriginator(); // fail unless current process is originator
194 void kill(); // augment parent's kill
195
196 private:
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
200 };
201
202
203 #endif //_H_SESSION