prompt.c revision 36314
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.2 1998/05/21 21:47:57 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  static char shostname[MAXHOSTNAMELEN];
76  const char *pconnect, *pauth;
77
78  if (p->TermMode || !p->needprompt)
79    return;
80
81  p->needprompt = 0;
82
83  if (p->nonewline)
84    p->nonewline = 0;
85  else
86    fprintf(p->Term, "\n");
87
88  if (p->auth == LOCAL_AUTH)
89    pauth = " ON ";
90  else
91    pauth = " on ";
92
93  if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
94    pconnect = "PPP";
95  else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
96    pconnect = "PPp";
97  else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
98    pconnect = "Ppp";
99  else
100    pconnect = "ppp";
101
102  if (*shostname == '\0') {
103    char *dot;
104
105    if (gethostname(shostname, sizeof shostname))
106      strcpy(shostname, "localhost");
107    else if ((dot = strchr(shostname, '.')))
108      *dot = '\0';
109  }
110
111  fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
112  fflush(p->Term);
113}
114
115static int
116prompt_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
117{
118  struct prompt *p = descriptor2prompt(d);
119  int sets;
120
121  sets = 0;
122
123  if (!p->active)
124    return sets;
125
126  if (p->fd_in >= 0) {
127    if (r) {
128      FD_SET(p->fd_in, r);
129      log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
130      sets++;
131    }
132    if (e) {
133      FD_SET(p->fd_in, e);
134      log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
135      sets++;
136    }
137    if (sets && *n < p->fd_in + 1)
138      *n = p->fd_in + 1;
139  }
140
141  prompt_Display(p);
142
143  return sets;
144}
145
146static int
147prompt_IsSet(struct descriptor *d, const fd_set *fdset)
148{
149  struct prompt *p = descriptor2prompt(d);
150  return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
151}
152
153
154static void
155prompt_ShowHelp(struct prompt *p)
156{
157  prompt_Printf(p, "The following commands are available:\r\n");
158  prompt_Printf(p, " ~p\tEnter Packet mode\r\n");
159  prompt_Printf(p, " ~-\tDecrease log level\r\n");
160  prompt_Printf(p, " ~+\tIncrease log level\r\n");
161  prompt_Printf(p, " ~t\tShow timers\r\n");
162  prompt_Printf(p, " ~m\tShow memory map\r\n");
163  prompt_Printf(p, " ~.\tTerminate program\r\n");
164  prompt_Printf(p, " ~?\tThis help\r\n");
165}
166
167static void
168prompt_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
169{
170  struct prompt *p = descriptor2prompt(d);
171  int n;
172  char ch;
173  static int ttystate;
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 (ttystate) {
225    case 0:
226      if (ch == '~')
227	ttystate++;
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      ttystate = 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      p->fd_in = STDIN_FILENO;
289      p->fd_out = STDOUT_FILENO;
290      p->Term = stdout;
291      p->owner = NULL;
292      p->auth = LOCAL_AUTH;
293      p->src.type = "Controller";
294      strncpy(p->src.from, ttyname(p->fd_out), sizeof p->src.from - 1);
295      p->src.from[sizeof p->src.from - 1] = '\0';
296      tcgetattr(p->fd_in, &p->oldtio);	/* Save original tty mode */
297    } else {
298      p->fd_in = p->fd_out = fd;
299      p->Term = fdopen(fd, "a+");
300      p->owner = s;
301      p->auth = *s->passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
302      p->src.type = "unknown";
303      *p->src.from = '\0';
304    }
305    p->TermMode = NULL;
306    p->nonewline = 1;
307    p->needprompt = 1;
308    p->bundle = bundle;
309    log_RegisterPrompt(p);
310  }
311
312  return p;
313}
314
315void
316prompt_Destroy(struct prompt *p, int verbose)
317{
318  if (p->Term != stdout) {
319    fclose(p->Term);
320    close(p->fd_in);
321    if (p->fd_out != p->fd_in)
322      close(p->fd_out);
323    if (verbose)
324      log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
325  } else
326    prompt_TtyOldMode(p);
327
328  log_UnRegisterPrompt(p);
329  free(p);
330}
331
332void
333prompt_Printf(struct prompt *p, const char *fmt,...)
334{
335  if (p && p->active) {
336    va_list ap;
337    va_start(ap, fmt);
338    vfprintf(p->Term, fmt, ap);
339    fflush(p->Term);
340    va_end(ap);
341    p->nonewline = 1;
342  }
343}
344
345void
346prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
347{
348  if (p && p->active) {
349    vfprintf(p->Term, fmt, ap);
350    fflush(p->Term);
351    p->nonewline = 1;
352  }
353}
354
355void
356prompt_TtyInit(struct prompt *p)
357{
358  int stat, fd = p ? p->fd_in : STDIN_FILENO;
359  struct termios newtio;
360
361  stat = fcntl(fd, F_GETFL, 0);
362  if (stat > 0) {
363    stat |= O_NONBLOCK;
364    fcntl(fd, F_SETFL, stat);
365  }
366
367  if (p)
368    newtio = p->oldtio;
369  else
370    tcgetattr(fd, &newtio);
371
372  newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
373  newtio.c_iflag = 0;
374  newtio.c_oflag &= ~OPOST;
375  newtio.c_cc[VEOF] = _POSIX_VDISABLE;
376  if (!p)
377    newtio.c_cc[VINTR] = _POSIX_VDISABLE;
378  newtio.c_cc[VMIN] = 1;
379  newtio.c_cc[VTIME] = 0;
380  newtio.c_cflag |= CS8;
381  tcsetattr(fd, TCSANOW, &newtio);
382  if (p)
383    p->comtio = newtio;
384}
385
386/*
387 *  Set tty into command mode. We allow canonical input and echo processing.
388 */
389void
390prompt_TtyCommandMode(struct prompt *p)
391{
392  struct termios newtio;
393  int stat;
394
395  tcgetattr(p->fd_in, &newtio);
396  newtio.c_lflag |= (ECHO | ISIG | ICANON);
397  newtio.c_iflag = p->oldtio.c_iflag;
398  newtio.c_oflag |= OPOST;
399  tcsetattr(p->fd_in, TCSADRAIN, &newtio);
400
401  stat = fcntl(p->fd_in, F_GETFL, 0);
402  if (stat > 0) {
403    stat |= O_NONBLOCK;
404    fcntl(p->fd_in, F_SETFL, stat);
405  }
406
407  p->TermMode = NULL;
408}
409
410/*
411 * Set tty into terminal mode which is used while we invoke term command.
412 */
413void
414prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
415{
416  int stat;
417
418  prompt_Printf(p, "Entering terminal mode on %s.\n", dl->name);
419  prompt_Printf(p, "Type `~?' for help.\n");
420
421  if (p->Term == stdout)
422    tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
423
424  stat = fcntl(p->fd_in, F_GETFL, 0);
425  if (stat > 0) {
426    stat &= ~O_NONBLOCK;
427    fcntl(p->fd_in, F_SETFL, stat);
428  }
429  p->TermMode = dl;
430}
431
432void
433prompt_TtyOldMode(struct prompt *p)
434{
435  int stat;
436
437  stat = fcntl(p->fd_in, F_GETFL, 0);
438  if (stat > 0) {
439    stat &= ~O_NONBLOCK;
440    fcntl(p->fd_in, F_SETFL, stat);
441  }
442
443  if (p->Term == stdout)
444    tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
445}
446
447pid_t
448prompt_pgrp(struct prompt *p)
449{
450  return tcgetpgrp(p->fd_in);
451}
452
453int
454PasswdCommand(struct cmdargs const *arg)
455{
456  const char *pass;
457
458  if (!arg->prompt) {
459    log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
460    return 0;
461  }
462
463  if (arg->prompt->owner == NULL) {
464    log_Printf(LogWARN, "passwd: Not required\n");
465    return 0;
466  }
467
468  if (arg->argc == arg->argn)
469    pass = "";
470  else if (arg->argc > arg->argn+1)
471    return -1;
472  else
473    pass = arg->argv[arg->argn];
474
475  if (!strcmp(arg->prompt->owner->passwd, pass))
476    arg->prompt->auth = LOCAL_AUTH;
477  else
478    arg->prompt->auth = LOCAL_NO_AUTH;
479
480  return 0;
481}
482
483static struct pppTimer bgtimer;
484
485static void
486prompt_TimedContinue(void *v)
487{
488  prompt_Continue((struct prompt *)v);
489}
490
491void
492prompt_Continue(struct prompt *p)
493{
494  timer_Stop(&bgtimer);
495  if (getpgrp() == prompt_pgrp(p)) {
496    prompt_TtyCommandMode(p);
497    p->nonewline = 1;
498    prompt_Required(p);
499    log_ActivatePrompt(p);
500  } else if (!p->owner) {
501    bgtimer.func = prompt_TimedContinue;
502    bgtimer.name = "prompt bg";
503    bgtimer.load = SECTICKS;
504    bgtimer.arg = p;
505    timer_Start(&bgtimer);
506  }
507}
508
509void
510prompt_Suspend(struct prompt *p)
511{
512  if (getpgrp() == prompt_pgrp(p)) {
513    prompt_TtyOldMode(p);
514    log_DeactivatePrompt(p);
515  }
516}
517