📄 pgpldap.c
字号:
singleHost[j] = '\0';
/* Connect to server */
err = pgpLDAPConnect( pgpLDAP, singleHost, port );
if( IsPGPError( err ) )
continue;
else
connected = TRUE;
}
pgpLDAP->result = kPGPldapResult_Success;
goto done;
error:
done:
if( IsntNull( singleHost ) )
(void) PGPFreeData( singleHost );
return err;
}
PGPError
PGPldapSimpleBindSync(
PGPldapContextRef pgpLDAP,
char * dn,
char * password )
{
PGPError err = kPGPError_NoErr;
PGPldapMessageID messageID = kInvalidPGPldapMessageID;
PGPldapType messageType = kPGPldapType_None;
PGPldapMessageRef ldapMessage = kInvalidPGPldapMessageRef;
PGPValidateLDAPContextRef( pgpLDAP );
err = PGPldapSimpleBind (pgpLDAP, dn, password, &messageID ); CKERR;
err = PGPNewLDAPMessage( pgpLDAP, &ldapMessage ); CKERR;
err = PGPldapGetResult( pgpLDAP, messageID, FALSE, NULL, ldapMessage,
&messageType ); CKERR;
error:
if( PGPldapMessageRefIsValid( ldapMessage ) )
(void) PGPFreeLDAPMessage( ldapMessage );
return( err ? err : PGPldapResultToError( pgpLDAP, pgpLDAP->result ) );
}
PGPError
PGPldapSimpleBind(
PGPldapContextRef pgpLDAP,
char * dn,
char * password,
PGPldapMessageID * messageID )
{
PGPberElementRef ber = kInvalidPGPberElementRef;
PGPError err = kPGPError_NoErr;
PGPValidatePtr( pgpLDAP );
PGPValidatePtr( messageID );
if( IsNull( dn ) )
dn = (char *) "";
if( IsNull( password ) )
password = (char *) "";
err = pgpNewBERElement( pgpLDAP->memMgr, &ber ); CKERR;
err = PGPberAppend( ber, "{it{ists}}",
++( pgpLDAP->nextMessageID ),
kPGPldapRequest_Bind,
kPGPldap_DefaultVersion,
dn,
kPGPldapAuth_Simple,
password ); CKERR;
err = pgpLDAPSend( pgpLDAP, ber ); CKERR;
*messageID = pgpLDAP->nextMessageID;
error:
if( PGPberElementRefIsValid( ber ) )
(void) PGPFreeBERElement( ber );
return err;
}
PGPError
PGPldapBindSync(
PGPldapContextRef pgpLDAP,
char * dn,
char * password,
PGPldapAuth method )
{
PGPValidateLDAPContextRef( pgpLDAP );
/* We only support simple authentication */
if( method != kPGPldapAuth_Simple )
return kPGPldapResult_StrongAuthNotSupported;
return PGPldapSimpleBindSync( pgpLDAP, dn, password );
}
PGPError
PGPldapBind(
PGPldapContextRef pgpLDAP,
char * dn,
char * password,
PGPldapAuth method,
PGPldapMessageID * messageID )
{
PGPValidateLDAPContextRef( pgpLDAP );
PGPValidatePtr( messageID );
/* We only support simple authentication */
if( method != kPGPldapAuth_Simple )
return kPGPldapResult_StrongAuthNotSupported;
return PGPldapSimpleBind( pgpLDAP, dn, password, messageID );
}
PGPError
PGPldapUnbind(
PGPldapContextRef pgpLDAP )
{
PGPberElementRef ber = kInvalidPGPberElementRef;
PGPError err = kPGPError_NoErr;
PGPValidateLDAPContextRef( pgpLDAP );
err = pgpNewBERElement( pgpLDAP->memMgr, &ber); CKERR;
err = PGPberAppend( ber, "{itn}",
++(pgpLDAP->nextMessageID),
kPGPldapRequest_Unbind ); CKERR;
err = pgpLDAPSend( pgpLDAP, ber ); CKERR;
if( PGPSocketRefIsValid( pgpLDAP->sock ) )
(void) PGPCloseSocket( pgpLDAP->sock );
pgpLDAP->sock = kInvalidPGPSocketRef;
pgpLDAP->result = kPGPldapResult_Success;
goto done;
error:
done:
if( PGPberElementRefIsValid( ber ) )
(void) PGPFreeBERElement( ber );
return err;
}
PGPError
PGPldapSetOption(
PGPldapContextRef pgpLDAP,
PGPUInt32 option,
void * value )
{
PGPValidateLDAPContextRef( pgpLDAP );
switch( option )
{
case kPGPldapOpt_Deref:
pgpLDAP->deref = (PGPldapDeref) value;
break;
case kPGPldapOpt_Sizelimit:
pgpLDAP->sizelimit = (PGPUInt32) value;
break;
case kPGPldapOpt_Timelimit:
pgpLDAP->timelimit = (PGPUInt32) value;
break;
case kPGPldapOpt_Desc:
pgpLDAP->sock = (PGPSocketRef) value;
break;
default:
return kPGPError_BadParams;
}
return kPGPError_NoErr;
}
PGPError
PGPldapGetOption(
PGPldapContextRef pgpLDAP,
PGPUInt32 option,
void * value )
{
PGPValidateLDAPContextRef( pgpLDAP );
PGPValidatePtr( value );
switch( option )
{
case kPGPldapOpt_Deref:
*((PGPldapDeref *) value) = pgpLDAP->deref;
break;
case kPGPldapOpt_Sizelimit:
*((PGPUInt32 *) value) = pgpLDAP->sizelimit;
break;
case kPGPldapOpt_Timelimit:
*((PGPUInt32 *) value) = pgpLDAP->timelimit;
break;
case kPGPldapOpt_Desc:
*((PGPSocketRef *) value) = pgpLDAP->sock;
break;
default:
return kPGPError_BadParams;
}
return kPGPError_NoErr;
}
PGPError
pgpCopyLDAPMessage(
PGPldapMessageRef src,
PGPldapMessageRef dst )
{
PGPValidateLDAPMessageRef( src );
PGPValidateLDAPMessageRef( dst );
dst->messageID = src->messageID;
dst->type = src->type;
/*
* Set this PGPldapMessageRef to point to the _same_
* PGPberElementRef -- it will get freed later when we
* free the PGPldapContextRef.
*/
dst->ber = src->ber;
return kPGPError_NoErr;
}
static PGPError
pgpSeekLastLDAPMessage(
PGPldapMessageRef first,
PGPldapMessageRef * last )
{
PGPValidateLDAPMessageRef( first );
PGPValidatePtr( last );
for( *last = first;
(*last)->next != kInvalidPGPldapMessageRef;
*last = (*last)->next )
; /* NULL */
return kPGPError_NoErr;
}
static PGPError
pgpAddLDAPMessageToLDAPContext(
PGPldapContextRef pgpLDAP,
PGPldapMessageRef message,
PGPberElementRef ber )
{
PGPError err = kPGPError_NoErr;
PGPldapMessageRef lastMessage = kInvalidPGPldapMessageRef;
PGPValidateLDAPContextRef( pgpLDAP );
PGPValidateLDAPMessageRef( message );
PGPValidateBERElementRef( ber );
message->ber = ber;
err = PGPberRead( ber,
"it",
&(message->messageID),
&(message->type) ); CKERR;
/* Add message to pgpLDAP's list of responses */
if( PGPldapMessageRefIsValid( pgpLDAP->response ) )
{
err = pgpSeekLastLDAPMessage( pgpLDAP->response, &lastMessage ); CKERR;
lastMessage->next = message;
message->prev = lastMessage;
}
else
pgpLDAP->response = message;
error:
return err;
}
static PGPError
pgpReadLDAPResponse(
PGPldapContextRef pgpLDAP,
PGPldapMessageRef * message )
{
PGPberElementRef ber = kInvalidPGPberElementRef;
PGPldapMessageRef newMessage = kInvalidPGPldapMessageRef;
PGPError err = kPGPError_NoErr;
PGPValidateLDAPContextRef( pgpLDAP );
PGPValidatePtr( message );
err = pgpNewBERElement( pgpLDAP->memMgr, &ber ); CKERR;
err = PGPberReadResponse( ber, pgpLDAP->sock ); CKERR;
err = PGPNewLDAPMessage( pgpLDAP, &newMessage ); CKERR;
err = pgpAddLDAPMessageToLDAPContext( pgpLDAP, newMessage, ber ); CKERR;
*message = newMessage;
goto done;
error:
if( PGPberElementRefIsValid( ber ) )
(void) PGPFreeBERElement( ber );
ber = kInvalidPGPberElementRef;
if( PGPldapMessageRefIsValid( newMessage ) )
(void) PGPFreeLDAPMessage( newMessage );
newMessage = kInvalidPGPldapMessageRef;
done:
return err;
}
PGPError
pgpLDAPGetMessageErrno(
PGPldapContextRef pgpLDAP,
PGPldapMessageRef message )
{
PGPError err = kPGPError_NoErr;
PGPberElementRef ber = kInvalidPGPberElementRef;
PGPldapType tag = kPGPldapType_None;
PGPSize len = 0;
PGPValidateLDAPContextRef( pgpLDAP );
PGPValidateLDAPMessageRef( message );
ber = message->ber;
if( IsntNull( pgpLDAP->matched ) )
(void) PGPFreeData( pgpLDAP->matched );
pgpLDAP->matched = NULL;
if( IsntNull( pgpLDAP->message ) )
(void) PGPFreeData( pgpLDAP->message );
pgpLDAP->message = NULL;
err = PGPberRewind( ber ); CKERR;
err = PGPberRead( ber, "e", &( pgpLDAP->result ) ); CKERR;
err = PGPberPeek( ber, (PGPberType *)&tag, &len ); CKERR;
pgpLDAP->matched = PGPNewData( pgpLDAP->memMgr, len + 1,
kPGPMemoryMgrFlags_Clear );
if( IsNull( pgpLDAP->matched ) )
{
err = kPGPError_OutOfMemory;
goto error;
}
err = PGPberRead( ber, "s", pgpLDAP->matched ); CKERR;
err = PGPberPeek( ber, (PGPberType *)&tag, &len ); CKERR;
pgpLDAP->message = PGPNewData( pgpLDAP->memMgr, len + 1,
kPGPMemoryMgrFlags_Clear );
if( IsNull( pgpLDAP->message ) )
{
err = kPGPError_OutOfMemory;
goto error;
}
err = PGPberRead( ber, "s", pgpLDAP->message ); CKERR;
goto done;
error:
if( IsntNull( pgpLDAP->matched ) )
(void) PGPFreeData( pgpLDAP->matched );
pgpLDAP->matched = NULL;
if( IsntNull( pgpLDAP->message ) )
(void) PGPFreeData( pgpLDAP->message );
pgpLDAP->message = NULL;
done:
return err;
}
/*
* This is the big LDAP function. This is the function that gets
* responses from an LDAP server and saves them away for later retrieval.
*
* If we are looking for the response of a specific PGPldapMessageID,
* we need to: 1) look through our existing responses for it, or 2)
* wait for it until we timeout.
*
* We also need to get any outstanding responses from the server and
* save them away in the pgpLDAP->responses chain.
*/
PGPError
PGPldapGetResult(
PGPldapContextRef pgpLDAP,
PGPldapMessageID messageID,
PGPBoolean all,
PGPSocketsTimeValue * timeout,
PGPldapMessageRef result,
PGPldapType * messageType )
{
PGPldapMessageRef message = kInvalidPGPldapMessageRef;
PGPldapMessageRef lastResult = kInvalidPGPldapMessageRef;
PGPldapMessageRef prevLastResult = kInvalidPGPldapMessageRef;
PGPSocketSet readSet;
PGPSocketsTimeValue tv = { 0, 0 };
PGPTime startTime = 0;
PGPTime endTime = 0;
PGPBoolean found = FALSE;
PGPInt32 retval = 0;
PGPError err = kPGPError_NoErr;
PGPValidateLDAPContextRef( pgpLDAP );
PGPValidateLDAPMessageRef( result );
PGPValidatePtr( messageType );
lastResult = result;
/*
* Look for any new responses we may have gotten and add them to
* the PGPldapContextRef
*/
PGPSOCKETSET_ZERO( &readSet );
PGPSOCKETSET_SET( pgpLDAP->sock, &readSet );
while( ((retval = PGPSelect( (PGPInt32) pgpLDAP->sock + 1, &readSet, NULL, NULL, &tv ))) )
{
if( retval < 0 )
{
err = PGPGetLastSocketsError();
goto done;
}
err = pgpReadLDAPResponse( pgpLDAP, &message ); CKERR;
}
/* Look in our existing chain of server responses */
for( message = pgpLDAP->response;
message != kInvalidPGPldapMessageRef;
message = message->next )
{
if( ( messageID == message->messageID ) ||
( messageID == kPGPldapMessageID_Any ) )
{
/* Found it */
if( !PGPldapMessageRefIsValid( lastResult ) )
{
/*
* We're returning a chain of PGPldapMessageRefs and
* we've already found one, so we need to make a new
* PGPldapMessageRef and add it to the end of our
* chain of responses.
*/
err = PGPNewLDAPMessage( pgpLDAP, &lastResult ); CKERR;
prevLastResult->next = lastResult;
lastResult->prev = prevLastResult;
}
err = pgpCopyLDAPMessage( message, lastResult ); CKERR;
*messageType = message->type;
if( !all )
{
found = TRUE;
break;
}
if( ( messageID == message->messageID ) ||
( messageID == kPGPldapMessageID_Any ) )
{
if( message->type == kPGPldapResponse_SearchResult )
{
found = TRUE;
break;
}
}
prevLastResult = lastResult;
lastResult = kInvalidPGPldapMessageRef;
}
}
if( !found )
{
/* Wait for the message (and save away any other messages we get) */
if( IsntNull( timeout ) )
tv = *timeout;
/* NULL timeout means to block, tv = { 0, 0 } means check and quit */
while( ( tv.tv_sec >= 0 ) || ( IsNull( timeout ) ) )
{
startTime = PGPGetTime();
PGPSOCKETSET_ZERO( &readSet );
PGPSOCKETSET_SET( pgpLDAP->sock, &readSet );
if ( ((retval = PGPSelect( (PGPInt32) pgpLDAP->sock + 1, &readSet, NULL, NULL,
( IsNull( timeout ) ? NULL : &tv ) ) ) ) )
{
/* Something came in or there was an error */
if( retval < 0 )
{
err = PGPGetLastSocketsError();
goto done;
}
err = pgpReadLDAPResponse( pgpLDAP, &message ); CKERR;
if( ( messageID == message->messageID ) ||
( messageID == kPGPldapMessageID_Any ) )
{
/* Found it */
if( !PGPldapMessageRefIsValid( lastResult ) )
{
/* New PGPldapMessageRef at end of chain */
err = PGPNewLDAPMessage( pgpLDAP, &lastResult ); CKERR;
prevLastResult->next = lastResult;
lastResult->prev = prevLastResult;
}
}
err = pgpCopyLDAPMessage( message, lastResult ); CKERR;
*messageType = message->type;
if( !all )
{
found = TRUE;
break;
}
if( ( messageID == message->messageID ) ||
( messageID == kPGPldapMessageID_Any ) )
{
if( message->type == kPGPldapResponse_SearchResult )
{
found = TRUE;
break;
}
}
prevLastResult = lastResult;
lastResult = kInvalidPGPldapMessageRef;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -