xref: /dflybsd-src/contrib/tcpdump/print-syslog.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
2411677aeSAaron LI  * Copyright (c) 1998-2004  Hannes Gredler <hannes@gredler.at>
341c99275SPeter Avalos  *      The TCPDUMP project
441c99275SPeter Avalos  *
541c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos  * modification, are permitted provided that: (1) source code
741c99275SPeter Avalos  * distributions retain the above copyright notice and this paragraph
841c99275SPeter Avalos  * in its entirety, and (2) distributions including binary code include
941c99275SPeter Avalos  * the above copyright notice and this paragraph in its entirety in
1041c99275SPeter Avalos  * the documentation or other materials provided with the distribution.
1141c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
1241c99275SPeter Avalos  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
1341c99275SPeter Avalos  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1441c99275SPeter Avalos  * FOR A PARTICULAR PURPOSE.
1541c99275SPeter Avalos  */
1641c99275SPeter Avalos 
17411677aeSAaron LI /* \summary: Syslog protocol printer */
18*ed775ee7SAntonio Huete Jimenez /* specification: RFC 3164 (not RFC 5424) */
1941c99275SPeter Avalos 
2041c99275SPeter Avalos #ifdef HAVE_CONFIG_H
21*ed775ee7SAntonio Huete Jimenez #include <config.h>
2241c99275SPeter Avalos #endif
2341c99275SPeter Avalos 
24*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
2541c99275SPeter Avalos 
26411677aeSAaron LI #include "netdissect.h"
2741c99275SPeter Avalos #include "extract.h"
28411677aeSAaron LI 
2941c99275SPeter Avalos 
3041c99275SPeter Avalos /*
3141c99275SPeter Avalos  * tokenlists and #defines taken from Ethereal - Network traffic analyzer
3241c99275SPeter Avalos  * by Gerald Combs <gerald@ethereal.com>
3341c99275SPeter Avalos  */
3441c99275SPeter Avalos 
3541c99275SPeter Avalos #define SYSLOG_SEVERITY_MASK 0x0007  /* 0000 0000 0000 0111 */
3641c99275SPeter Avalos #define SYSLOG_FACILITY_MASK 0x03f8  /* 0000 0011 1111 1000 */
37*ed775ee7SAntonio Huete Jimenez #define SYSLOG_MAX_DIGITS 3 /* The maximum number of priority digits to read in. */
3841c99275SPeter Avalos 
3941c99275SPeter Avalos static const struct tok syslog_severity_values[] = {
4041c99275SPeter Avalos   { 0,      "emergency" },
4141c99275SPeter Avalos   { 1,      "alert" },
4241c99275SPeter Avalos   { 2,      "critical" },
4341c99275SPeter Avalos   { 3,      "error" },
4441c99275SPeter Avalos   { 4,      "warning" },
4541c99275SPeter Avalos   { 5,      "notice" },
4641c99275SPeter Avalos   { 6,      "info" },
4741c99275SPeter Avalos   { 7,      "debug" },
4841c99275SPeter Avalos   { 0, NULL },
4941c99275SPeter Avalos };
5041c99275SPeter Avalos 
5141c99275SPeter Avalos static const struct tok syslog_facility_values[] = {
5241c99275SPeter Avalos   { 0,     "kernel" },
5341c99275SPeter Avalos   { 1,     "user" },
5441c99275SPeter Avalos   { 2,     "mail" },
5541c99275SPeter Avalos   { 3,     "daemon" },
5641c99275SPeter Avalos   { 4,     "auth" },
5741c99275SPeter Avalos   { 5,     "syslog" },
5841c99275SPeter Avalos   { 6,     "lpr" },
5941c99275SPeter Avalos   { 7,     "news" },
6041c99275SPeter Avalos   { 8,     "uucp" },
6141c99275SPeter Avalos   { 9,     "cron" },
6241c99275SPeter Avalos   { 10,    "authpriv" },
6341c99275SPeter Avalos   { 11,    "ftp" },
6441c99275SPeter Avalos   { 12,    "ntp" },
6541c99275SPeter Avalos   { 13,    "security" },
6641c99275SPeter Avalos   { 14,    "console" },
6741c99275SPeter Avalos   { 15,    "cron" },
6841c99275SPeter Avalos   { 16,    "local0" },
6941c99275SPeter Avalos   { 17,    "local1" },
7041c99275SPeter Avalos   { 18,    "local2" },
7141c99275SPeter Avalos   { 19,    "local3" },
7241c99275SPeter Avalos   { 20,    "local4" },
7341c99275SPeter Avalos   { 21,    "local5" },
7441c99275SPeter Avalos   { 22,    "local6" },
7541c99275SPeter Avalos   { 23,    "local7" },
7641c99275SPeter Avalos   { 0, NULL },
7741c99275SPeter Avalos };
7841c99275SPeter Avalos 
7941c99275SPeter Avalos void
syslog_print(netdissect_options * ndo,const u_char * pptr,u_int len)80411677aeSAaron LI syslog_print(netdissect_options *ndo,
81*ed775ee7SAntonio Huete Jimenez              const u_char *pptr, u_int len)
8241c99275SPeter Avalos {
83411677aeSAaron LI     uint16_t msg_off = 0;
84411677aeSAaron LI     uint16_t pri = 0;
85411677aeSAaron LI     uint16_t facility,severity;
8641c99275SPeter Avalos 
87*ed775ee7SAntonio Huete Jimenez     ndo->ndo_protocol = "syslog";
8841c99275SPeter Avalos     /* extract decimal figures that are
8941c99275SPeter Avalos      * encapsulated within < > tags
9041c99275SPeter Avalos      * based on this decimal figure extract the
9141c99275SPeter Avalos      * severity and facility values
9241c99275SPeter Avalos      */
9341c99275SPeter Avalos 
94*ed775ee7SAntonio Huete Jimenez     if (GET_U_1(pptr) != '<')
95*ed775ee7SAntonio Huete Jimenez         goto invalid;
9641c99275SPeter Avalos     msg_off++;
97*ed775ee7SAntonio Huete Jimenez 
98*ed775ee7SAntonio Huete Jimenez     while (msg_off <= SYSLOG_MAX_DIGITS &&
99*ed775ee7SAntonio Huete Jimenez            GET_U_1(pptr + msg_off) >= '0' &&
100*ed775ee7SAntonio Huete Jimenez            GET_U_1(pptr + msg_off) <= '9') {
101*ed775ee7SAntonio Huete Jimenez         pri = pri * 10 + (GET_U_1(pptr + msg_off) - '0');
10241c99275SPeter Avalos         msg_off++;
10341c99275SPeter Avalos     }
104*ed775ee7SAntonio Huete Jimenez 
105*ed775ee7SAntonio Huete Jimenez     if (GET_U_1(pptr + msg_off) != '>')
106*ed775ee7SAntonio Huete Jimenez         goto invalid;
107411677aeSAaron LI     msg_off++;
10841c99275SPeter Avalos 
10941c99275SPeter Avalos     facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
11041c99275SPeter Avalos     severity = pri & SYSLOG_SEVERITY_MASK;
11141c99275SPeter Avalos 
112411677aeSAaron LI     if (ndo->ndo_vflag < 1 )
11341c99275SPeter Avalos     {
114*ed775ee7SAntonio Huete Jimenez         ND_PRINT("SYSLOG %s.%s, length: %u",
11541c99275SPeter Avalos                tok2str(syslog_facility_values, "unknown (%u)", facility),
11641c99275SPeter Avalos                tok2str(syslog_severity_values, "unknown (%u)", severity),
117*ed775ee7SAntonio Huete Jimenez                len);
11841c99275SPeter Avalos         return;
11941c99275SPeter Avalos     }
12041c99275SPeter Avalos 
121*ed775ee7SAntonio Huete Jimenez     ND_PRINT("SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
12241c99275SPeter Avalos            len,
12341c99275SPeter Avalos            tok2str(syslog_facility_values, "unknown (%u)", facility),
12441c99275SPeter Avalos            facility,
12541c99275SPeter Avalos            tok2str(syslog_severity_values, "unknown (%u)", severity),
126*ed775ee7SAntonio Huete Jimenez            severity);
12741c99275SPeter Avalos 
12841c99275SPeter Avalos     /* print the syslog text in verbose mode */
129*ed775ee7SAntonio Huete Jimenez     /*
130*ed775ee7SAntonio Huete Jimenez      * RFC 3164 Section 4.1.3: "There is no ending delimiter to this part.
131*ed775ee7SAntonio Huete Jimenez      * The MSG part of the syslog packet MUST contain visible (printing)
132*ed775ee7SAntonio Huete Jimenez      * characters."
133*ed775ee7SAntonio Huete Jimenez      *
134*ed775ee7SAntonio Huete Jimenez      * RFC 5424 Section 8.2: "This document does not impose any mandatory
135*ed775ee7SAntonio Huete Jimenez      * restrictions on the MSG or PARAM-VALUE content.  As such, they MAY
136*ed775ee7SAntonio Huete Jimenez      * contain control characters, including the NUL character."
137*ed775ee7SAntonio Huete Jimenez      *
138*ed775ee7SAntonio Huete Jimenez      * Hence, to aid in protocol debugging, print the full MSG without
139*ed775ee7SAntonio Huete Jimenez      * beautification to make it clear what was transmitted on the wire.
140*ed775ee7SAntonio Huete Jimenez      */
141*ed775ee7SAntonio Huete Jimenez     if (len > msg_off)
142*ed775ee7SAntonio Huete Jimenez         (void)nd_printn(ndo, pptr + msg_off, len - msg_off, NULL);
14341c99275SPeter Avalos 
144411677aeSAaron LI     if (ndo->ndo_vflag > 1)
145411677aeSAaron LI         print_unknown_data(ndo, pptr, "\n\t", len);
14641c99275SPeter Avalos     return;
14741c99275SPeter Avalos 
148*ed775ee7SAntonio Huete Jimenez invalid:
149*ed775ee7SAntonio Huete Jimenez     nd_print_invalid(ndo);
15041c99275SPeter Avalos }
151