2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
28 // server - the actual Server object
33 #include "securityserver.h"
34 #include "structure.h"
35 #include <security_utilities/machserver.h>
36 #include <security_utilities/powerwatch.h>
37 #include <security_cdsa_client/cssmclient.h>
38 #include <security_cdsa_client/cspclient.h>
39 #include <security_cdsa_client/osxsigner.h>
40 #include <security_utilities/devrandom.h>
41 #include <security_cdsa_utilities/uniformrandom.h>
42 #include "codesigdb.h"
43 #include "connection.h"
46 #include "localdatabase.h"
47 #include "kcdatabase.h"
48 #include "authority.h"
49 #include "AuthorizationEngine.h"
52 #define EQUIVALENCEDBPATH "/var/db/CodeEquivalenceDatabase"
55 // The authority itself. You will usually only have one of these.
57 class Authority
: public Authorization::Engine
{
59 Authority(const char *configFile
);
63 class Server
: public PerGlobal
,
64 public MachPlusPlus::MachServer
,
65 public UniformRandomBlobs
<DevRandomGenerator
> {
67 Server(Authority
&myAuthority
, CodeSignatures
&signatures
, const char *bootstrapName
);
70 // run the server until it shuts down
74 // Retrieve pieces of the Server's object web.
75 // These are all static methods that use the active() Server of this thread.
77 static Server
&active() { return safer_cast
<Server
&>(MachServer::active()); }
78 static const char *bootstrapName() { return active().mBootstrapName
.c_str(); }
80 static Connection
&connection(mach_port_t replyPort
);
81 static Connection
&connection(bool tolerant
= false);
82 static void requestComplete();
84 static Process
&process();
85 static Session
&session();
87 static RefPointer
<Key
> key(KeyHandle key
);
88 static RefPointer
<Key
> optionalKey(KeyHandle k
) { return (k
== noKey
) ? NULL
: key(k
); }
89 static RefPointer
<Database
> database(DbHandle db
);
90 static RefPointer
<KeychainDatabase
> keychain(DbHandle db
);
91 static RefPointer
<Database
> optionalDatabase(DbHandle db
);
92 static Authority
&authority() { return active().mAuthority
; }
93 static CodeSignatures
&codeSignatures() { return active().mCodeSignatures
; }
94 static SecurityServerAcl
&aclBearer(AclKind kind
, CSSM_HANDLE handle
);
95 static CssmClient::CSP
&csp() { return active().getCsp(); }
100 // set up a new connection
106 void setupConnection(ConnectLevel type
, Port servicePort
, Port replyPort
, Port taskPort
,
107 const security_token_t
&securityToken
,
108 const ClientSetupInfo
*info
= NULL
, const char *executablePath
= NULL
);
110 void endConnection(Port replyPort
);
112 static void releaseWhenDone(Allocator
&alloc
, void *memory
)
113 { MachServer::active().releaseWhenDone(alloc
, memory
); }
114 static void releaseWhenDone(void *memory
)
115 { releaseWhenDone(Allocator::standard(), memory
); }
118 // implementation methods of MachServer
119 boolean_t
handle(mach_msg_header_t
*in
, mach_msg_header_t
*out
);
120 void notifyDeadName(Port port
);
121 void notifyNoSenders(Port port
, mach_port_mscount_t
);
124 class SleepWatcher
: public MachPlusPlus::PortPowerWatcher
{
126 void systemWillSleep();
128 SleepWatcher sleepWatcher
;
131 // mach bootstrap registration name
132 std::string mBootstrapName
;
134 // connection map (by client reply port)
135 PortMap
<Connection
> mConnections
;
137 // process map (by process task port)
138 PortMap
<Process
> mProcesses
;
140 // Current connection, if any (per thread).
141 // Set as a side effect of calling connection(mach_port_t)
142 // and returned by connection(bool).
143 ThreadNexus
<RefPointer
<Connection
> > mCurrentConnection
;
146 CssmClient::Cssm mCssm
; // CSSM instance
147 CssmClient::Module mCSPModule
; // CSP module
148 CssmClient::CSP mCSP
; // CSP attachment
149 CssmClient::CSP
&getCsp(); // lazily initialize, then return CSP attachment
151 Authority
&mAuthority
;
152 CodeSignatures
&mCodeSignatures
;