More includes.
[heimdal.git] / lib / roken / rkpty.c
blob172f5f9593951efe8e5913540c7371801c85085e
1 /*
2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "config.h"
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
52 #include "roken.h"
53 #include <getarg.h>
55 struct command {
56 enum { CMD_EXPECT = 0, CMD_SEND, CMD_PASSWORD } type;
57 unsigned int lineno;
58 char *str;
59 struct command *next;
66 static struct command *commands, **next = &commands;
68 static sig_atomic_t alarmset = 0;
70 static int verbose;
71 static int timeout = 10;
72 static int master;
73 static int slave;
74 static char line[256] = { 0 };
76 static void
77 caught_signal(int signo)
79 alarmset = signo;
83 static void
84 open_pty(void)
86 #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
87 if(openpty(&master, &slave, line, 0, 0) == 0)
88 return;
89 #endif /* HAVE_OPENPTY .... */
90 /* more cases, like open /dev/ptmx, etc */
92 exit(77);
99 static char *
100 iscmd(const char *buf, const char *s)
102 size_t len = strlen(s);
103 if (strncmp(buf, s, len) != 0)
104 return NULL;
105 return estrdup(buf + len);
108 static void
109 parse_configuration(const char *fn)
111 struct command *c;
112 char s[1024];
113 char *str;
114 unsigned int lineno = 0;
115 FILE *cmd;
117 cmd = fopen(fn, "r");
118 if (cmd == NULL)
119 err(1, "open: %s", fn);
121 while (fgets(s, sizeof(s), cmd) != NULL) {
123 s[strcspn(s, "#\n")] = '\0';
124 lineno++;
126 c = calloc(1, sizeof(*c));
127 if (c == NULL)
128 errx(1, "malloc");
130 c->lineno = lineno;
131 (*next) = c;
132 next = &(c->next);
134 if ((str = iscmd(s, "expect ")) != NULL) {
135 c->type = CMD_EXPECT;
136 c->str = str;
137 } else if ((str = iscmd(s, "send ")) != NULL) {
138 c->type = CMD_SEND;
139 c->str = str;
140 } else if ((str = iscmd(s, "password ")) != NULL) {
141 c->type = CMD_PASSWORD;
142 c->str = str;
143 } else
144 errx(1, "Invalid command on line %d: %s", lineno, s);
147 fclose(cmd);
155 static int
156 eval_parent(pid_t pid)
158 struct command *c;
159 char in;
160 size_t len = 0;
161 ssize_t sret;
163 for (c = commands; c != NULL; c = c->next) {
164 switch(c->type) {
165 case CMD_EXPECT:
166 if (verbose)
167 printf("[expecting %s]", c->str);
168 len = 0;
169 alarm(timeout);
170 while((sret = read(master, &in, sizeof(in))) > 0) {
171 alarm(timeout);
172 if (verbose > 1)
173 printf("%c", in);
174 if (c->str[len] != in) {
175 len = 0;
176 continue;
178 len++;
179 if (c->str[len] == '\0')
180 break;
182 alarm(0);
183 if (alarmset == SIGALRM)
184 errx(1, "timeout waiting for %s (line %u)",
185 c->str, c->lineno);
186 else if (alarmset)
187 errx(1, "got a signal %d waiting for %s (line %u)",
188 alarmset, c->str, c->lineno);
189 if (sret <= 0)
190 errx(1, "end command while waiting for %s (line %u)",
191 c->str, c->lineno);
192 break;
193 case CMD_SEND:
194 case CMD_PASSWORD: {
195 size_t i = 0;
196 const char *msg = (c->type == CMD_PASSWORD) ? "****" : c->str;
198 if (verbose)
199 printf("[send %s]", msg);
201 len = strlen(c->str);
203 while (i < len) {
204 if (c->str[i] == '\\' && i < len - 1) {
205 char ctrl;
206 i++;
207 switch(c->str[i]) {
208 case 'n': ctrl = '\n'; break;
209 case 'r': ctrl = '\r'; break;
210 case 't': ctrl = '\t'; break;
211 default:
212 errx(1, "unknown control char %c (line %u)",
213 c->str[i], c->lineno);
215 if (net_write(master, &ctrl, 1) != 1)
216 errx(1, "command refused input (line %u)", c->lineno);
217 } else {
218 if (net_write(master, &c->str[i], 1) != 1)
219 errx(1, "command refused input (line %u)", c->lineno);
221 i++;
223 break;
225 default:
226 abort();
229 while(read(master, &in, sizeof(in)) > 0)
230 if (verbose > 1)
231 printf("%c", in);
233 if (verbose)
234 printf("[end of program]\n");
237 * Fetch status from child
240 int ret, status;
242 ret = waitpid(pid, &status, 0);
243 if (ret == -1)
244 err(1, "waitpid");
245 if (WIFEXITED(status) && WEXITSTATUS(status))
246 return WEXITSTATUS(status);
247 else if (WIFSIGNALED(status)) {
248 printf("killed by signal: %d\n", WTERMSIG(status));
249 return 1;
252 return 0;
259 static struct getargs args[] = {
260 { "timeout", 't', arg_integer, &timeout, "timout", "seconds" },
261 { "verbose", 'v', arg_counter, &verbose, "verbose debugging" }
264 static void
265 usage(int ret)
267 arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "infile command..");
268 exit (ret);
272 main(int argc, char **argv)
274 int optidx = 0;
275 pid_t pid;
277 setprogname(argv[0]);
279 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
280 usage(1);
282 argv += optidx;
283 argc -= optidx;
285 if (argc < 2)
286 usage(1);
288 parse_configuration(argv[0]);
290 argv += 1;
291 argc -= 1;
293 open_pty();
295 pid = fork();
296 switch (pid) {
297 case -1:
298 err(1, "Failed to fork");
299 case 0:
301 if(setsid()<0)
302 err(1, "setsid");
304 dup2(slave, STDIN_FILENO);
305 dup2(slave, STDOUT_FILENO);
306 dup2(slave, STDERR_FILENO);
307 closefrom(STDERR_FILENO + 1);
309 execvp(argv[0], argv); /* add NULL to end of array ? */
310 err(1, "Failed to exec: %s", argv[0]);
311 default:
312 close(slave);
314 struct sigaction sa;
316 sa.sa_handler = caught_signal;
317 sa.sa_flags = 0;
318 sigemptyset (&sa.sa_mask);
320 sigaction(SIGALRM, &sa, NULL);
323 return eval_parent(pid);