1997-12-21 12:11:13 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
|
|
|
|
*
|
1998-01-21 02:15:33 +00:00
|
|
|
* $Id: log.c,v 1.24 1997/12/24 09:29:05 brian Exp $
|
1997-10-26 01:04:02 +00:00
|
|
|
*/
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
#include "command.h"
|
1997-06-09 03:27:43 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "loadalias.h"
|
1997-11-09 14:18:55 +00:00
|
|
|
#include "defs.h"
|
1997-06-09 03:27:43 +00:00
|
|
|
#include "vars.h"
|
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
static const char *LogNames[] = {
|
1997-08-25 00:29:32 +00:00
|
|
|
"Async",
|
|
|
|
"Carrier",
|
|
|
|
"CCP",
|
|
|
|
"Chat",
|
|
|
|
"Command",
|
|
|
|
"Connect",
|
|
|
|
"Debug",
|
|
|
|
"HDLC",
|
1997-11-09 06:22:49 +00:00
|
|
|
"ID0",
|
1997-08-25 00:29:32 +00:00
|
|
|
"IPCP",
|
|
|
|
"LCP",
|
|
|
|
"Link",
|
|
|
|
"LQM",
|
|
|
|
"Phase",
|
|
|
|
"TCP/IP",
|
|
|
|
"Tun",
|
|
|
|
"Warning",
|
|
|
|
"Error",
|
|
|
|
"Alert"
|
1997-06-09 03:27:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MSK(n) (1<<((n)-1))
|
|
|
|
|
|
|
|
static u_long LogMask = MSK(LogLINK) | MSK(LogCARRIER) | MSK(LogPHASE);
|
1997-11-04 01:17:05 +00:00
|
|
|
static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
|
1997-06-09 03:27:43 +00:00
|
|
|
static int LogTunno = -1;
|
|
|
|
|
|
|
|
static int
|
|
|
|
syslogLevel(int lev)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
switch (lev) {
|
|
|
|
case LogDEBUG:return LOG_DEBUG;
|
|
|
|
case LogWARN:
|
|
|
|
return LOG_WARNING;
|
|
|
|
case LogERROR:
|
|
|
|
return LOG_ERR;
|
|
|
|
case LogALERT:
|
|
|
|
return LOG_ALERT;
|
|
|
|
}
|
|
|
|
return lev >= LogMIN && lev <= LogMAX ? LOG_INFO : 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
const char *
|
|
|
|
LogName(int id)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
return id < LogMIN || id > LogMAX ? "Unknown" : LogNames[id - 1];
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-06-09 03:27:43 +00:00
|
|
|
LogKeep(int id)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
if (id >= LogMIN && id <= LogMAXCONF)
|
|
|
|
LogMask |= MSK(id);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-11-04 01:17:05 +00:00
|
|
|
void
|
|
|
|
LogKeepLocal(int id)
|
|
|
|
{
|
|
|
|
if (id >= LogMIN && id <= LogMAXCONF)
|
|
|
|
LogMaskLocal |= MSK(id);
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1997-06-09 03:27:43 +00:00
|
|
|
LogDiscard(int id)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
if (id >= LogMIN && id <= LogMAXCONF)
|
|
|
|
LogMask &= ~MSK(id);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-11-04 01:17:05 +00:00
|
|
|
void
|
|
|
|
LogDiscardLocal(int id)
|
|
|
|
{
|
|
|
|
if (id >= LogMIN && id <= LogMAXCONF)
|
|
|
|
LogMaskLocal &= ~MSK(id);
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1997-06-09 03:27:43 +00:00
|
|
|
LogDiscardAll()
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
LogMask = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-11-04 01:17:05 +00:00
|
|
|
void
|
|
|
|
LogDiscardAllLocal()
|
|
|
|
{
|
|
|
|
LogMaskLocal = 0;
|
|
|
|
}
|
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
int
|
|
|
|
LogIsKept(int id)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-11-04 01:17:05 +00:00
|
|
|
if (id < LogMIN || id > LogMAX)
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
1997-11-04 01:17:05 +00:00
|
|
|
if (id > LogMAXCONF)
|
|
|
|
return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
|
|
|
|
|
|
|
|
return ((LogMaskLocal & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
|
|
|
|
((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
void
|
1997-06-09 03:27:43 +00:00
|
|
|
LogOpen(const char *Name)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
openlog(Name, LOG_PID, LOG_DAEMON);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1997-06-09 03:27:43 +00:00
|
|
|
LogSetTun(int tunno)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
LogTunno = tunno;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-06-09 03:27:43 +00:00
|
|
|
LogClose()
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
closelog();
|
|
|
|
LogTunno = -1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-11-22 03:37:54 +00:00
|
|
|
LogPrintf(int lev, const char *fmt,...)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (LogIsKept(lev)) {
|
|
|
|
static char nfmt[200];
|
|
|
|
|
1997-11-04 01:17:05 +00:00
|
|
|
if ((LogIsKept(lev) & LOG_KEPT_LOCAL) && VarTerm) {
|
|
|
|
if ((LogIsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1)
|
|
|
|
snprintf(nfmt, sizeof nfmt, "tun%d: %s: %s",
|
|
|
|
LogTunno, LogName(lev), fmt);
|
|
|
|
else
|
|
|
|
snprintf(nfmt, sizeof nfmt, "%s: %s", LogName(lev), fmt);
|
|
|
|
vfprintf(VarTerm, nfmt, ap);
|
1997-11-11 12:26:34 +00:00
|
|
|
fflush(VarTerm);
|
1997-11-04 01:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((LogIsKept(lev) & LOG_KEPT_SYSLOG) && (lev != LogWARN || !VarTerm)) {
|
|
|
|
if ((LogIsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1)
|
|
|
|
snprintf(nfmt, sizeof nfmt, "tun%d: %s: %s",
|
|
|
|
LogTunno, LogName(lev), fmt);
|
|
|
|
else
|
|
|
|
snprintf(nfmt, sizeof nfmt, "%s: %s", LogName(lev), fmt);
|
1997-08-25 00:29:32 +00:00
|
|
|
vsyslog(syslogLevel(lev), nfmt, ap);
|
1997-11-04 01:17:05 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
va_end(ap);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-11-22 03:37:54 +00:00
|
|
|
LogDumpBp(int lev, const char *hdr, const struct mbuf * bp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-10-22 01:22:51 +00:00
|
|
|
if (LogIsKept(lev)) {
|
1997-11-04 01:17:05 +00:00
|
|
|
char buf[50];
|
1997-10-22 01:22:51 +00:00
|
|
|
char *b;
|
|
|
|
u_char *ptr;
|
|
|
|
int f;
|
|
|
|
|
|
|
|
if (hdr && *hdr)
|
|
|
|
LogPrintf(lev, "%s\n", hdr);
|
|
|
|
|
|
|
|
b = buf;
|
|
|
|
do {
|
|
|
|
f = bp->cnt;
|
|
|
|
ptr = MBUF_CTOP(bp);
|
|
|
|
while (f--) {
|
|
|
|
sprintf(b, " %02x", (int) *ptr++);
|
|
|
|
b += 3;
|
1997-11-04 01:17:05 +00:00
|
|
|
if (b == buf + sizeof buf - 2) {
|
|
|
|
strcpy(b, "\n");
|
1997-10-22 01:22:51 +00:00
|
|
|
LogPrintf(lev, buf);
|
|
|
|
b = buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((bp = bp->next) != NULL);
|
|
|
|
|
1997-11-04 01:17:05 +00:00
|
|
|
if (b > buf) {
|
|
|
|
strcpy(b, "\n");
|
1997-10-22 01:22:51 +00:00
|
|
|
LogPrintf(lev, buf);
|
1997-11-04 01:17:05 +00:00
|
|
|
}
|
1997-10-22 01:22:51 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-03-13 14:53:55 +00:00
|
|
|
|
|
|
|
void
|
1997-11-22 03:37:54 +00:00
|
|
|
LogDumpBuff(int lev, const char *hdr, const u_char * ptr, int n)
|
1997-03-13 14:53:55 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
if (LogIsKept(lev)) {
|
1997-11-04 01:17:05 +00:00
|
|
|
char buf[50];
|
1997-08-25 00:29:32 +00:00
|
|
|
char *b;
|
|
|
|
|
|
|
|
if (hdr && *hdr)
|
1997-10-22 01:22:51 +00:00
|
|
|
LogPrintf(lev, "%s\n", hdr);
|
1997-08-25 00:29:32 +00:00
|
|
|
while (n > 0) {
|
|
|
|
b = buf;
|
1997-12-24 09:29:17 +00:00
|
|
|
for (b = buf; b != buf + sizeof buf - 2 && n--; b += 3)
|
1997-08-25 00:29:32 +00:00
|
|
|
sprintf(b, " %02x", (int) *ptr++);
|
1997-11-04 01:17:05 +00:00
|
|
|
strcpy(b, "\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
LogPrintf(lev, buf);
|
1997-06-09 03:27:43 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1997-03-13 14:53:55 +00:00
|
|
|
}
|