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