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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
|
/*
* atheme-services: A collection of minimalist IRC services
* logger.c: Logging routines
*
* Copyright (c) 2005-2009 Atheme Project (http://www.atheme.org)
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "atheme.h"
static logfile_t *log_file;
int log_force;
static mowgli_list_t log_files = { NULL, NULL, 0 };
/* private destructor function for logfile_t. */
static void logfile_delete_file(void *vdata)
{
logfile_t *lf = (logfile_t *) vdata;
logfile_unregister(lf);
fclose(lf->log_file);
free(lf->log_path);
metadata_delete_all(lf);
free(lf);
}
static void logfile_delete_channel(void *vdata)
{
logfile_t *lf = (logfile_t *) vdata;
logfile_unregister(lf);
free(lf->log_path);
metadata_delete_all(lf);
free(lf);
}
static void logfile_join_channels(channel_t *c)
{
mowgli_node_t *n;
return_if_fail(c != NULL);
MOWGLI_ITER_FOREACH(n, log_files.head)
{
logfile_t *lf = n->data;
if (!irccasecmp(c->name, lf->log_path))
{
if (!(c->flags & CHAN_LOG))
{
c->flags |= CHAN_LOG;
joinall(lf->log_path);
}
return;
}
}
}
static void logfile_join_service(service_t *svs)
{
mowgli_node_t *n;
channel_t *c;
return_if_fail(svs != NULL && svs->me != NULL);
/* no botserv bots */
if (svs->botonly)
return;
MOWGLI_ITER_FOREACH(n, log_files.head)
{
logfile_t *lf = n->data;
if (!VALID_GLOBAL_CHANNEL_PFX(lf->log_path))
continue;
c = channel_find(lf->log_path);
if (c == NULL || !(c->flags & CHAN_LOG))
continue;
join(c->name, svs->me->nick);
}
}
static void logfile_part_removed(void *unused)
{
channel_t *c;
mowgli_patricia_iteration_state_t state;
mowgli_node_t *n;
bool valid;
MOWGLI_PATRICIA_FOREACH(c, &state, chanlist)
{
if (!(c->flags & CHAN_LOG))
continue;
valid = false;
MOWGLI_ITER_FOREACH(n, log_files.head)
{
logfile_t *lf = n->data;
if (!irccasecmp(c->name, lf->log_path))
{
valid = true;
break;
}
}
if (!valid)
{
c->flags &= ~CHAN_LOG;
partall(c->name);
}
}
}
/*
* logfile_strip_control_codes(const char *buf)
*
* Duplicates a string, stripping control codes in the
* process.
*
* Inputs:
* - buffer to strip
*
* Outputs:
* - duplicated buffer without control codes
*
* Side Effects:
* - none
*/
static const char *
logfile_strip_control_codes(const char *buf)
{
static char outbuf[BUFSIZE];
const char *in = buf;
char *out = outbuf;
for (; *in != '\0'; in++)
{
if (*in > 31)
{
*out++ = *in;
continue;
}
else if (*in == 3)
{
in++;
while (isdigit((unsigned char)*in))
in++;
}
}
*out = '\0';
return outbuf;
}
/*
* logfile_write(logfile_t *lf, const char *buf)
*
* Writes an I/O stream to a static file.
*
* Inputs:
* - logfile_t representing the I/O stream.
* - data to write to the file
*
* Outputs:
* - none
*
* Side Effects:
* - none
*/
static void logfile_write(logfile_t *lf, const char *buf)
{
char datetime[BUFSIZE];
time_t t;
struct tm tm;
return_if_fail(lf != NULL);
return_if_fail(lf->log_file != NULL);
return_if_fail(buf != NULL);
time(&t);
tm = *localtime(&t);
strftime(datetime, sizeof datetime, "[%Y-%m-%d %H:%M:%S]", &tm);
fprintf((FILE *) lf->log_file, "%s %s\n", datetime, logfile_strip_control_codes(buf));
fflush((FILE *) lf->log_file);
}
/*
* logfile_write_irc(logfile_t *lf, const char *buf)
*
* Writes an I/O stream to an IRC target.
*
* Inputs:
* - logfile_t representing the I/O stream.
* - data to write to the IRC target
*
* Outputs:
* - none
*
* Side Effects:
* - none
*/
static void logfile_write_irc(logfile_t *lf, const char *buf)
{
channel_t *c;
return_if_fail(lf != NULL);
return_if_fail(lf->log_path != NULL);
return_if_fail(buf != NULL);
if (!me.connected || me.bursting)
return;
c = channel_find(lf->log_path);
if (c != NULL && c->flags & CHAN_LOG)
{
size_t targetlen;
char targetbuf[NICKLEN];
service_t *svs = NULL;
memset(targetbuf, '\0', sizeof targetbuf);
targetlen = (strchr(buf, ' ') - buf);
if (targetlen < NICKLEN)
{
mowgli_strlcpy(targetbuf, buf, sizeof targetbuf);
targetbuf[targetlen] = '\0';
svs = service_find_nick(targetbuf);
targetlen++;
if (svs == NULL)
targetlen = 0;
}
else
targetlen = 0;
if (svs == NULL)
svs = service_find("operserv");
if (svs == NULL)
svs = service_find_any();
if (svs != NULL && svs->me != NULL)
msg(svs->me->nick, c->name, "%s", (buf + targetlen));
else
wallops("%s", (buf + targetlen));
}
}
/*
* logfile_write_snotices(logfile_t *lf, const char *buf)
*
* Writes an I/O stream to IRC snotes.
*
* Inputs:
* - logfile_t representing the I/O stream.
* - data to write to the IRC target
*
* Outputs:
* - none
*
* Side Effects:
* - none
*/
static void logfile_write_snotices(logfile_t *lf, const char *buf)
{
channel_t *c;
return_if_fail(lf != NULL);
return_if_fail(lf->log_path != NULL);
return_if_fail(buf != NULL);
if (!me.connected || me.bursting)
return;
wallops("%s", buf);
}
/*
* logfile_register(logfile_t *lf)
*
* Registers a log I/O stream.
*
* Inputs:
* - logfile_t representing the I/O stream.
*
* Outputs:
* - none
*
* Side Effects:
* - log_files is populated with the given object.
*/
void logfile_register(logfile_t *lf)
{
mowgli_node_add(lf, &lf->node, &log_files);
}
/*
* logfile_unregister(logfile_t *lf)
*
* Unregisters a log I/O stream.
*
* Inputs:
* - logfile_t representing the I/O stream.
*
* Outputs:
* - none
*
* Side Effects:
* - the given object is removed from log_files, but remains valid.
*/
void logfile_unregister(logfile_t *lf)
{
mowgli_node_delete(&lf->node, &log_files);
}
/*
* logfile_new(const char *log_path, unsigned int log_mask)
*
* Logfile object factory.
*
* Inputs:
* - path to new logfile
* - bitmask of events to log
*
* Outputs:
* - a logfile_t object describing how this logfile should be used
*
* Side Effects:
* - log_files is populated with the newly created logfile_t.
*/
logfile_t *logfile_new(const char *path, unsigned int log_mask)
{
static bool hooked = false;
static time_t lastfail = 0;
channel_t *c;
logfile_t *lf = scalloc(sizeof(logfile_t), 1);
if (!strcasecmp(path, "!snotices") || !strcasecmp(path, "!wallops"))
{
object_init(object(lf), path, logfile_delete_channel);
lf->log_path = sstrdup(path);
lf->log_type = LOG_INTERACTIVE;
lf->write_func = logfile_write_snotices;
}
else if (!VALID_GLOBAL_CHANNEL_PFX(path))
{
object_init(object(lf), path, logfile_delete_file);
if ((lf->log_file = fopen(path, "a")) == NULL)
{
free(lf);
if (me.connected && lastfail + 3600 < CURRTIME)
{
lastfail = CURRTIME;
wallops(_("Could not open log file (%s), log entries will be missing!"), strerror(errno));
}
return NULL;
}
#ifdef FD_CLOEXEC
fcntl(fileno((FILE *)lf->log_file), F_SETFD, FD_CLOEXEC);
#endif
lf->log_path = sstrdup(path);
lf->log_type = LOG_NONINTERACTIVE;
lf->write_func = logfile_write;
}
else
{
object_init(object(lf), path, logfile_delete_channel);
lf->log_path = sstrdup(path);
lf->log_type = LOG_INTERACTIVE;
lf->write_func = logfile_write_irc;
c = channel_find(lf->log_path);
if (me.connected && c != NULL)
{
joinall(lf->log_path);
c->flags |= CHAN_LOG;
}
if (!hooked)
{
hook_add_event("channel_add");
hook_add_channel_add(logfile_join_channels);
hook_add_event("service_introduce");
hook_add_service_introduce(logfile_join_service);
hook_add_event("config_ready");
hook_add_config_ready(logfile_part_removed);
hooked = true;
}
}
lf->log_mask = log_mask;
logfile_register(lf);
return lf;
}
/*
* log_open(void)
*
* Initializes the logging subsystem.
*
* Inputs:
* - none
*
* Outputs:
* - none
*
* Side Effects:
* - the log_files list is populated with the master
* atheme.log reference.
*/
void log_open(void)
{
log_file = logfile_new(log_path, LG_ERROR | LG_INFO | LG_CMD_ADMIN);
}
/*
* log_shutdown(void)
*
* Shuts down the logging subsystem.
*
* Inputs:
* - none
*
* Outputs:
* - none
*
* Side Effects:
* - logfile_t objects in the log_files list are destroyed.
*/
void log_shutdown(void)
{
mowgli_node_t *n, *tn;
MOWGLI_ITER_FOREACH_SAFE(n, tn, log_files.head)
object_unref(n->data);
}
/*
* log_debug_enabled(void)
*
* Determines whether debug logging (LG_DEBUG and/or LG_RAWDATA) is enabled.
*
* Inputs:
* - none
*
* Outputs:
* - boolean
*
* Side Effects:
* - none
*/
bool log_debug_enabled(void)
{
mowgli_node_t *n;
logfile_t *lf;
if (log_force)
return true;
MOWGLI_ITER_FOREACH(n, log_files.head)
{
lf = n->data;
if (lf->log_mask & (LG_DEBUG | LG_RAWDATA))
return true;
}
return false;
}
/*
* log_master_set_mask(unsigned int mask)
*
* Sets the logging mask for the main log file.
*
* Inputs:
* - mask
*
* Outputs:
* - none
*
* Side Effects:
* - logging mask updated
*/
void log_master_set_mask(unsigned int mask)
{
/* couldn't be opened etc */
if (log_file == NULL)
return;
log_file->log_mask = mask;
}
/*
* logfile_find_mask(unsigned int log_mask)
*
* Returns a log file that logs all of the bits in log_mask (and possibly
* more), or NULL if there is none. If an exact match exists, it is returned.
*
* Inputs:
* - log mask
*
* Outputs:
* - logfile_t object pointer, guaranteed to be a file, not some
* other kind of log stream
*
* Side Effects:
* - none
*/
logfile_t *logfile_find_mask(unsigned int log_mask)
{
mowgli_node_t *n;
logfile_t *lf;
MOWGLI_ITER_FOREACH(n, log_files.head)
{
lf = n->data;
if (lf->write_func != logfile_write)
continue;
if (lf->log_mask == log_mask)
return lf;
}
MOWGLI_ITER_FOREACH(n, log_files.head)
{
lf = n->data;
if (lf->write_func != logfile_write)
continue;
if ((lf->log_mask & log_mask) == log_mask)
return lf;
}
return NULL;
}
static void vslog_ext(log_type_t type, unsigned int level, const char *fmt,
va_list args)
{
static bool in_slog = false;
char buf[BUFSIZE];
mowgli_node_t *n;
char datetime[BUFSIZE];
time_t t;
struct tm tm;
if (in_slog)
return;
in_slog = true;
vsnprintf(buf, BUFSIZE, fmt, args);
time(&t);
tm = *localtime(&t);
strftime(datetime, sizeof datetime, "[%Y-%m-%d %H:%M:%S]", &tm);
MOWGLI_ITER_FOREACH(n, log_files.head)
{
logfile_t *lf = (logfile_t *) n->data;
if (type != LOG_ANY && type != lf->log_type)
continue;
if ((lf != log_file || !log_force) && !(level & lf->log_mask))
continue;
return_if_fail(lf->write_func != NULL);
lf->write_func(lf, buf);
}
/*
* if the event is in the default loglevel, and we are starting, then
* display it in the controlling terminal.
*/
if (type != LOG_INTERACTIVE && ((runflags & (RF_LIVE | RF_STARTING) &&
(log_file != NULL ? log_file->log_mask : LG_ERROR | LG_INFO) & level) ||
(runflags & RF_LIVE && log_force)))
fprintf(stderr, "%s %s\n", datetime, logfile_strip_control_codes(buf));
in_slog = false;
}
static PRINTFLIKE(3, 4) void slog_ext(log_type_t type, unsigned int level,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vslog_ext(type, level, fmt, args);
va_end(args);
}
/*
* slog(unsigned int level, const char *fmt, ...)
*
* Handles the basic logging of log messages to the log files defined
* in the configuration file. All I/O is handled here, and no longer
* in the various sourceinfo_t log transforms.
*
* Inputs:
* - a bitmask of the various log categories this event qualifies to
* - a printf-style format string
* - (optional) additional arguments to be used with that format string
*
* Outputs:
* - none
*
* Side Effects:
* - logfiles are updated depending on how they are configured.
*/
void slog(unsigned int level, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vslog_ext(LOG_ANY, level, fmt, args);
va_end(args);
}
/*
* logcommand(sourceinfo_t *si, int level, const char *fmt, ...)
*
* Logs usage of a command from a user or other source (RPC call) as
* described by sourceinfo_t.
*
* Inputs:
* - sourceinfo_t object which describes the source of the command call
* - bitmask of log categories to log the command use to.
* - printf-style format string
* - (optional) additional parameters
*
* Outputs:
* - none
*
* Side Effects:
* - qualifying logfile_t objects in log_files are updated.
*/
void logcommand(sourceinfo_t *si, int level, const char *fmt, ...)
{
va_list args;
char lbuf[BUFSIZE];
va_start(args, fmt);
vsnprintf(lbuf, BUFSIZE, fmt, args);
va_end(args);
if (si->su != NULL)
logcommand_user(si->service, si->su, level, "%s", lbuf);
else
logcommand_external(si->service, si->v != NULL ? si->v->description : "unknown", si->connection, si->sourcedesc, si->smu, level, "%s", lbuf);
}
/*
* logcommand_user(service_t *svs, user_t *source, int level, const char *fmt, ...)
*
* Logs usage of a command from a user as described by sourceinfo_t.
*
* Inputs:
* - service_t object which describes the service the command is attached to
* - user_t object which describes the source of the command call
* - bitmask of log categories to log the command use to.
* - printf-style format string
* - (optional) additional parameters
*
* Outputs:
* - none
*
* Side Effects:
* - qualifying logfile_t objects in log_files are updated.
*/
void logcommand_user(service_t *svs, user_t *source, int level, const char *fmt, ...)
{
va_list args;
char lbuf[BUFSIZE];
char accountbuf[NICKLEN * 5]; /* entity name len is NICKLEN * 4, plus another for the ID */
bool showaccount;
va_start(args, fmt);
vsnprintf(lbuf, BUFSIZE, fmt, args);
va_end(args);
accountbuf[0] = '\0';
if (source->myuser != NULL)
snprintf(accountbuf, sizeof accountbuf, "%s/%s",
entity(source->myuser)->name, entity(source->myuser)->id);
slog_ext(LOG_NONINTERACTIVE, level, "%s %s:%s!%s@%s[%s] %s",
service_get_log_target(svs),
accountbuf,
source->nick, source->user, source->host,
source->ip != NULL ? source->ip : source->host,
lbuf);
showaccount = source->myuser == NULL || irccasecmp(entity(source->myuser)->name, source->nick);
slog_ext(LOG_INTERACTIVE, level, "%s %s%s%s%s %s",
service_get_log_target(svs),
source->nick,
showaccount ? " (" : "",
showaccount ? (source->myuser ? entity(source->myuser)->name : "") : "",
showaccount ? ")" : "",
lbuf);
}
/*
* logcommand_external(service_t *svs, const char *type, connection_t *source,
* const char *sourcedesc, myuser_t *login, int level, const char *fmt, ...)
*
* Logs usage of a command from an RPC call as described by sourceinfo_t.
*
* Inputs:
* - service_t object which describes the service the command is attached to
* - string which describes the type of RPC call
* - connection_t which describes the socket source of the RPC call
* - string which describes the socket source of the RPC call
* - myuser_t which describes the services account used for the RPC call
* - bitmask of log categories to log the command use to.
* - printf-style format string
* - (optional) additional parameters
*
* Outputs:
* - none
*
* Side Effects:
* - qualifying logfile_t objects in log_files are updated.
*/
void logcommand_external(service_t *svs, const char *type, connection_t *source, const char *sourcedesc, myuser_t *mu, int level, const char *fmt, ...)
{
va_list args;
char lbuf[BUFSIZE];
va_start(args, fmt);
vsnprintf(lbuf, BUFSIZE, fmt, args);
va_end(args);
slog_ext(LOG_NONINTERACTIVE, level, "%s %s:%s(%s)[%s] %s",
service_get_log_target(svs),
mu != NULL ? entity(mu)->name : "",
type,
source != NULL ? source->hbuf : "<noconn>",
sourcedesc != NULL ? sourcedesc : "<unknown>",
lbuf);
slog_ext(LOG_INTERACTIVE, level, "%s <%s>%s %s",
service_get_log_target(svs),
type,
mu != NULL ? entity(mu)->name : "",
lbuf);
}
/*
* logaudit_denycmd(sourceinfo_t *si, command_t *cmd, const char *userlevel)
*
* Logs a command access denial for auditing.
*
* Inputs:
* - sourceinfo_t object that was denied
* - command_t object that was denied
* - optional userlevel (if none, will be NULL)
*
* Outputs:
* - nothing
*
* Side Effects:
* - qualifying logfile_t objects in log_files are updated
*/
void logaudit_denycmd(sourceinfo_t *si, command_t *cmd, const char *userlevel)
{
slog_ext(LOG_NONINTERACTIVE, LG_DENYCMD, "DENYCMD: [%s] was denied execution of [%s], need privileges [%s %s]",
get_source_security_label(si), cmd->name, cmd->access, userlevel != NULL ? userlevel : "");
slog_ext(LOG_INTERACTIVE, LG_DENYCMD, "DENYCMD: \2%s\2 was denied execution of \2%s\2, need privileges \2%s %s\2",
get_source_security_label(si), cmd->name, cmd->access, userlevel != NULL ? userlevel : "");
}
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
* vim:ts=8
* vim:sw=8
* vim:noexpandtab
*/
|