#include "sstransit.h"
#include <servers/netname.h>
#include <Security/debugging.h>
using MachPlusPlus::check;
using MachPlusPlus::Bootstrap;
using CodeSigning::OSXCode;
namespace Security {
namespace SecurityServer {
ClientSession::DidChangeKeyAclCallback *ClientSession::mCallback = NULL;
void *ClientSession::mCallbackContext = NULL;
UnixPlusPlus::StaticForkMonitor ClientSession::mHasForked;
ModuleNexus<ClientSession::Global> ClientSession::mGlobal;
bool ClientSession::mSetupSession;
const char *ClientSession::mContactName;
ClientSession::ClientSession(CssmAllocator &std, CssmAllocator &rtn)
: internalAllocator(std), returnAllocator(rtn)
{ }
ClientSession::~ClientSession()
{ }
void
ClientSession::registerForAclEdits(DidChangeKeyAclCallback *callback, void *context)
{
mCallback = callback;
mCallbackContext = context;
}
void ClientSession::activate()
{
if (mHasForked()) {
secdebug("SSclnt", "process has forked (now pid=%d) - resetting connection object", getpid());
mGlobal.reset();
}
Global &global = mGlobal();
Thread &thread = global.thread();
if (!thread) {
IPCN(ucsp_client_setupThread(UCSP_ARGS, mach_task_self()));
thread.registered = true;
global.serverPort.requestNotify(thread.replyPort, MACH_NOTIFY_DEAD_NAME, true);
secdebug("SSclnt", "Thread registered with %s", mContactName);
}
}
void ClientSession::contactName(const char *name)
{
mContactName = name;
}
const char *ClientSession::contactName() const
{
return mContactName;
}
ClientSession::Global::Global()
{
IFDEBUG(if (!mContactName) mContactName = getenv(SECURITYSERVER_BOOTSTRAP_ENV));
if (!mContactName)
mContactName = SECURITYSERVER_BOOTSTRAP_NAME;
secdebug("SSclnt", "Locating %s", mContactName);
serverPort = Bootstrap().lookup(mContactName);
secdebug("SSclnt", "contacting %s at port %d", mContactName, serverPort.port());
string extForm;
try {
myself = OSXCode::main();
extForm = myself->encode();
secdebug("SSclnt", "my OSXCode extForm=%s", extForm.c_str());
} catch (...) {
secdebug("SSclnt", "failed to obtain my own OSXCode");
}
ClientSetupInfo info = { SSPROTOVERSION };
Thread &thread = this->thread();
if (mSetupSession) {
secdebug("SSclnt", "sending session setup request");
mSetupSession = false;
IPCN(ucsp_client_setupNew(serverPort, thread.replyPort, &rcode,
mach_task_self(), info, extForm.c_str(), &serverPort.port()));
secdebug("SSclnt", "new session server port is %d", serverPort.port());
} else {
IPCN(ucsp_client_setup(serverPort, thread.replyPort, &rcode,
mach_task_self(), info, extForm.c_str()));
}
thread.registered = true; serverPort.requestNotify(thread.replyPort, MACH_NOTIFY_DEAD_NAME, true);
secdebug("SSclnt", "contact with %s established", mContactName);
}
void ClientSession::terminate()
{
secdebug("SSclnt", "ClientSession::terminate() call ignored");
}
void ClientSession::addApplicationAclSubject(KeyHandle key, CSSM_ACL_AUTHORIZATION_TAG tag)
{
if (mCallback && mCallbackContext)
{
secdebug("keyacl", "ClientSession::addApplicationAclSubject(keyHandle: %lu tag: %lu)", key, tag);
mCallback(mCallbackContext, *this, key, tag);
}
else
secdebug("keyacl", "ClientSession::addApplicationAclSubject() with NULL mCallback");
}
} }