xref: /freebsd/usr.sbin/ppp/log.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*-
2  * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/types.h>
30 
31 #include <ctype.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <syslog.h>
36 #include <termios.h>
37 
38 #include "defs.h"
39 #include "command.h"
40 #include "mbuf.h"
41 #include "log.h"
42 #include "descriptor.h"
43 #include "prompt.h"
44 
45 static const char * const LogNames[] = {
46   "Async",
47   "CBCP",
48   "CCP",
49   "Chat",
50   "Command",
51   "Connect",
52   "Debug",
53   "DNS",
54   "Filter",			/* Log discarded packets */
55   "HDLC",
56   "ID0",
57   "IPCP",
58   "LCP",
59   "LQM",
60   "Phase",
61   "Physical",
62   "Sync",
63   "TCP/IP",
64   "Timer",
65   "Tun",
66   "Warning",
67   "Error",
68   "Alert"
69 };
70 
71 #define MSK(n) (1<<((n)-1))
72 
73 static u_long LogMask = MSK(LogPHASE);
74 static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
75 static int LogTunno = -1;
76 static struct prompt *promptlist;	/* Where to log local stuff */
77 struct prompt *log_PromptContext;
78 int log_PromptListChanged;
79 
80 struct prompt *
81 log_PromptList()
82 {
83   return promptlist;
84 }
85 
86 void
87 log_RegisterPrompt(struct prompt *prompt)
88 {
89   prompt->next = promptlist;
90   promptlist = prompt;
91   prompt->active = 1;
92   log_DiscardAllLocal(&prompt->logmask);
93 }
94 
95 void
96 log_ActivatePrompt(struct prompt *prompt)
97 {
98   prompt->active = 1;
99   LogMaskLocal |= prompt->logmask;
100 }
101 
102 static void
103 LogSetMaskLocal(void)
104 {
105   struct prompt *p;
106 
107   LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
108   for (p = promptlist; p; p = p->next)
109     LogMaskLocal |= p->logmask;
110 }
111 
112 void
113 log_DeactivatePrompt(struct prompt *prompt)
114 {
115   if (prompt->active) {
116     prompt->active = 0;
117     LogSetMaskLocal();
118   }
119 }
120 
121 void
122 log_UnRegisterPrompt(struct prompt *prompt)
123 {
124   if (prompt) {
125     struct prompt **p;
126 
127     for (p = &promptlist; *p; p = &(*p)->next)
128       if (*p == prompt) {
129         *p = prompt->next;
130         prompt->next = NULL;
131         break;
132       }
133     LogSetMaskLocal();
134     log_PromptListChanged++;
135   }
136 }
137 
138 void
139 log_DestroyPrompts(struct server *s)
140 {
141   struct prompt *p, *pn, *pl;
142 
143   p = promptlist;
144   pl = NULL;
145   while (p) {
146     pn = p->next;
147     if (s && p->owner == s) {
148       if (pl)
149         pl->next = p->next;
150       else
151         promptlist = p->next;
152       p->next = NULL;
153       prompt_Destroy(p, 1);
154     } else
155       pl = p;
156     p = pn;
157   }
158 }
159 
160 void
161 log_DisplayPrompts()
162 {
163   struct prompt *p;
164 
165   for (p = promptlist; p; p = p->next)
166     prompt_Required(p);
167 }
168 
169 void
170 log_WritePrompts(struct datalink *dl, const char *fmt,...)
171 {
172   va_list ap;
173   struct prompt *p;
174 
175   va_start(ap, fmt);
176   for (p = promptlist; p; p = p->next)
177     if (prompt_IsTermMode(p, dl))
178       prompt_vPrintf(p, fmt, ap);
179   va_end(ap);
180 }
181 
182 void
183 log_SetTtyCommandMode(struct datalink *dl)
184 {
185   struct prompt *p;
186 
187   for (p = promptlist; p; p = p->next)
188     if (prompt_IsTermMode(p, dl))
189       prompt_TtyCommandMode(p);
190 }
191 
192 static int
193 syslogLevel(int lev)
194 {
195   switch (lev) {
196   case LogDEBUG:
197   case LogTIMER:
198     return LOG_DEBUG;
199   case LogWARN:
200     return LOG_WARNING;
201   case LogERROR:
202     return LOG_ERR;
203   case LogALERT:
204     return LOG_ALERT;
205   }
206   return lev >= LogMIN && lev <= LogMAX ? LOG_INFO : 0;
207 }
208 
209 const char *
210 log_Name(int id)
211 {
212   return id < LogMIN || id > LogMAX ? "Unknown" : LogNames[id - 1];
213 }
214 
215 void
216 log_Keep(int id)
217 {
218   if (id >= LogMIN && id <= LogMAXCONF)
219     LogMask |= MSK(id);
220 }
221 
222 void
223 log_KeepLocal(int id, u_long *mask)
224 {
225   if (id >= LogMIN && id <= LogMAXCONF) {
226     LogMaskLocal |= MSK(id);
227     *mask |= MSK(id);
228   }
229 }
230 
231 void
232 log_Discard(int id)
233 {
234   if (id >= LogMIN && id <= LogMAXCONF)
235     LogMask &= ~MSK(id);
236 }
237 
238 void
239 log_DiscardLocal(int id, u_long *mask)
240 {
241   if (id >= LogMIN && id <= LogMAXCONF) {
242     *mask &= ~MSK(id);
243     LogSetMaskLocal();
244   }
245 }
246 
247 void
248 log_DiscardAll()
249 {
250   LogMask = 0;
251 }
252 
253 void
254 log_DiscardAllLocal(u_long *mask)
255 {
256   *mask = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
257   LogSetMaskLocal();
258 }
259 
260 int
261 log_IsKept(int id)
262 {
263   if (id < LogMIN || id > LogMAX)
264     return 0;
265   if (id > LogMAXCONF)
266     return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
267 
268   return ((LogMaskLocal & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
269     ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
270 }
271 
272 int
273 log_IsKeptLocal(int id, u_long mask)
274 {
275   if (id < LogMIN || id > LogMAX)
276     return 0;
277   if (id > LogMAXCONF)
278     return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
279 
280   return ((mask & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
281     ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
282 }
283 
284 void
285 log_Open(const char *Name)
286 {
287   openlog(Name, LOG_PID, LOG_DAEMON);
288 }
289 
290 void
291 log_SetTun(int tunno)
292 {
293   LogTunno = tunno;
294 }
295 
296 void
297 log_Close()
298 {
299   closelog();
300   LogTunno = -1;
301 }
302 
303 void
304 log_Printf(int lev, const char *fmt,...)
305 {
306   va_list ap;
307   struct prompt *prompt;
308 
309   va_start(ap, fmt);
310   if (log_IsKept(lev)) {
311     char nfmt[200];
312 
313     if (promptlist && (log_IsKept(lev) & LOG_KEPT_LOCAL)) {
314       if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1)
315         snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
316 	         LogTunno, log_Name(lev), fmt);
317       else
318         snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
319 
320       if (log_PromptContext && lev == LogWARN)
321         /* Warnings just go to the current prompt */
322         prompt_vPrintf(log_PromptContext, nfmt, ap);
323       else for (prompt = promptlist; prompt; prompt = prompt->next)
324         if (lev > LogMAXCONF || (prompt->logmask & MSK(lev)))
325           prompt_vPrintf(prompt, nfmt, ap);
326     }
327 
328     if ((log_IsKept(lev) & LOG_KEPT_SYSLOG) &&
329         (lev != LogWARN || !log_PromptContext)) {
330       if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1)
331         snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
332 	         LogTunno, log_Name(lev), fmt);
333       else
334         snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
335       vsyslog(syslogLevel(lev), nfmt, ap);
336     }
337   }
338   va_end(ap);
339 }
340 
341 void
342 log_DumpBp(int lev, const char *hdr, const struct mbuf *bp)
343 {
344   if (log_IsKept(lev)) {
345     char buf[68];
346     char *b, *c;
347     const u_char *ptr;
348     int f;
349 
350     if (hdr && *hdr)
351       log_Printf(lev, "%s\n", hdr);
352 
353     b = buf;
354     c = b + 50;
355     do {
356       f = bp->m_len;
357       ptr = CONST_MBUF_CTOP(bp);
358       while (f--) {
359 	sprintf(b, " %02x", (int) *ptr);
360         *c++ = isprint(*ptr) ? *ptr : '.';
361         ptr++;
362         b += 3;
363         if (b == buf + 48) {
364           memset(b, ' ', 2);
365           *c = '\0';
366           log_Printf(lev, "%s\n", buf);
367           b = buf;
368           c = b + 50;
369         }
370       }
371     } while ((bp = bp->m_next) != NULL);
372 
373     if (b > buf) {
374       memset(b, ' ', 50 - (b - buf));
375       *c = '\0';
376       log_Printf(lev, "%s\n", buf);
377     }
378   }
379 }
380 
381 void
382 log_DumpBuff(int lev, const char *hdr, const u_char *ptr, int n)
383 {
384   if (log_IsKept(lev)) {
385     char buf[68];
386     char *b, *c;
387 
388     if (hdr && *hdr)
389       log_Printf(lev, "%s\n", hdr);
390     while (n > 0) {
391       b = buf;
392       c = b + 50;
393       for (b = buf; b != buf + 48 && n--; b += 3, ptr++) {
394 	sprintf(b, " %02x", (int) *ptr);
395         *c++ = isprint(*ptr) ? *ptr : '.';
396       }
397       memset(b, ' ', 50 - (b - buf));
398       *c = '\0';
399       log_Printf(lev, "%s\n", buf);
400     }
401   }
402 }
403 
404 int
405 log_ShowLevel(struct cmdargs const *arg)
406 {
407   int i;
408 
409   prompt_Printf(arg->prompt, "Log:  ");
410   for (i = LogMIN; i <= LogMAX; i++)
411     if (log_IsKept(i) & LOG_KEPT_SYSLOG)
412       prompt_Printf(arg->prompt, " %s", log_Name(i));
413 
414   prompt_Printf(arg->prompt, "\nLocal:");
415   for (i = LogMIN; i <= LogMAX; i++)
416     if (log_IsKeptLocal(i, arg->prompt->logmask) & LOG_KEPT_LOCAL)
417       prompt_Printf(arg->prompt, " %s", log_Name(i));
418 
419   prompt_Printf(arg->prompt, "\n");
420 
421   return 0;
422 }
423 
424 int
425 log_SetLevel(struct cmdargs const *arg)
426 {
427   int i, res, argc, local;
428   char const *const *argv, *argp;
429 
430   argc = arg->argc - arg->argn;
431   argv = arg->argv + arg->argn;
432   res = 0;
433 
434   if (argc == 0 || strcasecmp(argv[0], "local"))
435     local = 0;
436   else {
437     if (arg->prompt == NULL) {
438       log_Printf(LogWARN, "set log local: Only available on the"
439                  " command line\n");
440       return 1;
441     }
442     argc--;
443     argv++;
444     local = 1;
445   }
446 
447   if (argc == 0 || (argv[0][0] != '+' && argv[0][0] != '-')) {
448     if (local)
449       log_DiscardAllLocal(&arg->prompt->logmask);
450     else
451       log_DiscardAll();
452   }
453 
454   while (argc--) {
455     argp = **argv == '+' || **argv == '-' ? *argv + 1 : *argv;
456     /* Special case 'all' */
457     if (strcasecmp(argp, "all") == 0) {
458         if (**argv == '-') {
459           if (local)
460             for (i = LogMIN; i <= LogMAX; i++)
461               log_DiscardLocal(i, &arg->prompt->logmask);
462           else
463             for (i = LogMIN; i <= LogMAX; i++)
464               log_Discard(i);
465         } else if (local)
466           for (i = LogMIN; i <= LogMAX; i++)
467             log_KeepLocal(i, &arg->prompt->logmask);
468         else
469           for (i = LogMIN; i <= LogMAX; i++)
470             log_Keep(i);
471         argv++;
472         continue;
473     }
474     for (i = LogMIN; i <= LogMAX; i++)
475       if (strcasecmp(argp, log_Name(i)) == 0) {
476 	if (**argv == '-') {
477           if (local)
478             log_DiscardLocal(i, &arg->prompt->logmask);
479           else
480 	    log_Discard(i);
481 	} else if (local)
482           log_KeepLocal(i, &arg->prompt->logmask);
483         else
484           log_Keep(i);
485 	break;
486       }
487     if (i > LogMAX) {
488       log_Printf(LogWARN, "%s: Invalid log value\n", argp);
489       res = -1;
490     }
491     argv++;
492   }
493   return res;
494 }
495 
496 int
497 log_ShowWho(struct cmdargs const *arg)
498 {
499   struct prompt *p;
500 
501   for (p = promptlist; p; p = p->next) {
502     prompt_Printf(arg->prompt, "%s (%s)", p->src.type, p->src.from);
503     if (p == arg->prompt)
504       prompt_Printf(arg->prompt, " *");
505     if (!p->active)
506       prompt_Printf(arg->prompt, " ^Z");
507     prompt_Printf(arg->prompt, "\n");
508   }
509 
510   return 0;
511 }
512