aboutsummaryrefslogtreecommitdiffstats
path: root/ui/dissect_opts.c
blob: 4369bd2683ebdc079831f5f395b9f9f2ed32ba0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* dissect_opts.c
 * Routines for dissection options setting
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include <glib.h>

#include <epan/prefs.h>
#include <epan/timestamp.h>
#include <epan/addr_resolv.h>
#include <epan/disabled_protos.h>

#include "ui/decode_as_utils.h"

#if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
#include <epan/dissectors/read_keytab_file.h>
#endif

#include <wsutil/clopts_common.h>
#include <wsutil/strtoi.h>
#include <wsutil/cmdarg_err.h>
#include <wsutil/file_util.h>
#include <wsutil/ws_assert.h>

#include "ui/dissect_opts.h"

dissect_options global_dissect_options = {
    .time_format = TS_NOT_SET,
    .time_precision = TS_PREC_NOT_SET
};

gboolean
dissect_opts_handle_opt(int opt, char *optarg_str_p)
{
    char badopt;
    char *dotp;
    ts_precision tsp;

    switch(opt) {
    case 'd':        /* Decode as rule */
        if (!decode_as_command_option(optarg_str_p))
             return FALSE;
        break;
    case 'K':        /* Kerberos keytab file */
#if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
        read_keytab_file(optarg_str_p);
#else
        cmdarg_err("-K specified, but Kerberos keytab file support isn't present");
        return FALSE;
#endif
        break;
    case 'n':        /* No name resolution */
        disable_name_resolution();
        break;
    case 'N':        /* Select what types of addresses/port #s to resolve */
        badopt = string_to_name_resolve(optarg_str_p, &gbl_resolv_flags);
        if (badopt != '\0') {
            cmdarg_err("-N specifies unknown resolving option '%c'; valid options are:",
                       badopt);
            cmdarg_err_cont("\t'd' to enable address resolution from captured DNS packets\n"
                            "\t'g' to enable address geolocation information from MaxMind databases\n"
                            "\t'm' to enable MAC address resolution\n"
                            "\t'n' to enable network address resolution\n"
                            "\t'N' to enable using external resolvers (e.g., DNS)\n"
                            "\t    for network address resolution\n"
                            "\t't' to enable transport-layer port number resolution\n"
                            "\t'v' to enable VLAN IDs to names resolution");
            return FALSE;
        }
        break;
    case 't':        /* Time stamp type */
        tsp = TS_PREC_NOT_SET;
        dotp = strchr(optarg_str_p, '.');
        if (dotp != NULL) {
            if (strcmp(dotp + 1, "") == 0) {
                /* Nothing specified; use appropriate precision for the file. */
                tsp = TS_PREC_AUTO;
            } else {
                /*
                 * Precision must be a number giving the number of
                 * digits of precision.
                 */
                guint32 val;

                if (!ws_strtou32(dotp + 1, NULL, &val) || val > WS_TSPREC_MAX) {
                    cmdarg_err("Invalid .N time stamp precision \"%s\"; N must be a value between 0 and %u or absent",
                               dotp + 1, WS_TSPREC_MAX);
                    return FALSE;
                }
                tsp = val;
            }
            /* Mask the '.' while checking format. */
            *dotp = '\0';
        }
        if (strcmp(optarg_str_p, "r") == 0)
            global_dissect_options.time_format = TS_RELATIVE;
        else if (strcmp(optarg_str_p, "a") == 0)
            global_dissect_options.time_format = TS_ABSOLUTE;
        else if (strcmp(optarg_str_p, "ad") == 0)
            global_dissect_options.time_format = TS_ABSOLUTE_WITH_YMD;
        else if (strcmp(optarg_str_p, "adoy") == 0)
            global_dissect_options.time_format = TS_ABSOLUTE_WITH_YDOY;
        else if (strcmp(optarg_str_p, "d") == 0)
            global_dissect_options.time_format = TS_DELTA;
        else if (strcmp(optarg_str_p, "dd") == 0)
            global_dissect_options.time_format = TS_DELTA_DIS;
        else if (strcmp(optarg_str_p, "e") == 0)
            global_dissect_options.time_format = TS_EPOCH;
        else if (strcmp(optarg_str_p, "u") == 0)
            global_dissect_options.time_format = TS_UTC;
        else if (strcmp(optarg_str_p, "ud") == 0)
            global_dissect_options.time_format = TS_UTC_WITH_YMD;
        else if (strcmp(optarg_str_p, "udoy") == 0)
            global_dissect_options.time_format = TS_UTC_WITH_YDOY;
        else if (optarg_str_p != dotp) {
            /* If (optarg_str_p == dotp), user only set precision. */
            cmdarg_err("Invalid time stamp type \"%s\"; it must be one of:", optarg_str_p);
            cmdarg_err_cont("\t\"a\"    for absolute\n"
                            "\t\"ad\"   for absolute with YYYY-MM-DD date\n"
                            "\t\"adoy\" for absolute with YYYY/DOY date\n"
                            "\t\"d\"    for delta\n"
                            "\t\"dd\"   for delta displayed\n"
                            "\t\"e\"    for epoch\n"
                            "\t\"r\"    for relative\n"
                            "\t\"u\"    for absolute UTC\n"
                            "\t\"ud\"   for absolute UTC with YYYY-MM-DD date\n"
                            "\t\"udoy\" for absolute UTC with YYYY/DOY date");
            if (dotp)
                *dotp = '.';
            return FALSE;
        }
        if (dotp) {
            *dotp = '.';
            global_dissect_options.time_precision = tsp;
        }
        break;
    case 'u':        /* Seconds type */
        if (strcmp(optarg_str_p, "s") == 0)
            timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
        else if (strcmp(optarg_str_p, "hms") == 0)
            timestamp_set_seconds_type(TS_SECONDS_HOUR_MIN_SEC);
        else {
            cmdarg_err("Invalid seconds type \"%s\"; it must be one of:", optarg_str_p);
            cmdarg_err_cont("\t\"s\"   for seconds\n"
                            "\t\"hms\" for hours, minutes and seconds");
            return FALSE;
        }
        break;
    case LONGOPT_DISABLE_PROTOCOL: /* disable dissection of protocol */
        global_dissect_options.disable_protocol_slist = g_slist_append(global_dissect_options.disable_protocol_slist, optarg_str_p);
        break;
    case LONGOPT_ENABLE_HEURISTIC: /* enable heuristic dissection of protocol */
        global_dissect_options.enable_heur_slist = g_slist_append(global_dissect_options.enable_heur_slist, optarg_str_p);
        break;
    case LONGOPT_DISABLE_HEURISTIC: /* disable heuristic dissection of protocol */
        global_dissect_options.disable_heur_slist = g_slist_append(global_dissect_options.disable_heur_slist, optarg_str_p);
        break;
    case LONGOPT_ENABLE_PROTOCOL: /* enable dissection of protocol (that is disableed by default) */
        global_dissect_options.enable_protocol_slist = g_slist_append(global_dissect_options.enable_protocol_slist, optarg_str_p);
        break;
    case LONGOPT_ONLY_PROTOCOLS: /* enable dissection of these comma separated protocols only */
        proto_disable_all();
        for (char *ps = strtok (optarg_str_p, ","); ps; ps = strtok(NULL, ",")){
          global_dissect_options.enable_protocol_slist = g_slist_append(global_dissect_options.enable_protocol_slist, ps);
        }
        break;
    case LONGOPT_DISABLE_ALL_PROTOCOLS: /* disable dissection of all protocols */
        proto_disable_all();
        break;
    default:
        /* the caller is responsible to send us only the right opt's */
        ws_assert_not_reached();
    }
    return TRUE;
}

