change the signature that asn2wrs generates for functions to marm all parameters...
[obnox/wireshark/wip.git] / epan / dissectors / packet-syslog.c
1 /* packet-syslog.c
2  * Routines for syslog message dissection
3  *
4  * Copyright 2000, Gerald Combs <gerald[AT]wireshark.org>
5  *
6  * Support for passing SS7 MSUs (from the Cisco ITP Packet Logging
7  * facility) to the MTP3 dissector by Abhik Sarkar <sarkar.abhik[AT]gmail.com>
8  * with some rework by Jeff Morriss <jeff.morriss[AT]ulticom.com>
9  *
10  * $Id$
11  *
12  * Wireshark - Network traffic analyzer
13  * By Gerald Combs <gerald[AT]wireshark.org>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38
39 #include <string.h>
40 #include <glib.h>
41 #include <epan/packet.h>
42 #include <epan/strutil.h>
43
44 #define UDP_PORT_SYSLOG 514
45
46 #define PRIORITY_MASK 0x0007  /* 0000 0111 */
47 #define FACILITY_MASK 0x03f8  /* 1111 1000 */
48
49 /* The maximum number if priority digits to read in. */
50 #define MAX_DIGITS 3
51
52 #define LEVEL_EMERG     0
53 #define LEVEL_ALERT     1
54 #define LEVEL_CRIT      2       
55 #define LEVEL_ERR       3
56 #define LEVEL_WARNING   4
57 #define LEVEL_NOTICE    5
58 #define LEVEL_INFO      6
59 #define LEVEL_DEBUG     7
60 static const value_string short_lev[] = {
61   { LEVEL_EMERG,        "EMERG" },
62   { LEVEL_ALERT,        "ALERT" },
63   { LEVEL_CRIT,         "CRIT" },
64   { LEVEL_ERR,          "ERR" },
65   { LEVEL_WARNING,      "WARNING" },
66   { LEVEL_NOTICE,       "NOTICE" },
67   { LEVEL_INFO,         "INFO" },
68   { LEVEL_DEBUG,        "DEBUG" },
69   { 0, NULL }
70 };
71
72 #define FAC_KERN        0
73 #define FAC_USER        1
74 #define FAC_MAIL        2
75 #define FAC_DAEMON      3
76 #define FAC_AUTH        4
77 #define FAC_SYSLOG      5
78 #define FAC_LPR         6
79 #define FAC_NEWS        7
80 #define FAC_UUCP        8
81 #define FAC_CRON        9
82 #define FAC_AUTHPRIV    10
83 #define FAC_FTP         11
84 #define FAC_NTP         12
85 #define FAC_LOGAUDIT    13
86 #define FAC_LOGALERT    14
87 #define FAC_CRON_SOL    15
88 #define FAC_LOCAL0      16
89 #define FAC_LOCAL1      17
90 #define FAC_LOCAL2      18
91 #define FAC_LOCAL3      19
92 #define FAC_LOCAL4      20
93 #define FAC_LOCAL5      21
94 #define FAC_LOCAL6      22
95 #define FAC_LOCAL7      23
96 static const value_string short_fac[] = {
97   { FAC_KERN,           "KERN" },
98   { FAC_USER,           "USER" },
99   { FAC_MAIL,           "MAIL" },
100   { FAC_DAEMON,         "DAEMON" },
101   { FAC_AUTH,           "AUTH" },
102   { FAC_SYSLOG,         "SYSLOG" },
103   { FAC_LPR,            "LPR" },
104   { FAC_NEWS,           "NEWS" },
105   { FAC_UUCP,           "UUCP" },
106   { FAC_CRON,           "CRON" },       /* The BSDs, Linux, and others */
107   { FAC_AUTHPRIV,       "AUTHPRIV" },
108   { FAC_FTP,            "FTP" },
109   { FAC_NTP,            "NTP" },
110   { FAC_LOGAUDIT,       "LOGAUDIT" },
111   { FAC_LOGALERT,       "LOGALERT" },
112   { FAC_CRON_SOL,       "CRON" },       /* Solaris */
113   { FAC_LOCAL0,         "LOCAL0" },
114   { FAC_LOCAL1,         "LOCAL1" },
115   { FAC_LOCAL2,         "LOCAL2" },
116   { FAC_LOCAL3,         "LOCAL3" },
117   { FAC_LOCAL4,         "LOCAL4" },
118   { FAC_LOCAL5,         "LOCAL5" },
119   { FAC_LOCAL6,         "LOCAL6" },
120   { FAC_LOCAL7,         "LOCAL7" },
121   { 0, NULL }
122 };
123
124 static const value_string long_lev[] = {
125   { LEVEL_EMERG,        "EMERG - system is unusable" },
126   { LEVEL_ALERT,        "ALERT - action must be taken immediately" },
127   { LEVEL_CRIT,         "CRIT - critical conditions" },
128   { LEVEL_ERR,          "ERR - error conditions" },
129   { LEVEL_WARNING,      "WARNING - warning conditions" },
130   { LEVEL_NOTICE,       "NOTICE - normal but significant condition" },
131   { LEVEL_INFO,         "INFO - informational" },
132   { LEVEL_DEBUG,        "DEBUG - debug-level messages" },
133   { 0, NULL }
134 };
135
136 static const value_string long_fac[] = {
137   { FAC_KERN,           "KERN - kernel messages" },
138   { FAC_USER,           "USER - random user-level messages" },
139   { FAC_MAIL,           "MAIL - mail system" },
140   { FAC_DAEMON,         "DAEMON - system daemons" },
141   { FAC_AUTH,           "AUTH - security/authorization messages" },
142   { FAC_SYSLOG,         "SYSLOG - messages generated internally by syslogd" },
143   { FAC_LPR,            "LPR - line printer subsystem" },
144   { FAC_NEWS,           "NEWS - network news subsystem" },
145   { FAC_UUCP,           "UUCP - UUCP subsystem" },
146   { FAC_CRON,           "CRON - clock daemon (BSD, Linux)" },
147   { FAC_AUTHPRIV,       "AUTHPRIV - security/authorization messages (private)" },
148   { FAC_FTP,            "FTP - ftp daemon" },
149   { FAC_NTP,            "NTP - ntp subsystem" },
150   { FAC_LOGAUDIT,       "LOGAUDIT - log audit" },
151   { FAC_LOGALERT,       "LOGALERT - log alert" },
152   { FAC_CRON_SOL,       "CRON - clock daemon (Solaris)" },
153   { FAC_LOCAL0,         "LOCAL0 - reserved for local use" },
154   { FAC_LOCAL1,         "LOCAL1 - reserved for local use" },
155   { FAC_LOCAL2,         "LOCAL2 - reserved for local use" },
156   { FAC_LOCAL3,         "LOCAL3 - reserved for local use" },
157   { FAC_LOCAL4,         "LOCAL4 - reserved for local use" },
158   { FAC_LOCAL5,         "LOCAL5 - reserved for local use" },
159   { FAC_LOCAL6,         "LOCAL6 - reserved for local use" },
160   { FAC_LOCAL7,         "LOCAL7 - reserved for local use" },
161   { 0, NULL }
162 };
163
164 static gint proto_syslog = -1;
165 static gint hf_syslog_level = -1;
166 static gint hf_syslog_facility = -1;
167 static gint hf_syslog_msg = -1;
168 static gint hf_syslog_msu_present = -1;
169
170 static gint ett_syslog = -1;
171
172 static dissector_handle_t mtp_handle;
173
174 /*  The Cisco ITP's packet logging facility allows selected (SS7) MSUs to be 
175  *  to be encapsulated in syslog UDP datagrams and sent to a monitoring tool.
176  *  However, no actual tool to monitor/decode the MSUs is provided. The aim
177  *  of this routine is to extract the hex dump of the MSU from the syslog
178  *  packet so that it can be passed on to the mtp3 dissector for decoding.
179  */
180 static tvbuff_t *
181 mtp3_msu_present(gint fac, gint level, const char *msg_str)
182 {
183   size_t nbytes;
184   gchar **split_string, *msu_hex_dump;
185   tvbuff_t *mtp3_tvb = NULL;
186   guint8 *byte_array;
187
188   /*  In the sample capture I have, all MSUs are LOCAL0.DEBUG.
189    *  Try to optimize this routine for most syslog users by short-cutting
190    *  out here.
191    */
192   if (!(fac == FAC_LOCAL0 && level == LEVEL_DEBUG))
193     return NULL;
194
195   if (strstr(msg_str, "msu=") == NULL)
196     return NULL;
197
198   split_string = g_strsplit(msg_str, "msu=", 2);
199   msu_hex_dump = split_string[1];
200
201   if (msu_hex_dump && strlen(msu_hex_dump)) {
202     byte_array = convert_string_to_hex(msu_hex_dump, &nbytes);
203
204     mtp3_tvb = tvb_new_real_data(byte_array, nbytes, nbytes);
205     tvb_set_free_cb(mtp3_tvb, g_free);
206   }
207
208   g_strfreev(split_string);
209
210   return(mtp3_tvb);
211 }
212
213 /* The message format is defined in RFC 3164 */
214 static void
215 dissect_syslog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
216 {
217   gint pri = -1, lev = -1, fac = -1;
218   gint msg_off = 0, msg_len;
219   proto_item *ti;
220   proto_tree *syslog_tree;
221   const char *msg_str;
222   tvbuff_t *mtp3_tvb;
223
224   if (check_col(pinfo->cinfo, COL_PROTOCOL))
225     col_set_str(pinfo->cinfo, COL_PROTOCOL, "Syslog");
226   if (check_col(pinfo->cinfo, COL_INFO))
227     col_clear(pinfo->cinfo, COL_INFO);
228
229   if (tvb_get_guint8(tvb, msg_off) == '<') {
230     /* A facility and level follow. */
231     msg_off++;
232     pri = 0;
233     while (tvb_bytes_exist(tvb, msg_off, 1) &&
234            isdigit(tvb_get_guint8(tvb, msg_off)) && msg_off <= MAX_DIGITS) {
235       pri = pri * 10 + (tvb_get_guint8(tvb, msg_off) - '0');
236       msg_off++;
237     }
238     if (tvb_get_guint8(tvb, msg_off) == '>')
239       msg_off++;
240     fac = (pri & FACILITY_MASK) >> 3;
241     lev = pri & PRIORITY_MASK;
242   }
243
244   msg_len = tvb_ensure_length_remaining(tvb, msg_off);
245   msg_str = tvb_format_text(tvb, msg_off, msg_len);
246
247   mtp3_tvb = mtp3_msu_present(fac, lev, msg_str);
248
249   if (mtp3_tvb == NULL && check_col(pinfo->cinfo, COL_INFO)) {
250     if (pri >= 0) {
251       col_add_fstr(pinfo->cinfo, COL_INFO, "%s.%s: %s",
252         val_to_str(fac, short_fac, "UNKNOWN"),
253         val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
254     } else {
255       col_add_str(pinfo->cinfo, COL_INFO, msg_str);
256     }
257   }
258
259   if (tree) {
260     if (pri >= 0) {
261       ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
262         "Syslog message: %s.%s: %s",
263         val_to_str(fac, short_fac, "UNKNOWN"),
264         val_to_str(lev, short_lev, "UNKNOWN"), msg_str);
265     } else {
266       ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
267         "Syslog message: (unknown): %s", msg_str);
268     }
269     syslog_tree = proto_item_add_subtree(ti, ett_syslog);
270     if (pri >= 0) {
271       ti = proto_tree_add_uint(syslog_tree, hf_syslog_facility, tvb, 0,
272         msg_off, pri);
273       ti = proto_tree_add_uint(syslog_tree, hf_syslog_level, tvb, 0,
274         msg_off, pri);
275     }
276     proto_tree_add_item(syslog_tree, hf_syslog_msg, tvb, msg_off,
277       msg_len, FALSE);
278
279     if (mtp3_tvb) {
280       proto_item *mtp3_item;
281       mtp3_item = proto_tree_add_boolean(syslog_tree, hf_syslog_msu_present,
282                                          tvb, msg_off, msg_len, TRUE);
283       PROTO_ITEM_SET_GENERATED(mtp3_item);
284     }
285   }
286
287   /* Call MTP dissector if encapsulated MSU was found */
288   if (mtp3_tvb) {
289     call_dissector(mtp_handle, mtp3_tvb, pinfo, tree);
290   }
291
292   return;
293 }
294
295 /* Register the protocol with Wireshark */
296 void proto_register_syslog(void)
297 {
298
299   /* Setup list of header fields */
300   static hf_register_info hf[] = {
301     { &hf_syslog_facility,
302       { "Facility",           "syslog.facility",
303       FT_UINT8, BASE_DEC, VALS(long_fac), FACILITY_MASK,
304       "Message facility", HFILL }
305     },
306     { &hf_syslog_level,
307       { "Level",              "syslog.level",
308       FT_UINT8, BASE_DEC, VALS(long_lev), PRIORITY_MASK,
309       "Message level", HFILL }
310     },
311     { &hf_syslog_msg,
312       { "Message",            "syslog.msg",
313       FT_STRING, BASE_NONE, NULL, 0x0,
314       "Message Text", HFILL }
315     },
316     { &hf_syslog_msu_present,
317       { "SS7 MSU present",    "syslog.msu_present",
318       FT_BOOLEAN, BASE_NONE, NULL, 0x0,
319       "True if an SS7 MSU was detected in the syslog message",
320       HFILL }
321     }
322   };
323
324   /* Setup protocol subtree array */
325   static gint *ett[] = {
326     &ett_syslog,
327   };
328
329   /* Register the protocol name and description */
330   proto_syslog = proto_register_protocol("Syslog message", "Syslog", "syslog");
331
332   /* Required function calls to register the header fields and subtrees used */
333   proto_register_field_array(proto_syslog, hf, array_length(hf));
334   proto_register_subtree_array(ett, array_length(ett));
335 }
336
337 void
338 proto_reg_handoff_syslog(void)
339 {
340   dissector_handle_t syslog_handle;
341
342   syslog_handle = create_dissector_handle(dissect_syslog, proto_syslog);
343   dissector_add("udp.port", UDP_PORT_SYSLOG, syslog_handle);
344
345   /* Find the mtp3 dissector */
346   mtp_handle = find_dissector("mtp3");
347 }