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