1998-02-10 03:23:50 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1998-02-10 03:23:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
1998-03-16 22:54:35 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
2001-08-14 16:05:52 +00:00
|
|
|
#include <sys/socket.h>
|
1998-04-10 13:19:23 +00:00
|
|
|
#include <sys/un.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
|
1998-04-03 19:26:29 +00:00
|
|
|
#include <errno.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
1998-04-03 19:26:02 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "layer.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "descriptor.h"
|
|
|
|
#include "prompt.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "auth.h"
|
|
|
|
#include "iplist.h"
|
|
|
|
#include "throughput.h"
|
1998-03-16 22:54:35 +00:00
|
|
|
#include "slcompress.h"
|
1998-08-26 17:39:37 +00:00
|
|
|
#include "mbuf.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-02-16 00:01:12 +00:00
|
|
|
#include "hdlc.h"
|
2000-07-19 02:10:35 +00:00
|
|
|
#include "lcp.h"
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ncpaddr.h"
|
1998-08-26 17:39:37 +00:00
|
|
|
#include "ipcp.h"
|
|
|
|
#include "filter.h"
|
1998-02-16 00:01:12 +00:00
|
|
|
#include "async.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "ccp.h"
|
1998-02-16 00:01:12 +00:00
|
|
|
#include "link.h"
|
|
|
|
#include "physical.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "mp.h"
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ipv6cp.h"
|
|
|
|
#include "ncp.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "bundle.h"
|
1998-02-17 01:05:47 +00:00
|
|
|
#include "chat.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "chap.h"
|
1998-08-07 18:42:51 +00:00
|
|
|
#include "cbcp.h"
|
1998-02-17 01:05:47 +00:00
|
|
|
#include "datalink.h"
|
1998-04-03 19:26:02 +00:00
|
|
|
#include "server.h"
|
1998-07-04 22:04:12 +00:00
|
|
|
#include "main.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
static void
|
|
|
|
prompt_Display(struct prompt *p)
|
|
|
|
{
|
1998-06-15 19:06:58 +00:00
|
|
|
/* XXX: See Index2Nam() - should we only figure this out once ? */
|
2001-03-09 20:31:02 +00:00
|
|
|
static char shostname[MAXHOSTNAMELEN];
|
1998-04-03 19:26:02 +00:00
|
|
|
const char *pconnect, *pauth;
|
|
|
|
|
|
|
|
if (p->TermMode || !p->needprompt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p->needprompt = 0;
|
|
|
|
|
|
|
|
if (p->nonewline)
|
|
|
|
p->nonewline = 0;
|
|
|
|
else
|
|
|
|
fprintf(p->Term, "\n");
|
|
|
|
|
|
|
|
if (p->auth == LOCAL_AUTH)
|
|
|
|
pauth = " ON ";
|
|
|
|
else
|
|
|
|
pauth = " on ";
|
|
|
|
|
|
|
|
if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
|
|
|
|
pconnect = "PPP";
|
2003-03-28 18:23:43 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
else if (!Enabled(p->bundle, OPT_IPCP) &&
|
|
|
|
p->bundle->ncp.ipv6cp.fsm.state == ST_OPENED)
|
|
|
|
pconnect = "PPP";
|
|
|
|
#endif
|
1998-04-03 19:26:02 +00:00
|
|
|
else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
|
|
|
|
pconnect = "PPp";
|
|
|
|
else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
|
|
|
|
pconnect = "Ppp";
|
|
|
|
else
|
|
|
|
pconnect = "ppp";
|
|
|
|
|
|
|
|
if (*shostname == '\0') {
|
1998-04-18 01:01:28 +00:00
|
|
|
char *dot;
|
1998-04-03 19:26:02 +00:00
|
|
|
|
2001-03-08 23:51:50 +00:00
|
|
|
if (gethostname(shostname, sizeof shostname) || *shostname == '\0')
|
1998-04-03 19:26:02 +00:00
|
|
|
strcpy(shostname, "localhost");
|
1998-04-18 01:01:28 +00:00
|
|
|
else if ((dot = strchr(shostname, '.')))
|
|
|
|
*dot = '\0';
|
1998-04-03 19:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
|
|
|
|
fflush(p->Term);
|
|
|
|
}
|
1998-02-10 03:23:50 +00:00
|
|
|
|
|
|
|
static int
|
2004-09-05 01:46:52 +00:00
|
|
|
prompt_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w __unused,
|
|
|
|
fd_set *e, int *n)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
|
|
|
struct prompt *p = descriptor2prompt(d);
|
1998-02-13 05:10:26 +00:00
|
|
|
int sets;
|
1998-02-10 03:23:50 +00:00
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
sets = 0;
|
1998-04-05 18:25:34 +00:00
|
|
|
|
|
|
|
if (!p->active)
|
|
|
|
return sets;
|
|
|
|
|
1998-02-10 03:23:50 +00:00
|
|
|
if (p->fd_in >= 0) {
|
1998-02-13 05:10:26 +00:00
|
|
|
if (r) {
|
|
|
|
FD_SET(p->fd_in, r);
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
|
1998-02-13 05:10:26 +00:00
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
if (e) {
|
|
|
|
FD_SET(p->fd_in, e);
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
|
1998-02-13 05:10:26 +00:00
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
if (sets && *n < p->fd_in + 1)
|
1998-02-10 03:23:50 +00:00
|
|
|
*n = p->fd_in + 1;
|
|
|
|
}
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Display(p);
|
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
return sets;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-03-14 01:46:09 +00:00
|
|
|
prompt_IsSet(struct fdescriptor *d, const fd_set *fdset)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
|
|
|
struct prompt *p = descriptor2prompt(d);
|
|
|
|
return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
prompt_ShowHelp(struct prompt *p)
|
|
|
|
{
|
1998-06-16 07:15:11 +00:00
|
|
|
prompt_Printf(p, "The following commands are available:\n");
|
|
|
|
prompt_Printf(p, " ~p\tEnter Packet mode\n");
|
|
|
|
prompt_Printf(p, " ~t\tShow timers\n");
|
|
|
|
prompt_Printf(p, " ~m\tShow memory map\n");
|
|
|
|
prompt_Printf(p, " ~.\tTerminate program\n");
|
|
|
|
prompt_Printf(p, " ~?\tThis help\n");
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-09-05 01:46:52 +00:00
|
|
|
prompt_Read(struct fdescriptor *d, struct bundle *bundle,
|
|
|
|
const fd_set *fdset __unused)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
|
|
|
struct prompt *p = descriptor2prompt(d);
|
1999-09-06 08:16:33 +00:00
|
|
|
struct prompt *op;
|
1998-02-10 03:23:50 +00:00
|
|
|
int n;
|
|
|
|
char ch;
|
|
|
|
char linebuff[LINE_LEN];
|
|
|
|
|
1998-02-17 19:28:13 +00:00
|
|
|
if (p->TermMode == NULL) {
|
1998-02-10 03:23:50 +00:00
|
|
|
n = read(p->fd_in, linebuff, sizeof linebuff - 1);
|
|
|
|
if (n > 0) {
|
|
|
|
if (linebuff[n-1] == '\n')
|
|
|
|
linebuff[--n] = '\0';
|
|
|
|
else
|
|
|
|
linebuff[n] = '\0';
|
1998-05-01 19:26:12 +00:00
|
|
|
p->nonewline = 1; /* Maybe command_Decode does a prompt */
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Required(p);
|
1999-09-06 08:16:33 +00:00
|
|
|
if (n) {
|
|
|
|
if ((op = log_PromptContext) == NULL)
|
|
|
|
log_PromptContext = p;
|
1999-12-20 20:30:02 +00:00
|
|
|
if (!command_Decode(bundle, linebuff, n, p, p->src.from))
|
|
|
|
prompt_Printf(p, "Syntax error\n");
|
1999-09-06 08:16:33 +00:00
|
|
|
log_PromptContext = op;
|
|
|
|
}
|
1998-02-10 03:23:50 +00:00
|
|
|
} else if (n <= 0) {
|
1998-05-23 22:24:50 +00:00
|
|
|
log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
|
1998-07-04 22:04:12 +00:00
|
|
|
if (!p->owner)
|
2004-09-05 01:46:52 +00:00
|
|
|
Cleanup();
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Destroy(p, 0);
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-02-17 19:28:35 +00:00
|
|
|
switch (p->TermMode->state) {
|
|
|
|
case DATALINK_CLOSED:
|
|
|
|
prompt_Printf(p, "Link lost, terminal mode.\n");
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_TtyCommandMode(p);
|
|
|
|
p->nonewline = 0;
|
|
|
|
prompt_Required(p);
|
1998-02-17 19:28:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case DATALINK_READY:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATALINK_OPEN:
|
|
|
|
prompt_Printf(p, "\nPacket mode detected.\n");
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_TtyCommandMode(p);
|
|
|
|
p->nonewline = 0;
|
1998-02-17 19:28:35 +00:00
|
|
|
/* We'll get a prompt because of our status change */
|
2002-08-25 13:30:43 +00:00
|
|
|
/* FALLTHROUGH */
|
1998-02-17 19:28:35 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* Wait 'till we're in a state we care about */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-02-10 03:23:50 +00:00
|
|
|
/*
|
|
|
|
* We are in terminal mode, decode special sequences
|
|
|
|
*/
|
|
|
|
n = read(p->fd_in, &ch, 1);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
|
1998-02-10 03:23:50 +00:00
|
|
|
|
|
|
|
if (n > 0) {
|
1998-06-15 19:06:58 +00:00
|
|
|
switch (p->readtilde) {
|
1998-02-10 03:23:50 +00:00
|
|
|
case 0:
|
|
|
|
if (ch == '~')
|
1998-06-15 19:06:58 +00:00
|
|
|
p->readtilde = 1;
|
1998-02-10 03:23:50 +00:00
|
|
|
else
|
1998-05-01 19:26:12 +00:00
|
|
|
if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
|
1998-04-03 19:26:29 +00:00
|
|
|
prompt_TtyCommandMode(p);
|
|
|
|
}
|
1998-02-10 03:23:50 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (ch) {
|
|
|
|
case '?':
|
|
|
|
prompt_ShowHelp(p);
|
|
|
|
break;
|
|
|
|
case 'p':
|
1998-02-17 19:28:35 +00:00
|
|
|
datalink_Up(p->TermMode, 0, 1);
|
|
|
|
prompt_Printf(p, "\nPacket mode.\n");
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_TtyCommandMode(p);
|
1998-02-17 19:28:35 +00:00
|
|
|
break;
|
1998-02-10 03:23:50 +00:00
|
|
|
case '.':
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_TtyCommandMode(p);
|
|
|
|
p->nonewline = 0;
|
|
|
|
prompt_Required(p);
|
1998-02-10 03:23:50 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Show(0, p);
|
1998-04-03 19:21:56 +00:00
|
|
|
break;
|
1998-02-10 03:23:50 +00:00
|
|
|
case 'm':
|
1998-06-16 07:15:11 +00:00
|
|
|
{
|
|
|
|
struct cmdargs arg;
|
|
|
|
|
|
|
|
arg.cmdtab = NULL;
|
|
|
|
arg.cmd = NULL;
|
|
|
|
arg.argc = 0;
|
|
|
|
arg.argn = 0;
|
|
|
|
arg.argv = NULL;
|
|
|
|
arg.bundle = bundle;
|
|
|
|
arg.cx = p->TermMode;
|
|
|
|
arg.prompt = p;
|
2002-06-15 08:03:30 +00:00
|
|
|
|
1998-06-16 07:15:11 +00:00
|
|
|
mbuf_Show(&arg);
|
|
|
|
}
|
1998-04-03 19:21:56 +00:00
|
|
|
break;
|
1998-02-10 03:23:50 +00:00
|
|
|
default:
|
1998-05-01 19:26:12 +00:00
|
|
|
if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
|
1998-04-03 19:26:29 +00:00
|
|
|
prompt_TtyCommandMode(p);
|
|
|
|
}
|
1998-02-10 03:23:50 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-06-15 19:06:58 +00:00
|
|
|
p->readtilde = 0;
|
1998-02-10 03:23:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-06-24 19:33:36 +00:00
|
|
|
static int
|
2004-09-05 01:46:52 +00:00
|
|
|
prompt_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused,
|
|
|
|
const fd_set *fdset __unused)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
1998-02-23 00:38:44 +00:00
|
|
|
/* We never want to write here ! */
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
|
1998-06-24 19:33:36 +00:00
|
|
|
return 0;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
struct prompt *
|
|
|
|
prompt_Create(struct server *s, struct bundle *bundle, int fd)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
1998-04-03 19:26:02 +00:00
|
|
|
struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
|
|
|
|
|
|
|
|
if (p != NULL) {
|
|
|
|
p->desc.type = PROMPT_DESCRIPTOR;
|
|
|
|
p->desc.UpdateSet = prompt_UpdateSet;
|
|
|
|
p->desc.IsSet = prompt_IsSet;
|
|
|
|
p->desc.Read = prompt_Read;
|
|
|
|
p->desc.Write = prompt_Write;
|
|
|
|
|
|
|
|
if (fd == PROMPT_STD) {
|
1998-05-27 22:43:37 +00:00
|
|
|
char *tty = ttyname(STDIN_FILENO);
|
|
|
|
|
|
|
|
if (!tty) {
|
|
|
|
free(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-04-03 19:26:02 +00:00
|
|
|
p->fd_in = STDIN_FILENO;
|
|
|
|
p->fd_out = STDOUT_FILENO;
|
|
|
|
p->Term = stdout;
|
|
|
|
p->owner = NULL;
|
|
|
|
p->auth = LOCAL_AUTH;
|
1998-04-10 13:19:23 +00:00
|
|
|
p->src.type = "Controller";
|
1998-05-27 22:43:37 +00:00
|
|
|
strncpy(p->src.from, tty, sizeof p->src.from - 1);
|
1998-04-10 13:19:23 +00:00
|
|
|
p->src.from[sizeof p->src.from - 1] = '\0';
|
1998-04-03 19:26:02 +00:00
|
|
|
tcgetattr(p->fd_in, &p->oldtio); /* Save original tty mode */
|
|
|
|
} else {
|
|
|
|
p->fd_in = p->fd_out = fd;
|
|
|
|
p->Term = fdopen(fd, "a+");
|
|
|
|
p->owner = s;
|
2001-01-26 01:41:34 +00:00
|
|
|
p->auth = *s->cfg.passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
|
1998-04-10 13:19:23 +00:00
|
|
|
p->src.type = "unknown";
|
|
|
|
*p->src.from = '\0';
|
1998-04-03 19:26:02 +00:00
|
|
|
}
|
|
|
|
p->TermMode = NULL;
|
|
|
|
p->nonewline = 1;
|
|
|
|
p->needprompt = 1;
|
1998-06-15 19:06:58 +00:00
|
|
|
p->readtilde = 0;
|
1998-04-03 19:26:02 +00:00
|
|
|
p->bundle = bundle;
|
|
|
|
log_RegisterPrompt(p);
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
return p;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Destroy(struct prompt *p, int verbose)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
1998-05-27 22:43:37 +00:00
|
|
|
if (p) {
|
|
|
|
if (p->Term != stdout) {
|
|
|
|
fclose(p->Term);
|
|
|
|
close(p->fd_in);
|
|
|
|
if (p->fd_out != p->fd_in)
|
|
|
|
close(p->fd_out);
|
|
|
|
if (verbose)
|
|
|
|
log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
|
|
|
|
} else
|
|
|
|
prompt_TtyOldMode(p);
|
|
|
|
|
|
|
|
log_UnRegisterPrompt(p);
|
|
|
|
free(p);
|
|
|
|
}
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_Printf(struct prompt *p, const char *fmt,...)
|
|
|
|
{
|
1998-04-05 18:25:34 +00:00
|
|
|
if (p && p->active) {
|
1998-02-10 03:23:50 +00:00
|
|
|
va_list ap;
|
1998-06-16 07:15:11 +00:00
|
|
|
|
1998-02-10 03:23:50 +00:00
|
|
|
va_start(ap, fmt);
|
1998-06-16 07:15:11 +00:00
|
|
|
prompt_vPrintf(p, fmt, ap);
|
1998-02-10 03:23:50 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
|
|
|
|
{
|
1998-04-05 18:25:34 +00:00
|
|
|
if (p && p->active) {
|
1998-06-16 07:15:11 +00:00
|
|
|
char nfmt[LINE_LEN];
|
|
|
|
const char *pfmt;
|
|
|
|
|
|
|
|
if (p->TermMode) {
|
|
|
|
/* Stuff '\r' in front of '\n' 'cos we're in raw mode */
|
2004-09-05 01:46:52 +00:00
|
|
|
size_t len = strlen(fmt);
|
1998-06-16 07:15:11 +00:00
|
|
|
|
1999-04-03 11:54:00 +00:00
|
|
|
if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
|
|
|
|
(len == 1 || fmt[len-2] != '\r')) {
|
1998-06-16 07:15:11 +00:00
|
|
|
strcpy(nfmt, fmt);
|
|
|
|
strcpy(nfmt + len - 1, "\r\n");
|
|
|
|
pfmt = nfmt;
|
|
|
|
} else
|
|
|
|
pfmt = fmt;
|
|
|
|
} else
|
|
|
|
pfmt = fmt;
|
|
|
|
vfprintf(p->Term, pfmt, ap);
|
1998-02-10 03:23:50 +00:00
|
|
|
fflush(p->Term);
|
1998-04-08 18:27:29 +00:00
|
|
|
p->nonewline = 1;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-04-10 13:19:23 +00:00
|
|
|
prompt_TtyInit(struct prompt *p)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
1998-04-10 13:19:23 +00:00
|
|
|
int stat, fd = p ? p->fd_in : STDIN_FILENO;
|
|
|
|
struct termios newtio;
|
1998-02-10 03:23:50 +00:00
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
stat = fcntl(fd, F_GETFL, 0);
|
1998-02-10 03:23:50 +00:00
|
|
|
if (stat > 0) {
|
|
|
|
stat |= O_NONBLOCK;
|
1998-04-10 13:19:23 +00:00
|
|
|
fcntl(fd, F_SETFL, stat);
|
1998-04-03 19:26:02 +00:00
|
|
|
}
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if (p)
|
1998-04-03 19:26:02 +00:00
|
|
|
newtio = p->oldtio;
|
1998-04-10 13:19:23 +00:00
|
|
|
else
|
|
|
|
tcgetattr(fd, &newtio);
|
|
|
|
|
|
|
|
newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
|
|
|
|
newtio.c_iflag = 0;
|
|
|
|
newtio.c_oflag &= ~OPOST;
|
|
|
|
if (!p)
|
|
|
|
newtio.c_cc[VINTR] = _POSIX_VDISABLE;
|
|
|
|
newtio.c_cc[VMIN] = 1;
|
|
|
|
newtio.c_cc[VTIME] = 0;
|
|
|
|
newtio.c_cflag |= CS8;
|
|
|
|
tcsetattr(fd, TCSANOW, &newtio);
|
|
|
|
if (p)
|
1998-04-03 19:26:02 +00:00
|
|
|
p->comtio = newtio;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set tty into command mode. We allow canonical input and echo processing.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
prompt_TtyCommandMode(struct prompt *p)
|
|
|
|
{
|
|
|
|
struct termios newtio;
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
tcgetattr(p->fd_in, &newtio);
|
|
|
|
newtio.c_lflag |= (ECHO | ISIG | ICANON);
|
|
|
|
newtio.c_iflag = p->oldtio.c_iflag;
|
|
|
|
newtio.c_oflag |= OPOST;
|
|
|
|
tcsetattr(p->fd_in, TCSADRAIN, &newtio);
|
1998-04-03 19:26:02 +00:00
|
|
|
|
1998-02-10 03:23:50 +00:00
|
|
|
stat = fcntl(p->fd_in, F_GETFL, 0);
|
|
|
|
if (stat > 0) {
|
|
|
|
stat |= O_NONBLOCK;
|
1998-04-03 19:26:02 +00:00
|
|
|
fcntl(p->fd_in, F_SETFL, stat);
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
1998-04-03 19:26:02 +00:00
|
|
|
|
1998-02-17 19:28:13 +00:00
|
|
|
p->TermMode = NULL;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set tty into terminal mode which is used while we invoke term command.
|
|
|
|
*/
|
|
|
|
void
|
1998-02-17 19:28:13 +00:00
|
|
|
prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
if (p->Term == stdout)
|
|
|
|
tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
|
|
|
|
|
1998-02-10 03:23:50 +00:00
|
|
|
stat = fcntl(p->fd_in, F_GETFL, 0);
|
|
|
|
if (stat > 0) {
|
|
|
|
stat &= ~O_NONBLOCK;
|
1998-04-03 19:26:02 +00:00
|
|
|
fcntl(p->fd_in, F_SETFL, stat);
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
1998-02-17 19:28:13 +00:00
|
|
|
p->TermMode = dl;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_TtyOldMode(struct prompt *p)
|
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
stat = fcntl(p->fd_in, F_GETFL, 0);
|
|
|
|
if (stat > 0) {
|
|
|
|
stat &= ~O_NONBLOCK;
|
1998-04-03 19:26:02 +00:00
|
|
|
fcntl(p->fd_in, F_SETFL, stat);
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
1998-04-03 19:26:02 +00:00
|
|
|
|
|
|
|
if (p->Term == stdout)
|
|
|
|
tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
prompt_pgrp(struct prompt *p)
|
|
|
|
{
|
1998-04-03 19:26:02 +00:00
|
|
|
return tcgetpgrp(p->fd_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PasswdCommand(struct cmdargs const *arg)
|
|
|
|
{
|
|
|
|
const char *pass;
|
|
|
|
|
|
|
|
if (!arg->prompt) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
|
1998-04-03 19:26:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg->prompt->owner == NULL) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "passwd: Not required\n");
|
1998-04-03 19:26:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-04-14 23:17:24 +00:00
|
|
|
if (arg->argc == arg->argn)
|
1998-04-03 19:26:02 +00:00
|
|
|
pass = "";
|
1998-04-14 23:17:24 +00:00
|
|
|
else if (arg->argc > arg->argn+1)
|
1998-04-03 19:26:02 +00:00
|
|
|
return -1;
|
|
|
|
else
|
1998-04-14 23:17:24 +00:00
|
|
|
pass = arg->argv[arg->argn];
|
1998-04-03 19:26:02 +00:00
|
|
|
|
2001-01-26 01:41:34 +00:00
|
|
|
if (!strcmp(arg->prompt->owner->cfg.passwd, pass))
|
1998-04-03 19:26:02 +00:00
|
|
|
arg->prompt->auth = LOCAL_AUTH;
|
|
|
|
else
|
|
|
|
arg->prompt->auth = LOCAL_NO_AUTH;
|
|
|
|
|
|
|
|
return 0;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
1998-04-05 18:25:34 +00:00
|
|
|
|
|
|
|
static struct pppTimer bgtimer;
|
|
|
|
|
|
|
|
static void
|
|
|
|
prompt_TimedContinue(void *v)
|
|
|
|
{
|
|
|
|
prompt_Continue((struct prompt *)v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_Continue(struct prompt *p)
|
|
|
|
{
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Stop(&bgtimer);
|
1998-04-05 18:25:34 +00:00
|
|
|
if (getpgrp() == prompt_pgrp(p)) {
|
|
|
|
prompt_TtyCommandMode(p);
|
|
|
|
p->nonewline = 1;
|
|
|
|
prompt_Required(p);
|
1998-05-23 22:24:50 +00:00
|
|
|
log_ActivatePrompt(p);
|
1998-04-05 18:25:34 +00:00
|
|
|
} else if (!p->owner) {
|
|
|
|
bgtimer.func = prompt_TimedContinue;
|
|
|
|
bgtimer.name = "prompt bg";
|
|
|
|
bgtimer.load = SECTICKS;
|
|
|
|
bgtimer.arg = p;
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Start(&bgtimer);
|
1998-04-05 18:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_Suspend(struct prompt *p)
|
|
|
|
{
|
|
|
|
if (getpgrp() == prompt_pgrp(p)) {
|
|
|
|
prompt_TtyOldMode(p);
|
1998-05-23 22:24:50 +00:00
|
|
|
log_DeactivatePrompt(p);
|
1998-04-05 18:25:34 +00:00
|
|
|
}
|
|
|
|
}
|