prompt.c revision 37010
1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$Id: prompt.c,v 1.4 1998/05/27 22:43:37 brian Exp $
27 */
28
29#include <sys/param.h>
30#include <netinet/in.h>
31#include <netinet/in_systm.h>
32#include <netinet/ip.h>
33#include <sys/un.h>
34
35#include <errno.h>
36#include <stdarg.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/fcntl.h>
41#include <termios.h>
42#include <unistd.h>
43
44#include "defs.h"
45#include "timer.h"
46#include "command.h"
47#include "log.h"
48#include "descriptor.h"
49#include "prompt.h"
50#include "fsm.h"
51#include "lcp.h"
52#include "auth.h"
53#include "iplist.h"
54#include "throughput.h"
55#include "slcompress.h"
56#include "ipcp.h"
57#include "filter.h"
58#include "lqr.h"
59#include "hdlc.h"
60#include "async.h"
61#include "mbuf.h"
62#include "ccp.h"
63#include "link.h"
64#include "physical.h"
65#include "mp.h"
66#include "bundle.h"
67#include "chat.h"
68#include "chap.h"
69#include "datalink.h"
70#include "server.h"
71
72static void
73prompt_Display(struct prompt *p)
74{
75  /* XXX: See Index2Nam() - should we only figure this out once ? */
76  static char shostname[MAXHOSTNAMELEN];
77  const char *pconnect, *pauth;
78
79  if (p->TermMode || !p->needprompt)
80    return;
81
82  p->needprompt = 0;
83
84  if (p->nonewline)
85    p->nonewline = 0;
86  else
87    fprintf(p->Term, "\n");
88
89  if (p->auth == LOCAL_AUTH)
90    pauth = " ON ";
91  else
92    pauth = " on ";
93
94  if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
95    pconnect = "PPP";
96  else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
97    pconnect = "PPp";
98  else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
99    pconnect = "Ppp";
100  else
101    pconnect = "ppp";
102
103  if (*shostname == '\0') {
104    char *dot;
105
106    if (gethostname(shostname, sizeof shostname))
107      strcpy(shostname, "localhost");
108    else if ((dot = strchr(shostname, '.')))
109      *dot = '\0';
110  }
111
112  fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
113  fflush(p->Term);
114}
115
116static int
117prompt_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
118{
119  struct prompt *p = descriptor2prompt(d);
120  int sets;
121
122  sets = 0;
123
124  if (!p->active)
125    return sets;
126
127  if (p->fd_in >= 0) {
128    if (r) {
129      FD_SET(p->fd_in, r);
130      log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
131      sets++;
132    }
133    if (e) {
134      FD_SET(p->fd_in, e);
135      log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
136      sets++;
137    }
138    if (sets && *n < p->fd_in + 1)
139      *n = p->fd_in + 1;
140  }
141
142  prompt_Display(p);
143
144  return sets;
145}
146
147static int
148prompt_IsSet(struct descriptor *d, const fd_set *fdset)
149{
150  struct prompt *p = descriptor2prompt(d);
151  return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
152}
153
154
155static void
156prompt_ShowHelp(struct prompt *p)
157{
158  prompt_Printf(p, "The following commands are available:\r\n");
159  prompt_Printf(p, " ~p\tEnter Packet mode\r\n");
160  prompt_Printf(p, " ~-\tDecrease log level\r\n");
161  prompt_Printf(p, " ~+\tIncrease log level\r\n");
162  prompt_Printf(p, " ~t\tShow timers\r\n");
163  prompt_Printf(p, " ~m\tShow memory map\r\n");
164  prompt_Printf(p, " ~.\tTerminate program\r\n");
165  prompt_Printf(p, " ~?\tThis help\r\n");
166}
167
168static void
169prompt_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
170{
171  struct prompt *p = descriptor2prompt(d);
172  int n;
173  char ch;
174  char linebuff[LINE_LEN];
175
176  if (p->TermMode == NULL) {
177    n = read(p->fd_in, linebuff, sizeof linebuff - 1);
178    if (n > 0) {
179      if (linebuff[n-1] == '\n')
180        linebuff[--n] = '\0';
181      else
182        linebuff[n] = '\0';
183      p->nonewline = 1;		/* Maybe command_Decode does a prompt */
184      prompt_Required(p);
185      if (n)
186        command_Decode(bundle, linebuff, n, p, p->src.from);
187    } else if (n <= 0) {
188      log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
189      prompt_Destroy(p, 0);
190    }
191    return;
192  }
193
194  switch (p->TermMode->state) {
195    case DATALINK_CLOSED:
196      prompt_Printf(p, "Link lost, terminal mode.\n");
197      prompt_TtyCommandMode(p);
198      p->nonewline = 0;
199      prompt_Required(p);
200      return;
201
202    case DATALINK_READY:
203      break;
204
205    case DATALINK_OPEN:
206      prompt_Printf(p, "\nPacket mode detected.\n");
207      prompt_TtyCommandMode(p);
208      p->nonewline = 0;
209      /* We'll get a prompt because of our status change */
210      /* Fall through */
211
212    default:
213      /* Wait 'till we're in a state we care about */
214      return;
215  }
216
217  /*
218   * We are in terminal mode, decode special sequences
219   */
220  n = read(p->fd_in, &ch, 1);
221  log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
222
223  if (n > 0) {
224    switch (p->readtilde) {
225    case 0:
226      if (ch == '~')
227        p->readtilde = 1;
228      else
229	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
230	  log_Printf(LogERROR, "error writing to modem: %s\n", strerror(errno));
231          prompt_TtyCommandMode(p);
232        }
233      break;
234    case 1:
235      switch (ch) {
236      case '?':
237	prompt_ShowHelp(p);
238	break;
239      case 'p':
240        datalink_Up(p->TermMode, 0, 1);
241        prompt_Printf(p, "\nPacket mode.\n");
242	prompt_TtyCommandMode(p);
243        break;
244      case '.':
245	prompt_TtyCommandMode(p);
246        p->nonewline = 0;
247        prompt_Required(p);
248	break;
249      case 't':
250	timer_Show(0, p);
251	break;
252      case 'm':
253	mbuf_Show(NULL);
254	break;
255      default:
256	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
257	  log_Printf(LogERROR, "error writing to modem: %s\n", strerror(errno));
258          prompt_TtyCommandMode(p);
259        }
260	break;
261      }
262      p->readtilde = 0;
263      break;
264    }
265  }
266}
267
268static void
269prompt_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
270{
271  /* We never want to write here ! */
272  log_Printf(LogERROR, "prompt_Write: Internal error: Bad call !\n");
273}
274
275struct prompt *
276prompt_Create(struct server *s, struct bundle *bundle, int fd)
277{
278  struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
279
280  if (p != NULL) {
281    p->desc.type = PROMPT_DESCRIPTOR;
282    p->desc.UpdateSet = prompt_UpdateSet;
283    p->desc.IsSet = prompt_IsSet;
284    p->desc.Read = prompt_Read;
285    p->desc.Write = prompt_Write;
286
287    if (fd == PROMPT_STD) {
288      char *tty = ttyname(STDIN_FILENO);
289
290      if (!tty) {
291        free(p);
292        return NULL;
293      }
294      p->fd_in = STDIN_FILENO;
295      p->fd_out = STDOUT_FILENO;
296      p->Term = stdout;
297      p->owner = NULL;
298      p->auth = LOCAL_AUTH;
299      p->src.type = "Controller";
300      strncpy(p->src.from, tty, sizeof p->src.from - 1);
301      p->src.from[sizeof p->src.from - 1] = '\0';
302      tcgetattr(p->fd_in, &p->oldtio);	/* Save original tty mode */
303    } else {
304      p->fd_in = p->fd_out = fd;
305      p->Term = fdopen(fd, "a+");
306      p->owner = s;
307      p->auth = *s->passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
308      p->src.type = "unknown";
309      *p->src.from = '\0';
310    }
311    p->TermMode = NULL;
312    p->nonewline = 1;
313    p->needprompt = 1;
314    p->readtilde = 0;
315    p->bundle = bundle;
316    log_RegisterPrompt(p);
317  }
318
319  return p;
320}
321
322void
323prompt_Destroy(struct prompt *p, int verbose)
324{
325  if (p) {
326    if (p->Term != stdout) {
327      fclose(p->Term);
328      close(p->fd_in);
329      if (p->fd_out != p->fd_in)
330        close(p->fd_out);
331      if (verbose)
332        log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
333    } else
334      prompt_TtyOldMode(p);
335
336    log_UnRegisterPrompt(p);
337    free(p);
338  }
339}
340
341void
342prompt_Printf(struct prompt *p, const char *fmt,...)
343{
344  if (p && p->active) {
345    va_list ap;
346    va_start(ap, fmt);
347    vfprintf(p->Term, fmt, ap);
348    fflush(p->Term);
349    va_end(ap);
350    p->nonewline = 1;
351  }
352}
353
354void
355prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
356{
357  if (p && p->active) {
358    vfprintf(p->Term, fmt, ap);
359    fflush(p->Term);
360    p->nonewline = 1;
361  }
362}
363
364void
365prompt_TtyInit(struct prompt *p)
366{
367  int stat, fd = p ? p->fd_in : STDIN_FILENO;
368  struct termios newtio;
369
370  stat = fcntl(fd, F_GETFL, 0);
371  if (stat > 0) {
372    stat |= O_NONBLOCK;
373    fcntl(fd, F_SETFL, stat);
374  }
375
376  if (p)
377    newtio = p->oldtio;
378  else
379    tcgetattr(fd, &newtio);
380
381  newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
382  newtio.c_iflag = 0;
383  newtio.c_oflag &= ~OPOST;
384  newtio.c_cc[VEOF] = _POSIX_VDISABLE;
385  if (!p)
386    newtio.c_cc[VINTR] = _POSIX_VDISABLE;
387  newtio.c_cc[VMIN] = 1;
388  newtio.c_cc[VTIME] = 0;
389  newtio.c_cflag |= CS8;
390  tcsetattr(fd, TCSANOW, &newtio);
391  if (p)
392    p->comtio = newtio;
393}
394
395/*
396 *  Set tty into command mode. We allow canonical input and echo processing.
397 */
398void
399prompt_TtyCommandMode(struct prompt *p)
400{
401  struct termios newtio;
402  int stat;
403
404  tcgetattr(p->fd_in, &newtio);
405  newtio.c_lflag |= (ECHO | ISIG | ICANON);
406  newtio.c_iflag = p->oldtio.c_iflag;
407  newtio.c_oflag |= OPOST;
408  tcsetattr(p->fd_in, TCSADRAIN, &newtio);
409
410  stat = fcntl(p->fd_in, F_GETFL, 0);
411  if (stat > 0) {
412    stat |= O_NONBLOCK;
413    fcntl(p->fd_in, F_SETFL, stat);
414  }
415
416  p->TermMode = NULL;
417}
418
419/*
420 * Set tty into terminal mode which is used while we invoke term command.
421 */
422void
423prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
424{
425  int stat;
426
427  prompt_Printf(p, "Entering terminal mode on %s.\n", dl->name);
428  prompt_Printf(p, "Type `~?' for help.\n");
429
430  if (p->Term == stdout)
431    tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
432
433  stat = fcntl(p->fd_in, F_GETFL, 0);
434  if (stat > 0) {
435    stat &= ~O_NONBLOCK;
436    fcntl(p->fd_in, F_SETFL, stat);
437  }
438  p->TermMode = dl;
439}
440
441void
442prompt_TtyOldMode(struct prompt *p)
443{
444  int stat;
445
446  stat = fcntl(p->fd_in, F_GETFL, 0);
447  if (stat > 0) {
448    stat &= ~O_NONBLOCK;
449    fcntl(p->fd_in, F_SETFL, stat);
450  }
451
452  if (p->Term == stdout)
453    tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
454}
455
456pid_t
457prompt_pgrp(struct prompt *p)
458{
459  return tcgetpgrp(p->fd_in);
460}
461
462int
463PasswdCommand(struct cmdargs const *arg)
464{
465  const char *pass;
466
467  if (!arg->prompt) {
468    log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
469    return 0;
470  }
471
472  if (arg->prompt->owner == NULL) {
473    log_Printf(LogWARN, "passwd: Not required\n");
474    return 0;
475  }
476
477  if (arg->argc == arg->argn)
478    pass = "";
479  else if (arg->argc > arg->argn+1)
480    return -1;
481  else
482    pass = arg->argv[arg->argn];
483
484  if (!strcmp(arg->prompt->owner->passwd, pass))
485    arg->prompt->auth = LOCAL_AUTH;
486  else
487    arg->prompt->auth = LOCAL_NO_AUTH;
488
489  return 0;
490}
491
492static struct pppTimer bgtimer;
493
494static void
495prompt_TimedContinue(void *v)
496{
497  prompt_Continue((struct prompt *)v);
498}
499
500void
501prompt_Continue(struct prompt *p)
502{
503  timer_Stop(&bgtimer);
504  if (getpgrp() == prompt_pgrp(p)) {
505    prompt_TtyCommandMode(p);
506    p->nonewline = 1;
507    prompt_Required(p);
508    log_ActivatePrompt(p);
509  } else if (!p->owner) {
510    bgtimer.func = prompt_TimedContinue;
511    bgtimer.name = "prompt bg";
512    bgtimer.load = SECTICKS;
513    bgtimer.arg = p;
514    timer_Start(&bgtimer);
515  }
516}
517
518void
519prompt_Suspend(struct prompt *p)
520{
521  if (getpgrp() == prompt_pgrp(p)) {
522    prompt_TtyOldMode(p);
523    log_DeactivatePrompt(p);
524  }
525}
526