/src/S2OPC/src/Common/configuration/sopc_audit.c
Line | Count | Source |
1 | | /* |
2 | | * Licensed to Systerel under one or more contributor license |
3 | | * agreements. See the NOTICE file distributed with this work |
4 | | * for additional information regarding copyright ownership. |
5 | | * Systerel licenses this file to you under the Apache |
6 | | * License, Version 2.0 (the "License"); you may not use this |
7 | | * file except in compliance with the License. You may obtain |
8 | | * a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, |
13 | | * software distributed under the License is distributed on an |
14 | | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | * KIND, either express or implied. See the License for the |
16 | | * specific language governing permissions and limitations |
17 | | * under the License. |
18 | | */ |
19 | | |
20 | | #include "sopc_audit.h" |
21 | | |
22 | | #include <stddef.h> |
23 | | |
24 | | #include "sopc_common_constants.h" |
25 | | #include "sopc_helper_string.h" |
26 | | #include "sopc_logger.h" |
27 | | #include "sopc_macros.h" |
28 | | #include "sopc_mem_alloc.h" |
29 | | |
30 | | #if S2OPC_HAS_AUDITING |
31 | | |
32 | | /** Content of internal configuration. */ |
33 | | typedef struct Audit_Configuration_Internal |
34 | | { |
35 | | SOPC_Log_Instance* auditEntry; |
36 | | SOPC_Audit_OptionMask options; |
37 | | } Audit_Configuration_Internal; |
38 | | |
39 | | /* static variables */ |
40 | | static Audit_Configuration_Internal* gAuditCfg = NULL; |
41 | | |
42 | | /* Functions */ |
43 | | bool SOPC_Audit_Initialize(const SOPC_Audit_Configuration* optAuditConfig) |
44 | | { |
45 | | if (NULL != gAuditCfg) |
46 | | { |
47 | | SOPC_Logger_TraceError(SOPC_LOG_MODULE_COMMON, "SOPC_Audit_Initialize because audit is already started"); |
48 | | return false; |
49 | | } |
50 | | |
51 | | SOPC_ReturnStatus result = SOPC_STATUS_OK; |
52 | | if (NULL != optAuditConfig) |
53 | | { |
54 | | SOPC_Logger_TraceInfo(SOPC_LOG_MODULE_COMMON, "Initializing the Auditing option (Options flags=%04X)", |
55 | | (unsigned) optAuditConfig->options); |
56 | | if (0 != (optAuditConfig->options & ~SOPC_Audit_SupportedSecuOptions)) |
57 | | { |
58 | | result = SOPC_STATUS_NOT_SUPPORTED; |
59 | | } |
60 | | else |
61 | | { |
62 | | if (NULL != optAuditConfig->auditEntryPath) |
63 | | { |
64 | | SOPC_Logger_TraceInfo(SOPC_LOG_MODULE_COMMON, "Initializing the Auditing Entry option in '%s'", |
65 | | optAuditConfig->auditEntryPath); |
66 | | } |
67 | | else |
68 | | { |
69 | | SOPC_Logger_TraceInfo(SOPC_LOG_MODULE_COMMON, |
70 | | "No Auditing Entry activated since configuration path provided is NULL"); |
71 | | } |
72 | | gAuditCfg = (Audit_Configuration_Internal*) SOPC_Malloc(sizeof(*gAuditCfg)); |
73 | | if (NULL == gAuditCfg) |
74 | | { |
75 | | result = SOPC_STATUS_OUT_OF_MEMORY; |
76 | | } |
77 | | else |
78 | | { |
79 | | gAuditCfg->options = optAuditConfig->options; |
80 | | SOPC_CircularLogFile_Configuration logcfg; |
81 | | logcfg.logDirPath = optAuditConfig->auditEntryPath; |
82 | | logcfg.logFileName = "AuditLog"; |
83 | | logcfg.logMaxBytes = optAuditConfig->logMaxBytes; |
84 | | logcfg.logMaxFiles = optAuditConfig->logMaxFiles; |
85 | | gAuditCfg->auditEntry = SOPC_Log_CreateFileInstance(&logcfg, "AUDIT"); |
86 | | SOPC_Log_SetLogLevel(gAuditCfg->auditEntry, SOPC_LOG_LEVEL_INFO); |
87 | | } |
88 | | } |
89 | | } |
90 | | return (SOPC_STATUS_OK == result); |
91 | | } |
92 | | |
93 | | bool SOPC_Audit_IsAuditing(void) |
94 | | { |
95 | | return (NULL != gAuditCfg); |
96 | | } |
97 | | |
98 | | bool SOPC_Audit_HasOption(SOPC_Audit_OptionMask opt) |
99 | | { |
100 | | return (NULL != gAuditCfg) && ((opt & gAuditCfg->options) != 0); |
101 | | } |
102 | | |
103 | | void SOPC_Audit_Clear(void) |
104 | | { |
105 | | if (NULL != gAuditCfg) |
106 | | { |
107 | | SOPC_Log_ClearInstance(&gAuditCfg->auditEntry); |
108 | | SOPC_Free(gAuditCfg); |
109 | | gAuditCfg = NULL; |
110 | | } |
111 | | } |
112 | | |
113 | | SOPC_Log_Instance* SOPC_Audit_LogEntry(void) |
114 | | { |
115 | | return gAuditCfg->auditEntry; |
116 | | } |
117 | | #else // !S2OPC_HAS_AUDITING |
118 | | |
119 | | bool SOPC_Audit_Initialize(const SOPC_Audit_Configuration* optAuditConfig) |
120 | 0 | { |
121 | 0 | return NULL == optAuditConfig; |
122 | 0 | } |
123 | | |
124 | | bool SOPC_Audit_HasOption(SOPC_Audit_OptionMask opt) |
125 | 0 | { |
126 | 0 | SOPC_UNUSED_ARG(opt); |
127 | 0 | return false; |
128 | 0 | } |
129 | | |
130 | | bool SOPC_Audit_IsAuditing(void) |
131 | 0 | { |
132 | 0 | return false; |
133 | 0 | } |
134 | | |
135 | 0 | void SOPC_Audit_Clear(void) {} |
136 | | |
137 | | SOPC_Log_Instance* SOPC_Audit_LogEntry(void) |
138 | 0 | { |
139 | | return NULL; |
140 | 0 | } |
141 | | |
142 | | #endif |