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