2 * Copyright (c) 2006-2007 Apple 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@
24 // clientid - track and manage identity of securityd clients
28 #include "osxcodewrap.h"
32 // Constructing a ClientIdentification doesn't do much.
33 // We're waiting for setup(), which should be called by the child class's
36 ClientIdentification::ClientIdentification()
42 // Initialize the ClientIdentification.
43 // This creates a process-level code object for the client.
45 void ClientIdentification::setup(pid_t pid
)
47 if (IFDEBUG(OSStatus rc
=)SecCodeCreateWithPID(pid
, kSecCSDefaultFlags
,
48 &mClientProcess
.aref()))
49 secdebug("clientid", "could not get code for process %d: OSStatus=%ld",
55 // Return a SecCodeRef for the client process itself, regardless of
56 // which guest within it is currently active.
57 // Think twice before using this.
59 SecCodeRef
ClientIdentification::processCode() const
61 return mClientProcess
;
66 // Return a SecCodeRef for the currently active guest within the client
69 // We make a fair effort to cache client guest identities without over-growing
70 // the cache. Note that there's currently no protocol for being notified of
71 // a guest's death or disappearance (independent from the host process's death),
72 // so we couldn't track guests live even if we tried.
74 // Note that this consults Server::connection for the currently serviced
75 // Connection object, so this is not entirely a function of ClientIdentification state.
77 SecCodeRef
ClientIdentification::currentGuest() const
79 if (GuestState
*guest
= current())
82 return mClientProcess
;
85 ClientIdentification::GuestState
*ClientIdentification::current() const
87 // if we have no client identification, we can't find a current guest either
91 SecGuestRef guestRef
= Server::connection().guestRef();
93 // try to deliver an already-cached entry
95 StLock
<Mutex
> _(mLock
);
96 GuestMap::iterator it
= mGuests
.find(guestRef
);
97 if (it
!= mGuests
.end())
101 // okay, make a new one (this may take a while)
102 CFRef
<CFDictionaryRef
> attributes
= (guestRef
== kSecNoGuest
)
104 : makeCFDictionary(1, kSecGuestAttributeCanonical
, CFTempNumber(guestRef
).get());
105 Server::active().longTermActivity();
106 CFRef
<SecCodeRef
> code
;
107 switch (OSStatus rc
= SecCodeCopyGuestWithAttributes(processCode(),
108 attributes
, kSecCSDefaultFlags
, &code
.aref())) {
111 case errSecCSUnsigned
: // not signed; clearly not a host
112 case errSecCSNotAHost
: // signed but not marked as a (potential) host
113 code
= mClientProcess
;
115 case errSecCSNoSuchCode
: // potential host, but...
116 if (guestRef
== kSecNoGuest
) { // ... no guests (yet), so return the process
117 code
= mClientProcess
;
120 // else fall through // ... the guest we expected to be there isn't
122 MacOSError::throwMe(rc
);
124 StLock
<Mutex
> _(mLock
);
125 GuestState
&slot
= mGuests
[guestRef
];
126 if (!slot
.code
) // if another thread didn't get here first...
133 // Support for the legacy hash identification mechanism.
134 // The legacy machinery deals exclusively in terms of processes.
135 // It knows nothing about guests and their identities.
137 string
ClientIdentification::getPath() const
139 assert(mClientProcess
);
140 return codePath(currentGuest());
143 const CssmData
ClientIdentification::getHash() const
145 if (GuestState
*guest
= current()) {
146 if (!guest
->gotHash
) {
147 RefPointer
<OSXCode
> clientCode
= new OSXCodeWrap(guest
->code
);
148 OSXVerifier::makeLegacyHash(clientCode
, guest
->legacyHash
);
149 guest
->gotHash
= true;
151 return CssmData::wrap(guest
->legacyHash
, SHA1::digestLength
);
158 // Bonus function: get the path out of a SecCodeRef
160 std::string
codePath(SecStaticCodeRef code
)
162 CFRef
<CFURLRef
> path
;
163 MacOSError::check(SecCodeCopyPath(code
, kSecCSDefaultFlags
, &path
.aref()));
164 return cfString(path
);
169 // Debug dump support
171 #if defined(DEBUGDUMP)
173 static void dumpCode(SecCodeRef code
)
175 CFRef
<CFURLRef
> path
;
176 if (OSStatus rc
= SecCodeCopyPath(code
, kSecCSDefaultFlags
, &path
.aref()))
177 Debug::dump("unknown(rc=%ld)", rc
);
179 Debug::dump("%s", cfString(path
).c_str());
182 void ClientIdentification::dump()
184 Debug::dump(" client=");
185 dumpCode(mClientProcess
);
186 for (GuestMap::const_iterator it
= mGuests
.begin(); it
!= mGuests
.end(); ++it
) {
187 Debug::dump(" guest(0x%x)=", it
->first
);
188 dumpCode(it
->second
.code
);
189 if (it
->second
.gotHash
)
190 Debug::dump(" [got hash]");