2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <sys/socket.h>
43 #include <sys/fcntl.h>
52 #include "descriptor.h"
57 #include "throughput.h"
58 #include "slcompress.h"
85 prompt_Display(struct prompt *p)
87 /* XXX: See Index2Nam() - should we only figure this out once ? */
88 static char shostname[MAXHOSTNAMELEN];
89 const char *pconnect, *pauth;
91 if (p->TermMode || !p->needprompt)
99 fprintf(p->Term, "\n");
101 if (p->auth == LOCAL_AUTH)
106 if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
109 else if (!Enabled(p->bundle, OPT_IPCP) &&
110 p->bundle->ncp.ipv6cp.fsm.state == ST_OPENED)
113 else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
115 else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
120 if (*shostname == '\0') {
123 if (gethostname(shostname, sizeof shostname) || *shostname == '\0')
124 strcpy(shostname, "localhost");
125 else if ((dot = strchr(shostname, '.')))
129 fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
134 prompt_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w __unused,
137 struct prompt *p = descriptor2prompt(d);
148 log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
153 log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
156 if (sets && *n < p->fd_in + 1)
166 prompt_IsSet(struct fdescriptor *d, const fd_set *fdset)
168 struct prompt *p = descriptor2prompt(d);
169 return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
174 prompt_ShowHelp(struct prompt *p)
176 prompt_Printf(p, "The following commands are available:\n");
177 prompt_Printf(p, " ~p\tEnter Packet mode\n");
178 prompt_Printf(p, " ~t\tShow timers\n");
179 prompt_Printf(p, " ~m\tShow memory map\n");
180 prompt_Printf(p, " ~.\tTerminate program\n");
181 prompt_Printf(p, " ~?\tThis help\n");
185 prompt_Read(struct fdescriptor *d, struct bundle *bundle,
186 const fd_set *fdset __unused)
188 struct prompt *p = descriptor2prompt(d);
192 char linebuff[LINE_LEN];
194 if (p->TermMode == NULL) {
195 n = read(p->fd_in, linebuff, sizeof linebuff - 1);
197 if (linebuff[n-1] == '\n')
198 linebuff[--n] = '\0';
201 p->nonewline = 1; /* Maybe command_Decode does a prompt */
204 if ((op = log_PromptContext) == NULL)
205 log_PromptContext = p;
206 if (!command_Decode(bundle, linebuff, n, p, p->src.from))
207 prompt_Printf(p, "Syntax error\n");
208 log_PromptContext = op;
211 log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
214 prompt_Destroy(p, 0);
219 switch (p->TermMode->state) {
220 case DATALINK_CLOSED:
221 prompt_Printf(p, "Link lost, terminal mode.\n");
222 prompt_TtyCommandMode(p);
231 prompt_Printf(p, "\nPacket mode detected.\n");
232 prompt_TtyCommandMode(p);
234 /* We'll get a prompt because of our status change */
238 /* Wait 'till we're in a state we care about */
243 * We are in terminal mode, decode special sequences
245 n = read(p->fd_in, &ch, 1);
246 log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
249 switch (p->readtilde) {
254 if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
255 log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
256 prompt_TtyCommandMode(p);
265 datalink_Up(p->TermMode, 0, 1);
266 prompt_Printf(p, "\nPacket mode.\n");
267 prompt_TtyCommandMode(p);
270 prompt_TtyCommandMode(p);
287 arg.cx = p->TermMode;
294 if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
295 log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
296 prompt_TtyCommandMode(p);
307 prompt_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused,
308 const fd_set *fdset __unused)
310 /* We never want to write here ! */
311 log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
316 prompt_Create(struct server *s, struct bundle *bundle, int fd)
318 struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
321 p->desc.type = PROMPT_DESCRIPTOR;
322 p->desc.UpdateSet = prompt_UpdateSet;
323 p->desc.IsSet = prompt_IsSet;
324 p->desc.Read = prompt_Read;
325 p->desc.Write = prompt_Write;
327 if (fd == PROMPT_STD) {
328 char *tty = ttyname(STDIN_FILENO);
334 p->fd_in = STDIN_FILENO;
335 p->fd_out = STDOUT_FILENO;
338 p->auth = LOCAL_AUTH;
339 p->src.type = "Controller";
340 strncpy(p->src.from, tty, sizeof p->src.from - 1);
341 p->src.from[sizeof p->src.from - 1] = '\0';
342 tcgetattr(p->fd_in, &p->oldtio); /* Save original tty mode */
344 p->fd_in = p->fd_out = fd;
345 p->Term = fdopen(fd, "a+");
347 p->auth = *s->cfg.passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
348 p->src.type = "unknown";
356 log_RegisterPrompt(p);
363 prompt_Destroy(struct prompt *p, int verbose)
366 if (p->Term != stdout) {
369 if (p->fd_out != p->fd_in)
372 log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
374 prompt_TtyOldMode(p);
376 log_UnRegisterPrompt(p);
382 prompt_Printf(struct prompt *p, const char *fmt,...)
384 if (p && p->active) {
388 prompt_vPrintf(p, fmt, ap);
394 prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
396 if (p && p->active) {
401 /* Stuff '\r' in front of '\n' 'cos we're in raw mode */
402 size_t len = strlen(fmt);
404 if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
405 (len == 1 || fmt[len-2] != '\r')) {
407 strcpy(nfmt + len - 1, "\r\n");
413 vfprintf(p->Term, pfmt, ap);
420 prompt_TtyInit(struct prompt *p)
422 int stat, fd = p ? p->fd_in : STDIN_FILENO;
423 struct termios newtio;
425 stat = fcntl(fd, F_GETFL, 0);
428 fcntl(fd, F_SETFL, stat);
434 tcgetattr(fd, &newtio);
436 newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
438 newtio.c_oflag &= ~OPOST;
440 newtio.c_cc[VINTR] = _POSIX_VDISABLE;
441 newtio.c_cc[VMIN] = 1;
442 newtio.c_cc[VTIME] = 0;
443 newtio.c_cflag |= CS8;
444 tcsetattr(fd, TCSANOW, &newtio);
450 * Set tty into command mode. We allow canonical input and echo processing.
453 prompt_TtyCommandMode(struct prompt *p)
455 struct termios newtio;
458 tcgetattr(p->fd_in, &newtio);
459 newtio.c_lflag |= (ECHO | ISIG | ICANON);
460 newtio.c_iflag = p->oldtio.c_iflag;
461 newtio.c_oflag |= OPOST;
462 tcsetattr(p->fd_in, TCSADRAIN, &newtio);
464 stat = fcntl(p->fd_in, F_GETFL, 0);
467 fcntl(p->fd_in, F_SETFL, stat);
474 * Set tty into terminal mode which is used while we invoke term command.
477 prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
481 if (p->Term == stdout)
482 tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
484 stat = fcntl(p->fd_in, F_GETFL, 0);
487 fcntl(p->fd_in, F_SETFL, stat);
493 prompt_TtyOldMode(struct prompt *p)
497 stat = fcntl(p->fd_in, F_GETFL, 0);
500 fcntl(p->fd_in, F_SETFL, stat);
503 if (p->Term == stdout)
504 tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
508 prompt_pgrp(struct prompt *p)
510 return tcgetpgrp(p->fd_in);
514 PasswdCommand(struct cmdargs const *arg)
519 log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
523 if (arg->prompt->owner == NULL) {
524 log_Printf(LogWARN, "passwd: Not required\n");
528 if (arg->argc == arg->argn)
530 else if (arg->argc > arg->argn+1)
533 pass = arg->argv[arg->argn];
535 if (!strcmp(arg->prompt->owner->cfg.passwd, pass))
536 arg->prompt->auth = LOCAL_AUTH;
538 arg->prompt->auth = LOCAL_NO_AUTH;
543 static struct pppTimer bgtimer;
546 prompt_TimedContinue(void *v)
548 prompt_Continue((struct prompt *)v);
552 prompt_Continue(struct prompt *p)
554 timer_Stop(&bgtimer);
555 if (getpgrp() == prompt_pgrp(p)) {
556 prompt_TtyCommandMode(p);
559 log_ActivatePrompt(p);
560 } else if (!p->owner) {
561 bgtimer.func = prompt_TimedContinue;
562 bgtimer.name = "prompt bg";
563 bgtimer.load = SECTICKS;
565 timer_Start(&bgtimer);
570 prompt_Suspend(struct prompt *p)
572 if (getpgrp() == prompt_pgrp(p)) {
573 prompt_TtyOldMode(p);
574 log_DeactivatePrompt(p);