xref: /netbsd-src/external/bsd/tcpdump/dist/print-syslog.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
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 #include <sys/cdefs.h>
18 #ifndef lint
19 __RCSID("$NetBSD: print-syslog.c,v 1.10 2024/09/02 16:15:33 christos Exp $");
20 #endif
21 
22 /* \summary: Syslog protocol printer */
23 /* specification: RFC 3164 (not RFC 5424) */
24 
25 #include <config.h>
26 
27 #include "netdissect-stdinc.h"
28 
29 #include "netdissect.h"
30 #include "extract.h"
31 
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 of 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              const u_char *pptr, u_int len)
85 {
86     uint16_t msg_off = 0;
87     uint16_t pri = 0;
88     uint16_t facility,severity;
89 
90     ndo->ndo_protocol = "syslog";
91     /* extract decimal figures that are
92      * encapsulated within < > tags
93      * based on this decimal figure extract the
94      * severity and facility values
95      */
96 
97     if (GET_U_1(pptr) != '<')
98         goto invalid;
99     msg_off++;
100 
101     while (msg_off <= SYSLOG_MAX_DIGITS &&
102            GET_U_1(pptr + msg_off) >= '0' &&
103            GET_U_1(pptr + msg_off) <= '9') {
104         pri = pri * 10 + (GET_U_1(pptr + msg_off) - '0');
105         msg_off++;
106     }
107 
108     if (GET_U_1(pptr + msg_off) != '>')
109         goto invalid;
110     msg_off++;
111 
112     facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
113     severity = pri & SYSLOG_SEVERITY_MASK;
114 
115     if (ndo->ndo_vflag < 1 ) {
116         ND_PRINT("SYSLOG %s.%s, length: %u",
117                tok2str(syslog_facility_values, "unknown (%u)", facility),
118                tok2str(syslog_severity_values, "unknown (%u)", severity),
119                len);
120         return;
121     }
122 
123     ND_PRINT("SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
124            len,
125            tok2str(syslog_facility_values, "unknown (%u)", facility),
126            facility,
127            tok2str(syslog_severity_values, "unknown (%u)", severity),
128            severity);
129 
130     /* print the syslog text in verbose mode */
131     /*
132      * RFC 3164 Section 4.1.3: "There is no ending delimiter to this part.
133      * The MSG part of the syslog packet MUST contain visible (printing)
134      * characters."
135      *
136      * RFC 5424 Section 8.2: "This document does not impose any mandatory
137      * restrictions on the MSG or PARAM-VALUE content.  As such, they MAY
138      * contain control characters, including the NUL character."
139      *
140      * Hence, to aid in protocol debugging, print the full MSG without
141      * beautification to make it clear what was transmitted on the wire.
142      */
143     if (len > msg_off)
144         (void)nd_printn(ndo, pptr + msg_off, len - msg_off, NULL);
145 
146     if (ndo->ndo_vflag > 1)
147         print_unknown_data(ndo, pptr, "\n\t", len);
148     return;
149 
150 invalid:
151     nd_print_invalid(ndo);
152 }
153