s4:heimdal: import lorikeet-heimdal-200911122202 (commit 9291fd2d101f3eecec550178634f...
[Samba/ekacnet.git] / source4 / heimdal / lib / roken / rkpty.c
blob6043e2b81564bcf553385da93f50d59b3df8f1d1
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
51 #ifdef HAVE_LIBUTIL_H
52 #include <libutil.h>
53 #endif
55 #ifdef STREAMSPTY
56 #include <stropts.h>
57 #endif /* STREAMPTY */
59 #include "roken.h"
60 #include <getarg.h>
62 struct command {
63 enum { CMD_EXPECT = 0, CMD_SEND, CMD_PASSWORD } type;
64 unsigned int lineno;
65 char *str;
66 struct command *next;
73 static struct command *commands, **next = &commands;
75 static sig_atomic_t alarmset = 0;
77 static int timeout = 10;
78 static int verbose;
79 static int help_flag;
80 static int version_flag;
82 static int master;
83 static int slave;
84 static char line[256] = { 0 };
86 static void
87 caught_signal(int signo)
89 alarmset = signo;
93 static void
94 open_pty(void)
96 #ifdef _AIX
97 printf("implement open_pty\n");
98 exit(77);
99 #endif
100 #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
101 if(openpty(&master, &slave, line, 0, 0) == 0)
102 return;
103 #endif /* HAVE_OPENPTY .... */
104 #ifdef STREAMSPTY
106 char *clone[] = {
107 "/dev/ptc",
108 "/dev/ptmx",
109 "/dev/ptm",
110 "/dev/ptym/clone",
111 NULL
113 char **q;
115 for(q = clone; *q; q++){
116 master = open(*q, O_RDWR);
117 if(master >= 0){
118 #ifdef HAVE_GRANTPT
119 grantpt(master);
120 #endif
121 #ifdef HAVE_UNLOCKPT
122 unlockpt(master);
123 #endif
124 strlcpy(line, ptsname(master), sizeof(line));
125 slave = open(line, O_RDWR);
126 if (slave < 0)
127 errx(1, "failed to open slave when using %s", *q);
128 ioctl(slave, I_PUSH, "ptem");
129 ioctl(slave, I_PUSH, "ldterm");
131 return;
135 #endif /* STREAMSPTY */
137 /* more cases, like open /dev/ptmx, etc */
139 exit(77);
146 static char *
147 iscmd(const char *buf, const char *s)
149 size_t len = strlen(s);
150 if (strncmp(buf, s, len) != 0)
151 return NULL;
152 return estrdup(buf + len);
155 static void
156 parse_configuration(const char *fn)
158 struct command *c;
159 char s[1024];
160 char *str;
161 unsigned int lineno = 0;
162 FILE *cmd;
164 cmd = fopen(fn, "r");
165 if (cmd == NULL)
166 err(1, "open: %s", fn);
168 while (fgets(s, sizeof(s), cmd) != NULL) {
170 s[strcspn(s, "#\n")] = '\0';
171 lineno++;
173 c = calloc(1, sizeof(*c));
174 if (c == NULL)
175 errx(1, "malloc");
177 c->lineno = lineno;
178 (*next) = c;
179 next = &(c->next);
181 if ((str = iscmd(s, "expect ")) != NULL) {
182 c->type = CMD_EXPECT;
183 c->str = str;
184 } else if ((str = iscmd(s, "send ")) != NULL) {
185 c->type = CMD_SEND;
186 c->str = str;
187 } else if ((str = iscmd(s, "password ")) != NULL) {
188 c->type = CMD_PASSWORD;
189 c->str = str;
190 } else
191 errx(1, "Invalid command on line %d: %s", lineno, s);
194 fclose(cmd);
202 static int
203 eval_parent(pid_t pid)
205 struct command *c;
206 char in;
207 size_t len = 0;
208 ssize_t sret;
210 for (c = commands; c != NULL; c = c->next) {
211 switch(c->type) {
212 case CMD_EXPECT:
213 if (verbose)
214 printf("[expecting %s]", c->str);
215 len = 0;
216 alarm(timeout);
217 while((sret = read(master, &in, sizeof(in))) > 0) {
218 alarm(timeout);
219 printf("%c", in);
220 if (c->str[len] != in) {
221 len = 0;
222 continue;
224 len++;
225 if (c->str[len] == '\0')
226 break;
228 alarm(0);
229 if (alarmset == SIGALRM)
230 errx(1, "timeout waiting for %s (line %u)",
231 c->str, c->lineno);
232 else if (alarmset)
233 errx(1, "got a signal %d waiting for %s (line %u)",
234 alarmset, c->str, c->lineno);
235 if (sret <= 0)
236 errx(1, "end command while waiting for %s (line %u)",
237 c->str, c->lineno);
238 break;
239 case CMD_SEND:
240 case CMD_PASSWORD: {
241 size_t i = 0;
242 const char *msg = (c->type == CMD_PASSWORD) ? "****" : c->str;
244 if (verbose)
245 printf("[send %s]", msg);
247 len = strlen(c->str);
249 while (i < len) {
250 if (c->str[i] == '\\' && i < len - 1) {
251 char ctrl;
252 i++;
253 switch(c->str[i]) {
254 case 'n': ctrl = '\n'; break;
255 case 'r': ctrl = '\r'; break;
256 case 't': ctrl = '\t'; break;
257 default:
258 errx(1, "unknown control char %c (line %u)",
259 c->str[i], c->lineno);
261 if (net_write(master, &ctrl, 1) != 1)
262 errx(1, "command refused input (line %u)", c->lineno);
263 } else {
264 if (net_write(master, &c->str[i], 1) != 1)
265 errx(1, "command refused input (line %u)", c->lineno);
267 i++;
269 break;
271 default:
272 abort();
275 while(read(master, &in, sizeof(in)) > 0)
276 printf("%c", in);
278 if (verbose)
279 printf("[end of program]\n");
282 * Fetch status from child
285 int ret, status;
287 ret = waitpid(pid, &status, 0);
288 if (ret == -1)
289 err(1, "waitpid");
290 if (WIFEXITED(status) && WEXITSTATUS(status))
291 return WEXITSTATUS(status);
292 else if (WIFSIGNALED(status)) {
293 printf("killed by signal: %d\n", WTERMSIG(status));
294 return 1;
297 return 0;
304 static struct getargs args[] = {
305 { "timeout", 't', arg_integer, &timeout, "timout", "seconds" },
306 { "verbose", 'v', arg_counter, &verbose, "verbose debugging" },
307 { "version", 0, arg_flag, &version_flag, "print version" },
308 { "help", 0, arg_flag, &help_flag, NULL }
311 static void
312 usage(int ret)
314 arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "infile command..");
315 exit (ret);
319 main(int argc, char **argv)
321 int optidx = 0;
322 pid_t pid;
324 setprogname(argv[0]);
326 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
327 usage(1);
329 if (help_flag)
330 usage (0);
332 if (version_flag) {
333 fprintf (stderr, "%s from %s-%s\n", getprogname(), PACKAGE, VERSION);
334 return 0;
337 argv += optidx;
338 argc -= optidx;
340 if (argc < 2)
341 usage(1);
343 parse_configuration(argv[0]);
345 argv += 1;
347 open_pty();
349 pid = fork();
350 switch (pid) {
351 case -1:
352 err(1, "Failed to fork");
353 case 0:
355 if(setsid()<0)
356 err(1, "setsid");
358 dup2(slave, STDIN_FILENO);
359 dup2(slave, STDOUT_FILENO);
360 dup2(slave, STDERR_FILENO);
361 closefrom(STDERR_FILENO + 1);
363 execvp(argv[0], argv); /* add NULL to end of array ? */
364 err(1, "Failed to exec: %s", argv[0]);
365 default:
366 close(slave);
368 struct sigaction sa;
370 sa.sa_handler = caught_signal;
371 sa.sa_flags = 0;
372 sigemptyset (&sa.sa_mask);
374 sigaction(SIGALRM, &sa, NULL);
377 return eval_parent(pid);