diff options
Diffstat (limited to 'web/attachments/301093-check_by_ssh_patch.diff')
-rw-r--r-- | web/attachments/301093-check_by_ssh_patch.diff | 310 |
1 files changed, 310 insertions, 0 deletions
diff --git a/web/attachments/301093-check_by_ssh_patch.diff b/web/attachments/301093-check_by_ssh_patch.diff new file mode 100644 index 0000000..ca57d51 --- /dev/null +++ b/web/attachments/301093-check_by_ssh_patch.diff | |||
@@ -0,0 +1,310 @@ | |||
1 | --- plugins/check_by_ssh.c | ||
2 | +++ plugins/check_by_ssh.c | ||
3 | @@ -38,8 +38,11 @@ const char *email = "nagiosplug-devel@li | ||
4 | #include "netutils.h" | ||
5 | #include "utils.h" | ||
6 | #include "runcmd.h" | ||
7 | +#ifndef MAXARGS | ||
8 | +#define MAXARGS 255 | ||
9 | +#endif | ||
10 | |||
11 | -int process_arguments (int, char **); | ||
12 | +int process_arguments (int, char **, char **); | ||
13 | int validate_arguments (void); | ||
14 | void print_help (void); | ||
15 | void print_usage (void); | ||
16 | @@ -49,7 +52,6 @@ unsigned int services = 0; | ||
17 | int skip_stdout = 0; | ||
18 | int skip_stderr = 0; | ||
19 | char *remotecmd = NULL; | ||
20 | -char *comm = NULL; | ||
21 | char *hostname = NULL; | ||
22 | char *outputfile = NULL; | ||
23 | char *host_shortname = NULL; | ||
24 | @@ -68,9 +70,16 @@ main (int argc, char **argv) | ||
25 | time_t local_time; | ||
26 | FILE *fp = NULL; | ||
27 | struct output chld_out, chld_err; | ||
28 | + char **newargv; | ||
29 | + | ||
30 | + | ||
31 | + if ((newargv = calloc ((MAXARGS + 1), sizeof(char *))) == NULL) | ||
32 | + printf (_("malloc failed")); | ||
33 | + | ||
34 | + newargv[MAXARGS] = (char *)1; | ||
35 | |||
36 | remotecmd = ""; | ||
37 | - comm = strdup (SSH_COMMAND); | ||
38 | + newargv[0] = strdup (SSH_COMMAND); | ||
39 | |||
40 | setlocale (LC_ALL, ""); | ||
41 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
42 | @@ -80,7 +89,7 @@ main (int argc, char **argv) | ||
43 | argv=np_extra_opts (&argc, argv, progname); | ||
44 | |||
45 | /* process arguments */ | ||
46 | - if (process_arguments (argc, argv) == ERROR) | ||
47 | + if (process_arguments (argc, argv, newargv) == ERROR) | ||
48 | usage_va(_("Could not parse arguments")); | ||
49 | |||
50 | /* Set signal handling and alarm timeout */ | ||
51 | @@ -90,11 +99,28 @@ main (int argc, char **argv) | ||
52 | alarm (timeout_interval); | ||
53 | |||
54 | /* run the command */ | ||
55 | - if (verbose) | ||
56 | - printf ("%s\n", comm); | ||
57 | + if (verbose) { | ||
58 | + printf ("%s ", newargv[0]); | ||
59 | + for (i=1; i <= MAXARGS; i++) { | ||
60 | + if (newargv[i] > (char *)1 ) { | ||
61 | + printf ("'%s' ", newargv[i]); | ||
62 | + } | ||
63 | + } | ||
64 | + printf ("\n"); | ||
65 | + } | ||
66 | + | ||
67 | + result = np_runcmdv(newargv[0], newargv, &chld_out, &chld_err, 0); | ||
68 | |||
69 | - result = np_runcmd(comm, &chld_out, &chld_err, 0); | ||
70 | |||
71 | + // free memory to prevent valgrind errors | ||
72 | + for (i=0; i <= MAXARGS; i++) { | ||
73 | + if (newargv[i] > (char *)1 ) { | ||
74 | + free (newargv[i]); | ||
75 | + newargv[i]=0; | ||
76 | + } | ||
77 | + } | ||
78 | + free (newargv); | ||
79 | + | ||
80 | if (skip_stdout == -1) /* --skip-stdout specified without argument */ | ||
81 | skip_stdout = chld_out.lines; | ||
82 | if (skip_stderr == -1) /* --skip-stderr specified without argument */ | ||
83 | @@ -152,10 +178,11 @@ main (int argc, char **argv) | ||
84 | |||
85 | /* process command-line arguments */ | ||
86 | int | ||
87 | -process_arguments (int argc, char **argv) | ||
88 | +process_arguments (int argc, char **argv, char **newargv) | ||
89 | { | ||
90 | int c; | ||
91 | char *p1, *p2; | ||
92 | + char **opts = newargv + 2; | ||
93 | |||
94 | int option = 0; | ||
95 | static struct option longopts[] = { | ||
96 | @@ -222,7 +249,8 @@ process_arguments (int argc, char **argv | ||
97 | case 'p': /* port number */ | ||
98 | if (!is_integer (optarg)) | ||
99 | usage_va(_("Port must be a positive integer")); | ||
100 | - asprintf (&comm,"%s -p %s", comm, optarg); | ||
101 | + add_argument (&opts, "-p"); | ||
102 | + add_argument (&opts, optarg); | ||
103 | break; | ||
104 | case 'O': /* output file */ | ||
105 | outputfile = optarg; | ||
106 | @@ -246,16 +274,27 @@ process_arguments (int argc, char **argv | ||
107 | case 'u': | ||
108 | c = 'l'; | ||
109 | case 'l': /* login name */ | ||
110 | + add_argument (&opts, "-l"); | ||
111 | + add_argument (&opts, optarg); | ||
112 | + break; | ||
113 | case 'i': /* identity */ | ||
114 | - asprintf (&comm, "%s -%c %s", comm, c, optarg); | ||
115 | + add_argument (&opts, "-i"); | ||
116 | + add_argument (&opts, optarg); | ||
117 | break; | ||
118 | - | ||
119 | case '1': /* Pass these switches directly to ssh */ | ||
120 | + add_argument (&opts, "-1"); | ||
121 | + break; | ||
122 | case '2': /* 1 to force version 1, 2 to force version 2 */ | ||
123 | + add_argument (&opts, "-2"); | ||
124 | + break; | ||
125 | case '4': /* -4 for IPv4 */ | ||
126 | - case '6': /* -6 for IPv6 */ | ||
127 | + add_argument (&opts, "-4"); | ||
128 | + break; | ||
129 | + case '6': /* -6 for IPv6 */ | ||
130 | + add_argument (&opts, "-6"); | ||
131 | + break; | ||
132 | case 'f': /* fork to background */ | ||
133 | - asprintf (&comm, "%s -%c", comm, c); | ||
134 | + add_argument (&opts, "-f"); | ||
135 | break; | ||
136 | case 'C': /* Command for remote machine */ | ||
137 | commands++; | ||
138 | @@ -280,10 +319,11 @@ process_arguments (int argc, char **argv | ||
139 | skip_stderr = atoi (optarg); | ||
140 | break; | ||
141 | case 'o': /* Extra options for the ssh command */ | ||
142 | - asprintf (&comm, "%s -%c '%s'", comm, c, optarg); | ||
143 | + add_argument (&opts, "-o"); | ||
144 | + add_argument (&opts, optarg); | ||
145 | break; | ||
146 | case 'q': /* Tell the ssh command to be quiet */ | ||
147 | - asprintf (&comm, "%s -%c", comm, c); | ||
148 | + add_argument (&opts, "-q"); | ||
149 | break; | ||
150 | default: /* help */ | ||
151 | usage5(); | ||
152 | @@ -313,12 +353,25 @@ process_arguments (int argc, char **argv | ||
153 | if (remotecmd == NULL || strlen (remotecmd) <= 1) | ||
154 | usage_va(_("No remotecmd")); | ||
155 | |||
156 | - asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd); | ||
157 | + add_argument (&opts, remotecmd); | ||
158 | + opts = newargv+1; | ||
159 | + add_argument (&opts, hostname); | ||
160 | |||
161 | return validate_arguments (); | ||
162 | } | ||
163 | |||
164 | +int add_argument (char ***argv, const char *str) | ||
165 | +{ | ||
166 | + | ||
167 | + if (**argv == (char *)1) | ||
168 | + die (STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, MAXARGS); | ||
169 | |||
170 | + **argv = (char *)malloc((strlen(str) * sizeof(char)) +1); | ||
171 | + | ||
172 | + strcpy(**argv, str); | ||
173 | + (*argv)++; | ||
174 | + return 0; | ||
175 | +} | ||
176 | |||
177 | int | ||
178 | validate_arguments (void) | ||
179 | --- plugins/runcmd.c | ||
180 | +++ plugins/runcmd.c | ||
181 | @@ -46,6 +46,7 @@ | ||
182 | #ifdef HAVE_SYS_WAIT_H | ||
183 | # include <sys/wait.h> | ||
184 | #endif | ||
185 | +#include <sys/ioctl.h> | ||
186 | |||
187 | /** macros **/ | ||
188 | #ifndef WEXITSTATUS | ||
189 | @@ -87,6 +88,8 @@ static long maxfd = 0; | ||
190 | static int np_runcmd_open(const char *, int *, int *) | ||
191 | __attribute__((__nonnull__(1, 2, 3))); | ||
192 | |||
193 | +static int np_runcmd_open_argv(const char *, char **, int *, int *); | ||
194 | + | ||
195 | static int np_fetch_output(int, output *, int) | ||
196 | __attribute__((__nonnull__(2))); | ||
197 | |||
198 | @@ -118,24 +121,14 @@ void np_runcmd_init(void) | ||
199 | static int | ||
200 | np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) | ||
201 | { | ||
202 | - char *env[2]; | ||
203 | char *cmd = NULL; | ||
204 | char **argv = NULL; | ||
205 | char *str; | ||
206 | int argc; | ||
207 | size_t cmdlen; | ||
208 | - pid_t pid; | ||
209 | -#ifdef RLIMIT_CORE | ||
210 | - struct rlimit limit; | ||
211 | -#endif | ||
212 | |||
213 | int i = 0; | ||
214 | |||
215 | - if(!np_pids) NP_RUNCMD_INIT; | ||
216 | - | ||
217 | - env[0] = strdup("LC_ALL=C"); | ||
218 | - env[1] = '\0'; | ||
219 | - | ||
220 | /* if no command was passed, return with no error */ | ||
221 | if (cmdstring == NULL) | ||
222 | return -1; | ||
223 | @@ -191,6 +184,35 @@ np_runcmd_open(const char *cmdstring, in | ||
224 | argv[i++] = str; | ||
225 | } | ||
226 | |||
227 | + | ||
228 | + return (np_runcmd_open_argv(argv[0], argv, pfd, pfderr)); | ||
229 | +} | ||
230 | + | ||
231 | +static int | ||
232 | +np_runcmd_open_argv(const char *cmdstring, char **argv, int *pfd, int *pfderr) | ||
233 | +{ | ||
234 | + char *env[2]; | ||
235 | + pid_t pid; | ||
236 | +#ifdef RLIMIT_CORE | ||
237 | + struct rlimit limit; | ||
238 | +#endif | ||
239 | + | ||
240 | + int i = 0; | ||
241 | + int master,slave; | ||
242 | + | ||
243 | + if(!np_pids) NP_RUNCMD_INIT; | ||
244 | + | ||
245 | + env[0] = strdup("LC_ALL=C"); | ||
246 | + env[1] = '\0'; | ||
247 | + | ||
248 | + /* if no command was passed, return with an error */ | ||
249 | + if (cmdstring == NULL) | ||
250 | + return -1; | ||
251 | + | ||
252 | + /* make sure that the command name is also argv[0] */ | ||
253 | + if (strcmp(cmdstring, argv[0]) != 0) | ||
254 | + return -1; | ||
255 | + | ||
256 | if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) | ||
257 | return -1; /* errno set by the failing function */ | ||
258 | |||
259 | @@ -202,6 +224,11 @@ np_runcmd_open(const char *cmdstring, in | ||
260 | limit.rlim_cur = 0; | ||
261 | setrlimit (RLIMIT_CORE, &limit); | ||
262 | #endif | ||
263 | +#ifdef TIOCNOTTY | ||
264 | + /* disassociate from tty to prevent e.g. ssh asking for passwords */ | ||
265 | + if(isatty(STDOUT_FILENO)) | ||
266 | + ioctl(STDOUT_FILENO, TIOCNOTTY); | ||
267 | +#endif | ||
268 | close (pfd[0]); | ||
269 | if (pfd[1] != STDOUT_FILENO) { | ||
270 | dup2 (pfd[1], STDOUT_FILENO); | ||
271 | @@ -235,7 +262,6 @@ np_runcmd_open(const char *cmdstring, in | ||
272 | return pfd[0]; | ||
273 | } | ||
274 | |||
275 | - | ||
276 | static int | ||
277 | np_runcmd_close(int fd) | ||
278 | { | ||
279 | @@ -360,3 +386,21 @@ np_runcmd(const char *cmd, output *out, | ||
280 | |||
281 | return np_runcmd_close(fd); | ||
282 | } | ||
283 | + | ||
284 | +int | ||
285 | +np_runcmdv(const char *cmd, char ** argv, output *out, output *err, int flags) | ||
286 | +{ | ||
287 | + int fd, pfd_out[2], pfd_err[2]; | ||
288 | + | ||
289 | + /* initialize the structs */ | ||
290 | + if(out) memset(out, 0, sizeof(output)); | ||
291 | + if(err) memset(err, 0, sizeof(output)); | ||
292 | + | ||
293 | + if((fd = np_runcmd_open_argv(cmd, argv, pfd_out, pfd_err)) == -1) | ||
294 | + die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); | ||
295 | + | ||
296 | + if(out) out->lines = np_fetch_output(pfd_out[0], out, flags); | ||
297 | + if(err) err->lines = np_fetch_output(pfd_err[0], err, flags); | ||
298 | + | ||
299 | + return np_runcmd_close(fd); | ||
300 | +} | ||
301 | --- plugins/runcmd.h | ||
302 | +++ plugins/runcmd.h | ||
303 | @@ -40,6 +40,7 @@ typedef struct output output; | ||
304 | |||
305 | /** prototypes **/ | ||
306 | int np_runcmd(const char *, output *, output *, int); | ||
307 | +int np_runcmdv(const char *, char **, output *, output *, int); | ||
308 | void popen_timeout_alarm_handler(int) | ||
309 | __attribute__((__noreturn__)); | ||
310 | |||