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