xref: /freebsd/contrib/tcpdump/print-syslog.c (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
1 /*
2  * Copyright (c) 1998-2004  Hannes Gredler <hannes@gredler.at>
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 /* \summary: Syslog protocol printer */
18 /* specification: RFC 3164 (not RFC 5424) */
19 
20 #include <config.h>
21 
22 #include "netdissect-stdinc.h"
23 
24 #include "netdissect.h"
25 #include "extract.h"
26 
27 
28 /*
29  * tokenlists and #defines taken from Ethereal - Network traffic analyzer
30  * by Gerald Combs <gerald@ethereal.com>
31  */
32 
33 #define SYSLOG_SEVERITY_MASK 0x0007  /* 0000 0000 0000 0111 */
34 #define SYSLOG_FACILITY_MASK 0x03f8  /* 0000 0011 1111 1000 */
35 #define SYSLOG_MAX_DIGITS 3 /* The maximum number of priority digits to read in. */
36 
37 static const struct tok syslog_severity_values[] = {
38   { 0,      "emergency" },
39   { 1,      "alert" },
40   { 2,      "critical" },
41   { 3,      "error" },
42   { 4,      "warning" },
43   { 5,      "notice" },
44   { 6,      "info" },
45   { 7,      "debug" },
46   { 0, NULL },
47 };
48 
49 static const struct tok syslog_facility_values[] = {
50   { 0,     "kernel" },
51   { 1,     "user" },
52   { 2,     "mail" },
53   { 3,     "daemon" },
54   { 4,     "auth" },
55   { 5,     "syslog" },
56   { 6,     "lpr" },
57   { 7,     "news" },
58   { 8,     "uucp" },
59   { 9,     "cron" },
60   { 10,    "authpriv" },
61   { 11,    "ftp" },
62   { 12,    "ntp" },
63   { 13,    "security" },
64   { 14,    "console" },
65   { 15,    "cron" },
66   { 16,    "local0" },
67   { 17,    "local1" },
68   { 18,    "local2" },
69   { 19,    "local3" },
70   { 20,    "local4" },
71   { 21,    "local5" },
72   { 22,    "local6" },
73   { 23,    "local7" },
74   { 0, NULL },
75 };
76 
77 void
78 syslog_print(netdissect_options *ndo,
79              const u_char *pptr, u_int len)
80 {
81     uint16_t msg_off = 0;
82     uint16_t pri = 0;
83     uint16_t facility,severity;
84 
85     ndo->ndo_protocol = "syslog";
86     /* extract decimal figures that are
87      * encapsulated within < > tags
88      * based on this decimal figure extract the
89      * severity and facility values
90      */
91 
92     if (GET_U_1(pptr) != '<')
93         goto invalid;
94     msg_off++;
95 
96     while (msg_off <= SYSLOG_MAX_DIGITS &&
97            GET_U_1(pptr + msg_off) >= '0' &&
98            GET_U_1(pptr + msg_off) <= '9') {
99         pri = pri * 10 + (GET_U_1(pptr + msg_off) - '0');
100         msg_off++;
101     }
102 
103     if (GET_U_1(pptr + msg_off) != '>')
104         goto invalid;
105     msg_off++;
106 
107     facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
108     severity = pri & SYSLOG_SEVERITY_MASK;
109 
110     if (ndo->ndo_vflag < 1 ) {
111         ND_PRINT("SYSLOG %s.%s, length: %u",
112                tok2str(syslog_facility_values, "unknown (%u)", facility),
113                tok2str(syslog_severity_values, "unknown (%u)", severity),
114                len);
115         return;
116     }
117 
118     ND_PRINT("SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
119            len,
120            tok2str(syslog_facility_values, "unknown (%u)", facility),
121            facility,
122            tok2str(syslog_severity_values, "unknown (%u)", severity),
123            severity);
124 
125     /* print the syslog text in verbose mode */
126     /*
127      * RFC 3164 Section 4.1.3: "There is no ending delimiter to this part.
128      * The MSG part of the syslog packet MUST contain visible (printing)
129      * characters."
130      *
131      * RFC 5424 Section 8.2: "This document does not impose any mandatory
132      * restrictions on the MSG or PARAM-VALUE content.  As such, they MAY
133      * contain control characters, including the NUL character."
134      *
135      * Hence, to aid in protocol debugging, print the full MSG without
136      * beautification to make it clear what was transmitted on the wire.
137      */
138     if (len > msg_off)
139         (void)nd_printn(ndo, pptr + msg_off, len - msg_off, NULL);
140 
141     if (ndo->ndo_vflag > 1)
142         print_unknown_data(ndo, pptr, "\n\t", len);
143     return;
144 
145 invalid:
146     nd_print_invalid(ndo);
147 }
148