2014-01-21 09:01:55 +00:00
|
|
|
/**
|
2014-03-01 16:27:52 +00:00
|
|
|
* rofi
|
2014-01-21 09:01:55 +00:00
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2015-02-12 21:34:06 +00:00
|
|
|
* Copyright 2013-2015 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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 <X11/X.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-02-12 21:26:28 +00:00
|
|
|
#include "dialogs/ssh-dialog.h"
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2014-05-13 08:45:59 +00:00
|
|
|
#define SSH_CACHE_FILE "rofi-2.sshcache"
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2015-01-14 10:23:14 +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
|
|
|
|
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;
|
2014-08-25 18:02:48 +00:00
|
|
|
g_spawn_async ( NULL, args, NULL,
|
|
|
|
G_SPAWN_SEARCH_PATH,
|
2014-08-27 17:44:15 +00:00
|
|
|
NULL, NULL, NULL, &error );
|
2014-04-22 09:11:46 +00:00
|
|
|
|
2014-09-03 11:07:26 +00:00
|
|
|
if ( error != NULL ) {
|
|
|
|
char *msg = g_strdup_printf ( "Failed to execute: 'ssh %s'\nError: '%s'", host,
|
|
|
|
error->message );
|
|
|
|
error_dialog ( msg );
|
|
|
|
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 );
|
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
|
|
|
|
|
|
|
return 0;
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
|
|
|
// execute sub-process
|
2014-03-22 20:04:19 +00:00
|
|
|
static pid_t exec_ssh ( const char *cmd )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( !cmd || !cmd[0] ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2014-08-25 18:02:48 +00:00
|
|
|
execshssh ( cmd );
|
2014-01-21 09:01:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This happens in non-critical time (After launching app)
|
|
|
|
* It is allowed to be a bit slower.
|
|
|
|
*/
|
2014-08-09 09:40:42 +00:00
|
|
|
char *path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
|
|
|
|
history_set ( path, cmd );
|
|
|
|
g_free ( path );
|
|
|
|
|
2014-08-25 18:02:48 +00:00
|
|
|
return 0;
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
2014-03-22 20:04:19 +00:00
|
|
|
static void delete_ssh ( const char *cmd )
|
2014-02-01 13:06:08 +00:00
|
|
|
{
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( !cmd || !cmd[0] ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-05-13 08:45:59 +00:00
|
|
|
char *path = NULL;
|
2014-08-09 09:40:42 +00:00
|
|
|
path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
|
|
|
|
history_remove ( path, cmd );
|
|
|
|
g_free ( path );
|
2014-02-01 13:06:08 +00:00
|
|
|
}
|
2014-08-28 22:01:36 +00:00
|
|
|
static int ssh_sort_func ( const void *a, const void *b )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2014-03-22 20:04:19 +00:00
|
|
|
const char *astr = *( const char * const * ) a;
|
|
|
|
const char *bstr = *( const char * const * ) b;
|
2014-08-07 19:42:16 +00:00
|
|
|
return g_utf8_collate ( astr, bstr );
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
2014-07-20 10:29:27 +00:00
|
|
|
static char ** get_ssh ( unsigned int *length )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
2015-02-19 12:22:10 +00:00
|
|
|
unsigned int num_favorites = 0;
|
|
|
|
char *path;
|
|
|
|
char **retv = NULL;
|
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
|
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 20:04:19 +00:00
|
|
|
fclose ( fd );
|
2014-01-21 09:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check this is still fast enough. (takes 1ms on laptop.)
|
2014-07-20 10:29:27 +00:00
|
|
|
if ( ( *length ) > num_favorites ) {
|
2014-08-28 22:01:36 +00:00
|
|
|
qsort ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), ssh_sort_func );
|
2014-03-11 19:16:44 +00:00
|
|
|
}
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( path );
|
2014-01-21 09:01:55 +00:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:43:26 +00:00
|
|
|
SwitcherMode ssh_switcher_dialog ( char **input, G_GNUC_UNUSED void *data )
|
2014-01-21 09:01:55 +00:00
|
|
|
{
|
|
|
|
SwitcherMode retv = MODE_EXIT;
|
|
|
|
// act as a launcher
|
2014-07-20 10:29:27 +00:00
|
|
|
unsigned int cmd_list_length = 0;
|
|
|
|
char **cmd_list = get_ssh ( &cmd_list_length );
|
2014-01-21 09:01:55 +00:00
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( cmd_list == NULL ) {
|
2014-08-09 09:40:42 +00:00
|
|
|
cmd_list = g_malloc_n ( 2, sizeof ( char * ) );
|
|
|
|
cmd_list[0] = g_strdup ( "No ssh hosts found" );
|
2014-01-21 09:01:55 +00:00
|
|
|
cmd_list[1] = NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-22 20:04:19 +00:00
|
|
|
int shift = 0;
|
2014-02-01 13:03:23 +00:00
|
|
|
int selected_line = 0;
|
2014-08-03 15:05:06 +00:00
|
|
|
int mretv = menu ( cmd_list, cmd_list_length, input, "ssh:",
|
2015-01-12 13:13:46 +00:00
|
|
|
NULL, &shift, token_match, NULL, &selected_line,
|
|
|
|
config.levenshtein_sort );
|
2014-02-01 22:04:45 +00:00
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( mretv == MENU_NEXT ) {
|
2014-01-22 08:24:31 +00:00
|
|
|
retv = NEXT_DIALOG;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2014-11-11 20:50:16 +00:00
|
|
|
else if ( mretv == MENU_PREVIOUS ) {
|
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
2014-07-21 21:19:45 +00:00
|
|
|
else if ( mretv == MENU_QUICK_SWITCH ) {
|
|
|
|
retv = selected_line;
|
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
exec_ssh ( cmd_list[selected_line] );
|
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
exec_ssh ( *input );
|
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
|
2014-02-01 13:06:08 +00:00
|
|
|
delete_ssh ( cmd_list[selected_line] );
|
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
|
|
|
}
|
|
|
|
|
2014-08-09 09:40:42 +00:00
|
|
|
g_strfreev ( cmd_list );
|
2014-01-21 09:01:55 +00:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|