xref: /netbsd-src/external/bsd/tcpdump/dist/print-syslog.c (revision cc576e1d8e4f4078fd4e81238abca9fca216f6ec)
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 __RCSID("$NetBSD: print-syslog.c,v 1.6 2017/01/24 23:29:14 christos Exp $");
20 #endif
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <netdissect-stdinc.h>
27 
28 #include "netdissect.h"
29 #include "extract.h"
30 
31 static const char tstr[] = "[|syslog]";
32 
33 /*
34  * tokenlists and #defines taken from Ethereal - Network traffic analyzer
35  * by Gerald Combs <gerald@ethereal.com>
36  */
37 
38 #define SYSLOG_SEVERITY_MASK 0x0007  /* 0000 0000 0000 0111 */
39 #define SYSLOG_FACILITY_MASK 0x03f8  /* 0000 0011 1111 1000 */
40 #define SYSLOG_MAX_DIGITS 3 /* The maximum number if priority digits to read in. */
41 
42 static const struct tok syslog_severity_values[] = {
43   { 0,      "emergency" },
44   { 1,      "alert" },
45   { 2,      "critical" },
46   { 3,      "error" },
47   { 4,      "warning" },
48   { 5,      "notice" },
49   { 6,      "info" },
50   { 7,      "debug" },
51   { 0, NULL },
52 };
53 
54 static const struct tok syslog_facility_values[] = {
55   { 0,     "kernel" },
56   { 1,     "user" },
57   { 2,     "mail" },
58   { 3,     "daemon" },
59   { 4,     "auth" },
60   { 5,     "syslog" },
61   { 6,     "lpr" },
62   { 7,     "news" },
63   { 8,     "uucp" },
64   { 9,     "cron" },
65   { 10,    "authpriv" },
66   { 11,    "ftp" },
67   { 12,    "ntp" },
68   { 13,    "security" },
69   { 14,    "console" },
70   { 15,    "cron" },
71   { 16,    "local0" },
72   { 17,    "local1" },
73   { 18,    "local2" },
74   { 19,    "local3" },
75   { 20,    "local4" },
76   { 21,    "local5" },
77   { 22,    "local6" },
78   { 23,    "local7" },
79   { 0, NULL },
80 };
81 
82 void
83 syslog_print(netdissect_options *ndo,
84              register const u_char *pptr, register u_int len)
85 {
86     uint16_t msg_off = 0;
87     uint16_t pri = 0;
88     uint16_t facility,severity;
89 
90     /* extract decimal figures that are
91      * encapsulated within < > tags
92      * based on this decimal figure extract the
93      * severity and facility values
94      */
95 
96     ND_TCHECK2(*pptr, 1);
97     if (*(pptr+msg_off) == '<') {
98         msg_off++;
99         ND_TCHECK2(*(pptr + msg_off), 1);
100         while ( *(pptr+msg_off) >= '0' &&
101                 *(pptr+msg_off) <= '9' &&
102                 msg_off <= SYSLOG_MAX_DIGITS) {
103             pri = pri * 10 + (*(pptr+msg_off) - '0');
104             msg_off++;
105             ND_TCHECK2(*(pptr + msg_off), 1);
106         }
107         if (*(pptr+msg_off) != '>') {
108             ND_PRINT((ndo, "%s", tstr));
109             return;
110         }
111         msg_off++;
112     } else {
113         ND_PRINT((ndo, "%s", tstr));
114         return;
115     }
116 
117     facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
118     severity = pri & SYSLOG_SEVERITY_MASK;
119 
120     if (ndo->ndo_vflag < 1 )
121     {
122         ND_PRINT((ndo, "SYSLOG %s.%s, length: %u",
123                tok2str(syslog_facility_values, "unknown (%u)", facility),
124                tok2str(syslog_severity_values, "unknown (%u)", severity),
125                len));
126         return;
127     }
128 
129     ND_PRINT((ndo, "SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
130            len,
131            tok2str(syslog_facility_values, "unknown (%u)", facility),
132            facility,
133            tok2str(syslog_severity_values, "unknown (%u)", severity),
134            severity));
135 
136     /* print the syslog text in verbose mode */
137     for (; msg_off < len; msg_off++) {
138         ND_TCHECK2(*(pptr + msg_off), 1);
139         safeputchar(ndo, *(pptr + msg_off));
140     }
141 
142     if (ndo->ndo_vflag > 1)
143         print_unknown_data(ndo, pptr, "\n\t", len);
144 
145     return;
146 
147 trunc:
148     ND_PRINT((ndo, "%s", tstr));
149 }
150