xref: /freebsd/usr.sbin/ppp/log.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
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.
27c39934eaSBrian Somers  *
2897d92980SPeter Wemm  * $FreeBSD$
2975240ed1SBrian Somers  */
3075240ed1SBrian Somers 
312764b86aSBrian Somers #include <sys/types.h>
3275240ed1SBrian Somers 
33ba16c840SBrian Somers #include <ctype.h>
3453c9f6c0SAtsushi Murai #include <stdarg.h>
3553c9f6c0SAtsushi Murai #include <stdio.h>
3670a91e4cSBrian Somers #include <string.h>
3775240ed1SBrian Somers #include <syslog.h>
3885b542cfSBrian Somers #include <termios.h>
3975240ed1SBrian Somers 
40c9e11a11SBrian Somers #include "defs.h"
41b6e82f33SBrian Somers #include "command.h"
42927145beSBrian Somers #include "mbuf.h"
43927145beSBrian Somers #include "log.h"
4485b542cfSBrian Somers #include "descriptor.h"
4585b542cfSBrian Somers #include "prompt.h"
46af57ed9fSAtsushi Murai 
47182c898aSBrian Somers static const char *const LogNames[] = {
48927145beSBrian Somers   "Async",
4992b09558SBrian Somers   "CBCP",
50cb611434SBrian Somers   "CCP",
51927145beSBrian Somers   "Chat",
52927145beSBrian Somers   "Command",
53927145beSBrian Somers   "Connect",
54927145beSBrian Somers   "Debug",
5552c9ca19SBrian Somers   "DNS",
5606a43ce0SBrian Somers   "Filter",			/* Log discarded packets */
57927145beSBrian Somers   "HDLC",
585106c671SBrian Somers   "ID0",
59cb611434SBrian Somers   "IPCP",
6030949fd4SBrian Somers   "IPV6CP",
61927145beSBrian Somers   "LCP",
62927145beSBrian Somers   "LQM",
63927145beSBrian Somers   "Phase",
646815097bSBrian Somers   "Physical",
65e715b13bSBrian Somers   "Radius",
666815097bSBrian Somers   "Sync",
67927145beSBrian Somers   "TCP/IP",
68b1ac9332SBrian Somers   "Timer",
69927145beSBrian Somers   "Tun",
70927145beSBrian Somers   "Warning",
71927145beSBrian Somers   "Error",
72927145beSBrian Somers   "Alert"
73927145beSBrian Somers };
74af57ed9fSAtsushi Murai 
75927145beSBrian Somers #define MSK(n) (1<<((n)-1))
76af57ed9fSAtsushi Murai 
77d47dceb8SBrian Somers static u_long LogMask = MSK(LogPHASE);
78a1e8f937SBrian Somers static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
79927145beSBrian Somers static int LogTunno = -1;
80f2f5156fSBrian Somers static const char *LogIfaceName;
810f2f3eb3SBrian Somers static struct prompt *promptlist;	/* Where to log local stuff */
82b4f63b0bSBrian Somers struct prompt *log_PromptContext;
830bdcbcbeSBrian Somers int log_PromptListChanged;
840f2f3eb3SBrian Somers 
850f2f3eb3SBrian Somers struct prompt *
86672eba24SJohn Baldwin log_PromptList(void)
870f2f3eb3SBrian Somers {
880f2f3eb3SBrian Somers   return promptlist;
890f2f3eb3SBrian Somers }
90b6217683SBrian Somers 
91b6217683SBrian Somers void
92b6217683SBrian Somers log_RegisterPrompt(struct prompt *prompt)
93b6217683SBrian Somers {
940f2f3eb3SBrian Somers   prompt->next = promptlist;
950f2f3eb3SBrian Somers   promptlist = prompt;
960f2f3eb3SBrian Somers   prompt->active = 1;
970f2f3eb3SBrian Somers   log_DiscardAllLocal(&prompt->logmask);
98b6217683SBrian Somers }
990f2f3eb3SBrian Somers 
1000f2f3eb3SBrian Somers void
1010f2f3eb3SBrian Somers log_ActivatePrompt(struct prompt *prompt)
1020f2f3eb3SBrian Somers {
1030f2f3eb3SBrian Somers   prompt->active = 1;
1040f2f3eb3SBrian Somers   LogMaskLocal |= prompt->logmask;
105b6217683SBrian Somers }
106b6217683SBrian Somers 
10767568487SBrian Somers static void
10867568487SBrian Somers LogSetMaskLocal(void)
10967568487SBrian Somers {
11067568487SBrian Somers   struct prompt *p;
11167568487SBrian Somers 
11267568487SBrian Somers   LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
1130f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next)
11467568487SBrian Somers     LogMaskLocal |= p->logmask;
11567568487SBrian Somers }
11667568487SBrian Somers 
117b6217683SBrian Somers void
1180f2f3eb3SBrian Somers log_DeactivatePrompt(struct prompt *prompt)
1190f2f3eb3SBrian Somers {
1200f2f3eb3SBrian Somers   if (prompt->active) {
1210f2f3eb3SBrian Somers     prompt->active = 0;
1220f2f3eb3SBrian Somers     LogSetMaskLocal();
1230f2f3eb3SBrian Somers   }
1240f2f3eb3SBrian Somers }
1250f2f3eb3SBrian Somers 
1260f2f3eb3SBrian Somers void
127b6217683SBrian Somers log_UnRegisterPrompt(struct prompt *prompt)
128b6217683SBrian Somers {
129b6217683SBrian Somers   if (prompt) {
130b6217683SBrian Somers     struct prompt **p;
131b6217683SBrian Somers 
1320f2f3eb3SBrian Somers     for (p = &promptlist; *p; p = &(*p)->next)
133b6217683SBrian Somers       if (*p == prompt) {
1340f2f3eb3SBrian Somers         *p = prompt->next;
1350f2f3eb3SBrian Somers         prompt->next = NULL;
136b6217683SBrian Somers         break;
137b6217683SBrian Somers       }
13867568487SBrian Somers     LogSetMaskLocal();
1390bdcbcbeSBrian Somers     log_PromptListChanged++;
140b6217683SBrian Somers   }
141b6217683SBrian Somers }
142af57ed9fSAtsushi Murai 
1430f2f3eb3SBrian Somers void
1440f2f3eb3SBrian Somers log_DestroyPrompts(struct server *s)
1450f2f3eb3SBrian Somers {
146a39fd214SBrian Somers   struct prompt *p, *pn, *pl;
1470f2f3eb3SBrian Somers 
1480f2f3eb3SBrian Somers   p = promptlist;
149a39fd214SBrian Somers   pl = NULL;
1500f2f3eb3SBrian Somers   while (p) {
1510f2f3eb3SBrian Somers     pn = p->next;
152a39fd214SBrian Somers     if (s && p->owner == s) {
153a39fd214SBrian Somers       if (pl)
154a39fd214SBrian Somers         pl->next = p->next;
155a39fd214SBrian Somers       else
156a39fd214SBrian Somers         promptlist = p->next;
1570f2f3eb3SBrian Somers       p->next = NULL;
1580f2f3eb3SBrian Somers       prompt_Destroy(p, 1);
159a39fd214SBrian Somers     } else
160a39fd214SBrian Somers       pl = p;
1610f2f3eb3SBrian Somers     p = pn;
1620f2f3eb3SBrian Somers   }
1630f2f3eb3SBrian Somers }
1640f2f3eb3SBrian Somers 
1650f2f3eb3SBrian Somers void
166672eba24SJohn Baldwin log_DisplayPrompts(void)
1670f2f3eb3SBrian Somers {
1680f2f3eb3SBrian Somers   struct prompt *p;
1690f2f3eb3SBrian Somers 
1700f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next)
1710f2f3eb3SBrian Somers     prompt_Required(p);
1720f2f3eb3SBrian Somers }
1730f2f3eb3SBrian Somers 
1740f2f3eb3SBrian Somers void
175bf1d3ff6SBrian Somers log_WritePrompts(struct datalink *dl, const char *fmt,...)
1760f2f3eb3SBrian Somers {
177bf1d3ff6SBrian Somers   va_list ap;
1780f2f3eb3SBrian Somers   struct prompt *p;
1790f2f3eb3SBrian Somers 
180bf1d3ff6SBrian Somers   va_start(ap, fmt);
1810f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next)
1820f2f3eb3SBrian Somers     if (prompt_IsTermMode(p, dl))
183bf1d3ff6SBrian Somers       prompt_vPrintf(p, fmt, ap);
184bf1d3ff6SBrian Somers   va_end(ap);
1850f2f3eb3SBrian Somers }
1860f2f3eb3SBrian Somers 
1870f2f3eb3SBrian Somers void
1880f2f3eb3SBrian Somers log_SetTtyCommandMode(struct datalink *dl)
1890f2f3eb3SBrian Somers {
1900f2f3eb3SBrian Somers   struct prompt *p;
1910f2f3eb3SBrian Somers 
1920f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next)
1930f2f3eb3SBrian Somers     if (prompt_IsTermMode(p, dl))
1940f2f3eb3SBrian Somers       prompt_TtyCommandMode(p);
1950f2f3eb3SBrian Somers }
1960f2f3eb3SBrian Somers 
197927145beSBrian Somers static int
198927145beSBrian Somers syslogLevel(int lev)
199927145beSBrian Somers {
200927145beSBrian Somers   switch (lev) {
20146df5aa7SBrian Somers   case LogLOG:
20246df5aa7SBrian Somers     return LOG_INFO;
2036f384573SBrian Somers   case LogDEBUG:
2046f384573SBrian Somers   case LogTIMER:
2056f384573SBrian Somers     return LOG_DEBUG;
206944f7098SBrian Somers   case LogWARN:
207944f7098SBrian Somers     return LOG_WARNING;
208944f7098SBrian Somers   case LogERROR:
209944f7098SBrian Somers     return LOG_ERR;
210944f7098SBrian Somers   case LogALERT:
211944f7098SBrian Somers     return LOG_ALERT;
212927145beSBrian Somers   }
213927145beSBrian Somers   return lev >= LogMIN && lev <= LogMAX ? LOG_INFO : 0;
214927145beSBrian Somers }
215af57ed9fSAtsushi Murai 
216927145beSBrian Somers const char *
217dd7e2610SBrian Somers log_Name(int id)
218927145beSBrian Somers {
21946df5aa7SBrian Somers   if (id == LogLOG)
22046df5aa7SBrian Somers     return "LOG";
221927145beSBrian Somers   return id < LogMIN || id > LogMAX ? "Unknown" : LogNames[id - 1];
222927145beSBrian Somers }
223af57ed9fSAtsushi Murai 
224af57ed9fSAtsushi Murai void
225dd7e2610SBrian Somers log_Keep(int id)
226af57ed9fSAtsushi Murai {
227927145beSBrian Somers   if (id >= LogMIN && id <= LogMAXCONF)
228927145beSBrian Somers     LogMask |= MSK(id);
229af57ed9fSAtsushi Murai }
230927145beSBrian Somers 
231927145beSBrian Somers void
232dd7e2610SBrian Somers log_KeepLocal(int id, u_long *mask)
233a1e8f937SBrian Somers {
234b6217683SBrian Somers   if (id >= LogMIN && id <= LogMAXCONF) {
235a1e8f937SBrian Somers     LogMaskLocal |= MSK(id);
236b6217683SBrian Somers     *mask |= MSK(id);
237b6217683SBrian Somers   }
238a1e8f937SBrian Somers }
239a1e8f937SBrian Somers 
240a1e8f937SBrian Somers void
241dd7e2610SBrian Somers log_Discard(int id)
242927145beSBrian Somers {
243927145beSBrian Somers   if (id >= LogMIN && id <= LogMAXCONF)
244927145beSBrian Somers     LogMask &= ~MSK(id);
245927145beSBrian Somers }
246927145beSBrian Somers 
247927145beSBrian Somers void
248dd7e2610SBrian Somers log_DiscardLocal(int id, u_long *mask)
249a1e8f937SBrian Somers {
250b6217683SBrian Somers   if (id >= LogMIN && id <= LogMAXCONF) {
251b6217683SBrian Somers     *mask &= ~MSK(id);
25267568487SBrian Somers     LogSetMaskLocal();
253b6217683SBrian Somers   }
254a1e8f937SBrian Somers }
255a1e8f937SBrian Somers 
256a1e8f937SBrian Somers void
257672eba24SJohn Baldwin log_DiscardAll(void)
258927145beSBrian Somers {
259927145beSBrian Somers   LogMask = 0;
260af57ed9fSAtsushi Murai }
261af57ed9fSAtsushi Murai 
262a1e8f937SBrian Somers void
263dd7e2610SBrian Somers log_DiscardAllLocal(u_long *mask)
264a1e8f937SBrian Somers {
26567568487SBrian Somers   *mask = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
26667568487SBrian Somers   LogSetMaskLocal();
267a1e8f937SBrian Somers }
268a1e8f937SBrian Somers 
269af57ed9fSAtsushi Murai int
270dd7e2610SBrian Somers log_IsKept(int id)
271af57ed9fSAtsushi Murai {
27246df5aa7SBrian Somers   if (id == LogLOG)
27346df5aa7SBrian Somers     return LOG_KEPT_SYSLOG;
274a1e8f937SBrian Somers   if (id < LogMIN || id > LogMAX)
275927145beSBrian Somers     return 0;
276a1e8f937SBrian Somers   if (id > LogMAXCONF)
277a1e8f937SBrian Somers     return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
278a1e8f937SBrian Somers 
279a1e8f937SBrian Somers   return ((LogMaskLocal & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
280a1e8f937SBrian Somers     ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
281af57ed9fSAtsushi Murai }
282af57ed9fSAtsushi Murai 
283b6217683SBrian Somers int
284dd7e2610SBrian Somers log_IsKeptLocal(int id, u_long mask)
285b6217683SBrian Somers {
286b6217683SBrian Somers   if (id < LogMIN || id > LogMAX)
287b6217683SBrian Somers     return 0;
288b6217683SBrian Somers   if (id > LogMAXCONF)
289b6217683SBrian Somers     return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
290b6217683SBrian Somers 
291b6217683SBrian Somers   return ((mask & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
292b6217683SBrian Somers     ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
293b6217683SBrian Somers }
294b6217683SBrian Somers 
295af57ed9fSAtsushi Murai void
296dd7e2610SBrian Somers log_Open(const char *Name)
297af57ed9fSAtsushi Murai {
298927145beSBrian Somers   openlog(Name, LOG_PID, LOG_DAEMON);
299af57ed9fSAtsushi Murai }
300af57ed9fSAtsushi Murai 
301af57ed9fSAtsushi Murai void
302f2f5156fSBrian Somers log_SetTun(int tunno, const char *ifaceName)
303af57ed9fSAtsushi Murai {
304927145beSBrian Somers   LogTunno = tunno;
305f2f5156fSBrian Somers   LogIfaceName = ifaceName;
306af57ed9fSAtsushi Murai }
307af57ed9fSAtsushi Murai 
308af57ed9fSAtsushi Murai void
309672eba24SJohn Baldwin log_Close(void)
310af57ed9fSAtsushi Murai {
311927145beSBrian Somers   closelog();
312927145beSBrian Somers   LogTunno = -1;
313f2f5156fSBrian Somers   LogIfaceName = NULL;
3146ed9fb2fSBrian Somers }
315af57ed9fSAtsushi Murai 
316af57ed9fSAtsushi Murai void
317dd7e2610SBrian Somers log_Printf(int lev, const char *fmt,...)
31853c9f6c0SAtsushi Murai {
31953c9f6c0SAtsushi Murai   va_list ap;
320b6217683SBrian Somers   struct prompt *prompt;
321944f7098SBrian Somers 
322dd7e2610SBrian Somers   if (log_IsKept(lev)) {
323d93d3a9cSBrian Somers     char nfmt[200];
32453c9f6c0SAtsushi Murai 
32590d25921SBrian Somers     va_start(ap, fmt);
326b4f63b0bSBrian Somers     if (promptlist && (log_IsKept(lev) & LOG_KEPT_LOCAL)) {
327f2f5156fSBrian Somers       if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1) {
328f2f5156fSBrian Somers         if (LogIfaceName)
329f2f5156fSBrian Somers           snprintf(nfmt, sizeof nfmt, "%s%d(%s): %s: %s", TUN_NAME,
330f2f5156fSBrian Somers 	         LogTunno, LogIfaceName, log_Name(lev), fmt);
331f2f5156fSBrian Somers         else
3321384bd27SBrian Somers           snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
333dd7e2610SBrian Somers 	         LogTunno, log_Name(lev), fmt);
334f2f5156fSBrian Somers       } else
335dd7e2610SBrian Somers         snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
336b6217683SBrian Somers 
337b4f63b0bSBrian Somers       if (log_PromptContext && lev == LogWARN)
338b4f63b0bSBrian Somers         /* Warnings just go to the current prompt */
339b4f63b0bSBrian Somers         prompt_vPrintf(log_PromptContext, nfmt, ap);
340b4f63b0bSBrian Somers       else for (prompt = promptlist; prompt; prompt = prompt->next)
341b6217683SBrian Somers         if (lev > LogMAXCONF || (prompt->logmask & MSK(lev)))
342b6217683SBrian Somers           prompt_vPrintf(prompt, nfmt, ap);
343a1e8f937SBrian Somers     }
34490d25921SBrian Somers     va_end(ap);
345a1e8f937SBrian Somers 
34690d25921SBrian Somers     va_start(ap, fmt);
3470f2f3eb3SBrian Somers     if ((log_IsKept(lev) & LOG_KEPT_SYSLOG) &&
348b4f63b0bSBrian Somers         (lev != LogWARN || !log_PromptContext)) {
349f2f5156fSBrian Somers       if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1) {
350f2f5156fSBrian Somers         if (LogIfaceName)
351f2f5156fSBrian Somers           snprintf(nfmt, sizeof nfmt, "%s%d(%s): %s: %s", TUN_NAME,
352f2f5156fSBrian Somers 	         LogTunno, LogIfaceName, log_Name(lev), fmt);
353f2f5156fSBrian Somers         else
3541384bd27SBrian Somers           snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
355dd7e2610SBrian Somers 	         LogTunno, log_Name(lev), fmt);
356f2f5156fSBrian Somers       } else
357dd7e2610SBrian Somers         snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
358927145beSBrian Somers       vsyslog(syslogLevel(lev), nfmt, ap);
359927145beSBrian Somers     }
36053c9f6c0SAtsushi Murai     va_end(ap);
36153c9f6c0SAtsushi Murai   }
36290d25921SBrian Somers }
36353c9f6c0SAtsushi Murai 
36453c9f6c0SAtsushi Murai void
365dd7e2610SBrian Somers log_DumpBp(int lev, const char *hdr, const struct mbuf *bp)
366af57ed9fSAtsushi Murai {
367dd7e2610SBrian Somers   if (log_IsKept(lev)) {
368ba16c840SBrian Somers     char buf[68];
369ba16c840SBrian Somers     char *b, *c;
3709773f8c0SBrian Somers     const u_char *ptr;
371a9e8f807SBrian Somers     int f;
372a9e8f807SBrian Somers 
373a9e8f807SBrian Somers     if (hdr && *hdr)
374dd7e2610SBrian Somers       log_Printf(lev, "%s\n", hdr);
375a9e8f807SBrian Somers 
376a9e8f807SBrian Somers     b = buf;
377ba16c840SBrian Somers     c = b + 50;
378a9e8f807SBrian Somers     do {
37926af0ae9SBrian Somers       f = bp->m_len;
3809773f8c0SBrian Somers       ptr = CONST_MBUF_CTOP(bp);
381a9e8f807SBrian Somers       while (f--) {
382ba16c840SBrian Somers 	sprintf(b, " %02x", (int) *ptr);
383ba16c840SBrian Somers         *c++ = isprint(*ptr) ? *ptr : '.';
384ba16c840SBrian Somers         ptr++;
385a9e8f807SBrian Somers         b += 3;
386ba16c840SBrian Somers         if (b == buf + 48) {
387ba16c840SBrian Somers           memset(b, ' ', 2);
3889cf01ccfSBrian Somers           *c = '\0';
3899cf01ccfSBrian Somers           log_Printf(lev, "%s\n", buf);
390a9e8f807SBrian Somers           b = buf;
391ba16c840SBrian Somers           c = b + 50;
392a9e8f807SBrian Somers         }
393a9e8f807SBrian Somers       }
39426af0ae9SBrian Somers     } while ((bp = bp->m_next) != NULL);
395a9e8f807SBrian Somers 
396a1e8f937SBrian Somers     if (b > buf) {
397ba16c840SBrian Somers       memset(b, ' ', 50 - (b - buf));
3989cf01ccfSBrian Somers       *c = '\0';
3999cf01ccfSBrian Somers       log_Printf(lev, "%s\n", buf);
400a9e8f807SBrian Somers     }
401af57ed9fSAtsushi Murai   }
402a1e8f937SBrian Somers }
403af57ed9fSAtsushi Murai 
404af57ed9fSAtsushi Murai void
405dd7e2610SBrian Somers log_DumpBuff(int lev, const char *hdr, const u_char *ptr, int n)
406af57ed9fSAtsushi Murai {
407dd7e2610SBrian Somers   if (log_IsKept(lev)) {
408ba16c840SBrian Somers     char buf[68];
409ba16c840SBrian Somers     char *b, *c;
410af57ed9fSAtsushi Murai 
411927145beSBrian Somers     if (hdr && *hdr)
412dd7e2610SBrian Somers       log_Printf(lev, "%s\n", hdr);
413927145beSBrian Somers     while (n > 0) {
414927145beSBrian Somers       b = buf;
415ba16c840SBrian Somers       c = b + 50;
416ba16c840SBrian Somers       for (b = buf; b != buf + 48 && n--; b += 3, ptr++) {
417ba16c840SBrian Somers 	sprintf(b, " %02x", (int) *ptr);
418ba16c840SBrian Somers         *c++ = isprint(*ptr) ? *ptr : '.';
419ba16c840SBrian Somers       }
420ba16c840SBrian Somers       memset(b, ' ', 50 - (b - buf));
4219cf01ccfSBrian Somers       *c = '\0';
4229cf01ccfSBrian Somers       log_Printf(lev, "%s\n", buf);
423af57ed9fSAtsushi Murai     }
424af57ed9fSAtsushi Murai   }
425c6c740beSBrian Somers }
426b6217683SBrian Somers 
427b6217683SBrian Somers int
428b6217683SBrian Somers log_ShowLevel(struct cmdargs const *arg)
429b6217683SBrian Somers {
430b6217683SBrian Somers   int i;
431b6217683SBrian Somers 
432b6217683SBrian Somers   prompt_Printf(arg->prompt, "Log:  ");
433b6217683SBrian Somers   for (i = LogMIN; i <= LogMAX; i++)
434dd7e2610SBrian Somers     if (log_IsKept(i) & LOG_KEPT_SYSLOG)
435dd7e2610SBrian Somers       prompt_Printf(arg->prompt, " %s", log_Name(i));
436b6217683SBrian Somers 
437b6217683SBrian Somers   prompt_Printf(arg->prompt, "\nLocal:");
438b6217683SBrian Somers   for (i = LogMIN; i <= LogMAX; i++)
439dd7e2610SBrian Somers     if (log_IsKeptLocal(i, arg->prompt->logmask) & LOG_KEPT_LOCAL)
440dd7e2610SBrian Somers       prompt_Printf(arg->prompt, " %s", log_Name(i));
441b6217683SBrian Somers 
442b6217683SBrian Somers   prompt_Printf(arg->prompt, "\n");
443b6217683SBrian Somers 
444b6217683SBrian Somers   return 0;
445b6217683SBrian Somers }
446b6217683SBrian Somers 
447b6217683SBrian Somers int
448b6217683SBrian Somers log_SetLevel(struct cmdargs const *arg)
449b6217683SBrian Somers {
450b6217683SBrian Somers   int i, res, argc, local;
451b6217683SBrian Somers   char const *const *argv, *argp;
452b6217683SBrian Somers 
45325092092SBrian Somers   argc = arg->argc - arg->argn;
45425092092SBrian Somers   argv = arg->argv + arg->argn;
455b6217683SBrian Somers   res = 0;
456b6217683SBrian Somers 
457b6217683SBrian Somers   if (argc == 0 || strcasecmp(argv[0], "local"))
458b6217683SBrian Somers     local = 0;
459b6217683SBrian Somers   else {
460b6217683SBrian Somers     if (arg->prompt == NULL) {
4619b996792SBrian Somers       log_Printf(LogWARN, "set log local: Only available on the"
4629b996792SBrian Somers                  " command line\n");
463b6217683SBrian Somers       return 1;
464b6217683SBrian Somers     }
465b6217683SBrian Somers     argc--;
466b6217683SBrian Somers     argv++;
467b6217683SBrian Somers     local = 1;
468b6217683SBrian Somers   }
469b6217683SBrian Somers 
470e43ebac1SBrian Somers   if (argc == 0 || (argv[0][0] != '+' && argv[0][0] != '-')) {
471b6217683SBrian Somers     if (local)
472dd7e2610SBrian Somers       log_DiscardAllLocal(&arg->prompt->logmask);
473b6217683SBrian Somers     else
474dd7e2610SBrian Somers       log_DiscardAll();
475e43ebac1SBrian Somers   }
476b6217683SBrian Somers 
477b6217683SBrian Somers   while (argc--) {
478b6217683SBrian Somers     argp = **argv == '+' || **argv == '-' ? *argv + 1 : *argv;
4797f03ca53SBrian Somers     /* Special case 'all' */
4807f03ca53SBrian Somers     if (strcasecmp(argp, "all") == 0) {
4817f03ca53SBrian Somers         if (**argv == '-') {
4827f03ca53SBrian Somers           if (local)
4837f03ca53SBrian Somers             for (i = LogMIN; i <= LogMAX; i++)
4847f03ca53SBrian Somers               log_DiscardLocal(i, &arg->prompt->logmask);
4857f03ca53SBrian Somers           else
4867f03ca53SBrian Somers             for (i = LogMIN; i <= LogMAX; i++)
4877f03ca53SBrian Somers               log_Discard(i);
4887f03ca53SBrian Somers         } else if (local)
4897f03ca53SBrian Somers           for (i = LogMIN; i <= LogMAX; i++)
4907f03ca53SBrian Somers             log_KeepLocal(i, &arg->prompt->logmask);
4917f03ca53SBrian Somers         else
4927f03ca53SBrian Somers           for (i = LogMIN; i <= LogMAX; i++)
4937f03ca53SBrian Somers             log_Keep(i);
4947f03ca53SBrian Somers         argv++;
4957f03ca53SBrian Somers         continue;
4967f03ca53SBrian Somers     }
497b6217683SBrian Somers     for (i = LogMIN; i <= LogMAX; i++)
498dd7e2610SBrian Somers       if (strcasecmp(argp, log_Name(i)) == 0) {
499e43ebac1SBrian Somers 	if (**argv == '-') {
500b6217683SBrian Somers           if (local)
501dd7e2610SBrian Somers             log_DiscardLocal(i, &arg->prompt->logmask);
502b6217683SBrian Somers           else
503dd7e2610SBrian Somers 	    log_Discard(i);
504e43ebac1SBrian Somers 	} else if (local)
505dd7e2610SBrian Somers           log_KeepLocal(i, &arg->prompt->logmask);
506b6217683SBrian Somers         else
507dd7e2610SBrian Somers           log_Keep(i);
508b6217683SBrian Somers 	break;
509b6217683SBrian Somers       }
510b6217683SBrian Somers     if (i > LogMAX) {
511dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Invalid log value\n", argp);
512b6217683SBrian Somers       res = -1;
513b6217683SBrian Somers     }
514b6217683SBrian Somers     argv++;
515b6217683SBrian Somers   }
516b6217683SBrian Somers   return res;
517b6217683SBrian Somers }
518b6217683SBrian Somers 
519b6217683SBrian Somers int
520b6217683SBrian Somers log_ShowWho(struct cmdargs const *arg)
521b6217683SBrian Somers {
522b6217683SBrian Somers   struct prompt *p;
523b6217683SBrian Somers 
5240f2f3eb3SBrian Somers   for (p = promptlist; p; p = p->next) {
525565e35e5SBrian Somers     prompt_Printf(arg->prompt, "%s (%s)", p->src.type, p->src.from);
526f91ad6b0SBrian Somers     if (p == arg->prompt)
527f91ad6b0SBrian Somers       prompt_Printf(arg->prompt, " *");
528f91ad6b0SBrian Somers     if (!p->active)
529f91ad6b0SBrian Somers       prompt_Printf(arg->prompt, " ^Z");
530f91ad6b0SBrian Somers     prompt_Printf(arg->prompt, "\n");
531f91ad6b0SBrian Somers   }
532b6217683SBrian Somers 
533b6217683SBrian Somers   return 0;
534b6217683SBrian Somers }
535