1997-11-18 00:22:07 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
1997-06-28 01:04:54 +00:00
|
|
|
#include <sys/types.h>
|
1997-11-07 20:20:15 +00:00
|
|
|
|
1997-06-28 01:04:54 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
1997-07-28 22:17:48 +00:00
|
|
|
#include <arpa/inet.h>
|
1997-06-28 01:04:54 +00:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netdb.h>
|
1997-11-07 20:20:15 +00:00
|
|
|
|
1997-11-11 13:27:47 +00:00
|
|
|
#include <sys/time.h>
|
1997-11-18 00:20:29 +00:00
|
|
|
#include <errno.h>
|
1997-11-07 20:20:15 +00:00
|
|
|
#include <histedit.h>
|
1997-11-18 00:20:29 +00:00
|
|
|
#include <setjmp.h>
|
1997-06-28 01:04:54 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1997-11-07 20:20:15 +00:00
|
|
|
#include <string.h>
|
1997-11-11 13:27:47 +00:00
|
|
|
#include <time.h>
|
1997-11-07 20:20:15 +00:00
|
|
|
#include <unistd.h>
|
1997-06-28 01:04:54 +00:00
|
|
|
|
|
|
|
#define LINELEN 2048
|
|
|
|
static char Buffer[LINELEN], Command[LINELEN];
|
|
|
|
|
1997-11-07 20:20:15 +00:00
|
|
|
static int
|
|
|
|
Usage()
|
1997-06-28 01:04:54 +00:00
|
|
|
{
|
1997-11-07 20:20:15 +00:00
|
|
|
fprintf(stderr, "Usage: pppctl [-v] [ -t n ] [ -p passwd ] "
|
|
|
|
"Port|LocalSock [command[;command]...]\n");
|
|
|
|
fprintf(stderr, " -v tells pppctl to output all"
|
|
|
|
" conversation\n");
|
|
|
|
fprintf(stderr, " -t n specifies a timeout of n"
|
1997-11-18 00:20:29 +00:00
|
|
|
" seconds when connecting (default 2)\n");
|
1997-06-28 01:04:54 +00:00
|
|
|
fprintf(stderr, " -p passwd specifies your password\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int TimedOut = 0;
|
1997-11-07 20:20:15 +00:00
|
|
|
static void
|
|
|
|
Timeout(int Sig)
|
1997-06-28 01:04:54 +00:00
|
|
|
{
|
|
|
|
TimedOut = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define REC_PASSWD (1)
|
|
|
|
#define REC_SHOW (2)
|
|
|
|
#define REC_VERBOSE (4)
|
|
|
|
|
|
|
|
static char *passwd;
|
1997-11-07 20:20:15 +00:00
|
|
|
static char *prompt;
|
1997-06-28 01:04:54 +00:00
|
|
|
|
1997-11-07 20:20:15 +00:00
|
|
|
static char *
|
|
|
|
GetPrompt(EditLine *e)
|
|
|
|
{
|
|
|
|
if (prompt == NULL)
|
|
|
|
prompt = "";
|
|
|
|
return prompt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1997-11-18 00:20:29 +00:00
|
|
|
Receive(int fd, int display)
|
1997-06-28 01:04:54 +00:00
|
|
|
{
|
|
|
|
int Result;
|
|
|
|
int len;
|
|
|
|
char *last;
|
|
|
|
|
1997-11-07 20:20:15 +00:00
|
|
|
prompt = Buffer;
|
1997-06-28 01:04:54 +00:00
|
|
|
len = 0;
|
|
|
|
while (Result = read(fd, Buffer+len, sizeof(Buffer)-len-1), Result != -1) {
|
1997-11-18 00:20:29 +00:00
|
|
|
if (Result == 0 && errno != EINTR) {
|
|
|
|
Result = -1;
|
|
|
|
break;
|
|
|
|
}
|
1997-06-28 01:04:54 +00:00
|
|
|
len += Result;
|
|
|
|
Buffer[len] = '\0';
|
|
|
|
if (TimedOut) {
|
|
|
|
if (display & REC_VERBOSE)
|
|
|
|
write(1,Buffer,len);
|
|
|
|
Result = -1;
|
|
|
|
break;
|
|
|
|
} else if (len > 2 && !strcmp(Buffer+len-2, "> ")) {
|
1997-11-09 18:50:34 +00:00
|
|
|
prompt = strrchr(Buffer, '\n');
|
1997-06-28 01:04:54 +00:00
|
|
|
if (display & (REC_SHOW|REC_VERBOSE)) {
|
|
|
|
if (display & REC_VERBOSE)
|
|
|
|
last = Buffer+len-1;
|
|
|
|
else
|
1997-11-09 18:50:34 +00:00
|
|
|
last = prompt;
|
1997-06-28 01:04:54 +00:00
|
|
|
if (last) {
|
1997-11-07 20:20:15 +00:00
|
|
|
last++;
|
1997-06-28 01:04:54 +00:00
|
|
|
write(1, Buffer, last-Buffer);
|
|
|
|
}
|
|
|
|
}
|
1997-11-09 18:50:34 +00:00
|
|
|
prompt = prompt == NULL ? Buffer : prompt+1;
|
1997-06-28 01:04:54 +00:00
|
|
|
for (last = Buffer+len-2; last > Buffer && *last != ' '; last--)
|
|
|
|
;
|
|
|
|
if (last > Buffer+3 && !strncmp(last-3, " on", 3)) {
|
|
|
|
/* a password is required ! */
|
|
|
|
if (display & REC_PASSWD) {
|
|
|
|
/* password time */
|
|
|
|
if (!passwd)
|
|
|
|
passwd = getpass("Password: ");
|
|
|
|
sprintf(Buffer, "passwd %s\n", passwd);
|
1997-11-07 20:20:15 +00:00
|
|
|
memset(passwd, '\0', strlen(passwd));
|
1997-06-28 01:04:54 +00:00
|
|
|
if (display & REC_VERBOSE)
|
|
|
|
write(1, Buffer, strlen(Buffer));
|
|
|
|
write(fd, Buffer, strlen(Buffer));
|
1997-11-07 20:20:15 +00:00
|
|
|
memset(Buffer, '\0', strlen(Buffer));
|
1997-11-18 00:20:29 +00:00
|
|
|
return Receive(fd, display & ~REC_PASSWD);
|
1997-06-28 01:04:54 +00:00
|
|
|
}
|
|
|
|
Result = 1;
|
|
|
|
} else
|
|
|
|
Result = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
1997-11-11 12:28:02 +00:00
|
|
|
static int data = -1;
|
1997-11-18 00:20:29 +00:00
|
|
|
static jmp_buf pppdead;
|
1997-11-11 12:28:02 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
check_fd(int sig)
|
|
|
|
{
|
|
|
|
if (data != -1) {
|
1997-11-11 13:27:47 +00:00
|
|
|
struct timeval t;
|
|
|
|
fd_set f;
|
1997-11-11 12:28:02 +00:00
|
|
|
static char buf[LINELEN];
|
1997-11-18 00:20:29 +00:00
|
|
|
int len;
|
1997-11-11 12:28:02 +00:00
|
|
|
|
1997-11-11 13:27:47 +00:00
|
|
|
FD_ZERO(&f);
|
|
|
|
FD_SET(data, &f);
|
|
|
|
t.tv_sec = t.tv_usec = 0;
|
1997-11-18 00:20:29 +00:00
|
|
|
if (select(data+1, &f, NULL, NULL, &t) > 0) {
|
|
|
|
len = read(data, buf, sizeof buf);
|
|
|
|
if (len > 0)
|
|
|
|
write(1, buf, len);
|
|
|
|
else
|
|
|
|
longjmp(pppdead, -1);
|
|
|
|
}
|
1997-11-11 12:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
smartgets(EditLine *e, int *count, int fd)
|
|
|
|
{
|
|
|
|
const char *result;
|
|
|
|
|
|
|
|
data = fd;
|
|
|
|
signal(SIGALRM, check_fd);
|
|
|
|
ualarm(500000, 500000);
|
1997-11-18 00:20:29 +00:00
|
|
|
result = setjmp(pppdead) ? NULL : el_gets(e, count);
|
1997-11-11 12:28:02 +00:00
|
|
|
ualarm(0,0);
|
|
|
|
signal(SIGALRM, SIG_DFL);
|
|
|
|
data = -1;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-06-28 01:04:54 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct servent *s;
|
|
|
|
struct hostent *h;
|
|
|
|
struct sockaddr *sock;
|
|
|
|
struct sockaddr_in ifsin;
|
|
|
|
struct sockaddr_un ifsun;
|
|
|
|
int socksz, arg, fd, len, verbose;
|
|
|
|
unsigned TimeoutVal;
|
|
|
|
char *DoneWord = "x", *next, *start;
|
|
|
|
struct sigaction act, oact;
|
|
|
|
|
|
|
|
verbose = 0;
|
|
|
|
TimeoutVal = 2;
|
|
|
|
|
|
|
|
for (arg = 1; arg < argc; arg++)
|
|
|
|
if (*argv[arg] == '-') {
|
|
|
|
for (start = argv[arg] + 1; *start; start++)
|
|
|
|
switch (*start) {
|
|
|
|
case 't':
|
|
|
|
TimeoutVal = (unsigned)atoi
|
|
|
|
(start[1] ? start + 1 : argv[++arg]);
|
|
|
|
start = DoneWord;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
verbose = REC_VERBOSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
passwd = (start[1] ? start + 1 : argv[++arg]);
|
|
|
|
start = DoneWord;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
1997-11-07 02:54:49 +00:00
|
|
|
if (argc < arg + 1)
|
1997-06-28 01:04:54 +00:00
|
|
|
return Usage();
|
|
|
|
|
|
|
|
if (*argv[arg] == '/') {
|
|
|
|
sock = (struct sockaddr *)&ifsun;
|
|
|
|
socksz = sizeof ifsun;
|
|
|
|
|
|
|
|
ifsun.sun_len = strlen(argv[arg]);
|
|
|
|
if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
|
|
|
|
fprintf(stderr, "%s: Path too long\n", argv[arg]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ifsun.sun_family = AF_LOCAL;
|
|
|
|
strcpy(ifsun.sun_path, argv[arg]);
|
|
|
|
|
|
|
|
if (fd = socket(AF_LOCAL, SOCK_STREAM, 0), fd < 0) {
|
|
|
|
fprintf(stderr, "Cannot create local domain socket\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
} else {
|
1997-07-12 19:26:49 +00:00
|
|
|
char *port, *host, *colon;
|
1997-11-07 20:20:15 +00:00
|
|
|
int hlen;
|
1997-07-12 19:26:49 +00:00
|
|
|
|
|
|
|
colon = strchr(argv[arg], ':');
|
|
|
|
if (colon) {
|
|
|
|
port = colon + 1;
|
|
|
|
*colon = '\0';
|
|
|
|
host = argv[arg];
|
|
|
|
} else {
|
|
|
|
port = argv[arg];
|
1997-07-28 22:17:48 +00:00
|
|
|
host = "127.0.0.1";
|
1997-07-12 19:26:49 +00:00
|
|
|
}
|
1997-06-28 01:04:54 +00:00
|
|
|
sock = (struct sockaddr *)&ifsin;
|
|
|
|
socksz = sizeof ifsin;
|
1997-07-28 22:17:48 +00:00
|
|
|
hlen = strlen(host);
|
1997-06-28 01:04:54 +00:00
|
|
|
|
1997-07-28 22:17:48 +00:00
|
|
|
if (strspn(host, "0123456789.") == hlen) {
|
|
|
|
if (!inet_aton(host, (struct in_addr *)&ifsin.sin_addr.s_addr)) {
|
|
|
|
fprintf(stderr, "Cannot translate %s\n", host);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if ((h = gethostbyname(host)) == 0) {
|
1997-07-12 19:26:49 +00:00
|
|
|
fprintf(stderr, "Cannot resolve %s\n", host);
|
1997-06-28 01:04:54 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1997-07-28 22:17:48 +00:00
|
|
|
else
|
|
|
|
ifsin.sin_addr.s_addr = *(u_long *)h->h_addr_list[0];
|
|
|
|
|
1997-07-12 19:26:49 +00:00
|
|
|
if (colon)
|
|
|
|
*colon = ':';
|
1997-06-28 01:04:54 +00:00
|
|
|
|
1997-07-12 19:26:49 +00:00
|
|
|
if (strspn(port, "0123456789") == strlen(port))
|
|
|
|
ifsin.sin_port = htons(atoi(port));
|
|
|
|
else if (s = getservbyname(port, "tcp"), !s) {
|
|
|
|
fprintf(stderr, "%s isn't a valid port or service!\n", port);
|
1997-06-28 01:04:54 +00:00
|
|
|
return Usage();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ifsin.sin_port = s->s_port;
|
|
|
|
|
|
|
|
ifsin.sin_len = sizeof(ifsin);
|
|
|
|
ifsin.sin_family = AF_INET;
|
|
|
|
|
|
|
|
if (fd = socket(AF_INET, SOCK_STREAM, 0), fd < 0) {
|
|
|
|
fprintf(stderr, "Cannot create internet socket\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TimedOut = 0;
|
|
|
|
if (TimeoutVal) {
|
|
|
|
act.sa_handler = Timeout;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
sigaction(SIGALRM, &act, &oact);
|
|
|
|
alarm(TimeoutVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(fd, sock, socksz) < 0) {
|
|
|
|
if (TimeoutVal) {
|
|
|
|
alarm(0);
|
|
|
|
sigaction(SIGALRM, &oact, 0);
|
|
|
|
}
|
|
|
|
if (TimedOut)
|
|
|
|
fputs("Timeout: ", stderr);
|
|
|
|
fprintf(stderr, "Cannot connect to socket %s\n", argv[arg]);
|
|
|
|
close(fd);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TimeoutVal) {
|
|
|
|
alarm(0);
|
|
|
|
sigaction(SIGALRM, &oact, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
Command[sizeof(Command)-1] = '\0';
|
|
|
|
for (arg++; arg < argc; arg++) {
|
|
|
|
if (len && len < sizeof(Command)-1)
|
|
|
|
strcpy(Command+len++, " ");
|
|
|
|
strncpy(Command+len, argv[arg], sizeof(Command)-len-1);
|
|
|
|
len += strlen(Command+len);
|
|
|
|
}
|
|
|
|
|
1997-11-18 00:20:29 +00:00
|
|
|
switch (Receive(fd, verbose | REC_PASSWD))
|
1997-06-28 01:04:54 +00:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
fprintf(stderr, "Password incorrect\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
1997-11-07 02:54:49 +00:00
|
|
|
if (len == 0) {
|
1997-11-07 20:20:15 +00:00
|
|
|
EditLine *edit;
|
|
|
|
History *hist;
|
|
|
|
const char *l, *env;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
hist = history_init();
|
|
|
|
if ((env = getenv("EL_SIZE"))) {
|
|
|
|
size = atoi(env);
|
|
|
|
if (size < 0)
|
|
|
|
size = 20;
|
|
|
|
} else
|
|
|
|
size = 20;
|
|
|
|
history(hist, H_EVENT, size);
|
|
|
|
|
|
|
|
edit = el_init("pppctl", stdin, stdout);
|
|
|
|
el_source(edit, NULL);
|
|
|
|
el_set(edit, EL_PROMPT, GetPrompt);
|
|
|
|
if ((env = getenv("EL_EDITOR")))
|
|
|
|
if (!strcmp(env, "vi"))
|
|
|
|
el_set(edit, EL_EDITOR, "vi");
|
|
|
|
else if (!strcmp(env, "emacs"))
|
|
|
|
el_set(edit, EL_EDITOR, "emacs");
|
|
|
|
el_set(edit, EL_SIGNAL, 1);
|
|
|
|
el_set(edit, EL_HIST, history, (const char *)hist);
|
1997-11-11 12:28:02 +00:00
|
|
|
while ((l = smartgets(edit, &len, fd))) {
|
1997-11-07 20:20:15 +00:00
|
|
|
if (len > 1)
|
|
|
|
history(hist, H_ENTER, l);
|
|
|
|
write(fd, l, len);
|
1997-11-18 00:20:29 +00:00
|
|
|
if (Receive(fd, REC_SHOW) != 0)
|
1997-11-07 02:54:49 +00:00
|
|
|
break;
|
1997-06-28 01:04:54 +00:00
|
|
|
}
|
1997-11-18 00:20:29 +00:00
|
|
|
fprintf(stderr, "Connection closed\n");
|
1997-11-07 20:20:15 +00:00
|
|
|
el_end(edit);
|
|
|
|
history_end(hist);
|
1997-11-07 02:54:49 +00:00
|
|
|
} else {
|
|
|
|
start = Command;
|
|
|
|
do {
|
1997-11-07 20:20:15 +00:00
|
|
|
next = strchr(start, ';');
|
1997-11-07 02:54:49 +00:00
|
|
|
while (*start == ' ' || *start == '\t')
|
|
|
|
start++;
|
|
|
|
if (next)
|
|
|
|
*next = '\0';
|
|
|
|
strcpy(Buffer, start);
|
|
|
|
Buffer[sizeof(Buffer)-2] = '\0';
|
|
|
|
strcat(Buffer, "\n");
|
|
|
|
if (verbose)
|
|
|
|
write(1, Buffer, strlen(Buffer));
|
|
|
|
write(fd, Buffer, strlen(Buffer));
|
1997-11-18 00:20:29 +00:00
|
|
|
if (Receive(fd, verbose | REC_SHOW) != 0) {
|
1997-11-07 02:54:49 +00:00
|
|
|
fprintf(stderr, "No reply from ppp\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next)
|
|
|
|
start = ++next;
|
|
|
|
} while (next && *next);
|
|
|
|
if (verbose)
|
|
|
|
puts("");
|
|
|
|
}
|
1997-06-28 01:04:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "ppp is not responding\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|