📄 pgprpcbackend.c
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: pgpRPCBackEnd.c,v 1.18 2002/08/06 20:11:13 dallen Exp $
____________________________________________________________________________*/
#include "pgpErrors.h"
#include "pgpRPCBackEndAPI.h"
#include "pgpRPCMsg.h"
#include "pgpMem.h"
#include "pgpThreads.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pwd.h>
#include <strings.h>
#include <sys/wait.h>
static PGPBoolean sRPCEnabled = 0;
static PGPMutex_t sRPCMutex;
#define kPGPsdkServiceName "pgp-agent"
PGPBoolean
pgpRPCEnabled()
{
return sRPCEnabled;
}
#if PGP_UNIX && !PGP_UNIX_DARWIN
/* Use unix domain sockets */
static int sd;
static PGPError
sMakeSockPath(char *sockpath)
{
struct passwd *pw;
pw = getpwuid(getuid());
sprintf(sockpath, "/tmp/.pgp-agent-%s-%d", pw->pw_name, (int) getuid());
return kPGPError_NoErr;
}
PGPError
pgpSDKInitBackEnd(PGPUInt32 backEndAPIVersion)
{
int n;
struct sockaddr_un addr;
PGPError err;
if (backEndAPIVersion != kPGPsdkBackEndAPIVersion)
return kPGPError_ImproperInitialization;
sd = socket(PF_UNIX, SOCK_STREAM, 0);
if (sd == -1)
return kPGPError_ImproperInitialization;
PGPMutexCreate(&sRPCMutex, NULL);
bzero(&addr, sizeof(addr));
addr.sun_family = AF_UNIX;
sMakeSockPath(addr.sun_path);
/* Try to connecto to sdk service */
n = connect(sd, (struct sockaddr *)&addr, sizeof(addr));
if (n == -1)
{
pid_t pid;
int status;
/* sdk service isn't running - start it! */
if ((pid = fork()) == 0)
{
execvp(kPGPsdkServiceName, NULL);
exit(-1);
}
else if (pid == -1)
return kPGPError_ImproperInitialization;
waitpid(pid, &status, 0);
if (WEXITSTATUS(status) != 0)
return kPGPError_ImproperInitialization;
/* sdk service should be running now. Try to connect again */
n = connect(sd, (struct sockaddr *)&addr, sizeof(addr));
if (n == -1)
return kPGPError_ImproperInitialization;
}
err = pgpConnect_backRPC();
if (err != kPGPError_NoErr)
return kPGPError_RPCFailed;
sRPCEnabled = 1;
return kPGPError_NoErr;
}
PGPInt32
pgpRPCSendPacket(PGPPackMsg *pkt, PGPPackMsg *rply_pkt, PGPBoolean okayToBlock)
{
char *reply_buf;
char header_buf[8];
PGPUInt32 reply_len;
PGPInt32 status, length;
int n;
(void) okayToBlock;
PGPMutexLock(&sRPCMutex);
((PGPInt32 *)pkt->base)[2] = pkt->ptr;
n = write(sd, pkt->base, pkt->ptr);
if (n != pkt->ptr) {
printf("pgpRPCSendPacket(): write error\n");
PGPMutexUnlock(&sRPCMutex);
exit(1);
}
/*
* Second 32-bit word in the reply is the length. We need to read
* first the header and length, then allocate the rest of the packet,
* then we can finish reading from the service.
*/
reply_len = 0;
do {
reply_len += read(sd, header_buf + reply_len, sizeof(header_buf) - reply_len );
} while( reply_len < sizeof(header_buf) );
/* Read length from the buffer */
length = *((PGPInt32 *)( header_buf + sizeof( PGPInt32 ) ) );
reply_buf = (char *)PGPNewData(PGPGetDefaultMemoryMgr(), length, 0);
if( reply_buf == NULL )
{
PGPMutexUnlock(&sRPCMutex);
return kPGPError_OutOfMemory;
}
/* Make reply_buf be a flat buffer of all the data */
pgpCopyMemory( header_buf, reply_buf, sizeof( header_buf ) );
/* Now read the rest of the packet */
reply_len = 0;
do
{
reply_len += read(sd,
reply_buf + sizeof( header_buf ) + reply_len,
length - sizeof( header_buf ) - reply_len );
}
while( reply_len < (PGPUInt32) length - sizeof( header_buf ) );
PGPFreeData(pkt->base); /* Free Data-Send Buffer */
rply_pkt->base = reply_buf;
rply_pkt->length = reply_len;
rply_pkt->ptr = 0;
status = unpack_int32(rply_pkt);
length = unpack_int32(rply_pkt);
if (IsPGPError(status))
PGPFreeData(reply_buf);
PGPMutexUnlock(&sRPCMutex);
return status;
}
void
pgpSDKCleanupBackEnd()
{
close(sd);
PGPMutexDestroy(&sRPCMutex);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -