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