prompt.c revision 51005
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 51005 1999-09-06 08:16:33Z 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 "lcp.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"
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 ? */
8236285Sbrian  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
11236285Sbrian    if (gethostname(shostname, sizeof shostname))
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
12336285Sbrianprompt_UpdateSet(struct descriptor *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
15436285Sbrianprompt_IsSet(struct descriptor *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
17336285Sbrianprompt_Read(struct descriptor *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;
19336285Sbrian        command_Decode(bundle, linebuff, n, p, p->src.from);
19451005Sbrian        log_PromptContext = op;
19551005Sbrian      }
19636285Sbrian    } else if (n <= 0) {
19736314Sbrian      log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
19837386Sbrian      if (!p->owner)
19937386Sbrian        Cleanup(EX_NORMAL);
20036285Sbrian      prompt_Destroy(p, 0);
20136285Sbrian    }
20236285Sbrian    return;
20336285Sbrian  }
20436285Sbrian
20536285Sbrian  switch (p->TermMode->state) {
20636285Sbrian    case DATALINK_CLOSED:
20736285Sbrian      prompt_Printf(p, "Link lost, terminal mode.\n");
20836285Sbrian      prompt_TtyCommandMode(p);
20936285Sbrian      p->nonewline = 0;
21036285Sbrian      prompt_Required(p);
21136285Sbrian      return;
21236285Sbrian
21336285Sbrian    case DATALINK_READY:
21436285Sbrian      break;
21536285Sbrian
21636285Sbrian    case DATALINK_OPEN:
21736285Sbrian      prompt_Printf(p, "\nPacket mode detected.\n");
21836285Sbrian      prompt_TtyCommandMode(p);
21936285Sbrian      p->nonewline = 0;
22036285Sbrian      /* We'll get a prompt because of our status change */
22136285Sbrian      /* Fall through */
22236285Sbrian
22336285Sbrian    default:
22436285Sbrian      /* Wait 'till we're in a state we care about */
22536285Sbrian      return;
22636285Sbrian  }
22736285Sbrian
22836285Sbrian  /*
22936285Sbrian   * We are in terminal mode, decode special sequences
23036285Sbrian   */
23136285Sbrian  n = read(p->fd_in, &ch, 1);
23236285Sbrian  log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
23336285Sbrian
23436285Sbrian  if (n > 0) {
23537010Sbrian    switch (p->readtilde) {
23636285Sbrian    case 0:
23736285Sbrian      if (ch == '~')
23837010Sbrian        p->readtilde = 1;
23936285Sbrian      else
24036285Sbrian	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
24137019Sbrian	  log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
24236285Sbrian          prompt_TtyCommandMode(p);
24336285Sbrian        }
24436285Sbrian      break;
24536285Sbrian    case 1:
24636285Sbrian      switch (ch) {
24736285Sbrian      case '?':
24836285Sbrian	prompt_ShowHelp(p);
24936285Sbrian	break;
25036285Sbrian      case 'p':
25136285Sbrian        datalink_Up(p->TermMode, 0, 1);
25236285Sbrian        prompt_Printf(p, "\nPacket mode.\n");
25336285Sbrian	prompt_TtyCommandMode(p);
25436285Sbrian        break;
25536285Sbrian      case '.':
25636285Sbrian	prompt_TtyCommandMode(p);
25736285Sbrian        p->nonewline = 0;
25836285Sbrian        prompt_Required(p);
25936285Sbrian	break;
26036285Sbrian      case 't':
26136285Sbrian	timer_Show(0, p);
26236285Sbrian	break;
26336285Sbrian      case 'm':
26437011Sbrian        {
26537011Sbrian          struct cmdargs arg;
26637011Sbrian
26737011Sbrian          arg.cmdtab = NULL;
26837011Sbrian          arg.cmd = NULL;
26937011Sbrian          arg.argc = 0;
27037011Sbrian          arg.argn = 0;
27137011Sbrian          arg.argv = NULL;
27237011Sbrian          arg.bundle = bundle;
27337011Sbrian          arg.cx = p->TermMode;
27437011Sbrian          arg.prompt = p;
27537011Sbrian
27637011Sbrian	  mbuf_Show(&arg);
27737011Sbrian        }
27836285Sbrian	break;
27936285Sbrian      default:
28036285Sbrian	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
28137019Sbrian	  log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
28236285Sbrian          prompt_TtyCommandMode(p);
28336285Sbrian        }
28436285Sbrian	break;
28536285Sbrian      }
28637010Sbrian      p->readtilde = 0;
28736285Sbrian      break;
28836285Sbrian    }
28936285Sbrian  }
29036285Sbrian}
29136285Sbrian
29237141Sbrianstatic int
29336285Sbrianprompt_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
29436285Sbrian{
29536285Sbrian  /* We never want to write here ! */
29637019Sbrian  log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
29737141Sbrian  return 0;
29836285Sbrian}
29936285Sbrian
30036285Sbrianstruct prompt *
30136285Sbrianprompt_Create(struct server *s, struct bundle *bundle, int fd)
30236285Sbrian{
30336285Sbrian  struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
30436285Sbrian
30536285Sbrian  if (p != NULL) {
30636285Sbrian    p->desc.type = PROMPT_DESCRIPTOR;
30736285Sbrian    p->desc.UpdateSet = prompt_UpdateSet;
30836285Sbrian    p->desc.IsSet = prompt_IsSet;
30936285Sbrian    p->desc.Read = prompt_Read;
31036285Sbrian    p->desc.Write = prompt_Write;
31136285Sbrian
31236285Sbrian    if (fd == PROMPT_STD) {
31336431Sbrian      char *tty = ttyname(STDIN_FILENO);
31436431Sbrian
31536431Sbrian      if (!tty) {
31636431Sbrian        free(p);
31736431Sbrian        return NULL;
31836431Sbrian      }
31936285Sbrian      p->fd_in = STDIN_FILENO;
32036285Sbrian      p->fd_out = STDOUT_FILENO;
32136285Sbrian      p->Term = stdout;
32236285Sbrian      p->owner = NULL;
32336285Sbrian      p->auth = LOCAL_AUTH;
32436285Sbrian      p->src.type = "Controller";
32536431Sbrian      strncpy(p->src.from, tty, sizeof p->src.from - 1);
32636285Sbrian      p->src.from[sizeof p->src.from - 1] = '\0';
32736285Sbrian      tcgetattr(p->fd_in, &p->oldtio);	/* Save original tty mode */
32836285Sbrian    } else {
32936285Sbrian      p->fd_in = p->fd_out = fd;
33036285Sbrian      p->Term = fdopen(fd, "a+");
33136285Sbrian      p->owner = s;
33236285Sbrian      p->auth = *s->passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
33336285Sbrian      p->src.type = "unknown";
33436285Sbrian      *p->src.from = '\0';
33536285Sbrian    }
33636285Sbrian    p->TermMode = NULL;
33736285Sbrian    p->nonewline = 1;
33836285Sbrian    p->needprompt = 1;
33937010Sbrian    p->readtilde = 0;
34036285Sbrian    p->bundle = bundle;
34136285Sbrian    log_RegisterPrompt(p);
34236285Sbrian  }
34336285Sbrian
34436285Sbrian  return p;
34536285Sbrian}
34636285Sbrian
34736285Sbrianvoid
34836285Sbrianprompt_Destroy(struct prompt *p, int verbose)
34936285Sbrian{
35036431Sbrian  if (p) {
35136431Sbrian    if (p->Term != stdout) {
35236431Sbrian      fclose(p->Term);
35336431Sbrian      close(p->fd_in);
35436431Sbrian      if (p->fd_out != p->fd_in)
35536431Sbrian        close(p->fd_out);
35636431Sbrian      if (verbose)
35736431Sbrian        log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
35836431Sbrian    } else
35936431Sbrian      prompt_TtyOldMode(p);
36036285Sbrian
36136431Sbrian    log_UnRegisterPrompt(p);
36236431Sbrian    free(p);
36336431Sbrian  }
36436285Sbrian}
36536285Sbrian
36636285Sbrianvoid
36736285Sbrianprompt_Printf(struct prompt *p, const char *fmt,...)
36836285Sbrian{
36936285Sbrian  if (p && p->active) {
37036285Sbrian    va_list ap;
37137011Sbrian
37236285Sbrian    va_start(ap, fmt);
37337011Sbrian    prompt_vPrintf(p, fmt, ap);
37436285Sbrian    va_end(ap);
37536285Sbrian  }
37636285Sbrian}
37736285Sbrian
37836285Sbrianvoid
37936285Sbrianprompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
38036285Sbrian{
38136285Sbrian  if (p && p->active) {
38237011Sbrian    char nfmt[LINE_LEN];
38337011Sbrian    const char *pfmt;
38437011Sbrian
38537011Sbrian    if (p->TermMode) {
38637011Sbrian      /* Stuff '\r' in front of '\n' 'cos we're in raw mode */
38737011Sbrian      int len = strlen(fmt);
38837011Sbrian
38945264Sbrian      if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
39045264Sbrian          (len == 1 || fmt[len-2] != '\r')) {
39137011Sbrian        strcpy(nfmt, fmt);
39237011Sbrian        strcpy(nfmt + len - 1, "\r\n");
39337011Sbrian        pfmt = nfmt;
39437011Sbrian      } else
39537011Sbrian        pfmt = fmt;
39637011Sbrian    } else
39737011Sbrian      pfmt = fmt;
39837011Sbrian    vfprintf(p->Term, pfmt, ap);
39936285Sbrian    fflush(p->Term);
40036285Sbrian    p->nonewline = 1;
40136285Sbrian  }
40236285Sbrian}
40336285Sbrian
40436285Sbrianvoid
40536285Sbrianprompt_TtyInit(struct prompt *p)
40636285Sbrian{
40736285Sbrian  int stat, fd = p ? p->fd_in : STDIN_FILENO;
40836285Sbrian  struct termios newtio;
40936285Sbrian
41036285Sbrian  stat = fcntl(fd, F_GETFL, 0);
41136285Sbrian  if (stat > 0) {
41236285Sbrian    stat |= O_NONBLOCK;
41336285Sbrian    fcntl(fd, F_SETFL, stat);
41436285Sbrian  }
41536285Sbrian
41636285Sbrian  if (p)
41736285Sbrian    newtio = p->oldtio;
41836285Sbrian  else
41936285Sbrian    tcgetattr(fd, &newtio);
42036285Sbrian
42136285Sbrian  newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
42236285Sbrian  newtio.c_iflag = 0;
42336285Sbrian  newtio.c_oflag &= ~OPOST;
42436285Sbrian  if (!p)
42536285Sbrian    newtio.c_cc[VINTR] = _POSIX_VDISABLE;
42636285Sbrian  newtio.c_cc[VMIN] = 1;
42736285Sbrian  newtio.c_cc[VTIME] = 0;
42836285Sbrian  newtio.c_cflag |= CS8;
42936285Sbrian  tcsetattr(fd, TCSANOW, &newtio);
43036285Sbrian  if (p)
43136285Sbrian    p->comtio = newtio;
43236285Sbrian}
43336285Sbrian
43436285Sbrian/*
43536285Sbrian *  Set tty into command mode. We allow canonical input and echo processing.
43636285Sbrian */
43736285Sbrianvoid
43836285Sbrianprompt_TtyCommandMode(struct prompt *p)
43936285Sbrian{
44036285Sbrian  struct termios newtio;
44136285Sbrian  int stat;
44236285Sbrian
44336285Sbrian  tcgetattr(p->fd_in, &newtio);
44436285Sbrian  newtio.c_lflag |= (ECHO | ISIG | ICANON);
44536285Sbrian  newtio.c_iflag = p->oldtio.c_iflag;
44636285Sbrian  newtio.c_oflag |= OPOST;
44736285Sbrian  tcsetattr(p->fd_in, TCSADRAIN, &newtio);
44836285Sbrian
44936285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
45036285Sbrian  if (stat > 0) {
45136285Sbrian    stat |= O_NONBLOCK;
45236285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
45336285Sbrian  }
45436285Sbrian
45536285Sbrian  p->TermMode = NULL;
45636285Sbrian}
45736285Sbrian
45836285Sbrian/*
45936285Sbrian * Set tty into terminal mode which is used while we invoke term command.
46036285Sbrian */
46136285Sbrianvoid
46236285Sbrianprompt_TtyTermMode(struct prompt *p, struct datalink *dl)
46336285Sbrian{
46436285Sbrian  int stat;
46536285Sbrian
46636285Sbrian  if (p->Term == stdout)
46736285Sbrian    tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
46836285Sbrian
46936285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
47036285Sbrian  if (stat > 0) {
47136285Sbrian    stat &= ~O_NONBLOCK;
47236285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
47336285Sbrian  }
47436285Sbrian  p->TermMode = dl;
47536285Sbrian}
47636285Sbrian
47736285Sbrianvoid
47836285Sbrianprompt_TtyOldMode(struct prompt *p)
47936285Sbrian{
48036285Sbrian  int stat;
48136285Sbrian
48236285Sbrian  stat = fcntl(p->fd_in, F_GETFL, 0);
48336285Sbrian  if (stat > 0) {
48436285Sbrian    stat &= ~O_NONBLOCK;
48536285Sbrian    fcntl(p->fd_in, F_SETFL, stat);
48636285Sbrian  }
48736285Sbrian
48836285Sbrian  if (p->Term == stdout)
48936285Sbrian    tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
49036285Sbrian}
49136285Sbrian
49236285Sbrianpid_t
49336285Sbrianprompt_pgrp(struct prompt *p)
49436285Sbrian{
49536285Sbrian  return tcgetpgrp(p->fd_in);
49636285Sbrian}
49736285Sbrian
49836285Sbrianint
49936285SbrianPasswdCommand(struct cmdargs const *arg)
50036285Sbrian{
50136285Sbrian  const char *pass;
50236285Sbrian
50336285Sbrian  if (!arg->prompt) {
50436285Sbrian    log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
50536285Sbrian    return 0;
50636285Sbrian  }
50736285Sbrian
50836285Sbrian  if (arg->prompt->owner == NULL) {
50936285Sbrian    log_Printf(LogWARN, "passwd: Not required\n");
51036285Sbrian    return 0;
51136285Sbrian  }
51236285Sbrian
51336285Sbrian  if (arg->argc == arg->argn)
51436285Sbrian    pass = "";
51536285Sbrian  else if (arg->argc > arg->argn+1)
51636285Sbrian    return -1;
51736285Sbrian  else
51836285Sbrian    pass = arg->argv[arg->argn];
51936285Sbrian
52036285Sbrian  if (!strcmp(arg->prompt->owner->passwd, pass))
52136285Sbrian    arg->prompt->auth = LOCAL_AUTH;
52236285Sbrian  else
52336285Sbrian    arg->prompt->auth = LOCAL_NO_AUTH;
52436285Sbrian
52536285Sbrian  return 0;
52636285Sbrian}
52736285Sbrian
52836285Sbrianstatic struct pppTimer bgtimer;
52936285Sbrian
53036285Sbrianstatic void
53136285Sbrianprompt_TimedContinue(void *v)
53236285Sbrian{
53336285Sbrian  prompt_Continue((struct prompt *)v);
53436285Sbrian}
53536285Sbrian
53636285Sbrianvoid
53736285Sbrianprompt_Continue(struct prompt *p)
53836285Sbrian{
53936285Sbrian  timer_Stop(&bgtimer);
54036285Sbrian  if (getpgrp() == prompt_pgrp(p)) {
54136285Sbrian    prompt_TtyCommandMode(p);
54236285Sbrian    p->nonewline = 1;
54336285Sbrian    prompt_Required(p);
54436314Sbrian    log_ActivatePrompt(p);
54536285Sbrian  } else if (!p->owner) {
54636285Sbrian    bgtimer.func = prompt_TimedContinue;
54736285Sbrian    bgtimer.name = "prompt bg";
54836285Sbrian    bgtimer.load = SECTICKS;
54936285Sbrian    bgtimer.arg = p;
55036285Sbrian    timer_Start(&bgtimer);
55136285Sbrian  }
55236285Sbrian}
55336285Sbrian
55436285Sbrianvoid
55536285Sbrianprompt_Suspend(struct prompt *p)
55636285Sbrian{
55736285Sbrian  if (getpgrp() == prompt_pgrp(p)) {
55836285Sbrian    prompt_TtyOldMode(p);
55936314Sbrian    log_DeactivatePrompt(p);
56036285Sbrian  }
56136285Sbrian}
562