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