xref: /netbsd-src/external/bsd/tcpdump/dist/print-syslog.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*
2  * Copyright (c) 1998-2004  Hannes Gredler <hannes@tcpdump.org>
3  *      The TCPDUMP project
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code
7  * distributions retain the above copyright notice and this paragraph
8  * in its entirety, and (2) distributions including binary code include
9  * the above copyright notice and this paragraph in its entirety in
10  * the documentation or other materials provided with the distribution.
11  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
12  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
13  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14  * FOR A PARTICULAR PURPOSE.
15  */
16 
17 #include <sys/cdefs.h>
18 #ifndef lint
19 #if 0
20 static const char rcsid[] _U_ =
21     "@(#) Header: /tcpdump/master/tcpdump/print-syslog.c,v 1.1 2004-10-29 11:42:53 hannes Exp";
22 #else
23 __RCSID("$NetBSD: print-syslog.c,v 1.2 2010/12/05 05:11:31 christos Exp $");
24 #endif
25 #endif
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <tcpdump-stdinc.h>
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include "interface.h"
37 #include "extract.h"
38 #include "addrtoname.h"
39 
40 /*
41  * tokenlists and #defines taken from Ethereal - Network traffic analyzer
42  * by Gerald Combs <gerald@ethereal.com>
43  */
44 
45 #define SYSLOG_SEVERITY_MASK 0x0007  /* 0000 0000 0000 0111 */
46 #define SYSLOG_FACILITY_MASK 0x03f8  /* 0000 0011 1111 1000 */
47 #define SYSLOG_MAX_DIGITS 3 /* The maximum number if priority digits to read in. */
48 
49 static const struct tok syslog_severity_values[] = {
50   { 0,      "emergency" },
51   { 1,      "alert" },
52   { 2,      "critical" },
53   { 3,      "error" },
54   { 4,      "warning" },
55   { 5,      "notice" },
56   { 6,      "info" },
57   { 7,      "debug" },
58   { 0, NULL },
59 };
60 
61 static const struct tok syslog_facility_values[] = {
62   { 0,     "kernel" },
63   { 1,     "user" },
64   { 2,     "mail" },
65   { 3,     "daemon" },
66   { 4,     "auth" },
67   { 5,     "syslog" },
68   { 6,     "lpr" },
69   { 7,     "news" },
70   { 8,     "uucp" },
71   { 9,     "cron" },
72   { 10,    "authpriv" },
73   { 11,    "ftp" },
74   { 12,    "ntp" },
75   { 13,    "security" },
76   { 14,    "console" },
77   { 15,    "cron" },
78   { 16,    "local0" },
79   { 17,    "local1" },
80   { 18,    "local2" },
81   { 19,    "local3" },
82   { 20,    "local4" },
83   { 21,    "local5" },
84   { 22,    "local6" },
85   { 23,    "local7" },
86   { 0, NULL },
87 };
88 
89 void
90 syslog_print(register const u_char *pptr, register u_int len)
91 {
92     u_int16_t msg_off = 0;
93     u_int16_t pri = 0;
94     u_int16_t facility,severity;
95 
96     /* extract decimal figures that are
97      * encapsulated within < > tags
98      * based on this decimal figure extract the
99      * severity and facility values
100      */
101 
102     if (!TTEST2(*pptr, 1))
103         goto trunc;
104 
105     if (*(pptr+msg_off) == '<') {
106         msg_off++;
107 
108         if (!TTEST2(*(pptr+msg_off), 1))
109             goto trunc;
110 
111         while ( *(pptr+msg_off) >= '0' &&
112                 *(pptr+msg_off) <= '9' &&
113                 msg_off <= SYSLOG_MAX_DIGITS) {
114 
115             if (!TTEST2(*(pptr+msg_off), 1))
116                 goto trunc;
117 
118             pri = pri * 10 + (*(pptr+msg_off) - '0');
119             msg_off++;
120 
121             if (!TTEST2(*(pptr+msg_off), 1))
122                 goto trunc;
123 
124         if (*(pptr+msg_off) == '>')
125             msg_off++;
126         }
127     } else {
128         printf("[|syslog]");
129         return;
130     }
131 
132     facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
133     severity = pri & SYSLOG_SEVERITY_MASK;
134 
135 
136     if (vflag < 1 )
137     {
138         printf("SYSLOG %s.%s, length: %u",
139                tok2str(syslog_facility_values, "unknown (%u)", facility),
140                tok2str(syslog_severity_values, "unknown (%u)", severity),
141                len);
142         return;
143     }
144 
145     printf("SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
146            len,
147            tok2str(syslog_facility_values, "unknown (%u)", facility),
148            facility,
149            tok2str(syslog_severity_values, "unknown (%u)", severity),
150            severity);
151 
152     /* print the syslog text in verbose mode */
153     for (; msg_off < len; msg_off++) {
154         if (!TTEST2(*(pptr+msg_off), 1))
155             goto trunc;
156         safeputchar(*(pptr+msg_off));
157     }
158 
159     if (vflag > 1) {
160         if(!print_unknown_data(pptr,"\n\t",len))
161             return;
162     }
163 
164     return;
165 
166 trunc:
167         printf("[|syslog]");
168 }
169