1 /* dissect_opts.c
2  * Routines for dissection options setting
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #include <config.h>
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 #include <string.h>
17 
18 #include <errno.h>
19 
20 #include <glib.h>
21 
22 #include <epan/prefs.h>
23 #include <epan/timestamp.h>
24 #include <epan/addr_resolv.h>
25 #include <epan/disabled_protos.h>
26 
27 #include "ui/decode_as_utils.h"
28 
29 #if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
30 #include <epan/dissectors/read_keytab_file.h>
31 #endif
32 
33 #include <ui/clopts_common.h>
34 #include <ui/cmdarg_err.h>
35 #include <wsutil/file_util.h>
36 #include <wsutil/ws_assert.h>
37 
38 #include "ui/dissect_opts.h"
39 
40 dissect_options global_dissect_options;
41 
42 void
dissect_opts_init(void)43 dissect_opts_init(void)
44 {
45     global_dissect_options.time_format = TS_NOT_SET;
46     global_dissect_options.disable_protocol_slist = NULL;
47     global_dissect_options.enable_protocol_slist = NULL;
48     global_dissect_options.enable_heur_slist = NULL;
49     global_dissect_options.disable_heur_slist = NULL;
50 }
51 
52 gboolean
dissect_opts_handle_opt(int opt,char * optarg_str_p)53 dissect_opts_handle_opt(int opt, char *optarg_str_p)
54 {
55     char badopt;
56 
57     switch(opt) {
58     case 'd':        /* Decode as rule */
59         if (!decode_as_command_option(optarg_str_p))
60              return FALSE;
61         break;
62     case 'K':        /* Kerberos keytab file */
63 #if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
64         read_keytab_file(optarg_str_p);
65 #else
66         cmdarg_err("-K specified, but Kerberos keytab file support isn't present");
67         return FALSE;
68 #endif
69         break;
70     case 'n':        /* No name resolution */
71         disable_name_resolution();
72         break;
73     case 'N':        /* Select what types of addresses/port #s to resolve */
74         badopt = string_to_name_resolve(optarg_str_p, &gbl_resolv_flags);
75         if (badopt != '\0') {
76             cmdarg_err("-N specifies unknown resolving option '%c'; valid options are:",
77                        badopt);
78             cmdarg_err_cont("\t'd' to enable address resolution from captured DNS packets\n"
79                             "\t'm' to enable MAC address resolution\n"
80                             "\t'n' to enable network address resolution\n"
81                             "\t'N' to enable using external resolvers (e.g., DNS)\n"
82                             "\t    for network address resolution\n"
83                             "\t't' to enable transport-layer port number resolution\n"
84                             "\t'v' to enable VLAN IDs to names resolution");
85             return FALSE;
86         }
87         break;
88     case 't':        /* Time stamp type */
89         if (strcmp(optarg_str_p, "r") == 0)
90             global_dissect_options.time_format = TS_RELATIVE;
91         else if (strcmp(optarg_str_p, "a") == 0)
92             global_dissect_options.time_format = TS_ABSOLUTE;
93         else if (strcmp(optarg_str_p, "ad") == 0)
94             global_dissect_options.time_format = TS_ABSOLUTE_WITH_YMD;
95         else if (strcmp(optarg_str_p, "adoy") == 0)
96             global_dissect_options.time_format = TS_ABSOLUTE_WITH_YDOY;
97         else if (strcmp(optarg_str_p, "d") == 0)
98             global_dissect_options.time_format = TS_DELTA;
99         else if (strcmp(optarg_str_p, "dd") == 0)
100             global_dissect_options.time_format = TS_DELTA_DIS;
101         else if (strcmp(optarg_str_p, "e") == 0)
102             global_dissect_options.time_format = TS_EPOCH;
103         else if (strcmp(optarg_str_p, "u") == 0)
104             global_dissect_options.time_format = TS_UTC;
105         else if (strcmp(optarg_str_p, "ud") == 0)
106             global_dissect_options.time_format = TS_UTC_WITH_YMD;
107         else if (strcmp(optarg_str_p, "udoy") == 0)
108             global_dissect_options.time_format = TS_UTC_WITH_YDOY;
109         else {
110             cmdarg_err("Invalid time stamp type \"%s\"; it must be one of:", optarg_str_p);
111             cmdarg_err_cont("\t\"a\"    for absolute\n"
112                             "\t\"ad\"   for absolute with YYYY-MM-DD date\n"
113                             "\t\"adoy\" for absolute with YYYY/DOY date\n"
114                             "\t\"d\"    for delta\n"
115                             "\t\"dd\"   for delta displayed\n"
116                             "\t\"e\"    for epoch\n"
117                             "\t\"r\"    for relative\n"
118                             "\t\"u\"    for absolute UTC\n"
119                             "\t\"ud\"   for absolute UTC with YYYY-MM-DD date\n"
120                             "\t\"udoy\" for absolute UTC with YYYY/DOY date");
121             return FALSE;
122         }
123         break;
124     case 'u':        /* Seconds type */
125         if (strcmp(optarg_str_p, "s") == 0)
126             timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
127         else if (strcmp(optarg_str_p, "hms") == 0)
128             timestamp_set_seconds_type(TS_SECONDS_HOUR_MIN_SEC);
129         else {
130             cmdarg_err("Invalid seconds type \"%s\"; it must be one of:", optarg_str_p);
131             cmdarg_err_cont("\t\"s\"   for seconds\n"
132                             "\t\"hms\" for hours, minutes and seconds");
133             return FALSE;
134         }
135         break;
136     case LONGOPT_DISABLE_PROTOCOL: /* disable dissection of protocol */
137         global_dissect_options.disable_protocol_slist = g_slist_append(global_dissect_options.disable_protocol_slist, optarg_str_p);
138         break;
139     case LONGOPT_ENABLE_HEURISTIC: /* enable heuristic dissection of protocol */
140         global_dissect_options.enable_heur_slist = g_slist_append(global_dissect_options.enable_heur_slist, optarg_str_p);
141         break;
142     case LONGOPT_DISABLE_HEURISTIC: /* disable heuristic dissection of protocol */
143         global_dissect_options.disable_heur_slist = g_slist_append(global_dissect_options.disable_heur_slist, optarg_str_p);
144         break;
145     case LONGOPT_ENABLE_PROTOCOL: /* enable dissection of protocol (that is disableed by default) */
146         global_dissect_options.enable_protocol_slist = g_slist_append(global_dissect_options.enable_protocol_slist, optarg_str_p);
147         break;
148     default:
149         /* the caller is responsible to send us only the right opt's */
150         ws_assert_not_reached();
151     }
152     return TRUE;
153 }
154 
155 gboolean
setup_enabled_and_disabled_protocols(void)156 setup_enabled_and_disabled_protocols(void)
157 {
158     gboolean success = TRUE;
159 
160     if (global_dissect_options.disable_protocol_slist) {
161         GSList *proto_disable;
162 
163         for (proto_disable = global_dissect_options.disable_protocol_slist; proto_disable != NULL; proto_disable = g_slist_next(proto_disable))
164             proto_disable_proto_by_name((char*)proto_disable->data);
165     }
166 
167     if (global_dissect_options.enable_protocol_slist) {
168         GSList *proto_enable;
169 
170         for (proto_enable = global_dissect_options.enable_protocol_slist; proto_enable != NULL; proto_enable = g_slist_next(proto_enable))
171             proto_enable_proto_by_name((char*)proto_enable->data);
172     }
173 
174     if (global_dissect_options.enable_heur_slist) {
175         GSList *heur_enable;
176 
177         for (heur_enable = global_dissect_options.enable_heur_slist; heur_enable != NULL; heur_enable = g_slist_next(heur_enable)) {
178             if (!proto_enable_heuristic_by_name((char*)heur_enable->data, TRUE)) {
179                 cmdarg_err("No such protocol %s, can't enable", (char*)heur_enable->data);
180                 success = FALSE;
181             }
182         }
183     }
184 
185     if (global_dissect_options.disable_heur_slist) {
186         GSList *heur_disable;
187 
188         for (heur_disable = global_dissect_options.disable_heur_slist; heur_disable != NULL; heur_disable = g_slist_next(heur_disable)) {
189             if (!proto_enable_heuristic_by_name((char*)heur_disable->data, FALSE)) {
190                 cmdarg_err("No such protocol %s, can't disable", (char*)heur_disable->data);
191                 success = FALSE;
192             }
193         }
194     }
195     return success;
196 }
197