xref: /netbsd-src/external/bsd/tcpdump/dist/print-syslog.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
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.8 2017/09/08 14:01:13 christos Exp $");
20 #endif
21 
22 /* \summary: Syslog protocol printer */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <netdissect-stdinc.h>
29 
30 #include "netdissect.h"
31 #include "extract.h"
32 
33 static const char tstr[] = "[|syslog]";
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 if 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              register const u_char *pptr, register u_int len)
87 {
88     uint16_t msg_off = 0;
89     uint16_t pri = 0;
90     uint16_t facility,severity;
91 
92     /* extract decimal figures that are
93      * encapsulated within < > tags
94      * based on this decimal figure extract the
95      * severity and facility values
96      */
97 
98     ND_TCHECK2(*pptr, 1);
99     if (*(pptr+msg_off) == '<') {
100         msg_off++;
101         ND_TCHECK2(*(pptr + msg_off), 1);
102         while ( *(pptr+msg_off) >= '0' &&
103                 *(pptr+msg_off) <= '9' &&
104                 msg_off <= SYSLOG_MAX_DIGITS) {
105             pri = pri * 10 + (*(pptr+msg_off) - '0');
106             msg_off++;
107             ND_TCHECK2(*(pptr + msg_off), 1);
108         }
109         if (*(pptr+msg_off) != '>') {
110             ND_PRINT((ndo, "%s", tstr));
111             return;
112         }
113         msg_off++;
114     } else {
115         ND_PRINT((ndo, "%s", tstr));
116         return;
117     }
118 
119     facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
120     severity = pri & SYSLOG_SEVERITY_MASK;
121 
122     if (ndo->ndo_vflag < 1 )
123     {
124         ND_PRINT((ndo, "SYSLOG %s.%s, length: %u",
125                tok2str(syslog_facility_values, "unknown (%u)", facility),
126                tok2str(syslog_severity_values, "unknown (%u)", severity),
127                len));
128         return;
129     }
130 
131     ND_PRINT((ndo, "SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
132            len,
133            tok2str(syslog_facility_values, "unknown (%u)", facility),
134            facility,
135            tok2str(syslog_severity_values, "unknown (%u)", severity),
136            severity));
137 
138     /* print the syslog text in verbose mode */
139     for (; msg_off < len; msg_off++) {
140         ND_TCHECK2(*(pptr + msg_off), 1);
141         safeputchar(ndo, *(pptr + msg_off));
142     }
143 
144     if (ndo->ndo_vflag > 1)
145         print_unknown_data(ndo, pptr, "\n\t", len);
146 
147     return;
148 
149 trunc:
150     ND_PRINT((ndo, "%s", tstr));
151 }
152