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