typedef gboolean (proto_set_func)(const char *);

static gboolean
process_enable_disable_list(GSList *list, proto_set_func callback)
{
    gboolean success = TRUE;
    gboolean rv;
    GSList *iter;
    char *c;
    char *proto_name;

    for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
        proto_name = (char *)iter->data;
        c = strchr(proto_name, ',');
        if (c == NULL) {
            rv = callback(proto_name);
            if (!rv) {
                cmdarg_err("No such protocol %s", proto_name);
                success = FALSE;
            }
        }
        else {
            char *start;
            char save;

            start = proto_name;
            while(1) {
                if (c != NULL) {
                    save = *c;
                    *c = '\0';
                }
                rv = callback(start);
                if (!rv) {
                    cmdarg_err("No such protocol %s", start);
                    success = FALSE;
                }
                if (c != NULL) {
                    *c = save;
                    start = (save == ',' ? c+1 : c);
                    c = strchr(start, ',');
                }
                else {
                    break;
                }
            }
        }
    }

    return success;
}

gboolean
setup_enabled_and_disabled_protocols(void)
{
    gboolean success = TRUE;

    success &= process_enable_disable_list(global_dissect_options.disable_protocol_slist,
            proto_disable_proto_by_name);
    success &= process_enable_disable_list(global_dissect_options.enable_protocol_slist,
            proto_enable_proto_by_name);
    success &= process_enable_disable_list(global_dissect_options.enable_heur_slist,
            proto_enable_heuristic_by_name);
    success &= process_enable_disable_list(global_dissect_options.disable_heur_slist,
            proto_disable_heuristic_by_name);
    return success;
}