2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
42 #include "descriptor.h"
45 static const char *const LogNames[] = {
54 "Filter", /* Log discarded packets */
72 #define MSK(n) (1<<((n)-1))
74 static u_long LogMask = MSK(LogPHASE);
75 static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
76 static int LogTunno = -1;
77 static struct prompt *promptlist; /* Where to log local stuff */
78 struct prompt *log_PromptContext;
79 int log_PromptListChanged;
88 log_RegisterPrompt(struct prompt *prompt)
90 prompt->next = promptlist;
93 log_DiscardAllLocal(&prompt->logmask);
97 log_ActivatePrompt(struct prompt *prompt)
100 LogMaskLocal |= prompt->logmask;
104 LogSetMaskLocal(void)
108 LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
109 for (p = promptlist; p; p = p->next)
110 LogMaskLocal |= p->logmask;
114 log_DeactivatePrompt(struct prompt *prompt)
116 if (prompt->active) {
123 log_UnRegisterPrompt(struct prompt *prompt)
128 for (p = &promptlist; *p; p = &(*p)->next)
135 log_PromptListChanged++;
140 log_DestroyPrompts(struct server *s)
142 struct prompt *p, *pn, *pl;
148 if (s && p->owner == s) {
152 promptlist = p->next;
154 prompt_Destroy(p, 1);
166 for (p = promptlist; p; p = p->next)
171 log_WritePrompts(struct datalink *dl, const char *fmt,...)
177 for (p = promptlist; p; p = p->next)
178 if (prompt_IsTermMode(p, dl))
179 prompt_vPrintf(p, fmt, ap);
184 log_SetTtyCommandMode(struct datalink *dl)
188 for (p = promptlist; p; p = p->next)
189 if (prompt_IsTermMode(p, dl))
190 prompt_TtyCommandMode(p);
209 return lev >= LogMIN && lev <= LogMAX ? LOG_INFO : 0;
217 return id < LogMIN || id > LogMAX ? "Unknown" : LogNames[id - 1];
223 if (id >= LogMIN && id <= LogMAXCONF)
228 log_KeepLocal(int id, u_long *mask)
230 if (id >= LogMIN && id <= LogMAXCONF) {
231 LogMaskLocal |= MSK(id);
239 if (id >= LogMIN && id <= LogMAXCONF)
244 log_DiscardLocal(int id, u_long *mask)
246 if (id >= LogMIN && id <= LogMAXCONF) {
259 log_DiscardAllLocal(u_long *mask)
261 *mask = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
269 return LOG_KEPT_SYSLOG;
270 if (id < LogMIN || id > LogMAX)
273 return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
275 return ((LogMaskLocal & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
276 ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
280 log_IsKeptLocal(int id, u_long mask)
282 if (id < LogMIN || id > LogMAX)
285 return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
287 return ((mask & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
288 ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
292 log_Open(const char *Name)
294 openlog(Name, LOG_PID, LOG_DAEMON);
298 log_SetTun(int tunno)
311 log_Printf(int lev, const char *fmt,...)
314 struct prompt *prompt;
316 if (log_IsKept(lev)) {
320 if (promptlist && (log_IsKept(lev) & LOG_KEPT_LOCAL)) {
321 if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1)
322 snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
323 LogTunno, log_Name(lev), fmt);
325 snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
327 if (log_PromptContext && lev == LogWARN)
328 /* Warnings just go to the current prompt */
329 prompt_vPrintf(log_PromptContext, nfmt, ap);
330 else for (prompt = promptlist; prompt; prompt = prompt->next)
331 if (lev > LogMAXCONF || (prompt->logmask & MSK(lev)))
332 prompt_vPrintf(prompt, nfmt, ap);
337 if ((log_IsKept(lev) & LOG_KEPT_SYSLOG) &&
338 (lev != LogWARN || !log_PromptContext)) {
339 if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1)
340 snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
341 LogTunno, log_Name(lev), fmt);
343 snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
344 vsyslog(syslogLevel(lev), nfmt, ap);
351 log_DumpBp(int lev, const char *hdr, const struct mbuf *bp)
353 if (log_IsKept(lev)) {
360 log_Printf(lev, "%s\n", hdr);
366 ptr = CONST_MBUF_CTOP(bp);
368 sprintf(b, " %02x", (int) *ptr);
369 *c++ = isprint(*ptr) ? *ptr : '.';
375 log_Printf(lev, "%s\n", buf);
380 } while ((bp = bp->m_next) != NULL);
383 memset(b, ' ', 50 - (b - buf));
385 log_Printf(lev, "%s\n", buf);
391 log_DumpBuff(int lev, const char *hdr, const u_char *ptr, int n)
393 if (log_IsKept(lev)) {
398 log_Printf(lev, "%s\n", hdr);
402 for (b = buf; b != buf + 48 && n--; b += 3, ptr++) {
403 sprintf(b, " %02x", (int) *ptr);
404 *c++ = isprint(*ptr) ? *ptr : '.';
406 memset(b, ' ', 50 - (b - buf));
408 log_Printf(lev, "%s\n", buf);
414 log_ShowLevel(struct cmdargs const *arg)
418 prompt_Printf(arg->prompt, "Log: ");
419 for (i = LogMIN; i <= LogMAX; i++)
420 if (log_IsKept(i) & LOG_KEPT_SYSLOG)
421 prompt_Printf(arg->prompt, " %s", log_Name(i));
423 prompt_Printf(arg->prompt, "\nLocal:");
424 for (i = LogMIN; i <= LogMAX; i++)
425 if (log_IsKeptLocal(i, arg->prompt->logmask) & LOG_KEPT_LOCAL)
426 prompt_Printf(arg->prompt, " %s", log_Name(i));
428 prompt_Printf(arg->prompt, "\n");
434 log_SetLevel(struct cmdargs const *arg)
436 int i, res, argc, local;
437 char const *const *argv, *argp;
439 argc = arg->argc - arg->argn;
440 argv = arg->argv + arg->argn;
443 if (argc == 0 || strcasecmp(argv[0], "local"))
446 if (arg->prompt == NULL) {
447 log_Printf(LogWARN, "set log local: Only available on the"
456 if (argc == 0 || (argv[0][0] != '+' && argv[0][0] != '-')) {
458 log_DiscardAllLocal(&arg->prompt->logmask);
464 argp = **argv == '+' || **argv == '-' ? *argv + 1 : *argv;
465 /* Special case 'all' */
466 if (strcasecmp(argp, "all") == 0) {
469 for (i = LogMIN; i <= LogMAX; i++)
470 log_DiscardLocal(i, &arg->prompt->logmask);
472 for (i = LogMIN; i <= LogMAX; i++)
475 for (i = LogMIN; i <= LogMAX; i++)
476 log_KeepLocal(i, &arg->prompt->logmask);
478 for (i = LogMIN; i <= LogMAX; i++)
483 for (i = LogMIN; i <= LogMAX; i++)
484 if (strcasecmp(argp, log_Name(i)) == 0) {
487 log_DiscardLocal(i, &arg->prompt->logmask);
491 log_KeepLocal(i, &arg->prompt->logmask);
497 log_Printf(LogWARN, "%s: Invalid log value\n", argp);
506 log_ShowWho(struct cmdargs const *arg)
510 for (p = promptlist; p; p = p->next) {
511 prompt_Printf(arg->prompt, "%s (%s)", p->src.type, p->src.from);
512 if (p == arg->prompt)
513 prompt_Printf(arg->prompt, " *");
515 prompt_Printf(arg->prompt, " ^Z");
516 prompt_Printf(arg->prompt, "\n");