1 | /*
|
---|
2 | * Copyright (c) 2008 Kungliga Tekniska Högskolan
|
---|
3 | * (Royal Institute of Technology, Stockholm, Sweden).
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * 1. Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | *
|
---|
13 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | *
|
---|
17 | * 3. Neither the name of the Institute nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include "config.h"
|
---|
35 |
|
---|
36 | #ifndef HAVE_SYS_TYPES_H
|
---|
37 | #include <sys/types.h>
|
---|
38 | #endif
|
---|
39 | #ifdef HAVE_SYS_WAIT_H
|
---|
40 | #include <sys/wait.h>
|
---|
41 | #endif
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <unistd.h>
|
---|
45 | #ifdef HAVE_PTY_H
|
---|
46 | #include <pty.h>
|
---|
47 | #endif
|
---|
48 | #ifdef HAVE_UTIL_H
|
---|
49 | #include <util.h>
|
---|
50 | #endif
|
---|
51 | #ifdef HAVE_LIBUTIL_H
|
---|
52 | #include <libutil.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #ifdef STREAMSPTY
|
---|
56 | #include <stropts.h>
|
---|
57 | #endif /* STREAMPTY */
|
---|
58 |
|
---|
59 | #include "roken.h"
|
---|
60 | #include <getarg.h>
|
---|
61 |
|
---|
62 | struct command {
|
---|
63 | enum { CMD_EXPECT = 0, CMD_SEND, CMD_PASSWORD } type;
|
---|
64 | unsigned int lineno;
|
---|
65 | char *str;
|
---|
66 | struct command *next;
|
---|
67 | };
|
---|
68 |
|
---|
69 | /*
|
---|
70 | *
|
---|
71 | */
|
---|
72 |
|
---|
73 | static struct command *commands, **next = &commands;
|
---|
74 |
|
---|
75 | static sig_atomic_t alarmset = 0;
|
---|
76 |
|
---|
77 | static int timeout = 10;
|
---|
78 | static int verbose;
|
---|
79 | static int help_flag;
|
---|
80 | static int version_flag;
|
---|
81 |
|
---|
82 | static int master;
|
---|
83 | static int slave;
|
---|
84 | static char line[256] = { 0 };
|
---|
85 |
|
---|
86 | static void
|
---|
87 | caught_signal(int signo)
|
---|
88 | {
|
---|
89 | alarmset = signo;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | static void
|
---|
94 | open_pty(void)
|
---|
95 | {
|
---|
96 | #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
|
---|
97 | if(openpty(&master, &slave, line, 0, 0) == 0)
|
---|
98 | return;
|
---|
99 | #endif /* HAVE_OPENPTY .... */
|
---|
100 | #ifdef STREAMSPTY
|
---|
101 | {
|
---|
102 | char *clone[] = {
|
---|
103 | "/dev/ptc",
|
---|
104 | "/dev/ptmx",
|
---|
105 | "/dev/ptm",
|
---|
106 | "/dev/ptym/clone",
|
---|
107 | NULL
|
---|
108 | };
|
---|
109 | char **q;
|
---|
110 |
|
---|
111 | for(q = clone; *q; q++){
|
---|
112 | master = open(*q, O_RDWR);
|
---|
113 | if(master >= 0){
|
---|
114 | #ifdef HAVE_GRANTPT
|
---|
115 | grantpt(master);
|
---|
116 | #endif
|
---|
117 | #ifdef HAVE_UNLOCKPT
|
---|
118 | unlockpt(master);
|
---|
119 | #endif
|
---|
120 | strlcpy(line, ptsname(master), sizeof(line));
|
---|
121 | slave = open(line, O_RDWR);
|
---|
122 | if (slave < 0)
|
---|
123 | errx(1, "failed to open slave when using %s", q);
|
---|
124 | ioctl(slave, I_PUSH, "ptem");
|
---|
125 | ioctl(slave, I_PUSH, "ldterm");
|
---|
126 |
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | #endif /* STREAMSPTY */
|
---|
132 |
|
---|
133 | /* more cases, like open /dev/ptmx, etc */
|
---|
134 |
|
---|
135 | exit(77);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*
|
---|
139 | *
|
---|
140 | */
|
---|
141 |
|
---|
142 | static char *
|
---|
143 | iscmd(const char *buf, const char *s)
|
---|
144 | {
|
---|
145 | size_t len = strlen(s);
|
---|
146 | if (strncmp(buf, s, len) != 0)
|
---|
147 | return NULL;
|
---|
148 | return estrdup(buf + len);
|
---|
149 | }
|
---|
150 |
|
---|
151 | static void
|
---|
152 | parse_configuration(const char *fn)
|
---|
153 | {
|
---|
154 | struct command *c;
|
---|
155 | char s[1024];
|
---|
156 | char *str;
|
---|
157 | unsigned int lineno = 0;
|
---|
158 | FILE *cmd;
|
---|
159 |
|
---|
160 | cmd = fopen(fn, "r");
|
---|
161 | if (cmd == NULL)
|
---|
162 | err(1, "open: %s", fn);
|
---|
163 |
|
---|
164 | while (fgets(s, sizeof(s), cmd) != NULL) {
|
---|
165 |
|
---|
166 | s[strcspn(s, "#\n")] = '\0';
|
---|
167 | lineno++;
|
---|
168 |
|
---|
169 | c = calloc(1, sizeof(*c));
|
---|
170 | if (c == NULL)
|
---|
171 | errx(1, "malloc");
|
---|
172 |
|
---|
173 | c->lineno = lineno;
|
---|
174 | (*next) = c;
|
---|
175 | next = &(c->next);
|
---|
176 |
|
---|
177 | if ((str = iscmd(s, "expect ")) != NULL) {
|
---|
178 | c->type = CMD_EXPECT;
|
---|
179 | c->str = str;
|
---|
180 | } else if ((str = iscmd(s, "send ")) != NULL) {
|
---|
181 | c->type = CMD_SEND;
|
---|
182 | c->str = str;
|
---|
183 | } else if ((str = iscmd(s, "password ")) != NULL) {
|
---|
184 | c->type = CMD_PASSWORD;
|
---|
185 | c->str = str;
|
---|
186 | } else
|
---|
187 | errx(1, "Invalid command on line %d: %s", lineno, s);
|
---|
188 | }
|
---|
189 |
|
---|
190 | fclose(cmd);
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /*
|
---|
195 | *
|
---|
196 | */
|
---|
197 |
|
---|
198 | static int
|
---|
199 | eval_parent(pid_t pid)
|
---|
200 | {
|
---|
201 | struct command *c;
|
---|
202 | char in;
|
---|
203 | size_t len = 0;
|
---|
204 | ssize_t sret;
|
---|
205 |
|
---|
206 | for (c = commands; c != NULL; c = c->next) {
|
---|
207 | switch(c->type) {
|
---|
208 | case CMD_EXPECT:
|
---|
209 | if (verbose)
|
---|
210 | printf("[expecting %s]", c->str);
|
---|
211 | len = 0;
|
---|
212 | alarm(timeout);
|
---|
213 | while((sret = read(master, &in, sizeof(in))) > 0) {
|
---|
214 | alarm(timeout);
|
---|
215 | printf("%c", in);
|
---|
216 | if (c->str[len] != in) {
|
---|
217 | len = 0;
|
---|
218 | continue;
|
---|
219 | }
|
---|
220 | len++;
|
---|
221 | if (c->str[len] == '\0')
|
---|
222 | break;
|
---|
223 | }
|
---|
224 | alarm(0);
|
---|
225 | if (alarmset == SIGALRM)
|
---|
226 | errx(1, "timeout waiting for %s (line %u)",
|
---|
227 | c->str, c->lineno);
|
---|
228 | else if (alarmset)
|
---|
229 | errx(1, "got a signal %d waiting for %s (line %u)",
|
---|
230 | alarmset, c->str, c->lineno);
|
---|
231 | if (sret <= 0)
|
---|
232 | errx(1, "end command while waiting for %s (line %u)",
|
---|
233 | c->str, c->lineno);
|
---|
234 | break;
|
---|
235 | case CMD_SEND:
|
---|
236 | case CMD_PASSWORD: {
|
---|
237 | size_t i = 0;
|
---|
238 | const char *msg = (c->type == CMD_PASSWORD) ? "****" : c->str;
|
---|
239 |
|
---|
240 | if (verbose)
|
---|
241 | printf("[send %s]", msg);
|
---|
242 |
|
---|
243 | len = strlen(c->str);
|
---|
244 |
|
---|
245 | while (i < len) {
|
---|
246 | if (c->str[i] == '\\' && i < len - 1) {
|
---|
247 | char ctrl;
|
---|
248 | i++;
|
---|
249 | switch(c->str[i]) {
|
---|
250 | case 'n': ctrl = '\n'; break;
|
---|
251 | case 'r': ctrl = '\r'; break;
|
---|
252 | case 't': ctrl = '\t'; break;
|
---|
253 | default:
|
---|
254 | errx(1, "unknown control char %c (line %u)",
|
---|
255 | c->str[i], c->lineno);
|
---|
256 | }
|
---|
257 | if (net_write(master, &ctrl, 1) != 1)
|
---|
258 | errx(1, "command refused input (line %u)", c->lineno);
|
---|
259 | } else {
|
---|
260 | if (net_write(master, &c->str[i], 1) != 1)
|
---|
261 | errx(1, "command refused input (line %u)", c->lineno);
|
---|
262 | }
|
---|
263 | i++;
|
---|
264 | }
|
---|
265 | break;
|
---|
266 | }
|
---|
267 | default:
|
---|
268 | abort();
|
---|
269 | }
|
---|
270 | }
|
---|
271 | while(read(master, &in, sizeof(in)) > 0)
|
---|
272 | printf("%c", in);
|
---|
273 |
|
---|
274 | if (verbose)
|
---|
275 | printf("[end of program]\n");
|
---|
276 |
|
---|
277 | /*
|
---|
278 | * Fetch status from child
|
---|
279 | */
|
---|
280 | {
|
---|
281 | int ret, status;
|
---|
282 |
|
---|
283 | ret = waitpid(pid, &status, 0);
|
---|
284 | if (ret == -1)
|
---|
285 | err(1, "waitpid");
|
---|
286 | if (WIFEXITED(status) && WEXITSTATUS(status))
|
---|
287 | return WEXITSTATUS(status);
|
---|
288 | else if (WIFSIGNALED(status)) {
|
---|
289 | printf("killed by signal: %d\n", WTERMSIG(status));
|
---|
290 | return 1;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | return 0;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /*
|
---|
297 | *
|
---|
298 | */
|
---|
299 |
|
---|
300 | static struct getargs args[] = {
|
---|
301 | { "timeout", 't', arg_integer, &timeout, "timout", "seconds" },
|
---|
302 | { "verbose", 'v', arg_counter, &verbose, "verbose debugging" },
|
---|
303 | { "version", 0, arg_flag, &version_flag, "print version" },
|
---|
304 | { "help", 0, arg_flag, &help_flag, NULL }
|
---|
305 | };
|
---|
306 |
|
---|
307 | static void
|
---|
308 | usage(int ret)
|
---|
309 | {
|
---|
310 | arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "infile command..");
|
---|
311 | exit (ret);
|
---|
312 | }
|
---|
313 |
|
---|
314 | int
|
---|
315 | main(int argc, char **argv)
|
---|
316 | {
|
---|
317 | int optidx = 0;
|
---|
318 | pid_t pid;
|
---|
319 |
|
---|
320 | setprogname(argv[0]);
|
---|
321 |
|
---|
322 | if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
|
---|
323 | usage(1);
|
---|
324 |
|
---|
325 | if (help_flag)
|
---|
326 | usage (0);
|
---|
327 |
|
---|
328 | if (version_flag) {
|
---|
329 | fprintf (stderr, "%s from %s-%s\n", getprogname(), PACKAGE, VERSION);
|
---|
330 | return 0;
|
---|
331 | }
|
---|
332 |
|
---|
333 | argv += optidx;
|
---|
334 | argc -= optidx;
|
---|
335 |
|
---|
336 | if (argc < 2)
|
---|
337 | usage(1);
|
---|
338 |
|
---|
339 | parse_configuration(argv[0]);
|
---|
340 |
|
---|
341 | argv += 1;
|
---|
342 |
|
---|
343 | open_pty();
|
---|
344 |
|
---|
345 | pid = fork();
|
---|
346 | switch (pid) {
|
---|
347 | case -1:
|
---|
348 | err(1, "Failed to fork");
|
---|
349 | case 0:
|
---|
350 |
|
---|
351 | if(setsid()<0)
|
---|
352 | err(1, "setsid");
|
---|
353 |
|
---|
354 | dup2(slave, STDIN_FILENO);
|
---|
355 | dup2(slave, STDOUT_FILENO);
|
---|
356 | dup2(slave, STDERR_FILENO);
|
---|
357 | closefrom(STDERR_FILENO + 1);
|
---|
358 |
|
---|
359 | execvp(argv[0], argv); /* add NULL to end of array ? */
|
---|
360 | err(1, "Failed to exec: %s", argv[0]);
|
---|
361 | default:
|
---|
362 | close(slave);
|
---|
363 | {
|
---|
364 | struct sigaction sa;
|
---|
365 |
|
---|
366 | sa.sa_handler = caught_signal;
|
---|
367 | sa.sa_flags = 0;
|
---|
368 | sigemptyset (&sa.sa_mask);
|
---|
369 |
|
---|
370 | sigaction(SIGALRM, &sa, NULL);
|
---|
371 | }
|
---|
372 |
|
---|
373 | return eval_parent(pid);
|
---|
374 | }
|
---|
375 | }
|
---|