Initial import from FreeBSD RELENG_4:
[games.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  */
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, const fd_set *fdset)
374 {
375   struct chat *c = descriptor2chat(d);
376
377   if (c->state == CHAT_EXPECT) {
378     ssize_t in;
379     char *abegin, *ebegin, *begin, *aend, *eend, *end;
380     int n;
381
382     /*
383      * XXX - should this read only 1 byte to guarantee that we don't
384      * swallow any ppp talk from the peer ?
385      */
386     in = BUFLEFT(c);
387     if (in > sizeof c->buf / 2)
388       in = sizeof c->buf / 2;
389
390     in = physical_Read(c->physical, c->bufend, in);
391     if (in <= 0)
392       return;
393
394     /* `begin' and `end' delimit where we're going to strncmp() from */
395     ebegin = c->bufend - c->arglen + 1;
396     eend = ebegin + in;
397     if (ebegin < c->bufstart)
398       ebegin = c->bufstart;
399
400     if (c->abort.num) {
401       abegin = c->bufend - c->abort.string[0].len + 1;
402       aend = c->bufend - c->abort.string[c->abort.num-1].len + in + 1;
403       if (abegin < c->bufstart)
404         abegin = c->bufstart;
405     } else {
406       abegin = ebegin;
407       aend = eend;
408     }
409     begin = abegin < ebegin ? abegin : ebegin;
410     end = aend < eend ? eend : aend;
411
412     c->bufend += in;
413
414     chat_UpdateLog(c, in);
415
416     if (c->bufend > c->buf + sizeof c->buf / 2) {
417       /* Shuffle our receive buffer back a bit */
418       int chop;
419
420       for (chop = begin - c->buf; chop; chop--)
421         if (c->buf[chop] == '\n')
422           /* found some already-logged garbage to remove :-) */
423           break;
424
425       if (!chop)
426         chop = begin - c->buf;
427
428       if (chop) {
429         char *from, *to;
430
431         to = c->buf;
432         from = to + chop;
433         while (from < c->bufend)
434           *to++ = *from++;
435         c->bufstart -= chop;
436         c->bufend -= chop;
437         begin -= chop;
438         end -= chop;
439         abegin -= chop;
440         aend -= chop;
441         ebegin -= chop;
442         eend -= chop;
443       }
444     }
445
446     for (; begin < end; begin++)
447       if (begin >= ebegin && begin < eend &&
448           !strncmp(begin, c->argptr, c->arglen)) {
449         /* Got it ! */
450         timer_Stop(&c->timeout);
451         if (memchr(begin + c->arglen - 1, '\n',
452             c->bufend - begin - c->arglen + 1) == NULL) {
453           /* force it into the log */
454           end = c->bufend;
455           c->bufend = begin + c->arglen;
456           chat_UpdateLog(c, -1);
457           c->bufend = end;
458         }
459         c->bufstart = begin + c->arglen;
460         c->argptr += c->arglen;
461         c->arglen = 0;
462         break;
463       } else if (begin >= abegin && begin < aend) {
464         for (n = c->abort.num - 1; n >= 0; n--) {
465           if (begin + c->abort.string[n].len > c->bufend)
466             break;
467           if (!strncmp(begin, c->abort.string[n].data,
468                        c->abort.string[n].len)) {
469             if (memchr(begin + c->abort.string[n].len - 1, '\n',
470                 c->bufend - begin - c->abort.string[n].len + 1) == NULL) {
471               /* force it into the log */
472               end = c->bufend;
473               c->bufend = begin + c->abort.string[n].len;
474               chat_UpdateLog(c, -1);
475               c->bufend = end;
476             }
477             c->bufstart = begin + c->abort.string[n].len;
478             c->state = CHAT_FAILED;
479             return;
480           }
481         }
482       }
483   }
484 }
485
486 static int
487 chat_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
488 {
489   struct chat *c = descriptor2chat(d);
490   int result = 0;
491
492   if (c->state == CHAT_SEND) {
493     int wrote;
494
495     if (strstr(c->argv[c->arg], "\\P"))            /* Don't log the password */
496       log_Printf(LogCHAT, "Send: %s\n", c->argv[c->arg]);
497     else {
498       int sz;
499
500       sz = c->arglen - 1;
501       while (sz >= 0 && c->argptr[sz] == '\n')
502         sz--;
503       log_Printf(LogCHAT, "Send: %.*s\n", sz + 1, c->argptr);
504     }
505
506     if (physical_IsSync(c->physical)) {
507       /*
508        * XXX: Fix me
509        * This data should be stuffed down through the link layers
510        */
511       /* There's always room for the HDLC header */
512       c->argptr -= 2;
513       c->arglen += 2;
514       memcpy(c->argptr, "\377\003", 2); /* Prepend HDLC header */
515     }
516
517     wrote = physical_Write(c->physical, c->argptr, c->arglen);
518     result = wrote > 0 ? 1 : 0;
519     if (wrote == -1) {
520       if (errno != EINTR) {
521         log_Printf(LogWARN, "chat_Write: %s\n", strerror(errno));
522         result = -1;
523       }
524       if (physical_IsSync(c->physical)) {
525         c->argptr += 2;
526         c->arglen -= 2;
527       }
528     } else if (wrote < 2 && physical_IsSync(c->physical)) {
529       /* Oops - didn't even write our HDLC header ! */
530       c->argptr += 2;
531       c->arglen -= 2;
532     } else {
533       c->argptr += wrote;
534       c->arglen -= wrote;
535     }
536   }
537
538   return result;
539 }
540
541 void
542 chat_Init(struct chat *c, struct physical *p)
543 {
544   c->desc.type = CHAT_DESCRIPTOR;
545   c->desc.UpdateSet = chat_UpdateSet;
546   c->desc.IsSet = chat_IsSet;
547   c->desc.Read = chat_Read;
548   c->desc.Write = chat_Write;
549   c->physical = p;
550   *c->script = '\0';
551   c->argc = 0;
552   c->arg = -1;
553   c->argptr = NULL;
554   c->nargptr = NULL;
555   c->bufstart = c->bufend = c->buf;
556
557   memset(&c->pause, '\0', sizeof c->pause);
558   memset(&c->timeout, '\0', sizeof c->timeout);
559 }
560
561 int
562 chat_Setup(struct chat *c, const char *data, const char *phone)
563 {
564   c->state = CHAT_EXPECT;
565
566   if (data == NULL) {
567     *c->script = '\0';
568     c->argc = 0;
569   } else {
570     strncpy(c->script, data, sizeof c->script - 1);
571     c->script[sizeof c->script - 1] = '\0';
572     c->argc = MakeArgs(c->script, c->argv, VECSIZE(c->argv), PARSE_NOHASH);
573   }
574
575   c->arg = -1;
576   c->argptr = NULL;
577   c->nargptr = NULL;
578
579   c->TimeoutSec = 30;
580   c->TimedOut = 0;
581   c->phone = phone;
582   c->abort.num = 0;
583
584   timer_Stop(&c->pause);
585   timer_Stop(&c->timeout);
586
587   return c->argc >= 0;
588 }
589
590 void
591 chat_Finish(struct chat *c)
592 {
593   timer_Stop(&c->pause);
594   timer_Stop(&c->timeout);
595   while (c->abort.num)
596     free(c->abort.string[--c->abort.num].data);
597   c->abort.num = 0;
598 }
599
600 void
601 chat_Destroy(struct chat *c)
602 {
603   chat_Finish(c);
604 }
605
606 /*
607  *  \c  don't add a cr
608  *  \d  Sleep a little (delay 2 seconds
609  *  \n  Line feed character
610  *  \P  Auth Key password
611  *  \p  pause 0.25 sec
612  *  \r  Carrige return character
613  *  \s  Space character
614  *  \T  Telephone number(s) (defined via `set phone')
615  *  \t  Tab character
616  *  \U  Auth User
617  */
618 static char *
619 ExpandString(struct chat *c, const char *str, char *result, int reslen, int cr)
620 {
621   int len;
622
623   result[--reslen] = '\0';
624   while (*str && reslen > 0) {
625     switch (*str) {
626     case '\\':
627       str++;
628       switch (*str) {
629       case 'c':
630         cr = 0;
631         break;
632       case 'd':         /* Delay 2 seconds */
633         chat_Pause(c, 2 * SECTICKS);
634         break;
635       case 'p':
636         chat_Pause(c, SECTICKS / 4);
637         break;          /* Delay 0.25 seconds */
638       case 'n':
639         *result++ = '\n';
640         reslen--;
641         break;
642       case 'r':
643         *result++ = '\r';
644         reslen--;
645         break;
646       case 's':
647         *result++ = ' ';
648         reslen--;
649         break;
650       case 't':
651         *result++ = '\t';
652         reslen--;
653         break;
654       case 'P':
655         strncpy(result, c->physical->dl->bundle->cfg.auth.key, reslen);
656         len = strlen(result);
657         reslen -= len;
658         result += len;
659         break;
660       case 'T':
661         if (c->phone) {
662           strncpy(result, c->phone, reslen);
663           len = strlen(result);
664           reslen -= len;
665           result += len;
666         }
667         break;
668       case 'U':
669         strncpy(result, c->physical->dl->bundle->cfg.auth.name, reslen);
670         len = strlen(result);
671         reslen -= len;
672         result += len;
673         break;
674       default:
675         reslen--;
676         *result++ = *str;
677         break;
678       }
679       if (*str)
680         str++;
681       break;
682     case '^':
683       str++;
684       if (*str) {
685         *result++ = *str++ & 0x1f;
686         reslen--;
687       }
688       break;
689     default:
690       *result++ = *str++;
691       reslen--;
692       break;
693     }
694   }
695   if (--reslen > 0) {
696     if (cr)
697       *result++ = '\r';
698   }
699   if (--reslen > 0)
700     *result++ = '\0';
701   return (result);
702 }
703
704 static void
705 ExecStr(struct physical *physical, char *command, char *out, int olen)
706 {
707   pid_t pid;
708   int fids[2];
709   char *argv[MAXARGS], *vector[MAXARGS], *startout, *endout;
710   int stat, nb, argc, i;
711
712   log_Printf(LogCHAT, "Exec: %s\n", command);
713   if ((argc = MakeArgs(command, vector, VECSIZE(vector),
714                        PARSE_REDUCE|PARSE_NOHASH)) <= 0) {
715     if (argc < 0)
716       log_Printf(LogWARN, "Syntax error in exec command\n");
717     *out = '\0';
718     return;
719   }
720
721   if (pipe(fids) < 0) {
722     log_Printf(LogCHAT, "Unable to create pipe in ExecStr: %s\n",
723               strerror(errno));
724     *out = '\0';
725     return;
726   }
727   if ((pid = fork()) == 0) {
728     command_Expand(argv, argc, (char const *const *)vector,
729                    physical->dl->bundle, 0, getpid());
730     close(fids[0]);
731     timer_TermService();
732     if (fids[1] == STDIN_FILENO)
733       fids[1] = dup(fids[1]);
734     dup2(physical->fd, STDIN_FILENO);
735     dup2(fids[1], STDERR_FILENO);
736     dup2(STDIN_FILENO, STDOUT_FILENO);
737     close(3);
738     if (open(_PATH_TTY, O_RDWR) != 3)
739       open(_PATH_DEVNULL, O_RDWR);      /* Leave it closed if it fails... */
740     for (i = getdtablesize(); i > 3; i--)
741       fcntl(i, F_SETFD, 1);
742 #ifndef NOSUID
743     setuid(ID0realuid());
744 #endif
745     execvp(argv[0], argv);
746     fprintf(stderr, "execvp: %s: %s\n", argv[0], strerror(errno));
747     _exit(127);
748   } else {
749     char *name = strdup(vector[0]);
750
751     close(fids[1]);
752     endout = out + olen - 1;
753     startout = out;
754     while (out < endout) {
755       nb = read(fids[0], out, 1);
756       if (nb <= 0)
757         break;
758       out++;
759     }
760     *out = '\0';
761     close(fids[0]);
762     close(fids[1]);
763     waitpid(pid, &stat, WNOHANG);
764     if (WIFSIGNALED(stat)) {
765       log_Printf(LogWARN, "%s: signal %d\n", name, WTERMSIG(stat));
766       free(name);
767       *out = '\0';
768       return;
769     } else if (WIFEXITED(stat)) {
770       switch (WEXITSTATUS(stat)) {
771         case 0:
772           free(name);
773           break;
774         case 127:
775           log_Printf(LogWARN, "%s: %s\n", name, startout);
776           free(name);
777           *out = '\0';
778           return;
779           break;
780         default:
781           log_Printf(LogWARN, "%s: exit %d\n", name, WEXITSTATUS(stat));
782           free(name);
783           *out = '\0';
784           return;
785           break;
786       }
787     } else {
788       log_Printf(LogWARN, "%s: Unexpected exit result\n", name);
789       free(name);
790       *out = '\0';
791       return;
792     }
793   }
794 }