summaryrefslogtreecommitdiffstats
path: root/source/ssh-dialog.c
blob: 8316eb805e9ad20d60b72a57a50544ad538a37ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
 * rofi
 *
 * MIT/X11 License
 * Copyright 2013-2014 Qball  Cow <qball@gmpclient.org>
 *
 * 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>
#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>
#include <errno.h>
#include <helper.h>

#include "rofi.h"
#include "history.h"
#include "ssh-dialog.h"
#ifdef TIMING
#include <time.h>
#endif

#define SSH_CACHE_FILE    "rofi-2.sshcache"

static inline int execshssh ( const char *host )
{
    /**
     * I am not happy about this code, it causes 7 mallocs and frees
     */
    char **args = NULL;
    int  argsv  = 0;
    helper_parse_setup ( config.ssh_command, &args, &argsv, "{host}", host, NULL );

    GError *error = NULL;
    g_spawn_async ( NULL, args, NULL,
                    G_SPAWN_SEARCH_PATH,
                    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 );
        // print error.
        g_error_free ( error );
    }
    // Free the args list.
    g_strfreev ( args );

    return 0;
}
// execute sub-process
static pid_t exec_ssh ( const char *cmd )
{
    if ( !cmd || !cmd[0] ) {
        return -1;
    }

    execshssh ( cmd );

    /**
     * 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;
}
static void delete_ssh ( const char *cmd )
{
    if ( !cmd || !cmd[0] ) {
        return;
    }
    char *path = NULL;
    path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
    history_remove ( path, cmd );
    g_free ( path );
}
static int ssh_sort_func ( const void *a, const void *b )
{
    const char *astr = *( const char * const * ) a;
    const char *bstr = *( const char * const * ) b;
    return g_utf8_collate ( astr, bstr );
}
static char ** get_ssh ( unsigned int *length )
{
    unsigned int    num_favorites = 0;
    char            *path;
    char            **retv = NULL;
#ifdef TIMING
    struct timespec start, stop;
    clock_gettime ( CLOCK_REALTIME, &start );
#endif

    if ( getenv ( "HOME" ) == NULL ) {
        return NULL;
    }

    path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
    retv = history_get_list ( path, length );
    g_free ( path );
    num_favorites = ( *length );

    FILE       *fd = NULL;
    const char *hd = getenv ( "HOME" );
    path = g_strdup_printf ( "%s/%s", hd, ".ssh/config" );
    fd   = fopen ( path, "r" );

    if ( fd != NULL ) {
        char buffer[1024];
        while ( fgets ( buffer, 1024, fd ) != NULL ) {
            if ( strncasecmp ( buffer, "Host", 4 ) == 0 && isspace ( buffer[4] ) ) {
                int start = 0, stop = 0;
                buffer[strlen ( buffer ) - 1] = '\0';

                for ( start = 4; isspace ( buffer[start] ); start++ ) {
                    ;
                }

                for ( stop = start; !isspace ( buffer[stop] ); stop++ ) {
                    // We do not want to show wildcard entries, as you cannot ssh to them.
                    if (  buffer[stop] == '?' || buffer[stop] == '*' ) {
                        stop = start;
                        break;
                    }
                }

                int found = 0;
                if ( start == stop ) {
                    continue;
                }

                // This is a nice little penalty, but doable? time will tell.
                // given num_favorites is max 25.
                for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
                    if ( strncasecmp ( &buffer[start], retv[j], stop - start ) == 0 ) {
                        found = 1;
                    }
                }

                if ( found == 1 ) {
                    continue;
                }

                retv                  = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
                retv[( *length )]     = strndup ( &buffer[start], stop - start );
                retv[( *length ) + 1] = NULL;
                ( *length )++;
            }
        }

        fclose ( fd );
    }

    // TODO: check this is still fast enough. (takes 1ms on laptop.)
    if ( ( *length ) > num_favorites ) {
        qsort ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), ssh_sort_func );
    }
    g_free ( path );
#ifdef TIMING
    clock_gettime ( CLOCK_REALTIME, &stop );

    if ( stop.tv_sec != start.tv_sec ) {
        stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
    }

    long diff = stop.tv_nsec - start.tv_nsec;
    printf ( "Time elapsed: %ld us\n", diff / 1000 );
#endif
    return retv;
}

SwitcherMode ssh_switcher_dialog ( char **input, G_GNUC_UNUSED void *data )
{
    SwitcherMode retv = MODE_EXIT;
    // act as a launcher
    unsigned int cmd_list_length = 0;
    char         **cmd_list      = get_ssh ( &cmd_list_length );

    if ( cmd_list == NULL ) {
        cmd_list    = g_malloc_n ( 2, sizeof ( char * ) );
        cmd_list[0] = g_strdup ( "No ssh hosts found" );
        cmd_list[1] = NULL;
    }

    int shift         = 0;
    int selected_line = 0;
    int mretv         = menu ( cmd_list, cmd_list_length, input, "ssh:",
                               NULL, &shift, token_match, NULL, &selected_line, config.levenshtein_sort );

    if ( mretv == MENU_NEXT ) {
        retv = NEXT_DIALOG;
    }
    else if ( mretv == MENU_PREVIOUS ) {
        retv = PREVIOUS_DIALOG;
    }
    else if ( mretv == MENU_QUICK_SWITCH ) {
        retv = selected_line;
    }
    else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {