[Fedora-directory-commits] esc/src/lib/coolkey CoolKey.cpp, 1.6, 1.7 CoolKey.h, 1.5, 1.6 CoolKeyHandler.cpp, 1.3, 1.4
Jack Magne (jmagne)
fedora-directory-commits at redhat.com
Thu Jun 7 21:21:19 UTC 2007
Author: jmagne
Update of /cvs/dirsec/esc/src/lib/coolkey
In directory cvs-int.fedora.redhat.com:/tmp/cvs-serv3367
Modified Files:
CoolKey.cpp CoolKey.h CoolKeyHandler.cpp
Log Message:
New log file support #206783, r. mharmsen.
Index: CoolKey.cpp
===================================================================
RCS file: /cvs/dirsec/esc/src/lib/coolkey/CoolKey.cpp,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- CoolKey.cpp 7 May 2007 23:51:46 -0000 1.6
+++ CoolKey.cpp 7 Jun 2007 21:21:17 -0000 1.7
@@ -16,6 +16,7 @@
* END COPYRIGHT BLOCK **/
#define FORCE_PR_LOG 1
+#define LINE_BUF_SIZE 512
#include "SlotUtils.h"
@@ -46,6 +47,161 @@
HRESULT ClearActiveKeyList(void);
ActiveKeyNode *GetNodeInActiveKeyList(const CoolKey *aKey);
+class CoolKeyLogger {
+public:
+
+ CoolKeyLogger(char *logFileName, int maxNumLines);
+ ~CoolKeyLogger();
+
+ void LogMsg(int logLevel, const char *fmt, ...);
+ void LogMsg(int logLevel,const char *msg, va_list argp);
+
+ void init();
+
+ int IsInitialized() { return initialized; }
+
+private:
+
+ void LockLog();
+ void UnlockLog();
+
+ PRLock *logLock;
+
+ int maxLines;
+
+ char *pathName;
+ PRFileDesc *fd;
+
+ int initialized;
+
+};
+
+CoolKeyLogger::CoolKeyLogger(char *logFileName, int maxNumLines)
+{
+ fd = NULL;
+ logLock = NULL;
+
+ maxLines = maxNumLines;
+ if(logFileName)
+ pathName = strdup(logFileName);
+ initialized = 0;
+}
+
+CoolKeyLogger::~CoolKeyLogger()
+{
+ char tBuff[56];
+
+ PR_LOG( coolKeyLog, PR_LOG_DEBUG, ("%s ~CoolKeyLogger:\n",GetTStamp(tBuff,56)));
+ LockLog();
+
+ PR_Close(fd);
+
+ fd = NULL;
+
+ UnlockLog();
+
+ PR_DestroyLock(logLock);
+
+ logLock = NULL;
+
+ if(pathName)
+ free(pathName);
+
+ pathName = NULL;
+}
+
+void CoolKeyLogger::LockLog()
+{
+ PR_Lock(logLock);
+}
+
+void CoolKeyLogger::UnlockLog()
+{
+ PR_Unlock(logLock);
+}
+
+void CoolKeyLogger::init()
+{
+ char tBuff[56];
+
+ PRFileInfo info;
+
+ if( !pathName)
+ return;
+
+ logLock = PR_NewLock();
+
+ PRStatus rv = PR_GetFileInfo(pathName,&info);
+
+ int fileSize = 0;
+
+ if(rv == PR_SUCCESS)
+ {
+ fileSize = info.size;
+ PR_LOG( coolKeyLog, PR_LOG_DEBUG, ("%s File info size %d! \n",GetTStamp(tBuff,56),fileSize));
+ }
+
+ //Assume average line size of about 40
+
+ if((fileSize / 40) > maxLines)
+ {
+
+ PR_LOG( coolKeyLog, PR_LOG_DEBUG, ("%s Number of lines too big, truncate file %d! \n",GetTStamp(tBuff,56),fileSize / 80));
+
+ fd = PR_Open(pathName, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0600);
+ }
+ else
+ {
+ fd = PR_Open(pathName, PR_WRONLY | PR_CREATE_FILE | PR_APPEND, 0600);
+ }
+
+ if(!fd)
+ return;
+
+ initialized = 1;
+
+ return;
+}
+
+void CoolKeyLogger::LogMsg(int logLevel, const char *fmt, ...)
+{
+ va_list ap;
+ char line[LINE_BUF_SIZE];
+
+ if(!initialized)
+ return;
+
+ va_start(ap, fmt);
+
+ int end = PR_vsnprintf(line, sizeof(line)-1, fmt, ap);
+
+ LockLog();
+
+ PR_Write(fd,line,end);
+
+ UnlockLog();
+
+ va_end(ap);
+}
+
+void CoolKeyLogger::LogMsg(int logLevel, const char *msg, va_list argp)
+{
+ char line[LINE_BUF_SIZE];
+
+ if(!initialized)
+ return;
+
+ int end = PR_vsnprintf(line, sizeof(line)-1, msg, argp);
+
+ LockLog();
+
+ PR_Write(fd,line,end);
+
+ UnlockLog();
+}
+
+static CoolKeyLogger *g_Log = NULL;
+
COOLKEY_API HRESULT CoolKeyInit(const char *aAppDir)
{
char tBuff[56];
@@ -92,6 +248,9 @@
g_NSSManager = 0;
}
+ if(g_Log)
+ delete g_Log ;
+
return S_OK;
}
@@ -903,7 +1062,7 @@
assert(cardCtxt);
if (!cardCtxt) {
- PR_LOG( coolKeyLog, PR_LOG_ERROR, ("%s Attempting to get key issuer info. Can't create Card Context !.\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Attempting to get key issuer info. Can't create Card Context !.\n",GetTStamp(tBuff,56));
result = E_FAIL;
goto done;
}
@@ -911,7 +1070,7 @@
conn = CKYCardConnection_Create(cardCtxt);
assert(conn);
if (!conn) {
- PR_LOG( coolKeyLog, PR_LOG_ERROR, ("%s Attempting to get key issuer info. Can't create Card Connection!\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Attempting to get key issuer info. Can't create Card Connection!\n",GetTStamp(tBuff,56));
result = E_FAIL;
goto done;
}
@@ -919,14 +1078,14 @@
readerName = GetReaderNameForKeyID(aKey);
assert(readerName);
if (!readerName) {
- PR_LOG( coolKeyLog, PR_LOG_ERROR, ("%s Attempting to get key issuer info. Can't get reader name!\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Attempting to get key issuer info. Can't get reader name!\n",GetTStamp(tBuff,56));
result = E_FAIL;
goto done;
}
status = CKYCardConnection_Connect(conn, readerName);
if (status != CKYSUCCESS) {
- PR_LOG( coolKeyLog, PR_LOG_ERROR, ("%s Attempting to get key issuer info. Can't connect to Card!\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Attempting to get key issuer info. Can't connect to Card!\n",GetTStamp(tBuff,56));
result = E_FAIL;
goto done;
@@ -938,7 +1097,7 @@
apduRC = 0;
status = CKYApplet_SelectCoolKeyManager(conn, &apduRC);
if (status != CKYSUCCESS) {
- PR_LOG( coolKeyLog, PR_LOG_ERROR, ("%s Attempting to get key issuer info. Can't select CoolKey manager!\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Attempting to get key issuer info. Can't select CoolKey manager!\n",GetTStamp(tBuff,56));
goto done;
}
@@ -946,7 +1105,7 @@
&apduRC);
if(status != CKYSUCCESS)
{
- PR_LOG( coolKeyLog, PR_LOG_ERROR, ("%s Attempting to get key issuer info. Error actually getting IssuerInfo!\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Attempting to get key issuer info. Error actually getting IssuerInfo!\n",GetTStamp(tBuff,56));
result = E_FAIL;
goto done;
}
@@ -1153,6 +1312,42 @@
return res;
}
+HRESULT CoolKeyInitializeLog(char *logFileName, int maxNumLines)
+{
+ if(g_Log)
+ return S_OK;
+
+ g_Log = new CoolKeyLogger(logFileName,maxNumLines);
+
+ if(g_Log)
+ g_Log->init();
+ else
+ return E_FAIL;
+
+ if(g_Log->IsInitialized())
+ return S_OK;
+ else
+ return E_FAIL;
+}
+
+HRESULT CoolKeyLogMsg(int logLevel, const char *fmt, ...)
+{
+
+ if(!g_Log)
+ return S_OK;
+
+ va_list ap;
+
+
+ va_start(ap, fmt);
+
+ g_Log->LogMsg(logLevel,fmt,ap);
+
+ va_end(ap);
+
+ return S_OK;
+}
+
//Utility function to get Time Stamp
char *GetTStamp(char *aTime,int aSize)
{
Index: CoolKey.h
===================================================================
RCS file: /cvs/dirsec/esc/src/lib/coolkey/CoolKey.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- CoolKey.h 7 May 2007 23:51:46 -0000 1.5
+++ CoolKey.h 7 Jun 2007 21:21:17 -0000 1.6
@@ -176,6 +176,11 @@
COOLKEY_API int CoolKeyGetAppletVer(const CoolKey *aKey, const bool isMajor);
+COOLKEY_API HRESULT CoolKeyInitializeLog(char *logFileName, int maxNumLines);
+
+COOLKEY_API HRESULT CoolKeyLogMsg(int logLevel, const char *fmt, ...);
+
+
//Utility time function
char *GetTStamp(char *aTime,int aSize);
}
Index: CoolKeyHandler.cpp
===================================================================
RCS file: /cvs/dirsec/esc/src/lib/coolkey/CoolKeyHandler.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CoolKeyHandler.cpp 24 Feb 2007 02:16:41 -0000 1.3
+++ CoolKeyHandler.cpp 7 Jun 2007 21:21:17 -0000 1.4
@@ -453,7 +453,7 @@
const char *readerName = NULL;
if (!aKey || aKey->mKeyType != eCKType_CoolKey || !aKey->mKeyID) {
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Cannot begin CoolKey operation. Insuficient input parameters. \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Cannot begin CoolKey operation. Insuficient input parameters. \n",GetTStamp(tBuff,56));
goto done;
}
@@ -466,14 +466,14 @@
if (!readerName) {
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Cannot begin CoolKey operation. Cannot locate card reader name! \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Cannot begin CoolKey operation. Cannot locate card reader name! \n",GetTStamp(tBuff,56));
goto done;
}
mDataLock = PR_NewLock();
if (!mDataLock)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Cannot begin CoolKey operation. Cannnot initialize internal locking mechanism.\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Cannot begin CoolKey operation. Cannnot initialize internal locking mechanism.\n",GetTStamp(tBuff,56));
return E_FAIL;
}
@@ -481,7 +481,7 @@
mDataCondVar = PR_NewCondVar(mDataLock);
if (!mDataCondVar)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Cannot begin CoolKey operation. Cannot initialize internal syncronization mechanism.\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Cannot begin CoolKey operation. Cannot initialize internal syncronization mechanism.\n",GetTStamp(tBuff,56));
return E_FAIL;
}
@@ -493,7 +493,7 @@
if(!mCharHostName || !mRAUrl)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Cannot begin CoolKey operation. Didn't collect proper config information.\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Cannot begin CoolKey operation. Didn't collect proper config information.\n",GetTStamp(tBuff,56));
error_no = config_error_no;
goto done;
}
@@ -502,7 +502,7 @@
mCardContext = CKYCardContext_Create(SCARD_SCOPE_USER);
if (!mCardContext) {
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Cannot begin CoolKey operation. Cannot create card context! \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Cannot begin CoolKey operation. Cannot create card context! \n",GetTStamp(tBuff,56));
error_no = CARD_CONTEXT_ERROR;
goto done;
}
@@ -510,7 +510,7 @@
mPDUWriter = new PDUWriterThread(this);
if (!mPDUWriter) {
error_no = PDU_WRITER_ERROR;
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Cannot begin CoolKey operation. Cannot create internal PDU writer thread!\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Cannot begin CoolKey operation. Cannot create internal PDU writer thread!\n",GetTStamp(tBuff,56));
goto done;
}
@@ -581,7 +581,7 @@
if(!keyID)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR,("%s Collecting CoolKey preferences. Cannot get keyID , cannot proceed. \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR,"%s Collecting CoolKey preferences. Cannot get keyID , cannot proceed. \n",GetTStamp(tBuff,56));
return;
}
@@ -621,7 +621,7 @@
if(!tps_url)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Collecting CoolKey preferences. Cannot find value for the TPS URL. \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Collecting CoolKey preferences. Cannot find value for the TPS URL. \n",GetTStamp(tBuff,56));
return;
}
@@ -651,7 +651,7 @@
pos = tps_url_str.find(non_ssl_str,0);
if(pos == string::npos)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Collecting CoolKey preferences. TPS URL has specified an illegal protocol! \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Collecting CoolKey preferences. TPS URL has specified an illegal protocol! \n",GetTStamp(tBuff,56));
return;
}
@@ -692,7 +692,7 @@
if(!host_name_port_str.length())
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Collecting CoolKey preferences. Bad hostname and port value!.\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg(PR_LOG_ERROR, "%s Collecting CoolKey preferences. Bad hostname and port value!.\n",GetTStamp(tBuff,56));
return;
}
@@ -1198,7 +1198,7 @@
PR_LOG( coolKeyLogHN, PR_LOG_DEBUG, ("%s CoolKeyHandler::ProcessTokenPDU:\n",GetTStamp(tBuff,56)));
if(!req || !context)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Processing HTTP message. Bad input data. \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Processing HTTP message. Bad input data. \n",GetTStamp(tBuff,56));
return;
}
@@ -1210,7 +1210,7 @@
if(size == 0)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Processing HTTP message. Can't extract PDU data from message! \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg(PR_LOG_ERROR, "%s Processing HTTP message. Can't extract PDU data from message! \n",GetTStamp(tBuff,56));
context->HttpDisconnect();
return;
}
@@ -1231,10 +1231,10 @@
CKYStatus status = CKYCardConnection_ExchangeAPDU(context->GetCardConnection(),
requestAPDU, &response);
if (status != CKYSUCCESS) {
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR,
- ("%s Processing HTTP message. Can't write apdu to card! status %d response[0] %x response[1] %x error %d \n"
+ CoolKeyLogMsg( PR_LOG_ERROR,
+ "%s Processing HTTP message. Can't write apdu to card! status %d response[0] %x response[1] %x error %d \n"
,GetTStamp(tBuff,56) ,status,CKYBuffer_GetChar(&response,0),CKYBuffer_GetChar(&response,1),
- CKYCardConnection_GetLastError(context->GetCardConnection())));
+ CKYCardConnection_GetLastError(context->GetCardConnection()));
context->HttpDisconnect(ERR_CONN_TOKEN);
@@ -1248,7 +1248,7 @@
if(pduSizeRet == 0 || !pduDataRet )
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Processing HTTP message. No PDU response from card! \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Processing HTTP message. No PDU response from card! \n",GetTStamp(tBuff,56));
context->HttpDisconnect(ERR_CONN_TOKEN);
return;
}
@@ -1267,7 +1267,7 @@
if(res == 0)
{
- PR_LOG( coolKeyLogHN, PR_LOG_ERROR, ("%s Processing HTTP message. Write back to TPS failed , disconnecting. \n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ERROR, "%s Processing HTTP message. Write back to TPS failed , disconnecting. \n",GetTStamp(tBuff,56));
context->HttpDisconnect();
}
else
@@ -1619,32 +1619,33 @@
case ENROLL:
if (result == 0) {
- PR_LOG( coolKeyLogHN, PR_LOG_ALWAYS, ("%s Key Enrollment success.\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg(PR_LOG_ALWAYS,"%s Key Enrollment success.\n",GetTStamp(tBuff,56));
CoolKeyAuthenticate(context->GetAutoCoolKey(), context->GetPIN());
CoolKeyNotify(context->GetAutoCoolKey(), eCKState_EnrollmentComplete,
context->GetScreenName() == NULL ? 1 : 0);
} else {
- PR_LOG( coolKeyLogHN, PR_LOG_ALWAYS, ("%s Key Enrollment failure. Error: %d.\n",GetTStamp(tBuff,56),description));
+ CoolKeyLogMsg( PR_LOG_ALWAYS, "%s Key Enrollment failure. Error: %d.\n",GetTStamp(tBuff,56),description);
CoolKeyNotify(context->GetAutoCoolKey(), eCKState_EnrollmentError, description); // XXX: Need INIT_FAILED error code!
}
break;
case RESET_PIN:
if (result == 0) {
- PR_LOG( coolKeyLogHN, PR_LOG_ALWAYS, ("%s Key Reset Password success.\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg(PR_LOG_ALWAYS,"%s Key Reset Password success.\n",GetTStamp(tBuff,56));
+
CoolKeyAuthenticate(context->GetAutoCoolKey(), context->GetPIN());
CoolKeyNotify(context->GetAutoCoolKey(), eCKState_PINResetComplete, 0);
} else {
- PR_LOG( coolKeyLogHN, PR_LOG_ALWAYS, ("%s Key Reset Password failure. Error: %d.\n",GetTStamp(tBuff,56),description));
+ CoolKeyLogMsg(PR_LOG_ALWAYS, "%s Key Reset Password failure. Error: %d.\n",GetTStamp(tBuff,56),description);
CoolKeyNotify(context->GetAutoCoolKey(), eCKState_PINResetError, description); // XXX: Need PIN_RESET_FAILED error code!
}
break;
case FORMAT:
if (result == 0) {
- PR_LOG( coolKeyLogHN, PR_LOG_ALWAYS, ("%s Key Format success.\n",GetTStamp(tBuff,56)));
+ CoolKeyLogMsg( PR_LOG_ALWAYS, "%s Key Format success.\n",GetTStamp(tBuff,56));
CoolKeyNotify(context->GetAutoCoolKey(), eCKState_FormatComplete, 0);
} else {
- PR_LOG( coolKeyLogHN, PR_LOG_ALWAYS, ("%s Key Format failure. Error: %d.\n",GetTStamp(tBuff,56),description));
+ CoolKeyLogMsg( PR_LOG_ALWAYS, "%s Key Format failure. Error: %d.\n",GetTStamp(tBuff,56),description);
CoolKeyNotify(context->GetAutoCoolKey(), eCKState_FormatError, description); // XXX: Need FORMAT_FAILED error code!
}
break;
More information about the Fedora-directory-commits
mailing list