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