File: sshcmd.c

package info (click to toggle)
pdsh 2.27-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,448 kB
  • sloc: ansic: 19,331; sh: 10,828; makefile: 508; perl: 163
file content (380 lines) | stat: -rw-r--r-- 9,666 bytes parent folder | download
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*****************************************************************************\
 *  $Id$
 *****************************************************************************
 *  Copyright (C) 2001-2006 The Regents of the University of California.
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 *  Written by Jim Garlick <garlick@llnl.gov>.
 *  UCRL-CODE-2003-005.
 *  
 *  This file is part of Pdsh, a parallel remote shell program.
 *  For details, see <http://www.llnl.gov/linux/pdsh/>.
 *  
 *  Pdsh is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *  
 *  Pdsh is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 *  details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with Pdsh; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
\*****************************************************************************/

/*
 * This is an rcmd() replacement originally by Chris Siebenmann 
 * <cks@utcc.utoronto.ca>.  There was no copyright information on the original.
 * If this finds its way back to the original author please let me know if
 * you would like this header block changed...
 * 
 * Brought in to pdsh from USC rdist -jg
 * Changes:
 * - added fd2p arg handling
 * - changed name, func prototype, and added sshcmd() and rshcmd() wrappers
 * - use 'err' for output
 * - unset DISPLAY and call setsid() so ssh won't hang prompting for passphrase
 */

#if     HAVE_CONFIG_H
#include "config.h"
#endif


#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <stdlib.h>             /* putenv */
#if	HAVE_UNISTD_H
#include <unistd.h>
#endif
#if	HAVE_PTHREAD_H
#include <pthread.h>
#endif
#include <string.h>             /* memset */

#include <stddef.h>
#include <sys/socket.h> 
#include <sys/wait.h>

#include "src/common/xmalloc.h"
#include "src/common/xstring.h"
#include "src/common/err.h"
#include "src/common/list.h"
#include "src/common/split.h"
#include "src/common/pipecmd.h"
#include "src/pdsh/dsh.h"
#include "src/pdsh/mod.h"

#define HBUF_LEN	1024

#if STATIC_MODULES
#  define pdsh_module_info sshcmd_module_info
#  define pdsh_module_priority sshcmd_module_priority
#endif    

int pdsh_module_priority = DEFAULT_MODULE_PRIORITY;

    
static int mod_ssh_postop(opt_t *opt);
static int mod_ssh_exit (void);

static int sshcmd_init(opt_t *);
static int sshcmd_signal(int, void *arg, int);
static int sshcmd(char *, char *, char *, char *, char *, int, int *, void **);
static int sshcmd_destroy (pipecmd_t p);
static int sshcmd_args_init (void);

List ssh_args_list =     NULL;

/*
 *  Export generic pdsh module operations:
 */
struct pdsh_module_operations sshcmd_module_ops = {
    (ModInitF)       NULL,
    (ModExitF)       mod_ssh_exit,
    (ModReadWcollF)  NULL,
    (ModPostOpF)     mod_ssh_postop
};

/*
 *  Export rcmd module operations
 */
struct pdsh_rcmd_operations sshcmd_rcmd_ops = {
    (RcmdInitF)    sshcmd_init,
    (RcmdSigF)     sshcmd_signal,
    (RcmdF)        sshcmd,
    (RcmdDestroyF) sshcmd_destroy
};

/* 
 * Export module options
 */
struct pdsh_module_option sshcmd_module_options[] = 
 { 
   PDSH_OPT_TABLE_END
 };

/* 
 * Sshcmd module info 
 */
struct pdsh_module pdsh_module_info = {
  "rcmd",
  "ssh",
  "Jim Garlick <garlick@llnl.gov>",
  "ssh based rcmd connect method",
  DSH | PCP, 

  &sshcmd_module_ops,
  &sshcmd_rcmd_ops,
  &sshcmd_module_options[0],
};

static char **ssh_argv_create (const char **remote_argv)
{
    int n;
    char *arg;
    char **argv;
    const char **p;
    ListIterator i;

    n = 0;
    for (p = remote_argv; *p; p++)
        n++;

    n += list_count (ssh_args_list) + 2;
    argv = (char **) Malloc (n * sizeof (char *));
    memset (argv, 0, n);

    n = 0;
    i = list_iterator_create (ssh_args_list);
    while ((arg = list_next (i))) 
        argv[n++] = Strdup (arg);
    list_iterator_destroy (i);

    /* Append remote_argv to standard list of args */
    for (p = remote_argv; *p; p++)
        argv[n++] = Strdup (*p);

    return (argv);
}

static void ssh_argv_destroy (char **args)
{
    int i = 0;
    while (args[i])
        Free ((void **) &args[i++]);
    Free ((void **) &args);
}

static int ssh_args_prepend_timeout (int timeout)
{
#if SSH_HAS_CONNECT_TIMEOUT
    char buf[64];

    if (timeout <= 0)
        return (0);

    snprintf (buf, 64, SSH_CONNECT_TIMEOUT_OPTION, timeout);
    list_prepend (ssh_args_list, Strdup (buf));
#endif
    return (0);
}

static int mod_ssh_postop(opt_t *opt)
{
    sshcmd_args_init ();
    ssh_args_prepend_timeout (opt->connect_timeout);

    /*
     *  Append PATH=...; to ssh args if DSHPATH was set
     */
    if (opt->dshpath)
        list_append (ssh_args_list, Strdup (opt->dshpath));

    return 0;
}

