prompt.c revision 112753
136285Sbrian/*-
236285Sbrian * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
336285Sbrian * All rights reserved.
436285Sbrian *
536285Sbrian * Redistribution and use in source and binary forms, with or without
636285Sbrian * modification, are permitted provided that the following conditions
736285Sbrian * are met:
836285Sbrian * 1. Redistributions of source code must retain the above copyright
936285Sbrian *    notice, this list of conditions and the following disclaimer.
1036285Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1136285Sbrian *    notice, this list of conditions and the following disclaimer in the
1236285Sbrian *    documentation and/or other materials provided with the distribution.
1336285Sbrian *
1436285Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1536285Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1636285Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1736285Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1836285Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1936285Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2036285Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2136285Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2236285Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2336285Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2436285Sbrian * SUCH DAMAGE.
2536285Sbrian *
2650479Speter * $FreeBSD: head/usr.sbin/ppp/prompt.c 112753 2003-03-28 18:23:43Z ume $
2736285Sbrian */
2836285Sbrian
2936285Sbrian#include <sys/param.h>
3036285Sbrian#include <netinet/in.h>
3136285Sbrian#include <netinet/in_systm.h>
3236285Sbrian#include <netinet/ip.h>
3381634Sbrian#include <sys/socket.h>
3436285Sbrian#include <sys/un.h>
3536285Sbrian
3636285Sbrian#include <errno.h>
3736285Sbrian#include <stdarg.h>
3836285Sbrian#include <stdio.h>
3936285Sbrian#include <stdlib.h>
4036285Sbrian#include <string.h>
4136285Sbrian#include <sys/fcntl.h>
4236285Sbrian#include <termios.h>
4336285Sbrian#include <unistd.h>
4436285Sbrian
4546686Sbrian#include "layer.h"
4636285Sbrian#include "defs.h"
4736285Sbrian#include "timer.h"
4836285Sbrian#include "command.h"
4936285Sbrian#include "log.h"
5036285Sbrian#include "descriptor.h"
5136285Sbrian#include "prompt.h"
5236285Sbrian#include "fsm.h"
5336285Sbrian#include "auth.h"
5436285Sbrian#include "iplist.h"
5536285Sbrian#include "throughput.h"
5636285Sbrian#include "slcompress.h"
5738557Sbrian#include "mbuf.h"
5838557Sbrian#include "lqr.h"
5938557Sbrian#include "hdlc.h"
6063484Sbrian#include "lcp.h"
6181634Sbrian#include "ncpaddr.h"
6236285Sbrian#include "ipcp.h"
6336285Sbrian#include "filter.h"
6436285Sbrian#include "async.h"
6536285Sbrian#include "ccp.h"
6636285Sbrian#include "link.h"
6736285Sbrian#include "physical.h"
6836285Sbrian#include "mp.h"
6943313Sbrian#ifndef NORADIUS
7043313Sbrian#include "radius.h"
7143313Sbrian#endif
7281634Sbrian#include "ipv6cp.h"
7381634Sbrian#include "ncp.h"
7436285Sbrian#include "bundle.h"
7536285Sbrian#include "chat.h"
7636285Sbrian#include "chap.h"
7738174Sbrian#include "cbcp.h"
7836285Sbrian#include "datalink.h"
7936285Sbrian#include "server.h"
8037386Sbrian#include "main.h"
8136285Sbrian
8236285Sbrianstatic void
8336285Sbrianprompt_Display(struct prompt *p)
8436285Sbrian{
8537010Sbrian  /* XXX: See Index2Nam() - should we only figure this out once ? */
8674049Sbrian  static char shostname[MAXHOSTNAMELEN];
8736285Sbrian  const char *pconnect, *pauth;
8836285Sbrian
8936285Sbrian  if (p->TermMode || !p->needprompt)
9036285Sbrian    return;
9136285Sbrian
9236285Sbrian  p->needprompt = 0;
9336285Sbrian
9436285Sbrian  if (p->nonewline)
9536285Sbrian    p->nonewline = 0;
9636285Sbrian  else
9736285Sbrian    fprintf(p->Term, "\n");
9836285Sbrian
9936285Sbrian  if (p->auth == LOCAL_AUTH)
10036285Sbrian    pauth = " ON ";
10136285Sbrian  else
10236285Sbrian    pauth = " on ";
10336285Sbrian
10436285Sbrian  if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
10536285Sbrian    pconnect = "PPP";
106112753Sume#ifndef NOINET6
107112753Sume  else if (!Enabled(p->bundle, OPT_IPCP) &&
108112753Sume	   p->bundle->ncp.ipv6cp.fsm.state == ST_OPENED)
109112753Sume    pconnect = "PPP";
110112753Sume#endif
11136285Sbrian  else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
11236285Sbrian    pconnect = "PPp";
11336285Sbrian  else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
11436285Sbrian    pconnect = "Ppp";
11536285Sbrian  else
11636285Sbrian    pconnect = "ppp";
11736285Sbrian
11836285Sbrian  if (*shostname == '\0') {
11936285Sbrian    char *dot;
12036285Sbrian
12174001Sbrian    if (gethostname(shostname, sizeof shostname) || *shostname == '\0')
12236285Sbrian      strcpy(shostname, "localhost");
12336285Sbrian    else if ((dot = strchr(shostname, '.')))
12436285Sbrian      *dot = '\0';
12536285Sbrian  }
12636285Sbrian
12736285Sbrian  fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
12836285Sbrian  fflush(p->Term);
12936285Sbrian}
13036285Sbrian
13136285Sbrianstatic int
13258028Sbrianprompt_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
13336285Sbrian{
13436285Sbrian  struct prompt *p = descriptor2prompt(d);
13536285Sbrian  int sets;
13636285Sbrian
13736285Sbrian  sets = 0;
13836285Sbrian
13936285Sbrian  if (!p->active)
14036285Sbrian    return sets;
14136285Sbrian
14236285Sbrian  if (p->fd_in >= 0) {
14336285Sbrian    if (r) {
14436285Sbrian      FD_SET(p->fd_in, r);
14536285Sbrian      log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
14636285Sbrian      sets++;
14736285Sbrian    }
14836285Sbrian    if (e) {
14936285Sbrian      FD_SET(p->fd_in, e);
15036285Sbrian      log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
15136285Sbrian      sets++;
15236285Sbrian    }
15336285Sbrian    if (sets && *n < p->fd_in + 1)
15436285Sbrian      *n = p->fd_in + 1;
15536285Sbrian  }
15636285Sbrian
15736285Sbrian  prompt_Display(p);
15836285Sbrian
15936285Sbrian  return sets;
16036285Sbrian}
16136285Sbrian
16236285Sbrianstatic int
16358028Sbrianprompt_IsSet(struct fdescriptor *d, const fd_set *fdset)
16436285Sbrian{
16536285Sbrian  struct prompt *p = descriptor2prompt(d);
16636285Sbrian  return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
16736285Sbrian}
16836285Sbrian
16936285Sbrian
17036285Sbrianstatic void
17136285Sbrianprompt_ShowHelp(struct prompt *p)
17236285Sbrian{
17337011Sbrian  prompt_Printf(p, "The following commands are available:\n");
17437011Sbrian  prompt_Printf(p, " ~p\tEnter Packet mode\n");
17537011Sbrian  prompt_Printf(p, " ~t\tShow timers\n");
17637011Sbrian  prompt_Printf(p, " ~m\tShow memory map\n");
17737011Sbrian  prompt_Printf(p, " ~.\tTerminate program\n");
17837011Sbrian  prompt_Printf(p, " ~?\tThis help\n");
17936285Sbrian}
18036285Sbrian
18136285Sbrianstatic void
18258028Sbrianprompt_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
18336285Sbrian{
18436285Sbrian  struct prompt *p = descriptor2prompt(d);
18551005Sbrian  struct prompt *op;
18636285Sbrian  int n;
18736285Sbrian  char ch;
18836285Sbrian  char linebuff[LINE_LEN];
18936285Sbrian
19036285Sbrian  if (p->TermMode == NULL) {
19136285Sbrian    n = read(p->fd_in, linebuff, sizeof linebuff - 1);
19236285Sbrian    if (n > 0) {
19336285Sbrian      if (linebuff[n-1] == '\n')
19436285Sbrian        linebuff[--n] = '\0';
19536285Sbrian      else
19636285Sbrian        linebuff[n] = '\0';
19736285Sbrian      p->nonewline = 1;		/* Maybe command_Decode does a prompt */
19836285Sbrian      prompt_Required(p);
19951005Sbrian      if (n) {
20051005Sbrian        if ((op = log_PromptContext) == NULL)
20151005Sbrian          log_PromptContext = p;
20254914Sbrian        if (!command_Decode(bundle, linebuff, n, p, p->src.from))
20354914Sbrian          prompt_Printf(p, "Syntax error\n");
20451005Sbrian        log_PromptContext = op;
20551005Sbrian      }
20636285Sbrian    } else if (n <= 0) {
20736314Sbrian      log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
20837386Sbrian      if (!p->owner)
20937386Sbrian        Cleanup(EX_NORMAL);
21036285Sbrian      prompt_Destroy(p, 0);
21136285Sbrian    }
21236285Sbrian    return;
21336285Sbrian  }
21436285Sbrian
21536285Sbrian  switch (p->TermMode->state) {
21636285Sbrian    case DATALINK_CLOSED:
21736285Sbrian      prompt_Printf(p, "Link lost, terminal mode.\n");
21836285Sbrian      prompt_TtyCommandMode(p);
21936285Sbrian      p->nonewline = 0;
22036285Sbrian      prompt_Required(p);
22136285Sbrian      return;
22236285Sbrian
22336285Sbrian    case DATALINK_READY:
22436285Sbrian      break;
22536285Sbrian
22636285Sbrian    case DATALINK_OPEN:
22736285Sbrian      prompt_Printf(p, "\nPacket mode detected.\n");
22836285Sbrian      prompt_TtyCommandMode(p);
22936285Sbrian      p->nonewline = 0;
23036285Sbrian      /* We'll get a prompt because of our status change */
231102413Scharnier      /* FALLTHROUGH */
23236285Sbrian
23336285Sbrian    default:
23436285Sbrian      /* Wait 'till we're in a state we care about */
23536285Sbrian      return;
23636285Sbrian  }
23736285Sbrian
23836285Sbrian  /*
23936285Sbrian   * We are in terminal mode, decode special sequences
24036285Sbrian   */
24136285Sbrian  n = read(p->fd_in, &ch, 1);
24236285Sbrian  log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
24336285Sbrian
24436285Sbrian  if (n > 0) {
24537010Sbrian    switch (p->readtilde) {
24636285Sbrian    case 0:
24736285Sbrian      if (ch == '~')
24837010Sbrian        p->readtilde = 1;
24936285Sbrian      else
25036285Sbrian	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
25137019Sbrian	  log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
25236285Sbrian          prompt_TtyCommandMode(p);
25336285Sbrian        }
25436285Sbrian      break;
25536285Sbrian    case 1:
25636285Sbrian      switch (ch) {
25736285Sbrian      case '?':
25836285Sbrian	prompt_ShowHelp(p);
25936285Sbrian	break;
26036285Sbrian      case 'p':
26136285Sbrian        datalink_Up(p->TermMode, 0, 1);
26236285Sbrian        prompt_Printf(p, "\nPacket mode.\n");
26336285Sbrian	prompt_TtyCommandMode(p);
26436285Sbrian        break;
26536285Sbrian      case '.':
26636285Sbrian	prompt_TtyCommandMode(p);
26736285Sbrian        p->nonewline = 0;
26836285Sbrian        prompt_Required(p);
26936285Sbrian	break;
27036285Sbrian      case 't':
27136285Sbrian	timer_Show(0, p);
27236285Sbrian	break;
27336285Sbrian      case 'm':
27437011Sbrian        {
27537011Sbrian          struct cmdargs arg;
27637011Sbrian
27737011Sbrian          arg.cmdtab = NULL;
27837011Sbrian          arg.cmd = NULL;
27937011Sbrian          arg.argc = 0;
28037011Sbrian          arg.argn = 0;
28137011Sbrian          arg.argv = NULL;
28237011Sbrian          arg.bundle = bundle;
28337011Sbrian          arg.cx = p->TermMode;
28437011Sbrian          arg.prompt = p;
28598243Sbrian
28637011Sbrian	  mbuf_Show(&arg);
28737011Sbrian        }
28836285Sbrian	break;
28936285Sbrian      default:
29036285Sbrian	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
29137019Sbrian	  log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
29236285Sbrian          prompt_TtyCommandMode(p);
29336285Sbrian        }
29436285Sbrian	break;
29536285Sbrian      }
29637010Sbrian      p->readtilde = 0;
29736285Sbrian      break;
29836285Sbrian    }
29936285Sbrian  }
30036285Sbrian}
30136285Sbrian
30237141Sbrianstatic int
30358028Sbrianprompt_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
30436285Sbrian{
30536285Sbrian  /* We never want to write here ! */
30637019Sbrian  log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
30737141Sbrian  return 0;
30836285Sbrian}
30936285Sbrian
31036285Sbrianstruct prompt *
31136285Sbrianprompt_Create(struct server *s, struct bundle *bundle, int fd)
31236285Sbrian{
31336285Sbrian  struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
31436285Sbrian
31536285Sbrian  if (p != NULL) {
31636285Sbrian    p->desc.type = PROMPT_DESCRIPTOR;
31736285Sbrian    p->desc.UpdateSet = prompt_UpdateSet;
31836285Sbrian    p->desc.IsSet = prompt_IsSet;
31936285Sbrian    p->desc.Read = prompt_Read;
32036285Sbrian    p->desc.Write = prompt_Write;
32136285Sbrian
32236285Sbrian    if (fd == PROMPT_STD) {
32336431Sbrian      char *tty = ttyname(STDIN_FILENO);
32436431Sbrian
32536431Sbrian      if (!tty) {
32636431Sbrian        free(p);
32736431Sbrian        return NULL;
32836431Sbrian      }
32936285Sbrian      p->fd_in = STDIN_FILENO;
33036285Sbrian      p->fd_out = STDOUT_FILENO;
33136285Sbrian      p->Term = stdout;
33236285Sbrian      p->owner = NULL;
33336285Sbrian      p->auth = LOCAL_AUTH;
33436285Sbrian      p->src.type = "Controller";
33536431Sbrian      strncpy(p->src.from, tty, sizeof p->src.from - 1);
33636285Sbrian      p->src.from[sizeof p->src.from - 1] = '\0';
33736285Sbrian      tcgetattr(p->fd_in, &p->oldtio);	/* Save original tty mode */
33836285Sbrian    } else {
33936285Sbrian      p->fd_in = p->fd_out = fd;
34036285Sbrian      p->Term = fdopen(fd, "a+");
34136285Sbrian      p->owner = s;
34271657Sbrian      p->auth = *s->cfg.passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
34336285Sbrian      p->src.type = "unknown";
34436285Sbrian      *p->src.from = '\0';
34536285Sbrian    }
34636285Sbrian    p->TermMode = NULL;
34736285Sbrian    p->nonewline = 1;
34836285Sbrian    p->needprompt = 1;
34937010Sbrian    p->readtilde = 0;
35036285Sbrian    p->bundle = bundle;
35136285Sbrian    log_RegisterPrompt(p);
35236285Sbrian  }
35336285Sbrian
35436285Sbrian  return p;
35536285Sbrian}
35636285Sbrian
35736285Sbrianvoid
35836285Sbrianprompt_Destroy(struct prompt *p, int verbose)
35936285Sbrian{
36036431Sbrian  if (p) {
36136431Sbrian    if (p->Term != stdout) {
36236431Sbrian      fclose(p->Term);
36336431Sbrian      close(p->fd_in);
36436431Sbrian      if (p->fd_out != p->fd_in)
36536431Sbrian        close(p->fd_out);
36636431Sbrian      if (verbose)
36736431Sbrian        log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
36836431Sbrian    } else
36936431Sbrian      prompt_TtyOldMode(p);
37036285Sbrian
37136431Sbrian    log_UnRegisterPrompt(p);
37236431Sbrian    free(p);
37336431Sbrian  }
37436285Sbrian}
37536285Sbrian
37636285Sbrianvoid
37736285Sbrianprompt_Printf(struct prompt *p, const char *fmt,...)
37836285Sbrian{
37936285Sbrian  if (p && p->active) {
38036285Sbrian    va_list ap;
38137011Sbrian
38236285Sbrian    va_start(ap, fmt);
38337011Sbrian    prompt_vPrintf(p, fmt, ap);
38436285Sbrian    va_end(ap);
38536285Sbrian  }
38636285Sbrian}
38736285Sbrian
38836285Sbrianvoid
38936285Sbrianprompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
39036285Sbrian{
39136285Sbrian  if (p && p->active) {
39237011Sbrian    char nfmt[LINE_LEN];
39337011Sbrian    const char *pfmt;
39437011Sbrian
39537011Sbrian    if (p->TermMode) {
39637011Sbrian      /* Stuff '\r' in front of '\n' 'cos we're in raw mode */
39737011Sbrian      int len = strlen(fmt);
39837011Sbrian
39945264Sbrian      if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
40045264Sbrian          (len == 1 || fmt[len-2] != '\r')) {
40137011Sbrian        strcpy(nfmt, fmt);
40237011Sbrian        strcpy(nfmt + len - 1, "\r\n");
40337011Sbrian        pfmt = nfmt;
40437011Sbrian      } else
40537011Sbrian        pfmt = fmt;
40637011Sbrian    } else
40737011Sbrian      pfmt = fmt;
40837011Sbrian    vfprintf(p->Term, pfmt, ap);
40936285Sbrian    fflush(p->Term);
41036285Sbrian    p->nonewline = 1;
41136285Sbrian  }
41236285Sbrian}
41336285Sbrian
41436285Sbrianvoid
41536285Sbrianprompt_TtyInit(struct prompt *p)
41636285Sbrian{
41736285Sbrian  int stat, fd = p ? p->fd_in : STDIN_FILENO;
41836285Sbrian  struct termios newtio;
41936285Sbrian
42036285Sbrian  stat = fcntl(fd, F_GETFL, 0);
42136285Sbrian  if (stat > 0) {
42236285Sbrian    stat |= O_NONBLOCK;
42336285Sbrian    fcntl(fd, F_SETFL, stat);
42436285Sbrian  }
42536285Sbrian
42636285Sbrian  if (p)
42736285Sbrian    newtio = p->oldtio;
42836285Sbrian  else
42936285Sbrian    tcgetattr(fd, &newtio);
43036285Sbrian
43136285Sbrian  newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
43236285Sbrian  newtio.c_iflag = 0;
43336285Sbrian  newtio.c_oflag &= ~OPOST;
43436285Sbrian  if (!p)
43536285Sbrian    newtio.c_cc[VINTR] = _POSIX_VDISABLE;
43636285Sbrian  newtio.c_cc[VMIN] = 1;
43736285Sbrian  newtio.c_cc[VTIME] = 0;
43836285Sbrian  newtio.c_cflag |= CS8;
43936285Sbrian  tcsetattr(fd, TCSANOW, &newtio);
44036285Sbrian  if (p)
44136285Sbrian    p->comtio = newtio;
44236285Sbrian}
44336285Sbrian
44436285Sbrian/*
44536285Sbrian *  Set tty into command mode. We allow canonical input and echo processing.
44636285Sbrian */
44736285Sbrianvoid
44836285Sbrianprompt_TtyCommandMode(struct prompt *p)
44936285Sbrian{
45036285Sbrian  struct termios newtio;
45136285Sbrian  int stat;
45236285Sbrian
45336285Sbrian  tcgetattr(p->fd_in, &newtio);
45436285Sbrian  newtio.c_lflag |= (ECHO | ISIG | ICANON);
45536285Sbrian  newtio.c_iflag = p->oldtio.c_iflag;
45636285Sbrian  newtio.c_oflag |= OPOST;
45736285Sbrian  tcsetattr(p->fd_in, TCSADRAIN, &newtio);
45836285Sbrian
45936285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
46036285Sbrian  if (stat > 0) {
46136285Sbrian    stat |= O_NONBLOCK;
46236285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
46336285Sbrian  }
46436285Sbrian
46536285Sbrian  p->TermMode = NULL;
46636285Sbrian}
46736285Sbrian
46836285Sbrian/*
46936285Sbrian * Set tty into terminal mode which is used while we invoke term command.
47036285Sbrian */
47136285Sbrianvoid
47236285Sbrianprompt_TtyTermMode(struct prompt *p, struct datalink *dl)
47336285Sbrian{
47436285Sbrian  int stat;
47536285Sbrian
47636285Sbrian  if (p->Term == stdout)
47736285Sbrian    tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
47836285Sbrian
47936285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
48036285Sbrian  if (stat > 0) {
48136285Sbrian    stat &= ~O_NONBLOCK;
48236285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
48336285Sbrian  }
48436285Sbrian  p->TermMode = dl;
48536285Sbrian}
48636285Sbrian
48736285Sbrianvoid
48836285Sbrianprompt_TtyOldMode(struct prompt *p)
48936285Sbrian{
49036285Sbrian  int stat;
49136285Sbrian
49236285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
49336285Sbrian  if (stat > 0) {
49436285Sbrian    stat &= ~O_NONBLOCK;
49536285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
49636285Sbrian  }
49736285Sbrian
49836285Sbrian  if (p->Term == stdout)
49936285Sbrian    tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
50036285Sbrian}
50136285Sbrian
50236285Sbrianpid_t
50336285Sbrianprompt_pgrp(struct prompt *p)
50436285Sbrian{
50536285Sbrian  return tcgetpgrp(p->fd_in);
50636285Sbrian}
50736285Sbrian
50836285Sbrianint
50936285SbrianPasswdCommand(struct cmdargs const *arg)
51036285Sbrian{
51136285Sbrian  const char *pass;
51236285Sbrian
51336285Sbrian  if (!arg->prompt) {
51436285Sbrian    log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
51536285Sbrian    return 0;
51636285Sbrian  }
51736285Sbrian
51836285Sbrian  if (arg->prompt->owner == NULL) {
51936285Sbrian    log_Printf(LogWARN, "passwd: Not required\n");
52036285Sbrian    return 0;
52136285Sbrian  }
52236285Sbrian
52336285Sbrian  if (arg->argc == arg->argn)
52436285Sbrian    pass = "";
52536285Sbrian  else if (arg->argc > arg->argn+1)
52636285Sbrian    return -1;
52736285Sbrian  else
52836285Sbrian    pass = arg->argv[arg->argn];
52936285Sbrian
53071657Sbrian  if (!strcmp(arg->prompt->owner->cfg.passwd, pass))
53136285Sbrian    arg->prompt->auth = LOCAL_AUTH;
53236285Sbrian  else
53336285Sbrian    arg->prompt->auth = LOCAL_NO_AUTH;
53436285Sbrian
53536285Sbrian  return 0;
53636285Sbrian}
53736285Sbrian
53836285Sbrianstatic struct pppTimer bgtimer;
53936285Sbrian
54036285Sbrianstatic void
54136285Sbrianprompt_TimedContinue(void *v)
54236285Sbrian{
54336285Sbrian  prompt_Continue((struct prompt *)v);
54436285Sbrian}
54536285Sbrian
54636285Sbrianvoid
54736285Sbrianprompt_Continue(struct prompt *p)
54836285Sbrian{
54936285Sbrian  timer_Stop(&bgtimer);
55036285Sbrian  if (getpgrp() == prompt_pgrp(p)) {
55136285Sbrian    prompt_TtyCommandMode(p);
55236285Sbrian    p->nonewline = 1;
55336285Sbrian    prompt_Required(p);
55436314Sbrian    log_ActivatePrompt(p);
55536285Sbrian  } else if (!p->owner) {
55636285Sbrian    bgtimer.func = prompt_TimedContinue;
55736285Sbrian    bgtimer.name = "prompt bg";
55836285Sbrian    bgtimer.load = SECTICKS;
55936285Sbrian    bgtimer.arg = p;
56036285Sbrian    timer_Start(&bgtimer);
56136285Sbrian  }
56236285Sbrian}
56336285Sbrian
56436285Sbrianvoid
56536285Sbrianprompt_Suspend(struct prompt *p)
56636285Sbrian{
56736285Sbrian  if (getpgrp() == prompt_pgrp(p)) {
56836285Sbrian    prompt_TtyOldMode(p);
56936314Sbrian    log_DeactivatePrompt(p);
57036285Sbrian  }
57136285Sbrian}
572