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