24 |
|
|
25 |
|
#include "stdinc.h" |
26 |
|
|
27 |
– |
#ifdef USE_SYSLOG |
28 |
– |
# ifdef HAVE_SYS_SYSLOG_H |
29 |
– |
# include <sys/syslog.h> |
30 |
– |
# else |
31 |
– |
# ifdef HAVE_SYSLOG_H |
32 |
– |
# include <syslog.h> |
33 |
– |
# endif |
34 |
– |
# endif |
35 |
– |
#endif |
36 |
– |
|
37 |
– |
#include "client.h" /* Needed for struct Client */ |
38 |
– |
#include "common.h" |
27 |
|
#include "s_log.h" |
28 |
|
#include "fileio.h" |
29 |
|
#include "irc_string.h" |
30 |
|
#include "sprintf_irc.h" |
31 |
|
#include "ircd.h" |
32 |
|
#include "s_misc.h" |
45 |
– |
#include "event.h" /* Needed for EVH etc. */ |
33 |
|
#include "s_conf.h" |
34 |
|
#include "memory.h" |
35 |
|
|
36 |
|
/* some older syslogs would overflow at 2024 */ |
37 |
|
#define LOG_BUFSIZE 2000 |
38 |
|
|
39 |
< |
static FBFILE *logFile; |
40 |
< |
static int logLevel = INIT_LOG_LEVEL; |
41 |
< |
|
42 |
< |
#ifndef SYSLOG_USERS |
43 |
< |
static EVH user_log_resync; |
57 |
< |
static FBFILE *user_log_fb = NULL; |
58 |
< |
#endif |
39 |
> |
static struct { |
40 |
> |
char path[PATH_MAX + 1]; |
41 |
> |
size_t size; |
42 |
> |
FBFILE *file; |
43 |
> |
} log_type_table[LOG_TYPE_LAST]; |
44 |
|
|
45 |
|
|
46 |
< |
#ifdef USE_SYSLOG |
47 |
< |
static const int sysLogLevel[] = |
46 |
> |
int |
47 |
> |
log_add_file(unsigned int type, size_t size, const char *path) |
48 |
|
{ |
49 |
< |
LOG_CRIT, |
50 |
< |
LOG_ERR, |
51 |
< |
LOG_WARNING, |
52 |
< |
LOG_NOTICE, |
68 |
< |
LOG_INFO, |
69 |
< |
LOG_INFO, |
70 |
< |
LOG_INFO |
71 |
< |
}; |
72 |
< |
#endif |
49 |
> |
if (log_type_table[type].file) |
50 |
> |
fbclose(log_type_table[type].file); |
51 |
> |
else |
52 |
> |
log_type_table[type].file = MyMalloc(sizeof(FBFILE)); |
53 |
|
|
54 |
< |
static const char *logLevelToString[] = |
55 |
< |
{ |
56 |
< |
"L_CRIT", |
57 |
< |
"L_ERROR", |
58 |
< |
"L_WARN", |
59 |
< |
"L_NOTICE", |
60 |
< |
"L_TRACE", |
81 |
< |
"L_INFO", |
82 |
< |
"L_DEBUG" |
83 |
< |
}; |
84 |
< |
|
85 |
< |
/* |
86 |
< |
* open_log - open ircd logging file |
87 |
< |
* returns true (1) if successful, false (0) otherwise |
88 |
< |
*/ |
89 |
< |
static int |
90 |
< |
open_log(const char *filename) |
54 |
> |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
55 |
> |
|
56 |
> |
return (log_type_table[type].file = fbopen(path, "a")) != NULL; |
57 |
> |
} |
58 |
> |
|
59 |
> |
void |
60 |
> |
log_close_all(void) |
61 |
|
{ |
62 |
< |
logFile = fbopen(filename, "a"); |
62 |
> |
unsigned int type = 0; |
63 |
|
|
64 |
< |
if (logFile == NULL) |
64 |
> |
while (++type < LOG_TYPE_LAST) |
65 |
|
{ |
66 |
< |
#ifdef USE_SYSLOG |
67 |
< |
syslog(LOG_ERR, "Unable to open log file: %s: %s", |
98 |
< |
filename, strerror(errno)); |
99 |
< |
#endif |
100 |
< |
return (0); |
101 |
< |
} |
66 |
> |
if (log_type_table[type].file == NULL) |
67 |
> |
continue; |
68 |
|
|
69 |
< |
return (1); |
69 |
> |
fbclose(log_type_table[type].file); |
70 |
> |
MyFree(log_type_table[type].file); |
71 |
> |
} |
72 |
|
} |
73 |
|
|
74 |
|
static void |
75 |
< |
write_log(const char *message) |
75 |
> |
write_log(unsigned int type, const char *message) |
76 |
|
{ |
77 |
|
char buf[LOG_BUFSIZE]; |
78 |
|
size_t nbytes = 0; |
79 |
|
|
80 |
< |
if (logFile == NULL) |
80 |
> |
if (log_type_table[type].file == NULL) |
81 |
|
return; |
82 |
|
|
83 |
< |
#ifdef _WIN32 |
84 |
< |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\r\n", |
85 |
< |
smalldate(CurrentTime), message); |
86 |
< |
#else |
87 |
< |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
88 |
< |
smalldate(CurrentTime), message); |
89 |
< |
#endif |
122 |
< |
fbputs(buf, logFile, nbytes); |
83 |
> |
if (ConfigLoggingEntry.timestamp) |
84 |
> |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
85 |
> |
smalldate(CurrentTime), message); |
86 |
> |
else |
87 |
> |
nbytes = snprintf(buf, sizeof(buf), "%s\n", message); |
88 |
> |
|
89 |
> |
fbputs(buf, log_type_table[type].file, nbytes); |
90 |
|
} |
91 |
|
|
92 |
|
void |
93 |
< |
ilog(const int priority, const char *fmt, ...) |
93 |
> |
ilog(unsigned int type, const char *fmt, ...) |
94 |
|
{ |
95 |
|
char buf[LOG_BUFSIZE]; |
96 |
|
va_list args; |
97 |
|
|
98 |
< |
assert(priority > -1); |
99 |
< |
|
133 |
< |
if (fmt == NULL) |
134 |
< |
return; |
135 |
< |
|
136 |
< |
if (priority > logLevel) |
137 |
< |
return; |
98 |
> |
assert(fmt); |
99 |
> |
assert(type >= 0 && type < LOG_TYPE_LAST); |
100 |
|
|
101 |
|
va_start(args, fmt); |
102 |
< |
vsprintf(buf, fmt, args); |
102 |
> |
vsnprintf(buf, sizeof(buf), fmt, args); |
103 |
|
va_end(args); |
104 |
|
|
143 |
– |
#ifdef USE_SYSLOG |
144 |
– |
if (priority <= L_DEBUG) |
145 |
– |
syslog(sysLogLevel[priority], "%s", buf); |
146 |
– |
#endif |
105 |
|
if (ConfigLoggingEntry.use_logging) |
106 |
< |
write_log(buf); |
107 |
< |
} |
150 |
< |
|
151 |
< |
void |
152 |
< |
init_log(const char *filename) |
153 |
< |
{ |
154 |
< |
open_log(filename); |
155 |
< |
#ifdef USE_SYSLOG |
156 |
< |
openlog("ircd", LOG_PID | LOG_NDELAY, LOG_FACILITY); |
157 |
< |
#endif |
158 |
< |
#ifndef SYSLOG_USERS |
159 |
< |
eventAddIsh("user_log_resync", user_log_resync, NULL, 60); |
160 |
< |
#endif |
161 |
< |
} |
162 |
< |
|
163 |
< |
void |
164 |
< |
reopen_log(const char *filename) |
165 |
< |
{ |
166 |
< |
if (logFile != NULL) |
167 |
< |
fbclose(logFile); |
168 |
< |
open_log(filename); |
169 |
< |
} |
170 |
< |
|
171 |
< |
void |
172 |
< |
set_log_level(const int level) |
173 |
< |
{ |
174 |
< |
if (L_ERROR < level && level <= L_DEBUG) |
175 |
< |
logLevel = level; |
176 |
< |
} |
177 |
< |
|
178 |
< |
int |
179 |
< |
get_log_level(void) |
180 |
< |
{ |
181 |
< |
return(logLevel); |
182 |
< |
} |
183 |
< |
|
184 |
< |
const char * |
185 |
< |
get_log_level_as_string(int level) |
186 |
< |
{ |
187 |
< |
if (level > L_DEBUG) |
188 |
< |
level = L_DEBUG; |
189 |
< |
else if (level < L_ERROR) |
190 |
< |
level = L_ERROR; |
191 |
< |
|
192 |
< |
return(logLevelToString[level]); |
193 |
< |
} |
194 |
< |
|
195 |
< |
/* log_user_exit() |
196 |
< |
* |
197 |
< |
* inputs - pointer to connecting client |
198 |
< |
* output - NONE |
199 |
< |
* side effects - Current exiting client is logged to |
200 |
< |
* either SYSLOG or to file. |
201 |
< |
*/ |
202 |
< |
void |
203 |
< |
log_user_exit(struct Client *source_p) |
204 |
< |
{ |
205 |
< |
time_t on_for = CurrentTime - source_p->firsttime; |
206 |
< |
#ifdef SYSLOG_USERS |
207 |
< |
if (IsClient(source_p)) |
208 |
< |
{ |
209 |
< |
ilog(L_INFO, "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu", |
210 |
< |
myctime(source_p->firsttime), (unsigned int)(on_for / 3600), |
211 |
< |
(unsigned int)((on_for % 3600)/60), (unsigned int)(on_for % 60), |
212 |
< |
source_p->name, source_p->username, source_p->host, |
213 |
< |
source_p->localClient->send.bytes>>10, |
214 |
< |
source_p->localClient->recv.bytes>>10); |
215 |
< |
} |
216 |
< |
#else |
217 |
< |
{ |
218 |
< |
char linebuf[BUFSIZ]; |
219 |
< |
|
220 |
< |
/* |
221 |
< |
* This conditional makes the logfile active only after |
222 |
< |
* it's been created - thus logging can be turned off by |
223 |
< |
* removing the file. |
224 |
< |
* -Taner |
225 |
< |
*/ |
226 |
< |
if (IsClient(source_p)) |
227 |
< |
{ |
228 |
< |
if (user_log_fb == NULL) |
229 |
< |
{ |
230 |
< |
if ((ConfigLoggingEntry.userlog[0] != '\0') && |
231 |
< |
(user_log_fb = fbopen(ConfigLoggingEntry.userlog, "r")) != NULL) |
232 |
< |
{ |
233 |
< |
fbclose(user_log_fb); |
234 |
< |
user_log_fb = fbopen(ConfigLoggingEntry.userlog, "a"); |
235 |
< |
} |
236 |
< |
} |
237 |
< |
|
238 |
< |
if (user_log_fb != NULL) |
239 |
< |
{ |
240 |
< |
size_t nbytes = ircsprintf(linebuf, |
241 |
< |
"%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu\n", |
242 |
< |
myctime(source_p->firsttime), |
243 |
< |
(unsigned int)(on_for / 3600), |
244 |
< |
(unsigned int)((on_for % 3600)/60), |
245 |
< |
(unsigned int)(on_for % 60), |
246 |
< |
source_p->name, source_p->username, source_p->host, |
247 |
< |
source_p->localClient->send.bytes>>10, |
248 |
< |
source_p->localClient->recv.bytes>>10); |
249 |
< |
fbputs(linebuf, user_log_fb, nbytes); |
250 |
< |
} |
251 |
< |
} |
252 |
< |
} |
253 |
< |
#endif |
254 |
< |
} |
255 |
< |
|
256 |
< |
#ifndef SYSLOG_USERS |
257 |
< |
/* user_log_resync() |
258 |
< |
* |
259 |
< |
* inputs - NONE |
260 |
< |
* output - NONE |
261 |
< |
* side effects - |
262 |
< |
*/ |
263 |
< |
static void |
264 |
< |
user_log_resync(void *notused) |
265 |
< |
{ |
266 |
< |
if (user_log_fb != NULL) |
267 |
< |
{ |
268 |
< |
fbclose(user_log_fb); |
269 |
< |
user_log_fb = NULL; |
270 |
< |
} |
271 |
< |
} |
272 |
< |
#endif |
273 |
< |
|
274 |
< |
/* log_oper_action() |
275 |
< |
* |
276 |
< |
* inputs - type of oper log entry |
277 |
< |
* - pointer to oper |
278 |
< |
* - const char *pattern == format string |
279 |
< |
* - var args for format string |
280 |
< |
* output - none |
281 |
< |
* side effects - corresponding log is written to, if its present. |
282 |
< |
* |
283 |
< |
* rewritten sept 5 2005 - Dianora |
284 |
< |
*/ |
285 |
< |
void |
286 |
< |
log_oper_action(int log_type, const struct Client *source_p, |
287 |
< |
const char *pattern, ...) |
288 |
< |
{ |
289 |
< |
va_list args; |
290 |
< |
char linebuf[IRCD_BUFSIZE]; |
291 |
< |
FBFILE *log_fb; |
292 |
< |
char *logfile; |
293 |
< |
const char *log_message; |
294 |
< |
size_t nbytes; |
295 |
< |
size_t n_preamble; |
296 |
< |
char *p; |
297 |
< |
|
298 |
< |
switch(log_type) |
299 |
< |
{ |
300 |
< |
case LOG_OPER_TYPE: |
301 |
< |
logfile = ConfigLoggingEntry.operlog; |
302 |
< |
log_message = "OPER"; |
303 |
< |
break; |
304 |
< |
case LOG_FAILED_OPER_TYPE: |
305 |
< |
logfile = ConfigLoggingEntry.failed_operlog; |
306 |
< |
log_message = "FAILED OPER"; |
307 |
< |
break; |
308 |
< |
case LOG_KLINE_TYPE: |
309 |
< |
logfile = ConfigLoggingEntry.klinelog; |
310 |
< |
log_message = "KLINE"; |
311 |
< |
break; |
312 |
< |
case LOG_RKLINE_TYPE: |
313 |
< |
logfile = ConfigLoggingEntry.klinelog; |
314 |
< |
log_message = "RKLINE"; |
315 |
< |
break; |
316 |
< |
case LOG_DLINE_TYPE: |
317 |
< |
logfile = ConfigLoggingEntry.klinelog; |
318 |
< |
log_message = "DLINE"; |
319 |
< |
break; |
320 |
< |
case LOG_TEMP_DLINE_TYPE: |
321 |
< |
logfile = ConfigLoggingEntry.klinelog; |
322 |
< |
log_message = "TEMP DLINE"; |
323 |
< |
break; |
324 |
< |
case LOG_TEMP_KLINE_TYPE: |
325 |
< |
logfile = ConfigLoggingEntry.klinelog; |
326 |
< |
log_message = "TEMP KLINE"; |
327 |
< |
break; |
328 |
< |
case LOG_GLINE_TYPE: |
329 |
< |
logfile = ConfigLoggingEntry.glinelog; |
330 |
< |
log_message = "GLINE"; |
331 |
< |
break; |
332 |
< |
case LOG_KILL_TYPE: |
333 |
< |
logfile = ConfigLoggingEntry.killlog; |
334 |
< |
log_message = "KILL"; |
335 |
< |
break; |
336 |
< |
case LOG_IOERR_TYPE: |
337 |
< |
logfile = ConfigLoggingEntry.ioerrlog; |
338 |
< |
log_message = "IO ERR"; |
339 |
< |
break; |
340 |
< |
default: |
341 |
< |
return; |
342 |
< |
} |
343 |
< |
|
344 |
< |
if (*logfile == '\0') |
345 |
< |
return; |
346 |
< |
|
347 |
< |
p = linebuf; |
348 |
< |
if (source_p != NULL) |
349 |
< |
{ |
350 |
< |
n_preamble = ircsprintf(linebuf, "%s %s by (%s!%s@%s) :", |
351 |
< |
myctime(CurrentTime), log_message, |
352 |
< |
source_p->name, source_p->username, source_p->host); |
353 |
< |
|
354 |
< |
} |
355 |
< |
else |
356 |
< |
{ |
357 |
< |
n_preamble = ircsprintf(linebuf, "%s %s :", |
358 |
< |
myctime(CurrentTime), log_message); |
359 |
< |
} |
360 |
< |
|
361 |
< |
p += n_preamble; |
362 |
< |
|
363 |
< |
if ((log_fb = fbopen(logfile, "r")) != NULL) |
364 |
< |
{ |
365 |
< |
fbclose(log_fb); |
366 |
< |
log_fb = fbopen(logfile, "a"); |
367 |
< |
if (log_fb == NULL) |
368 |
< |
return; |
369 |
< |
va_start(args, pattern); |
370 |
< |
/* XXX add check for IRCD_BUFSIZE-(n_preamble+1) < 0 ? -db */ |
371 |
< |
nbytes = vsnprintf(p, IRCD_BUFSIZE-(n_preamble+1), pattern, args); |
372 |
< |
nbytes += n_preamble; |
373 |
< |
va_end(args); |
374 |
< |
fbputs(linebuf, log_fb, nbytes); |
375 |
< |
fbclose(log_fb); |
376 |
< |
} |
377 |
< |
} |
106 |
> |
write_log(type, buf); |
107 |
> |
} |