xref: /freebsd/contrib/tcpdump/print-syslog.c (revision 56e53cb8ef000c3ef72337a4095987a932cdedef)
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 /* \summary: Syslog protocol printer */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include <netdissect-stdinc.h>
24 
25 #include "netdissect.h"
26 #include "extract.h"
27 
28 static const char tstr[] = "[|syslog]";
29 
30 /*
31  * tokenlists and #defines taken from Ethereal - Network traffic analyzer
32  * by Gerald Combs <gerald@ethereal.com>
33  */
34 
35 #define SYSLOG_SEVERITY_MASK 0x0007  /* 0000 0000 0000 0111 */
36 #define SYSLOG_FACILITY_MASK 0x03f8  /* 0000 0011 1111 1000 */
37 #define SYSLOG_MAX_DIGITS 3 /* The maximum number if priority digits to read in. */
38 
39 static const struct tok syslog_severity_values[] = {
40   { 0,      "emergency" },
41   { 1,      "alert" },
42   { 2,      "critical" },
43   { 3,      "error" },
44   { 4,      "warning" },
45   { 5,      "notice" },
46   { 6,      "info" },
47   { 7,      "debug" },
48   { 0, NULL },
49 };
50 
51 static const struct tok syslog_facility_values[] = {
52   { 0,     "kernel" },
53   { 1,     "user" },
54   { 2,     "mail" },
55   { 3,     "daemon" },
56   { 4,     "auth" },
57   { 5,     "syslog" },
58   { 6,     "lpr" },
59   { 7,     "news" },
60   { 8,     "uucp" },
61   { 9,     "cron" },
62   { 10,    "authpriv" },
63   { 11,    "ftp" },
64   { 12,    "ntp" },
65   { 13,    "security" },
66   { 14,    "console" },
67   { 15,    "cron" },
68   { 16,    "local0" },
69   { 17,    "local1" },
70   { 18,    "local2" },
71   { 19,    "local3" },
72   { 20,    "local4" },
73   { 21,    "local5" },
74   { 22,    "local6" },
75   { 23,    "local7" },
76   { 0, NULL },
77 };
78 
79 void
80 syslog_print(netdissect_options *ndo,
81              register const u_char *pptr, register u_int len)
82 {
83     uint16_t msg_off = 0;
84     uint16_t pri = 0;
85     uint16_t facility,severity;
86 
87     /* extract decimal figures that are
88      * encapsulated within < > tags
89      * based on this decimal figure extract the
90      * severity and facility values
91      */
92 
93     ND_TCHECK2(*pptr, 1);
94     if (*(pptr+msg_off) == '<') {
95         msg_off++;
96         ND_TCHECK2(*(pptr + msg_off), 1);
97         while ( *(pptr+msg_off) >= '0' &&
98                 *(pptr+msg_off) <= '9' &&
99                 msg_off <= SYSLOG_MAX_DIGITS) {
100             pri = pri * 10 + (*(pptr+msg_off) - '0');
101             msg_off++;
102             ND_TCHECK2(*(pptr + msg_off), 1);
103         }
104         if (*(pptr+msg_off) != '>') {
105             ND_PRINT((ndo, "%s", tstr));
106             return;
107         }
108         msg_off++;
109     } else {
110         ND_PRINT((ndo, "%s", tstr));
111         return;
112     }
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((ndo, "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((ndo, "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     for (; msg_off < len; msg_off++) {
135         ND_TCHECK2(*(pptr + msg_off), 1);
136         safeputchar(ndo, *(pptr + msg_off));
137     }
138 
139     if (ndo->ndo_vflag > 1)
140         print_unknown_data(ndo, pptr, "\n\t", len);
141 
142     return;
143 
144 trunc:
145     ND_PRINT((ndo, "%s", tstr));
146 }
147