1 /************************************************************************
2 * IRC - Internet Relay Chat, src/ircd_log.c
3 * Copyright (C) 1999 Thomas Helvey (BleepSoft)
4 * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "ircd_alloc.h"
29 #include "ircd_reply.h"
30 #include "ircd_snprintf.h"
31 #include "ircd_string.h"
44 #include <sys/types.h>
50 #define LOG_BUFSIZE 2048
52 /* select default log level cutoff */
54 # define L_DEFAULT L_DEBUG
56 # define L_DEFAULT L_INFO
59 #define LOG_DOSYSLOG 0x10
60 #define LOG_DOFILELOG 0x20
61 #define LOG_DOSNOTICE 0x40
63 #define LOG_DOMASK (LOG_DOSYSLOG | LOG_DOFILELOG | LOG_DOSNOTICE)
65 /* Map severity levels to strings and syslog levels */
66 static struct LevelData {
70 unsigned int snomask; /* 0 means use default in LogDesc */
72 #define L(level, syslog, mask) { L_ ## level, #level, (syslog), (mask) }
73 L(CRIT, LOG_CRIT, SNO_OLDSNO),
75 L(WARNING, LOG_WARNING, 0),
76 L(NOTICE, LOG_NOTICE, 0),
77 L(TRACE, LOG_INFO, 0),
79 L(DEBUG, LOG_INFO, SNO_DEBUG),
81 { L_LAST_LEVEL, 0, 0, 0 }
84 /* Just in case some implementation of syslog has them... */
89 #define LOG_NONE -1 /* don't syslog */
90 #define LOG_DEFAULT 0 /* syslog to logInfo.facility */
91 #define LOG_NOTFOUND -2 /* didn't find a facility corresponding to name */
93 /* Map names to syslog facilities--allows syslog configuration from .conf */
98 #define F(fac) { #fac, LOG_ ## fac }
99 F(NONE), F(DEFAULT), F(AUTH),
103 F(CRON), F(DAEMON), F(LOCAL0), F(LOCAL1), F(LOCAL2), F(LOCAL3),
104 F(LOCAL4), F(LOCAL5), F(LOCAL6), F(LOCAL7), F(LPR), F(MAIL),
105 F(NEWS), F(USER), F(UUCP),
110 #define SNO_NONE 0x00000000 /* don't send server notices */
111 #define SNO_NOTFOUND 0xffffffff /* didn't find a SNO_MASK value for name */
113 /* Map names to snomask values--allows configuration from .conf */
116 unsigned int snomask;
118 #define M(mask) { #mask, SNO_ ## mask }
119 M(NONE), M(OLDSNO), M(SERVKILL), M(OPERKILL), M(HACK2),
120 M(HACK3), M(UNAUTH), M(TCPCOMMON), M(TOOMANY), M(HACK4),
121 M(GLINE), M(NETWORK), M(IPMISMATCH), M(THROTTLE), M(OLDREALOP),
122 M(CONNEXIT), M(DEBUG),
127 #define LOG_MARK_FILE 0x0001 /* file has been changed */
128 #define LOG_MARK_FACILITY 0x0002 /* facility has been changed */
129 #define LOG_MARK_SNOMASK 0x0004 /* snomask has been changed */
130 #define LOG_MARK_LEVEL 0x0008 /* level has been changed */
132 /* Descriptions of all logging subsystems */
133 static struct LogDesc {
134 enum LogSys subsys; /* number for subsystem */
135 char *name; /* subsystem name */
136 struct LogFile *file; /* file descriptor for subsystem */
137 unsigned int mark; /* subsystem has been changed */
138 int def_fac; /* default facility */
139 unsigned int def_sno; /* default snomask */
140 int facility; /* -1 means don't use syslog */
141 unsigned int snomask; /* 0 means no server message */
142 enum LogLevel level; /* logging level */
144 #define S(sys, p, sn) { LS_##sys, #sys, 0, 0, (p), (sn), (p), (sn), L_DEFAULT }
146 S(CONFIG, 0, SNO_OLDSNO),
147 S(OPERMODE, -1, SNO_HACK4),
148 S(GLINE, -1, SNO_GLINE),
149 S(JUPE, -1, SNO_NETWORK),
151 S(NETWORK, -1, SNO_NETWORK),
155 S(OPER, -1, SNO_OLDREALOP),
158 S(DEBUG, -1, SNO_DEBUG),
161 { LS_LAST_SYSTEM, 0, 0, -1, 0, -1, 0 }
164 /* describes a log file */
166 struct LogFile *next; /* next log file descriptor */
167 struct LogFile **prev_p; /* what points to us */
168 int fd; /* file's descriptor-- -1 if not open */
169 int ref; /* how many things refer to us? */
170 char *file; /* file name */
173 /* modifiable static information */
175 struct LogFile *filelist; /* list of log files */
176 struct LogFile *freelist; /* list of free'd log files */
177 int facility; /* default facility */
178 const char *procname; /* process's name */
179 struct LogFile *dbfile; /* debug file */
180 } logInfo = { 0, 0, LOG_USER, "ircd", 0 };
182 /* helper routine to open a log file if needed */
184 log_open(struct LogFile *lf)
186 /* only open the file if we haven't already */
187 if (lf && lf->fd < 0) {
188 alarm(3); /* if NFS hangs, we hang only for 3 seconds */
189 lf->fd = open(lf->file, O_WRONLY | O_CREAT | O_APPEND,
197 /* reopen debug log */
199 log_debug_reopen(void)
201 if (!logInfo.dbfile) /* no open debugging file */
204 if (!logInfo.dbfile->file) { /* using terminal output */
205 logInfo.dbfile->fd = 2;
209 /* Ok, it's a real file; close it if necessary and use log_open to open it */
210 if (logInfo.dbfile->fd >= 0) {
211 close(logInfo.dbfile->fd);
212 logInfo.dbfile->fd = -1; /* mark that it's closed for log_open */
215 log_open(logInfo.dbfile);
217 if (logInfo.dbfile->fd < 0) { /* try again with /dev/null */
218 if ((logInfo.dbfile->fd = open("/dev/null", O_WRONLY)) < 0)
222 /* massage the file descriptor to be stderr */
223 if (logInfo.dbfile->fd != 2) {
225 fd = dup2(logInfo.dbfile->fd, 2);
226 close(logInfo.dbfile->fd);
227 logInfo.dbfile->fd = fd;
231 /* initialize debugging log */
233 log_debug_init(int usetty)
235 logInfo.dbfile = MyMalloc(sizeof(struct LogFile));
237 logInfo.dbfile->next = 0; /* initialize debugging filename */
238 logInfo.dbfile->prev_p = 0;
239 logInfo.dbfile->fd = -1;
240 logInfo.dbfile->ref = 1;
242 if (usetty) /* store pathname to use */
243 logInfo.dbfile->file = 0;
245 DupString(logInfo.dbfile->file, LOGFILE);
247 log_debug_reopen(); /* open the debug log */
249 logDesc[LS_DEBUG].file = logInfo.dbfile; /* remember where it went */
252 #endif /* DEBUGMODE */
254 /* set the debug log file */
256 log_debug_file(const char *file)
262 /* If we weren't started with debugging enabled, or if we're using
263 * the terminal, don't do anything at all.
265 if (!logInfo.dbfile || !logInfo.dbfile->file)
268 MyFree(logInfo.dbfile->file); /* free old pathname */
269 DupString(logInfo.dbfile->file, file); /* store new pathname */
271 log_debug_reopen(); /* reopen the debug log */
272 #endif /* DEBUGMODE */
276 /* called in place of open_log(), this stores the process name and prepares
280 log_init(const char *process_name)
282 /* store the process name; probably belongs in ircd.c, but oh well... */
283 if (!EmptyString(process_name))
284 logInfo.procname = process_name;
286 /* ok, open syslog; default facility: LOG_USER */
287 openlog(logInfo.procname, LOG_PID | LOG_NDELAY, logInfo.facility);
290 /* Files are persistently open; this closes and reopens them to allow
296 log_close(); /* close everything...we reopen on demand */
299 log_debug_reopen(); /* reopen debugging log if necessary */
300 #endif /* DEBUGMODE */
302 /* reopen syslog, if needed; default facility: LOG_USER */
303 openlog(logInfo.procname, LOG_PID | LOG_NDELAY, logInfo.facility);
306 /* close the log files */
312 closelog(); /* close syslog */
314 for (ptr = logInfo.filelist; ptr; ptr = ptr->next) {
316 close(ptr->fd); /* close all the files... */
321 if (logInfo.dbfile && logInfo.dbfile->file) {
322 if (logInfo.dbfile->fd >= 0)
323 close(logInfo.dbfile->fd); /* close the debug log file */
325 logInfo.dbfile->fd = -1;
329 /* These write entries to a log file */
331 log_write(enum LogSys subsys, enum LogLevel severity, unsigned int flags,
332 const char *fmt, ...)
337 log_vwrite(subsys, severity, flags, fmt, vl);
342 log_vwrite(enum LogSys subsys, enum LogLevel severity, unsigned int flags,
343 const char *fmt, va_list vl)
346 struct LogDesc *desc;
347 struct LevelData *ldata;
349 struct iovec vector[3];
351 char buf[LOG_BUFSIZE];
352 /* 1234567890123456789012 3 */
353 /* [2000-11-28 16:11:20] \0 */
356 /* check basic assumptions */
357 assert(-1 < (int)subsys);
358 assert((int)subsys < LS_LAST_SYSTEM);
359 assert(-1 < (int)severity);
360 assert((int)severity < L_LAST_LEVEL);
361 assert(0 == (flags & ~LOG_NOMASK));
364 /* find the log data and the severity data */
365 desc = &logDesc[subsys];
366 ldata = &levelData[severity];
368 /* check the set of ordering assumptions */
369 assert(desc->subsys == subsys);
370 assert(ldata->level == severity);
372 /* check severity... */
373 if (severity > desc->level)
376 /* figure out where all we need to log */
377 if (!(flags & LOG_NOFILELOG) && desc->file) {
378 log_open(desc->file);
379 if (desc->file->fd >= 0) /* don't log to file if we can't open the file */
380 flags |= LOG_DOFILELOG;
383 if (!(flags & LOG_NOSYSLOG) && desc->facility >= 0)
384 flags |= LOG_DOSYSLOG; /* will syslog */
386 if (!(flags & LOG_NOSNOTICE) && (desc->snomask != 0 || ldata->snomask != 0))
387 flags |= LOG_DOSNOTICE; /* will send a server notice */
389 /* short-circuit if there's nothing to do... */
390 if (!(flags & LOG_DOMASK))
393 /* Build the basic log string */
397 /* save the length for writev */
398 /* Log format: "SYSTEM [SEVERITY]: log message" */
400 ircd_snprintf(0, buf, sizeof(buf), "%s [%s]: %v", desc->name,
403 /* if we have something to write to... */
404 if (flags & LOG_DOFILELOG) {
406 tstamp = localtime(&curtime); /* build the timestamp */
409 ircd_snprintf(0, timebuf, sizeof(timebuf), "[%d-%d-%d %d:%02d:%02d] ",
410 tstamp->tm_year + 1900, tstamp->tm_mon + 1,
411 tstamp->tm_mday, tstamp->tm_hour, tstamp->tm_min,
414 /* set up the remaining parts of the writev vector... */
415 vector[0].iov_base = timebuf;
416 vector[1].iov_base = buf;
418 vector[2].iov_base = "\n"; /* terminate lines with a \n */
419 vector[2].iov_len = 1;
421 /* write it out to the log file */
422 writev(desc->file->fd, vector, 3);
425 /* oh yeah, syslog it too... */
426 if (flags & LOG_DOSYSLOG)
427 syslog(ldata->syslog | desc->facility, "%s", buf);
429 /* can't forget server notices... */
430 if (flags & LOG_DOSNOTICE)
431 sendto_opmask_butone(0, ldata->snomask ? ldata->snomask : desc->snomask,
435 /* log kills for fun and profit */
437 log_write_kill(const struct Client *victim, const struct Client *killer,
438 const char *inpath, const char *path)
441 log_write(IsServer(killer) ? LS_SERVKILL : LS_OPERKILL, L_TRACE, 0,
442 "A local client %#C KILLED by %#C Path: %s!%s",
443 victim, killer, inpath, path);
445 log_write(IsServer(killer) ? LS_SERVKILL : LS_OPERKILL, L_TRACE, 0,
446 "KILL from %C For %C Path: %s!%s", killer, victim, inpath, path);
449 /* return a struct LogFile for a specific filename--reference counted */
450 static struct LogFile *
451 log_file_create(const char *file)
457 /* if one already exists for that file, return it */
458 for (tmp = logInfo.filelist; tmp; tmp = tmp->next)
459 if (!strcmp(tmp->file, file)) {
464 if (logInfo.freelist) { /* pop one off the free list */
465 tmp = logInfo.freelist;
466 logInfo.freelist = tmp->next;
467 } else /* allocate a new one */
468 tmp = MyMalloc(sizeof(struct LogFile));
470 tmp->fd = -1; /* initialize the structure */
472 DupString(tmp->file, file);
474 tmp->next = logInfo.filelist; /* link it into the list... */
475 tmp->prev_p = &logInfo.filelist;
476 if (logInfo.filelist)
477 logInfo.filelist->prev_p = &tmp->next;
478 logInfo.filelist = tmp;
483 /* destroy a log file descriptor, under the control of the reference count */
485 log_file_destroy(struct LogFile *lf)
489 if (--lf->ref == 0) {
490 if (lf->next) /* clip it out of the list */
491 lf->next->prev_p = lf->prev_p;
492 *lf->prev_p = lf->next;
494 lf->prev_p = 0; /* we won't use it for the free list */
498 MyFree(lf->file); /* free the file name */
500 lf->next = logInfo.freelist; /* stack it onto the free list */
501 logInfo.freelist = lf;
505 /* finds a subsystem given its name */
506 static struct LogDesc *
507 log_find(const char *subsys)
513 /* find the named subsystem */
514 for (i = 0; i < LS_LAST_SYSTEM; i++)
515 if (!ircd_strcmp(subsys, logDesc[i].name))
518 return 0; /* not found */
521 /* canonicalize subsystem names */
523 log_canon(const char *subsys)
525 struct LogDesc *desc;
527 if (!(desc = log_find(subsys)))
533 /* find a level given its name */
535 log_lev_find(const char *level)
541 /* find the named level */
542 for (i = 0; levelData[i].string; i++)
543 if (!ircd_strcmp(level, levelData[i].string))
544 return levelData[i].level;
546 return L_LAST_LEVEL; /* not found */
549 /* return a name for a level */
551 log_lev_name(enum LogLevel lev)
553 assert(-1 < (int)lev);
554 assert((int)lev < L_LAST_LEVEL);
555 assert(lev == levelData[lev].level);
557 return levelData[lev].string;
560 /* find a facility given its name */
562 log_fac_find(const char *facility)
566 assert(0 != facility);
568 /* find the named facility */
569 for (i = 0; facilities[i].name; i++)
570 if (!ircd_strcmp(facility, facilities[i].name))
571 return facilities[i].facility;
573 return LOG_NOTFOUND; /* not found */
576 /* return a name for a facility */
578 log_fac_name(int fac)
582 /* find the facility */
583 for (i = 0; facilities[i].name; i++)
584 if (facilities[i].facility == fac)
585 return facilities[i].name;
587 return 0; /* not found; should never happen */
590 /* find a snomask value given its name */
592 log_sno_find(const char *maskname)
596 assert(0 != maskname);
598 /* find the named snomask */
599 for (i = 0; masks[i].name; i++)
600 if (!ircd_strcmp(maskname, masks[i].name))
601 return masks[i].snomask;
603 return SNO_NOTFOUND; /* not found */
606 /* return a name for a snomask value */
608 log_sno_name(unsigned int sno)
612 /* find the snomask */
613 for (i = 0; masks[i].name; i++)
614 if (masks[i].snomask == sno)
615 return masks[i].name;
617 return 0; /* not found; should never happen */
620 /* set the log file for a subsystem */
622 log_set_file(const char *subsys, const char *filename)
624 struct LogDesc *desc;
627 if (!(desc = log_find(subsys)))
631 desc->mark |= LOG_MARK_FILE; /* mark that file has been changed */
633 desc->mark &= ~LOG_MARK_FILE; /* file has been reset to defaults */
635 /* no change, don't go to the trouble of destroying and recreating */
636 if (desc->file && filename && !strcmp(desc->file->file, filename))
639 /* debug log is special, since it has to be opened on fd 2 */
640 if (desc->subsys == LS_DEBUG)
641 return log_debug_file(filename);
643 if (desc->file) /* destroy previous entry... */
644 log_file_destroy(desc->file);
646 /* set the file to use */
647 desc->file = filename ? log_file_create(filename) : 0;
652 /* get the log file for a subsystem */
654 log_get_file(const char *subsys)
656 struct LogDesc *desc;
659 if (!(desc = log_find(subsys)))
662 return desc->file ? desc->file->file : 0;
665 /* set the facility for a subsystem */
667 log_set_facility(const char *subsys, const char *facility)
669 struct LogDesc *desc;
673 if (!(desc = log_find(subsys)))
676 /* set syslog facility */
677 if (EmptyString(facility)) {
678 desc->facility = desc->def_fac;
679 desc->mark &= ~LOG_MARK_FACILITY;
680 } else if ((fac = log_fac_find(facility)) != LOG_NOTFOUND) {
681 desc->facility = fac;
682 if (fac == desc->def_fac)
683 desc->mark &= ~LOG_MARK_FACILITY;
685 desc->mark |= LOG_MARK_FACILITY;
692 /* get the facility for a subsystem */
694 log_get_facility(const char *subsys)
696 struct LogDesc *desc;
699 if (!(desc = log_find(subsys)))
702 /* find the facility's name */
703 return log_fac_name(desc->facility);
706 /* set the snomask value for a subsystem */
708 log_set_snomask(const char *subsys, const char *snomask)
710 struct LogDesc *desc;
711 unsigned int sno = SNO_DEFAULT;
714 if (!(desc = log_find(subsys)))
717 /* set snomask value */
718 if (EmptyString(snomask)) {
719 desc->snomask = desc->def_sno;
720 desc->mark &= ~LOG_MARK_SNOMASK;
721 } else if ((sno = log_sno_find(snomask)) != SNO_NOTFOUND) {
723 if (sno == desc->def_sno)
724 desc->mark &= ~LOG_MARK_SNOMASK;
726 desc->mark |= LOG_MARK_SNOMASK;
733 /* get the snomask value for a subsystem */
735 log_get_snomask(const char *subsys)
737 struct LogDesc *desc;
740 if (!(desc = log_find(subsys)))
743 /* find the snomask value's name */
744 return log_sno_name(desc->snomask);
747 /* set the level for a subsystem */
749 log_set_level(const char *subsys, const char *level)
751 struct LogDesc *desc;
755 if (!(desc = log_find(subsys)))
758 /* set logging level */
759 if (EmptyString(level)) {
760 desc->level = L_DEFAULT;
761 desc->mark &= ~LOG_MARK_LEVEL;
762 } else if ((lev = log_lev_find(level)) != L_LAST_LEVEL) {
764 if (lev == L_DEFAULT)
765 desc->mark &= ~LOG_MARK_LEVEL;
767 desc->mark |= LOG_MARK_LEVEL;
774 /* get the level for a subsystem */
776 log_get_level(const char *subsys)
778 struct LogDesc *desc;
781 if (!(desc = log_find(subsys)))
784 /* find the level's name */
785 return log_lev_name(desc->level);
788 /* set the overall default syslog facility */
790 log_set_default(const char *facility)
794 oldfac = logInfo.facility;
796 if (EmptyString(facility))
797 logInfo.facility = LOG_USER;
798 else if ((fac = log_fac_find(facility)) != LOG_NOTFOUND &&
799 fac != LOG_NONE && fac != LOG_DEFAULT)
800 logInfo.facility = fac;
804 if (logInfo.facility != oldfac) {
805 closelog(); /* reopen syslog with new facility setting */
806 openlog(logInfo.procname, LOG_PID | LOG_NDELAY, logInfo.facility);
812 /* get the overall default syslog facility */
814 log_get_default(void)
816 /* find the facility's name */
817 return log_fac_name(logInfo.facility);
820 /* Clear the marks... */
822 log_feature_unmark(void)
826 for (i = 0; i < LS_LAST_SYSTEM; i++)
830 /* Reset unmarked fields to defaults... */
832 log_feature_mark(int flag)
839 for (i = 0; i < LS_LAST_SYSTEM; i++) {
840 if (!(logDesc[i].mark & LOG_MARK_FILE)) {
841 if (logDesc[i].subsys != LS_DEBUG) { /* debug is special */
842 if (logDesc[i].file) /* destroy previous entry... */
843 log_file_destroy(logDesc[i].file);
848 if (!(logDesc[i].mark & LOG_MARK_FACILITY)) /* set default facility */
849 logDesc[i].facility = logDesc[i].def_fac;
851 if (!(logDesc[i].mark & LOG_MARK_SNOMASK)) /* set default snomask */
852 logDesc[i].snomask = logDesc[i].def_sno;
854 if (!(logDesc[i].mark & LOG_MARK_LEVEL)) /* set default level */
855 logDesc[i].level = L_DEFAULT;
858 return 0; /* we don't have a notify handler */
861 /* Report feature settings */
863 log_feature_report(struct Client *to, int flag)
867 for (i = 0; i < LS_LAST_SYSTEM; i++) {
868 if (logDesc[i].mark & LOG_MARK_FILE) /* report file */
869 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F LOG %s FILE %s",
870 logDesc[i].name, logDesc[i].file->file);
872 if (logDesc[i].mark & LOG_MARK_FACILITY) /* report facility */
873 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F LOG %s FACILITY %s",
874 logDesc[i].name, log_fac_name(logDesc[i].facility));
876 if (logDesc[i].mark & LOG_MARK_SNOMASK) /* report snomask */
877 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F LOG %s SNOMASK %s",
878 logDesc[i].name, log_sno_name(logDesc[i].snomask));
880 if (logDesc[i].mark & LOG_MARK_LEVEL) /* report log level */
881 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F LOG %s LEVEL %s",
882 logDesc[i].name, log_lev_name(logDesc[i].level));
885 if (flag) /* report default facility */
886 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F LOG %s",
887 log_fac_name(logInfo.facility));