xref: /freebsd/usr.sbin/ppp/chat.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1b6dec9f0SBrian Somers /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4b6dec9f0SBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5b6dec9f0SBrian Somers  * All rights reserved.
6af57ed9fSAtsushi Murai  *
7b6dec9f0SBrian Somers  * Redistribution and use in source and binary forms, with or without
8b6dec9f0SBrian Somers  * modification, are permitted provided that the following conditions
9b6dec9f0SBrian Somers  * are met:
10b6dec9f0SBrian Somers  * 1. Redistributions of source code must retain the above copyright
11b6dec9f0SBrian Somers  *    notice, this list of conditions and the following disclaimer.
12b6dec9f0SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
13b6dec9f0SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
14b6dec9f0SBrian Somers  *    documentation and/or other materials provided with the distribution.
15af57ed9fSAtsushi Murai  *
16b6dec9f0SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b6dec9f0SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b6dec9f0SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b6dec9f0SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b6dec9f0SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b6dec9f0SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b6dec9f0SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b6dec9f0SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b6dec9f0SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b6dec9f0SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b6dec9f0SBrian Somers  * SUCH DAMAGE.
27af57ed9fSAtsushi Murai  */
2875240ed1SBrian Somers 
29972a1bcfSBrian Somers #include <sys/param.h>
301ae349f5Scvs2svn #include <netinet/in.h>
3192f4ff1cSBrian Somers #include <netinet/in_systm.h>
3292f4ff1cSBrian Somers #include <netinet/ip.h>
3330949fd4SBrian Somers #include <sys/socket.h>
341fa665f5SBrian Somers #include <sys/un.h>
351ae349f5Scvs2svn 
3675240ed1SBrian Somers #include <errno.h>
3775240ed1SBrian Somers #include <fcntl.h>
38b762af4fSBrian Somers #include <paths.h>
3975240ed1SBrian Somers #include <stdio.h>
4075240ed1SBrian Somers #include <stdlib.h>
4175240ed1SBrian Somers #include <string.h>
4275240ed1SBrian Somers #include <sys/wait.h>
4375240ed1SBrian Somers #include <termios.h>
4475240ed1SBrian Somers #include <unistd.h>
4575240ed1SBrian Somers 
465d9e6103SBrian Somers #include "layer.h"
4775240ed1SBrian Somers #include "mbuf.h"
4875240ed1SBrian Somers #include "log.h"
4975240ed1SBrian Somers #include "defs.h"
5075240ed1SBrian Somers #include "timer.h"
51879ed6faSBrian Somers #include "lqr.h"
526140ba11SBrian Somers #include "hdlc.h"
536140ba11SBrian Somers #include "throughput.h"
546140ba11SBrian Somers #include "fsm.h"
556140ba11SBrian Somers #include "lcp.h"
563b0f8d2eSBrian Somers #include "ccp.h"
576140ba11SBrian Somers #include "link.h"
586140ba11SBrian Somers #include "async.h"
5942d4d396SBrian Somers #include "descriptor.h"
6063b73463SBrian Somers #include "physical.h"
61274e766cSBrian Somers #include "chat.h"
6292f4ff1cSBrian Somers #include "mp.h"
6392f4ff1cSBrian Somers #include "auth.h"
6492f4ff1cSBrian Somers #include "chap.h"
6592f4ff1cSBrian Somers #include "slcompress.h"
6692f4ff1cSBrian Somers #include "iplist.h"
6730949fd4SBrian Somers #include "ncpaddr.h"
6892f4ff1cSBrian Somers #include "ipcp.h"
6992f4ff1cSBrian Somers #include "filter.h"
7092b09558SBrian Somers #include "cbcp.h"
71a8103305SBrian Somers #include "command.h"
7292f4ff1cSBrian Somers #include "datalink.h"
73972a1bcfSBrian Somers #ifndef NORADIUS
74972a1bcfSBrian Somers #include "radius.h"
75972a1bcfSBrian Somers #endif
7630949fd4SBrian Somers #include "ipv6cp.h"
7730949fd4SBrian Somers #include "ncp.h"
7892f4ff1cSBrian Somers #include "bundle.h"
79e7d008b4SBrian Somers #include "id.h"
8075240ed1SBrian Somers 
81b6dec9f0SBrian Somers #define BUFLEFT(c) (sizeof (c)->buf - ((c)->bufend - (c)->buf))
8275240ed1SBrian Somers 
83b6dec9f0SBrian Somers static void ExecStr(struct physical *, char *, char *, int);
84d5015272SBrian Somers static char *ExpandString(struct chat *, const char *, char *, int, int);
85af57ed9fSAtsushi Murai 
86b6dec9f0SBrian Somers static void
chat_PauseTimer(void * v)87b6dec9f0SBrian Somers chat_PauseTimer(void *v)
88b6dec9f0SBrian Somers {
89b6dec9f0SBrian Somers   struct chat *c = (struct chat *)v;
90dd7e2610SBrian Somers   timer_Stop(&c->pause);
91b6dec9f0SBrian Somers   c->pause.load = 0;
92b6dec9f0SBrian Somers }
9353c9f6c0SAtsushi Murai 
94b6dec9f0SBrian Somers static void
chat_Pause(struct chat * c,u_long load)95b6dec9f0SBrian Somers chat_Pause(struct chat *c, u_long load)
96b6dec9f0SBrian Somers {
97dd7e2610SBrian Somers   timer_Stop(&c->pause);
98b6dec9f0SBrian Somers   c->pause.load += load;
99b6dec9f0SBrian Somers   c->pause.func = chat_PauseTimer;
1003b0f8d2eSBrian Somers   c->pause.name = "chat pause";
101b6dec9f0SBrian Somers   c->pause.arg = c;
102dd7e2610SBrian Somers   timer_Start(&c->pause);
103b6dec9f0SBrian Somers }
104af57ed9fSAtsushi Murai 
105b6dec9f0SBrian Somers static void
chat_TimeoutTimer(void * v)106b6dec9f0SBrian Somers chat_TimeoutTimer(void *v)
107b6dec9f0SBrian Somers {
108b6dec9f0SBrian Somers   struct chat *c = (struct chat *)v;
109dd7e2610SBrian Somers   timer_Stop(&c->timeout);
110b6dec9f0SBrian Somers   c->TimedOut = 1;
111b6dec9f0SBrian Somers }
112b6dec9f0SBrian Somers 
113b6dec9f0SBrian Somers static void
chat_SetTimeout(struct chat * c)114b6dec9f0SBrian Somers chat_SetTimeout(struct chat *c)
115b6dec9f0SBrian Somers {
116dd7e2610SBrian Somers   timer_Stop(&c->timeout);
117b6dec9f0SBrian Somers   if (c->TimeoutSec > 0) {
118b6dec9f0SBrian Somers     c->timeout.load = SECTICKS * c->TimeoutSec;
119b6dec9f0SBrian Somers     c->timeout.func = chat_TimeoutTimer;
1203b0f8d2eSBrian Somers     c->timeout.name = "chat timeout";
121b6dec9f0SBrian Somers     c->timeout.arg = c;
122dd7e2610SBrian Somers     timer_Start(&c->timeout);
123b6dec9f0SBrian Somers   }
124b6dec9f0SBrian Somers }
125b6dec9f0SBrian Somers 
126b6dec9f0SBrian Somers static char *
chat_NextChar(char * ptr,char ch)127b6dec9f0SBrian Somers chat_NextChar(char *ptr, char ch)
128b6dec9f0SBrian Somers {
129b6dec9f0SBrian Somers   for (; *ptr; ptr++)
130b6dec9f0SBrian Somers     if (*ptr == ch)
131b6dec9f0SBrian Somers       return ptr;
132b6dec9f0SBrian Somers     else if (*ptr == '\\')
133b6dec9f0SBrian Somers       if (*++ptr == '\0')
134b6dec9f0SBrian Somers         return NULL;
135b6dec9f0SBrian Somers 
136b6dec9f0SBrian Somers   return NULL;
137b6dec9f0SBrian Somers }
138b6dec9f0SBrian Somers 
139b6dec9f0SBrian Somers static int
chat_UpdateSet(struct fdescriptor * d,fd_set * r,fd_set * w,fd_set * e,int * n)140f013f33eSBrian Somers chat_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
141b6dec9f0SBrian Somers {
142b6dec9f0SBrian Somers   struct chat *c = descriptor2chat(d);
143b6dec9f0SBrian Somers   int special, gotabort, gottimeout, needcr;
14499294c8bSBrian Somers   int TimedOut = c->TimedOut;
145d93d3a9cSBrian Somers   static char arg_term;		/* An empty string */
146b6dec9f0SBrian Somers 
147b6dec9f0SBrian Somers   if (c->pause.state == TIMER_RUNNING)
148b6dec9f0SBrian Somers     return 0;
149b6dec9f0SBrian Somers 
15099294c8bSBrian Somers   if (TimedOut) {
151dd7e2610SBrian Somers     log_Printf(LogCHAT, "Expect timeout\n");
152b6dec9f0SBrian Somers     if (c->nargptr == NULL)
153b6dec9f0SBrian Somers       c->state = CHAT_FAILED;
154b6dec9f0SBrian Somers     else {
15599294c8bSBrian Somers       /* c->state = CHAT_EXPECT; */
156cdbbb6b5SBrian Somers       c->argptr = &arg_term;
1572df95816SChristian Brueffer       /*
1582df95816SChristian Brueffer 	We have to clear the input buffer, because it contains output
1592df95816SChristian Brueffer 	from the previous (timed out) command.
1602df95816SChristian Brueffer       */
1612df95816SChristian Brueffer       c->bufstart = c->bufend;
162b6dec9f0SBrian Somers     }
16399294c8bSBrian Somers     c->TimedOut = 0;
164b6dec9f0SBrian Somers   }
165b6dec9f0SBrian Somers 
166b6dec9f0SBrian Somers   if (c->state != CHAT_EXPECT && c->state != CHAT_SEND)
167b6dec9f0SBrian Somers     return 0;
168b6dec9f0SBrian Somers 
169b6dec9f0SBrian Somers   gottimeout = gotabort = 0;
170b6dec9f0SBrian Somers 
171b6dec9f0SBrian Somers   if (c->arg < c->argc && (c->arg < 0 || *c->argptr == '\0')) {
172b6dec9f0SBrian Somers     /* Go get the next string */
173b6dec9f0SBrian Somers     if (c->arg < 0 || c->state == CHAT_SEND)
174b6dec9f0SBrian Somers       c->state = CHAT_EXPECT;
175b6dec9f0SBrian Somers     else
176b6dec9f0SBrian Somers       c->state = CHAT_SEND;
177b6dec9f0SBrian Somers 
178b6dec9f0SBrian Somers     special = 1;
179b6dec9f0SBrian Somers     while (special && (c->nargptr || c->arg < c->argc - 1)) {
18099294c8bSBrian Somers       if (c->arg < 0 || (!TimedOut && c->state == CHAT_SEND))
181b6dec9f0SBrian Somers         c->nargptr = NULL;
182b6dec9f0SBrian Somers 
183b6dec9f0SBrian Somers       if (c->nargptr != NULL) {
184b6dec9f0SBrian Somers         /* We're doing expect-send-expect.... */
185b6dec9f0SBrian Somers         c->argptr = c->nargptr;
186b6dec9f0SBrian Somers         /* Put the '-' back in case we ever want to rerun our script */
187b6dec9f0SBrian Somers         c->nargptr[-1] = '-';
188b6dec9f0SBrian Somers         c->nargptr = chat_NextChar(c->nargptr, '-');
189b6dec9f0SBrian Somers         if (c->nargptr != NULL)
190b6dec9f0SBrian Somers           *c->nargptr++ = '\0';
191b6dec9f0SBrian Somers       } else {
192b6dec9f0SBrian Somers         int minus;
193b6dec9f0SBrian Somers 
1946a32b414SBrian Somers         if ((c->argptr = c->argv[++c->arg]) == NULL) {
1956a32b414SBrian Somers           /* End of script - all ok */
1966a32b414SBrian Somers           c->state = CHAT_DONE;
1976a32b414SBrian Somers           return 0;
1986a32b414SBrian Somers         }
199b6dec9f0SBrian Somers 
200b6dec9f0SBrian Somers         if (c->state == CHAT_EXPECT) {
201b6dec9f0SBrian Somers           /* Look for expect-send-expect sequence */
202b6dec9f0SBrian Somers           c->nargptr = c->argptr;
203b6dec9f0SBrian Somers           minus = 0;
204b6dec9f0SBrian Somers           while ((c->nargptr = chat_NextChar(c->nargptr, '-'))) {
205b6dec9f0SBrian Somers             c->nargptr++;
206b6dec9f0SBrian Somers             minus++;
207b6dec9f0SBrian Somers           }
208b6dec9f0SBrian Somers 
209b6dec9f0SBrian Somers           if (minus % 2)
210dd7e2610SBrian Somers             log_Printf(LogWARN, "chat_UpdateSet: \"%s\": Uneven number of"
211b6dec9f0SBrian Somers                       " '-' chars, all ignored\n", c->argptr);
212b6dec9f0SBrian Somers           else if (minus) {
213b6dec9f0SBrian Somers             c->nargptr = chat_NextChar(c->argptr, '-');
214b6dec9f0SBrian Somers             *c->nargptr++ = '\0';
215b6dec9f0SBrian Somers           }
216b6dec9f0SBrian Somers         }
217b6dec9f0SBrian Somers       }
218b6dec9f0SBrian Somers 
219b6dec9f0SBrian Somers       /*
220b6dec9f0SBrian Somers        * c->argptr now temporarily points into c->script (via c->argv)
221b6dec9f0SBrian Somers        * If it's an expect-send-expect sequence, we've just got the correct
222b6dec9f0SBrian Somers        * portion of that sequence.
223b6dec9f0SBrian Somers        */
224b6dec9f0SBrian Somers 
225a2240f35SBrian Somers       needcr = c->state == CHAT_SEND &&
226a2240f35SBrian Somers                (*c->argptr != '!' || c->argptr[1] == '!');
227b6dec9f0SBrian Somers 
228b6dec9f0SBrian Somers       /* We leave room for a potential HDLC header in the target string */
229d5015272SBrian Somers       ExpandString(c, c->argptr, c->exp + 2, sizeof c->exp - 2, needcr);
230b6dec9f0SBrian Somers 
2314d9c2bb9SBrian Somers       /*
2324d9c2bb9SBrian Somers        * Now read our string.  If it's not a special string, we unset
2334d9c2bb9SBrian Somers        * ``special'' to break out of the loop.
2344d9c2bb9SBrian Somers        */
235b6dec9f0SBrian Somers       if (gotabort) {
236486f40c9SBrian Somers         if (c->abort.num < MAXABORTS) {
237cdbbb6b5SBrian Somers           int len, i;
238486f40c9SBrian Somers 
239486f40c9SBrian Somers           len = strlen(c->exp+2);
240cdbbb6b5SBrian Somers           for (i = 0; i < c->abort.num; i++)
241cdbbb6b5SBrian Somers             if (len > c->abort.string[i].len) {
242486f40c9SBrian Somers               int last;
243486f40c9SBrian Somers 
244cdbbb6b5SBrian Somers               for (last = c->abort.num; last > i; last--) {
245486f40c9SBrian Somers                 c->abort.string[last].data = c->abort.string[last-1].data;
246486f40c9SBrian Somers                 c->abort.string[last].len = c->abort.string[last-1].len;
247486f40c9SBrian Somers               }
248486f40c9SBrian Somers               break;
249486f40c9SBrian Somers             }
250cdbbb6b5SBrian Somers           c->abort.string[i].len = len;
2515d604c11SBrian Somers           if ((c->abort.string[i].data = (char *)malloc(len+1)) != NULL) {
252cdbbb6b5SBrian Somers             memcpy(c->abort.string[i].data, c->exp+2, len+1);
253486f40c9SBrian Somers             c->abort.num++;
2545d604c11SBrian Somers 	  }
255486f40c9SBrian Somers         } else
256dd7e2610SBrian Somers           log_Printf(LogERROR, "chat_UpdateSet: too many abort strings\n");
257b6dec9f0SBrian Somers         gotabort = 0;
258b6dec9f0SBrian Somers       } else if (gottimeout) {
259b6dec9f0SBrian Somers         c->TimeoutSec = atoi(c->exp + 2);
260b6dec9f0SBrian Somers         if (c->TimeoutSec <= 0)
261b6dec9f0SBrian Somers           c->TimeoutSec = 30;
262b6dec9f0SBrian Somers         gottimeout = 0;
263b6dec9f0SBrian Somers       } else if (c->nargptr == NULL && !strcmp(c->exp+2, "ABORT"))
264b6dec9f0SBrian Somers         gotabort = 1;
265b6dec9f0SBrian Somers       else if (c->nargptr == NULL && !strcmp(c->exp+2, "TIMEOUT"))
266b6dec9f0SBrian Somers         gottimeout = 1;
267b6dec9f0SBrian Somers       else {
268a2240f35SBrian Somers         if (c->exp[2] == '!' && c->exp[3] != '!')
269a2240f35SBrian Somers           ExecStr(c->physical, c->exp + 3, c->exp + 3, sizeof c->exp - 3);
270b6dec9f0SBrian Somers 
27156c9cf9dSBrian Somers         if (c->exp[2] == '\0') {
272b6dec9f0SBrian Somers           /* Empty string, reparse (this may be better as a `goto start') */
273cdbbb6b5SBrian Somers           c->argptr = &arg_term;
274b6dec9f0SBrian Somers           return chat_UpdateSet(d, r, w, e, n);
27556c9cf9dSBrian Somers         }
276b6dec9f0SBrian Somers 
277b6dec9f0SBrian Somers         special = 0;
278b6dec9f0SBrian Somers       }
279b6dec9f0SBrian Somers     }
280b6dec9f0SBrian Somers 
281b6dec9f0SBrian Somers     if (special) {
282b6dec9f0SBrian Somers       if (gottimeout)
283dd7e2610SBrian Somers         log_Printf(LogWARN, "chat_UpdateSet: TIMEOUT: Argument expected\n");
284b6dec9f0SBrian Somers       else if (gotabort)
285dd7e2610SBrian Somers         log_Printf(LogWARN, "chat_UpdateSet: ABORT: Argument expected\n");
286b6dec9f0SBrian Somers 
287b6dec9f0SBrian Somers       /* End of script - all ok */
288b6dec9f0SBrian Somers       c->state = CHAT_DONE;
289b6dec9f0SBrian Somers       return 0;
290b6dec9f0SBrian Somers     }
291b6dec9f0SBrian Somers 
292b6dec9f0SBrian Somers     /* set c->argptr to point in the right place */
293a2240f35SBrian Somers     c->argptr = c->exp + (c->exp[2] == '!' ? 3 : 2);
294b6dec9f0SBrian Somers     c->arglen = strlen(c->argptr);
295b6dec9f0SBrian Somers 
296b6dec9f0SBrian Somers     if (c->state == CHAT_EXPECT) {
297b6dec9f0SBrian Somers       /* We must check to see if the string's already been found ! */
298b6dec9f0SBrian Somers       char *begin, *end;
299b6dec9f0SBrian Somers 
300b6dec9f0SBrian Somers       end = c->bufend - c->arglen + 1;
301b6dec9f0SBrian Somers       if (end < c->bufstart)
302b6dec9f0SBrian Somers         end = c->bufstart;
303b6dec9f0SBrian Somers       for (begin = c->bufstart; begin < end; begin++)
304b6dec9f0SBrian Somers         if (!strncmp(begin, c->argptr, c->arglen)) {
305b6dec9f0SBrian Somers           c->bufstart = begin + c->arglen;
306b6dec9f0SBrian Somers           c->argptr += c->arglen;
307b6dec9f0SBrian Somers           c->arglen = 0;
308b6dec9f0SBrian Somers           /* Continue - we've already read our expect string */
309b6dec9f0SBrian Somers           return chat_UpdateSet(d, r, w, e, n);
310b6dec9f0SBrian Somers         }
311b6dec9f0SBrian Somers 
312dd7e2610SBrian Somers       log_Printf(LogCHAT, "Expect(%d): %s\n", c->TimeoutSec, c->argptr);
313b6dec9f0SBrian Somers       chat_SetTimeout(c);
314b6dec9f0SBrian Somers     }
315b6dec9f0SBrian Somers   }
316b6dec9f0SBrian Somers 
317b6dec9f0SBrian Somers   /*
318b6dec9f0SBrian Somers    * We now have c->argptr pointing at what we want to expect/send and
319b6dec9f0SBrian Somers    * c->state saying what we want to do... we now know what to put in
320b6dec9f0SBrian Somers    * the fd_set :-)
321b6dec9f0SBrian Somers    */
322b6dec9f0SBrian Somers 
323b6dec9f0SBrian Somers   if (c->state == CHAT_EXPECT)
3245d9e6103SBrian Somers     return physical_doUpdateSet(&c->physical->desc, r, NULL, e, n, 1);
325b6dec9f0SBrian Somers   else
3265d9e6103SBrian Somers     return physical_doUpdateSet(&c->physical->desc, NULL, w, e, n, 1);
327b6dec9f0SBrian Somers }
328b6dec9f0SBrian Somers 
329b6dec9f0SBrian Somers static int
chat_IsSet(struct fdescriptor * d,const fd_set * fdset)330f013f33eSBrian Somers chat_IsSet(struct fdescriptor *d, const fd_set *fdset)
331b6dec9f0SBrian Somers {
332b6dec9f0SBrian Somers   struct chat *c = descriptor2chat(d);
333ddd68080SBrian Somers   return c->argptr && physical_IsSet(&c->physical->desc, fdset);
334b6dec9f0SBrian Somers }
335b6dec9f0SBrian Somers 
336b6dec9f0SBrian Somers static void
chat_UpdateLog(struct chat * c,int in)337b6dec9f0SBrian Somers chat_UpdateLog(struct chat *c, int in)
338b6dec9f0SBrian Somers {
339dd7e2610SBrian Somers   if (log_IsKept(LogCHAT) || log_IsKept(LogCONNECT)) {
340b6dec9f0SBrian Somers     /*
341b6dec9f0SBrian Somers      * If a linefeed appears in the last `in' characters of `c's input
342b6dec9f0SBrian Somers      * buffer, output from there, all the way back to the last linefeed.
343b6dec9f0SBrian Somers      * This is called for every read of `in' bytes.
344b6dec9f0SBrian Somers      */
345486f40c9SBrian Somers     char *ptr, *end, *stop, ch;
346b6dec9f0SBrian Somers     int level;
347b6dec9f0SBrian Somers 
348dd7e2610SBrian Somers     level = log_IsKept(LogCHAT) ? LogCHAT : LogCONNECT;
349486f40c9SBrian Somers     if (in == -1)
350486f40c9SBrian Somers       end = ptr = c->bufend;
351486f40c9SBrian Somers     else {
352b6dec9f0SBrian Somers       ptr = c->bufend - in;
353b6dec9f0SBrian Somers       for (end = c->bufend - 1; end >= ptr; end--)
354b6dec9f0SBrian Somers         if (*end == '\n')
355b6dec9f0SBrian Somers           break;
356486f40c9SBrian Somers     }
357b6dec9f0SBrian Somers 
358b6dec9f0SBrian Somers     if (end >= ptr) {
359486f40c9SBrian Somers       for (ptr = c->bufend - (in == -1 ? 1 : in + 1); ptr >= c->bufstart; ptr--)
360b6dec9f0SBrian Somers         if (*ptr == '\n')
361b6dec9f0SBrian Somers           break;
362b6dec9f0SBrian Somers       ptr++;
363b6dec9f0SBrian Somers       stop = NULL;
364486f40c9SBrian Somers       while (stop < end) {
365486f40c9SBrian Somers         if ((stop = memchr(ptr, '\n', end - ptr)) == NULL)
366486f40c9SBrian Somers           stop = end;
367486f40c9SBrian Somers         ch = *stop;
368b6dec9f0SBrian Somers         *stop = '\0';
369b6dec9f0SBrian Somers         if (level == LogCHAT || strstr(ptr, "CONNECT"))
370dd7e2610SBrian Somers           log_Printf(level, "Received: %s\n", ptr);
371486f40c9SBrian Somers         *stop = ch;
372b6dec9f0SBrian Somers         ptr = stop + 1;
373b6dec9f0SBrian Somers       }
374b6dec9f0SBrian Somers     }
375b6dec9f0SBrian Somers   }
376b6dec9f0SBrian Somers }
377b6dec9f0SBrian Somers 
378b6dec9f0SBrian Somers static void
chat_Read(struct fdescriptor * d,struct bundle * bundle __unused,const fd_set * fdset __unused)379057f1760SBrian Somers chat_Read(struct fdescriptor *d, struct bundle *bundle __unused,
380057f1760SBrian Somers 	  const fd_set *fdset __unused)
381b6dec9f0SBrian Somers {
382b6dec9f0SBrian Somers   struct chat *c = descriptor2chat(d);
383b6dec9f0SBrian Somers 
384b6dec9f0SBrian Somers   if (c->state == CHAT_EXPECT) {
385b6dec9f0SBrian Somers     ssize_t in;
386486f40c9SBrian Somers     char *abegin, *ebegin, *begin, *aend, *eend, *end;
387486f40c9SBrian Somers     int n;
388b6dec9f0SBrian Somers 
389b6dec9f0SBrian Somers     /*
390b6dec9f0SBrian Somers      * XXX - should this read only 1 byte to guarantee that we don't
391b6dec9f0SBrian Somers      * swallow any ppp talk from the peer ?
392b6dec9f0SBrian Somers      */
393b6dec9f0SBrian Somers     in = BUFLEFT(c);
394057f1760SBrian Somers     if (in > (ssize_t)sizeof c->buf / 2)
395b6dec9f0SBrian Somers       in = sizeof c->buf / 2;
396b6dec9f0SBrian Somers 
397dd7e2610SBrian Somers     in = physical_Read(c->physical, c->bufend, in);
398b6dec9f0SBrian Somers     if (in <= 0)
399b6dec9f0SBrian Somers       return;
400b6dec9f0SBrian Somers 
401b6dec9f0SBrian Somers     /* `begin' and `end' delimit where we're going to strncmp() from */
402486f40c9SBrian Somers     ebegin = c->bufend - c->arglen + 1;
403486f40c9SBrian Somers     eend = ebegin + in;
404486f40c9SBrian Somers     if (ebegin < c->bufstart)
405486f40c9SBrian Somers       ebegin = c->bufstart;
406486f40c9SBrian Somers 
407486f40c9SBrian Somers     if (c->abort.num) {
408486f40c9SBrian Somers       abegin = c->bufend - c->abort.string[0].len + 1;
409486f40c9SBrian Somers       aend = c->bufend - c->abort.string[c->abort.num-1].len + in + 1;
410486f40c9SBrian Somers       if (abegin < c->bufstart)
411486f40c9SBrian Somers         abegin = c->bufstart;
412486f40c9SBrian Somers     } else {
413486f40c9SBrian Somers       abegin = ebegin;
414486f40c9SBrian Somers       aend = eend;
415486f40c9SBrian Somers     }
416486f40c9SBrian Somers     begin = abegin < ebegin ? abegin : ebegin;
417486f40c9SBrian Somers     end = aend < eend ? eend : aend;
418486f40c9SBrian Somers 
419b6dec9f0SBrian Somers     c->bufend += in;
420b6dec9f0SBrian Somers 
421b6dec9f0SBrian Somers     chat_UpdateLog(c, in);
422b6dec9f0SBrian Somers 
423b6dec9f0SBrian Somers     if (c->bufend > c->buf + sizeof c->buf / 2) {
424b6dec9f0SBrian Somers       /* Shuffle our receive buffer back a bit */
425b6dec9f0SBrian Somers       int chop;
426b6dec9f0SBrian Somers 
427b6dec9f0SBrian Somers       for (chop = begin - c->buf; chop; chop--)
428b6dec9f0SBrian Somers         if (c->buf[chop] == '\n')
429b6dec9f0SBrian Somers           /* found some already-logged garbage to remove :-) */
430b6dec9f0SBrian Somers           break;
431486f40c9SBrian Somers 
432486f40c9SBrian Somers       if (!chop)
433b6dec9f0SBrian Somers         chop = begin - c->buf;
434486f40c9SBrian Somers 
435b6dec9f0SBrian Somers       if (chop) {
436b6dec9f0SBrian Somers         char *from, *to;
437b6dec9f0SBrian Somers 
438b6dec9f0SBrian Somers         to = c->buf;
439b6dec9f0SBrian Somers         from = to + chop;
440b6dec9f0SBrian Somers         while (from < c->bufend)
441b6dec9f0SBrian Somers           *to++ = *from++;
442b6dec9f0SBrian Somers         c->bufstart -= chop;
443b6dec9f0SBrian Somers         c->bufend -= chop;
444b6dec9f0SBrian Somers         begin -= chop;
445b6dec9f0SBrian Somers         end -= chop;
446486f40c9SBrian Somers         abegin -= chop;
447486f40c9SBrian Somers         aend -= chop;
448486f40c9SBrian Somers         ebegin -= chop;
449486f40c9SBrian Somers         eend -= chop;
450b6dec9f0SBrian Somers       }
451b6dec9f0SBrian Somers     }
452b6dec9f0SBrian Somers 
453b6dec9f0SBrian Somers     for (; begin < end; begin++)
454486f40c9SBrian Somers       if (begin >= ebegin && begin < eend &&
455486f40c9SBrian Somers           !strncmp(begin, c->argptr, c->arglen)) {
456b6dec9f0SBrian Somers         /* Got it ! */
457ca618bcfSBrian Somers         timer_Stop(&c->timeout);
458486f40c9SBrian Somers         if (memchr(begin + c->arglen - 1, '\n',
459486f40c9SBrian Somers             c->bufend - begin - c->arglen + 1) == NULL) {
460486f40c9SBrian Somers           /* force it into the log */
461b6dec9f0SBrian Somers           end = c->bufend;
462b6dec9f0SBrian Somers           c->bufend = begin + c->arglen;
463486f40c9SBrian Somers           chat_UpdateLog(c, -1);
464b6dec9f0SBrian Somers           c->bufend = end;
465b6dec9f0SBrian Somers         }
466b6dec9f0SBrian Somers         c->bufstart = begin + c->arglen;
467b6dec9f0SBrian Somers         c->argptr += c->arglen;
468b6dec9f0SBrian Somers         c->arglen = 0;
469b6dec9f0SBrian Somers         break;
470486f40c9SBrian Somers       } else if (begin >= abegin && begin < aend) {
471486f40c9SBrian Somers         for (n = c->abort.num - 1; n >= 0; n--) {
472486f40c9SBrian Somers           if (begin + c->abort.string[n].len > c->bufend)
473486f40c9SBrian Somers             break;
474486f40c9SBrian Somers           if (!strncmp(begin, c->abort.string[n].data,
475486f40c9SBrian Somers                        c->abort.string[n].len)) {
476486f40c9SBrian Somers             if (memchr(begin + c->abort.string[n].len - 1, '\n',
477486f40c9SBrian Somers                 c->bufend - begin - c->abort.string[n].len + 1) == NULL) {
478486f40c9SBrian Somers               /* force it into the log */
479486f40c9SBrian Somers               end = c->bufend;
480486f40c9SBrian Somers               c->bufend = begin + c->abort.string[n].len;
481486f40c9SBrian Somers               chat_UpdateLog(c, -1);
482486f40c9SBrian Somers               c->bufend = end;
483486f40c9SBrian Somers             }
484486f40c9SBrian Somers             c->bufstart = begin + c->abort.string[n].len;
485486f40c9SBrian Somers             c->state = CHAT_FAILED;
486486f40c9SBrian Somers             return;
487486f40c9SBrian Somers           }
488486f40c9SBrian Somers         }
489b6dec9f0SBrian Somers       }
490b6dec9f0SBrian Somers   }
491b6dec9f0SBrian Somers }
492b6dec9f0SBrian Somers 
4931af29a6eSBrian Somers static int
chat_Write(struct fdescriptor * d,struct bundle * bundle __unused,const fd_set * fdset __unused)494057f1760SBrian Somers chat_Write(struct fdescriptor *d, struct bundle *bundle __unused,
495057f1760SBrian Somers 	   const fd_set *fdset __unused)
496b6dec9f0SBrian Somers {
497b6dec9f0SBrian Somers   struct chat *c = descriptor2chat(d);
4981af29a6eSBrian Somers   int result = 0;
499b6dec9f0SBrian Somers 
500b6dec9f0SBrian Somers   if (c->state == CHAT_SEND) {
501b6dec9f0SBrian Somers     int wrote;
502b6dec9f0SBrian Somers 
503b6dec9f0SBrian Somers     if (strstr(c->argv[c->arg], "\\P"))            /* Don't log the password */
504dd7e2610SBrian Somers       log_Printf(LogCHAT, "Send: %s\n", c->argv[c->arg]);
505b6dec9f0SBrian Somers     else {
506b6dec9f0SBrian Somers       int sz;
507b6dec9f0SBrian Somers 
508b6dec9f0SBrian Somers       sz = c->arglen - 1;
509b6dec9f0SBrian Somers       while (sz >= 0 && c->argptr[sz] == '\n')
510b6dec9f0SBrian Somers         sz--;
511dd7e2610SBrian Somers       log_Printf(LogCHAT, "Send: %.*s\n", sz + 1, c->argptr);
512b6dec9f0SBrian Somers     }
513b6dec9f0SBrian Somers 
514dd7e2610SBrian Somers     if (physical_IsSync(c->physical)) {
5154d9c2bb9SBrian Somers       /*
5164d9c2bb9SBrian Somers        * XXX: Fix me
5174d9c2bb9SBrian Somers        * This data should be stuffed down through the link layers
5184d9c2bb9SBrian Somers        */
519b6dec9f0SBrian Somers       /* There's always room for the HDLC header */
520b6dec9f0SBrian Somers       c->argptr -= 2;
521b6dec9f0SBrian Somers       c->arglen += 2;
522b6dec9f0SBrian Somers       memcpy(c->argptr, "\377\003", 2);	/* Prepend HDLC header */
523b6dec9f0SBrian Somers     }
524b6dec9f0SBrian Somers 
525dd7e2610SBrian Somers     wrote = physical_Write(c->physical, c->argptr, c->arglen);
526fb11a9c2SBrian Somers     result = wrote > 0 ? 1 : 0;
527b6dec9f0SBrian Somers     if (wrote == -1) {
528fb11a9c2SBrian Somers       if (errno != EINTR) {
529fb11a9c2SBrian Somers         log_Printf(LogWARN, "chat_Write: %s\n", strerror(errno));
530fb11a9c2SBrian Somers 	result = -1;
531fb11a9c2SBrian Somers       }
532dd7e2610SBrian Somers       if (physical_IsSync(c->physical)) {
533b6dec9f0SBrian Somers         c->argptr += 2;
534b6dec9f0SBrian Somers         c->arglen -= 2;
535b6dec9f0SBrian Somers       }
536dd7e2610SBrian Somers     } else if (wrote < 2 && physical_IsSync(c->physical)) {
537b6dec9f0SBrian Somers       /* Oops - didn't even write our HDLC header ! */
538b6dec9f0SBrian Somers       c->argptr += 2;
539b6dec9f0SBrian Somers       c->arglen -= 2;
540b6dec9f0SBrian Somers     } else {
541b6dec9f0SBrian Somers       c->argptr += wrote;
542b6dec9f0SBrian Somers       c->arglen -= wrote;
543b6dec9f0SBrian Somers     }
544b6dec9f0SBrian Somers   }
5451af29a6eSBrian Somers 
5461af29a6eSBrian Somers   return result;
547b6dec9f0SBrian Somers }
548b6dec9f0SBrian Somers 
549b6dec9f0SBrian Somers void
chat_Init(struct chat * c,struct physical * p)550ffcfaec7SBrian Somers chat_Init(struct chat *c, struct physical *p)
551b6dec9f0SBrian Somers {
552b6dec9f0SBrian Somers   c->desc.type = CHAT_DESCRIPTOR;
553b6dec9f0SBrian Somers   c->desc.UpdateSet = chat_UpdateSet;
554b6dec9f0SBrian Somers   c->desc.IsSet = chat_IsSet;
555b6dec9f0SBrian Somers   c->desc.Read = chat_Read;
556b6dec9f0SBrian Somers   c->desc.Write = chat_Write;
557b6dec9f0SBrian Somers   c->physical = p;
558ffcfaec7SBrian Somers   *c->script = '\0';
559ffcfaec7SBrian Somers   c->argc = 0;
560ffcfaec7SBrian Somers   c->arg = -1;
561ffcfaec7SBrian Somers   c->argptr = NULL;
562ffcfaec7SBrian Somers   c->nargptr = NULL;
563ffcfaec7SBrian Somers   c->bufstart = c->bufend = c->buf;
564b6dec9f0SBrian Somers 
565ffcfaec7SBrian Somers   memset(&c->pause, '\0', sizeof c->pause);
566ffcfaec7SBrian Somers   memset(&c->timeout, '\0', sizeof c->timeout);
567ffcfaec7SBrian Somers }
568ffcfaec7SBrian Somers 
569c39aa54eSBrian Somers int
chat_Setup(struct chat * c,const char * data,const char * phone)570ffcfaec7SBrian Somers chat_Setup(struct chat *c, const char *data, const char *phone)
571ffcfaec7SBrian Somers {
572b6dec9f0SBrian Somers   c->state = CHAT_EXPECT;
573b6dec9f0SBrian Somers 
5745b8b8060SBrian Somers   if (data == NULL) {
5755b8b8060SBrian Somers     *c->script = '\0';
5765b8b8060SBrian Somers     c->argc = 0;
5775b8b8060SBrian Somers   } else {
578b6dec9f0SBrian Somers     strncpy(c->script, data, sizeof c->script - 1);
579b6dec9f0SBrian Somers     c->script[sizeof c->script - 1] = '\0';
5805b78bdf8SBrian Somers     c->argc = MakeArgs(c->script, c->argv, VECSIZE(c->argv), PARSE_NOHASH);
5815b8b8060SBrian Somers   }
582b6dec9f0SBrian Somers 
583b6dec9f0SBrian Somers   c->arg = -1;
584b6dec9f0SBrian Somers   c->argptr = NULL;
585b6dec9f0SBrian Somers   c->nargptr = NULL;
586b6dec9f0SBrian Somers 
587b6dec9f0SBrian Somers   c->TimeoutSec = 30;
588b6dec9f0SBrian Somers   c->TimedOut = 0;
589f9545805SBrian Somers   c->phone = phone;
590486f40c9SBrian Somers   c->abort.num = 0;
591b6dec9f0SBrian Somers 
592ffcfaec7SBrian Somers   timer_Stop(&c->pause);
593ffcfaec7SBrian Somers   timer_Stop(&c->timeout);
594c39aa54eSBrian Somers 
595c39aa54eSBrian Somers   return c->argc >= 0;
596b6dec9f0SBrian Somers }
597b6dec9f0SBrian Somers 
598b6dec9f0SBrian Somers void
chat_Finish(struct chat * c)599ffcfaec7SBrian Somers chat_Finish(struct chat *c)
600b6dec9f0SBrian Somers {
601dd7e2610SBrian Somers   timer_Stop(&c->pause);
602dd7e2610SBrian Somers   timer_Stop(&c->timeout);
603486f40c9SBrian Somers   while (c->abort.num)
604486f40c9SBrian Somers     free(c->abort.string[--c->abort.num].data);
60539d94652SBrian Somers   c->abort.num = 0;
606b6dec9f0SBrian Somers }
607af57ed9fSAtsushi Murai 
608ffcfaec7SBrian Somers void
chat_Destroy(struct chat * c)609ffcfaec7SBrian Somers chat_Destroy(struct chat *c)
610ffcfaec7SBrian Somers {
611ffcfaec7SBrian Somers   chat_Finish(c);
612ffcfaec7SBrian Somers }
613ffcfaec7SBrian Somers 
614af57ed9fSAtsushi Murai /*
61599c02d39SWarner Losh  *  \c	don't add a cr
61699c02d39SWarner Losh  *  \d  Sleep a little (delay 2 seconds
61799c02d39SWarner Losh  *  \n  Line feed character
61899c02d39SWarner Losh  *  \P  Auth Key password
61999c02d39SWarner Losh  *  \p  pause 0.25 sec
620af57ed9fSAtsushi Murai  *  \r	Carrige return character
621af57ed9fSAtsushi Murai  *  \s  Space character
6221dff3fc5SAndrey A. Chernov  *  \T  Telephone number(s) (defined via `set phone')
623af57ed9fSAtsushi Murai  *  \t  Tab character
62499c02d39SWarner Losh  *  \U  Auth User
625af57ed9fSAtsushi Murai  */
62692f4ff1cSBrian Somers static char *
ExpandString(struct chat * c,const char * str,char * result,int reslen,int cr)6274d9c2bb9SBrian Somers ExpandString(struct chat *c, const char *str, char *result, int reslen, int cr)
628af57ed9fSAtsushi Murai {
6294d9c2bb9SBrian Somers   int len;
630af57ed9fSAtsushi Murai 
63199c02d39SWarner Losh   result[--reslen] = '\0';
63299c02d39SWarner Losh   while (*str && reslen > 0) {
633af57ed9fSAtsushi Murai     switch (*str) {
634af57ed9fSAtsushi Murai     case '\\':
635af57ed9fSAtsushi Murai       str++;
636af57ed9fSAtsushi Murai       switch (*str) {
637af57ed9fSAtsushi Murai       case 'c':
6384d9c2bb9SBrian Somers 	cr = 0;
639af57ed9fSAtsushi Murai 	break;
640af57ed9fSAtsushi Murai       case 'd':		/* Delay 2 seconds */
641b6dec9f0SBrian Somers         chat_Pause(c, 2 * SECTICKS);
642944f7098SBrian Somers 	break;
643af57ed9fSAtsushi Murai       case 'p':
644b6dec9f0SBrian Somers         chat_Pause(c, SECTICKS / 4);
6454d9c2bb9SBrian Somers 	break;		/* Delay 0.25 seconds */
646af57ed9fSAtsushi Murai       case 'n':
647944f7098SBrian Somers 	*result++ = '\n';
648944f7098SBrian Somers 	reslen--;
649944f7098SBrian Somers 	break;
650af57ed9fSAtsushi Murai       case 'r':
651944f7098SBrian Somers 	*result++ = '\r';
652944f7098SBrian Somers 	reslen--;
653944f7098SBrian Somers 	break;
654af57ed9fSAtsushi Murai       case 's':
655944f7098SBrian Somers 	*result++ = ' ';
656944f7098SBrian Somers 	reslen--;
657944f7098SBrian Somers 	break;
658af57ed9fSAtsushi Murai       case 't':
659944f7098SBrian Somers 	*result++ = '\t';
660944f7098SBrian Somers 	reslen--;
661944f7098SBrian Somers 	break;
662af57ed9fSAtsushi Murai       case 'P':
66392f4ff1cSBrian Somers 	strncpy(result, c->physical->dl->bundle->cfg.auth.key, reslen);
6644d9c2bb9SBrian Somers         len = strlen(result);
6654d9c2bb9SBrian Somers 	reslen -= len;
6664d9c2bb9SBrian Somers 	result += len;
667af57ed9fSAtsushi Murai 	break;
668af57ed9fSAtsushi Murai       case 'T':
66992b09558SBrian Somers         if (c->phone) {
670f9545805SBrian Somers           strncpy(result, c->phone, reslen);
6714d9c2bb9SBrian Somers           len = strlen(result);
6724d9c2bb9SBrian Somers           reslen -= len;
6734d9c2bb9SBrian Somers           result += len;
67492b09558SBrian Somers         }
675af57ed9fSAtsushi Murai 	break;
676af57ed9fSAtsushi Murai       case 'U':
67792f4ff1cSBrian Somers 	strncpy(result, c->physical->dl->bundle->cfg.auth.name, reslen);
6784d9c2bb9SBrian Somers         len = strlen(result);
6794d9c2bb9SBrian Somers 	reslen -= len;
6804d9c2bb9SBrian Somers 	result += len;
681af57ed9fSAtsushi Murai 	break;
682af57ed9fSAtsushi Murai       default:
68399c02d39SWarner Losh 	reslen--;
68499c02d39SWarner Losh 	*result++ = *str;
68599c02d39SWarner Losh 	break;
686af57ed9fSAtsushi Murai       }
68799c02d39SWarner Losh       if (*str)
68899c02d39SWarner Losh 	str++;
689af57ed9fSAtsushi Murai       break;
690af57ed9fSAtsushi Murai     case '^':
691af57ed9fSAtsushi Murai       str++;
69299c02d39SWarner Losh       if (*str) {
693af57ed9fSAtsushi Murai 	*result++ = *str++ & 0x1f;
69499c02d39SWarner Losh 	reslen--;
69599c02d39SWarner Losh       }
696af57ed9fSAtsushi Murai       break;
697af57ed9fSAtsushi Murai     default:
698af57ed9fSAtsushi Murai       *result++ = *str++;
69999c02d39SWarner Losh       reslen--;
700af57ed9fSAtsushi Murai       break;
701af57ed9fSAtsushi Murai     }
702af57ed9fSAtsushi Murai   }
70399c02d39SWarner Losh   if (--reslen > 0) {
7044d9c2bb9SBrian Somers     if (cr)
705af57ed9fSAtsushi Murai       *result++ = '\r';
70699c02d39SWarner Losh   }
70799c02d39SWarner Losh   if (--reslen > 0)
708af57ed9fSAtsushi Murai     *result++ = '\0';
709af57ed9fSAtsushi Murai   return (result);
710af57ed9fSAtsushi Murai }
711af57ed9fSAtsushi Murai 
712944f7098SBrian Somers static void
ExecStr(struct physical * physical,char * command,char * out,int olen)71363b73463SBrian Somers ExecStr(struct physical *physical, char *command, char *out, int olen)
714313572f3SBrian Somers {
715313572f3SBrian Somers   pid_t pid;
716313572f3SBrian Somers   int fids[2];
717a8103305SBrian Somers   char *argv[MAXARGS], *vector[MAXARGS], *startout, *endout;
71844e73c12SBrian Somers   int stat, nb, argc, i;
719313572f3SBrian Somers 
720dd7e2610SBrian Somers   log_Printf(LogCHAT, "Exec: %s\n", command);
7215b78bdf8SBrian Somers   if ((argc = MakeArgs(command, vector, VECSIZE(vector),
7225b78bdf8SBrian Somers                        PARSE_REDUCE|PARSE_NOHASH)) <= 0) {
723c39aa54eSBrian Somers     if (argc < 0)
724c39aa54eSBrian Somers       log_Printf(LogWARN, "Syntax error in exec command\n");
725c39aa54eSBrian Somers     *out = '\0';
726c39aa54eSBrian Somers     return;
727c39aa54eSBrian Somers   }
728313572f3SBrian Somers 
729313572f3SBrian Somers   if (pipe(fids) < 0) {
730dd7e2610SBrian Somers     log_Printf(LogCHAT, "Unable to create pipe in ExecStr: %s\n",
731313572f3SBrian Somers 	      strerror(errno));
732c78db105SBrian Somers     *out = '\0';
733c78db105SBrian Somers     return;
734313572f3SBrian Somers   }
735313572f3SBrian Somers   if ((pid = fork()) == 0) {
73646df5aa7SBrian Somers     command_Expand(argv, argc, (char const *const *)vector,
73746df5aa7SBrian Somers                    physical->dl->bundle, 0, getpid());
7381384bd27SBrian Somers     close(fids[0]);
739dd7e2610SBrian Somers     timer_TermService();
74044e73c12SBrian Somers     if (fids[1] == STDIN_FILENO)
74144e73c12SBrian Somers       fids[1] = dup(fids[1]);
7425d9e6103SBrian Somers     dup2(physical->fd, STDIN_FILENO);
743b762af4fSBrian Somers     dup2(fids[1], STDERR_FILENO);
74444e73c12SBrian Somers     dup2(STDIN_FILENO, STDOUT_FILENO);
745b762af4fSBrian Somers     close(3);
74644e73c12SBrian Somers     if (open(_PATH_TTY, O_RDWR) != 3)
74744e73c12SBrian Somers       open(_PATH_DEVNULL, O_RDWR);	/* Leave it closed if it fails... */
74844e73c12SBrian Somers     for (i = getdtablesize(); i > 3; i--)
74944e73c12SBrian Somers       fcntl(i, F_SETFD, 1);
75068602c3eSBrian Somers #ifndef NOSUID
751a19a5c02SBrian Somers     setuid(ID0realuid());
75268602c3eSBrian Somers #endif
753a8103305SBrian Somers     execvp(argv[0], argv);
754a8103305SBrian Somers     fprintf(stderr, "execvp: %s: %s\n", argv[0], strerror(errno));
75544e73c12SBrian Somers     _exit(127);
756313572f3SBrian Somers   } else {
757313572f3SBrian Somers     char *name = strdup(vector[0]);
758313572f3SBrian Somers 
759313572f3SBrian Somers     close(fids[1]);
760313572f3SBrian Somers     endout = out + olen - 1;
761313572f3SBrian Somers     startout = out;
762313572f3SBrian Somers     while (out < endout) {
763313572f3SBrian Somers       nb = read(fids[0], out, 1);
764313572f3SBrian Somers       if (nb <= 0)
765313572f3SBrian Somers 	break;
766313572f3SBrian Somers       out++;
767313572f3SBrian Somers     }
768313572f3SBrian Somers     *out = '\0';
769313572f3SBrian Somers     close(fids[0]);
770313572f3SBrian Somers     close(fids[1]);
771313572f3SBrian Somers     waitpid(pid, &stat, WNOHANG);
772313572f3SBrian Somers     if (WIFSIGNALED(stat)) {
773dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: signal %d\n", name, WTERMSIG(stat));
774313572f3SBrian Somers       free(name);
775c78db105SBrian Somers       *out = '\0';
776c78db105SBrian Somers       return;
777313572f3SBrian Somers     } else if (WIFEXITED(stat)) {
778313572f3SBrian Somers       switch (WEXITSTATUS(stat)) {
779313572f3SBrian Somers         case 0:
780313572f3SBrian Somers           free(name);
781313572f3SBrian Somers           break;
782313572f3SBrian Somers         case 127:
783dd7e2610SBrian Somers           log_Printf(LogWARN, "%s: %s\n", name, startout);
784313572f3SBrian Somers           free(name);
785c78db105SBrian Somers           *out = '\0';
786c78db105SBrian Somers           return;
787313572f3SBrian Somers           break;
788313572f3SBrian Somers         default:
789dd7e2610SBrian Somers           log_Printf(LogWARN, "%s: exit %d\n", name, WEXITSTATUS(stat));
790313572f3SBrian Somers           free(name);
791c78db105SBrian Somers           *out = '\0';
792c78db105SBrian Somers           return;
793313572f3SBrian Somers           break;
794313572f3SBrian Somers       }
795313572f3SBrian Somers     } else {
796dd7e2610SBrian Somers       log_Printf(LogWARN, "%s: Unexpected exit result\n", name);
797313572f3SBrian Somers       free(name);
798c78db105SBrian Somers       *out = '\0';
799c78db105SBrian Somers       return;
800313572f3SBrian Somers     }
801313572f3SBrian Somers   }
802313572f3SBrian Somers }
803