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>
33 #include <sys/socket.h>
41 #include <sys/fcntl.h>
50 #include "descriptor.h"
55 #include "throughput.h"
56 #include "slcompress.h"
84 prompt_Display(struct prompt *p)
86 /* XXX: See Index2Nam() - should we only figure this out once ? */
87 static char shostname[MAXHOSTNAMELEN];
88 const char *pconnect, *pauth;
90 if (p->TermMode || !p->needprompt)
98 fprintf(p->Term, "\n");
100 if (p->auth == LOCAL_AUTH)
105 if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
107 else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
109 else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
114 if (*shostname == '\0') {
117 if (gethostname(shostname, sizeof shostname) || *shostname == '\0')
118 strcpy(shostname, "localhost");
119 else if ((dot = strchr(shostname, '.')))
123 fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
128 prompt_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
130 struct prompt *p = descriptor2prompt(d);
141 log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
146 log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
149 if (sets && *n < p->fd_in + 1)
159 prompt_IsSet(struct fdescriptor *d, const fd_set *fdset)
161 struct prompt *p = descriptor2prompt(d);
162 return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
167 prompt_ShowHelp(struct prompt *p)
169 prompt_Printf(p, "The following commands are available:\n");
170 prompt_Printf(p, " ~p\tEnter Packet mode\n");
171 prompt_Printf(p, " ~t\tShow timers\n");
172 prompt_Printf(p, " ~m\tShow memory map\n");
173 prompt_Printf(p, " ~.\tTerminate program\n");
174 prompt_Printf(p, " ~?\tThis help\n");
178 prompt_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
180 struct prompt *p = descriptor2prompt(d);
184 char linebuff[LINE_LEN];
186 if (p->TermMode == NULL) {
187 n = read(p->fd_in, linebuff, sizeof linebuff - 1);
189 if (linebuff[n-1] == '\n')
190 linebuff[--n] = '\0';
193 p->nonewline = 1; /* Maybe command_Decode does a prompt */
196 if ((op = log_PromptContext) == NULL)
197 log_PromptContext = p;
198 if (!command_Decode(bundle, linebuff, n, p, p->src.from))
199 prompt_Printf(p, "Syntax error\n");
200 log_PromptContext = op;
203 log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
206 prompt_Destroy(p, 0);
211 switch (p->TermMode->state) {
212 case DATALINK_CLOSED:
213 prompt_Printf(p, "Link lost, terminal mode.\n");
214 prompt_TtyCommandMode(p);
223 prompt_Printf(p, "\nPacket mode detected.\n");
224 prompt_TtyCommandMode(p);
226 /* We'll get a prompt because of our status change */
230 /* Wait 'till we're in a state we care about */
235 * We are in terminal mode, decode special sequences
237 n = read(p->fd_in, &ch, 1);
238 log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
241 switch (p->readtilde) {
246 if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
247 log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
248 prompt_TtyCommandMode(p);
257 datalink_Up(p->TermMode, 0, 1);
258 prompt_Printf(p, "\nPacket mode.\n");
259 prompt_TtyCommandMode(p);
262 prompt_TtyCommandMode(p);
279 arg.cx = p->TermMode;
286 if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
287 log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
288 prompt_TtyCommandMode(p);
299 prompt_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
301 /* We never want to write here ! */
302 log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
307 prompt_Create(struct server *s, struct bundle *bundle, int fd)
309 struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
312 p->desc.type = PROMPT_DESCRIPTOR;
313 p->desc.UpdateSet = prompt_UpdateSet;
314 p->desc.IsSet = prompt_IsSet;
315 p->desc.Read = prompt_Read;
316 p->desc.Write = prompt_Write;
318 if (fd == PROMPT_STD) {
319 char *tty = ttyname(STDIN_FILENO);
325 p->fd_in = STDIN_FILENO;
326 p->fd_out = STDOUT_FILENO;
329 p->auth = LOCAL_AUTH;
330 p->src.type = "Controller";
331 strncpy(p->src.from, tty, sizeof p->src.from - 1);
332 p->src.from[sizeof p->src.from - 1] = '\0';
333 tcgetattr(p->fd_in, &p->oldtio); /* Save original tty mode */
335 p->fd_in = p->fd_out = fd;
336 p->Term = fdopen(fd, "a+");
338 p->auth = *s->cfg.passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
339 p->src.type = "unknown";
347 log_RegisterPrompt(p);
354 prompt_Destroy(struct prompt *p, int verbose)
357 if (p->Term != stdout) {
360 if (p->fd_out != p->fd_in)
363 log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
365 prompt_TtyOldMode(p);
367 log_UnRegisterPrompt(p);
373 prompt_Printf(struct prompt *p, const char *fmt,...)
375 if (p && p->active) {
379 prompt_vPrintf(p, fmt, ap);
385 prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
387 if (p && p->active) {
392 /* Stuff '\r' in front of '\n' 'cos we're in raw mode */
393 int len = strlen(fmt);
395 if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
396 (len == 1 || fmt[len-2] != '\r')) {
398 strcpy(nfmt + len - 1, "\r\n");
404 vfprintf(p->Term, pfmt, ap);
411 prompt_TtyInit(struct prompt *p)
413 int stat, fd = p ? p->fd_in : STDIN_FILENO;
414 struct termios newtio;
416 stat = fcntl(fd, F_GETFL, 0);
419 fcntl(fd, F_SETFL, stat);
425 tcgetattr(fd, &newtio);
427 newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
429 newtio.c_oflag &= ~OPOST;
431 newtio.c_cc[VINTR] = _POSIX_VDISABLE;
432 newtio.c_cc[VMIN] = 1;
433 newtio.c_cc[VTIME] = 0;
434 newtio.c_cflag |= CS8;
435 tcsetattr(fd, TCSANOW, &newtio);
441 * Set tty into command mode. We allow canonical input and echo processing.
444 prompt_TtyCommandMode(struct prompt *p)
446 struct termios newtio;
449 tcgetattr(p->fd_in, &newtio);
450 newtio.c_lflag |= (ECHO | ISIG | ICANON);
451 newtio.c_iflag = p->oldtio.c_iflag;
452 newtio.c_oflag |= OPOST;
453 tcsetattr(p->fd_in, TCSADRAIN, &newtio);
455 stat = fcntl(p->fd_in, F_GETFL, 0);
458 fcntl(p->fd_in, F_SETFL, stat);
465 * Set tty into terminal mode which is used while we invoke term command.
468 prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
472 if (p->Term == stdout)
473 tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
475 stat = fcntl(p->fd_in, F_GETFL, 0);
478 fcntl(p->fd_in, F_SETFL, stat);
484 prompt_TtyOldMode(struct prompt *p)
488 stat = fcntl(p->fd_in, F_GETFL, 0);
491 fcntl(p->fd_in, F_SETFL, stat);
494 if (p->Term == stdout)
495 tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
499 prompt_pgrp(struct prompt *p)
501 return tcgetpgrp(p->fd_in);
505 PasswdCommand(struct cmdargs const *arg)
510 log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
514 if (arg->prompt->owner == NULL) {
515 log_Printf(LogWARN, "passwd: Not required\n");
519 if (arg->argc == arg->argn)
521 else if (arg->argc > arg->argn+1)
524 pass = arg->argv[arg->argn];
526 if (!strcmp(arg->prompt->owner->cfg.passwd, pass))
527 arg->prompt->auth = LOCAL_AUTH;
529 arg->prompt->auth = LOCAL_NO_AUTH;
534 static struct pppTimer bgtimer;
537 prompt_TimedContinue(void *v)
539 prompt_Continue((struct prompt *)v);
543 prompt_Continue(struct prompt *p)
545 timer_Stop(&bgtimer);
546 if (getpgrp() == prompt_pgrp(p)) {
547 prompt_TtyCommandMode(p);
550 log_ActivatePrompt(p);
551 } else if (!p->owner) {
552 bgtimer.func = prompt_TimedContinue;
553 bgtimer.name = "prompt bg";
554 bgtimer.load = SECTICKS;
556 timer_Start(&bgtimer);
561 prompt_Suspend(struct prompt *p)
563 if (getpgrp() == prompt_pgrp(p)) {
564 prompt_TtyOldMode(p);
565 log_DeactivatePrompt(p);