2016-01-05 18:49:13 +00:00
|
|
|
/*
|
2014-03-01 16:27:52 +00:00
|
|
|
* rofi
|
2014-01-21 09:01:55 +00:00
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2020-01-01 11:23:12 +00:00
|
|
|
* Copyright © 2013-2020 Qball Cow <qball@gmpclient.org>
|
2014-01-21 09:01:55 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-05 18:49:13 +00:00
|
|
|
/**
|
|
|
|
* \ingroup SSHMode
|
|
|
|
* @{
|
|
|
|
*/
|
2017-04-15 09:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log domain for the ssh modi.
|
|
|
|
*/
|
2017-04-27 20:59:14 +00:00
|
|
|
#define G_LOG_DOMAIN "Dialogs.Ssh"
|
2017-04-15 09:58:49 +00:00
|
|
|
|
2014-08-22 07:26:46 +00:00
|
|
|
#include <config.h>
|
2017-02-17 13:06:31 +00:00
|
|
|
#include <glib.h>
|
2014-01-21 09:01:55 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2014-01-21 13:56:25 +00:00
|
|
|
#include <errno.h>
|
2014-09-03 11:07:26 +00:00
|
|
|
#include <helper.h>
|
2017-03-08 15:56:28 +00:00
|
|
|
#include <glob.h>
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2014-03-12 07:41:38 +00:00
|
|
|
#include "rofi.h"
|
2016-01-07 15:01:56 +00:00
|
|
|
#include "settings.h"
|
2014-05-13 08:45:59 +00:00
|
|
|
#include "history.h"
|
2015-03-25 07:36:19 +00:00
|
|
|
#include "dialogs/ssh.h"
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2019-09-30 08:35:12 +00:00
|
|
|
/**
|
|
|
|
* Holding an ssh entry.
|
|
|
|
*/
|
2020-02-02 12:56:37 +00:00
|
|
|
typedef struct _SshEntry
|
|
|
|
{
|
2019-09-30 08:35:12 +00:00
|
|
|
/** SSH hostname */
|
2019-03-04 21:05:08 +00:00
|
|
|
char *hostname;
|
2019-09-30 08:35:12 +00:00
|
|
|
/** SSH port number */
|
2019-03-04 21:05:08 +00:00
|
|
|
int port;
|
|
|
|
} SshEntry;
|
|
|
|
/**
|
|
|
|
* The internal data structure holding the private data of the SSH Mode.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
GList *user_known_hosts;
|
2019-03-04 21:05:08 +00:00
|
|
|
/** List if available ssh hosts.*/
|
2020-02-02 12:56:37 +00:00
|
|
|
SshEntry *hosts_list;
|
2019-03-04 21:05:08 +00:00
|
|
|
/** Length of the #hosts_list.*/
|
|
|
|
unsigned int hosts_list_length;
|
|
|
|
} SSHModePrivateData;
|
|
|
|
|
2016-01-05 18:49:13 +00:00
|
|
|
/**
|
2017-11-23 17:41:52 +00:00
|
|
|
* Name of the history file where previously chosen hosts are stored.
|
2016-01-05 18:49:13 +00:00
|
|
|
*/
|
2016-01-05 20:19:30 +00:00
|
|
|
#define SSH_CACHE_FILE "rofi-2.sshcache"
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2016-01-05 18:49:13 +00:00
|
|
|
/**
|
|
|
|
* Used in get_ssh() when splitting lines from the user's
|
|
|
|
* SSH config file into tokens.
|
|
|
|
*/
|
2015-01-14 21:14:15 +00:00
|
|
|
#define SSH_TOKEN_DELIM "= \t\r\n"
|
2015-01-14 10:23:14 +00:00
|
|
|
|
2016-01-05 20:19:30 +00:00
|
|
|
/**
|
2019-02-10 13:26:06 +00:00
|
|
|
* @param entry The host to connect too
|
2016-01-05 20:19:30 +00:00
|
|
|
*
|
|
|
|
* SSH into the selected host.
|
|
|
|
*
|
|
|
|
* @returns FALSE On failure, TRUE on success
|
|
|
|
*/
|
2020-02-02 12:56:37 +00:00
|
|
|
static int execshssh ( const SshEntry *entry )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
char **args = NULL;
|
|
|
|
int argsv = 0;
|
2019-02-10 12:55:09 +00:00
|
|
|
gchar *portstr = NULL;
|
|
|
|
if ( entry->port > 0 ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
portstr = g_strdup_printf ( "%d", entry->port );
|
2019-02-10 12:55:09 +00:00
|
|
|
}
|
|
|
|
helper_parse_setup ( config.ssh_command, &args, &argsv,
|
2020-02-02 12:56:37 +00:00
|
|
|
"{host}", entry->hostname,
|
|
|
|
"{port}", portstr,
|
|
|
|
(char *) 0 );
|
2019-02-10 12:55:09 +00:00
|
|
|
g_free ( portstr );
|
2016-01-05 20:19:30 +00:00
|
|
|
|
2019-02-10 12:55:09 +00:00
|
|
|
gsize l = strlen ( "Connecting to '' via rofi" ) + strlen ( entry->hostname ) + 1;
|
2017-06-02 12:57:28 +00:00
|
|
|
gchar *desc = g_newa ( gchar, l );
|
|
|
|
|
2019-02-10 12:55:09 +00:00
|
|
|
g_snprintf ( desc, l, "Connecting to '%s' via rofi", entry->hostname );
|
2017-06-02 12:57:28 +00:00
|
|
|
|
|
|
|
RofiHelperExecuteContext context = {
|
|
|
|
.name = "ssh",
|
|
|
|
.description = desc,
|
|
|
|
.command = "ssh",
|
|
|
|
};
|
2019-02-10 12:55:09 +00:00
|
|
|
return helper_execute ( NULL, args, "ssh ", entry->hostname, &context );
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
2016-01-05 20:19:30 +00:00
|
|
|
|
|
|
|
/**
|
2019-02-10 13:26:06 +00:00
|
|
|
* @param entry The host to connect too
|
2016-01-05 20:19:30 +00:00
|
|
|
*
|
|
|
|
* SSH into the selected host, if successful update history.
|
|
|
|
*/
|
2019-02-10 12:55:09 +00:00
|
|
|
static void exec_ssh ( const SshEntry *entry )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
if ( !( entry->hostname ) || !( entry->hostname[0] ) ) {
|
2016-01-05 20:19:30 +00:00
|
|
|
return;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2019-02-10 12:55:09 +00:00
|
|
|
if ( !execshssh ( entry ) ) {
|
2016-01-05 20:19:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2016-01-05 20:19:30 +00:00
|
|
|
// This happens in non-critical time (After launching app)
|
|
|
|
// It is allowed to be a bit slower.
|
|
|
|
char *path = g_build_filename ( cache_dir, SSH_CACHE_FILE, NULL );
|
2019-02-10 12:55:09 +00:00
|
|
|
// TODO update.
|
|
|
|
if ( entry->port > 0 ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
char *store = g_strdup_printf ( "%s\x1F%d", entry->hostname, entry->port );
|
2019-02-10 12:55:09 +00:00
|
|
|
history_set ( path, store );
|
|
|
|
g_free ( store );
|
2020-02-02 12:56:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-02-10 12:55:09 +00:00
|
|
|
history_set ( path, entry->hostname );
|
|
|
|
}
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( path );
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
2016-01-05 20:19:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param host The host to remove from history
|
|
|
|
*
|
|
|
|
* Remove host from history.
|
|
|
|
*/
|
|
|
|
static void delete_ssh ( const char *host )
|
2014-02-01 13:06:08 +00:00
|
|
|
{
|
2016-01-05 20:19:30 +00:00
|
|
|
if ( !host || !host[0] ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-01-05 20:19:30 +00:00
|
|
|
char *path = g_build_filename ( cache_dir, SSH_CACHE_FILE, NULL );
|
|
|
|
history_remove ( path, host );
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( path );
|
2014-02-01 13:06:08 +00:00
|
|
|
}
|
2015-03-29 10:27:00 +00:00
|
|
|
|
2015-09-20 14:57:47 +00:00
|
|
|
/**
|
2019-09-30 08:57:23 +00:00
|
|
|
* @param path Path of the known host file.
|
2015-09-20 14:57:47 +00:00
|
|
|
* @param retv list of hosts
|
2016-01-05 20:19:30 +00:00
|
|
|
* @param length pointer to length of list [in][out]
|
2015-09-20 14:57:47 +00:00
|
|
|
*
|
|
|
|
* Read 'known_hosts' file when entries are not hashsed.
|
|
|
|
*
|
2016-01-05 20:19:30 +00:00
|
|
|
* @returns updated list of hosts.
|
2015-09-20 14:57:47 +00:00
|
|
|
*/
|
2019-03-04 21:05:08 +00:00
|
|
|
static SshEntry *read_known_hosts_file ( const char *path, SshEntry * retv, unsigned int *length )
|
2015-09-20 14:57:47 +00:00
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
FILE *fd = fopen ( path, "r" );
|
2015-09-20 14:57:47 +00:00
|
|
|
if ( fd != NULL ) {
|
2016-04-10 12:30:13 +00:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
2015-09-20 14:57:47 +00:00
|
|
|
// Reading one line per time.
|
2016-04-10 12:30:13 +00:00
|
|
|
while ( getline ( &buffer, &buffer_length, fd ) > 0 ) {
|
2018-09-22 14:16:25 +00:00
|
|
|
// Strip whitespace.
|
2020-02-02 12:56:37 +00:00
|
|
|
char *start = g_strstrip ( &( buffer[0] ) );
|
2018-09-22 14:16:25 +00:00
|
|
|
// Find start.
|
2020-02-02 12:56:37 +00:00
|
|
|
if ( *start == '#' || *start == '@' ) {
|
2018-09-22 14:16:25 +00:00
|
|
|
// skip comments or cert-authority or revoked items.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( *start == '|' ) {
|
|
|
|
// Skip hashed hostnames.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Find end of hostname set.
|
2020-02-02 12:56:37 +00:00
|
|
|
char *end = strstr ( start, " " );
|
2018-09-22 14:16:25 +00:00
|
|
|
if ( end == NULL ) {
|
|
|
|
// Something is wrong.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*end = '\0';
|
|
|
|
char *sep = start;
|
2020-02-02 12:56:37 +00:00
|
|
|
start = strsep ( &sep, ", " );
|
|
|
|
while ( start ) {
|
2019-02-10 12:55:09 +00:00
|
|
|
int port = 0;
|
|
|
|
if ( start[0] == '[' ) {
|
|
|
|
start++;
|
2020-02-02 12:56:37 +00:00
|
|
|
char *end = strchr ( start, ']' );
|
|
|
|
if ( end[1] == ':' ) {
|
2019-02-10 12:55:09 +00:00
|
|
|
*end = '\0';
|
2019-04-20 08:46:44 +00:00
|
|
|
errno = 0;
|
2020-02-02 12:56:37 +00:00
|
|
|
gchar *endptr = NULL;
|
|
|
|
gint64 number = g_ascii_strtoll ( &( end[2] ), &endptr, 10 );
|
2019-05-10 18:29:11 +00:00
|
|
|
if ( errno != 0 ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
g_warning ( "Failed to parse port number: %s.", &( end[2] ) );
|
|
|
|
}
|
|
|
|
else if ( endptr == &( end[2] ) ) {
|
|
|
|
g_warning ( "Failed to parse port number: %s, invalid number.", &( end[2] ) );
|
|
|
|
}
|
|
|
|
else if ( number < 0 || number > 65535 ) {
|
|
|
|
g_warning ( "Failed to parse port number: %s, out of range.", &( end[2] ) );
|
|
|
|
}
|
|
|
|
else {
|
2019-04-20 08:46:44 +00:00
|
|
|
port = number;
|
2019-03-29 12:50:48 +00:00
|
|
|
}
|
2019-02-10 12:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-20 14:57:47 +00:00
|
|
|
// Is this host name already in the list?
|
|
|
|
// We often get duplicates in hosts file, so lets check this.
|
|
|
|
int found = 0;
|
|
|
|
for ( unsigned int j = 0; j < ( *length ); j++ ) {
|
2019-02-10 12:55:09 +00:00
|
|
|
if ( !g_ascii_strcasecmp ( start, retv[j].hostname ) ) {
|
2015-09-20 14:57:47 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !found ) {
|
|
|
|
// Add this host name to the list.
|
2019-02-10 12:55:09 +00:00
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( SshEntry ) );
|
|
|
|
retv[( *length )].hostname = g_strdup ( start );
|
2019-03-04 21:05:08 +00:00
|
|
|
retv[( *length )].port = port;
|
2019-02-10 12:55:09 +00:00
|
|
|
retv[( *length ) + 1].hostname = NULL;
|
|
|
|
retv[( *length ) + 1].port = 0;
|
2015-09-20 14:57:47 +00:00
|
|
|
( *length )++;
|
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
start = strsep ( &sep, ", " );
|
2015-09-20 14:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-10 12:30:13 +00:00
|
|
|
if ( buffer != NULL ) {
|
|
|
|
free ( buffer );
|
|
|
|
}
|
2015-09-26 19:06:36 +00:00
|
|
|
if ( fclose ( fd ) != 0 ) {
|
2017-04-15 09:58:49 +00:00
|
|
|
g_warning ( "Failed to close hosts file: '%s'", g_strerror ( errno ) );
|
2015-09-26 19:06:36 +00:00
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-03-04 21:05:08 +00:00
|
|
|
g_debug ( "Failed to open KnownHostFile: '%s'", path );
|
2015-09-20 14:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
2016-01-05 20:19:30 +00:00
|
|
|
|
2015-03-29 10:27:00 +00:00
|
|
|
/**
|
2016-01-05 20:19:30 +00:00
|
|
|
* @param retv The list of hosts to update.
|
|
|
|
* @param length The length of the list retv [in][out]
|
|
|
|
*
|
|
|
|
* Read `/etc/hosts` and appends them to the list retv
|
|
|
|
*
|
|
|
|
* @returns an updated list with the added hosts.
|
2015-03-29 10:27:00 +00:00
|
|
|
*/
|
2019-02-10 12:55:09 +00:00
|
|
|
static SshEntry *read_hosts_file ( SshEntry * retv, unsigned int *length )
|
2015-03-29 10:27:00 +00:00
|
|
|
{
|
|
|
|
// Read the hosts file.
|
|
|
|
FILE *fd = fopen ( "/etc/hosts", "r" );
|
|
|
|
if ( fd != NULL ) {
|
2016-04-10 12:30:13 +00:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
2015-03-29 10:27:00 +00:00
|
|
|
// Reading one line per time.
|
2016-04-10 12:30:13 +00:00
|
|
|
while ( getline ( &buffer, &buffer_length, fd ) > 0 ) {
|
2015-03-29 10:27:00 +00:00
|
|
|
// Evaluate one line.
|
2017-06-03 18:35:50 +00:00
|
|
|
unsigned int index = 0, ti = 0;
|
2015-03-29 10:27:00 +00:00
|
|
|
char *token = buffer;
|
|
|
|
|
|
|
|
// Tokenize it.
|
|
|
|
do {
|
|
|
|
char c = buffer[index];
|
|
|
|
// Break on space, tab, newline and \0.
|
|
|
|
if ( c == ' ' || c == '\t' || c == '\n' || c == '\0' || c == '#' ) {
|
|
|
|
buffer[index] = '\0';
|
|
|
|
// Ignore empty tokens
|
|
|
|
if ( token[0] != '\0' ) {
|
|
|
|
ti++;
|
|
|
|
// and first token.
|
|
|
|
if ( ti > 1 ) {
|
|
|
|
// Is this host name already in the list?
|
|
|
|
// We often get duplicates in hosts file, so lets check this.
|
|
|
|
int found = 0;
|
|
|
|
for ( unsigned int j = 0; j < ( *length ); j++ ) {
|
2019-02-10 12:55:09 +00:00
|
|
|
if ( !g_ascii_strcasecmp ( token, retv[j].hostname ) ) {
|
2015-03-29 10:27:00 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !found ) {
|
|
|
|
// Add this host name to the list.
|
|
|
|
retv = g_realloc ( retv,
|
2019-06-24 20:09:19 +00:00
|
|
|
( ( *length ) + 2 ) * sizeof ( SshEntry ) );
|
2019-02-10 12:55:09 +00:00
|
|
|
retv[( *length )].hostname = g_strdup ( token );
|
2019-05-10 18:29:11 +00:00
|
|
|
retv[( *length )].port = 0;
|
2019-02-10 12:55:09 +00:00
|
|
|
retv[( *length ) + 1].hostname = NULL;
|
2015-03-29 10:27:00 +00:00
|
|
|
( *length )++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set start to next element.
|
|
|
|
token = &buffer[index + 1];
|
|
|
|
// Everything after comment ignore.
|
|
|
|
if ( c == '#' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Skip to the next entry.
|
|
|
|
index++;
|
|
|
|
} while ( buffer[index] != '\0' && buffer[index] != '#' );
|
|
|
|
}
|
2016-04-10 12:30:13 +00:00
|
|
|
if ( buffer != NULL ) {
|
|
|
|
free ( buffer );
|
|
|
|
}
|
2015-07-31 08:21:32 +00:00
|
|
|
if ( fclose ( fd ) != 0 ) {
|
2017-04-15 09:58:49 +00:00
|
|
|
g_warning ( "Failed to close hosts file: '%s'", g_strerror ( errno ) );
|
2015-07-31 08:21:32 +00:00
|
|
|
}
|
2015-03-29 10:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:05:08 +00:00
|
|
|
static void add_known_hosts_file ( SSHModePrivateData *pd, const char *token )
|
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
GList *item = g_list_find_custom ( pd->user_known_hosts, token, (GCompareFunc) g_strcmp0 );
|
2019-03-04 21:05:08 +00:00
|
|
|
if ( item == NULL ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
g_debug ( "Add '%s' to UserKnownHost list", token );
|
2019-03-04 21:05:08 +00:00
|
|
|
pd->user_known_hosts = g_list_append ( pd->user_known_hosts, g_strdup ( token ) );
|
2020-02-02 12:56:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
g_debug ( "File '%s' already in UserKnownHostsFile list", token );
|
2019-03-04 21:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_ssh_config_file ( SSHModePrivateData *pd, const char *filename, SshEntry **retv, unsigned int *length, unsigned int num_favorites )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2017-03-17 13:07:11 +00:00
|
|
|
FILE *fd = fopen ( filename, "r" );
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2017-04-15 09:37:50 +00:00
|
|
|
g_debug ( "Parsing ssh config file: %s", filename );
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( fd != NULL ) {
|
2016-11-01 07:04:39 +00:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
|
|
|
char *strtok_pointer = NULL;
|
2016-04-10 12:30:13 +00:00
|
|
|
while ( getline ( &buffer, &buffer_length, fd ) > 0 ) {
|
2015-01-14 10:23:14 +00:00
|
|
|
// Each line is either empty, a comment line starting with a '#'
|
|
|
|
// character or of the form "keyword [=] arguments", where there may
|
|
|
|
// be multiple (possibly quoted) arguments separated by whitespace.
|
|
|
|
// The keyword is separated from its arguments by whitespace OR by
|
|
|
|
// optional whitespace and a '=' character.
|
2016-11-01 07:04:39 +00:00
|
|
|
char *token = strtok_r ( buffer, SSH_TOKEN_DELIM, &strtok_pointer );
|
2015-01-14 10:23:14 +00:00
|
|
|
// Skip empty lines and comment lines. Also skip lines where the
|
|
|
|
// keyword is not "Host".
|
2017-03-08 15:56:28 +00:00
|
|
|
if ( !token || *token == '#' ) {
|
2015-01-10 14:31:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
char *low_token = g_ascii_strdown ( token, -1 );
|
2019-04-24 19:59:36 +00:00
|
|
|
if ( g_strcmp0 ( low_token, "include" ) == 0 ) {
|
2017-03-08 15:56:28 +00:00
|
|
|
token = strtok_r ( NULL, SSH_TOKEN_DELIM, &strtok_pointer );
|
2017-04-15 09:37:50 +00:00
|
|
|
g_debug ( "Found Include: %s", token );
|
2017-03-17 13:07:11 +00:00
|
|
|
gchar *path = rofi_expand_path ( token );
|
2017-03-08 15:56:28 +00:00
|
|
|
gchar *full_path = NULL;
|
2017-03-17 13:07:11 +00:00
|
|
|
if ( !g_path_is_absolute ( path ) ) {
|
2017-03-08 15:56:28 +00:00
|
|
|
char *dirname = g_path_get_dirname ( filename );
|
|
|
|
full_path = g_build_filename ( dirname, path, NULL );
|
2017-03-17 13:07:11 +00:00
|
|
|
g_free ( dirname );
|
|
|
|
}
|
|
|
|
else {
|
2017-03-08 15:56:28 +00:00
|
|
|
full_path = g_strdup ( path );
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2017-05-11 16:30:44 +00:00
|
|
|
glob_t globbuf = { .gl_pathc = 0, .gl_pathv = NULL, .gl_offs = 0 };
|
2014-01-22 09:01:45 +00:00
|
|
|
|
2017-03-17 13:07:11 +00:00
|
|
|
if ( glob ( full_path, 0, NULL, &globbuf ) == 0 ) {
|
|
|
|
for ( size_t iter = 0; iter < globbuf.gl_pathc; iter++ ) {
|
2019-03-04 21:05:08 +00:00
|
|
|
parse_ssh_config_file ( pd, globbuf.gl_pathv[iter], retv, length, num_favorites );
|
2017-03-08 15:56:28 +00:00
|
|
|
}
|
2015-06-14 12:37:53 +00:00
|
|
|
}
|
2017-03-08 15:56:28 +00:00
|
|
|
globfree ( &globbuf );
|
2015-06-14 12:37:53 +00:00
|
|
|
|
2017-03-08 15:56:28 +00:00
|
|
|
g_free ( full_path );
|
|
|
|
g_free ( path );
|
|
|
|
}
|
2019-04-24 19:59:36 +00:00
|
|
|
else if ( g_strcmp0 ( low_token, "userknownhostsfile" ) == 0 ) {
|
2019-03-04 21:05:08 +00:00
|
|
|
while ( ( token = strtok_r ( NULL, SSH_TOKEN_DELIM, &strtok_pointer ) ) ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
g_debug ( "Found extra UserKnownHostsFile: %s", token );
|
2019-03-04 21:05:08 +00:00
|
|
|
add_known_hosts_file ( pd, token );
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 19:59:36 +00:00
|
|
|
else if ( g_strcmp0 ( low_token, "host" ) == 0 ) {
|
2017-03-08 15:56:28 +00:00
|
|
|
// Now we know that this is a "Host" line.
|
|
|
|
// The "Host" keyword is followed by one more host names separated
|
|
|
|
// by whitespace; while host names may be quoted with double quotes
|
|
|
|
// to represent host names containing spaces, we don't support this
|
|
|
|
// (how many host names contain spaces?).
|
|
|
|
while ( ( token = strtok_r ( NULL, SSH_TOKEN_DELIM, &strtok_pointer ) ) ) {
|
|
|
|
// We do not want to show wildcard entries, as you cannot ssh to them.
|
|
|
|
const char *const sep = "*?";
|
|
|
|
if ( *token == '!' || strpbrk ( token, sep ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If comment, skip from now on.
|
|
|
|
if ( *token == '#' ) {
|
2015-01-14 10:23:14 +00:00
|
|
|
break;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2014-01-21 13:56:25 +00:00
|
|
|
|
2017-03-08 15:56:28 +00:00
|
|
|
// Is this host name already in the history file?
|
|
|
|
// This is a nice little penalty, but doable? time will tell.
|
|
|
|
// given num_favorites is max 25.
|
|
|
|
int found = 0;
|
|
|
|
for ( unsigned int j = 0; j < num_favorites; j++ ) {
|
2019-02-10 12:55:09 +00:00
|
|
|
if ( !g_ascii_strcasecmp ( token, ( *retv )[j].hostname ) ) {
|
2017-03-08 15:56:28 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-21 09:35:59 +00:00
|
|
|
|
2017-03-08 15:56:28 +00:00
|
|
|
if ( found ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this host name to the list.
|
2019-05-10 18:29:11 +00:00
|
|
|
( *retv ) = g_realloc ( ( *retv ), ( ( *length ) + 2 ) * sizeof ( SshEntry ) );
|
2019-02-10 12:55:09 +00:00
|
|
|
( *retv )[( *length )].hostname = g_strdup ( token );
|
2019-05-10 18:29:11 +00:00
|
|
|
( *retv )[( *length )].port = 0;
|
2019-02-10 12:55:09 +00:00
|
|
|
( *retv )[( *length ) + 1].hostname = NULL;
|
2017-03-08 15:56:28 +00:00
|
|
|
( *length )++;
|
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
2019-04-24 19:59:36 +00:00
|
|
|
g_free ( low_token );
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
2016-04-10 12:30:13 +00:00
|
|
|
if ( buffer != NULL ) {
|
|
|
|
free ( buffer );
|
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2015-07-31 08:21:32 +00:00
|
|
|
if ( fclose ( fd ) != 0 ) {
|
2017-04-15 09:58:49 +00:00
|
|
|
g_warning ( "Failed to close ssh configuration file: '%s'", g_strerror ( errno ) );
|
2015-07-31 08:21:32 +00:00
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
2017-03-08 15:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-30 08:57:23 +00:00
|
|
|
* @param pd The plugin data handle
|
2017-03-08 15:56:28 +00:00
|
|
|
* @param length The number of found ssh hosts [out]
|
|
|
|
*
|
|
|
|
* Gets the list available SSH hosts.
|
|
|
|
*
|
2019-09-30 08:57:23 +00:00
|
|
|
* @returns an array of strings containing all the hosts.
|
2017-03-08 15:56:28 +00:00
|
|
|
*/
|
2019-03-04 21:05:08 +00:00
|
|
|
static SshEntry * get_ssh ( SSHModePrivateData *pd, unsigned int *length )
|
2017-03-08 15:56:28 +00:00
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
SshEntry *retv = NULL;
|
2017-03-08 15:56:28 +00:00
|
|
|
unsigned int num_favorites = 0;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
if ( g_get_home_dir () == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = g_build_filename ( cache_dir, SSH_CACHE_FILE, NULL );
|
2019-02-10 12:55:09 +00:00
|
|
|
char **h = history_get_list ( path, length );
|
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
retv = malloc ( ( *length ) * sizeof ( SshEntry ) );
|
|
|
|
for ( unsigned int i = 0; i < ( *length ); i++ ) {
|
|
|
|
int port = 0;
|
2019-03-08 12:46:10 +00:00
|
|
|
char *portstr = strchr ( h[i], '\x1F' );
|
2019-02-10 12:55:09 +00:00
|
|
|
if ( portstr != NULL ) {
|
|
|
|
*portstr = '\0';
|
2020-02-02 12:56:37 +00:00
|
|
|
errno = 0;
|
|
|
|
gchar *endptr = NULL;
|
|
|
|
gint64 number = g_ascii_strtoll ( &( portstr[1] ), &endptr, 10 );
|
2019-05-10 18:29:11 +00:00
|
|
|
if ( errno != 0 ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
g_warning ( "Failed to parse port number: %s.", &( portstr[1] ) );
|
|
|
|
}
|
|
|
|
else if ( endptr == &( portstr[1] ) ) {
|
|
|
|
g_warning ( "Failed to parse port number: %s, invalid number.", &( portstr[1] ) );
|
|
|
|
}
|
|
|
|
else if ( number < 0 || number > 65535 ) {
|
|
|
|
g_warning ( "Failed to parse port number: %s, out of range.", &( portstr[1] ) );
|
|
|
|
}
|
|
|
|
else {
|
2019-04-20 08:46:44 +00:00
|
|
|
port = number;
|
2019-05-10 18:29:11 +00:00
|
|
|
}
|
2019-02-10 12:55:09 +00:00
|
|
|
}
|
|
|
|
retv[i].hostname = h[i];
|
2020-02-02 12:56:37 +00:00
|
|
|
retv[i].port = port;
|
2019-02-10 12:55:09 +00:00
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
g_free ( h );
|
2019-02-10 12:55:09 +00:00
|
|
|
|
2017-03-08 15:56:28 +00:00
|
|
|
g_free ( path );
|
|
|
|
num_favorites = ( *length );
|
|
|
|
|
2019-03-04 21:05:08 +00:00
|
|
|
const char *hd = g_get_home_dir ();
|
|
|
|
path = g_build_filename ( hd, ".ssh", "config", NULL );
|
|
|
|
parse_ssh_config_file ( pd, path, &retv, length, num_favorites );
|
|
|
|
|
2017-03-08 15:56:28 +00:00
|
|
|
if ( config.parse_known_hosts == TRUE ) {
|
2019-03-04 21:05:08 +00:00
|
|
|
char *path = g_build_filename ( g_get_home_dir (), ".ssh", "known_hosts", NULL );
|
|
|
|
retv = read_known_hosts_file ( path, retv, length );
|
|
|
|
g_free ( path );
|
2020-02-02 12:56:37 +00:00
|
|
|
for ( GList *iter = g_list_first ( pd->user_known_hosts ); iter; iter = g_list_next ( iter ) ) {
|
|
|
|
char *path = rofi_expand_path ( (const char *) iter->data );
|
2020-08-28 14:41:59 +00:00
|
|
|
retv = read_known_hosts_file ( (const char *) path, retv, length );
|
2020-02-02 12:56:37 +00:00
|
|
|
g_free ( path );
|
2019-03-04 21:05:08 +00:00
|
|
|
}
|
2017-03-08 15:56:28 +00:00
|
|
|
}
|
|
|
|
if ( config.parse_hosts == TRUE ) {
|
|
|
|
retv = read_hosts_file ( retv, length );
|
|
|
|
}
|
|
|
|
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( path );
|
2014-01-21 09:01:55 +00:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2016-01-05 18:49:13 +00:00
|
|
|
/**
|
2016-01-05 20:19:30 +00:00
|
|
|
* @param sw Object handle to the SSH Mode object
|
2016-01-05 18:49:13 +00:00
|
|
|
*
|
2016-01-05 20:19:30 +00:00
|
|
|
* Initializes the SSH Mode private data object and
|
2016-01-05 18:49:13 +00:00
|
|
|
* loads the relevant ssh information.
|
|
|
|
*/
|
2016-01-08 08:16:59 +00:00
|
|
|
static int ssh_mode_init ( Mode *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
if ( mode_get_private_data ( sw ) == NULL ) {
|
2015-03-27 19:28:53 +00:00
|
|
|
SSHModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
2016-01-07 20:27:20 +00:00
|
|
|
mode_set_private_data ( sw, (void *) pd );
|
2019-03-04 21:05:08 +00:00
|
|
|
pd->hosts_list = get_ssh ( pd, &( pd->hosts_list_length ) );
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2016-01-08 08:16:59 +00:00
|
|
|
return TRUE;
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2014-02-01 22:04:45 +00:00
|
|
|
|
2016-01-05 18:49:13 +00:00
|
|
|
/**
|
2016-01-05 20:19:30 +00:00
|
|
|
* @param sw Object handle to the SSH Mode object
|
|
|
|
*
|
2016-01-05 18:49:13 +00:00
|
|
|
* Get the number of SSH entries.
|
|
|
|
*
|
|
|
|
* @returns the number of ssh entries.
|
|
|
|
*/
|
2015-11-25 08:26:38 +00:00
|
|
|
static unsigned int ssh_mode_get_num_entries ( const Mode *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
const SSHModePrivateData *rmpd = (const SSHModePrivateData *) mode_get_private_data ( sw );
|
2016-01-05 20:19:30 +00:00
|
|
|
return rmpd->hosts_list_length;
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2018-02-06 20:03:07 +00:00
|
|
|
/**
|
|
|
|
* @param sw Object handle to the SSH Mode object
|
|
|
|
*
|
|
|
|
* Cleanup the SSH Mode. Free all allocated memory and NULL the private data pointer.
|
|
|
|
*/
|
|
|
|
static void ssh_mode_destroy ( Mode *sw )
|
|
|
|
{
|
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) mode_get_private_data ( sw );
|
|
|
|
if ( rmpd != NULL ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
for ( unsigned int i = 0; i < rmpd->hosts_list_length; i++ ) {
|
|
|
|
g_free ( rmpd->hosts_list[i].hostname );
|
2019-02-10 12:55:09 +00:00
|
|
|
}
|
2019-03-04 21:05:08 +00:00
|
|
|
g_list_free_full ( rmpd->user_known_hosts, g_free );
|
2019-02-10 12:55:09 +00:00
|
|
|
g_free ( rmpd->hosts_list );
|
2018-02-06 20:03:07 +00:00
|
|
|
g_free ( rmpd );
|
|
|
|
mode_set_private_data ( sw, NULL );
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 18:49:13 +00:00
|
|
|
|
2016-01-05 20:19:30 +00:00
|
|
|
/**
|
|
|
|
* @param sw Object handle to the SSH Mode object
|
|
|
|
* @param mretv The menu return value.
|
|
|
|
* @param input Pointer to the user input string.
|
|
|
|
* @param selected_line the line selected by the user.
|
|
|
|
*
|
|
|
|
* Acts on the user interaction.
|
|
|
|
*
|
|
|
|
* @returns the next #ModeMode.
|
|
|
|
*/
|
2015-11-25 08:26:38 +00:00
|
|
|
static ModeMode ssh_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2015-11-25 08:26:38 +00:00
|
|
|
ModeMode retv = MODE_EXIT;
|
2016-01-07 20:27:20 +00:00
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) mode_get_private_data ( sw );
|
2020-09-08 15:30:03 +00:00
|
|
|
if ( ( mretv & MENU_OK ) && rmpd->hosts_list[selected_line].hostname != NULL ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
exec_ssh ( &( rmpd->hosts_list[selected_line] ) );
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
|
2019-02-10 12:55:09 +00:00
|
|
|
SshEntry entry = { .hostname = *input, .port = 0 };
|
|
|
|
exec_ssh ( &entry );
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2019-02-10 12:55:09 +00:00
|
|
|
else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->hosts_list[selected_line].hostname ) {
|
|
|
|
delete_ssh ( rmpd->hosts_list[selected_line].hostname );
|
2014-02-01 13:39:49 +00:00
|
|
|
// Stay
|
2014-07-21 19:39:24 +00:00
|
|
|
retv = RELOAD_DIALOG;
|
2018-02-06 20:03:07 +00:00
|
|
|
ssh_mode_destroy ( sw );
|
|
|
|
ssh_mode_init ( sw );
|
2021-06-01 08:44:23 +00:00
|
|
|
}
|
|
|
|
else if ( mretv & MENU_CUSTOM_COMMAND ) {
|
2021-04-10 21:04:05 +00:00
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
|
2016-01-05 20:19:30 +00:00
|
|
|
/**
|
|
|
|
* @param sw Object handle to the SSH Mode object
|
|
|
|
* @param selected_line The line to view
|
|
|
|
* @param state The state of the entry [out]
|
2017-03-11 12:40:05 +00:00
|
|
|
* @param attr_list List of extra rendering attributes to set [out]
|
2016-01-05 20:19:30 +00:00
|
|
|
* @param get_entry
|
|
|
|
*
|
|
|
|
* Gets the string as it should be displayed and the display state.
|
|
|
|
* If get_entry is FALSE only the state is set.
|
|
|
|
*
|
|
|
|
* @return the string as it should be displayed and the display state.
|
|
|
|
*/
|
2017-03-11 12:40:05 +00:00
|
|
|
static char *_get_display_value ( const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **attr_list, int get_entry )
|
2015-11-21 22:59:59 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) mode_get_private_data ( sw );
|
2019-02-10 12:55:09 +00:00
|
|
|
return get_entry ? g_strdup ( rmpd->hosts_list[selected_line].hostname ) : NULL;
|
2015-11-21 22:59:59 +00:00
|
|
|
}
|
2016-01-05 20:19:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param sw Object handle to the SSH Mode object
|
|
|
|
* @param tokens The set of tokens to match against
|
|
|
|
* @param index The index of the entry to match
|
|
|
|
*
|
|
|
|
* Match entry against the set of tokens.
|
|
|
|
*
|
|
|
|
* @returns TRUE if matches
|
|
|
|
*/
|
2017-09-29 06:32:09 +00:00
|
|
|
static int ssh_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
|
2015-03-31 20:45:02 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) mode_get_private_data ( sw );
|
2019-02-10 12:55:09 +00:00
|
|
|
return helper_token_match ( tokens, rmpd->hosts_list[index].hostname );
|
2015-11-21 22:59:59 +00:00
|
|
|
}
|
2016-01-07 20:27:20 +00:00
|
|
|
#include "mode-private.h"
|
2015-11-25 08:26:38 +00:00
|
|
|
Mode ssh_mode =
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
.name = "ssh",
|
2016-01-12 21:17:53 +00:00
|
|
|
.cfg_name_key = "display-ssh",
|
2016-01-07 20:27:20 +00:00
|
|
|
._init = ssh_mode_init,
|
|
|
|
._get_num_entries = ssh_mode_get_num_entries,
|
|
|
|
._result = ssh_mode_result,
|
|
|
|
._destroy = ssh_mode_destroy,
|
|
|
|
._token_match = ssh_token_match,
|
|
|
|
._get_display_value = _get_display_value,
|
|
|
|
._get_completion = NULL,
|
2016-05-26 06:39:33 +00:00
|
|
|
._preprocess_input = NULL,
|
2016-01-07 20:27:20 +00:00
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-03-27 19:28:53 +00:00
|
|
|
};
|
2020-10-12 19:39:36 +00:00
|
|
|
/**@}*/
|