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
|
2015-12-31 23:27:00 +00:00
|
|
|
* Copyright 2013-2016 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
|
|
|
|
* @{
|
|
|
|
*/
|
2014-08-22 07:26:46 +00:00
|
|
|
#include <config.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>
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2014-03-12 07:41:38 +00:00
|
|
|
#include "rofi.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
|
|
|
|
2016-01-05 18:49:13 +00:00
|
|
|
/**
|
|
|
|
* Name of the history file where previously choosen hosts are stored.
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* @param host The host to connect too
|
|
|
|
*
|
|
|
|
* SSH into the selected host.
|
|
|
|
*
|
|
|
|
* @returns FALSE On failure, TRUE on success
|
|
|
|
*/
|
2014-03-22 20:04:19 +00:00
|
|
|
static inline int execshssh ( const char *host )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2014-09-03 11:07:26 +00:00
|
|
|
char **args = NULL;
|
|
|
|
int argsv = 0;
|
|
|
|
helper_parse_setup ( config.ssh_command, &args, &argsv, "{host}", host, NULL );
|
2014-08-25 18:02:48 +00:00
|
|
|
|
2014-08-27 17:44:15 +00:00
|
|
|
GError *error = NULL;
|
2015-09-19 18:59:50 +00:00
|
|
|
g_spawn_async ( NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error );
|
2014-04-22 09:11:46 +00:00
|
|
|
|
2014-09-03 11:07:26 +00:00
|
|
|
if ( error != NULL ) {
|
2015-09-19 18:59:50 +00:00
|
|
|
char *msg = g_strdup_printf ( "Failed to execute: 'ssh %s'\nError: '%s'", host, error->message );
|
2015-06-08 21:22:35 +00:00
|
|
|
error_dialog ( msg, FALSE );
|
2014-09-03 11:07:26 +00:00
|
|
|
g_free ( msg );
|
2014-08-27 17:44:15 +00:00
|
|
|
// print error.
|
2014-09-03 11:07:26 +00:00
|
|
|
g_error_free ( error );
|
2016-01-05 20:19:30 +00:00
|
|
|
|
|
|
|
g_strfreev ( args );
|
|
|
|
return FALSE;
|
2014-08-27 17:44:15 +00:00
|
|
|
}
|
2014-04-22 09:11:46 +00:00
|
|
|
// Free the args list.
|
2014-08-09 09:40:42 +00:00
|
|
|
g_strfreev ( args );
|
2014-08-25 18:02:48 +00:00
|
|
|
|
2016-01-05 20:19:30 +00:00
|
|
|
return TRUE;
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
2016-01-05 20:19:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param host The host to connect too
|
|
|
|
*
|
|
|
|
* SSH into the selected host, if successful update history.
|
|
|
|
*/
|
|
|
|
static void exec_ssh ( const char *host )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2016-01-05 20:19:30 +00:00
|
|
|
if ( !host || !host[0] ) {
|
|
|
|
return;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2016-01-05 20:19:30 +00:00
|
|
|
if ( !execshssh ( host ) ) {
|
|
|
|
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 );
|
|
|
|
history_set ( path, host );
|
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
|
|
|
/**
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
static char **read_known_hosts_file ( char ** retv, unsigned int *length )
|
|
|
|
{
|
|
|
|
char *path = g_build_filename ( g_getenv ( "HOME" ), ".ssh", "known_hosts", NULL );
|
|
|
|
FILE *fd = fopen ( path, "r" );
|
|
|
|
if ( fd != NULL ) {
|
|
|
|
char buffer[1024];
|
|
|
|
// Reading one line per time.
|
|
|
|
while ( fgets ( buffer, sizeof ( buffer ), fd ) ) {
|
|
|
|
char *sep = strstr ( buffer, "," );
|
|
|
|
|
|
|
|
if ( sep != NULL ) {
|
|
|
|
*sep = '\0';
|
|
|
|
// 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++ ) {
|
|
|
|
if ( !g_ascii_strcasecmp ( buffer, retv[j] ) ) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !found ) {
|
|
|
|
// Add this host name to the list.
|
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
|
|
|
retv[( *length )] = g_strdup ( buffer );
|
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
( *length )++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-26 19:06:36 +00:00
|
|
|
if ( fclose ( fd ) != 0 ) {
|
|
|
|
fprintf ( stderr, "Failed to close hosts file: '%s'\n", strerror ( errno ) );
|
|
|
|
}
|
2015-09-20 14:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free ( path );
|
|
|
|
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
|
|
|
*/
|
|
|
|
static char **read_hosts_file ( char ** retv, unsigned int *length )
|
|
|
|
{
|
|
|
|
// Read the hosts file.
|
|
|
|
FILE *fd = fopen ( "/etc/hosts", "r" );
|
|
|
|
if ( fd != NULL ) {
|
|
|
|
char buffer[1024];
|
|
|
|
// Reading one line per time.
|
|
|
|
while ( fgets ( buffer, sizeof ( buffer ), fd ) ) {
|
|
|
|
// Evaluate one line.
|
|
|
|
unsigned int index = 0, ti = 0;
|
|
|
|
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++ ) {
|
|
|
|
if ( !g_ascii_strcasecmp ( token, retv[j] ) ) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !found ) {
|
|
|
|
// Add this host name to the list.
|
|
|
|
retv = g_realloc ( retv,
|
|
|
|
( ( *length ) + 2 ) * sizeof ( char* ) );
|
|
|
|
retv[( *length )] = g_strdup ( token );
|
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
( *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] != '#' );
|
|
|
|
}
|
2015-07-31 08:21:32 +00:00
|
|
|
if ( fclose ( fd ) != 0 ) {
|
|
|
|
fprintf ( stderr, "Failed to close hosts file: '%s'\n", strerror ( errno ) );
|
|
|
|
}
|
2015-03-29 10:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2016-01-05 20:19:30 +00:00
|
|
|
/**
|
|
|
|
* @param length The number of found ssh hosts [out]
|
|
|
|
*
|
|
|
|
* Gets the list available SSH hosts.
|
|
|
|
*
|
|
|
|
* @return an array of strings containing all the hosts.
|
|
|
|
*/
|
2015-03-27 19:28:53 +00:00
|
|
|
static char ** get_ssh ( unsigned int *length )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
char **retv = NULL;
|
2015-02-19 12:22:10 +00:00
|
|
|
unsigned int num_favorites = 0;
|
|
|
|
char *path;
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( getenv ( "HOME" ) == NULL ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2014-08-09 09:40:42 +00:00
|
|
|
path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
|
|
|
|
retv = history_get_list ( path, length );
|
|
|
|
g_free ( path );
|
|
|
|
num_favorites = ( *length );
|
2014-05-13 20:11:42 +00:00
|
|
|
|
2015-10-31 16:26:13 +00:00
|
|
|
if ( config.parse_known_hosts == TRUE ) {
|
|
|
|
retv = read_known_hosts_file ( retv, length );
|
|
|
|
}
|
2015-03-29 10:27:00 +00:00
|
|
|
if ( config.parse_hosts == TRUE ) {
|
|
|
|
retv = read_hosts_file ( retv, length );
|
|
|
|
}
|
|
|
|
|
2014-05-22 07:33:32 +00:00
|
|
|
FILE *fd = NULL;
|
2014-03-22 20:04:19 +00:00
|
|
|
const char *hd = getenv ( "HOME" );
|
2014-08-09 09:40:42 +00:00
|
|
|
path = g_strdup_printf ( "%s/%s", hd, ".ssh/config" );
|
|
|
|
fd = fopen ( path, "r" );
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( fd != NULL ) {
|
2014-05-13 08:45:59 +00:00
|
|
|
char buffer[1024];
|
2015-01-14 21:14:15 +00:00
|
|
|
while ( fgets ( buffer, sizeof ( buffer ), fd ) ) {
|
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.
|
2015-01-14 21:14:15 +00:00
|
|
|
char *token = strtok ( buffer, SSH_TOKEN_DELIM );
|
2015-01-14 10:23:14 +00:00
|
|
|
|
|
|
|
// Skip empty lines and comment lines. Also skip lines where the
|
|
|
|
// keyword is not "Host".
|
2015-01-14 21:14:15 +00:00
|
|
|
if ( !token || *token == '#' || g_ascii_strcasecmp ( token, "Host" ) ) {
|
2015-01-10 14:31:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-21 09:13:42 +00:00
|
|
|
|
2015-01-14 10:23:14 +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?).
|
2015-01-14 21:14:15 +00:00
|
|
|
while ( ( token = strtok ( NULL, SSH_TOKEN_DELIM ) ) ) {
|
2015-01-14 10:23:14 +00:00
|
|
|
// We do not want to show wildcard entries, as you cannot ssh to them.
|
2015-01-14 21:14:15 +00:00
|
|
|
if ( *token == '!' || strpbrk ( token, "*?" ) ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-22 09:01:45 +00:00
|
|
|
|
2015-06-14 12:37:53 +00:00
|
|
|
// If comment, skip from now on.
|
|
|
|
if ( *token == '#' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-14 14:52:40 +00:00
|
|
|
// Is this host name already in the history file?
|
2014-01-21 09:35:59 +00:00
|
|
|
// This is a nice little penalty, but doable? time will tell.
|
|
|
|
// given num_favorites is max 25.
|
2015-01-14 10:23:14 +00:00
|
|
|
int found = 0;
|
|
|
|
for ( unsigned int j = 0; j < num_favorites; j++ ) {
|
2015-01-14 21:14:15 +00:00
|
|
|
if ( !g_ascii_strcasecmp ( token, retv[j] ) ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
found = 1;
|
2015-01-14 10:23:14 +00:00
|
|
|
break;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2014-01-21 09:35:59 +00:00
|
|
|
}
|
2014-01-21 13:56:25 +00:00
|
|
|
|
2015-01-14 10:23:14 +00:00
|
|
|
if ( found ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-21 09:35:59 +00:00
|
|
|
|
2015-01-14 10:23:14 +00:00
|
|
|
// Add this host name to the list.
|
2014-08-09 09:40:42 +00:00
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
2015-01-14 15:02:37 +00:00
|
|
|
retv[( *length )] = g_strdup ( token );
|
2014-08-09 09:40:42 +00:00
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
( *length )++;
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 08:21:32 +00:00
|
|
|
if ( fclose ( fd ) != 0 ) {
|
|
|
|
fprintf ( stderr, "Failed to close ssh configuration file: '%s'\n", strerror ( errno ) );
|
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* The internal data structure holding the private data of the SSH Mode.
|
|
|
|
*/
|
2015-03-27 19:28:53 +00:00
|
|
|
typedef struct _SSHModePrivateData
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2016-01-05 18:49:13 +00:00
|
|
|
/** List if available ssh hosts.*/
|
2016-01-05 20:19:30 +00:00
|
|
|
char **hosts_list;
|
|
|
|
/** Length of the #hosts_list.*/
|
|
|
|
unsigned int hosts_list_length;
|
2015-03-27 19:28:53 +00:00
|
|
|
} SSHModePrivateData;
|
2014-01-21 09:01:55 +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
|
|
|
*
|
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.
|
|
|
|
*/
|
2015-11-25 08:26:38 +00:00
|
|
|
static void ssh_mode_init ( Mode *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
|
|
|
if ( sw->private_data == NULL ) {
|
|
|
|
SSHModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
|
|
|
sw->private_data = (void *) pd;
|
2016-01-05 20:19:30 +00:00
|
|
|
pd->hosts_list = get_ssh ( &( pd->hosts_list_length ) );
|
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
|
|
|
{
|
2015-11-23 20:52:55 +00:00
|
|
|
const SSHModePrivateData *rmpd = (const SSHModePrivateData *) sw->private_data;
|
2016-01-05 20:19:30 +00:00
|
|
|
return rmpd->hosts_list_length;
|
2015-03-27 19:28:53 +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
|
|
|
|
* @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;
|
2015-03-27 19:28:53 +00:00
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) sw->private_data;
|
|
|
|
if ( mretv & MENU_NEXT ) {
|
2014-01-22 08:24:31 +00:00
|
|
|
retv = NEXT_DIALOG;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
else if ( mretv & MENU_PREVIOUS ) {
|
2014-11-11 20:50:16 +00:00
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
else if ( mretv & MENU_QUICK_SWITCH ) {
|
2015-05-03 11:04:03 +00:00
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2014-07-21 21:19:45 +00:00
|
|
|
}
|
2016-01-05 20:19:30 +00:00
|
|
|
else if ( ( mretv & MENU_OK ) && rmpd->hosts_list[selected_line] != NULL ) {
|
|
|
|
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' ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
exec_ssh ( *input );
|
|
|
|
}
|
2016-01-05 20:19:30 +00:00
|
|
|
else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->hosts_list[selected_line] ) {
|
|
|
|
delete_ssh ( rmpd->hosts_list[selected_line] );
|
|
|
|
g_strfreev ( rmpd->hosts_list );
|
|
|
|
rmpd->hosts_list_length = 0;
|
|
|
|
rmpd->hosts_list = NULL;
|
2014-02-01 13:39:49 +00:00
|
|
|
// Stay
|
2014-07-21 19:39:24 +00:00
|
|
|
retv = RELOAD_DIALOG;
|
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
|
|
|
|
*
|
|
|
|
* Cleanup the SSH Mode. Free all allocated memory and NULL the private data pointer.
|
|
|
|
*/
|
2015-11-25 08:26:38 +00:00
|
|
|
static void ssh_mode_destroy ( Mode *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) sw->private_data;
|
|
|
|
if ( rmpd != NULL ) {
|
2016-01-05 20:19:30 +00:00
|
|
|
g_strfreev ( rmpd->hosts_list );
|
2015-03-27 19:28:53 +00:00
|
|
|
g_free ( rmpd );
|
|
|
|
sw->private_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
* @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.
|
|
|
|
*/
|
2015-11-25 08:26:38 +00:00
|
|
|
static char *mgrv ( const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, int get_entry )
|
2015-11-21 22:59:59 +00:00
|
|
|
{
|
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) sw->private_data;
|
2016-01-05 20:19:30 +00:00
|
|
|
return get_entry ? g_strdup ( rmpd->hosts_list[selected_line] ) : 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 not_ascii If the entry is pure-ascii
|
|
|
|
* @param case_sensitive If the entry should be matched case sensitive
|
|
|
|
* @param index The index of the entry to match
|
|
|
|
*
|
|
|
|
* Match entry against the set of tokens.
|
|
|
|
*
|
|
|
|
* @returns TRUE if matches
|
|
|
|
*/
|
2015-11-25 08:26:38 +00:00
|
|
|
static int ssh_token_match ( const Mode *sw, char **tokens, int not_ascii, int case_sensitive, unsigned int index )
|
2015-03-31 20:45:02 +00:00
|
|
|
{
|
2015-11-21 22:59:59 +00:00
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) sw->private_data;
|
2016-01-05 20:19:30 +00:00
|
|
|
return token_match ( tokens, rmpd->hosts_list[index], not_ascii, case_sensitive );
|
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 index The index of the entry to match
|
|
|
|
*
|
|
|
|
* Check if the selected entry contains non-ascii symbols.
|
|
|
|
*
|
|
|
|
* @returns TRUE if string contains non-ascii symbols
|
|
|
|
*/
|
2015-11-25 08:26:38 +00:00
|
|
|
static int ssh_is_not_ascii ( const Mode *sw, unsigned int index )
|
2015-11-21 22:59:59 +00:00
|
|
|
{
|
|
|
|
SSHModePrivateData *rmpd = (SSHModePrivateData *) sw->private_data;
|
2016-01-05 20:19:30 +00:00
|
|
|
return !g_str_is_ascii ( rmpd->hosts_list[index] );
|
2015-03-31 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
Mode ssh_mode =
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2015-11-21 22:59:59 +00:00
|
|
|
.name = "ssh",
|
|
|
|
.keycfg = NULL,
|
|
|
|
.keystr = NULL,
|
|
|
|
.modmask = AnyModifier,
|
|
|
|
.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,
|
|
|
|
.mgrv = mgrv,
|
2015-11-24 12:59:35 +00:00
|
|
|
.get_completion = NULL,
|
2015-11-21 22:59:59 +00:00
|
|
|
.is_not_ascii = ssh_is_not_ascii,
|
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-03-27 19:28:53 +00:00
|
|
|
};
|
2016-01-05 18:49:13 +00:00
|
|
|
/*@}*/
|