]> git.saurik.com Git - apple/securityd.git/blob - src/session.h
securityd-36489.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
81 virtual bool haveOriginatorUid() const = 0;
82 virtual uid_t originatorUid() const = 0;
83 Credential originatorCredential() const { return mOriginatorCredential; }
84
85 virtual CFDataRef copyUserPrefs() = 0;
86
87 static std::string kUsername;
88 static std::string kRealname;
89
90 protected:
91 void setAttributes(SessionAttributeBits attrs) { mAttributes |= attrs; }
92
93 public:
94 const CredentialSet &authCredentials() const { return mSessionCreds; }
95
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);
103
104 OSStatus authExternalize(const AuthorizationBlob &auth, AuthorizationExternalForm &extForm);
105 OSStatus authInternalize(const AuthorizationExternalForm &extForm, AuthorizationBlob &auth);
106
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);
110
111 private:
112 struct AuthorizationExternalBlob {
113 AuthorizationBlob blob;
114 mach_port_t session;
115 };
116
117 protected:
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);
123
124 public:
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);
129
130 static void processSystemSleep();
131 void processLockAll();
132
133 RefPointer<AuthHostInstance> authhost(const AuthHostType hostType = securityAgent, const bool restart = false);
134
135 protected:
136 Bootstrap mBootstrap; // session bootstrap port
137 Port mServicePort; // SecurityServer service port for this session
138 SessionAttributeBits mAttributes; // attribute bits (see AuthSession.h)
139
140 mutable Mutex mCredsLock; // lock for mSessionCreds
141 CredentialSet mSessionCreds; // shared session authorization credentials
142
143 mutable Mutex mAuthHostLock;
144 AuthHostInstance *mSecurityAgent;
145 AuthHostInstance *mAuthHost;
146
147 CFRef<CFDataRef> mSessionAgentPrefs;
148 Credential mOriginatorCredential;
149
150 void kill();
151
152 protected:
153 static PortMap<Session> mSessions;
154 };
155
156 template <class SessionType>
157 SessionType &Session::find(SecuritySessionId id)
158 {
159 if (SessionType *ssn = dynamic_cast<SessionType *>(&find(id)))
160 return *ssn;
161 else
162 MacOSError::throwMe(errSessionInvalidId);
163 }
164
165
166 //
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
169 // immortal.
170 // Currently, telnet sessions et al also default into this session, but this will change
171 // (we hope).
172 //
173 class RootSession : public Session {
174 public:
175 RootSession(Server &server, SessionAttributeBits attrs = 0);
176
177 bool haveOriginatorUid() const { return true; }
178 uid_t originatorUid() const { return 0; }
179 CFDataRef copyUserPrefs() { return NULL; }
180 };
181
182
183 //
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).
188 //
189 class DynamicSession : private ReceivePort, public Session {
190 public:
191 DynamicSession(TaskPort taskPort);
192 ~DynamicSession();
193
194 void setupAttributes(SessionCreationFlags flags, SessionAttributeBits attrs);
195
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();
201
202 protected:
203 void checkOriginator(); // fail unless current process is originator
204 void kill(); // augment parent's kill
205
206 private:
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
210 };
211
212
213 #endif //_H_SESSION