xref: /freebsd/usr.sbin/ppp/prompt.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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.14 1999/04/03 11:54:00 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 "layer.h"
45 #include "defs.h"
46 #include "timer.h"
47 #include "command.h"
48 #include "log.h"
49 #include "descriptor.h"
50 #include "prompt.h"
51 #include "fsm.h"
52 #include "lcp.h"
53 #include "auth.h"
54 #include "iplist.h"
55 #include "throughput.h"
56 #include "slcompress.h"
57 #include "mbuf.h"
58 #include "lqr.h"
59 #include "hdlc.h"
60 #include "ipcp.h"
61 #include "filter.h"
62 #include "async.h"
63 #include "ccp.h"
64 #include "link.h"
65 #include "physical.h"
66 #include "mp.h"
67 #ifndef NORADIUS
68 #include "radius.h"
69 #endif
70 #include "bundle.h"
71 #include "chat.h"
72 #include "chap.h"
73 #include "cbcp.h"
74 #include "datalink.h"
75 #include "server.h"
76 #include "main.h"
77 
78 static void
79 prompt_Display(struct prompt *p)
80 {
81   /* XXX: See Index2Nam() - should we only figure this out once ? */
82   static char shostname[MAXHOSTNAMELEN];
83   const char *pconnect, *pauth;
84 
85   if (p->TermMode || !p->needprompt)
86     return;
87 
88   p->needprompt = 0;
89 
90   if (p->nonewline)
91     p->nonewline = 0;
92   else
93     fprintf(p->Term, "\n");
94 
95   if (p->auth == LOCAL_AUTH)
96     pauth = " ON ";
97   else
98     pauth = " on ";
99 
100   if (p->bundle->ncp.ipcp.fsm.state == ST_OPENED)
101     pconnect = "PPP";
102   else if (bundle_Phase(p->bundle) == PHASE_NETWORK)
103     pconnect = "PPp";
104   else if (bundle_Phase(p->bundle) == PHASE_AUTHENTICATE)
105     pconnect = "Ppp";
106   else
107     pconnect = "ppp";
108 
109   if (*shostname == '\0') {
110     char *dot;
111 
112     if (gethostname(shostname, sizeof shostname))
113       strcpy(shostname, "localhost");
114     else if ((dot = strchr(shostname, '.')))
115       *dot = '\0';
116   }
117 
118   fprintf(p->Term, "%s%s%s> ", pconnect, pauth, shostname);
119   fflush(p->Term);
120 }
121 
122 static int
123 prompt_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
124 {
125   struct prompt *p = descriptor2prompt(d);
126   int sets;
127 
128   sets = 0;
129 
130   if (!p->active)
131     return sets;
132 
133   if (p->fd_in >= 0) {
134     if (r) {
135       FD_SET(p->fd_in, r);
136       log_Printf(LogTIMER, "prompt %s: fdset(r) %d\n", p->src.from, p->fd_in);
137       sets++;
138     }
139     if (e) {
140       FD_SET(p->fd_in, e);
141       log_Printf(LogTIMER, "prompt %s: fdset(e) %d\n", p->src.from, p->fd_in);
142       sets++;
143     }
144     if (sets && *n < p->fd_in + 1)
145       *n = p->fd_in + 1;
146   }
147 
148   prompt_Display(p);
149 
150   return sets;
151 }
152 
153 static int
154 prompt_IsSet(struct descriptor *d, const fd_set *fdset)
155 {
156   struct prompt *p = descriptor2prompt(d);
157   return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
158 }
159 
160 
161 static void
162 prompt_ShowHelp(struct prompt *p)
163 {
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");
170 }
171 
172 static void
173 prompt_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
174 {
175   struct prompt *p = descriptor2prompt(d);
176   int n;
177   char ch;
178   char linebuff[LINE_LEN];
179 
180   if (p->TermMode == NULL) {
181     n = read(p->fd_in, linebuff, sizeof linebuff - 1);
182     if (n > 0) {
183       if (linebuff[n-1] == '\n')
184         linebuff[--n] = '\0';
185       else
186         linebuff[n] = '\0';
187       p->nonewline = 1;		/* Maybe command_Decode does a prompt */
188       prompt_Required(p);
189       if (n)
190         command_Decode(bundle, linebuff, n, p, p->src.from);
191     } else if (n <= 0) {
192       log_Printf(LogPHASE, "%s: Client connection closed.\n", p->src.from);
193       if (!p->owner)
194         Cleanup(EX_NORMAL);
195       prompt_Destroy(p, 0);
196     }
197     return;
198   }
199 
200   switch (p->TermMode->state) {
201     case DATALINK_CLOSED:
202       prompt_Printf(p, "Link lost, terminal mode.\n");
203       prompt_TtyCommandMode(p);
204       p->nonewline = 0;
205       prompt_Required(p);
206       return;
207 
208     case DATALINK_READY:
209       break;
210 
211     case DATALINK_OPEN:
212       prompt_Printf(p, "\nPacket mode detected.\n");
213       prompt_TtyCommandMode(p);
214       p->nonewline = 0;
215       /* We'll get a prompt because of our status change */
216       /* Fall through */
217 
218     default:
219       /* Wait 'till we're in a state we care about */
220       return;
221   }
222 
223   /*
224    * We are in terminal mode, decode special sequences
225    */
226   n = read(p->fd_in, &ch, 1);
227   log_Printf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
228 
229   if (n > 0) {
230     switch (p->readtilde) {
231     case 0:
232       if (ch == '~')
233         p->readtilde = 1;
234       else
235 	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
236 	  log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
237           prompt_TtyCommandMode(p);
238         }
239       break;
240     case 1:
241       switch (ch) {
242       case '?':
243 	prompt_ShowHelp(p);
244 	break;
245       case 'p':
246         datalink_Up(p->TermMode, 0, 1);
247         prompt_Printf(p, "\nPacket mode.\n");
248 	prompt_TtyCommandMode(p);
249         break;
250       case '.':
251 	prompt_TtyCommandMode(p);
252         p->nonewline = 0;
253         prompt_Required(p);
254 	break;
255       case 't':
256 	timer_Show(0, p);
257 	break;
258       case 'm':
259         {
260           struct cmdargs arg;
261 
262           arg.cmdtab = NULL;
263           arg.cmd = NULL;
264           arg.argc = 0;
265           arg.argn = 0;
266           arg.argv = NULL;
267           arg.bundle = bundle;
268           arg.cx = p->TermMode;
269           arg.prompt = p;
270 
271 	  mbuf_Show(&arg);
272         }
273 	break;
274       default:
275 	if (physical_Write(p->TermMode->physical, &ch, n) < 0) {
276 	  log_Printf(LogWARN, "error writing to modem: %s\n", strerror(errno));
277           prompt_TtyCommandMode(p);
278         }
279 	break;
280       }
281       p->readtilde = 0;
282       break;
283     }
284   }
285 }
286 
287 static int
288 prompt_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
289 {
290   /* We never want to write here ! */
291   log_Printf(LogALERT, "prompt_Write: Internal error: Bad call !\n");
292   return 0;
293 }
294 
295 struct prompt *
296 prompt_Create(struct server *s, struct bundle *bundle, int fd)
297 {
298   struct prompt *p = (struct prompt *)malloc(sizeof(struct prompt));
299 
300   if (p != NULL) {
301     p->desc.type = PROMPT_DESCRIPTOR;
302     p->desc.UpdateSet = prompt_UpdateSet;
303     p->desc.IsSet = prompt_IsSet;
304     p->desc.Read = prompt_Read;
305     p->desc.Write = prompt_Write;
306 
307     if (fd == PROMPT_STD) {
308       char *tty = ttyname(STDIN_FILENO);
309 
310       if (!tty) {
311         free(p);
312         return NULL;
313       }
314       p->fd_in = STDIN_FILENO;
315       p->fd_out = STDOUT_FILENO;
316       p->Term = stdout;
317       p->owner = NULL;
318       p->auth = LOCAL_AUTH;
319       p->src.type = "Controller";
320       strncpy(p->src.from, tty, sizeof p->src.from - 1);
321       p->src.from[sizeof p->src.from - 1] = '\0';
322       tcgetattr(p->fd_in, &p->oldtio);	/* Save original tty mode */
323     } else {
324       p->fd_in = p->fd_out = fd;
325       p->Term = fdopen(fd, "a+");
326       p->owner = s;
327       p->auth = *s->passwd ? LOCAL_NO_AUTH : LOCAL_AUTH;
328       p->src.type = "unknown";
329       *p->src.from = '\0';
330     }
331     p->TermMode = NULL;
332     p->nonewline = 1;
333     p->needprompt = 1;
334     p->readtilde = 0;
335     p->bundle = bundle;
336     log_RegisterPrompt(p);
337   }
338 
339   return p;
340 }
341 
342 void
343 prompt_Destroy(struct prompt *p, int verbose)
344 {
345   if (p) {
346     if (p->Term != stdout) {
347       fclose(p->Term);
348       close(p->fd_in);
349       if (p->fd_out != p->fd_in)
350         close(p->fd_out);
351       if (verbose)
352         log_Printf(LogPHASE, "%s: Client connection dropped.\n", p->src.from);
353     } else
354       prompt_TtyOldMode(p);
355 
356     log_UnRegisterPrompt(p);
357     free(p);
358   }
359 }
360 
361 void
362 prompt_Printf(struct prompt *p, const char *fmt,...)
363 {
364   if (p && p->active) {
365     va_list ap;
366 
367     va_start(ap, fmt);
368     prompt_vPrintf(p, fmt, ap);
369     va_end(ap);
370   }
371 }
372 
373 void
374 prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
375 {
376   if (p && p->active) {
377     char nfmt[LINE_LEN];
378     const char *pfmt;
379 
380     if (p->TermMode) {
381       /* Stuff '\r' in front of '\n' 'cos we're in raw mode */
382       int len = strlen(fmt);
383 
384       if (len && len < sizeof nfmt - 1 && fmt[len-1] == '\n' &&
385           (len == 1 || fmt[len-2] != '\r')) {
386         strcpy(nfmt, fmt);
387         strcpy(nfmt + len - 1, "\r\n");
388         pfmt = nfmt;
389       } else
390         pfmt = fmt;
391     } else
392       pfmt = fmt;
393     vfprintf(p->Term, pfmt, ap);
394     fflush(p->Term);
395     p->nonewline = 1;
396   }
397 }
398 
399 void
400 prompt_TtyInit(struct prompt *p)
401 {
402   int stat, fd = p ? p->fd_in : STDIN_FILENO;
403   struct termios newtio;
404 
405   stat = fcntl(fd, F_GETFL, 0);
406   if (stat > 0) {
407     stat |= O_NONBLOCK;
408     fcntl(fd, F_SETFL, stat);
409   }
410 
411   if (p)
412     newtio = p->oldtio;
413   else
414     tcgetattr(fd, &newtio);
415 
416   newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
417   newtio.c_iflag = 0;
418   newtio.c_oflag &= ~OPOST;
419   if (!p)
420     newtio.c_cc[VINTR] = _POSIX_VDISABLE;
421   newtio.c_cc[VMIN] = 1;
422   newtio.c_cc[VTIME] = 0;
423   newtio.c_cflag |= CS8;
424   tcsetattr(fd, TCSANOW, &newtio);
425   if (p)
426     p->comtio = newtio;
427 }
428 
429 /*
430  *  Set tty into command mode. We allow canonical input and echo processing.
431  */
432 void
433 prompt_TtyCommandMode(struct prompt *p)
434 {
435   struct termios newtio;
436   int stat;
437 
438   tcgetattr(p->fd_in, &newtio);
439   newtio.c_lflag |= (ECHO | ISIG | ICANON);
440   newtio.c_iflag = p->oldtio.c_iflag;
441   newtio.c_oflag |= OPOST;
442   tcsetattr(p->fd_in, TCSADRAIN, &newtio);
443 
444   stat = fcntl(p->fd_in, F_GETFL, 0);
445   if (stat > 0) {
446     stat |= O_NONBLOCK;
447     fcntl(p->fd_in, F_SETFL, stat);
448   }
449 
450   p->TermMode = NULL;
451 }
452 
453 /*
454  * Set tty into terminal mode which is used while we invoke term command.
455  */
456 void
457 prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
458 {
459   int stat;
460 
461   if (p->Term == stdout)
462     tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
463 
464   stat = fcntl(p->fd_in, F_GETFL, 0);
465   if (stat > 0) {
466     stat &= ~O_NONBLOCK;
467     fcntl(p->fd_in, F_SETFL, stat);
468   }
469   p->TermMode = dl;
470 }
471 
472 void
473 prompt_TtyOldMode(struct prompt *p)
474 {
475   int stat;
476 
477   stat = fcntl(p->fd_in, F_GETFL, 0);
478   if (stat > 0) {
479     stat &= ~O_NONBLOCK;
480     fcntl(p->fd_in, F_SETFL, stat);
481   }
482 
483   if (p->Term == stdout)
484     tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
485 }
486 
487 pid_t
488 prompt_pgrp(struct prompt *p)
489 {
490   return tcgetpgrp(p->fd_in);
491 }
492 
493 int
494 PasswdCommand(struct cmdargs const *arg)
495 {
496   const char *pass;
497 
498   if (!arg->prompt) {
499     log_Printf(LogWARN, "passwd: Cannot specify without a prompt\n");
500     return 0;
501   }
502 
503   if (arg->prompt->owner == NULL) {
504     log_Printf(LogWARN, "passwd: Not required\n");
505     return 0;
506   }
507 
508   if (arg->argc == arg->argn)
509     pass = "";
510   else if (arg->argc > arg->argn+1)
511     return -1;
512   else
513     pass = arg->argv[arg->argn];
514 
515   if (!strcmp(arg->prompt->owner->passwd, pass))
516     arg->prompt->auth = LOCAL_AUTH;
517   else
518     arg->prompt->auth = LOCAL_NO_AUTH;
519 
520   return 0;
521 }
522 
523 static struct pppTimer bgtimer;
524 
525 static void
526 prompt_TimedContinue(void *v)
527 {
528   prompt_Continue((struct prompt *)v);
529 }
530 
531 void
532 prompt_Continue(struct prompt *p)
533 {
534   timer_Stop(&bgtimer);
535   if (getpgrp() == prompt_pgrp(p)) {
536     prompt_TtyCommandMode(p);
537     p->nonewline = 1;
538     prompt_Required(p);
539     log_ActivatePrompt(p);
540   } else if (!p->owner) {
541     bgtimer.func = prompt_TimedContinue;
542     bgtimer.name = "prompt bg";
543     bgtimer.load = SECTICKS;
544     bgtimer.arg = p;
545     timer_Start(&bgtimer);
546   }
547 }
548 
549 void
550 prompt_Suspend(struct prompt *p)
551 {
552   if (getpgrp() == prompt_pgrp(p)) {
553     prompt_TtyOldMode(p);
554     log_DeactivatePrompt(p);
555   }
556 }
557