summaryrefslogtreecommitdiffstats
path: root/plugins/check_by_ssh.c
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@aei.ca>2009-05-26 08:17:07 (GMT)
committerThomas Guyot-Sionnest <dermoth@aei.ca>2009-05-26 08:28:41 (GMT)
commitfcc4db5aaadad9784d4a940aea239db46f1b0fe9 (patch)
tree1a6b31bc38ff60b8ca86ac512916a2b9b04e541b /plugins/check_by_ssh.c
parent221cb4920606552d670fdd264757dffe5d884015 (diff)
downloadmonitoring-plugins-fcc4db5aaadad9784d4a940aea239db46f1b0fe9.tar.gz
Fix check_by_ssh interpretation of quotes in -C parameter (#1985246, #2268675)
Diffstat (limited to 'plugins/check_by_ssh.c')
-rw-r--r--plugins/check_by_ssh.c72
1 files changed, 56 insertions, 16 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c
index d0dbf34..793c03d 100644
--- a/plugins/check_by_ssh.c
+++ b/plugins/check_by_ssh.c
@@ -31,12 +31,17 @@ const char *copyright = "2000-2008";
31const char *email = "nagiosplug-devel@lists.sourceforge.net"; 31const char *email = "nagiosplug-devel@lists.sourceforge.net";
32 32
33#include "common.h" 33#include "common.h"
34#include "netutils.h"
35#include "utils.h" 34#include "utils.h"
36#include "runcmd.h" 35#include "netutils.h"
36#include "utils_cmd.h"
37
38#ifndef NP_MAXARGS
39#define NP_MAXARGS 1024
40#endif
37 41
38int process_arguments (int, char **); 42int process_arguments (int, char **);
39int validate_arguments (void); 43int validate_arguments (void);
44void comm_append (const char *);
40void print_help (void); 45void print_help (void);
41void print_usage (void); 46void print_usage (void);
42 47
@@ -45,7 +50,8 @@ unsigned int services = 0;
45int skip_stdout = 0; 50int skip_stdout = 0;
46int skip_stderr = 0; 51int skip_stderr = 0;
47char *remotecmd = NULL; 52char *remotecmd = NULL;
48char *comm = NULL; 53char **commargv = NULL;
54int commargc = 0;
49char *hostname = NULL; 55char *hostname = NULL;
50char *outputfile = NULL; 56char *outputfile = NULL;
51char *host_shortname = NULL; 57char *host_shortname = NULL;
@@ -63,10 +69,10 @@ main (int argc, char **argv)
63 int i; 69 int i;
64 time_t local_time; 70 time_t local_time;
65 FILE *fp = NULL; 71 FILE *fp = NULL;
66 struct output chld_out, chld_err; 72 output chld_out, chld_err;
67 73
68 remotecmd = ""; 74 remotecmd = "";
69 comm = strdup (SSH_COMMAND); 75 comm_append(SSH_COMMAND);
70 76
71 setlocale (LC_ALL, ""); 77 setlocale (LC_ALL, "");
72 bindtextdomain (PACKAGE, LOCALEDIR); 78 bindtextdomain (PACKAGE, LOCALEDIR);
@@ -80,16 +86,19 @@ main (int argc, char **argv)
80 usage_va(_("Could not parse arguments")); 86 usage_va(_("Could not parse arguments"));
81 87
82 /* Set signal handling and alarm timeout */ 88 /* Set signal handling and alarm timeout */
83 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { 89 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
84 usage_va(_("Cannot catch SIGALRM")); 90 usage_va(_("Cannot catch SIGALRM"));
85 } 91 }
86 alarm (timeout_interval); 92 alarm (timeout_interval);
87 93
88 /* run the command */ 94 /* run the command */
89 if (verbose) 95 if (verbose) {
90 printf ("%s\n", comm); 96 printf ("Command: %s\n", commargv[0]);
97 for (i=1; i<commargc; i++)
98 printf ("Argument %i: %s\n", i, commargv[i]);
99 }
91 100
92 result = np_runcmd(comm, &chld_out, &chld_err, 0); 101 result = cmd_run_array (commargv, &chld_out, &chld_err, 0);
93 102
94 if (skip_stdout == -1) /* --skip-stdout specified without argument */ 103 if (skip_stdout == -1) /* --skip-stdout specified without argument */
95 skip_stdout = chld_out.lines; 104 skip_stdout = chld_out.lines;
@@ -218,7 +227,8 @@ process_arguments (int argc, char **argv)
218 case 'p': /* port number */ 227 case 'p': /* port number */
219 if (!is_integer (optarg)) 228 if (!is_integer (optarg))
220 usage_va(_("Port must be a positive integer")); 229 usage_va(_("Port must be a positive integer"));
221 asprintf (&comm,"%s -p %s", comm, optarg); 230 comm_append("-p");
231 comm_append(optarg);
222 break; 232 break;
223 case 'O': /* output file */ 233 case 'O': /* output file */
224 outputfile = optarg; 234 outputfile = optarg;
@@ -240,18 +250,32 @@ process_arguments (int argc, char **argv)
240 break; 250 break;
241 251
242 case 'u': 252 case 'u':
243 c = 'l'; 253 comm_append("-l");
254 comm_append(optarg);
255 break;
244 case 'l': /* login name */ 256 case 'l': /* login name */
257 comm_append("-l");
258 comm_append(optarg);
259 break;
245 case 'i': /* identity */ 260 case 'i': /* identity */
246 asprintf (&comm, "%s -%c %s", comm, c, optarg); 261 comm_append("-i");
262 comm_append(optarg);
247 break; 263 break;
248 264
249 case '1': /* Pass these switches directly to ssh */ 265 case '1': /* Pass these switches directly to ssh */
266 comm_append("-1");
267 break;
250 case '2': /* 1 to force version 1, 2 to force version 2 */ 268 case '2': /* 1 to force version 1, 2 to force version 2 */
269 comm_append("-2");
270 break;
251 case '4': /* -4 for IPv4 */ 271 case '4': /* -4 for IPv4 */
272 comm_append("-4");
273 break;
252 case '6': /* -6 for IPv6 */ 274 case '6': /* -6 for IPv6 */
275 comm_append("-6");
276 break;
253 case 'f': /* fork to background */ 277 case 'f': /* fork to background */
254 asprintf (&comm, "%s -%c", comm, c); 278 comm_append("-f");
255 break; 279 break;
256 case 'C': /* Command for remote machine */ 280 case 'C': /* Command for remote machine */
257 commands++; 281 commands++;
@@ -276,10 +300,11 @@ process_arguments (int argc, char **argv)
276 skip_stderr = atoi (optarg); 300 skip_stderr = atoi (optarg);
277 break; 301 break;
278 case 'o': /* Extra options for the ssh command */ 302 case 'o': /* Extra options for the ssh command */
279 asprintf (&comm, "%s -%c '%s'", comm, c, optarg); 303 comm_append("-o");
304 comm_append(optarg);
280 break; 305 break;
281 case 'q': /* Tell the ssh command to be quiet */ 306 case 'q': /* Tell the ssh command to be quiet */
282 asprintf (&comm, "%s -%c", comm, c); 307 comm_append("-q");
283 break; 308 break;
284 default: /* help */ 309 default: /* help */
285 usage5(); 310 usage5();
@@ -309,12 +334,27 @@ process_arguments (int argc, char **argv)
309 if (remotecmd == NULL || strlen (remotecmd) <= 1) 334 if (remotecmd == NULL || strlen (remotecmd) <= 1)
310 usage_va(_("No remotecmd")); 335 usage_va(_("No remotecmd"));
311 336
312 asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd); 337 comm_append(hostname);
338 comm_append(remotecmd);
313 339
314 return validate_arguments (); 340 return validate_arguments ();
315} 341}
316 342
317 343
344void
345comm_append (const char *str)
346{
347
348 if (++commargc > NP_MAXARGS)
349 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS);
350
351 if ((commargv = (char **)realloc(commargv, (commargc+1) * sizeof(char *))) == NULL)
352 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n"));
353
354 commargv[commargc-1] = strdup(str);
355 commargv[commargc] = NULL;
356
357}
318 358
319int 359int
320validate_arguments (void) 360validate_arguments (void)