1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* s_log.c: Logger functions. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
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" |
39 |
#include "s_log.h" |
40 |
#include "fileio.h" |
41 |
#include "irc_string.h" |
42 |
#include "sprintf_irc.h" |
43 |
#include "ircd.h" |
44 |
#include "s_misc.h" |
45 |
#include "event.h" /* Needed for EVH etc. */ |
46 |
#include "s_conf.h" |
47 |
#include "memory.h" |
48 |
|
49 |
/* some older syslogs would overflow at 2024 */ |
50 |
#define LOG_BUFSIZE 2000 |
51 |
|
52 |
static FBFILE *logFile; |
53 |
static int logLevel = INIT_LOG_LEVEL; |
54 |
|
55 |
#ifndef SYSLOG_USERS |
56 |
static EVH user_log_resync; |
57 |
static FBFILE *user_log_fb = NULL; |
58 |
#endif |
59 |
|
60 |
|
61 |
#ifdef USE_SYSLOG |
62 |
static const int sysLogLevel[] = |
63 |
{ |
64 |
LOG_CRIT, |
65 |
LOG_ERR, |
66 |
LOG_WARNING, |
67 |
LOG_NOTICE, |
68 |
LOG_INFO, |
69 |
LOG_INFO, |
70 |
LOG_INFO |
71 |
}; |
72 |
#endif |
73 |
|
74 |
static const char *logLevelToString[] = |
75 |
{ |
76 |
"L_CRIT", |
77 |
"L_ERROR", |
78 |
"L_WARN", |
79 |
"L_NOTICE", |
80 |
"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) |
91 |
{ |
92 |
logFile = fbopen(filename, "a"); |
93 |
|
94 |
if (logFile == NULL) |
95 |
{ |
96 |
#ifdef USE_SYSLOG |
97 |
syslog(LOG_ERR, "Unable to open log file: %s: %s", |
98 |
filename, strerror(errno)); |
99 |
#endif |
100 |
return (0); |
101 |
} |
102 |
|
103 |
return (1); |
104 |
} |
105 |
|
106 |
static void |
107 |
write_log(const char *message) |
108 |
{ |
109 |
char buf[LOG_BUFSIZE]; |
110 |
size_t nbytes = 0; |
111 |
|
112 |
if (logFile == NULL) |
113 |
return; |
114 |
|
115 |
#ifdef _WIN32 |
116 |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\r\n", |
117 |
smalldate(CurrentTime), message); |
118 |
#else |
119 |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
120 |
smalldate(CurrentTime), message); |
121 |
#endif |
122 |
fbputs(buf, logFile, nbytes); |
123 |
} |
124 |
|
125 |
void |
126 |
ilog(const int priority, const char *fmt, ...) |
127 |
{ |
128 |
char buf[LOG_BUFSIZE]; |
129 |
va_list args; |
130 |
|
131 |
assert(priority > -1); |
132 |
|
133 |
if (fmt == NULL) |
134 |
return; |
135 |
|
136 |
if (priority > logLevel) |
137 |
return; |
138 |
|
139 |
va_start(args, fmt); |
140 |
vsprintf(buf, fmt, args); |
141 |
va_end(args); |
142 |
|
143 |
#ifdef USE_SYSLOG |
144 |
if (priority <= L_DEBUG) |
145 |
syslog(sysLogLevel[priority], "%s", buf); |
146 |
#endif |
147 |
if (ConfigLoggingEntry.use_logging) |
148 |
write_log(buf); |
149 |
} |
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 |
} |