prompt.c revision 74049
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 74049 2001-03-09 20:31:02Z brian $
2736285Sbrian */
2836285Sbrian
2936285Sbrian#include <sys/param.h>
3036285Sbrian#include <netinet/in.h>
3136285Sbrian#include <netinet/in_systm.h>
3236285Sbrian#include <netinet/ip.h>
3336285Sbrian#include <sys/un.h>
3436285Sbrian
3536285Sbrian#include <errno.h>
3636285Sbrian#include <stdarg.h>
3736285Sbrian#include <stdio.h>
3836285Sbrian#include <stdlib.h>
3936285Sbrian#include <string.h>
4036285Sbrian#include <sys/fcntl.h>
4136285Sbrian#include <termios.h>
4236285Sbrian#include <unistd.h>
4336285Sbrian
4446686Sbrian#include "layer.h"
4536285Sbrian#include "defs.h"
4636285Sbrian#include "timer.h"
4736285Sbrian#include "command.h"
4836285Sbrian#include "log.h"
4936285Sbrian#include "descriptor.h"
5036285Sbrian#include "prompt.h"
5136285Sbrian#include "fsm.h"
5236285Sbrian#include "auth.h"
5336285Sbrian#include "iplist.h"
5436285Sbrian#include "throughput.h"
5536285Sbrian#include "slcompress.h"
5638557Sbrian#include "mbuf.h"
5738557Sbrian#include "lqr.h"
5838557Sbrian#include "hdlc.h"
5963484Sbrian#include "lcp.h"
6036285Sbrian#include "ipcp.h"
6136285Sbrian#include "filter.h"
6236285Sbrian#include "async.h"
6336285Sbrian#include "ccp.h"
6436285Sbrian#include "link.h"
6536285Sbrian#include "physical.h"
6636285Sbrian#include "mp.h"
6743313Sbrian#ifndef NORADIUS
6843313Sbrian#include "radius.h"
6943313Sbrian#endif
7036285Sbrian#include "bundle.h"
7136285Sbrian#include "chat.h"
7236285Sbrian#include "chap.h"
7338174Sbrian#include "cbcp.h"
7436285Sbrian#include "datalink.h"
7536285Sbrian#include "server.h"
7637386Sbrian#include "main.h"
7736285Sbrian
7836285Sbrianstatic void
7936285Sbrianprompt_Display(struct prompt *p)
8036285Sbrian{
8137010Sbrian  /* XXX: See Index2Nam() - should we only figure this out once ? */
8274049Sbrian  static char shostname[MAXHOSTNAMELEN];
8336285Sbrian  const char *pconnect, *pauth;
8436285Sbrian
8536285Sbrian  if (p->TermMode || !p->needprompt)
8636285Sbrian    return;
8736285Sbrian
8836285Sbrian  p->needprompt = 0;
8936285Sbrian
9036285Sbrian  if (p->nonewline)
9136285Sbrian    p->nonewline = 0;
9236285Sbrian  else
9336285Sbrian    fprintf(p->Term, "\n");
9436285Sbrian
9536285Sbrian  if (p->auth == LOCAL_AUTH)
9636285Sbrian    pauth = " ON ";
9736285Sbrian  else
9836285Sbrian    pauth = " on ";
9936285Sbrian
10036285Sbrian  if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
10136285Sbrian    pconnect = "PPP";
10236285Sbrian  else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
10336285Sbrian    pconnect = "PPp";
10436285Sbrian  else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
10536285Sbrian    pconnect = "Ppp";
10636285Sbrian  else
10736285Sbrian    pconnect = "ppp";
10836285Sbrian
10936285Sbrian  if (*shostname == '\0') {
11036285Sbrian    char *dot;
11136285Sbrian
11274001Sbrian    if (gethostname(shostname, sizeof shostname) || *shostname == '\0')
11336285Sbrian      strcpy(shostname, "localhost");
11436285Sbrian    else if ((dot = strchr(shostname, '.')))
11536285Sbrian      *dot = '\0';
11636285Sbrian  }
11736285Sbrian
11836285Sbrian  fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
11936285Sbrian  fflush(p->Term);
12036285Sbrian}
12136285Sbrian
12236285Sbrianstatic int
12358028Sbrianprompt_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
12436285Sbrian{
12536285Sbrian  struct prompt *p = descriptor2prompt(d);
12636285Sbrian  int sets;
12736285Sbrian
12836285Sbrian  sets = 0;
12936285Sbrian
13036285Sbrian  if (!p->active)
13136285Sbrian    return sets;
13236285Sbrian
13336285Sbrian  if (p->fd_in >= 0) {
13436285Sbrian    if (r) {
13536285Sbrian      FD_SET(p->fd_in, r);
13636285Sbrian      log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
13736285Sbrian      sets++;
13836285Sbrian    }
13936285Sbrian    if (e) {
14036285Sbrian      FD_SET(p->fd_in, e);
14136285Sbrian      log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
14236285Sbrian      sets++;
14336285Sbrian    }
14436285Sbrian    if (sets && *n < p->fd_in + 1)
14536285Sbrian      *n = p->fd_in + 1;
14636285Sbrian  }
14736285Sbrian
14836285Sbrian  prompt_Display(p);
14936285Sbrian
15036285Sbrian  return sets;
15136285Sbrian}
15236285Sbrian
15336285Sbrianstatic int
15458028Sbrianprompt_IsSet(struct fdescriptor *d, const fd_set *fdset)
15536285Sbrian{
15636285Sbrian  struct prompt *p = descriptor2prompt(d);
15736285Sbrian  return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
15836285Sbrian}
15936285Sbrian
16036285Sbrian
16136285Sbrianstatic void
16236285Sbrianprompt_ShowHelp(struct prompt *p)
16336285Sbrian{
16437011Sbrian  prompt_Printf(p, "The following commands are available:\n");
16537011Sbrian  prompt_Printf(p, " ~p\tEnter Packet mode\n");
16637011Sbrian  prompt_Printf(p, " ~t\tShow timers\n");
16737011Sbrian  prompt_Printf(p, " ~m\tShow memory map\n");
16837011Sbrian  prompt_Printf(p, " ~.\tTerminate program\n");
16937011Sbrian  prompt_Printf(p, " ~?\tThis help\n");
17036285Sbrian}
17136285Sbrian
17236285Sbrianstatic void
17358028Sbrianprompt_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
17436285Sbrian{
17536285Sbrian  struct prompt *p = descriptor2prompt(d);
17651005Sbrian  struct prompt *op;
17736285Sbrian  int n;
17836285Sbrian  char ch;
17936285Sbrian  char linebuff[LINE_LEN];
18036285Sbrian
18136285Sbrian  if (p->TermMode == NULL) {
18236285Sbrian    n = read(p->fd_in, linebuff, sizeof linebuff - 1);
18336285Sbrian    if (n > 0) {
18436285Sbrian      if (linebuff[n-1] == '\n')
18536285Sbrian        linebuff[--n] = '\0';
18636285Sbrian      else
18736285Sbrian        linebuff[n] = '\0';
18836285Sbrian      p->nonewline = 1;		/* Maybe command_Decode does a prompt */
18936285Sbrian      prompt_Required(p);
19051005Sbrian      if (n) {
19151005Sbrian        if ((op = log_PromptContext) == NULL)
19251005Sbrian          log_PromptContext = p;
19354914Sbrian        if (!command_Decode(bundle, linebuff, n, p, p->src.from))
19454914Sbrian          prompt_Printf(p, "Syntax error\n");
19551005Sbrian        log_PromptContext = op;
19651005Sbrian      }
19736285Sbrian    } else if (n <= 0) {
19836314Sbrian      log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
19937386Sbrian      if (!p->owner)
20037386Sbrian        Cleanup(EX_NORMAL);
20136285Sbrian      prompt_Destroy(p, 0);
20236285Sbrian    }
20336285Sbrian    return;
20436285Sbrian  }
20536285Sbrian
20636285Sbrian  switch (p->TermMode->state) {
20736285Sbrian    case DATALINK_CLOSED:
20836285Sbrian      prompt_Printf(p, "Link lost, terminal mode.\n");
20936285Sbrian      prompt_TtyCommandMode(p);
21036285Sbrian      p->nonewline = 0;
21136285Sbrian      prompt_Required(p);
21236285Sbrian      return;
21336285Sbrian
21436285Sbrian    case DATALINK_READY:
21536285Sbrian      break;
21636285Sbrian
21736285Sbrian    case DATALINK_OPEN:
21836285Sbrian      prompt_Printf(p, "\nPacket mode detected.\n");
21936285Sbrian      prompt_TtyCommandMode(p);
22036285Sbrian      p->nonewline = 0;
22136285Sbrian      /* We'll get a prompt because of our status change */
22236285Sbrian      /* Fall through */
22336285Sbrian
22436285Sbrian    default:
22536285Sbrian      /* Wait 'till we're in a state we care about */
22636285Sbrian      return;
22736285Sbrian  }
22836285Sbrian
22936285Sbrian  /*
23036285Sbrian   * We are in terminal mode, decode special sequences
23136285Sbrian   */
23236285Sbrian  n = read(p->fd_in, &ch, 1);
23336285Sbrian  log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
23436285Sbrian
23536285Sbrian  if (n > 0) {
23637010Sbrian    switch (p->readtilde) {
23736285Sbrian    case 0:
23836285Sbrian      if (ch == '~')
23937010Sbrian        p->readtilde = 1;
24036285Sbrian      else
24136285Sbrian	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
24237019Sbrian	  log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
24336285Sbrian          prompt_TtyCommandMode(p);
24436285Sbrian        }
24536285Sbrian      break;
24636285Sbrian    case 1:
24736285Sbrian      switch (ch) {
24836285Sbrian      case '?':
24936285Sbrian	prompt_ShowHelp(p);
25036285Sbrian	break;
25136285Sbrian      case 'p':
25236285Sbrian        datalink_Up(p->TermMode, 0, 1);
25336285Sbrian        prompt_Printf(p, "\nPacket mode.\n");
25436285Sbrian	prompt_TtyCommandMode(p);
25536285Sbrian        break;
25636285Sbrian      case '.':
25736285Sbrian	prompt_TtyCommandMode(p);
25836285Sbrian        p->nonewline = 0;
25936285Sbrian        prompt_Required(p);
26036285Sbrian	break;
26136285Sbrian      case 't':
26236285Sbrian	timer_Show(0, p);
26336285Sbrian	break;
26436285Sbrian      case 'm':
26537011Sbrian        {
26637011Sbrian          struct cmdargs arg;
26737011Sbrian
26837011Sbrian          arg.cmdtab = NULL;
26937011Sbrian          arg.cmd = NULL;
27037011Sbrian          arg.argc = 0;
27137011Sbrian          arg.argn = 0;
27237011Sbrian          arg.argv = NULL;
27337011Sbrian          arg.bundle = bundle;
27437011Sbrian          arg.cx = p->TermMode;
27537011Sbrian          arg.prompt = p;
27637011Sbrian
27737011Sbrian	  mbuf_Show(&arg);
27837011Sbrian        }
27936285Sbrian	break;
28036285Sbrian      default:
28136285Sbrian	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
28237019Sbrian	  log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
28336285Sbrian          prompt_TtyCommandMode(p);
28436285Sbrian        }
28536285Sbrian	break;
28636285Sbrian      }
28737010Sbrian      p->readtilde = 0;
28836285Sbrian      break;
28936285Sbrian    }
29036285Sbrian  }
29136285Sbrian}
29236285Sbrian
29337141Sbrianstatic int
29458028Sbrianprompt_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
29536285Sbrian{
29636285Sbrian  /* We never want to write here ! */
29737019Sbrian  log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
29837141Sbrian  return 0;
29936285Sbrian}
30036285Sbrian
30136285Sbrianstruct prompt *
30236285Sbrianprompt_Create(struct server *s, struct bundle *bundle, int fd)
30336285Sbrian{
30436285Sbrian  struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
30536285Sbrian
30636285Sbrian  if (p != NULL) {
30736285Sbrian    p->desc.type = PROMPT_DESCRIPTOR;
30836285Sbrian    p->desc.UpdateSet = prompt_UpdateSet;
30936285Sbrian    p->desc.IsSet = prompt_IsSet;
31036285Sbrian    p->desc.Read = prompt_Read;
31136285Sbrian    p->desc.Write = prompt_Write;
31236285Sbrian
31336285Sbrian    if (fd == PROMPT_STD) {
31436431Sbrian      char *tty = ttyname(STDIN_FILENO);
31536431Sbrian
31636431Sbrian      if (!tty) {
31736431Sbrian        free(p);
31836431Sbrian        return NULL;
31936431Sbrian      }
32036285Sbrian      p->fd_in = STDIN_FILENO;
32136285Sbrian      p->fd_out = STDOUT_FILENO;
32236285Sbrian      p->Term = stdout;
32336285Sbrian      p->owner = NULL;
32436285Sbrian      p->auth = LOCAL_AUTH;
32536285Sbrian      p->src.type = "Controller";
32636431Sbrian      strncpy(p->src.from, tty, sizeof p->src.from - 1);
32736285Sbrian      p->src.from[sizeof p->src.from - 1] = '\0';
32836285Sbrian      tcgetattr(p->fd_in, &p->oldtio);	/* Save original tty mode */
32936285Sbrian    } else {
33036285Sbrian      p->fd_in = p->fd_out = fd;
33136285Sbrian      p->Term = fdopen(fd, "a+");
33236285Sbrian      p->owner = s;
33371657Sbrian      p->auth = *s->cfg.passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
33436285Sbrian      p->src.type = "unknown";
33536285Sbrian      *p->src.from = '\0';
33636285Sbrian    }
33736285Sbrian    p->TermMode = NULL;
33836285Sbrian    p->nonewline = 1;
33936285Sbrian    p->needprompt = 1;
34037010Sbrian    p->readtilde = 0;
34136285Sbrian    p->bundle = bundle;
34236285Sbrian    log_RegisterPrompt(p);
34336285Sbrian  }
34436285Sbrian
34536285Sbrian  return p;
34636285Sbrian}
34736285Sbrian
34836285Sbrianvoid
34936285Sbrianprompt_Destroy(struct prompt *p, int verbose)
35036285Sbrian{
35136431Sbrian  if (p) {
35236431Sbrian    if (p->Term != stdout) {
35336431Sbrian      fclose(p->Term);
35436431Sbrian      close(p->fd_in);
35536431Sbrian      if (p->fd_out != p->fd_in)
35636431Sbrian        close(p->fd_out);
35736431Sbrian      if (verbose)
35836431Sbrian        log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
35936431Sbrian    } else
36036431Sbrian      prompt_TtyOldMode(p);
36136285Sbrian
36236431Sbrian    log_UnRegisterPrompt(p);
36336431Sbrian    free(p);
36436431Sbrian  }
36536285Sbrian}
36636285Sbrian
36736285Sbrianvoid
36836285Sbrianprompt_Printf(struct prompt *p, const char *fmt,...)
36936285Sbrian{
37036285Sbrian  if (p && p->active) {
37136285Sbrian    va_list ap;
37237011Sbrian
37336285Sbrian    va_start(ap, fmt);
37437011Sbrian    prompt_vPrintf(p, fmt, ap);
37536285Sbrian    va_end(ap);
37636285Sbrian  }
37736285Sbrian}
37836285Sbrian
37936285Sbrianvoid
38036285Sbrianprompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
38136285Sbrian{
38236285Sbrian  if (p && p->active) {
38337011Sbrian    char nfmt[LINE_LEN];
38437011Sbrian    const char *pfmt;
38537011Sbrian
38637011Sbrian    if (p->TermMode) {
38737011Sbrian      /* Stuff '\r' in front of '\n' 'cos we're in raw mode */
38837011Sbrian      int len = strlen(fmt);
38937011Sbrian
39045264Sbrian      if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
39145264Sbrian          (len == 1 || fmt[len-2] != '\r')) {
39237011Sbrian        strcpy(nfmt, fmt);
39337011Sbrian        strcpy(nfmt + len - 1, "\r\n");
39437011Sbrian        pfmt = nfmt;
39537011Sbrian      } else
39637011Sbrian        pfmt = fmt;
39737011Sbrian    } else
39837011Sbrian      pfmt = fmt;
39937011Sbrian    vfprintf(p->Term, pfmt, ap);
40036285Sbrian    fflush(p->Term);
40136285Sbrian    p->nonewline = 1;
40236285Sbrian  }
40336285Sbrian}
40436285Sbrian
40536285Sbrianvoid
40636285Sbrianprompt_TtyInit(struct prompt *p)
40736285Sbrian{
40836285Sbrian  int stat, fd = p ? p->fd_in : STDIN_FILENO;
40936285Sbrian  struct termios newtio;
41036285Sbrian
41136285Sbrian  stat = fcntl(fd, F_GETFL, 0);
41236285Sbrian  if (stat > 0) {
41336285Sbrian    stat |= O_NONBLOCK;
41436285Sbrian    fcntl(fd, F_SETFL, stat);
41536285Sbrian  }
41636285Sbrian
41736285Sbrian  if (p)
41836285Sbrian    newtio = p->oldtio;
41936285Sbrian  else
42036285Sbrian    tcgetattr(fd, &newtio);
42136285Sbrian
42236285Sbrian  newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
42336285Sbrian  newtio.c_iflag = 0;
42436285Sbrian  newtio.c_oflag &= ~OPOST;
42536285Sbrian  if (!p)
42636285Sbrian    newtio.c_cc[VINTR] = _POSIX_VDISABLE;
42736285Sbrian  newtio.c_cc[VMIN] = 1;
42836285Sbrian  newtio.c_cc[VTIME] = 0;
42936285Sbrian  newtio.c_cflag |= CS8;
43036285Sbrian  tcsetattr(fd, TCSANOW, &newtio);
43136285Sbrian  if (p)
43236285Sbrian    p->comtio = newtio;
43336285Sbrian}
43436285Sbrian
43536285Sbrian/*
43636285Sbrian *  Set tty into command mode. We allow canonical input and echo processing.
43736285Sbrian */
43836285Sbrianvoid
43936285Sbrianprompt_TtyCommandMode(struct prompt *p)
44036285Sbrian{
44136285Sbrian  struct termios newtio;
44236285Sbrian  int stat;
44336285Sbrian
44436285Sbrian  tcgetattr(p->fd_in, &newtio);
44536285Sbrian  newtio.c_lflag |= (ECHO | ISIG | ICANON);
44636285Sbrian  newtio.c_iflag = p->oldtio.c_iflag;
44736285Sbrian  newtio.c_oflag |= OPOST;
44836285Sbrian  tcsetattr(p->fd_in, TCSADRAIN, &newtio);
44936285Sbrian
45036285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
45136285Sbrian  if (stat > 0) {
45236285Sbrian    stat |= O_NONBLOCK;
45336285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
45436285Sbrian  }
45536285Sbrian
45636285Sbrian  p->TermMode = NULL;
45736285Sbrian}
45836285Sbrian
45936285Sbrian/*
46036285Sbrian * Set tty into terminal mode which is used while we invoke term command.
46136285Sbrian */
46236285Sbrianvoid
46336285Sbrianprompt_TtyTermMode(struct prompt *p, struct datalink *dl)
46436285Sbrian{
46536285Sbrian  int stat;
46636285Sbrian
46736285Sbrian  if (p->Term == stdout)
46836285Sbrian    tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
46936285Sbrian
47036285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
47136285Sbrian  if (stat > 0) {
47236285Sbrian    stat &= ~O_NONBLOCK;
47336285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
47436285Sbrian  }
47536285Sbrian  p->TermMode = dl;
47636285Sbrian}
47736285Sbrian
47836285Sbrianvoid
47936285Sbrianprompt_TtyOldMode(struct prompt *p)
48036285Sbrian{
48136285Sbrian  int stat;
48236285Sbrian
48336285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
48436285Sbrian  if (stat > 0) {
48536285Sbrian    stat &= ~O_NONBLOCK;
48636285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
48736285Sbrian  }
48836285Sbrian
48936285Sbrian  if (p->Term == stdout)
49036285Sbrian    tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
49136285Sbrian}
49236285Sbrian
49336285Sbrianpid_t
49436285Sbrianprompt_pgrp(struct prompt *p)
49536285Sbrian{
49636285Sbrian  return tcgetpgrp(p->fd_in);
49736285Sbrian}
49836285Sbrian
49936285Sbrianint
50036285SbrianPasswdCommand(struct cmdargs const *arg)
50136285Sbrian{
50236285Sbrian  const char *pass;
50336285Sbrian
50436285Sbrian  if (!arg->prompt) {
50536285Sbrian    log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
50636285Sbrian    return 0;
50736285Sbrian  }
50836285Sbrian
50936285Sbrian  if (arg->prompt->owner == NULL) {
51036285Sbrian    log_Printf(LogWARN, "passwd: Not required\n");
51136285Sbrian    return 0;
51236285Sbrian  }
51336285Sbrian
51436285Sbrian  if (arg->argc == arg->argn)
51536285Sbrian    pass = "";
51636285Sbrian  else if (arg->argc > arg->argn+1)
51736285Sbrian    return -1;
51836285Sbrian  else
51936285Sbrian    pass = arg->argv[arg->argn];
52036285Sbrian
52171657Sbrian  if (!strcmp(arg->prompt->owner->cfg.passwd, pass))
52236285Sbrian    arg->prompt->auth = LOCAL_AUTH;
52336285Sbrian  else
52436285Sbrian    arg->prompt->auth = LOCAL_NO_AUTH;
52536285Sbrian
52636285Sbrian  return 0;
52736285Sbrian}
52836285Sbrian
52936285Sbrianstatic struct pppTimer bgtimer;
53036285Sbrian
53136285Sbrianstatic void
53236285Sbrianprompt_TimedContinue(void *v)
53336285Sbrian{
53436285Sbrian  prompt_Continue((struct prompt *)v);
53536285Sbrian}
53636285Sbrian
53736285Sbrianvoid
53836285Sbrianprompt_Continue(struct prompt *p)
53936285Sbrian{
54036285Sbrian  timer_Stop(&bgtimer);
54136285Sbrian  if (getpgrp() == prompt_pgrp(p)) {
54236285Sbrian    prompt_TtyCommandMode(p);
54336285Sbrian    p->nonewline = 1;
54436285Sbrian    prompt_Required(p);
54536314Sbrian    log_ActivatePrompt(p);
54636285Sbrian  } else if (!p->owner) {
54736285Sbrian    bgtimer.func = prompt_TimedContinue;
54836285Sbrian    bgtimer.name = "prompt bg";
54936285Sbrian    bgtimer.load = SECTICKS;
55036285Sbrian    bgtimer.arg = p;
55136285Sbrian    timer_Start(&bgtimer);
55236285Sbrian  }
55336285Sbrian}
55436285Sbrian
55536285Sbrianvoid
55636285Sbrianprompt_Suspend(struct prompt *p)
55736285Sbrian{
55836285Sbrian  if (getpgrp() == prompt_pgrp(p)) {
55936285Sbrian    prompt_TtyOldMode(p);
56036314Sbrian    log_DeactivatePrompt(p);
56136285Sbrian  }
56236285Sbrian}
563