xref: /dragonfly/contrib/tcpdump/print-syslog.c (revision 36a3d1d6)
1 /*
2  * Copyright (c) 1998-2004  Hannes Gredler <hannes@tcpdump.org>
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 #ifndef lint
18 static const char rcsid[] _U_ =
19     "@(#) $Header: /tcpdump/master/tcpdump/print-syslog.c,v 1.1 2004-10-29 11:42:53 hannes Exp $";
20 #endif
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <tcpdump-stdinc.h>
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 #include "interface.h"
32 #include "extract.h"
33 #include "addrtoname.h"
34 
35 /*
36  * tokenlists and #defines taken from Ethereal - Network traffic analyzer
37  * by Gerald Combs <gerald@ethereal.com>
38  */
39 
40 #define SYSLOG_SEVERITY_MASK 0x0007  /* 0000 0000 0000 0111 */
41 #define SYSLOG_FACILITY_MASK 0x03f8  /* 0000 0011 1111 1000 */
42 #define SYSLOG_MAX_DIGITS 3 /* The maximum number if priority digits to read in. */
43 
44 static const struct tok syslog_severity_values[] = {
45   { 0,      "emergency" },
46   { 1,      "alert" },
47   { 2,      "critical" },
48   { 3,      "error" },
49   { 4,      "warning" },
50   { 5,      "notice" },
51   { 6,      "info" },
52   { 7,      "debug" },
53   { 0, NULL },
54 };
55 
56 static const struct tok syslog_facility_values[] = {
57   { 0,     "kernel" },
58   { 1,     "user" },
59   { 2,     "mail" },
60   { 3,     "daemon" },
61   { 4,     "auth" },
62   { 5,     "syslog" },
63   { 6,     "lpr" },
64   { 7,     "news" },
65   { 8,     "uucp" },
66   { 9,     "cron" },
67   { 10,    "authpriv" },
68   { 11,    "ftp" },
69   { 12,    "ntp" },
70   { 13,    "security" },
71   { 14,    "console" },
72   { 15,    "cron" },
73   { 16,    "local0" },
74   { 17,    "local1" },
75   { 18,    "local2" },
76   { 19,    "local3" },
77   { 20,    "local4" },
78   { 21,    "local5" },
79   { 22,    "local6" },
80   { 23,    "local7" },
81   { 0, NULL },
82 };
83 
84 void
85 syslog_print(register const u_char *pptr, register u_int len)
86 {
87     u_int16_t msg_off = 0;
88     u_int16_t pri = 0;
89     u_int16_t facility,severity;
90 
91     /* extract decimal figures that are
92      * encapsulated within < > tags
93      * based on this decimal figure extract the
94      * severity and facility values
95      */
96 
97     if (!TTEST2(*pptr, 1))
98         goto trunc;
99 
100     if (*(pptr+msg_off) == '<') {
101         msg_off++;
102 
103         if (!TTEST2(*(pptr+msg_off), 1))
104             goto trunc;
105 
106         while ( *(pptr+msg_off) >= '0' &&
107                 *(pptr+msg_off) <= '9' &&
108                 msg_off <= SYSLOG_MAX_DIGITS) {
109 
110             if (!TTEST2(*(pptr+msg_off), 1))
111                 goto trunc;
112 
113             pri = pri * 10 + (*(pptr+msg_off) - '0');
114             msg_off++;
115 
116             if (!TTEST2(*(pptr+msg_off), 1))
117                 goto trunc;
118 
119         if (*(pptr+msg_off) == '>')
120             msg_off++;
121         }
122     } else {
123         printf("[|syslog]");
124         return;
125     }
126 
127     facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
128     severity = pri & SYSLOG_SEVERITY_MASK;
129 
130 
131     if (vflag < 1 )
132     {
133         printf("SYSLOG %s.%s, length: %u",
134                tok2str(syslog_facility_values, "unknown (%u)", facility),
135                tok2str(syslog_severity_values, "unknown (%u)", severity),
136                len);
137         return;
138     }
139 
140     printf("SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
141            len,
142            tok2str(syslog_facility_values, "unknown (%u)", facility),
143            facility,
144            tok2str(syslog_severity_values, "unknown (%u)", severity),
145            severity);
146 
147     /* print the syslog text in verbose mode */
148     for (; msg_off < len; msg_off++) {
149         if (!TTEST2(*(pptr+msg_off), 1))
150             goto trunc;
151         safeputchar(*(pptr+msg_off));
152     }
153 
154     if (vflag > 1) {
155         if(!print_unknown_data(pptr,"\n\t",len))
156             return;
157     }
158 
159     return;
160 
161 trunc:
162         printf("[|syslog]");
163 }
164