TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / extcap / sshdump.c
blobfef552e22774c5c6bf645878862f4179386ffba0
1 /* sshdump.c
2 * sshdump is extcap tool used to capture data using a remote ssh host
4 * Copyright 2015, Dario Lombardo
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
14 #define WS_LOG_DOMAIN "sshdump"
16 #include <extcap/extcap-base.h>
17 #include <extcap/ssh-base.h>
18 #include <wsutil/interface.h>
19 #include <wsutil/file_util.h>
20 #include <wsutil/strtoi.h>
21 #include <wsutil/filesystem.h>
22 #include <wsutil/privileges.h>
23 #include <wsutil/please_report_bug.h>
24 #include <wsutil/wslog.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <fcntl.h>
30 #include <cli_main.h>
32 static char* sshdump_extcap_interface;
33 #define DEFAULT_SSHDUMP_EXTCAP_INTERFACE "sshdump"
35 #define SSHDUMP_VERSION_MAJOR "1"
36 #define SSHDUMP_VERSION_MINOR "2"
37 #define SSHDUMP_VERSION_RELEASE "0"
39 #define SSH_READ_BLOCK_SIZE 256
41 enum {
42 EXTCAP_BASE_OPTIONS_ENUM,
43 OPT_HELP,
44 OPT_VERSION,
45 OPT_REMOTE_HOST,
46 OPT_REMOTE_PORT,
47 OPT_REMOTE_USERNAME,
48 OPT_REMOTE_PASSWORD,
49 OPT_REMOTE_INTERFACE,
50 OPT_REMOTE_CAPTURE_COMMAND_SELECT,
51 OPT_REMOTE_CAPTURE_COMMAND,
52 OPT_REMOTE_FILTER,
53 OPT_SSHKEY,
54 OPT_SSHKEY_PASSPHRASE,
55 OPT_PROXYCOMMAND,
56 OPT_SSH_SHA1,
57 OPT_REMOTE_COUNT,
58 OPT_REMOTE_SUDO, // Deprecated
59 OPT_REMOTE_PRIV,
60 OPT_REMOTE_PRIV_USER,
61 OPT_REMOTE_NOPROM
64 static const struct ws_option longopts[] = {
65 EXTCAP_BASE_OPTIONS,
66 { "help", ws_no_argument, NULL, OPT_HELP},
67 { "version", ws_no_argument, NULL, OPT_VERSION},
68 SSH_BASE_PACKET_OPTIONS,
69 { "remote-capture-command-select", ws_required_argument, NULL, OPT_REMOTE_CAPTURE_COMMAND_SELECT},
70 { "remote-capture-command", ws_required_argument, NULL, OPT_REMOTE_CAPTURE_COMMAND},
71 { "remote-sudo", ws_no_argument, NULL, OPT_REMOTE_SUDO }, // Deprecated
72 { "remote-priv", ws_required_argument, NULL, OPT_REMOTE_PRIV },
73 { "remote-priv-user", ws_required_argument, NULL, OPT_REMOTE_PRIV_USER },
74 { "remote-noprom", ws_no_argument, NULL, OPT_REMOTE_NOPROM },
75 { 0, 0, 0, 0}
78 static char* interfaces_list_to_filter(GSList* if_list, unsigned int remote_port);
80 static int ssh_loop_read(ssh_channel channel, FILE* fp)
82 int nbytes;
83 int ret = EXIT_SUCCESS;
84 char buffer[SSH_READ_BLOCK_SIZE];
86 /* read from stdin until data are available */
87 while (ssh_channel_is_open(channel) && !ssh_channel_is_eof(channel)) {
88 nbytes = ssh_channel_read(channel, buffer, SSH_READ_BLOCK_SIZE, 0);
89 if (nbytes < 0) {
90 ws_warning("Error reading from channel");
91 goto end;
93 if (nbytes == 0) {
94 break;
96 if (fwrite(buffer, 1, nbytes, fp) != (unsigned)nbytes) {
97 ws_warning("Error writing to fifo");
98 ret = EXIT_FAILURE;
99 goto end;
101 fflush(fp);
104 /* read loop finished... maybe something wrong happened. Read from stderr */
105 while (ssh_channel_is_open(channel) && !ssh_channel_is_eof(channel)) {
106 nbytes = ssh_channel_read(channel, buffer, SSH_READ_BLOCK_SIZE, 1);
107 if (nbytes < 0) {
108 ws_warning("Error reading from channel");
109 goto end;
111 if (fwrite(buffer, 1, nbytes, stderr) != (unsigned)nbytes) {
112 ws_warning("Error writing to stderr");
113 break;
117 end:
118 if (ssh_channel_send_eof(channel) != SSH_OK) {
119 ws_warning("Error sending EOF in ssh channel");
120 ret = EXIT_FAILURE;
122 return ret;
125 static char* local_interfaces_to_filter(const uint16_t remote_port)
127 GSList* interfaces = local_interfaces_to_list();
128 char* filter = interfaces_list_to_filter(interfaces, remote_port);
129 g_slist_free_full(interfaces, g_free);
130 return filter;
133 static ssh_channel run_ssh_command(ssh_session sshs, const char* capture_command_select,
134 const char* capture_command, const char* privilege, bool noprom,
135 const char* iface, const char* cfilter, const uint32_t count)
137 char* cmdline = NULL;
138 ssh_channel channel;
139 char** ifaces_array = NULL;
140 int ifaces_array_num = 0;
141 GString *ifaces_string;
142 char *ifaces = NULL;
143 char* quoted_iface = NULL;
144 char* quoted_filter = NULL;
145 char* count_str = NULL;
146 unsigned int remote_port = 22;
148 channel = ssh_channel_new(sshs);
149 if (!channel) {
150 ws_warning("Can't create channel");
151 return NULL;
154 if (ssh_channel_open_session(channel) != SSH_OK) {
155 ws_warning("Can't open session");
156 ssh_channel_free(channel);
157 return NULL;
160 ssh_options_get_port(sshs, &remote_port);
162 if (capture_command_select == NULL || !g_strcmp0(capture_command_select, "other")) {
163 if (capture_command && *capture_command) {
164 cmdline = g_strdup(capture_command);
165 ws_debug("Remote capture command has disabled other options");
166 } else {
167 capture_command_select = "tcpdump";
171 /* escape parameters to go save with the shell */
172 if (!g_strcmp0(capture_command_select, "tcpdump")) {
173 quoted_iface = iface ? g_shell_quote(iface) : NULL;
174 quoted_filter = g_shell_quote(cfilter ? cfilter : "");
175 if (count > 0)
176 count_str = ws_strdup_printf("-c %u", count);
178 cmdline = ws_strdup_printf("%s tcpdump -U %s%s %s -w - %s %s",
179 privilege,
180 quoted_iface ? "-i " : "",
181 quoted_iface ? quoted_iface : "",
182 noprom ? "-p" : "",
183 count_str ? count_str : "",
184 quoted_filter);
185 } else if (!g_strcmp0(capture_command_select, "dumpcap")) {
186 if (iface) {
187 ifaces_array = g_strsplit(iface, " ", -1);
188 ifaces_string = g_string_new(NULL);
189 while (ifaces_array[ifaces_array_num])
191 quoted_iface = g_shell_quote(ifaces_array[ifaces_array_num]);
192 g_string_append_printf(ifaces_string, "-i %s ", quoted_iface);
193 ifaces_array_num++;
195 ifaces = g_string_free(ifaces_string, FALSE);
197 quoted_filter = g_shell_quote(cfilter ? cfilter : "");
198 if (count > 0)
199 count_str = ws_strdup_printf("-c %u", count);
201 cmdline = ws_strdup_printf("%s dumpcap %s %s -w - %s -f %s",
202 privilege,
203 noprom ? "-p" : "",
204 ifaces ? ifaces : "",
205 count_str ? count_str : "",
206 quoted_filter);
208 g_free(ifaces);
209 g_strfreev(ifaces_array);
212 ws_debug("Running: %s", cmdline);
213 if (ssh_channel_request_exec(channel, cmdline) != SSH_OK) {
214 ws_warning("Can't request exec");
215 ssh_channel_close(channel);
216 ssh_channel_free(channel);
217 channel = NULL;
220 g_free(quoted_iface);
221 g_free(quoted_filter);
222 g_free(cmdline);
223 g_free(count_str);
225 return channel;
228 static int ssh_open_remote_connection(const ssh_params_t* params, const char* iface, const char* cfilter,
229 const char* capture_command_select, const char* capture_command, const char* privilege,
230 bool noprom, const uint32_t count, const char* fifo)
232 ssh_session sshs = NULL;
233 ssh_channel channel = NULL;
234 FILE* fp = stdout;
235 int ret = EXIT_FAILURE;
236 char* err_info = NULL;
238 if (g_strcmp0(fifo, "-")) {
239 /* Open or create the output file */
240 fp = fopen(fifo, "wb");
241 if (fp == NULL) {
242 ws_warning("Error creating output file: %s (%s)", fifo, g_strerror(errno));
243 return EXIT_FAILURE;
247 sshs = create_ssh_connection(params, &err_info);
249 if (!sshs) {
250 ws_warning("Error creating connection.");
251 goto cleanup;
254 channel = run_ssh_command(sshs, capture_command_select, capture_command, privilege, noprom, iface, cfilter, count);
256 if (!channel) {
257 ws_warning("Can't run ssh command.");
258 goto cleanup;
261 /* read from channel and write into fp */
262 if (ssh_loop_read(channel, fp) != EXIT_SUCCESS) {
263 ws_warning("Error in read loop.");
264 ret = EXIT_FAILURE;
265 goto cleanup;
268 ret = EXIT_SUCCESS;
269 cleanup:
270 if (err_info)
271 ws_warning("%s", err_info);
272 g_free(err_info);
274 /* clean up and exit */
275 ssh_cleanup(&sshs, &channel);
277 if (g_strcmp0(fifo, "-"))
278 fclose(fp);
279 return ret;
282 static char* interfaces_list_to_filter(GSList* interfaces, unsigned int remote_port)
284 GString* filter = g_string_new(NULL);
285 GSList* cur;
287 // If no port is given, assume the default one. This might not be
288 // correct if the port is looked up from the ssh config file, but it is
289 // better than nothing.
290 if (remote_port == 0) {
291 remote_port = 22;
294 if (!interfaces) {
295 g_string_append_printf(filter, "not port %u", remote_port);
296 } else {
297 g_string_append_printf(filter, "not ((host %s", (char*)interfaces->data);
298 cur = g_slist_next(interfaces);
299 while (cur) {
300 g_string_append_printf(filter, " or host %s", (char*)cur->data);
301 cur = g_slist_next(cur);
303 g_string_append_printf(filter, ") and port %u)", remote_port);
305 return g_string_free(filter, FALSE);
308 static int list_config(char *interface, unsigned int remote_port)
310 unsigned inc = 0;
311 char* ipfilter;
313 if (!interface) {
314 ws_warning("ERROR: No interface specified.");
315 return EXIT_FAILURE;
318 if (g_strcmp0(interface, sshdump_extcap_interface)) {
319 ws_warning("ERROR: interface must be %s", sshdump_extcap_interface);
320 return EXIT_FAILURE;
323 ipfilter = local_interfaces_to_filter(remote_port);
325 printf("arg {number=%u}{call=--remote-host}{display=Remote SSH server address}"
326 "{type=string}{tooltip=The remote SSH host. It can be both "
327 "an IP address or a hostname}{required=true}{group=Server}\n", inc++);
328 printf("arg {number=%u}{call=--remote-port}{display=Remote SSH server port}"
329 "{type=unsigned}{default=22}{tooltip=The remote SSH host port (1-65535)}"
330 "{range=1,65535}{group=Server}\n", inc++);
331 printf("arg {number=%u}{call=--remote-username}{display=Remote SSH server username}"
332 "{type=string}{tooltip=The remote SSH username. If not provided, "
333 "the current user will be used}{group=Authentication}\n", inc++);
334 printf("arg {number=%u}{call=--remote-password}{display=Remote SSH server password}"
335 "{type=password}{tooltip=The SSH password, used when other methods (SSH agent "
336 "or key files) are unavailable.}{group=Authentication}\n", inc++);
337 printf("arg {number=%u}{call=--sshkey}{display=Path to SSH private key}"
338 "{type=fileselect}{tooltip=The path on the local filesystem of the private SSH key (OpenSSH format)}"
339 "{mustexist=true}{group=Authentication}\n", inc++);
340 printf("arg {number=%u}{call=--sshkey-passphrase}{display=SSH key passphrase}"
341 "{type=password}{tooltip=Passphrase to unlock the SSH private key}{group=Authentication}\n",
342 inc++);
343 printf("arg {number=%u}{call=--proxycommand}{display=ProxyCommand}"
344 "{type=string}{tooltip=The command to use as proxy for the SSH connection}"
345 "{group=Authentication}\n", inc++);
346 printf("arg {number=%u}{call=--ssh-sha1}{display=Support SHA-1 keys (deprecated)}"
347 "{type=boolflag}{tooltip=Support keys and key exchange algorithms using SHA-1 (deprecated)}{group=Authentication}"
348 "\n", inc++);
349 printf("arg {number=%u}{call=--remote-interface}{display=Remote interface}"
350 "{type=string}{tooltip=The remote network interface used for capture"
351 "}{group=Capture}\n", inc++);
352 printf("arg {number=%u}{call=--remote-capture-command-select}{display=Remote capture command selection}"
353 "{type=radio}{tooltip=The remote capture command to build a command line for}{group=Capture}\n", inc);
354 printf("value {arg=%u}{value=dumpcap}{display=dumpcap}\n", inc);
355 printf("value {arg=%u}{value=tcpdump}{display=tcpdump}{default=true}\n", inc);
356 printf("value {arg=%u}{value=other}{display=Other:}\n", inc++);
357 printf("arg {number=%u}{call=--remote-capture-command}{display=Remote capture command}"
358 "{type=string}{tooltip=The remote command used to capture}{group=Capture}\n", inc++);
359 // Deprecated
360 //printf("arg {number=%u}{call=--remote-sudo}{display=Use sudo on the remote machine}"
361 // "{type=boolflag}{tooltip=Prepend the capture command with sudo on the remote machine}"
362 // "{group=Capture}\n", inc++);
363 printf("arg {number=%u}{call=--remote-priv}{display=Gain capture privilege on the remote machine}"
364 "{type=radio}{tooltip=Optionally prepend the capture command with sudo or doas on the remote machine}"
365 "{group=Capture}\n", inc);
366 printf("value {arg=%u}{value=none}{display=none}{default=true}\n", inc);
367 printf("value {arg=%u}{value=sudo}{display=sudo}\n", inc);
368 printf("value {arg=%u}{value=doas -n}{display=doas}\n", inc++);
369 printf("arg {number=%u}{call=--remote-priv-user}{display=Privileged user name for sudo or doas}"
370 "{type=string}{tooltip=User name of privileged user to execute the capture command on the remote machine}"
371 "{group=Capture}\n", inc++);
372 printf("arg {number=%u}{call=--remote-noprom}{display=No promiscuous mode}"
373 "{type=boolflag}{tooltip=Don't use promiscuous mode on the remote machine}{group=Capture}"
374 "\n", inc++);
375 printf("arg {number=%u}{call=--remote-filter}{display=Remote capture filter}{type=string}"
376 "{tooltip=The remote capture filter}", inc++);
377 if (ipfilter)
378 printf("{default=%s}", ipfilter);
379 printf("{group=Capture}\n");
380 printf("arg {number=%u}{call=--remote-count}{display=Packets to capture}"
381 "{type=unsigned}{default=0}{tooltip=The number of remote packets to capture. (Default: inf)}"
382 "{group=Capture}\n", inc++);
384 extcap_config_debug(&inc);
386 g_free(ipfilter);
388 return EXIT_SUCCESS;
391 static char* concat_filters(const char* extcap_filter, const char* remote_filter)
393 if (!extcap_filter && remote_filter)
394 return g_strdup(remote_filter);
396 if (!remote_filter && extcap_filter)
397 return g_strdup(extcap_filter);
399 if (!remote_filter && !extcap_filter)
400 return NULL;
402 return ws_strdup_printf("(%s) and (%s)", extcap_filter, remote_filter);
405 int main(int argc, char *argv[])
407 char* err_msg;
408 int result;
409 int option_idx = 0;
410 ssh_params_t* ssh_params = ssh_params_new();
411 char* remote_interface = NULL;
412 char* remote_capture_command_select = NULL;
413 char* remote_capture_command = NULL;
414 char* remote_filter = NULL;
415 uint32_t count = 0;
416 int ret = EXIT_FAILURE;
417 extcap_parameters* extcap_conf = g_new0(extcap_parameters, 1);
418 char* help_url;
419 char* help_header = NULL;
420 char* priv = NULL;
421 char* priv_user = NULL;
422 bool noprom = false;
423 char* interface_description = g_strdup("SSH remote capture");
425 /* Set the program name. */
426 g_set_prgname("sshdump");
428 /* Initialize log handler early so we can have proper logging during startup. */
429 extcap_log_init();
431 sshdump_extcap_interface = g_path_get_basename(argv[0]);
432 if (g_str_has_suffix(sshdump_extcap_interface, ".exe")) {
433 sshdump_extcap_interface[strlen(sshdump_extcap_interface) - 4] = '\0';
437 * Get credential information for later use.
439 init_process_policies();
442 * Attempt to get the pathname of the directory containing the
443 * executable file.
445 err_msg = configuration_init(argv[0]);
446 if (err_msg != NULL) {
447 ws_warning("Can't get pathname of directory containing the extcap program: %s.",
448 err_msg);
449 g_free(err_msg);
452 help_url = data_file_url("sshdump.html");
453 extcap_base_set_util_info(extcap_conf, argv[0], SSHDUMP_VERSION_MAJOR, SSHDUMP_VERSION_MINOR,
454 SSHDUMP_VERSION_RELEASE, help_url);
455 g_free(help_url);
456 add_libssh_info(extcap_conf);
457 if (g_strcmp0(sshdump_extcap_interface, DEFAULT_SSHDUMP_EXTCAP_INTERFACE)) {
458 char* temp = interface_description;
459 interface_description = ws_strdup_printf("%s, custom version", interface_description);
460 g_free(temp);
462 extcap_base_register_interface(extcap_conf, sshdump_extcap_interface, interface_description, 147, "Remote capture dependent DLT");
463 g_free(interface_description);
465 help_header = ws_strdup_printf(
466 " %s --extcap-interfaces\n"
467 " %s --extcap-interface=%s --extcap-dlts\n"
468 " %s --extcap-interface=%s --extcap-config\n"
469 " %s --extcap-interface=%s --remote-host myhost --remote-port 22222 "
470 "--remote-username myuser --remote-interface eth2 --remote-capture-command 'tcpdump -U -i eth0 -w -' "
471 "--fifo=FILENAME --capture\n", argv[0], argv[0], sshdump_extcap_interface, argv[0],
472 sshdump_extcap_interface, argv[0], sshdump_extcap_interface);
473 extcap_help_add_header(extcap_conf, help_header);
474 g_free(help_header);
475 extcap_help_add_option(extcap_conf, "--help", "print this help");
476 extcap_help_add_option(extcap_conf, "--version", "print the version");
477 extcap_help_add_option(extcap_conf, "--remote-host <host>", "the remote SSH host");
478 extcap_help_add_option(extcap_conf, "--remote-port <port>", "the remote SSH port");
479 extcap_help_add_option(extcap_conf, "--remote-username <username>", "the remote SSH username");
480 extcap_help_add_option(extcap_conf, "--remote-password <password>", "the remote SSH password. If not specified, ssh-agent and ssh-key are used");
481 extcap_help_add_option(extcap_conf, "--sshkey <private key path>", "the path of the SSH key (OpenSSH format)");
482 extcap_help_add_option(extcap_conf, "--sshkey-passphrase <private key passphrase>", "the passphrase to unlock private SSH key");
483 extcap_help_add_option(extcap_conf, "--proxycommand <proxy command>", "the command to use as proxy for the SSH connection");
484 extcap_help_add_option(extcap_conf, "--ssh-sha1", "support keys and key exchange using SHA-1 (deprecated)");
485 extcap_help_add_option(extcap_conf, "--remote-interface <iface>", "the remote capture interface");
486 extcap_help_add_option(extcap_conf, "--remote-capture-command-select <selection>", "dumpcap, tcpdump or other remote capture command");
487 extcap_help_add_option(extcap_conf, "--remote-capture-command <capture command>", "the remote capture command");
488 //extcap_help_add_option(extcap_conf, "--remote-sudo", "use sudo on the remote machine to capture"); // Deprecated
489 extcap_help_add_option(extcap_conf, "--remote-priv <selection>", "none, sudo or doas");
490 extcap_help_add_option(extcap_conf, "--remote-priv-user <username>", "privileged user name");
491 extcap_help_add_option(extcap_conf, "--remote-noprom", "don't use promiscuous mode on the remote machine");
492 extcap_help_add_option(extcap_conf, "--remote-filter <filter>", "a filter for remote capture (default: don't listen on local interfaces IPs)");
493 extcap_help_add_option(extcap_conf, "--remote-count <count>", "the number of packets to capture");
495 ws_opterr = 0;
496 ws_optind = 0;
498 if (argc == 1) {
499 extcap_help_print(extcap_conf);
500 goto end;
503 while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
505 switch (result) {
507 case OPT_HELP:
508 extcap_help_print(extcap_conf);
509 ret = EXIT_SUCCESS;
510 goto end;
512 case OPT_VERSION:
513 extcap_version_print(extcap_conf);
514 ret = EXIT_SUCCESS;
515 goto end;
517 case OPT_REMOTE_HOST:
518 g_free(ssh_params->host);
519 ssh_params->host = g_strdup(ws_optarg);
520 break;
522 case OPT_REMOTE_PORT:
523 if (!ws_strtou16(ws_optarg, NULL, &ssh_params->port) || ssh_params->port == 0) {
524 ws_warning("Invalid port: %s", ws_optarg);
525 goto end;
527 break;
529 case OPT_REMOTE_USERNAME:
530 g_free(ssh_params->username);
531 ssh_params->username = g_strdup(ws_optarg);
532 break;
534 case OPT_REMOTE_PASSWORD:
535 g_free(ssh_params->password);
536 ssh_params->password = g_strdup(ws_optarg);
537 memset(ws_optarg, 'X', strlen(ws_optarg));
538 break;
540 case OPT_SSHKEY:
541 g_free(ssh_params->sshkey_path);
542 ssh_params->sshkey_path = g_strdup(ws_optarg);
543 break;
545 case OPT_SSHKEY_PASSPHRASE:
546 g_free(ssh_params->sshkey_passphrase);
547 ssh_params->sshkey_passphrase = g_strdup(ws_optarg);
548 memset(ws_optarg, 'X', strlen(ws_optarg));
549 break;
551 case OPT_PROXYCOMMAND:
552 g_free(ssh_params->proxycommand);
553 ssh_params->proxycommand = g_strdup(ws_optarg);
554 break;
556 case OPT_SSH_SHA1:
557 ssh_params->ssh_sha1 = true;
558 break;
560 case OPT_REMOTE_INTERFACE:
561 g_free(remote_interface);
562 remote_interface = g_strdup(ws_optarg);
563 break;
565 case OPT_REMOTE_CAPTURE_COMMAND_SELECT:
566 g_free(remote_capture_command_select);
567 remote_capture_command_select = g_strdup(ws_optarg);
568 break;
570 case OPT_REMOTE_CAPTURE_COMMAND:
571 g_free(remote_capture_command);
572 remote_capture_command = g_strdup(ws_optarg);
573 break;
575 case OPT_REMOTE_SUDO:
576 // Deprecated
577 g_free(priv);
578 priv = g_strdup("sudo");
579 break;
581 case OPT_REMOTE_PRIV:
582 g_free(priv);
583 priv = g_strdup(ws_optarg);
584 break;
586 case OPT_REMOTE_PRIV_USER:
587 g_free(priv_user);
588 priv_user = g_strdup(ws_optarg);
589 break;
591 case OPT_REMOTE_FILTER:
592 g_free(remote_filter);
593 remote_filter = g_strdup(ws_optarg);
594 break;
596 case OPT_REMOTE_COUNT:
597 if (!ws_strtou32(ws_optarg, NULL, &count)) {
598 ws_warning("Invalid value for count: %s", ws_optarg);
599 goto end;
601 break;
603 case OPT_REMOTE_NOPROM:
604 noprom = true;
605 break;
607 case ':':
608 /* missing option argument */
609 ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]);
610 break;
612 default:
613 if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg)) {
614 ws_warning("Invalid option: %s", argv[ws_optind - 1]);
615 goto end;
620 extcap_cmdline_debug(argv, argc);
622 if (extcap_base_handle_interface(extcap_conf)) {
623 ret = EXIT_SUCCESS;
624 goto end;
627 if (extcap_conf->show_config) {
628 ret = list_config(extcap_conf->interface, ssh_params->port);
629 goto end;
632 err_msg = ws_init_sockets();
633 if (err_msg != NULL) {
634 ws_warning("ERROR: %s", err_msg);
635 g_free(err_msg);
636 ws_warning("%s", please_report_bug());
637 goto end;
640 if (extcap_conf->capture) {
641 char* filter;
642 char* privilege;
644 if (!ssh_params->host) {
645 ws_warning("Missing parameter: --remote-host");
646 goto end;
649 if ((priv) && g_strcmp0(priv, "none") && strlen(g_strstrip(priv))) {
650 if ((priv_user) && strlen(g_strstrip(priv_user)))
651 /* Both sudo and doas use the same command line option */
652 privilege = g_strconcat(priv, " -u ", priv_user, NULL);
653 else
654 privilege = g_strdup(priv);
655 } else {
656 privilege = g_strdup("");
659 // This may result in the use of a different port number than was given in
660 // the default filter string, as presented in the config dialog. The default
661 // given is always using the default SSH port since there's no remote SSH port
662 // given on the command line to get the extcap arguments.
663 // However the remote SSH port used here is the one given on the command line
664 // when the capture us started, which is the intended one.
665 // And this is only happening when no remote filter is specified on the command
666 // line to start the capture.
667 if (remote_filter == NULL)
668 remote_filter = local_interfaces_to_filter(ssh_params->port);
669 filter = concat_filters(extcap_conf->capture_filter, remote_filter);
670 ssh_params_set_log_level(ssh_params, extcap_conf->debug);
671 ret = ssh_open_remote_connection(ssh_params, remote_interface,
672 filter, remote_capture_command_select, remote_capture_command,
673 privilege, noprom, count, extcap_conf->fifo);
674 g_free(filter);
675 g_free(privilege);
676 } else {
677 ws_debug("You should not come here... maybe some parameter missing?");
678 ret = EXIT_FAILURE;
681 end:
682 /* clean up stuff */
683 ssh_params_free(ssh_params);
684 g_free(remote_capture_command_select);
685 g_free(remote_capture_command);
686 g_free(remote_interface);
687 g_free(remote_filter);
688 g_free(priv);
689 g_free(priv_user);
690 extcap_base_cleanup(&extcap_conf);
691 return ret;
695 * Editor modelines - https://www.wireshark.org/tools/modelines.html
697 * Local variables:
698 * c-basic-offset: 8
699 * tab-width: 8
700 * indent-tabs-mode: t
701 * End:
703 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
704 * :indentSize=8:tabSize=8:noTabs=false: