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