Update manuf to current IEEE entries.
[obnox/wireshark/wip.git] / packet-syslog.c
1 /* packet-syslog.c
2  * Routines for syslog message dissection
3  *
4  * Copyright 2000, Gerald Combs <gerald@ethereal.com>
5  *
6  * $Id: packet-syslog.c,v 1.19 2002/08/28 21:00:35 jmayer Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #include <string.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38
39 #define UDP_PORT_SYSLOG 514
40
41 #define PRIORITY_MASK 0x0007  /* 0000 0111 */
42 #define FACILITY_MASK 0x03f8  /* 1111 1000 */
43
44 /* The maximum number if priority digits to read in. */
45 #define MAX_DIGITS 3
46
47 #define COL_INFO_LEN 32
48 #define ELLIPSIS "..." /* ISO 8859-1 doesn't appear to have a real ellipsis. */
49
50 static const value_string short_lev[] = {
51   { 0,      "EMERG" },
52   { 1,      "ALERT" },
53   { 2,      "CRIT" },
54   { 3,      "ERR" },
55   { 4,      "WARNING" },
56   { 5,      "NOTICE" },
57   { 6,      "INFO" },
58   { 7,      "DEBUG" },
59   { 0, NULL },
60 };
61
62 static const value_string short_fac[] = {
63   { 0,     "KERN" },
64   { 1,     "USER" },
65   { 2,     "MAIL" },
66   { 3,     "DAEMON" },
67   { 4,     "AUTH" },
68   { 5,     "SYSLOG" },
69   { 6,     "LPR" },
70   { 7,     "NEWS" },
71   { 8,     "UUCP" },
72   { 9,     "CRON" },            /* The BSDs, Linux, and others */
73   { 10,    "AUTHPRIV" },
74   { 11,    "FTP" },
75   { 15,    "CRON" },            /* Solaris */
76   { 16,    "LOCAL0" },
77   { 17,    "LOCAL1" },
78   { 18,    "LOCAL2" },
79   { 19,    "LOCAL3" },
80   { 20,    "LOCAL4" },
81   { 21,    "LOCAL5" },
82   { 22,    "LOCAL6" },
83   { 23,    "LOCAL7" },
84   { 0, NULL },
85 };
86
87 static const value_string long_lev[] = {
88   { 0,      "EMERG - system is unusable" },
89   { 1,      "ALERT - action must be taken immediately" },
90   { 2,      "CRIT - critical conditions" },
91   { 3,      "ERR - error conditions" },
92   { 4,      "WARNING - warning conditions" },
93   { 5,      "NOTICE - normal but significant condition" },
94   { 6,      "INFO - informational" },
95   { 7,      "DEBUG - debug-level messages" },
96   { 0, NULL },
97 };
98
99 static const value_string long_fac[] = {
100   { 0,     "KERN - kernel messages" },
101   { 1,     "USER - random user-level messages" },
102   { 2,     "MAIL - mail system" },
103   { 3,     "DAEMON - system daemons" },
104   { 4,     "AUTH - security/authorization messages" },
105   { 5,     "SYSLOG - messages generated internally by syslogd" },
106   { 6,     "LPR - line printer subsystem" },
107   { 7,     "NEWS - network news subsystem" },
108   { 8,     "UUCP - UUCP subsystem" },
109   { 9,     "CRON - clock daemon (BSD, Linux)" },
110   { 10,    "AUTHPRIV - security/authorization messages (private)" },
111   { 11,    "FTP - ftp daemon" },
112   { 15,    "CRON - clock daemon (Solaris)" },
113   { 16,    "LOCAL0 - reserved for local use" },
114   { 17,    "LOCAL1 - reserved for local use" },
115   { 18,    "LOCAL2 - reserved for local use" },
116   { 19,    "LOCAL3 - reserved for local use" },
117   { 20,    "LOCAL4 - reserved for local use" },
118   { 21,    "LOCAL5 - reserved for local use" },
119   { 22,    "LOCAL6 - reserved for local use" },
120   { 23,    "LOCAL7 - reserved for local use" },
121   { 0, NULL },
122 };
123
124 static gint proto_syslog = -1;
125 static gint hf_syslog_level = -1;
126 static gint hf_syslog_facility = -1;
127 static gint hf_syslog_msg = -1;
128
129 static gint ett_syslog = -1;
130
131 /* I couldn't find any documentation for the syslog message format.
132    According to the BSD sources, the message format is '<', P, '>', and
133    T.  P is a decimal value, which should be treated as an 8 bit
134    unsigned integer.  The lower three bits comprise the level, and the
135    upper five bits are the facility.  T is the message text.
136  */
137
138 static void dissect_syslog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
139 {
140   gint pri = -1, lev = -1, fac = -1;
141   gint msg_off = 0, msg_len;
142   gint ellipsis_len = (COL_INFO_LEN - strlen(ELLIPSIS)) - 1;
143   proto_item *ti;
144   proto_tree *syslog_tree;
145   gchar msg_str[COL_INFO_LEN];
146
147   if (check_col(pinfo->cinfo, COL_PROTOCOL))
148     col_set_str(pinfo->cinfo, COL_PROTOCOL, "Syslog");
149   if (check_col(pinfo->cinfo, COL_INFO))
150     col_clear(pinfo->cinfo, COL_INFO);
151
152   if (tvb_get_guint8(tvb, msg_off) == '<') {
153     /* A facility and level follow. */
154     msg_off++;
155     pri = 0;
156     while (tvb_bytes_exist(tvb, msg_off, 1) &&
157            isdigit(tvb_get_guint8(tvb, msg_off)) && msg_off <= MAX_DIGITS) {
158       pri = pri * 10 + (tvb_get_guint8(tvb, msg_off) - '0');
159       msg_off++;
160     }
161     if (tvb_get_guint8(tvb, msg_off) == '>')
162       msg_off++;
163     fac = (pri & FACILITY_MASK) >> 3;
164     lev = pri & PRIORITY_MASK;
165   }
166
167   /* Copy the message into a string buffer, with a trailing ellipsis if needed. */
168   msg_len = tvb_ensure_length_remaining(tvb, msg_off);
169   if (msg_len >= COL_INFO_LEN) {
170     tvb_memcpy(tvb, msg_str, msg_off, ellipsis_len);
171     strcpy (msg_str + ellipsis_len, ELLIPSIS);
172   } else {
173     tvb_memcpy(tvb, msg_str, msg_off, msg_len);
174     msg_str[msg_len] = '\0';
175   }
176
177   if (check_col(pinfo->cinfo, COL_INFO)) {
178     if (pri >= 0) {
179       col_add_fstr(pinfo->cinfo, COL_INFO, "%s.%s: %s",
180         val_to_str(fac, short_fac, "UNKNOWN"),
181         val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
182     } else {
183       col_add_fstr(pinfo->cinfo, COL_INFO, "%s", msg_str);
184     }
185   }
186
187   if (tree) {
188     if (pri >= 0) {
189       ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
190         "Syslog message: %s.%s: %s",
191         val_to_str(fac, short_fac, "UNKNOWN"),
192         val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
193     } else {
194       ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
195         "Syslog message: (unknown): %s", msg_str);
196     }
197     syslog_tree = proto_item_add_subtree(ti, ett_syslog);
198     if (pri >= 0) {
199       ti = proto_tree_add_uint(syslog_tree, hf_syslog_facility, tvb, 0,
200         msg_off, pri);
201       ti = proto_tree_add_uint(syslog_tree, hf_syslog_level, tvb, 0,
202         msg_off, pri);
203     }
204     proto_tree_add_item(syslog_tree, hf_syslog_msg, tvb, msg_off,
205       msg_len, FALSE);
206   }
207   return;
208 }
209
210 /* Register the protocol with Ethereal */
211 void proto_register_syslog(void)
212 {
213
214   /* Setup list of header fields */
215   static hf_register_info hf[] = {
216     { &hf_syslog_facility,
217       { "Facility",           "syslog.facility",
218       FT_UINT8, BASE_DEC, VALS(long_fac), FACILITY_MASK,
219       "Message facility", HFILL }
220     },
221     { &hf_syslog_level,
222       { "Level",              "syslog.level",
223       FT_UINT8, BASE_DEC, VALS(long_lev), PRIORITY_MASK,
224       "Message level", HFILL }
225     },
226     { &hf_syslog_msg,
227       { "Message",            "syslog.msg",
228       FT_STRING, BASE_NONE, NULL, 0x0,
229       "Message Text", HFILL }
230     },
231   };
232
233   /* Setup protocol subtree array */
234   static gint *ett[] = {
235     &ett_syslog,
236   };
237
238   /* Register the protocol name and description */
239   proto_syslog = proto_register_protocol("Syslog message", "Syslog", "syslog");
240
241   /* Required function calls to register the header fields and subtrees used */
242   proto_register_field_array(proto_syslog, hf, array_length(hf));
243   proto_register_subtree_array(ett, array_length(ett));
244 }
245
246 void
247 proto_reg_handoff_syslog(void)
248 {
249   dissector_handle_t syslog_handle;
250
251   syslog_handle = create_dissector_handle(dissect_syslog, proto_syslog);
252   dissector_add("udp.port", UDP_PORT_SYSLOG, syslog_handle);
253 }