Reduce the CinemaScope-like proportions of the preferences dialog by
[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.11 2001/06/18 02:17:53 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   if (check_col(pinfo->fd, COL_PROTOCOL))
149     col_set_str(pinfo->fd, COL_PROTOCOL, "Syslog");
150   if (check_col(pinfo->fd, COL_INFO))
151     col_clear(pinfo->fd, COL_INFO);
152     
153   msg_len = tvb_length(tvb);
154   
155   if (tvb_get_guint8(tvb, 0) == '<' && isdigit(tvb_get_guint8(tvb, 1))) {
156     msg_off++;
157     pri = 0;
158     while (isdigit(tvb_get_guint8(tvb, msg_off)) && msg_off <= MAX_DIGITS &&
159            tvb_length_remaining(tvb, msg_off)) {
160       pri = pri * 10 + (tvb_get_guint8(tvb, msg_off) - '0');
161       msg_off++;
162     }
163     if (tvb_get_guint8(tvb, msg_off) == '>')
164       msg_off++;
165     fac = (pri & FACILITY_MASK) >> 3;
166     lev = pri & PRIORITY_MASK;
167   }
168
169   /* Copy the message into a string buffer, with a trailing ellipsis if needed. */
170   msg_len = tvb_length_remaining(tvb, msg_off);
171   if (msg_len >= COL_INFO_LEN) {
172     tvb_memcpy(tvb, msg_str, msg_off, ellipsis_len);
173     strcpy (msg_str + ellipsis_len, ELLIPSIS);
174   } else {
175     tvb_memcpy(tvb, msg_str, msg_off, msg_len);
176     msg_str[msg_len] = '\0';
177   }
178     
179   if (check_col(pinfo->fd, COL_INFO)) {
180     if (pri >= 0) {
181       col_add_fstr(pinfo->fd, COL_INFO, "%s.%s: %s", 
182         val_to_str(fac, short_fac, "UNKNOWN"),
183         val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
184     } else {
185       col_add_fstr(pinfo->fd, COL_INFO, "%s", msg_str);
186     }
187   }
188   
189   if (tree) {
190     if (pri >= 0) {
191       ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0,
192         tvb_length(tvb), "Syslog message: %s.%s: %s",
193         val_to_str(fac, short_fac, "UNKNOWN"),
194         val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
195     } else {
196       ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0,
197         tvb_length(tvb), "Syslog message: (unknown): %s", msg_str);
198     }
199     syslog_tree = proto_item_add_subtree(ti, ett_syslog);
200     if (pri >= 0) {
201       ti = proto_tree_add_uint(syslog_tree, hf_syslog_facility, tvb, 0,
202         msg_off, pri);
203       ti = proto_tree_add_uint(syslog_tree, hf_syslog_level, tvb, 0,
204         msg_off, pri);
205     }
206     proto_tree_add_uint_format(syslog_tree, hf_syslog_msg_len, tvb, msg_off,
207       msg_len, msg_len, "Message (%d byte%s)", msg_len, plurality(msg_len, "", "s"));
208   }
209   return;
210 }
211  
212 /* Register the protocol with Ethereal */
213 void proto_register_syslog(void)
214 {                 
215
216   /* Setup list of header fields */
217   static hf_register_info hf[] = {
218     { &hf_syslog_facility,
219       { "Facility",           "syslog.facility",
220       FT_UINT8, BASE_DEC, VALS(long_fac), FACILITY_MASK,
221       "Message facility", HFILL }
222     },
223     { &hf_syslog_level,
224       { "Level",              "syslog.level",
225       FT_UINT8, BASE_DEC, VALS(long_lev), PRIORITY_MASK,
226       "Message level", HFILL }
227     },
228     { &hf_syslog_msg_len,
229       { "Message length",     "syslog.msg_len",
230       FT_UINT32, BASE_DEC, NULL, 0x0,
231       "Message length, excluding priority descriptor", HFILL }
232     },
233   };
234
235   /* Setup protocol subtree array */
236   static gint *ett[] = {
237     &ett_syslog,
238   };
239
240   /* Register the protocol name and description */
241   proto_syslog = proto_register_protocol("Syslog message", "Syslog", "syslog");
242
243   /* Required function calls to register the header fields and subtrees used */
244   proto_register_field_array(proto_syslog, hf, array_length(hf));
245   proto_register_subtree_array(ett, array_length(ett));
246 };
247
248 void
249 proto_reg_handoff_syslog(void)
250 {
251   dissector_add("udp.port", UDP_PORT_SYSLOG, dissect_syslog, proto_syslog);
252 }