static int sshcmd_init(opt_t * opt)
{
    /*
     * Drop privileges if running setuid root
     */
    if ((geteuid() == 0) && (getuid() != 0))
        setuid (getuid ());

    /*
     *  Do not resolve hostnames in pdsh when using ssh
     */
    if (rcmd_opt_set (RCMD_OPT_RESOLVE_HOSTS, 0) < 0)
        errx ("%p: sshcmd_init: rcmd_opt_set: %m\n");

    return 0;
}

static int mod_ssh_exit (void)
{
    if (ssh_args_list)
        list_destroy (ssh_args_list);

    return 0;
}

/*
 * SSH doesn't support signal forwarding, at least the way pdsh uses it
 *  at this time. Instead we always send SIGTERM which seems to have the
 *  desired effect of killing off ssh most of the time.
 */
static int sshcmd_signal(int fd, void *arg, int signum)
{
    /*
     *  Always send SIGTERM. SIGINT doesn't seem to get forwarded by ssh, and
     *    really termination of the connection is probably the desired result.
     */
    err ("sending SIGTERM to ssh %s\n", pipecmd_target ((pipecmd_t) arg));
    return (pipecmd_signal ((pipecmd_t) arg, SIGTERM));
}

static int
sshcmd(char *ahost, char *addr, char *luser, char *ruser, char *cmd,
         int rank, int *fd2p, void **arg)
{
    pipecmd_t p = NULL;
    const char **remote_argv = pdsh_remote_argv ();
    const char *alt_argv[] = { cmd, NULL };
    char **ssh_args;

    /*
     *  If running as pdcp/rpdcp, then the dsh code has rewritten
     *   the cmd to invoke pdcp server on remote nodes. Therefore
     *    avoid using pdsh_remote_argv() directly, instead use cmd:
     */
    if (pdsh_personality() == PCP)
        remote_argv = alt_argv;
    /*
     *  In interactive dsh mode, pdsh_remote_argv() will be empty
     *   so we can't use it.
     */
    if (!remote_argv || !*remote_argv)
        remote_argv = alt_argv;

    ssh_args = ssh_argv_create (remote_argv);

    if (!(p = pipecmd ("ssh", (const char **) ssh_args, ahost, ruser, rank)))
        goto out;

    if (fd2p)
        *fd2p = pipecmd_stderrfd (p);

    *arg = (void *) p;

out:
    ssh_argv_destroy (ssh_args);
    return (p ? pipecmd_stdoutfd (p) : -1);
}

static int 
sshcmd_destroy (pipecmd_t p)
{
    int status = 0;

    if (pipecmd_wait (p, &status) < 0)
        err ("%p: %S: wait on ssh cmd: %m\n", pipecmd_target (p));  

    pipecmd_destroy (p);

    return WEXITSTATUS (status);
}

/*
 *  Check string argument [arg] for parameter substitution sequence [s].
 *    If [s] is found in [arg] then also check that [s] is not preceeded
 *    by '%' which would have the effect of escaping the parameter
 *    substitution.
 */
static int arg_has_parameter (const char *arg, const char *s)
{
    char *p;
    if ((p = strstr (arg, s)) && ((p == arg) || (*(p-1) != '%')))
        return 1;
    return 0;
}

/*
 *  Scan the current ssh_args_list for presence of %u and %h.
 *   If they are not present, assume we need to append them to the
 *   ssh args.
 */
static int fixup_ssh_args (List ssh_args_list)
{
    ListIterator i = list_iterator_create (ssh_args_list);
    char *arg;
    int got_user = 0;
    int got_host = 0;

    while ((arg = list_next (i))) {
        if (arg_has_parameter (arg, "%u"))
            got_user = 1;
        if (arg_has_parameter (arg, "%h"))
            got_host = 1;
    }

    if (!got_user) {
        if (got_host) {
            /*
             *  Ensure "%lu" is not inserted after "%h":
             */
            list_iterator_reset (i);
            arg = list_find (i, (ListFindF) arg_has_parameter, "%h");
            list_insert (i, Strdup ("-l%u"));
        }
        else
            list_append (ssh_args_list, Strdup ("-l%u"));
    }

    /*
     *  Always append "%h" to end of args
     */
     if (!got_host)
         list_append (ssh_args_list, Strdup ("%h"));

     list_iterator_destroy (i);
     return (0);
     }

static int sshcmd_args_init (void)
{
    char *val = NULL;
    char *str = NULL;

    /*
     *  Place SSH_ARGS_APPEND at the beggining of the args string
     *   because %h must always come last...
     */
    if ((val = getenv ("PDSH_SSH_ARGS_APPEND"))) {
        str = Strdup (val);
        xstrcatchar (&str, ' ');
    }

    if (!(val = getenv ("PDSH_SSH_ARGS")))
        val = "-2 -a -x -l%u %h";
    xstrcat (&str, val);

    ssh_args_list = list_split (" ", str);
    Free ((void **) &str);

    /*
     *   Append "-l%u" and "%h" if necessary:
     */
    fixup_ssh_args (ssh_args_list);

    return (0);
}

/*
 * vi:tabstop=4 shiftwidth=4 expandtab
 */