xref: /onnv-gate/usr/src/lib/libkmsagent/common/KMSAuditLogger.cpp (revision 12720:3db6e0082404)
1*12720SWyllys.Ingersoll@Sun.COM /*
2*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER START
3*12720SWyllys.Ingersoll@Sun.COM  *
4*12720SWyllys.Ingersoll@Sun.COM  * The contents of this file are subject to the terms of the
5*12720SWyllys.Ingersoll@Sun.COM  * Common Development and Distribution License (the "License").
6*12720SWyllys.Ingersoll@Sun.COM  * You may not use this file except in compliance with the License.
7*12720SWyllys.Ingersoll@Sun.COM  *
8*12720SWyllys.Ingersoll@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12720SWyllys.Ingersoll@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*12720SWyllys.Ingersoll@Sun.COM  * See the License for the specific language governing permissions
11*12720SWyllys.Ingersoll@Sun.COM  * and limitations under the License.
12*12720SWyllys.Ingersoll@Sun.COM  *
13*12720SWyllys.Ingersoll@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*12720SWyllys.Ingersoll@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12720SWyllys.Ingersoll@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*12720SWyllys.Ingersoll@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*12720SWyllys.Ingersoll@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*12720SWyllys.Ingersoll@Sun.COM  *
19*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER END
20*12720SWyllys.Ingersoll@Sun.COM  */
21*12720SWyllys.Ingersoll@Sun.COM 
22*12720SWyllys.Ingersoll@Sun.COM /*
23*12720SWyllys.Ingersoll@Sun.COM  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*12720SWyllys.Ingersoll@Sun.COM  */
25*12720SWyllys.Ingersoll@Sun.COM 
26*12720SWyllys.Ingersoll@Sun.COM /**
27*12720SWyllys.Ingersoll@Sun.COM  * \file KMSAuditLogger.cpp
28*12720SWyllys.Ingersoll@Sun.COM  */
29*12720SWyllys.Ingersoll@Sun.COM 
30*12720SWyllys.Ingersoll@Sun.COM #ifndef WIN32
31*12720SWyllys.Ingersoll@Sun.COM //#include <syslog.h>
32*12720SWyllys.Ingersoll@Sun.COM #include <stdarg.h>
33*12720SWyllys.Ingersoll@Sun.COM #endif
34*12720SWyllys.Ingersoll@Sun.COM 
35*12720SWyllys.Ingersoll@Sun.COM #include <stdio.h>
36*12720SWyllys.Ingersoll@Sun.COM 
37*12720SWyllys.Ingersoll@Sun.COM #ifndef METAWARE
38*12720SWyllys.Ingersoll@Sun.COM #include <sys/timeb.h>
39*12720SWyllys.Ingersoll@Sun.COM #endif
40*12720SWyllys.Ingersoll@Sun.COM 
41*12720SWyllys.Ingersoll@Sun.COM #include <time.h>
42*12720SWyllys.Ingersoll@Sun.COM 
43*12720SWyllys.Ingersoll@Sun.COM #include "KMSAuditLogger.h"
44*12720SWyllys.Ingersoll@Sun.COM #include "ApplianceParameters.h"
45*12720SWyllys.Ingersoll@Sun.COM 
46*12720SWyllys.Ingersoll@Sun.COM #define AGENT_LOG_FILE              "KMSAgentLog.log"
47*12720SWyllys.Ingersoll@Sun.COM 
48*12720SWyllys.Ingersoll@Sun.COM // globals for file logging
49*12720SWyllys.Ingersoll@Sun.COM static FILE* g_fpLogFileHandle = NULL;
50*12720SWyllys.Ingersoll@Sun.COM static K_MUTEX_HANDLE g_stLogFileMutex;
51*12720SWyllys.Ingersoll@Sun.COM static char g_sLogFileName[MAX_LOG_FILE_NAME_LENGTH];
52*12720SWyllys.Ingersoll@Sun.COM 
53*12720SWyllys.Ingersoll@Sun.COM // Find header in AuditLogger.h
InitializeFileLogging(const char * const i_sWorkingDirectory)54*12720SWyllys.Ingersoll@Sun.COM int InitializeFileLogging( const char* const i_sWorkingDirectory )
55*12720SWyllys.Ingersoll@Sun.COM {
56*12720SWyllys.Ingersoll@Sun.COM     FATAL_ASSERT( i_sWorkingDirectory );
57*12720SWyllys.Ingersoll@Sun.COM     if ( g_fpLogFileHandle != NULL )
58*12720SWyllys.Ingersoll@Sun.COM     {
59*12720SWyllys.Ingersoll@Sun.COM         return false;
60*12720SWyllys.Ingersoll@Sun.COM     }
61*12720SWyllys.Ingersoll@Sun.COM 
62*12720SWyllys.Ingersoll@Sun.COM     char sLogFileName[MAX_LOG_FILE_NAME_LENGTH];
63*12720SWyllys.Ingersoll@Sun.COM     strncpy( sLogFileName, i_sWorkingDirectory, MAX_LOG_FILE_NAME_LENGTH );
64*12720SWyllys.Ingersoll@Sun.COM 
65*12720SWyllys.Ingersoll@Sun.COM     if ( sLogFileName[ strlen( sLogFileName )-1 ] != PATH_SEPARATOR )
66*12720SWyllys.Ingersoll@Sun.COM     {
67*12720SWyllys.Ingersoll@Sun.COM         sLogFileName[ strlen(sLogFileName) ] = PATH_SEPARATOR ;
68*12720SWyllys.Ingersoll@Sun.COM         sLogFileName[ strlen(sLogFileName) + 1 ] = '\0';
69*12720SWyllys.Ingersoll@Sun.COM     }
70*12720SWyllys.Ingersoll@Sun.COM 
71*12720SWyllys.Ingersoll@Sun.COM     strncat( sLogFileName, AGENT_LOG_FILE, MAX_LOG_FILE_NAME_LENGTH );
72*12720SWyllys.Ingersoll@Sun.COM 
73*12720SWyllys.Ingersoll@Sun.COM     strcpy(g_sLogFileName, sLogFileName);
74*12720SWyllys.Ingersoll@Sun.COM 
75*12720SWyllys.Ingersoll@Sun.COM     if ( K_CreateMutex( &g_stLogFileMutex ) != K_SYS_OK )
76*12720SWyllys.Ingersoll@Sun.COM     {
77*12720SWyllys.Ingersoll@Sun.COM         return false;
78*12720SWyllys.Ingersoll@Sun.COM     }
79*12720SWyllys.Ingersoll@Sun.COM 
80*12720SWyllys.Ingersoll@Sun.COM     if ( NULL == ( g_fpLogFileHandle = fopen( g_sLogFileName, "a+t" ) ) )
81*12720SWyllys.Ingersoll@Sun.COM     {
82*12720SWyllys.Ingersoll@Sun.COM         return false;
83*12720SWyllys.Ingersoll@Sun.COM     }
84*12720SWyllys.Ingersoll@Sun.COM 
85*12720SWyllys.Ingersoll@Sun.COM     return true;
86*12720SWyllys.Ingersoll@Sun.COM }
87*12720SWyllys.Ingersoll@Sun.COM 
88*12720SWyllys.Ingersoll@Sun.COM // Find header in AuditLogger.h
FinalizeFileLogging()89*12720SWyllys.Ingersoll@Sun.COM int FinalizeFileLogging()
90*12720SWyllys.Ingersoll@Sun.COM {
91*12720SWyllys.Ingersoll@Sun.COM     FATAL_ASSERT( g_fpLogFileHandle != NULL );
92*12720SWyllys.Ingersoll@Sun.COM 
93*12720SWyllys.Ingersoll@Sun.COM     K_DestroyMutex( g_stLogFileMutex );
94*12720SWyllys.Ingersoll@Sun.COM 
95*12720SWyllys.Ingersoll@Sun.COM     bool bSuccess = ( 0 == fclose( g_fpLogFileHandle ) );
96*12720SWyllys.Ingersoll@Sun.COM 
97*12720SWyllys.Ingersoll@Sun.COM     g_fpLogFileHandle = NULL;
98*12720SWyllys.Ingersoll@Sun.COM 
99*12720SWyllys.Ingersoll@Sun.COM     return bSuccess;
100*12720SWyllys.Ingersoll@Sun.COM }
101*12720SWyllys.Ingersoll@Sun.COM 
102*12720SWyllys.Ingersoll@Sun.COM // Find header in AuditLogger.h
LogToFile(int i_iErrno,const char * const i_sLogLine)103*12720SWyllys.Ingersoll@Sun.COM extern "C" int LogToFile( int i_iErrno,
104*12720SWyllys.Ingersoll@Sun.COM                const char* const i_sLogLine )
105*12720SWyllys.Ingersoll@Sun.COM {
106*12720SWyllys.Ingersoll@Sun.COM     if ( g_fpLogFileHandle == NULL )
107*12720SWyllys.Ingersoll@Sun.COM     {
108*12720SWyllys.Ingersoll@Sun.COM         return false;
109*12720SWyllys.Ingersoll@Sun.COM     }
110*12720SWyllys.Ingersoll@Sun.COM 
111*12720SWyllys.Ingersoll@Sun.COM     CAutoMutex oAutoMutex( g_stLogFileMutex );
112*12720SWyllys.Ingersoll@Sun.COM 
113*12720SWyllys.Ingersoll@Sun.COM     if (0 > fputs( i_sLogLine, g_fpLogFileHandle ) )
114*12720SWyllys.Ingersoll@Sun.COM     {
115*12720SWyllys.Ingersoll@Sun.COM         return false;
116*12720SWyllys.Ingersoll@Sun.COM     }
117*12720SWyllys.Ingersoll@Sun.COM 
118*12720SWyllys.Ingersoll@Sun.COM     if ( 0 > fputs( "\n", g_fpLogFileHandle ) )
119*12720SWyllys.Ingersoll@Sun.COM     {
120*12720SWyllys.Ingersoll@Sun.COM         return false;
121*12720SWyllys.Ingersoll@Sun.COM     }
122*12720SWyllys.Ingersoll@Sun.COM 
123*12720SWyllys.Ingersoll@Sun.COM     if ( fflush( g_fpLogFileHandle ) != 0 )
124*12720SWyllys.Ingersoll@Sun.COM     {
125*12720SWyllys.Ingersoll@Sun.COM         return false;
126*12720SWyllys.Ingersoll@Sun.COM     }
127*12720SWyllys.Ingersoll@Sun.COM 
128*12720SWyllys.Ingersoll@Sun.COM     return true;
129*12720SWyllys.Ingersoll@Sun.COM }
130*12720SWyllys.Ingersoll@Sun.COM 
131*12720SWyllys.Ingersoll@Sun.COM static const int g_iMaxLogFileLineLength = MAX_LOG_FILE_LINE_LENGTH;
132*12720SWyllys.Ingersoll@Sun.COM 
133*12720SWyllys.Ingersoll@Sun.COM 
Log_function(int i_iErrno,const char * const i_sOperation,const char * const i_sEntityID,const char * const i_sNetworkAddress,const char * const i_sMessage)134*12720SWyllys.Ingersoll@Sun.COM int Log_function(
135*12720SWyllys.Ingersoll@Sun.COM    int i_iErrno,
136*12720SWyllys.Ingersoll@Sun.COM    const char* const i_sOperation,
137*12720SWyllys.Ingersoll@Sun.COM    const char* const i_sEntityID,
138*12720SWyllys.Ingersoll@Sun.COM    const char* const i_sNetworkAddress,
139*12720SWyllys.Ingersoll@Sun.COM    const char* const i_sMessage )
140*12720SWyllys.Ingersoll@Sun.COM {
141*12720SWyllys.Ingersoll@Sun.COM     char sFileLogEntry[500];
142*12720SWyllys.Ingersoll@Sun.COM     const int iTempSize = 100;
143*12720SWyllys.Ingersoll@Sun.COM 
144*12720SWyllys.Ingersoll@Sun.COM     timeb stTime;
145*12720SWyllys.Ingersoll@Sun.COM     ftime(&stTime);
146*12720SWyllys.Ingersoll@Sun.COM 
147*12720SWyllys.Ingersoll@Sun.COM     struct tm* pstTime = gmtime( &(stTime.time) );
148*12720SWyllys.Ingersoll@Sun.COM 
149*12720SWyllys.Ingersoll@Sun.COM     K_snprintf(
150*12720SWyllys.Ingersoll@Sun.COM         sFileLogEntry,
151*12720SWyllys.Ingersoll@Sun.COM         iTempSize,
152*12720SWyllys.Ingersoll@Sun.COM         "%04d-%02d-%02d %02d:%02d:%02d.%03dZ",
153*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_year+1900,
154*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_mon+1,
155*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_mday,
156*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_hour,
157*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_min,
158*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_sec,
159*12720SWyllys.Ingersoll@Sun.COM         stTime.millitm);
160*12720SWyllys.Ingersoll@Sun.COM 
161*12720SWyllys.Ingersoll@Sun.COM     if ( i_sEntityID )
162*12720SWyllys.Ingersoll@Sun.COM     {
163*12720SWyllys.Ingersoll@Sun.COM         strcat(sFileLogEntry," AgentID=");
164*12720SWyllys.Ingersoll@Sun.COM         strcat(sFileLogEntry,i_sEntityID);
165*12720SWyllys.Ingersoll@Sun.COM     }
166*12720SWyllys.Ingersoll@Sun.COM 
167*12720SWyllys.Ingersoll@Sun.COM     if ( i_sNetworkAddress )
168*12720SWyllys.Ingersoll@Sun.COM     {
169*12720SWyllys.Ingersoll@Sun.COM         strcat(sFileLogEntry," KMA Address=");
170*12720SWyllys.Ingersoll@Sun.COM         strcat(sFileLogEntry, i_sNetworkAddress);
171*12720SWyllys.Ingersoll@Sun.COM     }
172*12720SWyllys.Ingersoll@Sun.COM     if ( i_sOperation )
173*12720SWyllys.Ingersoll@Sun.COM     {
174*12720SWyllys.Ingersoll@Sun.COM         strcat(sFileLogEntry, " Operation=");
175*12720SWyllys.Ingersoll@Sun.COM         strcat(sFileLogEntry,i_sOperation);
176*12720SWyllys.Ingersoll@Sun.COM     }
177*12720SWyllys.Ingersoll@Sun.COM 
178*12720SWyllys.Ingersoll@Sun.COM     if ( i_sMessage )
179*12720SWyllys.Ingersoll@Sun.COM     {
180*12720SWyllys.Ingersoll@Sun.COM         strcat(sFileLogEntry, " Msg=");
181*12720SWyllys.Ingersoll@Sun.COM         strcat(sFileLogEntry, i_sMessage);
182*12720SWyllys.Ingersoll@Sun.COM     }
183*12720SWyllys.Ingersoll@Sun.COM 
184*12720SWyllys.Ingersoll@Sun.COM     return LogToFile( i_iErrno, sFileLogEntry );
185*12720SWyllys.Ingersoll@Sun.COM }
186*12720SWyllys.Ingersoll@Sun.COM 
Log2(char * msg1,char * msg2)187*12720SWyllys.Ingersoll@Sun.COM int Log2(char* msg1,
188*12720SWyllys.Ingersoll@Sun.COM          char* msg2)
189*12720SWyllys.Ingersoll@Sun.COM {
190*12720SWyllys.Ingersoll@Sun.COM    return 0;
191*12720SWyllys.Ingersoll@Sun.COM }
192