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
26 * $FreeBSD: src/usr.sbin/ppp/prompt.c,v 1.20.2.5 2003/04/05 10:51:39 ume Exp $
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"
83 prompt_Display(struct prompt *p)
85 /* XXX: See Index2Nam() - should we only figure this out once ? */
86 static char shostname[MAXHOSTNAMELEN];
87 const char *pconnect, *pauth;
89 if (p->TermMode || !p->needprompt)
97 fprintf(p->Term, "\n");
99 if (p->auth == LOCAL_AUTH)
104 if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
107 else if (!Enabled(p->bundle, OPT_IPCP) &&
108 p->bundle->ncp.ipv6cp.fsm.state == ST_OPENED)
111 else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
113 else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
118 if (*shostname == '\0') {
121 if (gethostname(shostname, sizeof shostname) || *shostname == '\0')
122 strcpy(shostname, "localhost");
123 else if ((dot = strchr(shostname, '.')))
127 fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
132 prompt_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
134 struct prompt *p = descriptor2prompt(d);
145 log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
150 log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
153 if (sets && *n < p->fd_in + 1)
163 prompt_IsSet(struct fdescriptor *d, const fd_set *fdset)
165 struct prompt *p = descriptor2prompt(d);
166 return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
171 prompt_ShowHelp(struct prompt *p)
173 prompt_Printf(p, "The following commands are available:\n");
174 prompt_Printf(p, " ~p\tEnter Packet mode\n");
175 prompt_Printf(p, " ~t\tShow timers\n");
176 prompt_Printf(p, " ~m\tShow memory map\n");
177 prompt_Printf(p, " ~.\tTerminate program\n");
178 prompt_Printf(p, " ~?\tThis help\n");
182 prompt_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
184 struct prompt *p = descriptor2prompt(d);
188 char linebuff[LINE_LEN];
190 if (p->TermMode == NULL) {
191 n = read(p->fd_in, linebuff, sizeof linebuff - 1);
193 if (linebuff[n-1] == '\n')
194 linebuff[--n] = '\0';
197 p->nonewline = 1; /* Maybe command_Decode does a prompt */
200 if ((op = log_PromptContext) == NULL)
201 log_PromptContext = p;
202 if (!command_Decode(bundle, linebuff, n, p, p->src.from))
203 prompt_Printf(p, "Syntax error\n");
204 log_PromptContext = op;
207 log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
210 prompt_Destroy(p, 0);
215 switch (p->TermMode->state) {
216 case DATALINK_CLOSED:
217 prompt_Printf(p, "Link lost, terminal mode.\n");
218 prompt_TtyCommandMode(p);
227 prompt_Printf(p, "\nPacket mode detected.\n");
228 prompt_TtyCommandMode(p);
230 /* We'll get a prompt because of our status change */
234 /* Wait 'till we're in a state we care about */
239 * We are in terminal mode, decode special sequences
241 n = read(p->fd_in, &ch, 1);
242 log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
245 switch (p->readtilde) {
250 if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
251 log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
252 prompt_TtyCommandMode(p);
261 datalink_Up(p->TermMode, 0, 1);
262 prompt_Printf(p, "\nPacket mode.\n");
263 prompt_TtyCommandMode(p);
266 prompt_TtyCommandMode(p);
283 arg.cx = p->TermMode;
290 if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
291 log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
292 prompt_TtyCommandMode(p);
303 prompt_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
305 /* We never want to write here ! */
306 log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
311 prompt_Create(struct server *s, struct bundle *bundle, int fd)
313 struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
316 p->desc.type = PROMPT_DESCRIPTOR;
317 p->desc.UpdateSet = prompt_UpdateSet;
318 p->desc.IsSet = prompt_IsSet;
319 p->desc.Read = prompt_Read;
320 p->desc.Write = prompt_Write;
322 if (fd == PROMPT_STD) {
323 char *tty = ttyname(STDIN_FILENO);
329 p->fd_in = STDIN_FILENO;
330 p->fd_out = STDOUT_FILENO;
333 p->auth = LOCAL_AUTH;
334 p->src.type = "Controller";
335 strncpy(p->src.from, tty, sizeof p->src.from - 1);
336 p->src.from[sizeof p->src.from - 1] = '\0';
337 tcgetattr(p->fd_in, &p->oldtio); /* Save original tty mode */
339 p->fd_in = p->fd_out = fd;
340 p->Term = fdopen(fd, "a+");
342 p->auth = *s->cfg.passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
343 p->src.type = "unknown";
351 log_RegisterPrompt(p);
358 prompt_Destroy(struct prompt *p, int verbose)
361 if (p->Term != stdout) {
364 if (p->fd_out != p->fd_in)
367 log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
369 prompt_TtyOldMode(p);
371 log_UnRegisterPrompt(p);
377 prompt_Printf(struct prompt *p, const char *fmt,...)
379 if (p && p->active) {
383 prompt_vPrintf(p, fmt, ap);
389 prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
391 if (p && p->active) {
396 /* Stuff '\r' in front of '\n' 'cos we're in raw mode */
397 int len = strlen(fmt);
399 if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
400 (len == 1 || fmt[len-2] != '\r')) {
402 strcpy(nfmt + len - 1, "\r\n");
408 vfprintf(p->Term, pfmt, ap);
415 prompt_TtyInit(struct prompt *p)
417 int stat, fd = p ? p->fd_in : STDIN_FILENO;
418 struct termios newtio;
420 stat = fcntl(fd, F_GETFL, 0);
423 fcntl(fd, F_SETFL, stat);
429 tcgetattr(fd, &newtio);
431 newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
433 newtio.c_oflag &= ~OPOST;
435 newtio.c_cc[VINTR] = _POSIX_VDISABLE;
436 newtio.c_cc[VMIN] = 1;
437 newtio.c_cc[VTIME] = 0;
438 newtio.c_cflag |= CS8;
439 tcsetattr(fd, TCSANOW, &newtio);
445 * Set tty into command mode. We allow canonical input and echo processing.
448 prompt_TtyCommandMode(struct prompt *p)
450 struct termios newtio;
453 tcgetattr(p->fd_in, &newtio);
454 newtio.c_lflag |= (ECHO | ISIG | ICANON);
455 newtio.c_iflag = p->oldtio.c_iflag;
456 newtio.c_oflag |= OPOST;
457 tcsetattr(p->fd_in, TCSADRAIN, &newtio);
459 stat = fcntl(p->fd_in, F_GETFL, 0);
462 fcntl(p->fd_in, F_SETFL, stat);
469 * Set tty into terminal mode which is used while we invoke term command.
472 prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
476 if (p->Term == stdout)
477 tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
479 stat = fcntl(p->fd_in, F_GETFL, 0);
482 fcntl(p->fd_in, F_SETFL, stat);
488 prompt_TtyOldMode(struct prompt *p)
492 stat = fcntl(p->fd_in, F_GETFL, 0);
495 fcntl(p->fd_in, F_SETFL, stat);
498 if (p->Term == stdout)
499 tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
503 prompt_pgrp(struct prompt *p)
505 return tcgetpgrp(p->fd_in);
509 PasswdCommand(struct cmdargs const *arg)
514 log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
518 if (arg->prompt->owner == NULL) {
519 log_Printf(LogWARN, "passwd: Not required\n");
523 if (arg->argc == arg->argn)
525 else if (arg->argc > arg->argn+1)
528 pass = arg->argv[arg->argn];
530 if (!strcmp(arg->prompt->owner->cfg.passwd, pass))
531 arg->prompt->auth = LOCAL_AUTH;
533 arg->prompt->auth = LOCAL_NO_AUTH;
538 static struct pppTimer bgtimer;
541 prompt_TimedContinue(void *v)
543 prompt_Continue((struct prompt *)v);
547 prompt_Continue(struct prompt *p)
549 timer_Stop(&bgtimer);
550 if (getpgrp() == prompt_pgrp(p)) {
551 prompt_TtyCommandMode(p);
554 log_ActivatePrompt(p);
555 } else if (!p->owner) {
556 bgtimer.func = prompt_TimedContinue;
557 bgtimer.name = "prompt bg";
558 bgtimer.load = SECTICKS;
560 timer_Start(&bgtimer);
565 prompt_Suspend(struct prompt *p)
567 if (getpgrp() == prompt_pgrp(p)) {
568 prompt_TtyOldMode(p);
569 log_DeactivatePrompt(p);