1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-25 13:55:34 -05:00
rofi/source/dialogs/ssh.c

346 lines
11 KiB
C
Raw Normal View History

2014-01-21 04:01:55 -05:00
/**
2014-03-01 11:27:52 -05:00
* rofi
2014-01-21 04:01:55 -05:00
*
* MIT/X11 License
2015-04-14 16:12:21 -04:00
* Copyright 2013-2015 Qball Cow <qball@gmpclient.org>
2014-01-21 04:01:55 -05: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.
*
*/
#include <config.h>
2014-01-21 04:01:55 -05: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 08:56:25 -05:00
#include <errno.h>
#include <helper.h>
2014-01-21 04:01:55 -05:00
2014-03-12 03:41:38 -04:00
#include "rofi.h"
#include "history.h"
2015-03-25 03:36:19 -04:00
#include "dialogs/ssh.h"
2014-01-21 04:01:55 -05:00
#define SSH_CACHE_FILE "rofi-2.sshcache"
2014-01-21 04:01:55 -05:00
// Used in get_ssh() when splitting lines from the user's
// SSH config file into tokens.
2015-01-14 16:14:15 -05:00
#define SSH_TOKEN_DELIM "= \t\r\n"
2014-03-22 16:04:19 -04:00
static inline int execshssh ( const char *host )
2014-01-21 04:01:55 -05:00
{
char **args = NULL;
int argsv = 0;
helper_parse_setup ( config.ssh_command, &args, &argsv, "{host}", host, NULL );
2014-08-27 13:44:15 -04:00
GError *error = NULL;
g_spawn_async ( NULL, args, NULL,
G_SPAWN_SEARCH_PATH,
2014-08-27 13:44:15 -04:00
NULL, NULL, NULL, &error );
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 13:44:15 -04:00
// print error.
g_error_free ( error );
2014-08-27 13:44:15 -04:00
}
// Free the args list.
g_strfreev ( args );
return 0;
2014-01-21 04:01:55 -05:00
}
// execute sub-process
2014-03-22 16:04:19 -04:00
static pid_t exec_ssh ( const char *cmd )
2014-01-21 04:01:55 -05:00
{
2014-06-04 15:29:23 -04:00
if ( !cmd || !cmd[0] ) {
2014-03-22 16:04:19 -04:00
return -1;
}
2014-01-21 04:01:55 -05:00
execshssh ( cmd );
2014-01-21 04:01:55 -05:00
/**
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
char *path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
history_set ( path, cmd );
g_free ( path );
return 0;
2014-01-21 04:01:55 -05:00
}
2014-03-22 16:04:19 -04:00
static void delete_ssh ( const char *cmd )
2014-02-01 08:06:08 -05:00
{
2014-06-04 15:29:23 -04:00
if ( !cmd || !cmd[0] ) {
2014-03-22 16:04:19 -04:00
return;
}
char *path = NULL;
path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
history_remove ( path, cmd );
g_free ( path );
2014-02-01 08:06:08 -05:00
}
2014-08-28 18:01:36 -04:00
static int ssh_sort_func ( const void *a, const void *b )
2014-01-21 04:01:55 -05:00
{
2014-03-22 16:04:19 -04:00
const char *astr = *( const char * const * ) a;
const char *bstr = *( const char * const * ) b;
return g_utf8_collate ( astr, bstr );
2014-01-21 04:01:55 -05:00
}
2015-03-29 06:27:00 -04:00
/**
* Read /etc/hosts
*/
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] != '#' );
}
fclose ( fd );
}
return retv;
}
2015-03-27 15:28:53 -04:00
static char ** get_ssh ( unsigned int *length )
2014-01-21 04:01:55 -05:00
{
2015-03-27 15:28:53 -04:00
char **retv = NULL;
2015-02-19 07:22:10 -05:00
unsigned int num_favorites = 0;
char *path;
2014-01-21 04:01:55 -05:00
2014-06-04 15:29:23 -04:00
if ( getenv ( "HOME" ) == NULL ) {
2014-03-22 16:04:19 -04:00
return NULL;
}
2014-01-21 04:01:55 -05: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 16:11:42 -04:00
2015-03-29 06:27:00 -04:00
if ( config.parse_hosts == TRUE ) {
retv = read_hosts_file ( retv, length );
}
2014-05-22 03:33:32 -04:00
FILE *fd = NULL;
2014-03-22 16:04:19 -04:00
const char *hd = getenv ( "HOME" );
path = g_strdup_printf ( "%s/%s", hd, ".ssh/config" );
fd = fopen ( path, "r" );
2014-01-21 04:01:55 -05:00
2014-06-04 15:29:23 -04:00
if ( fd != NULL ) {
char buffer[1024];
2015-01-14 16:14:15 -05:00
while ( fgets ( buffer, sizeof ( buffer ), fd ) ) {
// 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 16:14:15 -05:00
char *token = strtok ( buffer, SSH_TOKEN_DELIM );
// Skip empty lines and comment lines. Also skip lines where the
// keyword is not "Host".
2015-01-14 16:14:15 -05:00
if ( !token || *token == '#' || g_ascii_strcasecmp ( token, "Host" ) ) {
2015-01-10 09:31:13 -05:00
continue;
}
2014-01-21 04:13:42 -05: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 16:14:15 -05:00
while ( ( token = strtok ( NULL, SSH_TOKEN_DELIM ) ) ) {
// We do not want to show wildcard entries, as you cannot ssh to them.
2015-01-14 16:14:15 -05:00
if ( *token == '!' || strpbrk ( token, "*?" ) ) {
2014-03-22 16:04:19 -04:00
continue;
}
2014-01-22 04:01:45 -05:00
2015-01-14 09:52:40 -05:00
// Is this host name already in the history file?
2014-01-21 04:35:59 -05:00
// 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++ ) {
2015-01-14 16:14:15 -05:00
if ( !g_ascii_strcasecmp ( token, retv[j] ) ) {
2014-03-22 16:04:19 -04:00
found = 1;
break;
2014-03-22 16:04:19 -04:00
}
2014-01-21 04:35:59 -05:00
}
2014-01-21 08:56:25 -05:00
if ( found ) {
2014-03-22 16:04:19 -04:00
continue;
}
2014-01-21 04:35:59 -05:00
// 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 )++;
2014-01-21 04:01:55 -05:00
}
}
2014-03-22 16:04:19 -04:00
fclose ( fd );
2014-01-21 04:01:55 -05:00
}
// TODO: check this is still fast enough. (takes 1ms on laptop.)
if ( ( *length ) > num_favorites ) {
2014-08-28 18:01:36 -04:00
qsort ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), ssh_sort_func );
2014-03-11 15:16:44 -04:00
}
g_free ( path );
2014-01-21 04:01:55 -05:00
return retv;
}
2015-03-27 15:28:53 -04:00
typedef struct _SSHModePrivateData
2014-01-21 04:01:55 -05:00
{
2015-03-27 15:28:53 -04:00
unsigned int id;
char **cmd_list;
unsigned int cmd_list_length;
} SSHModePrivateData;
2014-01-21 04:01:55 -05:00
2015-03-27 15:28:53 -04:00
static void ssh_mode_init ( Switcher *sw )
{
if ( sw->private_data == NULL ) {
SSHModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
sw->private_data = (void *) pd;
}
}
2015-03-27 15:28:53 -04:00
static char ** ssh_mode_get_data ( unsigned int *length, Switcher *sw )
{
SSHModePrivateData *rmpd = (SSHModePrivateData *) sw->private_data;
if ( rmpd->cmd_list == NULL ) {
rmpd->cmd_list_length = 0;
rmpd->cmd_list = get_ssh ( &( rmpd->cmd_list_length ) );
}
*length = rmpd->cmd_list_length;
return rmpd->cmd_list;
}
static SwitcherMode ssh_mode_result ( int mretv, char **input, unsigned int selected_line, Switcher *sw )
{
SwitcherMode retv = MODE_EXIT;
SSHModePrivateData *rmpd = (SSHModePrivateData *) sw->private_data;
if ( mretv & MENU_NEXT ) {
2014-01-22 03:24:31 -05:00
retv = NEXT_DIALOG;
2014-03-22 16:04:19 -04:00
}
2015-03-27 15:28:53 -04:00
else if ( mretv & MENU_PREVIOUS ) {
2014-11-11 15:50:16 -05:00
retv = PREVIOUS_DIALOG;
}
2015-03-27 15:28:53 -04:00
else if ( mretv & MENU_QUICK_SWITCH ) {
retv = ( mretv & MENU_LOWER_MASK );
}
2015-03-27 15:28:53 -04:00
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
exec_ssh ( rmpd->cmd_list[selected_line] );
2014-03-22 16:04:19 -04:00
}
2015-03-27 15:28:53 -04:00
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
2014-03-22 16:04:19 -04:00
exec_ssh ( *input );
}
2015-03-27 15:28:53 -04:00
else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->cmd_list[selected_line] ) {
delete_ssh ( rmpd->cmd_list[selected_line] );
2014-02-01 08:39:49 -05:00
// Stay
retv = RELOAD_DIALOG;
2014-01-21 04:01:55 -05:00
}
return retv;
}
2015-03-27 15:28:53 -04:00
static void ssh_mode_destroy ( Switcher *sw )
{
SSHModePrivateData *rmpd = (SSHModePrivateData *) sw->private_data;
if ( rmpd != NULL ) {
g_strfreev ( rmpd->cmd_list );
g_free ( rmpd );
sw->private_data = NULL;
}
}
static const char *mgrv ( unsigned int selected_line, void *sw, G_GNUC_UNUSED int *state )
{
SSHModePrivateData *rmpd = ( (Switcher *) sw )->private_data;
return rmpd->cmd_list[selected_line];
}
2015-03-27 15:28:53 -04:00
Switcher ssh_mode =
{
.name = "ssh",
.tb = NULL,
.keycfg = NULL,
.keystr = NULL,
.modmask = AnyModifier,
.init = ssh_mode_init,
.get_data = ssh_mode_get_data,
.result = ssh_mode_result,
.destroy = ssh_mode_destroy,
.token_match = token_match,
.mgrv = mgrv,
2015-03-27 15:28:53 -04:00
.private_data = NULL,
.free = NULL
};