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