]> git.saurik.com Git - apple/security.git/blob - SecurityServer/server.h
Security-163.tar.gz
[apple/security.git] / SecurityServer / server.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // server - the actual Server object
21 //
22 #ifndef _H_SERVER
23 #define _H_SERVER
24
25 #include "securityserver.h"
26 #include <Security/machserver.h>
27 #include <Security/powerwatch.h>
28 #include <Security/cssmclient.h>
29 #include <Security/cspclient.h>
30 #include <Security/osxsigner.h>
31 #include <Security/devrandom.h>
32 #include <Security/uniformrandom.h>
33 #include "codesigdb.h"
34 #include "connection.h"
35 #include "key.h"
36 #include "xdatabase.h"
37 #include "authority.h"
38 #include <map>
39
40 #define EQUIVALENCEDBPATH "/var/db/CodeEquivalenceDatabase"
41
42
43 class Server : public MachPlusPlus::MachServer,
44 public UniformRandomBlobs<DevRandomGenerator> {
45 public:
46 Server(Authority &myAuthority, CodeSignatures &signatures, const char *bootstrapName);
47 ~Server();
48
49 // run the server until it shuts down
50 void run();
51
52 //
53 // Retrieve pieces of the Server's object web.
54 // These are all static methods that use the active() Server of this thread.
55 //
56 static Server &active() { return safer_cast<Server &>(MachServer::active()); }
57 static const char *bootstrapName() { return active().mBootstrapName.c_str(); }
58
59 static Connection &connection(mach_port_t replyPort);
60 static Connection &connection(bool tolerant = false);
61 static void requestComplete();
62
63 static Key &key(KeyHandle key)
64 { return findHandle<Key>(key, CSSMERR_CSP_INVALID_KEY_REFERENCE); }
65 static Key *optionalKey(KeyHandle k) { return (k == noKey) ? NULL : &key(k); }
66 static Database &database(DbHandle db)
67 { return findHandle<Database>(db, CSSMERR_DL_INVALID_DB_HANDLE); }
68 static Database *optionalDatabase(DbHandle db) { return db ? &database(db) : NULL; }
69 static Authority &authority() { return active().mAuthority; }
70 static CodeSignatures &codeSignatures() { return active().mCodeSignatures; }
71 static SecurityServerAcl &aclBearer(AclKind kind, CSSM_HANDLE handle);
72 static CssmClient::CSP &csp() { return active().getCsp(); }
73
74 void loadCssm();
75
76 public:
77 // set up a new connection
78 enum ConnectLevel {
79 connectNewSession,
80 connectNewProcess,
81 connectNewThread
82 };
83 void setupConnection(ConnectLevel type, Port servicePort, Port replyPort, Port taskPort,
84 const security_token_t &securityToken,
85 const ClientSetupInfo *info = NULL, const char *executablePath = NULL);
86
87 void endConnection(Port replyPort);
88
89 static void releaseWhenDone(CssmAllocator &alloc, void *memory)
90 { MachServer::active().releaseWhenDone(alloc, memory); }
91 static void releaseWhenDone(void *memory)
92 { releaseWhenDone(CssmAllocator::standard(), memory); }
93
94 protected:
95 // implementation methods of MachServer
96 boolean_t handle(mach_msg_header_t *in, mach_msg_header_t *out);
97 void notifyDeadName(Port port);
98 void notifyNoSenders(Port port, mach_port_mscount_t);
99
100 private:
101 class SleepWatcher : public MachPlusPlus::PortPowerWatcher {
102 public:
103 void systemWillSleep();
104 };
105 SleepWatcher sleepWatcher;
106
107 private:
108 Mutex lock; // master lock
109
110 // mach bootstrap registration name
111 std::string mBootstrapName;
112
113 // map of connections (by client reply port)
114 typedef map<mach_port_t, Connection *> ConnectionMap;
115 ConnectionMap connections;
116
117 // map of processes (by process task port)
118 typedef map<mach_port_t, Process *> ProcessMap;
119 ProcessMap processes;
120
121 // Current connection, if any (per thread).
122 // Set as a side effect of calling connection(mach_port_t)
123 // and returned by connection(bool).
124 PerThreadPointer<Connection> mCurrentConnection;
125
126 // CSSM components
127 CssmClient::Cssm mCssm; // CSSM instance
128 CssmClient::Module mCSPModule; // CSP module
129 CssmClient::CSP mCSP; // CSP attachment
130 CssmClient::CSP &getCsp(); // lazily initialize, then return CSP attachment
131
132 Authority &mAuthority;
133 CodeSignatures &mCodeSignatures;
134 };
135
136 #endif //_H_SERVER