2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
40 #include <sys/fcntl.h>
49 #include "descriptor.h"
54 #include "throughput.h"
55 #include "slcompress.h"
79 prompt_Display(struct prompt *p)
81 /* XXX: See Index2Nam() - should we only figure this out once ? */
82 static char shostname[MAXHOSTNAMELEN];
83 const char *pconnect, *pauth;
85 if (p->TermMode || !p->needprompt)
93 fprintf(p->Term, "\n");
95 if (p->auth == LOCAL_AUTH)
100 if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
102 else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
104 else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
109 if (*shostname == '\0') {
112 if (gethostname(shostname, sizeof shostname) || *shostname == '\0')
113 strcpy(shostname, "localhost");
114 else if ((dot = strchr(shostname, '.')))
118 fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
123 prompt_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
125 struct prompt *p = descriptor2prompt(d);
136 log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
141 log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
144 if (sets && *n < p->fd_in + 1)
154 prompt_IsSet(struct fdescriptor *d, const fd_set *fdset)
156 struct prompt *p = descriptor2prompt(d);
157 return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
162 prompt_ShowHelp(struct prompt *p)
164 prompt_Printf(p, "The following commands are available:\n");
165 prompt_Printf(p, " ~p\tEnter Packet mode\n");
166 prompt_Printf(p, " ~t\tShow timers\n");
167 prompt_Printf(p, " ~m\tShow memory map\n");
168 prompt_Printf(p, " ~.\tTerminate program\n");
169 prompt_Printf(p, " ~?\tThis help\n");
173 prompt_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
175 struct prompt *p = descriptor2prompt(d);
179 char linebuff[LINE_LEN];
181 if (p->TermMode == NULL) {
182 n = read(p->fd_in, linebuff, sizeof linebuff - 1);
184 if (linebuff[n-1] == '\n')
185 linebuff[--n] = '\0';
188 p->nonewline = 1; /* Maybe command_Decode does a prompt */
191 if ((op = log_PromptContext) == NULL)
192 log_PromptContext = p;
193 if (!command_Decode(bundle, linebuff, n, p, p->src.from))
194 prompt_Printf(p, "Syntax error\n");
195 log_PromptContext = op;
198 log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
201 prompt_Destroy(p, 0);
206 switch (p->TermMode->state) {
207 case DATALINK_CLOSED:
208 prompt_Printf(p, "Link lost, terminal mode.\n");
209 prompt_TtyCommandMode(p);
218 prompt_Printf(p, "\nPacket mode detected.\n");
219 prompt_TtyCommandMode(p);
221 /* We'll get a prompt because of our status change */
225 /* Wait 'till we're in a state we care about */
230 * We are in terminal mode, decode special sequences
232 n = read(p->fd_in, &ch, 1);
233 log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
236 switch (p->readtilde) {
241 if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
242 log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
243 prompt_TtyCommandMode(p);
252 datalink_Up(p->TermMode, 0, 1);
253 prompt_Printf(p, "\nPacket mode.\n");
254 prompt_TtyCommandMode(p);
257 prompt_TtyCommandMode(p);
274 arg.cx = p->TermMode;
281 if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
282 log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
283 prompt_TtyCommandMode(p);
294 prompt_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
296 /* We never want to write here ! */
297 log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
302 prompt_Create(struct server *s, struct bundle *bundle, int fd)
304 struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
307 p->desc.type = PROMPT_DESCRIPTOR;
308 p->desc.UpdateSet = prompt_UpdateSet;
309 p->desc.IsSet = prompt_IsSet;
310 p->desc.Read = prompt_Read;
311 p->desc.Write = prompt_Write;
313 if (fd == PROMPT_STD) {
314 char *tty = ttyname(STDIN_FILENO);
320 p->fd_in = STDIN_FILENO;
321 p->fd_out = STDOUT_FILENO;
324 p->auth = LOCAL_AUTH;
325 p->src.type = "Controller";
326 strncpy(p->src.from, tty, sizeof p->src.from - 1);
327 p->src.from[sizeof p->src.from - 1] = '\0';
328 tcgetattr(p->fd_in, &p->oldtio); /* Save original tty mode */
330 p->fd_in = p->fd_out = fd;
331 p->Term = fdopen(fd, "a+");
333 p->auth = *s->cfg.passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
334 p->src.type = "unknown";
342 log_RegisterPrompt(p);
349 prompt_Destroy(struct prompt *p, int verbose)
352 if (p->Term != stdout) {
355 if (p->fd_out != p->fd_in)
358 log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
360 prompt_TtyOldMode(p);
362 log_UnRegisterPrompt(p);
368 prompt_Printf(struct prompt *p, const char *fmt,...)
370 if (p && p->active) {
374 prompt_vPrintf(p, fmt, ap);
380 prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
382 if (p && p->active) {
387 /* Stuff '\r' in front of '\n' 'cos we're in raw mode */
388 int len = strlen(fmt);
390 if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
391 (len == 1 || fmt[len-2] != '\r')) {
393 strcpy(nfmt + len - 1, "\r\n");
399 vfprintf(p->Term, pfmt, ap);
406 prompt_TtyInit(struct prompt *p)
408 int stat, fd = p ? p->fd_in : STDIN_FILENO;
409 struct termios newtio;
411 stat = fcntl(fd, F_GETFL, 0);
414 fcntl(fd, F_SETFL, stat);
420 tcgetattr(fd, &newtio);
422 newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
424 newtio.c_oflag &= ~OPOST;
426 newtio.c_cc[VINTR] = _POSIX_VDISABLE;
427 newtio.c_cc[VMIN] = 1;
428 newtio.c_cc[VTIME] = 0;
429 newtio.c_cflag |= CS8;
430 tcsetattr(fd, TCSANOW, &newtio);
436 * Set tty into command mode. We allow canonical input and echo processing.
439 prompt_TtyCommandMode(struct prompt *p)
441 struct termios newtio;
444 tcgetattr(p->fd_in, &newtio);
445 newtio.c_lflag |= (ECHO | ISIG | ICANON);
446 newtio.c_iflag = p->oldtio.c_iflag;
447 newtio.c_oflag |= OPOST;
448 tcsetattr(p->fd_in, TCSADRAIN, &newtio);
450 stat = fcntl(p->fd_in, F_GETFL, 0);
453 fcntl(p->fd_in, F_SETFL, stat);
460 * Set tty into terminal mode which is used while we invoke term command.
463 prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
467 if (p->Term == stdout)
468 tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
470 stat = fcntl(p->fd_in, F_GETFL, 0);
473 fcntl(p->fd_in, F_SETFL, stat);
479 prompt_TtyOldMode(struct prompt *p)
483 stat = fcntl(p->fd_in, F_GETFL, 0);
486 fcntl(p->fd_in, F_SETFL, stat);
489 if (p->Term == stdout)
490 tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
494 prompt_pgrp(struct prompt *p)
496 return tcgetpgrp(p->fd_in);
500 PasswdCommand(struct cmdargs const *arg)
505 log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
509 if (arg->prompt->owner == NULL) {
510 log_Printf(LogWARN, "passwd: Not required\n");
514 if (arg->argc == arg->argn)
516 else if (arg->argc > arg->argn+1)
519 pass = arg->argv[arg->argn];
521 if (!strcmp(arg->prompt->owner->cfg.passwd, pass))
522 arg->prompt->auth = LOCAL_AUTH;
524 arg->prompt->auth = LOCAL_NO_AUTH;
529 static struct pppTimer bgtimer;
532 prompt_TimedContinue(void *v)
534 prompt_Continue((struct prompt *)v);
538 prompt_Continue(struct prompt *p)
540 timer_Stop(&bgtimer);
541 if (getpgrp() == prompt_pgrp(p)) {
542 prompt_TtyCommandMode(p);
545 log_ActivatePrompt(p);
546 } else if (!p->owner) {
547 bgtimer.func = prompt_TimedContinue;
548 bgtimer.name = "prompt bg";
549 bgtimer.load = SECTICKS;
551 timer_Start(&bgtimer);
556 prompt_Suspend(struct prompt *p)
558 if (getpgrp() == prompt_pgrp(p)) {
559 prompt_TtyOldMode(p);
560 log_DeactivatePrompt(p);