| 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 */ |
| 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" |
| 44 |
– |
#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; |
| 56 |
< |
static FBFILE *user_log_fb = NULL; |
| 57 |
< |
#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, |
| 67 |
< |
LOG_INFO, |
| 68 |
< |
LOG_INFO, |
| 69 |
< |
LOG_INFO |
| 70 |
< |
}; |
| 71 |
< |
#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", |
| 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) |
| 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", |
| 97 |
< |
filename, strerror(errno)); |
| 98 |
< |
#endif |
| 99 |
< |
return (0); |
| 100 |
< |
} |
| 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 |
< |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
| 84 |
< |
smalldate(CurrentTime), message); |
| 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, logFile, nbytes); |
| 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 |
< |
|
| 128 |
< |
if (fmt == NULL) |
| 129 |
< |
return; |
| 130 |
< |
|
| 131 |
< |
if (priority > logLevel) |
| 132 |
< |
return; |
| 98 |
> |
assert(fmt); |
| 99 |
> |
assert(type >= 0 && type < LOG_TYPE_LAST); |
| 100 |
|
|
| 101 |
|
va_start(args, fmt); |
| 102 |
|
vsnprintf(buf, sizeof(buf), fmt, args); |
| 103 |
|
va_end(args); |
| 104 |
|
|
| 138 |
– |
#ifdef USE_SYSLOG |
| 139 |
– |
if (priority <= L_DEBUG) |
| 140 |
– |
syslog(sysLogLevel[priority], "%s", buf); |
| 141 |
– |
#endif |
| 105 |
|
if (ConfigLoggingEntry.use_logging) |
| 106 |
< |
write_log(buf); |
| 107 |
< |
} |
| 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 |
< |
} |
| 106 |
> |
write_log(type, buf); |
| 107 |
> |
} |