1c39934eaSBrian Somers /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4c39934eaSBrian Somers * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
5c39934eaSBrian Somers * All rights reserved.
6c39934eaSBrian Somers *
7c39934eaSBrian Somers * Redistribution and use in source and binary forms, with or without
8c39934eaSBrian Somers * modification, are permitted provided that the following conditions
9c39934eaSBrian Somers * are met:
10c39934eaSBrian Somers * 1. Redistributions of source code must retain the above copyright
11c39934eaSBrian Somers * notice, this list of conditions and the following disclaimer.
12c39934eaSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright
13c39934eaSBrian Somers * notice, this list of conditions and the following disclaimer in the
14c39934eaSBrian Somers * documentation and/or other materials provided with the distribution.
15c39934eaSBrian Somers *
16c39934eaSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c39934eaSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c39934eaSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c39934eaSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c39934eaSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c39934eaSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c39934eaSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c39934eaSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c39934eaSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c39934eaSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c39934eaSBrian Somers * SUCH DAMAGE.
2775240ed1SBrian Somers */
2875240ed1SBrian Somers
292764b86aSBrian Somers #include <sys/types.h>
3075240ed1SBrian Somers
31ba16c840SBrian Somers #include <ctype.h>
3253c9f6c0SAtsushi Murai #include <stdarg.h>
3353c9f6c0SAtsushi Murai #include <stdio.h>
3470a91e4cSBrian Somers #include <string.h>
3575240ed1SBrian Somers #include <syslog.h>
3685b542cfSBrian Somers #include <termios.h>
3775240ed1SBrian Somers
38c9e11a11SBrian Somers #include "defs.h"
39b6e82f33SBrian Somers #include "command.h"
40927145beSBrian Somers #include "mbuf.h"
41927145beSBrian Somers #include "log.h"
4285b542cfSBrian Somers #include "descriptor.h"
4385b542cfSBrian Somers #include "prompt.h"
44af57ed9fSAtsushi Murai
45182c898aSBrian Somers static const char *const LogNames[] = {
46927145beSBrian Somers "Async",
4792b09558SBrian Somers "CBCP",
48cb611434SBrian Somers "CCP",
49927145beSBrian Somers "Chat",
50927145beSBrian Somers "Command",
51927145beSBrian Somers "Connect",
52927145beSBrian Somers "Debug",
5352c9ca19SBrian Somers "DNS",
5406a43ce0SBrian Somers "Filter", /* Log discarded packets */
55927145beSBrian Somers "HDLC",
565106c671SBrian Somers "ID0",
57cb611434SBrian Somers "IPCP",
5830949fd4SBrian Somers "IPV6CP",
59927145beSBrian Somers "LCP",
60927145beSBrian Somers "LQM",
61927145beSBrian Somers "Phase",
626815097bSBrian Somers "Physical",
63e715b13bSBrian Somers "Radius",
646815097bSBrian Somers "Sync",
65927145beSBrian Somers "TCP/IP",
66b1ac9332SBrian Somers "Timer",
67927145beSBrian Somers "Tun",
68927145beSBrian Somers "Warning",
69927145beSBrian Somers "Error",
70927145beSBrian Somers "Alert"
71927145beSBrian Somers };
72af57ed9fSAtsushi Murai
73927145beSBrian Somers #define MSK(n) (1<<((n)-1))
74af57ed9fSAtsushi Murai
75d47dceb8SBrian Somers static u_long LogMask = MSK(LogPHASE);
76a1e8f937SBrian Somers static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
77927145beSBrian Somers static int LogTunno = -1;
78f2f5156fSBrian Somers static const char *LogIfaceName;
790f2f3eb3SBrian Somers static struct prompt *promptlist; /* Where to log local stuff */
80b4f63b0bSBrian Somers struct prompt *log_PromptContext;
810bdcbcbeSBrian Somers int log_PromptListChanged;
820f2f3eb3SBrian Somers
830f2f3eb3SBrian Somers struct prompt *
log_PromptList(void)84672eba24SJohn Baldwin log_PromptList(void)
850f2f3eb3SBrian Somers {
860f2f3eb3SBrian Somers return promptlist;
870f2f3eb3SBrian Somers }
88b6217683SBrian Somers
89b6217683SBrian Somers void
log_RegisterPrompt(struct prompt * prompt)90b6217683SBrian Somers log_RegisterPrompt(struct prompt *prompt)
91b6217683SBrian Somers {
920f2f3eb3SBrian Somers prompt->next = promptlist;
930f2f3eb3SBrian Somers promptlist = prompt;
940f2f3eb3SBrian Somers prompt->active = 1;
950f2f3eb3SBrian Somers log_DiscardAllLocal(&prompt->logmask);
96b6217683SBrian Somers }
970f2f3eb3SBrian Somers
980f2f3eb3SBrian Somers void
log_ActivatePrompt(struct prompt * prompt)990f2f3eb3SBrian Somers log_ActivatePrompt(struct prompt *prompt)
1000f2f3eb3SBrian Somers {
1010f2f3eb3SBrian Somers prompt->active = 1;
1020f2f3eb3SBrian Somers LogMaskLocal |= prompt->logmask;
103b6217683SBrian Somers }
104b6217683SBrian Somers
10567568487SBrian Somers static void
LogSetMaskLocal(void)10667568487SBrian Somers LogSetMaskLocal(void)
10767568487SBrian Somers {
10867568487SBrian Somers struct prompt *p;
10967568487SBrian Somers
11067568487SBrian Somers LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
1110f2f3eb3SBrian Somers for (p = promptlist; p; p = p->next)
11267568487SBrian Somers LogMaskLocal |= p->logmask;
11367568487SBrian Somers }
11467568487SBrian Somers
115b6217683SBrian Somers void
log_DeactivatePrompt(struct prompt * prompt)1160f2f3eb3SBrian Somers log_DeactivatePrompt(struct prompt *prompt)
1170f2f3eb3SBrian Somers {
1180f2f3eb3SBrian Somers if (prompt->active) {
1190f2f3eb3SBrian Somers prompt->active = 0;
1200f2f3eb3SBrian Somers LogSetMaskLocal();
1210f2f3eb3SBrian Somers }
1220f2f3eb3SBrian Somers }
1230f2f3eb3SBrian Somers
1240f2f3eb3SBrian Somers void
log_UnRegisterPrompt(struct prompt * prompt)125b6217683SBrian Somers log_UnRegisterPrompt(struct prompt *prompt)
126b6217683SBrian Somers {
127b6217683SBrian Somers if (prompt) {
128b6217683SBrian Somers struct prompt **p;
129b6217683SBrian Somers
1300f2f3eb3SBrian Somers for (p = &promptlist; *p; p = &(*p)->next)
131b6217683SBrian Somers if (*p == prompt) {
1320f2f3eb3SBrian Somers *p = prompt->next;
1330f2f3eb3SBrian Somers prompt->next = NULL;
134b6217683SBrian Somers break;
135b6217683SBrian Somers }
13667568487SBrian Somers LogSetMaskLocal();
1370bdcbcbeSBrian Somers log_PromptListChanged++;
138b6217683SBrian Somers }
139b6217683SBrian Somers }
140af57ed9fSAtsushi Murai
1410f2f3eb3SBrian Somers void
log_DestroyPrompts(struct server * s)1420f2f3eb3SBrian Somers log_DestroyPrompts(struct server *s)
1430f2f3eb3SBrian Somers {
144a39fd214SBrian Somers struct prompt *p, *pn, *pl;
1450f2f3eb3SBrian Somers
1460f2f3eb3SBrian Somers p = promptlist;
147a39fd214SBrian Somers pl = NULL;
1480f2f3eb3SBrian Somers while (p) {
1490f2f3eb3SBrian Somers pn = p->next;
150a39fd214SBrian Somers if (s && p->owner == s) {
151a39fd214SBrian Somers if (pl)
152a39fd214SBrian Somers pl->next = p->next;
153a39fd214SBrian Somers else
154a39fd214SBrian Somers promptlist = p->next;
1550f2f3eb3SBrian Somers p->next = NULL;
1560f2f3eb3SBrian Somers prompt_Destroy(p, 1);
157a39fd214SBrian Somers } else
158a39fd214SBrian Somers pl = p;
1590f2f3eb3SBrian Somers p = pn;
1600f2f3eb3SBrian Somers }
1610f2f3eb3SBrian Somers }
1620f2f3eb3SBrian Somers
1630f2f3eb3SBrian Somers void
log_DisplayPrompts(void)164672eba24SJohn Baldwin log_DisplayPrompts(void)
1650f2f3eb3SBrian Somers {
1660f2f3eb3SBrian Somers struct prompt *p;
1670f2f3eb3SBrian Somers
1680f2f3eb3SBrian Somers for (p = promptlist; p; p = p->next)
1690f2f3eb3SBrian Somers prompt_Required(p);
1700f2f3eb3SBrian Somers }
1710f2f3eb3SBrian Somers
1720f2f3eb3SBrian Somers void
log_WritePrompts(struct datalink * dl,const char * fmt,...)173bf1d3ff6SBrian Somers log_WritePrompts(struct datalink *dl, const char *fmt,...)
1740f2f3eb3SBrian Somers {
175bf1d3ff6SBrian Somers va_list ap;
1760f2f3eb3SBrian Somers struct prompt *p;
1770f2f3eb3SBrian Somers
178bf1d3ff6SBrian Somers va_start(ap, fmt);
1790f2f3eb3SBrian Somers for (p = promptlist; p; p = p->next)
1800f2f3eb3SBrian Somers if (prompt_IsTermMode(p, dl))
181bf1d3ff6SBrian Somers prompt_vPrintf(p, fmt, ap);
182bf1d3ff6SBrian Somers va_end(ap);
1830f2f3eb3SBrian Somers }
1840f2f3eb3SBrian Somers
1850f2f3eb3SBrian Somers void
log_SetTtyCommandMode(struct datalink * dl)1860f2f3eb3SBrian Somers log_SetTtyCommandMode(struct datalink *dl)
1870f2f3eb3SBrian Somers {
1880f2f3eb3SBrian Somers struct prompt *p;
1890f2f3eb3SBrian Somers
1900f2f3eb3SBrian Somers for (p = promptlist; p; p = p->next)
1910f2f3eb3SBrian Somers if (prompt_IsTermMode(p, dl))
1920f2f3eb3SBrian Somers prompt_TtyCommandMode(p);
1930f2f3eb3SBrian Somers }
1940f2f3eb3SBrian Somers
195927145beSBrian Somers static int
syslogLevel(int lev)196927145beSBrian Somers syslogLevel(int lev)
197927145beSBrian Somers {
198927145beSBrian Somers switch (lev) {
19946df5aa7SBrian Somers case LogLOG:
20046df5aa7SBrian Somers return LOG_INFO;
2016f384573SBrian Somers case LogDEBUG:
2026f384573SBrian Somers case LogTIMER:
2036f384573SBrian Somers return LOG_DEBUG;
204944f7098SBrian Somers case LogWARN:
205944f7098SBrian Somers return LOG_WARNING;
206944f7098SBrian Somers case LogERROR:
207944f7098SBrian Somers return LOG_ERR;
208944f7098SBrian Somers case LogALERT:
209944f7098SBrian Somers return LOG_ALERT;
210927145beSBrian Somers }
211927145beSBrian Somers return lev >= LogMIN && lev <= LogMAX ? LOG_INFO : 0;
212927145beSBrian Somers }
213af57ed9fSAtsushi Murai
214927145beSBrian Somers const char *
log_Name(int id)215dd7e2610SBrian Somers log_Name(int id)
216927145beSBrian Somers {
21746df5aa7SBrian Somers if (id == LogLOG)
21846df5aa7SBrian Somers return "LOG";
219927145beSBrian Somers return id < LogMIN || id > LogMAX ? "Unknown" : LogNames[id - 1];
220927145beSBrian Somers }
221af57ed9fSAtsushi Murai
222af57ed9fSAtsushi Murai void
log_Keep(int id)223dd7e2610SBrian Somers log_Keep(int id)
224af57ed9fSAtsushi Murai {
225927145beSBrian Somers if (id >= LogMIN && id <= LogMAXCONF)
226927145beSBrian Somers LogMask |= MSK(id);
227af57ed9fSAtsushi Murai }
228927145beSBrian Somers
229927145beSBrian Somers void
log_KeepLocal(int id,u_long * mask)230dd7e2610SBrian Somers log_KeepLocal(int id, u_long *mask)
231a1e8f937SBrian Somers {
232b6217683SBrian Somers if (id >= LogMIN && id <= LogMAXCONF) {
233a1e8f937SBrian Somers LogMaskLocal |= MSK(id);
234b6217683SBrian Somers *mask |= MSK(id);
235b6217683SBrian Somers }
236a1e8f937SBrian Somers }
237a1e8f937SBrian Somers
238a1e8f937SBrian Somers void
log_Discard(int id)239dd7e2610SBrian Somers log_Discard(int id)
240927145beSBrian Somers {
241927145beSBrian Somers if (id >= LogMIN && id <= LogMAXCONF)
242927145beSBrian Somers LogMask &= ~MSK(id);
243927145beSBrian Somers }
244927145beSBrian Somers
245927145beSBrian Somers void
log_DiscardLocal(int id,u_long * mask)246dd7e2610SBrian Somers log_DiscardLocal(int id, u_long *mask)
247a1e8f937SBrian Somers {
248b6217683SBrian Somers if (id >= LogMIN && id <= LogMAXCONF) {
249b6217683SBrian Somers *mask &= ~MSK(id);
25067568487SBrian Somers LogSetMaskLocal();
251b6217683SBrian Somers }
252a1e8f937SBrian Somers }
253a1e8f937SBrian Somers
254a1e8f937SBrian Somers void
log_DiscardAll(void)255672eba24SJohn Baldwin log_DiscardAll(void)
256927145beSBrian Somers {
257927145beSBrian Somers LogMask = 0;
258af57ed9fSAtsushi Murai }
259af57ed9fSAtsushi Murai
260a1e8f937SBrian Somers void
log_DiscardAllLocal(u_long * mask)261dd7e2610SBrian Somers log_DiscardAllLocal(u_long *mask)
262a1e8f937SBrian Somers {
26367568487SBrian Somers *mask = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
26467568487SBrian Somers LogSetMaskLocal();
265a1e8f937SBrian Somers }
266a1e8f937SBrian Somers
267af57ed9fSAtsushi Murai int
log_IsKept(int id)268dd7e2610SBrian Somers log_IsKept(int id)
269af57ed9fSAtsushi Murai {
27046df5aa7SBrian Somers if (id == LogLOG)
27146df5aa7SBrian Somers return LOG_KEPT_SYSLOG;
272a1e8f937SBrian Somers if (id < LogMIN || id > LogMAX)
273927145beSBrian Somers return 0;
274a1e8f937SBrian Somers if (id > LogMAXCONF)
275a1e8f937SBrian Somers return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
276a1e8f937SBrian Somers
277a1e8f937SBrian Somers return ((LogMaskLocal & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
278a1e8f937SBrian Somers ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
279af57ed9fSAtsushi Murai }
280af57ed9fSAtsushi Murai
281b6217683SBrian Somers int
log_IsKeptLocal(int id,u_long mask)282dd7e2610SBrian Somers log_IsKeptLocal(int id, u_long mask)
283b6217683SBrian Somers {
284b6217683SBrian Somers if (id < LogMIN || id > LogMAX)
285b6217683SBrian Somers return 0;
286b6217683SBrian Somers if (id > LogMAXCONF)
287b6217683SBrian Somers return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
288b6217683SBrian Somers
289b6217683SBrian Somers return ((mask & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
290b6217683SBrian Somers ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
291b6217683SBrian Somers }
292b6217683SBrian Somers
293af57ed9fSAtsushi Murai void
log_Open(const char * Name)294dd7e2610SBrian Somers log_Open(const char *Name)
295af57ed9fSAtsushi Murai {
296927145beSBrian Somers openlog(Name, LOG_PID, LOG_DAEMON);
297af57ed9fSAtsushi Murai }
298af57ed9fSAtsushi Murai
299af57ed9fSAtsushi Murai void
log_SetTun(int tunno,const char * ifaceName)300f2f5156fSBrian Somers log_SetTun(int tunno, const char *ifaceName)
301af57ed9fSAtsushi Murai {
302927145beSBrian Somers LogTunno = tunno;
303f2f5156fSBrian Somers LogIfaceName = ifaceName;
304af57ed9fSAtsushi Murai }
305af57ed9fSAtsushi Murai
306af57ed9fSAtsushi Murai void
log_Close(void)307672eba24SJohn Baldwin log_Close(void)
308af57ed9fSAtsushi Murai {
309927145beSBrian Somers closelog();
310927145beSBrian Somers LogTunno = -1;
311f2f5156fSBrian Somers LogIfaceName = NULL;
3126ed9fb2fSBrian Somers }
313af57ed9fSAtsushi Murai
314af57ed9fSAtsushi Murai void
log_Printf(int lev,const char * fmt,...)315dd7e2610SBrian Somers log_Printf(int lev, const char *fmt,...)
31653c9f6c0SAtsushi Murai {
31753c9f6c0SAtsushi Murai va_list ap;
318b6217683SBrian Somers struct prompt *prompt;
319944f7098SBrian Somers
320dd7e2610SBrian Somers if (log_IsKept(lev)) {
321d93d3a9cSBrian Somers char nfmt[200];
32253c9f6c0SAtsushi Murai
32390d25921SBrian Somers va_start(ap, fmt);
324b4f63b0bSBrian Somers if (promptlist && (log_IsKept(lev) & LOG_KEPT_LOCAL)) {
325f2f5156fSBrian Somers if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1) {
326f2f5156fSBrian Somers if (LogIfaceName)
327f2f5156fSBrian Somers snprintf(nfmt, sizeof nfmt, "%s%d(%s): %s: %s", TUN_NAME,
328f2f5156fSBrian Somers LogTunno, LogIfaceName, log_Name(lev), fmt);
329f2f5156fSBrian Somers else
3301384bd27SBrian Somers snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
331dd7e2610SBrian Somers LogTunno, log_Name(lev), fmt);
332f2f5156fSBrian Somers } else
333dd7e2610SBrian Somers snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
334b6217683SBrian Somers
335b4f63b0bSBrian Somers if (log_PromptContext && lev == LogWARN)
336b4f63b0bSBrian Somers /* Warnings just go to the current prompt */
337b4f63b0bSBrian Somers prompt_vPrintf(log_PromptContext, nfmt, ap);
338b4f63b0bSBrian Somers else for (prompt = promptlist; prompt; prompt = prompt->next)
339b6217683SBrian Somers if (lev > LogMAXCONF || (prompt->logmask & MSK(lev)))
340b6217683SBrian Somers prompt_vPrintf(prompt, nfmt, ap);
341a1e8f937SBrian Somers }
34290d25921SBrian Somers va_end(ap);
343a1e8f937SBrian Somers
34490d25921SBrian Somers va_start(ap, fmt);
3450f2f3eb3SBrian Somers if ((log_IsKept(lev) & LOG_KEPT_SYSLOG) &&
346b4f63b0bSBrian Somers (lev != LogWARN || !log_PromptContext)) {
347f2f5156fSBrian Somers if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1) {
348f2f5156fSBrian Somers if (LogIfaceName)
349f2f5156fSBrian Somers snprintf(nfmt, sizeof nfmt, "%s%d(%s): %s: %s", TUN_NAME,
350f2f5156fSBrian Somers LogTunno, LogIfaceName, log_Name(lev), fmt);
351f2f5156fSBrian Somers else
3521384bd27SBrian Somers snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
353dd7e2610SBrian Somers LogTunno, log_Name(lev), fmt);
354f2f5156fSBrian Somers } else
355dd7e2610SBrian Somers snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
356927145beSBrian Somers vsyslog(syslogLevel(lev), nfmt, ap);
357927145beSBrian Somers }
35853c9f6c0SAtsushi Murai va_end(ap);
35953c9f6c0SAtsushi Murai }
36090d25921SBrian Somers }
36153c9f6c0SAtsushi Murai
36253c9f6c0SAtsushi Murai void
log_DumpBp(int lev,const char * hdr,const struct mbuf * bp)363dd7e2610SBrian Somers log_DumpBp(int lev, const char *hdr, const struct mbuf *bp)
364af57ed9fSAtsushi Murai {
365dd7e2610SBrian Somers if (log_IsKept(lev)) {
366ba16c840SBrian Somers char buf[68];
367ba16c840SBrian Somers char *b, *c;
3689773f8c0SBrian Somers const u_char *ptr;
369a9e8f807SBrian Somers int f;
370a9e8f807SBrian Somers
371a9e8f807SBrian Somers if (hdr && *hdr)
372dd7e2610SBrian Somers log_Printf(lev, "%s\n", hdr);
373a9e8f807SBrian Somers
374a9e8f807SBrian Somers b = buf;
375ba16c840SBrian Somers c = b + 50;
376a9e8f807SBrian Somers do {
37726af0ae9SBrian Somers f = bp->m_len;
3789773f8c0SBrian Somers ptr = CONST_MBUF_CTOP(bp);
379a9e8f807SBrian Somers while (f--) {
380ba16c840SBrian Somers sprintf(b, " %02x", (int) *ptr);
381ba16c840SBrian Somers *c++ = isprint(*ptr) ? *ptr : '.';
382ba16c840SBrian Somers ptr++;
383a9e8f807SBrian Somers b += 3;
384ba16c840SBrian Somers if (b == buf + 48) {
385ba16c840SBrian Somers memset(b, ' ', 2);
3869cf01ccfSBrian Somers *c = '\0';
3879cf01ccfSBrian Somers log_Printf(lev, "%s\n", buf);
388a9e8f807SBrian Somers b = buf;
389ba16c840SBrian Somers c = b + 50;
390a9e8f807SBrian Somers }
391a9e8f807SBrian Somers }
39226af0ae9SBrian Somers } while ((bp = bp->m_next) != NULL);
393a9e8f807SBrian Somers
394a1e8f937SBrian Somers if (b > buf) {
395ba16c840SBrian Somers memset(b, ' ', 50 - (b - buf));
3969cf01ccfSBrian Somers *c = '\0';
3979cf01ccfSBrian Somers log_Printf(lev, "%s\n", buf);
398a9e8f807SBrian Somers }
399af57ed9fSAtsushi Murai }
400a1e8f937SBrian Somers }
401af57ed9fSAtsushi Murai
402af57ed9fSAtsushi Murai void
log_DumpBuff(int lev,const char * hdr,const u_char * ptr,int n)403dd7e2610SBrian Somers log_DumpBuff(int lev, const char *hdr, const u_char *ptr, int n)
404af57ed9fSAtsushi Murai {
405dd7e2610SBrian Somers if (log_IsKept(lev)) {
406ba16c840SBrian Somers char buf[68];
407ba16c840SBrian Somers char *b, *c;
408af57ed9fSAtsushi Murai
409927145beSBrian Somers if (hdr && *hdr)
410dd7e2610SBrian Somers log_Printf(lev, "%s\n", hdr);
411927145beSBrian Somers while (n > 0) {
412927145beSBrian Somers b = buf;
413ba16c840SBrian Somers c = b + 50;
414ba16c840SBrian Somers for (b = buf; b != buf + 48 && n--; b += 3, ptr++) {
415ba16c840SBrian Somers sprintf(b, " %02x", (int) *ptr);
416ba16c840SBrian Somers *c++ = isprint(*ptr) ? *ptr : '.';
417ba16c840SBrian Somers }
418ba16c840SBrian Somers memset(b, ' ', 50 - (b - buf));
4199cf01ccfSBrian Somers *c = '\0';
4209cf01ccfSBrian Somers log_Printf(lev, "%s\n", buf);
421af57ed9fSAtsushi Murai }
422af57ed9fSAtsushi Murai }
423c6c740beSBrian Somers }
424b6217683SBrian Somers
425b6217683SBrian Somers int
log_ShowLevel(struct cmdargs const * arg)426b6217683SBrian Somers log_ShowLevel(struct cmdargs const *arg)
427b6217683SBrian Somers {
428b6217683SBrian Somers int i;
429b6217683SBrian Somers
430b6217683SBrian Somers prompt_Printf(arg->prompt, "Log: ");
431b6217683SBrian Somers for (i = LogMIN; i <= LogMAX; i++)
432dd7e2610SBrian Somers if (log_IsKept(i) & LOG_KEPT_SYSLOG)
433dd7e2610SBrian Somers prompt_Printf(arg->prompt, " %s", log_Name(i));
434b6217683SBrian Somers
435b6217683SBrian Somers prompt_Printf(arg->prompt, "\nLocal:");
436b6217683SBrian Somers for (i = LogMIN; i <= LogMAX; i++)
437dd7e2610SBrian Somers if (log_IsKeptLocal(i, arg->prompt->logmask) & LOG_KEPT_LOCAL)
438dd7e2610SBrian Somers prompt_Printf(arg->prompt, " %s", log_Name(i));
439b6217683SBrian Somers
440b6217683SBrian Somers prompt_Printf(arg->prompt, "\n");
441b6217683SBrian Somers
442b6217683SBrian Somers return 0;
443b6217683SBrian Somers }
444b6217683SBrian Somers
445b6217683SBrian Somers int
log_SetLevel(struct cmdargs const * arg)446b6217683SBrian Somers log_SetLevel(struct cmdargs const *arg)
447b6217683SBrian Somers {
448b6217683SBrian Somers int i, res, argc, local;
449b6217683SBrian Somers char const *const *argv, *argp;
450b6217683SBrian Somers
45125092092SBrian Somers argc = arg->argc - arg->argn;
45225092092SBrian Somers argv = arg->argv + arg->argn;
453b6217683SBrian Somers res = 0;
454b6217683SBrian Somers
455b6217683SBrian Somers if (argc == 0 || strcasecmp(argv[0], "local"))
456b6217683SBrian Somers local = 0;
457b6217683SBrian Somers else {
458b6217683SBrian Somers if (arg->prompt == NULL) {
4599b996792SBrian Somers log_Printf(LogWARN, "set log local: Only available on the"
4609b996792SBrian Somers " command line\n");
461b6217683SBrian Somers return 1;
462b6217683SBrian Somers }
463b6217683SBrian Somers argc--;
464b6217683SBrian Somers argv++;
465b6217683SBrian Somers local = 1;
466b6217683SBrian Somers }
467b6217683SBrian Somers
468e43ebac1SBrian Somers if (argc == 0 || (argv[0][0] != '+' && argv[0][0] != '-')) {
469b6217683SBrian Somers if (local)
470dd7e2610SBrian Somers log_DiscardAllLocal(&arg->prompt->logmask);
471b6217683SBrian Somers else
472dd7e2610SBrian Somers log_DiscardAll();
473e43ebac1SBrian Somers }
474b6217683SBrian Somers
475b6217683SBrian Somers while (argc--) {
476b6217683SBrian Somers argp = **argv == '+' || **argv == '-' ? *argv + 1 : *argv;
4777f03ca53SBrian Somers /* Special case 'all' */
4787f03ca53SBrian Somers if (strcasecmp(argp, "all") == 0) {
4797f03ca53SBrian Somers if (**argv == '-') {
4807f03ca53SBrian Somers if (local)
4817f03ca53SBrian Somers for (i = LogMIN; i <= LogMAX; i++)
4827f03ca53SBrian Somers log_DiscardLocal(i, &arg->prompt->logmask);
4837f03ca53SBrian Somers else
4847f03ca53SBrian Somers for (i = LogMIN; i <= LogMAX; i++)
4857f03ca53SBrian Somers log_Discard(i);
4867f03ca53SBrian Somers } else if (local)
4877f03ca53SBrian Somers for (i = LogMIN; i <= LogMAX; i++)
4887f03ca53SBrian Somers log_KeepLocal(i, &arg->prompt->logmask);
4897f03ca53SBrian Somers else
4907f03ca53SBrian Somers for (i = LogMIN; i <= LogMAX; i++)
4917f03ca53SBrian Somers log_Keep(i);
4927f03ca53SBrian Somers argv++;
4937f03ca53SBrian Somers continue;
4947f03ca53SBrian Somers }
495b6217683SBrian Somers for (i = LogMIN; i <= LogMAX; i++)
496dd7e2610SBrian Somers if (strcasecmp(argp, log_Name(i)) == 0) {
497e43ebac1SBrian Somers if (**argv == '-') {
498b6217683SBrian Somers if (local)
499dd7e2610SBrian Somers log_DiscardLocal(i, &arg->prompt->logmask);
500b6217683SBrian Somers else
501dd7e2610SBrian Somers log_Discard(i);
502e43ebac1SBrian Somers } else if (local)
503dd7e2610SBrian Somers log_KeepLocal(i, &arg->prompt->logmask);
504b6217683SBrian Somers else
505dd7e2610SBrian Somers log_Keep(i);
506b6217683SBrian Somers break;
507b6217683SBrian Somers }
508b6217683SBrian Somers if (i > LogMAX) {
509dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid log value\n", argp);
510b6217683SBrian Somers res = -1;
511b6217683SBrian Somers }
512b6217683SBrian Somers argv++;
513b6217683SBrian Somers }
514b6217683SBrian Somers return res;
515b6217683SBrian Somers }
516b6217683SBrian Somers
517b6217683SBrian Somers int
log_ShowWho(struct cmdargs const * arg)518b6217683SBrian Somers log_ShowWho(struct cmdargs const *arg)
519b6217683SBrian Somers {
520b6217683SBrian Somers struct prompt *p;
521b6217683SBrian Somers
5220f2f3eb3SBrian Somers for (p = promptlist; p; p = p->next) {
523565e35e5SBrian Somers prompt_Printf(arg->prompt, "%s (%s)", p->src.type, p->src.from);
524f91ad6b0SBrian Somers if (p == arg->prompt)
525f91ad6b0SBrian Somers prompt_Printf(arg->prompt, " *");
526f91ad6b0SBrian Somers if (!p->active)
527f91ad6b0SBrian Somers prompt_Printf(arg->prompt, " ^Z");
528f91ad6b0SBrian Somers prompt_Printf(arg->prompt, "\n");
529f91ad6b0SBrian Somers }
530b6217683SBrian Somers
531b6217683SBrian Somers return 0;
532b6217683SBrian Somers }
533