-
Notifications
You must be signed in to change notification settings - Fork 21
/
mpssh.c
606 lines (537 loc) · 17.4 KB
/
mpssh.c
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*-
* Copyright (c) 2005-2015 Nikolay Denev <ndenev@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mpssh.h"
#include "host.h"
#include "pslot.h"
const char Ver[] = "1.4-dev";
/* global vars */
struct procslot *ps = NULL;
char *cmd = NULL;
char *user = NULL;
char *fname = NULL;
char *outdir = NULL;
char *label = NULL;
char *script = NULL;
char *base_script = NULL;
char *ident_file = NULL;
int children = 0;
int maxchld = 0;
int blind = 0;
int done = 0;
int delay = 10;
int hostcount = 0;
int pslots = 0;
int user_len_max = 0;
int host_len_max = 0;
int print_exit = 0;
int local_command = 0;
int ssh_hkey_check = 1;
int ssh_quiet = 0;
int ssh_conn_tmout = 30;
int verbose = 0;
int no_err = 0;
int no_out = 0;
sigset_t sigmask;
sigset_t osigmask;
/* function declarations */
struct host *host_readlist(char *);
void host_free(struct host *);
struct procslot *pslot_add(struct procslot *, int, struct host *);
struct procslot *pslot_del(struct procslot *);
struct procslot *pslot_bypid(struct procslot *, int);
void pslot_printbuf(struct procslot *, int);
int pslot_readbuf(struct procslot *, int);
/*
* child reaping routine. it is installed as signal
* hanler for the SIG_CHLD signal.
*/
void
reap_child()
{
int pid;
int ret;
while ((pid = waitpid(-1, &ret, WNOHANG)) > 0) {
done++;
ps = pslot_bypid(ps, pid);
ps->pid = 0;
if (WIFEXITED(ret))
ps->ret = WEXITSTATUS(ret);
else
ps->ret = 255;
while (pslot_readbuf(ps, OUT))
pslot_printbuf(ps, OUT);
while (pslot_readbuf(ps, ERR))
pslot_printbuf(ps, ERR);
/*
* make sure that we print some output in verbose mode
* even if there is no data in the buffer
*/
pslot_printbuf(ps, OUT);
ps = pslot_del(ps);
/* decrement this last, its used in pslot_bypid */
children--;
}
return;
}
void
child()
{
char *ssh_argv[19];
int sap;
char *lcmd;
int len_u;
char *user_arg;
/* enough for -p65535\0 */
char port_arg[8];
char tmo_arg[32];
ps->pid = 0;
sap = 0;
/* close stdin of the child, so it won't accept input */
close(0);
/* close the parent end of the pipes */
close(ps->io.out[0]);
close(ps->io.err[0]);
if (dup2(ps->io.out[1], 1) == -1)
perr("stdout dup fail %s\n",
strerror(errno));
if (dup2(ps->io.err[1], 2) == -1)
perr("stderr dup fail %s\n",
strerror(errno));
#ifdef TESTING
ssh_argv[sap++] = "/bin/echo";
#endif
ssh_argv[sap++] = SSHPATH;
ssh_argv[sap++] = "-oNumberOfPasswordPrompts=0";
if (ssh_quiet)
ssh_argv[sap++] = "-q";
/* space for -l and \0 */
len_u = strlen(ps->hst->user) + 3;
user_arg = calloc(1, len_u);
if (user_arg == NULL) {
exit(1);
}
snprintf(user_arg, len_u, "-l%s", ps->hst->user);
ssh_argv[sap++] = user_arg;
if (ps->hst->port != NON_DEFINED_PORT) {
snprintf(port_arg, sizeof(port_arg), "-p%d", ps->hst->port);
ssh_argv[sap++] = port_arg;
}
if (ssh_hkey_check)
ssh_argv[sap++] = "-oStrictHostKeyChecking=yes";
else
ssh_argv[sap++] = "-oStrictHostKeyChecking=no";
snprintf(tmo_arg,sizeof(tmo_arg), "-oConnectTimeout=%d",
ssh_conn_tmout);
ssh_argv[sap++] = tmo_arg;
if (local_command) {
snprintf(port_arg, sizeof(port_arg), "-P%d", (ps->hst->port
!= NON_DEFINED_PORT ? ps->hst->port : DEFAULT_PORT));
ssh_argv[sap++] = "-oPermitLocalCommand=yes";
lcmd = calloc(1, 2048);
snprintf(lcmd, 2048, "-oLocalCommand=%s %s -p %s %s@%s:%s",
SCPPATH,
port_arg,
script,
ps->hst->user,
ps->hst->host,
base_script);
ssh_argv[sap++] = lcmd;
}
if (ident_file) {
ssh_argv[sap++] = "-i";
ssh_argv[sap++] = ident_file;
}
ssh_argv[sap++] = ps->hst->host;
if (local_command) {
char *remexec;
remexec = calloc(1, strlen(base_script)+3);
snprintf(remexec, strlen(base_script)+3, "./%s", base_script);
ssh_argv[sap++] = remexec;
} else {
ssh_argv[sap++] = cmd;
}
ssh_argv[sap++] = NULL;
#ifdef TESTING
execv("/bin/echo", ssh_argv);
#else
execv(SSHPATH, ssh_argv);
#endif
perr("failed to exec the ssh binary");
exit(1);
}
/*
* print program version and exit
*/
void
show_ver()
{
printf("mpssh-%s\n", Ver);
exit(0);
}
/*
* routine displaing the usage, and various error messages
* supplied from the main() routine.
*/
void
usage(char *msg)
{
if (!msg) {
printf("\n Usage: mpssh [-u username] [-p numprocs] [-f hostlist]\n"
" [-e] [-b] [-o /some/dir] [-s] [-v] <command>\n\n"
" -b, --blind enable blind mode (no remote output)\n"
" -d, --delay delay between each ssh fork (default %d msec)\n"
" -e, --exit print the remote command return code\n"
" -E, --no-err suppress stderr output\n"
" -f, --file=FILE file with the list of hosts or - for stdin\n"
" -h, --help this screen\n"
" -l, --label=LABEL connect only to hosts under label LABEL\n"
" -i, --identity=FILE use the private key in FILE to connect to hosts\n"
" -o, --outdir=DIR save the remote output in this directory\n"
" -O, --no-out suppress stdout output\n"
" -p, --procs=NPROC number of parallel ssh processes (default %d)\n"
" -q, --quiet run ssh with -q\n"
" -r, --script copy local script to remote host and execute it\n"
" -s, --nokeychk disable ssh strict host key check\n"
" -t, --conntmout ssh connect timeout (default %d sec)\n"
" -u, --user=USER ssh login as this username\n"
" -v, --verbose be more verbose (i.e. show usernames used)\n"
" -V, --version show program version\n"
"\n", delay, DEFCHLD, ssh_conn_tmout);
} else {
printf("\n *** %s\n\n", msg);
}
exit(0);
}
void
parse_opts(int *argc, char ***argv)
{
int opt;
struct stat scstat;
static struct option longopts[] = {
{ "blind", no_argument, NULL, 'b' },
{ "exit", no_argument, NULL, 'e' },
{ "file", required_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{ "identity", required_argument, NULL, 'i' },
{ "label", required_argument, NULL, 'l' },
{ "outdir", required_argument, NULL, 'o' },
{ "procs", required_argument, NULL, 'p' },
{ "quiet", no_argument, NULL, 'q' },
{ "script", required_argument, NULL, 'r' },
{ "nokeychk", no_argument, NULL, 's' },
{ "no-err", no_argument, NULL, 'E' },
{ "no-out", no_argument, NULL, 'O' },
{ "conntmout", required_argument, NULL, 't' },
{ "user", required_argument, NULL, 'u' },
{ "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0},
};
while ((opt = getopt_long(*argc, *argv,
"bd:eEf:hi:l:o:Op:qr:u:t:svV", longopts, NULL)) != -1) {
switch (opt) {
case 'b':
blind = 1;
break;
case 'd':
delay = (int)strtol(optarg,(char **)NULL,10);
if (delay == 0 && errno == EINVAL)
usage("invalid delay value");
if (delay < 0) usage("delay can't be negative");
break;
case 'e':
print_exit = 1;
break;
case 'E':
no_err = 1;
break;
case 'f':
if (fname)
usage("one filename allowed");
fname = optarg;
break;
case 'h':
usage(NULL);
break;
case 'i':
ident_file = optarg;
break;
case 'l':
label = optarg;
break;
case 'o':
if (outdir)
usage("one output dir allowed");
outdir = optarg;
break;
case 'O':
no_out = 1;
break;
case 'p':
maxchld = (int)strtol(optarg,(char **)NULL,10);
if (maxchld < 0) usage("bad numproc");
if (maxchld > MAXCHLD) maxchld = MAXCHLD;
break;
case 'q':
ssh_quiet = 1;
break;
case 'r':
local_command = 1;
script = optarg;
if (stat(script, &scstat) < 0) {
usage("can't stat script file");
}
if (!(S_ISREG(scstat.st_mode) && scstat.st_mode & 0111)) {
usage("script file is not executable");
}
base_script = basename(script);
break;
case 's':
ssh_hkey_check = 0;
break;
case 't':
ssh_conn_tmout = (int)strtol(optarg,(char **)NULL,10);
break;
case 'u':
if (user)
usage("one username allowed");
user = optarg;
if (user && strlen(user) > MAXUSER)
usage("username too long");
break;
case 'v':
verbose = 1;
break;
case 'V':
show_ver();
break;
case '?':
usage("unrecognized option");
break;
default:
usage(NULL);
}
}
*argc -= optind;
*argv += optind;
if (!maxchld)
maxchld = DEFCHLD;
if (local_command) {
if(*argc)
usage("can't use remote command when executing local script");
return;
}
if (*argc > 1)
usage("too many arguments");
if (*argc < 1)
usage("command missing, use -h for help");
cmd = *argv[0];
if (strlen(cmd) > MAXCMD)
usage("command too long");
return;
}
/*
* Routine to handle stdout and stderr
* output file creation and opening
* when output to file mode is enabled.
*/
int
setupoutdirfiles(struct procslot *p)
{
int i;
/*
* alloc enough space for the string consisting
* of a directoryname, slash, username, @ sign,
* hostname, a dot and a three letter file
* extension (out/err) and the terminating null
*/
i = strlen(outdir);
i += strlen(p->hst->user);
i += strlen(p->hst->host);
i += 7;
/* setup the stdout output file */
p->outf[0].name = calloc(1, i);
if (!p->outf[0].name) {
perr("unable to malloc memory for filename\n");
return(1);
}
sprintf(p->outf[0].name, "%s/%s@%s.out",
outdir, p->hst->user, p->hst->host);
p->outf[0].fh = fopen(p->outf[0].name, "w");
if (!p->outf[0].fh) {
perr("unable to open : %s\n", p->outf[0].name);
return(1);
}
/* setup the stderr output file */
p->outf[1].name = calloc(1, i);
if (!p->outf[1].name) {
perr("unable to malloc memory for filename\n");
return(1);
}
sprintf(p->outf[1].name, "%s/%s@%s.err",
outdir, p->hst->user, p->hst->host);
p->outf[1].fh = fopen(p->outf[1].name, "w");
if (!p->outf[1].fh) {
perr("unable to open : %s\n", p->outf[1].name);
return(1);
}
return(0);
}
/*
* Main routine
*/
int
main(int argc, char *argv[])
{
struct host *hst, *tofree;
int i;
int pid;
int tty;
fd_set readfds;
int children_fds;
struct timeval *timeout;
struct timeval notimeout;
struct passwd *pw;
parse_opts(&argc, &argv);
if (!user) {
pw = getpwuid(getuid());
user = pw->pw_name;
}
hst = host_readlist(fname);
if (hst == NULL) {
perr("host list file empty, "
"does not exist or no valid entries\n");
exit(1);
}
tofree = hst;
/* Console Printf if we are running on tty */
tty = isatty(fileno(stdout));
#define tty_printf(...) if (tty) fprintf(stdout, __VA_ARGS__)
tty_printf( "MPSSH - Mass Parallel Ssh Ver.%s\n"
"(c)2005-2013 Nikolay Denev <ndenev@gmail.com>\n\n"
" [*] read (%d) hosts from the list\n",
Ver, hostcount);
if (local_command) {
tty_printf( " [*] uploading and executing the script \"%s\" as user \"%s\"\n",
script, user);
} else {
tty_printf( " [*] executing \"%s\" as user \"%s\"\n", cmd, user);
}
if (label)
tty_printf(" [*] only on hosts labeled \"%s\"\n", label);
if (!ssh_hkey_check)
tty_printf(" [*] strict host key check disabled\n");
if (blind)
tty_printf(" [*] blind mode enabled\n");
if (verbose)
tty_printf(" [*] verbose mode enabled\n");
if (outdir) {
if (!access(outdir, R_OK | W_OK | X_OK)) {
tty_printf(" [*] using output directory : %s\n", outdir);
} else {
tty_printf(" [*] creating output directory : %s\n", outdir);
if (mkdir(outdir, 0755)) {
perr("\n *** can't create output dir : ");
perror(outdir);
exit(1);
}
}
}
tty_printf(" [*] spawning %d parallel ssh sessions\n\n",
maxchld);
fflush(NULL);
/* install the signal handler for SIGCHLD */
signal(SIGCHLD, reap_child);
if (outdir)
umask(022);
while (hst || children) {
BLOCK_SIGCHLD;
if (hst && (children < maxchld)) {
ps = pslot_add(ps, 0, hst);
if (outdir)
setupoutdirfiles(ps);
switch (pid = fork()) {
case 0:
/* child, does not return */
child();
break;
case -1:
/* error */
perr("unable to fork: %s\n",
strerror(errno));
break;
default:
/* parent */
ps->pid = pid;
/* close the child's end of the pipes */
close(ps->io.out[1]);
close(ps->io.err[1]);
children++;
break;
}
/* delay between each sshd fork */
if (delay)
usleep(delay * 1000);
hst = hst->next;
}
FD_ZERO(&readfds);
children_fds = children;
for (i=0; i <= children_fds; i++) {
FD_SET(ps->io.out[0], &readfds);
FD_SET(ps->io.err[0], &readfds);
if (ps->next)
ps = ps->next;
}
if (children == maxchld || !hst) {
timeout = NULL;
} else {
memset(¬imeout, 0, sizeof(struct timeval));
timeout = ¬imeout;
}
UNBLOCK_SIGCHLD;
if (select(MAXFD , &readfds, NULL, NULL, timeout) > 0 ) {
BLOCK_SIGCHLD;
if (ps) {
for (i=0; i <= children_fds; i++) {
if (FD_ISSET(ps->io.out[0], &readfds)) {
while (pslot_readbuf(ps, OUT))
pslot_printbuf(ps, OUT);
}
if (FD_ISSET(ps->io.err[0], &readfds)) {
while (pslot_readbuf(ps, ERR))
pslot_printbuf(ps, ERR);
}
ps = ps->next;
}
}
UNBLOCK_SIGCHLD;
}
}
tty_printf("\n Done. %d hosts processed.\n", done);
host_free(tofree);
return(0);
}