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
26 * $FreeBSD: src/usr.sbin/ppp/log.c,v 1.44.2.5 2002/09/01 02:12:28 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/log.c,v 1.4 2008/05/19 10:19:49 corecode Exp $
30 #include <sys/types.h>
31 #include <sys/select.h>
44 #include "descriptor.h"
47 static const char *const LogNames
[] = {
56 "Filter", /* Log discarded packets */
74 #define MSK(n) (1<<((n)-1))
76 static u_long LogMask
= MSK(LogPHASE
);
77 static u_long LogMaskLocal
= MSK(LogERROR
) | MSK(LogALERT
) | MSK(LogWARN
);
78 static int LogTunno
= -1;
79 static struct prompt
*promptlist
; /* Where to log local stuff */
80 struct prompt
*log_PromptContext
;
81 int log_PromptListChanged
;
90 log_RegisterPrompt(struct prompt
*prompt
)
92 prompt
->next
= promptlist
;
95 log_DiscardAllLocal(&prompt
->logmask
);
99 log_ActivatePrompt(struct prompt
*prompt
)
102 LogMaskLocal
|= prompt
->logmask
;
106 LogSetMaskLocal(void)
110 LogMaskLocal
= MSK(LogERROR
) | MSK(LogALERT
) | MSK(LogWARN
);
111 for (p
= promptlist
; p
; p
= p
->next
)
112 LogMaskLocal
|= p
->logmask
;
116 log_DeactivatePrompt(struct prompt
*prompt
)
118 if (prompt
->active
) {
125 log_UnRegisterPrompt(struct prompt
*prompt
)
130 for (p
= &promptlist
; *p
; p
= &(*p
)->next
)
137 log_PromptListChanged
++;
142 log_DestroyPrompts(struct server
*s
)
144 struct prompt
*p
, *pn
, *pl
;
150 if (s
&& p
->owner
== s
) {
154 promptlist
= p
->next
;
156 prompt_Destroy(p
, 1);
164 log_DisplayPrompts(void)
168 for (p
= promptlist
; p
; p
= p
->next
)
173 log_WritePrompts(struct datalink
*dl
, const char *fmt
,...)
179 for (p
= promptlist
; p
; p
= p
->next
)
180 if (prompt_IsTermMode(p
, dl
))
181 prompt_vPrintf(p
, fmt
, ap
);
186 log_SetTtyCommandMode(struct datalink
*dl
)
190 for (p
= promptlist
; p
; p
= p
->next
)
191 if (prompt_IsTermMode(p
, dl
))
192 prompt_TtyCommandMode(p
);
211 return lev
>= LogMIN
&& lev
<= LogMAX
? LOG_INFO
: 0;
219 return id
< LogMIN
|| id
> LogMAX
? "Unknown" : LogNames
[id
- 1];
225 if (id
>= LogMIN
&& id
<= LogMAXCONF
)
230 log_KeepLocal(int id
, u_long
*mask
)
232 if (id
>= LogMIN
&& id
<= LogMAXCONF
) {
233 LogMaskLocal
|= MSK(id
);
241 if (id
>= LogMIN
&& id
<= LogMAXCONF
)
246 log_DiscardLocal(int id
, u_long
*mask
)
248 if (id
>= LogMIN
&& id
<= LogMAXCONF
) {
261 log_DiscardAllLocal(u_long
*mask
)
263 *mask
= MSK(LogERROR
) | MSK(LogALERT
) | MSK(LogWARN
);
271 return LOG_KEPT_SYSLOG
;
272 if (id
< LogMIN
|| id
> LogMAX
)
275 return LOG_KEPT_LOCAL
| LOG_KEPT_SYSLOG
;
277 return ((LogMaskLocal
& MSK(id
)) ? LOG_KEPT_LOCAL
: 0) |
278 ((LogMask
& MSK(id
)) ? LOG_KEPT_SYSLOG
: 0);
282 log_IsKeptLocal(int id
, u_long mask
)
284 if (id
< LogMIN
|| id
> LogMAX
)
287 return LOG_KEPT_LOCAL
| LOG_KEPT_SYSLOG
;
289 return ((mask
& MSK(id
)) ? LOG_KEPT_LOCAL
: 0) |
290 ((LogMask
& MSK(id
)) ? LOG_KEPT_SYSLOG
: 0);
294 log_Open(const char *Name
)
296 openlog(Name
, LOG_PID
, LOG_DAEMON
);
300 log_SetTun(int tunno
)
313 log_Printf(int lev
, const char *fmt
,...)
316 struct prompt
*prompt
;
318 if (log_IsKept(lev
)) {
322 if (promptlist
&& (log_IsKept(lev
) & LOG_KEPT_LOCAL
)) {
323 if ((log_IsKept(LogTUN
) & LOG_KEPT_LOCAL
) && LogTunno
!= -1)
324 snprintf(nfmt
, sizeof nfmt
, "%s%d: %s: %s", TUN_NAME
,
325 LogTunno
, log_Name(lev
), fmt
);
327 snprintf(nfmt
, sizeof nfmt
, "%s: %s", log_Name(lev
), fmt
);
329 if (log_PromptContext
&& lev
== LogWARN
)
330 /* Warnings just go to the current prompt */
331 prompt_vPrintf(log_PromptContext
, nfmt
, ap
);
332 else for (prompt
= promptlist
; prompt
; prompt
= prompt
->next
)
333 if (lev
> LogMAXCONF
|| (prompt
->logmask
& MSK(lev
)))
334 prompt_vPrintf(prompt
, nfmt
, ap
);
339 if ((log_IsKept(lev
) & LOG_KEPT_SYSLOG
) &&
340 (lev
!= LogWARN
|| !log_PromptContext
)) {
341 if ((log_IsKept(LogTUN
) & LOG_KEPT_SYSLOG
) && LogTunno
!= -1)
342 snprintf(nfmt
, sizeof nfmt
, "%s%d: %s: %s", TUN_NAME
,
343 LogTunno
, log_Name(lev
), fmt
);
345 snprintf(nfmt
, sizeof nfmt
, "%s: %s", log_Name(lev
), fmt
);
346 vsyslog(syslogLevel(lev
), nfmt
, ap
);
353 log_DumpBp(int lev
, const char *hdr
, const struct mbuf
*bp
)
355 if (log_IsKept(lev
)) {
362 log_Printf(lev
, "%s\n", hdr
);
368 ptr
= CONST_MBUF_CTOP(bp
);
370 sprintf(b
, " %02x", (int) *ptr
);
371 *c
++ = isprint(*ptr
) ? *ptr
: '.';
377 log_Printf(lev
, "%s\n", buf
);
382 } while ((bp
= bp
->m_next
) != NULL
);
385 memset(b
, ' ', 50 - (b
- buf
));
387 log_Printf(lev
, "%s\n", buf
);
393 log_DumpBuff(int lev
, const char *hdr
, const u_char
*ptr
, int n
)
395 if (log_IsKept(lev
)) {
400 log_Printf(lev
, "%s\n", hdr
);
404 for (b
= buf
; b
!= buf
+ 48 && n
--; b
+= 3, ptr
++) {
405 sprintf(b
, " %02x", (int) *ptr
);
406 *c
++ = isprint(*ptr
) ? *ptr
: '.';
408 memset(b
, ' ', 50 - (b
- buf
));
410 log_Printf(lev
, "%s\n", buf
);
416 log_ShowLevel(struct cmdargs
const *arg
)
420 prompt_Printf(arg
->prompt
, "Log: ");
421 for (i
= LogMIN
; i
<= LogMAX
; i
++)
422 if (log_IsKept(i
) & LOG_KEPT_SYSLOG
)
423 prompt_Printf(arg
->prompt
, " %s", log_Name(i
));
425 prompt_Printf(arg
->prompt
, "\nLocal:");
426 for (i
= LogMIN
; i
<= LogMAX
; i
++)
427 if (log_IsKeptLocal(i
, arg
->prompt
->logmask
) & LOG_KEPT_LOCAL
)
428 prompt_Printf(arg
->prompt
, " %s", log_Name(i
));
430 prompt_Printf(arg
->prompt
, "\n");
436 log_SetLevel(struct cmdargs
const *arg
)
438 int i
, res
, argc
, local
;
439 char const *const *argv
, *argp
;
441 argc
= arg
->argc
- arg
->argn
;
442 argv
= arg
->argv
+ arg
->argn
;
445 if (argc
== 0 || strcasecmp(argv
[0], "local"))
448 if (arg
->prompt
== NULL
) {
449 log_Printf(LogWARN
, "set log local: Only available on the"
458 if (argc
== 0 || (argv
[0][0] != '+' && argv
[0][0] != '-')) {
460 log_DiscardAllLocal(&arg
->prompt
->logmask
);
466 argp
= **argv
== '+' || **argv
== '-' ? *argv
+ 1 : *argv
;
467 /* Special case 'all' */
468 if (strcasecmp(argp
, "all") == 0) {
471 for (i
= LogMIN
; i
<= LogMAX
; i
++)
472 log_DiscardLocal(i
, &arg
->prompt
->logmask
);
474 for (i
= LogMIN
; i
<= LogMAX
; i
++)
477 for (i
= LogMIN
; i
<= LogMAX
; i
++)
478 log_KeepLocal(i
, &arg
->prompt
->logmask
);
480 for (i
= LogMIN
; i
<= LogMAX
; i
++)
485 for (i
= LogMIN
; i
<= LogMAX
; i
++)
486 if (strcasecmp(argp
, log_Name(i
)) == 0) {
489 log_DiscardLocal(i
, &arg
->prompt
->logmask
);
493 log_KeepLocal(i
, &arg
->prompt
->logmask
);
499 log_Printf(LogWARN
, "%s: Invalid log value\n", argp
);
508 log_ShowWho(struct cmdargs
const *arg
)
512 for (p
= promptlist
; p
; p
= p
->next
) {
513 prompt_Printf(arg
->prompt
, "%s (%s)", p
->src
.type
, p
->src
.from
);
514 if (p
== arg
->prompt
)
515 prompt_Printf(arg
->prompt
, " *");
517 prompt_Printf(arg
->prompt
, " ^Z");
518 prompt_Printf(arg
->prompt
, "\n");