1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Most of codes are derived from chat.c by Karl Fox (karl@MorningStar.Com).
|
|
|
|
*
|
|
|
|
* Chat -- a program for automatic session establishment (i.e. dial
|
|
|
|
* the phone and log in).
|
|
|
|
*
|
|
|
|
* This software is in the public domain.
|
|
|
|
*
|
|
|
|
* Please send all bug reports, requests for information, etc. to:
|
|
|
|
*
|
|
|
|
* Karl Fox <karl@MorningStar.Com>
|
|
|
|
* Morning Star Technologies, Inc.
|
|
|
|
* 1760 Zollinger Road
|
|
|
|
* Columbus, OH 43221
|
|
|
|
* (614)451-1883
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1997-12-27 07:22:12 +00:00
|
|
|
* $Id: chat.c,v 1.42 1997/12/24 09:28:54 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
* o Support more UUCP compatible control sequences.
|
|
|
|
* o Dialing shoud not block monitor process.
|
1995-03-11 15:18:55 +00:00
|
|
|
* o Reading modem by select should be unified into main.c
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <ctype.h>
|
1995-02-26 12:18:08 +00:00
|
|
|
#include <errno.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <setjmp.h>
|
1997-02-25 14:05:17 +00:00
|
|
|
#include <signal.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-05-26 00:44:10 +00:00
|
|
|
#include <sys/socket.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
#include "command.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
1997-05-26 00:44:10 +00:00
|
|
|
#include "loadalias.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "vars.h"
|
1997-05-10 01:22:19 +00:00
|
|
|
#include "chat.h"
|
1997-03-13 12:45:35 +00:00
|
|
|
#include "sig.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "modem.h"
|
|
|
|
|
|
|
|
#ifndef isblank
|
|
|
|
#define isblank(c) ((c) == '\t' || (c) == ' ')
|
|
|
|
#endif
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-09 14:18:55 +00:00
|
|
|
#define IBSIZE LINE_LEN
|
1995-02-26 12:18:08 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
static int TimeoutSec;
|
|
|
|
static int abort_next, timeout_next;
|
|
|
|
static int numaborts;
|
1997-10-26 01:04:02 +00:00
|
|
|
static char *AbortStrings[50];
|
|
|
|
static char inbuff[IBSIZE * 2 + 1];
|
1997-12-27 07:22:12 +00:00
|
|
|
static jmp_buf ChatEnv;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
#define MATCH 1
|
|
|
|
#define NOMATCH 0
|
|
|
|
#define ABORT -1
|
|
|
|
|
|
|
|
static char *
|
1997-06-23 23:10:13 +00:00
|
|
|
findblank(char *p, int instring)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
if (instring) {
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '\\') {
|
|
|
|
strcpy(p, p + 1);
|
|
|
|
if (!*p)
|
|
|
|
break;
|
|
|
|
} else if (*p == '"')
|
1997-08-25 00:29:32 +00:00
|
|
|
return (p);
|
1995-01-31 06:29:58 +00:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*p) {
|
|
|
|
if (isblank(*p))
|
1997-08-25 00:29:32 +00:00
|
|
|
return (p);
|
1995-01-31 06:29:58 +00:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-06-23 23:10:13 +00:00
|
|
|
MakeArgs(char *script, char **pvect, int maxargs)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int nargs, nb;
|
|
|
|
int instring;
|
|
|
|
|
|
|
|
nargs = 0;
|
|
|
|
while (*script) {
|
|
|
|
nb = strspn(script, " \t");
|
|
|
|
script += nb;
|
|
|
|
if (*script) {
|
|
|
|
if (*script == '"') {
|
|
|
|
instring = 1;
|
|
|
|
script++;
|
|
|
|
if (*script == '\0')
|
1997-08-25 00:29:32 +00:00
|
|
|
break; /* Shouldn't return here. Need to null
|
|
|
|
* terminate below */
|
1995-01-31 06:29:58 +00:00
|
|
|
} else
|
|
|
|
instring = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
if (nargs >= maxargs - 1)
|
1997-05-07 23:01:25 +00:00
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
*pvect++ = script;
|
|
|
|
nargs++;
|
|
|
|
script = findblank(script, instring);
|
|
|
|
if (*script)
|
|
|
|
*script++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pvect = NULL;
|
|
|
|
return nargs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
|
|
|
char *
|
1997-11-22 03:37:54 +00:00
|
|
|
ExpandString(const char *str, char *result, int reslen, int sendmode)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int addcr = 0;
|
1996-03-08 09:03:09 +00:00
|
|
|
char *phone;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
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 */
|
1997-10-24 22:36:31 +00:00
|
|
|
nointr_sleep(2);
|
1997-08-25 00:29:32 +00:00
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
case 'p':
|
1997-10-24 22:36:31 +00:00
|
|
|
nointr_usleep(250000);
|
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':
|
1997-08-25 00:29:32 +00:00
|
|
|
strncpy(result, VarAuthKey, 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':
|
1997-08-17 20:38:45 +00:00
|
|
|
if (VarAltPhone == NULL) {
|
|
|
|
if (VarNextPhone == NULL) {
|
1997-12-24 09:29:17 +00:00
|
|
|
strncpy(VarPhoneCopy, VarPhoneList, sizeof VarPhoneCopy - 1);
|
|
|
|
VarPhoneCopy[sizeof VarPhoneCopy - 1] = '\0';
|
1997-08-17 20:38:45 +00:00
|
|
|
VarNextPhone = VarPhoneCopy;
|
|
|
|
}
|
|
|
|
VarAltPhone = strsep(&VarNextPhone, ":");
|
1996-03-08 13:22:23 +00:00
|
|
|
}
|
1997-08-17 20:38:45 +00:00
|
|
|
phone = strsep(&VarAltPhone, "|");
|
1997-01-10 07:53:28 +00:00
|
|
|
strncpy(result, phone, reslen);
|
|
|
|
reslen -= strlen(result);
|
|
|
|
result += strlen(result);
|
1997-06-09 03:27:43 +00:00
|
|
|
if (VarTerm)
|
|
|
|
fprintf(VarTerm, "Phone: %s\n", phone);
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogPHASE, "Phone: %s\n", phone);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case 'U':
|
1997-01-10 07:53:28 +00:00
|
|
|
strncpy(result, VarAuthName, reslen);
|
|
|
|
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-11-09 14:18:55 +00:00
|
|
|
#define MAXLOGBUFF LINE_LEN
|
1996-05-11 20:48:42 +00:00
|
|
|
static char logbuff[MAXLOGBUFF];
|
|
|
|
static int loglen = 0;
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
clear_log(void)
|
1997-06-23 23:10:13 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
memset(logbuff, 0, MAXLOGBUFF);
|
1996-05-11 20:48:42 +00:00
|
|
|
loglen = 0;
|
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
flush_log(void)
|
1997-06-23 23:10:13 +00:00
|
|
|
{
|
1997-06-09 03:27:43 +00:00
|
|
|
if (LogIsKept(LogCONNECT))
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogCONNECT, "%s\n", logbuff);
|
1997-08-25 00:29:32 +00:00
|
|
|
else if (LogIsKept(LogCARRIER) && strstr(logbuff, "CARRIER"))
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogCARRIER, "%s\n", logbuff);
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1996-05-11 20:48:42 +00:00
|
|
|
clear_log();
|
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
connect_log(const char *str, int single_p)
|
1997-06-23 23:10:13 +00:00
|
|
|
{
|
1996-05-11 20:48:42 +00:00
|
|
|
int space = MAXLOGBUFF - loglen - 1;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1996-05-11 20:48:42 +00:00
|
|
|
while (space--) {
|
|
|
|
if (*str == '\n') {
|
|
|
|
flush_log();
|
|
|
|
} else {
|
|
|
|
logbuff[loglen++] = *str;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (single_p || !*++str)
|
|
|
|
break;
|
1996-05-11 20:48:42 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (!space)
|
|
|
|
flush_log();
|
1996-05-11 20:48:42 +00:00
|
|
|
}
|
|
|
|
|
1997-12-27 07:22:12 +00:00
|
|
|
static void
|
|
|
|
ExecStr(char *command, char *out, int olen)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int fids[2];
|
|
|
|
char *vector[MAXARGS], *startout, *endout;
|
|
|
|
int stat, nb;
|
|
|
|
|
|
|
|
LogPrintf(LogCHAT, "Exec: %s\n", command);
|
|
|
|
MakeArgs(command, vector, VECSIZE(vector));
|
|
|
|
|
|
|
|
if (pipe(fids) < 0) {
|
|
|
|
LogPrintf(LogCHAT, "Unable to create pipe in ExecStr: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
longjmp(ChatEnv, 2);
|
|
|
|
}
|
|
|
|
if ((pid = fork()) == 0) {
|
|
|
|
TermTimerService();
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGQUIT, SIG_DFL);
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
signal(SIGHUP, SIG_DFL);
|
|
|
|
signal(SIGALRM, SIG_DFL);
|
|
|
|
if (modem == 2) {
|
|
|
|
int nmodem;
|
|
|
|
nmodem = dup(modem);
|
|
|
|
close(modem);
|
|
|
|
modem = nmodem;
|
|
|
|
}
|
|
|
|
close(fids[0]);
|
|
|
|
dup2(fids[1], 2);
|
|
|
|
close(fids[1]);
|
|
|
|
dup2(modem, 0);
|
|
|
|
dup2(modem, 1);
|
|
|
|
if ((nb = open("/dev/tty", O_RDWR)) > 3) {
|
|
|
|
dup2(nb, 3);
|
|
|
|
close(nb);
|
|
|
|
}
|
|
|
|
setuid(geteuid());
|
|
|
|
execvp(vector[0], vector);
|
|
|
|
fprintf(stderr, "execvp failed: %s: %s\n", vector[0], strerror(errno));
|
|
|
|
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)) {
|
|
|
|
LogPrintf(LogWARN, "%s: signal %d\n", name, WTERMSIG(stat));
|
|
|
|
free(name);
|
|
|
|
longjmp(ChatEnv, 3);
|
|
|
|
} else if (WIFEXITED(stat)) {
|
|
|
|
switch (WEXITSTATUS(stat)) {
|
|
|
|
case 0:
|
|
|
|
free(name);
|
|
|
|
break;
|
|
|
|
case 127:
|
|
|
|
LogPrintf(LogWARN, "%s: %s\n", name, startout);
|
|
|
|
free(name);
|
|
|
|
longjmp(ChatEnv, 4);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrintf(LogWARN, "%s: exit %d\n", name, WEXITSTATUS(stat));
|
|
|
|
free(name);
|
|
|
|
longjmp(ChatEnv, 5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogPrintf(LogWARN, "%s: Unexpected exit result\n", name);
|
|
|
|
free(name);
|
|
|
|
longjmp(ChatEnv, 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static int
|
1997-11-22 03:37:54 +00:00
|
|
|
WaitforString(const char *estr)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
char *s, *str, ch;
|
|
|
|
char *inp;
|
|
|
|
fd_set rfds;
|
1997-05-10 01:22:19 +00:00
|
|
|
int i, nfds, nb;
|
|
|
|
char buff[IBSIZE];
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef SIGALRM
|
|
|
|
int omask;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
omask = sigblock(sigmask(SIGALRM));
|
|
|
|
#endif
|
1996-05-11 20:48:42 +00:00
|
|
|
clear_log();
|
1997-12-27 07:22:12 +00:00
|
|
|
if (*estr == '!') {
|
|
|
|
ExpandString(estr + 1, buff, sizeof buff, 0);
|
|
|
|
ExecStr(buff, buff, sizeof buff);
|
|
|
|
} else {
|
|
|
|
ExpandString(estr, buff, sizeof buff, 0);
|
|
|
|
}
|
|
|
|
if (LogIsKept(LogCHAT)) {
|
|
|
|
s = buff + strlen(buff) - 1;
|
|
|
|
while (s >= buff && *s == '\n')
|
|
|
|
s--;
|
|
|
|
if (!strcmp(estr, buff))
|
|
|
|
LogPrintf(LogCHAT, "Wait for (%d): %.*s\n",
|
|
|
|
TimeoutSec, s - buff + 1, buff);
|
|
|
|
else
|
|
|
|
LogPrintf(LogCHAT, "Wait for (%d): %s --> %.*s\n",
|
|
|
|
TimeoutSec, estr, s - buff + 1, buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buff[0] == '\0')
|
|
|
|
return (MATCH);
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
str = buff;
|
|
|
|
inp = inbuff;
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
if (strlen(str) >= IBSIZE) {
|
|
|
|
str[IBSIZE - 1] = 0;
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogCHAT, "Truncating String to %d character: %s\n", IBSIZE, str);
|
1996-04-06 02:00:17 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
nfds = modem + 1;
|
|
|
|
s = str;
|
|
|
|
for (;;) {
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(modem, &rfds);
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* Because it is not clear whether select() modifies timeout value, it is
|
|
|
|
* better to initialize timeout values everytime.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
timeout.tv_sec = TimeoutSec;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
i = select(nfds, &rfds, NULL, NULL, &timeout);
|
|
|
|
#ifdef notdef
|
|
|
|
TimerService();
|
|
|
|
#endif
|
|
|
|
if (i < 0) {
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef SIGALRM
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
sigsetmask(omask);
|
|
|
|
#endif
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "WaitForString: select(): %s\n", strerror(errno));
|
1995-02-26 12:18:08 +00:00
|
|
|
*inp = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
return (NOMATCH);
|
|
|
|
} else if (i == 0) { /* Timeout reached! */
|
1995-02-26 12:18:08 +00:00
|
|
|
*inp = 0;
|
|
|
|
if (inp != inbuff)
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogCHAT, "Got: %s\n", inbuff);
|
|
|
|
LogPrintf(LogCHAT, "Can't get (%d).\n", timeout.tv_sec);
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef SIGALRM
|
|
|
|
sigsetmask(omask);
|
|
|
|
#endif
|
1997-08-25 00:29:32 +00:00
|
|
|
return (NOMATCH);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
if (FD_ISSET(modem, &rfds)) { /* got something */
|
1995-02-26 12:18:08 +00:00
|
|
|
if (DEV_IS_SYNC) {
|
1996-04-06 02:00:17 +00:00
|
|
|
int length;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
|
|
|
if ((length = strlen(inbuff)) > IBSIZE) {
|
1997-10-26 01:04:02 +00:00
|
|
|
/* shuffle down next part */
|
|
|
|
memcpy(inbuff, &(inbuff[IBSIZE]), IBSIZE + 1);
|
1997-08-25 00:29:32 +00:00
|
|
|
length = strlen(inbuff);
|
1996-04-06 02:00:17 +00:00
|
|
|
}
|
|
|
|
nb = read(modem, &(inbuff[length]), IBSIZE);
|
|
|
|
inbuff[nb + length] = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
connect_log(inbuff, 0);
|
1995-02-26 12:18:08 +00:00
|
|
|
if (strstr(inbuff, str)) {
|
|
|
|
#ifdef SIGALRM
|
1997-08-25 00:29:32 +00:00
|
|
|
sigsetmask(omask);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1996-05-11 20:48:42 +00:00
|
|
|
flush_log();
|
1997-08-25 00:29:32 +00:00
|
|
|
return (MATCH);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
for (i = 0; i < numaborts; i++) {
|
|
|
|
if (strstr(inbuff, AbortStrings[i])) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogCHAT, "Abort: %s\n", AbortStrings[i]);
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef SIGALRM
|
1997-08-25 00:29:32 +00:00
|
|
|
sigsetmask(omask);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1996-05-11 20:48:42 +00:00
|
|
|
flush_log();
|
1997-08-25 00:29:32 +00:00
|
|
|
return (ABORT);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
} else {
|
1997-08-25 00:29:32 +00:00
|
|
|
if (read(modem, &ch, 1) < 0) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogERROR, "read error: %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
*inp = '\0';
|
|
|
|
return (NOMATCH);
|
1997-05-10 01:22:19 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
connect_log(&ch, 1);
|
|
|
|
*inp++ = ch;
|
|
|
|
if (ch == *s) {
|
1995-02-26 12:18:08 +00:00
|
|
|
s++;
|
|
|
|
if (*s == '\0') {
|
|
|
|
#ifdef SIGALRM
|
1997-08-25 00:29:32 +00:00
|
|
|
sigsetmask(omask);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
|
|
|
*inp = 0;
|
1996-05-11 20:48:42 +00:00
|
|
|
flush_log();
|
1997-08-25 00:29:32 +00:00
|
|
|
return (MATCH);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
} else
|
1995-02-26 12:18:08 +00:00
|
|
|
s = str;
|
1997-08-17 22:47:07 +00:00
|
|
|
if (inp == inbuff + IBSIZE) {
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(inbuff, inp - 100, 100);
|
1997-08-17 22:47:07 +00:00
|
|
|
inp = inbuff + 100;
|
|
|
|
}
|
|
|
|
if (s == str) {
|
1995-02-26 12:18:08 +00:00
|
|
|
for (i = 0; i < numaborts; i++) { /* Look for Abort strings */
|
|
|
|
int len;
|
|
|
|
char *s1;
|
|
|
|
|
|
|
|
s1 = AbortStrings[i];
|
|
|
|
len = strlen(s1);
|
|
|
|
if ((len <= inp - inbuff) && (strncmp(inp - len, s1, len) == 0)) {
|
1997-08-31 22:59:49 +00:00
|
|
|
LogPrintf(LogCHAT, "Abort: %s\n", s1);
|
1995-02-26 12:18:08 +00:00
|
|
|
*inp = 0;
|
|
|
|
#ifdef SIGALRM
|
1997-08-25 00:29:32 +00:00
|
|
|
sigsetmask(omask);
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|
1996-05-11 20:48:42 +00:00
|
|
|
flush_log();
|
1997-08-25 00:29:32 +00:00
|
|
|
return (ABORT);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1997-11-22 03:37:54 +00:00
|
|
|
SendString(const char *str)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1995-02-26 12:18:08 +00:00
|
|
|
char *cp;
|
1997-05-10 01:22:19 +00:00
|
|
|
int on;
|
1997-11-09 14:18:55 +00:00
|
|
|
char buff[LINE_LEN];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (abort_next) {
|
|
|
|
abort_next = 0;
|
1997-12-24 09:29:17 +00:00
|
|
|
ExpandString(str, buff, sizeof buff, 0);
|
1995-01-31 06:29:58 +00:00
|
|
|
AbortStrings[numaborts++] = strdup(buff);
|
|
|
|
} else if (timeout_next) {
|
|
|
|
timeout_next = 0;
|
|
|
|
TimeoutSec = atoi(str);
|
|
|
|
if (TimeoutSec <= 0)
|
|
|
|
TimeoutSec = 30;
|
|
|
|
} else {
|
1995-02-26 12:18:08 +00:00
|
|
|
if (*str == '!') {
|
1997-12-24 09:29:17 +00:00
|
|
|
ExpandString(str + 1, buff + 2, sizeof buff - 2, 0);
|
1997-12-27 07:22:12 +00:00
|
|
|
ExecStr(buff + 2, buff + 2, sizeof buff - 2);
|
1995-02-26 12:18:08 +00:00
|
|
|
} else {
|
1997-12-24 09:29:17 +00:00
|
|
|
ExpandString(str, buff + 2, sizeof buff - 2, 1);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1997-08-31 22:59:49 +00:00
|
|
|
if (strstr(str, "\\P")) /* Do not log the password itself. */
|
1997-12-27 07:22:12 +00:00
|
|
|
LogPrintf(LogCHAT, "Sending: %s", str);
|
|
|
|
else {
|
|
|
|
cp = buff + strlen(buff + 2) + 1;
|
|
|
|
while (cp >= buff + 2 && *cp == '\n')
|
|
|
|
cp--;
|
|
|
|
LogPrintf(LogCHAT, "Sending: %.*s\n", cp - buff - 1, buff + 2);
|
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
cp = buff;
|
|
|
|
if (DEV_IS_SYNC)
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(buff, "\377\003", 2); /* Prepend HDLC header */
|
1995-02-26 12:18:08 +00:00
|
|
|
else
|
|
|
|
cp += 2;
|
|
|
|
on = strlen(cp);
|
1997-05-10 01:22:19 +00:00
|
|
|
write(modem, cp, on);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static int
|
1997-06-23 23:10:13 +00:00
|
|
|
ExpectString(char *str)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
char *minus;
|
|
|
|
int state;
|
|
|
|
|
|
|
|
if (strcmp(str, "ABORT") == 0) {
|
|
|
|
++abort_next;
|
1997-08-25 00:29:32 +00:00
|
|
|
return (MATCH);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
if (strcmp(str, "TIMEOUT") == 0) {
|
|
|
|
++timeout_next;
|
1997-08-25 00:29:32 +00:00
|
|
|
return (MATCH);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-12-27 07:22:12 +00:00
|
|
|
LogPrintf(LogCHAT, "Expecting: %s\n", str);
|
1995-01-31 06:29:58 +00:00
|
|
|
while (*str) {
|
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* Check whether if string contains sub-send-expect.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
for (minus = str; *minus; minus++) {
|
|
|
|
if (*minus == '-') {
|
|
|
|
if (minus == str || minus[-1] != '\\')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (*minus == '-') { /* We have sub-send-expect. */
|
1997-11-22 03:37:54 +00:00
|
|
|
*minus = '\0'; /* XXX: Cheat with the const string */
|
1995-01-31 06:29:58 +00:00
|
|
|
state = WaitforString(str);
|
1997-11-22 03:37:54 +00:00
|
|
|
*minus = '-'; /* XXX: Cheat with the const string */
|
|
|
|
minus++;
|
1995-01-31 06:29:58 +00:00
|
|
|
if (state != NOMATCH)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (state);
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* Can't get expect string. Sendout send part.
|
|
|
|
*/
|
|
|
|
str = minus;
|
|
|
|
for (minus = str; *minus; minus++) {
|
|
|
|
if (*minus == '-') {
|
|
|
|
if (minus == str || minus[-1] != '\\')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*minus == '-') {
|
1997-11-22 03:37:54 +00:00
|
|
|
*minus = '\0'; /* XXX: Cheat with the const string */
|
1995-01-31 06:29:58 +00:00
|
|
|
SendString(str);
|
1997-11-22 03:37:54 +00:00
|
|
|
*minus = '-'; /* XXX: Cheat with the const string */
|
|
|
|
str = ++minus;
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
|
|
|
SendString(str);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (MATCH);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* Simple case. Wait for string.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1997-08-25 00:29:32 +00:00
|
|
|
return (WaitforString(str));
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (MATCH);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
static void (*oint) (int);
|
1997-06-23 23:10:13 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
StopDial(int sig)
|
|
|
|
{
|
|
|
|
LogPrintf(LogPHASE, "DoChat: Caught signal %d, abort connect\n", sig);
|
1997-08-25 00:29:32 +00:00
|
|
|
longjmp(ChatEnv, 1);
|
1997-06-23 23:10:13 +00:00
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
int
|
1997-06-23 23:10:13 +00:00
|
|
|
DoChat(char *script)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-11-22 03:37:54 +00:00
|
|
|
char *vector[MAXARGS];
|
|
|
|
char *const *argv;
|
1997-12-27 07:22:12 +00:00
|
|
|
int argc, n, state, err;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1997-07-14 01:41:35 +00:00
|
|
|
if (!script || !*script)
|
|
|
|
return MATCH;
|
|
|
|
|
1997-12-27 07:22:12 +00:00
|
|
|
if ((err = setjmp(ChatEnv))) {
|
1997-06-23 23:10:13 +00:00
|
|
|
signal(SIGINT, oint);
|
1997-12-27 07:22:12 +00:00
|
|
|
if (err == 1)
|
|
|
|
/* Caught a SIGINT during chat */
|
|
|
|
return (-1);
|
|
|
|
return (NOMATCH);
|
1997-06-23 23:10:13 +00:00
|
|
|
}
|
1997-07-01 21:31:28 +00:00
|
|
|
oint = signal(SIGINT, StopDial);
|
1997-06-23 23:10:13 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
timeout_next = abort_next = 0;
|
|
|
|
for (n = 0; AbortStrings[n]; n++) {
|
|
|
|
free(AbortStrings[n]);
|
|
|
|
AbortStrings[n] = NULL;
|
|
|
|
}
|
|
|
|
numaborts = 0;
|
|
|
|
|
1997-12-24 09:29:17 +00:00
|
|
|
memset(vector, '\0', sizeof vector);
|
1997-11-22 03:37:54 +00:00
|
|
|
argc = MakeArgs(script, vector, VECSIZE(vector));
|
1995-01-31 06:29:58 +00:00
|
|
|
argv = vector;
|
|
|
|
TimeoutSec = 30;
|
|
|
|
while (*argv) {
|
|
|
|
if (strcmp(*argv, "P_ZERO") == 0 ||
|
|
|
|
strcmp(*argv, "P_ODD") == 0 || strcmp(*argv, "P_EVEN") == 0) {
|
|
|
|
ChangeParity(*argv++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
state = ExpectString(*argv++);
|
|
|
|
switch (state) {
|
|
|
|
case MATCH:
|
|
|
|
if (*argv)
|
|
|
|
SendString(*argv++);
|
|
|
|
break;
|
|
|
|
case ABORT:
|
|
|
|
case NOMATCH:
|
1997-06-23 23:10:13 +00:00
|
|
|
signal(SIGINT, oint);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (NOMATCH);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1997-06-23 23:10:13 +00:00
|
|
|
signal(SIGINT, oint);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (MATCH);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|