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