xref: /freebsd/usr.sbin/ppp/chat.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/wait.h>
43 #include <termios.h>
44 #include <unistd.h>
45 
46 #include "layer.h"
47 #include "mbuf.h"
48 #include "log.h"
49 #include "defs.h"
50 #include "timer.h"
51 #include "lqr.h"
52 #include "hdlc.h"
53 #include "throughput.h"
54 #include "fsm.h"
55 #include "lcp.h"
56 #include "ccp.h"
57 #include "link.h"
58 #include "async.h"
59 #include "descriptor.h"
60 #include "physical.h"
61 #include "chat.h"
62 #include "mp.h"
63 #include "auth.h"
64 #include "chap.h"
65 #include "slcompress.h"
66 #include "iplist.h"
67 #include "ncpaddr.h"
68 #include "ipcp.h"
69 #include "filter.h"
70 #include "cbcp.h"
71 #include "command.h"
72 #include "datalink.h"
73 #ifndef NORADIUS
74 #include "radius.h"
75 #endif
76 #include "ipv6cp.h"
77 #include "ncp.h"
78 #include "bundle.h"
79 #include "id.h"
80 
81 #define BUFLEFT(c) (sizeof (c)->buf - ((c)->bufend - (c)->buf))
82 
83 static void ExecStr(struct physical *, char *, char *, int);
84 static char *ExpandString(struct chat *, const char *, char *, int, int);
85 
86 static void
87 chat_PauseTimer(void *v)
88 {
89   struct chat *c = (struct chat *)v;
90   timer_Stop(&c->pause);
91   c->pause.load = 0;
92 }
93 
94 static void
95 chat_Pause(struct chat *c, u_long load)
96 {
97   timer_Stop(&c->pause);
98   c->pause.load += load;
99   c->pause.func = chat_PauseTimer;
100   c->pause.name = "chat pause";
101   c->pause.arg = c;
102   timer_Start(&c->pause);
103 }
104 
105 static void
106 chat_TimeoutTimer(void *v)
107 {
108   struct chat *c = (struct chat *)v;
109   timer_Stop(&c->timeout);
110   c->TimedOut = 1;
111 }
112 
113 static void
114 chat_SetTimeout(struct chat *c)
115 {
116   timer_Stop(&c->timeout);
117   if (c->TimeoutSec > 0) {
118     c->timeout.load = SECTICKS * c->TimeoutSec;
119     c->timeout.func = chat_TimeoutTimer;
120     c->timeout.name = "chat timeout";
121     c->timeout.arg = c;
122     timer_Start(&c->timeout);
123   }
124 }
125 
126 static char *
127 chat_NextChar(char *ptr, char ch)
128 {
129   for (; *ptr; ptr++)
130     if (*ptr == ch)
131       return ptr;
132     else if (*ptr == '\\')
133       if (*++ptr == '\0')
134         return NULL;
135 
136   return NULL;
137 }
138 
139 static int
140 chat_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
141 {
142   struct chat *c = descriptor2chat(d);
143   int special, gotabort, gottimeout, needcr;
144   int TimedOut = c->TimedOut;
145   static char arg_term;		/* An empty string */
146 
147   if (c->pause.state == TIMER_RUNNING)
148     return 0;
149 
150   if (TimedOut) {
151     log_Printf(LogCHAT, "Expect timeout\n");
152     if (c->nargptr == NULL)
153       c->state = CHAT_FAILED;
154     else {
155       /* c->state = CHAT_EXPECT; */
156       c->argptr = &arg_term;
157       /*
158 	We have to clear the input buffer, because it contains output
159 	from the previous (timed out) command.
160       */
161       c->bufstart = c->bufend;
162     }
163     c->TimedOut = 0;
164   }
165 
166   if (c->state != CHAT_EXPECT && c->state != CHAT_SEND)
167     return 0;
168 
169   gottimeout = gotabort = 0;
170 
171   if (c->arg < c->argc && (c->arg < 0 || *c->argptr == '\0')) {
172     /* Go get the next string */
173     if (c->arg < 0 || c->state == CHAT_SEND)
174       c->state = CHAT_EXPECT;
175     else
176       c->state = CHAT_SEND;
177 
178     special = 1;
179     while (special && (c->nargptr || c->arg < c->argc - 1)) {
180       if (c->arg < 0 || (!TimedOut && c->state == CHAT_SEND))
181         c->nargptr = NULL;
182 
183       if (c->nargptr != NULL) {
184         /* We're doing expect-send-expect.... */
185         c->argptr = c->nargptr;
186         /* Put the '-' back in case we ever want to rerun our script */
187         c->nargptr[-1] = '-';
188         c->nargptr = chat_NextChar(c->nargptr, '-');
189         if (c->nargptr != NULL)
190           *c->nargptr++ = '\0';
191       } else {
192         int minus;
193 
194         if ((c->argptr = c->argv[++c->arg]) == NULL) {
195           /* End of script - all ok */
196           c->state = CHAT_DONE;
197           return 0;
198         }
199 
200         if (c->state == CHAT_EXPECT) {
201           /* Look for expect-send-expect sequence */
202           c->nargptr = c->argptr;
203           minus = 0;
204           while ((c->nargptr = chat_NextChar(c->nargptr, '-'))) {
205             c->nargptr++;
206             minus++;
207           }
208 
209           if (minus % 2)
210             log_Printf(LogWARN, "chat_UpdateSet: \"%s\": Uneven number of"
211                       " '-' chars, all ignored\n", c->argptr);
212           else if (minus) {
213             c->nargptr = chat_NextChar(c->argptr, '-');
214             *c->nargptr++ = '\0';
215           }
216         }
217       }
218 
219       /*
220        * c->argptr now temporarily points into c->script (via c->argv)
221        * If it's an expect-send-expect sequence, we've just got the correct
222        * portion of that sequence.
223        */
224 
225       needcr = c->state == CHAT_SEND &&
226                (*c->argptr != '!' || c->argptr[1] == '!');
227 
228       /* We leave room for a potential HDLC header in the target string */
229       ExpandString(c, c->argptr, c->exp + 2, sizeof c->exp - 2, needcr);
230 
231       /*
232        * Now read our string.  If it's not a special string, we unset
233        * ``special'' to break out of the loop.
234        */
235       if (gotabort) {
236         if (c->abort.num < MAXABORTS) {
237           int len, i;
238 
239           len = strlen(c->exp+2);
240           for (i = 0; i < c->abort.num; i++)
241             if (len > c->abort.string[i].len) {
242               int last;
243 
244               for (last = c->abort.num; last > i; last--) {
245                 c->abort.string[last].data = c->abort.string[last-1].data;
246                 c->abort.string[last].len = c->abort.string[last-1].len;
247               }
248               break;
249             }
250           c->abort.string[i].len = len;
251           if ((c->abort.string[i].data = (char *)malloc(len+1)) != NULL) {
252             memcpy(c->abort.string[i].data, c->exp+2, len+1);
253             c->abort.num++;
254 	  }
255         } else
256           log_Printf(LogERROR, "chat_UpdateSet: too many abort strings\n");
257         gotabort = 0;
258       } else if (gottimeout) {
259         c->TimeoutSec = atoi(c->exp + 2);
260         if (c->TimeoutSec <= 0)
261           c->TimeoutSec = 30;
262         gottimeout = 0;
263       } else if (c->nargptr == NULL && !strcmp(c->exp+2, "ABORT"))
264         gotabort = 1;
265       else if (c->nargptr == NULL && !strcmp(c->exp+2, "TIMEOUT"))
266         gottimeout = 1;
267       else {
268         if (c->exp[2] == '!' && c->exp[3] != '!')
269           ExecStr(c->physical, c->exp + 3, c->exp + 3, sizeof c->exp - 3);
270 
271         if (c->exp[2] == '\0') {
272           /* Empty string, reparse (this may be better as a `goto start') */
273           c->argptr = &arg_term;
274           return chat_UpdateSet(d, r, w, e, n);
275         }
276 
277         special = 0;
278       }
279     }
280 
281     if (special) {
282       if (gottimeout)
283         log_Printf(LogWARN, "chat_UpdateSet: TIMEOUT: Argument expected\n");
284       else if (gotabort)
285         log_Printf(LogWARN, "chat_UpdateSet: ABORT: Argument expected\n");
286 
287       /* End of script - all ok */
288       c->state = CHAT_DONE;
289       return 0;
290     }
291 
292     /* set c->argptr to point in the right place */
293     c->argptr = c->exp + (c->exp[2] == '!' ? 3 : 2);
294     c->arglen = strlen(c->argptr);
295 
296     if (c->state == CHAT_EXPECT) {
297       /* We must check to see if the string's already been found ! */
298       char *begin, *end;
299 
300       end = c->bufend - c->arglen + 1;
301       if (end < c->bufstart)
302         end = c->bufstart;
303       for (begin = c->bufstart; begin < end; begin++)
304         if (!strncmp(begin, c->argptr, c->arglen)) {
305           c->bufstart = begin + c->arglen;
306           c->argptr += c->arglen;
307           c->arglen = 0;
308           /* Continue - we've already read our expect string */
309           return chat_UpdateSet(d, r, w, e, n);
310         }
311 
312       log_Printf(LogCHAT, "Expect(%d): %s\n", c->TimeoutSec, c->argptr);
313       chat_SetTimeout(c);
314     }
315   }
316 
317   /*
318    * We now have c->argptr pointing at what we want to expect/send and
319    * c->state saying what we want to do... we now know what to put in
320    * the fd_set :-)
321    */
322 
323   if (c->state == CHAT_EXPECT)
324     return physical_doUpdateSet(&c->physical->desc, r, NULL, e, n, 1);
325   else
326     return physical_doUpdateSet(&c->physical->desc, NULL, w, e, n, 1);
327 }
328 
329 static int
330 chat_IsSet(struct fdescriptor *d, const fd_set *fdset)
331 {
332   struct chat *c = descriptor2chat(d);
333   return c->argptr && physical_IsSet(&c->physical->desc, fdset);
334 }
335 
336 static void
337 chat_UpdateLog(struct chat *c, int in)
338 {
339   if (log_IsKept(LogCHAT) || log_IsKept(LogCONNECT)) {
340     /*
341      * If a linefeed appears in the last `in' characters of `c's input
342      * buffer, output from there, all the way back to the last linefeed.
343      * This is called for every read of `in' bytes.
344      */
345     char *ptr, *end, *stop, ch;
346     int level;
347 
348     level = log_IsKept(LogCHAT) ? LogCHAT : LogCONNECT;
349     if (in == -1)
350       end = ptr = c->bufend;
351     else {
352       ptr = c->bufend - in;
353       for (end = c->bufend - 1; end >= ptr; end--)
354         if (*end == '\n')
355           break;
356     }
357 
358     if (end >= ptr) {
359       for (ptr = c->bufend - (in == -1 ? 1 : in + 1); ptr >= c->bufstart; ptr--)
360         if (*ptr == '\n')
361           break;
362       ptr++;
363       stop = NULL;
364       while (stop < end) {
365         if ((stop = memchr(ptr, '\n', end - ptr)) == NULL)
366           stop = end;
367         ch = *stop;
368         *stop = '\0';
369         if (level == LogCHAT || strstr(ptr, "CONNECT"))
370           log_Printf(level, "Received: %s\n", ptr);
371         *stop = ch;
372         ptr = stop + 1;
373       }
374     }
375   }
376 }
377 
378 static void
379 chat_Read(struct fdescriptor *d, struct bundle *bundle __unused,
380 	  const fd_set *fdset __unused)
381 {
382   struct chat *c = descriptor2chat(d);
383 
384   if (c->state == CHAT_EXPECT) {
385     ssize_t in;
386     char *abegin, *ebegin, *begin, *aend, *eend, *end;
387     int n;
388 
389     /*
390      * XXX - should this read only 1 byte to guarantee that we don't
391      * swallow any ppp talk from the peer ?
392      */
393     in = BUFLEFT(c);
394     if (in > (ssize_t)sizeof c->buf / 2)
395       in = sizeof c->buf / 2;
396 
397     in = physical_Read(c->physical, c->bufend, in);
398     if (in <= 0)
399       return;
400 
401     /* `begin' and `end' delimit where we're going to strncmp() from */
402     ebegin = c->bufend - c->arglen + 1;
403     eend = ebegin + in;
404     if (ebegin < c->bufstart)
405       ebegin = c->bufstart;
406 
407     if (c->abort.num) {
408       abegin = c->bufend - c->abort.string[0].len + 1;
409       aend = c->bufend - c->abort.string[c->abort.num-1].len + in + 1;
410       if (abegin < c->bufstart)
411         abegin = c->bufstart;
412     } else {
413       abegin = ebegin;
414       aend = eend;
415     }
416     begin = abegin < ebegin ? abegin : ebegin;
417     end = aend < eend ? eend : aend;
418 
419     c->bufend += in;
420 
421     chat_UpdateLog(c, in);
422 
423     if (c->bufend > c->buf + sizeof c->buf / 2) {
424       /* Shuffle our receive buffer back a bit */
425       int chop;
426 
427       for (chop = begin - c->buf; chop; chop--)
428         if (c->buf[chop] == '\n')
429           /* found some already-logged garbage to remove :-) */
430           break;
431 
432       if (!chop)
433         chop = begin - c->buf;
434 
435       if (chop) {
436         char *from, *to;
437 
438         to = c->buf;
439         from = to + chop;
440         while (from < c->bufend)
441           *to++ = *from++;
442         c->bufstart -= chop;
443         c->bufend -= chop;
444         begin -= chop;
445         end -= chop;
446         abegin -= chop;
447         aend -= chop;
448         ebegin -= chop;
449         eend -= chop;
450       }
451     }
452 
453     for (; begin < end; begin++)
454       if (begin >= ebegin && begin < eend &&
455           !strncmp(begin, c->argptr, c->arglen)) {
456         /* Got it ! */
457         timer_Stop(&c->timeout);
458         if (memchr(begin + c->arglen - 1, '\n',
459             c->bufend - begin - c->arglen + 1) == NULL) {
460           /* force it into the log */
461           end = c->bufend;
462           c->bufend = begin + c->arglen;
463           chat_UpdateLog(c, -1);
464           c->bufend = end;
465         }
466         c->bufstart = begin + c->arglen;
467         c->argptr += c->arglen;
468         c->arglen = 0;
469         break;
470       } else if (begin >= abegin && begin < aend) {
471         for (n = c->abort.num - 1; n >= 0; n--) {
472           if (begin + c->abort.string[n].len > c->bufend)
473             break;
474           if (!strncmp(begin, c->abort.string[n].data,
475                        c->abort.string[n].len)) {
476             if (memchr(begin + c->abort.string[n].len - 1, '\n',
477                 c->bufend - begin - c->abort.string[n].len + 1) == NULL) {
478               /* force it into the log */
479               end = c->bufend;
480               c->bufend = begin + c->abort.string[n].len;
481               chat_UpdateLog(c, -1);
482               c->bufend = end;
483             }
484             c->bufstart = begin + c->abort.string[n].len;
485             c->state = CHAT_FAILED;
486             return;
487           }
488         }
489       }
490   }
491 }
492 
493 static int
494 chat_Write(struct fdescriptor *d, struct bundle *bundle __unused,
495 	   const fd_set *fdset __unused)
496 {
497   struct chat *c = descriptor2chat(d);
498   int result = 0;
499 
500   if (c->state == CHAT_SEND) {
501     int wrote;
502 
503     if (strstr(c->argv[c->arg], "\\P"))            /* Don't log the password */
504       log_Printf(LogCHAT, "Send: %s\n", c->argv[c->arg]);
505     else {
506       int sz;
507 
508       sz = c->arglen - 1;
509       while (sz >= 0 && c->argptr[sz] == '\n')
510         sz--;
511       log_Printf(LogCHAT, "Send: %.*s\n", sz + 1, c->argptr);
512     }
513 
514     if (physical_IsSync(c->physical)) {
515       /*
516        * XXX: Fix me
517        * This data should be stuffed down through the link layers
518        */
519       /* There's always room for the HDLC header */
520       c->argptr -= 2;
521       c->arglen += 2;
522       memcpy(c->argptr, "\377\003", 2);	/* Prepend HDLC header */
523     }
524 
525     wrote = physical_Write(c->physical, c->argptr, c->arglen);
526     result = wrote > 0 ? 1 : 0;
527     if (wrote == -1) {
528       if (errno != EINTR) {
529         log_Printf(LogWARN, "chat_Write: %s\n", strerror(errno));
530 	result = -1;
531       }
532       if (physical_IsSync(c->physical)) {
533         c->argptr += 2;
534         c->arglen -= 2;
535       }
536     } else if (wrote < 2 && physical_IsSync(c->physical)) {
537       /* Oops - didn't even write our HDLC header ! */
538       c->argptr += 2;
539       c->arglen -= 2;
540     } else {
541       c->argptr += wrote;
542       c->arglen -= wrote;
543     }
544   }
545 
546   return result;
547 }
548 
549 void
550 chat_Init(struct chat *c, struct physical *p)
551 {
552   c->desc.type = CHAT_DESCRIPTOR;
553   c->desc.UpdateSet = chat_UpdateSet;
554   c->desc.IsSet = chat_IsSet;
555   c->desc.Read = chat_Read;
556   c->desc.Write = chat_Write;
557   c->physical = p;
558   *c->script = '\0';
559   c->argc = 0;
560   c->arg = -1;
561   c->argptr = NULL;
562   c->nargptr = NULL;
563   c->bufstart = c->bufend = c->buf;
564 
565   memset(&c->pause, '\0', sizeof c->pause);
566   memset(&c->timeout, '\0', sizeof c->timeout);
567 }
568 
569 int
570 chat_Setup(struct chat *c, const char *data, const char *phone)
571 {
572   c->state = CHAT_EXPECT;
573 
574   if (data == NULL) {
575     *c->script = '\0';
576     c->argc = 0;
577   } else {
578     strncpy(c->script, data, sizeof c->script - 1);
579     c->script[sizeof c->script - 1] = '\0';
580     c->argc = MakeArgs(c->script, c->argv, VECSIZE(c->argv), PARSE_NOHASH);
581   }
582 
583   c->arg = -1;
584   c->argptr = NULL;
585   c->nargptr = NULL;
586 
587   c->TimeoutSec = 30;
588   c->TimedOut = 0;
589   c->phone = phone;
590   c->abort.num = 0;
591 
592   timer_Stop(&c->pause);
593   timer_Stop(&c->timeout);
594 
595   return c->argc >= 0;
596 }
597 
598 void
599 chat_Finish(struct chat *c)
600 {
601   timer_Stop(&c->pause);
602   timer_Stop(&c->timeout);
603   while (c->abort.num)
604     free(c->abort.string[--c->abort.num].data);
605   c->abort.num = 0;
606 }
607 
608 void
609 chat_Destroy(struct chat *c)
610 {
611   chat_Finish(c);
612 }
613 
614 /*
615  *  \c	don't add a cr
616  *  \d  Sleep a little (delay 2 seconds
617  *  \n  Line feed character
618  *  \P  Auth Key password
619  *  \p  pause 0.25 sec
620  *  \r	Carrige return character
621  *  \s  Space character
622  *  \T  Telephone number(s) (defined via `set phone')
623  *  \t  Tab character
624  *  \U  Auth User
625  */
626 static char *
627 ExpandString(struct chat *c, const char *str, char *result, int reslen, int cr)
628 {
629   int len;
630 
631   result[--reslen] = '\0';
632   while (*str && reslen > 0) {
633     switch (*str) {
634     case '\\':
635       str++;
636       switch (*str) {
637       case 'c':
638 	cr = 0;
639 	break;
640       case 'd':		/* Delay 2 seconds */
641         chat_Pause(c, 2 * SECTICKS);
642 	break;
643       case 'p':
644         chat_Pause(c, SECTICKS / 4);
645 	break;		/* Delay 0.25 seconds */
646       case 'n':
647 	*result++ = '\n';
648 	reslen--;
649 	break;
650       case 'r':
651 	*result++ = '\r';
652 	reslen--;
653 	break;
654       case 's':
655 	*result++ = ' ';
656 	reslen--;
657 	break;
658       case 't':
659 	*result++ = '\t';
660 	reslen--;
661 	break;
662       case 'P':
663 	strncpy(result, c->physical->dl->bundle->cfg.auth.key, reslen);
664         len = strlen(result);
665 	reslen -= len;
666 	result += len;
667 	break;
668       case 'T':
669         if (c->phone) {
670           strncpy(result, c->phone, reslen);
671           len = strlen(result);
672           reslen -= len;
673           result += len;
674         }
675 	break;
676       case 'U':
677 	strncpy(result, c->physical->dl->bundle->cfg.auth.name, reslen);
678         len = strlen(result);
679 	reslen -= len;
680 	result += len;
681 	break;
682       default:
683 	reslen--;
684 	*result++ = *str;
685 	break;
686       }
687       if (*str)
688 	str++;
689       break;
690     case '^':
691       str++;
692       if (*str) {
693 	*result++ = *str++ & 0x1f;
694 	reslen--;
695       }
696       break;
697     default:
698       *result++ = *str++;
699       reslen--;
700       break;
701     }
702   }
703   if (--reslen > 0) {
704     if (cr)
705       *result++ = '\r';
706   }
707   if (--reslen > 0)
708     *result++ = '\0';
709   return (result);
710 }
711 
712 static void
713 ExecStr(struct physical *physical, char *command, char *out, int olen)
714 {
715   pid_t pid;
716   int fids[2];
717   char *argv[MAXARGS], *vector[MAXARGS], *startout, *endout;
718   int stat, nb, argc, i;
719 
720   log_Printf(LogCHAT, "Exec: %s\n", command);
721   if ((argc = MakeArgs(command, vector, VECSIZE(vector),
722                        PARSE_REDUCE|PARSE_NOHASH)) <= 0) {
723     if (argc < 0)
724       log_Printf(LogWARN, "Syntax error in exec command\n");
725     *out = '\0';
726     return;
727   }
728 
729   if (pipe(fids) < 0) {
730     log_Printf(LogCHAT, "Unable to create pipe in ExecStr: %s\n",
731 	      strerror(errno));
732     *out = '\0';
733     return;
734   }
735   if ((pid = fork()) == 0) {
736     command_Expand(argv, argc, (char const *const *)vector,
737                    physical->dl->bundle, 0, getpid());
738     close(fids[0]);
739     timer_TermService();
740     if (fids[1] == STDIN_FILENO)
741       fids[1] = dup(fids[1]);
742     dup2(physical->fd, STDIN_FILENO);
743     dup2(fids[1], STDERR_FILENO);
744     dup2(STDIN_FILENO, STDOUT_FILENO);
745     close(3);
746     if (open(_PATH_TTY, O_RDWR) != 3)
747       open(_PATH_DEVNULL, O_RDWR);	/* Leave it closed if it fails... */
748     for (i = getdtablesize(); i > 3; i--)
749       fcntl(i, F_SETFD, 1);
750 #ifndef NOSUID
751     setuid(ID0realuid());
752 #endif
753     execvp(argv[0], argv);
754     fprintf(stderr, "execvp: %s: %s\n", argv[0], strerror(errno));
755     _exit(127);
756   } else {
757     char *name = strdup(vector[0]);
758 
759     close(fids[1]);
760     endout = out + olen - 1;
761     startout = out;
762     while (out < endout) {
763       nb = read(fids[0], out, 1);
764       if (nb <= 0)
765 	break;
766       out++;
767     }
768     *out = '\0';
769     close(fids[0]);
770     close(fids[1]);
771     waitpid(pid, &stat, WNOHANG);
772     if (WIFSIGNALED(stat)) {
773       log_Printf(LogWARN, "%s: signal %d\n", name, WTERMSIG(stat));
774       free(name);
775       *out = '\0';
776       return;
777     } else if (WIFEXITED(stat)) {
778       switch (WEXITSTATUS(stat)) {
779         case 0:
780           free(name);
781           break;
782         case 127:
783           log_Printf(LogWARN, "%s: %s\n", name, startout);
784           free(name);
785           *out = '\0';
786           return;
787           break;
788         default:
789           log_Printf(LogWARN, "%s: exit %d\n", name, WEXITSTATUS(stat));
790           free(name);
791           *out = '\0';
792           return;
793           break;
794       }
795     } else {
796       log_Printf(LogWARN, "%s: Unexpected exit result\n", name);
797       free(name);
798       *out = '\0';
799       return;
800     }
801   }
802 }
803