📄 pgpkeyserver.cpp
字号:
/*____________________________________________________________________________
Copyright (C) 1997 Network Associates Inc. and affiliated companies.
All rights reserved.
$Id: pgpKeyServer.cpp,v 1.42.6.2 1999/06/04 02:14:40 heller Exp $
____________________________________________________________________________*/
#include "pgpContext.h"
#include "pgpEncodePriv.h"
#include "pgpFileIO.h"
#include "pgpFileUtilities.h"
#include "pgpKeyServer.h"
#include "pgpMem.h"
#include "pgpOptionListPriv.h"
#include "pgpSockets.h"
#include "pgpSDKNetworkLibPriv.h"
#include "CKeyServer.h"
#if PGP_WIN32 || PGP_UNIX
#include "pgpRMWOLock.h"
static PGPRMWOLock sBusyLock;
static PGPRMWOLock sCancelLock;
#endif
#define elemsof(x) ((unsigned)(sizeof(x)/sizeof(*x)))
static PGPInt32 sNumberOfInits = 0;
class StKeyServerBusy {
public:
StKeyServerBusy(CKeyServer * inKeyServer,
PGPBoolean inClosing = false);
virtual ~StKeyServerBusy();
protected:
CKeyServer * mKeyServer;
PGPBoolean mClosing;
};
StKeyServerBusy::StKeyServerBusy(
CKeyServer * inKeyServer,
PGPBoolean inClosing)
: mKeyServer(inKeyServer), mClosing(inClosing)
{
#if PGP_WIN32 || PGP_UNIX
PGPRMWOLockStartWriting(&sBusyLock);
PGPRMWOLockStartReading(&sCancelLock);
#endif
if (mKeyServer->IsBusy() || (! mClosing && mKeyServer->IsCanceled()))
{
#if PGP_WIN32 || PGP_UNIX
PGPRMWOLockStopReading(&sCancelLock);
PGPRMWOLockStopWriting(&sBusyLock);
#endif
ThrowPGPError_(kPGPError_ServerInProgress);
} else {
mKeyServer->SetBusy(true);
}
#if PGP_WIN32 || PGP_UNIX
PGPRMWOLockStopReading(&sCancelLock);
PGPRMWOLockStopWriting(&sBusyLock);
#endif
}
StKeyServerBusy::~StKeyServerBusy()
{
if (mClosing) {
#if PGP_WIN32 || PGP_UNIX
PGPRMWOLockStartWriting(&sCancelLock);
#endif
mKeyServer->ResetCanceled();
#if PGP_WIN32 || PGP_UNIX
PGPRMWOLockStopWriting(&sCancelLock);
#endif
}
#if PGP_WIN32 || PGP_UNIX
PGPRMWOLockStartWriting(&sBusyLock);
#endif
mKeyServer->SetBusy(false);
#if PGP_WIN32 || PGP_UNIX
PGPRMWOLockStopWriting(&sBusyLock);
#endif
}
static PGPError
MapErrors(
PGPError inError)
{
PGPError result = inError;
switch (inError) {
case kPGPError_SocketsInProgress:
{
result = kPGPError_ServerInProgress;
}
break;
}
return result;
}
PGPError
PGPSetKeyServerIdleEventHandler(
PGPEventHandlerProcPtr inCallback,
PGPUserValue inUserData)
{
PGPError result = kPGPError_NoErr;
try {
CKeyServer::SetIdleEventHandler(inCallback, inUserData);
}
catch (PGPError exception) {
result = MapErrors(exception);
}
#if ! PGP_WIN32
catch (...) {
result = kPGPError_UnknownError;
}
#endif
return result;
}
PGPError
PGPGetKeyServerIdleEventHandler(
PGPEventHandlerProcPtr * outCallback,
PGPUserValue * outUserData)
{
PGPError result = kPGPError_NoErr;
try {
CKeyServer::GetIdleEventHandler(outCallback, outUserData);
}
catch (PGPError exception) {
result = MapErrors(exception);
}
#if ! PGP_WIN32
catch (...) {
result = kPGPError_UnknownError;
}
#endif
return result;
}
PGPError
PGPKeyServerCreateThreadStorage(
PGPKeyServerThreadStorageRef * outPreviousStorage)
{
PGPError err = kPGPError_NoErr;
try {
if (outPreviousStorage == 0) {
ThrowPGPError_(kPGPError_BadParams);
}
*outPreviousStorage = kInvalidPGPKeyServerThreadStorageRef;
CKeyServer::CreateThreadStorage(
(SThreadContext **) outPreviousStorage);
}
catch (PGPError exception) {
err = MapErrors(exception);
}
#if ! PGP_WIN32
catch (...) {
err = kPGPError_UnknownError;
}
#endif
return err;
}
PGPError
PGPKeyServerDisposeThreadStorage(
PGPKeyServerThreadStorageRef inPreviousStorage)
{
PGPError err = kPGPError_NoErr;
try {
CKeyServer::DisposeThreadStorage(
(SThreadContext *) inPreviousStorage);
}
catch (PGPError exception) {
err = MapErrors(exception);
}
#if ! PGP_WIN32
catch (...) {
err = kPGPError_UnknownError;
}
#endif
return err;
}
PGPError
PGPKeyServerInit()
{
PGPError result = kPGPError_NoErr;
#if PGP_WIN32
EnterCriticalSection(&gKeyServerInitMutex);
#endif
if (sNumberOfInits == 0) {
try {
CKeyServer::Initialize();
}
catch (PGPError exception) {
result = MapErrors(exception);
}
catch (...) {
result = kPGPError_UnknownError;
#if PGP_WIN32
LeaveCriticalSection(&gKeyServerInitMutex);
throw;
#endif
}
#if PGP_WIN32 || PGP_UNIX
if (IsntPGPError(result)) {
InitializePGPRMWOLock(&sBusyLock);
InitializePGPRMWOLock(&sCancelLock);
}
#endif
}
if (IsntPGPError(result)) {
sNumberOfInits++;
}
#if PGP_WIN32
LeaveCriticalSection(&gKeyServerInitMutex);
#endif
return result;
}
PGPError
PGPKeyServerCleanup()
{
PGPError result = kPGPError_NoErr;
#if PGP_WIN32
EnterCriticalSection(&gKeyServerInitMutex);
#endif
sNumberOfInits--;
if (sNumberOfInits == 0) {
try {
CKeyServer::Cleanup();
}
catch (PGPError exception) {
result = MapErrors(exception);
}
catch (...) {
result = kPGPError_UnknownError;
#if PGP_WIN32
LeaveCriticalSection(&gKeyServerInitMutex);
throw;
#endif
}
#if PGP_WIN32 || PGP_UNIX
DeletePGPRMWOLock(&sCancelLock);
DeletePGPRMWOLock(&sBusyLock);
#endif
} else if (sNumberOfInits < 0) {
sNumberOfInits = 0;
}
#if PGP_WIN32
LeaveCriticalSection(&gKeyServerInitMutex);
#endif
return result;
}
PGPError
PGPNewKeyServerFromURL(
PGPContextRef inContext,
const char * inURL,
PGPKeyServerAccessType inAccessType,
PGPKeyServerKeySpace inKeySpace,
PGPKeyServerRef * outKeyServerRef)
{
PGPError result = kPGPError_NoErr;
try {
// Validation
PGPValidatePtr(outKeyServerRef);
*outKeyServerRef = kInvalidPGPKeyServerRef;
PGPValidateContext(inContext);
PGPValidatePtr(inURL);
PGPValidateParam((inAccessType >= kPGPKeyServerAccessType_Default) &&
(inAccessType <= kPGPKeyServerAccessType_Administrator));
PGPValidateParam((inKeySpace >= kPGPKeyServerKeySpace_Default) &&
(inKeySpace <= kPGPKeyServerKeySpace_Pending));
*outKeyServerRef = (PGPKeyServerRef) CKeyServer::NewKeyServerFromURL(
inContext,
inURL,
inAccessType,
inKeySpace);
}
catch (PGPError exception) {
result = MapErrors(exception);
}
#if !PGP_WIN32
catch (...) {
result = kPGPError_UnknownError;
}
#endif
return result;
}
PGPError
PGPNewKeyServerFromHostAddress(
PGPContextRef inContext,
PGPUInt32 inAddress,
PGPUInt16 inPort,
PGPKeyServerProtocol inType,
PGPKeyServerAccessType inAccessType,
PGPKeyServerKeySpace inKeySpace,
PGPKeyServerRef * outKeyServerRef)
{
PGPError result = kPGPError_NoErr;
try {
// Validation
PGPValidatePtr(outKeyServerRef);
*outKeyServerRef = kInvalidPGPKeyServerRef;
PGPValidateContext(inContext);
PGPValidateParam((inType >= kPGPKeyServerProtocol_LDAP) &&
(inType <= kPGPKeyServerProtocol_HTTPS));
PGPValidateParam((inAccessType >= kPGPKeyServerAccessType_Default) &&
(inAccessType <= kPGPKeyServerAccessType_Administrator));
PGPValidateParam((inKeySpace >= kPGPKeyServerKeySpace_Default) &&
(inKeySpace <= kPGPKeyServerKeySpace_Pending));
*outKeyServerRef =
(PGPKeyServerRef) CKeyServer::NewKeyServerFromHostAddress(
inContext,
inAddress,
inPort,
inType,
inAccessType,
inKeySpace);
}
catch (PGPError exception) {
result = MapErrors(exception);
}
#if !PGP_WIN32
catch (...) {
result = kPGPError_UnknownError;
}
#endif
return result;
}
PGPError
PGPNewKeyServerFromHostName(
PGPContextRef inContext,
const char * inHostName,
PGPUInt16 inPort,
PGPKeyServerProtocol inType,
PGPKeyServerAccessType inAccessType,
PGPKeyServerKeySpace inKeySpace,
PGPKeyServerRef * outKeyServerRef)
{
PGPError result = kPGPError_NoErr;
try {
// Validation
PGPValidatePtr(outKeyServerRef);
*outKeyServerRef = kInvalidPGPKeyServerRef;
PGPValidateContext(inContext);
PGPValidatePtr(inHostName);
PGPValidateParam((inType >= kPGPKeyServerProtocol_LDAP) &&
(inType <= kPGPKeyServerProtocol_HTTPS));
PGPValidateParam((inAccessType >= kPGPKeyServerAccessType_Default) &&
(inAccessType <= kPGPKeyServerAccessType_Administrator));
PGPValidateParam((inKeySpace >= kPGPKeyServerKeySpace_Default) &&
(inKeySpace <= kPGPKeyServerKeySpace_Pending));
*outKeyServerRef =
(PGPKeyServerRef) CKeyServer::NewKeyServerFromHostName(
inContext,
inHostName,
inPort,
0,
inType,
inAccessType,
inKeySpace);
}
catch (PGPError exception) {
result = MapErrors(exception);
}
#if !PGP_WIN32
catch (...) {
result = kPGPError_UnknownError;
}
#endif
return result;
}
typedef struct KeyServerOptions
{
PGPKeyServerProtocol protocol;
PGPKeyServerKeySpace keySpace;
PGPKeyServerAccessType accessType;
const char *url;
const char *hostName;
PGPUInt32 hostAddress;
PGPUInt16 hostPort;
} KeyServerOptions;
static PGPError
GatherKeyServerOptions(
PGPOptionListRef optionList,
KeyServerOptions *options)
{
PGPError err = kPGPError_NoErr;
PGPUInt32 numAddressOptions = 0;
PGPInt32 tempInt;
PGPBoolean haveOption;
pgpAssert( pgpOptionListIsValid( optionList ) );
pgpAssert( IsntNull( options ) );
pgpClearMemory( options, sizeof( *options ) );
options->protocol = kPGPKeyServerProtocol_LDAP;
options->keySpace = kPGPKeyServerKeySpace_Default;
options->accessType = kPGPKeyServerAccessType_Default;
err = pgpFindOptionArgs( optionList,
kPGPOptionType_KeyServerProtocol, FALSE, "%b%d",
&haveOption, &tempInt );
if( haveOption )
{
options->protocol = (PGPKeyServerProtocol) tempInt;
}
if( IsntPGPError( err ) )
{
err = pgpFindOptionArgs( optionList,
kPGPOptionType_KeyServerKeySpace, FALSE, "%b%d",
&haveOption, &tempInt );
if( haveOption )
{
options->keySpace = (PGPKeyServerKeySpace) tempInt;
}
}
if( IsntPGPError( err ) )
{
err = pgpFindOptionArgs( optionList,
kPGPOptionType_KeyServerAccessType, FALSE, "%b%d",
&haveOption, &tempInt );
if( haveOption )
{
options->accessType = (PGPKeyServerAccessType) tempInt;
}
}
if( IsntPGPError( err ) )
{
err = pgpFindOptionArgs( optionList,
kPGPOptionType_URL, FALSE, "%p", &options->url );
if( IsntNull( options->url ) )
{
++numAddressOptions;
}
}
if( IsntPGPError( err ) )
{
PGPONetHostNameDesc *desc = 0;
err = pgpFindOptionArgs( optionList,
kPGPOptionType_HostName, FALSE, "%p", &desc );
if( IsntNull( desc ) )
{
options->hostName = desc->hostName;
options->hostPort = desc->port;
++numAddressOptions;
}
}
if( IsntPGPError( err ) )
{
PGPONetHostAddressDesc *desc = 0;
err = pgpFindOptionArgs( optionList,
kPGPOptionType_HostAddress, FALSE, "%p", &desc );
if( IsntNull( desc ) )
{
options->hostAddress = desc->hostAddress;
options->hostPort = desc->port;
++numAddressOptions;
}
}
if( IsntPGPError( err ) && numAddressOptions != 1 )
{
pgpDebugMsg( "PGPNewKeyServer: Invalid host specification" );
err = kPGPError_BadParams;
}
return err;
}
static PGPError
pgpNewX509KeyServer(
PGPContextRef context,
PGPKeyServerClass serverClass,
KeyServerOptions * options,
PGPKeyServerRef *outKeyServerRef)
{
PGPError result = kPGPError_NoErr;
try {
if (options->url != 0) {
*outKeyServerRef = (PGPKeyServerRef) CKeyServer::NewKeyServerFromURL(
context,
options->url,
options->accessType,
options->keySpace,
serverClass);
} else if (options->hostName != 0) {
*outKeyServerRef = (PGPKeyServerRef) CKeyServer::NewKeyServerFromHostName(
context,
options->hostName,
options->hostPort,
0,
options->protocol,
options->accessType,
options->keySpace,
serverClass);
} else {
*outKeyServerRef = (PGPKeyServerRef) CKeyServer::NewKeyServerFromHostAddress(
context,
options->hostAddress,
options->hostPort,
options->protocol,
options->accessType,
options->keySpace,
serverClass);
}
}
catch (PGPError exception) {
result = MapErrors(exception);
}
#if !PGP_WIN32
catch (...) {
result = kPGPError_UnknownError;
}
#endif
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -