1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#if defined(__linux__)
#define _DEFAULT_SOURCE
#endif
#include "hybbx/security.h"
#if !defined(HYBBX_CLIENT_BUILD)
#include "hybbx/config.h"
#endif
#include "hybbx/limits.h"
#include "hybbx/util.h"
#include "hybbx/log.h"
#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#define HYBBX_SECURITY_LINE_MAX 512u
static char g_security_dir[HYBBX_PATH_MAX];
static int g_security_ready;
static FILE *g_security_file;
static pthread_mutex_t g_security_lock = PTHREAD_MUTEX_INITIALIZER;
static int mkdir_p(const char *path)
{
char buf[HYBBX_PATH_MAX];
size_t len;
size_t i;
if (path == NULL || path[0] == '\0') {
return -1;
}
hybbx_strlcpy(buf, path, sizeof(buf));
len = strlen(buf);
while (len > 0 && buf[len - 1] == '/') {
buf[--len] = '\0';
}
for (i = 1; i < len; i++) {
if (buf[i] != '/') {
continue;
}
buf[i] = '\0';
if (buf[0] != '\0' && mkdir(buf, 0755) != 0 && errno != EEXIST) {
return -1;
}
buf[i] = '/';
}
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
return -1;
}
return 0;
}
static int security_open_file(void)
{
char path[HYBBX_PATH_MAX];
if (g_security_dir[0] == '\0') {
return -1;
}
if (g_security_file != NULL) {
return 0;
}
if (mkdir_p(g_security_dir) != 0) {
hybbx_log_warn("[security] cannot create directory %s",
g_security_dir);
return -1;
}
if (hybbx_path_join(path, sizeof(path), g_security_dir,
HYBBX_SECURITY_LOG_FILE) != HYBBX_OK) {
return -1;
}
g_security_file = fopen(path, "a");
if (g_security_file == NULL) {
hybbx_log_warn("[security] cannot open %s", path);
return -1;
}
return 0;
}
#if !defined(HYBBX_CLIENT_BUILD)
void hybbx_security_log_config_apply(const struct hybbx_config *config)
{
const char *dir_raw;
hybbx_security_log_shutdown();
g_security_dir[0] = '\0';
g_security_ready = 0;
if (config != NULL) {
dir_raw = hybbx_config_get(config, "log", "dir", NULL);
if (dir_raw != NULL && dir_raw[0] != '\0') {
if (hybbx_path_resolve(g_security_dir, sizeof(g_security_dir),
dir_raw) != HYBBX_OK) {
hybbx_log_warn("[security] invalid log dir path");
return;
}
} else {
if (hybbx_path_resolve(g_security_dir, sizeof(g_security_dir),
HYBBX_DIR_LOGS) != HYBBX_OK) {
hybbx_log_warn("[security] cannot resolve default log dir");
return;
}
}
} else if (hybbx_path_resolve(g_security_dir, sizeof(g_security_dir),
HYBBX_DIR_LOGS) != HYBBX_OK) {
return;
}
g_security_ready = 1;
if (security_open_file() != 0) {
g_security_ready = 0;
return;
}
hybbx_log_info("[security] log=%s/%s", g_security_dir, HYBBX_SECURITY_LOG_FILE);
hybbx_security_log_write("startup");
}
#else
void hybbx_security_log_config_apply(const struct hybbx_config *config)
{
(void)config;
}
#endif
void hybbx_security_log_write(const char *fmt, ...)
{
char message[HYBBX_SECURITY_LINE_MAX];
char line[HYBBX_SECURITY_LINE_MAX + 64];
char stamp[32];
va_list ap;
time_t now;
struct tm tm_buf;
struct tm *tm;
if (!g_security_ready || fmt == NULL) {
return;
}
va_start(ap, fmt);
vsnprintf(message, sizeof(message), fmt, ap);
va_end(ap);
now = time(NULL);
tm = localtime_r(&now, &tm_buf);
if (tm == NULL) {
return;
}
if (strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", tm) == 0) {
return;
}
snprintf(line, sizeof(line), "%s %s\n", stamp, message);
pthread_mutex_lock(&g_security_lock);
if (security_open_file() == 0) {
fputs(line, g_security_file);
fflush(g_security_file);
}
pthread_mutex_unlock(&g_security_lock);
}
hybbx_result_t hybbx_security_log_current_path(char *out, size_t out_len)
{
if (out == NULL || out_len == 0) {
return HYBBX_ERR_INVALID;
}
out[0] = '\0';
if (!g_security_ready || g_security_dir[0] == '\0') {
return HYBBX_ERR_NOT_FOUND;
}
return hybbx_path_join(out, out_len, g_security_dir, HYBBX_SECURITY_LOG_FILE);
}
void hybbx_security_log_shutdown(void)
{
pthread_mutex_lock(&g_security_lock);
if (g_security_file != NULL) {
fclose(g_security_file);
g_security_file = NULL;
}
pthread_mutex_unlock(&g_security_lock);
g_security_ready = 0;
}
|