1998-02-13 05:10:26 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
1998-02-13 05:10:26 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
1998-02-13 05:10:26 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
1999-05-08 11:07:56 +00:00
|
|
|
* $Id: chat.c,v 1.54 1999/02/12 00:52:29 brian Exp $
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1998-02-13 05:10:26 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
#include <sys/param.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <netinet/in.h>
|
1998-04-03 19:24:49 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1998-04-28 01:25:46 +00:00
|
|
|
#include <sys/un.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#include <errno.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <fcntl.h>
|
1998-05-01 19:22:23 +00:00
|
|
|
#include <paths.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "layer.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-02-02 19:33:40 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "throughput.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "lcp.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "ccp.h"
|
1998-02-02 19:33:40 +00:00
|
|
|
#include "link.h"
|
|
|
|
#include "async.h"
|
1998-02-09 19:21:11 +00:00
|
|
|
#include "descriptor.h"
|
1998-01-29 00:49:32 +00:00
|
|
|
#include "physical.h"
|
1997-05-10 01:22:19 +00:00
|
|
|
#include "chat.h"
|
1998-04-03 19:24:49 +00:00
|
|
|
#include "mp.h"
|
|
|
|
#include "auth.h"
|
|
|
|
#include "chap.h"
|
|
|
|
#include "slcompress.h"
|
|
|
|
#include "iplist.h"
|
|
|
|
#include "ipcp.h"
|
|
|
|
#include "filter.h"
|
1998-08-07 18:42:51 +00:00
|
|
|
#include "cbcp.h"
|
1999-02-12 00:52:30 +00:00
|
|
|
#include "command.h"
|
1998-04-03 19:24:49 +00:00
|
|
|
#include "datalink.h"
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
1998-04-03 19:24:49 +00:00
|
|
|
#include "bundle.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
#define BUFLEFT(c) (sizeof (c)->buf - ((c)->bufend - (c)->buf))
|
|
|
|
|
|
|
|
static void ExecStr(struct physical *, char *, char *, int);
|
1998-03-09 19:25:35 +00:00
|
|
|
static char *ExpandString(struct chat *, const char *, char *, int, int);
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
chat_PauseTimer(void *v)
|
|
|
|
{
|
|
|
|
struct chat *c = (struct chat *)v;
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Stop(&c->pause);
|
1998-02-13 05:10:26 +00:00
|
|
|
c->pause.load = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chat_Pause(struct chat *c, u_long load)
|
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Stop(&c->pause);
|
1998-02-13 05:10:26 +00:00
|
|
|
c->pause.load += load;
|
|
|
|
c->pause.func = chat_PauseTimer;
|
1998-04-03 19:21:56 +00:00
|
|
|
c->pause.name = "chat pause";
|
1998-02-13 05:10:26 +00:00
|
|
|
c->pause.arg = c;
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Start(&c->pause);
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chat_TimeoutTimer(void *v)
|
|
|
|
{
|
|
|
|
struct chat *c = (struct chat *)v;
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Stop(&c->timeout);
|
1998-02-13 05:10:26 +00:00
|
|
|
c->TimedOut = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chat_SetTimeout(struct chat *c)
|
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Stop(&c->timeout);
|
1998-02-13 05:10:26 +00:00
|
|
|
if (c->TimeoutSec > 0) {
|
|
|
|
c->timeout.load = SECTICKS * c->TimeoutSec;
|
|
|
|
c->timeout.func = chat_TimeoutTimer;
|
1998-04-03 19:21:56 +00:00
|
|
|
c->timeout.name = "chat timeout";
|
1998-02-13 05:10:26 +00:00
|
|
|
c->timeout.arg = c;
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Start(&c->timeout);
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
chat_NextChar(char *ptr, char ch)
|
|
|
|
{
|
|
|
|
for (; *ptr; ptr++)
|
|
|
|
if (*ptr == ch)
|
|
|
|
return ptr;
|
|
|
|
else if (*ptr == '\\')
|
|
|
|
if (*++ptr == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
chat_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
|
|
|
|
{
|
|
|
|
struct chat *c = descriptor2chat(d);
|
|
|
|
int special, gotabort, gottimeout, needcr;
|
1998-04-10 23:51:33 +00:00
|
|
|
int TimedOut = c->TimedOut;
|
1998-06-15 19:06:58 +00:00
|
|
|
static char arg_term; /* An empty string */
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
if (c->pause.state == TIMER_RUNNING)
|
|
|
|
return 0;
|
|
|
|
|
1998-04-10 23:51:33 +00:00
|
|
|
if (TimedOut) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCHAT, "Expect timeout\n");
|
1998-04-10 23:51:33 +00:00
|
|
|
if (c->nargptr == NULL)
|
1998-02-13 05:10:26 +00:00
|
|
|
c->state = CHAT_FAILED;
|
|
|
|
else {
|
1998-04-10 23:51:33 +00:00
|
|
|
/* c->state = CHAT_EXPECT; */
|
1998-04-18 01:01:28 +00:00
|
|
|
c->argptr = &arg_term;
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
1998-04-10 23:51:33 +00:00
|
|
|
c->TimedOut = 0;
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c->state != CHAT_EXPECT && c->state != CHAT_SEND)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
gottimeout = gotabort = 0;
|
|
|
|
|
|
|
|
if (c->arg < c->argc && (c->arg < 0 || *c->argptr == '\0')) {
|
|
|
|
/* Go get the next string */
|
|
|
|
if (c->arg < 0 || c->state == CHAT_SEND)
|
|
|
|
c->state = CHAT_EXPECT;
|
|
|
|
else
|
|
|
|
c->state = CHAT_SEND;
|
|
|
|
|
|
|
|
special = 1;
|
|
|
|
while (special && (c->nargptr || c->arg < c->argc - 1)) {
|
1998-04-10 23:51:33 +00:00
|
|
|
if (c->arg < 0 || (!TimedOut && c->state == CHAT_SEND))
|
1998-02-13 05:10:26 +00:00
|
|
|
c->nargptr = NULL;
|
|
|
|
|
|
|
|
if (c->nargptr != NULL) {
|
|
|
|
/* We're doing expect-send-expect.... */
|
|
|
|
c->argptr = c->nargptr;
|
|
|
|
/* Put the '-' back in case we ever want to rerun our script */
|
|
|
|
c->nargptr[-1] = '-';
|
|
|
|
c->nargptr = chat_NextChar(c->nargptr, '-');
|
|
|
|
if (c->nargptr != NULL)
|
|
|
|
*c->nargptr++ = '\0';
|
|
|
|
} else {
|
|
|
|
int minus;
|
|
|
|
|
|
|
|
c->argptr = c->argv[++c->arg];
|
|
|
|
|
|
|
|
if (c->state == CHAT_EXPECT) {
|
|
|
|
/* Look for expect-send-expect sequence */
|
|
|
|
c->nargptr = c->argptr;
|
|
|
|
minus = 0;
|
|
|
|
while ((c->nargptr = chat_NextChar(c->nargptr, '-'))) {
|
|
|
|
c->nargptr++;
|
|
|
|
minus++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minus % 2)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "chat_UpdateSet: \"%s\": Uneven number of"
|
1998-02-13 05:10:26 +00:00
|
|
|
" '-' chars, all ignored\n", c->argptr);
|
|
|
|
else if (minus) {
|
|
|
|
c->nargptr = chat_NextChar(c->argptr, '-');
|
|
|
|
*c->nargptr++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* c->argptr now temporarily points into c->script (via c->argv)
|
|
|
|
* If it's an expect-send-expect sequence, we've just got the correct
|
|
|
|
* portion of that sequence.
|
|
|
|
*/
|
|
|
|
|
|
|
|
needcr = c->state == CHAT_SEND && *c->argptr != '!';
|
|
|
|
|
|
|
|
/* We leave room for a potential HDLC header in the target string */
|
1998-03-09 19:25:35 +00:00
|
|
|
ExpandString(c, c->argptr, c->exp + 2, sizeof c->exp - 2, needcr);
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
if (gotabort) {
|
1998-02-26 17:54:43 +00:00
|
|
|
if (c->abort.num < MAXABORTS) {
|
1998-04-18 01:01:28 +00:00
|
|
|
int len, i;
|
1998-02-26 17:54:43 +00:00
|
|
|
|
|
|
|
len = strlen(c->exp+2);
|
1998-04-18 01:01:28 +00:00
|
|
|
for (i = 0; i < c->abort.num; i++)
|
|
|
|
if (len > c->abort.string[i].len) {
|
1998-02-26 17:54:43 +00:00
|
|
|
int last;
|
|
|
|
|
1998-04-18 01:01:28 +00:00
|
|
|
for (last = c->abort.num; last > i; last--) {
|
1998-02-26 17:54:43 +00:00
|
|
|
c->abort.string[last].data = c->abort.string[last-1].data;
|
|
|
|
c->abort.string[last].len = c->abort.string[last-1].len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1998-04-18 01:01:28 +00:00
|
|
|
c->abort.string[i].len = len;
|
|
|
|
c->abort.string[i].data = (char *)malloc(len+1);
|
|
|
|
memcpy(c->abort.string[i].data, c->exp+2, len+1);
|
1998-02-26 17:54:43 +00:00
|
|
|
c->abort.num++;
|
|
|
|
} else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "chat_UpdateSet: too many abort strings\n");
|
1998-02-13 05:10:26 +00:00
|
|
|
gotabort = 0;
|
|
|
|
} else if (gottimeout) {
|
|
|
|
c->TimeoutSec = atoi(c->exp + 2);
|
|
|
|
if (c->TimeoutSec <= 0)
|
|
|
|
c->TimeoutSec = 30;
|
|
|
|
gottimeout = 0;
|
|
|
|
} else if (c->nargptr == NULL && !strcmp(c->exp+2, "ABORT"))
|
|
|
|
gotabort = 1;
|
|
|
|
else if (c->nargptr == NULL && !strcmp(c->exp+2, "TIMEOUT"))
|
|
|
|
gottimeout = 1;
|
|
|
|
else {
|
|
|
|
if (c->exp[2] == '!')
|
|
|
|
ExecStr(c->physical, c->exp + 3, c->exp + 2, sizeof c->exp - 2);
|
|
|
|
|
1998-02-18 00:28:06 +00:00
|
|
|
if (c->exp[2] == '\0') {
|
1998-02-13 05:10:26 +00:00
|
|
|
/* Empty string, reparse (this may be better as a `goto start') */
|
1998-04-18 01:01:28 +00:00
|
|
|
c->argptr = &arg_term;
|
1998-02-13 05:10:26 +00:00
|
|
|
return chat_UpdateSet(d, r, w, e, n);
|
1998-02-18 00:28:06 +00:00
|
|
|
}
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
special = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (special) {
|
|
|
|
if (gottimeout)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "chat_UpdateSet: TIMEOUT: Argument expected\n");
|
1998-02-13 05:10:26 +00:00
|
|
|
else if (gotabort)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "chat_UpdateSet: ABORT: Argument expected\n");
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
/* End of script - all ok */
|
|
|
|
c->state = CHAT_DONE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set c->argptr to point in the right place */
|
|
|
|
c->argptr = c->exp + 2;
|
|
|
|
c->arglen = strlen(c->argptr);
|
|
|
|
|
|
|
|
if (c->state == CHAT_EXPECT) {
|
|
|
|
/* We must check to see if the string's already been found ! */
|
|
|
|
char *begin, *end;
|
|
|
|
|
|
|
|
end = c->bufend - c->arglen + 1;
|
|
|
|
if (end < c->bufstart)
|
|
|
|
end = c->bufstart;
|
|
|
|
for (begin = c->bufstart; begin < end; begin++)
|
|
|
|
if (!strncmp(begin, c->argptr, c->arglen)) {
|
|
|
|
c->bufstart = begin + c->arglen;
|
|
|
|
c->argptr += c->arglen;
|
|
|
|
c->arglen = 0;
|
|
|
|
/* Continue - we've already read our expect string */
|
|
|
|
return chat_UpdateSet(d, r, w, e, n);
|
|
|
|
}
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCHAT, "Expect(%d): %s\n", c->TimeoutSec, c->argptr);
|
1998-02-13 05:10:26 +00:00
|
|
|
chat_SetTimeout(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We now have c->argptr pointing at what we want to expect/send and
|
|
|
|
* c->state saying what we want to do... we now know what to put in
|
|
|
|
* the fd_set :-)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (c->state == CHAT_EXPECT)
|
1999-05-08 11:07:56 +00:00
|
|
|
return physical_doUpdateSet(&c->physical->desc, r, NULL, e, n, 1);
|
1998-02-13 05:10:26 +00:00
|
|
|
else
|
1999-05-08 11:07:56 +00:00
|
|
|
return physical_doUpdateSet(&c->physical->desc, NULL, w, e, n, 1);
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-03-20 19:48:28 +00:00
|
|
|
chat_IsSet(struct descriptor *d, const fd_set *fdset)
|
1998-02-13 05:10:26 +00:00
|
|
|
{
|
|
|
|
struct chat *c = descriptor2chat(d);
|
1998-05-01 19:26:12 +00:00
|
|
|
return physical_IsSet(&c->physical->desc, fdset);
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chat_UpdateLog(struct chat *c, int in)
|
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
if (log_IsKept(LogCHAT) || log_IsKept(LogCONNECT)) {
|
1998-02-13 05:10:26 +00:00
|
|
|
/*
|
|
|
|
* If a linefeed appears in the last `in' characters of `c's input
|
|
|
|
* buffer, output from there, all the way back to the last linefeed.
|
|
|
|
* This is called for every read of `in' bytes.
|
|
|
|
*/
|
1998-02-26 17:54:43 +00:00
|
|
|
char *ptr, *end, *stop, ch;
|
1998-02-13 05:10:26 +00:00
|
|
|
int level;
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
level = log_IsKept(LogCHAT) ? LogCHAT : LogCONNECT;
|
1998-02-26 17:54:43 +00:00
|
|
|
if (in == -1)
|
|
|
|
end = ptr = c->bufend;
|
|
|
|
else {
|
|
|
|
ptr = c->bufend - in;
|
|
|
|
for (end = c->bufend - 1; end >= ptr; end--)
|
|
|
|
if (*end == '\n')
|
|
|
|
break;
|
|
|
|
}
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
if (end >= ptr) {
|
1998-02-26 17:54:43 +00:00
|
|
|
for (ptr = c->bufend - (in == -1 ? 1 : in + 1); ptr >= c->bufstart; ptr--)
|
1998-02-13 05:10:26 +00:00
|
|
|
if (*ptr == '\n')
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
stop = NULL;
|
1998-02-26 17:54:43 +00:00
|
|
|
while (stop < end) {
|
|
|
|
if ((stop = memchr(ptr, '\n', end - ptr)) == NULL)
|
|
|
|
stop = end;
|
|
|
|
ch = *stop;
|
1998-02-13 05:10:26 +00:00
|
|
|
*stop = '\0';
|
|
|
|
if (level == LogCHAT || strstr(ptr, "CONNECT"))
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(level, "Received: %s\n", ptr);
|
1998-02-26 17:54:43 +00:00
|
|
|
*stop = ch;
|
1998-02-13 05:10:26 +00:00
|
|
|
ptr = stop + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chat_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
|
|
|
{
|
|
|
|
struct chat *c = descriptor2chat(d);
|
|
|
|
|
|
|
|
if (c->state == CHAT_EXPECT) {
|
|
|
|
ssize_t in;
|
1998-02-26 17:54:43 +00:00
|
|
|
char *abegin, *ebegin, *begin, *aend, *eend, *end;
|
|
|
|
int n;
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - should this read only 1 byte to guarantee that we don't
|
|
|
|
* swallow any ppp talk from the peer ?
|
|
|
|
*/
|
|
|
|
in = BUFLEFT(c);
|
|
|
|
if (in > sizeof c->buf / 2)
|
|
|
|
in = sizeof c->buf / 2;
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
in = physical_Read(c->physical, c->bufend, in);
|
1998-02-13 05:10:26 +00:00
|
|
|
if (in <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* `begin' and `end' delimit where we're going to strncmp() from */
|
1998-02-26 17:54:43 +00:00
|
|
|
ebegin = c->bufend - c->arglen + 1;
|
|
|
|
eend = ebegin + in;
|
|
|
|
if (ebegin < c->bufstart)
|
|
|
|
ebegin = c->bufstart;
|
|
|
|
|
|
|
|
if (c->abort.num) {
|
|
|
|
abegin = c->bufend - c->abort.string[0].len + 1;
|
|
|
|
aend = c->bufend - c->abort.string[c->abort.num-1].len + in + 1;
|
|
|
|
if (abegin < c->bufstart)
|
|
|
|
abegin = c->bufstart;
|
|
|
|
} else {
|
|
|
|
abegin = ebegin;
|
|
|
|
aend = eend;
|
|
|
|
}
|
|
|
|
begin = abegin < ebegin ? abegin : ebegin;
|
|
|
|
end = aend < eend ? eend : aend;
|
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
c->bufend += in;
|
|
|
|
|
|
|
|
chat_UpdateLog(c, in);
|
|
|
|
|
|
|
|
if (c->bufend > c->buf + sizeof c->buf / 2) {
|
|
|
|
/* Shuffle our receive buffer back a bit */
|
|
|
|
int chop;
|
|
|
|
|
|
|
|
for (chop = begin - c->buf; chop; chop--)
|
|
|
|
if (c->buf[chop] == '\n')
|
|
|
|
/* found some already-logged garbage to remove :-) */
|
|
|
|
break;
|
1998-02-26 17:54:43 +00:00
|
|
|
|
|
|
|
if (!chop)
|
1998-02-13 05:10:26 +00:00
|
|
|
chop = begin - c->buf;
|
1998-02-26 17:54:43 +00:00
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
if (chop) {
|
|
|
|
char *from, *to;
|
|
|
|
|
|
|
|
to = c->buf;
|
|
|
|
from = to + chop;
|
|
|
|
while (from < c->bufend)
|
|
|
|
*to++ = *from++;
|
|
|
|
c->bufstart -= chop;
|
|
|
|
c->bufend -= chop;
|
|
|
|
begin -= chop;
|
|
|
|
end -= chop;
|
1998-02-26 17:54:43 +00:00
|
|
|
abegin -= chop;
|
|
|
|
aend -= chop;
|
|
|
|
ebegin -= chop;
|
|
|
|
eend -= chop;
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; begin < end; begin++)
|
1998-02-26 17:54:43 +00:00
|
|
|
if (begin >= ebegin && begin < eend &&
|
|
|
|
!strncmp(begin, c->argptr, c->arglen)) {
|
1998-02-13 05:10:26 +00:00
|
|
|
/* Got it ! */
|
1998-10-27 22:53:25 +00:00
|
|
|
timer_Stop(&c->timeout);
|
1998-02-26 17:54:43 +00:00
|
|
|
if (memchr(begin + c->arglen - 1, '\n',
|
|
|
|
c->bufend - begin - c->arglen + 1) == NULL) {
|
|
|
|
/* force it into the log */
|
1998-02-13 05:10:26 +00:00
|
|
|
end = c->bufend;
|
|
|
|
c->bufend = begin + c->arglen;
|
1998-02-26 17:54:43 +00:00
|
|
|
chat_UpdateLog(c, -1);
|
1998-02-13 05:10:26 +00:00
|
|
|
c->bufend = end;
|
|
|
|
}
|
|
|
|
c->bufstart = begin + c->arglen;
|
|
|
|
c->argptr += c->arglen;
|
|
|
|
c->arglen = 0;
|
|
|
|
break;
|
1998-02-26 17:54:43 +00:00
|
|
|
} else if (begin >= abegin && begin < aend) {
|
|
|
|
for (n = c->abort.num - 1; n >= 0; n--) {
|
|
|
|
if (begin + c->abort.string[n].len > c->bufend)
|
|
|
|
break;
|
|
|
|
if (!strncmp(begin, c->abort.string[n].data,
|
|
|
|
c->abort.string[n].len)) {
|
|
|
|
if (memchr(begin + c->abort.string[n].len - 1, '\n',
|
|
|
|
c->bufend - begin - c->abort.string[n].len + 1) == NULL) {
|
|
|
|
/* force it into the log */
|
|
|
|
end = c->bufend;
|
|
|
|
c->bufend = begin + c->abort.string[n].len;
|
|
|
|
chat_UpdateLog(c, -1);
|
|
|
|
c->bufend = end;
|
|
|
|
}
|
|
|
|
c->bufstart = begin + c->abort.string[n].len;
|
|
|
|
c->state = CHAT_FAILED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-06-24 19:33:36 +00:00
|
|
|
static int
|
1998-02-23 00:38:44 +00:00
|
|
|
chat_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
1998-02-13 05:10:26 +00:00
|
|
|
{
|
|
|
|
struct chat *c = descriptor2chat(d);
|
1998-06-24 19:33:36 +00:00
|
|
|
int result = 0;
|
1998-02-13 05:10:26 +00:00
|
|
|
|
|
|
|
if (c->state == CHAT_SEND) {
|
|
|
|
int wrote;
|
|
|
|
|
|
|
|
if (strstr(c->argv[c->arg], "\\P")) /* Don't log the password */
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCHAT, "Send: %s\n", c->argv[c->arg]);
|
1998-02-13 05:10:26 +00:00
|
|
|
else {
|
|
|
|
int sz;
|
|
|
|
|
|
|
|
sz = c->arglen - 1;
|
|
|
|
while (sz >= 0 && c->argptr[sz] == '\n')
|
|
|
|
sz--;
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCHAT, "Send: %.*s\n", sz + 1, c->argptr);
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
if (physical_IsSync(c->physical)) {
|
1998-02-13 05:10:26 +00:00
|
|
|
/* There's always room for the HDLC header */
|
|
|
|
c->argptr -= 2;
|
|
|
|
c->arglen += 2;
|
|
|
|
memcpy(c->argptr, "\377\003", 2); /* Prepend HDLC header */
|
|
|
|
}
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
wrote = physical_Write(c->physical, c->argptr, c->arglen);
|
1998-06-24 19:33:36 +00:00
|
|
|
result = wrote ? 1 : 0;
|
1998-02-13 05:10:26 +00:00
|
|
|
if (wrote == -1) {
|
|
|
|
if (errno != EINTR)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "chat_Write: %s\n", strerror(errno));
|
|
|
|
if (physical_IsSync(c->physical)) {
|
1998-02-13 05:10:26 +00:00
|
|
|
c->argptr += 2;
|
|
|
|
c->arglen -= 2;
|
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
} else if (wrote < 2 && physical_IsSync(c->physical)) {
|
1998-02-13 05:10:26 +00:00
|
|
|
/* Oops - didn't even write our HDLC header ! */
|
|
|
|
c->argptr += 2;
|
|
|
|
c->arglen -= 2;
|
|
|
|
} else {
|
|
|
|
c->argptr += wrote;
|
|
|
|
c->arglen -= wrote;
|
|
|
|
}
|
|
|
|
}
|
1998-06-24 19:33:36 +00:00
|
|
|
|
|
|
|
return result;
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-03-09 19:24:58 +00:00
|
|
|
chat_Init(struct chat *c, struct physical *p, const char *data, int emptybuf,
|
|
|
|
const char *phone)
|
1998-02-13 05:10:26 +00:00
|
|
|
{
|
|
|
|
c->desc.type = CHAT_DESCRIPTOR;
|
|
|
|
c->desc.UpdateSet = chat_UpdateSet;
|
|
|
|
c->desc.IsSet = chat_IsSet;
|
|
|
|
c->desc.Read = chat_Read;
|
|
|
|
c->desc.Write = chat_Write;
|
|
|
|
c->physical = p;
|
|
|
|
|
|
|
|
c->state = CHAT_EXPECT;
|
|
|
|
|
1998-02-16 19:10:03 +00:00
|
|
|
if (data == NULL) {
|
|
|
|
*c->script = '\0';
|
|
|
|
c->argc = 0;
|
|
|
|
} else {
|
|
|
|
strncpy(c->script, data, sizeof c->script - 1);
|
|
|
|
c->script[sizeof c->script - 1] = '\0';
|
|
|
|
c->argc = MakeArgs(c->script, c->argv, VECSIZE(c->argv));
|
|
|
|
}
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
c->arg = -1;
|
|
|
|
c->argptr = NULL;
|
|
|
|
c->nargptr = NULL;
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
if (emptybuf)
|
|
|
|
c->bufstart = c->bufend = c->buf;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
c->TimeoutSec = 30;
|
|
|
|
c->TimedOut = 0;
|
1998-03-09 19:24:58 +00:00
|
|
|
c->phone = phone;
|
1998-02-26 17:54:43 +00:00
|
|
|
c->abort.num = 0;
|
1995-02-26 12:18:08 +00:00
|
|
|
|
1998-04-18 23:17:26 +00:00
|
|
|
memset(&c->pause, '\0', sizeof c->pause);
|
|
|
|
memset(&c->timeout, '\0', sizeof c->timeout);
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
void
|
|
|
|
chat_Destroy(struct chat *c)
|
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Stop(&c->pause);
|
|
|
|
timer_Stop(&c->timeout);
|
1998-02-26 17:54:43 +00:00
|
|
|
while (c->abort.num)
|
|
|
|
free(c->abort.string[--c->abort.num].data);
|
1998-04-18 23:17:26 +00:00
|
|
|
c->abort.num = 0;
|
1998-02-13 05:10:26 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
/*
|
1997-01-10 07:53:28 +00:00
|
|
|
* \c don't add a cr
|
|
|
|
* \d Sleep a little (delay 2 seconds
|
|
|
|
* \n Line feed character
|
|
|
|
* \P Auth Key password
|
|
|
|
* \p pause 0.25 sec
|
1995-01-31 06:29:58 +00:00
|
|
|
* \r Carrige return character
|
|
|
|
* \s Space character
|
1996-03-08 09:03:09 +00:00
|
|
|
* \T Telephone number(s) (defined via `set phone')
|
1995-01-31 06:29:58 +00:00
|
|
|
* \t Tab character
|
1997-01-10 07:53:28 +00:00
|
|
|
* \U Auth User
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1998-04-03 19:24:49 +00:00
|
|
|
static char *
|
1998-03-09 19:25:35 +00:00
|
|
|
ExpandString(struct chat *c, const char *str, char *result, int reslen,
|
1998-02-13 05:10:26 +00:00
|
|
|
int sendmode)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int addcr = 0;
|
|
|
|
|
1997-01-10 07:53:28 +00:00
|
|
|
result[--reslen] = '\0';
|
1995-01-31 06:29:58 +00:00
|
|
|
if (sendmode)
|
|
|
|
addcr = 1;
|
1997-01-10 07:53:28 +00:00
|
|
|
while (*str && reslen > 0) {
|
1995-01-31 06:29:58 +00:00
|
|
|
switch (*str) {
|
|
|
|
case '\\':
|
|
|
|
str++;
|
|
|
|
switch (*str) {
|
|
|
|
case 'c':
|
|
|
|
if (sendmode)
|
|
|
|
addcr = 0;
|
|
|
|
break;
|
|
|
|
case 'd': /* Delay 2 seconds */
|
1998-03-09 19:25:35 +00:00
|
|
|
chat_Pause(c, 2 * SECTICKS);
|
1997-08-25 00:29:32 +00:00
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
case 'p':
|
1998-03-09 19:25:35 +00:00
|
|
|
chat_Pause(c, SECTICKS / 4);
|
1997-08-25 00:29:32 +00:00
|
|
|
break; /* Pause 0.25 sec */
|
1995-01-31 06:29:58 +00:00
|
|
|
case 'n':
|
1997-08-25 00:29:32 +00:00
|
|
|
*result++ = '\n';
|
|
|
|
reslen--;
|
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
case 'r':
|
1997-08-25 00:29:32 +00:00
|
|
|
*result++ = '\r';
|
|
|
|
reslen--;
|
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
case 's':
|
1997-08-25 00:29:32 +00:00
|
|
|
*result++ = ' ';
|
|
|
|
reslen--;
|
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
case 't':
|
1997-08-25 00:29:32 +00:00
|
|
|
*result++ = '\t';
|
|
|
|
reslen--;
|
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
case 'P':
|
1998-04-03 19:24:49 +00:00
|
|
|
strncpy(result, c->physical->dl->bundle->cfg.auth.key, reslen);
|
1997-01-10 07:53:28 +00:00
|
|
|
reslen -= strlen(result);
|
|
|
|
result += strlen(result);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case 'T':
|
1998-08-07 18:42:51 +00:00
|
|
|
if (c->phone) {
|
|
|
|
strncpy(result, c->phone, reslen);
|
|
|
|
reslen -= strlen(result);
|
|
|
|
result += strlen(result);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case 'U':
|
1998-04-03 19:24:49 +00:00
|
|
|
strncpy(result, c->physical->dl->bundle->cfg.auth.name, reslen);
|
1997-01-10 07:53:28 +00:00
|
|
|
reslen -= strlen(result);
|
|
|
|
result += strlen(result);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
1997-01-10 07:53:28 +00:00
|
|
|
reslen--;
|
1997-08-25 00:29:32 +00:00
|
|
|
*result++ = *str;
|
1997-01-10 07:53:28 +00:00
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (*str)
|
|
|
|
str++;
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
str++;
|
1997-01-10 07:53:28 +00:00
|
|
|
if (*str) {
|
1995-01-31 06:29:58 +00:00
|
|
|
*result++ = *str++ & 0x1f;
|
1997-01-10 07:53:28 +00:00
|
|
|
reslen--;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*result++ = *str++;
|
1997-01-10 07:53:28 +00:00
|
|
|
reslen--;
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-01-10 07:53:28 +00:00
|
|
|
if (--reslen > 0) {
|
|
|
|
if (addcr)
|
|
|
|
*result++ = '\r';
|
|
|
|
}
|
|
|
|
if (--reslen > 0)
|
|
|
|
*result++ = '\0';
|
1997-08-25 00:29:32 +00:00
|
|
|
return (result);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-12-27 07:22:12 +00:00
|
|
|
static void
|
1998-01-29 00:49:32 +00:00
|
|
|
ExecStr(struct physical *physical, char *command, char *out, int olen)
|
1997-12-27 07:22:12 +00:00
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int fids[2];
|
1999-02-12 00:52:30 +00:00
|
|
|
char *argv[MAXARGS], *vector[MAXARGS], *startout, *endout;
|
|
|
|
int stat, nb, argc;
|
1997-12-27 07:22:12 +00:00
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCHAT, "Exec: %s\n", command);
|
1999-02-12 00:52:30 +00:00
|
|
|
argc = MakeArgs(command, vector, VECSIZE(vector));
|
|
|
|
command_Expand(argv, argc, (char const *const *)vector,
|
|
|
|
physical->dl->bundle, 0);
|
1997-12-27 07:22:12 +00:00
|
|
|
|
|
|
|
if (pipe(fids) < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCHAT, "Unable to create pipe in ExecStr: %s\n",
|
1997-12-27 07:22:12 +00:00
|
|
|
strerror(errno));
|
1998-02-13 05:31:03 +00:00
|
|
|
*out = '\0';
|
|
|
|
return;
|
1997-12-27 07:22:12 +00:00
|
|
|
}
|
|
|
|
if ((pid = fork()) == 0) {
|
1998-05-28 23:17:51 +00:00
|
|
|
close(fids[0]);
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_TermService();
|
1998-05-01 19:22:23 +00:00
|
|
|
fids[1] = fcntl(fids[1], F_DUPFD, 4);
|
1999-05-08 11:07:56 +00:00
|
|
|
dup2(physical->fd, STDIN_FILENO);
|
1998-05-01 19:22:23 +00:00
|
|
|
dup2(STDIN_FILENO, STDOUT_FILENO);
|
|
|
|
dup2(fids[1], STDERR_FILENO);
|
|
|
|
close(3);
|
|
|
|
if (open(_PATH_TTY, O_RDWR) == 3)
|
1998-04-30 23:53:56 +00:00
|
|
|
fcntl(3, F_SETFD, 0); /* Clear close-on-exec flag */
|
1998-05-01 19:22:23 +00:00
|
|
|
else
|
|
|
|
fcntl(3, F_SETFD, 1); /* Set close-on-exec flag */
|
1997-12-27 07:22:12 +00:00
|
|
|
setuid(geteuid());
|
1999-02-12 00:52:30 +00:00
|
|
|
execvp(argv[0], argv);
|
|
|
|
fprintf(stderr, "execvp: %s: %s\n", argv[0], strerror(errno));
|
1997-12-27 07:22:12 +00:00
|
|
|
exit(127);
|
|
|
|
} else {
|
|
|
|
char *name = strdup(vector[0]);
|
|
|
|
|
|
|
|
close(fids[1]);
|
|
|
|
endout = out + olen - 1;
|
|
|
|
startout = out;
|
|
|
|
while (out < endout) {
|
|
|
|
nb = read(fids[0], out, 1);
|
|
|
|
if (nb <= 0)
|
|
|
|
break;
|
|
|
|
out++;
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
close(fids[0]);
|
|
|
|
close(fids[1]);
|
|
|
|
waitpid(pid, &stat, WNOHANG);
|
|
|
|
if (WIFSIGNALED(stat)) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "%s: signal %d\n", name, WTERMSIG(stat));
|
1997-12-27 07:22:12 +00:00
|
|
|
free(name);
|
1998-02-13 05:31:03 +00:00
|
|
|
*out = '\0';
|
|
|
|
return;
|
1997-12-27 07:22:12 +00:00
|
|
|
} else if (WIFEXITED(stat)) {
|
|
|
|
switch (WEXITSTATUS(stat)) {
|
|
|
|
case 0:
|
|
|
|
free(name);
|
|
|
|
break;
|
|
|
|
case 127:
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "%s: %s\n", name, startout);
|
1997-12-27 07:22:12 +00:00
|
|
|
free(name);
|
1998-02-13 05:31:03 +00:00
|
|
|
*out = '\0';
|
|
|
|
return;
|
1997-12-27 07:22:12 +00:00
|
|
|
break;
|
|
|
|
default:
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "%s: exit %d\n", name, WEXITSTATUS(stat));
|
1997-12-27 07:22:12 +00:00
|
|
|
free(name);
|
1998-02-13 05:31:03 +00:00
|
|
|
*out = '\0';
|
|
|
|
return;
|
1997-12-27 07:22:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "%s: Unexpected exit result\n", name);
|
1997-12-27 07:22:12 +00:00
|
|
|
free(name);
|
1998-02-13 05:31:03 +00:00
|
|
|
*out = '\0';
|
|
|
|
return;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|